blob: 1e210c6afa8898e4f2588644caa7fcc163f246fc [file] [log] [blame]
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -03001==================================
Dong Jia Shi25627ba2017-03-17 04:17:41 +01002vfio-ccw: the basic infrastructure
3==================================
4
5Introduction
6------------
7
8Here we describe the vfio support for I/O subchannel devices for
9Linux/s390. Motivation for vfio-ccw is to passthrough subchannels to a
10virtual machine, while vfio is the means.
11
12Different than other hardware architectures, s390 has defined a unified
13I/O access method, which is so called Channel I/O. It has its own access
14patterns:
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -030015
Dong Jia Shi25627ba2017-03-17 04:17:41 +010016- Channel programs run asynchronously on a separate (co)processor.
17- The channel subsystem will access any memory designated by the caller
18 in the channel program directly, i.e. there is no iommu involved.
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -030019
Dong Jia Shi25627ba2017-03-17 04:17:41 +010020Thus when we introduce vfio support for these devices, we realize it
21with a mediated device (mdev) implementation. The vfio mdev will be
22added to an iommu group, so as to make itself able to be managed by the
23vfio framework. And we add read/write callbacks for special vfio I/O
24regions to pass the channel programs from the mdev to its parent device
25(the real I/O subchannel device) to do further address translation and
26to perform I/O instructions.
27
28This document does not intend to explain the s390 I/O architecture in
29every detail. More information/reference could be found here:
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -030030
Dong Jia Shi25627ba2017-03-17 04:17:41 +010031- A good start to know Channel I/O in general:
32 https://en.wikipedia.org/wiki/Channel_I/O
33- s390 architecture:
34 s390 Principles of Operation manual (IBM Form. No. SA22-7832)
Cornelia Huck69cfd922018-01-11 17:58:43 +010035- The existing QEMU code which implements a simple emulated channel
Dong Jia Shi25627ba2017-03-17 04:17:41 +010036 subsystem could also be a good reference. It makes it easier to follow
37 the flow.
38 qemu/hw/s390x/css.c
39
40For vfio mediated device framework:
Mauro Carvalho Chehabbaa293e2019-06-27 15:39:22 -030041- Documentation/driver-api/vfio-mediated-device.rst
Dong Jia Shi25627ba2017-03-17 04:17:41 +010042
43Motivation of vfio-ccw
44----------------------
45
Cornelia Huck69cfd922018-01-11 17:58:43 +010046Typically, a guest virtualized via QEMU/KVM on s390 only sees
Dong Jia Shi25627ba2017-03-17 04:17:41 +010047paravirtualized virtio devices via the "Virtio Over Channel I/O
48(virtio-ccw)" transport. This makes virtio devices discoverable via
49standard operating system algorithms for handling channel devices.
50
51However this is not enough. On s390 for the majority of devices, which
52use the standard Channel I/O based mechanism, we also need to provide
Cornelia Huck69cfd922018-01-11 17:58:43 +010053the functionality of passing through them to a QEMU virtual machine.
Dong Jia Shi25627ba2017-03-17 04:17:41 +010054This includes devices that don't have a virtio counterpart (e.g. tape
55drives) or that have specific characteristics which guests want to
56exploit.
57
58For passing a device to a guest, we want to use the same interface as
Cornelia Huck69cfd922018-01-11 17:58:43 +010059everybody else, namely vfio. We implement this vfio support for channel
60devices via the vfio mediated device framework and the subchannel device
61driver "vfio_ccw".
Dong Jia Shi25627ba2017-03-17 04:17:41 +010062
63Access patterns of CCW devices
64------------------------------
65
66s390 architecture has implemented a so called channel subsystem, that
67provides a unified view of the devices physically attached to the
68systems. Though the s390 hardware platform knows about a huge variety of
69different peripheral attachments like disk devices (aka. DASDs), tapes,
70communication controllers, etc. They can all be accessed by a well
71defined access method and they are presenting I/O completion a unified
72way: I/O interruptions.
73
74All I/O requires the use of channel command words (CCWs). A CCW is an
75instruction to a specialized I/O channel processor. A channel program is
76a sequence of CCWs which are executed by the I/O channel subsystem. To
77issue a channel program to the channel subsystem, it is required to
78build an operation request block (ORB), which can be used to point out
79the format of the CCW and other control information to the system. The
80operating system signals the I/O channel subsystem to begin executing
81the channel program with a SSCH (start sub-channel) instruction. The
82central processor is then free to proceed with non-I/O instructions
83until interrupted. The I/O completion result is received by the
84interrupt handler in the form of interrupt response block (IRB).
85
86Back to vfio-ccw, in short:
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -030087
Dong Jia Shi25627ba2017-03-17 04:17:41 +010088- ORBs and channel programs are built in guest kernel (with guest
89 physical addresses).
90- ORBs and channel programs are passed to the host kernel.
91- Host kernel translates the guest physical addresses to real addresses
92 and starts the I/O with issuing a privileged Channel I/O instruction
93 (e.g SSCH).
94- channel programs run asynchronously on a separate processor.
95- I/O completion will be signaled to the host with I/O interruptions.
96 And it will be copied as IRB to user space to pass it back to the
97 guest.
98
99Physical vfio ccw device and its child mdev
100-------------------------------------------
101
102As mentioned above, we realize vfio-ccw with a mdev implementation.
103
104Channel I/O does not have IOMMU hardware support, so the physical
105vfio-ccw device does not have an IOMMU level translation or isolation.
106
Cornelia Huck69cfd922018-01-11 17:58:43 +0100107Subchannel I/O instructions are all privileged instructions. When
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100108handling the I/O instruction interception, vfio-ccw has the software
109policing and translation how the channel program is programmed before
110it gets sent to hardware.
111
112Within this implementation, we have two drivers for two types of
113devices:
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -0300114
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100115- The vfio_ccw driver for the physical subchannel device.
116 This is an I/O subchannel driver for the real subchannel device. It
117 realizes a group of callbacks and registers to the mdev framework as a
118 parent (physical) device. As a consequence, mdev provides vfio_ccw a
119 generic interface (sysfs) to create mdev devices. A vfio mdev could be
120 created by vfio_ccw then and added to the mediated bus. It is the vfio
121 device that added to an IOMMU group and a vfio group.
122 vfio_ccw also provides an I/O region to accept channel program
123 request from user space and store I/O interrupt result for user
124 space to retrieve. To notify user space an I/O completion, it offers
125 an interface to setup an eventfd fd for asynchronous signaling.
126
127- The vfio_mdev driver for the mediated vfio ccw device.
128 This is provided by the mdev framework. It is a vfio device driver for
129 the mdev that created by vfio_ccw.
Cornelia Huck69cfd922018-01-11 17:58:43 +0100130 It realizes a group of vfio device driver callbacks, adds itself to a
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100131 vfio group, and registers itself to the mdev framework as a mdev
132 driver.
133 It uses a vfio iommu backend that uses the existing map and unmap
134 ioctls, but rather than programming them into an IOMMU for a device,
135 it simply stores the translations for use by later requests. This
136 means that a device programmed in a VM with guest physical addresses
137 can have the vfio kernel convert that address to process virtual
138 address, pin the page and program the hardware with the host physical
139 address in one step.
140 For a mdev, the vfio iommu backend will not pin the pages during the
141 VFIO_IOMMU_MAP_DMA ioctl. Mdev framework will only maintain a database
142 of the iova<->vaddr mappings in this operation. And they export a
143 vfio_pin_pages and a vfio_unpin_pages interfaces from the vfio iommu
144 backend for the physical devices to pin and unpin pages by demand.
145
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -0300146Below is a high Level block diagram::
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100147
148 +-------------+
149 | |
150 | +---------+ | mdev_register_driver() +--------------+
151 | | Mdev | +<-----------------------+ |
152 | | bus | | | vfio_mdev.ko |
153 | | driver | +----------------------->+ |<-> VFIO user
154 | +---------+ | probe()/remove() +--------------+ APIs
155 | |
156 | MDEV CORE |
157 | MODULE |
158 | mdev.ko |
159 | +---------+ | mdev_register_device() +--------------+
160 | |Physical | +<-----------------------+ |
161 | | device | | | vfio_ccw.ko |<-> subchannel
162 | |interface| +----------------------->+ | device
163 | +---------+ | callback +--------------+
164 +-------------+
165
166The process of how these work together.
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -0300167
Dong Jia Shi25627ba2017-03-17 04:17:41 +01001681. vfio_ccw.ko drives the physical I/O subchannel, and registers the
169 physical device (with callbacks) to mdev framework.
170 When vfio_ccw probing the subchannel device, it registers device
171 pointer and callbacks to the mdev framework. Mdev related file nodes
172 under the device node in sysfs would be created for the subchannel
173 device, namely 'mdev_create', 'mdev_destroy' and
174 'mdev_supported_types'.
1752. Create a mediated vfio ccw device.
176 Use the 'mdev_create' sysfs file, we need to manually create one (and
177 only one for our case) mediated device.
1783. vfio_mdev.ko drives the mediated ccw device.
179 vfio_mdev is also the vfio device drvier. It will probe the mdev and
180 add it to an iommu_group and a vfio_group. Then we could pass through
181 the mdev to a guest.
182
183vfio-ccw I/O region
184-------------------
185
186An I/O region is used to accept channel program request from user
187space and store I/O interrupt result for user space to retrieve. The
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -0300188definition of the region is::
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100189
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -0300190 struct ccw_io_region {
191 #define ORB_AREA_SIZE 12
192 __u8 orb_area[ORB_AREA_SIZE];
193 #define SCSW_AREA_SIZE 12
194 __u8 scsw_area[SCSW_AREA_SIZE];
195 #define IRB_AREA_SIZE 96
196 __u8 irb_area[IRB_AREA_SIZE];
197 __u32 ret_code;
198 } __packed;
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100199
200While starting an I/O request, orb_area should be filled with the
201guest ORB, and scsw_area should be filled with the SCSW of the Virtual
202Subchannel.
203
204irb_area stores the I/O result.
205
206ret_code stores a return code for each access of the region.
207
Cornelia Huck69cfd922018-01-11 17:58:43 +0100208vfio-ccw operation details
209--------------------------
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100210
Cornelia Huck69cfd922018-01-11 17:58:43 +0100211vfio-ccw follows what vfio-pci did on the s390 platform and uses
212vfio-iommu-type1 as the vfio iommu backend.
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100213
214* CCW translation APIs
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -0300215 A group of APIs (start with `cp_`) to do CCW translation. The CCWs
Cornelia Huck69cfd922018-01-11 17:58:43 +0100216 passed in by a user space program are organized with their guest
217 physical memory addresses. These APIs will copy the CCWs into kernel
218 space, and assemble a runnable kernel channel program by updating the
219 guest physical addresses with their corresponding host physical addresses.
220 Note that we have to use IDALs even for direct-access CCWs, as the
221 referenced memory can be located anywhere, including above 2G.
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100222
223* vfio_ccw device driver
Cornelia Huck69cfd922018-01-11 17:58:43 +0100224 This driver utilizes the CCW translation APIs and introduces
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100225 vfio_ccw, which is the driver for the I/O subchannel devices you want
226 to pass through.
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -0300227 vfio_ccw implements the following vfio ioctls::
228
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100229 VFIO_DEVICE_GET_INFO
230 VFIO_DEVICE_GET_IRQ_INFO
231 VFIO_DEVICE_GET_REGION_INFO
232 VFIO_DEVICE_RESET
233 VFIO_DEVICE_SET_IRQS
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -0300234
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100235 This provides an I/O region, so that the user space program can pass a
236 channel program to the kernel, to do further CCW translation before
237 issuing them to a real device.
238 This also provides the SET_IRQ ioctl to setup an event notifier to
239 notify the user space program the I/O completion in an asynchronous
240 way.
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100241
Cornelia Huck69cfd922018-01-11 17:58:43 +0100242The use of vfio-ccw is not limited to QEMU, while QEMU is definitely a
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100243good example to get understand how these patches work. Here is a little
Cornelia Huck69cfd922018-01-11 17:58:43 +0100244bit more detail how an I/O request triggered by the QEMU guest will be
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100245handled (without error handling).
246
247Explanation:
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100248
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -0300249- Q1-Q7: QEMU side process.
250- K1-K5: Kernel side process.
251
252Q1.
253 Get I/O region info during initialization.
254
255Q2.
256 Setup event notifier and handler to handle I/O completion.
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100257
258... ...
259
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -0300260Q3.
261 Intercept a ssch instruction.
262Q4.
263 Write the guest channel program and ORB to the I/O region.
264
265 K1.
266 Copy from guest to kernel.
267 K2.
268 Translate the guest channel program to a host kernel space
269 channel program, which becomes runnable for a real device.
270 K3.
271 With the necessary information contained in the orb passed in
272 by QEMU, issue the ccwchain to the device.
273 K4.
274 Return the ssch CC code.
275Q5.
276 Return the CC code to the guest.
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100277
278... ...
279
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -0300280 K5.
281 Interrupt handler gets the I/O result and write the result to
282 the I/O region.
283 K6.
284 Signal QEMU to retrieve the result.
285
286Q6.
287 Get the signal and event handler reads out the result from the I/O
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100288 region.
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -0300289Q7.
290 Update the irb for the guest.
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100291
292Limitations
293-----------
294
295The current vfio-ccw implementation focuses on supporting basic commands
296needed to implement block device functionality (read/write) of DASD/ECKD
297device only. Some commands may need special handling in the future, for
298example, anything related to path grouping.
299
300DASD is a kind of storage device. While ECKD is a data recording format.
301More information for DASD and ECKD could be found here:
302https://en.wikipedia.org/wiki/Direct-access_storage_device
303https://en.wikipedia.org/wiki/Count_key_data
304
Cornelia Huck69cfd922018-01-11 17:58:43 +0100305Together with the corresponding work in QEMU, we can bring the passed
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100306through DASD/ECKD device online in a guest now and use it as a block
307device.
308
Cornelia Huck69cfd922018-01-11 17:58:43 +0100309While the current code allows the guest to start channel programs via
310START SUBCHANNEL, support for HALT SUBCHANNEL or CLEAR SUBCHANNEL is
311not yet implemented.
312
313vfio-ccw supports classic (command mode) channel I/O only. Transport
314mode (HPF) is not supported.
315
316QDIO subchannels are currently not supported. Classic devices other than
317DASD/ECKD might work, but have not been tested.
318
Dong Jia Shi25627ba2017-03-17 04:17:41 +0100319Reference
320---------
3211. ESA/s390 Principles of Operation manual (IBM Form. No. SA22-7832)
3222. ESA/390 Common I/O Device Commands manual (IBM Form. No. SA22-7204)
3233. https://en.wikipedia.org/wiki/Channel_I/O
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -03003244. Documentation/s390/cds.rst
Mauro Carvalho Chehabbaa293e2019-06-27 15:39:22 -03003255. Documentation/driver-api/vfio.rst
3266. Documentation/driver-api/vfio-mediated-device.rst