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