vfio_ccw_drv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * VFIO based Physical Subchannel device driver
  4. *
  5. * Copyright IBM Corp. 2017
  6. * Copyright Red Hat, Inc. 2019
  7. *
  8. * Author(s): Dong Jia Shi <[email protected]>
  9. * Xiao Feng Ren <[email protected]>
  10. * Cornelia Huck <[email protected]>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/mdev.h>
  16. #include <asm/isc.h>
  17. #include "chp.h"
  18. #include "ioasm.h"
  19. #include "css.h"
  20. #include "vfio_ccw_private.h"
  21. struct workqueue_struct *vfio_ccw_work_q;
  22. static struct kmem_cache *vfio_ccw_io_region;
  23. static struct kmem_cache *vfio_ccw_cmd_region;
  24. static struct kmem_cache *vfio_ccw_schib_region;
  25. static struct kmem_cache *vfio_ccw_crw_region;
  26. debug_info_t *vfio_ccw_debug_msg_id;
  27. debug_info_t *vfio_ccw_debug_trace_id;
  28. /*
  29. * Helpers
  30. */
  31. int vfio_ccw_sch_quiesce(struct subchannel *sch)
  32. {
  33. struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
  34. DECLARE_COMPLETION_ONSTACK(completion);
  35. int iretry, ret = 0;
  36. iretry = 255;
  37. do {
  38. ret = cio_cancel_halt_clear(sch, &iretry);
  39. if (ret == -EIO) {
  40. pr_err("vfio_ccw: could not quiesce subchannel 0.%x.%04x!\n",
  41. sch->schid.ssid, sch->schid.sch_no);
  42. break;
  43. }
  44. /*
  45. * Flush all I/O and wait for
  46. * cancel/halt/clear completion.
  47. */
  48. private->completion = &completion;
  49. spin_unlock_irq(sch->lock);
  50. if (ret == -EBUSY)
  51. wait_for_completion_timeout(&completion, 3*HZ);
  52. private->completion = NULL;
  53. flush_workqueue(vfio_ccw_work_q);
  54. spin_lock_irq(sch->lock);
  55. ret = cio_disable_subchannel(sch);
  56. } while (ret == -EBUSY);
  57. return ret;
  58. }
  59. static void vfio_ccw_sch_io_todo(struct work_struct *work)
  60. {
  61. struct vfio_ccw_private *private;
  62. struct irb *irb;
  63. bool is_final;
  64. bool cp_is_finished = false;
  65. private = container_of(work, struct vfio_ccw_private, io_work);
  66. irb = &private->irb;
  67. is_final = !(scsw_actl(&irb->scsw) &
  68. (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT));
  69. if (scsw_is_solicited(&irb->scsw)) {
  70. cp_update_scsw(&private->cp, &irb->scsw);
  71. if (is_final && private->state == VFIO_CCW_STATE_CP_PENDING) {
  72. cp_free(&private->cp);
  73. cp_is_finished = true;
  74. }
  75. }
  76. mutex_lock(&private->io_mutex);
  77. memcpy(private->io_region->irb_area, irb, sizeof(*irb));
  78. mutex_unlock(&private->io_mutex);
  79. /*
  80. * Reset to IDLE only if processing of a channel program
  81. * has finished. Do not overwrite a possible processing
  82. * state if the interrupt was unsolicited, or if the final
  83. * interrupt was for HSCH or CSCH.
  84. */
  85. if (cp_is_finished)
  86. private->state = VFIO_CCW_STATE_IDLE;
  87. if (private->io_trigger)
  88. eventfd_signal(private->io_trigger, 1);
  89. }
  90. static void vfio_ccw_crw_todo(struct work_struct *work)
  91. {
  92. struct vfio_ccw_private *private;
  93. private = container_of(work, struct vfio_ccw_private, crw_work);
  94. if (!list_empty(&private->crw) && private->crw_trigger)
  95. eventfd_signal(private->crw_trigger, 1);
  96. }
  97. /*
  98. * Css driver callbacks
  99. */
  100. static void vfio_ccw_sch_irq(struct subchannel *sch)
  101. {
  102. struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
  103. inc_irq_stat(IRQIO_CIO);
  104. vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_INTERRUPT);
  105. }
  106. static struct vfio_ccw_private *vfio_ccw_alloc_private(struct subchannel *sch)
  107. {
  108. struct vfio_ccw_private *private;
  109. private = kzalloc(sizeof(*private), GFP_KERNEL);
  110. if (!private)
  111. return ERR_PTR(-ENOMEM);
  112. private->sch = sch;
  113. mutex_init(&private->io_mutex);
  114. private->state = VFIO_CCW_STATE_STANDBY;
  115. INIT_LIST_HEAD(&private->crw);
  116. INIT_WORK(&private->io_work, vfio_ccw_sch_io_todo);
  117. INIT_WORK(&private->crw_work, vfio_ccw_crw_todo);
  118. private->cp.guest_cp = kcalloc(CCWCHAIN_LEN_MAX, sizeof(struct ccw1),
  119. GFP_KERNEL);
  120. if (!private->cp.guest_cp)
  121. goto out_free_private;
  122. private->io_region = kmem_cache_zalloc(vfio_ccw_io_region,
  123. GFP_KERNEL | GFP_DMA);
  124. if (!private->io_region)
  125. goto out_free_cp;
  126. private->cmd_region = kmem_cache_zalloc(vfio_ccw_cmd_region,
  127. GFP_KERNEL | GFP_DMA);
  128. if (!private->cmd_region)
  129. goto out_free_io;
  130. private->schib_region = kmem_cache_zalloc(vfio_ccw_schib_region,
  131. GFP_KERNEL | GFP_DMA);
  132. if (!private->schib_region)
  133. goto out_free_cmd;
  134. private->crw_region = kmem_cache_zalloc(vfio_ccw_crw_region,
  135. GFP_KERNEL | GFP_DMA);
  136. if (!private->crw_region)
  137. goto out_free_schib;
  138. return private;
  139. out_free_schib:
  140. kmem_cache_free(vfio_ccw_schib_region, private->schib_region);
  141. out_free_cmd:
  142. kmem_cache_free(vfio_ccw_cmd_region, private->cmd_region);
  143. out_free_io:
  144. kmem_cache_free(vfio_ccw_io_region, private->io_region);
  145. out_free_cp:
  146. kfree(private->cp.guest_cp);
  147. out_free_private:
  148. mutex_destroy(&private->io_mutex);
  149. kfree(private);
  150. return ERR_PTR(-ENOMEM);
  151. }
  152. static void vfio_ccw_free_private(struct vfio_ccw_private *private)
  153. {
  154. struct vfio_ccw_crw *crw, *temp;
  155. list_for_each_entry_safe(crw, temp, &private->crw, next) {
  156. list_del(&crw->next);
  157. kfree(crw);
  158. }
  159. kmem_cache_free(vfio_ccw_crw_region, private->crw_region);
  160. kmem_cache_free(vfio_ccw_schib_region, private->schib_region);
  161. kmem_cache_free(vfio_ccw_cmd_region, private->cmd_region);
  162. kmem_cache_free(vfio_ccw_io_region, private->io_region);
  163. kfree(private->cp.guest_cp);
  164. mutex_destroy(&private->io_mutex);
  165. kfree(private);
  166. }
  167. static int vfio_ccw_sch_probe(struct subchannel *sch)
  168. {
  169. struct pmcw *pmcw = &sch->schib.pmcw;
  170. struct vfio_ccw_private *private;
  171. int ret = -ENOMEM;
  172. if (pmcw->qf) {
  173. dev_warn(&sch->dev, "vfio: ccw: does not support QDIO: %s\n",
  174. dev_name(&sch->dev));
  175. return -ENODEV;
  176. }
  177. private = vfio_ccw_alloc_private(sch);
  178. if (IS_ERR(private))
  179. return PTR_ERR(private);
  180. dev_set_drvdata(&sch->dev, private);
  181. private->mdev_type.sysfs_name = "io";
  182. private->mdev_type.pretty_name = "I/O subchannel (Non-QDIO)";
  183. private->mdev_types[0] = &private->mdev_type;
  184. ret = mdev_register_parent(&private->parent, &sch->dev,
  185. &vfio_ccw_mdev_driver,
  186. private->mdev_types, 1);
  187. if (ret)
  188. goto out_free;
  189. VFIO_CCW_MSG_EVENT(4, "bound to subchannel %x.%x.%04x\n",
  190. sch->schid.cssid, sch->schid.ssid,
  191. sch->schid.sch_no);
  192. return 0;
  193. out_free:
  194. dev_set_drvdata(&sch->dev, NULL);
  195. vfio_ccw_free_private(private);
  196. return ret;
  197. }
  198. static void vfio_ccw_sch_remove(struct subchannel *sch)
  199. {
  200. struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
  201. mdev_unregister_parent(&private->parent);
  202. dev_set_drvdata(&sch->dev, NULL);
  203. vfio_ccw_free_private(private);
  204. VFIO_CCW_MSG_EVENT(4, "unbound from subchannel %x.%x.%04x\n",
  205. sch->schid.cssid, sch->schid.ssid,
  206. sch->schid.sch_no);
  207. }
  208. static void vfio_ccw_sch_shutdown(struct subchannel *sch)
  209. {
  210. struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
  211. vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_CLOSE);
  212. vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_NOT_OPER);
  213. }
  214. /**
  215. * vfio_ccw_sch_event - process subchannel event
  216. * @sch: subchannel
  217. * @process: non-zero if function is called in process context
  218. *
  219. * An unspecified event occurred for this subchannel. Adjust data according
  220. * to the current operational state of the subchannel. Return zero when the
  221. * event has been handled sufficiently or -EAGAIN when this function should
  222. * be called again in process context.
  223. */
  224. static int vfio_ccw_sch_event(struct subchannel *sch, int process)
  225. {
  226. struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
  227. unsigned long flags;
  228. int rc = -EAGAIN;
  229. spin_lock_irqsave(sch->lock, flags);
  230. if (!device_is_registered(&sch->dev))
  231. goto out_unlock;
  232. if (work_pending(&sch->todo_work))
  233. goto out_unlock;
  234. rc = 0;
  235. if (cio_update_schib(sch))
  236. vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_NOT_OPER);
  237. out_unlock:
  238. spin_unlock_irqrestore(sch->lock, flags);
  239. return rc;
  240. }
  241. static void vfio_ccw_queue_crw(struct vfio_ccw_private *private,
  242. unsigned int rsc,
  243. unsigned int erc,
  244. unsigned int rsid)
  245. {
  246. struct vfio_ccw_crw *crw;
  247. /*
  248. * If unable to allocate a CRW, just drop the event and
  249. * carry on. The guest will either see a later one or
  250. * learn when it issues its own store subchannel.
  251. */
  252. crw = kzalloc(sizeof(*crw), GFP_ATOMIC);
  253. if (!crw)
  254. return;
  255. /*
  256. * Build the CRW based on the inputs given to us.
  257. */
  258. crw->crw.rsc = rsc;
  259. crw->crw.erc = erc;
  260. crw->crw.rsid = rsid;
  261. list_add_tail(&crw->next, &private->crw);
  262. queue_work(vfio_ccw_work_q, &private->crw_work);
  263. }
  264. static int vfio_ccw_chp_event(struct subchannel *sch,
  265. struct chp_link *link, int event)
  266. {
  267. struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
  268. int mask = chp_ssd_get_mask(&sch->ssd_info, link);
  269. int retry = 255;
  270. if (!private || !mask)
  271. return 0;
  272. trace_vfio_ccw_chp_event(private->sch->schid, mask, event);
  273. VFIO_CCW_MSG_EVENT(2, "sch %x.%x.%04x: mask=0x%x event=%d\n",
  274. sch->schid.cssid,
  275. sch->schid.ssid, sch->schid.sch_no,
  276. mask, event);
  277. if (cio_update_schib(sch))
  278. return -ENODEV;
  279. switch (event) {
  280. case CHP_VARY_OFF:
  281. /* Path logically turned off */
  282. sch->opm &= ~mask;
  283. sch->lpm &= ~mask;
  284. if (sch->schib.pmcw.lpum & mask)
  285. cio_cancel_halt_clear(sch, &retry);
  286. break;
  287. case CHP_OFFLINE:
  288. /* Path is gone */
  289. if (sch->schib.pmcw.lpum & mask)
  290. cio_cancel_halt_clear(sch, &retry);
  291. vfio_ccw_queue_crw(private, CRW_RSC_CPATH, CRW_ERC_PERRN,
  292. link->chpid.id);
  293. break;
  294. case CHP_VARY_ON:
  295. /* Path logically turned on */
  296. sch->opm |= mask;
  297. sch->lpm |= mask;
  298. break;
  299. case CHP_ONLINE:
  300. /* Path became available */
  301. sch->lpm |= mask & sch->opm;
  302. vfio_ccw_queue_crw(private, CRW_RSC_CPATH, CRW_ERC_INIT,
  303. link->chpid.id);
  304. break;
  305. }
  306. return 0;
  307. }
  308. static struct css_device_id vfio_ccw_sch_ids[] = {
  309. { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
  310. { /* end of list */ },
  311. };
  312. MODULE_DEVICE_TABLE(css, vfio_ccw_sch_ids);
  313. static struct css_driver vfio_ccw_sch_driver = {
  314. .drv = {
  315. .name = "vfio_ccw",
  316. .owner = THIS_MODULE,
  317. },
  318. .subchannel_type = vfio_ccw_sch_ids,
  319. .irq = vfio_ccw_sch_irq,
  320. .probe = vfio_ccw_sch_probe,
  321. .remove = vfio_ccw_sch_remove,
  322. .shutdown = vfio_ccw_sch_shutdown,
  323. .sch_event = vfio_ccw_sch_event,
  324. .chp_event = vfio_ccw_chp_event,
  325. };
  326. static int __init vfio_ccw_debug_init(void)
  327. {
  328. vfio_ccw_debug_msg_id = debug_register("vfio_ccw_msg", 16, 1,
  329. 11 * sizeof(long));
  330. if (!vfio_ccw_debug_msg_id)
  331. goto out_unregister;
  332. debug_register_view(vfio_ccw_debug_msg_id, &debug_sprintf_view);
  333. debug_set_level(vfio_ccw_debug_msg_id, 2);
  334. vfio_ccw_debug_trace_id = debug_register("vfio_ccw_trace", 16, 1, 16);
  335. if (!vfio_ccw_debug_trace_id)
  336. goto out_unregister;
  337. debug_register_view(vfio_ccw_debug_trace_id, &debug_hex_ascii_view);
  338. debug_set_level(vfio_ccw_debug_trace_id, 2);
  339. return 0;
  340. out_unregister:
  341. debug_unregister(vfio_ccw_debug_msg_id);
  342. debug_unregister(vfio_ccw_debug_trace_id);
  343. return -1;
  344. }
  345. static void vfio_ccw_debug_exit(void)
  346. {
  347. debug_unregister(vfio_ccw_debug_msg_id);
  348. debug_unregister(vfio_ccw_debug_trace_id);
  349. }
  350. static void vfio_ccw_destroy_regions(void)
  351. {
  352. kmem_cache_destroy(vfio_ccw_crw_region);
  353. kmem_cache_destroy(vfio_ccw_schib_region);
  354. kmem_cache_destroy(vfio_ccw_cmd_region);
  355. kmem_cache_destroy(vfio_ccw_io_region);
  356. }
  357. static int __init vfio_ccw_sch_init(void)
  358. {
  359. int ret;
  360. ret = vfio_ccw_debug_init();
  361. if (ret)
  362. return ret;
  363. vfio_ccw_work_q = create_singlethread_workqueue("vfio-ccw");
  364. if (!vfio_ccw_work_q) {
  365. ret = -ENOMEM;
  366. goto out_regions;
  367. }
  368. vfio_ccw_io_region = kmem_cache_create_usercopy("vfio_ccw_io_region",
  369. sizeof(struct ccw_io_region), 0,
  370. SLAB_ACCOUNT, 0,
  371. sizeof(struct ccw_io_region), NULL);
  372. if (!vfio_ccw_io_region) {
  373. ret = -ENOMEM;
  374. goto out_regions;
  375. }
  376. vfio_ccw_cmd_region = kmem_cache_create_usercopy("vfio_ccw_cmd_region",
  377. sizeof(struct ccw_cmd_region), 0,
  378. SLAB_ACCOUNT, 0,
  379. sizeof(struct ccw_cmd_region), NULL);
  380. if (!vfio_ccw_cmd_region) {
  381. ret = -ENOMEM;
  382. goto out_regions;
  383. }
  384. vfio_ccw_schib_region = kmem_cache_create_usercopy("vfio_ccw_schib_region",
  385. sizeof(struct ccw_schib_region), 0,
  386. SLAB_ACCOUNT, 0,
  387. sizeof(struct ccw_schib_region), NULL);
  388. if (!vfio_ccw_schib_region) {
  389. ret = -ENOMEM;
  390. goto out_regions;
  391. }
  392. vfio_ccw_crw_region = kmem_cache_create_usercopy("vfio_ccw_crw_region",
  393. sizeof(struct ccw_crw_region), 0,
  394. SLAB_ACCOUNT, 0,
  395. sizeof(struct ccw_crw_region), NULL);
  396. if (!vfio_ccw_crw_region) {
  397. ret = -ENOMEM;
  398. goto out_regions;
  399. }
  400. ret = mdev_register_driver(&vfio_ccw_mdev_driver);
  401. if (ret)
  402. goto out_regions;
  403. isc_register(VFIO_CCW_ISC);
  404. ret = css_driver_register(&vfio_ccw_sch_driver);
  405. if (ret) {
  406. isc_unregister(VFIO_CCW_ISC);
  407. goto out_driver;
  408. }
  409. return ret;
  410. out_driver:
  411. mdev_unregister_driver(&vfio_ccw_mdev_driver);
  412. out_regions:
  413. vfio_ccw_destroy_regions();
  414. destroy_workqueue(vfio_ccw_work_q);
  415. vfio_ccw_debug_exit();
  416. return ret;
  417. }
  418. static void __exit vfio_ccw_sch_exit(void)
  419. {
  420. css_driver_unregister(&vfio_ccw_sch_driver);
  421. mdev_unregister_driver(&vfio_ccw_mdev_driver);
  422. isc_unregister(VFIO_CCW_ISC);
  423. vfio_ccw_destroy_regions();
  424. destroy_workqueue(vfio_ccw_work_q);
  425. vfio_ccw_debug_exit();
  426. }
  427. module_init(vfio_ccw_sch_init);
  428. module_exit(vfio_ccw_sch_exit);
  429. MODULE_LICENSE("GPL v2");