aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/mellanox/mlx5/core/lib/devcom.c
blob: e7d59cfa8708e1617f78b28974977a9588026d1f (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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
/* Copyright (c) 2018 Mellanox Technologies */

#include <linux/mlx5/vport.h>
#include <linux/list.h>
#include "lib/devcom.h"
#include "mlx5_core.h"

static LIST_HEAD(devcom_dev_list);
static LIST_HEAD(devcom_comp_list);
/* protect device list */
static DEFINE_MUTEX(dev_list_lock);
/* protect component list */
static DEFINE_MUTEX(comp_list_lock);

#define devcom_for_each_component(iter) \
	list_for_each_entry(iter, &devcom_comp_list, comp_list)

struct mlx5_devcom_dev {
	struct list_head list;
	struct mlx5_core_dev *dev;
	struct kref ref;
};

struct mlx5_devcom_comp {
	struct list_head comp_list;
	enum mlx5_devcom_component id;
	u64 key;
	struct list_head comp_dev_list_head;
	mlx5_devcom_event_handler_t handler;
	struct kref ref;
	bool ready;
	struct rw_semaphore sem;
	struct lock_class_key lock_key;
};

struct mlx5_devcom_comp_dev {
	struct list_head list;
	struct mlx5_devcom_comp *comp;
	struct mlx5_devcom_dev *devc;
	void __rcu *data;
};

static bool devcom_dev_exists(struct mlx5_core_dev *dev)
{
	struct mlx5_devcom_dev *iter;

	list_for_each_entry(iter, &devcom_dev_list, list)
		if (iter->dev == dev)
			return true;

	return false;
}

static struct mlx5_devcom_dev *
mlx5_devcom_dev_alloc(struct mlx5_core_dev *dev)
{
	struct mlx5_devcom_dev *devc;

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

	devc->dev = dev;
	kref_init(&devc->ref);
	return devc;
}

struct mlx5_devcom_dev *
mlx5_devcom_register_device(struct mlx5_core_dev *dev)
{
	struct mlx5_devcom_dev *devc;

	mutex_lock(&dev_list_lock);

	if (devcom_dev_exists(dev)) {
		devc = ERR_PTR(-EEXIST);
		goto out;
	}

	devc = mlx5_devcom_dev_alloc(dev);
	if (!devc) {
		devc = ERR_PTR(-ENOMEM);
		goto out;
	}

	list_add_tail(&devc->list, &devcom_dev_list);
out:
	mutex_unlock(&dev_list_lock);
	return devc;
}

static void
mlx5_devcom_dev_release(struct kref *ref)
{
	struct mlx5_devcom_dev *devc = container_of(ref, struct mlx5_devcom_dev, ref);

	mutex_lock(&dev_list_lock);
	list_del(&devc->list);
	mutex_unlock(&dev_list_lock);
	kfree(devc);
}

void mlx5_devcom_unregister_device(struct mlx5_devcom_dev *devc)
{
	if (!IS_ERR_OR_NULL(devc))
		kref_put(&devc->ref, mlx5_devcom_dev_release);
}

static struct mlx5_devcom_comp *
mlx5_devcom_comp_alloc(u64 id, u64 key, mlx5_devcom_event_handler_t handler)
{
	struct mlx5_devcom_comp *comp;

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

	comp->id = id;
	comp->key = key;
	comp->handler = handler;
	init_rwsem(&comp->sem);
	lockdep_register_key(&comp->lock_key);
	lockdep_set_class(&comp->sem, &comp->lock_key);
	kref_init(&comp->ref);
	INIT_LIST_HEAD(&comp->comp_dev_list_head);

	return comp;
}

static void
mlx5_devcom_comp_release(struct kref *ref)
{
	struct mlx5_devcom_comp *comp = container_of(ref, struct mlx5_devcom_comp, ref);

	mutex_lock(&comp_list_lock);
	list_del(&comp->comp_list);
	mutex_unlock(&comp_list_lock);
	lockdep_unregister_key(&comp->lock_key);
	kfree(comp);
}

static struct mlx5_devcom_comp_dev *
devcom_alloc_comp_dev(struct mlx5_devcom_dev *devc,
		      struct mlx5_devcom_comp *comp,
		      void *data)
{
	struct mlx5_devcom_comp_dev *devcom;

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

	kref_get(&devc->ref);
	devcom->devc = devc;
	devcom->comp = comp;
	rcu_assign_pointer(devcom->data, data);

	down_write(&comp->sem);
	list_add_tail(&devcom->list, &comp->comp_dev_list_head);
	up_write(&comp->sem);

	return devcom;
}

static void
devcom_free_comp_dev(struct mlx5_devcom_comp_dev *devcom)
{
	struct mlx5_devcom_comp *comp = devcom->comp;

	down_write(&comp->sem);
	list_del(&devcom->list);
	up_write(&comp->sem);

	kref_put(&devcom->devc->ref, mlx5_devcom_dev_release);
	kfree(devcom);
	kref_put(&comp->ref, mlx5_devcom_comp_release);
}

static bool
devcom_component_equal(struct mlx5_devcom_comp *devcom,
		       enum mlx5_devcom_component id,
		       u64 key)
{
	return devcom->id == id && devcom->key == key;
}

static struct mlx5_devcom_comp *
devcom_component_get(struct mlx5_devcom_dev *devc,
		     enum mlx5_devcom_component id,
		     u64 key,
		     mlx5_devcom_event_handler_t handler)
{
	struct mlx5_devcom_comp *comp;

	devcom_for_each_component(comp) {
		if (devcom_component_equal(comp, id, key)) {
			if (handler == comp->handler) {
				kref_get(&comp->ref);
				return comp;
			}

			mlx5_core_err(devc->dev,
				      "Cannot register existing devcom component with different handler\n");
			return ERR_PTR(-EINVAL);
		}
	}

	return NULL;
}

struct mlx5_devcom_comp_dev *
mlx5_devcom_register_component(struct mlx5_devcom_dev *devc,
			       enum mlx5_devcom_component id,
			       u64 key,
			       mlx5_devcom_event_handler_t handler,
			       void *data)
{
	struct mlx5_devcom_comp_dev *devcom;
	struct mlx5_devcom_comp *comp;

	if (IS_ERR_OR_NULL(devc))
		return NULL;

	mutex_lock(&comp_list_lock);
	comp = devcom_component_get(devc, id, key, handler);
	if (IS_ERR(comp)) {
		devcom = ERR_PTR(-EINVAL);
		goto out_unlock;
	}

	if (!comp) {
		comp = mlx5_devcom_comp_alloc(id, key, handler);
		if (IS_ERR(comp)) {
			devcom = ERR_CAST(comp);
			goto out_unlock;
		}
		list_add_tail(&comp->comp_list, &devcom_comp_list);
	}
	mutex_unlock(&comp_list_lock);

	devcom = devcom_alloc_comp_dev(devc, comp, data);
	if (IS_ERR(devcom))
		kref_put(&comp->ref, mlx5_devcom_comp_release);

	return devcom;

out_unlock:
	mutex_unlock(&comp_list_lock);
	return devcom;
}

void mlx5_devcom_unregister_component(struct mlx5_devcom_comp_dev *devcom)
{
	if (!IS_ERR_OR_NULL(devcom))
		devcom_free_comp_dev(devcom);
}

int mlx5_devcom_comp_get_size(struct mlx5_devcom_comp_dev *devcom)
{
	struct mlx5_devcom_comp *comp = devcom->comp;

	return kref_read(&comp->ref);
}

int mlx5_devcom_send_event(struct mlx5_devcom_comp_dev *devcom,
			   int event, int rollback_event,
			   void *event_data)
{
	struct mlx5_devcom_comp_dev *pos;
	struct mlx5_devcom_comp *comp;
	int err = 0;
	void *data;

	if (IS_ERR_OR_NULL(devcom))
		return -ENODEV;

	comp = devcom->comp;
	down_write(&comp->sem);
	list_for_each_entry(pos, &comp->comp_dev_list_head, list) {
		data = rcu_dereference_protected(pos->data, lockdep_is_held(&comp->sem));

		if (pos != devcom && data) {
			err = comp->handler(event, data, event_data);
			if (err)
				goto rollback;
		}
	}

	up_write(&comp->sem);
	return 0;

rollback:
	if (list_entry_is_head(pos, &comp->comp_dev_list_head, list))
		goto out;
	pos = list_prev_entry(pos, list);
	list_for_each_entry_from_reverse(pos, &comp->comp_dev_list_head, list) {
		data = rcu_dereference_protected(pos->data, lockdep_is_held(&comp->sem));

		if (pos != devcom && data)
			comp->handler(rollback_event, data, event_data);
	}
out:
	up_write(&comp->sem);
	return err;
}

void mlx5_devcom_comp_set_ready(struct mlx5_devcom_comp_dev *devcom, bool ready)
{
	WARN_ON(!rwsem_is_locked(&devcom->comp->sem));

	WRITE_ONCE(devcom->comp->ready, ready);
}

bool mlx5_devcom_comp_is_ready(struct mlx5_devcom_comp_dev *devcom)
{
	if (IS_ERR_OR_NULL(devcom))
		return false;

	return READ_ONCE(devcom->comp->ready);
}

bool mlx5_devcom_for_each_peer_begin(struct mlx5_devcom_comp_dev *devcom)
{
	struct mlx5_devcom_comp *comp;

	if (IS_ERR_OR_NULL(devcom))
		return false;

	comp = devcom->comp;
	down_read(&comp->sem);
	if (!READ_ONCE(comp->ready)) {
		up_read(&comp->sem);
		return false;
	}

	return true;
}

void mlx5_devcom_for_each_peer_end(struct mlx5_devcom_comp_dev *devcom)
{
	up_read(&devcom->comp->sem);
}

void *mlx5_devcom_get_next_peer_data(struct mlx5_devcom_comp_dev *devcom,
				     struct mlx5_devcom_comp_dev **pos)
{
	struct mlx5_devcom_comp *comp = devcom->comp;
	struct mlx5_devcom_comp_dev *tmp;
	void *data;

	tmp = list_prepare_entry(*pos, &comp->comp_dev_list_head, list);

	list_for_each_entry_continue(tmp, &comp->comp_dev_list_head, list) {
		if (tmp != devcom) {
			data = rcu_dereference_protected(tmp->data, lockdep_is_held(&comp->sem));
			if (data)
				break;
		}
	}

	if (list_entry_is_head(tmp, &comp->comp_dev_list_head, list))
		return NULL;

	*pos = tmp;
	return data;
}

void *mlx5_devcom_get_next_peer_data_rcu(struct mlx5_devcom_comp_dev *devcom,
					 struct mlx5_devcom_comp_dev **pos)
{
	struct mlx5_devcom_comp *comp = devcom->comp;
	struct mlx5_devcom_comp_dev *tmp;
	void *data;

	tmp = list_prepare_entry(*pos, &comp->comp_dev_list_head, list);

	list_for_each_entry_continue(tmp, &comp->comp_dev_list_head, list) {
		if (tmp != devcom) {
			/* This can change concurrently, however 'data' pointer will remain
			 * valid for the duration of RCU read section.
			 */
			if (!READ_ONCE(comp->ready))
				return NULL;
			data = rcu_dereference(tmp->data);
			if (data)
				break;
		}
	}

	if (list_entry_is_head(tmp, &comp->comp_dev_list_head, list))
		return NULL;

	*pos = tmp;
	return data;
}

void mlx5_devcom_comp_lock(struct mlx5_devcom_comp_dev *devcom)
{
	if (IS_ERR_OR_NULL(devcom))
		return;
	down_write(&devcom->comp->sem);
}

void mlx5_devcom_comp_unlock(struct mlx5_devcom_comp_dev *devcom)
{
	if (IS_ERR_OR_NULL(devcom))
		return;
	up_write(&devcom->comp->sem);
}

int mlx5_devcom_comp_trylock(struct mlx5_devcom_comp_dev *devcom)
{
	if (IS_ERR_OR_NULL(devcom))
		return 0;
	return down_write_trylock(&devcom->comp->sem);
}