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