vfio_ccw_ops.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Physical device callbacks for vfio_ccw
  3. *
  4. * Copyright IBM Corp. 2017
  5. *
  6. * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
  7. * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
  8. */
  9. #include <linux/vfio.h>
  10. #include <linux/mdev.h>
  11. #include "vfio_ccw_private.h"
  12. static int vfio_ccw_mdev_reset(struct mdev_device *mdev)
  13. {
  14. struct vfio_ccw_private *private;
  15. struct subchannel *sch;
  16. int ret;
  17. private = dev_get_drvdata(mdev_parent_dev(mdev));
  18. sch = private->sch;
  19. /*
  20. * TODO:
  21. * In the cureent stage, some things like "no I/O running" and "no
  22. * interrupt pending" are clear, but we are not sure what other state
  23. * we need to care about.
  24. * There are still a lot more instructions need to be handled. We
  25. * should come back here later.
  26. */
  27. ret = vfio_ccw_sch_quiesce(sch);
  28. if (ret)
  29. return ret;
  30. ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
  31. if (!ret)
  32. private->state = VFIO_CCW_STATE_IDLE;
  33. return ret;
  34. }
  35. static int vfio_ccw_mdev_notifier(struct notifier_block *nb,
  36. unsigned long action,
  37. void *data)
  38. {
  39. struct vfio_ccw_private *private =
  40. container_of(nb, struct vfio_ccw_private, nb);
  41. /*
  42. * Vendor drivers MUST unpin pages in response to an
  43. * invalidation.
  44. */
  45. if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
  46. struct vfio_iommu_type1_dma_unmap *unmap = data;
  47. if (!cp_iova_pinned(&private->cp, unmap->iova))
  48. return NOTIFY_OK;
  49. if (vfio_ccw_mdev_reset(private->mdev))
  50. return NOTIFY_BAD;
  51. cp_free(&private->cp);
  52. return NOTIFY_OK;
  53. }
  54. return NOTIFY_DONE;
  55. }
  56. static ssize_t name_show(struct kobject *kobj, struct device *dev, char *buf)
  57. {
  58. return sprintf(buf, "I/O subchannel (Non-QDIO)\n");
  59. }
  60. MDEV_TYPE_ATTR_RO(name);
  61. static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
  62. char *buf)
  63. {
  64. return sprintf(buf, "%s\n", VFIO_DEVICE_API_CCW_STRING);
  65. }
  66. MDEV_TYPE_ATTR_RO(device_api);
  67. static ssize_t available_instances_show(struct kobject *kobj,
  68. struct device *dev, char *buf)
  69. {
  70. struct vfio_ccw_private *private = dev_get_drvdata(dev);
  71. return sprintf(buf, "%d\n", atomic_read(&private->avail));
  72. }
  73. MDEV_TYPE_ATTR_RO(available_instances);
  74. static struct attribute *mdev_types_attrs[] = {
  75. &mdev_type_attr_name.attr,
  76. &mdev_type_attr_device_api.attr,
  77. &mdev_type_attr_available_instances.attr,
  78. NULL,
  79. };
  80. static struct attribute_group mdev_type_group = {
  81. .name = "io",
  82. .attrs = mdev_types_attrs,
  83. };
  84. struct attribute_group *mdev_type_groups[] = {
  85. &mdev_type_group,
  86. NULL,
  87. };
  88. static int vfio_ccw_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
  89. {
  90. struct vfio_ccw_private *private =
  91. dev_get_drvdata(mdev_parent_dev(mdev));
  92. if (private->state == VFIO_CCW_STATE_NOT_OPER)
  93. return -ENODEV;
  94. if (atomic_dec_if_positive(&private->avail) < 0)
  95. return -EPERM;
  96. private->mdev = mdev;
  97. private->state = VFIO_CCW_STATE_IDLE;
  98. return 0;
  99. }
  100. static int vfio_ccw_mdev_remove(struct mdev_device *mdev)
  101. {
  102. struct vfio_ccw_private *private =
  103. dev_get_drvdata(mdev_parent_dev(mdev));
  104. if ((private->state != VFIO_CCW_STATE_NOT_OPER) &&
  105. (private->state != VFIO_CCW_STATE_STANDBY)) {
  106. if (!vfio_ccw_mdev_reset(mdev))
  107. private->state = VFIO_CCW_STATE_STANDBY;
  108. /* The state will be NOT_OPER on error. */
  109. }
  110. private->mdev = NULL;
  111. atomic_inc(&private->avail);
  112. return 0;
  113. }
  114. static int vfio_ccw_mdev_open(struct mdev_device *mdev)
  115. {
  116. struct vfio_ccw_private *private =
  117. dev_get_drvdata(mdev_parent_dev(mdev));
  118. unsigned long events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
  119. private->nb.notifier_call = vfio_ccw_mdev_notifier;
  120. return vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
  121. &events, &private->nb);
  122. }
  123. void vfio_ccw_mdev_release(struct mdev_device *mdev)
  124. {
  125. struct vfio_ccw_private *private =
  126. dev_get_drvdata(mdev_parent_dev(mdev));
  127. vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
  128. &private->nb);
  129. }
  130. static ssize_t vfio_ccw_mdev_read(struct mdev_device *mdev,
  131. char __user *buf,
  132. size_t count,
  133. loff_t *ppos)
  134. {
  135. struct vfio_ccw_private *private;
  136. struct ccw_io_region *region;
  137. if (*ppos + count > sizeof(*region))
  138. return -EINVAL;
  139. private = dev_get_drvdata(mdev_parent_dev(mdev));
  140. region = &private->io_region;
  141. if (copy_to_user(buf, (void *)region + *ppos, count))
  142. return -EFAULT;
  143. return count;
  144. }
  145. static ssize_t vfio_ccw_mdev_write(struct mdev_device *mdev,
  146. const char __user *buf,
  147. size_t count,
  148. loff_t *ppos)
  149. {
  150. struct vfio_ccw_private *private;
  151. struct ccw_io_region *region;
  152. if (*ppos + count > sizeof(*region))
  153. return -EINVAL;
  154. private = dev_get_drvdata(mdev_parent_dev(mdev));
  155. if (private->state != VFIO_CCW_STATE_IDLE)
  156. return -EACCES;
  157. region = &private->io_region;
  158. if (copy_from_user((void *)region + *ppos, buf, count))
  159. return -EFAULT;
  160. vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_IO_REQ);
  161. if (region->ret_code != 0) {
  162. private->state = VFIO_CCW_STATE_IDLE;
  163. return region->ret_code;
  164. }
  165. return count;
  166. }
  167. static int vfio_ccw_mdev_get_device_info(struct vfio_device_info *info)
  168. {
  169. info->flags = VFIO_DEVICE_FLAGS_CCW | VFIO_DEVICE_FLAGS_RESET;
  170. info->num_regions = VFIO_CCW_NUM_REGIONS;
  171. info->num_irqs = VFIO_CCW_NUM_IRQS;
  172. return 0;
  173. }
  174. static int vfio_ccw_mdev_get_region_info(struct vfio_region_info *info,
  175. u16 *cap_type_id,
  176. void **cap_type)
  177. {
  178. switch (info->index) {
  179. case VFIO_CCW_CONFIG_REGION_INDEX:
  180. info->offset = 0;
  181. info->size = sizeof(struct ccw_io_region);
  182. info->flags = VFIO_REGION_INFO_FLAG_READ
  183. | VFIO_REGION_INFO_FLAG_WRITE;
  184. return 0;
  185. default:
  186. return -EINVAL;
  187. }
  188. }
  189. int vfio_ccw_mdev_get_irq_info(struct vfio_irq_info *info)
  190. {
  191. if (info->index != VFIO_CCW_IO_IRQ_INDEX)
  192. return -EINVAL;
  193. info->count = 1;
  194. info->flags = VFIO_IRQ_INFO_EVENTFD;
  195. return 0;
  196. }
  197. static int vfio_ccw_mdev_set_irqs(struct mdev_device *mdev,
  198. uint32_t flags,
  199. void __user *data)
  200. {
  201. struct vfio_ccw_private *private;
  202. struct eventfd_ctx **ctx;
  203. if (!(flags & VFIO_IRQ_SET_ACTION_TRIGGER))
  204. return -EINVAL;
  205. private = dev_get_drvdata(mdev_parent_dev(mdev));
  206. ctx = &private->io_trigger;
  207. switch (flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
  208. case VFIO_IRQ_SET_DATA_NONE:
  209. {
  210. if (*ctx)
  211. eventfd_signal(*ctx, 1);
  212. return 0;
  213. }
  214. case VFIO_IRQ_SET_DATA_BOOL:
  215. {
  216. uint8_t trigger;
  217. if (get_user(trigger, (uint8_t __user *)data))
  218. return -EFAULT;
  219. if (trigger && *ctx)
  220. eventfd_signal(*ctx, 1);
  221. return 0;
  222. }
  223. case VFIO_IRQ_SET_DATA_EVENTFD:
  224. {
  225. int32_t fd;
  226. if (get_user(fd, (int32_t __user *)data))
  227. return -EFAULT;
  228. if (fd == -1) {
  229. if (*ctx)
  230. eventfd_ctx_put(*ctx);
  231. *ctx = NULL;
  232. } else if (fd >= 0) {
  233. struct eventfd_ctx *efdctx;
  234. efdctx = eventfd_ctx_fdget(fd);
  235. if (IS_ERR(efdctx))
  236. return PTR_ERR(efdctx);
  237. if (*ctx)
  238. eventfd_ctx_put(*ctx);
  239. *ctx = efdctx;
  240. } else
  241. return -EINVAL;
  242. return 0;
  243. }
  244. default:
  245. return -EINVAL;
  246. }
  247. }
  248. static ssize_t vfio_ccw_mdev_ioctl(struct mdev_device *mdev,
  249. unsigned int cmd,
  250. unsigned long arg)
  251. {
  252. int ret = 0;
  253. unsigned long minsz;
  254. switch (cmd) {
  255. case VFIO_DEVICE_GET_INFO:
  256. {
  257. struct vfio_device_info info;
  258. minsz = offsetofend(struct vfio_device_info, num_irqs);
  259. if (copy_from_user(&info, (void __user *)arg, minsz))
  260. return -EFAULT;
  261. if (info.argsz < minsz)
  262. return -EINVAL;
  263. ret = vfio_ccw_mdev_get_device_info(&info);
  264. if (ret)
  265. return ret;
  266. return copy_to_user((void __user *)arg, &info, minsz);
  267. }
  268. case VFIO_DEVICE_GET_REGION_INFO:
  269. {
  270. struct vfio_region_info info;
  271. u16 cap_type_id = 0;
  272. void *cap_type = NULL;
  273. minsz = offsetofend(struct vfio_region_info, offset);
  274. if (copy_from_user(&info, (void __user *)arg, minsz))
  275. return -EFAULT;
  276. if (info.argsz < minsz)
  277. return -EINVAL;
  278. ret = vfio_ccw_mdev_get_region_info(&info, &cap_type_id,
  279. &cap_type);
  280. if (ret)
  281. return ret;
  282. return copy_to_user((void __user *)arg, &info, minsz);
  283. }
  284. case VFIO_DEVICE_GET_IRQ_INFO:
  285. {
  286. struct vfio_irq_info info;
  287. minsz = offsetofend(struct vfio_irq_info, count);
  288. if (copy_from_user(&info, (void __user *)arg, minsz))
  289. return -EFAULT;
  290. if (info.argsz < minsz || info.index >= VFIO_CCW_NUM_IRQS)
  291. return -EINVAL;
  292. ret = vfio_ccw_mdev_get_irq_info(&info);
  293. if (ret)
  294. return ret;
  295. if (info.count == -1)
  296. return -EINVAL;
  297. return copy_to_user((void __user *)arg, &info, minsz);
  298. }
  299. case VFIO_DEVICE_SET_IRQS:
  300. {
  301. struct vfio_irq_set hdr;
  302. size_t data_size;
  303. void __user *data;
  304. minsz = offsetofend(struct vfio_irq_set, count);
  305. if (copy_from_user(&hdr, (void __user *)arg, minsz))
  306. return -EFAULT;
  307. ret = vfio_set_irqs_validate_and_prepare(&hdr, 1,
  308. VFIO_CCW_NUM_IRQS,
  309. &data_size);
  310. if (ret)
  311. return ret;
  312. data = (void __user *)(arg + minsz);
  313. return vfio_ccw_mdev_set_irqs(mdev, hdr.flags, data);
  314. }
  315. case VFIO_DEVICE_RESET:
  316. return vfio_ccw_mdev_reset(mdev);
  317. default:
  318. return -ENOTTY;
  319. }
  320. }
  321. static const struct mdev_parent_ops vfio_ccw_mdev_ops = {
  322. .owner = THIS_MODULE,
  323. .supported_type_groups = mdev_type_groups,
  324. .create = vfio_ccw_mdev_create,
  325. .remove = vfio_ccw_mdev_remove,
  326. .open = vfio_ccw_mdev_open,
  327. .release = vfio_ccw_mdev_release,
  328. .read = vfio_ccw_mdev_read,
  329. .write = vfio_ccw_mdev_write,
  330. .ioctl = vfio_ccw_mdev_ioctl,
  331. };
  332. int vfio_ccw_mdev_reg(struct subchannel *sch)
  333. {
  334. return mdev_register_device(&sch->dev, &vfio_ccw_mdev_ops);
  335. }
  336. void vfio_ccw_mdev_unreg(struct subchannel *sch)
  337. {
  338. mdev_unregister_device(&sch->dev);
  339. }