1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Private stuff for vfio_ccw driver
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 *            Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
10 *            Cornelia Huck <cohuck@redhat.com>
11 */
12
13#ifndef _VFIO_CCW_PRIVATE_H_
14#define _VFIO_CCW_PRIVATE_H_
15
16#include <linux/completion.h>
17#include <linux/eventfd.h>
18#include <linux/workqueue.h>
19#include <linux/vfio_ccw.h>
20#include <linux/vfio.h>
21#include <linux/mdev.h>
22#include <asm/crw.h>
23#include <asm/debug.h>
24
25#include "css.h"
26#include "vfio_ccw_cp.h"
27
28#define VFIO_CCW_OFFSET_SHIFT   10
29#define VFIO_CCW_OFFSET_TO_INDEX(off)	(off >> VFIO_CCW_OFFSET_SHIFT)
30#define VFIO_CCW_INDEX_TO_OFFSET(index)	((u64)(index) << VFIO_CCW_OFFSET_SHIFT)
31#define VFIO_CCW_OFFSET_MASK	(((u64)(1) << VFIO_CCW_OFFSET_SHIFT) - 1)
32
33/* capability chain handling similar to vfio-pci */
34struct vfio_ccw_private;
35struct vfio_ccw_region;
36
37struct vfio_ccw_regops {
38	ssize_t	(*read)(struct vfio_ccw_private *private, char __user *buf,
39			size_t count, loff_t *ppos);
40	ssize_t	(*write)(struct vfio_ccw_private *private,
41			 const char __user *buf, size_t count, loff_t *ppos);
42	void	(*release)(struct vfio_ccw_private *private,
43			   struct vfio_ccw_region *region);
44};
45
46struct vfio_ccw_region {
47	u32				type;
48	u32				subtype;
49	const struct vfio_ccw_regops	*ops;
50	void				*data;
51	size_t				size;
52	u32				flags;
53};
54
55int vfio_ccw_register_dev_region(struct vfio_ccw_private *private,
56				 unsigned int subtype,
57				 const struct vfio_ccw_regops *ops,
58				 size_t size, u32 flags, void *data);
59void vfio_ccw_unregister_dev_regions(struct vfio_ccw_private *private);
60
61int vfio_ccw_register_async_dev_regions(struct vfio_ccw_private *private);
62int vfio_ccw_register_schib_dev_regions(struct vfio_ccw_private *private);
63int vfio_ccw_register_crw_dev_regions(struct vfio_ccw_private *private);
64
65struct vfio_ccw_crw {
66	struct list_head	next;
67	struct crw		crw;
68};
69
70/**
71 * struct vfio_ccw_parent
72 *
73 * @dev: embedded device struct
74 * @parent: parent data structures for mdevs created
75 * @mdev_type(s): identifying information for mdevs created
76 */
77struct vfio_ccw_parent {
78	struct device		dev;
79
80	struct mdev_parent	parent;
81	struct mdev_type	mdev_type;
82	struct mdev_type	*mdev_types[];
83};
84
85/**
86 * struct vfio_ccw_private
87 * @vdev: Embedded VFIO device
88 * @state: internal state of the device
89 * @completion: synchronization helper of the I/O completion
90 * @io_region: MMIO region to input/output I/O arguments/results
91 * @io_mutex: protect against concurrent update of I/O regions
92 * @region: additional regions for other subchannel operations
93 * @cmd_region: MMIO region for asynchronous I/O commands other than START
94 * @schib_region: MMIO region for SCHIB information
95 * @crw_region: MMIO region for getting channel report words
96 * @num_regions: number of additional regions
97 * @cp: channel program for the current I/O operation
98 * @irb: irb info received from interrupt
99 * @scsw: scsw info
100 * @io_trigger: eventfd ctx for signaling userspace I/O results
101 * @crw_trigger: eventfd ctx for signaling userspace CRW information
102 * @req_trigger: eventfd ctx for signaling userspace to return device
103 * @io_work: work for deferral process of I/O handling
104 * @crw_work: work for deferral process of CRW handling
105 */
106struct vfio_ccw_private {
107	struct vfio_device vdev;
108	int			state;
109	struct completion	*completion;
110	struct ccw_io_region	*io_region;
111	struct mutex		io_mutex;
112	struct vfio_ccw_region *region;
113	struct ccw_cmd_region	*cmd_region;
114	struct ccw_schib_region *schib_region;
115	struct ccw_crw_region	*crw_region;
116	int num_regions;
117
118	struct channel_program	cp;
119	struct irb		irb;
120	union scsw		scsw;
121	struct list_head	crw;
122
123	struct eventfd_ctx	*io_trigger;
124	struct eventfd_ctx	*crw_trigger;
125	struct eventfd_ctx	*req_trigger;
126	struct work_struct	io_work;
127	struct work_struct	crw_work;
128} __aligned(8);
129
130int vfio_ccw_sch_quiesce(struct subchannel *sch);
131void vfio_ccw_sch_io_todo(struct work_struct *work);
132void vfio_ccw_crw_todo(struct work_struct *work);
133
134extern struct mdev_driver vfio_ccw_mdev_driver;
135
136/*
137 * States of the device statemachine.
138 */
139enum vfio_ccw_state {
140	VFIO_CCW_STATE_NOT_OPER,
141	VFIO_CCW_STATE_STANDBY,
142	VFIO_CCW_STATE_IDLE,
143	VFIO_CCW_STATE_CP_PROCESSING,
144	VFIO_CCW_STATE_CP_PENDING,
145	/* last element! */
146	NR_VFIO_CCW_STATES
147};
148
149/*
150 * Asynchronous events of the device statemachine.
151 */
152enum vfio_ccw_event {
153	VFIO_CCW_EVENT_NOT_OPER,
154	VFIO_CCW_EVENT_IO_REQ,
155	VFIO_CCW_EVENT_INTERRUPT,
156	VFIO_CCW_EVENT_ASYNC_REQ,
157	VFIO_CCW_EVENT_OPEN,
158	VFIO_CCW_EVENT_CLOSE,
159	/* last element! */
160	NR_VFIO_CCW_EVENTS
161};
162
163/*
164 * Action called through jumptable.
165 */
166typedef void (fsm_func_t)(struct vfio_ccw_private *, enum vfio_ccw_event);
167extern fsm_func_t *vfio_ccw_jumptable[NR_VFIO_CCW_STATES][NR_VFIO_CCW_EVENTS];
168
169static inline void vfio_ccw_fsm_event(struct vfio_ccw_private *private,
170				      enum vfio_ccw_event event)
171{
172	struct subchannel *sch = to_subchannel(private->vdev.dev->parent);
173
174	if (sch)
175		trace_vfio_ccw_fsm_event(sch->schid, private->state, event);
176	vfio_ccw_jumptable[private->state][event](private, event);
177}
178
179extern struct workqueue_struct *vfio_ccw_work_q;
180extern struct kmem_cache *vfio_ccw_io_region;
181extern struct kmem_cache *vfio_ccw_cmd_region;
182extern struct kmem_cache *vfio_ccw_schib_region;
183extern struct kmem_cache *vfio_ccw_crw_region;
184
185/* s390 debug feature, similar to base cio */
186extern debug_info_t *vfio_ccw_debug_msg_id;
187extern debug_info_t *vfio_ccw_debug_trace_id;
188
189#define VFIO_CCW_TRACE_EVENT(imp, txt) \
190		debug_text_event(vfio_ccw_debug_trace_id, imp, txt)
191
192#define VFIO_CCW_MSG_EVENT(imp, args...) \
193		debug_sprintf_event(vfio_ccw_debug_msg_id, imp, ##args)
194
195static inline void VFIO_CCW_HEX_EVENT(int level, void *data, int length)
196{
197	debug_event(vfio_ccw_debug_trace_id, level, data, length);
198}
199
200#endif
201