xref: /openbmc/qemu/pc-bios/s390-ccw/virtio.c (revision 7d87775f)
1 /*
2  * Virtio driver bits
3  *
4  * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5  *
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  */
10 
11 #include <string.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"
19 
20 #define VRING_WAIT_REPLY_TIMEOUT 30
21 
22 static VRing block[VIRTIO_MAX_VQS];
23 static char ring_area[VIRTIO_RING_SIZE * VIRTIO_MAX_VQS]
24                      __attribute__((__aligned__(PAGE_SIZE)));
25 
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,
35 };
36 
37 VDev *virtio_get_device(void)
38 {
39     return &vdev;
40 }
41 
42 VirtioDevType virtio_get_device_type(void)
43 {
44     return vdev.senseid.cu_model;
45 }
46 
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)
50 {
51     register unsigned long r_nr asm("1") = nr;
52     register unsigned long r_param1 asm("2") = param1;
53     register unsigned long r_param2 asm("3") = param2;
54     register unsigned long r_param3 asm("4") = param3;
55     register long retval asm("2");
56 
57     asm volatile ("diag %%r2,%%r4,0x500"
58                   : "=d" (retval)
59                   : "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)
60                   : "memory", "cc");
61 
62     return retval;
63 }
64 
65 static long virtio_notify(SubChannelId schid, int vq_idx, long cookie)
66 {
67     return kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, *(u32 *)&schid,
68                          vq_idx, cookie);
69 }
70 
71 /***********************************************
72  *             Virtio functions                *
73  ***********************************************/
74 
75 int drain_irqs(SubChannelId schid)
76 {
77     Irb irb = {};
78     int r = 0;
79 
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;
88             }
89             return r;
90         }
91     }
92 }
93 
94 static int run_ccw(VDev *vdev, int cmd, void *ptr, int len, bool sli)
95 {
96     Ccw1 ccw = {};
97 
98     ccw.cmd_code = cmd;
99     ccw.cda = (long)ptr;
100     ccw.count = len;
101 
102     if (sli) {
103         ccw.flags |= CCW_FLAG_SLI;
104     }
105 
106     return do_cio(vdev->schid, vdev->senseid.cu_type, ptr2u32(&ccw), CCW_FMT1);
107 }
108 
109 static void vring_init(VRing *vr, VqInfo *info)
110 {
111     void *p = (void *) info->queue;
112 
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));
120 
121     /* Zero out all relevant field */
122     vr->avail->flags = 0;
123     vr->avail->idx = 0;
124 
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;
131 
132     debug_print_addr("init vr", vr);
133 }
134 
135 bool vring_notify(VRing *vr)
136 {
137     vr->cookie = virtio_notify(vr->schid, vr->id, vr->cookie);
138     return vr->cookie >= 0;
139 }
140 
141 void vring_send_buf(VRing *vr, void *p, int len, int flags)
142 {
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;
146     }
147 
148     vr->desc[vr->next_idx].addr = (unsigned long)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++;
154 
155     /* Chains only have a single ID */
156     if (!(flags & VRING_DESC_F_NEXT)) {
157         vr->avail->idx++;
158     }
159 }
160 
161 int vr_poll(VRing *vr)
162 {
163     if (vr->used->idx == vr->used_idx) {
164         vring_notify(vr);
165         yield();
166         return 0;
167     }
168 
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 */
174 }
175 
176 /*
177  * Wait for the host to reply.
178  *
179  * timeout is in seconds if > 0.
180  *
181  * Returns 0 on success, 1 on timeout.
182  */
183 int vring_wait_reply(void)
184 {
185     unsigned long target_second = get_time_seconds() + vdev.wait_reply_timeout;
186 
187     /* Wait for any queue to be updated by the host */
188     do {
189         int i, r = 0;
190 
191         for (i = 0; i < vdev.nr_vqs; i++) {
192             r += vr_poll(&vdev.vrings[i]);
193         }
194         yield();
195         if (r) {
196             return 0;
197         }
198     } while (!vdev.wait_reply_timeout || (get_time_seconds() < target_second));
199 
200     return 1;
201 }
202 
203 int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd)
204 {
205     VRing *vr = &vdev->vrings[vqid];
206     int i = 0;
207 
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);
212 
213     vring_wait_reply();
214     if (drain_irqs(vr->schid)) {
215         return -1;
216     }
217     return 0;
218 }
219 
220 int virtio_setup_ccw(VDev *vdev)
221 {
222     int i, cfg_size = 0;
223     uint8_t status;
224     struct VirtioFeatureDesc {
225         uint32_t features;
226         uint8_t index;
227     } __attribute__((packed)) feats;
228 
229     if (!virtio_is_supported(vdev->schid)) {
230         puts("Virtio unsupported for this device ID");
231         return -ENODEV;
232     }
233     /* device ID has been established now */
234 
235     vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
236     vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
237 
238     run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
239 
240     status = VIRTIO_CONFIG_S_ACKNOWLEDGE;
241     if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
242         puts("Could not write ACKNOWLEDGE status to host");
243         return -EIO;
244     }
245 
246     switch (vdev->senseid.cu_model) {
247     case VIRTIO_ID_NET:
248         vdev->nr_vqs = 2;
249         vdev->cmd_vr_idx = 0;
250         cfg_size = sizeof(vdev->config.net);
251         break;
252     case VIRTIO_ID_BLOCK:
253         vdev->nr_vqs = 1;
254         vdev->cmd_vr_idx = 0;
255         cfg_size = sizeof(vdev->config.blk);
256         break;
257     case VIRTIO_ID_SCSI:
258         vdev->nr_vqs = 3;
259         vdev->cmd_vr_idx = VR_REQUEST;
260         cfg_size = sizeof(vdev->config.scsi);
261         break;
262     default:
263         puts("Unsupported virtio device");
264         return -ENODEV;
265     }
266 
267     status |= VIRTIO_CONFIG_S_DRIVER;
268     if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
269         puts("Could not write DRIVER status to host");
270         return -EIO;
271     }
272 
273     /* Feature negotiation */
274     for (i = 0; i < ARRAY_SIZE(vdev->guest_features); i++) {
275         feats.features = 0;
276         feats.index = i;
277         if (run_ccw(vdev, CCW_CMD_READ_FEAT, &feats, sizeof(feats), false)) {
278             puts("Could not get features bits");
279             return -EIO;
280         }
281 
282         vdev->guest_features[i] &= bswap32(feats.features);
283         feats.features = bswap32(vdev->guest_features[i]);
284         if (run_ccw(vdev, CCW_CMD_WRITE_FEAT, &feats, sizeof(feats), false)) {
285             puts("Could not set features bits");
286             return -EIO;
287         }
288     }
289 
290     if (run_ccw(vdev, CCW_CMD_READ_CONF, &vdev->config, cfg_size, false)) {
291         puts("Could not get virtio device configuration");
292         return -EIO;
293     }
294 
295     for (i = 0; i < vdev->nr_vqs; i++) {
296         VqInfo info = {
297             .queue = (unsigned long long) ring_area + (i * VIRTIO_RING_SIZE),
298             .align = KVM_S390_VIRTIO_RING_ALIGN,
299             .index = i,
300             .num = 0,
301         };
302         VqConfig config = {
303             .index = i,
304             .num = 0,
305         };
306 
307         if (run_ccw(vdev, CCW_CMD_READ_VQ_CONF, &config, sizeof(config),
308                 false)) {
309             puts("Could not get virtio device VQ config");
310             return -EIO;
311         }
312         info.num = config.num;
313         vring_init(&vdev->vrings[i], &info);
314         vdev->vrings[i].schid = vdev->schid;
315         if (run_ccw(vdev, CCW_CMD_SET_VQ, &info, sizeof(info), false)) {
316             puts("Cannot set VQ info");
317             return -EIO;
318         }
319     }
320 
321     status |= VIRTIO_CONFIG_S_DRIVER_OK;
322     if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
323         puts("Could not write DRIVER_OK status to host");
324         return -EIO;
325     }
326 
327     return 0;
328 }
329 
330 bool virtio_is_supported(SubChannelId schid)
331 {
332     vdev.schid = schid;
333     memset(&vdev.senseid, 0, sizeof(vdev.senseid));
334 
335     /*
336      * Run sense id command.
337      * The size of the senseid data differs between devices (notably,
338      * between virtio devices and dasds), so specify the largest possible
339      * size and suppress the incorrect length indication for smaller sizes.
340      */
341     if (run_ccw(&vdev, CCW_CMD_SENSE_ID, &vdev.senseid, sizeof(vdev.senseid),
342                 true)) {
343         return false;
344     }
345     if (vdev.senseid.cu_type == 0x3832) {
346         switch (vdev.senseid.cu_model) {
347         case VIRTIO_ID_BLOCK:
348         case VIRTIO_ID_SCSI:
349         case VIRTIO_ID_NET:
350             return true;
351         }
352     }
353     return false;
354 }
355