xref: /openbmc/linux/drivers/s390/cio/vfio_ccw_fsm.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2bbe37e4cSDong Jia Shi /*
3bbe37e4cSDong Jia Shi  * Finite state machine for vfio-ccw device handling
4bbe37e4cSDong Jia Shi  *
5bbe37e4cSDong Jia Shi  * Copyright IBM Corp. 2017
6d5afd5d1SCornelia Huck  * Copyright Red Hat, Inc. 2019
7bbe37e4cSDong Jia Shi  *
8bbe37e4cSDong Jia Shi  * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
9d5afd5d1SCornelia Huck  *            Cornelia Huck <cohuck@redhat.com>
10bbe37e4cSDong Jia Shi  */
11bbe37e4cSDong Jia Shi 
12bbe37e4cSDong Jia Shi #include <linux/vfio.h>
13bbe37e4cSDong Jia Shi 
1462ec0d49SEric Farman #include <asm/isc.h>
1562ec0d49SEric Farman 
16bbe37e4cSDong Jia Shi #include "ioasm.h"
17bbe37e4cSDong Jia Shi #include "vfio_ccw_private.h"
18bbe37e4cSDong Jia Shi 
fsm_io_helper(struct vfio_ccw_private * private)19bbe37e4cSDong Jia Shi static int fsm_io_helper(struct vfio_ccw_private *private)
20bbe37e4cSDong Jia Shi {
21008a011dSEric Farman 	struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
22bbe37e4cSDong Jia Shi 	union orb *orb;
23bbe37e4cSDong Jia Shi 	int ccode;
24bbe37e4cSDong Jia Shi 	__u8 lpm;
25bbe37e4cSDong Jia Shi 	unsigned long flags;
263368e547SCornelia Huck 	int ret;
27bbe37e4cSDong Jia Shi 
28bbe37e4cSDong Jia Shi 	spin_lock_irqsave(sch->lock, flags);
29bbe37e4cSDong Jia Shi 
30*9fbed59fSEric Farman 	orb = cp_get_orb(&private->cp, sch);
3171189f26SCornelia Huck 	if (!orb) {
3271189f26SCornelia Huck 		ret = -EIO;
3371189f26SCornelia Huck 		goto out;
3471189f26SCornelia Huck 	}
35bbe37e4cSDong Jia Shi 
3660e05d1cSCornelia Huck 	VFIO_CCW_TRACE_EVENT(5, "stIO");
3760e05d1cSCornelia Huck 	VFIO_CCW_TRACE_EVENT(5, dev_name(&sch->dev));
3860e05d1cSCornelia Huck 
39bbe37e4cSDong Jia Shi 	/* Issue "Start Subchannel" */
40bbe37e4cSDong Jia Shi 	ccode = ssch(sch->schid, orb);
41bbe37e4cSDong Jia Shi 
4260e05d1cSCornelia Huck 	VFIO_CCW_HEX_EVENT(5, &ccode, sizeof(ccode));
4360e05d1cSCornelia Huck 
44bbe37e4cSDong Jia Shi 	switch (ccode) {
45bbe37e4cSDong Jia Shi 	case 0:
46bbe37e4cSDong Jia Shi 		/*
47bbe37e4cSDong Jia Shi 		 * Initialize device status information
48bbe37e4cSDong Jia Shi 		 */
49bbe37e4cSDong Jia Shi 		sch->schib.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
503368e547SCornelia Huck 		ret = 0;
51690f6a15SCornelia Huck 		private->state = VFIO_CCW_STATE_CP_PENDING;
523368e547SCornelia Huck 		break;
53bbe37e4cSDong Jia Shi 	case 1:		/* Status pending */
54bbe37e4cSDong Jia Shi 	case 2:		/* Busy */
553368e547SCornelia Huck 		ret = -EBUSY;
563368e547SCornelia Huck 		break;
57bbe37e4cSDong Jia Shi 	case 3:		/* Device/path not operational */
58bbe37e4cSDong Jia Shi 	{
59bbe37e4cSDong Jia Shi 		lpm = orb->cmd.lpm;
60bbe37e4cSDong Jia Shi 		if (lpm != 0)
61bbe37e4cSDong Jia Shi 			sch->lpm &= ~lpm;
62bbe37e4cSDong Jia Shi 		else
63bbe37e4cSDong Jia Shi 			sch->lpm = 0;
64bbe37e4cSDong Jia Shi 
65bbe37e4cSDong Jia Shi 		if (cio_update_schib(sch))
663368e547SCornelia Huck 			ret = -ENODEV;
673368e547SCornelia Huck 		else
683368e547SCornelia Huck 			ret = sch->lpm ? -EACCES : -ENODEV;
693368e547SCornelia Huck 		break;
70bbe37e4cSDong Jia Shi 	}
71bbe37e4cSDong Jia Shi 	default:
723368e547SCornelia Huck 		ret = ccode;
73bbe37e4cSDong Jia Shi 	}
7471189f26SCornelia Huck out:
753368e547SCornelia Huck 	spin_unlock_irqrestore(sch->lock, flags);
763368e547SCornelia Huck 	return ret;
77bbe37e4cSDong Jia Shi }
78bbe37e4cSDong Jia Shi 
fsm_do_halt(struct vfio_ccw_private * private)79d5afd5d1SCornelia Huck static int fsm_do_halt(struct vfio_ccw_private *private)
80d5afd5d1SCornelia Huck {
81008a011dSEric Farman 	struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
82d5afd5d1SCornelia Huck 	unsigned long flags;
83d5afd5d1SCornelia Huck 	int ccode;
84d5afd5d1SCornelia Huck 	int ret;
85d5afd5d1SCornelia Huck 
86d5afd5d1SCornelia Huck 	spin_lock_irqsave(sch->lock, flags);
87d5afd5d1SCornelia Huck 
8860e05d1cSCornelia Huck 	VFIO_CCW_TRACE_EVENT(2, "haltIO");
8960e05d1cSCornelia Huck 	VFIO_CCW_TRACE_EVENT(2, dev_name(&sch->dev));
9060e05d1cSCornelia Huck 
91d5afd5d1SCornelia Huck 	/* Issue "Halt Subchannel" */
92d5afd5d1SCornelia Huck 	ccode = hsch(sch->schid);
93d5afd5d1SCornelia Huck 
9460e05d1cSCornelia Huck 	VFIO_CCW_HEX_EVENT(2, &ccode, sizeof(ccode));
9560e05d1cSCornelia Huck 
96d5afd5d1SCornelia Huck 	switch (ccode) {
97d5afd5d1SCornelia Huck 	case 0:
98d5afd5d1SCornelia Huck 		/*
99d5afd5d1SCornelia Huck 		 * Initialize device status information
100d5afd5d1SCornelia Huck 		 */
101d5afd5d1SCornelia Huck 		sch->schib.scsw.cmd.actl |= SCSW_ACTL_HALT_PEND;
102d5afd5d1SCornelia Huck 		ret = 0;
103d5afd5d1SCornelia Huck 		break;
104d5afd5d1SCornelia Huck 	case 1:		/* Status pending */
105d5afd5d1SCornelia Huck 	case 2:		/* Busy */
106d5afd5d1SCornelia Huck 		ret = -EBUSY;
107d5afd5d1SCornelia Huck 		break;
108d5afd5d1SCornelia Huck 	case 3:		/* Device not operational */
109d5afd5d1SCornelia Huck 		ret = -ENODEV;
110d5afd5d1SCornelia Huck 		break;
111d5afd5d1SCornelia Huck 	default:
112d5afd5d1SCornelia Huck 		ret = ccode;
113d5afd5d1SCornelia Huck 	}
114d5afd5d1SCornelia Huck 	spin_unlock_irqrestore(sch->lock, flags);
115d5afd5d1SCornelia Huck 	return ret;
116d5afd5d1SCornelia Huck }
117d5afd5d1SCornelia Huck 
fsm_do_clear(struct vfio_ccw_private * private)118d5afd5d1SCornelia Huck static int fsm_do_clear(struct vfio_ccw_private *private)
119d5afd5d1SCornelia Huck {
120008a011dSEric Farman 	struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
121d5afd5d1SCornelia Huck 	unsigned long flags;
122d5afd5d1SCornelia Huck 	int ccode;
123d5afd5d1SCornelia Huck 	int ret;
124d5afd5d1SCornelia Huck 
125d5afd5d1SCornelia Huck 	spin_lock_irqsave(sch->lock, flags);
126d5afd5d1SCornelia Huck 
12760e05d1cSCornelia Huck 	VFIO_CCW_TRACE_EVENT(2, "clearIO");
12860e05d1cSCornelia Huck 	VFIO_CCW_TRACE_EVENT(2, dev_name(&sch->dev));
12960e05d1cSCornelia Huck 
130d5afd5d1SCornelia Huck 	/* Issue "Clear Subchannel" */
131d5afd5d1SCornelia Huck 	ccode = csch(sch->schid);
132d5afd5d1SCornelia Huck 
13360e05d1cSCornelia Huck 	VFIO_CCW_HEX_EVENT(2, &ccode, sizeof(ccode));
13460e05d1cSCornelia Huck 
135d5afd5d1SCornelia Huck 	switch (ccode) {
136d5afd5d1SCornelia Huck 	case 0:
137d5afd5d1SCornelia Huck 		/*
138d5afd5d1SCornelia Huck 		 * Initialize device status information
139d5afd5d1SCornelia Huck 		 */
140d5afd5d1SCornelia Huck 		sch->schib.scsw.cmd.actl = SCSW_ACTL_CLEAR_PEND;
141d5afd5d1SCornelia Huck 		/* TODO: check what else we might need to clear */
142d5afd5d1SCornelia Huck 		ret = 0;
143d5afd5d1SCornelia Huck 		break;
144d5afd5d1SCornelia Huck 	case 3:		/* Device not operational */
145d5afd5d1SCornelia Huck 		ret = -ENODEV;
146d5afd5d1SCornelia Huck 		break;
147d5afd5d1SCornelia Huck 	default:
148d5afd5d1SCornelia Huck 		ret = ccode;
149d5afd5d1SCornelia Huck 	}
150d5afd5d1SCornelia Huck 	spin_unlock_irqrestore(sch->lock, flags);
151d5afd5d1SCornelia Huck 	return ret;
152d5afd5d1SCornelia Huck }
153d5afd5d1SCornelia Huck 
fsm_notoper(struct vfio_ccw_private * private,enum vfio_ccw_event event)154bbe37e4cSDong Jia Shi static void fsm_notoper(struct vfio_ccw_private *private,
155bbe37e4cSDong Jia Shi 			enum vfio_ccw_event event)
156bbe37e4cSDong Jia Shi {
157008a011dSEric Farman 	struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
158bbe37e4cSDong Jia Shi 
1594cc2c051SEric Farman 	VFIO_CCW_MSG_EVENT(2, "sch %x.%x.%04x: notoper event %x state %x\n",
1604cc2c051SEric Farman 			   sch->schid.cssid,
1614cc2c051SEric Farman 			   sch->schid.ssid,
1624cc2c051SEric Farman 			   sch->schid.sch_no,
1634cc2c051SEric Farman 			   event,
1644cc2c051SEric Farman 			   private->state);
16560e05d1cSCornelia Huck 
166bbe37e4cSDong Jia Shi 	/*
167bbe37e4cSDong Jia Shi 	 * TODO:
168bbe37e4cSDong Jia Shi 	 * Probably we should send the machine check to the guest.
169bbe37e4cSDong Jia Shi 	 */
170bbe37e4cSDong Jia Shi 	css_sched_sch_todo(sch, SCH_TODO_UNREG);
171bbe37e4cSDong Jia Shi 	private->state = VFIO_CCW_STATE_NOT_OPER;
172204b394aSEric Farman 
173204b394aSEric Farman 	/* This is usually handled during CLOSE event */
174204b394aSEric Farman 	cp_free(&private->cp);
175bbe37e4cSDong Jia Shi }
176bbe37e4cSDong Jia Shi 
177bbe37e4cSDong Jia Shi /*
178bbe37e4cSDong Jia Shi  * No operation action.
179bbe37e4cSDong Jia Shi  */
fsm_nop(struct vfio_ccw_private * private,enum vfio_ccw_event event)180bbe37e4cSDong Jia Shi static void fsm_nop(struct vfio_ccw_private *private,
181bbe37e4cSDong Jia Shi 		    enum vfio_ccw_event event)
182bbe37e4cSDong Jia Shi {
183bbe37e4cSDong Jia Shi }
184bbe37e4cSDong Jia Shi 
fsm_io_error(struct vfio_ccw_private * private,enum vfio_ccw_event event)185bbe37e4cSDong Jia Shi static void fsm_io_error(struct vfio_ccw_private *private,
186bbe37e4cSDong Jia Shi 			 enum vfio_ccw_event event)
187bbe37e4cSDong Jia Shi {
188bbe37e4cSDong Jia Shi 	pr_err("vfio-ccw: FSM: I/O request from state:%d\n", private->state);
189c98e16b2SEric Farman 	private->io_region->ret_code = -EIO;
190bbe37e4cSDong Jia Shi }
191bbe37e4cSDong Jia Shi 
fsm_io_busy(struct vfio_ccw_private * private,enum vfio_ccw_event event)192bbe37e4cSDong Jia Shi static void fsm_io_busy(struct vfio_ccw_private *private,
193bbe37e4cSDong Jia Shi 			enum vfio_ccw_event event)
194bbe37e4cSDong Jia Shi {
195c98e16b2SEric Farman 	private->io_region->ret_code = -EBUSY;
196bbe37e4cSDong Jia Shi }
197bbe37e4cSDong Jia Shi 
fsm_io_retry(struct vfio_ccw_private * private,enum vfio_ccw_event event)198690f6a15SCornelia Huck static void fsm_io_retry(struct vfio_ccw_private *private,
199690f6a15SCornelia Huck 			 enum vfio_ccw_event event)
200690f6a15SCornelia Huck {
201690f6a15SCornelia Huck 	private->io_region->ret_code = -EAGAIN;
202690f6a15SCornelia Huck }
203690f6a15SCornelia Huck 
fsm_async_error(struct vfio_ccw_private * private,enum vfio_ccw_event event)204d5afd5d1SCornelia Huck static void fsm_async_error(struct vfio_ccw_private *private,
205d5afd5d1SCornelia Huck 			    enum vfio_ccw_event event)
206d5afd5d1SCornelia Huck {
207d5afd5d1SCornelia Huck 	struct ccw_cmd_region *cmd_region = private->cmd_region;
208d5afd5d1SCornelia Huck 
209d5afd5d1SCornelia Huck 	pr_err("vfio-ccw: FSM: %s request from state:%d\n",
210d5afd5d1SCornelia Huck 	       cmd_region->command == VFIO_CCW_ASYNC_CMD_HSCH ? "halt" :
211d5afd5d1SCornelia Huck 	       cmd_region->command == VFIO_CCW_ASYNC_CMD_CSCH ? "clear" :
212d5afd5d1SCornelia Huck 	       "<unknown>", private->state);
213d5afd5d1SCornelia Huck 	cmd_region->ret_code = -EIO;
214d5afd5d1SCornelia Huck }
215d5afd5d1SCornelia Huck 
fsm_async_retry(struct vfio_ccw_private * private,enum vfio_ccw_event event)216d5afd5d1SCornelia Huck static void fsm_async_retry(struct vfio_ccw_private *private,
217d5afd5d1SCornelia Huck 			    enum vfio_ccw_event event)
218d5afd5d1SCornelia Huck {
219d5afd5d1SCornelia Huck 	private->cmd_region->ret_code = -EAGAIN;
220d5afd5d1SCornelia Huck }
221d5afd5d1SCornelia Huck 
fsm_disabled_irq(struct vfio_ccw_private * private,enum vfio_ccw_event event)222bbe37e4cSDong Jia Shi static void fsm_disabled_irq(struct vfio_ccw_private *private,
223bbe37e4cSDong Jia Shi 			     enum vfio_ccw_event event)
224bbe37e4cSDong Jia Shi {
225008a011dSEric Farman 	struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
226bbe37e4cSDong Jia Shi 
227bbe37e4cSDong Jia Shi 	/*
228bbe37e4cSDong Jia Shi 	 * An interrupt in a disabled state means a previous disable was not
229bbe37e4cSDong Jia Shi 	 * successful - should not happen, but we try to disable again.
230bbe37e4cSDong Jia Shi 	 */
231bbe37e4cSDong Jia Shi 	cio_disable_subchannel(sch);
232bbe37e4cSDong Jia Shi }
get_schid(struct vfio_ccw_private * p)2333cd90214SHalil Pasic inline struct subchannel_id get_schid(struct vfio_ccw_private *p)
2343cd90214SHalil Pasic {
235008a011dSEric Farman 	struct subchannel *sch = to_subchannel(p->vdev.dev->parent);
236008a011dSEric Farman 
237008a011dSEric Farman 	return sch->schid;
2383cd90214SHalil Pasic }
239bbe37e4cSDong Jia Shi 
240bbe37e4cSDong Jia Shi /*
241bbe37e4cSDong Jia Shi  * Deal with the ccw command request from the userspace.
242bbe37e4cSDong Jia Shi  */
fsm_io_request(struct vfio_ccw_private * private,enum vfio_ccw_event event)243bbe37e4cSDong Jia Shi static void fsm_io_request(struct vfio_ccw_private *private,
244bbe37e4cSDong Jia Shi 			   enum vfio_ccw_event event)
245bbe37e4cSDong Jia Shi {
246bbe37e4cSDong Jia Shi 	union orb *orb;
247bbe37e4cSDong Jia Shi 	union scsw *scsw = &private->scsw;
248c98e16b2SEric Farman 	struct ccw_io_region *io_region = private->io_region;
2493cd90214SHalil Pasic 	char *errstr = "request";
25060e05d1cSCornelia Huck 	struct subchannel_id schid = get_schid(private);
251bbe37e4cSDong Jia Shi 
252690f6a15SCornelia Huck 	private->state = VFIO_CCW_STATE_CP_PROCESSING;
253bbe37e4cSDong Jia Shi 	memcpy(scsw, io_region->scsw_area, sizeof(*scsw));
254bbe37e4cSDong Jia Shi 
255bbe37e4cSDong Jia Shi 	if (scsw->cmd.fctl & SCSW_FCTL_START_FUNC) {
256bbe37e4cSDong Jia Shi 		orb = (union orb *)io_region->orb_area;
257bbe37e4cSDong Jia Shi 
2589851bc77SCornelia Huck 		/* Don't try to build a cp if transport mode is specified. */
2599851bc77SCornelia Huck 		if (orb->tm.b) {
2609851bc77SCornelia Huck 			io_region->ret_code = -EOPNOTSUPP;
26160e05d1cSCornelia Huck 			VFIO_CCW_MSG_EVENT(2,
2623566ee1dSMichael Kawano 					   "sch %x.%x.%04x: transport mode\n",
2633566ee1dSMichael Kawano 					   schid.cssid,
26460e05d1cSCornelia Huck 					   schid.ssid, schid.sch_no);
2653cd90214SHalil Pasic 			errstr = "transport mode";
2669851bc77SCornelia Huck 			goto err_out;
2679851bc77SCornelia Huck 		}
2680a587956SJason Gunthorpe 		io_region->ret_code = cp_init(&private->cp, orb);
2693cd90214SHalil Pasic 		if (io_region->ret_code) {
27060e05d1cSCornelia Huck 			VFIO_CCW_MSG_EVENT(2,
2713566ee1dSMichael Kawano 					   "sch %x.%x.%04x: cp_init=%d\n",
2723566ee1dSMichael Kawano 					   schid.cssid,
27360e05d1cSCornelia Huck 					   schid.ssid, schid.sch_no,
27460e05d1cSCornelia Huck 					   io_region->ret_code);
2753cd90214SHalil Pasic 			errstr = "cp init";
276bbe37e4cSDong Jia Shi 			goto err_out;
2773cd90214SHalil Pasic 		}
278bbe37e4cSDong Jia Shi 
279bbe37e4cSDong Jia Shi 		io_region->ret_code = cp_prefetch(&private->cp);
280bbe37e4cSDong Jia Shi 		if (io_region->ret_code) {
28160e05d1cSCornelia Huck 			VFIO_CCW_MSG_EVENT(2,
2823566ee1dSMichael Kawano 					   "sch %x.%x.%04x: cp_prefetch=%d\n",
2833566ee1dSMichael Kawano 					   schid.cssid,
28460e05d1cSCornelia Huck 					   schid.ssid, schid.sch_no,
28560e05d1cSCornelia Huck 					   io_region->ret_code);
2863cd90214SHalil Pasic 			errstr = "cp prefetch";
287bbe37e4cSDong Jia Shi 			cp_free(&private->cp);
288bbe37e4cSDong Jia Shi 			goto err_out;
289bbe37e4cSDong Jia Shi 		}
290bbe37e4cSDong Jia Shi 
291bbe37e4cSDong Jia Shi 		/* Start channel program and wait for I/O interrupt. */
292bbe37e4cSDong Jia Shi 		io_region->ret_code = fsm_io_helper(private);
293bbe37e4cSDong Jia Shi 		if (io_region->ret_code) {
29460e05d1cSCornelia Huck 			VFIO_CCW_MSG_EVENT(2,
2953566ee1dSMichael Kawano 					   "sch %x.%x.%04x: fsm_io_helper=%d\n",
2963566ee1dSMichael Kawano 					   schid.cssid,
29760e05d1cSCornelia Huck 					   schid.ssid, schid.sch_no,
29860e05d1cSCornelia Huck 					   io_region->ret_code);
2993cd90214SHalil Pasic 			errstr = "cp fsm_io_helper";
300bbe37e4cSDong Jia Shi 			cp_free(&private->cp);
301bbe37e4cSDong Jia Shi 			goto err_out;
302bbe37e4cSDong Jia Shi 		}
303bbe37e4cSDong Jia Shi 		return;
304bbe37e4cSDong Jia Shi 	} else if (scsw->cmd.fctl & SCSW_FCTL_HALT_FUNC) {
30560e05d1cSCornelia Huck 		VFIO_CCW_MSG_EVENT(2,
3063566ee1dSMichael Kawano 				   "sch %x.%x.%04x: halt on io_region\n",
3073566ee1dSMichael Kawano 				   schid.cssid,
30860e05d1cSCornelia Huck 				   schid.ssid, schid.sch_no);
309d5afd5d1SCornelia Huck 		/* halt is handled via the async cmd region */
310bbe37e4cSDong Jia Shi 		io_region->ret_code = -EOPNOTSUPP;
311bbe37e4cSDong Jia Shi 		goto err_out;
312bbe37e4cSDong Jia Shi 	} else if (scsw->cmd.fctl & SCSW_FCTL_CLEAR_FUNC) {
31360e05d1cSCornelia Huck 		VFIO_CCW_MSG_EVENT(2,
3143566ee1dSMichael Kawano 				   "sch %x.%x.%04x: clear on io_region\n",
3153566ee1dSMichael Kawano 				   schid.cssid,
31660e05d1cSCornelia Huck 				   schid.ssid, schid.sch_no);
317d5afd5d1SCornelia Huck 		/* clear is handled via the async cmd region */
318bbe37e4cSDong Jia Shi 		io_region->ret_code = -EOPNOTSUPP;
319bbe37e4cSDong Jia Shi 		goto err_out;
320bbe37e4cSDong Jia Shi 	}
321bbe37e4cSDong Jia Shi 
322bbe37e4cSDong Jia Shi err_out:
3236c02ac4cSEric Farman 	private->state = VFIO_CCW_STATE_IDLE;
32485298880SEric Farman 	trace_vfio_ccw_fsm_io_request(scsw->cmd.fctl, schid,
3253cd90214SHalil Pasic 				      io_region->ret_code, errstr);
326bbe37e4cSDong Jia Shi }
327bbe37e4cSDong Jia Shi 
328bbe37e4cSDong Jia Shi /*
329d5afd5d1SCornelia Huck  * Deal with an async request from userspace.
330d5afd5d1SCornelia Huck  */
fsm_async_request(struct vfio_ccw_private * private,enum vfio_ccw_event event)331d5afd5d1SCornelia Huck static void fsm_async_request(struct vfio_ccw_private *private,
332d5afd5d1SCornelia Huck 			      enum vfio_ccw_event event)
333d5afd5d1SCornelia Huck {
334d5afd5d1SCornelia Huck 	struct ccw_cmd_region *cmd_region = private->cmd_region;
335d5afd5d1SCornelia Huck 
336d5afd5d1SCornelia Huck 	switch (cmd_region->command) {
337d5afd5d1SCornelia Huck 	case VFIO_CCW_ASYNC_CMD_HSCH:
338d5afd5d1SCornelia Huck 		cmd_region->ret_code = fsm_do_halt(private);
339d5afd5d1SCornelia Huck 		break;
340d5afd5d1SCornelia Huck 	case VFIO_CCW_ASYNC_CMD_CSCH:
341d5afd5d1SCornelia Huck 		cmd_region->ret_code = fsm_do_clear(private);
342d5afd5d1SCornelia Huck 		break;
343d5afd5d1SCornelia Huck 	default:
344d5afd5d1SCornelia Huck 		/* should not happen? */
345d5afd5d1SCornelia Huck 		cmd_region->ret_code = -EINVAL;
346d5afd5d1SCornelia Huck 	}
347d5950b02SEric Farman 
348d5950b02SEric Farman 	trace_vfio_ccw_fsm_async_request(get_schid(private),
349d5950b02SEric Farman 					 cmd_region->command,
350d5950b02SEric Farman 					 cmd_region->ret_code);
351d5afd5d1SCornelia Huck }
352d5afd5d1SCornelia Huck 
353d5afd5d1SCornelia Huck /*
354bbe37e4cSDong Jia Shi  * Got an interrupt for a normal io (state busy).
355bbe37e4cSDong Jia Shi  */
fsm_irq(struct vfio_ccw_private * private,enum vfio_ccw_event event)356bbe37e4cSDong Jia Shi static void fsm_irq(struct vfio_ccw_private *private,
357bbe37e4cSDong Jia Shi 		    enum vfio_ccw_event event)
358bbe37e4cSDong Jia Shi {
359008a011dSEric Farman 	struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
360c9c31b07SDong Jia Shi 	struct irb *irb = this_cpu_ptr(&cio_irb);
361bbe37e4cSDong Jia Shi 
36260e05d1cSCornelia Huck 	VFIO_CCW_TRACE_EVENT(6, "IRQ");
363008a011dSEric Farman 	VFIO_CCW_TRACE_EVENT(6, dev_name(&sch->dev));
36460e05d1cSCornelia Huck 
365bbe37e4cSDong Jia Shi 	memcpy(&private->irb, irb, sizeof(*irb));
366bbe37e4cSDong Jia Shi 
367bbe37e4cSDong Jia Shi 	queue_work(vfio_ccw_work_q, &private->io_work);
368bbe37e4cSDong Jia Shi 
369bbe37e4cSDong Jia Shi 	if (private->completion)
370bbe37e4cSDong Jia Shi 		complete(private->completion);
371bbe37e4cSDong Jia Shi }
372bbe37e4cSDong Jia Shi 
fsm_open(struct vfio_ccw_private * private,enum vfio_ccw_event event)37362ec0d49SEric Farman static void fsm_open(struct vfio_ccw_private *private,
37462ec0d49SEric Farman 		     enum vfio_ccw_event event)
37562ec0d49SEric Farman {
376008a011dSEric Farman 	struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
37762ec0d49SEric Farman 	int ret;
37862ec0d49SEric Farman 
37962ec0d49SEric Farman 	spin_lock_irq(sch->lock);
38062ec0d49SEric Farman 	sch->isc = VFIO_CCW_ISC;
38162ec0d49SEric Farman 	ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
382204b394aSEric Farman 	if (ret)
383204b394aSEric Farman 		goto err_unlock;
384204b394aSEric Farman 
385204b394aSEric Farman 	private->state = VFIO_CCW_STATE_IDLE;
38662ec0d49SEric Farman 	spin_unlock_irq(sch->lock);
387204b394aSEric Farman 	return;
388204b394aSEric Farman 
389204b394aSEric Farman err_unlock:
390204b394aSEric Farman 	spin_unlock_irq(sch->lock);
391204b394aSEric Farman 	vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_NOT_OPER);
39262ec0d49SEric Farman }
39362ec0d49SEric Farman 
fsm_close(struct vfio_ccw_private * private,enum vfio_ccw_event event)394f4b4ed44SEric Farman static void fsm_close(struct vfio_ccw_private *private,
395f4b4ed44SEric Farman 		      enum vfio_ccw_event event)
396f4b4ed44SEric Farman {
397008a011dSEric Farman 	struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
398f4b4ed44SEric Farman 	int ret;
399f4b4ed44SEric Farman 
400f4b4ed44SEric Farman 	spin_lock_irq(sch->lock);
401f4b4ed44SEric Farman 
402f4b4ed44SEric Farman 	if (!sch->schib.pmcw.ena)
403204b394aSEric Farman 		goto err_unlock;
404f4b4ed44SEric Farman 
405f4b4ed44SEric Farman 	ret = cio_disable_subchannel(sch);
406f4b4ed44SEric Farman 	if (ret == -EBUSY)
4074eb91966SEric Farman 		ret = vfio_ccw_sch_quiesce(sch);
408204b394aSEric Farman 	if (ret)
409204b394aSEric Farman 		goto err_unlock;
410f4b4ed44SEric Farman 
411204b394aSEric Farman 	private->state = VFIO_CCW_STATE_STANDBY;
412f4b4ed44SEric Farman 	spin_unlock_irq(sch->lock);
413f4b4ed44SEric Farman 	cp_free(&private->cp);
414204b394aSEric Farman 	return;
415204b394aSEric Farman 
416204b394aSEric Farman err_unlock:
417204b394aSEric Farman 	spin_unlock_irq(sch->lock);
418204b394aSEric Farman 	vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_NOT_OPER);
419f4b4ed44SEric Farman }
420f4b4ed44SEric Farman 
421bbe37e4cSDong Jia Shi /*
422bbe37e4cSDong Jia Shi  * Device statemachine
423bbe37e4cSDong Jia Shi  */
424bbe37e4cSDong Jia Shi fsm_func_t *vfio_ccw_jumptable[NR_VFIO_CCW_STATES][NR_VFIO_CCW_EVENTS] = {
425bbe37e4cSDong Jia Shi 	[VFIO_CCW_STATE_NOT_OPER] = {
426bbe37e4cSDong Jia Shi 		[VFIO_CCW_EVENT_NOT_OPER]	= fsm_nop,
427bbe37e4cSDong Jia Shi 		[VFIO_CCW_EVENT_IO_REQ]		= fsm_io_error,
428d5afd5d1SCornelia Huck 		[VFIO_CCW_EVENT_ASYNC_REQ]	= fsm_async_error,
429bbe37e4cSDong Jia Shi 		[VFIO_CCW_EVENT_INTERRUPT]	= fsm_disabled_irq,
430204b394aSEric Farman 		[VFIO_CCW_EVENT_OPEN]		= fsm_nop,
431f4b4ed44SEric Farman 		[VFIO_CCW_EVENT_CLOSE]		= fsm_nop,
432bbe37e4cSDong Jia Shi 	},
433bbe37e4cSDong Jia Shi 	[VFIO_CCW_STATE_STANDBY] = {
434bbe37e4cSDong Jia Shi 		[VFIO_CCW_EVENT_NOT_OPER]	= fsm_notoper,
435bbe37e4cSDong Jia Shi 		[VFIO_CCW_EVENT_IO_REQ]		= fsm_io_error,
436d5afd5d1SCornelia Huck 		[VFIO_CCW_EVENT_ASYNC_REQ]	= fsm_async_error,
437204b394aSEric Farman 		[VFIO_CCW_EVENT_INTERRUPT]	= fsm_disabled_irq,
438204b394aSEric Farman 		[VFIO_CCW_EVENT_OPEN]		= fsm_open,
439204b394aSEric Farman 		[VFIO_CCW_EVENT_CLOSE]		= fsm_notoper,
440bbe37e4cSDong Jia Shi 	},
441bbe37e4cSDong Jia Shi 	[VFIO_CCW_STATE_IDLE] = {
442bbe37e4cSDong Jia Shi 		[VFIO_CCW_EVENT_NOT_OPER]	= fsm_notoper,
443bbe37e4cSDong Jia Shi 		[VFIO_CCW_EVENT_IO_REQ]		= fsm_io_request,
444d5afd5d1SCornelia Huck 		[VFIO_CCW_EVENT_ASYNC_REQ]	= fsm_async_request,
445bbe37e4cSDong Jia Shi 		[VFIO_CCW_EVENT_INTERRUPT]	= fsm_irq,
44662ec0d49SEric Farman 		[VFIO_CCW_EVENT_OPEN]		= fsm_notoper,
447f4b4ed44SEric Farman 		[VFIO_CCW_EVENT_CLOSE]		= fsm_close,
448bbe37e4cSDong Jia Shi 	},
449690f6a15SCornelia Huck 	[VFIO_CCW_STATE_CP_PROCESSING] = {
450690f6a15SCornelia Huck 		[VFIO_CCW_EVENT_NOT_OPER]	= fsm_notoper,
451690f6a15SCornelia Huck 		[VFIO_CCW_EVENT_IO_REQ]		= fsm_io_retry,
452d5afd5d1SCornelia Huck 		[VFIO_CCW_EVENT_ASYNC_REQ]	= fsm_async_retry,
453690f6a15SCornelia Huck 		[VFIO_CCW_EVENT_INTERRUPT]	= fsm_irq,
45462ec0d49SEric Farman 		[VFIO_CCW_EVENT_OPEN]		= fsm_notoper,
455f4b4ed44SEric Farman 		[VFIO_CCW_EVENT_CLOSE]		= fsm_close,
456690f6a15SCornelia Huck 	},
457690f6a15SCornelia Huck 	[VFIO_CCW_STATE_CP_PENDING] = {
458bbe37e4cSDong Jia Shi 		[VFIO_CCW_EVENT_NOT_OPER]	= fsm_notoper,
459bbe37e4cSDong Jia Shi 		[VFIO_CCW_EVENT_IO_REQ]		= fsm_io_busy,
460d5afd5d1SCornelia Huck 		[VFIO_CCW_EVENT_ASYNC_REQ]	= fsm_async_request,
461bbe37e4cSDong Jia Shi 		[VFIO_CCW_EVENT_INTERRUPT]	= fsm_irq,
46262ec0d49SEric Farman 		[VFIO_CCW_EVENT_OPEN]		= fsm_notoper,
463f4b4ed44SEric Farman 		[VFIO_CCW_EVENT_CLOSE]		= fsm_close,
464bbe37e4cSDong Jia Shi 	},
465bbe37e4cSDong Jia Shi };
466