summaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/mellanox/mlx5/core/lib/hv_vhca.c
blob: 84d1d75a1608cf79d78ff4188874b432053481b0 (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
// 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);
}

int mlx5_hv_vhca_init(struct mlx5_hv_vhca *hv_vhca)
{
	if (IS_ERR_OR_NULL(hv_vhca))
		return IS_ERR_OR_NULL(hv_vhca);

	return mlx5_hv_register_invalidate(hv_vhca->dev, hv_vhca,
					   mlx5_hv_vhca_invalidate);
}

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

	if (IS_ERR_OR_NULL(hv_vhca))
		return;

	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);
}

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);

	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);
}

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;
}