vfio_ccw_async.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Async I/O region for vfio_ccw
  4. *
  5. * Copyright Red Hat, Inc. 2019
  6. *
  7. * Author(s): Cornelia Huck <[email protected]>
  8. */
  9. #include <linux/vfio.h>
  10. #include "vfio_ccw_private.h"
  11. static ssize_t vfio_ccw_async_region_read(struct vfio_ccw_private *private,
  12. char __user *buf, size_t count,
  13. loff_t *ppos)
  14. {
  15. unsigned int i = VFIO_CCW_OFFSET_TO_INDEX(*ppos) - VFIO_CCW_NUM_REGIONS;
  16. loff_t pos = *ppos & VFIO_CCW_OFFSET_MASK;
  17. struct ccw_cmd_region *region;
  18. int ret;
  19. if (pos + count > sizeof(*region))
  20. return -EINVAL;
  21. mutex_lock(&private->io_mutex);
  22. region = private->region[i].data;
  23. if (copy_to_user(buf, (void *)region + pos, count))
  24. ret = -EFAULT;
  25. else
  26. ret = count;
  27. mutex_unlock(&private->io_mutex);
  28. return ret;
  29. }
  30. static ssize_t vfio_ccw_async_region_write(struct vfio_ccw_private *private,
  31. const char __user *buf, size_t count,
  32. loff_t *ppos)
  33. {
  34. unsigned int i = VFIO_CCW_OFFSET_TO_INDEX(*ppos) - VFIO_CCW_NUM_REGIONS;
  35. loff_t pos = *ppos & VFIO_CCW_OFFSET_MASK;
  36. struct ccw_cmd_region *region;
  37. int ret;
  38. if (pos + count > sizeof(*region))
  39. return -EINVAL;
  40. if (!mutex_trylock(&private->io_mutex))
  41. return -EAGAIN;
  42. region = private->region[i].data;
  43. if (copy_from_user((void *)region + pos, buf, count)) {
  44. ret = -EFAULT;
  45. goto out_unlock;
  46. }
  47. vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_ASYNC_REQ);
  48. ret = region->ret_code ? region->ret_code : count;
  49. out_unlock:
  50. mutex_unlock(&private->io_mutex);
  51. return ret;
  52. }
  53. static void vfio_ccw_async_region_release(struct vfio_ccw_private *private,
  54. struct vfio_ccw_region *region)
  55. {
  56. }
  57. static const struct vfio_ccw_regops vfio_ccw_async_region_ops = {
  58. .read = vfio_ccw_async_region_read,
  59. .write = vfio_ccw_async_region_write,
  60. .release = vfio_ccw_async_region_release,
  61. };
  62. int vfio_ccw_register_async_dev_regions(struct vfio_ccw_private *private)
  63. {
  64. return vfio_ccw_register_dev_region(private,
  65. VFIO_REGION_SUBTYPE_CCW_ASYNC_CMD,
  66. &vfio_ccw_async_region_ops,
  67. sizeof(struct ccw_cmd_region),
  68. VFIO_REGION_INFO_FLAG_READ |
  69. VFIO_REGION_INFO_FLAG_WRITE,
  70. private->cmd_region);
  71. }