2 * Finite state machine for vfio-ccw device handling
4 * Copyright IBM Corp. 2017
6 * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
9 #include <linux/vfio.h>
10 #include <linux/mdev.h>
13 #include "vfio_ccw_private.h"
15 static int fsm_io_helper(struct vfio_ccw_private *private)
17 struct subchannel *sch;
25 spin_lock_irqsave(sch->lock, flags);
26 private->state = VFIO_CCW_STATE_BUSY;
27 spin_unlock_irqrestore(sch->lock, flags);
29 orb = cp_get_orb(&private->cp, (u32)(addr_t)sch, sch->lpm);
31 /* Issue "Start Subchannel" */
32 ccode = ssch(sch->schid, orb);
37 * Initialize device status information
39 sch->schib.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
41 case 1: /* Status pending */
44 case 3: /* Device/path not operational */
52 if (cio_update_schib(sch))
55 return sch->lpm ? -EACCES : -ENODEV;
62 static void fsm_notoper(struct vfio_ccw_private *private,
63 enum vfio_ccw_event event)
65 struct subchannel *sch = private->sch;
69 * Probably we should send the machine check to the guest.
71 css_sched_sch_todo(sch, SCH_TODO_UNREG);
72 private->state = VFIO_CCW_STATE_NOT_OPER;
76 * No operation action.
78 static void fsm_nop(struct vfio_ccw_private *private,
79 enum vfio_ccw_event event)
83 static void fsm_io_error(struct vfio_ccw_private *private,
84 enum vfio_ccw_event event)
86 pr_err("vfio-ccw: FSM: I/O request from state:%d\n", private->state);
87 private->io_region.ret_code = -EIO;
90 static void fsm_io_busy(struct vfio_ccw_private *private,
91 enum vfio_ccw_event event)
93 private->io_region.ret_code = -EBUSY;
96 static void fsm_disabled_irq(struct vfio_ccw_private *private,
97 enum vfio_ccw_event event)
99 struct subchannel *sch = private->sch;
102 * An interrupt in a disabled state means a previous disable was not
103 * successful - should not happen, but we try to disable again.
105 cio_disable_subchannel(sch);
109 * Deal with the ccw command request from the userspace.
111 static void fsm_io_request(struct vfio_ccw_private *private,
112 enum vfio_ccw_event event)
115 union scsw *scsw = &private->scsw;
116 struct ccw_io_region *io_region = &private->io_region;
117 struct mdev_device *mdev = private->mdev;
119 private->state = VFIO_CCW_STATE_BOXED;
121 memcpy(scsw, io_region->scsw_area, sizeof(*scsw));
123 if (scsw->cmd.fctl & SCSW_FCTL_START_FUNC) {
124 orb = (union orb *)io_region->orb_area;
126 io_region->ret_code = cp_init(&private->cp, mdev_dev(mdev),
128 if (io_region->ret_code)
131 io_region->ret_code = cp_prefetch(&private->cp);
132 if (io_region->ret_code) {
133 cp_free(&private->cp);
137 /* Start channel program and wait for I/O interrupt. */
138 io_region->ret_code = fsm_io_helper(private);
139 if (io_region->ret_code) {
140 cp_free(&private->cp);
144 } else if (scsw->cmd.fctl & SCSW_FCTL_HALT_FUNC) {
145 /* XXX: Handle halt. */
146 io_region->ret_code = -EOPNOTSUPP;
148 } else if (scsw->cmd.fctl & SCSW_FCTL_CLEAR_FUNC) {
149 /* XXX: Handle clear. */
150 io_region->ret_code = -EOPNOTSUPP;
155 private->state = VFIO_CCW_STATE_IDLE;
159 * Got an interrupt for a normal io (state busy).
161 static void fsm_irq(struct vfio_ccw_private *private,
162 enum vfio_ccw_event event)
164 struct irb *irb = this_cpu_ptr(&cio_irb);
166 memcpy(&private->irb, irb, sizeof(*irb));
168 queue_work(vfio_ccw_work_q, &private->io_work);
170 if (private->completion)
171 complete(private->completion);
175 * Device statemachine
177 fsm_func_t *vfio_ccw_jumptable[NR_VFIO_CCW_STATES][NR_VFIO_CCW_EVENTS] = {
178 [VFIO_CCW_STATE_NOT_OPER] = {
179 [VFIO_CCW_EVENT_NOT_OPER] = fsm_nop,
180 [VFIO_CCW_EVENT_IO_REQ] = fsm_io_error,
181 [VFIO_CCW_EVENT_INTERRUPT] = fsm_disabled_irq,
183 [VFIO_CCW_STATE_STANDBY] = {
184 [VFIO_CCW_EVENT_NOT_OPER] = fsm_notoper,
185 [VFIO_CCW_EVENT_IO_REQ] = fsm_io_error,
186 [VFIO_CCW_EVENT_INTERRUPT] = fsm_irq,
188 [VFIO_CCW_STATE_IDLE] = {
189 [VFIO_CCW_EVENT_NOT_OPER] = fsm_notoper,
190 [VFIO_CCW_EVENT_IO_REQ] = fsm_io_request,
191 [VFIO_CCW_EVENT_INTERRUPT] = fsm_irq,
193 [VFIO_CCW_STATE_BOXED] = {
194 [VFIO_CCW_EVENT_NOT_OPER] = fsm_notoper,
195 [VFIO_CCW_EVENT_IO_REQ] = fsm_io_busy,
196 [VFIO_CCW_EVENT_INTERRUPT] = fsm_irq,
198 [VFIO_CCW_STATE_BUSY] = {
199 [VFIO_CCW_EVENT_NOT_OPER] = fsm_notoper,
200 [VFIO_CCW_EVENT_IO_REQ] = fsm_io_busy,
201 [VFIO_CCW_EVENT_INTERRUPT] = fsm_irq,