d46e98c7c1ec6cabe1316c7122cea4a33933927f
[linux-block.git] / Documentation / s390 / vfio-ap.rst
1 ===============================
2 Adjunct Processor (AP) facility
3 ===============================
4
5
6 Introduction
7 ============
8 The Adjunct Processor (AP) facility is an IBM Z cryptographic facility comprised
9 of three AP instructions and from 1 up to 256 PCIe cryptographic adapter cards.
10 The AP devices provide cryptographic functions to all CPUs assigned to a
11 linux system running in an IBM Z system LPAR.
12
13 The AP adapter cards are exposed via the AP bus. The motivation for vfio-ap
14 is to make AP cards available to KVM guests using the VFIO mediated device
15 framework. This implementation relies considerably on the s390 virtualization
16 facilities which do most of the hard work of providing direct access to AP
17 devices.
18
19 AP Architectural Overview
20 =========================
21 To facilitate the comprehension of the design, let's start with some
22 definitions:
23
24 * AP adapter
25
26   An AP adapter is an IBM Z adapter card that can perform cryptographic
27   functions. There can be from 0 to 256 adapters assigned to an LPAR. Adapters
28   assigned to the LPAR in which a linux host is running will be available to
29   the linux host. Each adapter is identified by a number from 0 to 255; however,
30   the maximum adapter number is determined by machine model and/or adapter type.
31   When installed, an AP adapter is accessed by AP instructions executed by any
32   CPU.
33
34   The AP adapter cards are assigned to a given LPAR via the system's Activation
35   Profile which can be edited via the HMC. When the linux host system is IPL'd
36   in the LPAR, the AP bus detects the AP adapter cards assigned to the LPAR and
37   creates a sysfs device for each assigned adapter. For example, if AP adapters
38   4 and 10 (0x0a) are assigned to the LPAR, the AP bus will create the following
39   sysfs device entries::
40
41     /sys/devices/ap/card04
42     /sys/devices/ap/card0a
43
44   Symbolic links to these devices will also be created in the AP bus devices
45   sub-directory::
46
47     /sys/bus/ap/devices/[card04]
48     /sys/bus/ap/devices/[card04]
49
50 * AP domain
51
52   An adapter is partitioned into domains. An adapter can hold up to 256 domains
53   depending upon the adapter type and hardware configuration. A domain is
54   identified by a number from 0 to 255; however, the maximum domain number is
55   determined by machine model and/or adapter type.. A domain can be thought of
56   as a set of hardware registers and memory used for processing AP commands. A
57   domain can be configured with a secure private key used for clear key
58   encryption. A domain is classified in one of two ways depending upon how it
59   may be accessed:
60
61     * Usage domains are domains that are targeted by an AP instruction to
62       process an AP command.
63
64     * Control domains are domains that are changed by an AP command sent to a
65       usage domain; for example, to set the secure private key for the control
66       domain.
67
68   The AP usage and control domains are assigned to a given LPAR via the system's
69   Activation Profile which can be edited via the HMC. When a linux host system
70   is IPL'd in the LPAR, the AP bus module detects the AP usage and control
71   domains assigned to the LPAR. The domain number of each usage domain and
72   adapter number of each AP adapter are combined to create AP queue devices
73   (see AP Queue section below). The domain number of each control domain will be
74   represented in a bitmask and stored in a sysfs file
75   /sys/bus/ap/ap_control_domain_mask. The bits in the mask, from most to least
76   significant bit, correspond to domains 0-255.
77
78 * AP Queue
79
80   An AP queue is the means by which an AP command is sent to a usage domain
81   inside a specific adapter. An AP queue is identified by a tuple
82   comprised of an AP adapter ID (APID) and an AP queue index (APQI). The
83   APQI corresponds to a given usage domain number within the adapter. This tuple
84   forms an AP Queue Number (APQN) uniquely identifying an AP queue. AP
85   instructions include a field containing the APQN to identify the AP queue to
86   which the AP command is to be sent for processing.
87
88   The AP bus will create a sysfs device for each APQN that can be derived from
89   the cross product of the AP adapter and usage domain numbers detected when the
90   AP bus module is loaded. For example, if adapters 4 and 10 (0x0a) and usage
91   domains 6 and 71 (0x47) are assigned to the LPAR, the AP bus will create the
92   following sysfs entries::
93
94     /sys/devices/ap/card04/04.0006
95     /sys/devices/ap/card04/04.0047
96     /sys/devices/ap/card0a/0a.0006
97     /sys/devices/ap/card0a/0a.0047
98
99   The following symbolic links to these devices will be created in the AP bus
100   devices subdirectory::
101
102     /sys/bus/ap/devices/[04.0006]
103     /sys/bus/ap/devices/[04.0047]
104     /sys/bus/ap/devices/[0a.0006]
105     /sys/bus/ap/devices/[0a.0047]
106
107 * AP Instructions:
108
109   There are three AP instructions:
110
111   * NQAP: to enqueue an AP command-request message to a queue
112   * DQAP: to dequeue an AP command-reply message from a queue
113   * PQAP: to administer the queues
114
115   AP instructions identify the domain that is targeted to process the AP
116   command; this must be one of the usage domains. An AP command may modify a
117   domain that is not one of the usage domains, but the modified domain
118   must be one of the control domains.
119
120 AP and SIE
121 ==========
122 Let's now take a look at how AP instructions executed on a guest are interpreted
123 by the hardware.
124
125 A satellite control block called the Crypto Control Block (CRYCB) is attached to
126 our main hardware virtualization control block. The CRYCB contains an AP Control
127 Block (APCB) that has three fields to identify the adapters, usage domains and
128 control domains assigned to the KVM guest:
129
130 * The AP Mask (APM) field is a bit mask that identifies the AP adapters assigned
131   to the KVM guest. Each bit in the mask, from left to right, corresponds to
132   an APID from 0-255. If a bit is set, the corresponding adapter is valid for
133   use by the KVM guest.
134
135 * The AP Queue Mask (AQM) field is a bit mask identifying the AP usage domains
136   assigned to the KVM guest. Each bit in the mask, from left to right,
137   corresponds to an AP queue index (APQI) from 0-255. If a bit is set, the
138   corresponding queue is valid for use by the KVM guest.
139
140 * The AP Domain Mask field is a bit mask that identifies the AP control domains
141   assigned to the KVM guest. The ADM bit mask controls which domains can be
142   changed by an AP command-request message sent to a usage domain from the
143   guest. Each bit in the mask, from left to right, corresponds to a domain from
144   0-255. If a bit is set, the corresponding domain can be modified by an AP
145   command-request message sent to a usage domain.
146
147 If you recall from the description of an AP Queue, AP instructions include
148 an APQN to identify the AP queue to which an AP command-request message is to be
149 sent (NQAP and PQAP instructions), or from which a command-reply message is to
150 be received (DQAP instruction). The validity of an APQN is defined by the matrix
151 calculated from the APM and AQM; it is the Cartesian product of all assigned
152 adapter numbers (APM) with all assigned queue indexes (AQM). For example, if
153 adapters 1 and 2 and usage domains 5 and 6 are assigned to a guest, the APQNs
154 (1,5), (1,6), (2,5) and (2,6) will be valid for the guest.
155
156 The APQNs can provide secure key functionality - i.e., a private key is stored
157 on the adapter card for each of its domains - so each APQN must be assigned to
158 at most one guest or to the linux host::
159
160    Example 1: Valid configuration:
161    ------------------------------
162    Guest1: adapters 1,2  domains 5,6
163    Guest2: adapter  1,2  domain 7
164
165    This is valid because both guests have a unique set of APQNs:
166       Guest1 has APQNs (1,5), (1,6), (2,5), (2,6);
167       Guest2 has APQNs (1,7), (2,7)
168
169    Example 2: Valid configuration:
170    ------------------------------
171    Guest1: adapters 1,2 domains 5,6
172    Guest2: adapters 3,4 domains 5,6
173
174    This is also valid because both guests have a unique set of APQNs:
175       Guest1 has APQNs (1,5), (1,6), (2,5), (2,6);
176       Guest2 has APQNs (3,5), (3,6), (4,5), (4,6)
177
178    Example 3: Invalid configuration:
179    --------------------------------
180    Guest1: adapters 1,2  domains 5,6
181    Guest2: adapter  1    domains 6,7
182
183    This is an invalid configuration because both guests have access to
184    APQN (1,6).
185
186 The Design
187 ==========
188 The design introduces three new objects:
189
190 1. AP matrix device
191 2. VFIO AP device driver (vfio_ap.ko)
192 3. VFIO AP mediated pass-through device
193
194 The VFIO AP device driver
195 -------------------------
196 The VFIO AP (vfio_ap) device driver serves the following purposes:
197
198 1. Provides the interfaces to secure APQNs for exclusive use of KVM guests.
199
200 2. Sets up the VFIO mediated device interfaces to manage a vfio_ap mediated
201    device and creates the sysfs interfaces for assigning adapters, usage
202    domains, and control domains comprising the matrix for a KVM guest.
203
204 3. Configures the APM, AQM and ADM in the APCB contained in the CRYCB referenced
205    by a KVM guest's SIE state description to grant the guest access to a matrix
206    of AP devices
207
208 Reserve APQNs for exclusive use of KVM guests
209 ---------------------------------------------
210 The following block diagram illustrates the mechanism by which APQNs are
211 reserved::
212
213                                 +------------------+
214                  7 remove       |                  |
215            +--------------------> cex4queue driver |
216            |                    |                  |
217            |                    +------------------+
218            |
219            |
220            |                    +------------------+          +----------------+
221            |  5 register driver |                  | 3 create |                |
222            |   +---------------->   Device core    +---------->  matrix device |
223            |   |                |                  |          |                |
224            |   |                +--------^---------+          +----------------+
225            |   |                         |
226            |   |                         +-------------------+
227            |   | +-----------------------------------+       |
228            |   | |      4 register AP driver         |       | 2 register device
229            |   | |                                   |       |
230   +--------+---+-v---+                      +--------+-------+-+
231   |                  |                      |                  |
232   |      ap_bus      +--------------------- >  vfio_ap driver  |
233   |                  |       8 probe        |                  |
234   +--------^---------+                      +--^--^------------+
235   6 edit   |                                   |  |
236     apmask |     +-----------------------------+  | 11 mdev create
237     aqmask |     |           1 modprobe           |
238   +--------+-----+---+           +----------------+-+         +----------------+
239   |                  |           |                  |10 create|     mediated   |
240   |      admin       |           | VFIO device core |--------->     matrix     |
241   |                  +           |                  |         |     device     |
242   +------+-+---------+           +--------^---------+         +--------^-------+
243          | |                              |                            |
244          | | 9 create vfio_ap-passthrough |                            |
245          | +------------------------------+                            |
246          +-------------------------------------------------------------+
247                      12  assign adapter/domain/control domain
248
249 The process for reserving an AP queue for use by a KVM guest is:
250
251 1. The administrator loads the vfio_ap device driver
252 2. The vfio-ap driver during its initialization will register a single 'matrix'
253    device with the device core. This will serve as the parent device for
254    all vfio_ap mediated devices used to configure an AP matrix for a guest.
255 3. The /sys/devices/vfio_ap/matrix device is created by the device core
256 4. The vfio_ap device driver will register with the AP bus for AP queue devices
257    of type 10 and higher (CEX4 and newer). The driver will provide the vfio_ap
258    driver's probe and remove callback interfaces. Devices older than CEX4 queues
259    are not supported to simplify the implementation by not needlessly
260    complicating the design by supporting older devices that will go out of
261    service in the relatively near future, and for which there are few older
262    systems around on which to test.
263 5. The AP bus registers the vfio_ap device driver with the device core
264 6. The administrator edits the AP adapter and queue masks to reserve AP queues
265    for use by the vfio_ap device driver.
266 7. The AP bus removes the AP queues reserved for the vfio_ap driver from the
267    default zcrypt cex4queue driver.
268 8. The AP bus probes the vfio_ap device driver to bind the queues reserved for
269    it.
270 9. The administrator creates a passthrough type vfio_ap mediated device to be
271    used by a guest
272 10. The administrator assigns the adapters, usage domains and control domains
273     to be exclusively used by a guest.
274
275 Set up the VFIO mediated device interfaces
276 ------------------------------------------
277 The VFIO AP device driver utilizes the common interfaces of the VFIO mediated
278 device core driver to:
279
280 * Register an AP mediated bus driver to add a vfio_ap mediated device to and
281   remove it from a VFIO group.
282 * Create and destroy a vfio_ap mediated device
283 * Add a vfio_ap mediated device to and remove it from the AP mediated bus driver
284 * Add a vfio_ap mediated device to and remove it from an IOMMU group
285
286 The following high-level block diagram shows the main components and interfaces
287 of the VFIO AP mediated device driver::
288
289    +-------------+
290    |             |
291    | +---------+ | mdev_register_driver() +--------------+
292    | |  Mdev   | +<-----------------------+              |
293    | |  bus    | |                        | vfio_mdev.ko |
294    | | driver  | +----------------------->+              |<-> VFIO user
295    | +---------+ |    probe()/remove()    +--------------+    APIs
296    |             |
297    |  MDEV CORE  |
298    |   MODULE    |
299    |   mdev.ko   |
300    | +---------+ | mdev_register_parent() +--------------+
301    | |Physical | +<-----------------------+              |
302    | | device  | |                        |  vfio_ap.ko  |<-> matrix
303    | |interface| +----------------------->+              |    device
304    | +---------+ |       callback         +--------------+
305    +-------------+
306
307 During initialization of the vfio_ap module, the matrix device is registered
308 with an 'mdev_parent_ops' structure that provides the sysfs attribute
309 structures, mdev functions and callback interfaces for managing the mediated
310 matrix device.
311
312 * sysfs attribute structures:
313
314   supported_type_groups
315     The VFIO mediated device framework supports creation of user-defined
316     mediated device types. These mediated device types are specified
317     via the 'supported_type_groups' structure when a device is registered
318     with the mediated device framework. The registration process creates the
319     sysfs structures for each mediated device type specified in the
320     'mdev_supported_types' sub-directory of the device being registered. Along
321     with the device type, the sysfs attributes of the mediated device type are
322     provided.
323
324     The VFIO AP device driver will register one mediated device type for
325     passthrough devices:
326
327       /sys/devices/vfio_ap/matrix/mdev_supported_types/vfio_ap-passthrough
328
329     Only the read-only attributes required by the VFIO mdev framework will
330     be provided::
331
332         ... name
333         ... device_api
334         ... available_instances
335         ... device_api
336
337     Where:
338
339         * name:
340             specifies the name of the mediated device type
341         * device_api:
342             the mediated device type's API
343         * available_instances:
344             the number of vfio_ap mediated passthrough devices
345             that can be created
346         * device_api:
347             specifies the VFIO API
348   mdev_attr_groups
349     This attribute group identifies the user-defined sysfs attributes of the
350     mediated device. When a device is registered with the VFIO mediated device
351     framework, the sysfs attribute files identified in the 'mdev_attr_groups'
352     structure will be created in the vfio_ap mediated device's directory. The
353     sysfs attributes for a vfio_ap mediated device are:
354
355     assign_adapter / unassign_adapter:
356       Write-only attributes for assigning/unassigning an AP adapter to/from the
357       vfio_ap mediated device. To assign/unassign an adapter, the APID of the
358       adapter is echoed into the respective attribute file.
359     assign_domain / unassign_domain:
360       Write-only attributes for assigning/unassigning an AP usage domain to/from
361       the vfio_ap mediated device. To assign/unassign a domain, the domain
362       number of the usage domain is echoed into the respective attribute
363       file.
364     matrix:
365       A read-only file for displaying the APQNs derived from the Cartesian
366       product of the adapter and domain numbers assigned to the vfio_ap mediated
367       device.
368     guest_matrix:
369       A read-only file for displaying the APQNs derived from the Cartesian
370       product of the adapter and domain numbers assigned to the APM and AQM
371       fields respectively of the KVM guest's CRYCB. This may differ from the
372       the APQNs assigned to the vfio_ap mediated device if any APQN does not
373       reference a queue device bound to the vfio_ap device driver (i.e., the
374       queue is not in the host's AP configuration).
375     assign_control_domain / unassign_control_domain:
376       Write-only attributes for assigning/unassigning an AP control domain
377       to/from the vfio_ap mediated device. To assign/unassign a control domain,
378       the ID of the domain to be assigned/unassigned is echoed into the
379       respective attribute file.
380     control_domains:
381       A read-only file for displaying the control domain numbers assigned to the
382       vfio_ap mediated device.
383
384 * functions:
385
386   create:
387     allocates the ap_matrix_mdev structure used by the vfio_ap driver to:
388
389     * Store the reference to the KVM structure for the guest using the mdev
390     * Store the AP matrix configuration for the adapters, domains, and control
391       domains assigned via the corresponding sysfs attributes files
392     * Store the AP matrix configuration for the adapters, domains and control
393       domains available to a guest. A guest may not be provided access to APQNs
394       referencing queue devices that do not exist, or are not bound to the
395       vfio_ap device driver.
396
397   remove:
398     deallocates the vfio_ap mediated device's ap_matrix_mdev structure.
399     This will be allowed only if a running guest is not using the mdev.
400
401 * callback interfaces
402
403   open_device:
404     The vfio_ap driver uses this callback to register a
405     VFIO_GROUP_NOTIFY_SET_KVM notifier callback function for the matrix mdev
406     devices. The open_device callback is invoked by userspace to connect the
407     VFIO iommu group for the matrix mdev device to the MDEV bus. Access to the
408     KVM structure used to configure the KVM guest is provided via this callback.
409     The KVM structure, is used to configure the guest's access to the AP matrix
410     defined via the vfio_ap mediated device's sysfs attribute files.
411
412   close_device:
413     unregisters the VFIO_GROUP_NOTIFY_SET_KVM notifier callback function for the
414     matrix mdev device and deconfigures the guest's AP matrix.
415
416   ioctl:
417     this callback handles the VFIO_DEVICE_GET_INFO and VFIO_DEVICE_RESET ioctls
418     defined by the vfio framework.
419
420 Configure the guest's AP resources
421 ----------------------------------
422 Configuring the AP resources for a KVM guest will be performed when the
423 VFIO_GROUP_NOTIFY_SET_KVM notifier callback is invoked. The notifier
424 function is called when userspace connects to KVM. The guest's AP resources are
425 configured via it's APCB by:
426
427 * Setting the bits in the APM corresponding to the APIDs assigned to the
428   vfio_ap mediated device via its 'assign_adapter' interface.
429 * Setting the bits in the AQM corresponding to the domains assigned to the
430   vfio_ap mediated device via its 'assign_domain' interface.
431 * Setting the bits in the ADM corresponding to the domain dIDs assigned to the
432   vfio_ap mediated device via its 'assign_control_domains' interface.
433
434 The linux device model precludes passing a device through to a KVM guest that
435 is not bound to the device driver facilitating its pass-through. Consequently,
436 an APQN that does not reference a queue device bound to the vfio_ap device
437 driver will not be assigned to a KVM guest's matrix. The AP architecture,
438 however, does not provide a means to filter individual APQNs from the guest's
439 matrix, so the adapters, domains and control domains assigned to vfio_ap
440 mediated device via its sysfs 'assign_adapter', 'assign_domain' and
441 'assign_control_domain' interfaces will be filtered before providing the AP
442 configuration to a guest:
443
444 * The APIDs of the adapters, the APQIs of the domains and the domain numbers of
445   the control domains assigned to the matrix mdev that are not also assigned to
446   the host's AP configuration will be filtered.
447
448 * Each APQN derived from the Cartesian product of the APIDs and APQIs assigned
449   to the vfio_ap mdev is examined and if any one of them does not reference a
450   queue device bound to the vfio_ap device driver, the adapter will not be
451   plugged into the guest (i.e., the bit corresponding to its APID will not be
452   set in the APM of the guest's APCB).
453
454 The CPU model features for AP
455 -----------------------------
456 The AP stack relies on the presence of the AP instructions as well as three
457 facilities: The AP Facilities Test (APFT) facility; the AP Query
458 Configuration Information (QCI) facility; and the AP Queue Interruption Control
459 facility. These features/facilities are made available to a KVM guest via the
460 following CPU model features:
461
462 1. ap: Indicates whether the AP instructions are installed on the guest. This
463    feature will be enabled by KVM only if the AP instructions are installed
464    on the host.
465
466 2. apft: Indicates the APFT facility is available on the guest. This facility
467    can be made available to the guest only if it is available on the host (i.e.,
468    facility bit 15 is set).
469
470 3. apqci: Indicates the AP QCI facility is available on the guest. This facility
471    can be made available to the guest only if it is available on the host (i.e.,
472    facility bit 12 is set).
473
474 4. apqi: Indicates AP Queue Interruption Control faclity is available on the
475    guest. This facility can be made available to the guest only if it is
476    available on the host (i.e., facility bit 65 is set).
477
478 Note: If the user chooses to specify a CPU model different than the 'host'
479 model to QEMU, the CPU model features and facilities need to be turned on
480 explicitly; for example::
481
482      /usr/bin/qemu-system-s390x ... -cpu z13,ap=on,apqci=on,apft=on,apqi=on
483
484 A guest can be precluded from using AP features/facilities by turning them off
485 explicitly; for example::
486
487      /usr/bin/qemu-system-s390x ... -cpu host,ap=off,apqci=off,apft=off,apqi=off
488
489 Note: If the APFT facility is turned off (apft=off) for the guest, the guest
490 will not see any AP devices. The zcrypt device drivers on the guest that
491 register for type 10 and newer AP devices - i.e., the cex4card and cex4queue
492 device drivers - need the APFT facility to ascertain the facilities installed on
493 a given AP device. If the APFT facility is not installed on the guest, then no
494 adapter or domain devices will get created by the AP bus running on the
495 guest because only type 10 and newer devices can be configured for guest use.
496
497 Example
498 =======
499 Let's now provide an example to illustrate how KVM guests may be given
500 access to AP facilities. For this example, we will show how to configure
501 three guests such that executing the lszcrypt command on the guests would
502 look like this:
503
504 Guest1
505 ------
506 =========== ===== ============
507 CARD.DOMAIN TYPE  MODE
508 =========== ===== ============
509 05          CEX5C CCA-Coproc
510 05.0004     CEX5C CCA-Coproc
511 05.00ab     CEX5C CCA-Coproc
512 06          CEX5A Accelerator
513 06.0004     CEX5A Accelerator
514 06.00ab     CEX5A Accelerator
515 =========== ===== ============
516
517 Guest2
518 ------
519 =========== ===== ============
520 CARD.DOMAIN TYPE  MODE
521 =========== ===== ============
522 05          CEX5C CCA-Coproc
523 05.0047     CEX5C CCA-Coproc
524 05.00ff     CEX5C CCA-Coproc
525 =========== ===== ============
526
527 Guest3
528 ------
529 =========== ===== ============
530 CARD.DOMAIN TYPE  MODE
531 =========== ===== ============
532 06          CEX5A Accelerator
533 06.0047     CEX5A Accelerator
534 06.00ff     CEX5A Accelerator
535 =========== ===== ============
536
537 These are the steps:
538
539 1. Install the vfio_ap module on the linux host. The dependency chain for the
540    vfio_ap module is:
541    * iommu
542    * s390
543    * zcrypt
544    * vfio
545    * vfio_mdev
546    * vfio_mdev_device
547    * KVM
548
549    To build the vfio_ap module, the kernel build must be configured with the
550    following Kconfig elements selected:
551    * IOMMU_SUPPORT
552    * S390
553    * ZCRYPT
554    * S390_AP_IOMMU
555    * VFIO
556    * KVM
557
558    If using make menuconfig select the following to build the vfio_ap module::
559
560      -> Device Drivers
561         -> IOMMU Hardware Support
562            select S390 AP IOMMU Support
563         -> VFIO Non-Privileged userspace driver framework
564            -> Mediated device driver frramework
565               -> VFIO driver for Mediated devices
566      -> I/O subsystem
567         -> VFIO support for AP devices
568
569 2. Secure the AP queues to be used by the three guests so that the host can not
570    access them. To secure them, there are two sysfs files that specify
571    bitmasks marking a subset of the APQN range as usable only by the default AP
572    queue device drivers. All remaining APQNs are available for use by
573    any other device driver. The vfio_ap device driver is currently the only
574    non-default device driver. The location of the sysfs files containing the
575    masks are::
576
577      /sys/bus/ap/apmask
578      /sys/bus/ap/aqmask
579
580    The 'apmask' is a 256-bit mask that identifies a set of AP adapter IDs
581    (APID). Each bit in the mask, from left to right, corresponds to an APID from
582    0-255. If a bit is set, the APID belongs to the subset of APQNs marked as
583    available only to the default AP queue device drivers.
584
585    The 'aqmask' is a 256-bit mask that identifies a set of AP queue indexes
586    (APQI). Each bit in the mask, from left to right, corresponds to an APQI from
587    0-255. If a bit is set, the APQI belongs to the subset of APQNs marked as
588    available only to the default AP queue device drivers.
589
590    The Cartesian product of the APIDs corresponding to the bits set in the
591    apmask and the APQIs corresponding to the bits set in the aqmask comprise
592    the subset of APQNs that can be used only by the host default device drivers.
593    All other APQNs are available to the non-default device drivers such as the
594    vfio_ap driver.
595
596    Take, for example, the following masks::
597
598       apmask:
599       0x7d00000000000000000000000000000000000000000000000000000000000000
600
601       aqmask:
602       0x8000000000000000000000000000000000000000000000000000000000000000
603
604    The masks indicate:
605
606    * Adapters 1, 2, 3, 4, 5, and 7 are available for use by the host default
607      device drivers.
608
609    * Domain 0 is available for use by the host default device drivers
610
611    * The subset of APQNs available for use only by the default host device
612      drivers are:
613
614      (1,0), (2,0), (3,0), (4.0), (5,0) and (7,0)
615
616    * All other APQNs are available for use by the non-default device drivers.
617
618    The APQN of each AP queue device assigned to the linux host is checked by the
619    AP bus against the set of APQNs derived from the Cartesian product of APIDs
620    and APQIs marked as available to the default AP queue device drivers. If a
621    match is detected,  only the default AP queue device drivers will be probed;
622    otherwise, the vfio_ap device driver will be probed.
623
624    By default, the two masks are set to reserve all APQNs for use by the default
625    AP queue device drivers. There are two ways the default masks can be changed:
626
627    1. The sysfs mask files can be edited by echoing a string into the
628       respective sysfs mask file in one of two formats:
629
630       * An absolute hex string starting with 0x - like "0x12345678" - sets
631         the mask. If the given string is shorter than the mask, it is padded
632         with 0s on the right; for example, specifying a mask value of 0x41 is
633         the same as specifying::
634
635            0x4100000000000000000000000000000000000000000000000000000000000000
636
637         Keep in mind that the mask reads from left to right, so the mask
638         above identifies device numbers 1 and 7 (01000001).
639
640         If the string is longer than the mask, the operation is terminated with
641         an error (EINVAL).
642
643       * Individual bits in the mask can be switched on and off by specifying
644         each bit number to be switched in a comma separated list. Each bit
645         number string must be prepended with a ('+') or minus ('-') to indicate
646         the corresponding bit is to be switched on ('+') or off ('-'). Some
647         valid values are:
648
649            - "+0"    switches bit 0 on
650            - "-13"   switches bit 13 off
651            - "+0x41" switches bit 65 on
652            - "-0xff" switches bit 255 off
653
654         The following example:
655
656               +0,-6,+0x47,-0xf0
657
658         Switches bits 0 and 71 (0x47) on
659
660         Switches bits 6 and 240 (0xf0) off
661
662         Note that the bits not specified in the list remain as they were before
663         the operation.
664
665    2. The masks can also be changed at boot time via parameters on the kernel
666       command line like this:
667
668          ap.apmask=0xffff ap.aqmask=0x40
669
670          This would create the following masks::
671
672             apmask:
673             0xffff000000000000000000000000000000000000000000000000000000000000
674
675             aqmask:
676             0x4000000000000000000000000000000000000000000000000000000000000000
677
678          Resulting in these two pools::
679
680             default drivers pool:    adapter 0-15, domain 1
681             alternate drivers pool:  adapter 16-255, domains 0, 2-255
682
683    **Note:**
684    Changing a mask such that one or more APQNs will be taken from a vfio_ap
685    mediated device (see below) will fail with an error (EBUSY). A message
686    is logged to the kernel ring buffer which can be viewed with the 'dmesg'
687    command. The output identifies each APQN flagged as 'in use' and identifies
688    the vfio_ap mediated device to which it is assigned; for example:
689
690    Userspace may not re-assign queue 05.0054 already assigned to 62177883-f1bb-47f0-914d-32a22e3a8804
691    Userspace may not re-assign queue 04.0054 already assigned to cef03c3c-903d-4ecc-9a83-40694cb8aee4
692
693 Securing the APQNs for our example
694 ----------------------------------
695    To secure the AP queues 05.0004, 05.0047, 05.00ab, 05.00ff, 06.0004, 06.0047,
696    06.00ab, and 06.00ff for use by the vfio_ap device driver, the corresponding
697    APQNs can be removed from the default masks using either of the following
698    commands::
699
700       echo -5,-6 > /sys/bus/ap/apmask
701
702       echo -4,-0x47,-0xab,-0xff > /sys/bus/ap/aqmask
703
704    Or the masks can be set as follows::
705
706       echo 0xf9ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff \
707       > apmask
708
709       echo 0xf7fffffffffffffffeffffffffffffffffffffffffeffffffffffffffffffffe \
710       > aqmask
711
712    This will result in AP queues 05.0004, 05.0047, 05.00ab, 05.00ff, 06.0004,
713    06.0047, 06.00ab, and 06.00ff getting bound to the vfio_ap device driver. The
714    sysfs directory for the vfio_ap device driver will now contain symbolic links
715    to the AP queue devices bound to it::
716
717      /sys/bus/ap
718      ... [drivers]
719      ...... [vfio_ap]
720      ......... [05.0004]
721      ......... [05.0047]
722      ......... [05.00ab]
723      ......... [05.00ff]
724      ......... [06.0004]
725      ......... [06.0047]
726      ......... [06.00ab]
727      ......... [06.00ff]
728
729    Keep in mind that only type 10 and newer adapters (i.e., CEX4 and later)
730    can be bound to the vfio_ap device driver. The reason for this is to
731    simplify the implementation by not needlessly complicating the design by
732    supporting older devices that will go out of service in the relatively near
733    future and for which there are few older systems on which to test.
734
735    The administrator, therefore, must take care to secure only AP queues that
736    can be bound to the vfio_ap device driver. The device type for a given AP
737    queue device can be read from the parent card's sysfs directory. For example,
738    to see the hardware type of the queue 05.0004:
739
740      cat /sys/bus/ap/devices/card05/hwtype
741
742    The hwtype must be 10 or higher (CEX4 or newer) in order to be bound to the
743    vfio_ap device driver.
744
745 3. Create the mediated devices needed to configure the AP matrixes for the
746    three guests and to provide an interface to the vfio_ap driver for
747    use by the guests::
748
749      /sys/devices/vfio_ap/matrix/
750      --- [mdev_supported_types]
751      ------ [vfio_ap-passthrough] (passthrough vfio_ap mediated device type)
752      --------- create
753      --------- [devices]
754
755    To create the mediated devices for the three guests::
756
757         uuidgen > create
758         uuidgen > create
759         uuidgen > create
760
761         or
762
763         echo $uuid1 > create
764         echo $uuid2 > create
765         echo $uuid3 > create
766
767    This will create three mediated devices in the [devices] subdirectory named
768    after the UUID written to the create attribute file. We call them $uuid1,
769    $uuid2 and $uuid3 and this is the sysfs directory structure after creation::
770
771      /sys/devices/vfio_ap/matrix/
772      --- [mdev_supported_types]
773      ------ [vfio_ap-passthrough]
774      --------- [devices]
775      ------------ [$uuid1]
776      --------------- assign_adapter
777      --------------- assign_control_domain
778      --------------- assign_domain
779      --------------- matrix
780      --------------- unassign_adapter
781      --------------- unassign_control_domain
782      --------------- unassign_domain
783
784      ------------ [$uuid2]
785      --------------- assign_adapter
786      --------------- assign_control_domain
787      --------------- assign_domain
788      --------------- matrix
789      --------------- unassign_adapter
790      ----------------unassign_control_domain
791      ----------------unassign_domain
792
793      ------------ [$uuid3]
794      --------------- assign_adapter
795      --------------- assign_control_domain
796      --------------- assign_domain
797      --------------- matrix
798      --------------- unassign_adapter
799      ----------------unassign_control_domain
800      ----------------unassign_domain
801
802    Note *****: The vfio_ap mdevs do not persist across reboots unless the
803                mdevctl tool is used to create and persist them.
804
805 4. The administrator now needs to configure the matrixes for the mediated
806    devices $uuid1 (for Guest1), $uuid2 (for Guest2) and $uuid3 (for Guest3).
807
808    This is how the matrix is configured for Guest1::
809
810       echo 5 > assign_adapter
811       echo 6 > assign_adapter
812       echo 4 > assign_domain
813       echo 0xab > assign_domain
814
815    Control domains can similarly be assigned using the assign_control_domain
816    sysfs file.
817
818    If a mistake is made configuring an adapter, domain or control domain,
819    you can use the unassign_xxx files to unassign the adapter, domain or
820    control domain.
821
822    To display the matrix configuration for Guest1::
823
824          cat matrix
825
826    To display the matrix that is or will be assigned to Guest1::
827
828          cat guest_matrix
829
830    This is how the matrix is configured for Guest2::
831
832       echo 5 > assign_adapter
833       echo 0x47 > assign_domain
834       echo 0xff > assign_domain
835
836    This is how the matrix is configured for Guest3::
837
838       echo 6 > assign_adapter
839       echo 0x47 > assign_domain
840       echo 0xff > assign_domain
841
842    In order to successfully assign an adapter:
843
844    * The adapter number specified must represent a value from 0 up to the
845      maximum adapter number configured for the system. If an adapter number
846      higher than the maximum is specified, the operation will terminate with
847      an error (ENODEV).
848
849      Note: The maximum adapter number can be obtained via the sysfs
850            /sys/bus/ap/ap_max_adapter_id attribute file.
851
852    * Each APQN derived from the Cartesian product of the APID of the adapter
853      being assigned and the APQIs of the domains previously assigned:
854
855      - Must only be available to the vfio_ap device driver as specified in the
856        sysfs /sys/bus/ap/apmask and /sys/bus/ap/aqmask attribute files. If even
857        one APQN is reserved for use by the host device driver, the operation
858        will terminate with an error (EADDRNOTAVAIL).
859
860      - Must NOT be assigned to another vfio_ap mediated device. If even one APQN
861        is assigned to another vfio_ap mediated device, the operation will
862        terminate with an error (EBUSY).
863
864      - Must NOT be assigned while the sysfs /sys/bus/ap/apmask and
865        sys/bus/ap/aqmask attribute files are being edited or the operation may
866        terminate with an error (EBUSY).
867
868    In order to successfully assign a domain:
869
870    * The domain number specified must represent a value from 0 up to the
871      maximum domain number configured for the system. If a domain number
872      higher than the maximum is specified, the operation will terminate with
873      an error (ENODEV).
874
875      Note: The maximum domain number can be obtained via the sysfs
876            /sys/bus/ap/ap_max_domain_id attribute file.
877
878     * Each APQN derived from the Cartesian product of the APQI of the domain
879       being assigned and the APIDs of the adapters previously assigned:
880
881      - Must only be available to the vfio_ap device driver as specified in the
882        sysfs /sys/bus/ap/apmask and /sys/bus/ap/aqmask attribute files. If even
883        one APQN is reserved for use by the host device driver, the operation
884        will terminate with an error (EADDRNOTAVAIL).
885
886      - Must NOT be assigned to another vfio_ap mediated device. If even one APQN
887        is assigned to another vfio_ap mediated device, the operation will
888        terminate with an error (EBUSY).
889
890      - Must NOT be assigned while the sysfs /sys/bus/ap/apmask and
891        sys/bus/ap/aqmask attribute files are being edited or the operation may
892        terminate with an error (EBUSY).
893
894    In order to successfully assign a control domain:
895
896    * The domain number specified must represent a value from 0 up to the maximum
897      domain number configured for the system. If a control domain number higher
898      than the maximum is specified, the operation will terminate with an
899      error (ENODEV).
900
901 5. Start Guest1::
902
903      /usr/bin/qemu-system-s390x ... -cpu host,ap=on,apqci=on,apft=on,apqi=on \
904         -device vfio-ap,sysfsdev=/sys/devices/vfio_ap/matrix/$uuid1 ...
905
906 7. Start Guest2::
907
908      /usr/bin/qemu-system-s390x ... -cpu host,ap=on,apqci=on,apft=on,apqi=on \
909         -device vfio-ap,sysfsdev=/sys/devices/vfio_ap/matrix/$uuid2 ...
910
911 7. Start Guest3::
912
913      /usr/bin/qemu-system-s390x ... -cpu host,ap=on,apqci=on,apft=on,apqi=on \
914         -device vfio-ap,sysfsdev=/sys/devices/vfio_ap/matrix/$uuid3 ...
915
916 When the guest is shut down, the vfio_ap mediated devices may be removed.
917
918 Using our example again, to remove the vfio_ap mediated device $uuid1::
919
920    /sys/devices/vfio_ap/matrix/
921       --- [mdev_supported_types]
922       ------ [vfio_ap-passthrough]
923       --------- [devices]
924       ------------ [$uuid1]
925       --------------- remove
926
927 ::
928
929    echo 1 > remove
930
931 This will remove all of the matrix mdev device's sysfs structures including
932 the mdev device itself. To recreate and reconfigure the matrix mdev device,
933 all of the steps starting with step 3 will have to be performed again. Note
934 that the remove will fail if a guest using the vfio_ap mdev is still running.
935
936 It is not necessary to remove a vfio_ap mdev, but one may want to
937 remove it if no guest will use it during the remaining lifetime of the linux
938 host. If the vfio_ap mdev is removed, one may want to also reconfigure
939 the pool of adapters and queues reserved for use by the default drivers.
940
941 Hot plug/unplug support:
942 ========================
943 An adapter, domain or control domain may be hot plugged into a running KVM
944 guest by assigning it to the vfio_ap mediated device being used by the guest if
945 the following conditions are met:
946
947 * The adapter, domain or control domain must also be assigned to the host's
948   AP configuration.
949
950 * Each APQN derived from the Cartesian product comprised of the APID of the
951   adapter being assigned and the APQIs of the domains assigned must reference a
952   queue device bound to the vfio_ap device driver.
953
954 * To hot plug a domain, each APQN derived from the Cartesian product
955   comprised of the APQI of the domain being assigned and the APIDs of the
956   adapters assigned must reference a queue device bound to the vfio_ap device
957   driver.
958
959 An adapter, domain or control domain may be hot unplugged from a running KVM
960 guest by unassigning it from the vfio_ap mediated device being used by the
961 guest.
962
963 Over-provisioning of AP queues for a KVM guest:
964 ===============================================
965 Over-provisioning is defined herein as the assignment of adapters or domains to
966 a vfio_ap mediated device that do not reference AP devices in the host's AP
967 configuration. The idea here is that when the adapter or domain becomes
968 available, it will be automatically hot-plugged into the KVM guest using
969 the vfio_ap mediated device to which it is assigned as long as each new APQN
970 resulting from plugging it in references a queue device bound to the vfio_ap
971 device driver.
972
973 Limitations
974 ===========
975 Live guest migration is not supported for guests using AP devices without
976 intervention by a system administrator. Before a KVM guest can be migrated,
977 the vfio_ap mediated device must be removed. Unfortunately, it can not be
978 removed manually (i.e., echo 1 > /sys/devices/vfio_ap/matrix/$UUID/remove) while
979 the mdev is in use by a KVM guest. If the guest is being emulated by QEMU,
980 its mdev can be hot unplugged from the guest in one of two ways:
981
982 1. If the KVM guest was started with libvirt, you can hot unplug the mdev via
983    the following commands:
984
985       virsh detach-device <guestname> <path-to-device-xml>
986
987       For example, to hot unplug mdev 62177883-f1bb-47f0-914d-32a22e3a8804 from
988       the guest named 'my-guest':
989
990          virsh detach-device my-guest ~/config/my-guest-hostdev.xml
991
992             The contents of my-guest-hostdev.xml:
993
994 .. code-block:: xml
995
996             <hostdev mode='subsystem' type='mdev' managed='no' model='vfio-ap'>
997               <source>
998                 <address uuid='62177883-f1bb-47f0-914d-32a22e3a8804'/>
999               </source>
1000             </hostdev>
1001
1002
1003       virsh qemu-monitor-command <guest-name> --hmp "device-del <device-id>"
1004
1005       For example, to hot unplug the vfio_ap mediated device identified on the
1006       qemu command line with 'id=hostdev0' from the guest named 'my-guest':
1007
1008 .. code-block:: sh
1009
1010          virsh qemu-monitor-command my-guest --hmp "device_del hostdev0"
1011
1012 2. A vfio_ap mediated device can be hot unplugged by attaching the qemu monitor
1013    to the guest and using the following qemu monitor command:
1014
1015       (QEMU) device-del id=<device-id>
1016
1017       For example, to hot unplug the vfio_ap mediated device that was specified
1018       on the qemu command line with 'id=hostdev0' when the guest was started:
1019
1020          (QEMU) device-del id=hostdev0
1021
1022 After live migration of the KVM guest completes, an AP configuration can be
1023 restored to the KVM guest by hot plugging a vfio_ap mediated device on the target
1024 system into the guest in one of two ways:
1025
1026 1. If the KVM guest was started with libvirt, you can hot plug a matrix mediated
1027    device into the guest via the following virsh commands:
1028
1029    virsh attach-device <guestname> <path-to-device-xml>
1030
1031       For example, to hot plug mdev 62177883-f1bb-47f0-914d-32a22e3a8804 into
1032       the guest named 'my-guest':
1033
1034          virsh attach-device my-guest ~/config/my-guest-hostdev.xml
1035
1036             The contents of my-guest-hostdev.xml:
1037
1038 .. code-block:: xml
1039
1040             <hostdev mode='subsystem' type='mdev' managed='no' model='vfio-ap'>
1041               <source>
1042                 <address uuid='62177883-f1bb-47f0-914d-32a22e3a8804'/>
1043               </source>
1044             </hostdev>
1045
1046
1047    virsh qemu-monitor-command <guest-name> --hmp \
1048    "device_add vfio-ap,sysfsdev=<path-to-mdev>,id=<device-id>"
1049
1050       For example, to hot plug the vfio_ap mediated device
1051       62177883-f1bb-47f0-914d-32a22e3a8804 into the guest named 'my-guest' with
1052       device-id hostdev0:
1053
1054       virsh qemu-monitor-command my-guest --hmp \
1055       "device_add vfio-ap,\
1056       sysfsdev=/sys/devices/vfio_ap/matrix/62177883-f1bb-47f0-914d-32a22e3a8804,\
1057       id=hostdev0"
1058
1059 2. A vfio_ap mediated device can be hot plugged by attaching the qemu monitor
1060    to the guest and using the following qemu monitor command:
1061
1062       (qemu) device_add "vfio-ap,sysfsdev=<path-to-mdev>,id=<device-id>"
1063
1064       For example, to plug the vfio_ap mediated device
1065       62177883-f1bb-47f0-914d-32a22e3a8804 into the guest with the device-id
1066       hostdev0:
1067
1068          (QEMU) device-add "vfio-ap,\
1069          sysfsdev=/sys/devices/vfio_ap/matrix/62177883-f1bb-47f0-914d-32a22e3a8804,\
1070          id=hostdev0"