1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2019 Mellanox Technologies */
4 #include <linux/mlx5/vport.h>
5 #include <rdma/ib_verbs.h>
6 #include <net/addrconf.h>
13 static void mlx5_rdma_disable_roce_steering(struct mlx5_core_dev *dev)
15 struct mlx5_core_roce *roce = &dev->priv.roce;
17 mlx5_del_flow_rules(roce->allow_rule);
18 mlx5_destroy_flow_group(roce->fg);
19 mlx5_destroy_flow_table(roce->ft);
22 static int mlx5_rdma_enable_roce_steering(struct mlx5_core_dev *dev)
24 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
25 struct mlx5_core_roce *roce = &dev->priv.roce;
26 struct mlx5_flow_handle *flow_rule = NULL;
27 struct mlx5_flow_table_attr ft_attr = {};
28 struct mlx5_flow_namespace *ns = NULL;
29 struct mlx5_flow_act flow_act = {};
30 struct mlx5_flow_spec *spec;
31 struct mlx5_flow_table *ft;
32 struct mlx5_flow_group *fg;
38 if (!(MLX5_CAP_FLOWTABLE_RDMA_RX(dev, ft_support) &&
39 MLX5_CAP_FLOWTABLE_RDMA_RX(dev, table_miss_action_domain)))
42 flow_group_in = kvzalloc(inlen, GFP_KERNEL);
45 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
47 kvfree(flow_group_in);
51 ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_RDMA_RX_KERNEL);
53 mlx5_core_err(dev, "Failed to get RDMA RX namespace");
59 ft = mlx5_create_flow_table(ns, &ft_attr);
61 mlx5_core_err(dev, "Failed to create RDMA RX flow table");
66 MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
67 MLX5_MATCH_MISC_PARAMETERS);
68 match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in,
70 MLX5_SET_TO_ONES(fte_match_param, match_criteria,
71 misc_parameters.source_port);
73 fg = mlx5_create_flow_group(ft, flow_group_in);
76 mlx5_core_err(dev, "Failed to create RDMA RX flow group err(%d)\n", err);
77 goto destroy_flow_table;
80 spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
81 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
83 MLX5_SET(fte_match_set_misc, misc, source_port,
84 dev->priv.eswitch->manager_vport);
85 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
87 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
89 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_ALLOW;
90 flow_rule = mlx5_add_flow_rules(ft, spec, &flow_act, NULL, 0);
91 if (IS_ERR(flow_rule)) {
92 err = PTR_ERR(flow_rule);
93 mlx5_core_err(dev, "Failed to add RoCE allow rule, err=%d\n",
95 goto destroy_flow_group;
99 kvfree(flow_group_in);
102 roce->allow_rule = flow_rule;
107 mlx5_destroy_flow_group(fg);
109 mlx5_destroy_flow_table(ft);
112 kvfree(flow_group_in);
116 static void mlx5_rdma_del_roce_addr(struct mlx5_core_dev *dev)
118 mlx5_core_roce_gid_set(dev, 0, 0, 0,
119 NULL, NULL, false, 0, 1);
122 static void mlx5_rdma_make_default_gid(struct mlx5_core_dev *dev, union ib_gid *gid)
126 mlx5_query_mac_address(dev, hw_id);
127 gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
128 addrconf_addr_eui48(&gid->raw[8], hw_id);
131 static int mlx5_rdma_add_roce_addr(struct mlx5_core_dev *dev)
136 mlx5_rdma_make_default_gid(dev, &gid);
137 return mlx5_core_roce_gid_set(dev, 0,
143 void mlx5_rdma_disable_roce(struct mlx5_core_dev *dev)
145 struct mlx5_core_roce *roce = &dev->priv.roce;
150 mlx5_rdma_disable_roce_steering(dev);
151 mlx5_rdma_del_roce_addr(dev);
152 mlx5_nic_vport_disable_roce(dev);
155 void mlx5_rdma_enable_roce(struct mlx5_core_dev *dev)
159 err = mlx5_nic_vport_enable_roce(dev);
161 mlx5_core_err(dev, "Failed to enable RoCE: %d\n", err);
165 err = mlx5_rdma_add_roce_addr(dev);
167 mlx5_core_err(dev, "Failed to add RoCE address: %d\n", err);
171 err = mlx5_rdma_enable_roce_steering(dev);
173 mlx5_core_err(dev, "Failed to enable RoCE steering: %d\n", err);
180 mlx5_rdma_del_roce_addr(dev);
182 mlx5_nic_vport_disable_roce(dev);