1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/virtio.h>
20#include <linux/virtio_ring.h>
21#include <linux/virtio_config.h>
22#include <linux/device.h>
23#include <linux/slab.h>
24#include <linux/module.h>
25#include <linux/hrtimer.h>
26
27
28
29#ifdef CONFIG_SMP
30
31
32
33#define virtio_mb(vq) \
34 do { if ((vq)->weak_barriers) smp_mb(); else mb(); } while(0)
35#define virtio_rmb(vq) \
36 do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0)
37#define virtio_wmb(vq) \
38 do { if ((vq)->weak_barriers) smp_wmb(); else wmb(); } while(0)
39#else
40
41
42
43#define virtio_mb(vq) mb()
44#define virtio_rmb(vq) rmb()
45#define virtio_wmb(vq) wmb()
46#endif
47
48#ifdef DEBUG
49
50#define BAD_RING(_vq, fmt, args...) \
51 do { \
52 dev_err(&(_vq)->vq.vdev->dev, \
53 "%s:"fmt, (_vq)->vq.name, ##args); \
54 BUG(); \
55 } while (0)
56
57#define START_USE(_vq) \
58 do { \
59 if ((_vq)->in_use) \
60 panic("%s:in_use = %i\n", \
61 (_vq)->vq.name, (_vq)->in_use); \
62 (_vq)->in_use = __LINE__; \
63 } while (0)
64#define END_USE(_vq) \
65 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
66#else
67#define BAD_RING(_vq, fmt, args...) \
68 do { \
69 dev_err(&_vq->vq.vdev->dev, \
70 "%s:"fmt, (_vq)->vq.name, ##args); \
71 (_vq)->broken = true; \
72 } while (0)
73#define START_USE(vq)
74#define END_USE(vq)
75#endif
76
77struct vring_virtqueue
78{
79 struct virtqueue vq;
80
81
82 struct vring vring;
83
84
85 bool weak_barriers;
86
87
88 bool broken;
89
90
91 bool indirect;
92
93
94 bool event;
95
96
97 unsigned int num_free;
98
99 unsigned int free_head;
100
101 unsigned int num_added;
102
103
104 u16 last_used_idx;
105
106
107 void (*notify)(struct virtqueue *vq);
108
109#ifdef DEBUG
110
111 unsigned int in_use;
112
113
114 bool last_add_time_valid;
115 ktime_t last_add_time;
116#endif
117
118
119 void *data[];
120};
121
122#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
123
124
125static int vring_add_indirect(struct vring_virtqueue *vq,
126 struct scatterlist sg[],
127 unsigned int out,
128 unsigned int in,
129 gfp_t gfp)
130{
131 struct vring_desc *desc;
132 unsigned head;
133 int i;
134
135 desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
136 if (!desc)
137 return -ENOMEM;
138
139
140 for (i = 0; i < out; i++) {
141 desc[i].flags = VRING_DESC_F_NEXT;
142 desc[i].addr = sg_phys(sg);
143 desc[i].len = sg->length;
144 desc[i].next = i+1;
145 sg++;
146 }
147 for (; i < (out + in); i++) {
148 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
149 desc[i].addr = sg_phys(sg);
150 desc[i].len = sg->length;
151 desc[i].next = i+1;
152 sg++;
153 }
154
155
156 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
157 desc[i-1].next = 0;
158
159
160 vq->num_free--;
161
162
163 head = vq->free_head;
164 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
165 vq->vring.desc[head].addr = virt_to_phys(desc);
166 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
167
168
169 vq->free_head = vq->vring.desc[head].next;
170
171 return head;
172}
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191int virtqueue_add_buf(struct virtqueue *_vq,
192 struct scatterlist sg[],
193 unsigned int out,
194 unsigned int in,
195 void *data,
196 gfp_t gfp)
197{
198 struct vring_virtqueue *vq = to_vvq(_vq);
199 unsigned int i, avail, uninitialized_var(prev);
200 int head;
201
202 START_USE(vq);
203
204 BUG_ON(data == NULL);
205
206#ifdef DEBUG
207 {
208 ktime_t now = ktime_get();
209
210
211 if (vq->last_add_time_valid)
212 WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
213 > 100);
214 vq->last_add_time = now;
215 vq->last_add_time_valid = true;
216 }
217#endif
218
219
220
221 if (vq->indirect && (out + in) > 1 && vq->num_free) {
222 head = vring_add_indirect(vq, sg, out, in, gfp);
223 if (likely(head >= 0))
224 goto add_head;
225 }
226
227 BUG_ON(out + in > vq->vring.num);
228 BUG_ON(out + in == 0);
229
230 if (vq->num_free < out + in) {
231 pr_debug("Can't add buf len %i - avail = %i\n",
232 out + in, vq->num_free);
233
234
235
236 if (out)
237 vq->notify(&vq->vq);
238 END_USE(vq);
239 return -ENOSPC;
240 }
241
242
243 vq->num_free -= out + in;
244
245 head = vq->free_head;
246 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
247 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
248 vq->vring.desc[i].addr = sg_phys(sg);
249 vq->vring.desc[i].len = sg->length;
250 prev = i;
251 sg++;
252 }
253 for (; in; i = vq->vring.desc[i].next, in--) {
254 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
255 vq->vring.desc[i].addr = sg_phys(sg);
256 vq->vring.desc[i].len = sg->length;
257 prev = i;
258 sg++;
259 }
260
261 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
262
263
264 vq->free_head = i;
265
266add_head:
267
268 vq->data[head] = data;
269
270
271
272 avail = (vq->vring.avail->idx & (vq->vring.num-1));
273 vq->vring.avail->ring[avail] = head;
274
275
276
277 virtio_wmb(vq);
278 vq->vring.avail->idx++;
279 vq->num_added++;
280
281
282
283 if (unlikely(vq->num_added == (1 << 16) - 1))
284 virtqueue_kick(_vq);
285
286 pr_debug("Added buffer head %i to %p\n", head, vq);
287 END_USE(vq);
288
289 return vq->num_free;
290}
291EXPORT_SYMBOL_GPL(virtqueue_add_buf);
292
293
294
295
296
297
298
299
300
301
302
303
304bool virtqueue_kick_prepare(struct virtqueue *_vq)
305{
306 struct vring_virtqueue *vq = to_vvq(_vq);
307 u16 new, old;
308 bool needs_kick;
309
310 START_USE(vq);
311
312
313 virtio_mb(vq);
314
315 old = vq->vring.avail->idx - vq->num_added;
316 new = vq->vring.avail->idx;
317 vq->num_added = 0;
318
319#ifdef DEBUG
320 if (vq->last_add_time_valid) {
321 WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
322 vq->last_add_time)) > 100);
323 }
324 vq->last_add_time_valid = false;
325#endif
326
327 if (vq->event) {
328 needs_kick = vring_need_event(vring_avail_event(&vq->vring),
329 new, old);
330 } else {
331 needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
332 }
333 END_USE(vq);
334 return needs_kick;
335}
336EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
337
338
339
340
341
342
343
344void virtqueue_notify(struct virtqueue *_vq)
345{
346 struct vring_virtqueue *vq = to_vvq(_vq);
347
348
349 vq->notify(_vq);
350}
351EXPORT_SYMBOL_GPL(virtqueue_notify);
352
353
354
355
356
357
358
359
360
361
362
363void virtqueue_kick(struct virtqueue *vq)
364{
365 if (virtqueue_kick_prepare(vq))
366 virtqueue_notify(vq);
367}
368EXPORT_SYMBOL_GPL(virtqueue_kick);
369
370static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
371{
372 unsigned int i;
373
374
375 vq->data[head] = NULL;
376
377
378 i = head;
379
380
381 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
382 kfree(phys_to_virt(vq->vring.desc[i].addr));
383
384 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
385 i = vq->vring.desc[i].next;
386 vq->num_free++;
387 }
388
389 vq->vring.desc[i].next = vq->free_head;
390 vq->free_head = head;
391
392 vq->num_free++;
393}
394
395static inline bool more_used(const struct vring_virtqueue *vq)
396{
397 return vq->last_used_idx != vq->vring.used->idx;
398}
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
417{
418 struct vring_virtqueue *vq = to_vvq(_vq);
419 void *ret;
420 unsigned int i;
421 u16 last_used;
422
423 START_USE(vq);
424
425 if (unlikely(vq->broken)) {
426 END_USE(vq);
427 return NULL;
428 }
429
430 if (!more_used(vq)) {
431 pr_debug("No more buffers in queue\n");
432 END_USE(vq);
433 return NULL;
434 }
435
436
437 virtio_rmb(vq);
438
439 last_used = (vq->last_used_idx & (vq->vring.num - 1));
440 i = vq->vring.used->ring[last_used].id;
441 *len = vq->vring.used->ring[last_used].len;
442
443 if (unlikely(i >= vq->vring.num)) {
444 BAD_RING(vq, "id %u out of range\n", i);
445 return NULL;
446 }
447 if (unlikely(!vq->data[i])) {
448 BAD_RING(vq, "id %u is not a head!\n", i);
449 return NULL;
450 }
451
452
453 ret = vq->data[i];
454 detach_buf(vq, i);
455 vq->last_used_idx++;
456
457
458
459 if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
460 vring_used_event(&vq->vring) = vq->last_used_idx;
461 virtio_mb(vq);
462 }
463
464#ifdef DEBUG
465 vq->last_add_time_valid = false;
466#endif
467
468 END_USE(vq);
469 return ret;
470}
471EXPORT_SYMBOL_GPL(virtqueue_get_buf);
472
473
474
475
476
477
478
479
480
481
482void virtqueue_disable_cb(struct virtqueue *_vq)
483{
484 struct vring_virtqueue *vq = to_vvq(_vq);
485
486 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
487}
488EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
489
490
491
492
493
494
495
496
497
498
499
500
501bool virtqueue_enable_cb(struct virtqueue *_vq)
502{
503 struct vring_virtqueue *vq = to_vvq(_vq);
504
505 START_USE(vq);
506
507
508
509
510
511
512 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
513 vring_used_event(&vq->vring) = vq->last_used_idx;
514 virtio_mb(vq);
515 if (unlikely(more_used(vq))) {
516 END_USE(vq);
517 return false;
518 }
519
520 END_USE(vq);
521 return true;
522}
523EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
539{
540 struct vring_virtqueue *vq = to_vvq(_vq);
541 u16 bufs;
542
543 START_USE(vq);
544
545
546
547
548
549
550 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
551
552 bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
553 vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
554 virtio_mb(vq);
555 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
556 END_USE(vq);
557 return false;
558 }
559
560 END_USE(vq);
561 return true;
562}
563EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
564
565
566
567
568
569
570
571
572
573void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
574{
575 struct vring_virtqueue *vq = to_vvq(_vq);
576 unsigned int i;
577 void *buf;
578
579 START_USE(vq);
580
581 for (i = 0; i < vq->vring.num; i++) {
582 if (!vq->data[i])
583 continue;
584
585 buf = vq->data[i];
586 detach_buf(vq, i);
587 vq->vring.avail->idx--;
588 END_USE(vq);
589 return buf;
590 }
591
592 BUG_ON(vq->num_free != vq->vring.num);
593
594 END_USE(vq);
595 return NULL;
596}
597EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
598
599irqreturn_t vring_interrupt(int irq, void *_vq)
600{
601 struct vring_virtqueue *vq = to_vvq(_vq);
602
603 if (!more_used(vq)) {
604 pr_debug("virtqueue interrupt with no work for %p\n", vq);
605 return IRQ_NONE;
606 }
607
608 if (unlikely(vq->broken))
609 return IRQ_HANDLED;
610
611 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
612 if (vq->vq.callback)
613 vq->vq.callback(&vq->vq);
614
615 return IRQ_HANDLED;
616}
617EXPORT_SYMBOL_GPL(vring_interrupt);
618
619struct virtqueue *vring_new_virtqueue(unsigned int num,
620 unsigned int vring_align,
621 struct virtio_device *vdev,
622 bool weak_barriers,
623 void *pages,
624 void (*notify)(struct virtqueue *),
625 void (*callback)(struct virtqueue *),
626 const char *name)
627{
628 struct vring_virtqueue *vq;
629 unsigned int i;
630
631
632 if (num & (num - 1)) {
633 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
634 return NULL;
635 }
636
637 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
638 if (!vq)
639 return NULL;
640
641 vring_init(&vq->vring, num, pages, vring_align);
642 vq->vq.callback = callback;
643 vq->vq.vdev = vdev;
644 vq->vq.name = name;
645 vq->notify = notify;
646 vq->weak_barriers = weak_barriers;
647 vq->broken = false;
648 vq->last_used_idx = 0;
649 vq->num_added = 0;
650 list_add_tail(&vq->vq.list, &vdev->vqs);
651#ifdef DEBUG
652 vq->in_use = false;
653 vq->last_add_time_valid = false;
654#endif
655
656 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
657 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
658
659
660 if (!callback)
661 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
662
663
664 vq->num_free = num;
665 vq->free_head = 0;
666 for (i = 0; i < num-1; i++) {
667 vq->vring.desc[i].next = i+1;
668 vq->data[i] = NULL;
669 }
670 vq->data[i] = NULL;
671
672 return &vq->vq;
673}
674EXPORT_SYMBOL_GPL(vring_new_virtqueue);
675
676void vring_del_virtqueue(struct virtqueue *vq)
677{
678 list_del(&vq->list);
679 kfree(to_vvq(vq));
680}
681EXPORT_SYMBOL_GPL(vring_del_virtqueue);
682
683
684void vring_transport_features(struct virtio_device *vdev)
685{
686 unsigned int i;
687
688 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
689 switch (i) {
690 case VIRTIO_RING_F_INDIRECT_DESC:
691 break;
692 case VIRTIO_RING_F_EVENT_IDX:
693 break;
694 default:
695
696 clear_bit(i, vdev->features);
697 }
698 }
699}
700EXPORT_SYMBOL_GPL(vring_transport_features);
701
702
703
704
705
706
707
708
709unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
710{
711
712 struct vring_virtqueue *vq = to_vvq(_vq);
713
714 return vq->vring.num;
715}
716EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
717
718MODULE_LICENSE("GPL");
719