blob: 2ebe506fe41aea8287781407bc6a40a6f49b4a28 [file] [log] [blame]
Rusty Russellec3d41c2007-10-22 11:03:36 +10001#ifndef _LINUX_VIRTIO_CONFIG_H
2#define _LINUX_VIRTIO_CONFIG_H
Rusty Russell674bfc22008-07-25 12:06:03 -05003
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06004#include <linux/err.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -05005#include <linux/bug.h>
Rusty Russell72e61eb2008-05-02 21:50:49 -05006#include <linux/virtio.h>
Michael S. Tsirkineef960a2014-10-22 15:35:56 +03007#include <linux/virtio_byteorder.h>
David Howells607ca462012-10-13 10:46:48 +01008#include <uapi/linux/virtio_config.h>
Rusty Russellec3d41c2007-10-22 11:03:36 +10009
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +010010struct irq_affinity;
11
Rusty Russellec3d41c2007-10-22 11:03:36 +100012/**
13 * virtio_config_ops - operations for configuring a virtio device
Rusty Russella586d4f2008-02-04 23:49:56 -050014 * @get: read the value of a configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100015 * vdev: the virtio_device
Rusty Russella586d4f2008-02-04 23:49:56 -050016 * offset: the offset of the configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100017 * buf: the buffer to write the field value into.
Rusty Russella586d4f2008-02-04 23:49:56 -050018 * len: the length of the buffer
Rusty Russella586d4f2008-02-04 23:49:56 -050019 * @set: write the value of a configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100020 * vdev: the virtio_device
Rusty Russella586d4f2008-02-04 23:49:56 -050021 * offset: the offset of the configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100022 * buf: the buffer to read the field value from.
Rusty Russella586d4f2008-02-04 23:49:56 -050023 * len: the length of the buffer
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +020024 * @generation: config generation counter
25 * vdev: the virtio_device
26 * Returns the config generation counter
Rusty Russellec3d41c2007-10-22 11:03:36 +100027 * @get_status: read the status byte
28 * vdev: the virtio_device
29 * Returns the status byte
30 * @set_status: write the status byte
31 * vdev: the virtio_device
32 * status: the new status byte
Rusty Russell6e5aa7e2008-02-04 23:50:03 -050033 * @reset: reset the device
34 * vdev: the virtio device
35 * After this, status and feature negotiation must be done again
Michael S. Tsirkine6af5782011-11-17 17:41:15 +020036 * Device must not be reset from its vq/config callbacks, or in
37 * parallel with being added/removed.
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060038 * @find_vqs: find virtqueues and instantiate them.
Rusty Russellec3d41c2007-10-22 11:03:36 +100039 * vdev: the virtio_device
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060040 * nvqs: the number of virtqueues to find
41 * vqs: on success, includes new virtqueues
42 * callbacks: array of callbacks, for each virtqueue
Michael S. Tsirkin6457f122012-09-05 21:47:45 +030043 * include a NULL entry for vqs that do not need a callback
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060044 * names: array of virtqueue names (mainly for debugging)
Michael S. Tsirkin6457f122012-09-05 21:47:45 +030045 * include a NULL entry for vqs unused by driver
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060046 * Returns 0 on success or error status
47 * @del_vqs: free virtqueues found by find_vqs().
Rusty Russellc45a6812008-05-02 21:50:50 -050048 * @get_features: get the array of feature bits for this device.
49 * vdev: the virtio_device
50 * Returns the first 32 feature bits (all we currently need).
Rusty Russellc6248962008-07-25 12:06:07 -050051 * @finalize_features: confirm what device features we'll be using.
Rusty Russellc45a6812008-05-02 21:50:50 -050052 * vdev: the virtio_device
Rusty Russellc6248962008-07-25 12:06:07 -050053 * This gives the final feature bits for the device: it can change
54 * the dev->feature bits if it wants.
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +020055 * Returns 0 on success or error status
Rick Jones66846042011-11-14 14:17:08 +000056 * @bus_name: return the bus name associated with the device
57 * vdev: the virtio_device
58 * This returns a pointer to the bus name a la pci_name from which
59 * the caller can then copy.
Jason Wang75a0a522012-08-28 13:54:14 +020060 * @set_vq_affinity: set the affinity for a virtqueue.
Rusty Russellec3d41c2007-10-22 11:03:36 +100061 */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060062typedef void vq_callback_t(struct virtqueue *);
Rusty Russell1842f232009-07-30 16:03:46 -060063struct virtio_config_ops {
Rusty Russella586d4f2008-02-04 23:49:56 -050064 void (*get)(struct virtio_device *vdev, unsigned offset,
Rusty Russellec3d41c2007-10-22 11:03:36 +100065 void *buf, unsigned len);
Rusty Russella586d4f2008-02-04 23:49:56 -050066 void (*set)(struct virtio_device *vdev, unsigned offset,
Rusty Russellec3d41c2007-10-22 11:03:36 +100067 const void *buf, unsigned len);
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +020068 u32 (*generation)(struct virtio_device *vdev);
Rusty Russellec3d41c2007-10-22 11:03:36 +100069 u8 (*get_status)(struct virtio_device *vdev);
70 void (*set_status)(struct virtio_device *vdev, u8 status);
Rusty Russell6e5aa7e2008-02-04 23:50:03 -050071 void (*reset)(struct virtio_device *vdev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060072 int (*find_vqs)(struct virtio_device *, unsigned nvqs,
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +010073 struct virtqueue *vqs[], vq_callback_t *callbacks[],
74 const char * const names[], struct irq_affinity *desc);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060075 void (*del_vqs)(struct virtio_device *);
Michael S. Tsirkind0254772014-10-07 16:39:43 +020076 u64 (*get_features)(struct virtio_device *vdev);
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +020077 int (*finalize_features)(struct virtio_device *vdev);
Rick Jones66846042011-11-14 14:17:08 +000078 const char *(*bus_name)(struct virtio_device *vdev);
Jason Wang75a0a522012-08-28 13:54:14 +020079 int (*set_vq_affinity)(struct virtqueue *vq, int cpu);
Rusty Russellec3d41c2007-10-22 11:03:36 +100080};
81
Rusty Russellc45a6812008-05-02 21:50:50 -050082/* If driver didn't advertise the feature, it will never appear. */
83void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
84 unsigned int fbit);
85
86/**
Michael S. Tsirkind4024af2014-11-27 21:19:02 +020087 * __virtio_test_bit - helper to test feature bits. For use by transports.
88 * Devices should normally use virtio_has_feature,
89 * which includes more checks.
Rusty Russellc45a6812008-05-02 21:50:50 -050090 * @vdev: the device
91 * @fbit: the feature bit
92 */
Michael S. Tsirkind4024af2014-11-27 21:19:02 +020093static inline bool __virtio_test_bit(const struct virtio_device *vdev,
94 unsigned int fbit)
95{
96 /* Did you forget to fix assumptions on max features? */
97 if (__builtin_constant_p(fbit))
Michael S. Tsirkind0254772014-10-07 16:39:43 +020098 BUILD_BUG_ON(fbit >= 64);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +020099 else
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200100 BUG_ON(fbit >= 64);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200101
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200102 return vdev->features & BIT_ULL(fbit);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200103}
104
105/**
106 * __virtio_set_bit - helper to set feature bits. For use by transports.
107 * @vdev: the device
108 * @fbit: the feature bit
109 */
110static inline void __virtio_set_bit(struct virtio_device *vdev,
111 unsigned int fbit)
112{
113 /* Did you forget to fix assumptions on max features? */
114 if (__builtin_constant_p(fbit))
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200115 BUILD_BUG_ON(fbit >= 64);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200116 else
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200117 BUG_ON(fbit >= 64);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200118
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200119 vdev->features |= BIT_ULL(fbit);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200120}
121
122/**
123 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
124 * @vdev: the device
125 * @fbit: the feature bit
126 */
127static inline void __virtio_clear_bit(struct virtio_device *vdev,
Rusty Russellc45a6812008-05-02 21:50:50 -0500128 unsigned int fbit)
129{
130 /* Did you forget to fix assumptions on max features? */
Rusty Russell1765e3a2011-01-24 14:45:10 -0600131 if (__builtin_constant_p(fbit))
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200132 BUILD_BUG_ON(fbit >= 64);
Rusty Russell1765e3a2011-01-24 14:45:10 -0600133 else
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200134 BUG_ON(fbit >= 64);
Rusty Russellc45a6812008-05-02 21:50:50 -0500135
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200136 vdev->features &= ~BIT_ULL(fbit);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200137}
138
139/**
140 * virtio_has_feature - helper to determine if this device has this feature.
141 * @vdev: the device
142 * @fbit: the feature bit
143 */
144static inline bool virtio_has_feature(const struct virtio_device *vdev,
145 unsigned int fbit)
146{
Mark McLoughlinee006b352009-05-11 18:11:44 +0100147 if (fbit < VIRTIO_TRANSPORT_F_START)
148 virtio_check_driver_offered_feature(vdev, fbit);
149
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200150 return __virtio_test_bit(vdev, fbit);
Rusty Russellc45a6812008-05-02 21:50:50 -0500151}
152
Michael S. Tsirkin1a937692016-04-18 12:58:14 +0300153/**
154 * virtio_has_iommu_quirk - determine whether this device has the iommu quirk
155 * @vdev: the device
156 */
157static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev)
158{
159 /*
160 * Note the reverse polarity of the quirk feature (compared to most
161 * other features), this is for compatibility with legacy systems.
162 */
163 return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
164}
165
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600166static inline
167struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
168 vq_callback_t *c, const char *n)
169{
170 vq_callback_t *callbacks[] = { c };
171 const char *names[] = { n };
172 struct virtqueue *vq;
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +0100173 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600174 if (err < 0)
175 return ERR_PTR(err);
176 return vq;
177}
Rick Jones66846042011-11-14 14:17:08 +0000178
Michael S. Tsirkin3569db52014-10-15 10:22:30 +1030179/**
180 * virtio_device_ready - enable vq use in probe function
181 * @vdev: the device
182 *
183 * Driver must call this to use vqs in the probe function.
184 *
185 * Note: vqs are enabled automatically after probe returns.
186 */
187static inline
188void virtio_device_ready(struct virtio_device *dev)
189{
190 unsigned status = dev->config->get_status(dev);
191
192 BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
193 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
194}
195
Rick Jones66846042011-11-14 14:17:08 +0000196static inline
197const char *virtio_bus_name(struct virtio_device *vdev)
198{
199 if (!vdev->config->bus_name)
200 return "virtio";
201 return vdev->config->bus_name(vdev);
202}
203
Jason Wang75a0a522012-08-28 13:54:14 +0200204/**
205 * virtqueue_set_affinity - setting affinity for a virtqueue
206 * @vq: the virtqueue
207 * @cpu: the cpu no.
208 *
209 * Pay attention the function are best-effort: the affinity hint may not be set
210 * due to config support, irq type and sharing.
211 *
212 */
213static inline
214int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
215{
216 struct virtio_device *vdev = vq->vdev;
217 if (vdev->config->set_vq_affinity)
218 return vdev->config->set_vq_affinity(vq, cpu);
219 return 0;
220}
221
Greg Kurzcf561f02015-04-24 14:24:27 +0200222static inline bool virtio_is_little_endian(struct virtio_device *vdev)
223{
Greg Kurz7d824102015-04-24 14:26:24 +0200224 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
225 virtio_legacy_is_little_endian();
Greg Kurzcf561f02015-04-24 14:24:27 +0200226}
227
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300228/* Memory accessors */
229static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
230{
Greg Kurzcf561f02015-04-24 14:24:27 +0200231 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300232}
233
234static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
235{
Greg Kurzcf561f02015-04-24 14:24:27 +0200236 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300237}
238
239static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
240{
Greg Kurzcf561f02015-04-24 14:24:27 +0200241 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300242}
243
244static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
245{
Greg Kurzcf561f02015-04-24 14:24:27 +0200246 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300247}
248
249static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
250{
Greg Kurzcf561f02015-04-24 14:24:27 +0200251 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300252}
253
254static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
255{
Greg Kurzcf561f02015-04-24 14:24:27 +0200256 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a2014-10-22 15:35:56 +0300257}
258
Rusty Russell0b90d062013-10-14 18:11:51 +1030259/* Config space accessors. */
260#define virtio_cread(vdev, structname, member, ptr) \
261 do { \
262 /* Must match the member's type, and be integer */ \
263 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
264 (*ptr) = 1; \
265 \
266 switch (sizeof(*ptr)) { \
267 case 1: \
268 *(ptr) = virtio_cread8(vdev, \
269 offsetof(structname, member)); \
270 break; \
271 case 2: \
272 *(ptr) = virtio_cread16(vdev, \
273 offsetof(structname, member)); \
274 break; \
275 case 4: \
276 *(ptr) = virtio_cread32(vdev, \
277 offsetof(structname, member)); \
278 break; \
279 case 8: \
280 *(ptr) = virtio_cread64(vdev, \
281 offsetof(structname, member)); \
282 break; \
283 default: \
284 BUG(); \
285 } \
286 } while(0)
287
288/* Config space accessors. */
289#define virtio_cwrite(vdev, structname, member, ptr) \
290 do { \
291 /* Must match the member's type, and be integer */ \
292 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
293 BUG_ON((*ptr) == 1); \
294 \
295 switch (sizeof(*ptr)) { \
296 case 1: \
297 virtio_cwrite8(vdev, \
298 offsetof(structname, member), \
299 *(ptr)); \
300 break; \
301 case 2: \
302 virtio_cwrite16(vdev, \
303 offsetof(structname, member), \
304 *(ptr)); \
305 break; \
306 case 4: \
307 virtio_cwrite32(vdev, \
308 offsetof(structname, member), \
309 *(ptr)); \
310 break; \
311 case 8: \
312 virtio_cwrite64(vdev, \
313 offsetof(structname, member), \
314 *(ptr)); \
315 break; \
316 default: \
317 BUG(); \
318 } \
319 } while(0)
320
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +0200321/* Read @count fields, @bytes each. */
322static inline void __virtio_cread_many(struct virtio_device *vdev,
323 unsigned int offset,
324 void *buf, size_t count, size_t bytes)
325{
326 u32 old, gen = vdev->config->generation ?
327 vdev->config->generation(vdev) : 0;
328 int i;
329
330 do {
331 old = gen;
332
333 for (i = 0; i < count; i++)
334 vdev->config->get(vdev, offset + bytes * i,
335 buf + i * bytes, bytes);
336
337 gen = vdev->config->generation ?
338 vdev->config->generation(vdev) : 0;
339 } while (gen != old);
340}
341
Rusty Russell0b90d062013-10-14 18:11:51 +1030342static inline void virtio_cread_bytes(struct virtio_device *vdev,
343 unsigned int offset,
344 void *buf, size_t len)
345{
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +0200346 __virtio_cread_many(vdev, offset, buf, len, 1);
Rusty Russell0b90d062013-10-14 18:11:51 +1030347}
348
Michael S. Tsirkincaa0e2d2015-04-01 08:21:51 +1030349static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
350{
351 u8 ret;
352 vdev->config->get(vdev, offset, &ret, sizeof(ret));
353 return ret;
354}
355
Rusty Russell0b90d062013-10-14 18:11:51 +1030356static inline void virtio_cwrite8(struct virtio_device *vdev,
357 unsigned int offset, u8 val)
358{
359 vdev->config->set(vdev, offset, &val, sizeof(val));
360}
361
362static inline u16 virtio_cread16(struct virtio_device *vdev,
363 unsigned int offset)
364{
365 u16 ret;
366 vdev->config->get(vdev, offset, &ret, sizeof(ret));
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300367 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
Rusty Russell0b90d062013-10-14 18:11:51 +1030368}
369
370static inline void virtio_cwrite16(struct virtio_device *vdev,
371 unsigned int offset, u16 val)
372{
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300373 val = (__force u16)cpu_to_virtio16(vdev, val);
Rusty Russell0b90d062013-10-14 18:11:51 +1030374 vdev->config->set(vdev, offset, &val, sizeof(val));
375}
376
377static inline u32 virtio_cread32(struct virtio_device *vdev,
378 unsigned int offset)
379{
380 u32 ret;
381 vdev->config->get(vdev, offset, &ret, sizeof(ret));
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300382 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
Rusty Russell0b90d062013-10-14 18:11:51 +1030383}
384
385static inline void virtio_cwrite32(struct virtio_device *vdev,
386 unsigned int offset, u32 val)
387{
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300388 val = (__force u32)cpu_to_virtio32(vdev, val);
Rusty Russell0b90d062013-10-14 18:11:51 +1030389 vdev->config->set(vdev, offset, &val, sizeof(val));
390}
391
392static inline u64 virtio_cread64(struct virtio_device *vdev,
393 unsigned int offset)
394{
395 u64 ret;
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +0200396 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300397 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
Rusty Russell0b90d062013-10-14 18:11:51 +1030398}
399
400static inline void virtio_cwrite64(struct virtio_device *vdev,
401 unsigned int offset, u64 val)
402{
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300403 val = (__force u64)cpu_to_virtio64(vdev, val);
Rusty Russell0b90d062013-10-14 18:11:51 +1030404 vdev->config->set(vdev, offset, &val, sizeof(val));
405}
406
407/* Conditional config space accessors. */
408#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
409 ({ \
410 int _r = 0; \
411 if (!virtio_has_feature(vdev, fbit)) \
412 _r = -ENOENT; \
413 else \
414 virtio_cread((vdev), structname, member, ptr); \
415 _r; \
416 })
Jason Wang75a0a522012-08-28 13:54:14 +0200417
Rusty Russellec3d41c2007-10-22 11:03:36 +1000418#endif /* _LINUX_VIRTIO_CONFIG_H */