1f08c3bdfSopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2019 Linus Walleij <linus.walleij@linaro.org>
4f08c3bdfSopenharmony_ci * Copyright (c) 2023 Linux Test Project
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci#ifndef LTP_IOPRIO_H
8f08c3bdfSopenharmony_ci#define LTP_IOPRIO_H
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci#include "lapi/ioprio.h"
11f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_cistatic const char * const to_class_str[] = {
14f08c3bdfSopenharmony_ci	[IOPRIO_CLASS_NONE] = "NONE",
15f08c3bdfSopenharmony_ci	[IOPRIO_CLASS_RT]   = "REALTIME",
16f08c3bdfSopenharmony_ci	[IOPRIO_CLASS_BE]   = "BEST-EFFORT",
17f08c3bdfSopenharmony_ci	[IOPRIO_CLASS_IDLE] = "IDLE"
18f08c3bdfSopenharmony_ci};
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic inline int sys_ioprio_get(int which, int who)
21f08c3bdfSopenharmony_ci{
22f08c3bdfSopenharmony_ci	return tst_syscall(__NR_ioprio_get, which, who);
23f08c3bdfSopenharmony_ci}
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic inline int sys_ioprio_set(int which, int who, int ioprio)
26f08c3bdfSopenharmony_ci{
27f08c3bdfSopenharmony_ci	return tst_syscall(__NR_ioprio_set, which, who, ioprio);
28f08c3bdfSopenharmony_ci}
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci/* Priority range from 0 (highest) to IOPRIO_PRIO_NUM (lowest) */
31f08c3bdfSopenharmony_cistatic inline int prio_in_range(int prio)
32f08c3bdfSopenharmony_ci{
33f08c3bdfSopenharmony_ci	if ((prio < 0) || (prio >= IOPRIO_PRIO_NUM))
34f08c3bdfSopenharmony_ci		return 0;
35f08c3bdfSopenharmony_ci	return 1;
36f08c3bdfSopenharmony_ci}
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci/* Priority range from 0 to 3 using the enum */
39f08c3bdfSopenharmony_cistatic inline int class_in_range(int class)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	if ((class < IOPRIO_CLASS_NONE) || (class > IOPRIO_CLASS_IDLE))
42f08c3bdfSopenharmony_ci		return 0;
43f08c3bdfSopenharmony_ci	return 1;
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic inline void ioprio_check_setting(int class, int prio, int report)
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	int res;
49f08c3bdfSopenharmony_ci	int newclass, newprio;
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	res = sys_ioprio_get(IOPRIO_WHO_PROCESS, 0);
52f08c3bdfSopenharmony_ci	if (res == -1) {
53f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO,
54f08c3bdfSopenharmony_ci			 "reading back prio failed");
55f08c3bdfSopenharmony_ci		return;
56f08c3bdfSopenharmony_ci	}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	newclass = IOPRIO_PRIO_CLASS(res);
59f08c3bdfSopenharmony_ci	newprio = IOPRIO_PRIO_LEVEL(res);
60f08c3bdfSopenharmony_ci	if (newclass != class)
61f08c3bdfSopenharmony_ci		tst_res(TFAIL,
62f08c3bdfSopenharmony_ci			"wrong class after setting, expected %s got %s",
63f08c3bdfSopenharmony_ci			to_class_str[class],
64f08c3bdfSopenharmony_ci			to_class_str[newclass]);
65f08c3bdfSopenharmony_ci	else if (newprio != prio)
66f08c3bdfSopenharmony_ci		tst_res(TFAIL,
67f08c3bdfSopenharmony_ci			"wrong prio after setting, expected %d got %d",
68f08c3bdfSopenharmony_ci			prio, newprio);
69f08c3bdfSopenharmony_ci	else if (report)
70f08c3bdfSopenharmony_ci		tst_res(TPASS, "ioprio_set new class %s, new prio %d",
71f08c3bdfSopenharmony_ci			to_class_str[newclass],
72f08c3bdfSopenharmony_ci			newprio);
73f08c3bdfSopenharmony_ci}
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ci#endif /* LTP_IOPRIO_H */
76