vfio_ccw_ops.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Physical device callbacks for vfio_ccw
  4. *
  5. * Copyright IBM Corp. 2017
  6. * Copyright Red Hat, Inc. 2019
  7. *
  8. * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
  9. * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
  10. * Cornelia Huck <cohuck@redhat.com>
  11. */
  12. #include <linux/vfio.h>
  13. #include <linux/nospec.h>
  14. #include <linux/slab.h>
  15. #include "vfio_ccw_private.h"
  16. static const struct vfio_device_ops vfio_ccw_dev_ops;
  17. static int vfio_ccw_mdev_reset(struct vfio_ccw_private *private)
  18. {
  19. /*
  20. * If the FSM state is seen as Not Operational after closing
  21. * and re-opening the mdev, return an error.
  22. */
  23. vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_CLOSE);
  24. vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_OPEN);
  25. if (private->state == VFIO_CCW_STATE_NOT_OPER)
  26. return -EINVAL;
  27. return 0;
  28. }
  29. static void vfio_ccw_dma_unmap(struct vfio_device *vdev, u64 iova, u64 length)
  30. {
  31. struct vfio_ccw_private *private =
  32. container_of(vdev, struct vfio_ccw_private, vdev);
  33. /* Drivers MUST unpin pages in response to an invalidation. */
  34. if (!cp_iova_pinned(&private->cp, iova, length))
  35. return;
  36. vfio_ccw_mdev_reset(private);
  37. }
  38. static int vfio_ccw_mdev_init_dev(struct vfio_device *vdev)
  39. {
  40. struct vfio_ccw_private *private =
  41. container_of(vdev, struct vfio_ccw_private, vdev);
  42. mutex_init(&private->io_mutex);
  43. private->state = VFIO_CCW_STATE_STANDBY;
  44. INIT_LIST_HEAD(&private->crw);
  45. INIT_WORK(&private->io_work, vfio_ccw_sch_io_todo);
  46. INIT_WORK(&private->crw_work, vfio_ccw_crw_todo);
  47. private->cp.guest_cp = kcalloc(CCWCHAIN_LEN_MAX, sizeof(struct ccw1),
  48. GFP_KERNEL);
  49. if (!private->cp.guest_cp)
  50. goto out_free_private;
  51. private->io_region = kmem_cache_zalloc(vfio_ccw_io_region,
  52. GFP_KERNEL | GFP_DMA);
  53. if (!private->io_region)
  54. goto out_free_cp;
  55. private->cmd_region = kmem_cache_zalloc(vfio_ccw_cmd_region,
  56. GFP_KERNEL | GFP_DMA);
  57. if (!private->cmd_region)
  58. goto out_free_io;
  59. private->schib_region = kmem_cache_zalloc(vfio_ccw_schib_region,
  60. GFP_KERNEL | GFP_DMA);
  61. if (!private->schib_region)
  62. goto out_free_cmd;
  63. private->crw_region = kmem_cache_zalloc(vfio_ccw_crw_region,
  64. GFP_KERNEL | GFP_DMA);
  65. if (!private->crw_region)
  66. goto out_free_schib;
  67. return 0;
  68. out_free_schib:
  69. kmem_cache_free(vfio_ccw_schib_region, private->schib_region);
  70. out_free_cmd:
  71. kmem_cache_free(vfio_ccw_cmd_region, private->cmd_region);
  72. out_free_io:
  73. kmem_cache_free(vfio_ccw_io_region, private->io_region);
  74. out_free_cp:
  75. kfree(private->cp.guest_cp);
  76. out_free_private:
  77. mutex_destroy(&private->io_mutex);
  78. return -ENOMEM;
  79. }
  80. static int vfio_ccw_mdev_probe(struct mdev_device *mdev)
  81. {
  82. struct subchannel *sch = to_subchannel(mdev->dev.parent);
  83. struct vfio_ccw_parent *parent = dev_get_drvdata(&sch->dev);
  84. struct vfio_ccw_private *private;
  85. int ret;
  86. private = vfio_alloc_device(vfio_ccw_private, vdev, &mdev->dev,
  87. &vfio_ccw_dev_ops);
  88. if (IS_ERR(private))
  89. return PTR_ERR(private);
  90. dev_set_drvdata(&parent->dev, private);
  91. VFIO_CCW_MSG_EVENT(2, "sch %x.%x.%04x: create\n",
  92. sch->schid.cssid,
  93. sch->schid.ssid,
  94. sch->schid.sch_no);
  95. ret = vfio_register_emulated_iommu_dev(&private->vdev);
  96. if (ret)
  97. goto err_put_vdev;
  98. dev_set_drvdata(&mdev->dev, private);
  99. return 0;
  100. err_put_vdev:
  101. dev_set_drvdata(&parent->dev, NULL);
  102. vfio_put_device(&private->vdev);
  103. return ret;
  104. }
  105. static void vfio_ccw_mdev_release_dev(struct vfio_device *vdev)
  106. {
  107. struct vfio_ccw_private *private =
  108. container_of(vdev, struct vfio_ccw_private, vdev);
  109. struct vfio_ccw_crw *crw, *temp;
  110. list_for_each_entry_safe(crw, temp, &private->crw, next) {
  111. list_del(&crw->next);
  112. kfree(crw);
  113. }
  114. kmem_cache_free(vfio_ccw_crw_region, private->crw_region);
  115. kmem_cache_free(vfio_ccw_schib_region, private->schib_region);
  116. kmem_cache_free(vfio_ccw_cmd_region, private->cmd_region);
  117. kmem_cache_free(vfio_ccw_io_region, private->io_region);
  118. kfree(private->cp.guest_cp);
  119. mutex_destroy(&private->io_mutex);
  120. }
  121. static void vfio_ccw_mdev_remove(struct mdev_device *mdev)
  122. {
  123. struct subchannel *sch = to_subchannel(mdev->dev.parent);
  124. struct vfio_ccw_parent *parent = dev_get_drvdata(&sch->dev);
  125. struct vfio_ccw_private *private = dev_get_drvdata(&parent->dev);
  126. VFIO_CCW_MSG_EVENT(2, "sch %x.%x.%04x: remove\n",
  127. sch->schid.cssid,
  128. sch->schid.ssid,
  129. sch->schid.sch_no);
  130. vfio_unregister_group_dev(&private->vdev);
  131. dev_set_drvdata(&parent->dev, NULL);
  132. vfio_put_device(&private->vdev);
  133. }
  134. static int vfio_ccw_mdev_open_device(struct vfio_device *vdev)
  135. {
  136. struct vfio_ccw_private *private =
  137. container_of(vdev, struct vfio_ccw_private, vdev);
  138. int ret;
  139. /* Device cannot simply be opened again from this state */
  140. if (private->state == VFIO_CCW_STATE_NOT_OPER)
  141. return -EINVAL;
  142. ret = vfio_ccw_register_async_dev_regions(private);
  143. if (ret)
  144. return ret;
  145. ret = vfio_ccw_register_schib_dev_regions(private);
  146. if (ret)
  147. goto out_unregister;
  148. ret = vfio_ccw_register_crw_dev_regions(private);
  149. if (ret)
  150. goto out_unregister;
  151. vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_OPEN);
  152. if (private->state == VFIO_CCW_STATE_NOT_OPER) {
  153. ret = -EINVAL;
  154. goto out_unregister;
  155. }
  156. return ret;
  157. out_unregister:
  158. vfio_ccw_unregister_dev_regions(private);
  159. return ret;
  160. }
  161. static void vfio_ccw_mdev_close_device(struct vfio_device *vdev)
  162. {
  163. struct vfio_ccw_private *private =
  164. container_of(vdev, struct vfio_ccw_private, vdev);
  165. vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_CLOSE);
  166. vfio_ccw_unregister_dev_regions(private);
  167. }
  168. static ssize_t vfio_ccw_mdev_read_io_region(struct vfio_ccw_private *private,
  169. char __user *buf, size_t count,
  170. loff_t *ppos)
  171. {
  172. loff_t pos = *ppos & VFIO_CCW_OFFSET_MASK;
  173. struct ccw_io_region *region;
  174. int ret;
  175. if (pos + count > sizeof(*region))
  176. return -EINVAL;
  177. mutex_lock(&private->io_mutex);
  178. region = private->io_region;
  179. if (copy_to_user(buf, (void *)region + pos, count))
  180. ret = -EFAULT;
  181. else
  182. ret = count;
  183. mutex_unlock(&private->io_mutex);
  184. return ret;
  185. }
  186. static ssize_t vfio_ccw_mdev_read(struct vfio_device *vdev,
  187. char __user *buf,
  188. size_t count,
  189. loff_t *ppos)
  190. {
  191. struct vfio_ccw_private *private =
  192. container_of(vdev, struct vfio_ccw_private, vdev);
  193. unsigned int index = VFIO_CCW_OFFSET_TO_INDEX(*ppos);
  194. if (index >= VFIO_CCW_NUM_REGIONS + private->num_regions)
  195. return -EINVAL;
  196. switch (index) {
  197. case VFIO_CCW_CONFIG_REGION_INDEX:
  198. return vfio_ccw_mdev_read_io_region(private, buf, count, ppos);
  199. default:
  200. index -= VFIO_CCW_NUM_REGIONS;
  201. return private->region[index].ops->read(private, buf, count,
  202. ppos);
  203. }
  204. return -EINVAL;
  205. }
  206. static ssize_t vfio_ccw_mdev_write_io_region(struct vfio_ccw_private *private,
  207. const char __user *buf,
  208. size_t count, loff_t *ppos)
  209. {
  210. loff_t pos = *ppos & VFIO_CCW_OFFSET_MASK;
  211. struct ccw_io_region *region;
  212. int ret;
  213. if (pos + count > sizeof(*region))
  214. return -EINVAL;
  215. if (!mutex_trylock(&private->io_mutex))
  216. return -EAGAIN;
  217. region = private->io_region;
  218. if (copy_from_user((void *)region + pos, buf, count)) {
  219. ret = -EFAULT;
  220. goto out_unlock;
  221. }
  222. vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_IO_REQ);
  223. ret = (region->ret_code != 0) ? region->ret_code : count;
  224. out_unlock:
  225. mutex_unlock(&private->io_mutex);
  226. return ret;
  227. }
  228. static ssize_t vfio_ccw_mdev_write(struct vfio_device *vdev,
  229. const char __user *buf,
  230. size_t count,
  231. loff_t *ppos)
  232. {
  233. struct vfio_ccw_private *private =
  234. container_of(vdev, struct vfio_ccw_private, vdev);
  235. unsigned int index = VFIO_CCW_OFFSET_TO_INDEX(*ppos);
  236. if (index >= VFIO_CCW_NUM_REGIONS + private->num_regions)
  237. return -EINVAL;
  238. switch (index) {
  239. case VFIO_CCW_CONFIG_REGION_INDEX:
  240. return vfio_ccw_mdev_write_io_region(private, buf, count, ppos);
  241. default:
  242. index -= VFIO_CCW_NUM_REGIONS;
  243. return private->region[index].ops->write(private, buf, count,
  244. ppos);
  245. }
  246. return -EINVAL;
  247. }
  248. static int vfio_ccw_mdev_get_device_info(struct vfio_ccw_private *private,
  249. struct vfio_device_info *info)
  250. {
  251. info->flags = VFIO_DEVICE_FLAGS_CCW | VFIO_DEVICE_FLAGS_RESET;
  252. info->num_regions = VFIO_CCW_NUM_REGIONS + private->num_regions;
  253. info->num_irqs = VFIO_CCW_NUM_IRQS;
  254. return 0;
  255. }
  256. static int vfio_ccw_mdev_get_region_info(struct vfio_ccw_private *private,
  257. struct vfio_region_info *info,
  258. unsigned long arg)
  259. {
  260. int i;
  261. switch (info->index) {
  262. case VFIO_CCW_CONFIG_REGION_INDEX:
  263. info->offset = 0;
  264. info->size = sizeof(struct ccw_io_region);
  265. info->flags = VFIO_REGION_INFO_FLAG_READ
  266. | VFIO_REGION_INFO_FLAG_WRITE;
  267. return 0;
  268. default: /* all other regions are handled via capability chain */
  269. {
  270. struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
  271. struct vfio_region_info_cap_type cap_type = {
  272. .header.id = VFIO_REGION_INFO_CAP_TYPE,
  273. .header.version = 1 };
  274. int ret;
  275. if (info->index >=
  276. VFIO_CCW_NUM_REGIONS + private->num_regions)
  277. return -EINVAL;
  278. info->index = array_index_nospec(info->index,
  279. VFIO_CCW_NUM_REGIONS +
  280. private->num_regions);
  281. i = info->index - VFIO_CCW_NUM_REGIONS;
  282. info->offset = VFIO_CCW_INDEX_TO_OFFSET(info->index);
  283. info->size = private->region[i].size;
  284. info->flags = private->region[i].flags;
  285. cap_type.type = private->region[i].type;
  286. cap_type.subtype = private->region[i].subtype;
  287. ret = vfio_info_add_capability(&caps, &cap_type.header,
  288. sizeof(cap_type));
  289. if (ret)
  290. return ret;
  291. info->flags |= VFIO_REGION_INFO_FLAG_CAPS;
  292. if (info->argsz < sizeof(*info) + caps.size) {
  293. info->argsz = sizeof(*info) + caps.size;
  294. info->cap_offset = 0;
  295. } else {
  296. vfio_info_cap_shift(&caps, sizeof(*info));
  297. if (copy_to_user((void __user *)arg + sizeof(*info),
  298. caps.buf, caps.size)) {
  299. kfree(caps.buf);
  300. return -EFAULT;
  301. }
  302. info->cap_offset = sizeof(*info);
  303. }
  304. kfree(caps.buf);
  305. }
  306. }
  307. return 0;
  308. }
  309. static int vfio_ccw_mdev_get_irq_info(struct vfio_irq_info *info)
  310. {
  311. switch (info->index) {
  312. case VFIO_CCW_IO_IRQ_INDEX:
  313. case VFIO_CCW_CRW_IRQ_INDEX:
  314. case VFIO_CCW_REQ_IRQ_INDEX:
  315. info->count = 1;
  316. info->flags = VFIO_IRQ_INFO_EVENTFD;
  317. break;
  318. default:
  319. return -EINVAL;
  320. }
  321. return 0;
  322. }
  323. static int vfio_ccw_mdev_set_irqs(struct vfio_ccw_private *private,
  324. uint32_t flags,
  325. uint32_t index,
  326. void __user *data)
  327. {
  328. struct eventfd_ctx **ctx;
  329. if (!(flags & VFIO_IRQ_SET_ACTION_TRIGGER))
  330. return -EINVAL;
  331. switch (index) {
  332. case VFIO_CCW_IO_IRQ_INDEX:
  333. ctx = &private->io_trigger;
  334. break;
  335. case VFIO_CCW_CRW_IRQ_INDEX:
  336. ctx = &private->crw_trigger;
  337. break;
  338. case VFIO_CCW_REQ_IRQ_INDEX:
  339. ctx = &private->req_trigger;
  340. break;
  341. default:
  342. return -EINVAL;
  343. }
  344. switch (flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
  345. case VFIO_IRQ_SET_DATA_NONE:
  346. {
  347. if (*ctx)
  348. eventfd_signal(*ctx);
  349. return 0;
  350. }
  351. case VFIO_IRQ_SET_DATA_BOOL:
  352. {
  353. uint8_t trigger;
  354. if (get_user(trigger, (uint8_t __user *)data))
  355. return -EFAULT;
  356. if (trigger && *ctx)
  357. eventfd_signal(*ctx);
  358. return 0;
  359. }
  360. case VFIO_IRQ_SET_DATA_EVENTFD:
  361. {
  362. int32_t fd;
  363. if (get_user(fd, (int32_t __user *)data))
  364. return -EFAULT;
  365. if (fd == -1) {
  366. if (*ctx)
  367. eventfd_ctx_put(*ctx);
  368. *ctx = NULL;
  369. } else if (fd >= 0) {
  370. struct eventfd_ctx *efdctx;
  371. efdctx = eventfd_ctx_fdget(fd);
  372. if (IS_ERR(efdctx))
  373. return PTR_ERR(efdctx);
  374. if (*ctx)
  375. eventfd_ctx_put(*ctx);
  376. *ctx = efdctx;
  377. } else
  378. return -EINVAL;
  379. return 0;
  380. }
  381. default:
  382. return -EINVAL;
  383. }
  384. }
  385. int vfio_ccw_register_dev_region(struct vfio_ccw_private *private,
  386. unsigned int subtype,
  387. const struct vfio_ccw_regops *ops,
  388. size_t size, u32 flags, void *data)
  389. {
  390. struct vfio_ccw_region *region;
  391. region = krealloc(private->region,
  392. (private->num_regions + 1) * sizeof(*region),
  393. GFP_KERNEL);
  394. if (!region)
  395. return -ENOMEM;
  396. private->region = region;
  397. private->region[private->num_regions].type = VFIO_REGION_TYPE_CCW;
  398. private->region[private->num_regions].subtype = subtype;
  399. private->region[private->num_regions].ops = ops;
  400. private->region[private->num_regions].size = size;
  401. private->region[private->num_regions].flags = flags;
  402. private->region[private->num_regions].data = data;
  403. private->num_regions++;
  404. return 0;
  405. }
  406. void vfio_ccw_unregister_dev_regions(struct vfio_ccw_private *private)
  407. {
  408. int i;
  409. for (i = 0; i < private->num_regions; i++)
  410. private->region[i].ops->release(private, &private->region[i]);
  411. private->num_regions = 0;
  412. kfree(private->region);
  413. private->region = NULL;
  414. }
  415. static ssize_t vfio_ccw_mdev_ioctl(struct vfio_device *vdev,
  416. unsigned int cmd,
  417. unsigned long arg)
  418. {
  419. struct vfio_ccw_private *private =
  420. container_of(vdev, struct vfio_ccw_private, vdev);
  421. int ret = 0;
  422. unsigned long minsz;
  423. switch (cmd) {
  424. case VFIO_DEVICE_GET_INFO:
  425. {
  426. struct vfio_device_info info;
  427. minsz = offsetofend(struct vfio_device_info, num_irqs);
  428. if (copy_from_user(&info, (void __user *)arg, minsz))
  429. return -EFAULT;
  430. if (info.argsz < minsz)
  431. return -EINVAL;
  432. ret = vfio_ccw_mdev_get_device_info(private, &info);
  433. if (ret)
  434. return ret;
  435. return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
  436. }
  437. case VFIO_DEVICE_GET_REGION_INFO:
  438. {
  439. struct vfio_region_info info;
  440. minsz = offsetofend(struct vfio_region_info, offset);
  441. if (copy_from_user(&info, (void __user *)arg, minsz))
  442. return -EFAULT;
  443. if (info.argsz < minsz)
  444. return -EINVAL;
  445. ret = vfio_ccw_mdev_get_region_info(private, &info, arg);
  446. if (ret)
  447. return ret;
  448. return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
  449. }
  450. case VFIO_DEVICE_GET_IRQ_INFO:
  451. {
  452. struct vfio_irq_info info;
  453. minsz = offsetofend(struct vfio_irq_info, count);
  454. if (copy_from_user(&info, (void __user *)arg, minsz))
  455. return -EFAULT;
  456. if (info.argsz < minsz || info.index >= VFIO_CCW_NUM_IRQS)
  457. return -EINVAL;
  458. ret = vfio_ccw_mdev_get_irq_info(&info);
  459. if (ret)
  460. return ret;
  461. if (info.count == -1)
  462. return -EINVAL;
  463. return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
  464. }
  465. case VFIO_DEVICE_SET_IRQS:
  466. {
  467. struct vfio_irq_set hdr;
  468. size_t data_size;
  469. void __user *data;
  470. minsz = offsetofend(struct vfio_irq_set, count);
  471. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  472. return -EFAULT;
  473. ret = vfio_set_irqs_validate_and_prepare(&hdr, 1,
  474. VFIO_CCW_NUM_IRQS,
  475. &data_size);
  476. if (ret)
  477. return ret;
  478. data = (void __user *)(arg + minsz);
  479. return vfio_ccw_mdev_set_irqs(private, hdr.flags, hdr.index,
  480. data);
  481. }
  482. case VFIO_DEVICE_RESET:
  483. return vfio_ccw_mdev_reset(private);
  484. default:
  485. return -ENOTTY;
  486. }
  487. }
  488. /* Request removal of the device*/
  489. static void vfio_ccw_mdev_request(struct vfio_device *vdev, unsigned int count)
  490. {
  491. struct vfio_ccw_private *private =
  492. container_of(vdev, struct vfio_ccw_private, vdev);
  493. struct device *dev = vdev->dev;
  494. if (private->req_trigger) {
  495. if (!(count % 10))
  496. dev_notice_ratelimited(dev,
  497. "Relaying device request to user (#%u)\n",
  498. count);
  499. eventfd_signal(private->req_trigger);
  500. } else if (count == 0) {
  501. dev_notice(dev,
  502. "No device request channel registered, blocked until released by user\n");
  503. }
  504. }
  505. static const struct vfio_device_ops vfio_ccw_dev_ops = {
  506. .init = vfio_ccw_mdev_init_dev,
  507. .release = vfio_ccw_mdev_release_dev,
  508. .open_device = vfio_ccw_mdev_open_device,
  509. .close_device = vfio_ccw_mdev_close_device,
  510. .read = vfio_ccw_mdev_read,
  511. .write = vfio_ccw_mdev_write,
  512. .ioctl = vfio_ccw_mdev_ioctl,
  513. .request = vfio_ccw_mdev_request,
  514. .dma_unmap = vfio_ccw_dma_unmap,
  515. .bind_iommufd = vfio_iommufd_emulated_bind,
  516. .unbind_iommufd = vfio_iommufd_emulated_unbind,
  517. .attach_ioas = vfio_iommufd_emulated_attach_ioas,
  518. .detach_ioas = vfio_iommufd_emulated_detach_ioas,
  519. };
  520. struct mdev_driver vfio_ccw_mdev_driver = {
  521. .device_api = VFIO_DEVICE_API_CCW_STRING,
  522. .max_instances = 1,
  523. .driver = {
  524. .name = "vfio_ccw_mdev",
  525. .owner = THIS_MODULE,
  526. .mod_name = KBUILD_MODNAME,
  527. },
  528. .probe = vfio_ccw_mdev_probe,
  529. .remove = vfio_ccw_mdev_remove,
  530. };