include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / tools / virtio / linux / virtio.h
blob669bcdd45805a7039473d1acd425effd6d77797c
1 #ifndef LINUX_VIRTIO_H
2 #define LINUX_VIRTIO_H
4 #include <stdbool.h>
5 #include <stdlib.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <assert.h>
11 #include <linux/types.h>
12 #include <errno.h>
14 typedef unsigned long long dma_addr_t;
16 struct scatterlist {
17 unsigned long page_link;
18 unsigned int offset;
19 unsigned int length;
20 dma_addr_t dma_address;
23 struct page {
24 unsigned long long dummy;
27 #define BUG_ON(__BUG_ON_cond) assert(!(__BUG_ON_cond))
29 /* Physical == Virtual */
30 #define virt_to_phys(p) ((unsigned long)p)
31 #define phys_to_virt(a) ((void *)(unsigned long)(a))
32 /* Page address: Virtual / 4K */
33 #define virt_to_page(p) ((struct page*)((virt_to_phys(p) / 4096) * \
34 sizeof(struct page)))
35 #define offset_in_page(p) (((unsigned long)p) % 4096)
36 #define sg_phys(sg) ((sg->page_link & ~0x3) / sizeof(struct page) * 4096 + \
37 sg->offset)
38 static inline void sg_mark_end(struct scatterlist *sg)
41 * Set termination bit, clear potential chain bit
43 sg->page_link |= 0x02;
44 sg->page_link &= ~0x01;
46 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
48 memset(sgl, 0, sizeof(*sgl) * nents);
49 sg_mark_end(&sgl[nents - 1]);
51 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
53 unsigned long page_link = sg->page_link & 0x3;
56 * In order for the low bit stealing approach to work, pages
57 * must be aligned at a 32-bit boundary as a minimum.
59 BUG_ON((unsigned long) page & 0x03);
60 sg->page_link = page_link | (unsigned long) page;
63 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
64 unsigned int len, unsigned int offset)
66 sg_assign_page(sg, page);
67 sg->offset = offset;
68 sg->length = len;
71 static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
72 unsigned int buflen)
74 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
77 static inline void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
79 sg_init_table(sg, 1);
80 sg_set_buf(sg, buf, buflen);
83 typedef __u16 u16;
85 typedef enum {
86 GFP_KERNEL,
87 GFP_ATOMIC,
88 } gfp_t;
89 typedef enum {
90 IRQ_NONE,
91 IRQ_HANDLED
92 } irqreturn_t;
94 static inline void *kmalloc(size_t s, gfp_t gfp)
96 return malloc(s);
99 static inline void kfree(void *p)
101 free(p);
104 #define container_of(ptr, type, member) ({ \
105 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
106 (type *)( (char *)__mptr - offsetof(type,member) );})
108 #define uninitialized_var(x) x = x
110 # ifndef likely
111 # define likely(x) (__builtin_expect(!!(x), 1))
112 # endif
113 # ifndef unlikely
114 # define unlikely(x) (__builtin_expect(!!(x), 0))
115 # endif
117 #define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
118 #ifdef DEBUG
119 #define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
120 #else
121 #define pr_debug(format, ...) do {} while (0)
122 #endif
123 #define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
124 #define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
126 /* TODO: empty stubs for now. Broken but enough for virtio_ring.c */
127 #define list_add_tail(a, b) do {} while (0)
128 #define list_del(a) do {} while (0)
130 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
131 #define BITS_PER_BYTE 8
132 #define BITS_PER_LONG (sizeof(long) * BITS_PER_BYTE)
133 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
134 /* TODO: Not atomic as it should be:
135 * we don't use this for anything important. */
136 static inline void clear_bit(int nr, volatile unsigned long *addr)
138 unsigned long mask = BIT_MASK(nr);
139 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
141 *p &= ~mask;
144 static inline int test_bit(int nr, const volatile unsigned long *addr)
146 return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
149 /* The only feature we care to support */
150 #define virtio_has_feature(dev, feature) \
151 test_bit((feature), (dev)->features)
152 /* end of stubs */
154 struct virtio_device {
155 void *dev;
156 unsigned long features[1];
159 struct virtqueue {
160 /* TODO: commented as list macros are empty stubs for now.
161 * Broken but enough for virtio_ring.c
162 * struct list_head list; */
163 void (*callback)(struct virtqueue *vq);
164 const char *name;
165 struct virtio_device *vdev;
166 void *priv;
169 #define EXPORT_SYMBOL_GPL(__EXPORT_SYMBOL_GPL_name) \
170 void __EXPORT_SYMBOL_GPL##__EXPORT_SYMBOL_GPL_name() { \
172 #define MODULE_LICENSE(__MODULE_LICENSE_value) \
173 const char *__MODULE_LICENSE_name = __MODULE_LICENSE_value
175 #define CONFIG_SMP
177 #if defined(__i386__) || defined(__x86_64__)
178 #define barrier() asm volatile("" ::: "memory")
179 #define mb() __sync_synchronize()
181 #define smp_mb() mb()
182 # define smp_rmb() barrier()
183 # define smp_wmb() barrier()
184 #else
185 #error Please fill in barrier macros
186 #endif
188 /* Interfaces exported by virtio_ring. */
189 int virtqueue_add_buf_gfp(struct virtqueue *vq,
190 struct scatterlist sg[],
191 unsigned int out_num,
192 unsigned int in_num,
193 void *data,
194 gfp_t gfp);
196 static inline int virtqueue_add_buf(struct virtqueue *vq,
197 struct scatterlist sg[],
198 unsigned int out_num,
199 unsigned int in_num,
200 void *data)
202 return virtqueue_add_buf_gfp(vq, sg, out_num, in_num, data, GFP_ATOMIC);
205 void virtqueue_kick(struct virtqueue *vq);
207 void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
209 void virtqueue_disable_cb(struct virtqueue *vq);
211 bool virtqueue_enable_cb(struct virtqueue *vq);
213 void *virtqueue_detach_unused_buf(struct virtqueue *vq);
214 struct virtqueue *vring_new_virtqueue(unsigned int num,
215 unsigned int vring_align,
216 struct virtio_device *vdev,
217 void *pages,
218 void (*notify)(struct virtqueue *vq),
219 void (*callback)(struct virtqueue *vq),
220 const char *name);
221 void vring_del_virtqueue(struct virtqueue *vq);
223 #endif