vfio_ccw_fsm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Finite state machine for vfio-ccw device handling
  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. * Cornelia Huck <cohuck@redhat.com>
  10. */
  11. #include <linux/vfio.h>
  12. #include <asm/isc.h>
  13. #include "ioasm.h"
  14. #include "vfio_ccw_private.h"
  15. static int fsm_io_helper(struct vfio_ccw_private *private)
  16. {
  17. struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
  18. union orb *orb;
  19. int ccode;
  20. __u8 lpm;
  21. unsigned long flags;
  22. int ret;
  23. spin_lock_irqsave(&sch->lock, flags);
  24. orb = cp_get_orb(&private->cp, sch);
  25. if (!orb) {
  26. ret = -EIO;
  27. goto out;
  28. }
  29. VFIO_CCW_TRACE_EVENT(5, "stIO");
  30. VFIO_CCW_TRACE_EVENT(5, dev_name(&sch->dev));
  31. /* Issue "Start Subchannel" */
  32. ccode = ssch(sch->schid, orb);
  33. VFIO_CCW_HEX_EVENT(5, &ccode, sizeof(ccode));
  34. switch (ccode) {
  35. case 0:
  36. /*
  37. * Initialize device status information
  38. */
  39. sch->schib.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
  40. ret = 0;
  41. private->state = VFIO_CCW_STATE_CP_PENDING;
  42. break;
  43. case 1: /* Status pending */
  44. case 2: /* Busy */
  45. ret = -EBUSY;
  46. break;
  47. case 3: /* Device/path not operational */
  48. {
  49. lpm = orb->cmd.lpm;
  50. if (lpm != 0)
  51. sch->lpm &= ~lpm;
  52. else
  53. sch->lpm = 0;
  54. if (cio_update_schib(sch))
  55. ret = -ENODEV;
  56. else
  57. ret = sch->lpm ? -EACCES : -ENODEV;
  58. break;
  59. }
  60. default:
  61. ret = ccode;
  62. }
  63. out:
  64. spin_unlock_irqrestore(&sch->lock, flags);
  65. return ret;
  66. }
  67. static int fsm_do_halt(struct vfio_ccw_private *private)
  68. {
  69. struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
  70. unsigned long flags;
  71. int ccode;
  72. int ret;
  73. spin_lock_irqsave(&sch->lock, flags);
  74. VFIO_CCW_TRACE_EVENT(2, "haltIO");
  75. VFIO_CCW_TRACE_EVENT(2, dev_name(&sch->dev));
  76. /* Issue "Halt Subchannel" */
  77. ccode = hsch(sch->schid);
  78. VFIO_CCW_HEX_EVENT(2, &ccode, sizeof(ccode));
  79. switch (ccode) {
  80. case 0:
  81. /*
  82. * Initialize device status information
  83. */
  84. sch->schib.scsw.cmd.actl |= SCSW_ACTL_HALT_PEND;
  85. ret = 0;
  86. break;
  87. case 1: /* Status pending */
  88. case 2: /* Busy */
  89. ret = -EBUSY;
  90. break;
  91. case 3: /* Device not operational */
  92. ret = -ENODEV;
  93. break;
  94. default:
  95. ret = ccode;
  96. }
  97. spin_unlock_irqrestore(&sch->lock, flags);
  98. return ret;
  99. }
  100. static int fsm_do_clear(struct vfio_ccw_private *private)
  101. {
  102. struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
  103. unsigned long flags;
  104. int ccode;
  105. int ret;
  106. spin_lock_irqsave(&sch->lock, flags);
  107. VFIO_CCW_TRACE_EVENT(2, "clearIO");
  108. VFIO_CCW_TRACE_EVENT(2, dev_name(&sch->dev));
  109. /* Issue "Clear Subchannel" */
  110. ccode = csch(sch->schid);
  111. VFIO_CCW_HEX_EVENT(2, &ccode, sizeof(ccode));
  112. switch (ccode) {
  113. case 0:
  114. /*
  115. * Initialize device status information
  116. */
  117. sch->schib.scsw.cmd.actl = SCSW_ACTL_CLEAR_PEND;
  118. /* TODO: check what else we might need to clear */
  119. ret = 0;
  120. break;
  121. case 3: /* Device not operational */
  122. ret = -ENODEV;
  123. break;
  124. default:
  125. ret = ccode;
  126. }
  127. spin_unlock_irqrestore(&sch->lock, flags);
  128. return ret;
  129. }
  130. static void fsm_notoper(struct vfio_ccw_private *private,
  131. enum vfio_ccw_event event)
  132. {
  133. struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
  134. VFIO_CCW_MSG_EVENT(2, "sch %x.%x.%04x: notoper event %x state %x\n",
  135. sch->schid.cssid,
  136. sch->schid.ssid,
  137. sch->schid.sch_no,
  138. event,
  139. private->state);
  140. /*
  141. * TODO:
  142. * Probably we should send the machine check to the guest.
  143. */
  144. css_sched_sch_todo(sch, SCH_TODO_UNREG);
  145. private->state = VFIO_CCW_STATE_NOT_OPER;
  146. /* This is usually handled during CLOSE event */
  147. cp_free(&private->cp);
  148. }
  149. /*
  150. * No operation action.
  151. */
  152. static void fsm_nop(struct vfio_ccw_private *private,
  153. enum vfio_ccw_event event)
  154. {
  155. }
  156. static void fsm_io_error(struct vfio_ccw_private *private,
  157. enum vfio_ccw_event event)
  158. {
  159. pr_err("vfio-ccw: FSM: I/O request from state:%d\n", private->state);
  160. private->io_region->ret_code = -EIO;
  161. }
  162. static void fsm_io_busy(struct vfio_ccw_private *private,
  163. enum vfio_ccw_event event)
  164. {
  165. private->io_region->ret_code = -EBUSY;
  166. }
  167. static void fsm_io_retry(struct vfio_ccw_private *private,
  168. enum vfio_ccw_event event)
  169. {
  170. private->io_region->ret_code = -EAGAIN;
  171. }
  172. static void fsm_async_error(struct vfio_ccw_private *private,
  173. enum vfio_ccw_event event)
  174. {
  175. struct ccw_cmd_region *cmd_region = private->cmd_region;
  176. pr_err("vfio-ccw: FSM: %s request from state:%d\n",
  177. cmd_region->command == VFIO_CCW_ASYNC_CMD_HSCH ? "halt" :
  178. cmd_region->command == VFIO_CCW_ASYNC_CMD_CSCH ? "clear" :
  179. "<unknown>", private->state);
  180. cmd_region->ret_code = -EIO;
  181. }
  182. static void fsm_async_retry(struct vfio_ccw_private *private,
  183. enum vfio_ccw_event event)
  184. {
  185. private->cmd_region->ret_code = -EAGAIN;
  186. }
  187. static void fsm_disabled_irq(struct vfio_ccw_private *private,
  188. enum vfio_ccw_event event)
  189. {
  190. struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
  191. /*
  192. * An interrupt in a disabled state means a previous disable was not
  193. * successful - should not happen, but we try to disable again.
  194. */
  195. cio_disable_subchannel(sch);
  196. }
  197. inline struct subchannel_id get_schid(struct vfio_ccw_private *p)
  198. {
  199. struct subchannel *sch = to_subchannel(p->vdev.dev->parent);
  200. return sch->schid;
  201. }
  202. /*
  203. * Deal with the ccw command request from the userspace.
  204. */
  205. static void fsm_io_request(struct vfio_ccw_private *private,
  206. enum vfio_ccw_event event)
  207. {
  208. union orb *orb;
  209. union scsw *scsw = &private->scsw;
  210. struct ccw_io_region *io_region = private->io_region;
  211. char *errstr = "request";
  212. struct subchannel_id schid = get_schid(private);
  213. private->state = VFIO_CCW_STATE_CP_PROCESSING;
  214. memcpy(scsw, io_region->scsw_area, sizeof(*scsw));
  215. if (scsw->cmd.fctl & SCSW_FCTL_START_FUNC) {
  216. orb = (union orb *)io_region->orb_area;
  217. /* Don't try to build a cp if transport mode is specified. */
  218. if (orb->tm.b) {
  219. io_region->ret_code = -EOPNOTSUPP;
  220. VFIO_CCW_MSG_EVENT(2,
  221. "sch %x.%x.%04x: transport mode\n",
  222. schid.cssid,
  223. schid.ssid, schid.sch_no);
  224. errstr = "transport mode";
  225. goto err_out;
  226. }
  227. io_region->ret_code = cp_init(&private->cp, orb);
  228. if (io_region->ret_code) {
  229. VFIO_CCW_MSG_EVENT(2,
  230. "sch %x.%x.%04x: cp_init=%d\n",
  231. schid.cssid,
  232. schid.ssid, schid.sch_no,
  233. io_region->ret_code);
  234. errstr = "cp init";
  235. goto err_out;
  236. }
  237. io_region->ret_code = cp_prefetch(&private->cp);
  238. if (io_region->ret_code) {
  239. VFIO_CCW_MSG_EVENT(2,
  240. "sch %x.%x.%04x: cp_prefetch=%d\n",
  241. schid.cssid,
  242. schid.ssid, schid.sch_no,
  243. io_region->ret_code);
  244. errstr = "cp prefetch";
  245. cp_free(&private->cp);
  246. goto err_out;
  247. }
  248. /* Start channel program and wait for I/O interrupt. */
  249. io_region->ret_code = fsm_io_helper(private);
  250. if (io_region->ret_code) {
  251. VFIO_CCW_MSG_EVENT(2,
  252. "sch %x.%x.%04x: fsm_io_helper=%d\n",
  253. schid.cssid,
  254. schid.ssid, schid.sch_no,
  255. io_region->ret_code);
  256. errstr = "cp fsm_io_helper";
  257. cp_free(&private->cp);
  258. goto err_out;
  259. }
  260. return;
  261. } else if (scsw->cmd.fctl & SCSW_FCTL_HALT_FUNC) {
  262. VFIO_CCW_MSG_EVENT(2,
  263. "sch %x.%x.%04x: halt on io_region\n",
  264. schid.cssid,
  265. schid.ssid, schid.sch_no);
  266. /* halt is handled via the async cmd region */
  267. io_region->ret_code = -EOPNOTSUPP;
  268. goto err_out;
  269. } else if (scsw->cmd.fctl & SCSW_FCTL_CLEAR_FUNC) {
  270. VFIO_CCW_MSG_EVENT(2,
  271. "sch %x.%x.%04x: clear on io_region\n",
  272. schid.cssid,
  273. schid.ssid, schid.sch_no);
  274. /* clear is handled via the async cmd region */
  275. io_region->ret_code = -EOPNOTSUPP;
  276. goto err_out;
  277. }
  278. err_out:
  279. private->state = VFIO_CCW_STATE_IDLE;
  280. trace_vfio_ccw_fsm_io_request(scsw->cmd.fctl, schid,
  281. io_region->ret_code, errstr);
  282. }
  283. /*
  284. * Deal with an async request from userspace.
  285. */
  286. static void fsm_async_request(struct vfio_ccw_private *private,
  287. enum vfio_ccw_event event)
  288. {
  289. struct ccw_cmd_region *cmd_region = private->cmd_region;
  290. switch (cmd_region->command) {
  291. case VFIO_CCW_ASYNC_CMD_HSCH:
  292. cmd_region->ret_code = fsm_do_halt(private);
  293. break;
  294. case VFIO_CCW_ASYNC_CMD_CSCH:
  295. cmd_region->ret_code = fsm_do_clear(private);
  296. break;
  297. default:
  298. /* should not happen? */
  299. cmd_region->ret_code = -EINVAL;
  300. }
  301. trace_vfio_ccw_fsm_async_request(get_schid(private),
  302. cmd_region->command,
  303. cmd_region->ret_code);
  304. }
  305. /*
  306. * Got an interrupt for a normal io (state busy).
  307. */
  308. static void fsm_irq(struct vfio_ccw_private *private,
  309. enum vfio_ccw_event event)
  310. {
  311. struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
  312. struct irb *irb = this_cpu_ptr(&cio_irb);
  313. VFIO_CCW_TRACE_EVENT(6, "IRQ");
  314. VFIO_CCW_TRACE_EVENT(6, dev_name(&sch->dev));
  315. memcpy(&private->irb, irb, sizeof(*irb));
  316. queue_work(vfio_ccw_work_q, &private->io_work);
  317. if (private->completion)
  318. complete(private->completion);
  319. }
  320. static void fsm_open(struct vfio_ccw_private *private,
  321. enum vfio_ccw_event event)
  322. {
  323. struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
  324. int ret;
  325. spin_lock_irq(&sch->lock);
  326. sch->isc = VFIO_CCW_ISC;
  327. ret = cio_enable_subchannel(sch, (u32)virt_to_phys(sch));
  328. if (ret)
  329. goto err_unlock;
  330. private->state = VFIO_CCW_STATE_IDLE;
  331. spin_unlock_irq(&sch->lock);
  332. return;
  333. err_unlock:
  334. spin_unlock_irq(&sch->lock);
  335. vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_NOT_OPER);
  336. }
  337. static void fsm_close(struct vfio_ccw_private *private,
  338. enum vfio_ccw_event event)
  339. {
  340. struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
  341. int ret;
  342. spin_lock_irq(&sch->lock);
  343. if (!sch->schib.pmcw.ena)
  344. goto err_unlock;
  345. ret = cio_disable_subchannel(sch);
  346. if (ret == -EBUSY)
  347. ret = vfio_ccw_sch_quiesce(sch);
  348. if (ret)
  349. goto err_unlock;
  350. private->state = VFIO_CCW_STATE_STANDBY;
  351. spin_unlock_irq(&sch->lock);
  352. cp_free(&private->cp);
  353. return;
  354. err_unlock:
  355. spin_unlock_irq(&sch->lock);
  356. vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_NOT_OPER);
  357. }
  358. /*
  359. * Device statemachine
  360. */
  361. fsm_func_t *vfio_ccw_jumptable[NR_VFIO_CCW_STATES][NR_VFIO_CCW_EVENTS] = {
  362. [VFIO_CCW_STATE_NOT_OPER] = {
  363. [VFIO_CCW_EVENT_NOT_OPER] = fsm_nop,
  364. [VFIO_CCW_EVENT_IO_REQ] = fsm_io_error,
  365. [VFIO_CCW_EVENT_ASYNC_REQ] = fsm_async_error,
  366. [VFIO_CCW_EVENT_INTERRUPT] = fsm_disabled_irq,
  367. [VFIO_CCW_EVENT_OPEN] = fsm_nop,
  368. [VFIO_CCW_EVENT_CLOSE] = fsm_nop,
  369. },
  370. [VFIO_CCW_STATE_STANDBY] = {
  371. [VFIO_CCW_EVENT_NOT_OPER] = fsm_notoper,
  372. [VFIO_CCW_EVENT_IO_REQ] = fsm_io_error,
  373. [VFIO_CCW_EVENT_ASYNC_REQ] = fsm_async_error,
  374. [VFIO_CCW_EVENT_INTERRUPT] = fsm_disabled_irq,
  375. [VFIO_CCW_EVENT_OPEN] = fsm_open,
  376. [VFIO_CCW_EVENT_CLOSE] = fsm_notoper,
  377. },
  378. [VFIO_CCW_STATE_IDLE] = {
  379. [VFIO_CCW_EVENT_NOT_OPER] = fsm_notoper,
  380. [VFIO_CCW_EVENT_IO_REQ] = fsm_io_request,
  381. [VFIO_CCW_EVENT_ASYNC_REQ] = fsm_async_request,
  382. [VFIO_CCW_EVENT_INTERRUPT] = fsm_irq,
  383. [VFIO_CCW_EVENT_OPEN] = fsm_notoper,
  384. [VFIO_CCW_EVENT_CLOSE] = fsm_close,
  385. },
  386. [VFIO_CCW_STATE_CP_PROCESSING] = {
  387. [VFIO_CCW_EVENT_NOT_OPER] = fsm_notoper,
  388. [VFIO_CCW_EVENT_IO_REQ] = fsm_io_retry,
  389. [VFIO_CCW_EVENT_ASYNC_REQ] = fsm_async_retry,
  390. [VFIO_CCW_EVENT_INTERRUPT] = fsm_irq,
  391. [VFIO_CCW_EVENT_OPEN] = fsm_notoper,
  392. [VFIO_CCW_EVENT_CLOSE] = fsm_close,
  393. },
  394. [VFIO_CCW_STATE_CP_PENDING] = {
  395. [VFIO_CCW_EVENT_NOT_OPER] = fsm_notoper,
  396. [VFIO_CCW_EVENT_IO_REQ] = fsm_io_busy,
  397. [VFIO_CCW_EVENT_ASYNC_REQ] = fsm_async_request,
  398. [VFIO_CCW_EVENT_INTERRUPT] = fsm_irq,
  399. [VFIO_CCW_EVENT_OPEN] = fsm_notoper,
  400. [VFIO_CCW_EVENT_CLOSE] = fsm_close,
  401. },
  402. };