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