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
15 #include "virtio-scsi.h"
19 #define VRING_WAIT_REPLY_TIMEOUT 30
21 static VRing block[VIRTIO_MAX_VQS];
22 static char ring_area[VIRTIO_RING_SIZE * VIRTIO_MAX_VQS]
23 __attribute__((__aligned__(PAGE_SIZE)));
29 .ring_area = ring_area,
30 .wait_reply_timeout = VRING_WAIT_REPLY_TIMEOUT,
31 .schid = { .one = 1 },
32 .scsi_block_size = VIRTIO_SCSI_BLOCK_SIZE,
36 VDev *virtio_get_device(void)
41 VirtioDevType virtio_get_device_type(void)
43 return vdev.senseid.cu_model;
46 /* virtio spec v1.0 para 4.3.3.2 */
47 static long kvm_hypercall(unsigned long nr, unsigned long param1,
48 unsigned long param2, unsigned long param3)
50 register ulong r_nr asm("1") = nr;
51 register ulong r_param1 asm("2") = param1;
52 register ulong r_param2 asm("3") = param2;
53 register ulong r_param3 asm("4") = param3;
54 register long retval asm("2");
56 asm volatile ("diag 2,4,0x500"
58 : "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)
64 static long virtio_notify(SubChannelId schid, int vq_idx, long cookie)
66 return kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, *(u32 *)&schid,
70 /***********************************************
72 ***********************************************/
74 int drain_irqs(SubChannelId schid)
80 /* FIXME: make use of TPI, for that enable subchannel and isc */
81 if (tsch(schid, &irb)) {
82 /* Might want to differentiate error codes later on. */
85 } else if (irb.scsw.dstat != 0xc) {
93 static int run_ccw(VDev *vdev, int cmd, void *ptr, int len, bool sli)
102 ccw.flags |= CCW_FLAG_SLI;
105 return do_cio(vdev->schid, vdev->senseid.cu_type, ptr2u32(&ccw), CCW_FMT1);
108 static void vring_init(VRing *vr, VqInfo *info)
110 void *p = (void *) info->queue;
112 debug_print_addr("init p", p);
113 vr->id = info->index;
116 vr->avail = p + info->num * sizeof(VRingDesc);
117 vr->used = (void *)(((unsigned long)&vr->avail->ring[info->num]
118 + info->align - 1) & ~(info->align - 1));
120 /* Zero out all relevant field */
121 vr->avail->flags = 0;
124 /* We're running with interrupts off anyways, so don't bother */
125 vr->used->flags = VRING_USED_F_NO_NOTIFY;
131 debug_print_addr("init vr", vr);
134 bool vring_notify(VRing *vr)
136 vr->cookie = virtio_notify(vr->schid, vr->id, vr->cookie);
137 return vr->cookie >= 0;
140 void vring_send_buf(VRing *vr, void *p, int len, int flags)
142 /* For follow-up chains we need to keep the first entry point */
143 if (!(flags & VRING_HIDDEN_IS_CHAIN)) {
144 vr->avail->ring[vr->avail->idx % vr->num] = vr->next_idx;
147 vr->desc[vr->next_idx].addr = (ulong)p;
148 vr->desc[vr->next_idx].len = len;
149 vr->desc[vr->next_idx].flags = flags & ~VRING_HIDDEN_IS_CHAIN;
150 vr->desc[vr->next_idx].next = vr->next_idx;
151 vr->desc[vr->next_idx].next++;
154 /* Chains only have a single ID */
155 if (!(flags & VRING_DESC_F_NEXT)) {
164 asm volatile("stck %0" : "=Q" (r) : : "cc");
168 ulong get_second(void)
170 return (get_clock() >> 12) / 1000000;
173 int vr_poll(VRing *vr)
175 if (vr->used->idx == vr->used_idx) {
181 vr->used_idx = vr->used->idx;
184 vr->desc[0].flags = 0;
185 return 1; /* vr has been updated */
189 * Wait for the host to reply.
191 * timeout is in seconds if > 0.
193 * Returns 0 on success, 1 on timeout.
195 int vring_wait_reply(void)
197 ulong target_second = get_second() + vdev.wait_reply_timeout;
199 /* Wait for any queue to be updated by the host */
203 for (i = 0; i < vdev.nr_vqs; i++) {
204 r += vr_poll(&vdev.vrings[i]);
210 } while (!vdev.wait_reply_timeout || (get_second() < target_second));
215 int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd)
217 VRing *vr = &vdev->vrings[vqid];
221 vring_send_buf(vr, cmd[i].data, cmd[i].size,
222 cmd[i].flags | (i ? VRING_HIDDEN_IS_CHAIN : 0));
223 } while (cmd[i++].flags & VRING_DESC_F_NEXT);
226 if (drain_irqs(vr->schid)) {
232 void virtio_setup_ccw(VDev *vdev)
234 int i, rc, cfg_size = 0;
235 unsigned char status = VIRTIO_CONFIG_S_DRIVER_OK;
236 struct VirtioFeatureDesc {
239 } __attribute__((packed)) feats;
241 IPL_assert(virtio_is_supported(vdev->schid), "PE");
242 /* device ID has been established now */
244 vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
245 vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
247 run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
249 switch (vdev->senseid.cu_model) {
252 vdev->cmd_vr_idx = 0;
253 cfg_size = sizeof(vdev->config.net);
255 case VIRTIO_ID_BLOCK:
257 vdev->cmd_vr_idx = 0;
258 cfg_size = sizeof(vdev->config.blk);
262 vdev->cmd_vr_idx = VR_REQUEST;
263 cfg_size = sizeof(vdev->config.scsi);
266 panic("Unsupported virtio device\n");
269 run_ccw(vdev, CCW_CMD_READ_CONF, &vdev->config, cfg_size, false) == 0,
270 "Could not get block device configuration");
272 /* Feature negotiation */
273 for (i = 0; i < ARRAY_SIZE(vdev->guest_features); i++) {
276 rc = run_ccw(vdev, CCW_CMD_READ_FEAT, &feats, sizeof(feats), false);
277 IPL_assert(rc == 0, "Could not get features bits");
278 vdev->guest_features[i] &= bswap32(feats.features);
279 feats.features = bswap32(vdev->guest_features[i]);
280 rc = run_ccw(vdev, CCW_CMD_WRITE_FEAT, &feats, sizeof(feats), false);
281 IPL_assert(rc == 0, "Could not set features bits");
284 for (i = 0; i < vdev->nr_vqs; i++) {
286 .queue = (unsigned long long) ring_area + (i * VIRTIO_RING_SIZE),
287 .align = KVM_S390_VIRTIO_RING_ALIGN,
297 run_ccw(vdev, CCW_CMD_READ_VQ_CONF, &config, sizeof(config), false) == 0,
298 "Could not get block device VQ configuration");
299 info.num = config.num;
300 vring_init(&vdev->vrings[i], &info);
301 vdev->vrings[i].schid = vdev->schid;
303 run_ccw(vdev, CCW_CMD_SET_VQ, &info, sizeof(info), false) == 0,
304 "Cannot set VQ info");
307 run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false) == 0,
308 "Could not write status to host");
311 bool virtio_is_supported(SubChannelId schid)
314 memset(&vdev.senseid, 0, sizeof(vdev.senseid));
317 * Run sense id command.
318 * The size of the senseid data differs between devices (notably,
319 * between virtio devices and dasds), so specify the largest possible
320 * size and suppress the incorrect length indication for smaller sizes.
322 if (run_ccw(&vdev, CCW_CMD_SENSE_ID, &vdev.senseid, sizeof(vdev.senseid),
326 if (vdev.senseid.cu_type == 0x3832) {
327 switch (vdev.senseid.cu_model) {
328 case VIRTIO_ID_BLOCK: