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