1 /*
2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33 #include <linux/netdevice.h>
34 #include <net/bonding.h>
35 #include <linux/mlx5/driver.h>
36 #include <linux/mlx5/eswitch.h>
37 #include <linux/mlx5/vport.h>
38 #include "lib/devcom.h"
39 #include "mlx5_core.h"
40 #include "eswitch.h"
41 #include "esw/acl/ofld.h"
42 #include "lag.h"
43 #include "mp.h"
44 #include "mpesw.h"
45
46 enum {
47 MLX5_LAG_EGRESS_PORT_1 = 1,
48 MLX5_LAG_EGRESS_PORT_2,
49 };
50
51 /* General purpose, use for short periods of time.
52 * Beware of lock dependencies (preferably, no locks should be acquired
53 * under it).
54 */
55 static DEFINE_SPINLOCK(lag_lock);
56
get_port_sel_mode(enum mlx5_lag_mode mode,unsigned long flags)57 static int get_port_sel_mode(enum mlx5_lag_mode mode, unsigned long flags)
58 {
59 if (test_bit(MLX5_LAG_MODE_FLAG_HASH_BASED, &flags))
60 return MLX5_LAG_PORT_SELECT_MODE_PORT_SELECT_FT;
61
62 if (mode == MLX5_LAG_MODE_MPESW)
63 return MLX5_LAG_PORT_SELECT_MODE_PORT_SELECT_MPESW;
64
65 return MLX5_LAG_PORT_SELECT_MODE_QUEUE_AFFINITY;
66 }
67
lag_active_port_bits(struct mlx5_lag * ldev)68 static u8 lag_active_port_bits(struct mlx5_lag *ldev)
69 {
70 u8 enabled_ports[MLX5_MAX_PORTS] = {};
71 u8 active_port = 0;
72 int num_enabled;
73 int idx;
74
75 mlx5_infer_tx_enabled(&ldev->tracker, ldev->ports, enabled_ports,
76 &num_enabled);
77 for (idx = 0; idx < num_enabled; idx++)
78 active_port |= BIT_MASK(enabled_ports[idx]);
79
80 return active_port;
81 }
82
mlx5_cmd_create_lag(struct mlx5_core_dev * dev,u8 * ports,int mode,unsigned long flags)83 static int mlx5_cmd_create_lag(struct mlx5_core_dev *dev, u8 *ports, int mode,
84 unsigned long flags)
85 {
86 bool fdb_sel_mode = test_bit(MLX5_LAG_MODE_FLAG_FDB_SEL_MODE_NATIVE,
87 &flags);
88 int port_sel_mode = get_port_sel_mode(mode, flags);
89 u32 in[MLX5_ST_SZ_DW(create_lag_in)] = {};
90 void *lag_ctx;
91
92 lag_ctx = MLX5_ADDR_OF(create_lag_in, in, ctx);
93 MLX5_SET(create_lag_in, in, opcode, MLX5_CMD_OP_CREATE_LAG);
94 MLX5_SET(lagc, lag_ctx, fdb_selection_mode, fdb_sel_mode);
95
96 switch (port_sel_mode) {
97 case MLX5_LAG_PORT_SELECT_MODE_QUEUE_AFFINITY:
98 MLX5_SET(lagc, lag_ctx, tx_remap_affinity_1, ports[0]);
99 MLX5_SET(lagc, lag_ctx, tx_remap_affinity_2, ports[1]);
100 break;
101 case MLX5_LAG_PORT_SELECT_MODE_PORT_SELECT_FT:
102 if (!MLX5_CAP_PORT_SELECTION(dev, port_select_flow_table_bypass))
103 break;
104
105 MLX5_SET(lagc, lag_ctx, active_port,
106 lag_active_port_bits(mlx5_lag_dev(dev)));
107 break;
108 default:
109 break;
110 }
111 MLX5_SET(lagc, lag_ctx, port_select_mode, port_sel_mode);
112
113 return mlx5_cmd_exec_in(dev, create_lag, in);
114 }
115
mlx5_cmd_modify_lag(struct mlx5_core_dev * dev,u8 num_ports,u8 * ports)116 static int mlx5_cmd_modify_lag(struct mlx5_core_dev *dev, u8 num_ports,
117 u8 *ports)
118 {
119 u32 in[MLX5_ST_SZ_DW(modify_lag_in)] = {};
120 void *lag_ctx = MLX5_ADDR_OF(modify_lag_in, in, ctx);
121
122 MLX5_SET(modify_lag_in, in, opcode, MLX5_CMD_OP_MODIFY_LAG);
123 MLX5_SET(modify_lag_in, in, field_select, 0x1);
124
125 MLX5_SET(lagc, lag_ctx, tx_remap_affinity_1, ports[0]);
126 MLX5_SET(lagc, lag_ctx, tx_remap_affinity_2, ports[1]);
127
128 return mlx5_cmd_exec_in(dev, modify_lag, in);
129 }
130
mlx5_cmd_create_vport_lag(struct mlx5_core_dev * dev)131 int mlx5_cmd_create_vport_lag(struct mlx5_core_dev *dev)
132 {
133 u32 in[MLX5_ST_SZ_DW(create_vport_lag_in)] = {};
134
135 MLX5_SET(create_vport_lag_in, in, opcode, MLX5_CMD_OP_CREATE_VPORT_LAG);
136
137 return mlx5_cmd_exec_in(dev, create_vport_lag, in);
138 }
139 EXPORT_SYMBOL(mlx5_cmd_create_vport_lag);
140
mlx5_cmd_destroy_vport_lag(struct mlx5_core_dev * dev)141 int mlx5_cmd_destroy_vport_lag(struct mlx5_core_dev *dev)
142 {
143 u32 in[MLX5_ST_SZ_DW(destroy_vport_lag_in)] = {};
144
145 MLX5_SET(destroy_vport_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_VPORT_LAG);
146
147 return mlx5_cmd_exec_in(dev, destroy_vport_lag, in);
148 }
149 EXPORT_SYMBOL(mlx5_cmd_destroy_vport_lag);
150
mlx5_infer_tx_disabled(struct lag_tracker * tracker,u8 num_ports,u8 * ports,int * num_disabled)151 static void mlx5_infer_tx_disabled(struct lag_tracker *tracker, u8 num_ports,
152 u8 *ports, int *num_disabled)
153 {
154 int i;
155
156 *num_disabled = 0;
157 for (i = 0; i < num_ports; i++) {
158 if (!tracker->netdev_state[i].tx_enabled ||
159 !tracker->netdev_state[i].link_up)
160 ports[(*num_disabled)++] = i;
161 }
162 }
163
mlx5_infer_tx_enabled(struct lag_tracker * tracker,u8 num_ports,u8 * ports,int * num_enabled)164 void mlx5_infer_tx_enabled(struct lag_tracker *tracker, u8 num_ports,
165 u8 *ports, int *num_enabled)
166 {
167 int i;
168
169 *num_enabled = 0;
170 for (i = 0; i < num_ports; i++) {
171 if (tracker->netdev_state[i].tx_enabled &&
172 tracker->netdev_state[i].link_up)
173 ports[(*num_enabled)++] = i;
174 }
175
176 if (*num_enabled == 0)
177 mlx5_infer_tx_disabled(tracker, num_ports, ports, num_enabled);
178 }
179
mlx5_lag_print_mapping(struct mlx5_core_dev * dev,struct mlx5_lag * ldev,struct lag_tracker * tracker,unsigned long flags)180 static void mlx5_lag_print_mapping(struct mlx5_core_dev *dev,
181 struct mlx5_lag *ldev,
182 struct lag_tracker *tracker,
183 unsigned long flags)
184 {
185 char buf[MLX5_MAX_PORTS * 10 + 1] = {};
186 u8 enabled_ports[MLX5_MAX_PORTS] = {};
187 int written = 0;
188 int num_enabled;
189 int idx;
190 int err;
191 int i;
192 int j;
193
194 if (test_bit(MLX5_LAG_MODE_FLAG_HASH_BASED, &flags)) {
195 mlx5_infer_tx_enabled(tracker, ldev->ports, enabled_ports,
196 &num_enabled);
197 for (i = 0; i < num_enabled; i++) {
198 err = scnprintf(buf + written, 4, "%d, ", enabled_ports[i] + 1);
199 if (err != 3)
200 return;
201 written += err;
202 }
203 buf[written - 2] = 0;
204 mlx5_core_info(dev, "lag map active ports: %s\n", buf);
205 } else {
206 for (i = 0; i < ldev->ports; i++) {
207 for (j = 0; j < ldev->buckets; j++) {
208 idx = i * ldev->buckets + j;
209 err = scnprintf(buf + written, 10,
210 " port %d:%d", i + 1, ldev->v2p_map[idx]);
211 if (err != 9)
212 return;
213 written += err;
214 }
215 }
216 mlx5_core_info(dev, "lag map:%s\n", buf);
217 }
218 }
219
220 static int mlx5_lag_netdev_event(struct notifier_block *this,
221 unsigned long event, void *ptr);
222 static void mlx5_do_bond_work(struct work_struct *work);
223
mlx5_ldev_free(struct kref * ref)224 static void mlx5_ldev_free(struct kref *ref)
225 {
226 struct mlx5_lag *ldev = container_of(ref, struct mlx5_lag, ref);
227
228 if (ldev->nb.notifier_call)
229 unregister_netdevice_notifier_net(&init_net, &ldev->nb);
230 mlx5_lag_mp_cleanup(ldev);
231 cancel_delayed_work_sync(&ldev->bond_work);
232 destroy_workqueue(ldev->wq);
233 mutex_destroy(&ldev->lock);
234 kfree(ldev);
235 }
236
mlx5_ldev_put(struct mlx5_lag * ldev)237 static void mlx5_ldev_put(struct mlx5_lag *ldev)
238 {
239 kref_put(&ldev->ref, mlx5_ldev_free);
240 }
241
mlx5_ldev_get(struct mlx5_lag * ldev)242 static void mlx5_ldev_get(struct mlx5_lag *ldev)
243 {
244 kref_get(&ldev->ref);
245 }
246
mlx5_lag_dev_alloc(struct mlx5_core_dev * dev)247 static struct mlx5_lag *mlx5_lag_dev_alloc(struct mlx5_core_dev *dev)
248 {
249 struct mlx5_lag *ldev;
250 int err;
251
252 ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
253 if (!ldev)
254 return NULL;
255
256 ldev->wq = create_singlethread_workqueue("mlx5_lag");
257 if (!ldev->wq) {
258 kfree(ldev);
259 return NULL;
260 }
261
262 kref_init(&ldev->ref);
263 mutex_init(&ldev->lock);
264 INIT_DELAYED_WORK(&ldev->bond_work, mlx5_do_bond_work);
265
266 ldev->nb.notifier_call = mlx5_lag_netdev_event;
267 if (register_netdevice_notifier_net(&init_net, &ldev->nb)) {
268 ldev->nb.notifier_call = NULL;
269 mlx5_core_err(dev, "Failed to register LAG netdev notifier\n");
270 }
271 ldev->mode = MLX5_LAG_MODE_NONE;
272
273 err = mlx5_lag_mp_init(ldev);
274 if (err)
275 mlx5_core_err(dev, "Failed to init multipath lag err=%d\n",
276 err);
277
278 ldev->ports = MLX5_CAP_GEN(dev, num_lag_ports);
279 ldev->buckets = 1;
280
281 return ldev;
282 }
283
mlx5_lag_dev_get_netdev_idx(struct mlx5_lag * ldev,struct net_device * ndev)284 int mlx5_lag_dev_get_netdev_idx(struct mlx5_lag *ldev,
285 struct net_device *ndev)
286 {
287 int i;
288
289 for (i = 0; i < ldev->ports; i++)
290 if (ldev->pf[i].netdev == ndev)
291 return i;
292
293 return -ENOENT;
294 }
295
__mlx5_lag_is_roce(struct mlx5_lag * ldev)296 static bool __mlx5_lag_is_roce(struct mlx5_lag *ldev)
297 {
298 return ldev->mode == MLX5_LAG_MODE_ROCE;
299 }
300
__mlx5_lag_is_sriov(struct mlx5_lag * ldev)301 static bool __mlx5_lag_is_sriov(struct mlx5_lag *ldev)
302 {
303 return ldev->mode == MLX5_LAG_MODE_SRIOV;
304 }
305
306 /* Create a mapping between steering slots and active ports.
307 * As we have ldev->buckets slots per port first assume the native
308 * mapping should be used.
309 * If there are ports that are disabled fill the relevant slots
310 * with mapping that points to active ports.
311 */
mlx5_infer_tx_affinity_mapping(struct lag_tracker * tracker,u8 num_ports,u8 buckets,u8 * ports)312 static void mlx5_infer_tx_affinity_mapping(struct lag_tracker *tracker,
313 u8 num_ports,
314 u8 buckets,
315 u8 *ports)
316 {
317 int disabled[MLX5_MAX_PORTS] = {};
318 int enabled[MLX5_MAX_PORTS] = {};
319 int disabled_ports_num = 0;
320 int enabled_ports_num = 0;
321 int idx;
322 u32 rand;
323 int i;
324 int j;
325
326 for (i = 0; i < num_ports; i++) {
327 if (tracker->netdev_state[i].tx_enabled &&
328 tracker->netdev_state[i].link_up)
329 enabled[enabled_ports_num++] = i;
330 else
331 disabled[disabled_ports_num++] = i;
332 }
333
334 /* Use native mapping by default where each port's buckets
335 * point the native port: 1 1 1 .. 1 2 2 2 ... 2 3 3 3 ... 3 etc
336 */
337 for (i = 0; i < num_ports; i++)
338 for (j = 0; j < buckets; j++) {
339 idx = i * buckets + j;
340 ports[idx] = MLX5_LAG_EGRESS_PORT_1 + i;
341 }
342
343 /* If all ports are disabled/enabled keep native mapping */
344 if (enabled_ports_num == num_ports ||
345 disabled_ports_num == num_ports)
346 return;
347
348 /* Go over the disabled ports and for each assign a random active port */
349 for (i = 0; i < disabled_ports_num; i++) {
350 for (j = 0; j < buckets; j++) {
351 get_random_bytes(&rand, 4);
352 ports[disabled[i] * buckets + j] = enabled[rand % enabled_ports_num] + 1;
353 }
354 }
355 }
356
mlx5_lag_has_drop_rule(struct mlx5_lag * ldev)357 static bool mlx5_lag_has_drop_rule(struct mlx5_lag *ldev)
358 {
359 int i;
360
361 for (i = 0; i < ldev->ports; i++)
362 if (ldev->pf[i].has_drop)
363 return true;
364 return false;
365 }
366
mlx5_lag_drop_rule_cleanup(struct mlx5_lag * ldev)367 static void mlx5_lag_drop_rule_cleanup(struct mlx5_lag *ldev)
368 {
369 int i;
370
371 for (i = 0; i < ldev->ports; i++) {
372 if (!ldev->pf[i].has_drop)
373 continue;
374
375 mlx5_esw_acl_ingress_vport_drop_rule_destroy(ldev->pf[i].dev->priv.eswitch,
376 MLX5_VPORT_UPLINK);
377 ldev->pf[i].has_drop = false;
378 }
379 }
380
mlx5_lag_drop_rule_setup(struct mlx5_lag * ldev,struct lag_tracker * tracker)381 static void mlx5_lag_drop_rule_setup(struct mlx5_lag *ldev,
382 struct lag_tracker *tracker)
383 {
384 u8 disabled_ports[MLX5_MAX_PORTS] = {};
385 struct mlx5_core_dev *dev;
386 int disabled_index;
387 int num_disabled;
388 int err;
389 int i;
390
391 /* First delete the current drop rule so there won't be any dropped
392 * packets
393 */
394 mlx5_lag_drop_rule_cleanup(ldev);
395
396 if (!ldev->tracker.has_inactive)
397 return;
398
399 mlx5_infer_tx_disabled(tracker, ldev->ports, disabled_ports, &num_disabled);
400
401 for (i = 0; i < num_disabled; i++) {
402 disabled_index = disabled_ports[i];
403 dev = ldev->pf[disabled_index].dev;
404 err = mlx5_esw_acl_ingress_vport_drop_rule_create(dev->priv.eswitch,
405 MLX5_VPORT_UPLINK);
406 if (!err)
407 ldev->pf[disabled_index].has_drop = true;
408 else
409 mlx5_core_err(dev,
410 "Failed to create lag drop rule, error: %d", err);
411 }
412 }
413
mlx5_cmd_modify_active_port(struct mlx5_core_dev * dev,u8 ports)414 static int mlx5_cmd_modify_active_port(struct mlx5_core_dev *dev, u8 ports)
415 {
416 u32 in[MLX5_ST_SZ_DW(modify_lag_in)] = {};
417 void *lag_ctx;
418
419 lag_ctx = MLX5_ADDR_OF(modify_lag_in, in, ctx);
420
421 MLX5_SET(modify_lag_in, in, opcode, MLX5_CMD_OP_MODIFY_LAG);
422 MLX5_SET(modify_lag_in, in, field_select, 0x2);
423
424 MLX5_SET(lagc, lag_ctx, active_port, ports);
425
426 return mlx5_cmd_exec_in(dev, modify_lag, in);
427 }
428
_mlx5_modify_lag(struct mlx5_lag * ldev,u8 * ports)429 static int _mlx5_modify_lag(struct mlx5_lag *ldev, u8 *ports)
430 {
431 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
432 u8 active_ports;
433 int ret;
434
435 if (test_bit(MLX5_LAG_MODE_FLAG_HASH_BASED, &ldev->mode_flags)) {
436 ret = mlx5_lag_port_sel_modify(ldev, ports);
437 if (ret ||
438 !MLX5_CAP_PORT_SELECTION(dev0, port_select_flow_table_bypass))
439 return ret;
440
441 active_ports = lag_active_port_bits(ldev);
442
443 return mlx5_cmd_modify_active_port(dev0, active_ports);
444 }
445 return mlx5_cmd_modify_lag(dev0, ldev->ports, ports);
446 }
447
mlx5_lag_active_backup_get_netdev(struct mlx5_core_dev * dev)448 static struct net_device *mlx5_lag_active_backup_get_netdev(struct mlx5_core_dev *dev)
449 {
450 struct net_device *ndev = NULL;
451 struct mlx5_lag *ldev;
452 unsigned long flags;
453 int i;
454
455 spin_lock_irqsave(&lag_lock, flags);
456 ldev = mlx5_lag_dev(dev);
457
458 if (!ldev)
459 goto unlock;
460
461 for (i = 0; i < ldev->ports; i++)
462 if (ldev->tracker.netdev_state[i].tx_enabled)
463 ndev = ldev->pf[i].netdev;
464 if (!ndev)
465 ndev = ldev->pf[ldev->ports - 1].netdev;
466
467 if (ndev)
468 dev_hold(ndev);
469
470 unlock:
471 spin_unlock_irqrestore(&lag_lock, flags);
472
473 return ndev;
474 }
475
mlx5_modify_lag(struct mlx5_lag * ldev,struct lag_tracker * tracker)476 void mlx5_modify_lag(struct mlx5_lag *ldev,
477 struct lag_tracker *tracker)
478 {
479 u8 ports[MLX5_MAX_PORTS * MLX5_LAG_MAX_HASH_BUCKETS] = {};
480 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
481 int idx;
482 int err;
483 int i;
484 int j;
485
486 mlx5_infer_tx_affinity_mapping(tracker, ldev->ports, ldev->buckets, ports);
487
488 for (i = 0; i < ldev->ports; i++) {
489 for (j = 0; j < ldev->buckets; j++) {
490 idx = i * ldev->buckets + j;
491 if (ports[idx] == ldev->v2p_map[idx])
492 continue;
493 err = _mlx5_modify_lag(ldev, ports);
494 if (err) {
495 mlx5_core_err(dev0,
496 "Failed to modify LAG (%d)\n",
497 err);
498 return;
499 }
500 memcpy(ldev->v2p_map, ports, sizeof(ports));
501
502 mlx5_lag_print_mapping(dev0, ldev, tracker,
503 ldev->mode_flags);
504 break;
505 }
506 }
507
508 if (tracker->tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP) {
509 struct net_device *ndev = mlx5_lag_active_backup_get_netdev(dev0);
510
511 if(!(ldev->mode == MLX5_LAG_MODE_ROCE))
512 mlx5_lag_drop_rule_setup(ldev, tracker);
513 /** Only sriov and roce lag should have tracker->tx_type set so
514 * no need to check the mode
515 */
516 blocking_notifier_call_chain(&dev0->priv.lag_nh,
517 MLX5_DRIVER_EVENT_ACTIVE_BACKUP_LAG_CHANGE_LOWERSTATE,
518 ndev);
519 }
520 }
521
mlx5_lag_set_port_sel_mode_roce(struct mlx5_lag * ldev,unsigned long * flags)522 static int mlx5_lag_set_port_sel_mode_roce(struct mlx5_lag *ldev,
523 unsigned long *flags)
524 {
525 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
526
527 if (!MLX5_CAP_PORT_SELECTION(dev0, port_select_flow_table)) {
528 if (ldev->ports > 2)
529 return -EINVAL;
530 return 0;
531 }
532
533 if (ldev->ports > 2)
534 ldev->buckets = MLX5_LAG_MAX_HASH_BUCKETS;
535
536 set_bit(MLX5_LAG_MODE_FLAG_HASH_BASED, flags);
537
538 return 0;
539 }
540
mlx5_lag_set_port_sel_mode_offloads(struct mlx5_lag * ldev,struct lag_tracker * tracker,enum mlx5_lag_mode mode,unsigned long * flags)541 static void mlx5_lag_set_port_sel_mode_offloads(struct mlx5_lag *ldev,
542 struct lag_tracker *tracker,
543 enum mlx5_lag_mode mode,
544 unsigned long *flags)
545 {
546 struct lag_func *dev0 = &ldev->pf[MLX5_LAG_P1];
547
548 if (mode == MLX5_LAG_MODE_MPESW)
549 return;
550
551 if (MLX5_CAP_PORT_SELECTION(dev0->dev, port_select_flow_table) &&
552 tracker->tx_type == NETDEV_LAG_TX_TYPE_HASH) {
553 if (ldev->ports > 2)
554 ldev->buckets = MLX5_LAG_MAX_HASH_BUCKETS;
555 set_bit(MLX5_LAG_MODE_FLAG_HASH_BASED, flags);
556 }
557 }
558
mlx5_lag_set_flags(struct mlx5_lag * ldev,enum mlx5_lag_mode mode,struct lag_tracker * tracker,bool shared_fdb,unsigned long * flags)559 static int mlx5_lag_set_flags(struct mlx5_lag *ldev, enum mlx5_lag_mode mode,
560 struct lag_tracker *tracker, bool shared_fdb,
561 unsigned long *flags)
562 {
563 bool roce_lag = mode == MLX5_LAG_MODE_ROCE;
564
565 *flags = 0;
566 if (shared_fdb) {
567 set_bit(MLX5_LAG_MODE_FLAG_SHARED_FDB, flags);
568 set_bit(MLX5_LAG_MODE_FLAG_FDB_SEL_MODE_NATIVE, flags);
569 }
570
571 if (mode == MLX5_LAG_MODE_MPESW)
572 set_bit(MLX5_LAG_MODE_FLAG_FDB_SEL_MODE_NATIVE, flags);
573
574 if (roce_lag)
575 return mlx5_lag_set_port_sel_mode_roce(ldev, flags);
576
577 mlx5_lag_set_port_sel_mode_offloads(ldev, tracker, mode, flags);
578 return 0;
579 }
580
mlx5_get_str_port_sel_mode(enum mlx5_lag_mode mode,unsigned long flags)581 char *mlx5_get_str_port_sel_mode(enum mlx5_lag_mode mode, unsigned long flags)
582 {
583 int port_sel_mode = get_port_sel_mode(mode, flags);
584
585 switch (port_sel_mode) {
586 case MLX5_LAG_PORT_SELECT_MODE_QUEUE_AFFINITY: return "queue_affinity";
587 case MLX5_LAG_PORT_SELECT_MODE_PORT_SELECT_FT: return "hash";
588 case MLX5_LAG_PORT_SELECT_MODE_PORT_SELECT_MPESW: return "mpesw";
589 default: return "invalid";
590 }
591 }
592
mlx5_lag_create_single_fdb(struct mlx5_lag * ldev)593 static int mlx5_lag_create_single_fdb(struct mlx5_lag *ldev)
594 {
595 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
596 struct mlx5_eswitch *master_esw = dev0->priv.eswitch;
597 int err;
598 int i;
599
600 for (i = MLX5_LAG_P1 + 1; i < ldev->ports; i++) {
601 struct mlx5_eswitch *slave_esw = ldev->pf[i].dev->priv.eswitch;
602
603 err = mlx5_eswitch_offloads_single_fdb_add_one(master_esw,
604 slave_esw, ldev->ports);
605 if (err)
606 goto err;
607 }
608 return 0;
609 err:
610 for (; i > MLX5_LAG_P1; i--)
611 mlx5_eswitch_offloads_single_fdb_del_one(master_esw,
612 ldev->pf[i].dev->priv.eswitch);
613 return err;
614 }
615
mlx5_create_lag(struct mlx5_lag * ldev,struct lag_tracker * tracker,enum mlx5_lag_mode mode,unsigned long flags)616 static int mlx5_create_lag(struct mlx5_lag *ldev,
617 struct lag_tracker *tracker,
618 enum mlx5_lag_mode mode,
619 unsigned long flags)
620 {
621 bool shared_fdb = test_bit(MLX5_LAG_MODE_FLAG_SHARED_FDB, &flags);
622 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
623 u32 in[MLX5_ST_SZ_DW(destroy_lag_in)] = {};
624 int err;
625
626 if (tracker)
627 mlx5_lag_print_mapping(dev0, ldev, tracker, flags);
628 mlx5_core_info(dev0, "shared_fdb:%d mode:%s\n",
629 shared_fdb, mlx5_get_str_port_sel_mode(mode, flags));
630
631 err = mlx5_cmd_create_lag(dev0, ldev->v2p_map, mode, flags);
632 if (err) {
633 mlx5_core_err(dev0,
634 "Failed to create LAG (%d)\n",
635 err);
636 return err;
637 }
638
639 if (shared_fdb) {
640 err = mlx5_lag_create_single_fdb(ldev);
641 if (err)
642 mlx5_core_err(dev0, "Can't enable single FDB mode\n");
643 else
644 mlx5_core_info(dev0, "Operation mode is single FDB\n");
645 }
646
647 if (err) {
648 MLX5_SET(destroy_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_LAG);
649 if (mlx5_cmd_exec_in(dev0, destroy_lag, in))
650 mlx5_core_err(dev0,
651 "Failed to deactivate RoCE LAG; driver restart required\n");
652 }
653 BLOCKING_INIT_NOTIFIER_HEAD(&dev0->priv.lag_nh);
654
655 return err;
656 }
657
mlx5_activate_lag(struct mlx5_lag * ldev,struct lag_tracker * tracker,enum mlx5_lag_mode mode,bool shared_fdb)658 int mlx5_activate_lag(struct mlx5_lag *ldev,
659 struct lag_tracker *tracker,
660 enum mlx5_lag_mode mode,
661 bool shared_fdb)
662 {
663 bool roce_lag = mode == MLX5_LAG_MODE_ROCE;
664 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
665 unsigned long flags = 0;
666 int err;
667
668 err = mlx5_lag_set_flags(ldev, mode, tracker, shared_fdb, &flags);
669 if (err)
670 return err;
671
672 if (mode != MLX5_LAG_MODE_MPESW) {
673 mlx5_infer_tx_affinity_mapping(tracker, ldev->ports, ldev->buckets, ldev->v2p_map);
674 if (test_bit(MLX5_LAG_MODE_FLAG_HASH_BASED, &flags)) {
675 err = mlx5_lag_port_sel_create(ldev, tracker->hash_type,
676 ldev->v2p_map);
677 if (err) {
678 mlx5_core_err(dev0,
679 "Failed to create LAG port selection(%d)\n",
680 err);
681 return err;
682 }
683 }
684 }
685
686 err = mlx5_create_lag(ldev, tracker, mode, flags);
687 if (err) {
688 if (test_bit(MLX5_LAG_MODE_FLAG_HASH_BASED, &flags))
689 mlx5_lag_port_sel_destroy(ldev);
690 if (roce_lag)
691 mlx5_core_err(dev0,
692 "Failed to activate RoCE LAG\n");
693 else
694 mlx5_core_err(dev0,
695 "Failed to activate VF LAG\n"
696 "Make sure all VFs are unbound prior to VF LAG activation or deactivation\n");
697 return err;
698 }
699
700 if (tracker && tracker->tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP &&
701 !roce_lag)
702 mlx5_lag_drop_rule_setup(ldev, tracker);
703
704 ldev->mode = mode;
705 ldev->mode_flags = flags;
706 return 0;
707 }
708
mlx5_deactivate_lag(struct mlx5_lag * ldev)709 int mlx5_deactivate_lag(struct mlx5_lag *ldev)
710 {
711 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
712 struct mlx5_eswitch *master_esw = dev0->priv.eswitch;
713 u32 in[MLX5_ST_SZ_DW(destroy_lag_in)] = {};
714 bool roce_lag = __mlx5_lag_is_roce(ldev);
715 unsigned long flags = ldev->mode_flags;
716 int err;
717 int i;
718
719 ldev->mode = MLX5_LAG_MODE_NONE;
720 ldev->mode_flags = 0;
721 mlx5_lag_mp_reset(ldev);
722
723 if (test_bit(MLX5_LAG_MODE_FLAG_SHARED_FDB, &flags)) {
724 for (i = MLX5_LAG_P1 + 1; i < ldev->ports; i++)
725 mlx5_eswitch_offloads_single_fdb_del_one(master_esw,
726 ldev->pf[i].dev->priv.eswitch);
727 clear_bit(MLX5_LAG_MODE_FLAG_SHARED_FDB, &flags);
728 }
729
730 MLX5_SET(destroy_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_LAG);
731 err = mlx5_cmd_exec_in(dev0, destroy_lag, in);
732 if (err) {
733 if (roce_lag) {
734 mlx5_core_err(dev0,
735 "Failed to deactivate RoCE LAG; driver restart required\n");
736 } else {
737 mlx5_core_err(dev0,
738 "Failed to deactivate VF LAG; driver restart required\n"
739 "Make sure all VFs are unbound prior to VF LAG activation or deactivation\n");
740 }
741 return err;
742 }
743
744 if (test_bit(MLX5_LAG_MODE_FLAG_HASH_BASED, &flags)) {
745 mlx5_lag_port_sel_destroy(ldev);
746 ldev->buckets = 1;
747 }
748 if (mlx5_lag_has_drop_rule(ldev))
749 mlx5_lag_drop_rule_cleanup(ldev);
750
751 return 0;
752 }
753
mlx5_lag_check_prereq(struct mlx5_lag * ldev)754 bool mlx5_lag_check_prereq(struct mlx5_lag *ldev)
755 {
756 #ifdef CONFIG_MLX5_ESWITCH
757 struct mlx5_core_dev *dev;
758 u8 mode;
759 #endif
760 bool roce_support;
761 int i;
762
763 for (i = 0; i < ldev->ports; i++)
764 if (!ldev->pf[i].dev)
765 return false;
766
767 #ifdef CONFIG_MLX5_ESWITCH
768 for (i = 0; i < ldev->ports; i++) {
769 dev = ldev->pf[i].dev;
770 if (mlx5_eswitch_num_vfs(dev->priv.eswitch) && !is_mdev_switchdev_mode(dev))
771 return false;
772 }
773
774 dev = ldev->pf[MLX5_LAG_P1].dev;
775 mode = mlx5_eswitch_mode(dev);
776 for (i = 0; i < ldev->ports; i++)
777 if (mlx5_eswitch_mode(ldev->pf[i].dev) != mode)
778 return false;
779
780 #else
781 for (i = 0; i < ldev->ports; i++)
782 if (mlx5_sriov_is_enabled(ldev->pf[i].dev))
783 return false;
784 #endif
785 roce_support = mlx5_get_roce_state(ldev->pf[MLX5_LAG_P1].dev);
786 for (i = 1; i < ldev->ports; i++)
787 if (mlx5_get_roce_state(ldev->pf[i].dev) != roce_support)
788 return false;
789
790 return true;
791 }
792
mlx5_lag_add_devices(struct mlx5_lag * ldev)793 void mlx5_lag_add_devices(struct mlx5_lag *ldev)
794 {
795 int i;
796
797 for (i = 0; i < ldev->ports; i++) {
798 if (!ldev->pf[i].dev)
799 continue;
800
801 if (ldev->pf[i].dev->priv.flags &
802 MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)
803 continue;
804
805 ldev->pf[i].dev->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
806 mlx5_rescan_drivers_locked(ldev->pf[i].dev);
807 }
808 }
809
mlx5_lag_remove_devices(struct mlx5_lag * ldev)810 void mlx5_lag_remove_devices(struct mlx5_lag *ldev)
811 {
812 int i;
813
814 for (i = 0; i < ldev->ports; i++) {
815 if (!ldev->pf[i].dev)
816 continue;
817
818 if (ldev->pf[i].dev->priv.flags &
819 MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)
820 continue;
821
822 ldev->pf[i].dev->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
823 mlx5_rescan_drivers_locked(ldev->pf[i].dev);
824 }
825 }
826
mlx5_disable_lag(struct mlx5_lag * ldev)827 void mlx5_disable_lag(struct mlx5_lag *ldev)
828 {
829 bool shared_fdb = test_bit(MLX5_LAG_MODE_FLAG_SHARED_FDB, &ldev->mode_flags);
830 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
831 bool roce_lag;
832 int err;
833 int i;
834
835 roce_lag = __mlx5_lag_is_roce(ldev);
836
837 if (shared_fdb) {
838 mlx5_lag_remove_devices(ldev);
839 } else if (roce_lag) {
840 if (!(dev0->priv.flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)) {
841 dev0->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
842 mlx5_rescan_drivers_locked(dev0);
843 }
844 for (i = 1; i < ldev->ports; i++)
845 mlx5_nic_vport_disable_roce(ldev->pf[i].dev);
846 }
847
848 err = mlx5_deactivate_lag(ldev);
849 if (err)
850 return;
851
852 if (shared_fdb || roce_lag)
853 mlx5_lag_add_devices(ldev);
854
855 if (shared_fdb)
856 for (i = 0; i < ldev->ports; i++)
857 if (!(ldev->pf[i].dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV))
858 mlx5_eswitch_reload_ib_reps(ldev->pf[i].dev->priv.eswitch);
859 }
860
mlx5_shared_fdb_supported(struct mlx5_lag * ldev)861 static bool mlx5_shared_fdb_supported(struct mlx5_lag *ldev)
862 {
863 struct mlx5_core_dev *dev;
864 int i;
865
866 for (i = MLX5_LAG_P1 + 1; i < ldev->ports; i++) {
867 dev = ldev->pf[i].dev;
868 if (is_mdev_switchdev_mode(dev) &&
869 mlx5_eswitch_vport_match_metadata_enabled(dev->priv.eswitch) &&
870 MLX5_CAP_GEN(dev, lag_native_fdb_selection) &&
871 MLX5_CAP_ESW(dev, root_ft_on_other_esw) &&
872 mlx5_eswitch_get_npeers(dev->priv.eswitch) ==
873 MLX5_CAP_GEN(dev, num_lag_ports) - 1)
874 continue;
875 return false;
876 }
877
878 dev = ldev->pf[MLX5_LAG_P1].dev;
879 if (is_mdev_switchdev_mode(dev) &&
880 mlx5_eswitch_vport_match_metadata_enabled(dev->priv.eswitch) &&
881 mlx5_esw_offloads_devcom_is_ready(dev->priv.eswitch) &&
882 MLX5_CAP_ESW(dev, esw_shared_ingress_acl) &&
883 mlx5_eswitch_get_npeers(dev->priv.eswitch) == MLX5_CAP_GEN(dev, num_lag_ports) - 1)
884 return true;
885
886 return false;
887 }
888
mlx5_lag_is_roce_lag(struct mlx5_lag * ldev)889 static bool mlx5_lag_is_roce_lag(struct mlx5_lag *ldev)
890 {
891 bool roce_lag = true;
892 int i;
893
894 for (i = 0; i < ldev->ports; i++)
895 roce_lag = roce_lag && !mlx5_sriov_is_enabled(ldev->pf[i].dev);
896
897 #ifdef CONFIG_MLX5_ESWITCH
898 for (i = 0; i < ldev->ports; i++)
899 roce_lag = roce_lag && is_mdev_legacy_mode(ldev->pf[i].dev);
900 #endif
901
902 return roce_lag;
903 }
904
mlx5_lag_should_modify_lag(struct mlx5_lag * ldev,bool do_bond)905 static bool mlx5_lag_should_modify_lag(struct mlx5_lag *ldev, bool do_bond)
906 {
907 return do_bond && __mlx5_lag_is_active(ldev) &&
908 ldev->mode != MLX5_LAG_MODE_MPESW;
909 }
910
mlx5_lag_should_disable_lag(struct mlx5_lag * ldev,bool do_bond)911 static bool mlx5_lag_should_disable_lag(struct mlx5_lag *ldev, bool do_bond)
912 {
913 return !do_bond && __mlx5_lag_is_active(ldev) &&
914 ldev->mode != MLX5_LAG_MODE_MPESW;
915 }
916
mlx5_do_bond(struct mlx5_lag * ldev)917 static void mlx5_do_bond(struct mlx5_lag *ldev)
918 {
919 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
920 struct lag_tracker tracker = { };
921 bool do_bond, roce_lag;
922 int err;
923 int i;
924
925 if (!mlx5_lag_is_ready(ldev)) {
926 do_bond = false;
927 } else {
928 /* VF LAG is in multipath mode, ignore bond change requests */
929 if (mlx5_lag_is_multipath(dev0))
930 return;
931
932 tracker = ldev->tracker;
933
934 do_bond = tracker.is_bonded && mlx5_lag_check_prereq(ldev);
935 }
936
937 if (do_bond && !__mlx5_lag_is_active(ldev)) {
938 bool shared_fdb = mlx5_shared_fdb_supported(ldev);
939
940 roce_lag = mlx5_lag_is_roce_lag(ldev);
941
942 if (shared_fdb || roce_lag)
943 mlx5_lag_remove_devices(ldev);
944
945 err = mlx5_activate_lag(ldev, &tracker,
946 roce_lag ? MLX5_LAG_MODE_ROCE :
947 MLX5_LAG_MODE_SRIOV,
948 shared_fdb);
949 if (err) {
950 if (shared_fdb || roce_lag)
951 mlx5_lag_add_devices(ldev);
952
953 return;
954 } else if (roce_lag) {
955 dev0->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
956 mlx5_rescan_drivers_locked(dev0);
957 for (i = 1; i < ldev->ports; i++) {
958 if (mlx5_get_roce_state(ldev->pf[i].dev))
959 mlx5_nic_vport_enable_roce(ldev->pf[i].dev);
960 }
961 } else if (shared_fdb) {
962 int i;
963
964 dev0->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
965 mlx5_rescan_drivers_locked(dev0);
966
967 for (i = 0; i < ldev->ports; i++) {
968 err = mlx5_eswitch_reload_ib_reps(ldev->pf[i].dev->priv.eswitch);
969 if (err)
970 break;
971 }
972
973 if (err) {
974 dev0->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
975 mlx5_rescan_drivers_locked(dev0);
976 mlx5_deactivate_lag(ldev);
977 mlx5_lag_add_devices(ldev);
978 for (i = 0; i < ldev->ports; i++)
979 mlx5_eswitch_reload_ib_reps(ldev->pf[i].dev->priv.eswitch);
980 mlx5_core_err(dev0, "Failed to enable lag\n");
981 return;
982 }
983 }
984 } else if (mlx5_lag_should_modify_lag(ldev, do_bond)) {
985 mlx5_modify_lag(ldev, &tracker);
986 } else if (mlx5_lag_should_disable_lag(ldev, do_bond)) {
987 mlx5_disable_lag(ldev);
988 }
989 }
990
991 /* The last mdev to unregister will destroy the workqueue before removing the
992 * devcom component, and as all the mdevs use the same devcom component we are
993 * guaranteed that the devcom is valid while the calling work is running.
994 */
mlx5_lag_get_devcom_comp(struct mlx5_lag * ldev)995 struct mlx5_devcom_comp_dev *mlx5_lag_get_devcom_comp(struct mlx5_lag *ldev)
996 {
997 struct mlx5_devcom_comp_dev *devcom = NULL;
998 int i;
999
1000 mutex_lock(&ldev->lock);
1001 for (i = 0; i < ldev->ports; i++) {
1002 if (ldev->pf[i].dev) {
1003 devcom = ldev->pf[i].dev->priv.hca_devcom_comp;
1004 break;
1005 }
1006 }
1007 mutex_unlock(&ldev->lock);
1008 return devcom;
1009 }
1010
mlx5_queue_bond_work(struct mlx5_lag * ldev,unsigned long delay)1011 static void mlx5_queue_bond_work(struct mlx5_lag *ldev, unsigned long delay)
1012 {
1013 queue_delayed_work(ldev->wq, &ldev->bond_work, delay);
1014 }
1015
mlx5_do_bond_work(struct work_struct * work)1016 static void mlx5_do_bond_work(struct work_struct *work)
1017 {
1018 struct delayed_work *delayed_work = to_delayed_work(work);
1019 struct mlx5_lag *ldev = container_of(delayed_work, struct mlx5_lag,
1020 bond_work);
1021 struct mlx5_devcom_comp_dev *devcom;
1022 int status;
1023
1024 devcom = mlx5_lag_get_devcom_comp(ldev);
1025 if (!devcom)
1026 return;
1027
1028 status = mlx5_devcom_comp_trylock(devcom);
1029 if (!status) {
1030 mlx5_queue_bond_work(ldev, HZ);
1031 return;
1032 }
1033
1034 mutex_lock(&ldev->lock);
1035 if (ldev->mode_changes_in_progress) {
1036 mutex_unlock(&ldev->lock);
1037 mlx5_devcom_comp_unlock(devcom);
1038 mlx5_queue_bond_work(ldev, HZ);
1039 return;
1040 }
1041
1042 mlx5_do_bond(ldev);
1043 mutex_unlock(&ldev->lock);
1044 mlx5_devcom_comp_unlock(devcom);
1045 }
1046
mlx5_handle_changeupper_event(struct mlx5_lag * ldev,struct lag_tracker * tracker,struct netdev_notifier_changeupper_info * info)1047 static int mlx5_handle_changeupper_event(struct mlx5_lag *ldev,
1048 struct lag_tracker *tracker,
1049 struct netdev_notifier_changeupper_info *info)
1050 {
1051 struct net_device *upper = info->upper_dev, *ndev_tmp;
1052 struct netdev_lag_upper_info *lag_upper_info = NULL;
1053 bool is_bonded, is_in_lag, mode_supported;
1054 bool has_inactive = 0;
1055 struct slave *slave;
1056 u8 bond_status = 0;
1057 int num_slaves = 0;
1058 int changed = 0;
1059 int idx;
1060
1061 if (!netif_is_lag_master(upper))
1062 return 0;
1063
1064 if (info->linking)
1065 lag_upper_info = info->upper_info;
1066
1067 /* The event may still be of interest if the slave does not belong to
1068 * us, but is enslaved to a master which has one or more of our netdevs
1069 * as slaves (e.g., if a new slave is added to a master that bonds two
1070 * of our netdevs, we should unbond).
1071 */
1072 rcu_read_lock();
1073 for_each_netdev_in_bond_rcu(upper, ndev_tmp) {
1074 idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev_tmp);
1075 if (idx >= 0) {
1076 slave = bond_slave_get_rcu(ndev_tmp);
1077 if (slave)
1078 has_inactive |= bond_is_slave_inactive(slave);
1079 bond_status |= (1 << idx);
1080 }
1081
1082 num_slaves++;
1083 }
1084 rcu_read_unlock();
1085
1086 /* None of this lagdev's netdevs are slaves of this master. */
1087 if (!(bond_status & GENMASK(ldev->ports - 1, 0)))
1088 return 0;
1089
1090 if (lag_upper_info) {
1091 tracker->tx_type = lag_upper_info->tx_type;
1092 tracker->hash_type = lag_upper_info->hash_type;
1093 }
1094
1095 tracker->has_inactive = has_inactive;
1096 /* Determine bonding status:
1097 * A device is considered bonded if both its physical ports are slaves
1098 * of the same lag master, and only them.
1099 */
1100 is_in_lag = num_slaves == ldev->ports &&
1101 bond_status == GENMASK(ldev->ports - 1, 0);
1102
1103 /* Lag mode must be activebackup or hash. */
1104 mode_supported = tracker->tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP ||
1105 tracker->tx_type == NETDEV_LAG_TX_TYPE_HASH;
1106
1107 is_bonded = is_in_lag && mode_supported;
1108 if (tracker->is_bonded != is_bonded) {
1109 tracker->is_bonded = is_bonded;
1110 changed = 1;
1111 }
1112
1113 if (!is_in_lag)
1114 return changed;
1115
1116 if (!mlx5_lag_is_ready(ldev))
1117 NL_SET_ERR_MSG_MOD(info->info.extack,
1118 "Can't activate LAG offload, PF is configured with more than 64 VFs");
1119 else if (!mode_supported)
1120 NL_SET_ERR_MSG_MOD(info->info.extack,
1121 "Can't activate LAG offload, TX type isn't supported");
1122
1123 return changed;
1124 }
1125
mlx5_handle_changelowerstate_event(struct mlx5_lag * ldev,struct lag_tracker * tracker,struct net_device * ndev,struct netdev_notifier_changelowerstate_info * info)1126 static int mlx5_handle_changelowerstate_event(struct mlx5_lag *ldev,
1127 struct lag_tracker *tracker,
1128 struct net_device *ndev,
1129 struct netdev_notifier_changelowerstate_info *info)
1130 {
1131 struct netdev_lag_lower_state_info *lag_lower_info;
1132 int idx;
1133
1134 if (!netif_is_lag_port(ndev))
1135 return 0;
1136
1137 idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev);
1138 if (idx < 0)
1139 return 0;
1140
1141 /* This information is used to determine virtual to physical
1142 * port mapping.
1143 */
1144 lag_lower_info = info->lower_state_info;
1145 if (!lag_lower_info)
1146 return 0;
1147
1148 tracker->netdev_state[idx] = *lag_lower_info;
1149
1150 return 1;
1151 }
1152
mlx5_handle_changeinfodata_event(struct mlx5_lag * ldev,struct lag_tracker * tracker,struct net_device * ndev)1153 static int mlx5_handle_changeinfodata_event(struct mlx5_lag *ldev,
1154 struct lag_tracker *tracker,
1155 struct net_device *ndev)
1156 {
1157 struct net_device *ndev_tmp;
1158 struct slave *slave;
1159 bool has_inactive = 0;
1160 int idx;
1161
1162 if (!netif_is_lag_master(ndev))
1163 return 0;
1164
1165 rcu_read_lock();
1166 for_each_netdev_in_bond_rcu(ndev, ndev_tmp) {
1167 idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev_tmp);
1168 if (idx < 0)
1169 continue;
1170
1171 slave = bond_slave_get_rcu(ndev_tmp);
1172 if (slave)
1173 has_inactive |= bond_is_slave_inactive(slave);
1174 }
1175 rcu_read_unlock();
1176
1177 if (tracker->has_inactive == has_inactive)
1178 return 0;
1179
1180 tracker->has_inactive = has_inactive;
1181
1182 return 1;
1183 }
1184
1185 /* this handler is always registered to netdev events */
mlx5_lag_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)1186 static int mlx5_lag_netdev_event(struct notifier_block *this,
1187 unsigned long event, void *ptr)
1188 {
1189 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1190 struct lag_tracker tracker;
1191 struct mlx5_lag *ldev;
1192 int changed = 0;
1193
1194 if (event != NETDEV_CHANGEUPPER &&
1195 event != NETDEV_CHANGELOWERSTATE &&
1196 event != NETDEV_CHANGEINFODATA)
1197 return NOTIFY_DONE;
1198
1199 ldev = container_of(this, struct mlx5_lag, nb);
1200
1201 tracker = ldev->tracker;
1202
1203 switch (event) {
1204 case NETDEV_CHANGEUPPER:
1205 changed = mlx5_handle_changeupper_event(ldev, &tracker, ptr);
1206 break;
1207 case NETDEV_CHANGELOWERSTATE:
1208 changed = mlx5_handle_changelowerstate_event(ldev, &tracker,
1209 ndev, ptr);
1210 break;
1211 case NETDEV_CHANGEINFODATA:
1212 changed = mlx5_handle_changeinfodata_event(ldev, &tracker, ndev);
1213 break;
1214 }
1215
1216 ldev->tracker = tracker;
1217
1218 if (changed)
1219 mlx5_queue_bond_work(ldev, 0);
1220
1221 return NOTIFY_DONE;
1222 }
1223
mlx5_ldev_add_netdev(struct mlx5_lag * ldev,struct mlx5_core_dev * dev,struct net_device * netdev)1224 static void mlx5_ldev_add_netdev(struct mlx5_lag *ldev,
1225 struct mlx5_core_dev *dev,
1226 struct net_device *netdev)
1227 {
1228 unsigned int fn = mlx5_get_dev_index(dev);
1229 unsigned long flags;
1230
1231 if (fn >= ldev->ports)
1232 return;
1233
1234 spin_lock_irqsave(&lag_lock, flags);
1235 ldev->pf[fn].netdev = netdev;
1236 ldev->tracker.netdev_state[fn].link_up = 0;
1237 ldev->tracker.netdev_state[fn].tx_enabled = 0;
1238 spin_unlock_irqrestore(&lag_lock, flags);
1239 }
1240
mlx5_ldev_remove_netdev(struct mlx5_lag * ldev,struct net_device * netdev)1241 static void mlx5_ldev_remove_netdev(struct mlx5_lag *ldev,
1242 struct net_device *netdev)
1243 {
1244 unsigned long flags;
1245 int i;
1246
1247 spin_lock_irqsave(&lag_lock, flags);
1248 for (i = 0; i < ldev->ports; i++) {
1249 if (ldev->pf[i].netdev == netdev) {
1250 ldev->pf[i].netdev = NULL;
1251 break;
1252 }
1253 }
1254 spin_unlock_irqrestore(&lag_lock, flags);
1255 }
1256
mlx5_ldev_add_mdev(struct mlx5_lag * ldev,struct mlx5_core_dev * dev)1257 static void mlx5_ldev_add_mdev(struct mlx5_lag *ldev,
1258 struct mlx5_core_dev *dev)
1259 {
1260 unsigned int fn = mlx5_get_dev_index(dev);
1261
1262 if (fn >= ldev->ports)
1263 return;
1264
1265 ldev->pf[fn].dev = dev;
1266 dev->priv.lag = ldev;
1267 }
1268
mlx5_ldev_remove_mdev(struct mlx5_lag * ldev,struct mlx5_core_dev * dev)1269 static void mlx5_ldev_remove_mdev(struct mlx5_lag *ldev,
1270 struct mlx5_core_dev *dev)
1271 {
1272 int i;
1273
1274 for (i = 0; i < ldev->ports; i++)
1275 if (ldev->pf[i].dev == dev)
1276 break;
1277
1278 if (i == ldev->ports)
1279 return;
1280
1281 ldev->pf[i].dev = NULL;
1282 dev->priv.lag = NULL;
1283 }
1284
1285 /* Must be called with HCA devcom component lock held */
__mlx5_lag_dev_add_mdev(struct mlx5_core_dev * dev)1286 static int __mlx5_lag_dev_add_mdev(struct mlx5_core_dev *dev)
1287 {
1288 struct mlx5_devcom_comp_dev *pos = NULL;
1289 struct mlx5_lag *ldev = NULL;
1290 struct mlx5_core_dev *tmp_dev;
1291
1292 tmp_dev = mlx5_devcom_get_next_peer_data(dev->priv.hca_devcom_comp, &pos);
1293 if (tmp_dev)
1294 ldev = mlx5_lag_dev(tmp_dev);
1295
1296 if (!ldev) {
1297 ldev = mlx5_lag_dev_alloc(dev);
1298 if (!ldev) {
1299 mlx5_core_err(dev, "Failed to alloc lag dev\n");
1300 return 0;
1301 }
1302 mlx5_ldev_add_mdev(ldev, dev);
1303 return 0;
1304 }
1305
1306 mutex_lock(&ldev->lock);
1307 if (ldev->mode_changes_in_progress) {
1308 mutex_unlock(&ldev->lock);
1309 return -EAGAIN;
1310 }
1311 mlx5_ldev_get(ldev);
1312 mlx5_ldev_add_mdev(ldev, dev);
1313 mutex_unlock(&ldev->lock);
1314
1315 return 0;
1316 }
1317
mlx5_lag_remove_mdev(struct mlx5_core_dev * dev)1318 void mlx5_lag_remove_mdev(struct mlx5_core_dev *dev)
1319 {
1320 struct mlx5_lag *ldev;
1321
1322 ldev = mlx5_lag_dev(dev);
1323 if (!ldev)
1324 return;
1325
1326 /* mdev is being removed, might as well remove debugfs
1327 * as early as possible.
1328 */
1329 mlx5_ldev_remove_debugfs(dev->priv.dbg.lag_debugfs);
1330 recheck:
1331 mutex_lock(&ldev->lock);
1332 if (ldev->mode_changes_in_progress) {
1333 mutex_unlock(&ldev->lock);
1334 msleep(100);
1335 goto recheck;
1336 }
1337 mlx5_ldev_remove_mdev(ldev, dev);
1338 mutex_unlock(&ldev->lock);
1339 mlx5_ldev_put(ldev);
1340 }
1341
mlx5_lag_add_mdev(struct mlx5_core_dev * dev)1342 void mlx5_lag_add_mdev(struct mlx5_core_dev *dev)
1343 {
1344 int err;
1345
1346 if (!mlx5_lag_is_supported(dev))
1347 return;
1348
1349 if (IS_ERR_OR_NULL(dev->priv.hca_devcom_comp))
1350 return;
1351
1352 recheck:
1353 mlx5_devcom_comp_lock(dev->priv.hca_devcom_comp);
1354 err = __mlx5_lag_dev_add_mdev(dev);
1355 mlx5_devcom_comp_unlock(dev->priv.hca_devcom_comp);
1356
1357 if (err) {
1358 msleep(100);
1359 goto recheck;
1360 }
1361 mlx5_ldev_add_debugfs(dev);
1362 }
1363
mlx5_lag_remove_netdev(struct mlx5_core_dev * dev,struct net_device * netdev)1364 void mlx5_lag_remove_netdev(struct mlx5_core_dev *dev,
1365 struct net_device *netdev)
1366 {
1367 struct mlx5_lag *ldev;
1368 bool lag_is_active;
1369
1370 ldev = mlx5_lag_dev(dev);
1371 if (!ldev)
1372 return;
1373
1374 mutex_lock(&ldev->lock);
1375 mlx5_ldev_remove_netdev(ldev, netdev);
1376 clear_bit(MLX5_LAG_FLAG_NDEVS_READY, &ldev->state_flags);
1377
1378 lag_is_active = __mlx5_lag_is_active(ldev);
1379 mutex_unlock(&ldev->lock);
1380
1381 if (lag_is_active)
1382 mlx5_queue_bond_work(ldev, 0);
1383 }
1384
mlx5_lag_add_netdev(struct mlx5_core_dev * dev,struct net_device * netdev)1385 void mlx5_lag_add_netdev(struct mlx5_core_dev *dev,
1386 struct net_device *netdev)
1387 {
1388 struct mlx5_lag *ldev;
1389 int i;
1390
1391 ldev = mlx5_lag_dev(dev);
1392 if (!ldev)
1393 return;
1394
1395 mutex_lock(&ldev->lock);
1396 mlx5_ldev_add_netdev(ldev, dev, netdev);
1397
1398 for (i = 0; i < ldev->ports; i++)
1399 if (!ldev->pf[i].netdev)
1400 break;
1401
1402 if (i >= ldev->ports)
1403 set_bit(MLX5_LAG_FLAG_NDEVS_READY, &ldev->state_flags);
1404 mutex_unlock(&ldev->lock);
1405 mlx5_queue_bond_work(ldev, 0);
1406 }
1407
mlx5_lag_is_roce(struct mlx5_core_dev * dev)1408 bool mlx5_lag_is_roce(struct mlx5_core_dev *dev)
1409 {
1410 struct mlx5_lag *ldev;
1411 unsigned long flags;
1412 bool res;
1413
1414 spin_lock_irqsave(&lag_lock, flags);
1415 ldev = mlx5_lag_dev(dev);
1416 res = ldev && __mlx5_lag_is_roce(ldev);
1417 spin_unlock_irqrestore(&lag_lock, flags);
1418
1419 return res;
1420 }
1421 EXPORT_SYMBOL(mlx5_lag_is_roce);
1422
mlx5_lag_is_active(struct mlx5_core_dev * dev)1423 bool mlx5_lag_is_active(struct mlx5_core_dev *dev)
1424 {
1425 struct mlx5_lag *ldev;
1426 unsigned long flags;
1427 bool res;
1428
1429 spin_lock_irqsave(&lag_lock, flags);
1430 ldev = mlx5_lag_dev(dev);
1431 res = ldev && __mlx5_lag_is_active(ldev);
1432 spin_unlock_irqrestore(&lag_lock, flags);
1433
1434 return res;
1435 }
1436 EXPORT_SYMBOL(mlx5_lag_is_active);
1437
mlx5_lag_mode_is_hash(struct mlx5_core_dev * dev)1438 bool mlx5_lag_mode_is_hash(struct mlx5_core_dev *dev)
1439 {
1440 struct mlx5_lag *ldev;
1441 unsigned long flags;
1442 bool res = 0;
1443
1444 spin_lock_irqsave(&lag_lock, flags);
1445 ldev = mlx5_lag_dev(dev);
1446 if (ldev)
1447 res = test_bit(MLX5_LAG_MODE_FLAG_HASH_BASED, &ldev->mode_flags);
1448 spin_unlock_irqrestore(&lag_lock, flags);
1449
1450 return res;
1451 }
1452 EXPORT_SYMBOL(mlx5_lag_mode_is_hash);
1453
mlx5_lag_is_master(struct mlx5_core_dev * dev)1454 bool mlx5_lag_is_master(struct mlx5_core_dev *dev)
1455 {
1456 struct mlx5_lag *ldev;
1457 unsigned long flags;
1458 bool res;
1459
1460 spin_lock_irqsave(&lag_lock, flags);
1461 ldev = mlx5_lag_dev(dev);
1462 res = ldev && __mlx5_lag_is_active(ldev) &&
1463 dev == ldev->pf[MLX5_LAG_P1].dev;
1464 spin_unlock_irqrestore(&lag_lock, flags);
1465
1466 return res;
1467 }
1468 EXPORT_SYMBOL(mlx5_lag_is_master);
1469
mlx5_lag_is_sriov(struct mlx5_core_dev * dev)1470 bool mlx5_lag_is_sriov(struct mlx5_core_dev *dev)
1471 {
1472 struct mlx5_lag *ldev;
1473 unsigned long flags;
1474 bool res;
1475
1476 spin_lock_irqsave(&lag_lock, flags);
1477 ldev = mlx5_lag_dev(dev);
1478 res = ldev && __mlx5_lag_is_sriov(ldev);
1479 spin_unlock_irqrestore(&lag_lock, flags);
1480
1481 return res;
1482 }
1483 EXPORT_SYMBOL(mlx5_lag_is_sriov);
1484
mlx5_lag_is_shared_fdb(struct mlx5_core_dev * dev)1485 bool mlx5_lag_is_shared_fdb(struct mlx5_core_dev *dev)
1486 {
1487 struct mlx5_lag *ldev;
1488 unsigned long flags;
1489 bool res;
1490
1491 spin_lock_irqsave(&lag_lock, flags);
1492 ldev = mlx5_lag_dev(dev);
1493 res = ldev && test_bit(MLX5_LAG_MODE_FLAG_SHARED_FDB, &ldev->mode_flags);
1494 spin_unlock_irqrestore(&lag_lock, flags);
1495
1496 return res;
1497 }
1498 EXPORT_SYMBOL(mlx5_lag_is_shared_fdb);
1499
mlx5_lag_disable_change(struct mlx5_core_dev * dev)1500 void mlx5_lag_disable_change(struct mlx5_core_dev *dev)
1501 {
1502 struct mlx5_lag *ldev;
1503
1504 ldev = mlx5_lag_dev(dev);
1505 if (!ldev)
1506 return;
1507
1508 mlx5_devcom_comp_lock(dev->priv.hca_devcom_comp);
1509 mutex_lock(&ldev->lock);
1510
1511 ldev->mode_changes_in_progress++;
1512 if (__mlx5_lag_is_active(ldev))
1513 mlx5_disable_lag(ldev);
1514
1515 mutex_unlock(&ldev->lock);
1516 mlx5_devcom_comp_unlock(dev->priv.hca_devcom_comp);
1517 }
1518
mlx5_lag_enable_change(struct mlx5_core_dev * dev)1519 void mlx5_lag_enable_change(struct mlx5_core_dev *dev)
1520 {
1521 struct mlx5_lag *ldev;
1522
1523 ldev = mlx5_lag_dev(dev);
1524 if (!ldev)
1525 return;
1526
1527 mutex_lock(&ldev->lock);
1528 ldev->mode_changes_in_progress--;
1529 mutex_unlock(&ldev->lock);
1530 mlx5_queue_bond_work(ldev, 0);
1531 }
1532
mlx5_lag_get_slave_port(struct mlx5_core_dev * dev,struct net_device * slave)1533 u8 mlx5_lag_get_slave_port(struct mlx5_core_dev *dev,
1534 struct net_device *slave)
1535 {
1536 struct mlx5_lag *ldev;
1537 unsigned long flags;
1538 u8 port = 0;
1539 int i;
1540
1541 spin_lock_irqsave(&lag_lock, flags);
1542 ldev = mlx5_lag_dev(dev);
1543 if (!(ldev && __mlx5_lag_is_roce(ldev)))
1544 goto unlock;
1545
1546 for (i = 0; i < ldev->ports; i++) {
1547 if (ldev->pf[i].netdev == slave) {
1548 port = i;
1549 break;
1550 }
1551 }
1552
1553 port = ldev->v2p_map[port * ldev->buckets];
1554
1555 unlock:
1556 spin_unlock_irqrestore(&lag_lock, flags);
1557 return port;
1558 }
1559 EXPORT_SYMBOL(mlx5_lag_get_slave_port);
1560
mlx5_lag_get_num_ports(struct mlx5_core_dev * dev)1561 u8 mlx5_lag_get_num_ports(struct mlx5_core_dev *dev)
1562 {
1563 struct mlx5_lag *ldev;
1564
1565 ldev = mlx5_lag_dev(dev);
1566 if (!ldev)
1567 return 0;
1568
1569 return ldev->ports;
1570 }
1571 EXPORT_SYMBOL(mlx5_lag_get_num_ports);
1572
mlx5_lag_get_next_peer_mdev(struct mlx5_core_dev * dev,int * i)1573 struct mlx5_core_dev *mlx5_lag_get_next_peer_mdev(struct mlx5_core_dev *dev, int *i)
1574 {
1575 struct mlx5_core_dev *peer_dev = NULL;
1576 struct mlx5_lag *ldev;
1577 unsigned long flags;
1578 int idx;
1579
1580 spin_lock_irqsave(&lag_lock, flags);
1581 ldev = mlx5_lag_dev(dev);
1582 if (!ldev)
1583 goto unlock;
1584
1585 if (*i == ldev->ports)
1586 goto unlock;
1587 for (idx = *i; idx < ldev->ports; idx++)
1588 if (ldev->pf[idx].dev != dev)
1589 break;
1590
1591 if (idx == ldev->ports) {
1592 *i = idx;
1593 goto unlock;
1594 }
1595 *i = idx + 1;
1596
1597 peer_dev = ldev->pf[idx].dev;
1598
1599 unlock:
1600 spin_unlock_irqrestore(&lag_lock, flags);
1601 return peer_dev;
1602 }
1603 EXPORT_SYMBOL(mlx5_lag_get_next_peer_mdev);
1604
mlx5_lag_query_cong_counters(struct mlx5_core_dev * dev,u64 * values,int num_counters,size_t * offsets)1605 int mlx5_lag_query_cong_counters(struct mlx5_core_dev *dev,
1606 u64 *values,
1607 int num_counters,
1608 size_t *offsets)
1609 {
1610 int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
1611 struct mlx5_core_dev **mdev;
1612 struct mlx5_lag *ldev;
1613 unsigned long flags;
1614 int num_ports;
1615 int ret, i, j;
1616 void *out;
1617
1618 out = kvzalloc(outlen, GFP_KERNEL);
1619 if (!out)
1620 return -ENOMEM;
1621
1622 mdev = kvzalloc(sizeof(mdev[0]) * MLX5_MAX_PORTS, GFP_KERNEL);
1623 if (!mdev) {
1624 ret = -ENOMEM;
1625 goto free_out;
1626 }
1627
1628 memset(values, 0, sizeof(*values) * num_counters);
1629
1630 spin_lock_irqsave(&lag_lock, flags);
1631 ldev = mlx5_lag_dev(dev);
1632 if (ldev && __mlx5_lag_is_active(ldev)) {
1633 num_ports = ldev->ports;
1634 for (i = 0; i < ldev->ports; i++)
1635 mdev[i] = ldev->pf[i].dev;
1636 } else {
1637 num_ports = 1;
1638 mdev[MLX5_LAG_P1] = dev;
1639 }
1640 spin_unlock_irqrestore(&lag_lock, flags);
1641
1642 for (i = 0; i < num_ports; ++i) {
1643 u32 in[MLX5_ST_SZ_DW(query_cong_statistics_in)] = {};
1644
1645 MLX5_SET(query_cong_statistics_in, in, opcode,
1646 MLX5_CMD_OP_QUERY_CONG_STATISTICS);
1647 ret = mlx5_cmd_exec_inout(mdev[i], query_cong_statistics, in,
1648 out);
1649 if (ret)
1650 goto free_mdev;
1651
1652 for (j = 0; j < num_counters; ++j)
1653 values[j] += be64_to_cpup((__be64 *)(out + offsets[j]));
1654 }
1655
1656 free_mdev:
1657 kvfree(mdev);
1658 free_out:
1659 kvfree(out);
1660 return ret;
1661 }
1662 EXPORT_SYMBOL(mlx5_lag_query_cong_counters);
1663