1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2018-2021, Intel Corporation. */
4 /* Link Aggregation code */
10 * ice_lag_set_primary - set PF LAG state as Primary
11 * @lag: LAG info struct
13 static void ice_lag_set_primary(struct ice_lag *lag)
15 struct ice_pf *pf = lag->pf;
20 if (lag->role != ICE_LAG_UNSET && lag->role != ICE_LAG_BACKUP) {
21 dev_warn(ice_pf_to_dev(pf), "%s: Attempt to be Primary, but incompatible state.\n",
22 netdev_name(lag->netdev));
26 lag->role = ICE_LAG_PRIMARY;
30 * ice_lag_set_backup - set PF LAG state to Backup
31 * @lag: LAG info struct
33 static void ice_lag_set_backup(struct ice_lag *lag)
35 struct ice_pf *pf = lag->pf;
40 if (lag->role != ICE_LAG_UNSET && lag->role != ICE_LAG_PRIMARY) {
41 dev_dbg(ice_pf_to_dev(pf), "%s: Attempt to be Backup, but incompatible state\n",
42 netdev_name(lag->netdev));
46 lag->role = ICE_LAG_BACKUP;
50 * ice_display_lag_info - print LAG info
51 * @lag: LAG info struct
53 static void ice_display_lag_info(struct ice_lag *lag)
55 const char *name, *peer, *upper, *role, *bonded, *primary;
56 struct device *dev = &lag->pf->pdev->dev;
58 name = lag->netdev ? netdev_name(lag->netdev) : "unset";
59 peer = lag->peer_netdev ? netdev_name(lag->peer_netdev) : "unset";
60 upper = lag->upper_netdev ? netdev_name(lag->upper_netdev) : "unset";
61 primary = lag->primary ? "TRUE" : "FALSE";
62 bonded = lag->bonded ? "BONDED" : "UNBONDED";
81 dev_dbg(dev, "%s %s, peer:%s, upper:%s, role:%s, primary:%s\n", name,
82 bonded, peer, upper, role, primary);
86 * ice_lag_info_event - handle NETDEV_BONDING_INFO event
87 * @lag: LAG info struct
88 * @ptr: opaque data pointer
90 * ptr is to be cast to (netdev_notifier_bonding_info *)
92 static void ice_lag_info_event(struct ice_lag *lag, void *ptr)
94 struct netdev_notifier_bonding_info *info;
95 struct netdev_bonding_info *bonding_info;
96 struct net_device *event_netdev;
97 const char *lag_netdev_name;
99 event_netdev = netdev_notifier_info_to_dev(ptr);
101 lag_netdev_name = netdev_name(lag->netdev);
102 bonding_info = &info->bonding_info;
104 if (event_netdev != lag->netdev || !lag->bonded || !lag->upper_netdev)
107 if (bonding_info->master.bond_mode != BOND_MODE_ACTIVEBACKUP) {
108 netdev_dbg(lag->netdev, "Bonding event recv, but mode not active/backup\n");
112 if (strcmp(bonding_info->slave.slave_name, lag_netdev_name)) {
113 netdev_dbg(lag->netdev, "Bonding event recv, but secondary info not for us\n");
117 if (bonding_info->slave.state)
118 ice_lag_set_backup(lag);
120 ice_lag_set_primary(lag);
123 ice_display_lag_info(lag);
127 * ice_lag_link - handle LAG link event
128 * @lag: LAG info struct
129 * @info: info from the netdev notifier
132 ice_lag_link(struct ice_lag *lag, struct netdev_notifier_changeupper_info *info)
134 struct net_device *netdev_tmp, *upper = info->upper_dev;
135 struct ice_pf *pf = lag->pf;
139 dev_warn(ice_pf_to_dev(pf), "%s Already part of a bond\n",
140 netdev_name(lag->netdev));
143 for_each_netdev_in_bond_rcu(upper, netdev_tmp)
147 if (lag->upper_netdev != upper) {
149 lag->upper_netdev = upper;
152 ice_clear_rdma_cap(pf);
155 lag->role = ICE_LAG_UNSET;
157 /* if this is the first element in an LAG mark as primary */
158 lag->primary = !!(peers == 1);
162 * ice_lag_unlink - handle unlink event
163 * @lag: LAG info struct
164 * @info: info from netdev notification
167 ice_lag_unlink(struct ice_lag *lag,
168 struct netdev_notifier_changeupper_info *info)
170 struct net_device *netdev_tmp, *upper = info->upper_dev;
171 struct ice_pf *pf = lag->pf;
175 netdev_dbg(lag->netdev, "bonding unlink event on non-LAG netdev\n");
179 /* determine if we are in the new LAG config or not */
181 for_each_netdev_in_bond_rcu(upper, netdev_tmp) {
182 if (netdev_tmp == lag->netdev) {
192 if (lag->upper_netdev) {
193 dev_put(lag->upper_netdev);
194 lag->upper_netdev = NULL;
197 lag->peer_netdev = NULL;
198 ice_set_rdma_cap(pf);
200 lag->role = ICE_LAG_NONE;
204 * ice_lag_unregister - handle netdev unregister events
205 * @lag: LAG info struct
206 * @netdev: netdev reporting the event
208 static void ice_lag_unregister(struct ice_lag *lag, struct net_device *netdev)
210 struct ice_pf *pf = lag->pf;
212 /* check to see if this event is for this netdev
213 * check that we are in an aggregate
215 if (netdev != lag->netdev || !lag->bonded)
218 if (lag->upper_netdev) {
219 dev_put(lag->upper_netdev);
220 lag->upper_netdev = NULL;
221 ice_set_rdma_cap(pf);
223 /* perform some cleanup in case we come back */
225 lag->role = ICE_LAG_NONE;
229 * ice_lag_changeupper_event - handle LAG changeupper event
230 * @lag: LAG info struct
231 * @ptr: opaque pointer data
233 * ptr is to be cast into netdev_notifier_changeupper_info
235 static void ice_lag_changeupper_event(struct ice_lag *lag, void *ptr)
237 struct netdev_notifier_changeupper_info *info;
238 struct net_device *netdev;
241 netdev = netdev_notifier_info_to_dev(ptr);
243 /* not for this netdev */
244 if (netdev != lag->netdev)
247 if (!info->upper_dev) {
248 netdev_dbg(netdev, "changeupper rcvd, but no upper defined\n");
252 netdev_dbg(netdev, "bonding %s\n", info->linking ? "LINK" : "UNLINK");
254 if (!netif_is_lag_master(info->upper_dev)) {
255 netdev_dbg(netdev, "changeupper rcvd, but not primary. bail\n");
260 ice_lag_link(lag, info);
262 ice_lag_unlink(lag, info);
264 ice_display_lag_info(lag);
268 * ice_lag_changelower_event - handle LAG changelower event
269 * @lag: LAG info struct
270 * @ptr: opaque data pointer
272 * ptr to be cast to netdev_notifier_changelowerstate_info
274 static void ice_lag_changelower_event(struct ice_lag *lag, void *ptr)
276 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
278 if (netdev != lag->netdev)
281 netdev_dbg(netdev, "bonding info\n");
283 if (!netif_is_lag_port(netdev))
284 netdev_dbg(netdev, "CHANGELOWER rcvd, but netdev not in LAG. Bail\n");
288 * ice_lag_event_handler - handle LAG events from netdev
289 * @notif_blk: notifier block registered by this netdev
291 * @ptr: opaque data containing notifier event
294 ice_lag_event_handler(struct notifier_block *notif_blk, unsigned long event,
297 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
300 lag = container_of(notif_blk, struct ice_lag, notif_block);
305 /* Check that the netdev is in the working namespace */
306 if (!net_eq(dev_net(netdev), &init_net))
310 case NETDEV_CHANGEUPPER:
311 ice_lag_changeupper_event(lag, ptr);
313 case NETDEV_CHANGELOWERSTATE:
314 ice_lag_changelower_event(lag, ptr);
316 case NETDEV_BONDING_INFO:
317 ice_lag_info_event(lag, ptr);
319 case NETDEV_UNREGISTER:
320 ice_lag_unregister(lag, netdev);
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 dev_put(lag->upper_netdev);
440 dev_put(lag->peer_netdev);