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