master
c 239 lines 6.35 KB
Raw
1 /*
2 * Virtio functionality for CCW devices
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 *
6 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
7 * Copyright 2025 IBM Corp.
8 *
9 * Author(s): Jared Rossi <jrossi@linux.ibm.com>
10 */
11
12 #include <string.h>
13 #include "s390-ccw.h"
14 #include "cio.h"
15 #include "virtio.h"
16 #include "virtio-ccw.h"
17 #include "virtio-scsi.h"
18 #include "bswap.h"
19 #include "helper.h"
20 #include "s390-time.h"
21
22 /* virtio spec v1.0 para 4.3.3.2 */
23 static long kvm_hypercall(unsigned long nr, unsigned long param1,
24 unsigned long param2, unsigned long param3)
25 {
26 register unsigned long r_nr asm("1") = nr;
27 register unsigned long r_param1 asm("2") = param1;
28 register unsigned long r_param2 asm("3") = param2;
29 register unsigned long r_param3 asm("4") = param3;
30 register long retval asm("2");
31
32 asm volatile ("diag %%r2,%%r4,0x500"
33 : "=d" (retval)
34 : "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)
35 : "memory", "cc");
36
37 return retval;
38 }
39
40 static int run_ccw(VDev *vdev, int cmd, void *ptr, int len, bool sli)
41 {
42 Ccw1 ccw = {};
43
44 ccw.cmd_code = cmd;
45 ccw.cda = (long)ptr;
46 ccw.count = len;
47
48 if (sli) {
49 ccw.flags |= CCW_FLAG_SLI;
50 }
51
52 return do_cio(vdev->schid, vdev->senseid.cu_type, ptr2u32(&ccw), CCW_FMT1);
53 }
54
55 bool virtio_ccw_is_supported(VDev *vdev)
56 {
57 memset(&vdev->senseid, 0, sizeof(vdev->senseid));
58
59 /*
60 * Run sense id command.
61 * The size of the senseid data differs between devices (notably,
62 * between virtio devices and dasds), so specify the largest possible
63 * size and suppress the incorrect length indication for smaller sizes.
64 */
65 if (run_ccw(vdev, CCW_CMD_SENSE_ID, &vdev->senseid, sizeof(vdev->senseid),
66 true)) {
67 return false;
68 }
69
70 vdev->dev_type = vdev->senseid.cu_model;
71
72 if (vdev->senseid.cu_type == 0x3832) {
73 switch (vdev->dev_type) {
74 case VIRTIO_ID_BLOCK:
75 case VIRTIO_ID_SCSI:
76 case VIRTIO_ID_NET:
77 return true;
78 default:
79 return false;
80 }
81 }
82 return false;
83 }
84
85 int drain_irqs_ccw(SubChannelId schid)
86 {
87 Irb irb = {};
88 int r = 0;
89
90 while (1) {
91 /* FIXME: make use of TPI, for that enable subchannel and isc */
92 if (tsch(schid, &irb)) {
93 /* Might want to differentiate error codes later on. */
94 if (irb.scsw.cstat) {
95 r = -EIO;
96 } else if (irb.scsw.dstat != 0xc) {
97 r = -EIO;
98 }
99 return r;
100 }
101 }
102 }
103
104 long virtio_ccw_notify(SubChannelId schid, int vq_idx, long cookie)
105 {
106 return kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, *(u32 *)&schid,
107 vq_idx, cookie);
108 }
109
110 int virtio_ccw_run(VDev *vdev, int vqid, VirtioCmd *cmd)
111 {
112 VRing *vr = &vdev->vrings[vqid];
113 int i = 0;
114
115 do {
116 vring_send_buf(vr, cmd[i].data, cmd[i].size,
117 cmd[i].flags | (i ? VRING_HIDDEN_IS_CHAIN : 0));
118 } while (cmd[i++].flags & VRING_DESC_F_NEXT);
119
120 vring_wait_reply();
121 if (drain_irqs()) {
122 return -1;
123 }
124 return 0;
125 }
126
127 int virtio_ccw_reset(VDev *vdev)
128 {
129 return run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
130 }
131
132 int virtio_ccw_setup(VDev *vdev)
133 {
134 int i, cfg_size = 0;
135 uint8_t status;
136 struct VirtioFeatureDesc {
137 uint32_t features;
138 uint8_t index;
139 } __attribute__((packed)) feats;
140
141 if (!virtio_ccw_is_supported(vdev)) {
142 puts("Virtio unsupported for this device ID");
143 return -ENODEV;
144 }
145 /* device ID has been established now */
146
147 vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
148 vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
149
150 virtio_reset(vdev);
151
152 status = VIRTIO_CONFIG_S_ACKNOWLEDGE;
153 if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
154 puts("Could not write ACKNOWLEDGE status to host");
155 return -EIO;
156 }
157
158 switch (vdev->dev_type) {
159 case VIRTIO_ID_NET:
160 vdev->nr_vqs = 2;
161 vdev->cmd_vr_idx = 0;
162 cfg_size = sizeof(vdev->config.net);
163 break;
164 case VIRTIO_ID_BLOCK:
165 vdev->nr_vqs = 1;
166 vdev->cmd_vr_idx = 0;
167 cfg_size = sizeof(vdev->config.blk);
168 break;
169 case VIRTIO_ID_SCSI:
170 vdev->nr_vqs = 3;
171 vdev->cmd_vr_idx = VR_REQUEST;
172 cfg_size = sizeof(vdev->config.scsi);
173 break;
174 default:
175 puts("Unsupported virtio device");
176 return -ENODEV;
177 }
178
179 status |= VIRTIO_CONFIG_S_DRIVER;
180 if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
181 puts("Could not write DRIVER status to host");
182 return -EIO;
183 }
184
185 /* Feature negotiation */
186 for (i = 0; i < ARRAY_SIZE(vdev->guest_features); i++) {
187 feats.features = 0;
188 feats.index = i;
189 if (run_ccw(vdev, CCW_CMD_READ_FEAT, &feats, sizeof(feats), false)) {
190 puts("Could not get features bits");
191 return -EIO;
192 }
193
194 vdev->guest_features[i] &= bswap32(feats.features);
195 feats.features = bswap32(vdev->guest_features[i]);
196 if (run_ccw(vdev, CCW_CMD_WRITE_FEAT, &feats, sizeof(feats), false)) {
197 puts("Could not set features bits");
198 return -EIO;
199 }
200 }
201
202 if (run_ccw(vdev, CCW_CMD_READ_CONF, &vdev->config, cfg_size, false)) {
203 puts("Could not get virtio device configuration");
204 return -EIO;
205 }
206
207 for (i = 0; i < vdev->nr_vqs; i++) {
208 VqInfo info = {
209 .queue = (unsigned long long) virtio_get_ring_area(i),
210 .align = KVM_S390_VIRTIO_RING_ALIGN,
211 .index = i,
212 .num = 0,
213 };
214 VqConfig config = {
215 .index = i,
216 .num = 0,
217 };
218
219 if (run_ccw(vdev, CCW_CMD_READ_VQ_CONF, &config, sizeof(config),
220 false)) {
221 puts("Could not get virtio device VQ config");
222 return -EIO;
223 }
224 info.num = config.num;
225 vring_init(&vdev->vrings[i], &info);
226 if (run_ccw(vdev, CCW_CMD_SET_VQ, &info, sizeof(info), false)) {
227 puts("Cannot set VQ info");
228 return -EIO;
229 }
230 }
231
232 status |= VIRTIO_CONFIG_S_DRIVER_OK;
233 if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
234 puts("Could not write DRIVER_OK status to host");
235 return -EIO;
236 }
237
238 return 0;
239 }