summaryrefslogtreecommitdiff
path: root/drivers/bus/fsl-mc/fsl-mc-uapi.c
blob: 9c4c1395fcdbf26a8c4ddaae0a948260e9e76d3d (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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// SPDX-License-Identifier: GPL-2.0
/*
 * Management Complex (MC) userspace support
 *
 * Copyright 2021 NXP
 *
 */

#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/miscdevice.h>

#include "fsl-mc-private.h"

struct uapi_priv_data {
	struct fsl_mc_uapi *uapi;
	struct fsl_mc_io *mc_io;
};

struct fsl_mc_cmd_desc {
	u16 cmdid_value;
	u16 cmdid_mask;
	int size;
	bool token;
	int flags;
};

#define FSL_MC_CHECK_MODULE_ID		BIT(0)
#define FSL_MC_CAP_NET_ADMIN_NEEDED	BIT(1)

enum fsl_mc_cmd_index {
	DPDBG_DUMP = 0,
	DPDBG_SET,
	DPRC_GET_CONTAINER_ID,
	DPRC_CREATE_CONT,
	DPRC_DESTROY_CONT,
	DPRC_ASSIGN,
	DPRC_UNASSIGN,
	DPRC_GET_OBJ_COUNT,
	DPRC_GET_OBJ,
	DPRC_GET_RES_COUNT,
	DPRC_GET_RES_IDS,
	DPRC_SET_OBJ_LABEL,
	DPRC_SET_LOCKED,
	DPRC_CONNECT,
	DPRC_DISCONNECT,
	DPRC_GET_POOL,
	DPRC_GET_POOL_COUNT,
	DPRC_GET_CONNECTION,
	DPCI_GET_LINK_STATE,
	DPCI_GET_PEER_ATTR,
	DPAIOP_GET_SL_VERSION,
	DPAIOP_GET_STATE,
	DPMNG_GET_VERSION,
	DPSECI_GET_TX_QUEUE,
	DPMAC_GET_COUNTER,
	DPMAC_GET_MAC_ADDR,
	DPNI_SET_PRIM_MAC,
	DPNI_GET_PRIM_MAC,
	DPNI_GET_STATISTICS,
	DPNI_GET_LINK_STATE,
	DPNI_GET_MAX_FRAME_LENGTH,
	DPSW_GET_TAILDROP,
	DPSW_SET_TAILDROP,
	DPSW_IF_GET_COUNTER,
	DPSW_IF_GET_MAX_FRAME_LENGTH,
	DPDMUX_GET_COUNTER,
	DPDMUX_IF_GET_MAX_FRAME_LENGTH,
	GET_ATTR,
	GET_IRQ_MASK,
	GET_IRQ_STATUS,
	CLOSE,
	OPEN,
	GET_API_VERSION,
	DESTROY,
	CREATE,
};

static struct fsl_mc_cmd_desc fsl_mc_accepted_cmds[] = {
	[DPDBG_DUMP] = {
		.cmdid_value = 0x1300,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 28,
	},
	[DPDBG_SET] = {
		.cmdid_value = 0x1400,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 28,
	},
	[DPRC_GET_CONTAINER_ID] = {
		.cmdid_value = 0x8300,
		.cmdid_mask = 0xFFF0,
		.token = false,
		.size = 8,
	},
	[DPRC_CREATE_CONT] = {
		.cmdid_value = 0x1510,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 40,
		.flags = FSL_MC_CAP_NET_ADMIN_NEEDED,
	},
	[DPRC_DESTROY_CONT] = {
		.cmdid_value = 0x1520,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 12,
		.flags = FSL_MC_CAP_NET_ADMIN_NEEDED,
	},
	[DPRC_ASSIGN] = {
		.cmdid_value = 0x1570,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 40,
		.flags = FSL_MC_CAP_NET_ADMIN_NEEDED,
	},
	[DPRC_UNASSIGN] = {
		.cmdid_value = 0x1580,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 40,
		.flags = FSL_MC_CAP_NET_ADMIN_NEEDED,
	},
	[DPRC_GET_OBJ_COUNT] = {
		.cmdid_value = 0x1590,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 16,
	},
	[DPRC_GET_OBJ] = {
		.cmdid_value = 0x15A0,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 12,
	},
	[DPRC_GET_RES_COUNT] = {
		.cmdid_value = 0x15B0,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 32,
	},
	[DPRC_GET_RES_IDS] = {
		.cmdid_value = 0x15C0,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 40,
	},
	[DPRC_SET_OBJ_LABEL] = {
		.cmdid_value = 0x1610,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 48,
		.flags = FSL_MC_CAP_NET_ADMIN_NEEDED,
	},
	[DPRC_SET_LOCKED] = {
		.cmdid_value = 0x16B0,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 16,
		.flags = FSL_MC_CAP_NET_ADMIN_NEEDED,
	},
	[DPRC_CONNECT] = {
		.cmdid_value = 0x1670,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 56,
		.flags = FSL_MC_CAP_NET_ADMIN_NEEDED,
	},
	[DPRC_DISCONNECT] = {
		.cmdid_value = 0x1680,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 32,
		.flags = FSL_MC_CAP_NET_ADMIN_NEEDED,
	},
	[DPRC_GET_POOL] = {
		.cmdid_value = 0x1690,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 12,
	},
	[DPRC_GET_POOL_COUNT] = {
		.cmdid_value = 0x16A0,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 8,
	},
	[DPRC_GET_CONNECTION] = {
		.cmdid_value = 0x16C0,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 32,
	},

	[DPCI_GET_LINK_STATE] = {
		.cmdid_value = 0x0E10,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 8,
	},
	[DPCI_GET_PEER_ATTR] = {
		.cmdid_value = 0x0E20,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 8,
	},
	[DPAIOP_GET_SL_VERSION] = {
		.cmdid_value = 0x2820,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 8,
	},
	[DPAIOP_GET_STATE] = {
		.cmdid_value = 0x2830,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 8,
	},
	[DPMNG_GET_VERSION] = {
		.cmdid_value = 0x8310,
		.cmdid_mask = 0xFFF0,
		.token = false,
		.size = 8,
	},
	[DPSECI_GET_TX_QUEUE] = {
		.cmdid_value = 0x1970,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 14,
	},
	[DPMAC_GET_COUNTER] = {
		.cmdid_value = 0x0c40,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 9,
	},
	[DPMAC_GET_MAC_ADDR] = {
		.cmdid_value = 0x0c50,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 8,
	},
	[DPNI_SET_PRIM_MAC] = {
		.cmdid_value = 0x2240,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 16,
		.flags = FSL_MC_CAP_NET_ADMIN_NEEDED,
	},
	[DPNI_GET_PRIM_MAC] = {
		.cmdid_value = 0x2250,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 8,
	},
	[DPNI_GET_STATISTICS] = {
		.cmdid_value = 0x25D0,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 10,
	},
	[DPNI_GET_LINK_STATE] = {
		.cmdid_value = 0x2150,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 8,
	},
	[DPNI_GET_MAX_FRAME_LENGTH] = {
		.cmdid_value = 0x2170,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 8,
	},
	[DPSW_GET_TAILDROP] = {
		.cmdid_value = 0x0A80,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 14,
	},
	[DPSW_SET_TAILDROP] = {
		.cmdid_value = 0x0A90,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 24,
		.flags = FSL_MC_CAP_NET_ADMIN_NEEDED,
	},
	[DPSW_IF_GET_COUNTER] = {
		.cmdid_value = 0x0340,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 11,
	},
	[DPSW_IF_GET_MAX_FRAME_LENGTH] = {
		.cmdid_value = 0x0450,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 10,
	},
	[DPDMUX_GET_COUNTER] = {
		.cmdid_value = 0x0b20,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 11,
	},
	[DPDMUX_IF_GET_MAX_FRAME_LENGTH] = {
		.cmdid_value = 0x0a20,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 10,
	},
	[GET_ATTR] = {
		.cmdid_value = 0x0040,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 8,
	},
	[GET_IRQ_MASK] = {
		.cmdid_value = 0x0150,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 13,
	},
	[GET_IRQ_STATUS] = {
		.cmdid_value = 0x0160,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 13,
	},
	[CLOSE] = {
		.cmdid_value = 0x8000,
		.cmdid_mask = 0xFFF0,
		.token = true,
		.size = 8,
	},

	/* Common commands amongst all types of objects. Must be checked last. */
	[OPEN] = {
		.cmdid_value = 0x8000,
		.cmdid_mask = 0xFC00,
		.token = false,
		.size = 12,
		.flags = FSL_MC_CHECK_MODULE_ID,
	},
	[GET_API_VERSION] = {
		.cmdid_value = 0xA000,
		.cmdid_mask = 0xFC00,
		.token = false,
		.size = 8,
		.flags = FSL_MC_CHECK_MODULE_ID,
	},
	[DESTROY] = {
		.cmdid_value = 0x9800,
		.cmdid_mask = 0xFC00,
		.token = true,
		.size = 12,
		.flags = FSL_MC_CHECK_MODULE_ID | FSL_MC_CAP_NET_ADMIN_NEEDED,
	},
	[CREATE] = {
		.cmdid_value = 0x9000,
		.cmdid_mask = 0xFC00,
		.token = true,
		.size = 64,
		.flags = FSL_MC_CHECK_MODULE_ID | FSL_MC_CAP_NET_ADMIN_NEEDED,
	},
};

#define FSL_MC_NUM_ACCEPTED_CMDS ARRAY_SIZE(fsl_mc_accepted_cmds)

#define FSL_MC_MAX_MODULE_ID 0x10

static int fsl_mc_command_check(struct fsl_mc_device *mc_dev,
				struct fsl_mc_command *mc_cmd)
{
	struct fsl_mc_cmd_desc *desc = NULL;
	int mc_cmd_max_size, i;
	bool token_provided;
	u16 cmdid, module_id;
	char *mc_cmd_end;
	char sum = 0;

	/* Check if this is an accepted MC command */
	cmdid = mc_cmd_hdr_read_cmdid(mc_cmd);
	for (i = 0; i < FSL_MC_NUM_ACCEPTED_CMDS; i++) {
		desc = &fsl_mc_accepted_cmds[i];
		if ((cmdid & desc->cmdid_mask) == desc->cmdid_value)
			break;
	}
	if (i == FSL_MC_NUM_ACCEPTED_CMDS) {
		dev_err(&mc_dev->dev, "MC command 0x%04x: cmdid not accepted\n", cmdid);
		return -EACCES;
	}

	/* Check if the size of the command is honored. Anything beyond the
	 * last valid byte of the command should be zeroed.
	 */
	mc_cmd_max_size = sizeof(*mc_cmd);
	mc_cmd_end = ((char *)mc_cmd) + desc->size;
	for (i = desc->size; i < mc_cmd_max_size; i++)
		sum |= *mc_cmd_end++;
	if (sum) {
		dev_err(&mc_dev->dev, "MC command 0x%04x: garbage beyond max size of %d bytes!\n",
			cmdid, desc->size);
		return -EACCES;
	}

	/* Some MC commands request a token to be passed so that object
	 * identification is possible. Check if the token passed in the command
	 * is as expected.
	 */
	token_provided = mc_cmd_hdr_read_token(mc_cmd) ? true : false;
	if (token_provided != desc->token) {
		dev_err(&mc_dev->dev, "MC command 0x%04x: token 0x%04x is invalid!\n",
			cmdid, mc_cmd_hdr_read_token(mc_cmd));
		return -EACCES;
	}

	/* If needed, check if the module ID passed is valid */
	if (desc->flags & FSL_MC_CHECK_MODULE_ID) {
		/* The module ID is represented by bits [4:9] from the cmdid */
		module_id = (cmdid & GENMASK(9, 4)) >> 4;
		if (module_id == 0 || module_id > FSL_MC_MAX_MODULE_ID) {
			dev_err(&mc_dev->dev, "MC command 0x%04x: unknown module ID 0x%x\n",
				cmdid, module_id);
			return -EACCES;
		}
	}

	/* Some commands alter how hardware resources are managed. For these
	 * commands, check for CAP_NET_ADMIN.
	 */
	if (desc->flags & FSL_MC_CAP_NET_ADMIN_NEEDED) {
		if (!capable(CAP_NET_ADMIN)) {
			dev_err(&mc_dev->dev, "MC command 0x%04x: needs CAP_NET_ADMIN!\n",
				cmdid);
			return -EPERM;
		}
	}

	return 0;
}

static int fsl_mc_uapi_send_command(struct fsl_mc_device *mc_dev, unsigned long arg,
				    struct fsl_mc_io *mc_io)
{
	struct fsl_mc_command mc_cmd;
	int error;

	error = copy_from_user(&mc_cmd, (void __user *)arg, sizeof(mc_cmd));
	if (error)
		return -EFAULT;

	error = fsl_mc_command_check(mc_dev, &mc_cmd);
	if (error)
		return error;

	error = mc_send_command(mc_io, &mc_cmd);
	if (error)
		return error;

	error = copy_to_user((void __user *)arg, &mc_cmd, sizeof(mc_cmd));
	if (error)
		return -EFAULT;

	return 0;
}

static int fsl_mc_uapi_dev_open(struct inode *inode, struct file *filep)
{
	struct fsl_mc_device *root_mc_device;
	struct uapi_priv_data *priv_data;
	struct fsl_mc_io *dynamic_mc_io;
	struct fsl_mc_uapi *mc_uapi;
	struct fsl_mc_bus *mc_bus;
	int error;

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

	mc_uapi = container_of(filep->private_data, struct fsl_mc_uapi, misc);
	mc_bus = container_of(mc_uapi, struct fsl_mc_bus, uapi_misc);
	root_mc_device = &mc_bus->mc_dev;

	mutex_lock(&mc_uapi->mutex);

	if (!mc_uapi->local_instance_in_use) {
		priv_data->mc_io = mc_uapi->static_mc_io;
		mc_uapi->local_instance_in_use = 1;
	} else {
		error = fsl_mc_portal_allocate(root_mc_device, 0,
					       &dynamic_mc_io);
		if (error) {
			dev_dbg(&root_mc_device->dev,
				"Could not allocate MC portal\n");
			goto error_portal_allocate;
		}

		priv_data->mc_io = dynamic_mc_io;
	}
	priv_data->uapi = mc_uapi;
	filep->private_data = priv_data;

	mutex_unlock(&mc_uapi->mutex);

	return 0;

error_portal_allocate:
	mutex_unlock(&mc_uapi->mutex);
	kfree(priv_data);

	return error;
}

static int fsl_mc_uapi_dev_release(struct inode *inode, struct file *filep)
{
	struct uapi_priv_data *priv_data;
	struct fsl_mc_uapi *mc_uapi;
	struct fsl_mc_io *mc_io;

	priv_data = filep->private_data;
	mc_uapi = priv_data->uapi;
	mc_io = priv_data->mc_io;

	mutex_lock(&mc_uapi->mutex);

	if (mc_io == mc_uapi->static_mc_io)
		mc_uapi->local_instance_in_use = 0;
	else
		fsl_mc_portal_free(mc_io);

	kfree(filep->private_data);
	filep->private_data =  NULL;

	mutex_unlock(&mc_uapi->mutex);

	return 0;
}

static long fsl_mc_uapi_dev_ioctl(struct file *file,
				  unsigned int cmd,
				  unsigned long arg)
{
	struct uapi_priv_data *priv_data = file->private_data;
	struct fsl_mc_device *root_mc_device;
	struct fsl_mc_bus *mc_bus;
	int error;

	mc_bus = container_of(priv_data->uapi, struct fsl_mc_bus, uapi_misc);
	root_mc_device = &mc_bus->mc_dev;

	switch (cmd) {
	case FSL_MC_SEND_MC_COMMAND:
		error = fsl_mc_uapi_send_command(root_mc_device, arg, priv_data->mc_io);
		break;
	default:
		dev_dbg(&root_mc_device->dev, "unexpected ioctl call number\n");
		error = -EINVAL;
	}

	return error;
}

static const struct file_operations fsl_mc_uapi_dev_fops = {
	.owner = THIS_MODULE,
	.open = fsl_mc_uapi_dev_open,
	.release = fsl_mc_uapi_dev_release,
	.unlocked_ioctl = fsl_mc_uapi_dev_ioctl,
};

int fsl_mc_uapi_create_device_file(struct fsl_mc_bus *mc_bus)
{
	struct fsl_mc_device *mc_dev = &mc_bus->mc_dev;
	struct fsl_mc_uapi *mc_uapi = &mc_bus->uapi_misc;
	int error;

	mc_uapi->misc.minor = MISC_DYNAMIC_MINOR;
	mc_uapi->misc.name = dev_name(&mc_dev->dev);
	mc_uapi->misc.fops = &fsl_mc_uapi_dev_fops;

	error = misc_register(&mc_uapi->misc);
	if (error)
		return error;

	mc_uapi->static_mc_io = mc_bus->mc_dev.mc_io;

	mutex_init(&mc_uapi->mutex);

	return 0;
}

void fsl_mc_uapi_remove_device_file(struct fsl_mc_bus *mc_bus)
{
	misc_deregister(&mc_bus->uapi_misc.misc);
}