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