aboutsummaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/mellanox/mlx5/core/steering/dr_arg.c
blob: 01ed6442095d7899979cbfed60e9209c69fc2d5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
// Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.

#include "dr_types.h"

#define DR_ICM_MODIFY_HDR_GRANULARITY_4K 12

/* modify-header arg pool */
enum dr_arg_chunk_size {
	DR_ARG_CHUNK_SIZE_1,
	DR_ARG_CHUNK_SIZE_MIN = DR_ARG_CHUNK_SIZE_1, /* keep updated when changing */
	DR_ARG_CHUNK_SIZE_2,
	DR_ARG_CHUNK_SIZE_3,
	DR_ARG_CHUNK_SIZE_4,
	DR_ARG_CHUNK_SIZE_MAX,
};

/* argument pool area */
struct dr_arg_pool {
	enum dr_arg_chunk_size log_chunk_size;
	struct mlx5dr_domain *dmn;
	struct list_head free_list;
	struct mutex mutex; /* protect arg pool */
};

struct mlx5dr_arg_mgr {
	struct mlx5dr_domain *dmn;
	struct dr_arg_pool *pools[DR_ARG_CHUNK_SIZE_MAX];
};

static int dr_arg_pool_alloc_objs(struct dr_arg_pool *pool)
{
	struct mlx5dr_arg_obj *arg_obj, *tmp_arg;
	struct list_head cur_list;
	u16 object_range;
	int num_of_objects;
	u32 obj_id = 0;
	int i, ret;

	INIT_LIST_HEAD(&cur_list);

	object_range =
		pool->dmn->info.caps.log_header_modify_argument_granularity;

	object_range =
		max_t(u32, pool->dmn->info.caps.log_header_modify_argument_granularity,
		      DR_ICM_MODIFY_HDR_GRANULARITY_4K);
	object_range =
		min_t(u32, pool->dmn->info.caps.log_header_modify_argument_max_alloc,
		      object_range);

	if (pool->log_chunk_size > object_range) {
		mlx5dr_err(pool->dmn, "Required chunk size (%d) is not supported\n",
			   pool->log_chunk_size);
		return -ENOMEM;
	}

	num_of_objects = (1 << (object_range - pool->log_chunk_size));
	/* Only one devx object per range */
	ret = mlx5dr_cmd_create_modify_header_arg(pool->dmn->mdev,
						  object_range,
						  pool->dmn->pdn,
						  &obj_id);
	if (ret) {
		mlx5dr_err(pool->dmn, "failed allocating object with range: %d:\n",
			   object_range);
		return -EAGAIN;
	}

	for (i = 0; i < num_of_objects; i++) {
		arg_obj = kzalloc(sizeof(*arg_obj), GFP_KERNEL);
		if (!arg_obj) {
			ret = -ENOMEM;
			goto clean_arg_obj;
		}

		arg_obj->log_chunk_size = pool->log_chunk_size;

		list_add_tail(&arg_obj->list_node, &cur_list);

		arg_obj->obj_id = obj_id;
		arg_obj->obj_offset = i * (1 << pool->log_chunk_size);
	}
	list_splice_tail_init(&cur_list, &pool->free_list);

	return 0;

clean_arg_obj:
	mlx5dr_cmd_destroy_modify_header_arg(pool->dmn->mdev, obj_id);
	list_for_each_entry_safe(arg_obj, tmp_arg, &cur_list, list_node) {
		list_del(&arg_obj->list_node);
		kfree(arg_obj);
	}
	return ret;
}

static struct mlx5dr_arg_obj *dr_arg_pool_get_arg_obj(struct dr_arg_pool *pool)
{
	struct mlx5dr_arg_obj *arg_obj = NULL;
	int ret;

	mutex_lock(&pool->mutex);
	if (list_empty(&pool->free_list)) {
		ret = dr_arg_pool_alloc_objs(pool);
		if (ret)
			goto out;
	}

	arg_obj = list_first_entry_or_null(&pool->free_list,
					   struct mlx5dr_arg_obj,
					   list_node);
	WARN(!arg_obj, "couldn't get dr arg obj from pool");

	if (arg_obj)
		list_del_init(&arg_obj->list_node);

out:
	mutex_unlock(&pool->mutex);
	return arg_obj;
}

static void dr_arg_pool_put_arg_obj(struct dr_arg_pool *pool,
				    struct mlx5dr_arg_obj *arg_obj)
{
	mutex_lock(&pool->mutex);
	list_add(&arg_obj->list_node, &pool->free_list);
	mutex_unlock(&pool->mutex);
}

