ccw.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * vfio based subchannel assignment support
  3. *
  4. * Copyright 2017 IBM Corp.
  5. * Copyright 2019 Red Hat, Inc.
  6. *
  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>
  10. * Cornelia Huck <cohuck@redhat.com>
  11. *
  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
  14. * directory.
  15. */
  16. #include "qemu/osdep.h"
  17. #include CONFIG_DEVICES /* CONFIG_IOMMUFD */
  18. #include <linux/vfio.h>
  19. #include <linux/vfio_ccw.h>
  20. #include <sys/ioctl.h>
  21. #include "qapi/error.h"
  22. #include "hw/vfio/vfio-common.h"
  23. #include "system/iommufd.h"
  24. #include "hw/s390x/s390-ccw.h"
  25. #include "hw/s390x/vfio-ccw.h"
  26. #include "hw/qdev-properties.h"
  27. #include "hw/s390x/ccw-device.h"
  28. #include "exec/address-spaces.h"
  29. #include "qemu/error-report.h"
  30. #include "qemu/main-loop.h"
  31. #include "qemu/module.h"
  32. struct VFIOCCWDevice {
  33. S390CCWDevice cdev;
  34. VFIODevice vdev;
  35. uint64_t io_region_size;
  36. uint64_t io_region_offset;
  37. struct ccw_io_region *io_region;
  38. uint64_t async_cmd_region_size;
  39. uint64_t async_cmd_region_offset;
  40. struct ccw_cmd_region *async_cmd_region;
  41. uint64_t schib_region_size;
  42. uint64_t schib_region_offset;
  43. struct ccw_schib_region *schib_region;
  44. uint64_t crw_region_size;
  45. uint64_t crw_region_offset;
  46. struct ccw_crw_region *crw_region;
  47. EventNotifier io_notifier;
  48. EventNotifier crw_notifier;
  49. EventNotifier req_notifier;
  50. bool force_orb_pfch;
  51. };
  52. static void vfio_ccw_compute_needs_reset(VFIODevice *vdev)
  53. {
  54. vdev->needs_reset = false;
  55. }
  56. /*
  57. * We don't need vfio_hot_reset_multi and vfio_eoi operations for
  58. * vfio_ccw device now.
  59. */
  60. struct VFIODeviceOps vfio_ccw_ops = {
  61. .vfio_compute_needs_reset = vfio_ccw_compute_needs_reset,
  62. };
  63. static IOInstEnding vfio_ccw_handle_request(SubchDev *sch)
  64. {
  65. VFIOCCWDevice *vcdev = VFIO_CCW(sch->driver_data);
  66. struct ccw_io_region *region = vcdev->io_region;
  67. int ret;
  68. if (!(sch->orb.ctrl0 & ORB_CTRL0_MASK_PFCH) && vcdev->force_orb_pfch) {
  69. sch->orb.ctrl0 |= ORB_CTRL0_MASK_PFCH;
  70. warn_report_once("vfio-ccw (devno %x.%x.%04x): PFCH flag forced",
  71. sch->cssid, sch->ssid, sch->devno);
  72. }
  73. QEMU_BUILD_BUG_ON(sizeof(region->orb_area) != sizeof(ORB));
  74. QEMU_BUILD_BUG_ON(sizeof(region->scsw_area) != sizeof(SCSW));
  75. QEMU_BUILD_BUG_ON(sizeof(region->irb_area) != sizeof(IRB));
  76. memset(region, 0, sizeof(*region));
  77. memcpy(region->orb_area, &sch->orb, sizeof(ORB));
  78. memcpy(region->scsw_area, &sch->curr_status.scsw, sizeof(SCSW));
  79. again:
  80. ret = pwrite(vcdev->vdev.fd, region,
  81. vcdev->io_region_size, vcdev->io_region_offset);
  82. if (ret != vcdev->io_region_size) {
  83. if (errno == EAGAIN) {
  84. goto again;
  85. }
  86. error_report("vfio-ccw: write I/O region failed with errno=%d", errno);
  87. ret = errno ? -errno : -EFAULT;
  88. } else {
  89. ret = 0;
  90. }
  91. switch (ret) {
  92. case 0:
  93. return IOINST_CC_EXPECTED;
  94. case -EBUSY:
  95. return IOINST_CC_BUSY;
  96. case -ENODEV:
  97. case -EACCES:
  98. return IOINST_CC_NOT_OPERATIONAL;
  99. case -EFAULT:
  100. default:
  101. sch_gen_unit_exception(sch);
  102. css_inject_io_interrupt(sch);
  103. return IOINST_CC_EXPECTED;
  104. }
  105. }
  106. static IOInstEnding vfio_ccw_handle_store(SubchDev *sch)
  107. {
  108. VFIOCCWDevice *vcdev = VFIO_CCW(sch->driver_data);
  109. SCHIB *schib = &sch->curr_status;
  110. struct ccw_schib_region *region = vcdev->schib_region;
  111. SCHIB *s;
  112. int ret;
  113. /* schib region not available so nothing else to do */
  114. if (!region) {
  115. return IOINST_CC_EXPECTED;
  116. }
  117. memset(region, 0, sizeof(*region));
  118. ret = pread(vcdev->vdev.fd, region, vcdev->schib_region_size,
  119. vcdev->schib_region_offset);
  120. if (ret == -1) {
  121. /*
  122. * Device is probably damaged, but store subchannel does not
  123. * have a nonzero cc defined for this scenario. Log an error,
  124. * and presume things are otherwise fine.
  125. */
  126. error_report("vfio-ccw: store region read failed with errno=%d", errno);
  127. return IOINST_CC_EXPECTED;
  128. }
  129. /*
  130. * Selectively copy path-related bits of the SCHIB,
  131. * rather than copying the entire struct.
  132. */
  133. s = (SCHIB *)region->schib_area;
  134. schib->pmcw.pnom = s->pmcw.pnom;
  135. schib->pmcw.lpum = s->pmcw.lpum;
  136. schib->pmcw.pam = s->pmcw.pam;
  137. schib->pmcw.pom = s->pmcw.pom;
  138. if (s->scsw.flags & SCSW_FLAGS_MASK_PNO) {
  139. schib->scsw.flags |= SCSW_FLAGS_MASK_PNO;
  140. }
  141. return IOINST_CC_EXPECTED;
  142. }
  143. static int vfio_ccw_handle_clear(SubchDev *sch)
  144. {
  145. VFIOCCWDevice *vcdev = VFIO_CCW(sch->driver_data);
  146. struct ccw_cmd_region *region = vcdev->async_cmd_region;
  147. int ret;
  148. if (!vcdev->async_cmd_region) {
  149. /* Async command region not available, fall back to emulation */
  150. return -ENOSYS;
  151. }
  152. memset(region, 0, sizeof(*region));
  153. region->command = VFIO_CCW_ASYNC_CMD_CSCH;
  154. again:
  155. ret = pwrite(vcdev->vdev.fd, region,
  156. vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset);
  157. if (ret != vcdev->async_cmd_region_size) {
  158. if (errno == EAGAIN) {
  159. goto again;
  160. }
  161. error_report("vfio-ccw: write cmd region failed with errno=%d", errno);
  162. ret = errno ? -errno : -EFAULT;
  163. } else {
  164. ret = 0;
  165. }
  166. switch (ret) {
  167. case 0:
  168. case -ENODEV:
  169. case -EACCES:
  170. return ret;
  171. case -EFAULT:
  172. default:
  173. sch_gen_unit_exception(sch);
  174. css_inject_io_interrupt(sch);
  175. return 0;
  176. }
  177. }
  178. static int vfio_ccw_handle_halt(SubchDev *sch)
  179. {
  180. VFIOCCWDevice *vcdev = VFIO_CCW(sch->driver_data);
  181. struct ccw_cmd_region *region = vcdev->async_cmd_region;
  182. int ret;
  183. if (!vcdev->async_cmd_region) {
  184. /* Async command region not available, fall back to emulation */
  185. return -ENOSYS;
  186. }
  187. memset(region, 0, sizeof(*region));
  188. region->command = VFIO_CCW_ASYNC_CMD_HSCH;
  189. again:
  190. ret = pwrite(vcdev->vdev.fd, region,
  191. vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset);
  192. if (ret != vcdev->async_cmd_region_size) {
  193. if (errno == EAGAIN) {
  194. goto again;
  195. }
  196. error_report("vfio-ccw: write cmd region failed with errno=%d", errno);
  197. ret = errno ? -errno : -EFAULT;
  198. } else {
  199. ret = 0;
  200. }
  201. switch (ret) {
  202. case 0:
  203. case -EBUSY:
  204. case -ENODEV:
  205. case -EACCES:
  206. return ret;
  207. case -EFAULT:
  208. default:
  209. sch_gen_unit_exception(sch);
  210. css_inject_io_interrupt(sch);
  211. return 0;
  212. }
  213. }
  214. static void vfio_ccw_reset(DeviceState *dev)
  215. {
  216. VFIOCCWDevice *vcdev = VFIO_CCW(dev);
  217. ioctl(vcdev->vdev.fd, VFIO_DEVICE_RESET);
  218. }
  219. static void vfio_ccw_crw_read(VFIOCCWDevice *vcdev)
  220. {
  221. struct ccw_crw_region *region = vcdev->crw_region;
  222. CRW crw;
  223. int size;
  224. /* Keep reading CRWs as long as data is returned */
  225. do {
  226. memset(region, 0, sizeof(*region));
  227. size = pread(vcdev->vdev.fd, region, vcdev->crw_region_size,
  228. vcdev->crw_region_offset);
  229. if (size == -1) {
  230. error_report("vfio-ccw: Read crw region failed with errno=%d",
  231. errno);
  232. break;
  233. }
  234. if (region->crw == 0) {
  235. /* No more CRWs to queue */
  236. break;
  237. }
  238. memcpy(&crw, &region->crw, sizeof(CRW));
  239. css_crw_add_to_queue(crw);
  240. } while (1);
  241. }
  242. static void vfio_ccw_req_notifier_handler(void *opaque)
  243. {
  244. VFIOCCWDevice *vcdev = opaque;
  245. Error *err = NULL;
  246. if (!event_notifier_test_and_clear(&vcdev->req_notifier)) {
  247. return;
  248. }
  249. qdev_unplug(DEVICE(vcdev), &err);
  250. if (err) {
  251. warn_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name);
  252. }
  253. }
  254. static void vfio_ccw_crw_notifier_handler(void *opaque)
  255. {
  256. VFIOCCWDevice *vcdev = opaque;
  257. while (event_notifier_test_and_clear(&vcdev->crw_notifier)) {
  258. vfio_ccw_crw_read(vcdev);
  259. }
  260. }
  261. static void vfio_ccw_io_notifier_handler(void *opaque)
  262. {
  263. VFIOCCWDevice *vcdev = opaque;
  264. struct ccw_io_region *region = vcdev->io_region;
  265. CcwDevice *ccw_dev = CCW_DEVICE(vcdev);
  266. SubchDev *sch = ccw_dev->sch;
  267. SCHIB *schib = &sch->curr_status;
  268. SCSW s;
  269. IRB irb;
  270. ESW esw;
  271. int size;
  272. if (!event_notifier_test_and_clear(&vcdev->io_notifier)) {
  273. return;
  274. }
  275. size = pread(vcdev->vdev.fd, region, vcdev->io_region_size,
  276. vcdev->io_region_offset);
  277. if (size == -1) {
  278. switch (errno) {
  279. case ENODEV:
  280. /* Generate a deferred cc 3 condition. */
  281. schib->scsw.flags |= SCSW_FLAGS_MASK_CC;
  282. schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
  283. schib->scsw.ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND);
  284. goto read_err;
  285. case EFAULT:
  286. /* Memory problem, generate channel data check. */
  287. schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
  288. schib->scsw.cstat = SCSW_CSTAT_DATA_CHECK;
  289. schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
  290. schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
  291. SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
  292. goto read_err;
  293. default:
  294. /* Error, generate channel program check. */
  295. schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
  296. schib->scsw.cstat = SCSW_CSTAT_PROG_CHECK;
  297. schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
  298. schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
  299. SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
  300. goto read_err;
  301. }
  302. } else if (size != vcdev->io_region_size) {
  303. /* Information transfer error, generate channel-control check. */
  304. schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
  305. schib->scsw.cstat = SCSW_CSTAT_CHN_CTRL_CHK;
  306. schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
  307. schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
  308. SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
  309. goto read_err;
  310. }
  311. memcpy(&irb, region->irb_area, sizeof(IRB));
  312. /* Update control block via irb. */
  313. s = schib->scsw;
  314. copy_scsw_to_guest(&s, &irb.scsw);
  315. schib->scsw = s;
  316. copy_esw_to_guest(&esw, &irb.esw);
  317. sch->esw = esw;
  318. /* If a uint check is pending, copy sense data. */
  319. if ((schib->scsw.dstat & SCSW_DSTAT_UNIT_CHECK) &&
  320. (schib->pmcw.chars & PMCW_CHARS_MASK_CSENSE)) {
  321. memcpy(sch->sense_data, irb.ecw, sizeof(irb.ecw));
  322. }
  323. read_err:
  324. css_inject_io_interrupt(sch);
  325. }
  326. static bool vfio_ccw_register_irq_notifier(VFIOCCWDevice *vcdev,
  327. unsigned int irq,
  328. Error **errp)
  329. {
  330. VFIODevice *vdev = &vcdev->vdev;
  331. g_autofree struct vfio_irq_info *irq_info = NULL;
  332. size_t argsz;
  333. int fd;
  334. EventNotifier *notifier;
  335. IOHandler *fd_read;
  336. switch (irq) {
  337. case VFIO_CCW_IO_IRQ_INDEX:
  338. notifier = &vcdev->io_notifier;
  339. fd_read = vfio_ccw_io_notifier_handler;
  340. break;
  341. case VFIO_CCW_CRW_IRQ_INDEX:
  342. notifier = &vcdev->crw_notifier;
  343. fd_read = vfio_ccw_crw_notifier_handler;
  344. break;
  345. case VFIO_CCW_REQ_IRQ_INDEX:
  346. notifier = &vcdev->req_notifier;
  347. fd_read = vfio_ccw_req_notifier_handler;
  348. break;
  349. default:
  350. error_setg(errp, "vfio: Unsupported device irq(%d)", irq);
  351. return false;
  352. }
  353. if (vdev->num_irqs < irq + 1) {
  354. error_setg(errp, "vfio: IRQ %u not available (number of irqs %u)",
  355. irq, vdev->num_irqs);
  356. return false;
  357. }
  358. argsz = sizeof(*irq_info);
  359. irq_info = g_malloc0(argsz);
  360. irq_info->index = irq;
  361. irq_info->argsz = argsz;
  362. if (ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO,
  363. irq_info) < 0 || irq_info->count < 1) {
  364. error_setg_errno(errp, errno, "vfio: Error getting irq info");
  365. return false;
  366. }
  367. if (event_notifier_init(notifier, 0)) {
  368. error_setg_errno(errp, errno,
  369. "vfio: Unable to init event notifier for irq (%d)",
  370. irq);
  371. return false;
  372. }
  373. fd = event_notifier_get_fd(notifier);
  374. qemu_set_fd_handler(fd, fd_read, NULL, vcdev);
  375. if (!vfio_set_irq_signaling(vdev, irq, 0,
  376. VFIO_IRQ_SET_ACTION_TRIGGER, fd, errp)) {
  377. qemu_set_fd_handler(fd, NULL, NULL, vcdev);
  378. event_notifier_cleanup(notifier);
  379. }
  380. return true;
  381. }
  382. static void vfio_ccw_unregister_irq_notifier(VFIOCCWDevice *vcdev,
  383. unsigned int irq)
  384. {
  385. Error *err = NULL;
  386. EventNotifier *notifier;
  387. switch (irq) {
  388. case VFIO_CCW_IO_IRQ_INDEX:
  389. notifier = &vcdev->io_notifier;
  390. break;
  391. case VFIO_CCW_CRW_IRQ_INDEX:
  392. notifier = &vcdev->crw_notifier;
  393. break;
  394. case VFIO_CCW_REQ_IRQ_INDEX:
  395. notifier = &vcdev->req_notifier;
  396. break;
  397. default:
  398. error_report("vfio: Unsupported device irq(%d)", irq);
  399. return;
  400. }
  401. if (!vfio_set_irq_signaling(&vcdev->vdev, irq, 0,
  402. VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) {
  403. warn_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name);
  404. }
  405. qemu_set_fd_handler(event_notifier_get_fd(notifier),
  406. NULL, NULL, vcdev);
  407. event_notifier_cleanup(notifier);
  408. }
  409. static bool vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp)
  410. {
  411. VFIODevice *vdev = &vcdev->vdev;
  412. struct vfio_region_info *info;
  413. int ret;
  414. /* Sanity check device */
  415. if (!(vdev->flags & VFIO_DEVICE_FLAGS_CCW)) {
  416. error_setg(errp, "vfio: Um, this isn't a vfio-ccw device");
  417. return false;
  418. }
  419. /*
  420. * We always expect at least the I/O region to be present. We also
  421. * may have a variable number of regions governed by capabilities.
  422. */
  423. if (vdev->num_regions < VFIO_CCW_CONFIG_REGION_INDEX + 1) {
  424. error_setg(errp, "vfio: too few regions (%u), expected at least %u",
  425. vdev->num_regions, VFIO_CCW_CONFIG_REGION_INDEX + 1);
  426. return false;
  427. }
  428. ret = vfio_get_region_info(vdev, VFIO_CCW_CONFIG_REGION_INDEX, &info);
  429. if (ret) {
  430. error_setg_errno(errp, -ret, "vfio: Error getting config info");
  431. return false;
  432. }
  433. vcdev->io_region_size = info->size;
  434. if (sizeof(*vcdev->io_region) != vcdev->io_region_size) {
  435. error_setg(errp, "vfio: Unexpected size of the I/O region");
  436. goto out_err;
  437. }
  438. vcdev->io_region_offset = info->offset;
  439. vcdev->io_region = g_malloc0(info->size);
  440. g_free(info);
  441. /* check for the optional async command region */
  442. ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW,
  443. VFIO_REGION_SUBTYPE_CCW_ASYNC_CMD, &info);
  444. if (!ret) {
  445. vcdev->async_cmd_region_size = info->size;
  446. if (sizeof(*vcdev->async_cmd_region) != vcdev->async_cmd_region_size) {
  447. error_setg(errp, "vfio: Unexpected size of the async cmd region");
  448. goto out_err;
  449. }
  450. vcdev->async_cmd_region_offset = info->offset;
  451. vcdev->async_cmd_region = g_malloc0(info->size);
  452. g_free(info);
  453. }
  454. ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW,
  455. VFIO_REGION_SUBTYPE_CCW_SCHIB, &info);
  456. if (!ret) {
  457. vcdev->schib_region_size = info->size;
  458. if (sizeof(*vcdev->schib_region) != vcdev->schib_region_size) {
  459. error_setg(errp, "vfio: Unexpected size of the schib region");
  460. goto out_err;
  461. }
  462. vcdev->schib_region_offset = info->offset;
  463. vcdev->schib_region = g_malloc(info->size);
  464. g_free(info);
  465. }
  466. ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW,
  467. VFIO_REGION_SUBTYPE_CCW_CRW, &info);
  468. if (!ret) {
  469. vcdev->crw_region_size = info->size;
  470. if (sizeof(*vcdev->crw_region) != vcdev->crw_region_size) {
  471. error_setg(errp, "vfio: Unexpected size of the CRW region");
  472. goto out_err;
  473. }
  474. vcdev->crw_region_offset = info->offset;
  475. vcdev->crw_region = g_malloc(info->size);
  476. g_free(info);
  477. }
  478. return true;
  479. out_err:
  480. g_free(vcdev->crw_region);
  481. g_free(vcdev->schib_region);
  482. g_free(vcdev->async_cmd_region);
  483. g_free(vcdev->io_region);
  484. g_free(info);
  485. return false;
  486. }
  487. static void vfio_ccw_put_region(VFIOCCWDevice *vcdev)
  488. {
  489. g_free(vcdev->crw_region);
  490. g_free(vcdev->schib_region);
  491. g_free(vcdev->async_cmd_region);
  492. g_free(vcdev->io_region);
  493. }
  494. static void vfio_ccw_realize(DeviceState *dev, Error **errp)
  495. {
  496. S390CCWDevice *cdev = S390_CCW_DEVICE(dev);
  497. VFIOCCWDevice *vcdev = VFIO_CCW(cdev);
  498. S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
  499. VFIODevice *vbasedev = &vcdev->vdev;
  500. Error *err = NULL;
  501. /* Call the class init function for subchannel. */
  502. if (cdc->realize) {
  503. if (!cdc->realize(cdev, vcdev->vdev.sysfsdev, errp)) {
  504. return;
  505. }
  506. }
  507. if (!vfio_device_get_name(vbasedev, errp)) {
  508. goto out_unrealize;
  509. }
  510. if (!vfio_attach_device(cdev->mdevid, vbasedev,
  511. &address_space_memory, errp)) {
  512. goto out_attach_dev_err;
  513. }
  514. if (!vfio_ccw_get_region(vcdev, errp)) {
  515. goto out_region_err;
  516. }
  517. if (!vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX, errp)) {
  518. goto out_io_notifier_err;
  519. }
  520. if (vcdev->crw_region) {
  521. if (!vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX,
  522. errp)) {
  523. goto out_irq_notifier_err;
  524. }
  525. }
  526. if (!vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX, &err)) {
  527. /*
  528. * Report this error, but do not make it a failing condition.
  529. * Lack of this IRQ in the host does not prevent normal operation.
  530. */
  531. warn_report_err(err);
  532. }
  533. return;
  534. out_irq_notifier_err:
  535. vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX);
  536. vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX);
  537. vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX);
  538. out_io_notifier_err:
  539. vfio_ccw_put_region(vcdev);
  540. out_region_err:
  541. vfio_detach_device(vbasedev);
  542. out_attach_dev_err:
  543. g_free(vbasedev->name);
  544. out_unrealize:
  545. if (cdc->unrealize) {
  546. cdc->unrealize(cdev);
  547. }
  548. }
  549. static void vfio_ccw_unrealize(DeviceState *dev)
  550. {
  551. S390CCWDevice *cdev = S390_CCW_DEVICE(dev);
  552. VFIOCCWDevice *vcdev = VFIO_CCW(cdev);
  553. S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
  554. vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX);
  555. vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX);
  556. vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX);
  557. vfio_ccw_put_region(vcdev);
  558. vfio_detach_device(&vcdev->vdev);
  559. g_free(vcdev->vdev.name);
  560. if (cdc->unrealize) {
  561. cdc->unrealize(cdev);
  562. }
  563. }
  564. static const Property vfio_ccw_properties[] = {
  565. DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice, vdev.sysfsdev),
  566. DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice, force_orb_pfch, false),
  567. #ifdef CONFIG_IOMMUFD
  568. DEFINE_PROP_LINK("iommufd", VFIOCCWDevice, vdev.iommufd,
  569. TYPE_IOMMUFD_BACKEND, IOMMUFDBackend *),
  570. #endif
  571. DEFINE_PROP_CCW_LOADPARM("loadparm", CcwDevice, loadparm),
  572. };
  573. static const VMStateDescription vfio_ccw_vmstate = {
  574. .name = "vfio-ccw",
  575. .unmigratable = 1,
  576. };
  577. static void vfio_ccw_instance_init(Object *obj)
  578. {
  579. VFIOCCWDevice *vcdev = VFIO_CCW(obj);
  580. VFIODevice *vbasedev = &vcdev->vdev;
  581. /* CCW device is mdev type device */
  582. vbasedev->mdev = true;
  583. /*
  584. * All vfio-ccw devices are believed to operate in a way compatible with
  585. * discarding of memory in RAM blocks, ie. pages pinned in the host are
  586. * in the current working set of the guest driver and therefore never
  587. * overlap e.g., with pages available to the guest balloon driver. This
  588. * needs to be set before vfio_get_device() for vfio common to handle
  589. * ram_block_discard_disable().
  590. */
  591. vfio_device_init(vbasedev, VFIO_DEVICE_TYPE_CCW, &vfio_ccw_ops,
  592. DEVICE(vcdev), true);
  593. }
  594. #ifdef CONFIG_IOMMUFD
  595. static void vfio_ccw_set_fd(Object *obj, const char *str, Error **errp)
  596. {
  597. vfio_device_set_fd(&VFIO_CCW(obj)->vdev, str, errp);
  598. }
  599. #endif
  600. static void vfio_ccw_class_init(ObjectClass *klass, void *data)
  601. {
  602. DeviceClass *dc = DEVICE_CLASS(klass);
  603. S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass);
  604. device_class_set_props(dc, vfio_ccw_properties);
  605. #ifdef CONFIG_IOMMUFD
  606. object_class_property_add_str(klass, "fd", NULL, vfio_ccw_set_fd);
  607. #endif
  608. dc->vmsd = &vfio_ccw_vmstate;
  609. dc->desc = "VFIO-based subchannel assignment";
  610. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  611. dc->realize = vfio_ccw_realize;
  612. dc->unrealize = vfio_ccw_unrealize;
  613. device_class_set_legacy_reset(dc, vfio_ccw_reset);
  614. cdc->handle_request = vfio_ccw_handle_request;
  615. cdc->handle_halt = vfio_ccw_handle_halt;
  616. cdc->handle_clear = vfio_ccw_handle_clear;
  617. cdc->handle_store = vfio_ccw_handle_store;
  618. object_class_property_set_description(klass, /* 2.10 */
  619. "sysfsdev",
  620. "Host sysfs path of assigned device");
  621. object_class_property_set_description(klass, /* 3.0 */
  622. "force-orb-pfch",
  623. "Force unlimited prefetch");
  624. #ifdef CONFIG_IOMMUFD
  625. object_class_property_set_description(klass, /* 9.0 */
  626. "iommufd",
  627. "Set host IOMMUFD backend device");
  628. #endif
  629. object_class_property_set_description(klass, /* 9.2 */
  630. "loadparm",
  631. "Define which devices that can be used for booting");
  632. }
  633. static const TypeInfo vfio_ccw_info = {
  634. .name = TYPE_VFIO_CCW,
  635. .parent = TYPE_S390_CCW,
  636. .instance_size = sizeof(VFIOCCWDevice),
  637. .instance_init = vfio_ccw_instance_init,
  638. .class_init = vfio_ccw_class_init,
  639. };
  640. static void register_vfio_ccw_type(void)
  641. {
  642. type_register_static(&vfio_ccw_info);
  643. }
  644. type_init(register_vfio_ccw_type)