xref: /qemu/docs/system/s390x/vfio-ap.rst (revision 458e056257e67254546e58158f3f74ce040c7ca1)
18f433524SCornelia HuckAdjunct Processor (AP) Device
28f433524SCornelia Huck=============================
38f433524SCornelia Huck
48f433524SCornelia Huck.. contents::
58f433524SCornelia Huck
68f433524SCornelia HuckIntroduction
78f433524SCornelia Huck------------
88f433524SCornelia Huck
98f433524SCornelia HuckThe IBM Adjunct Processor (AP) Cryptographic Facility is comprised
108f433524SCornelia Huckof three AP instructions and from 1 to 256 PCIe cryptographic adapter cards.
118f433524SCornelia HuckThese AP devices provide cryptographic functions to all CPUs assigned to a
128f433524SCornelia Hucklinux system running in an IBM Z system LPAR.
138f433524SCornelia Huck
148f433524SCornelia HuckOn s390x, AP adapter cards are exposed via the AP bus. This document
158f433524SCornelia Huckdescribes how those cards may be made available to KVM guests using the
168f433524SCornelia HuckVFIO mediated device framework.
178f433524SCornelia Huck
188f433524SCornelia HuckAP Architectural Overview
198f433524SCornelia Huck-------------------------
208f433524SCornelia Huck
218f433524SCornelia HuckIn order understand the terminology used in the rest of this document, let's
228f433524SCornelia Huckstart with some definitions:
238f433524SCornelia Huck
248f433524SCornelia Huck* AP adapter
258f433524SCornelia Huck
268f433524SCornelia Huck  An AP adapter is an IBM Z adapter card that can perform cryptographic
278f433524SCornelia Huck  functions. There can be from 0 to 256 adapters assigned to an LPAR depending
288f433524SCornelia Huck  on the machine model. Adapters assigned to the LPAR in which a linux host is
298f433524SCornelia Huck  running will be available to the linux host. Each adapter is identified by a
308f433524SCornelia Huck  number from 0 to 255; however, the maximum adapter number allowed is
318f433524SCornelia Huck  determined by machine model. When installed, an AP adapter is accessed by
328f433524SCornelia Huck  AP instructions executed by any CPU.
338f433524SCornelia Huck
348f433524SCornelia Huck* AP domain
358f433524SCornelia Huck
368f433524SCornelia Huck  An adapter is partitioned into domains. Each domain can be thought of as
378f433524SCornelia Huck  a set of hardware registers for processing AP instructions. An adapter can
388f433524SCornelia Huck  hold up to 256 domains; however, the maximum domain number allowed is
398f433524SCornelia Huck  determined by machine model. Each domain is identified by a number from 0 to
408f433524SCornelia Huck  255. Domains can be further classified into two types:
418f433524SCornelia Huck
428f433524SCornelia Huck    * Usage domains are domains that can be accessed directly to process AP
438f433524SCornelia Huck      commands
448f433524SCornelia Huck
458f433524SCornelia Huck    * Control domains are domains that are accessed indirectly by AP
468f433524SCornelia Huck      commands sent to a usage domain to control or change the domain; for
478f433524SCornelia Huck      example, to set a secure private key for the domain.
488f433524SCornelia Huck
498f433524SCornelia Huck* AP Queue
508f433524SCornelia Huck
518f433524SCornelia Huck  An AP queue is the means by which an AP command-request message is sent to an
528f433524SCornelia Huck  AP usage domain inside a specific AP. An AP queue is identified by a tuple
538f433524SCornelia Huck  comprised of an AP adapter ID (APID) and an AP queue index (APQI). The
548f433524SCornelia Huck  APQI corresponds to a given usage domain number within the adapter. This tuple
558f433524SCornelia Huck  forms an AP Queue Number (APQN) uniquely identifying an AP queue. AP
568f433524SCornelia Huck  instructions include a field containing the APQN to identify the AP queue to
578f433524SCornelia Huck  which the AP command-request message is to be sent for processing.
588f433524SCornelia Huck
598f433524SCornelia Huck* AP Instructions:
608f433524SCornelia Huck
618f433524SCornelia Huck  There are three AP instructions:
628f433524SCornelia Huck
638f433524SCornelia Huck  * NQAP: to enqueue an AP command-request message to a queue
648f433524SCornelia Huck  * DQAP: to dequeue an AP command-reply message from a queue
658f433524SCornelia Huck  * PQAP: to administer the queues
668f433524SCornelia Huck
678f433524SCornelia Huck  AP instructions identify the domain that is targeted to process the AP
688f433524SCornelia Huck  command; this must be one of the usage domains. An AP command may modify a
698f433524SCornelia Huck  domain that is not one of the usage domains, but the modified domain
708f433524SCornelia Huck  must be one of the control domains.
718f433524SCornelia Huck
728f433524SCornelia HuckStart Interpretive Execution (SIE) Instruction
738f433524SCornelia Huck----------------------------------------------
748f433524SCornelia Huck
758f433524SCornelia HuckA KVM guest is started by executing the Start Interpretive Execution (SIE)
768f433524SCornelia Huckinstruction. The SIE state description is a control block that contains the
778f433524SCornelia Huckstate information for a KVM guest and is supplied as input to the SIE
788f433524SCornelia Huckinstruction. The SIE state description contains a satellite control block called
798f433524SCornelia Huckthe Crypto Control Block (CRYCB). The CRYCB contains three fields to identify
808f433524SCornelia Huckthe adapters, usage domains and control domains assigned to the KVM guest:
818f433524SCornelia Huck
828f433524SCornelia Huck* The AP Mask (APM) field is a bit mask that identifies the AP adapters assigned
838f433524SCornelia Huck  to the KVM guest. Each bit in the mask, from left to right, corresponds to
848f433524SCornelia Huck  an APID from 0-255. If a bit is set, the corresponding adapter is valid for
858f433524SCornelia Huck  use by the KVM guest.
868f433524SCornelia Huck
878f433524SCornelia Huck* The AP Queue Mask (AQM) field is a bit mask identifying the AP usage domains
888f433524SCornelia Huck  assigned to the KVM guest. Each bit in the mask, from left to right,
898f433524SCornelia Huck  corresponds to  an AP queue index (APQI) from 0-255. If a bit is set, the
908f433524SCornelia Huck  corresponding queue is valid for use by the KVM guest.
918f433524SCornelia Huck
928f433524SCornelia Huck* The AP Domain Mask field is a bit mask that identifies the AP control domains
938f433524SCornelia Huck  assigned to the KVM guest. The ADM bit mask controls which domains can be
948f433524SCornelia Huck  changed by an AP command-request message sent to a usage domain from the
958f433524SCornelia Huck  guest. Each bit in the mask, from left to right, corresponds to a domain from
968f433524SCornelia Huck  0-255. If a bit is set, the corresponding domain can be modified by an AP
978f433524SCornelia Huck  command-request message sent to a usage domain.
988f433524SCornelia Huck
998f433524SCornelia HuckIf you recall from the description of an AP Queue, AP instructions include
1008f433524SCornelia Huckan APQN to identify the AP adapter and AP queue to which an AP command-request
1018f433524SCornelia Huckmessage is to be sent (NQAP and PQAP instructions), or from which a
1028f433524SCornelia Huckcommand-reply message is to be received (DQAP instruction). The validity of an
1038f433524SCornelia HuckAPQN is defined by the matrix calculated from the APM and AQM; it is the
1048f433524SCornelia Huckcross product of all assigned adapter numbers (APM) with all assigned queue
1058f433524SCornelia Huckindexes (AQM). For example, if adapters 1 and 2 and usage domains 5 and 6 are
1068f433524SCornelia Huckassigned to a guest, the APQNs (1,5), (1,6), (2,5) and (2,6) will be valid for
1078f433524SCornelia Huckthe guest.
1088f433524SCornelia Huck
1098f433524SCornelia HuckThe APQNs can provide secure key functionality - i.e., a private key is stored
1108f433524SCornelia Huckon the adapter card for each of its domains - so each APQN must be assigned to
1118f433524SCornelia Huckat most one guest or the linux host.
1128f433524SCornelia Huck
1138f433524SCornelia HuckExample 1: Valid configuration
1148f433524SCornelia Huck~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1158f433524SCornelia Huck
1168f433524SCornelia Huck+----------+--------+--------+
1178f433524SCornelia Huck|          | Guest1 | Guest2 |
1188f433524SCornelia Huck+==========+========+========+
1198f433524SCornelia Huck| adapters |  1, 2  |  1, 2  |
1208f433524SCornelia Huck+----------+--------+--------+
1218f433524SCornelia Huck| domains  |  5, 6  |  7     |
1228f433524SCornelia Huck+----------+--------+--------+
1238f433524SCornelia Huck
1248f433524SCornelia HuckThis is valid because both guests have a unique set of APQNs:
1258f433524SCornelia Huck
1268f433524SCornelia Huck* Guest1 has APQNs (1,5), (1,6), (2,5) and (2,6);
1278f433524SCornelia Huck* Guest2 has APQNs (1,7) and (2,7).
1288f433524SCornelia Huck
1298f433524SCornelia HuckExample 2: Valid configuration
1308f433524SCornelia Huck~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1318f433524SCornelia Huck
1328f433524SCornelia Huck+----------+--------+--------+
1338f433524SCornelia Huck|          | Guest1 | Guest2 |
1348f433524SCornelia Huck+==========+========+========+
1358f433524SCornelia Huck| adapters |  1, 2  |  3, 4  |
1368f433524SCornelia Huck+----------+--------+--------+
1378f433524SCornelia Huck| domains  |  5, 6  |  5, 6  |
1388f433524SCornelia Huck+----------+--------+--------+
1398f433524SCornelia Huck
1408f433524SCornelia HuckThis is also valid because both guests have a unique set of APQNs:
1418f433524SCornelia Huck
1428f433524SCornelia Huck* Guest1 has APQNs (1,5), (1,6), (2,5), (2,6);
1438f433524SCornelia Huck* Guest2 has APQNs (3,5), (3,6), (4,5), (4,6)
1448f433524SCornelia Huck
1458f433524SCornelia HuckExample 3: Invalid configuration
1468f433524SCornelia Huck~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1478f433524SCornelia Huck
1488f433524SCornelia Huck+----------+--------+--------+
1498f433524SCornelia Huck|          | Guest1 | Guest2 |
1508f433524SCornelia Huck+==========+========+========+
1518f433524SCornelia Huck| adapters |  1, 2  |  1     |
1528f433524SCornelia Huck+----------+--------+--------+
1538f433524SCornelia Huck| domains  |  5, 6  |  6, 7  |
1548f433524SCornelia Huck+----------+--------+--------+
1558f433524SCornelia Huck
1568f433524SCornelia HuckThis is an invalid configuration because both guests have access to
1578f433524SCornelia HuckAPQN (1,6).
1588f433524SCornelia Huck
1598f433524SCornelia HuckAP Matrix Configuration on Linux Host
1608f433524SCornelia Huck-------------------------------------
1618f433524SCornelia Huck
1628f433524SCornelia HuckA linux system is a guest of the LPAR in which it is running and has access to
1638f433524SCornelia Huckthe AP resources configured for the LPAR. The LPAR's AP matrix is
1648f433524SCornelia Huckconfigured via its Activation Profile which can be edited on the HMC. When the
1658f433524SCornelia Hucklinux system is started, the AP bus will detect the AP devices assigned to the
1668f433524SCornelia HuckLPAR and create the following in sysfs::
1678f433524SCornelia Huck
1688f433524SCornelia Huck  /sys/bus/ap
1698f433524SCornelia Huck  ... [devices]
1708f433524SCornelia Huck  ...... xx.yyyy
1718f433524SCornelia Huck  ...... ...
1728f433524SCornelia Huck  ...... cardxx
1738f433524SCornelia Huck  ...... ...
1748f433524SCornelia Huck
1758f433524SCornelia HuckWhere:
1768f433524SCornelia Huck
1778f433524SCornelia Huck``cardxx``
1788f433524SCornelia Huck  is AP adapter number xx (in hex)
1798f433524SCornelia Huck
1808f433524SCornelia Huck``xx.yyyy``
1818f433524SCornelia Huck  is an APQN with xx specifying the APID and yyyy specifying the APQI
1828f433524SCornelia Huck
1838f433524SCornelia HuckFor example, if AP adapters 5 and 6 and domains 4, 71 (0x47), 171 (0xab) and
1848f433524SCornelia Huck255 (0xff) are configured for the LPAR, the sysfs representation on the linux
1858f433524SCornelia Huckhost system would look like this::
1868f433524SCornelia Huck
1878f433524SCornelia Huck  /sys/bus/ap
1888f433524SCornelia Huck  ... [devices]
1898f433524SCornelia Huck  ...... 05.0004
1908f433524SCornelia Huck  ...... 05.0047
1918f433524SCornelia Huck  ...... 05.00ab
1928f433524SCornelia Huck  ...... 05.00ff
1938f433524SCornelia Huck  ...... 06.0004
1948f433524SCornelia Huck  ...... 06.0047
1958f433524SCornelia Huck  ...... 06.00ab
1968f433524SCornelia Huck  ...... 06.00ff
1978f433524SCornelia Huck  ...... card05
1988f433524SCornelia Huck  ...... card06
1998f433524SCornelia Huck
2008f433524SCornelia HuckA set of default device drivers are also created to control each type of AP
2018f433524SCornelia Huckdevice that can be assigned to the LPAR on which a linux host is running::
2028f433524SCornelia Huck
2038f433524SCornelia Huck  /sys/bus/ap
2048f433524SCornelia Huck  ... [drivers]
2058f433524SCornelia Huck  ...... [cex2acard]        for Crypto Express 2/3 accelerator cards
2068f433524SCornelia Huck  ...... [cex2aqueue]       for AP queues served by Crypto Express 2/3
2078f433524SCornelia Huck                            accelerator cards
2088f433524SCornelia Huck  ...... [cex4card]         for Crypto Express 4/5/6 accelerator and coprocessor
2098f433524SCornelia Huck                            cards
2108f433524SCornelia Huck  ...... [cex4queue]        for AP queues served by Crypto Express 4/5/6
2118f433524SCornelia Huck                            accelerator and coprocessor cards
2128f433524SCornelia Huck  ...... [pcixcccard]       for Crypto Express 2/3 coprocessor cards
2138f433524SCornelia Huck  ...... [pcixccqueue]      for AP queues served by Crypto Express 2/3
2148f433524SCornelia Huck                            coprocessor cards
2158f433524SCornelia Huck
2168f433524SCornelia HuckBinding AP devices to device drivers
2178f433524SCornelia Huck~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2188f433524SCornelia Huck
2198f433524SCornelia HuckThere are two sysfs files that specify bitmasks marking a subset of the APQN
2208f433524SCornelia Huckrange as 'usable by the default AP queue device drivers' or 'not usable by the
2218f433524SCornelia Huckdefault device drivers' and thus available for use by the alternate device
2228f433524SCornelia Huckdriver(s). The sysfs locations of the masks are::
2238f433524SCornelia Huck
2248f433524SCornelia Huck   /sys/bus/ap/apmask
2258f433524SCornelia Huck   /sys/bus/ap/aqmask
2268f433524SCornelia Huck
2278f433524SCornelia HuckThe ``apmask`` is a 256-bit mask that identifies a set of AP adapter IDs
2288f433524SCornelia Huck(APID). Each bit in the mask, from left to right (i.e., from most significant
2298f433524SCornelia Huckto least significant bit in big endian order), corresponds to an APID from
2308f433524SCornelia Huck0-255. If a bit is set, the APID is marked as usable only by the default AP
2318f433524SCornelia Huckqueue device drivers; otherwise, the APID is usable by the vfio_ap
2328f433524SCornelia Huckdevice driver.
2338f433524SCornelia Huck
2348f433524SCornelia HuckThe ``aqmask`` is a 256-bit mask that identifies a set of AP queue indexes
2358f433524SCornelia Huck(APQI). Each bit in the mask, from left to right (i.e., from most significant
2368f433524SCornelia Huckto least significant bit in big endian order), corresponds to an APQI from
2378f433524SCornelia Huck0-255. If a bit is set, the APQI is marked as usable only by the default AP
2388f433524SCornelia Huckqueue device drivers; otherwise, the APQI is usable by the vfio_ap device
2398f433524SCornelia Huckdriver.
2408f433524SCornelia Huck
2418f433524SCornelia HuckTake, for example, the following mask::
2428f433524SCornelia Huck
2438f433524SCornelia Huck      0x7dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
2448f433524SCornelia Huck
2458f433524SCornelia HuckIt indicates:
2468f433524SCornelia Huck
2478f433524SCornelia Huck      1, 2, 3, 4, 5, and 7-255 belong to the default drivers' pool, and 0 and 6
2488f433524SCornelia Huck      belong to the vfio_ap device driver's pool.
2498f433524SCornelia Huck
2508f433524SCornelia HuckThe APQN of each AP queue device assigned to the linux host is checked by the
2518f433524SCornelia HuckAP bus against the set of APQNs derived from the cross product of APIDs
2528f433524SCornelia Huckand APQIs marked as usable only by the default AP queue device drivers. If a
2538f433524SCornelia Huckmatch is detected,  only the default AP queue device drivers will be probed;
2548f433524SCornelia Huckotherwise, the vfio_ap device driver will be probed.
2558f433524SCornelia Huck
2568f433524SCornelia HuckBy default, the two masks are set to reserve all APQNs for use by the default
2578f433524SCornelia HuckAP queue device drivers. There are two ways the default masks can be changed:
2588f433524SCornelia Huck
2598f433524SCornelia Huck 1. The sysfs mask files can be edited by echoing a string into the
2608f433524SCornelia Huck    respective sysfs mask file in one of two formats:
2618f433524SCornelia Huck
2628f433524SCornelia Huck    * An absolute hex string starting with 0x - like "0x12345678" - sets
2638f433524SCornelia Huck      the mask. If the given string is shorter than the mask, it is padded
2648f433524SCornelia Huck      with 0s on the right; for example, specifying a mask value of 0x41 is
2658f433524SCornelia Huck      the same as specifying::
2668f433524SCornelia Huck
2678f433524SCornelia Huck           0x4100000000000000000000000000000000000000000000000000000000000000
2688f433524SCornelia Huck
2698f433524SCornelia Huck      Keep in mind that the mask reads from left to right (i.e., most
2708f433524SCornelia Huck      significant to least significant bit in big endian order), so the mask
2718f433524SCornelia Huck      above identifies device numbers 1 and 7 (``01000001``).
2728f433524SCornelia Huck
2738f433524SCornelia Huck      If the string is longer than the mask, the operation is terminated with
2748f433524SCornelia Huck      an error (EINVAL).
2758f433524SCornelia Huck
2768f433524SCornelia Huck    * Individual bits in the mask can be switched on and off by specifying
2778f433524SCornelia Huck      each bit number to be switched in a comma separated list. Each bit
2788f433524SCornelia Huck      number string must be prepended with a (``+``) or minus (``-``) to indicate
2798f433524SCornelia Huck      the corresponding bit is to be switched on (``+``) or off (``-``). Some
2808f433524SCornelia Huck      valid values are::
2818f433524SCornelia Huck
2828f433524SCornelia Huck           "+0"    switches bit 0 on
2838f433524SCornelia Huck           "-13"   switches bit 13 off
2848f433524SCornelia Huck           "+0x41" switches bit 65 on
2858f433524SCornelia Huck           "-0xff" switches bit 255 off
2868f433524SCornelia Huck
2878f433524SCornelia Huck      The following example::
2888f433524SCornelia Huck
2898f433524SCornelia Huck              +0,-6,+0x47,-0xf0
2908f433524SCornelia Huck
2918f433524SCornelia Huck      Switches bits 0 and 71 (0x47) on
2928f433524SCornelia Huck      Switches bits 6 and 240 (0xf0) off
2938f433524SCornelia Huck
2948f433524SCornelia Huck      Note that the bits not specified in the list remain as they were before
2958f433524SCornelia Huck      the operation.
2968f433524SCornelia Huck
2978f433524SCornelia Huck 2. The masks can also be changed at boot time via parameters on the kernel
2988f433524SCornelia Huck    command line like this::
2998f433524SCornelia Huck
3008f433524SCornelia Huck         ap.apmask=0xffff ap.aqmask=0x40
3018f433524SCornelia Huck
3028f433524SCornelia Huck    This would create the following masks:
3038f433524SCornelia Huck
3048f433524SCornelia Huck    apmask::
3058f433524SCornelia Huck
3068f433524SCornelia Huck            0xffff000000000000000000000000000000000000000000000000000000000000
3078f433524SCornelia Huck
3088f433524SCornelia Huck    aqmask::
3098f433524SCornelia Huck
3108f433524SCornelia Huck            0x4000000000000000000000000000000000000000000000000000000000000000
3118f433524SCornelia Huck
3128f433524SCornelia Huck    Resulting in these two pools::
3138f433524SCornelia Huck
3148f433524SCornelia Huck            default drivers pool:    adapter 0-15, domain 1
3158f433524SCornelia Huck            alternate drivers pool:  adapter 16-255, domains 0, 2-255
3168f433524SCornelia Huck
3178f433524SCornelia HuckConfiguring an AP matrix for a linux guest
3188f433524SCornelia Huck~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3198f433524SCornelia Huck
3208f433524SCornelia HuckThe sysfs interfaces for configuring an AP matrix for a guest are built on the
3218f433524SCornelia HuckVFIO mediated device framework. To configure an AP matrix for a guest, a
3228f433524SCornelia Huckmediated matrix device must first be created for the ``/sys/devices/vfio_ap/matrix``
3238f433524SCornelia Huckdevice. When the vfio_ap device driver is loaded, it registers with the VFIO
3248f433524SCornelia Huckmediated device framework. When the driver registers, the sysfs interfaces for
3258f433524SCornelia Huckcreating mediated matrix devices is created::
3268f433524SCornelia Huck
3278f433524SCornelia Huck  /sys/devices
3288f433524SCornelia Huck  ... [vfio_ap]
3298f433524SCornelia Huck  ......[matrix]
3308f433524SCornelia Huck  ......... [mdev_supported_types]
3318f433524SCornelia Huck  ............ [vfio_ap-passthrough]
3328f433524SCornelia Huck  ............... create
3338f433524SCornelia Huck  ............... [devices]
3348f433524SCornelia Huck
3358f433524SCornelia HuckA mediated AP matrix device is created by writing a UUID to the attribute file
3368f433524SCornelia Hucknamed ``create``, for example::
3378f433524SCornelia Huck
3388f433524SCornelia Huck   uuidgen > create
3398f433524SCornelia Huck
3408f433524SCornelia Huckor
3418f433524SCornelia Huck
3428f433524SCornelia Huck::
3438f433524SCornelia Huck
3448f433524SCornelia Huck   echo $uuid > create
3458f433524SCornelia Huck
3468f433524SCornelia HuckWhen a mediated AP matrix device is created, a sysfs directory named after
3478f433524SCornelia Huckthe UUID is created in the ``devices`` subdirectory::
3488f433524SCornelia Huck
3498f433524SCornelia Huck  /sys/devices
3508f433524SCornelia Huck  ... [vfio_ap]
3518f433524SCornelia Huck  ......[matrix]
3528f433524SCornelia Huck  ......... [mdev_supported_types]
3538f433524SCornelia Huck  ............ [vfio_ap-passthrough]
3548f433524SCornelia Huck  ............... create
3558f433524SCornelia Huck  ............... [devices]
3568f433524SCornelia Huck  .................. [$uuid]
3578f433524SCornelia Huck
3588f433524SCornelia HuckThere will also be three sets of attribute files created in the mediated
3598f433524SCornelia Huckmatrix device's sysfs directory to configure an AP matrix for the
3608f433524SCornelia HuckKVM guest::
3618f433524SCornelia Huck
3628f433524SCornelia Huck  /sys/devices
3638f433524SCornelia Huck  ... [vfio_ap]
3648f433524SCornelia Huck  ......[matrix]
3658f433524SCornelia Huck  ......... [mdev_supported_types]
3668f433524SCornelia Huck  ............ [vfio_ap-passthrough]
3678f433524SCornelia Huck  ............... create
3688f433524SCornelia Huck  ............... [devices]
3698f433524SCornelia Huck  .................. [$uuid]
3708f433524SCornelia Huck  ..................... assign_adapter
3718f433524SCornelia Huck  ..................... assign_control_domain
3728f433524SCornelia Huck  ..................... assign_domain
3738f433524SCornelia Huck  ..................... matrix
3748f433524SCornelia Huck  ..................... unassign_adapter
3758f433524SCornelia Huck  ..................... unassign_control_domain
3768f433524SCornelia Huck  ..................... unassign_domain
3778f433524SCornelia Huck
3788f433524SCornelia Huck``assign_adapter``
3798f433524SCornelia Huck   To assign an AP adapter to the mediated matrix device, its APID is written
3808f433524SCornelia Huck   to the ``assign_adapter`` file. This may be done multiple times to assign more
3818f433524SCornelia Huck   than one adapter. The APID may be specified using conventional semantics
3828f433524SCornelia Huck   as a decimal, hexadecimal, or octal number. For example, to assign adapters
3838f433524SCornelia Huck   4, 5 and 16 to a mediated matrix device in decimal, hexadecimal and octal
3848f433524SCornelia Huck   respectively::
3858f433524SCornelia Huck
3868f433524SCornelia Huck       echo 4 > assign_adapter
3878f433524SCornelia Huck       echo 0x5 > assign_adapter
3888f433524SCornelia Huck       echo 020 > assign_adapter
3898f433524SCornelia Huck
3908f433524SCornelia Huck   In order to successfully assign an adapter:
3918f433524SCornelia Huck
3928f433524SCornelia Huck   * The adapter number specified must represent a value from 0 up to the
3938f433524SCornelia Huck     maximum adapter number allowed by the machine model. If an adapter number
3948f433524SCornelia Huck     higher than the maximum is specified, the operation will terminate with
3958f433524SCornelia Huck     an error (ENODEV).
3968f433524SCornelia Huck
3978f433524SCornelia Huck   * All APQNs that can be derived from the adapter ID being assigned and the
3988f433524SCornelia Huck     IDs of the previously assigned domains must be bound to the vfio_ap device
3998f433524SCornelia Huck     driver. If no domains have yet been assigned, then there must be at least
4008f433524SCornelia Huck     one APQN with the specified APID bound to the vfio_ap driver. If no such
4018f433524SCornelia Huck     APQNs are bound to the driver, the operation will terminate with an
4028f433524SCornelia Huck     error (EADDRNOTAVAIL).
4038f433524SCornelia Huck
4048f433524SCornelia Huck   * No APQN that can be derived from the adapter ID and the IDs of the
4058f433524SCornelia Huck     previously assigned domains can be assigned to another mediated matrix
4068f433524SCornelia Huck     device. If an APQN is assigned to another mediated matrix device, the
4078f433524SCornelia Huck     operation will terminate with an error (EADDRINUSE).
4088f433524SCornelia Huck
4098f433524SCornelia Huck``unassign_adapter``
4108f433524SCornelia Huck   To unassign an AP adapter, its APID is written to the ``unassign_adapter``
4118f433524SCornelia Huck   file. This may also be done multiple times to unassign more than one adapter.
4128f433524SCornelia Huck
4138f433524SCornelia Huck``assign_domain``
4148f433524SCornelia Huck   To assign a usage domain, the domain number is written into the
4158f433524SCornelia Huck   ``assign_domain`` file. This may be done multiple times to assign more than one
4168f433524SCornelia Huck   usage domain. The domain number is specified using conventional semantics as
4178f433524SCornelia Huck   a decimal, hexadecimal, or octal number. For example, to assign usage domains
4188f433524SCornelia Huck   4, 8, and 71 to a mediated matrix device in decimal, hexadecimal and octal
4198f433524SCornelia Huck   respectively::
4208f433524SCornelia Huck
4218f433524SCornelia Huck      echo 4 > assign_domain
4228f433524SCornelia Huck      echo 0x8 > assign_domain
4238f433524SCornelia Huck      echo 0107 > assign_domain
4248f433524SCornelia Huck
4258f433524SCornelia Huck   In order to successfully assign a domain:
4268f433524SCornelia Huck
4278f433524SCornelia Huck   * The domain number specified must represent a value from 0 up to the
4288f433524SCornelia Huck     maximum domain number allowed by the machine model. If a domain number
4298f433524SCornelia Huck     higher than the maximum is specified, the operation will terminate with
4308f433524SCornelia Huck     an error (ENODEV).
4318f433524SCornelia Huck
4328f433524SCornelia Huck   * All APQNs that can be derived from the domain ID being assigned and the IDs
4338f433524SCornelia Huck     of the previously assigned adapters must be bound to the vfio_ap device
4348f433524SCornelia Huck     driver. If no domains have yet been assigned, then there must be at least
4358f433524SCornelia Huck     one APQN with the specified APQI bound to the vfio_ap driver. If no such
4368f433524SCornelia Huck     APQNs are bound to the driver, the operation will terminate with an
4378f433524SCornelia Huck     error (EADDRNOTAVAIL).
4388f433524SCornelia Huck
4398f433524SCornelia Huck   * No APQN that can be derived from the domain ID being assigned and the IDs
4408f433524SCornelia Huck     of the previously assigned adapters can be assigned to another mediated
4418f433524SCornelia Huck     matrix device. If an APQN is assigned to another mediated matrix device,
4428f433524SCornelia Huck     the operation will terminate with an error (EADDRINUSE).
4438f433524SCornelia Huck
4448f433524SCornelia Huck``unassign_domain``
4458f433524SCornelia Huck   To unassign a usage domain, the domain number is written into the
4468f433524SCornelia Huck   ``unassign_domain`` file. This may be done multiple times to unassign more than
4478f433524SCornelia Huck   one usage domain.
4488f433524SCornelia Huck
4498f433524SCornelia Huck``assign_control_domain``
4508f433524SCornelia Huck   To assign a control domain, the domain number is written into the
4518f433524SCornelia Huck   ``assign_control_domain`` file. This may be done multiple times to
4528f433524SCornelia Huck   assign more than one control domain. The domain number may be specified using
4538f433524SCornelia Huck   conventional semantics as a decimal, hexadecimal, or octal number. For
4548f433524SCornelia Huck   example, to assign  control domains 4, 8, and 71 to  a mediated matrix device
4558f433524SCornelia Huck   in decimal, hexadecimal and octal respectively::
4568f433524SCornelia Huck
4578f433524SCornelia Huck      echo 4 > assign_domain
4588f433524SCornelia Huck      echo 0x8 > assign_domain
4598f433524SCornelia Huck      echo 0107 > assign_domain
4608f433524SCornelia Huck
4618f433524SCornelia Huck   In order to successfully assign a control domain, the domain number
4628f433524SCornelia Huck   specified must represent a value from 0 up to the maximum domain number
4638f433524SCornelia Huck   allowed by the machine model. If a control domain number higher than the
4648f433524SCornelia Huck   maximum is specified, the operation will terminate with an error (ENODEV).
4658f433524SCornelia Huck
4668f433524SCornelia Huck``unassign_control_domain``
4678f433524SCornelia Huck   To unassign a control domain, the domain number is written into the
4688f433524SCornelia Huck   ``unassign_domain`` file. This may be done multiple times to unassign more than
4698f433524SCornelia Huck   one control domain.
4708f433524SCornelia Huck
4718f433524SCornelia HuckNotes: No changes to the AP matrix will be allowed while a guest using
4728f433524SCornelia Huckthe mediated matrix device is running. Attempts to assign an adapter,
4738f433524SCornelia Huckdomain or control domain will be rejected and an error (EBUSY) returned.
4748f433524SCornelia Huck
4758f433524SCornelia HuckStarting a Linux Guest Configured with an AP Matrix
4768f433524SCornelia Huck~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4778f433524SCornelia Huck
4788f433524SCornelia HuckTo provide a mediated matrix device for use by a guest, the following option
4798f433524SCornelia Huckmust be specified on the QEMU command line::
4808f433524SCornelia Huck
4818f433524SCornelia Huck   -device vfio_ap,sysfsdev=$path-to-mdev
4828f433524SCornelia Huck
4838f433524SCornelia HuckThe sysfsdev parameter specifies the path to the mediated matrix device.
4848f433524SCornelia HuckThere are a number of ways to specify this path::
4858f433524SCornelia Huck
4868f433524SCornelia Huck  /sys/devices/vfio_ap/matrix/$uuid
4878f433524SCornelia Huck  /sys/bus/mdev/devices/$uuid
4888f433524SCornelia Huck  /sys/bus/mdev/drivers/vfio_mdev/$uuid
4898f433524SCornelia Huck  /sys/devices/vfio_ap/matrix/mdev_supported_types/vfio_ap-passthrough/devices/$uuid
4908f433524SCornelia Huck
4918f433524SCornelia HuckWhen the linux guest is started, the guest will open the mediated
4928f433524SCornelia Huckmatrix device's file descriptor to get information about the mediated matrix
4938f433524SCornelia Huckdevice. The ``vfio_ap`` device driver will update the APM, AQM, and ADM fields in
4948f433524SCornelia Huckthe guest's CRYCB with the adapter, usage domain and control domains assigned
4958f433524SCornelia Huckvia the mediated matrix device's sysfs attribute files. Programs running on the
4968f433524SCornelia Hucklinux guest will then:
4978f433524SCornelia Huck
4988f433524SCornelia Huck1. Have direct access to the APQNs derived from the cross product of the AP
4998f433524SCornelia Huck   adapter numbers (APID) and queue indexes (APQI) specified in the APM and AQM
5008f433524SCornelia Huck   fields of the guests's CRYCB respectively. These APQNs identify the AP queues
5018f433524SCornelia Huck   that are valid for use by the guest; meaning, AP commands can be sent by the
5028f433524SCornelia Huck   guest to any of these queues for processing.
5038f433524SCornelia Huck
5048f433524SCornelia Huck2. Have authorization to process AP commands to change a control domain
5058f433524SCornelia Huck   identified in the ADM field of the guest's CRYCB. The AP command must be sent
5068f433524SCornelia Huck   to a valid APQN (see 1 above).
5078f433524SCornelia Huck
5088f433524SCornelia HuckCPU model features:
5098f433524SCornelia Huck
5108f433524SCornelia HuckThree CPU model features are available for controlling guest access to AP
5118f433524SCornelia Huckfacilities:
5128f433524SCornelia Huck
5138f433524SCornelia Huck1. AP facilities feature
5148f433524SCornelia Huck
5158f433524SCornelia Huck   The AP facilities feature indicates that AP facilities are installed on the
5168f433524SCornelia Huck   guest. This feature will be exposed for use only if the AP facilities
5178f433524SCornelia Huck   are installed on the host system. The feature is s390-specific and is
5188f433524SCornelia Huck   represented as a parameter of the -cpu option on the QEMU command line::
5198f433524SCornelia Huck
5208f433524SCornelia Huck      qemu-system-s390x -cpu $model,ap=on|off
5218f433524SCornelia Huck
5228f433524SCornelia Huck   Where:
5238f433524SCornelia Huck
5248f433524SCornelia Huck      ``$model``
5258f433524SCornelia Huck        is the CPU model defined for the guest (defaults to the model of
5268f433524SCornelia Huck        the host system if not specified).
5278f433524SCornelia Huck
5288f433524SCornelia Huck      ``ap=on|off``
5298f433524SCornelia Huck        indicates whether AP facilities are installed (on) or not
5308f433524SCornelia Huck        (off). The default for CPU models zEC12 or newer
5318f433524SCornelia Huck        is ``ap=on``. AP facilities must be installed on the guest if a
5328f433524SCornelia Huck        vfio-ap device (``-device vfio-ap,sysfsdev=$path``) is configured
5338f433524SCornelia Huck        for the guest, or the guest will fail to start.
5348f433524SCornelia Huck
5358f433524SCornelia Huck2. Query Configuration Information (QCI) facility
5368f433524SCornelia Huck
5378f433524SCornelia Huck   The QCI facility is used by the AP bus running on the guest to query the
5388f433524SCornelia Huck   configuration of the AP facilities. This facility will be available
5398f433524SCornelia Huck   only if the QCI facility is installed on the host system. The feature is
5408f433524SCornelia Huck   s390-specific and is represented as a parameter of the -cpu option on the
5418f433524SCornelia Huck   QEMU command line::
5428f433524SCornelia Huck
5438f433524SCornelia Huck      qemu-system-s390x -cpu $model,apqci=on|off
5448f433524SCornelia Huck
5458f433524SCornelia Huck   Where:
5468f433524SCornelia Huck
5478f433524SCornelia Huck      ``$model``
5488f433524SCornelia Huck        is the CPU model defined for the guest
5498f433524SCornelia Huck
5508f433524SCornelia Huck      ``apqci=on|off``
5518f433524SCornelia Huck        indicates whether the QCI facility is installed (on) or
5528f433524SCornelia Huck        not (off). The default for CPU models zEC12 or newer
5538f433524SCornelia Huck        is ``apqci=on``; for older models, QCI will not be installed.
5548f433524SCornelia Huck
5558f433524SCornelia Huck        If QCI is installed (``apqci=on``) but AP facilities are not
5568f433524SCornelia Huck        (``ap=off``), an error message will be logged, but the guest
5578f433524SCornelia Huck        will be allowed to start. It makes no sense to have QCI
5588f433524SCornelia Huck        installed if the AP facilities are not; this is considered
5598f433524SCornelia Huck        an invalid configuration.
5608f433524SCornelia Huck
5618f433524SCornelia Huck        If the QCI facility is not installed, APQNs with an APQI
5628f433524SCornelia Huck        greater than 15 will not be detected by the AP bus
5638f433524SCornelia Huck        running on the guest.
5648f433524SCornelia Huck
5658f433524SCornelia Huck3. Adjunct Process Facility Test (APFT) facility
5668f433524SCornelia Huck
5678f433524SCornelia Huck   The APFT facility is used by the AP bus running on the guest to test the
5688f433524SCornelia Huck   AP facilities available for a given AP queue. This facility will be available
5698f433524SCornelia Huck   only if the APFT facility is installed on the host system. The feature is
5708f433524SCornelia Huck   s390-specific and is represented as a parameter of the -cpu option on the
5718f433524SCornelia Huck   QEMU command line::
5728f433524SCornelia Huck
5738f433524SCornelia Huck      qemu-system-s390x -cpu $model,apft=on|off
5748f433524SCornelia Huck
5758f433524SCornelia Huck   Where:
5768f433524SCornelia Huck
5778f433524SCornelia Huck      ``$model``
5788f433524SCornelia Huck        is the CPU model defined for the guest (defaults to the model of
5798f433524SCornelia Huck        the host system if not specified).
5808f433524SCornelia Huck
5818f433524SCornelia Huck      ``apft=on|off``
5828f433524SCornelia Huck        indicates whether the APFT facility is installed (on) or
5838f433524SCornelia Huck        not (off). The default for CPU models zEC12 and
5848f433524SCornelia Huck        newer is ``apft=on`` for older models, APFT will not be
5858f433524SCornelia Huck        installed.
5868f433524SCornelia Huck
5878f433524SCornelia Huck        If APFT is installed (``apft=on``) but AP facilities are not
5888f433524SCornelia Huck        (``ap=off``), an error message will be logged, but the guest
5898f433524SCornelia Huck        will be allowed to start. It makes no sense to have APFT
5908f433524SCornelia Huck        installed if the AP facilities are not; this is considered
5918f433524SCornelia Huck        an invalid configuration.
5928f433524SCornelia Huck
5938f433524SCornelia Huck        It also makes no sense to turn APFT off because the AP bus
5948f433524SCornelia Huck        running on the guest will not detect CEX4 and newer devices
5958f433524SCornelia Huck        without it. Since only CEX4 and newer devices are supported
5968f433524SCornelia Huck        for guest usage, no AP devices can be made accessible to a
5978f433524SCornelia Huck        guest started without APFT installed.
5988f433524SCornelia Huck
5998f433524SCornelia HuckHot plug a vfio-ap device into a running guest
6008f433524SCornelia Huck~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6018f433524SCornelia Huck
6028f433524SCornelia HuckOnly one vfio-ap device can be attached to the virtual machine's ap-bus, so a
6038f433524SCornelia Huckvfio-ap device can be hot plugged if and only if no vfio-ap device is attached
6048f433524SCornelia Huckto the bus already, whether via the QEMU command line or a prior hot plug
6058f433524SCornelia Huckaction.
6068f433524SCornelia Huck
6078f433524SCornelia HuckTo hot plug a vfio-ap device, use the QEMU ``device_add`` command::
6088f433524SCornelia Huck
609*458e0562SChristian Borntraeger    (qemu) device_add vfio-ap,sysfsdev="$path-to-mdev",id="$id"
6108f433524SCornelia Huck
6118f433524SCornelia HuckWhere the ``$path-to-mdev`` value specifies the absolute path to a mediated
6128f433524SCornelia Huckdevice to which AP resources to be used by the guest have been assigned.
613*458e0562SChristian Borntraeger``$id`` is the name value for the optional id parameter.
6148f433524SCornelia Huck
6158f433524SCornelia HuckNote that on Linux guests, the AP devices will be created in the
6168f433524SCornelia Huck``/sys/bus/ap/devices`` directory when the AP bus subsequently performs its periodic
6178f433524SCornelia Huckscan, so there may be a short delay before the AP devices are accessible on the
6188f433524SCornelia Huckguest.
6198f433524SCornelia Huck
6208f433524SCornelia HuckThe command will fail if:
6218f433524SCornelia Huck
6228f433524SCornelia Huck* A vfio-ap device has already been attached to the virtual machine's ap-bus.
6238f433524SCornelia Huck
6248f433524SCornelia Huck* The CPU model features for controlling guest access to AP facilities are not
6258f433524SCornelia Huck  enabled (see 'CPU model features' subsection in the previous section).
6268f433524SCornelia Huck
6278f433524SCornelia HuckHot unplug a vfio-ap device from a running guest
6288f433524SCornelia Huck~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6298f433524SCornelia Huck
6308f433524SCornelia HuckA vfio-ap device can be unplugged from a running KVM guest if a vfio-ap device
6318f433524SCornelia Huckhas been attached to the virtual machine's ap-bus via the QEMU command line
6328f433524SCornelia Huckor a prior hot plug action.
6338f433524SCornelia Huck
6348f433524SCornelia HuckTo hot unplug a vfio-ap device, use the QEMU ``device_del`` command::
6358f433524SCornelia Huck
636*458e0562SChristian Borntraeger    (qemu) device_del "$id"
6378f433524SCornelia Huck
638*458e0562SChristian BorntraegerWhere ``$id`` is the same id that was specified at device creation.
6398f433524SCornelia Huck
6408f433524SCornelia HuckOn a Linux guest, the AP devices will be removed from the ``/sys/bus/ap/devices``
6418f433524SCornelia Huckdirectory on the guest when the AP bus subsequently performs its periodic scan,
6428f433524SCornelia Huckso there may be a short delay before the AP devices are no longer accessible by
6438f433524SCornelia Huckthe guest.
6448f433524SCornelia Huck
6458f433524SCornelia HuckThe command will fail if the ``$path-to-mdev`` specified on the ``device_del`` command
6468f433524SCornelia Huckdoes not match the value specified when the vfio-ap device was attached to
6478f433524SCornelia Huckthe virtual machine's ap-bus.
6488f433524SCornelia Huck
6498f433524SCornelia HuckExample: Configure AP Matrices for Three Linux Guests
6508f433524SCornelia Huck-----------------------------------------------------
6518f433524SCornelia Huck
6528f433524SCornelia HuckLet's now provide an example to illustrate how KVM guests may be given
6538f433524SCornelia Huckaccess to AP facilities. For this example, we will show how to configure
6548f433524SCornelia Huckthree guests such that executing the lszcrypt command on the guests would
6558f433524SCornelia Hucklook like this:
6568f433524SCornelia Huck
6578f433524SCornelia HuckGuest1::
6588f433524SCornelia Huck
6598f433524SCornelia Huck  CARD.DOMAIN TYPE  MODE
6608f433524SCornelia Huck  ------------------------------
6618f433524SCornelia Huck  05          CEX5C CCA-Coproc
6628f433524SCornelia Huck  05.0004     CEX5C CCA-Coproc
6638f433524SCornelia Huck  05.00ab     CEX5C CCA-Coproc
6648f433524SCornelia Huck  06          CEX5A Accelerator
6658f433524SCornelia Huck  06.0004     CEX5A Accelerator
6668f433524SCornelia Huck  06.00ab     CEX5C CCA-Coproc
6678f433524SCornelia Huck
6688f433524SCornelia HuckGuest2::
6698f433524SCornelia Huck
6708f433524SCornelia Huck  CARD.DOMAIN TYPE  MODE
6718f433524SCornelia Huck  ------------------------------
6728f433524SCornelia Huck  05          CEX5A Accelerator
6738f433524SCornelia Huck  05.0047     CEX5A Accelerator
6748f433524SCornelia Huck  05.00ff     CEX5A Accelerator
6758f433524SCornelia Huck
6768f433524SCornelia HuckGuest3::
6778f433524SCornelia Huck
6788f433524SCornelia Huck  CARD.DOMAIN TYPE  MODE
6798f433524SCornelia Huck  ------------------------------
6808f433524SCornelia Huck  06          CEX5A Accelerator
6818f433524SCornelia Huck  06.0047     CEX5A Accelerator
6828f433524SCornelia Huck  06.00ff     CEX5A Accelerator
6838f433524SCornelia Huck
6848f433524SCornelia HuckThese are the steps:
6858f433524SCornelia Huck
6868f433524SCornelia Huck1. Install the vfio_ap module on the linux host. The dependency chain for the
6878f433524SCornelia Huck   vfio_ap module is:
6888f433524SCornelia Huck
6898f433524SCornelia Huck   * iommu
6908f433524SCornelia Huck   * s390
6918f433524SCornelia Huck   * zcrypt
6928f433524SCornelia Huck   * vfio
6938f433524SCornelia Huck   * vfio_mdev
6948f433524SCornelia Huck   * vfio_mdev_device
6958f433524SCornelia Huck   * KVM
6968f433524SCornelia Huck
6978f433524SCornelia Huck   To build the vfio_ap module, the kernel build must be configured with the
6988f433524SCornelia Huck   following Kconfig elements selected:
6998f433524SCornelia Huck
7008f433524SCornelia Huck   * IOMMU_SUPPORT
7018f433524SCornelia Huck   * S390
7028f433524SCornelia Huck   * ZCRYPT
7038f433524SCornelia Huck   * S390_AP_IOMMU
7048f433524SCornelia Huck   * VFIO
7058f433524SCornelia Huck   * VFIO_MDEV
7068f433524SCornelia Huck   * VFIO_MDEV_DEVICE
7078f433524SCornelia Huck   * KVM
7088f433524SCornelia Huck
7098f433524SCornelia Huck   If using make menuconfig select the following to build the vfio_ap module::
7108f433524SCornelia Huck     -> Device Drivers
7118f433524SCornelia Huck        -> IOMMU Hardware Support
7128f433524SCornelia Huck           select S390 AP IOMMU Support
7138f433524SCornelia Huck        -> VFIO Non-Privileged userspace driver framework
7148f433524SCornelia Huck           -> Mediated device driver framework
7158f433524SCornelia Huck              -> VFIO driver for Mediated devices
7168f433524SCornelia Huck     -> I/O subsystem
7178f433524SCornelia Huck        -> VFIO support for AP devices
7188f433524SCornelia Huck
7198f433524SCornelia Huck2. Secure the AP queues to be used by the three guests so that the host can not
7208f433524SCornelia Huck   access them. To secure the AP queues 05.0004, 05.0047, 05.00ab, 05.00ff,
7218f433524SCornelia Huck   06.0004, 06.0047, 06.00ab, and 06.00ff for use by the vfio_ap device driver,
7228f433524SCornelia Huck   the corresponding APQNs must be removed from the default queue drivers pool
7238f433524SCornelia Huck   as follows::
7248f433524SCornelia Huck
7258f433524SCornelia Huck      echo -5,-6 > /sys/bus/ap/apmask
7268f433524SCornelia Huck
7278f433524SCornelia Huck      echo -4,-0x47,-0xab,-0xff > /sys/bus/ap/aqmask
7288f433524SCornelia Huck
7298f433524SCornelia Huck   This will result in AP queues 05.0004, 05.0047, 05.00ab, 05.00ff, 06.0004,
7308f433524SCornelia Huck   06.0047, 06.00ab, and 06.00ff getting bound to the vfio_ap device driver. The
7318f433524SCornelia Huck   sysfs directory for the vfio_ap device driver will now contain symbolic links
7328f433524SCornelia Huck   to the AP queue devices bound to it::
7338f433524SCornelia Huck
7348f433524SCornelia Huck     /sys/bus/ap
7358f433524SCornelia Huck     ... [drivers]
7368f433524SCornelia Huck     ...... [vfio_ap]
7378f433524SCornelia Huck     ......... [05.0004]
7388f433524SCornelia Huck     ......... [05.0047]
7398f433524SCornelia Huck     ......... [05.00ab]
7408f433524SCornelia Huck     ......... [05.00ff]
7418f433524SCornelia Huck     ......... [06.0004]
7428f433524SCornelia Huck     ......... [06.0047]
7438f433524SCornelia Huck     ......... [06.00ab]
7448f433524SCornelia Huck     ......... [06.00ff]
7458f433524SCornelia Huck
7468f433524SCornelia Huck   Keep in mind that only type 10 and newer adapters (i.e., CEX4 and later)
7478f433524SCornelia Huck   can be bound to the vfio_ap device driver. The reason for this is to
7488f433524SCornelia Huck   simplify the implementation by not needlessly complicating the design by
7498f433524SCornelia Huck   supporting older devices that will go out of service in the relatively near
7508f433524SCornelia Huck   future, and for which there are few older systems on which to test.
7518f433524SCornelia Huck
7528f433524SCornelia Huck   The administrator, therefore, must take care to secure only AP queues that
7538f433524SCornelia Huck   can be bound to the vfio_ap device driver. The device type for a given AP
7548f433524SCornelia Huck   queue device can be read from the parent card's sysfs directory. For example,
7558f433524SCornelia Huck   to see the hardware type of the queue 05.0004::
7568f433524SCornelia Huck
7578f433524SCornelia Huck     cat /sys/bus/ap/devices/card05/hwtype
7588f433524SCornelia Huck
7598f433524SCornelia Huck   The hwtype must be 10 or higher (CEX4 or newer) in order to be bound to the
7608f433524SCornelia Huck   vfio_ap device driver.
7618f433524SCornelia Huck
7628f433524SCornelia Huck3. Create the mediated devices needed to configure the AP matrixes for the
7638f433524SCornelia Huck   three guests and to provide an interface to the vfio_ap driver for
7648f433524SCornelia Huck   use by the guests::
7658f433524SCornelia Huck
7668f433524SCornelia Huck     /sys/devices/vfio_ap/matrix/
7678f433524SCornelia Huck     ... [mdev_supported_types]
7688f433524SCornelia Huck     ...... [vfio_ap-passthrough] (passthrough mediated matrix device type)
7698f433524SCornelia Huck     ......... create
7708f433524SCornelia Huck     ......... [devices]
7718f433524SCornelia Huck
7728f433524SCornelia Huck   To create the mediated devices for the three guests::
7738f433524SCornelia Huck
7748f433524SCornelia Huck       uuidgen > create
7758f433524SCornelia Huck       uuidgen > create
7768f433524SCornelia Huck       uuidgen > create
7778f433524SCornelia Huck
7788f433524SCornelia Huck   or
7798f433524SCornelia Huck
7808f433524SCornelia Huck   ::
7818f433524SCornelia Huck
7828f433524SCornelia Huck       echo $uuid1 > create
7838f433524SCornelia Huck       echo $uuid2 > create
7848f433524SCornelia Huck       echo $uuid3 > create
7858f433524SCornelia Huck
7868f433524SCornelia Huck   This will create three mediated devices in the [devices] subdirectory named
7878f433524SCornelia Huck   after the UUID used to create the mediated device. We'll call them $uuid1,
7888f433524SCornelia Huck   $uuid2 and $uuid3 and this is the sysfs directory structure after creation::
7898f433524SCornelia Huck
7908f433524SCornelia Huck     /sys/devices/vfio_ap/matrix/
7918f433524SCornelia Huck     ... [mdev_supported_types]
7928f433524SCornelia Huck     ...... [vfio_ap-passthrough]
7938f433524SCornelia Huck     ......... [devices]
7948f433524SCornelia Huck     ............ [$uuid1]
7958f433524SCornelia Huck     ............... assign_adapter
7968f433524SCornelia Huck     ............... assign_control_domain
7978f433524SCornelia Huck     ............... assign_domain
7988f433524SCornelia Huck     ............... matrix
7998f433524SCornelia Huck     ............... unassign_adapter
8008f433524SCornelia Huck     ............... unassign_control_domain
8018f433524SCornelia Huck     ............... unassign_domain
8028f433524SCornelia Huck
8038f433524SCornelia Huck     ............ [$uuid2]
8048f433524SCornelia Huck     ............... assign_adapter
8058f433524SCornelia Huck     ............... assign_control_domain
8068f433524SCornelia Huck     ............... assign_domain
8078f433524SCornelia Huck     ............... matrix
8088f433524SCornelia Huck     ............... unassign_adapter
8098f433524SCornelia Huck     ............... unassign_control_domain
8108f433524SCornelia Huck     ............... unassign_domain
8118f433524SCornelia Huck
8128f433524SCornelia Huck     ............ [$uuid3]
8138f433524SCornelia Huck     ............... assign_adapter
8148f433524SCornelia Huck     ............... assign_control_domain
8158f433524SCornelia Huck     ............... assign_domain
8168f433524SCornelia Huck     ............... matrix
8178f433524SCornelia Huck     ............... unassign_adapter
8188f433524SCornelia Huck     ............... unassign_control_domain
8198f433524SCornelia Huck     ............... unassign_domain
8208f433524SCornelia Huck
8218f433524SCornelia Huck4. The administrator now needs to configure the matrixes for the mediated
8228f433524SCornelia Huck   devices $uuid1 (for Guest1), $uuid2 (for Guest2) and $uuid3 (for Guest3).
8238f433524SCornelia Huck
8248f433524SCornelia Huck   This is how the matrix is configured for Guest1::
8258f433524SCornelia Huck
8268f433524SCornelia Huck      echo 5 > assign_adapter
8278f433524SCornelia Huck      echo 6 > assign_adapter
8288f433524SCornelia Huck      echo 4 > assign_domain
8298f433524SCornelia Huck      echo 0xab > assign_domain
8308f433524SCornelia Huck
8318f433524SCornelia Huck   Control domains can similarly be assigned using the assign_control_domain
8328f433524SCornelia Huck   sysfs file.
8338f433524SCornelia Huck
8348f433524SCornelia Huck   If a mistake is made configuring an adapter, domain or control domain,
8358f433524SCornelia Huck   you can use the ``unassign_xxx`` interfaces to unassign the adapter, domain or
8368f433524SCornelia Huck   control domain.
8378f433524SCornelia Huck
8388f433524SCornelia Huck   To display the matrix configuration for Guest1::
8398f433524SCornelia Huck
8408f433524SCornelia Huck         cat matrix
8418f433524SCornelia Huck
8428f433524SCornelia Huck   The output will display the APQNs in the format ``xx.yyyy``, where xx is
8438f433524SCornelia Huck   the adapter number and yyyy is the domain number. The output for Guest1
8448f433524SCornelia Huck   will look like this::
8458f433524SCornelia Huck
8468f433524SCornelia Huck         05.0004
8478f433524SCornelia Huck         05.00ab
8488f433524SCornelia Huck         06.0004
8498f433524SCornelia Huck         06.00ab
8508f433524SCornelia Huck
8518f433524SCornelia Huck   This is how the matrix is configured for Guest2::
8528f433524SCornelia Huck
8538f433524SCornelia Huck      echo 5 > assign_adapter
8548f433524SCornelia Huck      echo 0x47 > assign_domain
8558f433524SCornelia Huck      echo 0xff > assign_domain
8568f433524SCornelia Huck
8578f433524SCornelia Huck   This is how the matrix is configured for Guest3::
8588f433524SCornelia Huck
8598f433524SCornelia Huck      echo 6 > assign_adapter
8608f433524SCornelia Huck      echo 0x47 > assign_domain
8618f433524SCornelia Huck      echo 0xff > assign_domain
8628f433524SCornelia Huck
8638f433524SCornelia Huck5. Start Guest1::
8648f433524SCornelia Huck
8658f433524SCornelia Huck   /usr/bin/qemu-system-s390x ... -cpu host,ap=on,apqci=on,apft=on -device vfio-ap,sysfsdev=/sys/devices/vfio_ap/matrix/$uuid1 ...
8668f433524SCornelia Huck
8678f433524SCornelia Huck7. Start Guest2::
8688f433524SCornelia Huck
8698f433524SCornelia Huck   /usr/bin/qemu-system-s390x ... -cpu host,ap=on,apqci=on,apft=on -device vfio-ap,sysfsdev=/sys/devices/vfio_ap/matrix/$uuid2 ...
8708f433524SCornelia Huck
8718f433524SCornelia Huck7. Start Guest3::
8728f433524SCornelia Huck
8738f433524SCornelia Huck   /usr/bin/qemu-system-s390x ... -cpu host,ap=on,apqci=on,apft=on -device vfio-ap,sysfsdev=/sys/devices/vfio_ap/matrix/$uuid3 ...
8748f433524SCornelia Huck
8758f433524SCornelia HuckWhen the guest is shut down, the mediated matrix devices may be removed.
8768f433524SCornelia Huck
8778f433524SCornelia HuckUsing our example again, to remove the mediated matrix device $uuid1::
8788f433524SCornelia Huck
8798f433524SCornelia Huck   /sys/devices/vfio_ap/matrix/
8808f433524SCornelia Huck   ... [mdev_supported_types]
8818f433524SCornelia Huck   ...... [vfio_ap-passthrough]
8828f433524SCornelia Huck   ......... [devices]
8838f433524SCornelia Huck   ............ [$uuid1]
8848f433524SCornelia Huck   ............... remove
8858f433524SCornelia Huck
8868f433524SCornelia Huck
8878f433524SCornelia Huck   echo 1 > remove
8888f433524SCornelia Huck
8898f433524SCornelia HuckThis will remove all of the mdev matrix device's sysfs structures including
8908f433524SCornelia Huckthe mdev device itself. To recreate and reconfigure the mdev matrix device,
8918f433524SCornelia Huckall of the steps starting with step 3 will have to be performed again. Note
8928f433524SCornelia Huckthat the remove will fail if a guest using the mdev is still running.
8938f433524SCornelia Huck
8948f433524SCornelia HuckIt is not necessary to remove an mdev matrix device, but one may want to
8958f433524SCornelia Huckremove it if no guest will use it during the remaining lifetime of the linux
8968f433524SCornelia Huckhost. If the mdev matrix device is removed, one may want to also reconfigure
8978f433524SCornelia Huckthe pool of adapters and queues reserved for use by the default drivers.
8988f433524SCornelia Huck
8998f433524SCornelia HuckLimitations
9008f433524SCornelia Huck-----------
9018f433524SCornelia Huck
9028f433524SCornelia Huck* The KVM/kernel interfaces do not provide a way to prevent restoring an APQN
9038f433524SCornelia Huck  to the default drivers pool of a queue that is still assigned to a mediated
9048f433524SCornelia Huck  device in use by a guest. It is incumbent upon the administrator to
9058f433524SCornelia Huck  ensure there is no mediated device in use by a guest to which the APQN is
9068f433524SCornelia Huck  assigned lest the host be given access to the private data of the AP queue
9078f433524SCornelia Huck  device, such as a private key configured specifically for the guest.
9088f433524SCornelia Huck
9098f433524SCornelia Huck* Dynamically assigning AP resources to or unassigning AP resources from a
9108f433524SCornelia Huck  mediated matrix device - see `Configuring an AP matrix for a linux guest`_
9118f433524SCornelia Huck  section above - while a running guest is using it is currently not supported.
9128f433524SCornelia Huck
9138f433524SCornelia Huck* Live guest migration is not supported for guests using AP devices. If a guest
9148f433524SCornelia Huck  is using AP devices, the vfio-ap device configured for the guest must be
9158f433524SCornelia Huck  unplugged before migrating the guest (see `Hot unplug a vfio-ap device from a
9168f433524SCornelia Huck  running guest`_ section above.)
917