static struct dr_arg_pool *dr_arg_pool_create(struct mlx5dr_domain *dmn,
					      enum dr_arg_chunk_size chunk_size)
{
	struct dr_arg_pool *pool;

	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
	if (!pool)
		return NULL;

	pool->dmn = dmn;

	INIT_LIST_HEAD(&pool->free_list);
	mutex_init(&pool->mutex);

	pool->log_chunk_size = chunk_size;
	if (dr_arg_pool_alloc_objs(pool))
		goto free_pool;

	return pool;

free_pool:
	kfree(pool);

	return NULL;
}

static void dr_arg_pool_destroy(struct dr_arg_pool *pool)
{
	struct mlx5dr_arg_obj *arg_obj, *tmp_arg;

	list_for_each_entry_safe(arg_obj, tmp_arg, &pool->free_list, list_node) {
		list_del(&arg_obj->list_node);
		if (!arg_obj->obj_offset) /* the first in range */
			mlx5dr_cmd_destroy_modify_header_arg(pool->dmn->mdev, arg_obj->obj_id);
		kfree(arg_obj);
	}

	mutex_destroy(&pool->mutex);
	kfree(pool);
}

static enum dr_arg_chunk_size dr_arg_get_chunk_size(u16 num_of_actions)
{
	if (num_of_actions <= 8)
		return DR_ARG_CHUNK_SIZE_1;
	if (num_of_actions <= 16)
		return DR_ARG_CHUNK_SIZE_2;
	if (num_of_actions <= 32)
		return DR_ARG_CHUNK_SIZE_3;
	if (num_of_actions <= 64)
		return DR_ARG_CHUNK_SIZE_4;

	return DR_ARG_CHUNK_SIZE_MAX;
}

u32 mlx5dr_arg_get_obj_id(struct mlx5dr_arg_obj *arg_obj)
{
	return (arg_obj->obj_id + arg_obj->obj_offset);
}

struct mlx5dr_arg_obj *mlx5dr_arg_get_obj(struct mlx5dr_arg_mgr *mgr,
					  u16 num_of_actions,
					  u8 *data)
{
	u32 size = dr_arg_get_chunk_size(num_of_actions);
	struct mlx5dr_arg_obj *arg_obj;
	int ret;

	if (size >= DR_ARG_CHUNK_SIZE_MAX)
		return NULL;

	arg_obj = dr_arg_pool_get_arg_obj(mgr->pools[size]);
	if (!arg_obj) {
		mlx5dr_err(mgr->dmn, "Failed allocating args object for modify header\n");
		return NULL;
	}

	/* write it into the hw */
	ret = mlx5dr_send_postsend_args(mgr->dmn,
					mlx5dr_arg_get_obj_id(arg_obj),
					num_of_actions, data);
	if (ret) {
		mlx5dr_err(mgr->dmn, "Failed writing args object\n");
		goto put_obj;
	}

	return arg_obj;

put_obj:
	mlx5dr_arg_put_obj(mgr, arg_obj);
	return NULL;
}

void mlx5dr_arg_put_obj(struct mlx5dr_arg_mgr *mgr,
			struct mlx5dr_arg_obj *arg_obj)
{
	dr_arg_pool_put_arg_obj(mgr->pools[arg_obj->log_chunk_size], arg_obj);
}

struct mlx5dr_arg_mgr*
mlx5dr_arg_mgr_create(struct mlx5dr_domain *dmn)
{
	struct mlx5dr_arg_mgr *pool_mgr;
	int i;

	if (!mlx5dr_domain_is_support_ptrn_arg(dmn))
		return NULL;

	pool_mgr = kzalloc(sizeof(*pool_mgr), GFP_KERNEL);
	if (!pool_mgr)
		return NULL;

	pool_mgr->dmn = dmn;

	for (i = 0; i < DR_ARG_CHUNK_SIZE_MAX; i++) {
		pool_mgr->pools[i] = dr_arg_pool_create(dmn, i);
		if (!pool_mgr->pools[i])
			goto clean_pools;
	}

	return pool_mgr;

clean_pools:
	for (i--; i >= 0; i--)
		dr_arg_pool_destroy(pool_mgr->pools[i]);

	kfree(pool_mgr);
	return NULL;
}

void mlx5dr_arg_mgr_destroy(struct mlx5dr_arg_mgr *mgr)
{
	struct dr_arg_pool **pools;
	int i;

	if (!mgr)
		return;

	pools = mgr->pools;
	for (i = 0; i < DR_ARG_CHUNK_SIZE_MAX; i++)
		dr_arg_pool_destroy(pools[i]);

	kfree(mgr);
}