1/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2#ifndef _UAPI_LINUX_IOPRIO_H
3#define _UAPI_LINUX_IOPRIO_H
4
5#include <linux/stddef.h>
6#include <linux/types.h>
7
8/*
9 * Gives us 8 prio classes with 13-bits of data for each class
10 */
11#define IOPRIO_CLASS_SHIFT	13
12#define IOPRIO_NR_CLASSES	8
13#define IOPRIO_CLASS_MASK	(IOPRIO_NR_CLASSES - 1)
14#define IOPRIO_PRIO_MASK	((1UL << IOPRIO_CLASS_SHIFT) - 1)
15
16#define IOPRIO_PRIO_CLASS(ioprio)	\
17	(((ioprio) >> IOPRIO_CLASS_SHIFT) & IOPRIO_CLASS_MASK)
18#define IOPRIO_PRIO_DATA(ioprio)	((ioprio) & IOPRIO_PRIO_MASK)
19
20/*
21 * These are the io priority classes as implemented by the BFQ and mq-deadline
22 * schedulers. RT is the realtime class, it always gets premium service. For
23 * ATA disks supporting NCQ IO priority, RT class IOs will be processed using
24 * high priority NCQ commands. BE is the best-effort scheduling class, the
25 * default for any process. IDLE is the idle scheduling class, it is only
26 * served when no one else is using the disk.
27 */
28enum {
29	IOPRIO_CLASS_NONE	= 0,
30	IOPRIO_CLASS_RT		= 1,
31	IOPRIO_CLASS_BE		= 2,
32	IOPRIO_CLASS_IDLE	= 3,
33
34	/* Special class to indicate an invalid ioprio value */
35	IOPRIO_CLASS_INVALID	= 7,
36};
37
38/*
39 * The RT and BE priority classes both support up to 8 priority levels that
40 * can be specified using the lower 3-bits of the priority data.
41 */
42#define IOPRIO_LEVEL_NR_BITS		3
43#define IOPRIO_NR_LEVELS		(1 << IOPRIO_LEVEL_NR_BITS)
44#define IOPRIO_LEVEL_MASK		(IOPRIO_NR_LEVELS - 1)
45#define IOPRIO_PRIO_LEVEL(ioprio)	((ioprio) & IOPRIO_LEVEL_MASK)
46
47#define IOPRIO_BE_NR			IOPRIO_NR_LEVELS
48
49/*
50 * Possible values for the "which" argument of the ioprio_get() and
51 * ioprio_set() system calls (see "man ioprio_set").
52 */
53enum {
54	IOPRIO_WHO_PROCESS = 1,
55	IOPRIO_WHO_PGRP,
56	IOPRIO_WHO_USER,
57};
58
59/*
60 * Fallback BE class priority level.
61 */
62#define IOPRIO_NORM	4
63#define IOPRIO_BE_NORM	IOPRIO_NORM
64
65/*
66 * The 10 bits between the priority class and the priority level are used to
67 * optionally define I/O hints for any combination of I/O priority class and
68 * level. Depending on the kernel configuration, I/O scheduler being used and
69 * the target I/O device being used, hints can influence how I/Os are processed
70 * without affecting the I/O scheduling ordering defined by the I/O priority
71 * class and level.
72 */
73#define IOPRIO_HINT_SHIFT		IOPRIO_LEVEL_NR_BITS
74#define IOPRIO_HINT_NR_BITS		10
75#define IOPRIO_NR_HINTS			(1 << IOPRIO_HINT_NR_BITS)
76#define IOPRIO_HINT_MASK		(IOPRIO_NR_HINTS - 1)
77#define IOPRIO_PRIO_HINT(ioprio)	\
78	(((ioprio) >> IOPRIO_HINT_SHIFT) & IOPRIO_HINT_MASK)
79
80/*
81 * I/O hints.
82 */
83enum {
84	/* No hint */
85	IOPRIO_HINT_NONE = 0,
86
87	/*
88	 * Device command duration limits: indicate to the device a desired
89	 * duration limit for the commands that will be used to process an I/O.
90	 * These will currently only be effective for SCSI and ATA devices that
91	 * support the command duration limits feature. If this feature is
92	 * enabled, then the commands issued to the device to process an I/O with
93	 * one of these hints set will have the duration limit index (dld field)
94	 * set to the value of the hint.
95	 */
96	IOPRIO_HINT_DEV_DURATION_LIMIT_1 = 1,
97	IOPRIO_HINT_DEV_DURATION_LIMIT_2 = 2,
98	IOPRIO_HINT_DEV_DURATION_LIMIT_3 = 3,
99	IOPRIO_HINT_DEV_DURATION_LIMIT_4 = 4,
100	IOPRIO_HINT_DEV_DURATION_LIMIT_5 = 5,
101	IOPRIO_HINT_DEV_DURATION_LIMIT_6 = 6,
102	IOPRIO_HINT_DEV_DURATION_LIMIT_7 = 7,
103};
104
105#define IOPRIO_BAD_VALUE(val, max) ((val) < 0 || (val) >= (max))
106
107/*
108 * Return an I/O priority value based on a class, a level and a hint.
109 */
110static __always_inline __u16 ioprio_value(int prioclass, int priolevel,
111					  int priohint)
112{
113	if (IOPRIO_BAD_VALUE(prioclass, IOPRIO_NR_CLASSES) ||
114	    IOPRIO_BAD_VALUE(priolevel, IOPRIO_NR_LEVELS) ||
115	    IOPRIO_BAD_VALUE(priohint, IOPRIO_NR_HINTS))
116		return IOPRIO_CLASS_INVALID << IOPRIO_CLASS_SHIFT;
117
118	return (prioclass << IOPRIO_CLASS_SHIFT) |
119		(priohint << IOPRIO_HINT_SHIFT) | priolevel;
120}
121
122#define IOPRIO_PRIO_VALUE(prioclass, priolevel)			\
123	ioprio_value(prioclass, priolevel, IOPRIO_HINT_NONE)
124#define IOPRIO_PRIO_VALUE_HINT(prioclass, priolevel, priohint)	\
125	ioprio_value(prioclass, priolevel, priohint)
126
127#endif /* _UAPI_LINUX_IOPRIO_H */
128