vfio_ccw_fsm.c 11 KB

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