tests/9pfs: Turn fs_mkdir() into a helper
[qemu/ar7.git] / pc-bios / s390-ccw / virtio.c
blobab49840db853160c82de46a7065b86b3d5bf0eac
1 /*
2 * Virtio driver bits
4 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
6 * This work is licensed under the terms of the GNU GPL, version 2 or (at
7 * your option) any later version. See the COPYING file in the top-level
8 * directory.
9 */
11 #include "libc.h"
12 #include "s390-ccw.h"
13 #include "cio.h"
14 #include "virtio.h"
15 #include "virtio-scsi.h"
16 #include "bswap.h"
17 #include "helper.h"
18 #include "s390-time.h"
20 #define VRING_WAIT_REPLY_TIMEOUT 30
22 static VRing block[VIRTIO_MAX_VQS];
23 static char ring_area[VIRTIO_RING_SIZE * VIRTIO_MAX_VQS]
24 __attribute__((__aligned__(PAGE_SIZE)));
26 static VDev vdev = {
27 .nr_vqs = 1,
28 .vrings = block,
29 .cmd_vr_idx = 0,
30 .ring_area = ring_area,
31 .wait_reply_timeout = VRING_WAIT_REPLY_TIMEOUT,
32 .schid = { .one = 1 },
33 .scsi_block_size = VIRTIO_SCSI_BLOCK_SIZE,
34 .blk_factor = 1,
37 VDev *virtio_get_device(void)
39 return &vdev;
42 VirtioDevType virtio_get_device_type(void)
44 return vdev.senseid.cu_model;
47 /* virtio spec v1.0 para 4.3.3.2 */
48 static long kvm_hypercall(unsigned long nr, unsigned long param1,
49 unsigned long param2, unsigned long param3)
51 register ulong r_nr asm("1") = nr;
52 register ulong r_param1 asm("2") = param1;
53 register ulong r_param2 asm("3") = param2;
54 register ulong r_param3 asm("4") = param3;
55 register long retval asm("2");
57 asm volatile ("diag 2,4,0x500"
58 : "=d" (retval)
59 : "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)
60 : "memory", "cc");
62 return retval;
65 static long virtio_notify(SubChannelId schid, int vq_idx, long cookie)
67 return kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, *(u32 *)&schid,
68 vq_idx, cookie);
71 /***********************************************
72 * Virtio functions *
73 ***********************************************/
75 int drain_irqs(SubChannelId schid)
77 Irb irb = {};
78 int r = 0;
80 while (1) {
81 /* FIXME: make use of TPI, for that enable subchannel and isc */
82 if (tsch(schid, &irb)) {
83 /* Might want to differentiate error codes later on. */
84 if (irb.scsw.cstat) {
85 r = -EIO;
86 } else if (irb.scsw.dstat != 0xc) {
87 r = -EIO;
89 return r;
94 static int run_ccw(VDev *vdev, int cmd, void *ptr, int len, bool sli)
96 Ccw1 ccw = {};
98 ccw.cmd_code = cmd;
99 ccw.cda = (long)ptr;
100 ccw.count = len;
102 if (sli) {
103 ccw.flags |= CCW_FLAG_SLI;
106 return do_cio(vdev->schid, vdev->senseid.cu_type, ptr2u32(&ccw), CCW_FMT1);
109 static void vring_init(VRing *vr, VqInfo *info)
111 void *p = (void *) info->queue;
113 debug_print_addr("init p", p);
114 vr->id = info->index;
115 vr->num = info->num;
116 vr->desc = p;
117 vr->avail = p + info->num * sizeof(VRingDesc);
118 vr->used = (void *)(((unsigned long)&vr->avail->ring[info->num]
119 + info->align - 1) & ~(info->align - 1));
121 /* Zero out all relevant field */
122 vr->avail->flags = 0;
123 vr->avail->idx = 0;
125 /* We're running with interrupts off anyways, so don't bother */
126 vr->used->flags = VRING_USED_F_NO_NOTIFY;
127 vr->used->idx = 0;
128 vr->used_idx = 0;
129 vr->next_idx = 0;
130 vr->cookie = 0;
132 debug_print_addr("init vr", vr);
135 bool vring_notify(VRing *vr)
137 vr->cookie = virtio_notify(vr->schid, vr->id, vr->cookie);
138 return vr->cookie >= 0;
141 void vring_send_buf(VRing *vr, void *p, int len, int flags)
143 /* For follow-up chains we need to keep the first entry point */
144 if (!(flags & VRING_HIDDEN_IS_CHAIN)) {
145 vr->avail->ring[vr->avail->idx % vr->num] = vr->next_idx;
148 vr->desc[vr->next_idx].addr = (ulong)p;
149 vr->desc[vr->next_idx].len = len;
150 vr->desc[vr->next_idx].flags = flags & ~VRING_HIDDEN_IS_CHAIN;
151 vr->desc[vr->next_idx].next = vr->next_idx;
152 vr->desc[vr->next_idx].next++;
153 vr->next_idx++;
155 /* Chains only have a single ID */
156 if (!(flags & VRING_DESC_F_NEXT)) {
157 vr->avail->idx++;
161 int vr_poll(VRing *vr)
163 if (vr->used->idx == vr->used_idx) {
164 vring_notify(vr);
165 yield();
166 return 0;
169 vr->used_idx = vr->used->idx;
170 vr->next_idx = 0;
171 vr->desc[0].len = 0;
172 vr->desc[0].flags = 0;
173 return 1; /* vr has been updated */
177 * Wait for the host to reply.
179 * timeout is in seconds if > 0.
181 * Returns 0 on success, 1 on timeout.
183 int vring_wait_reply(void)
185 ulong target_second = get_time_seconds() + vdev.wait_reply_timeout;
187 /* Wait for any queue to be updated by the host */
188 do {
189 int i, r = 0;
191 for (i = 0; i < vdev.nr_vqs; i++) {
192 r += vr_poll(&vdev.vrings[i]);
194 yield();
195 if (r) {
196 return 0;
198 } while (!vdev.wait_reply_timeout || (get_time_seconds() < target_second));
200 return 1;
203 int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd)
205 VRing *vr = &vdev->vrings[vqid];
206 int i = 0;
208 do {
209 vring_send_buf(vr, cmd[i].data, cmd[i].size,
210 cmd[i].flags | (i ? VRING_HIDDEN_IS_CHAIN : 0));
211 } while (cmd[i++].flags & VRING_DESC_F_NEXT);
213 vring_wait_reply();
214 if (drain_irqs(vr->schid)) {
215 return -1;
217 return 0;
220 void virtio_setup_ccw(VDev *vdev)
222 int i, rc, cfg_size = 0;
223 unsigned char status = VIRTIO_CONFIG_S_DRIVER_OK;
224 struct VirtioFeatureDesc {
225 uint32_t features;
226 uint8_t index;
227 } __attribute__((packed)) feats;
229 IPL_assert(virtio_is_supported(vdev->schid), "PE");
230 /* device ID has been established now */
232 vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
233 vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
235 run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
237 switch (vdev->senseid.cu_model) {
238 case VIRTIO_ID_NET:
239 vdev->nr_vqs = 2;
240 vdev->cmd_vr_idx = 0;
241 cfg_size = sizeof(vdev->config.net);
242 break;
243 case VIRTIO_ID_BLOCK:
244 vdev->nr_vqs = 1;
245 vdev->cmd_vr_idx = 0;
246 cfg_size = sizeof(vdev->config.blk);
247 break;
248 case VIRTIO_ID_SCSI:
249 vdev->nr_vqs = 3;
250 vdev->cmd_vr_idx = VR_REQUEST;
251 cfg_size = sizeof(vdev->config.scsi);
252 break;
253 default:
254 panic("Unsupported virtio device\n");
256 IPL_assert(
257 run_ccw(vdev, CCW_CMD_READ_CONF, &vdev->config, cfg_size, false) == 0,
258 "Could not get block device configuration");
260 /* Feature negotiation */
261 for (i = 0; i < ARRAY_SIZE(vdev->guest_features); i++) {
262 feats.features = 0;
263 feats.index = i;
264 rc = run_ccw(vdev, CCW_CMD_READ_FEAT, &feats, sizeof(feats), false);
265 IPL_assert(rc == 0, "Could not get features bits");
266 vdev->guest_features[i] &= bswap32(feats.features);
267 feats.features = bswap32(vdev->guest_features[i]);
268 rc = run_ccw(vdev, CCW_CMD_WRITE_FEAT, &feats, sizeof(feats), false);
269 IPL_assert(rc == 0, "Could not set features bits");
272 for (i = 0; i < vdev->nr_vqs; i++) {
273 VqInfo info = {
274 .queue = (unsigned long long) ring_area + (i * VIRTIO_RING_SIZE),
275 .align = KVM_S390_VIRTIO_RING_ALIGN,
276 .index = i,
277 .num = 0,
279 VqConfig config = {
280 .index = i,
281 .num = 0,
284 IPL_assert(
285 run_ccw(vdev, CCW_CMD_READ_VQ_CONF, &config, sizeof(config), false) == 0,
286 "Could not get block device VQ configuration");
287 info.num = config.num;
288 vring_init(&vdev->vrings[i], &info);
289 vdev->vrings[i].schid = vdev->schid;
290 IPL_assert(
291 run_ccw(vdev, CCW_CMD_SET_VQ, &info, sizeof(info), false) == 0,
292 "Cannot set VQ info");
294 IPL_assert(
295 run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false) == 0,
296 "Could not write status to host");
299 bool virtio_is_supported(SubChannelId schid)
301 vdev.schid = schid;
302 memset(&vdev.senseid, 0, sizeof(vdev.senseid));
305 * Run sense id command.
306 * The size of the senseid data differs between devices (notably,
307 * between virtio devices and dasds), so specify the largest possible
308 * size and suppress the incorrect length indication for smaller sizes.
310 if (run_ccw(&vdev, CCW_CMD_SENSE_ID, &vdev.senseid, sizeof(vdev.senseid),
311 true)) {
312 return false;
314 if (vdev.senseid.cu_type == 0x3832) {
315 switch (vdev.senseid.cu_model) {
316 case VIRTIO_ID_BLOCK:
317 case VIRTIO_ID_SCSI:
318 case VIRTIO_ID_NET:
319 return true;
322 return false;