blob: 26c155bb639b5798ac2b35b5da9084891b28890a [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. Tsirkineef960a02014-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
10/**
11 * virtio_config_ops - operations for configuring a virtio device
Rusty Russella586d4f2008-02-04 23:49:56 -050012 * @get: read the value of a configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100013 * vdev: the virtio_device
Rusty Russella586d4f2008-02-04 23:49:56 -050014 * offset: the offset of the configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100015 * buf: the buffer to write the field value into.
Rusty Russella586d4f2008-02-04 23:49:56 -050016 * len: the length of the buffer
Rusty Russella586d4f2008-02-04 23:49:56 -050017 * @set: write the value of a configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100018 * vdev: the virtio_device
Rusty Russella586d4f2008-02-04 23:49:56 -050019 * offset: the offset of the configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100020 * buf: the buffer to read the field value from.
Rusty Russella586d4f2008-02-04 23:49:56 -050021 * len: the length of the buffer
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +020022 * @generation: config generation counter
23 * vdev: the virtio_device
24 * Returns the config generation counter
Rusty Russellec3d41c2007-10-22 11:03:36 +100025 * @get_status: read the status byte
26 * vdev: the virtio_device
27 * Returns the status byte
28 * @set_status: write the status byte
29 * vdev: the virtio_device
30 * status: the new status byte
Rusty Russell6e5aa7e2008-02-04 23:50:03 -050031 * @reset: reset the device
32 * vdev: the virtio device
33 * After this, status and feature negotiation must be done again
Michael S. Tsirkine6af5782011-11-17 17:41:15 +020034 * Device must not be reset from its vq/config callbacks, or in
35 * parallel with being added/removed.
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060036 * @find_vqs: find virtqueues and instantiate them.
Rusty Russellec3d41c2007-10-22 11:03:36 +100037 * vdev: the virtio_device
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060038 * nvqs: the number of virtqueues to find
39 * vqs: on success, includes new virtqueues
40 * callbacks: array of callbacks, for each virtqueue
Michael S. Tsirkin6457f122012-09-05 21:47:45 +030041 * include a NULL entry for vqs that do not need a callback
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060042 * names: array of virtqueue names (mainly for debugging)
Michael S. Tsirkin6457f122012-09-05 21:47:45 +030043 * include a NULL entry for vqs unused by driver
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060044 * Returns 0 on success or error status
45 * @del_vqs: free virtqueues found by find_vqs().
Rusty Russellc45a6812008-05-02 21:50:50 -050046 * @get_features: get the array of feature bits for this device.
47 * vdev: the virtio_device
48 * Returns the first 32 feature bits (all we currently need).
Rusty Russellc6248962008-07-25 12:06:07 -050049 * @finalize_features: confirm what device features we'll be using.
Rusty Russellc45a6812008-05-02 21:50:50 -050050 * vdev: the virtio_device
Rusty Russellc6248962008-07-25 12:06:07 -050051 * This gives the final feature bits for the device: it can change
52 * the dev->feature bits if it wants.
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +020053 * Returns 0 on success or error status
Rick Jones66846042011-11-14 14:17:08 +000054 * @bus_name: return the bus name associated with the device
55 * vdev: the virtio_device
56 * This returns a pointer to the bus name a la pci_name from which
57 * the caller can then copy.
Jason Wang75a0a522012-08-28 13:54:14 +020058 * @set_vq_affinity: set the affinity for a virtqueue.
Rusty Russellec3d41c2007-10-22 11:03:36 +100059 */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060060typedef void vq_callback_t(struct virtqueue *);
Rusty Russell1842f232009-07-30 16:03:46 -060061struct virtio_config_ops {
Rusty Russella586d4f2008-02-04 23:49:56 -050062 void (*get)(struct virtio_device *vdev, unsigned offset,
Rusty Russellec3d41c2007-10-22 11:03:36 +100063 void *buf, unsigned len);
Rusty Russella586d4f2008-02-04 23:49:56 -050064 void (*set)(struct virtio_device *vdev, unsigned offset,
Rusty Russellec3d41c2007-10-22 11:03:36 +100065 const void *buf, unsigned len);
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +020066 u32 (*generation)(struct virtio_device *vdev);
Rusty Russellec3d41c2007-10-22 11:03:36 +100067 u8 (*get_status)(struct virtio_device *vdev);
68 void (*set_status)(struct virtio_device *vdev, u8 status);
Rusty Russell6e5aa7e2008-02-04 23:50:03 -050069 void (*reset)(struct virtio_device *vdev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060070 int (*find_vqs)(struct virtio_device *, unsigned nvqs,
71 struct virtqueue *vqs[],
72 vq_callback_t *callbacks[],
Stefan Hajnoczif7ad26f2015-12-17 16:53:43 +080073 const char * const names[]);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060074 void (*del_vqs)(struct virtio_device *);
Michael S. Tsirkind0254772014-10-07 16:39:43 +020075 u64 (*get_features)(struct virtio_device *vdev);
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +020076 int (*finalize_features)(struct virtio_device *vdev);
Rick Jones66846042011-11-14 14:17:08 +000077 const char *(*bus_name)(struct virtio_device *vdev);
Jason Wang75a0a522012-08-28 13:54:14 +020078 int (*set_vq_affinity)(struct virtqueue *vq, int cpu);
Rusty Russellec3d41c2007-10-22 11:03:36 +100079};
80
Rusty Russellc45a6812008-05-02 21:50:50 -050081/* If driver didn't advertise the feature, it will never appear. */
82void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
83 unsigned int fbit);
84
85/**
Michael S. Tsirkind4024af2014-11-27 21:19:02 +020086 * __virtio_test_bit - helper to test feature bits. For use by transports.
87 * Devices should normally use virtio_has_feature,
88 * which includes more checks.
Rusty Russellc45a6812008-05-02 21:50:50 -050089 * @vdev: the device
90 * @fbit: the feature bit
91 */
Michael S. Tsirkind4024af2014-11-27 21:19:02 +020092static inline bool __virtio_test_bit(const struct virtio_device *vdev,
93 unsigned int fbit)
94{
95 /* Did you forget to fix assumptions on max features? */
96 if (__builtin_constant_p(fbit))
Michael S. Tsirkind0254772014-10-07 16:39:43 +020097 BUILD_BUG_ON(fbit >= 64);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +020098 else
Michael S. Tsirkind0254772014-10-07 16:39:43 +020099 BUG_ON(fbit >= 64);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200100
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200101 return vdev->features & BIT_ULL(fbit);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200102}
103
104/**
105 * __virtio_set_bit - helper to set feature bits. For use by transports.
106 * @vdev: the device
107 * @fbit: the feature bit
108 */
109static inline void __virtio_set_bit(struct virtio_device *vdev,
110 unsigned int fbit)
111{
112 /* Did you forget to fix assumptions on max features? */
113 if (__builtin_constant_p(fbit))
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200114 BUILD_BUG_ON(fbit >= 64);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200115 else
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200116 BUG_ON(fbit >= 64);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200117
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200118 vdev->features |= BIT_ULL(fbit);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200119}
120
121/**
122 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
123 * @vdev: the device
124 * @fbit: the feature bit
125 */
126static inline void __virtio_clear_bit(struct virtio_device *vdev,
Rusty Russellc45a6812008-05-02 21:50:50 -0500127 unsigned int fbit)
128{
129 /* Did you forget to fix assumptions on max features? */
Rusty Russell1765e3a2011-01-24 14:45:10 -0600130 if (__builtin_constant_p(fbit))
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200131 BUILD_BUG_ON(fbit >= 64);
Rusty Russell1765e3a2011-01-24 14:45:10 -0600132 else
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200133 BUG_ON(fbit >= 64);
Rusty Russellc45a6812008-05-02 21:50:50 -0500134
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200135 vdev->features &= ~BIT_ULL(fbit);
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200136}
137
138/**
139 * virtio_has_feature - helper to determine if this device has this feature.
140 * @vdev: the device
141 * @fbit: the feature bit
142 */
143static inline bool virtio_has_feature(const struct virtio_device *vdev,
144 unsigned int fbit)
145{
Mark McLoughlinee006b32009-05-11 18:11:44 +0100146 if (fbit < VIRTIO_TRANSPORT_F_START)
147 virtio_check_driver_offered_feature(vdev, fbit);
148
Michael S. Tsirkind4024af2014-11-27 21:19:02 +0200149 return __virtio_test_bit(vdev, fbit);
Rusty Russellc45a6812008-05-02 21:50:50 -0500150}
151
Michael S. Tsirkin1a937692016-04-18 12:58:14 +0300152/**
153 * virtio_has_iommu_quirk - determine whether this device has the iommu quirk
154 * @vdev: the device
155 */
156static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev)
157{
158 /*
159 * Note the reverse polarity of the quirk feature (compared to most
160 * other features), this is for compatibility with legacy systems.
161 */
162 return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
163}
164
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600165static inline
166struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
167 vq_callback_t *c, const char *n)
168{
169 vq_callback_t *callbacks[] = { c };
170 const char *names[] = { n };
171 struct virtqueue *vq;
172 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names);
173 if (err < 0)
174 return ERR_PTR(err);
175 return vq;
176}
Rick Jones66846042011-11-14 14:17:08 +0000177
Michael S. Tsirkin3569db52014-10-15 10:22:30 +1030178/**
179 * virtio_device_ready - enable vq use in probe function
180 * @vdev: the device
181 *
182 * Driver must call this to use vqs in the probe function.
183 *
184 * Note: vqs are enabled automatically after probe returns.
185 */
186static inline
187void virtio_device_ready(struct virtio_device *dev)
188{
189 unsigned status = dev->config->get_status(dev);
190
191 BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
192 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
193}
194
Rick Jones66846042011-11-14 14:17:08 +0000195static inline
196const char *virtio_bus_name(struct virtio_device *vdev)
197{
198 if (!vdev->config->bus_name)
199 return "virtio";
200 return vdev->config->bus_name(vdev);
201}
202
Jason Wang75a0a522012-08-28 13:54:14 +0200203/**
204 * virtqueue_set_affinity - setting affinity for a virtqueue
205 * @vq: the virtqueue
206 * @cpu: the cpu no.
207 *
208 * Pay attention the function are best-effort: the affinity hint may not be set
209 * due to config support, irq type and sharing.
210 *
211 */
212static inline
213int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
214{
215 struct virtio_device *vdev = vq->vdev;
216 if (vdev->config->set_vq_affinity)
217 return vdev->config->set_vq_affinity(vq, cpu);
218 return 0;
219}
220
Greg Kurzcf561f02015-04-24 14:24:27 +0200221static inline bool virtio_is_little_endian(struct virtio_device *vdev)
222{
Greg Kurz7d824102015-04-24 14:26:24 +0200223 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
224 virtio_legacy_is_little_endian();
Greg Kurzcf561f02015-04-24 14:24:27 +0200225}
226
Michael S. Tsirkineef960a02014-10-22 15:35:56 +0300227/* Memory accessors */
228static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
229{
Greg Kurzcf561f02015-04-24 14:24:27 +0200230 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a02014-10-22 15:35:56 +0300231}
232
233static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
234{
Greg Kurzcf561f02015-04-24 14:24:27 +0200235 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a02014-10-22 15:35:56 +0300236}
237
238static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
239{
Greg Kurzcf561f02015-04-24 14:24:27 +0200240 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a02014-10-22 15:35:56 +0300241}
242
243static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
244{
Greg Kurzcf561f02015-04-24 14:24:27 +0200245 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a02014-10-22 15:35:56 +0300246}
247
248static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
249{
Greg Kurzcf561f02015-04-24 14:24:27 +0200250 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a02014-10-22 15:35:56 +0300251}
252
253static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
254{
Greg Kurzcf561f02015-04-24 14:24:27 +0200255 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
Michael S. Tsirkineef960a02014-10-22 15:35:56 +0300256}
257
Rusty Russell0b90d062013-10-14 18:11:51 +1030258/* Config space accessors. */
259#define virtio_cread(vdev, structname, member, ptr) \
260 do { \
261 /* Must match the member's type, and be integer */ \
262 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
263 (*ptr) = 1; \
264 \
265 switch (sizeof(*ptr)) { \
266 case 1: \
267 *(ptr) = virtio_cread8(vdev, \
268 offsetof(structname, member)); \
269 break; \
270 case 2: \
271 *(ptr) = virtio_cread16(vdev, \
272 offsetof(structname, member)); \
273 break; \
274 case 4: \
275 *(ptr) = virtio_cread32(vdev, \
276 offsetof(structname, member)); \
277 break; \
278 case 8: \
279 *(ptr) = virtio_cread64(vdev, \
280 offsetof(structname, member)); \
281 break; \
282 default: \
283 BUG(); \
284 } \
285 } while(0)
286
287/* Config space accessors. */
288#define virtio_cwrite(vdev, structname, member, ptr) \
289 do { \
290 /* Must match the member's type, and be integer */ \
291 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
292 BUG_ON((*ptr) == 1); \
293 \
294 switch (sizeof(*ptr)) { \
295 case 1: \
296 virtio_cwrite8(vdev, \
297 offsetof(structname, member), \
298 *(ptr)); \
299 break; \
300 case 2: \
301 virtio_cwrite16(vdev, \
302 offsetof(structname, member), \
303 *(ptr)); \
304 break; \
305 case 4: \
306 virtio_cwrite32(vdev, \
307 offsetof(structname, member), \
308 *(ptr)); \
309 break; \
310 case 8: \
311 virtio_cwrite64(vdev, \
312 offsetof(structname, member), \
313 *(ptr)); \
314 break; \
315 default: \
316 BUG(); \
317 } \
318 } while(0)
319
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +0200320/* Read @count fields, @bytes each. */
321static inline void __virtio_cread_many(struct virtio_device *vdev,
322 unsigned int offset,
323 void *buf, size_t count, size_t bytes)
324{
325 u32 old, gen = vdev->config->generation ?
326 vdev->config->generation(vdev) : 0;
327 int i;
328
329 do {
330 old = gen;
331
332 for (i = 0; i < count; i++)
333 vdev->config->get(vdev, offset + bytes * i,
334 buf + i * bytes, bytes);
335
336 gen = vdev->config->generation ?
337 vdev->config->generation(vdev) : 0;
338 } while (gen != old);
339}
340
Rusty Russell0b90d062013-10-14 18:11:51 +1030341static inline void virtio_cread_bytes(struct virtio_device *vdev,
342 unsigned int offset,
343 void *buf, size_t len)
344{
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +0200345 __virtio_cread_many(vdev, offset, buf, len, 1);
Rusty Russell0b90d062013-10-14 18:11:51 +1030346}
347
Michael S. Tsirkincaa0e2d2015-04-01 08:21:51 +1030348static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
349{
350 u8 ret;
351 vdev->config->get(vdev, offset, &ret, sizeof(ret));
352 return ret;
353}
354
Rusty Russell0b90d062013-10-14 18:11:51 +1030355static inline void virtio_cwrite8(struct virtio_device *vdev,
356 unsigned int offset, u8 val)
357{
358 vdev->config->set(vdev, offset, &val, sizeof(val));
359}
360
361static inline u16 virtio_cread16(struct virtio_device *vdev,
362 unsigned int offset)
363{
364 u16 ret;
365 vdev->config->get(vdev, offset, &ret, sizeof(ret));
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300366 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
Rusty Russell0b90d062013-10-14 18:11:51 +1030367}
368
369static inline void virtio_cwrite16(struct virtio_device *vdev,
370 unsigned int offset, u16 val)
371{
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300372 val = (__force u16)cpu_to_virtio16(vdev, val);
Rusty Russell0b90d062013-10-14 18:11:51 +1030373 vdev->config->set(vdev, offset, &val, sizeof(val));
374}
375
376static inline u32 virtio_cread32(struct virtio_device *vdev,
377 unsigned int offset)
378{
379 u32 ret;
380 vdev->config->get(vdev, offset, &ret, sizeof(ret));
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300381 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
Rusty Russell0b90d062013-10-14 18:11:51 +1030382}
383
384static inline void virtio_cwrite32(struct virtio_device *vdev,
385 unsigned int offset, u32 val)
386{
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300387 val = (__force u32)cpu_to_virtio32(vdev, val);
Rusty Russell0b90d062013-10-14 18:11:51 +1030388 vdev->config->set(vdev, offset, &val, sizeof(val));
389}
390
391static inline u64 virtio_cread64(struct virtio_device *vdev,
392 unsigned int offset)
393{
394 u64 ret;
Michael S. Tsirkind71de9e2014-12-14 16:55:44 +0200395 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300396 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
Rusty Russell0b90d062013-10-14 18:11:51 +1030397}
398
399static inline void virtio_cwrite64(struct virtio_device *vdev,
400 unsigned int offset, u64 val)
401{
Michael S. Tsirkin92e6d742014-10-22 16:59:01 +0300402 val = (__force u64)cpu_to_virtio64(vdev, val);
Rusty Russell0b90d062013-10-14 18:11:51 +1030403 vdev->config->set(vdev, offset, &val, sizeof(val));
404}
405
406/* Conditional config space accessors. */
407#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
408 ({ \
409 int _r = 0; \
410 if (!virtio_has_feature(vdev, fbit)) \
411 _r = -ENOENT; \
412 else \
413 virtio_cread((vdev), structname, member, ptr); \
414 _r; \
415 })
Jason Wang75a0a522012-08-28 13:54:14 +0200416
Rusty Russellec3d41c2007-10-22 11:03:36 +1000417#endif /* _LINUX_VIRTIO_CONFIG_H */