]>
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" |
e70f971a | 18 | #include CONFIG_DEVICES /* CONFIG_IOMMUFD */ |
1dcac3e1 | 19 | #include <linux/vfio.h> |
c14e706c | 20 | #include <linux/vfio_ccw.h> |
1dcac3e1 XFR |
21 | #include <sys/ioctl.h> |
22 | ||
1dcac3e1 | 23 | #include "qapi/error.h" |
1dcac3e1 | 24 | #include "hw/vfio/vfio-common.h" |
e70f971a | 25 | #include "sysemu/iommufd.h" |
1dcac3e1 | 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 | { |
ecba6468 | 80 | VFIOCCWDevice *vcdev = VFIO_CCW(sch->driver_data); |
8ca2b376 XFR |
81 | struct ccw_io_region *region = vcdev->io_region; |
82 | int ret; | |
83 | ||
24e58a7b JR |
84 | if (!(sch->orb.ctrl0 & ORB_CTRL0_MASK_PFCH) && vcdev->force_orb_pfch) { |
85 | sch->orb.ctrl0 |= ORB_CTRL0_MASK_PFCH; | |
86 | warn_once_pfch(vcdev, sch, "PFCH flag forced"); | |
9a51c9ee HP |
87 | } |
88 | ||
8ca2b376 XFR |
89 | QEMU_BUILD_BUG_ON(sizeof(region->orb_area) != sizeof(ORB)); |
90 | QEMU_BUILD_BUG_ON(sizeof(region->scsw_area) != sizeof(SCSW)); | |
91 | QEMU_BUILD_BUG_ON(sizeof(region->irb_area) != sizeof(IRB)); | |
92 | ||
93 | memset(region, 0, sizeof(*region)); | |
94 | ||
66dc50f7 HP |
95 | memcpy(region->orb_area, &sch->orb, sizeof(ORB)); |
96 | memcpy(region->scsw_area, &sch->curr_status.scsw, sizeof(SCSW)); | |
8ca2b376 XFR |
97 | |
98 | again: | |
99 | ret = pwrite(vcdev->vdev.fd, region, | |
100 | vcdev->io_region_size, vcdev->io_region_offset); | |
101 | if (ret != vcdev->io_region_size) { | |
102 | if (errno == EAGAIN) { | |
103 | goto again; | |
104 | } | |
91f751dc | 105 | error_report("vfio-ccw: write I/O region failed with errno=%d", errno); |
d6cd6631 | 106 | ret = errno ? -errno : -EFAULT; |
66dc50f7 | 107 | } else { |
d6cd6631 | 108 | ret = 0; |
66dc50f7 HP |
109 | } |
110 | switch (ret) { | |
111 | case 0: | |
112 | return IOINST_CC_EXPECTED; | |
113 | case -EBUSY: | |
114 | return IOINST_CC_BUSY; | |
115 | case -ENODEV: | |
116 | case -EACCES: | |
117 | return IOINST_CC_NOT_OPERATIONAL; | |
118 | case -EFAULT: | |
119 | default: | |
120 | sch_gen_unit_exception(sch); | |
121 | css_inject_io_interrupt(sch); | |
122 | return IOINST_CC_EXPECTED; | |
8ca2b376 | 123 | } |
8ca2b376 XFR |
124 | } |
125 | ||
46ea3841 FA |
126 | static IOInstEnding vfio_ccw_handle_store(SubchDev *sch) |
127 | { | |
ecba6468 | 128 | VFIOCCWDevice *vcdev = VFIO_CCW(sch->driver_data); |
46ea3841 FA |
129 | SCHIB *schib = &sch->curr_status; |
130 | struct ccw_schib_region *region = vcdev->schib_region; | |
131 | SCHIB *s; | |
132 | int ret; | |
133 | ||
134 | /* schib region not available so nothing else to do */ | |
135 | if (!region) { | |
136 | return IOINST_CC_EXPECTED; | |
137 | } | |
138 | ||
139 | memset(region, 0, sizeof(*region)); | |
140 | ret = pread(vcdev->vdev.fd, region, vcdev->schib_region_size, | |
141 | vcdev->schib_region_offset); | |
142 | ||
143 | if (ret == -1) { | |
144 | /* | |
145 | * Device is probably damaged, but store subchannel does not | |
146 | * have a nonzero cc defined for this scenario. Log an error, | |
147 | * and presume things are otherwise fine. | |
148 | */ | |
149 | error_report("vfio-ccw: store region read failed with errno=%d", errno); | |
150 | return IOINST_CC_EXPECTED; | |
151 | } | |
152 | ||
153 | /* | |
154 | * Selectively copy path-related bits of the SCHIB, | |
155 | * rather than copying the entire struct. | |
156 | */ | |
157 | s = (SCHIB *)region->schib_area; | |
158 | schib->pmcw.pnom = s->pmcw.pnom; | |
159 | schib->pmcw.lpum = s->pmcw.lpum; | |
160 | schib->pmcw.pam = s->pmcw.pam; | |
161 | schib->pmcw.pom = s->pmcw.pom; | |
162 | ||
163 | if (s->scsw.flags & SCSW_FLAGS_MASK_PNO) { | |
164 | schib->scsw.flags |= SCSW_FLAGS_MASK_PNO; | |
165 | } | |
166 | ||
167 | return IOINST_CC_EXPECTED; | |
168 | } | |
169 | ||
8fadea24 CH |
170 | static int vfio_ccw_handle_clear(SubchDev *sch) |
171 | { | |
ecba6468 | 172 | VFIOCCWDevice *vcdev = VFIO_CCW(sch->driver_data); |
8fadea24 CH |
173 | struct ccw_cmd_region *region = vcdev->async_cmd_region; |
174 | int ret; | |
175 | ||
176 | if (!vcdev->async_cmd_region) { | |
177 | /* Async command region not available, fall back to emulation */ | |
178 | return -ENOSYS; | |
179 | } | |
180 | ||
181 | memset(region, 0, sizeof(*region)); | |
182 | region->command = VFIO_CCW_ASYNC_CMD_CSCH; | |
183 | ||
184 | again: | |
185 | ret = pwrite(vcdev->vdev.fd, region, | |
186 | vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset); | |
187 | if (ret != vcdev->async_cmd_region_size) { | |
188 | if (errno == EAGAIN) { | |
189 | goto again; | |
190 | } | |
191 | error_report("vfio-ccw: write cmd region failed with errno=%d", errno); | |
d6cd6631 | 192 | ret = errno ? -errno : -EFAULT; |
8fadea24 | 193 | } else { |
d6cd6631 | 194 | ret = 0; |
8fadea24 CH |
195 | } |
196 | switch (ret) { | |
197 | case 0: | |
198 | case -ENODEV: | |
199 | case -EACCES: | |
759a5d3b | 200 | return ret; |
8fadea24 CH |
201 | case -EFAULT: |
202 | default: | |
203 | sch_gen_unit_exception(sch); | |
204 | css_inject_io_interrupt(sch); | |
205 | return 0; | |
206 | } | |
207 | } | |
208 | ||
209 | static int vfio_ccw_handle_halt(SubchDev *sch) | |
210 | { | |
ecba6468 | 211 | VFIOCCWDevice *vcdev = VFIO_CCW(sch->driver_data); |
8fadea24 CH |
212 | struct ccw_cmd_region *region = vcdev->async_cmd_region; |
213 | int ret; | |
214 | ||
215 | if (!vcdev->async_cmd_region) { | |
216 | /* Async command region not available, fall back to emulation */ | |
217 | return -ENOSYS; | |
218 | } | |
219 | ||
220 | memset(region, 0, sizeof(*region)); | |
221 | region->command = VFIO_CCW_ASYNC_CMD_HSCH; | |
222 | ||
223 | again: | |
224 | ret = pwrite(vcdev->vdev.fd, region, | |
225 | vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset); | |
226 | if (ret != vcdev->async_cmd_region_size) { | |
227 | if (errno == EAGAIN) { | |
228 | goto again; | |
229 | } | |
230 | error_report("vfio-ccw: write cmd region failed with errno=%d", errno); | |
d6cd6631 | 231 | ret = errno ? -errno : -EFAULT; |
8fadea24 | 232 | } else { |
d6cd6631 | 233 | ret = 0; |
8fadea24 CH |
234 | } |
235 | switch (ret) { | |
236 | case 0: | |
237 | case -EBUSY: | |
238 | case -ENODEV: | |
239 | case -EACCES: | |
759a5d3b | 240 | return ret; |
8fadea24 CH |
241 | case -EFAULT: |
242 | default: | |
243 | sch_gen_unit_exception(sch); | |
244 | css_inject_io_interrupt(sch); | |
245 | return 0; | |
246 | } | |
247 | } | |
248 | ||
1dcac3e1 XFR |
249 | static void vfio_ccw_reset(DeviceState *dev) |
250 | { | |
ecba6468 | 251 | VFIOCCWDevice *vcdev = VFIO_CCW(dev); |
1dcac3e1 XFR |
252 | |
253 | ioctl(vcdev->vdev.fd, VFIO_DEVICE_RESET); | |
254 | } | |
255 | ||
f030532f FA |
256 | static void vfio_ccw_crw_read(VFIOCCWDevice *vcdev) |
257 | { | |
258 | struct ccw_crw_region *region = vcdev->crw_region; | |
259 | CRW crw; | |
260 | int size; | |
261 | ||
262 | /* Keep reading CRWs as long as data is returned */ | |
263 | do { | |
264 | memset(region, 0, sizeof(*region)); | |
265 | size = pread(vcdev->vdev.fd, region, vcdev->crw_region_size, | |
266 | vcdev->crw_region_offset); | |
267 | ||
268 | if (size == -1) { | |
269 | error_report("vfio-ccw: Read crw region failed with errno=%d", | |
270 | errno); | |
271 | break; | |
272 | } | |
273 | ||
274 | if (region->crw == 0) { | |
275 | /* No more CRWs to queue */ | |
276 | break; | |
277 | } | |
278 | ||
279 | memcpy(&crw, ®ion->crw, sizeof(CRW)); | |
280 | ||
281 | css_crw_add_to_queue(crw); | |
282 | } while (1); | |
283 | } | |
284 | ||
b2f96f9e EF |
285 | static void vfio_ccw_req_notifier_handler(void *opaque) |
286 | { | |
287 | VFIOCCWDevice *vcdev = opaque; | |
288 | Error *err = NULL; | |
289 | ||
290 | if (!event_notifier_test_and_clear(&vcdev->req_notifier)) { | |
291 | return; | |
292 | } | |
293 | ||
294 | qdev_unplug(DEVICE(vcdev), &err); | |
295 | if (err) { | |
296 | warn_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name); | |
297 | } | |
298 | } | |
299 | ||
f030532f FA |
300 | static void vfio_ccw_crw_notifier_handler(void *opaque) |
301 | { | |
302 | VFIOCCWDevice *vcdev = opaque; | |
303 | ||
304 | while (event_notifier_test_and_clear(&vcdev->crw_notifier)) { | |
305 | vfio_ccw_crw_read(vcdev); | |
306 | } | |
307 | } | |
308 | ||
4886b3e9 DJS |
309 | static void vfio_ccw_io_notifier_handler(void *opaque) |
310 | { | |
311 | VFIOCCWDevice *vcdev = opaque; | |
8ca2b376 | 312 | struct ccw_io_region *region = vcdev->io_region; |
4b447883 | 313 | CcwDevice *ccw_dev = CCW_DEVICE(vcdev); |
8ca2b376 | 314 | SubchDev *sch = ccw_dev->sch; |
e1d0b372 DB |
315 | SCHIB *schib = &sch->curr_status; |
316 | SCSW s; | |
8ca2b376 | 317 | IRB irb; |
c626710f | 318 | ESW esw; |
8ca2b376 | 319 | int size; |
4886b3e9 DJS |
320 | |
321 | if (!event_notifier_test_and_clear(&vcdev->io_notifier)) { | |
322 | return; | |
323 | } | |
8ca2b376 XFR |
324 | |
325 | size = pread(vcdev->vdev.fd, region, vcdev->io_region_size, | |
326 | vcdev->io_region_offset); | |
327 | if (size == -1) { | |
328 | switch (errno) { | |
329 | case ENODEV: | |
330 | /* Generate a deferred cc 3 condition. */ | |
e1d0b372 DB |
331 | schib->scsw.flags |= SCSW_FLAGS_MASK_CC; |
332 | schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; | |
333 | schib->scsw.ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND); | |
8ca2b376 XFR |
334 | goto read_err; |
335 | case EFAULT: | |
336 | /* Memory problem, generate channel data check. */ | |
e1d0b372 DB |
337 | schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND; |
338 | schib->scsw.cstat = SCSW_CSTAT_DATA_CHECK; | |
339 | schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; | |
340 | schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | | |
8ca2b376 XFR |
341 | SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; |
342 | goto read_err; | |
343 | default: | |
344 | /* Error, generate channel program check. */ | |
e1d0b372 DB |
345 | schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND; |
346 | schib->scsw.cstat = SCSW_CSTAT_PROG_CHECK; | |
347 | schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; | |
348 | schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | | |
8ca2b376 XFR |
349 | SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; |
350 | goto read_err; | |
351 | } | |
352 | } else if (size != vcdev->io_region_size) { | |
353 | /* Information transfer error, generate channel-control check. */ | |
e1d0b372 DB |
354 | schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND; |
355 | schib->scsw.cstat = SCSW_CSTAT_CHN_CTRL_CHK; | |
356 | schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; | |
357 | schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | | |
8ca2b376 XFR |
358 | SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; |
359 | goto read_err; | |
360 | } | |
361 | ||
362 | memcpy(&irb, region->irb_area, sizeof(IRB)); | |
363 | ||
364 | /* Update control block via irb. */ | |
e1d0b372 DB |
365 | s = schib->scsw; |
366 | copy_scsw_to_guest(&s, &irb.scsw); | |
367 | schib->scsw = s; | |
8ca2b376 | 368 | |
c626710f EF |
369 | copy_esw_to_guest(&esw, &irb.esw); |
370 | sch->esw = esw; | |
371 | ||
334e7685 | 372 | /* If a uint check is pending, copy sense data. */ |
e1d0b372 DB |
373 | if ((schib->scsw.dstat & SCSW_DSTAT_UNIT_CHECK) && |
374 | (schib->pmcw.chars & PMCW_CHARS_MASK_CSENSE)) { | |
334e7685 DJS |
375 | memcpy(sch->sense_data, irb.ecw, sizeof(irb.ecw)); |
376 | } | |
377 | ||
8ca2b376 XFR |
378 | read_err: |
379 | css_inject_io_interrupt(sch); | |
4886b3e9 DJS |
380 | } |
381 | ||
690e29b9 EF |
382 | static void vfio_ccw_register_irq_notifier(VFIOCCWDevice *vcdev, |
383 | unsigned int irq, | |
384 | Error **errp) | |
4886b3e9 DJS |
385 | { |
386 | VFIODevice *vdev = &vcdev->vdev; | |
387 | struct vfio_irq_info *irq_info; | |
4886b3e9 | 388 | size_t argsz; |
b5e89f04 | 389 | int fd; |
690e29b9 EF |
390 | EventNotifier *notifier; |
391 | IOHandler *fd_read; | |
392 | ||
393 | switch (irq) { | |
394 | case VFIO_CCW_IO_IRQ_INDEX: | |
395 | notifier = &vcdev->io_notifier; | |
396 | fd_read = vfio_ccw_io_notifier_handler; | |
397 | break; | |
f030532f FA |
398 | case VFIO_CCW_CRW_IRQ_INDEX: |
399 | notifier = &vcdev->crw_notifier; | |
400 | fd_read = vfio_ccw_crw_notifier_handler; | |
401 | break; | |
b2f96f9e EF |
402 | case VFIO_CCW_REQ_IRQ_INDEX: |
403 | notifier = &vcdev->req_notifier; | |
404 | fd_read = vfio_ccw_req_notifier_handler; | |
405 | break; | |
690e29b9 EF |
406 | default: |
407 | error_setg(errp, "vfio: Unsupported device irq(%d)", irq); | |
408 | return; | |
409 | } | |
4886b3e9 | 410 | |
690e29b9 | 411 | if (vdev->num_irqs < irq + 1) { |
6178d468 EF |
412 | error_setg(errp, "vfio: IRQ %u not available (number of irqs %u)", |
413 | irq, vdev->num_irqs); | |
4886b3e9 DJS |
414 | return; |
415 | } | |
416 | ||
28e22d4b | 417 | argsz = sizeof(*irq_info); |
4886b3e9 | 418 | irq_info = g_malloc0(argsz); |
690e29b9 | 419 | irq_info->index = irq; |
4886b3e9 DJS |
420 | irq_info->argsz = argsz; |
421 | if (ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO, | |
422 | irq_info) < 0 || irq_info->count < 1) { | |
423 | error_setg_errno(errp, errno, "vfio: Error getting irq info"); | |
424 | goto out_free_info; | |
425 | } | |
426 | ||
690e29b9 | 427 | if (event_notifier_init(notifier, 0)) { |
4886b3e9 | 428 | error_setg_errno(errp, errno, |
690e29b9 EF |
429 | "vfio: Unable to init event notifier for irq (%d)", |
430 | irq); | |
4886b3e9 DJS |
431 | goto out_free_info; |
432 | } | |
433 | ||
690e29b9 EF |
434 | fd = event_notifier_get_fd(notifier); |
435 | qemu_set_fd_handler(fd, fd_read, NULL, vcdev); | |
b5e89f04 | 436 | |
690e29b9 | 437 | if (vfio_set_irq_signaling(vdev, irq, 0, |
b5e89f04 CH |
438 | VFIO_IRQ_SET_ACTION_TRIGGER, fd, errp)) { |
439 | qemu_set_fd_handler(fd, NULL, NULL, vcdev); | |
690e29b9 | 440 | event_notifier_cleanup(notifier); |
4886b3e9 DJS |
441 | } |
442 | ||
4886b3e9 DJS |
443 | out_free_info: |
444 | g_free(irq_info); | |
445 | } | |
446 | ||
690e29b9 EF |
447 | static void vfio_ccw_unregister_irq_notifier(VFIOCCWDevice *vcdev, |
448 | unsigned int irq) | |
4886b3e9 | 449 | { |
b5e89f04 | 450 | Error *err = NULL; |
690e29b9 EF |
451 | EventNotifier *notifier; |
452 | ||
453 | switch (irq) { | |
454 | case VFIO_CCW_IO_IRQ_INDEX: | |
455 | notifier = &vcdev->io_notifier; | |
456 | break; | |
f030532f FA |
457 | case VFIO_CCW_CRW_IRQ_INDEX: |
458 | notifier = &vcdev->crw_notifier; | |
459 | break; | |
b2f96f9e EF |
460 | case VFIO_CCW_REQ_IRQ_INDEX: |
461 | notifier = &vcdev->req_notifier; | |
462 | break; | |
690e29b9 EF |
463 | default: |
464 | error_report("vfio: Unsupported device irq(%d)", irq); | |
465 | return; | |
466 | } | |
b5e89f04 | 467 | |
690e29b9 | 468 | if (vfio_set_irq_signaling(&vcdev->vdev, irq, 0, |
f5cf94cd | 469 | VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) { |
dcc9cf38 | 470 | warn_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name); |
4886b3e9 DJS |
471 | } |
472 | ||
690e29b9 | 473 | qemu_set_fd_handler(event_notifier_get_fd(notifier), |
4886b3e9 | 474 | NULL, NULL, vcdev); |
690e29b9 | 475 | event_notifier_cleanup(notifier); |
4886b3e9 DJS |
476 | } |
477 | ||
c14e706c DJS |
478 | static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp) |
479 | { | |
480 | VFIODevice *vdev = &vcdev->vdev; | |
481 | struct vfio_region_info *info; | |
482 | int ret; | |
483 | ||
484 | /* Sanity check device */ | |
485 | if (!(vdev->flags & VFIO_DEVICE_FLAGS_CCW)) { | |
486 | error_setg(errp, "vfio: Um, this isn't a vfio-ccw device"); | |
487 | return; | |
488 | } | |
489 | ||
8fadea24 CH |
490 | /* |
491 | * We always expect at least the I/O region to be present. We also | |
492 | * may have a variable number of regions governed by capabilities. | |
493 | */ | |
c14e706c | 494 | if (vdev->num_regions < VFIO_CCW_CONFIG_REGION_INDEX + 1) { |
8fadea24 CH |
495 | error_setg(errp, "vfio: too few regions (%u), expected at least %u", |
496 | vdev->num_regions, VFIO_CCW_CONFIG_REGION_INDEX + 1); | |
c14e706c DJS |
497 | return; |
498 | } | |
499 | ||
500 | ret = vfio_get_region_info(vdev, VFIO_CCW_CONFIG_REGION_INDEX, &info); | |
501 | if (ret) { | |
502 | error_setg_errno(errp, -ret, "vfio: Error getting config info"); | |
503 | return; | |
504 | } | |
505 | ||
506 | vcdev->io_region_size = info->size; | |
507 | if (sizeof(*vcdev->io_region) != vcdev->io_region_size) { | |
508 | error_setg(errp, "vfio: Unexpected size of the I/O region"); | |
2a3b9cba | 509 | goto out_err; |
c14e706c DJS |
510 | } |
511 | ||
512 | vcdev->io_region_offset = info->offset; | |
513 | vcdev->io_region = g_malloc0(info->size); | |
c8726f7b | 514 | g_free(info); |
c14e706c | 515 | |
8fadea24 CH |
516 | /* check for the optional async command region */ |
517 | ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW, | |
518 | VFIO_REGION_SUBTYPE_CCW_ASYNC_CMD, &info); | |
519 | if (!ret) { | |
520 | vcdev->async_cmd_region_size = info->size; | |
521 | if (sizeof(*vcdev->async_cmd_region) != vcdev->async_cmd_region_size) { | |
522 | error_setg(errp, "vfio: Unexpected size of the async cmd region"); | |
2a3b9cba | 523 | goto out_err; |
8fadea24 CH |
524 | } |
525 | vcdev->async_cmd_region_offset = info->offset; | |
526 | vcdev->async_cmd_region = g_malloc0(info->size); | |
c8726f7b | 527 | g_free(info); |
8fadea24 CH |
528 | } |
529 | ||
46ea3841 FA |
530 | ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW, |
531 | VFIO_REGION_SUBTYPE_CCW_SCHIB, &info); | |
532 | if (!ret) { | |
533 | vcdev->schib_region_size = info->size; | |
534 | if (sizeof(*vcdev->schib_region) != vcdev->schib_region_size) { | |
535 | error_setg(errp, "vfio: Unexpected size of the schib region"); | |
536 | goto out_err; | |
537 | } | |
538 | vcdev->schib_region_offset = info->offset; | |
539 | vcdev->schib_region = g_malloc(info->size); | |
c8726f7b | 540 | g_free(info); |
46ea3841 FA |
541 | } |
542 | ||
f030532f FA |
543 | ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW, |
544 | VFIO_REGION_SUBTYPE_CCW_CRW, &info); | |
545 | ||
546 | if (!ret) { | |
547 | vcdev->crw_region_size = info->size; | |
548 | if (sizeof(*vcdev->crw_region) != vcdev->crw_region_size) { | |
549 | error_setg(errp, "vfio: Unexpected size of the CRW region"); | |
550 | goto out_err; | |
551 | } | |
552 | vcdev->crw_region_offset = info->offset; | |
553 | vcdev->crw_region = g_malloc(info->size); | |
c8726f7b | 554 | g_free(info); |
f030532f FA |
555 | } |
556 | ||
2a3b9cba EF |
557 | return; |
558 | ||
559 | out_err: | |
f030532f | 560 | g_free(vcdev->crw_region); |
46ea3841 | 561 | g_free(vcdev->schib_region); |
2a3b9cba EF |
562 | g_free(vcdev->async_cmd_region); |
563 | g_free(vcdev->io_region); | |
564 | g_free(info); | |
565 | return; | |
c14e706c DJS |
566 | } |
567 | ||
568 | static void vfio_ccw_put_region(VFIOCCWDevice *vcdev) | |
569 | { | |
f030532f | 570 | g_free(vcdev->crw_region); |
46ea3841 | 571 | g_free(vcdev->schib_region); |
8fadea24 | 572 | g_free(vcdev->async_cmd_region); |
c14e706c DJS |
573 | g_free(vcdev->io_region); |
574 | } | |
575 | ||
1dcac3e1 XFR |
576 | static void vfio_ccw_realize(DeviceState *dev, Error **errp) |
577 | { | |
0cea1f62 | 578 | S390CCWDevice *cdev = S390_CCW_DEVICE(dev); |
ecba6468 | 579 | VFIOCCWDevice *vcdev = VFIO_CCW(cdev); |
1dcac3e1 | 580 | S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev); |
e08041ec | 581 | VFIODevice *vbasedev = &vcdev->vdev; |
1dcac3e1 | 582 | Error *err = NULL; |
e08041ec | 583 | int ret; |
1dcac3e1 XFR |
584 | |
585 | /* Call the class init function for subchannel. */ | |
586 | if (cdc->realize) { | |
587 | cdc->realize(cdev, vcdev->vdev.sysfsdev, &err); | |
588 | if (err) { | |
589 | goto out_err_propagate; | |
590 | } | |
591 | } | |
592 | ||
e08041ec EA |
593 | vbasedev->ops = &vfio_ccw_ops; |
594 | vbasedev->type = VFIO_DEVICE_TYPE_CCW; | |
595 | vbasedev->name = g_strdup_printf("%x.%x.%04x", vcdev->cdev.hostid.cssid, | |
596 | vcdev->cdev.hostid.ssid, | |
597 | vcdev->cdev.hostid.devid); | |
598 | vbasedev->dev = dev; | |
1dcac3e1 | 599 | |
e08041ec EA |
600 | /* |
601 | * All vfio-ccw devices are believed to operate in a way compatible with | |
602 | * discarding of memory in RAM blocks, ie. pages pinned in the host are | |
603 | * in the current working set of the guest driver and therefore never | |
604 | * overlap e.g., with pages available to the guest balloon driver. This | |
605 | * needs to be set before vfio_get_device() for vfio common to handle | |
606 | * ram_block_discard_disable(). | |
607 | */ | |
608 | vbasedev->ram_block_discard_allowed = true; | |
609 | ||
610 | ret = vfio_attach_device(cdev->mdevid, vbasedev, | |
611 | &address_space_memory, errp); | |
612 | if (ret) { | |
613 | goto out_attach_dev_err; | |
1dcac3e1 XFR |
614 | } |
615 | ||
c14e706c DJS |
616 | vfio_ccw_get_region(vcdev, &err); |
617 | if (err) { | |
618 | goto out_region_err; | |
619 | } | |
620 | ||
690e29b9 | 621 | vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX, &err); |
4886b3e9 | 622 | if (err) { |
b2f96f9e | 623 | goto out_io_notifier_err; |
4886b3e9 DJS |
624 | } |
625 | ||
f030532f FA |
626 | if (vcdev->crw_region) { |
627 | vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX, &err); | |
628 | if (err) { | |
dcc9cf38 | 629 | goto out_irq_notifier_err; |
f030532f FA |
630 | } |
631 | } | |
632 | ||
b2f96f9e EF |
633 | vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX, &err); |
634 | if (err) { | |
6178d468 EF |
635 | /* |
636 | * Report this error, but do not make it a failing condition. | |
637 | * Lack of this IRQ in the host does not prevent normal operation. | |
638 | */ | |
639 | error_report_err(err); | |
b2f96f9e EF |
640 | } |
641 | ||
1dcac3e1 XFR |
642 | return; |
643 | ||
dcc9cf38 EF |
644 | out_irq_notifier_err: |
645 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX); | |
646 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX); | |
b2f96f9e EF |
647 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX); |
648 | out_io_notifier_err: | |
4886b3e9 | 649 | vfio_ccw_put_region(vcdev); |
c14e706c | 650 | out_region_err: |
e08041ec EA |
651 | vfio_detach_device(vbasedev); |
652 | out_attach_dev_err: | |
653 | g_free(vbasedev->name); | |
1dcac3e1 | 654 | if (cdc->unrealize) { |
b69c3c21 | 655 | cdc->unrealize(cdev); |
1dcac3e1 XFR |
656 | } |
657 | out_err_propagate: | |
658 | error_propagate(errp, err); | |
659 | } | |
660 | ||
b69c3c21 | 661 | static void vfio_ccw_unrealize(DeviceState *dev) |
1dcac3e1 | 662 | { |
0cea1f62 | 663 | S390CCWDevice *cdev = S390_CCW_DEVICE(dev); |
ecba6468 | 664 | VFIOCCWDevice *vcdev = VFIO_CCW(cdev); |
1dcac3e1 | 665 | S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev); |
1dcac3e1 | 666 | |
b2f96f9e | 667 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX); |
f030532f | 668 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX); |
690e29b9 | 669 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX); |
c14e706c | 670 | vfio_ccw_put_region(vcdev); |
e08041ec EA |
671 | vfio_detach_device(&vcdev->vdev); |
672 | g_free(vcdev->vdev.name); | |
1dcac3e1 XFR |
673 | |
674 | if (cdc->unrealize) { | |
b69c3c21 | 675 | cdc->unrealize(cdev); |
1dcac3e1 XFR |
676 | } |
677 | } | |
678 | ||
679 | static Property vfio_ccw_properties[] = { | |
680 | DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice, vdev.sysfsdev), | |
9a51c9ee | 681 | DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice, force_orb_pfch, false), |
e70f971a ZD |
682 | #ifdef CONFIG_IOMMUFD |
683 | DEFINE_PROP_LINK("iommufd", VFIOCCWDevice, vdev.iommufd, | |
684 | TYPE_IOMMUFD_BACKEND, IOMMUFDBackend *), | |
685 | #endif | |
1dcac3e1 XFR |
686 | DEFINE_PROP_END_OF_LIST(), |
687 | }; | |
688 | ||
689 | static const VMStateDescription vfio_ccw_vmstate = { | |
da56e330 | 690 | .name = "vfio-ccw", |
1dcac3e1 XFR |
691 | .unmigratable = 1, |
692 | }; | |
693 | ||
694 | static void vfio_ccw_class_init(ObjectClass *klass, void *data) | |
695 | { | |
696 | DeviceClass *dc = DEVICE_CLASS(klass); | |
8ca2b376 | 697 | S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass); |
1dcac3e1 | 698 | |
4f67d30b | 699 | device_class_set_props(dc, vfio_ccw_properties); |
1dcac3e1 XFR |
700 | dc->vmsd = &vfio_ccw_vmstate; |
701 | dc->desc = "VFIO-based subchannel assignment"; | |
bd2aef10 | 702 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
1dcac3e1 XFR |
703 | dc->realize = vfio_ccw_realize; |
704 | dc->unrealize = vfio_ccw_unrealize; | |
705 | dc->reset = vfio_ccw_reset; | |
8ca2b376 XFR |
706 | |
707 | cdc->handle_request = vfio_ccw_handle_request; | |
8fadea24 CH |
708 | cdc->handle_halt = vfio_ccw_handle_halt; |
709 | cdc->handle_clear = vfio_ccw_handle_clear; | |
46ea3841 | 710 | cdc->handle_store = vfio_ccw_handle_store; |
1dcac3e1 XFR |
711 | } |
712 | ||
713 | static const TypeInfo vfio_ccw_info = { | |
714 | .name = TYPE_VFIO_CCW, | |
715 | .parent = TYPE_S390_CCW, | |
716 | .instance_size = sizeof(VFIOCCWDevice), | |
717 | .class_init = vfio_ccw_class_init, | |
718 | }; | |
719 | ||
720 | static void register_vfio_ccw_type(void) | |
721 | { | |
722 | type_register_static(&vfio_ccw_info); | |
723 | } | |
724 | ||
725 | type_init(register_vfio_ccw_type) |