1 /******************************************************************************
2 * Copyright (c) 2011 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
17 #include "virtio-blk.h"
19 #define DEFAULT_SECTOR_SIZE 512
22 * Initialize virtio-block device.
23 * @param dev pointer to virtio device information
26 virtioblk_init(struct virtio_device *dev)
28 struct vring_avail *vq_avail;
29 int blk_size = DEFAULT_SECTOR_SIZE;
33 // XXX That will clear the virtq base. We need to move
34 // initializing it to here anyway
36 // virtio_reset_device(dev);
38 /* Acknowledge device. */
39 virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE);
41 /* Tell HV that we know how to drive the device. */
42 virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE|VIRTIO_STAT_DRIVER);
44 /* Device specific setup - we support F_BLK_SIZE */
45 virtio_set_guest_features(dev, VIRTIO_BLK_F_BLK_SIZE);
47 vq_avail = virtio_get_vring_avail(dev, 0);
48 vq_avail->flags = VRING_AVAIL_F_NO_INTERRUPT;
51 /* Tell HV that setup succeeded */
52 virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE|VIRTIO_STAT_DRIVER
53 |VIRTIO_STAT_DRIVER_OK);
55 virtio_get_host_features(dev, &features);
56 if (features & VIRTIO_BLK_F_BLK_SIZE) {
57 blk_size = virtio_get_config(dev,
58 offset_of(struct virtio_blk_cfg, blk_size),
67 * Shutdown the virtio-block device.
68 * @param dev pointer to virtio device information
71 virtioblk_shutdown(struct virtio_device *dev)
74 virtio_set_status(dev, VIRTIO_STAT_FAILED);
77 virtio_reset_device(dev);
83 * @param reg pointer to "reg" property
84 * @param buf pointer to destination buffer
85 * @param blocknum block number of the first block that should be read
86 * @param cnt amount of blocks that should be read
87 * @return number of blocks that have been read successfully
90 virtioblk_read(struct virtio_device *dev, char *buf, long blocknum, long cnt)
92 struct vring_desc *desc;
94 static struct virtio_blk_req blkhdr;
95 //struct virtio_blk_config *blkconf;
97 uint32_t vq_size, time;
98 struct vring_desc *vq_desc; /* Descriptor vring */
99 struct vring_avail *vq_avail; /* "Available" vring */
100 struct vring_used *vq_used; /* "Used" vring */
101 volatile uint8_t status = -1;
102 volatile uint16_t *current_used_idx;
103 uint16_t last_used_idx;
104 int blk_size = DEFAULT_SECTOR_SIZE;
106 //printf("virtioblk_read: dev=%p buf=%p blocknum=%li count=%li\n",
107 // dev, buf, blocknum, cnt);
109 /* Check whether request is within disk capacity */
110 capacity = virtio_get_config(dev,
111 offset_of(struct virtio_blk_cfg, capacity),
113 if (blocknum + cnt - 1 > capacity) {
114 puts("virtioblk_read: Access beyond end of device!");
118 blk_size = virtio_get_config(dev,
119 offset_of(struct virtio_blk_cfg, blk_size),
121 if (blk_size % DEFAULT_SECTOR_SIZE) {
122 fprintf(stderr, "virtio-blk: Unaligned sector read %d\n", blk_size);
126 vq_size = virtio_get_qsize(dev, 0);
127 vq_desc = virtio_get_vring_desc(dev, 0);
128 vq_avail = virtio_get_vring_avail(dev, 0);
129 vq_used = virtio_get_vring_used(dev, 0);
131 last_used_idx = vq_used->idx;
132 current_used_idx = &vq_used->idx;
135 blkhdr.type = VIRTIO_BLK_T_IN | VIRTIO_BLK_T_BARRIER;
137 blkhdr.sector = blocknum * blk_size / DEFAULT_SECTOR_SIZE;
139 /* Determine descriptor index */
140 id = (vq_avail->idx * 3) % vq_size;
142 /* Set up virtqueue descriptor for header */
144 desc->addr = (uint64_t)&blkhdr;
145 desc->len = sizeof(struct virtio_blk_req);
146 desc->flags = VRING_DESC_F_NEXT;
147 desc->next = (id + 1) % vq_size;
149 /* Set up virtqueue descriptor for data */
150 desc = &vq_desc[(id + 1) % vq_size];
151 desc->addr = (uint64_t)buf;
152 desc->len = cnt * blk_size;
153 desc->flags = VRING_DESC_F_NEXT | VRING_DESC_F_WRITE;
154 desc->next = (id + 2) % vq_size;
156 /* Set up virtqueue descriptor for status */
157 desc = &vq_desc[(id + 2) % vq_size];
158 desc->addr = (uint64_t)&status;
160 desc->flags = VRING_DESC_F_WRITE;
163 vq_avail->ring[vq_avail->idx % vq_size] = id;
167 /* Tell HV that the queue is ready */
168 virtio_queue_notify(dev, 0);
170 /* Wait for host to consume the descriptor */
171 time = SLOF_GetTimer() + VIRTIO_TIMEOUT;
172 while (*current_used_idx == last_used_idx) {
173 // do something better
175 if (time < SLOF_GetTimer())
182 printf("virtioblk_read failed! status = %i\n", status);