1 /* 2 * vfio based subchannel assignment support 3 * 4 * Copyright 2017 IBM Corp. 5 * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com> 6 * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com> 7 * Pierre Morel <pmorel@linux.vnet.ibm.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or (at 10 * your option) any later version. See the COPYING file in the top-level 11 * directory. 12 */ 13 14 #include "qemu/osdep.h" 15 #include <linux/vfio.h> 16 #include <linux/vfio_ccw.h> 17 #include <sys/ioctl.h> 18 19 #include "qapi/error.h" 20 #include "hw/sysbus.h" 21 #include "hw/vfio/vfio.h" 22 #include "hw/vfio/vfio-common.h" 23 #include "hw/s390x/s390-ccw.h" 24 #include "hw/s390x/ccw-device.h" 25 #include "exec/address-spaces.h" 26 #include "qemu/error-report.h" 27 28 #define TYPE_VFIO_CCW "vfio-ccw" 29 typedef struct VFIOCCWDevice { 30 S390CCWDevice cdev; 31 VFIODevice vdev; 32 uint64_t io_region_size; 33 uint64_t io_region_offset; 34 struct ccw_io_region *io_region; 35 EventNotifier io_notifier; 36 bool force_orb_pfch; 37 bool warned_orb_pfch; 38 } VFIOCCWDevice; 39 40 static inline void warn_once(bool *warned, const char *fmt, ...) 41 { 42 va_list ap; 43 44 if (!warned || *warned) { 45 return; 46 } 47 *warned = true; 48 va_start(ap, fmt); 49 warn_vreport(fmt, ap); 50 va_end(ap); 51 } 52 53 static inline void warn_once_pfch(VFIOCCWDevice *vcdev, SubchDev *sch, 54 const char *msg) 55 { 56 warn_once(&vcdev->warned_orb_pfch, "vfio-ccw (devno %x.%x.%04x): %s", 57 sch->cssid, sch->ssid, sch->devno, msg); 58 } 59 60 static void vfio_ccw_compute_needs_reset(VFIODevice *vdev) 61 { 62 vdev->needs_reset = false; 63 } 64 65 /* 66 * We don't need vfio_hot_reset_multi and vfio_eoi operations for 67 * vfio_ccw device now. 68 */ 69 struct VFIODeviceOps vfio_ccw_ops = { 70 .vfio_compute_needs_reset = vfio_ccw_compute_needs_reset, 71 }; 72 73 static IOInstEnding vfio_ccw_handle_request(SubchDev *sch) 74 { 75 S390CCWDevice *cdev = sch->driver_data; 76 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); 77 struct ccw_io_region *region = vcdev->io_region; 78 int ret; 79 80 if (!(sch->orb.ctrl0 & ORB_CTRL0_MASK_PFCH)) { 81 if (!(vcdev->force_orb_pfch)) { 82 warn_once_pfch(vcdev, sch, "requires PFCH flag set"); 83 sch_gen_unit_exception(sch); 84 css_inject_io_interrupt(sch); 85 return IOINST_CC_EXPECTED; 86 } else { 87 sch->orb.ctrl0 |= ORB_CTRL0_MASK_PFCH; 88 warn_once_pfch(vcdev, sch, "PFCH flag forced"); 89 } 90 } 91 92 QEMU_BUILD_BUG_ON(sizeof(region->orb_area) != sizeof(ORB)); 93 QEMU_BUILD_BUG_ON(sizeof(region->scsw_area) != sizeof(SCSW)); 94 QEMU_BUILD_BUG_ON(sizeof(region->irb_area) != sizeof(IRB)); 95 96 memset(region, 0, sizeof(*region)); 97 98 memcpy(region->orb_area, &sch->orb, sizeof(ORB)); 99 memcpy(region->scsw_area, &sch->curr_status.scsw, sizeof(SCSW)); 100 101 again: 102 ret = pwrite(vcdev->vdev.fd, region, 103 vcdev->io_region_size, vcdev->io_region_offset); 104 if (ret != vcdev->io_region_size) { 105 if (errno == EAGAIN) { 106 goto again; 107 } 108 error_report("vfio-ccw: wirte I/O region failed with errno=%d", errno); 109 ret = -errno; 110 } else { 111 ret = region->ret_code; 112 } 113 switch (ret) { 114 case 0: 115 return IOINST_CC_EXPECTED; 116 case -EBUSY: 117 return IOINST_CC_BUSY; 118 case -ENODEV: 119 case -EACCES: 120 return IOINST_CC_NOT_OPERATIONAL; 121 case -EFAULT: 122 default: 123 sch_gen_unit_exception(sch); 124 css_inject_io_interrupt(sch); 125 return IOINST_CC_EXPECTED; 126 } 127 } 128 129 static void vfio_ccw_reset(DeviceState *dev) 130 { 131 CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev); 132 S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev); 133 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); 134 135 ioctl(vcdev->vdev.fd, VFIO_DEVICE_RESET); 136 } 137 138 static void vfio_ccw_io_notifier_handler(void *opaque) 139 { 140 VFIOCCWDevice *vcdev = opaque; 141 struct ccw_io_region *region = vcdev->io_region; 142 S390CCWDevice *cdev = S390_CCW_DEVICE(vcdev); 143 CcwDevice *ccw_dev = CCW_DEVICE(cdev); 144 SubchDev *sch = ccw_dev->sch; 145 SCSW *s = &sch->curr_status.scsw; 146 PMCW *p = &sch->curr_status.pmcw; 147 IRB irb; 148 int size; 149 150 if (!event_notifier_test_and_clear(&vcdev->io_notifier)) { 151 return; 152 } 153 154 size = pread(vcdev->vdev.fd, region, vcdev->io_region_size, 155 vcdev->io_region_offset); 156 if (size == -1) { 157 switch (errno) { 158 case ENODEV: 159 /* Generate a deferred cc 3 condition. */ 160 s->flags |= SCSW_FLAGS_MASK_CC; 161 s->ctrl &= ~SCSW_CTRL_MASK_STCTL; 162 s->ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND); 163 goto read_err; 164 case EFAULT: 165 /* Memory problem, generate channel data check. */ 166 s->ctrl &= ~SCSW_ACTL_START_PEND; 167 s->cstat = SCSW_CSTAT_DATA_CHECK; 168 s->ctrl &= ~SCSW_CTRL_MASK_STCTL; 169 s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | 170 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; 171 goto read_err; 172 default: 173 /* Error, generate channel program check. */ 174 s->ctrl &= ~SCSW_ACTL_START_PEND; 175 s->cstat = SCSW_CSTAT_PROG_CHECK; 176 s->ctrl &= ~SCSW_CTRL_MASK_STCTL; 177 s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | 178 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; 179 goto read_err; 180 } 181 } else if (size != vcdev->io_region_size) { 182 /* Information transfer error, generate channel-control check. */ 183 s->ctrl &= ~SCSW_ACTL_START_PEND; 184 s->cstat = SCSW_CSTAT_CHN_CTRL_CHK; 185 s->ctrl &= ~SCSW_CTRL_MASK_STCTL; 186 s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | 187 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; 188 goto read_err; 189 } 190 191 memcpy(&irb, region->irb_area, sizeof(IRB)); 192 193 /* Update control block via irb. */ 194 copy_scsw_to_guest(s, &irb.scsw); 195 196 /* If a uint check is pending, copy sense data. */ 197 if ((s->dstat & SCSW_DSTAT_UNIT_CHECK) && 198 (p->chars & PMCW_CHARS_MASK_CSENSE)) { 199 memcpy(sch->sense_data, irb.ecw, sizeof(irb.ecw)); 200 } 201 202 read_err: 203 css_inject_io_interrupt(sch); 204 } 205 206 static void vfio_ccw_register_io_notifier(VFIOCCWDevice *vcdev, Error **errp) 207 { 208 VFIODevice *vdev = &vcdev->vdev; 209 struct vfio_irq_info *irq_info; 210 struct vfio_irq_set *irq_set; 211 size_t argsz; 212 int32_t *pfd; 213 214 if (vdev->num_irqs < VFIO_CCW_IO_IRQ_INDEX + 1) { 215 error_setg(errp, "vfio: unexpected number of io irqs %u", 216 vdev->num_irqs); 217 return; 218 } 219 220 argsz = sizeof(*irq_info); 221 irq_info = g_malloc0(argsz); 222 irq_info->index = VFIO_CCW_IO_IRQ_INDEX; 223 irq_info->argsz = argsz; 224 if (ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO, 225 irq_info) < 0 || irq_info->count < 1) { 226 error_setg_errno(errp, errno, "vfio: Error getting irq info"); 227 goto out_free_info; 228 } 229 230 if (event_notifier_init(&vcdev->io_notifier, 0)) { 231 error_setg_errno(errp, errno, 232 "vfio: Unable to init event notifier for IO"); 233 goto out_free_info; 234 } 235 236 argsz = sizeof(*irq_set) + sizeof(*pfd); 237 irq_set = g_malloc0(argsz); 238 irq_set->argsz = argsz; 239 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | 240 VFIO_IRQ_SET_ACTION_TRIGGER; 241 irq_set->index = VFIO_CCW_IO_IRQ_INDEX; 242 irq_set->start = 0; 243 irq_set->count = 1; 244 pfd = (int32_t *) &irq_set->data; 245 246 *pfd = event_notifier_get_fd(&vcdev->io_notifier); 247 qemu_set_fd_handler(*pfd, vfio_ccw_io_notifier_handler, NULL, vcdev); 248 if (ioctl(vdev->fd, VFIO_DEVICE_SET_IRQS, irq_set)) { 249 error_setg(errp, "vfio: Failed to set up io notification"); 250 qemu_set_fd_handler(*pfd, NULL, NULL, vcdev); 251 event_notifier_cleanup(&vcdev->io_notifier); 252 } 253 254 g_free(irq_set); 255 256 out_free_info: 257 g_free(irq_info); 258 } 259 260 static void vfio_ccw_unregister_io_notifier(VFIOCCWDevice *vcdev) 261 { 262 struct vfio_irq_set *irq_set; 263 size_t argsz; 264 int32_t *pfd; 265 266 argsz = sizeof(*irq_set) + sizeof(*pfd); 267 irq_set = g_malloc0(argsz); 268 irq_set->argsz = argsz; 269 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | 270 VFIO_IRQ_SET_ACTION_TRIGGER; 271 irq_set->index = VFIO_CCW_IO_IRQ_INDEX; 272 irq_set->start = 0; 273 irq_set->count = 1; 274 pfd = (int32_t *) &irq_set->data; 275 *pfd = -1; 276 277 if (ioctl(vcdev->vdev.fd, VFIO_DEVICE_SET_IRQS, irq_set)) { 278 error_report("vfio: Failed to de-assign device io fd: %m"); 279 } 280 281 qemu_set_fd_handler(event_notifier_get_fd(&vcdev->io_notifier), 282 NULL, NULL, vcdev); 283 event_notifier_cleanup(&vcdev->io_notifier); 284 285 g_free(irq_set); 286 } 287 288 static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp) 289 { 290 VFIODevice *vdev = &vcdev->vdev; 291 struct vfio_region_info *info; 292 int ret; 293 294 /* Sanity check device */ 295 if (!(vdev->flags & VFIO_DEVICE_FLAGS_CCW)) { 296 error_setg(errp, "vfio: Um, this isn't a vfio-ccw device"); 297 return; 298 } 299 300 if (vdev->num_regions < VFIO_CCW_CONFIG_REGION_INDEX + 1) { 301 error_setg(errp, "vfio: Unexpected number of the I/O region %u", 302 vdev->num_regions); 303 return; 304 } 305 306 ret = vfio_get_region_info(vdev, VFIO_CCW_CONFIG_REGION_INDEX, &info); 307 if (ret) { 308 error_setg_errno(errp, -ret, "vfio: Error getting config info"); 309 return; 310 } 311 312 vcdev->io_region_size = info->size; 313 if (sizeof(*vcdev->io_region) != vcdev->io_region_size) { 314 error_setg(errp, "vfio: Unexpected size of the I/O region"); 315 g_free(info); 316 return; 317 } 318 319 vcdev->io_region_offset = info->offset; 320 vcdev->io_region = g_malloc0(info->size); 321 322 g_free(info); 323 } 324 325 static void vfio_ccw_put_region(VFIOCCWDevice *vcdev) 326 { 327 g_free(vcdev->io_region); 328 } 329 330 static void vfio_ccw_put_device(VFIOCCWDevice *vcdev) 331 { 332 g_free(vcdev->vdev.name); 333 vfio_put_base_device(&vcdev->vdev); 334 } 335 336 static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev, 337 Error **errp) 338 { 339 char *name = g_strdup_printf("%x.%x.%04x", vcdev->cdev.hostid.cssid, 340 vcdev->cdev.hostid.ssid, 341 vcdev->cdev.hostid.devid); 342 VFIODevice *vbasedev; 343 344 QLIST_FOREACH(vbasedev, &group->device_list, next) { 345 if (strcmp(vbasedev->name, name) == 0) { 346 error_setg(errp, "vfio: subchannel %s has already been attached", 347 name); 348 goto out_err; 349 } 350 } 351 352 if (vfio_get_device(group, vcdev->cdev.mdevid, &vcdev->vdev, errp)) { 353 goto out_err; 354 } 355 356 vcdev->vdev.ops = &vfio_ccw_ops; 357 vcdev->vdev.type = VFIO_DEVICE_TYPE_CCW; 358 vcdev->vdev.name = name; 359 vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj; 360 361 return; 362 363 out_err: 364 g_free(name); 365 } 366 367 static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp) 368 { 369 char *tmp, group_path[PATH_MAX]; 370 ssize_t len; 371 int groupid; 372 373 tmp = g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group", 374 cdev->hostid.cssid, cdev->hostid.ssid, 375 cdev->hostid.devid, cdev->mdevid); 376 len = readlink(tmp, group_path, sizeof(group_path)); 377 g_free(tmp); 378 379 if (len <= 0 || len >= sizeof(group_path)) { 380 error_setg(errp, "vfio: no iommu_group found"); 381 return NULL; 382 } 383 384 group_path[len] = 0; 385 386 if (sscanf(basename(group_path), "%d", &groupid) != 1) { 387 error_setg(errp, "vfio: failed to read %s", group_path); 388 return NULL; 389 } 390 391 return vfio_get_group(groupid, &address_space_memory, errp); 392 } 393 394 static void vfio_ccw_realize(DeviceState *dev, Error **errp) 395 { 396 VFIOGroup *group; 397 CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev); 398 S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev); 399 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); 400 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev); 401 Error *err = NULL; 402 403 /* Call the class init function for subchannel. */ 404 if (cdc->realize) { 405 cdc->realize(cdev, vcdev->vdev.sysfsdev, &err); 406 if (err) { 407 goto out_err_propagate; 408 } 409 } 410 411 group = vfio_ccw_get_group(cdev, &err); 412 if (!group) { 413 goto out_group_err; 414 } 415 416 vfio_ccw_get_device(group, vcdev, &err); 417 if (err) { 418 goto out_device_err; 419 } 420 421 vfio_ccw_get_region(vcdev, &err); 422 if (err) { 423 goto out_region_err; 424 } 425 426 vfio_ccw_register_io_notifier(vcdev, &err); 427 if (err) { 428 goto out_notifier_err; 429 } 430 431 return; 432 433 out_notifier_err: 434 vfio_ccw_put_region(vcdev); 435 out_region_err: 436 vfio_ccw_put_device(vcdev); 437 out_device_err: 438 vfio_put_group(group); 439 out_group_err: 440 if (cdc->unrealize) { 441 cdc->unrealize(cdev, NULL); 442 } 443 out_err_propagate: 444 error_propagate(errp, err); 445 } 446 447 static void vfio_ccw_unrealize(DeviceState *dev, Error **errp) 448 { 449 CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev); 450 S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev); 451 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); 452 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev); 453 VFIOGroup *group = vcdev->vdev.group; 454 455 vfio_ccw_unregister_io_notifier(vcdev); 456 vfio_ccw_put_region(vcdev); 457 vfio_ccw_put_device(vcdev); 458 vfio_put_group(group); 459 460 if (cdc->unrealize) { 461 cdc->unrealize(cdev, errp); 462 } 463 } 464 465 static Property vfio_ccw_properties[] = { 466 DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice, vdev.sysfsdev), 467 DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice, force_orb_pfch, false), 468 DEFINE_PROP_END_OF_LIST(), 469 }; 470 471 static const VMStateDescription vfio_ccw_vmstate = { 472 .name = TYPE_VFIO_CCW, 473 .unmigratable = 1, 474 }; 475 476 static void vfio_ccw_class_init(ObjectClass *klass, void *data) 477 { 478 DeviceClass *dc = DEVICE_CLASS(klass); 479 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass); 480 481 dc->props = vfio_ccw_properties; 482 dc->vmsd = &vfio_ccw_vmstate; 483 dc->desc = "VFIO-based subchannel assignment"; 484 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 485 dc->realize = vfio_ccw_realize; 486 dc->unrealize = vfio_ccw_unrealize; 487 dc->reset = vfio_ccw_reset; 488 489 cdc->handle_request = vfio_ccw_handle_request; 490 } 491 492 static const TypeInfo vfio_ccw_info = { 493 .name = TYPE_VFIO_CCW, 494 .parent = TYPE_S390_CCW, 495 .instance_size = sizeof(VFIOCCWDevice), 496 .class_init = vfio_ccw_class_init, 497 }; 498 499 static void register_vfio_ccw_type(void) 500 { 501 type_register_static(&vfio_ccw_info); 502 } 503 504 type_init(register_vfio_ccw_type) 505