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