1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_VIRTIO_CONFIG_H
3 #define _LINUX_VIRTIO_CONFIG_H
4 
5 #include <linux/err.h>
6 #include <linux/bug.h>
7 #include <linux/virtio.h>
8 #include <linux/virtio_byteorder.h>
9 #include <uapi/linux/virtio_config.h>
10 
11 struct irq_affinity;
12 
13 /**
14  * virtio_config_ops - operations for configuring a virtio device
15  * @get: read the value of a configuration field
16  *	vdev: the virtio_device
17  *	offset: the offset of the configuration field
18  *	buf: the buffer to write the field value into.
19  *	len: the length of the buffer
20  * @set: write the value of a configuration field
21  *	vdev: the virtio_device
22  *	offset: the offset of the configuration field
23  *	buf: the buffer to read the field value from.
24  *	len: the length of the buffer
25  * @generation: config generation counter
26  *	vdev: the virtio_device
27  *	Returns the config generation counter
28  * @get_status: read the status byte
29  *	vdev: the virtio_device
30  *	Returns the status byte
31  * @set_status: write the status byte
32  *	vdev: the virtio_device
33  *	status: the new status byte
34  * @reset: reset the device
35  *	vdev: the virtio device
36  *	After this, status and feature negotiation must be done again
37  *	Device must not be reset from its vq/config callbacks, or in
38  *	parallel with being added/removed.
39  * @find_vqs: find virtqueues and instantiate them.
40  *	vdev: the virtio_device
41  *	nvqs: the number of virtqueues to find
42  *	vqs: on success, includes new virtqueues
43  *	callbacks: array of callbacks, for each virtqueue
44  *		include a NULL entry for vqs that do not need a callback
45  *	names: array of virtqueue names (mainly for debugging)
46  *		include a NULL entry for vqs unused by driver
47  *	Returns 0 on success or error status
48  * @del_vqs: free virtqueues found by find_vqs().
49  * @get_features: get the array of feature bits for this device.
50  *	vdev: the virtio_device
51  *	Returns the first 32 feature bits (all we currently need).
52  * @finalize_features: confirm what device features we'll be using.
53  *	vdev: the virtio_device
54  *	This sends the driver feature bits to the device: it can change
55  *	the dev->feature bits if it wants.
56  * Note: despite the name this can be called any number of times.
57  *	Returns 0 on success or error status
58  * @bus_name: return the bus name associated with the device
59  *	vdev: the virtio_device
60  *      This returns a pointer to the bus name a la pci_name from which
61  *      the caller can then copy.
62  * @set_vq_affinity: set the affinity for a virtqueue.
63  * @get_vq_affinity: get the affinity for a virtqueue (optional).
64  */
65 typedef void vq_callback_t(struct virtqueue *);
66 struct virtio_config_ops {
67 	void (*get)(struct virtio_device *vdev, unsigned offset,
68 		    void *buf, unsigned len);
69 	void (*set)(struct virtio_device *vdev, unsigned offset,
70 		    const void *buf, unsigned len);
71 	u32 (*generation)(struct virtio_device *vdev);
72 	u8 (*get_status)(struct virtio_device *vdev);
73 	void (*set_status)(struct virtio_device *vdev, u8 status);
74 	void (*reset)(struct virtio_device *vdev);
75 	int (*find_vqs)(struct virtio_device *, unsigned nvqs,
76 			struct virtqueue *vqs[], vq_callback_t *callbacks[],
77 			const char * const names[], const bool *ctx,
78 			struct irq_affinity *desc);
79 	void (*del_vqs)(struct virtio_device *);
80 	u64 (*get_features)(struct virtio_device *vdev);
81 	int (*finalize_features)(struct virtio_device *vdev);
82 	const char *(*bus_name)(struct virtio_device *vdev);
83 	int (*set_vq_affinity)(struct virtqueue *vq,
84 			       const struct cpumask *cpu_mask);
85 	const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev,
86 			int index);
87 };
88 
89 /* If driver didn't advertise the feature, it will never appear. */
90 void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
91 					 unsigned int fbit);
92 
93 /**
94  * __virtio_test_bit - helper to test feature bits. For use by transports.
95  *                     Devices should normally use virtio_has_feature,
96  *                     which includes more checks.
97  * @vdev: the device
98  * @fbit: the feature bit
99  */
__virtio_test_bit(const struct virtio_device * vdev,unsigned int fbit)100 static inline bool __virtio_test_bit(const struct virtio_device *vdev,
101 				     unsigned int fbit)
102 {
103 	/* Did you forget to fix assumptions on max features? */
104 	if (__builtin_constant_p(fbit))
105 		BUILD_BUG_ON(fbit >= 64);
106 	else
107 		BUG_ON(fbit >= 64);
108 
109 	return vdev->features & BIT_ULL(fbit);
110 }
111 
112 /**
113  * __virtio_set_bit - helper to set feature bits. For use by transports.
114  * @vdev: the device
115  * @fbit: the feature bit
116  */
__virtio_set_bit(struct virtio_device * vdev,unsigned int fbit)117 static inline void __virtio_set_bit(struct virtio_device *vdev,
118 				    unsigned int fbit)
119 {
120 	/* Did you forget to fix assumptions on max features? */
121 	if (__builtin_constant_p(fbit))
122 		BUILD_BUG_ON(fbit >= 64);
123 	else
124 		BUG_ON(fbit >= 64);
125 
126 	vdev->features |= BIT_ULL(fbit);
127 }
128 
129 /**
130  * __virtio_clear_bit - helper to clear feature bits. For use by transports.
131  * @vdev: the device
132  * @fbit: the feature bit
133  */
__virtio_clear_bit(struct virtio_device * vdev,unsigned int fbit)134 static inline void __virtio_clear_bit(struct virtio_device *vdev,
135 				      unsigned int fbit)
136 {
137 	/* Did you forget to fix assumptions on max features? */
138 	if (__builtin_constant_p(fbit))
139 		BUILD_BUG_ON(fbit >= 64);
140 	else
141 		BUG_ON(fbit >= 64);
142 
143 	vdev->features &= ~BIT_ULL(fbit);
144 }
145 
146 /**
147  * virtio_has_feature - helper to determine if this device has this feature.
148  * @vdev: the device
149  * @fbit: the feature bit
150  */
virtio_has_feature(const struct virtio_device * vdev,unsigned int fbit)151 static inline bool virtio_has_feature(const struct virtio_device *vdev,
152 				      unsigned int fbit)
153 {
154 	if (fbit < VIRTIO_TRANSPORT_F_START)
155 		virtio_check_driver_offered_feature(vdev, fbit);
156 
157 	return __virtio_test_bit(vdev, fbit);
158 }
159 
160 /**
161  * virtio_has_iommu_quirk - determine whether this device has the iommu quirk
162  * @vdev: the device
163  */
virtio_has_iommu_quirk(const struct virtio_device * vdev)164 static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev)
165 {
166 	/*
167 	 * Note the reverse polarity of the quirk feature (compared to most
168 	 * other features), this is for compatibility with legacy systems.
169 	 */
170 	return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
171 }
172 
173 static inline
virtio_find_single_vq(struct virtio_device * vdev,vq_callback_t * c,const char * n)174 struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
175 					vq_callback_t *c, const char *n)
176 {
177 	vq_callback_t *callbacks[] = { c };
178 	const char *names[] = { n };
179 	struct virtqueue *vq;
180 	int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL,
181 					 NULL);
182 	if (err < 0)
183 		return ERR_PTR(err);
184 	return vq;
185 }
186 
187 static inline
virtio_find_vqs(struct virtio_device * vdev,unsigned nvqs,struct virtqueue * vqs[],vq_callback_t * callbacks[],const char * const names[],struct irq_affinity * desc)188 int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
189 			struct virtqueue *vqs[], vq_callback_t *callbacks[],
190 			const char * const names[],
191 			struct irq_affinity *desc)
192 {
193 	return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc);
194 }
195 
196 static inline
virtio_find_vqs_ctx(struct virtio_device * vdev,unsigned nvqs,struct virtqueue * vqs[],vq_callback_t * callbacks[],const char * const names[],const bool * ctx,struct irq_affinity * desc)197 int virtio_find_vqs_ctx(struct virtio_device *vdev, unsigned nvqs,
198 			struct virtqueue *vqs[], vq_callback_t *callbacks[],
199 			const char * const names[], const bool *ctx,
200 			struct irq_affinity *desc)
201 {
202 	return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, ctx,
203 				      desc);
204 }
205 
206 /**
207  * virtio_device_ready - enable vq use in probe function
208  * @vdev: the device
209  *
210  * Driver must call this to use vqs in the probe function.
211  *
212  * Note: vqs are enabled automatically after probe returns.
213  */
214 static inline
virtio_device_ready(struct virtio_device * dev)215 void virtio_device_ready(struct virtio_device *dev)
216 {
217 	unsigned status = dev->config->get_status(dev);
218 
219 	BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
220 	dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
221 }
222 
223 static inline
virtio_bus_name(struct virtio_device * vdev)224 const char *virtio_bus_name(struct virtio_device *vdev)
225 {
226 	if (!vdev->config->bus_name)
227 		return "virtio";
228 	return vdev->config->bus_name(vdev);
229 }
230 
231 /**
232  * virtqueue_set_affinity - setting affinity for a virtqueue
233  * @vq: the virtqueue
234  * @cpu: the cpu no.
235  *
236  * Pay attention the function are best-effort: the affinity hint may not be set
237  * due to config support, irq type and sharing.
238  *
239  */
240 static inline
virtqueue_set_affinity(struct virtqueue * vq,const struct cpumask * cpu_mask)241 int virtqueue_set_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask)
242 {
243 	struct virtio_device *vdev = vq->vdev;
244 	if (vdev->config->set_vq_affinity)
245 		return vdev->config->set_vq_affinity(vq, cpu_mask);
246 	return 0;
247 }
248 
virtio_is_little_endian(struct virtio_device * vdev)249 static inline bool virtio_is_little_endian(struct virtio_device *vdev)
250 {
251 	return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
252 		virtio_legacy_is_little_endian();
253 }
254 
255 /* Memory accessors */
virtio16_to_cpu(struct virtio_device * vdev,__virtio16 val)256 static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
257 {
258 	return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
259 }
260 
cpu_to_virtio16(struct virtio_device * vdev,u16 val)261 static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
262 {
263 	return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
264 }
265 
virtio32_to_cpu(struct virtio_device * vdev,__virtio32 val)266 static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
267 {
268 	return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
269 }
270 
cpu_to_virtio32(struct virtio_device * vdev,u32 val)271 static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
272 {
273 	return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
274 }
275 
virtio64_to_cpu(struct virtio_device * vdev,__virtio64 val)276 static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
277 {
278 	return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
279 }
280 
cpu_to_virtio64(struct virtio_device * vdev,u64 val)281 static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
282 {
283 	return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
284 }
285 
286 /* Config space accessors. */
287 #define virtio_cread(vdev, structname, member, ptr)			\
288 	do {								\
289 		/* Must match the member's type, and be integer */	\
290 		if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
291 			(*ptr) = 1;					\
292 									\
293 		switch (sizeof(*ptr)) {					\
294 		case 1:							\
295 			*(ptr) = virtio_cread8(vdev,			\
296 					       offsetof(structname, member)); \
297 			break;						\
298 		case 2:							\
299 			*(ptr) = virtio_cread16(vdev,			\
300 						offsetof(structname, member)); \
301 			break;						\
302 		case 4:							\
303 			*(ptr) = virtio_cread32(vdev,			\
304 						offsetof(structname, member)); \
305 			break;						\
306 		case 8:							\
307 			*(ptr) = virtio_cread64(vdev,			\
308 						offsetof(structname, member)); \
309 			break;						\
310 		default:						\
311 			BUG();						\
312 		}							\
313 	} while(0)
314 
315 /* Config space accessors. */
316 #define virtio_cwrite(vdev, structname, member, ptr)			\
317 	do {								\
318 		/* Must match the member's type, and be integer */	\
319 		if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
320 			BUG_ON((*ptr) == 1);				\
321 									\
322 		switch (sizeof(*ptr)) {					\
323 		case 1:							\
324 			virtio_cwrite8(vdev,				\
325 				       offsetof(structname, member),	\
326 				       *(ptr));				\
327 			break;						\
328 		case 2:							\
329 			virtio_cwrite16(vdev,				\
330 					offsetof(structname, member),	\
331 					*(ptr));			\
332 			break;						\
333 		case 4:							\
334 			virtio_cwrite32(vdev,				\
335 					offsetof(structname, member),	\
336 					*(ptr));			\
337 			break;						\
338 		case 8:							\
339 			virtio_cwrite64(vdev,				\
340 					offsetof(structname, member),	\
341 					*(ptr));			\
342 			break;						\
343 		default:						\
344 			BUG();						\
345 		}							\
346 	} while(0)
347 
348 /* Read @count fields, @bytes each. */
__virtio_cread_many(struct virtio_device * vdev,unsigned int offset,void * buf,size_t count,size_t bytes)349 static inline void __virtio_cread_many(struct virtio_device *vdev,
350 				       unsigned int offset,
351 				       void *buf, size_t count, size_t bytes)
352 {
353 	u32 old, gen = vdev->config->generation ?
354 		vdev->config->generation(vdev) : 0;
355 	int i;
356 
357 	do {
358 		old = gen;
359 
360 		for (i = 0; i < count; i++)
361 			vdev->config->get(vdev, offset + bytes * i,
362 					  buf + i * bytes, bytes);
363 
364 		gen = vdev->config->generation ?
365 			vdev->config->generation(vdev) : 0;
366 	} while (gen != old);
367 }
368 
virtio_cread_bytes(struct virtio_device * vdev,unsigned int offset,void * buf,size_t len)369 static inline void virtio_cread_bytes(struct virtio_device *vdev,
370 				      unsigned int offset,
371 				      void *buf, size_t len)
372 {
373 	__virtio_cread_many(vdev, offset, buf, len, 1);
374 }
375 
virtio_cread8(struct virtio_device * vdev,unsigned int offset)376 static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
377 {
378 	u8 ret;
379 	vdev->config->get(vdev, offset, &ret, sizeof(ret));
380 	return ret;
381 }
382 
virtio_cwrite8(struct virtio_device * vdev,unsigned int offset,u8 val)383 static inline void virtio_cwrite8(struct virtio_device *vdev,
384 				  unsigned int offset, u8 val)
385 {
386 	vdev->config->set(vdev, offset, &val, sizeof(val));
387 }
388 
virtio_cread16(struct virtio_device * vdev,unsigned int offset)389 static inline u16 virtio_cread16(struct virtio_device *vdev,
390 				 unsigned int offset)
391 {
392 	u16 ret;
393 	vdev->config->get(vdev, offset, &ret, sizeof(ret));
394 	return virtio16_to_cpu(vdev, (__force __virtio16)ret);
395 }
396 
virtio_cwrite16(struct virtio_device * vdev,unsigned int offset,u16 val)397 static inline void virtio_cwrite16(struct virtio_device *vdev,
398 				   unsigned int offset, u16 val)
399 {
400 	val = (__force u16)cpu_to_virtio16(vdev, val);
401 	vdev->config->set(vdev, offset, &val, sizeof(val));
402 }
403 
virtio_cread32(struct virtio_device * vdev,unsigned int offset)404 static inline u32 virtio_cread32(struct virtio_device *vdev,
405 				 unsigned int offset)
406 {
407 	u32 ret;
408 	vdev->config->get(vdev, offset, &ret, sizeof(ret));
409 	return virtio32_to_cpu(vdev, (__force __virtio32)ret);
410 }
411 
virtio_cwrite32(struct virtio_device * vdev,unsigned int offset,u32 val)412 static inline void virtio_cwrite32(struct virtio_device *vdev,
413 				   unsigned int offset, u32 val)
414 {
415 	val = (__force u32)cpu_to_virtio32(vdev, val);
416 	vdev->config->set(vdev, offset, &val, sizeof(val));
417 }
418 
virtio_cread64(struct virtio_device * vdev,unsigned int offset)419 static inline u64 virtio_cread64(struct virtio_device *vdev,
420 				 unsigned int offset)
421 {
422 	u64 ret;
423 	__virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
424 	return virtio64_to_cpu(vdev, (__force __virtio64)ret);
425 }
426 
virtio_cwrite64(struct virtio_device * vdev,unsigned int offset,u64 val)427 static inline void virtio_cwrite64(struct virtio_device *vdev,
428 				   unsigned int offset, u64 val)
429 {
430 	val = (__force u64)cpu_to_virtio64(vdev, val);
431 	vdev->config->set(vdev, offset, &val, sizeof(val));
432 }
433 
434 /* Conditional config space accessors. */
435 #define virtio_cread_feature(vdev, fbit, structname, member, ptr)	\
436 	({								\
437 		int _r = 0;						\
438 		if (!virtio_has_feature(vdev, fbit))			\
439 			_r = -ENOENT;					\
440 		else							\
441 			virtio_cread((vdev), structname, member, ptr);	\
442 		_r;							\
443 	})
444 
445 #endif /* _LINUX_VIRTIO_CONFIG_H */
446