11 #include <linux/types.h>
14 typedef unsigned long long dma_addr_t;
17 unsigned long page_link;
20 dma_addr_t dma_address;
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) * \
35 #define offset_in_page(p) (((unsigned long)p) % 4096)
36 #define sg_phys(sg) ((sg->page_link & ~0x3) / sizeof(struct page) * 4096 + \
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);
71 static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
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)
80 sg_set_buf(sg, buf, buflen);
96 static inline void *kmalloc(size_t s, gfp_t gfp)
101 static inline void kfree(void *p)
106 #define container_of(ptr, type, member) ({ \
107 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
108 (type *)( (char *)__mptr - offsetof(type,member) );})
110 #define uninitialized_var(x) x = x
113 # define likely(x) (__builtin_expect(!!(x), 1))
116 # define unlikely(x) (__builtin_expect(!!(x), 0))
119 #define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
121 #define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
123 #define pr_debug(format, ...) do {} while (0)
125 #define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
126 #define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
128 /* TODO: empty stubs for now. Broken but enough for virtio_ring.c */
129 #define list_add_tail(a, b) do {} while (0)
130 #define list_del(a) do {} while (0)
132 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
133 #define BITS_PER_BYTE 8
134 #define BITS_PER_LONG (sizeof(long) * BITS_PER_BYTE)
135 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
136 /* TODO: Not atomic as it should be:
137 * we don't use this for anything important. */
138 static inline void clear_bit(int nr, volatile unsigned long *addr)
140 unsigned long mask = BIT_MASK(nr);
141 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
146 static inline int test_bit(int nr, const volatile unsigned long *addr)
148 return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
151 /* The only feature we care to support */
152 #define virtio_has_feature(dev, feature) \
153 test_bit((feature), (dev)->features)
156 struct virtio_device {
158 unsigned long features[1];
162 /* TODO: commented as list macros are empty stubs for now.
163 * Broken but enough for virtio_ring.c
164 * struct list_head list; */
165 void (*callback)(struct virtqueue *vq);
167 struct virtio_device *vdev;
169 unsigned int num_free;
173 #define EXPORT_SYMBOL_GPL(__EXPORT_SYMBOL_GPL_name) \
174 void __EXPORT_SYMBOL_GPL##__EXPORT_SYMBOL_GPL_name() { \
176 #define MODULE_LICENSE(__MODULE_LICENSE_value) \
177 const char *__MODULE_LICENSE_name = __MODULE_LICENSE_value
181 #if defined(__i386__) || defined(__x86_64__)
182 #define barrier() asm volatile("" ::: "memory")
183 #define mb() __sync_synchronize()
185 #define smp_mb() mb()
186 # define smp_rmb() barrier()
187 # define smp_wmb() barrier()
188 /* Weak barriers should be used. If not - it's a bug */
189 # define rmb() abort()
190 # define wmb() abort()
192 #error Please fill in barrier macros
195 /* Interfaces exported by virtio_ring. */
196 int virtqueue_add_buf(struct virtqueue *vq,
197 struct scatterlist sg[],
198 unsigned int out_num,
203 void virtqueue_kick(struct virtqueue *vq);
205 void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
207 void virtqueue_disable_cb(struct virtqueue *vq);
209 bool virtqueue_enable_cb(struct virtqueue *vq);
210 bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
212 void *virtqueue_detach_unused_buf(struct virtqueue *vq);
213 struct virtqueue *vring_new_virtqueue(unsigned int index,
215 unsigned int vring_align,
216 struct virtio_device *vdev,
219 void (*notify)(struct virtqueue *vq),
220 void (*callback)(struct virtqueue *vq),
222 void vring_del_virtqueue(struct virtqueue *vq);