1 /******************************************************************************
2 * Copyright (c) 2012 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 *****************************************************************************/
18 #include "virtio-scsi.h"
20 int virtioscsi_send(struct virtio_device *dev,
21 struct virtio_scsi_req_cmd *req,
22 struct virtio_scsi_resp_cmd *resp,
23 int is_read, void *buf, uint64_t buf_len)
25 struct vring_desc *desc;
26 struct vring_desc *vq_desc; /* Descriptor vring */
27 struct vring_avail *vq_avail; /* "Available" vring */
28 struct vring_used *vq_used; /* "Used" vring */
30 volatile uint16_t *current_used_idx;
31 uint16_t last_used_idx;
33 uint32_t vq_size, time;
35 int vq = VIRTIO_SCSI_REQUEST_VQ;
37 vq_size = virtio_get_qsize(dev, vq);
38 vq_desc = virtio_get_vring_desc(dev, vq);
39 vq_avail = virtio_get_vring_avail(dev, vq);
40 vq_used = virtio_get_vring_used(dev, vq);
42 last_used_idx = vq_used->idx;
43 current_used_idx = &vq_used->idx;
45 /* Determine descriptor index */
46 id = (vq_avail->idx * 3) % vq_size;
49 desc->addr = (uint64_t)req;
50 desc->len = sizeof(*req);
51 desc->flags = VRING_DESC_F_NEXT;
52 desc->next = (id + 1) % vq_size;
54 /* Set up virtqueue descriptor for data */
55 desc = &vq_desc[(id + 1) % vq_size];
56 desc->addr = (uint64_t)resp;
57 desc->len = sizeof(*resp);
58 desc->flags = VRING_DESC_F_NEXT | VRING_DESC_F_WRITE;
59 desc->next = (id + 2) % vq_size;
62 /* Set up virtqueue descriptor for status */
63 desc = &vq_desc[(id + 2) % vq_size];
64 desc->addr = (uint64_t)buf;
66 desc->flags = is_read ? VRING_DESC_F_WRITE : 0;
69 desc->flags &= ~VRING_DESC_F_NEXT;
71 vq_avail->ring[vq_avail->idx % vq_size] = id;
75 /* Tell HV that the vq is ready */
76 virtio_queue_notify(dev, vq);
78 /* Wait for host to consume the descriptor */
79 time = SLOF_GetTimer() + VIRTIO_TIMEOUT;
80 while (*current_used_idx == last_used_idx) {
81 // do something better
83 if (time < SLOF_GetTimer())
91 * Initialize virtio-block device.
92 * @param dev pointer to virtio device information
94 int virtioscsi_init(struct virtio_device *dev)
96 struct vring_avail *vq_avail;
101 // XXX That will clear the virtq base. We need to move
102 // initializing it to here anyway
104 // virtio_reset_device(dev);
106 /* Acknowledge device. */
107 virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE);
109 /* Tell HV that we know how to drive the device. */
110 virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE|VIRTIO_STAT_DRIVER);
112 /* Device specific setup - we do not support special features right now */
113 virtio_set_guest_features(dev, 0);
116 qsize = virtio_get_qsize(dev, idx);
119 virtio_vring_size(qsize);
121 vq_avail = virtio_get_vring_avail(dev, 0);
122 vq_avail->flags = VRING_AVAIL_F_NO_INTERRUPT;
127 /* Tell HV that setup succeeded */
128 virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE|VIRTIO_STAT_DRIVER
129 |VIRTIO_STAT_DRIVER_OK);
135 * Shutdown the virtio-block device.
136 * @param dev pointer to virtio device information
138 void virtioscsi_shutdown(struct virtio_device *dev)
141 virtio_set_status(dev, VIRTIO_STAT_FAILED);
144 virtio_reset_device(dev);