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