Automatic merge of /spare/repo/linux-2.6/.git branch HEAD
[linux-2.6] / drivers / net / bonding / bond_3ad.c
1 /*
2  * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59
16  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  *
22  * 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 ": An illegal loopback occurred on adapter (%s)\n",
1202                                                 port->slave->dev->name);
1203                                 printk(KERN_ERR "Check the configuration to verify that all Adapters "
1204                                                 "are connected to 802.3ad compliant switch ports\n");
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 ": Warning: Port %d (on %s) was "
1382                                "related to aggregator %d but was not on its port list\n",
1383                                port->actor_port_number, port->slave->dev->name,
1384                                port->aggregator->aggregator_identifier);
1385                 }
1386         }
1387         // search on all aggregators for a suitable aggregator for this port
1388         for (aggregator = __get_first_agg(port); aggregator;
1389              aggregator = __get_next_agg(aggregator)) {
1390
1391                 // keep a free aggregator for later use(if needed)
1392                 if (!aggregator->lag_ports) {
1393                         if (!free_aggregator) {
1394                                 free_aggregator=aggregator;
1395                         }
1396                         continue;
1397                 }
1398                 // check if current aggregator suits us
1399                 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && // if all parameters match AND
1400                      !MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(port->partner_oper_system)) &&
1401                      (aggregator->partner_system_priority == port->partner_oper_system_priority) &&
1402                      (aggregator->partner_oper_aggregator_key == port->partner_oper_key)
1403                     ) &&
1404                     ((MAC_ADDRESS_COMPARE(&(port->partner_oper_system), &(null_mac_addr)) && // partner answers
1405                       !aggregator->is_individual)  // but is not individual OR
1406                     )
1407                    ) {
1408                         // attach to the founded aggregator
1409                         port->aggregator = aggregator;
1410                         port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier;
1411                         port->next_port_in_aggregator=aggregator->lag_ports;
1412                         port->aggregator->num_of_ports++;
1413                         aggregator->lag_ports=port;
1414                         dprintk("Port %d joined LAG %d(existing LAG)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1415
1416                         // mark this port as selected
1417                         port->sm_vars |= AD_PORT_SELECTED;
1418                         found = 1;
1419                         break;
1420                 }
1421         }
1422
1423         // the port couldn't find an aggregator - attach it to a new aggregator
1424         if (!found) {
1425                 if (free_aggregator) {
1426                         // assign port a new aggregator
1427                         port->aggregator = free_aggregator;
1428                         port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier;
1429
1430                         // update the new aggregator's parameters
1431                         // if port was responsed from the end-user
1432                         if (port->actor_oper_port_key & AD_DUPLEX_KEY_BITS) {// if port is full duplex
1433                                 port->aggregator->is_individual = 0;
1434                         } else {
1435                                 port->aggregator->is_individual = 1;
1436                         }
1437
1438                         port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
1439                         port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
1440                         port->aggregator->partner_system=port->partner_oper_system;
1441                         port->aggregator->partner_system_priority = port->partner_oper_system_priority;
1442                         port->aggregator->partner_oper_aggregator_key = port->partner_oper_key;
1443                         port->aggregator->receive_state = 1;
1444                         port->aggregator->transmit_state = 1;
1445                         port->aggregator->lag_ports = port;
1446                         port->aggregator->num_of_ports++;
1447
1448                         // mark this port as selected
1449                         port->sm_vars |= AD_PORT_SELECTED;
1450
1451                         dprintk("Port %d joined LAG %d(new LAG)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1452                 } else {
1453                         printk(KERN_ERR DRV_NAME ": Port %d (on %s) did not find a suitable aggregator\n",
1454                                port->actor_port_number, port->slave->dev->name);
1455                 }
1456         }
1457         // if all aggregator's ports are READY_N == TRUE, set ready=TRUE in all aggregator's ports
1458         // else set ready=FALSE in all aggregator's ports
1459         __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
1460
1461         if (!__check_agg_selection_timer(port) && (aggregator = __get_first_agg(port))) {
1462                 ad_agg_selection_logic(aggregator);
1463         }
1464 }
1465
1466 /**
1467  * ad_agg_selection_logic - select an aggregation group for a team
1468  * @aggregator: the aggregator we're looking at
1469  *
1470  * It is assumed that only one aggregator may be selected for a team.
1471  * The logic of this function is to select (at first time) the aggregator with
1472  * the most ports attached to it, and to reselect the active aggregator only if
1473  * the previous aggregator has no more ports related to it.
1474  *
1475  * FIXME: this function MUST be called with the first agg in the bond, or
1476  * __get_active_agg() won't work correctly. This function should be better
1477  * called with the bond itself, and retrieve the first agg from it.
1478  */
1479 static void ad_agg_selection_logic(struct aggregator *aggregator)
1480 {
1481         struct aggregator *best_aggregator = NULL, *active_aggregator = NULL;
1482         struct aggregator *last_active_aggregator = NULL, *origin_aggregator;
1483         struct port *port;
1484         u16 num_of_aggs=0;
1485
1486         origin_aggregator = aggregator;
1487
1488         //get current active aggregator
1489         last_active_aggregator = __get_active_agg(aggregator);
1490
1491         // search for the aggregator with the most ports attached to it.
1492         do {
1493                 // count how many candidate lag's we have
1494                 if (aggregator->lag_ports) {
1495                         num_of_aggs++;
1496                 }
1497                 if (aggregator->is_active && !aggregator->is_individual &&   // if current aggregator is the active aggregator
1498                     MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(null_mac_addr))) {   // and partner answers to 802.3ad PDUs
1499                         if (aggregator->num_of_ports) { // if any ports attached to the current aggregator
1500                                 best_aggregator=NULL;    // disregard the best aggregator that was chosen by now
1501                                 break;           // stop the selection of other aggregator if there are any ports attached to this active aggregator
1502                         } else { // no ports attached to this active aggregator
1503                                 aggregator->is_active = 0; // mark this aggregator as not active anymore
1504                         }
1505                 }
1506                 if (aggregator->num_of_ports) { // if any ports attached
1507                         if (best_aggregator) {  // if there is a candidte aggregator
1508                                 //The reasons for choosing new best aggregator:
1509                                 // 1. if current agg is NOT individual and the best agg chosen so far is individual OR
1510                                 // current and best aggs are both individual or both not individual, AND
1511                                 // 2a.  current agg partner reply but best agg partner do not reply OR
1512                                 // 2b.  current agg partner reply OR current agg partner do not reply AND best agg partner also do not reply AND
1513                                 //      current has more ports/bandwidth, or same amount of ports but current has faster ports, THEN
1514                                 //      current agg become best agg so far
1515
1516                                 //if current agg is NOT individual and the best agg chosen so far is individual change best_aggregator
1517                                 if (!aggregator->is_individual && best_aggregator->is_individual) {
1518                                         best_aggregator=aggregator;
1519                                 }
1520                                 // current and best aggs are both individual or both not individual
1521                                 else if ((aggregator->is_individual && best_aggregator->is_individual) ||
1522                                          (!aggregator->is_individual && !best_aggregator->is_individual)) {
1523                                         //  current and best aggs are both individual or both not individual AND
1524                                         //  current agg partner reply but best agg partner do not reply
1525                                         if ((MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(null_mac_addr)) &&
1526                                              !MAC_ADDRESS_COMPARE(&(best_aggregator->partner_system), &(null_mac_addr)))) {
1527                                                 best_aggregator=aggregator;
1528                                         }
1529                                         //  current agg partner reply OR current agg partner do not reply AND best agg partner also do not reply
1530                                         else if (! (!MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(null_mac_addr)) &&
1531                                                     MAC_ADDRESS_COMPARE(&(best_aggregator->partner_system), &(null_mac_addr)))) {
1532                                                 if ((__get_agg_selection_mode(aggregator->lag_ports) == AD_BANDWIDTH)&&
1533                                                     (__get_agg_bandwidth(aggregator) > __get_agg_bandwidth(best_aggregator))) {
1534                                                         best_aggregator=aggregator;
1535                                                 } else if (__get_agg_selection_mode(aggregator->lag_ports) == AD_COUNT) {
1536                                                         if (((aggregator->num_of_ports > best_aggregator->num_of_ports) &&
1537                                                              (aggregator->actor_oper_aggregator_key & AD_SPEED_KEY_BITS))||
1538                                                             ((aggregator->num_of_ports == best_aggregator->num_of_ports) &&
1539                                                              ((u16)(aggregator->actor_oper_aggregator_key & AD_SPEED_KEY_BITS) >
1540                                                               (u16)(best_aggregator->actor_oper_aggregator_key & AD_SPEED_KEY_BITS)))) {
1541                                                                 best_aggregator=aggregator;
1542                                                         }
1543                                                 }
1544                                         }
1545                                 }
1546                         } else {
1547                                 best_aggregator=aggregator;
1548                         }
1549                 }
1550                 aggregator->is_active = 0; // mark all aggregators as not active anymore
1551         } while ((aggregator = __get_next_agg(aggregator)));
1552
1553         // if we have new aggregator selected, don't replace the old aggregator if it has an answering partner,
1554         // or if both old aggregator and new aggregator don't have answering partner
1555         if (best_aggregator) {
1556                 if (last_active_aggregator && last_active_aggregator->lag_ports && last_active_aggregator->lag_ports->is_enabled &&
1557                     (MAC_ADDRESS_COMPARE(&(last_active_aggregator->partner_system), &(null_mac_addr)) ||   // partner answers OR
1558                      (!MAC_ADDRESS_COMPARE(&(last_active_aggregator->partner_system), &(null_mac_addr)) &&      // both old and new
1559                       !MAC_ADDRESS_COMPARE(&(best_aggregator->partner_system), &(null_mac_addr))))     // partner do not answer
1560                    ) {
1561                         // if new aggregator has link, and old aggregator does not, replace old aggregator.(do nothing)
1562                         // -> don't replace otherwise.
1563                         if (!(!last_active_aggregator->actor_oper_aggregator_key && best_aggregator->actor_oper_aggregator_key)) {
1564                                 best_aggregator=NULL;
1565                                 last_active_aggregator->is_active = 1; // don't replace good old aggregator
1566
1567                         }
1568                 }
1569         }
1570
1571         // if there is new best aggregator, activate it
1572         if (best_aggregator) {
1573                 for (aggregator = __get_first_agg(best_aggregator->lag_ports);
1574                     aggregator;
1575                     aggregator = __get_next_agg(aggregator)) {
1576
1577                         dprintk("Agg=%d; Ports=%d; a key=%d; p key=%d; Indiv=%d; Active=%d\n",
1578                                         aggregator->aggregator_identifier, aggregator->num_of_ports,
1579                                         aggregator->actor_oper_aggregator_key, aggregator->partner_oper_aggregator_key,
1580                                         aggregator->is_individual, aggregator->is_active);
1581                 }
1582
1583                 // check if any partner replys
1584                 if (best_aggregator->is_individual) {
1585                         printk(KERN_WARNING DRV_NAME ": Warning: No 802.3ad response from the link partner "
1586                                         "for any adapters in the bond\n");
1587                 }
1588
1589                 // check if there are more than one aggregator
1590                 if (num_of_aggs > 1) {
1591                         dprintk("Warning: More than one Link Aggregation Group was "
1592                                 "found in the bond. Only one group will function in the bond\n");
1593                 }
1594
1595                 best_aggregator->is_active = 1;
1596                 dprintk("LAG %d choosed as the active LAG\n", best_aggregator->aggregator_identifier);
1597                 dprintk("Agg=%d; Ports=%d; a key=%d; p key=%d; Indiv=%d; Active=%d\n",
1598                                 best_aggregator->aggregator_identifier, best_aggregator->num_of_ports,
1599                                 best_aggregator->actor_oper_aggregator_key, best_aggregator->partner_oper_aggregator_key,
1600                                 best_aggregator->is_individual, best_aggregator->is_active);
1601
1602                 // disable the ports that were related to the former active_aggregator
1603                 if (last_active_aggregator) {
1604                         for (port=last_active_aggregator->lag_ports; port; port=port->next_port_in_aggregator) {
1605                                 __disable_port(port);
1606                         }
1607                 }
1608         }
1609
1610         // if the selected aggregator is of join individuals(partner_system is NULL), enable their ports
1611         active_aggregator = __get_active_agg(origin_aggregator);
1612
1613         if (active_aggregator) {
1614                 if (!MAC_ADDRESS_COMPARE(&(active_aggregator->partner_system), &(null_mac_addr))) {
1615                         for (port=active_aggregator->lag_ports; port; port=port->next_port_in_aggregator) {
1616                                 __enable_port(port);
1617                         }
1618                 }
1619         }
1620 }
1621
1622 /**
1623  * ad_clear_agg - clear a given aggregator's parameters
1624  * @aggregator: the aggregator we're looking at
1625  *
1626  */
1627 static void ad_clear_agg(struct aggregator *aggregator)
1628 {
1629         if (aggregator) {
1630                 aggregator->is_individual = 0;
1631                 aggregator->actor_admin_aggregator_key = 0;
1632                 aggregator->actor_oper_aggregator_key = 0;
1633                 aggregator->partner_system = null_mac_addr;
1634                 aggregator->partner_system_priority = 0;
1635                 aggregator->partner_oper_aggregator_key = 0;
1636                 aggregator->receive_state = 0;
1637                 aggregator->transmit_state = 0;
1638                 aggregator->lag_ports = NULL;
1639                 aggregator->is_active = 0;
1640                 aggregator->num_of_ports = 0;
1641                 dprintk("LAG %d was cleared\n", aggregator->aggregator_identifier);
1642         }
1643 }
1644
1645 /**
1646  * ad_initialize_agg - initialize a given aggregator's parameters
1647  * @aggregator: the aggregator we're looking at
1648  *
1649  */
1650 static void ad_initialize_agg(struct aggregator *aggregator)
1651 {
1652         if (aggregator) {
1653                 ad_clear_agg(aggregator);
1654
1655                 aggregator->aggregator_mac_address = null_mac_addr;
1656                 aggregator->aggregator_identifier = 0;
1657                 aggregator->slave = NULL;
1658         }
1659 }
1660
1661 /**
1662  * ad_initialize_port - initialize a given port's parameters
1663  * @aggregator: the aggregator we're looking at
1664  * @lacp_fast: boolean. whether fast periodic should be used
1665  *
1666  */
1667 static void ad_initialize_port(struct port *port, int lacp_fast)
1668 {
1669         if (port) {
1670                 port->actor_port_number = 1;
1671                 port->actor_port_priority = 0xff;
1672                 port->actor_system = null_mac_addr;
1673                 port->actor_system_priority = 0xffff;
1674                 port->actor_port_aggregator_identifier = 0;
1675                 port->ntt = 0;
1676                 port->actor_admin_port_key = 1;
1677                 port->actor_oper_port_key  = 1;
1678                 port->actor_admin_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1679                 port->actor_oper_port_state  = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1680
1681                 if (lacp_fast) {
1682                         port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
1683                 }
1684
1685                 port->partner_admin_system = null_mac_addr;
1686                 port->partner_oper_system  = null_mac_addr;
1687                 port->partner_admin_system_priority = 0xffff;
1688                 port->partner_oper_system_priority  = 0xffff;
1689                 port->partner_admin_key = 1;
1690                 port->partner_oper_key  = 1;
1691                 port->partner_admin_port_number = 1;
1692                 port->partner_oper_port_number  = 1;
1693                 port->partner_admin_port_priority = 0xff;
1694                 port->partner_oper_port_priority  = 0xff;
1695                 port->partner_admin_port_state = 1;
1696                 port->partner_oper_port_state  = 1;
1697                 port->is_enabled = 1;
1698                 // ****** private parameters ******
1699                 port->sm_vars = 0x3;
1700                 port->sm_rx_state = 0;
1701                 port->sm_rx_timer_counter = 0;
1702                 port->sm_periodic_state = 0;
1703                 port->sm_periodic_timer_counter = 0;
1704                 port->sm_mux_state = 0;
1705                 port->sm_mux_timer_counter = 0;
1706                 port->sm_tx_state = 0;
1707                 port->sm_tx_timer_counter = 0;
1708                 port->slave = NULL;
1709                 port->aggregator = NULL;
1710                 port->next_port_in_aggregator = NULL;
1711                 port->transaction_id = 0;
1712
1713                 ad_initialize_lacpdu(&(port->lacpdu));
1714         }
1715 }
1716
1717 /**
1718  * ad_enable_collecting_distributing - enable a port's transmit/receive
1719  * @port: the port we're looking at
1720  *
1721  * Enable @port if it's in an active aggregator
1722  */
1723 static void ad_enable_collecting_distributing(struct port *port)
1724 {
1725         if (port->aggregator->is_active) {
1726                 dprintk("Enabling port %d(LAG %d)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1727                 __enable_port(port);
1728         }
1729 }
1730
1731 /**
1732  * ad_disable_collecting_distributing - disable a port's transmit/receive
1733  * @port: the port we're looking at
1734  *
1735  */
1736 static void ad_disable_collecting_distributing(struct port *port)
1737 {
1738         if (port->aggregator && MAC_ADDRESS_COMPARE(&(port->aggregator->partner_system), &(null_mac_addr))) {
1739                 dprintk("Disabling port %d(LAG %d)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1740                 __disable_port(port);
1741         }
1742 }
1743
1744 #if 0
1745 /**
1746  * ad_marker_info_send - send a marker information frame
1747  * @port: the port we're looking at
1748  *
1749  * This function does nothing since we decided not to implement send and handle
1750  * response for marker PDU's, in this stage, but only to respond to marker
1751  * information.
1752  */
1753 static void ad_marker_info_send(struct port *port)
1754 {
1755         struct marker marker;
1756         u16 index;
1757
1758         // fill the marker PDU with the appropriate values
1759         marker.subtype = 0x02;
1760         marker.version_number = 0x01;
1761         marker.tlv_type = AD_MARKER_INFORMATION_SUBTYPE;
1762         marker.marker_length = 0x16;
1763         // convert requester_port to Big Endian
1764         marker.requester_port = (((port->actor_port_number & 0xFF) << 8) |((u16)(port->actor_port_number & 0xFF00) >> 8));
1765         marker.requester_system = port->actor_system;
1766         // convert requester_port(u32) to Big Endian
1767         marker.requester_transaction_id = (((++port->transaction_id & 0xFF) << 24) |((port->transaction_id & 0xFF00) << 8) |((port->transaction_id & 0xFF0000) >> 8) |((port->transaction_id & 0xFF000000) >> 24));
1768         marker.pad = 0;
1769         marker.tlv_type_terminator = 0x00;
1770         marker.terminator_length = 0x00;
1771         for (index=0; index<90; index++) {
1772                 marker.reserved_90[index]=0;
1773         }
1774
1775         // send the marker information
1776         if (ad_marker_send(port, &marker) >= 0) {
1777                 dprintk("Sent Marker Information on port %d\n", port->actor_port_number);
1778         }
1779 }
1780 #endif
1781
1782 /**
1783  * ad_marker_info_received - handle receive of a Marker information frame
1784  * @marker_info: Marker info received
1785  * @port: the port we're looking at
1786  *
1787  */
1788 static void ad_marker_info_received(struct marker *marker_info,struct port *port)
1789 {
1790         struct marker marker;
1791
1792         // copy the received marker data to the response marker
1793         //marker = *marker_info;
1794         memcpy(&marker, marker_info, sizeof(struct marker));
1795         // change the marker subtype to marker response
1796         marker.tlv_type=AD_MARKER_RESPONSE_SUBTYPE;
1797         // send the marker response
1798
1799         if (ad_marker_send(port, &marker) >= 0) {
1800                 dprintk("Sent Marker Response on port %d\n", port->actor_port_number);
1801         }
1802 }
1803
1804 /**
1805  * ad_marker_response_received - handle receive of a marker response frame
1806  * @marker: marker PDU received
1807  * @port: the port we're looking at
1808  *
1809  * This function does nothing since we decided not to implement send and handle
1810  * response for marker PDU's, in this stage, but only to respond to marker
1811  * information.
1812  */
1813 static void ad_marker_response_received(struct marker *marker, struct port *port)
1814 {
1815         marker=NULL; // just to satisfy the compiler
1816         port=NULL;  // just to satisfy the compiler
1817         // DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW
1818 }
1819
1820 /**
1821  * ad_initialize_lacpdu - initialize a given lacpdu structure
1822  * @lacpdu: lacpdu structure to initialize
1823  *
1824  */
1825 static void ad_initialize_lacpdu(struct lacpdu *lacpdu)
1826 {
1827         u16 index;
1828
1829         // initialize lacpdu data
1830         lacpdu->subtype = 0x01;
1831         lacpdu->version_number = 0x01;
1832         lacpdu->tlv_type_actor_info = 0x01;
1833         lacpdu->actor_information_length = 0x14;
1834         // lacpdu->actor_system_priority    updated on send
1835         // lacpdu->actor_system             updated on send
1836         // lacpdu->actor_key                updated on send
1837         // lacpdu->actor_port_priority      updated on send
1838         // lacpdu->actor_port               updated on send
1839         // lacpdu->actor_state              updated on send
1840         lacpdu->tlv_type_partner_info = 0x02;
1841         lacpdu->partner_information_length = 0x14;
1842         for (index=0; index<=2; index++) {
1843                 lacpdu->reserved_3_1[index]=0;
1844         }
1845         // lacpdu->partner_system_priority  updated on send
1846         // lacpdu->partner_system           updated on send
1847         // lacpdu->partner_key              updated on send
1848         // lacpdu->partner_port_priority    updated on send
1849         // lacpdu->partner_port             updated on send
1850         // lacpdu->partner_state            updated on send
1851         for (index=0; index<=2; index++) {
1852                 lacpdu->reserved_3_2[index]=0;
1853         }
1854         lacpdu->tlv_type_collector_info = 0x03;
1855         lacpdu->collector_information_length= 0x10;
1856         lacpdu->collector_max_delay = AD_COLLECTOR_MAX_DELAY;
1857         for (index=0; index<=11; index++) {
1858                 lacpdu->reserved_12[index]=0;
1859         }
1860         lacpdu->tlv_type_terminator = 0x00;
1861         lacpdu->terminator_length = 0;
1862         for (index=0; index<=49; index++) {
1863                 lacpdu->reserved_50[index]=0;
1864         }
1865 }
1866
1867 //////////////////////////////////////////////////////////////////////////////////////
1868 // ================= AD exported functions to the main bonding code ==================
1869 //////////////////////////////////////////////////////////////////////////////////////
1870
1871 // Check aggregators status in team every T seconds
1872 #define AD_AGGREGATOR_SELECTION_TIMER  8
1873
1874 static u16 aggregator_identifier;
1875
1876 /**
1877  * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1878  * @bond: bonding struct to work on
1879  * @tick_resolution: tick duration (millisecond resolution)
1880  * @lacp_fast: boolean. whether fast periodic should be used
1881  *
1882  * Can be called only after the mac address of the bond is set.
1883  */
1884 void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution, int lacp_fast)
1885 {                         
1886         // check that the bond is not initialized yet
1887         if (MAC_ADDRESS_COMPARE(&(BOND_AD_INFO(bond).system.sys_mac_addr), &(bond->dev->dev_addr))) {
1888
1889                 aggregator_identifier = 0;
1890
1891                 BOND_AD_INFO(bond).lacp_fast = lacp_fast;
1892                 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
1893                 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
1894
1895                 // initialize how many times this module is called in one second(should be about every 100ms)
1896                 ad_ticks_per_sec = tick_resolution;
1897
1898                 // initialize the aggregator selection timer(to activate an aggregation selection after initialize)
1899                 BOND_AD_INFO(bond).agg_select_timer = (AD_AGGREGATOR_SELECTION_TIMER * ad_ticks_per_sec);
1900                 BOND_AD_INFO(bond).agg_select_mode = AD_BANDWIDTH;
1901         }
1902 }
1903
1904 /**
1905  * bond_3ad_bind_slave - initialize a slave's port
1906  * @slave: slave struct to work on
1907  *
1908  * Returns:   0 on success
1909  *          < 0 on error
1910  */
1911 int bond_3ad_bind_slave(struct slave *slave)
1912 {
1913         struct bonding *bond = bond_get_bond_by_slave(slave);
1914         struct port *port;
1915         struct aggregator *aggregator;
1916
1917         if (bond == NULL) {
1918                 printk(KERN_ERR "The slave %s is not attached to its bond\n", slave->dev->name);
1919                 return -1;
1920         }
1921
1922         //check that the slave has not been intialized yet.
1923         if (SLAVE_AD_INFO(slave).port.slave != slave) {
1924
1925                 // port initialization
1926                 port = &(SLAVE_AD_INFO(slave).port);
1927
1928                 ad_initialize_port(port, BOND_AD_INFO(bond).lacp_fast);
1929
1930                 port->slave = slave;
1931                 port->actor_port_number = SLAVE_AD_INFO(slave).id;
1932                 // key is determined according to the link speed, duplex and user key(which is yet not supported)
1933                 //              ------------------------------------------------------------
1934                 // Port key :   | User key                       |      Speed       |Duplex|
1935                 //              ------------------------------------------------------------
1936                 //              16                               6               1 0
1937                 port->actor_admin_port_key = 0; // initialize this parameter
1938                 port->actor_admin_port_key |= __get_duplex(port);
1939                 port->actor_admin_port_key |= (__get_link_speed(port) << 1);
1940                 port->actor_oper_port_key = port->actor_admin_port_key;
1941                 // if the port is not full duplex, then the port should be not lacp Enabled
1942                 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) {
1943                         port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1944                 }
1945                 // actor system is the bond's system
1946                 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
1947                 // tx timer(to verify that no more than MAX_TX_IN_SECOND lacpdu's are sent in one second)
1948                 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1949                 port->aggregator = NULL;
1950                 port->next_port_in_aggregator = NULL;
1951
1952                 __disable_port(port);
1953                 __initialize_port_locks(port);
1954
1955
1956                 // aggregator initialization
1957                 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1958
1959                 ad_initialize_agg(aggregator);
1960
1961                 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
1962                 aggregator->aggregator_identifier = (++aggregator_identifier);
1963                 aggregator->slave = slave;
1964                 aggregator->is_active = 0;
1965                 aggregator->num_of_ports = 0;
1966         }
1967
1968         return 0;
1969 }
1970
1971 /**
1972  * bond_3ad_unbind_slave - deinitialize a slave's port
1973  * @slave: slave struct to work on
1974  *
1975  * Search for the aggregator that is related to this port, remove the
1976  * aggregator and assign another aggregator for other port related to it
1977  * (if any), and remove the port.
1978  */
1979 void bond_3ad_unbind_slave(struct slave *slave)
1980 {
1981         struct port *port, *prev_port, *temp_port;
1982         struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1983         int select_new_active_agg = 0;
1984         
1985         // find the aggregator related to this slave
1986         aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1987
1988         // find the port related to this slave
1989         port = &(SLAVE_AD_INFO(slave).port);
1990
1991         // if slave is null, the whole port is not initialized
1992         if (!port->slave) {
1993                 printk(KERN_WARNING DRV_NAME ": Trying to unbind an uninitialized port on %s\n", slave->dev->name);
1994                 return;
1995         }
1996
1997         dprintk("Unbinding Link Aggregation Group %d\n", aggregator->aggregator_identifier);
1998
1999         /* Tell the partner that this port is not suitable for aggregation */
2000         port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
2001         __update_lacpdu_from_port(port);
2002         ad_lacpdu_send(port);
2003
2004         // check if this aggregator is occupied
2005         if (aggregator->lag_ports) {
2006                 // check if there are other ports related to this aggregator except
2007                 // the port related to this slave(thats ensure us that there is a
2008                 // reason to search for new aggregator, and that we will find one
2009                 if ((aggregator->lag_ports != port) || (aggregator->lag_ports->next_port_in_aggregator)) {
2010                         // find new aggregator for the related port(s)
2011                         new_aggregator = __get_first_agg(port);
2012                         for (; new_aggregator; new_aggregator = __get_next_agg(new_aggregator)) {
2013                                 // if the new aggregator is empty, or it connected to to our port only
2014                                 if (!new_aggregator->lag_ports || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator)) {
2015                                         break;
2016                                 }
2017                         }
2018                         // if new aggregator found, copy the aggregator's parameters
2019                         // and connect the related lag_ports to the new aggregator
2020                         if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
2021                                 dprintk("Some port(s) related to LAG %d - replaceing with LAG %d\n", aggregator->aggregator_identifier, new_aggregator->aggregator_identifier);
2022
2023                                 if ((new_aggregator->lag_ports == port) && new_aggregator->is_active) {
2024                                         printk(KERN_INFO DRV_NAME ": Removing an active aggregator\n");
2025                                         // select new active aggregator
2026                                          select_new_active_agg = 1;
2027                                 }
2028
2029                                 new_aggregator->is_individual = aggregator->is_individual;
2030                                 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2031                                 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2032                                 new_aggregator->partner_system = aggregator->partner_system;
2033                                 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2034                                 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2035                                 new_aggregator->receive_state = aggregator->receive_state;
2036                                 new_aggregator->transmit_state = aggregator->transmit_state;
2037                                 new_aggregator->lag_ports = aggregator->lag_ports;
2038                                 new_aggregator->is_active = aggregator->is_active;
2039                                 new_aggregator->num_of_ports = aggregator->num_of_ports;
2040
2041                                 // update the information that is written on the ports about the aggregator
2042                                 for (temp_port=aggregator->lag_ports; temp_port; temp_port=temp_port->next_port_in_aggregator) {
2043                                         temp_port->aggregator=new_aggregator;
2044                                         temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2045                                 }
2046
2047                                 // clear the aggregator
2048                                 ad_clear_agg(aggregator);
2049                                 
2050                                 if (select_new_active_agg) {
2051                                         ad_agg_selection_logic(__get_first_agg(port));
2052                                 }
2053                         } else {
2054                                 printk(KERN_WARNING DRV_NAME ": Warning: unbinding aggregator, "
2055                                        "and could not find a new aggregator for its ports\n");
2056                         }
2057                 } else { // in case that the only port related to this aggregator is the one we want to remove
2058                         select_new_active_agg = aggregator->is_active;
2059                         // clear the aggregator
2060                         ad_clear_agg(aggregator);
2061                         if (select_new_active_agg) {
2062                                 printk(KERN_INFO "Removing an active aggregator\n");
2063                                 // select new active aggregator
2064                                 ad_agg_selection_logic(__get_first_agg(port));
2065                         }
2066                 }
2067         }
2068
2069         dprintk("Unbinding port %d\n", port->actor_port_number);
2070         // find the aggregator that this port is connected to
2071         temp_aggregator = __get_first_agg(port);
2072         for (; temp_aggregator; temp_aggregator = __get_next_agg(temp_aggregator)) {
2073                 prev_port = NULL;
2074                 // search the port in the aggregator's related ports
2075                 for (temp_port=temp_aggregator->lag_ports; temp_port; prev_port=temp_port, temp_port=temp_port->next_port_in_aggregator) {
2076                         if (temp_port == port) { // the aggregator found - detach the port from this aggregator
2077                                 if (prev_port) {
2078                                         prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2079                                 } else {
2080                                         temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2081                                 }
2082                                 temp_aggregator->num_of_ports--;
2083                                 if (temp_aggregator->num_of_ports==0) {
2084                                         select_new_active_agg = temp_aggregator->is_active;
2085                                         // clear the aggregator
2086                                         ad_clear_agg(temp_aggregator);
2087                                         if (select_new_active_agg) {
2088                                                 printk(KERN_INFO "Removing an active aggregator\n");
2089                                                 // select new active aggregator
2090                                                 ad_agg_selection_logic(__get_first_agg(port));
2091                                         }
2092                                 }
2093                                 break;
2094                         }
2095                 }
2096         }
2097         port->slave=NULL;       
2098 }
2099
2100 /**
2101  * bond_3ad_state_machine_handler - handle state machines timeout
2102  * @bond: bonding struct to work on
2103  *
2104  * The state machine handling concept in this module is to check every tick
2105  * which state machine should operate any function. The execution order is
2106  * round robin, so when we have an interaction between state machines, the
2107  * reply of one to each other might be delayed until next tick.
2108  *
2109  * This function also complete the initialization when the agg_select_timer
2110  * times out, and it selects an aggregator for the ports that are yet not
2111  * related to any aggregator, and selects the active aggregator for a bond.
2112  */
2113 void bond_3ad_state_machine_handler(struct bonding *bond)
2114 {
2115         struct port *port;
2116         struct aggregator *aggregator;
2117
2118         read_lock(&bond->lock);
2119
2120         if (bond->kill_timers) {
2121                 goto out;
2122         }
2123
2124         //check if there are any slaves
2125         if (bond->slave_cnt == 0) {
2126                 goto re_arm;
2127         }
2128
2129         // check if agg_select_timer timer after initialize is timed out
2130         if (BOND_AD_INFO(bond).agg_select_timer && !(--BOND_AD_INFO(bond).agg_select_timer)) {
2131                 // select the active aggregator for the bond
2132                 if ((port = __get_first_port(bond))) {
2133                         if (!port->slave) {
2134                                 printk(KERN_WARNING DRV_NAME ": Warning: bond's first port is uninitialized\n");
2135                                 goto re_arm;
2136                         }
2137
2138                         aggregator = __get_first_agg(port);
2139                         ad_agg_selection_logic(aggregator);
2140                 }
2141         }
2142
2143         // for each port run the state machines
2144         for (port = __get_first_port(bond); port; port = __get_next_port(port)) {
2145                 if (!port->slave) {
2146                         printk(KERN_WARNING DRV_NAME ": Warning: Found an uninitialized port\n");
2147                         goto re_arm;
2148                 }
2149
2150                 ad_rx_machine(NULL, port);
2151                 ad_periodic_machine(port);
2152                 ad_port_selection_logic(port);
2153                 ad_mux_machine(port);
2154                 ad_tx_machine(port);
2155
2156                 // turn off the BEGIN bit, since we already handled it
2157                 if (port->sm_vars & AD_PORT_BEGIN) {
2158                         port->sm_vars &= ~AD_PORT_BEGIN;
2159                 }
2160         }
2161
2162 re_arm:
2163         mod_timer(&(BOND_AD_INFO(bond).ad_timer), jiffies + ad_delta_in_ticks);
2164 out:
2165         read_unlock(&bond->lock);
2166 }
2167
2168 /**
2169  * bond_3ad_rx_indication - handle a received frame
2170  * @lacpdu: received lacpdu
2171  * @slave: slave struct to work on
2172  * @length: length of the data received
2173  *
2174  * It is assumed that frames that were sent on this NIC don't returned as new
2175  * received frames (loopback). Since only the payload is given to this
2176  * function, it check for loopback.
2177  */
2178 static void bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave, u16 length)
2179 {
2180         struct port *port;
2181
2182         if (length >= sizeof(struct lacpdu)) {
2183
2184                 port = &(SLAVE_AD_INFO(slave).port);
2185
2186                 if (!port->slave) {
2187                         printk(KERN_WARNING DRV_NAME ": Warning: port of slave %s is uninitialized\n", slave->dev->name);
2188                         return;
2189                 }
2190
2191                 switch (lacpdu->subtype) {
2192                 case AD_TYPE_LACPDU:
2193                         __ntohs_lacpdu(lacpdu);
2194                         dprintk("Received LACPDU on port %d\n", port->actor_port_number);
2195                         ad_rx_machine(lacpdu, port);
2196                         break;
2197
2198                 case AD_TYPE_MARKER:
2199                         // No need to convert fields to Little Endian since we don't use the marker's fields.
2200
2201                         switch (((struct marker *)lacpdu)->tlv_type) {
2202                         case AD_MARKER_INFORMATION_SUBTYPE:
2203                                 dprintk("Received Marker Information on port %d\n", port->actor_port_number);
2204                                 ad_marker_info_received((struct marker *)lacpdu, port);
2205                                 break;
2206
2207                         case AD_MARKER_RESPONSE_SUBTYPE:
2208                                 dprintk("Received Marker Response on port %d\n", port->actor_port_number);
2209                                 ad_marker_response_received((struct marker *)lacpdu, port);
2210                                 break;
2211
2212                         default:
2213                                 dprintk("Received an unknown Marker subtype on slot %d\n", port->actor_port_number);
2214                         }
2215                 }
2216         }
2217 }
2218
2219 /**
2220  * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2221  * @slave: slave struct to work on
2222  *
2223  * Handle reselection of aggregator (if needed) for this port.
2224  */
2225 void bond_3ad_adapter_speed_changed(struct slave *slave)
2226 {
2227         struct port *port;
2228
2229         port = &(SLAVE_AD_INFO(slave).port);
2230
2231         // if slave is null, the whole port is not initialized
2232         if (!port->slave) {
2233                 printk(KERN_WARNING DRV_NAME ": Warning: speed changed for uninitialized port on %s\n",
2234                        slave->dev->name);
2235                 return;
2236         }
2237
2238         port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2239         port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1);
2240         dprintk("Port %d changed speed\n", port->actor_port_number);
2241         // there is no need to reselect a new aggregator, just signal the
2242         // state machines to reinitialize
2243         port->sm_vars |= AD_PORT_BEGIN;
2244 }
2245
2246 /**
2247  * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2248  * @slave: slave struct to work on
2249  *
2250  * Handle reselection of aggregator (if needed) for this port.
2251  */
2252 void bond_3ad_adapter_duplex_changed(struct slave *slave)
2253 {
2254         struct port *port;
2255
2256         port=&(SLAVE_AD_INFO(slave).port);
2257
2258         // if slave is null, the whole port is not initialized
2259         if (!port->slave) {
2260                 printk(KERN_WARNING DRV_NAME ": Warning: duplex changed for uninitialized port on %s\n",
2261                        slave->dev->name);
2262                 return;
2263         }
2264
2265         port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2266         port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port);
2267         dprintk("Port %d changed duplex\n", port->actor_port_number);
2268         // there is no need to reselect a new aggregator, just signal the
2269         // state machines to reinitialize
2270         port->sm_vars |= AD_PORT_BEGIN;
2271 }
2272
2273 /**
2274  * bond_3ad_handle_link_change - handle a slave's link status change indication
2275  * @slave: slave struct to work on
2276  * @status: whether the link is now up or down
2277  *
2278  * Handle reselection of aggregator (if needed) for this port.
2279  */
2280 void bond_3ad_handle_link_change(struct slave *slave, char link)
2281 {
2282         struct port *port;
2283
2284         port = &(SLAVE_AD_INFO(slave).port);
2285
2286         // if slave is null, the whole port is not initialized
2287         if (!port->slave) {
2288                 printk(KERN_WARNING DRV_NAME ": Warning: link status changed for uninitialized port on %s\n",
2289                         slave->dev->name);
2290                 return;
2291         }
2292
2293         // 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)
2294         // on link up we are forcing recheck on the duplex and speed since some of he adaptors(ce1000.lan) report
2295         if (link == BOND_LINK_UP) {
2296                 port->is_enabled = 1;
2297                 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2298                 port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port);
2299                 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2300                 port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1);
2301         } else {
2302                 /* link has failed */
2303                 port->is_enabled = 0;
2304                 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2305                 port->actor_oper_port_key= (port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS);
2306         }
2307         //BOND_PRINT_DBG(("Port %d changed link status to %s", port->actor_port_number, ((link == BOND_LINK_UP)?"UP":"DOWN")));
2308         // there is no need to reselect a new aggregator, just signal the
2309         // state machines to reinitialize
2310         port->sm_vars |= AD_PORT_BEGIN;
2311 }
2312
2313 /**
2314  * bond_3ad_get_active_agg_info - get information of the active aggregator
2315  * @bond: bonding struct to work on
2316  * @ad_info: ad_info struct to fill with the bond's info
2317  *
2318  * Returns:   0 on success
2319  *          < 0 on error
2320  */
2321 int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2322 {
2323         struct aggregator *aggregator = NULL;
2324         struct port *port;
2325
2326         for (port = __get_first_port(bond); port; port = __get_next_port(port)) {
2327                 if (port->aggregator && port->aggregator->is_active) {
2328                         aggregator = port->aggregator;
2329                         break;
2330                 }
2331         }
2332
2333         if (aggregator) {
2334                 ad_info->aggregator_id = aggregator->aggregator_identifier;
2335                 ad_info->ports = aggregator->num_of_ports;
2336                 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2337                 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2338                 memcpy(ad_info->partner_system, aggregator->partner_system.mac_addr_value, ETH_ALEN);
2339                 return 0;
2340         }
2341
2342         return -1;
2343 }
2344
2345 int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2346 {
2347         struct slave *slave, *start_at;
2348         struct bonding *bond = dev->priv;
2349         struct ethhdr *data = (struct ethhdr *)skb->data;
2350         int slave_agg_no;
2351         int slaves_in_agg;
2352         int agg_id;
2353         int i;
2354         struct ad_info ad_info;
2355         int res = 1;
2356
2357         /* make sure that the slaves list will
2358          * not change during tx
2359          */
2360         read_lock(&bond->lock);
2361
2362         if (!BOND_IS_OK(bond)) {
2363                 goto out;
2364         }
2365
2366         if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
2367                 printk(KERN_DEBUG "ERROR: bond_3ad_get_active_agg_info failed\n");
2368                 goto out;
2369         }
2370
2371         slaves_in_agg = ad_info.ports;
2372         agg_id = ad_info.aggregator_id;
2373
2374         if (slaves_in_agg == 0) {
2375                 /*the aggregator is empty*/
2376                 printk(KERN_DEBUG "ERROR: active aggregator is empty\n");
2377                 goto out;
2378         }
2379
2380         slave_agg_no = (data->h_dest[5]^bond->dev->dev_addr[5]) % slaves_in_agg;
2381
2382         bond_for_each_slave(bond, slave, i) {
2383                 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
2384
2385                 if (agg && (agg->aggregator_identifier == agg_id)) {
2386                         slave_agg_no--;
2387                         if (slave_agg_no < 0) {
2388                                 break;
2389                         }
2390                 }
2391         }
2392
2393         if (slave_agg_no >= 0) {
2394                 printk(KERN_ERR DRV_NAME ": Error: Couldn't find a slave to tx on for aggregator ID %d\n", agg_id);
2395                 goto out;
2396         }
2397
2398         start_at = slave;
2399
2400         bond_for_each_slave_from(bond, slave, i, start_at) {
2401                 int slave_agg_id = 0;
2402                 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
2403
2404                 if (agg) {
2405                         slave_agg_id = agg->aggregator_identifier;
2406                 }
2407
2408                 if (SLAVE_IS_OK(slave) && agg && (slave_agg_id == agg_id)) {
2409                         res = bond_dev_queue_xmit(bond, skb, slave->dev);
2410                         break;
2411                 }
2412         }
2413
2414 out:
2415         if (res) {
2416                 /* no suitable interface, frame not sent */
2417                 dev_kfree_skb(skb);
2418         }
2419         read_unlock(&bond->lock);
2420         return 0;
2421 }
2422
2423 int bond_3ad_lacpdu_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type* ptype)
2424 {
2425         struct bonding *bond = dev->priv;
2426         struct slave *slave = NULL;
2427         int ret = NET_RX_DROP;
2428
2429         if (!(dev->flags & IFF_MASTER)) {
2430                 goto out;
2431         }
2432
2433         read_lock(&bond->lock);
2434         slave = bond_get_slave_by_dev((struct bonding *)dev->priv,
2435                                       skb->real_dev);
2436         if (slave == NULL) {
2437                 goto out_unlock;
2438         }
2439
2440         bond_3ad_rx_indication((struct lacpdu *) skb->data, slave, skb->len);
2441
2442         ret = NET_RX_SUCCESS;
2443
2444 out_unlock:
2445         read_unlock(&bond->lock);
2446 out:
2447         dev_kfree_skb(skb);
2448
2449         return ret;
2450 }
2451