10a8a69ddSRusty Russell /* Virtio ring implementation. 20a8a69ddSRusty Russell * 30a8a69ddSRusty Russell * Copyright 2007 Rusty Russell IBM Corporation 40a8a69ddSRusty Russell * 50a8a69ddSRusty Russell * This program is free software; you can redistribute it and/or modify 60a8a69ddSRusty Russell * it under the terms of the GNU General Public License as published by 70a8a69ddSRusty Russell * the Free Software Foundation; either version 2 of the License, or 80a8a69ddSRusty Russell * (at your option) any later version. 90a8a69ddSRusty Russell * 100a8a69ddSRusty Russell * This program is distributed in the hope that it will be useful, 110a8a69ddSRusty Russell * but WITHOUT ANY WARRANTY; without even the implied warranty of 120a8a69ddSRusty Russell * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 130a8a69ddSRusty Russell * GNU General Public License for more details. 140a8a69ddSRusty Russell * 150a8a69ddSRusty Russell * You should have received a copy of the GNU General Public License 160a8a69ddSRusty Russell * along with this program; if not, write to the Free Software 170a8a69ddSRusty Russell * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 180a8a69ddSRusty Russell */ 190a8a69ddSRusty Russell #include <linux/virtio.h> 200a8a69ddSRusty Russell #include <linux/virtio_ring.h> 21e34f8725SRusty Russell #include <linux/virtio_config.h> 220a8a69ddSRusty Russell #include <linux/device.h> 230a8a69ddSRusty Russell 24d57ed95dSMichael S. Tsirkin /* virtio guest is communicating with a virtual "device" that actually runs on 25d57ed95dSMichael S. Tsirkin * a host processor. Memory barriers are used to control SMP effects. */ 26d57ed95dSMichael S. Tsirkin #ifdef CONFIG_SMP 27d57ed95dSMichael S. Tsirkin /* Where possible, use SMP barriers which are more lightweight than mandatory 28d57ed95dSMichael S. Tsirkin * barriers, because mandatory barriers control MMIO effects on accesses 29d57ed95dSMichael S. Tsirkin * through relaxed memory I/O windows (which virtio does not use). */ 30d57ed95dSMichael S. Tsirkin #define virtio_mb() smp_mb() 31d57ed95dSMichael S. Tsirkin #define virtio_rmb() smp_rmb() 32d57ed95dSMichael S. Tsirkin #define virtio_wmb() smp_wmb() 33d57ed95dSMichael S. Tsirkin #else 34d57ed95dSMichael S. Tsirkin /* We must force memory ordering even if guest is UP since host could be 35d57ed95dSMichael S. Tsirkin * running on another CPU, but SMP barriers are defined to barrier() in that 36d57ed95dSMichael S. Tsirkin * configuration. So fall back to mandatory barriers instead. */ 37d57ed95dSMichael S. Tsirkin #define virtio_mb() mb() 38d57ed95dSMichael S. Tsirkin #define virtio_rmb() rmb() 39d57ed95dSMichael S. Tsirkin #define virtio_wmb() wmb() 40d57ed95dSMichael S. Tsirkin #endif 41d57ed95dSMichael S. Tsirkin 420a8a69ddSRusty Russell #ifdef DEBUG 430a8a69ddSRusty Russell /* For development, we want to crash whenever the ring is screwed. */ 449499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...) \ 459499f5e7SRusty Russell do { \ 469499f5e7SRusty Russell dev_err(&(_vq)->vq.vdev->dev, \ 479499f5e7SRusty Russell "%s:"fmt, (_vq)->vq.name, ##args); \ 489499f5e7SRusty Russell BUG(); \ 499499f5e7SRusty Russell } while (0) 50c5f841f1SRusty Russell /* Caller is supposed to guarantee no reentry. */ 513a35ce7dSRoel Kluin #define START_USE(_vq) \ 52c5f841f1SRusty Russell do { \ 53c5f841f1SRusty Russell if ((_vq)->in_use) \ 549499f5e7SRusty Russell panic("%s:in_use = %i\n", \ 559499f5e7SRusty Russell (_vq)->vq.name, (_vq)->in_use); \ 56c5f841f1SRusty Russell (_vq)->in_use = __LINE__; \ 57c5f841f1SRusty Russell } while (0) 583a35ce7dSRoel Kluin #define END_USE(_vq) \ 5997a545abSRusty Russell do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) 600a8a69ddSRusty Russell #else 619499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...) \ 629499f5e7SRusty Russell do { \ 639499f5e7SRusty Russell dev_err(&_vq->vq.vdev->dev, \ 649499f5e7SRusty Russell "%s:"fmt, (_vq)->vq.name, ##args); \ 659499f5e7SRusty Russell (_vq)->broken = true; \ 669499f5e7SRusty Russell } while (0) 670a8a69ddSRusty Russell #define START_USE(vq) 680a8a69ddSRusty Russell #define END_USE(vq) 690a8a69ddSRusty Russell #endif 700a8a69ddSRusty Russell 710a8a69ddSRusty Russell struct vring_virtqueue 720a8a69ddSRusty Russell { 730a8a69ddSRusty Russell struct virtqueue vq; 740a8a69ddSRusty Russell 750a8a69ddSRusty Russell /* Actual memory layout for this queue */ 760a8a69ddSRusty Russell struct vring vring; 770a8a69ddSRusty Russell 780a8a69ddSRusty Russell /* Other side has made a mess, don't try any more. */ 790a8a69ddSRusty Russell bool broken; 800a8a69ddSRusty Russell 819fa29b9dSMark McLoughlin /* Host supports indirect buffers */ 829fa29b9dSMark McLoughlin bool indirect; 839fa29b9dSMark McLoughlin 840a8a69ddSRusty Russell /* Number of free buffers */ 850a8a69ddSRusty Russell unsigned int num_free; 860a8a69ddSRusty Russell /* Head of free buffer list. */ 870a8a69ddSRusty Russell unsigned int free_head; 880a8a69ddSRusty Russell /* Number we've added since last sync. */ 890a8a69ddSRusty Russell unsigned int num_added; 900a8a69ddSRusty Russell 910a8a69ddSRusty Russell /* Last used index we've seen. */ 921bc4953eSAnthony Liguori u16 last_used_idx; 930a8a69ddSRusty Russell 940a8a69ddSRusty Russell /* How to notify other side. FIXME: commonalize hcalls! */ 950a8a69ddSRusty Russell void (*notify)(struct virtqueue *vq); 960a8a69ddSRusty Russell 970a8a69ddSRusty Russell #ifdef DEBUG 980a8a69ddSRusty Russell /* They're supposed to lock for us. */ 990a8a69ddSRusty Russell unsigned int in_use; 1000a8a69ddSRusty Russell #endif 1010a8a69ddSRusty Russell 1020a8a69ddSRusty Russell /* Tokens for callbacks. */ 1030a8a69ddSRusty Russell void *data[]; 1040a8a69ddSRusty Russell }; 1050a8a69ddSRusty Russell 1060a8a69ddSRusty Russell #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq) 1070a8a69ddSRusty Russell 1089fa29b9dSMark McLoughlin /* Set up an indirect table of descriptors and add it to the queue. */ 1099fa29b9dSMark McLoughlin static int vring_add_indirect(struct vring_virtqueue *vq, 1109fa29b9dSMark McLoughlin struct scatterlist sg[], 1119fa29b9dSMark McLoughlin unsigned int out, 1129fa29b9dSMark McLoughlin unsigned int in) 1139fa29b9dSMark McLoughlin { 1149fa29b9dSMark McLoughlin struct vring_desc *desc; 1159fa29b9dSMark McLoughlin unsigned head; 1169fa29b9dSMark McLoughlin int i; 1179fa29b9dSMark McLoughlin 1189fa29b9dSMark McLoughlin desc = kmalloc((out + in) * sizeof(struct vring_desc), GFP_ATOMIC); 1199fa29b9dSMark McLoughlin if (!desc) 1209fa29b9dSMark McLoughlin return vq->vring.num; 1219fa29b9dSMark McLoughlin 1229fa29b9dSMark McLoughlin /* Transfer entries from the sg list into the indirect page */ 1239fa29b9dSMark McLoughlin for (i = 0; i < out; i++) { 1249fa29b9dSMark McLoughlin desc[i].flags = VRING_DESC_F_NEXT; 1259fa29b9dSMark McLoughlin desc[i].addr = sg_phys(sg); 1269fa29b9dSMark McLoughlin desc[i].len = sg->length; 1279fa29b9dSMark McLoughlin desc[i].next = i+1; 1289fa29b9dSMark McLoughlin sg++; 1299fa29b9dSMark McLoughlin } 1309fa29b9dSMark McLoughlin for (; i < (out + in); i++) { 1319fa29b9dSMark McLoughlin desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE; 1329fa29b9dSMark McLoughlin desc[i].addr = sg_phys(sg); 1339fa29b9dSMark McLoughlin desc[i].len = sg->length; 1349fa29b9dSMark McLoughlin desc[i].next = i+1; 1359fa29b9dSMark McLoughlin sg++; 1369fa29b9dSMark McLoughlin } 1379fa29b9dSMark McLoughlin 1389fa29b9dSMark McLoughlin /* Last one doesn't continue. */ 1399fa29b9dSMark McLoughlin desc[i-1].flags &= ~VRING_DESC_F_NEXT; 1409fa29b9dSMark McLoughlin desc[i-1].next = 0; 1419fa29b9dSMark McLoughlin 1429fa29b9dSMark McLoughlin /* We're about to use a buffer */ 1439fa29b9dSMark McLoughlin vq->num_free--; 1449fa29b9dSMark McLoughlin 1459fa29b9dSMark McLoughlin /* Use a single buffer which doesn't continue */ 1469fa29b9dSMark McLoughlin head = vq->free_head; 1479fa29b9dSMark McLoughlin vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT; 1489fa29b9dSMark McLoughlin vq->vring.desc[head].addr = virt_to_phys(desc); 1499fa29b9dSMark McLoughlin vq->vring.desc[head].len = i * sizeof(struct vring_desc); 1509fa29b9dSMark McLoughlin 1519fa29b9dSMark McLoughlin /* Update free pointer */ 1529fa29b9dSMark McLoughlin vq->free_head = vq->vring.desc[head].next; 1539fa29b9dSMark McLoughlin 1549fa29b9dSMark McLoughlin return head; 1559fa29b9dSMark McLoughlin } 1569fa29b9dSMark McLoughlin 1570a8a69ddSRusty Russell static int vring_add_buf(struct virtqueue *_vq, 1580a8a69ddSRusty Russell struct scatterlist sg[], 1590a8a69ddSRusty Russell unsigned int out, 1600a8a69ddSRusty Russell unsigned int in, 1610a8a69ddSRusty Russell void *data) 1620a8a69ddSRusty Russell { 1630a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 1640a8a69ddSRusty Russell unsigned int i, avail, head, uninitialized_var(prev); 1650a8a69ddSRusty Russell 1669fa29b9dSMark McLoughlin START_USE(vq); 1679fa29b9dSMark McLoughlin 1680a8a69ddSRusty Russell BUG_ON(data == NULL); 1699fa29b9dSMark McLoughlin 1709fa29b9dSMark McLoughlin /* If the host supports indirect descriptor tables, and we have multiple 1719fa29b9dSMark McLoughlin * buffers, then go indirect. FIXME: tune this threshold */ 1729fa29b9dSMark McLoughlin if (vq->indirect && (out + in) > 1 && vq->num_free) { 1739fa29b9dSMark McLoughlin head = vring_add_indirect(vq, sg, out, in); 1749fa29b9dSMark McLoughlin if (head != vq->vring.num) 1759fa29b9dSMark McLoughlin goto add_head; 1769fa29b9dSMark McLoughlin } 1779fa29b9dSMark McLoughlin 1780a8a69ddSRusty Russell BUG_ON(out + in > vq->vring.num); 1790a8a69ddSRusty Russell BUG_ON(out + in == 0); 1800a8a69ddSRusty Russell 1810a8a69ddSRusty Russell if (vq->num_free < out + in) { 1820a8a69ddSRusty Russell pr_debug("Can't add buf len %i - avail = %i\n", 1830a8a69ddSRusty Russell out + in, vq->num_free); 18444653eaeSRusty Russell /* FIXME: for historical reasons, we force a notify here if 18544653eaeSRusty Russell * there are outgoing parts to the buffer. Presumably the 18644653eaeSRusty Russell * host should service the ring ASAP. */ 18744653eaeSRusty Russell if (out) 188426e3e0aSRusty Russell vq->notify(&vq->vq); 1890a8a69ddSRusty Russell END_USE(vq); 1900a8a69ddSRusty Russell return -ENOSPC; 1910a8a69ddSRusty Russell } 1920a8a69ddSRusty Russell 1930a8a69ddSRusty Russell /* We're about to use some buffers from the free list. */ 1940a8a69ddSRusty Russell vq->num_free -= out + in; 1950a8a69ddSRusty Russell 1960a8a69ddSRusty Russell head = vq->free_head; 1970a8a69ddSRusty Russell for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) { 1980a8a69ddSRusty Russell vq->vring.desc[i].flags = VRING_DESC_F_NEXT; 19915f9c890SRusty Russell vq->vring.desc[i].addr = sg_phys(sg); 2000a8a69ddSRusty Russell vq->vring.desc[i].len = sg->length; 2010a8a69ddSRusty Russell prev = i; 2020a8a69ddSRusty Russell sg++; 2030a8a69ddSRusty Russell } 2040a8a69ddSRusty Russell for (; in; i = vq->vring.desc[i].next, in--) { 2050a8a69ddSRusty Russell vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE; 20615f9c890SRusty Russell vq->vring.desc[i].addr = sg_phys(sg); 2070a8a69ddSRusty Russell vq->vring.desc[i].len = sg->length; 2080a8a69ddSRusty Russell prev = i; 2090a8a69ddSRusty Russell sg++; 2100a8a69ddSRusty Russell } 2110a8a69ddSRusty Russell /* Last one doesn't continue. */ 2120a8a69ddSRusty Russell vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT; 2130a8a69ddSRusty Russell 2140a8a69ddSRusty Russell /* Update free pointer */ 2150a8a69ddSRusty Russell vq->free_head = i; 2160a8a69ddSRusty Russell 2179fa29b9dSMark McLoughlin add_head: 2180a8a69ddSRusty Russell /* Set token. */ 2190a8a69ddSRusty Russell vq->data[head] = data; 2200a8a69ddSRusty Russell 2210a8a69ddSRusty Russell /* Put entry in available array (but don't update avail->idx until they 2220a8a69ddSRusty Russell * do sync). FIXME: avoid modulus here? */ 2230a8a69ddSRusty Russell avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num; 2240a8a69ddSRusty Russell vq->vring.avail->ring[avail] = head; 2250a8a69ddSRusty Russell 2260a8a69ddSRusty Russell pr_debug("Added buffer head %i to %p\n", head, vq); 2270a8a69ddSRusty Russell END_USE(vq); 2283c1b27d5SRusty Russell 2293c1b27d5SRusty Russell /* If we're indirect, we can fit many (assuming not OOM). */ 2303c1b27d5SRusty Russell if (vq->indirect) 2313c1b27d5SRusty Russell return vq->num_free ? vq->vring.num : 0; 2323c1b27d5SRusty Russell return vq->num_free; 2330a8a69ddSRusty Russell } 2340a8a69ddSRusty Russell 2350a8a69ddSRusty Russell static void vring_kick(struct virtqueue *_vq) 2360a8a69ddSRusty Russell { 2370a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 2380a8a69ddSRusty Russell START_USE(vq); 2390a8a69ddSRusty Russell /* Descriptors and available array need to be set before we expose the 2400a8a69ddSRusty Russell * new available array entries. */ 241d57ed95dSMichael S. Tsirkin virtio_wmb(); 2420a8a69ddSRusty Russell 2430a8a69ddSRusty Russell vq->vring.avail->idx += vq->num_added; 2440a8a69ddSRusty Russell vq->num_added = 0; 2450a8a69ddSRusty Russell 2460a8a69ddSRusty Russell /* Need to update avail index before checking if we should notify */ 247d57ed95dSMichael S. Tsirkin virtio_mb(); 2480a8a69ddSRusty Russell 2490a8a69ddSRusty Russell if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY)) 2500a8a69ddSRusty Russell /* Prod other side to tell it about changes. */ 2510a8a69ddSRusty Russell vq->notify(&vq->vq); 2520a8a69ddSRusty Russell 2530a8a69ddSRusty Russell END_USE(vq); 2540a8a69ddSRusty Russell } 2550a8a69ddSRusty Russell 2560a8a69ddSRusty Russell static void detach_buf(struct vring_virtqueue *vq, unsigned int head) 2570a8a69ddSRusty Russell { 2580a8a69ddSRusty Russell unsigned int i; 2590a8a69ddSRusty Russell 2600a8a69ddSRusty Russell /* Clear data ptr. */ 2610a8a69ddSRusty Russell vq->data[head] = NULL; 2620a8a69ddSRusty Russell 2630a8a69ddSRusty Russell /* Put back on free list: find end */ 2640a8a69ddSRusty Russell i = head; 2659fa29b9dSMark McLoughlin 2669fa29b9dSMark McLoughlin /* Free the indirect table */ 2679fa29b9dSMark McLoughlin if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT) 2689fa29b9dSMark McLoughlin kfree(phys_to_virt(vq->vring.desc[i].addr)); 2699fa29b9dSMark McLoughlin 2700a8a69ddSRusty Russell while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) { 2710a8a69ddSRusty Russell i = vq->vring.desc[i].next; 2720a8a69ddSRusty Russell vq->num_free++; 2730a8a69ddSRusty Russell } 2740a8a69ddSRusty Russell 2750a8a69ddSRusty Russell vq->vring.desc[i].next = vq->free_head; 2760a8a69ddSRusty Russell vq->free_head = head; 2770a8a69ddSRusty Russell /* Plus final descriptor */ 2780a8a69ddSRusty Russell vq->num_free++; 2790a8a69ddSRusty Russell } 2800a8a69ddSRusty Russell 2810a8a69ddSRusty Russell static inline bool more_used(const struct vring_virtqueue *vq) 2820a8a69ddSRusty Russell { 2830a8a69ddSRusty Russell return vq->last_used_idx != vq->vring.used->idx; 2840a8a69ddSRusty Russell } 2850a8a69ddSRusty Russell 2860a8a69ddSRusty Russell static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len) 2870a8a69ddSRusty Russell { 2880a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 2890a8a69ddSRusty Russell void *ret; 2900a8a69ddSRusty Russell unsigned int i; 2910a8a69ddSRusty Russell 2920a8a69ddSRusty Russell START_USE(vq); 2930a8a69ddSRusty Russell 2945ef82752SRusty Russell if (unlikely(vq->broken)) { 2955ef82752SRusty Russell END_USE(vq); 2965ef82752SRusty Russell return NULL; 2975ef82752SRusty Russell } 2985ef82752SRusty Russell 2990a8a69ddSRusty Russell if (!more_used(vq)) { 3000a8a69ddSRusty Russell pr_debug("No more buffers in queue\n"); 3010a8a69ddSRusty Russell END_USE(vq); 3020a8a69ddSRusty Russell return NULL; 3030a8a69ddSRusty Russell } 3040a8a69ddSRusty Russell 3052d61ba95SMichael S. Tsirkin /* Only get used array entries after they have been exposed by host. */ 306d57ed95dSMichael S. Tsirkin virtio_rmb(); 3072d61ba95SMichael S. Tsirkin 3080a8a69ddSRusty Russell i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id; 3090a8a69ddSRusty Russell *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len; 3100a8a69ddSRusty Russell 3110a8a69ddSRusty Russell if (unlikely(i >= vq->vring.num)) { 3120a8a69ddSRusty Russell BAD_RING(vq, "id %u out of range\n", i); 3130a8a69ddSRusty Russell return NULL; 3140a8a69ddSRusty Russell } 3150a8a69ddSRusty Russell if (unlikely(!vq->data[i])) { 3160a8a69ddSRusty Russell BAD_RING(vq, "id %u is not a head!\n", i); 3170a8a69ddSRusty Russell return NULL; 3180a8a69ddSRusty Russell } 3190a8a69ddSRusty Russell 3200a8a69ddSRusty Russell /* detach_buf clears data, so grab it now. */ 3210a8a69ddSRusty Russell ret = vq->data[i]; 3220a8a69ddSRusty Russell detach_buf(vq, i); 3230a8a69ddSRusty Russell vq->last_used_idx++; 3240a8a69ddSRusty Russell END_USE(vq); 3250a8a69ddSRusty Russell return ret; 3260a8a69ddSRusty Russell } 3270a8a69ddSRusty Russell 32818445c4dSRusty Russell static void vring_disable_cb(struct virtqueue *_vq) 32918445c4dSRusty Russell { 33018445c4dSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 33118445c4dSRusty Russell 33218445c4dSRusty Russell vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; 33318445c4dSRusty Russell } 33418445c4dSRusty Russell 33518445c4dSRusty Russell static bool vring_enable_cb(struct virtqueue *_vq) 3360a8a69ddSRusty Russell { 3370a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 3380a8a69ddSRusty Russell 3390a8a69ddSRusty Russell START_USE(vq); 3400a8a69ddSRusty Russell 3410a8a69ddSRusty Russell /* We optimistically turn back on interrupts, then check if there was 3420a8a69ddSRusty Russell * more to do. */ 3430a8a69ddSRusty Russell vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT; 344d57ed95dSMichael S. Tsirkin virtio_mb(); 3450a8a69ddSRusty Russell if (unlikely(more_used(vq))) { 3460a8a69ddSRusty Russell END_USE(vq); 3470a8a69ddSRusty Russell return false; 3480a8a69ddSRusty Russell } 3490a8a69ddSRusty Russell 3500a8a69ddSRusty Russell END_USE(vq); 3510a8a69ddSRusty Russell return true; 3520a8a69ddSRusty Russell } 3530a8a69ddSRusty Russell 354*c021eac4SShirley Ma static void *vring_detach_unused_buf(struct virtqueue *_vq) 355*c021eac4SShirley Ma { 356*c021eac4SShirley Ma struct vring_virtqueue *vq = to_vvq(_vq); 357*c021eac4SShirley Ma unsigned int i; 358*c021eac4SShirley Ma void *buf; 359*c021eac4SShirley Ma 360*c021eac4SShirley Ma START_USE(vq); 361*c021eac4SShirley Ma 362*c021eac4SShirley Ma for (i = 0; i < vq->vring.num; i++) { 363*c021eac4SShirley Ma if (!vq->data[i]) 364*c021eac4SShirley Ma continue; 365*c021eac4SShirley Ma /* detach_buf clears data, so grab it now. */ 366*c021eac4SShirley Ma buf = vq->data[i]; 367*c021eac4SShirley Ma detach_buf(vq, i); 368*c021eac4SShirley Ma END_USE(vq); 369*c021eac4SShirley Ma return buf; 370*c021eac4SShirley Ma } 371*c021eac4SShirley Ma /* That should have freed everything. */ 372*c021eac4SShirley Ma BUG_ON(vq->num_free != vq->vring.num); 373*c021eac4SShirley Ma 374*c021eac4SShirley Ma END_USE(vq); 375*c021eac4SShirley Ma return NULL; 376*c021eac4SShirley Ma } 377*c021eac4SShirley Ma 3780a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq) 3790a8a69ddSRusty Russell { 3800a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 3810a8a69ddSRusty Russell 3820a8a69ddSRusty Russell if (!more_used(vq)) { 3830a8a69ddSRusty Russell pr_debug("virtqueue interrupt with no work for %p\n", vq); 3840a8a69ddSRusty Russell return IRQ_NONE; 3850a8a69ddSRusty Russell } 3860a8a69ddSRusty Russell 3870a8a69ddSRusty Russell if (unlikely(vq->broken)) 3880a8a69ddSRusty Russell return IRQ_HANDLED; 3890a8a69ddSRusty Russell 3900a8a69ddSRusty Russell pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); 39118445c4dSRusty Russell if (vq->vq.callback) 39218445c4dSRusty Russell vq->vq.callback(&vq->vq); 3930a8a69ddSRusty Russell 3940a8a69ddSRusty Russell return IRQ_HANDLED; 3950a8a69ddSRusty Russell } 396c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt); 3970a8a69ddSRusty Russell 3980a8a69ddSRusty Russell static struct virtqueue_ops vring_vq_ops = { 3990a8a69ddSRusty Russell .add_buf = vring_add_buf, 4000a8a69ddSRusty Russell .get_buf = vring_get_buf, 4010a8a69ddSRusty Russell .kick = vring_kick, 40218445c4dSRusty Russell .disable_cb = vring_disable_cb, 40318445c4dSRusty Russell .enable_cb = vring_enable_cb, 404*c021eac4SShirley Ma .detach_unused_buf = vring_detach_unused_buf, 4050a8a69ddSRusty Russell }; 4060a8a69ddSRusty Russell 4070a8a69ddSRusty Russell struct virtqueue *vring_new_virtqueue(unsigned int num, 40887c7d57cSRusty Russell unsigned int vring_align, 4090a8a69ddSRusty Russell struct virtio_device *vdev, 4100a8a69ddSRusty Russell void *pages, 4110a8a69ddSRusty Russell void (*notify)(struct virtqueue *), 4129499f5e7SRusty Russell void (*callback)(struct virtqueue *), 4139499f5e7SRusty Russell const char *name) 4140a8a69ddSRusty Russell { 4150a8a69ddSRusty Russell struct vring_virtqueue *vq; 4160a8a69ddSRusty Russell unsigned int i; 4170a8a69ddSRusty Russell 41842b36cc0SRusty Russell /* We assume num is a power of 2. */ 41942b36cc0SRusty Russell if (num & (num - 1)) { 42042b36cc0SRusty Russell dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); 42142b36cc0SRusty Russell return NULL; 42242b36cc0SRusty Russell } 42342b36cc0SRusty Russell 4240a8a69ddSRusty Russell vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL); 4250a8a69ddSRusty Russell if (!vq) 4260a8a69ddSRusty Russell return NULL; 4270a8a69ddSRusty Russell 42887c7d57cSRusty Russell vring_init(&vq->vring, num, pages, vring_align); 4290a8a69ddSRusty Russell vq->vq.callback = callback; 4300a8a69ddSRusty Russell vq->vq.vdev = vdev; 4310a8a69ddSRusty Russell vq->vq.vq_ops = &vring_vq_ops; 4329499f5e7SRusty Russell vq->vq.name = name; 4330a8a69ddSRusty Russell vq->notify = notify; 4340a8a69ddSRusty Russell vq->broken = false; 4350a8a69ddSRusty Russell vq->last_used_idx = 0; 4360a8a69ddSRusty Russell vq->num_added = 0; 4379499f5e7SRusty Russell list_add_tail(&vq->vq.list, &vdev->vqs); 4380a8a69ddSRusty Russell #ifdef DEBUG 4390a8a69ddSRusty Russell vq->in_use = false; 4400a8a69ddSRusty Russell #endif 4410a8a69ddSRusty Russell 4429fa29b9dSMark McLoughlin vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC); 4439fa29b9dSMark McLoughlin 4440a8a69ddSRusty Russell /* No callback? Tell other side not to bother us. */ 4450a8a69ddSRusty Russell if (!callback) 4460a8a69ddSRusty Russell vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; 4470a8a69ddSRusty Russell 4480a8a69ddSRusty Russell /* Put everything in free lists. */ 4490a8a69ddSRusty Russell vq->num_free = num; 4500a8a69ddSRusty Russell vq->free_head = 0; 4510a8a69ddSRusty Russell for (i = 0; i < num-1; i++) 4520a8a69ddSRusty Russell vq->vring.desc[i].next = i+1; 4530a8a69ddSRusty Russell 4540a8a69ddSRusty Russell return &vq->vq; 4550a8a69ddSRusty Russell } 456c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue); 4570a8a69ddSRusty Russell 4580a8a69ddSRusty Russell void vring_del_virtqueue(struct virtqueue *vq) 4590a8a69ddSRusty Russell { 4609499f5e7SRusty Russell list_del(&vq->list); 4610a8a69ddSRusty Russell kfree(to_vvq(vq)); 4620a8a69ddSRusty Russell } 463c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue); 4640a8a69ddSRusty Russell 465e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */ 466e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev) 467e34f8725SRusty Russell { 468e34f8725SRusty Russell unsigned int i; 469e34f8725SRusty Russell 470e34f8725SRusty Russell for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { 471e34f8725SRusty Russell switch (i) { 4729fa29b9dSMark McLoughlin case VIRTIO_RING_F_INDIRECT_DESC: 4739fa29b9dSMark McLoughlin break; 474e34f8725SRusty Russell default: 475e34f8725SRusty Russell /* We don't understand this bit. */ 476e34f8725SRusty Russell clear_bit(i, vdev->features); 477e34f8725SRusty Russell } 478e34f8725SRusty Russell } 479e34f8725SRusty Russell } 480e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features); 481e34f8725SRusty Russell 482c6fd4701SRusty Russell MODULE_LICENSE("GPL"); 483