s390-bios: Decouple channel i/o logic from virtio
[qemu/ar7.git] / pc-bios / s390-ccw / virtio.c
blobaa9da7253fb20187211bd3b35acb83292e3543ac
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"
18 #define VRING_WAIT_REPLY_TIMEOUT 30
20 static VRing block[VIRTIO_MAX_VQS];
21 static char ring_area[VIRTIO_RING_SIZE * VIRTIO_MAX_VQS]
22 __attribute__((__aligned__(PAGE_SIZE)));
24 static VDev vdev = {
25 .nr_vqs = 1,
26 .vrings = block,
27 .cmd_vr_idx = 0,
28 .ring_area = ring_area,
29 .wait_reply_timeout = VRING_WAIT_REPLY_TIMEOUT,
30 .schid = { .one = 1 },
31 .scsi_block_size = VIRTIO_SCSI_BLOCK_SIZE,
32 .blk_factor = 1,
35 VDev *virtio_get_device(void)
37 return &vdev;
40 VirtioDevType virtio_get_device_type(void)
42 return vdev.senseid.cu_model;
45 /* virtio spec v1.0 para 4.3.3.2 */
46 static long kvm_hypercall(unsigned long nr, unsigned long param1,
47 unsigned long param2, unsigned long param3)
49 register ulong r_nr asm("1") = nr;
50 register ulong r_param1 asm("2") = param1;
51 register ulong r_param2 asm("3") = param2;
52 register ulong r_param3 asm("4") = param3;
53 register long retval asm("2");
55 asm volatile ("diag 2,4,0x500"
56 : "=d" (retval)
57 : "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)
58 : "memory", "cc");
60 return retval;
63 static long virtio_notify(SubChannelId schid, int vq_idx, long cookie)
65 return kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, *(u32 *)&schid,
66 vq_idx, cookie);
69 /***********************************************
70 * Virtio functions *
71 ***********************************************/
73 int drain_irqs(SubChannelId schid)
75 Irb irb = {};
76 int r = 0;
78 while (1) {
79 /* FIXME: make use of TPI, for that enable subchannel and isc */
80 if (tsch(schid, &irb)) {
81 /* Might want to differentiate error codes later on. */
82 if (irb.scsw.cstat) {
83 r = -EIO;
84 } else if (irb.scsw.dstat != 0xc) {
85 r = -EIO;
87 return r;
92 static int run_ccw(VDev *vdev, int cmd, void *ptr, int len)
94 Ccw1 ccw = {};
95 CmdOrb orb = {};
96 int r;
98 enable_subchannel(vdev->schid);
100 /* start subchannel command */
101 orb.fmt = 1;
102 orb.cpa = (u32)(long)&ccw;
103 orb.lpm = 0x80;
105 ccw.cmd_code = cmd;
106 ccw.cda = (long)ptr;
107 ccw.count = len;
109 r = ssch(vdev->schid, &orb);
111 * XXX Wait until device is done processing the CCW. For now we can
112 * assume that a simple tsch will have finished the CCW processing,
113 * but the architecture allows for asynchronous operation
115 if (!r) {
116 r = drain_irqs(vdev->schid);
118 return r;
121 static void vring_init(VRing *vr, VqInfo *info)
123 void *p = (void *) info->queue;
125 debug_print_addr("init p", p);
126 vr->id = info->index;
127 vr->num = info->num;
128 vr->desc = p;
129 vr->avail = p + info->num * sizeof(VRingDesc);
130 vr->used = (void *)(((unsigned long)&vr->avail->ring[info->num]
131 + info->align - 1) & ~(info->align - 1));
133 /* Zero out all relevant field */
134 vr->avail->flags = 0;
135 vr->avail->idx = 0;
137 /* We're running with interrupts off anyways, so don't bother */
138 vr->used->flags = VRING_USED_F_NO_NOTIFY;
139 vr->used->idx = 0;
140 vr->used_idx = 0;
141 vr->next_idx = 0;
142 vr->cookie = 0;
144 debug_print_addr("init vr", vr);
147 bool vring_notify(VRing *vr)
149 vr->cookie = virtio_notify(vr->schid, vr->id, vr->cookie);
150 return vr->cookie >= 0;
153 void vring_send_buf(VRing *vr, void *p, int len, int flags)
155 /* For follow-up chains we need to keep the first entry point */
156 if (!(flags & VRING_HIDDEN_IS_CHAIN)) {
157 vr->avail->ring[vr->avail->idx % vr->num] = vr->next_idx;
160 vr->desc[vr->next_idx].addr = (ulong)p;
161 vr->desc[vr->next_idx].len = len;
162 vr->desc[vr->next_idx].flags = flags & ~VRING_HIDDEN_IS_CHAIN;
163 vr->desc[vr->next_idx].next = vr->next_idx;
164 vr->desc[vr->next_idx].next++;
165 vr->next_idx++;
167 /* Chains only have a single ID */
168 if (!(flags & VRING_DESC_F_NEXT)) {
169 vr->avail->idx++;
173 u64 get_clock(void)
175 u64 r;
177 asm volatile("stck %0" : "=Q" (r) : : "cc");
178 return r;
181 ulong get_second(void)
183 return (get_clock() >> 12) / 1000000;
186 int vr_poll(VRing *vr)
188 if (vr->used->idx == vr->used_idx) {
189 vring_notify(vr);
190 yield();
191 return 0;
194 vr->used_idx = vr->used->idx;
195 vr->next_idx = 0;
196 vr->desc[0].len = 0;
197 vr->desc[0].flags = 0;
198 return 1; /* vr has been updated */
202 * Wait for the host to reply.
204 * timeout is in seconds if > 0.
206 * Returns 0 on success, 1 on timeout.
208 int vring_wait_reply(void)
210 ulong target_second = get_second() + vdev.wait_reply_timeout;
212 /* Wait for any queue to be updated by the host */
213 do {
214 int i, r = 0;
216 for (i = 0; i < vdev.nr_vqs; i++) {
217 r += vr_poll(&vdev.vrings[i]);
219 yield();
220 if (r) {
221 return 0;
223 } while (!vdev.wait_reply_timeout || (get_second() < target_second));
225 return 1;
228 int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd)
230 VRing *vr = &vdev->vrings[vqid];
231 int i = 0;
233 do {
234 vring_send_buf(vr, cmd[i].data, cmd[i].size,
235 cmd[i].flags | (i ? VRING_HIDDEN_IS_CHAIN : 0));
236 } while (cmd[i++].flags & VRING_DESC_F_NEXT);
238 vring_wait_reply();
239 if (drain_irqs(vr->schid)) {
240 return -1;
242 return 0;
245 void virtio_setup_ccw(VDev *vdev)
247 int i, rc, cfg_size = 0;
248 unsigned char status = VIRTIO_CONFIG_S_DRIVER_OK;
249 struct VirtioFeatureDesc {
250 uint32_t features;
251 uint8_t index;
252 } __attribute__((packed)) feats;
254 IPL_assert(virtio_is_supported(vdev->schid), "PE");
255 /* device ID has been established now */
257 vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
258 vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
260 run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0);
262 switch (vdev->senseid.cu_model) {
263 case VIRTIO_ID_NET:
264 vdev->nr_vqs = 2;
265 vdev->cmd_vr_idx = 0;
266 cfg_size = sizeof(vdev->config.net);
267 break;
268 case VIRTIO_ID_BLOCK:
269 vdev->nr_vqs = 1;
270 vdev->cmd_vr_idx = 0;
271 cfg_size = sizeof(vdev->config.blk);
272 break;
273 case VIRTIO_ID_SCSI:
274 vdev->nr_vqs = 3;
275 vdev->cmd_vr_idx = VR_REQUEST;
276 cfg_size = sizeof(vdev->config.scsi);
277 break;
278 default:
279 panic("Unsupported virtio device\n");
281 IPL_assert(run_ccw(vdev, CCW_CMD_READ_CONF, &vdev->config, cfg_size) == 0,
282 "Could not get block device configuration");
284 /* Feature negotiation */
285 for (i = 0; i < ARRAY_SIZE(vdev->guest_features); i++) {
286 feats.features = 0;
287 feats.index = i;
288 rc = run_ccw(vdev, CCW_CMD_READ_FEAT, &feats, sizeof(feats));
289 IPL_assert(rc == 0, "Could not get features bits");
290 vdev->guest_features[i] &= bswap32(feats.features);
291 feats.features = bswap32(vdev->guest_features[i]);
292 rc = run_ccw(vdev, CCW_CMD_WRITE_FEAT, &feats, sizeof(feats));
293 IPL_assert(rc == 0, "Could not set features bits");
296 for (i = 0; i < vdev->nr_vqs; i++) {
297 VqInfo info = {
298 .queue = (unsigned long long) ring_area + (i * VIRTIO_RING_SIZE),
299 .align = KVM_S390_VIRTIO_RING_ALIGN,
300 .index = i,
301 .num = 0,
303 VqConfig config = {
304 .index = i,
305 .num = 0,
308 IPL_assert(
309 run_ccw(vdev, CCW_CMD_READ_VQ_CONF, &config, sizeof(config)) == 0,
310 "Could not get block device VQ configuration");
311 info.num = config.num;
312 vring_init(&vdev->vrings[i], &info);
313 vdev->vrings[i].schid = vdev->schid;
314 IPL_assert(run_ccw(vdev, CCW_CMD_SET_VQ, &info, sizeof(info)) == 0,
315 "Cannot set VQ info");
317 IPL_assert(
318 run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status)) == 0,
319 "Could not write status to host");
322 bool virtio_is_supported(SubChannelId schid)
324 vdev.schid = schid;
325 memset(&vdev.senseid, 0, sizeof(vdev.senseid));
326 /* run sense id command */
327 if (run_ccw(&vdev, CCW_CMD_SENSE_ID, &vdev.senseid, sizeof(vdev.senseid))) {
328 return false;
330 if (vdev.senseid.cu_type == 0x3832) {
331 switch (vdev.senseid.cu_model) {
332 case VIRTIO_ID_BLOCK:
333 case VIRTIO_ID_SCSI:
334 case VIRTIO_ID_NET:
335 return true;
338 return false;