]>
Commit | Line | Data |
---|---|---|
1dcac3e1 XFR |
1 | /* |
2 | * vfio based subchannel assignment support | |
3 | * | |
4 | * Copyright 2017 IBM Corp. | |
8fadea24 CH |
5 | * Copyright 2019 Red Hat, Inc. |
6 | * | |
1dcac3e1 XFR |
7 | * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com> |
8 | * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com> | |
9 | * Pierre Morel <pmorel@linux.vnet.ibm.com> | |
8fadea24 | 10 | * Cornelia Huck <cohuck@redhat.com> |
1dcac3e1 | 11 | * |
08b824aa CH |
12 | * This work is licensed under the terms of the GNU GPL, version 2 or (at |
13 | * your option) any later version. See the COPYING file in the top-level | |
1dcac3e1 XFR |
14 | * directory. |
15 | */ | |
16 | ||
e9808d09 | 17 | #include "qemu/osdep.h" |
1dcac3e1 | 18 | #include <linux/vfio.h> |
c14e706c | 19 | #include <linux/vfio_ccw.h> |
1dcac3e1 XFR |
20 | #include <sys/ioctl.h> |
21 | ||
1dcac3e1 XFR |
22 | #include "qapi/error.h" |
23 | #include "hw/sysbus.h" | |
24 | #include "hw/vfio/vfio.h" | |
25 | #include "hw/vfio/vfio-common.h" | |
26 | #include "hw/s390x/s390-ccw.h" | |
44445d86 | 27 | #include "hw/s390x/vfio-ccw.h" |
a27bd6c7 | 28 | #include "hw/qdev-properties.h" |
1dcac3e1 | 29 | #include "hw/s390x/ccw-device.h" |
d791937f | 30 | #include "exec/address-spaces.h" |
4886b3e9 | 31 | #include "qemu/error-report.h" |
db725815 | 32 | #include "qemu/main-loop.h" |
0b8fa32f | 33 | #include "qemu/module.h" |
1dcac3e1 | 34 | |
44445d86 | 35 | struct VFIOCCWDevice { |
1dcac3e1 XFR |
36 | S390CCWDevice cdev; |
37 | VFIODevice vdev; | |
c14e706c DJS |
38 | uint64_t io_region_size; |
39 | uint64_t io_region_offset; | |
40 | struct ccw_io_region *io_region; | |
8fadea24 CH |
41 | uint64_t async_cmd_region_size; |
42 | uint64_t async_cmd_region_offset; | |
43 | struct ccw_cmd_region *async_cmd_region; | |
46ea3841 FA |
44 | uint64_t schib_region_size; |
45 | uint64_t schib_region_offset; | |
46 | struct ccw_schib_region *schib_region; | |
f030532f FA |
47 | uint64_t crw_region_size; |
48 | uint64_t crw_region_offset; | |
49 | struct ccw_crw_region *crw_region; | |
4886b3e9 | 50 | EventNotifier io_notifier; |
f030532f | 51 | EventNotifier crw_notifier; |
b2f96f9e | 52 | EventNotifier req_notifier; |
9a51c9ee HP |
53 | bool force_orb_pfch; |
54 | bool warned_orb_pfch; | |
44445d86 | 55 | }; |
1dcac3e1 | 56 | |
9a51c9ee HP |
57 | static inline void warn_once_pfch(VFIOCCWDevice *vcdev, SubchDev *sch, |
58 | const char *msg) | |
59 | { | |
c55510b7 CH |
60 | warn_report_once_cond(&vcdev->warned_orb_pfch, |
61 | "vfio-ccw (devno %x.%x.%04x): %s", | |
62 | sch->cssid, sch->ssid, sch->devno, msg); | |
9a51c9ee HP |
63 | } |
64 | ||
1dcac3e1 XFR |
65 | static void vfio_ccw_compute_needs_reset(VFIODevice *vdev) |
66 | { | |
67 | vdev->needs_reset = false; | |
68 | } | |
69 | ||
70 | /* | |
71 | * We don't need vfio_hot_reset_multi and vfio_eoi operations for | |
72 | * vfio_ccw device now. | |
73 | */ | |
74 | struct VFIODeviceOps vfio_ccw_ops = { | |
75 | .vfio_compute_needs_reset = vfio_ccw_compute_needs_reset, | |
76 | }; | |
77 | ||
66dc50f7 | 78 | static IOInstEnding vfio_ccw_handle_request(SubchDev *sch) |
8ca2b376 | 79 | { |
66dc50f7 | 80 | S390CCWDevice *cdev = sch->driver_data; |
8ca2b376 XFR |
81 | VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); |
82 | struct ccw_io_region *region = vcdev->io_region; | |
83 | int ret; | |
84 | ||
24e58a7b JR |
85 | if (!(sch->orb.ctrl0 & ORB_CTRL0_MASK_PFCH) && vcdev->force_orb_pfch) { |
86 | sch->orb.ctrl0 |= ORB_CTRL0_MASK_PFCH; | |
87 | warn_once_pfch(vcdev, sch, "PFCH flag forced"); | |
9a51c9ee HP |
88 | } |
89 | ||
8ca2b376 XFR |
90 | QEMU_BUILD_BUG_ON(sizeof(region->orb_area) != sizeof(ORB)); |
91 | QEMU_BUILD_BUG_ON(sizeof(region->scsw_area) != sizeof(SCSW)); | |
92 | QEMU_BUILD_BUG_ON(sizeof(region->irb_area) != sizeof(IRB)); | |
93 | ||
94 | memset(region, 0, sizeof(*region)); | |
95 | ||
66dc50f7 HP |
96 | memcpy(region->orb_area, &sch->orb, sizeof(ORB)); |
97 | memcpy(region->scsw_area, &sch->curr_status.scsw, sizeof(SCSW)); | |
8ca2b376 XFR |
98 | |
99 | again: | |
100 | ret = pwrite(vcdev->vdev.fd, region, | |
101 | vcdev->io_region_size, vcdev->io_region_offset); | |
102 | if (ret != vcdev->io_region_size) { | |
103 | if (errno == EAGAIN) { | |
104 | goto again; | |
105 | } | |
91f751dc | 106 | error_report("vfio-ccw: write I/O region failed with errno=%d", errno); |
d6cd6631 | 107 | ret = errno ? -errno : -EFAULT; |
66dc50f7 | 108 | } else { |
d6cd6631 | 109 | ret = 0; |
66dc50f7 HP |
110 | } |
111 | switch (ret) { | |
112 | case 0: | |
113 | return IOINST_CC_EXPECTED; | |
114 | case -EBUSY: | |
115 | return IOINST_CC_BUSY; | |
116 | case -ENODEV: | |
117 | case -EACCES: | |
118 | return IOINST_CC_NOT_OPERATIONAL; | |
119 | case -EFAULT: | |
120 | default: | |
121 | sch_gen_unit_exception(sch); | |
122 | css_inject_io_interrupt(sch); | |
123 | return IOINST_CC_EXPECTED; | |
8ca2b376 | 124 | } |
8ca2b376 XFR |
125 | } |
126 | ||
46ea3841 FA |
127 | static IOInstEnding vfio_ccw_handle_store(SubchDev *sch) |
128 | { | |
129 | S390CCWDevice *cdev = sch->driver_data; | |
130 | VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); | |
131 | SCHIB *schib = &sch->curr_status; | |
132 | struct ccw_schib_region *region = vcdev->schib_region; | |
133 | SCHIB *s; | |
134 | int ret; | |
135 | ||
136 | /* schib region not available so nothing else to do */ | |
137 | if (!region) { | |
138 | return IOINST_CC_EXPECTED; | |
139 | } | |
140 | ||
141 | memset(region, 0, sizeof(*region)); | |
142 | ret = pread(vcdev->vdev.fd, region, vcdev->schib_region_size, | |
143 | vcdev->schib_region_offset); | |
144 | ||
145 | if (ret == -1) { | |
146 | /* | |
147 | * Device is probably damaged, but store subchannel does not | |
148 | * have a nonzero cc defined for this scenario. Log an error, | |
149 | * and presume things are otherwise fine. | |
150 | */ | |
151 | error_report("vfio-ccw: store region read failed with errno=%d", errno); | |
152 | return IOINST_CC_EXPECTED; | |
153 | } | |
154 | ||
155 | /* | |
156 | * Selectively copy path-related bits of the SCHIB, | |
157 | * rather than copying the entire struct. | |
158 | */ | |
159 | s = (SCHIB *)region->schib_area; | |
160 | schib->pmcw.pnom = s->pmcw.pnom; | |
161 | schib->pmcw.lpum = s->pmcw.lpum; | |
162 | schib->pmcw.pam = s->pmcw.pam; | |
163 | schib->pmcw.pom = s->pmcw.pom; | |
164 | ||
165 | if (s->scsw.flags & SCSW_FLAGS_MASK_PNO) { | |
166 | schib->scsw.flags |= SCSW_FLAGS_MASK_PNO; | |
167 | } | |
168 | ||
169 | return IOINST_CC_EXPECTED; | |
170 | } | |
171 | ||
8fadea24 CH |
172 | static int vfio_ccw_handle_clear(SubchDev *sch) |
173 | { | |
174 | S390CCWDevice *cdev = sch->driver_data; | |
175 | VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); | |
176 | struct ccw_cmd_region *region = vcdev->async_cmd_region; | |
177 | int ret; | |
178 | ||
179 | if (!vcdev->async_cmd_region) { | |
180 | /* Async command region not available, fall back to emulation */ | |
181 | return -ENOSYS; | |
182 | } | |
183 | ||
184 | memset(region, 0, sizeof(*region)); | |
185 | region->command = VFIO_CCW_ASYNC_CMD_CSCH; | |
186 | ||
187 | again: | |
188 | ret = pwrite(vcdev->vdev.fd, region, | |
189 | vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset); | |
190 | if (ret != vcdev->async_cmd_region_size) { | |
191 | if (errno == EAGAIN) { | |
192 | goto again; | |
193 | } | |
194 | error_report("vfio-ccw: write cmd region failed with errno=%d", errno); | |
d6cd6631 | 195 | ret = errno ? -errno : -EFAULT; |
8fadea24 | 196 | } else { |
d6cd6631 | 197 | ret = 0; |
8fadea24 CH |
198 | } |
199 | switch (ret) { | |
200 | case 0: | |
201 | case -ENODEV: | |
202 | case -EACCES: | |
203 | return 0; | |
204 | case -EFAULT: | |
205 | default: | |
206 | sch_gen_unit_exception(sch); | |
207 | css_inject_io_interrupt(sch); | |
208 | return 0; | |
209 | } | |
210 | } | |
211 | ||
212 | static int vfio_ccw_handle_halt(SubchDev *sch) | |
213 | { | |
214 | S390CCWDevice *cdev = sch->driver_data; | |
215 | VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); | |
216 | struct ccw_cmd_region *region = vcdev->async_cmd_region; | |
217 | int ret; | |
218 | ||
219 | if (!vcdev->async_cmd_region) { | |
220 | /* Async command region not available, fall back to emulation */ | |
221 | return -ENOSYS; | |
222 | } | |
223 | ||
224 | memset(region, 0, sizeof(*region)); | |
225 | region->command = VFIO_CCW_ASYNC_CMD_HSCH; | |
226 | ||
227 | again: | |
228 | ret = pwrite(vcdev->vdev.fd, region, | |
229 | vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset); | |
230 | if (ret != vcdev->async_cmd_region_size) { | |
231 | if (errno == EAGAIN) { | |
232 | goto again; | |
233 | } | |
234 | error_report("vfio-ccw: write cmd region failed with errno=%d", errno); | |
d6cd6631 | 235 | ret = errno ? -errno : -EFAULT; |
8fadea24 | 236 | } else { |
d6cd6631 | 237 | ret = 0; |
8fadea24 CH |
238 | } |
239 | switch (ret) { | |
240 | case 0: | |
241 | case -EBUSY: | |
242 | case -ENODEV: | |
243 | case -EACCES: | |
244 | return 0; | |
245 | case -EFAULT: | |
246 | default: | |
247 | sch_gen_unit_exception(sch); | |
248 | css_inject_io_interrupt(sch); | |
249 | return 0; | |
250 | } | |
251 | } | |
252 | ||
1dcac3e1 XFR |
253 | static void vfio_ccw_reset(DeviceState *dev) |
254 | { | |
255 | CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev); | |
256 | S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev); | |
257 | VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); | |
258 | ||
259 | ioctl(vcdev->vdev.fd, VFIO_DEVICE_RESET); | |
260 | } | |
261 | ||
f030532f FA |
262 | static void vfio_ccw_crw_read(VFIOCCWDevice *vcdev) |
263 | { | |
264 | struct ccw_crw_region *region = vcdev->crw_region; | |
265 | CRW crw; | |
266 | int size; | |
267 | ||
268 | /* Keep reading CRWs as long as data is returned */ | |
269 | do { | |
270 | memset(region, 0, sizeof(*region)); | |
271 | size = pread(vcdev->vdev.fd, region, vcdev->crw_region_size, | |
272 | vcdev->crw_region_offset); | |
273 | ||
274 | if (size == -1) { | |
275 | error_report("vfio-ccw: Read crw region failed with errno=%d", | |
276 | errno); | |
277 | break; | |
278 | } | |
279 | ||
280 | if (region->crw == 0) { | |
281 | /* No more CRWs to queue */ | |
282 | break; | |
283 | } | |
284 | ||
285 | memcpy(&crw, ®ion->crw, sizeof(CRW)); | |
286 | ||
287 | css_crw_add_to_queue(crw); | |
288 | } while (1); | |
289 | } | |
290 | ||
b2f96f9e EF |
291 | static void vfio_ccw_req_notifier_handler(void *opaque) |
292 | { | |
293 | VFIOCCWDevice *vcdev = opaque; | |
294 | Error *err = NULL; | |
295 | ||
296 | if (!event_notifier_test_and_clear(&vcdev->req_notifier)) { | |
297 | return; | |
298 | } | |
299 | ||
300 | qdev_unplug(DEVICE(vcdev), &err); | |
301 | if (err) { | |
302 | warn_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name); | |
303 | } | |
304 | } | |
305 | ||
f030532f FA |
306 | static void vfio_ccw_crw_notifier_handler(void *opaque) |
307 | { | |
308 | VFIOCCWDevice *vcdev = opaque; | |
309 | ||
310 | while (event_notifier_test_and_clear(&vcdev->crw_notifier)) { | |
311 | vfio_ccw_crw_read(vcdev); | |
312 | } | |
313 | } | |
314 | ||
4886b3e9 DJS |
315 | static void vfio_ccw_io_notifier_handler(void *opaque) |
316 | { | |
317 | VFIOCCWDevice *vcdev = opaque; | |
8ca2b376 XFR |
318 | struct ccw_io_region *region = vcdev->io_region; |
319 | S390CCWDevice *cdev = S390_CCW_DEVICE(vcdev); | |
320 | CcwDevice *ccw_dev = CCW_DEVICE(cdev); | |
321 | SubchDev *sch = ccw_dev->sch; | |
e1d0b372 DB |
322 | SCHIB *schib = &sch->curr_status; |
323 | SCSW s; | |
8ca2b376 XFR |
324 | IRB irb; |
325 | int size; | |
4886b3e9 DJS |
326 | |
327 | if (!event_notifier_test_and_clear(&vcdev->io_notifier)) { | |
328 | return; | |
329 | } | |
8ca2b376 XFR |
330 | |
331 | size = pread(vcdev->vdev.fd, region, vcdev->io_region_size, | |
332 | vcdev->io_region_offset); | |
333 | if (size == -1) { | |
334 | switch (errno) { | |
335 | case ENODEV: | |
336 | /* Generate a deferred cc 3 condition. */ | |
e1d0b372 DB |
337 | schib->scsw.flags |= SCSW_FLAGS_MASK_CC; |
338 | schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; | |
339 | schib->scsw.ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND); | |
8ca2b376 XFR |
340 | goto read_err; |
341 | case EFAULT: | |
342 | /* Memory problem, generate channel data check. */ | |
e1d0b372 DB |
343 | schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND; |
344 | schib->scsw.cstat = SCSW_CSTAT_DATA_CHECK; | |
345 | schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; | |
346 | schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | | |
8ca2b376 XFR |
347 | SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; |
348 | goto read_err; | |
349 | default: | |
350 | /* Error, generate channel program check. */ | |
e1d0b372 DB |
351 | schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND; |
352 | schib->scsw.cstat = SCSW_CSTAT_PROG_CHECK; | |
353 | schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; | |
354 | schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | | |
8ca2b376 XFR |
355 | SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; |
356 | goto read_err; | |
357 | } | |
358 | } else if (size != vcdev->io_region_size) { | |
359 | /* Information transfer error, generate channel-control check. */ | |
e1d0b372 DB |
360 | schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND; |
361 | schib->scsw.cstat = SCSW_CSTAT_CHN_CTRL_CHK; | |
362 | schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; | |
363 | schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | | |
8ca2b376 XFR |
364 | SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; |
365 | goto read_err; | |
366 | } | |
367 | ||
368 | memcpy(&irb, region->irb_area, sizeof(IRB)); | |
369 | ||
370 | /* Update control block via irb. */ | |
e1d0b372 DB |
371 | s = schib->scsw; |
372 | copy_scsw_to_guest(&s, &irb.scsw); | |
373 | schib->scsw = s; | |
8ca2b376 | 374 | |
334e7685 | 375 | /* If a uint check is pending, copy sense data. */ |
e1d0b372 DB |
376 | if ((schib->scsw.dstat & SCSW_DSTAT_UNIT_CHECK) && |
377 | (schib->pmcw.chars & PMCW_CHARS_MASK_CSENSE)) { | |
334e7685 DJS |
378 | memcpy(sch->sense_data, irb.ecw, sizeof(irb.ecw)); |
379 | } | |
380 | ||
8ca2b376 XFR |
381 | read_err: |
382 | css_inject_io_interrupt(sch); | |
4886b3e9 DJS |
383 | } |
384 | ||
690e29b9 EF |
385 | static void vfio_ccw_register_irq_notifier(VFIOCCWDevice *vcdev, |
386 | unsigned int irq, | |
387 | Error **errp) | |
4886b3e9 DJS |
388 | { |
389 | VFIODevice *vdev = &vcdev->vdev; | |
390 | struct vfio_irq_info *irq_info; | |
4886b3e9 | 391 | size_t argsz; |
b5e89f04 | 392 | int fd; |
690e29b9 EF |
393 | EventNotifier *notifier; |
394 | IOHandler *fd_read; | |
395 | ||
396 | switch (irq) { | |
397 | case VFIO_CCW_IO_IRQ_INDEX: | |
398 | notifier = &vcdev->io_notifier; | |
399 | fd_read = vfio_ccw_io_notifier_handler; | |
400 | break; | |
f030532f FA |
401 | case VFIO_CCW_CRW_IRQ_INDEX: |
402 | notifier = &vcdev->crw_notifier; | |
403 | fd_read = vfio_ccw_crw_notifier_handler; | |
404 | break; | |
b2f96f9e EF |
405 | case VFIO_CCW_REQ_IRQ_INDEX: |
406 | notifier = &vcdev->req_notifier; | |
407 | fd_read = vfio_ccw_req_notifier_handler; | |
408 | break; | |
690e29b9 EF |
409 | default: |
410 | error_setg(errp, "vfio: Unsupported device irq(%d)", irq); | |
411 | return; | |
412 | } | |
4886b3e9 | 413 | |
690e29b9 EF |
414 | if (vdev->num_irqs < irq + 1) { |
415 | error_setg(errp, "vfio: unexpected number of irqs %u", | |
4886b3e9 DJS |
416 | vdev->num_irqs); |
417 | return; | |
418 | } | |
419 | ||
28e22d4b | 420 | argsz = sizeof(*irq_info); |
4886b3e9 | 421 | irq_info = g_malloc0(argsz); |
690e29b9 | 422 | irq_info->index = irq; |
4886b3e9 DJS |
423 | irq_info->argsz = argsz; |
424 | if (ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO, | |
425 | irq_info) < 0 || irq_info->count < 1) { | |
426 | error_setg_errno(errp, errno, "vfio: Error getting irq info"); | |
427 | goto out_free_info; | |
428 | } | |
429 | ||
690e29b9 | 430 | if (event_notifier_init(notifier, 0)) { |
4886b3e9 | 431 | error_setg_errno(errp, errno, |
690e29b9 EF |
432 | "vfio: Unable to init event notifier for irq (%d)", |
433 | irq); | |
4886b3e9 DJS |
434 | goto out_free_info; |
435 | } | |
436 | ||
690e29b9 EF |
437 | fd = event_notifier_get_fd(notifier); |
438 | qemu_set_fd_handler(fd, fd_read, NULL, vcdev); | |
b5e89f04 | 439 | |
690e29b9 | 440 | if (vfio_set_irq_signaling(vdev, irq, 0, |
b5e89f04 CH |
441 | VFIO_IRQ_SET_ACTION_TRIGGER, fd, errp)) { |
442 | qemu_set_fd_handler(fd, NULL, NULL, vcdev); | |
690e29b9 | 443 | event_notifier_cleanup(notifier); |
4886b3e9 DJS |
444 | } |
445 | ||
4886b3e9 DJS |
446 | out_free_info: |
447 | g_free(irq_info); | |
448 | } | |
449 | ||
690e29b9 EF |
450 | static void vfio_ccw_unregister_irq_notifier(VFIOCCWDevice *vcdev, |
451 | unsigned int irq) | |
4886b3e9 | 452 | { |
b5e89f04 | 453 | Error *err = NULL; |
690e29b9 EF |
454 | EventNotifier *notifier; |
455 | ||
456 | switch (irq) { | |
457 | case VFIO_CCW_IO_IRQ_INDEX: | |
458 | notifier = &vcdev->io_notifier; | |
459 | break; | |
f030532f FA |
460 | case VFIO_CCW_CRW_IRQ_INDEX: |
461 | notifier = &vcdev->crw_notifier; | |
462 | break; | |
b2f96f9e EF |
463 | case VFIO_CCW_REQ_IRQ_INDEX: |
464 | notifier = &vcdev->req_notifier; | |
465 | break; | |
690e29b9 EF |
466 | default: |
467 | error_report("vfio: Unsupported device irq(%d)", irq); | |
468 | return; | |
469 | } | |
b5e89f04 | 470 | |
690e29b9 | 471 | if (vfio_set_irq_signaling(&vcdev->vdev, irq, 0, |
f5cf94cd | 472 | VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) { |
b5e89f04 | 473 | error_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name); |
4886b3e9 DJS |
474 | } |
475 | ||
690e29b9 | 476 | qemu_set_fd_handler(event_notifier_get_fd(notifier), |
4886b3e9 | 477 | NULL, NULL, vcdev); |
690e29b9 | 478 | event_notifier_cleanup(notifier); |
4886b3e9 DJS |
479 | } |
480 | ||
c14e706c DJS |
481 | static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp) |
482 | { | |
483 | VFIODevice *vdev = &vcdev->vdev; | |
484 | struct vfio_region_info *info; | |
485 | int ret; | |
486 | ||
487 | /* Sanity check device */ | |
488 | if (!(vdev->flags & VFIO_DEVICE_FLAGS_CCW)) { | |
489 | error_setg(errp, "vfio: Um, this isn't a vfio-ccw device"); | |
490 | return; | |
491 | } | |
492 | ||
8fadea24 CH |
493 | /* |
494 | * We always expect at least the I/O region to be present. We also | |
495 | * may have a variable number of regions governed by capabilities. | |
496 | */ | |
c14e706c | 497 | if (vdev->num_regions < VFIO_CCW_CONFIG_REGION_INDEX + 1) { |
8fadea24 CH |
498 | error_setg(errp, "vfio: too few regions (%u), expected at least %u", |
499 | vdev->num_regions, VFIO_CCW_CONFIG_REGION_INDEX + 1); | |
c14e706c DJS |
500 | return; |
501 | } | |
502 | ||
503 | ret = vfio_get_region_info(vdev, VFIO_CCW_CONFIG_REGION_INDEX, &info); | |
504 | if (ret) { | |
505 | error_setg_errno(errp, -ret, "vfio: Error getting config info"); | |
506 | return; | |
507 | } | |
508 | ||
509 | vcdev->io_region_size = info->size; | |
510 | if (sizeof(*vcdev->io_region) != vcdev->io_region_size) { | |
511 | error_setg(errp, "vfio: Unexpected size of the I/O region"); | |
2a3b9cba | 512 | goto out_err; |
c14e706c DJS |
513 | } |
514 | ||
515 | vcdev->io_region_offset = info->offset; | |
516 | vcdev->io_region = g_malloc0(info->size); | |
c8726f7b | 517 | g_free(info); |
c14e706c | 518 | |
8fadea24 CH |
519 | /* check for the optional async command region */ |
520 | ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW, | |
521 | VFIO_REGION_SUBTYPE_CCW_ASYNC_CMD, &info); | |
522 | if (!ret) { | |
523 | vcdev->async_cmd_region_size = info->size; | |
524 | if (sizeof(*vcdev->async_cmd_region) != vcdev->async_cmd_region_size) { | |
525 | error_setg(errp, "vfio: Unexpected size of the async cmd region"); | |
2a3b9cba | 526 | goto out_err; |
8fadea24 CH |
527 | } |
528 | vcdev->async_cmd_region_offset = info->offset; | |
529 | vcdev->async_cmd_region = g_malloc0(info->size); | |
c8726f7b | 530 | g_free(info); |
8fadea24 CH |
531 | } |
532 | ||
46ea3841 FA |
533 | ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW, |
534 | VFIO_REGION_SUBTYPE_CCW_SCHIB, &info); | |
535 | if (!ret) { | |
536 | vcdev->schib_region_size = info->size; | |
537 | if (sizeof(*vcdev->schib_region) != vcdev->schib_region_size) { | |
538 | error_setg(errp, "vfio: Unexpected size of the schib region"); | |
539 | goto out_err; | |
540 | } | |
541 | vcdev->schib_region_offset = info->offset; | |
542 | vcdev->schib_region = g_malloc(info->size); | |
c8726f7b | 543 | g_free(info); |
46ea3841 FA |
544 | } |
545 | ||
f030532f FA |
546 | ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW, |
547 | VFIO_REGION_SUBTYPE_CCW_CRW, &info); | |
548 | ||
549 | if (!ret) { | |
550 | vcdev->crw_region_size = info->size; | |
551 | if (sizeof(*vcdev->crw_region) != vcdev->crw_region_size) { | |
552 | error_setg(errp, "vfio: Unexpected size of the CRW region"); | |
553 | goto out_err; | |
554 | } | |
555 | vcdev->crw_region_offset = info->offset; | |
556 | vcdev->crw_region = g_malloc(info->size); | |
c8726f7b | 557 | g_free(info); |
f030532f FA |
558 | } |
559 | ||
2a3b9cba EF |
560 | return; |
561 | ||
562 | out_err: | |
f030532f | 563 | g_free(vcdev->crw_region); |
46ea3841 | 564 | g_free(vcdev->schib_region); |
2a3b9cba EF |
565 | g_free(vcdev->async_cmd_region); |
566 | g_free(vcdev->io_region); | |
567 | g_free(info); | |
568 | return; | |
c14e706c DJS |
569 | } |
570 | ||
571 | static void vfio_ccw_put_region(VFIOCCWDevice *vcdev) | |
572 | { | |
f030532f | 573 | g_free(vcdev->crw_region); |
46ea3841 | 574 | g_free(vcdev->schib_region); |
8fadea24 | 575 | g_free(vcdev->async_cmd_region); |
c14e706c DJS |
576 | g_free(vcdev->io_region); |
577 | } | |
578 | ||
c96f2c2a | 579 | static void vfio_ccw_put_device(VFIOCCWDevice *vcdev) |
1dcac3e1 XFR |
580 | { |
581 | g_free(vcdev->vdev.name); | |
582 | vfio_put_base_device(&vcdev->vdev); | |
583 | } | |
584 | ||
c96f2c2a GK |
585 | static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev, |
586 | Error **errp) | |
587 | { | |
588 | char *name = g_strdup_printf("%x.%x.%04x", vcdev->cdev.hostid.cssid, | |
589 | vcdev->cdev.hostid.ssid, | |
590 | vcdev->cdev.hostid.devid); | |
591 | VFIODevice *vbasedev; | |
592 | ||
593 | QLIST_FOREACH(vbasedev, &group->device_list, next) { | |
594 | if (strcmp(vbasedev->name, name) == 0) { | |
595 | error_setg(errp, "vfio: subchannel %s has already been attached", | |
596 | name); | |
597 | goto out_err; | |
598 | } | |
599 | } | |
238e9172 AW |
600 | |
601 | /* | |
602 | * All vfio-ccw devices are believed to operate in a way compatible with | |
aff92b82 DH |
603 | * discarding of memory in RAM blocks, ie. pages pinned in the host are |
604 | * in the current working set of the guest driver and therefore never | |
605 | * overlap e.g., with pages available to the guest balloon driver. This | |
606 | * needs to be set before vfio_get_device() for vfio common to handle | |
607 | * ram_block_discard_disable(). | |
238e9172 | 608 | */ |
aff92b82 | 609 | vcdev->vdev.ram_block_discard_allowed = true; |
c96f2c2a GK |
610 | |
611 | if (vfio_get_device(group, vcdev->cdev.mdevid, &vcdev->vdev, errp)) { | |
612 | goto out_err; | |
613 | } | |
614 | ||
615 | vcdev->vdev.ops = &vfio_ccw_ops; | |
616 | vcdev->vdev.type = VFIO_DEVICE_TYPE_CCW; | |
617 | vcdev->vdev.name = name; | |
618 | vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj; | |
619 | ||
620 | return; | |
621 | ||
622 | out_err: | |
623 | g_free(name); | |
624 | } | |
625 | ||
1dcac3e1 XFR |
626 | static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp) |
627 | { | |
628 | char *tmp, group_path[PATH_MAX]; | |
629 | ssize_t len; | |
630 | int groupid; | |
631 | ||
632 | tmp = g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group", | |
633 | cdev->hostid.cssid, cdev->hostid.ssid, | |
634 | cdev->hostid.devid, cdev->mdevid); | |
635 | len = readlink(tmp, group_path, sizeof(group_path)); | |
636 | g_free(tmp); | |
637 | ||
638 | if (len <= 0 || len >= sizeof(group_path)) { | |
639 | error_setg(errp, "vfio: no iommu_group found"); | |
640 | return NULL; | |
641 | } | |
642 | ||
643 | group_path[len] = 0; | |
644 | ||
645 | if (sscanf(basename(group_path), "%d", &groupid) != 1) { | |
646 | error_setg(errp, "vfio: failed to read %s", group_path); | |
647 | return NULL; | |
648 | } | |
649 | ||
650 | return vfio_get_group(groupid, &address_space_memory, errp); | |
651 | } | |
652 | ||
653 | static void vfio_ccw_realize(DeviceState *dev, Error **errp) | |
654 | { | |
1dcac3e1 XFR |
655 | VFIOGroup *group; |
656 | CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev); | |
657 | S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev); | |
658 | VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); | |
659 | S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev); | |
660 | Error *err = NULL; | |
661 | ||
662 | /* Call the class init function for subchannel. */ | |
663 | if (cdc->realize) { | |
664 | cdc->realize(cdev, vcdev->vdev.sysfsdev, &err); | |
665 | if (err) { | |
666 | goto out_err_propagate; | |
667 | } | |
668 | } | |
669 | ||
670 | group = vfio_ccw_get_group(cdev, &err); | |
671 | if (!group) { | |
672 | goto out_group_err; | |
673 | } | |
674 | ||
c96f2c2a GK |
675 | vfio_ccw_get_device(group, vcdev, &err); |
676 | if (err) { | |
1dcac3e1 XFR |
677 | goto out_device_err; |
678 | } | |
679 | ||
c14e706c DJS |
680 | vfio_ccw_get_region(vcdev, &err); |
681 | if (err) { | |
682 | goto out_region_err; | |
683 | } | |
684 | ||
690e29b9 | 685 | vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX, &err); |
4886b3e9 | 686 | if (err) { |
b2f96f9e | 687 | goto out_io_notifier_err; |
4886b3e9 DJS |
688 | } |
689 | ||
f030532f FA |
690 | if (vcdev->crw_region) { |
691 | vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX, &err); | |
692 | if (err) { | |
b2f96f9e | 693 | goto out_crw_notifier_err; |
f030532f FA |
694 | } |
695 | } | |
696 | ||
b2f96f9e EF |
697 | vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX, &err); |
698 | if (err) { | |
699 | goto out_req_notifier_err; | |
700 | } | |
701 | ||
1dcac3e1 XFR |
702 | return; |
703 | ||
b2f96f9e EF |
704 | out_req_notifier_err: |
705 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX); | |
706 | out_crw_notifier_err: | |
707 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX); | |
708 | out_io_notifier_err: | |
4886b3e9 | 709 | vfio_ccw_put_region(vcdev); |
c14e706c | 710 | out_region_err: |
c96f2c2a | 711 | vfio_ccw_put_device(vcdev); |
1dcac3e1 XFR |
712 | out_device_err: |
713 | vfio_put_group(group); | |
714 | out_group_err: | |
715 | if (cdc->unrealize) { | |
b69c3c21 | 716 | cdc->unrealize(cdev); |
1dcac3e1 XFR |
717 | } |
718 | out_err_propagate: | |
719 | error_propagate(errp, err); | |
720 | } | |
721 | ||
b69c3c21 | 722 | static void vfio_ccw_unrealize(DeviceState *dev) |
1dcac3e1 XFR |
723 | { |
724 | CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev); | |
725 | S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev); | |
726 | VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); | |
727 | S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev); | |
728 | VFIOGroup *group = vcdev->vdev.group; | |
729 | ||
b2f96f9e | 730 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX); |
f030532f | 731 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX); |
690e29b9 | 732 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX); |
c14e706c | 733 | vfio_ccw_put_region(vcdev); |
c96f2c2a | 734 | vfio_ccw_put_device(vcdev); |
1dcac3e1 XFR |
735 | vfio_put_group(group); |
736 | ||
737 | if (cdc->unrealize) { | |
b69c3c21 | 738 | cdc->unrealize(cdev); |
1dcac3e1 XFR |
739 | } |
740 | } | |
741 | ||
742 | static Property vfio_ccw_properties[] = { | |
743 | DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice, vdev.sysfsdev), | |
9a51c9ee | 744 | DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice, force_orb_pfch, false), |
1dcac3e1 XFR |
745 | DEFINE_PROP_END_OF_LIST(), |
746 | }; | |
747 | ||
748 | static const VMStateDescription vfio_ccw_vmstate = { | |
da56e330 | 749 | .name = "vfio-ccw", |
1dcac3e1 XFR |
750 | .unmigratable = 1, |
751 | }; | |
752 | ||
753 | static void vfio_ccw_class_init(ObjectClass *klass, void *data) | |
754 | { | |
755 | DeviceClass *dc = DEVICE_CLASS(klass); | |
8ca2b376 | 756 | S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass); |
1dcac3e1 | 757 | |
4f67d30b | 758 | device_class_set_props(dc, vfio_ccw_properties); |
1dcac3e1 XFR |
759 | dc->vmsd = &vfio_ccw_vmstate; |
760 | dc->desc = "VFIO-based subchannel assignment"; | |
bd2aef10 | 761 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
1dcac3e1 XFR |
762 | dc->realize = vfio_ccw_realize; |
763 | dc->unrealize = vfio_ccw_unrealize; | |
764 | dc->reset = vfio_ccw_reset; | |
8ca2b376 XFR |
765 | |
766 | cdc->handle_request = vfio_ccw_handle_request; | |
8fadea24 CH |
767 | cdc->handle_halt = vfio_ccw_handle_halt; |
768 | cdc->handle_clear = vfio_ccw_handle_clear; | |
46ea3841 | 769 | cdc->handle_store = vfio_ccw_handle_store; |
1dcac3e1 XFR |
770 | } |
771 | ||
772 | static const TypeInfo vfio_ccw_info = { | |
773 | .name = TYPE_VFIO_CCW, | |
774 | .parent = TYPE_S390_CCW, | |
775 | .instance_size = sizeof(VFIOCCWDevice), | |
776 | .class_init = vfio_ccw_class_init, | |
777 | }; | |
778 | ||
779 | static void register_vfio_ccw_type(void) | |
780 | { | |
781 | type_register_static(&vfio_ccw_info); | |
782 | } | |
783 | ||
784 | type_init(register_vfio_ccw_type) |