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