aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/mellanox/mlx5/core/lib/hv_vhca.c
blob: 4047629a876b1a6f87aa90af66052ba59abf447a (plain) (blame)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
// Copyright (c) 2018 Mellanox Technologies

#include <linux/hyperv.h>
#include "mlx5_core.h"
#include "lib/hv.h"
#include "lib/hv_vhca.h"

struct mlx5_hv_vhca {
	struct mlx5_core_dev       *dev;
	struct workqueue_struct    *work_queue;
	struct mlx5_hv_vhca_agent  *agents[MLX5_HV_VHCA_AGENT_MAX];
	struct mutex                agents_lock; /* Protect agents array */
};

struct mlx5_hv_vhca_work {
	struct work_struct     invalidate_work;
	struct mlx5_hv_vhca   *hv_vhca;
	u64                    block_mask;
};

struct mlx5_hv_vhca_data_block {
	u16     sequence;
	u16     offset;
	u8      reserved[4];
	u64     data[15];
};

struct mlx5_hv_vhca_agent {
	enum mlx5_hv_vhca_agent_type	 type;
	struct mlx5_hv_vhca		*hv_vhca;
	void				*priv;
	u16                              seq;
	void (*control)(struct mlx5_hv_vhca_agent *agent,
			struct mlx5_hv_vhca_control_block *block);
	void (*invalidate)(struct mlx5_hv_vhca_agent *agent,
			   u64 block_mask);
	void (*cleanup)(struct mlx5_hv_vhca_agent *agent);
};

struct mlx5_hv_vhca *mlx5_hv_vhca_create(struct mlx5_core_dev *dev)
{
	struct mlx5_hv_vhca *hv_vhca = NULL;

	hv_vhca = kzalloc(sizeof(*hv_vhca), GFP_KERNEL);
	if (!hv_vhca)
		return ERR_PTR(-ENOMEM);

	hv_vhca->work_queue = create_singlethread_workqueue("mlx5_hv_vhca");
	if (!hv_vhca->work_queue) {
		kfree(hv_vhca);
		return ERR_PTR(-ENOMEM);
	}

	hv_vhca->dev = dev;
	mutex_init(&hv_vhca->agents_lock);

	return hv_vhca;
}

void mlx5_hv_vhca_destroy(struct mlx5_hv_vhca *hv_vhca)
{
	if (IS_ERR_OR_NULL(hv_vhca))
		return;

	destroy_workqueue(hv_vhca->work_queue);
	kfree(hv_vhca);
}

static void mlx5_hv_vhca_invalidate_work(struct work_struct *work)
{
	struct mlx5_hv_vhca_work *hwork;
	struct mlx5_hv_vhca *hv_vhca;
	int i;

	hwork = container_of(work, struct mlx5_hv_vhca_work, invalidate_work);
	hv_vhca = hwork->hv_vhca;

	mutex_lock(&hv_vhca->agents_lock);
	for (i = 0; i < MLX5_HV_VHCA_AGENT_MAX; i++) {
		struct mlx5_hv_vhca_agent *agent = hv_vhca->agents[i];

		if (!agent || !agent->invalidate)
			continue;

		if (!(BIT(agent->type) & hwork->block_mask))
			continue;

		agent->invalidate(agent, hwork->block_mask);
	}
	mutex_unlock(&hv_vhca->agents_lock);

	kfree(hwork);
}

void mlx5_hv_vhca_invalidate(void *context, u64 block_mask)
{
	struct mlx5_hv_vhca *hv_vhca = (struct mlx5_hv_vhca *)context;
	struct mlx5_hv_vhca_work *work;

	work = kzalloc(sizeof(*work), GFP_ATOMIC);
	if (!work)
		return;

	INIT_WORK(&work->invalidate_work, mlx5_hv_vhca_invalidate_work);
	work->hv_vhca    = hv_vhca;
	work->block_mask = block_mask;

	queue_work(hv_vhca->work_queue, &work->invalidate_work);
}

#define AGENT_MASK(type) (type ? BIT(type - 1) : 0 /* control */)

static void mlx5_hv_vhca_agents_control(struct mlx5_hv_vhca *hv_vhca,
					struct mlx5_hv_vhca_control_block *block)
{
	int i;

