summaryrefslogtreecommitdiff
path: root/drivers/crypto/intel/qat/qat_common/adf_sysfs_rl.c
blob: abf9c52474eca9d97ff32545e1b1b5013b8540d6 (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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright(c) 2023 Intel Corporation */

#define dev_fmt(fmt) "RateLimiting: " fmt

#include <linux/dev_printk.h>
#include <linux/pci.h>
#include <linux/sysfs.h>
#include <linux/types.h>

#include "adf_common_drv.h"
#include "adf_rl.h"
#include "adf_sysfs_rl.h"

#define GET_RL_STRUCT(accel_dev) ((accel_dev)->rate_limiting->user_input)

enum rl_ops {
	ADD,
	UPDATE,
	RM,
	RM_ALL,
	GET,
};

enum rl_params {
	RP_MASK,
	ID,
	CIR,
	PIR,
	SRV,
	CAP_REM_SRV,
};

static const char *const rl_services[] = {
	[ADF_SVC_ASYM] = "asym",
	[ADF_SVC_SYM] = "sym",
	[ADF_SVC_DC] = "dc",
};

static const char *const rl_operations[] = {
	[ADD] = "add",
	[UPDATE] = "update",
	[RM] = "rm",
	[RM_ALL] = "rm_all",
	[GET] = "get",
};

static int set_param_u(struct device *dev, enum rl_params param, u64 set)
{
	struct adf_rl_interface_data *data;
	struct adf_accel_dev *accel_dev;
	int ret = 0;

	accel_dev = adf_devmgr_pci_to_accel_dev(to_pci_dev(dev));
	if (!accel_dev)
		return -EINVAL;

	data = &GET_RL_STRUCT(accel_dev);

	down_write(&data->lock);
	switch (param) {
	case RP_MASK:
		data->input.rp_mask = set;
		break;
	case CIR:
		data->input.cir = set;
		break;
	case PIR:
		data->input.pir = set;
		break;
	case SRV:
		data->input.srv = set;
		break;
	case CAP_REM_SRV:
		data->cap_rem_srv = set;
		break;
	default:
		ret = -EINVAL;
		break;
	}
	up_write(&data->lock);

	return ret;
}

static int set_param_s(struct device *dev, enum rl_params param, int set)
{
	struct adf_rl_interface_data *data;
	struct adf_accel_dev *accel_dev;

	accel_dev = adf_devmgr_pci_to_accel_dev(to_pci_dev(dev));
	if (!accel_dev || param != ID)
		return -EINVAL;

	data = &GET_RL_STRUCT(accel_dev);

	down_write(&data->lock);
	data->input.sla_id = set;
	up_write(&data->lock);

	return 0;
}

static int get_param_u(struct device *dev, enum rl_params param, u64 *get)
{
	struct adf_rl_interface_data *data;
	struct adf_accel_dev *accel_dev;
	int ret = 0;

	accel_dev = adf_devmgr_pci_to_accel_dev(to_pci_dev(dev));
	if (!accel_dev)
		return -EINVAL;

	data = &GET_RL_STRUCT(accel_dev);

	down_read(&data->lock);
	switch (param) {
	case RP_MASK:
		*get = data->input.rp_mask;
		break;
	case CIR:
		*get = data->input.cir;
		break;
	case PIR:
		*get = data->input.pir;
		break;
	case SRV:
		*get = data->input.srv;
		break;
	default:
		ret = -EINVAL;
	}
	up_read(&data->lock);

	return ret;
}

static int get_param_s(struct device *dev, enum rl_params param)
{
	struct adf_rl_interface_data *data;
	struct adf_accel_dev *accel_dev;
	int ret = 0;

	accel_dev = adf_devmgr_pci_to_accel_dev(to_pci_dev(dev));
	if (!accel_dev)
		return -EINVAL;

	data = &GET_RL_STRUCT(accel_dev);

	down_read(&data->lock);
	if (param == ID)
		ret = data->input.sla_id;
	up_read(&data->lock);

	return ret;
}

static ssize_t rp_show(struct device *dev, struct device_attribute *attr,
		       char *buf)
{
	int ret;
	u64 get;

	ret = get_param_u(dev, RP_MASK, &get);
	if (ret)
		return ret;

	return sysfs_emit(buf, "%#llx\n", get);
}

static ssize_t rp_store(struct device *dev, struct device_attribute *attr,
			const char *buf, size_t count)
{
	int err;
	u64 val;

	err = kstrtou64(buf, 16, &val);
	if (err)
		return err;

	err = set_param_u(dev, RP_MASK, val);
	if (err)
		return err;

	return count;
}
static DEVICE_ATTR_RW(rp);

static ssize_t id_show(struct device *dev, struct device_attribute *attr,
		       char *buf)
{
	return sysfs_emit(buf, "%d\n", get_param_s(dev, ID));
}

static ssize_t id_store(struct device *dev, struct device_attribute *attr,
			const char *buf, size_t count)
{
	int err;
	int val;

	err = kstrtoint(buf, 10, &val);
	if (err)
		return err;

	err = set_param_s(dev, ID, val);
	if (err)
		return err;

	return count;
}
static DEVICE_ATTR_RW(id);

static ssize_t cir_show(struct device *dev, struct device_attribute *attr,
			char *buf)
{
	int ret;
	u64 get;

	ret = get_param_u(dev, CIR, &get);
	if (ret)
		return ret;

	return sysfs_emit(buf, "%llu\n", get);
}

static ssize_t cir_store(struct device *dev, struct device_attribute *attr,
			 const char *buf, size_t count)
{
	unsigned int val;
	int err;

	err = kstrtouint(buf, 10, &val);
	if (err)
		return err;

	err = set_param_u(dev, CIR, val);
	if (err)
		return err;

	return count;
}
static DEVICE_ATTR_RW(cir);

static ssize_t pir_show(struct device *dev, struct device_attribute *attr,
			char *buf)
{
	int ret;
	u64 get;

	ret = get_param_u(dev, PIR, &get);
	if (ret)
		return ret;

	return sysfs_emit(buf, "%llu\n", get);
}

static ssize_t pir_store(struct device *dev, struct device_attribute *attr,
			 const char *buf, size_t count)
{
	unsigned int val;
	int err;

	err = kstrtouint(buf, 10, &val);
	if (err)
		return err;

	err = set_param_u(dev, PIR, val);
	if (err)
		return err;

	return count;
}
static DEVICE_ATTR_RW(pir);

static ssize_t srv_show(struct device *dev, struct device_attribute *attr,
			char *buf)
{
	int ret;
	u64 get;

	ret = get_param_u(dev, SRV, &get);
	if (ret)
		return ret;

	if (get == ADF_SVC_NONE)
		return -EINVAL;

	return sysfs_emit(buf, "%s\n", rl_services[get]);
}

static ssize_t srv_store(struct device *dev, struct device_attribute *attr,
			 const char *buf, size_t count)
{
	unsigned int val;
	int ret;

	ret = sysfs_match_string(rl_services, buf);
	if (ret < 0)
		return ret;

	val = ret;
	ret = set_param_u(dev, SRV, val);
	if (ret)
		return ret;

	return count;
}
static DEVICE_ATTR_RW(srv);

static ssize_t cap_rem_show(struct device *dev, struct device_attribute *attr,
			    char *buf)
{
	struct adf_rl_interface_data *data;
	struct adf_accel_dev *accel_dev;
	int ret, rem_cap;

	accel_dev = adf_devmgr_pci_to_accel_dev(to_pci_dev(dev));
	if (!accel_dev)
		return -EINVAL;

	data = &GET_RL_STRUCT(accel_dev);

	down_read(&data->lock);
	rem_cap = adf_rl_get_capability_remaining(accel_dev, data->cap_rem_srv,
						  RL_SLA_EMPTY_ID);
	up_read(&data->lock);
	if (rem_cap < 0)
		return rem_cap;

	ret = sysfs_emit(buf, "%u\n", rem_cap);

	return ret;
}

static ssize_t cap_rem_store(struct device *dev, struct device_attribute *attr,
			     const char *buf, size_t count)
{
	unsigned int val;
	int ret;

	ret = sysfs_match_string(rl_services, buf);
	if (ret < 0)
		return ret;

	val = ret;
	ret = set_param_u(dev, CAP_REM_SRV, val);
	if (ret)
		return ret;

	return count;
}
static DEVICE_ATTR_RW(cap_rem);

static ssize_t sla_op_store(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
{
	struct adf_rl_interface_data *data;
	struct adf_accel_dev *accel_dev;
	int ret;

	accel_dev = adf_devmgr_pci_to_accel_dev(to_pci_dev(dev));
	if (!accel_dev)
		return -EINVAL;

	data = &GET_RL_STRUCT(accel_dev);

	ret = sysfs_match_string(rl_operations, buf);
	if (ret < 0)
		return ret;

	down_write(&data->lock);
	switch (ret) {
	case ADD:
		data->input.parent_id = RL_PARENT_DEFAULT_ID;
		data->input.type = RL_LEAF;
		data->input.sla_id = 0;
		ret = adf_rl_add_sla(accel_dev, &data->input);
		if (ret)
			goto err_free_lock;
		break;
	case UPDATE:
		ret = adf_rl_update_sla(accel_dev, &data->input);
		if (ret)
			goto err_free_lock;
		break;
	case RM:
		ret = adf_rl_remove_sla(accel_dev, data->input.sla_id);
		if (ret)
			goto err_free_lock;
		break;
	case RM_ALL:
		adf_rl_remove_sla_all(accel_dev, false);
		break;
	case GET:
		ret = adf_rl_get_sla(accel_dev, &data->input);
		if (ret)
			goto err_free_lock;
		break;
	default:
		ret = -EINVAL;
		goto err_free_lock;
	}
	up_write(&data->lock);

	return count;

err_free_lock:
	up_write(&data->lock);

	return ret;
}
static DEVICE_ATTR_WO(sla_op);

static struct attribute *qat_rl_attrs[] = {
	&dev_attr_rp.attr,
	&dev_attr_id.attr,
	&dev_attr_cir.attr,
	&dev_attr_pir.attr,
	&dev_attr_srv.attr,
	&dev_attr_cap_rem.attr,
	&dev_attr_sla_op.attr,
	NULL,
};

static struct attribute_group qat_rl_group = {
	.attrs = qat_rl_attrs,
	.name = "qat_rl",
};

int adf_sysfs_rl_add(struct adf_accel_dev *accel_dev)
{
	struct adf_rl_interface_data *data;
	int ret;

	data = &GET_RL_STRUCT(accel_dev);

	ret = device_add_group(&GET_DEV(accel_dev), &qat_rl_group);
	if (ret)
		dev_err(&GET_DEV(accel_dev),
			"Failed to create qat_rl attribute group\n");

	data->cap_rem_srv = ADF_SVC_NONE;
	data->input.srv = ADF_SVC_NONE;

	return ret;
}

void adf_sysfs_rl_rm(struct adf_accel_dev *accel_dev)
{
	device_remove_group(&GET_DEV(accel_dev), &qat_rl_group);
}