1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2018-2021, Intel Corporation. */
4 /* Link Aggregation code */
10 * ice_lag_nop_handler - no-op Rx handler to disable LAG
11 * @pskb: pointer to skb pointer
13 rx_handler_result_t
ice_lag_nop_handler(struct sk_buff __always_unused
**pskb
)
15 return RX_HANDLER_PASS
;
19 * ice_lag_set_primary - set PF LAG state as Primary
20 * @lag: LAG info struct
22 static void ice_lag_set_primary(struct ice_lag
*lag
)
24 struct ice_pf
*pf
= lag
->pf
;
29 if (lag
->role
!= ICE_LAG_UNSET
&& lag
->role
!= ICE_LAG_BACKUP
) {
30 dev_warn(ice_pf_to_dev(pf
), "%s: Attempt to be Primary, but incompatible state.\n",
31 netdev_name(lag
->netdev
));
35 lag
->role
= ICE_LAG_PRIMARY
;
39 * ice_lag_set_backup - set PF LAG state to Backup
40 * @lag: LAG info struct
42 static void ice_lag_set_backup(struct ice_lag
*lag
)
44 struct ice_pf
*pf
= lag
->pf
;
49 if (lag
->role
!= ICE_LAG_UNSET
&& lag
->role
!= ICE_LAG_PRIMARY
) {
50 dev_dbg(ice_pf_to_dev(pf
), "%s: Attempt to be Backup, but incompatible state\n",
51 netdev_name(lag
->netdev
));
55 lag
->role
= ICE_LAG_BACKUP
;
59 * ice_display_lag_info - print LAG info
60 * @lag: LAG info struct
62 static void ice_display_lag_info(struct ice_lag
*lag
)
64 const char *name
, *peer
, *upper
, *role
, *bonded
, *master
;
65 struct device
*dev
= &lag
->pf
->pdev
->dev
;
67 name
= lag
->netdev
? netdev_name(lag
->netdev
) : "unset";
68 peer
= lag
->peer_netdev
? netdev_name(lag
->peer_netdev
) : "unset";
69 upper
= lag
->upper_netdev
? netdev_name(lag
->upper_netdev
) : "unset";
70 master
= lag
->master
? "TRUE" : "FALSE";
71 bonded
= lag
->bonded
? "BONDED" : "UNBONDED";
90 dev_dbg(dev
, "%s %s, peer:%s, upper:%s, role:%s, master:%s\n", name
,
91 bonded
, peer
, upper
, role
, master
);
95 * ice_lag_info_event - handle NETDEV_BONDING_INFO event
96 * @lag: LAG info struct
97 * @ptr: opaque data pointer
99 * ptr is to be cast to (netdev_notifier_bonding_info *)
101 static void ice_lag_info_event(struct ice_lag
*lag
, void *ptr
)
103 struct net_device
*event_netdev
, *netdev_tmp
;
104 struct netdev_notifier_bonding_info
*info
;
105 struct netdev_bonding_info
*bonding_info
;
106 const char *lag_netdev_name
;
108 event_netdev
= netdev_notifier_info_to_dev(ptr
);
110 lag_netdev_name
= netdev_name(lag
->netdev
);
111 bonding_info
= &info
->bonding_info
;
113 if (event_netdev
!= lag
->netdev
|| !lag
->bonded
|| !lag
->upper_netdev
)
116 if (bonding_info
->master
.bond_mode
!= BOND_MODE_ACTIVEBACKUP
) {
117 netdev_dbg(lag
->netdev
, "Bonding event recv, but mode not active/backup\n");
121 if (strcmp(bonding_info
->slave
.slave_name
, lag_netdev_name
)) {
122 netdev_dbg(lag
->netdev
, "Bonding event recv, but slave info not for us\n");
127 for_each_netdev_in_bond_rcu(lag
->upper_netdev
, netdev_tmp
) {
128 if (!netif_is_ice(netdev_tmp
))
131 if (netdev_tmp
&& netdev_tmp
!= lag
->netdev
&&
132 lag
->peer_netdev
!= netdev_tmp
) {
133 dev_hold(netdev_tmp
);
134 lag
->peer_netdev
= netdev_tmp
;
139 if (bonding_info
->slave
.state
)
140 ice_lag_set_backup(lag
);
142 ice_lag_set_primary(lag
);
145 ice_display_lag_info(lag
);
149 * ice_lag_link - handle LAG link event
150 * @lag: LAG info struct
151 * @info: info from the netdev notifier
154 ice_lag_link(struct ice_lag
*lag
, struct netdev_notifier_changeupper_info
*info
)
156 struct net_device
*netdev_tmp
, *upper
= info
->upper_dev
;
157 struct ice_pf
*pf
= lag
->pf
;
161 dev_warn(ice_pf_to_dev(pf
), "%s Already part of a bond\n",
162 netdev_name(lag
->netdev
));
165 for_each_netdev_in_bond_rcu(upper
, netdev_tmp
)
169 if (lag
->upper_netdev
!= upper
) {
171 lag
->upper_netdev
= upper
;
174 ice_clear_sriov_cap(pf
);
175 ice_clear_rdma_cap(pf
);
178 lag
->role
= ICE_LAG_UNSET
;
180 /* if this is the first element in an LAG mark as master */
181 lag
->master
= !!(peers
== 1);
185 * ice_lag_unlink - handle unlink event
186 * @lag: LAG info struct
187 * @info: info from netdev notification
190 ice_lag_unlink(struct ice_lag
*lag
,
191 struct netdev_notifier_changeupper_info
*info
)
193 struct net_device
*netdev_tmp
, *upper
= info
->upper_dev
;
194 struct ice_pf
*pf
= lag
->pf
;
198 netdev_dbg(lag
->netdev
, "bonding unlink event on non-LAG netdev\n");
202 /* determine if we are in the new LAG config or not */
204 for_each_netdev_in_bond_rcu(upper
, netdev_tmp
) {
205 if (netdev_tmp
== lag
->netdev
) {
215 if (lag
->upper_netdev
) {
216 dev_put(lag
->upper_netdev
);
217 lag
->upper_netdev
= NULL
;
220 if (lag
->peer_netdev
) {
221 dev_put(lag
->peer_netdev
);
222 lag
->peer_netdev
= NULL
;
225 ice_set_sriov_cap(pf
);
226 ice_set_rdma_cap(pf
);
228 lag
->role
= ICE_LAG_NONE
;
232 * ice_lag_changeupper_event - handle LAG changeupper event
233 * @lag: LAG info struct
234 * @ptr: opaque pointer data
236 * ptr is to be cast into netdev_notifier_changeupper_info
238 static void ice_lag_changeupper_event(struct ice_lag
*lag
, void *ptr
)
240 struct netdev_notifier_changeupper_info
*info
;
241 struct net_device
*netdev
;
244 netdev
= netdev_notifier_info_to_dev(ptr
);
246 /* not for this netdev */
247 if (netdev
!= lag
->netdev
)
250 if (!info
->upper_dev
) {
251 netdev_dbg(netdev
, "changeupper rcvd, but no upper defined\n");
255 netdev_dbg(netdev
, "bonding %s\n", info
->linking
? "LINK" : "UNLINK");
257 if (!netif_is_lag_master(info
->upper_dev
)) {
258 netdev_dbg(netdev
, "changeupper rcvd, but not master. bail\n");
263 ice_lag_link(lag
, info
);
265 ice_lag_unlink(lag
, info
);
267 ice_display_lag_info(lag
);
271 * ice_lag_changelower_event - handle LAG changelower event
272 * @lag: LAG info struct
273 * @ptr: opaque data pointer
275 * ptr to be cast to netdev_notifier_changelowerstate_info
277 static void ice_lag_changelower_event(struct ice_lag
*lag
, void *ptr
)
279 struct net_device
*netdev
= netdev_notifier_info_to_dev(ptr
);
281 if (netdev
!= lag
->netdev
)
284 netdev_dbg(netdev
, "bonding info\n");
286 if (!netif_is_lag_port(netdev
))
287 netdev_dbg(netdev
, "CHANGELOWER rcvd, but netdev not in LAG. Bail\n");
291 * ice_lag_event_handler - handle LAG events from netdev
292 * @notif_blk: notifier block registered by this netdev
294 * @ptr: opaque data containing notifier event
297 ice_lag_event_handler(struct notifier_block
*notif_blk
, unsigned long event
,
300 struct net_device
*netdev
= netdev_notifier_info_to_dev(ptr
);
303 lag
= container_of(notif_blk
, struct ice_lag
, notif_block
);
308 /* Check that the netdev is in the working namespace */
309 if (!net_eq(dev_net(netdev
), &init_net
))
313 case NETDEV_CHANGEUPPER
:
314 ice_lag_changeupper_event(lag
, ptr
);
316 case NETDEV_CHANGELOWERSTATE
:
317 ice_lag_changelower_event(lag
, ptr
);
319 case NETDEV_BONDING_INFO
:
320 ice_lag_info_event(lag
, ptr
);
330 * ice_register_lag_handler - register LAG handler on netdev
333 static int ice_register_lag_handler(struct ice_lag
*lag
)
335 struct device
*dev
= ice_pf_to_dev(lag
->pf
);
336 struct notifier_block
*notif_blk
;
338 notif_blk
= &lag
->notif_block
;
340 if (!notif_blk
->notifier_call
) {
341 notif_blk
->notifier_call
= ice_lag_event_handler
;
342 if (register_netdevice_notifier(notif_blk
)) {
343 notif_blk
->notifier_call
= NULL
;
344 dev_err(dev
, "FAIL register LAG event handler!\n");
347 dev_dbg(dev
, "LAG event handler registered\n");
353 * ice_unregister_lag_handler - unregister LAG handler on netdev
356 static void ice_unregister_lag_handler(struct ice_lag
*lag
)
358 struct device
*dev
= ice_pf_to_dev(lag
->pf
);
359 struct notifier_block
*notif_blk
;
361 notif_blk
= &lag
->notif_block
;
362 if (notif_blk
->notifier_call
) {
363 unregister_netdevice_notifier(notif_blk
);
364 dev_dbg(dev
, "LAG event handler unregistered\n");
369 * ice_init_lag - initialize support for LAG
372 * Alloc memory for LAG structs and initialize the elements.
373 * Memory will be freed in ice_deinit_lag
375 int ice_init_lag(struct ice_pf
*pf
)
377 struct device
*dev
= ice_pf_to_dev(pf
);
382 pf
->lag
= kzalloc(sizeof(*lag
), GFP_KERNEL
);
387 vsi
= ice_get_main_vsi(pf
);
389 dev_err(dev
, "couldn't get main vsi, link aggregation init fail\n");
395 lag
->netdev
= vsi
->netdev
;
396 lag
->role
= ICE_LAG_NONE
;
398 lag
->peer_netdev
= NULL
;
399 lag
->upper_netdev
= NULL
;
400 lag
->notif_block
.notifier_call
= NULL
;
402 err
= ice_register_lag_handler(lag
);
404 dev_warn(dev
, "INIT LAG: Failed to register event handler\n");
408 ice_display_lag_info(lag
);
410 dev_dbg(dev
, "INIT LAG complete\n");
420 * ice_deinit_lag - Clean up LAG
423 * Clean up kernel LAG info and free memory
424 * This function is meant to only be called on driver remove/shutdown
426 void ice_deinit_lag(struct ice_pf
*pf
)
436 ice_unregister_lag_handler(lag
);
438 if (lag
->upper_netdev
)
439 dev_put(lag
->upper_netdev
);
441 if (lag
->peer_netdev
)
442 dev_put(lag
->peer_netdev
);