xref: /openbmc/qemu/pc-bios/s390-ccw/virtio.c (revision 2df1eb27)
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 "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"
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 void virtio_setup_ccw(VDev *vdev)
221 {
222     int i, rc, cfg_size = 0;
223     uint8_t status;
224     struct VirtioFeatureDesc {
225         uint32_t features;
226         uint8_t index;
227     } __attribute__((packed)) feats;
228 
229     IPL_assert(virtio_is_supported(vdev->schid), "PE");
230     /* device ID has been established now */
231 
232     vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
233     vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
234 
235     run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
236 
237     status = VIRTIO_CONFIG_S_ACKNOWLEDGE;
238     rc = run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false);
239     IPL_assert(rc == 0, "Could not write ACKNOWLEDGE status to host");
240 
241     switch (vdev->senseid.cu_model) {
242     case VIRTIO_ID_NET:
243         vdev->nr_vqs = 2;
244         vdev->cmd_vr_idx = 0;
245         cfg_size = sizeof(vdev->config.net);
246         break;
247     case VIRTIO_ID_BLOCK:
248         vdev->nr_vqs = 1;
249         vdev->cmd_vr_idx = 0;
250         cfg_size = sizeof(vdev->config.blk);
251         break;
252     case VIRTIO_ID_SCSI:
253         vdev->nr_vqs = 3;
254         vdev->cmd_vr_idx = VR_REQUEST;
255         cfg_size = sizeof(vdev->config.scsi);
256         break;
257     default:
258         panic("Unsupported virtio device\n");
259     }
260 
261     status |= VIRTIO_CONFIG_S_DRIVER;
262     rc = run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false);
263     IPL_assert(rc == 0, "Could not write DRIVER status to host");
264 
265     /* Feature negotiation */
266     for (i = 0; i < ARRAY_SIZE(vdev->guest_features); i++) {
267         feats.features = 0;
268         feats.index = i;
269         rc = run_ccw(vdev, CCW_CMD_READ_FEAT, &feats, sizeof(feats), false);
270         IPL_assert(rc == 0, "Could not get features bits");
271         vdev->guest_features[i] &= bswap32(feats.features);
272         feats.features = bswap32(vdev->guest_features[i]);
273         rc = run_ccw(vdev, CCW_CMD_WRITE_FEAT, &feats, sizeof(feats), false);
274         IPL_assert(rc == 0, "Could not set features bits");
275     }
276 
277     rc = run_ccw(vdev, CCW_CMD_READ_CONF, &vdev->config, cfg_size, false);
278     IPL_assert(rc == 0, "Could not get virtio device configuration");
279 
280     for (i = 0; i < vdev->nr_vqs; i++) {
281         VqInfo info = {
282             .queue = (unsigned long long) ring_area + (i * VIRTIO_RING_SIZE),
283             .align = KVM_S390_VIRTIO_RING_ALIGN,
284             .index = i,
285             .num = 0,
286         };
287         VqConfig config = {
288             .index = i,
289             .num = 0,
290         };
291 
292         rc = run_ccw(vdev, CCW_CMD_READ_VQ_CONF, &config, sizeof(config), false);
293         IPL_assert(rc == 0, "Could not get virtio device VQ configuration");
294         info.num = config.num;
295         vring_init(&vdev->vrings[i], &info);
296         vdev->vrings[i].schid = vdev->schid;
297         IPL_assert(
298             run_ccw(vdev, CCW_CMD_SET_VQ, &info, sizeof(info), false) == 0,
299             "Cannot set VQ info");
300     }
301 
302     status |= VIRTIO_CONFIG_S_DRIVER_OK;
303     rc = run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false);
304     IPL_assert(rc == 0, "Could not write DRIVER_OK status to host");
305 }
306 
307 bool virtio_is_supported(SubChannelId schid)
308 {
309     vdev.schid = schid;
310     memset(&vdev.senseid, 0, sizeof(vdev.senseid));
311 
312     /*
313      * Run sense id command.
314      * The size of the senseid data differs between devices (notably,
315      * between virtio devices and dasds), so specify the largest possible
316      * size and suppress the incorrect length indication for smaller sizes.
317      */
318     if (run_ccw(&vdev, CCW_CMD_SENSE_ID, &vdev.senseid, sizeof(vdev.senseid),
319                 true)) {
320         return false;
321     }
322     if (vdev.senseid.cu_type == 0x3832) {
323         switch (vdev.senseid.cu_model) {
324         case VIRTIO_ID_BLOCK:
325         case VIRTIO_ID_SCSI:
326         case VIRTIO_ID_NET:
327             return true;
328         }
329     }
330     return false;
331 }
332