	for (i = 0; i < MLX5_HV_VHCA_AGENT_MAX; i++) {
		struct mlx5_hv_vhca_agent *agent = hv_vhca->agents[i];

		if (!agent || !agent->control)
			continue;

		if (!(AGENT_MASK(agent->type) & block->control))
			continue;

		agent->control(agent, block);
	}
}

static void mlx5_hv_vhca_capabilities(struct mlx5_hv_vhca *hv_vhca,
				      u32 *capabilities)
{
	int i;

	for (i = 0; i < MLX5_HV_VHCA_AGENT_MAX; i++) {
		struct mlx5_hv_vhca_agent *agent = hv_vhca->agents[i];

		if (agent)
			*capabilities |= AGENT_MASK(agent->type);
	}
}

static void
mlx5_hv_vhca_control_agent_invalidate(struct mlx5_hv_vhca_agent *agent,
				      u64 block_mask)
{
	struct mlx5_hv_vhca *hv_vhca = agent->hv_vhca;
	struct mlx5_core_dev *dev = hv_vhca->dev;
	struct mlx5_hv_vhca_control_block *block;
	u32 capabilities = 0;
	int err;

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

	err = mlx5_hv_read_config(dev, block, sizeof(*block), 0);
	if (err)
		goto free_block;

	mlx5_hv_vhca_capabilities(hv_vhca, &capabilities);

	/* In case no capabilities, send empty block in return */
	if (!capabilities) {
		memset(block, 0, sizeof(*block));
		goto write;
	}

	if (block->capabilities != capabilities)
		block->capabilities = capabilities;

	if (block->control & ~capabilities)
		goto free_block;

	mlx5_hv_vhca_agents_control(hv_vhca, block);
	block->command_ack = block->command;

write:
	mlx5_hv_write_config(dev, block, sizeof(*block), 0);

free_block:
	kfree(block);
}

static struct mlx5_hv_vhca_agent *
mlx5_hv_vhca_control_agent_create(struct mlx5_hv_vhca *hv_vhca)
{
	return mlx5_hv_vhca_agent_create(hv_vhca, MLX5_HV_VHCA_AGENT_CONTROL,
					 NULL,
					 mlx5_hv_vhca_control_agent_invalidate,
					 NULL, NULL);
}

static void mlx5_hv_vhca_control_agent_destroy(struct mlx5_hv_vhca_agent *agent)
{
	mlx5_hv_vhca_agent_destroy(agent);
}

int mlx5_hv_vhca_init(struct mlx5_hv_vhca *hv_vhca)
{
	struct mlx5_hv_vhca_agent *agent;
	int err;

	if (IS_ERR_OR_NULL(hv_vhca))
		return IS_ERR_OR_NULL(hv_vhca);

	err = mlx5_hv_register_invalidate(hv_vhca->dev, hv_vhca,
					  mlx5_hv_vhca_invalidate);
	if (err)
		return err;

	agent = mlx5_hv_vhca_control_agent_create(hv_vhca);
	if (IS_ERR_OR_NULL(agent)) {
		mlx5_hv_unregister_invalidate(hv_vhca->dev);
		return IS_ERR_OR_NULL(agent);
	}

	hv_vhca->agents[MLX5_HV_VHCA_AGENT_CONTROL] = agent;

	return 0;
}

void mlx5_hv_vhca_cleanup(struct mlx5_hv_vhca *hv_vhca)
{
	struct mlx5_hv_vhca_agent *agent;
	int i;

	if (IS_ERR_OR_NULL(hv_vhca))
		return;

	agent = hv_vhca->agents[MLX5_HV_VHCA_AGENT_CONTROL];
	if (agent)
		mlx5_hv_vhca_control_agent_destroy(agent);

	mutex_lock(&hv_vhca->agents_lock);
	for (i = 0; i < MLX5_HV_VHCA_AGENT_MAX; i++)
		WARN_ON(hv_vhca->agents[i]);

	mutex_unlock(&hv_vhca->agents_lock);

	mlx5_hv_unregister_invalidate(hv_vhca->dev);
}

