virtio_ring: switch to new memory access APIs
[deliverable/linux.git] / include / linux / virtio_config.h
CommitLineData
ec3d41c4
RR
1#ifndef _LINUX_VIRTIO_CONFIG_H
2#define _LINUX_VIRTIO_CONFIG_H
b4f68be6 3
d2a7ddda 4#include <linux/err.h>
187f1882 5#include <linux/bug.h>
72e61eb4 6#include <linux/virtio.h>
eef960a0 7#include <linux/virtio_byteorder.h>
607ca46e 8#include <uapi/linux/virtio_config.h>
ec3d41c4
RR
9
10/**
11 * virtio_config_ops - operations for configuring a virtio device
a586d4f6 12 * @get: read the value of a configuration field
ec3d41c4 13 * vdev: the virtio_device
a586d4f6 14 * offset: the offset of the configuration field
ec3d41c4 15 * buf: the buffer to write the field value into.
a586d4f6 16 * len: the length of the buffer
a586d4f6 17 * @set: write the value of a configuration field
ec3d41c4 18 * vdev: the virtio_device
a586d4f6 19 * offset: the offset of the configuration field
ec3d41c4 20 * buf: the buffer to read the field value from.
a586d4f6 21 * len: the length of the buffer
ec3d41c4
RR
22 * @get_status: read the status byte
23 * vdev: the virtio_device
24 * Returns the status byte
25 * @set_status: write the status byte
26 * vdev: the virtio_device
27 * status: the new status byte
6e5aa7ef
RR
28 * @reset: reset the device
29 * vdev: the virtio device
30 * After this, status and feature negotiation must be done again
e6af578c
MT
31 * Device must not be reset from its vq/config callbacks, or in
32 * parallel with being added/removed.
d2a7ddda 33 * @find_vqs: find virtqueues and instantiate them.
ec3d41c4 34 * vdev: the virtio_device
d2a7ddda
MT
35 * nvqs: the number of virtqueues to find
36 * vqs: on success, includes new virtqueues
37 * callbacks: array of callbacks, for each virtqueue
6457f126 38 * include a NULL entry for vqs that do not need a callback
d2a7ddda 39 * names: array of virtqueue names (mainly for debugging)
6457f126 40 * include a NULL entry for vqs unused by driver
d2a7ddda
MT
41 * Returns 0 on success or error status
42 * @del_vqs: free virtqueues found by find_vqs().
c45a6816
RR
43 * @get_features: get the array of feature bits for this device.
44 * vdev: the virtio_device
45 * Returns the first 32 feature bits (all we currently need).
c624896e 46 * @finalize_features: confirm what device features we'll be using.
c45a6816 47 * vdev: the virtio_device
c624896e
RR
48 * This gives the final feature bits for the device: it can change
49 * the dev->feature bits if it wants.
66846048
RJ
50 * @bus_name: return the bus name associated with the device
51 * vdev: the virtio_device
52 * This returns a pointer to the bus name a la pci_name from which
53 * the caller can then copy.
75a0a52b 54 * @set_vq_affinity: set the affinity for a virtqueue.
ec3d41c4 55 */
d2a7ddda 56typedef void vq_callback_t(struct virtqueue *);
1842f23c 57struct virtio_config_ops {
a586d4f6 58 void (*get)(struct virtio_device *vdev, unsigned offset,
ec3d41c4 59 void *buf, unsigned len);
a586d4f6 60 void (*set)(struct virtio_device *vdev, unsigned offset,
ec3d41c4
RR
61 const void *buf, unsigned len);
62 u8 (*get_status)(struct virtio_device *vdev);
63 void (*set_status)(struct virtio_device *vdev, u8 status);
6e5aa7ef 64 void (*reset)(struct virtio_device *vdev);
d2a7ddda
MT
65 int (*find_vqs)(struct virtio_device *, unsigned nvqs,
66 struct virtqueue *vqs[],
67 vq_callback_t *callbacks[],
68 const char *names[]);
69 void (*del_vqs)(struct virtio_device *);
d0254773 70 u64 (*get_features)(struct virtio_device *vdev);
c624896e 71 void (*finalize_features)(struct virtio_device *vdev);
66846048 72 const char *(*bus_name)(struct virtio_device *vdev);
75a0a52b 73 int (*set_vq_affinity)(struct virtqueue *vq, int cpu);
ec3d41c4
RR
74};
75
c45a6816
RR
76/* If driver didn't advertise the feature, it will never appear. */
77void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
78 unsigned int fbit);
79
80/**
d4024af5
MT
81 * __virtio_test_bit - helper to test feature bits. For use by transports.
82 * Devices should normally use virtio_has_feature,
83 * which includes more checks.
c45a6816
RR
84 * @vdev: the device
85 * @fbit: the feature bit
86 */
d4024af5
MT
87static inline bool __virtio_test_bit(const struct virtio_device *vdev,
88 unsigned int fbit)
89{
90 /* Did you forget to fix assumptions on max features? */
91 if (__builtin_constant_p(fbit))
d0254773 92 BUILD_BUG_ON(fbit >= 64);
d4024af5 93 else
d0254773 94 BUG_ON(fbit >= 64);
d4024af5 95
d0254773 96 return vdev->features & BIT_ULL(fbit);
d4024af5
MT
97}
98
99/**
100 * __virtio_set_bit - helper to set feature bits. For use by transports.
101 * @vdev: the device
102 * @fbit: the feature bit
103 */
104static inline void __virtio_set_bit(struct virtio_device *vdev,
105 unsigned int fbit)
106{
107 /* Did you forget to fix assumptions on max features? */
108 if (__builtin_constant_p(fbit))
d0254773 109 BUILD_BUG_ON(fbit >= 64);
d4024af5 110 else
d0254773 111 BUG_ON(fbit >= 64);
d4024af5 112
d0254773 113 vdev->features |= BIT_ULL(fbit);
d4024af5
MT
114}
115
116/**
117 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
118 * @vdev: the device
119 * @fbit: the feature bit
120 */
121static inline void __virtio_clear_bit(struct virtio_device *vdev,
c45a6816
RR
122 unsigned int fbit)
123{
124 /* Did you forget to fix assumptions on max features? */
1765e3a4 125 if (__builtin_constant_p(fbit))
d0254773 126 BUILD_BUG_ON(fbit >= 64);
1765e3a4 127 else
d0254773 128 BUG_ON(fbit >= 64);
c45a6816 129
d0254773 130 vdev->features &= ~BIT_ULL(fbit);
d4024af5
MT
131}
132
133/**
134 * virtio_has_feature - helper to determine if this device has this feature.
135 * @vdev: the device
136 * @fbit: the feature bit
137 */
138static inline bool virtio_has_feature(const struct virtio_device *vdev,
139 unsigned int fbit)
140{
ee006b35
MM
141 if (fbit < VIRTIO_TRANSPORT_F_START)
142 virtio_check_driver_offered_feature(vdev, fbit);
143
d4024af5 144 return __virtio_test_bit(vdev, fbit);
c45a6816
RR
145}
146
d2a7ddda
MT
147static inline
148struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
149 vq_callback_t *c, const char *n)
150{
151 vq_callback_t *callbacks[] = { c };
152 const char *names[] = { n };
153 struct virtqueue *vq;
154 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names);
155 if (err < 0)
156 return ERR_PTR(err);
157 return vq;
158}
66846048 159
3569db59
MT
160/**
161 * virtio_device_ready - enable vq use in probe function
162 * @vdev: the device
163 *
164 * Driver must call this to use vqs in the probe function.
165 *
166 * Note: vqs are enabled automatically after probe returns.
167 */
168static inline
169void virtio_device_ready(struct virtio_device *dev)
170{
171 unsigned status = dev->config->get_status(dev);
172
173 BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
174 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
175}
176
66846048
RJ
177static inline
178const char *virtio_bus_name(struct virtio_device *vdev)
179{
180 if (!vdev->config->bus_name)
181 return "virtio";
182 return vdev->config->bus_name(vdev);
183}
184
75a0a52b
JW
185/**
186 * virtqueue_set_affinity - setting affinity for a virtqueue
187 * @vq: the virtqueue
188 * @cpu: the cpu no.
189 *
190 * Pay attention the function are best-effort: the affinity hint may not be set
191 * due to config support, irq type and sharing.
192 *
193 */
194static inline
195int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
196{
197 struct virtio_device *vdev = vq->vdev;
198 if (vdev->config->set_vq_affinity)
199 return vdev->config->set_vq_affinity(vq, cpu);
200 return 0;
201}
202
eef960a0
MT
203/* Memory accessors */
204static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
205{
206 return __virtio16_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
207}
208
209static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
210{
211 return __cpu_to_virtio16(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
212}
213
214static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
215{
216 return __virtio32_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
217}
218
219static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
220{
221 return __cpu_to_virtio32(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
222}
223
224static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
225{
226 return __virtio64_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
227}
228
229static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
230{
231 return __cpu_to_virtio64(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
232}
233
0b90d062
RR
234/* Config space accessors. */
235#define virtio_cread(vdev, structname, member, ptr) \
236 do { \
237 /* Must match the member's type, and be integer */ \
238 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
239 (*ptr) = 1; \
240 \
241 switch (sizeof(*ptr)) { \
242 case 1: \
243 *(ptr) = virtio_cread8(vdev, \
244 offsetof(structname, member)); \
245 break; \
246 case 2: \
247 *(ptr) = virtio_cread16(vdev, \
248 offsetof(structname, member)); \
249 break; \
250 case 4: \
251 *(ptr) = virtio_cread32(vdev, \
252 offsetof(structname, member)); \
253 break; \
254 case 8: \
255 *(ptr) = virtio_cread64(vdev, \
256 offsetof(structname, member)); \
257 break; \
258 default: \
259 BUG(); \
260 } \
261 } while(0)
262
263/* Config space accessors. */
264#define virtio_cwrite(vdev, structname, member, ptr) \
265 do { \
266 /* Must match the member's type, and be integer */ \
267 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
268 BUG_ON((*ptr) == 1); \
269 \
270 switch (sizeof(*ptr)) { \
271 case 1: \
272 virtio_cwrite8(vdev, \
273 offsetof(structname, member), \
274 *(ptr)); \
275 break; \
276 case 2: \
277 virtio_cwrite16(vdev, \
278 offsetof(structname, member), \
279 *(ptr)); \
280 break; \
281 case 4: \
282 virtio_cwrite32(vdev, \
283 offsetof(structname, member), \
284 *(ptr)); \
285 break; \
286 case 8: \
287 virtio_cwrite64(vdev, \
288 offsetof(structname, member), \
289 *(ptr)); \
290 break; \
291 default: \
292 BUG(); \
293 } \
294 } while(0)
295
296static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
297{
298 u8 ret;
299 vdev->config->get(vdev, offset, &ret, sizeof(ret));
300 return ret;
301}
302
303static inline void virtio_cread_bytes(struct virtio_device *vdev,
304 unsigned int offset,
305 void *buf, size_t len)
306{
307 vdev->config->get(vdev, offset, buf, len);
308}
309
310static inline void virtio_cwrite8(struct virtio_device *vdev,
311 unsigned int offset, u8 val)
312{
313 vdev->config->set(vdev, offset, &val, sizeof(val));
314}
315
316static inline u16 virtio_cread16(struct virtio_device *vdev,
317 unsigned int offset)
318{
319 u16 ret;
320 vdev->config->get(vdev, offset, &ret, sizeof(ret));
321 return ret;
322}
323
324static inline void virtio_cwrite16(struct virtio_device *vdev,
325 unsigned int offset, u16 val)
326{
327 vdev->config->set(vdev, offset, &val, sizeof(val));
328}
329
330static inline u32 virtio_cread32(struct virtio_device *vdev,
331 unsigned int offset)
332{
333 u32 ret;
334 vdev->config->get(vdev, offset, &ret, sizeof(ret));
335 return ret;
336}
337
338static inline void virtio_cwrite32(struct virtio_device *vdev,
339 unsigned int offset, u32 val)
340{
341 vdev->config->set(vdev, offset, &val, sizeof(val));
342}
343
344static inline u64 virtio_cread64(struct virtio_device *vdev,
345 unsigned int offset)
346{
347 u64 ret;
348 vdev->config->get(vdev, offset, &ret, sizeof(ret));
349 return ret;
350}
351
352static inline void virtio_cwrite64(struct virtio_device *vdev,
353 unsigned int offset, u64 val)
354{
355 vdev->config->set(vdev, offset, &val, sizeof(val));
356}
357
358/* Conditional config space accessors. */
359#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
360 ({ \
361 int _r = 0; \
362 if (!virtio_has_feature(vdev, fbit)) \
363 _r = -ENOENT; \
364 else \
365 virtio_cread((vdev), structname, member, ptr); \
366 _r; \
367 })
75a0a52b 368
ec3d41c4 369#endif /* _LINUX_VIRTIO_CONFIG_H */
This page took 0.834945 seconds and 5 git commands to generate.