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