static void mlx5_hv_vhca_agents_update(struct mlx5_hv_vhca *hv_vhca)
{
	mlx5_hv_vhca_invalidate(hv_vhca, BIT(MLX5_HV_VHCA_AGENT_CONTROL));
}

struct mlx5_hv_vhca_agent *
mlx5_hv_vhca_agent_create(struct mlx5_hv_vhca *hv_vhca,
			  enum mlx5_hv_vhca_agent_type type,
			  void (*control)(struct mlx5_hv_vhca_agent*,
					  struct mlx5_hv_vhca_control_block *block),
			  void (*invalidate)(struct mlx5_hv_vhca_agent*,
					     u64 block_mask),
			  void (*cleaup)(struct mlx5_hv_vhca_agent *agent),
			  void *priv)
{
	struct mlx5_hv_vhca_agent *agent;

	if (IS_ERR_OR_NULL(hv_vhca))
		return ERR_PTR(-ENOMEM);

	if (type >= MLX5_HV_VHCA_AGENT_MAX)
		return ERR_PTR(-EINVAL);

	mutex_lock(&hv_vhca->agents_lock);
	if (hv_vhca->agents[type]) {
		mutex_unlock(&hv_vhca->agents_lock);
		return ERR_PTR(-EINVAL);
	}
	mutex_unlock(&hv_vhca->agents_lock);

	agent = kzalloc(sizeof(*agent), GFP_KERNEL);
	if (!agent)
		return ERR_PTR(-ENOMEM);

	agent->type      = type;
	agent->hv_vhca   = hv_vhca;
	agent->priv      = priv;
	agent->control   = control;
	agent->invalidate = invalidate;
	agent->cleanup   = cleaup;

	mutex_lock(&hv_vhca->agents_lock);
	hv_vhca->agents[type] = agent;
	mutex_unlock(&hv_vhca->agents_lock);

	mlx5_hv_vhca_agents_update(hv_vhca);

	return agent;
}

void mlx5_hv_vhca_agent_destroy(struct mlx5_hv_vhca_agent *agent)
{
	struct mlx5_hv_vhca *hv_vhca = agent->hv_vhca;

	mutex_lock(&hv_vhca->agents_lock);

	if (WARN_ON(agent != hv_vhca->agents[agent->type])) {
		mutex_unlock(&hv_vhca->agents_lock);
		return;
	}

	hv_vhca->agents[agent->type] = NULL;
	mutex_unlock(&hv_vhca->agents_lock);

	if (agent->cleanup)
		agent->cleanup(agent);

	kfree(agent);

	mlx5_hv_vhca_agents_update(hv_vhca);
}

static int mlx5_hv_vhca_data_block_prepare(struct mlx5_hv_vhca_agent *agent,
					   struct mlx5_hv_vhca_data_block *data_block,
					   void *src, int len, int *offset)
{
	int bytes = min_t(int, (int)sizeof(data_block->data), len);

	data_block->sequence = agent->seq;
	data_block->offset   = (*offset)++;
	memcpy(data_block->data, src, bytes);

	return bytes;
}

static void mlx5_hv_vhca_agent_seq_update(struct mlx5_hv_vhca_agent *agent)
{
	agent->seq++;
}

int mlx5_hv_vhca_agent_write(struct mlx5_hv_vhca_agent *agent,
			     void *buf, int len)
{
	int offset = agent->type * HV_CONFIG_BLOCK_SIZE_MAX;
	int block_offset = 0;
	int total = 0;
	int err;

	while (len) {
		struct mlx5_hv_vhca_data_block data_block = {0};
		int bytes;

		bytes = mlx5_hv_vhca_data_block_prepare(agent, &data_block,
							buf + total,
							len, &block_offset);
		if (!bytes)
			return -ENOMEM;

		err = mlx5_hv_write_config(agent->hv_vhca->dev, &data_block,
					   sizeof(data_block), offset);
		if (err)
			return err;

		total += bytes;
		len   -= bytes;
	}

	mlx5_hv_vhca_agent_seq_update(agent);

	return 0;
}

void *mlx5_hv_vhca_agent_priv(struct mlx5_hv_vhca_agent *agent)
{
	return agent->priv;
}