yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-s390-doc-detailed-specifications-for-AP-virtualizati.patch

ae23c9
From 3caa3a2cfbb83be5f52484a0542edc36cfac7b66 Mon Sep 17 00:00:00 2001
ae23c9
From: Thomas Huth <thuth@redhat.com>
ae23c9
Date: Mon, 15 Oct 2018 10:19:31 +0100
ae23c9
Subject: [PATCH 6/6] s390: doc: detailed specifications for AP virtualization
ae23c9
ae23c9
RH-Author: Thomas Huth <thuth@redhat.com>
ae23c9
Message-id: <1539598771-16223-7-git-send-email-thuth@redhat.com>
ae23c9
Patchwork-id: 82699
ae23c9
O-Subject: [RHEL-8 qemu-kvm PATCH 6/6] s390: doc: detailed specifications for AP virtualization
ae23c9
Bugzilla: 1508142
ae23c9
RH-Acked-by: David Hildenbrand <david@redhat.com>
ae23c9
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
ae23c9
RH-Acked-by: Jens Freimann <jfreimann@redhat.com>
ae23c9
ae23c9
From: Tony Krowiak <akrowiak@linux.ibm.com>
ae23c9
ae23c9
This patch provides documentation describing the AP architecture and
ae23c9
design concepts behind the virtualization of AP devices. It also
ae23c9
includes an example of how to configure AP devices for exclusive
ae23c9
use of KVM guests.
ae23c9
ae23c9
Signed-off-by: Tony Krowiak <akrowiak@linux.ibm.com>
ae23c9
Reviewed-by: Pierre Morel <pmorel@linux.ibm.com>
ae23c9
Tested-by: Pierre Morel <pmorel@linux.ibm.com>
ae23c9
Tested-by: Christian Borntraeger <borntraeger@de.ibm.com>
ae23c9
Message-Id: <20181010170309.12045-7-akrowiak@linux.ibm.com>
ae23c9
Signed-off-by: Cornelia Huck <cohuck@redhat.com>
ae23c9
(cherry picked from commit 694a8d703bfe06226a0574f5ec4af17a2b7060ef)
ae23c9
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ae23c9
---
ae23c9
 MAINTAINERS      |   2 +
ae23c9
 docs/vfio-ap.txt | 825 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
ae23c9
 2 files changed, 827 insertions(+)
ae23c9
 create mode 100644 docs/vfio-ap.txt
ae23c9
ae23c9
diff --git a/MAINTAINERS b/MAINTAINERS
ae23c9
index 99694d8..9b74756 100644
ae23c9
--- a/MAINTAINERS
ae23c9
+++ b/MAINTAINERS
ae23c9
@@ -96,6 +96,7 @@ F: include/hw/watchdog/wdt_diag288.h
ae23c9
 F: pc-bios/s390-ccw/
ae23c9
 F: pc-bios/s390-ccw.img
ae23c9
 F: target/s390x/
ae23c9
+F: docs/vfio-ap.txt
ae23c9
 K: ^Subject:.*(?i)s390x?
ae23c9
 T: git git://github.com/cohuck/qemu.git s390-next
ae23c9
 L: qemu-s390x@nongnu.org
ae23c9
@@ -1164,6 +1165,7 @@ F: hw/s390x/ap-bridge.c
ae23c9
 F: include/hw/s390x/ap-device.h
ae23c9
 F: include/hw/s390x/ap-bridge.h
ae23c9
 F: hw/vfio/ap.c
ae23c9
+F: docs/vfio-ap.txt
ae23c9
 L: qemu-s390x@nongnu.org
ae23c9
 
ae23c9
 vhost
ae23c9
diff --git a/docs/vfio-ap.txt b/docs/vfio-ap.txt
ae23c9
new file mode 100644
ae23c9
index 0000000..1233968
ae23c9
--- /dev/null
ae23c9
+++ b/docs/vfio-ap.txt
ae23c9
@@ -0,0 +1,825 @@
ae23c9
+Adjunct Processor (AP) Device
ae23c9
+=============================
ae23c9
+
ae23c9
+Contents:
ae23c9
+=========
ae23c9
+* Introduction
ae23c9
+* AP Architectural Overview
ae23c9
+* Start Interpretive Execution (SIE) Instruction
ae23c9
+* AP Matrix Configuration on Linux Host
ae23c9
+* Starting a Linux Guest Configured with an AP Matrix
ae23c9
+* Example: Configure AP Matrices for Three Linux Guests
ae23c9
+
ae23c9
+Introduction:
ae23c9
+============
ae23c9
+The IBM Adjunct Processor (AP) Cryptographic Facility is comprised
ae23c9
+of three AP instructions and from 1 to 256 PCIe cryptographic adapter cards.
ae23c9
+These AP devices provide cryptographic functions to all CPUs assigned to a
ae23c9
+linux system running in an IBM Z system LPAR.
ae23c9
+
ae23c9
+On s390x, AP adapter cards are exposed via the AP bus. This document
ae23c9
+describes how those cards may be made available to KVM guests using the
ae23c9
+VFIO mediated device framework.
ae23c9
+
ae23c9
+AP Architectural Overview:
ae23c9
+=========================
ae23c9
+In order understand the terminology used in the rest of this document, let's
ae23c9
+start with some definitions:
ae23c9
+
ae23c9
+* AP adapter
ae23c9
+
ae23c9
+  An AP adapter is an IBM Z adapter card that can perform cryptographic
ae23c9
+  functions. There can be from 0 to 256 adapters assigned to an LPAR depending
ae23c9
+  on the machine model. Adapters assigned to the LPAR in which a linux host is
ae23c9
+  running will be available to the linux host. Each adapter is identified by a
ae23c9
+  number from 0 to 255; however, the maximum adapter number allowed is
ae23c9
+  determined by machine model. When installed, an AP adapter is accessed by
ae23c9
+  AP instructions executed by any CPU.
ae23c9
+
ae23c9
+* AP domain
ae23c9
+
ae23c9
+  An adapter is partitioned into domains. Each domain can be thought of as
ae23c9
+  a set of hardware registers for processing AP instructions. An adapter can
ae23c9
+  hold up to 256 domains; however, the maximum domain number allowed is
ae23c9
+  determined by machine model. Each domain is identified by a number from 0 to
ae23c9
+  255. Domains can be further classified into two types:
ae23c9
+
ae23c9
+    * Usage domains are domains that can be accessed directly to process AP
ae23c9
+      commands
ae23c9
+
ae23c9
+    * Control domains are domains that are accessed indirectly by AP
ae23c9
+      commands sent to a usage domain to control or change the domain; for
ae23c9
+      example, to set a secure private key for the domain.
ae23c9
+
ae23c9
+* AP Queue
ae23c9
+
ae23c9
+  An AP queue is the means by which an AP command-request message is sent to an
ae23c9
+  AP usage domain inside a specific AP. An AP queue is identified by a tuple
ae23c9
+  comprised of an AP adapter ID (APID) and an AP queue index (APQI). The
ae23c9
+  APQI corresponds to a given usage domain number within the adapter. This tuple
ae23c9
+  forms an AP Queue Number (APQN) uniquely identifying an AP queue. AP
ae23c9
+  instructions include a field containing the APQN to identify the AP queue to
ae23c9
+  which the AP command-request message is to be sent for processing.
ae23c9
+
ae23c9
+* AP Instructions:
ae23c9
+
ae23c9
+  There are three AP instructions:
ae23c9
+
ae23c9
+  * NQAP: to enqueue an AP command-request message to a queue
ae23c9
+  * DQAP: to dequeue an AP command-reply message from a queue
ae23c9
+  * PQAP: to administer the queues
ae23c9
+
ae23c9
+  AP instructions identify the domain that is targeted to process the AP
ae23c9
+  command; this must be one of the usage domains. An AP command may modify a
ae23c9
+  domain that is not one of the usage domains, but the modified domain
ae23c9
+  must be one of the control domains.
ae23c9
+
ae23c9
+Start Interpretive Execution (SIE) Instruction
ae23c9
+==============================================
ae23c9
+A KVM guest is started by executing the Start Interpretive Execution (SIE)
ae23c9
+instruction. The SIE state description is a control block that contains the
ae23c9
+state information for a KVM guest and is supplied as input to the SIE
ae23c9
+instruction. The SIE state description contains a satellite control block called
ae23c9
+the Crypto Control Block (CRYCB). The CRYCB contains three fields to identify
ae23c9
+the adapters, usage domains and control domains assigned to the KVM guest:
ae23c9
+
ae23c9
+* The AP Mask (APM) field is a bit mask that identifies the AP adapters assigned
ae23c9
+  to the KVM guest. Each bit in the mask, from left to right, corresponds to
ae23c9
+  an APID from 0-255. If a bit is set, the corresponding adapter is valid for
ae23c9
+  use by the KVM guest.
ae23c9
+
ae23c9
+* The AP Queue Mask (AQM) field is a bit mask identifying the AP usage domains
ae23c9
+  assigned to the KVM guest. Each bit in the mask, from left to right,
ae23c9
+  corresponds to  an AP queue index (APQI) from 0-255. If a bit is set, the
ae23c9
+  corresponding queue is valid for use by the KVM guest.
ae23c9
+
ae23c9
+* The AP Domain Mask field is a bit mask that identifies the AP control domains
ae23c9
+  assigned to the KVM guest. The ADM bit mask controls which domains can be
ae23c9
+  changed by an AP command-request message sent to a usage domain from the
ae23c9
+  guest. Each bit in the mask, from left to right, corresponds to a domain from
ae23c9
+  0-255. If a bit is set, the corresponding domain can be modified by an AP
ae23c9
+  command-request message sent to a usage domain.
ae23c9
+
ae23c9
+If you recall from the description of an AP Queue, AP instructions include
ae23c9
+an APQN to identify the AP adapter and AP queue to which an AP command-request
ae23c9
+message is to be sent (NQAP and PQAP instructions), or from which a
ae23c9
+command-reply message is to be received (DQAP instruction). The validity of an
ae23c9
+APQN is defined by the matrix calculated from the APM and AQM; it is the
ae23c9
+cross product of all assigned adapter numbers (APM) with all assigned queue
ae23c9
+indexes (AQM). For example, if adapters 1 and 2 and usage domains 5 and 6 are
ae23c9
+assigned to a guest, the APQNs (1,5), (1,6), (2,5) and (2,6) will be valid for
ae23c9
+the guest.
ae23c9
+
ae23c9
+The APQNs can provide secure key functionality - i.e., a private key is stored
ae23c9
+on the adapter card for each of its domains - so each APQN must be assigned to
ae23c9
+at most one guest or the linux host.
ae23c9
+
ae23c9
+   Example 1: Valid configuration:
ae23c9
+   ------------------------------
ae23c9
+   Guest1: adapters 1,2  domains 5,6
ae23c9
+   Guest2: adapter  1,2  domain 7
ae23c9
+
ae23c9
+   This is valid because both guests have a unique set of APQNs: Guest1 has
ae23c9
+   APQNs (1,5), (1,6), (2,5) and (2,6); Guest2 has APQNs (1,7) and (2,7).
ae23c9
+
ae23c9
+   Example 2: Valid configuration:
ae23c9
+   ------------------------------
ae23c9
+   Guest1: adapters 1,2 domains 5,6
ae23c9
+   Guest2: adapters 3,4 domains 5,6
ae23c9
+
ae23c9
+   This is also valid because both guests have a unique set of APQNs:
ae23c9
+      Guest1 has APQNs (1,5), (1,6), (2,5), (2,6);
ae23c9
+      Guest2 has APQNs (3,5), (3,6), (4,5), (4,6)
ae23c9
+
ae23c9
+   Example 3: Invalid configuration:
ae23c9
+   --------------------------------
ae23c9
+   Guest1: adapters 1,2  domains 5,6
ae23c9
+   Guest2: adapter  1    domains 6,7
ae23c9
+
ae23c9
+   This is an invalid configuration because both guests have access to
ae23c9
+   APQN (1,6).
ae23c9
+
ae23c9
+AP Matrix Configuration on Linux Host:
ae23c9
+=====================================
ae23c9
+A linux system is a guest of the LPAR in which it is running and has access to
ae23c9
+the AP resources configured for the LPAR. The LPAR's AP matrix is
ae23c9
+configured via its Activation Profile which can be edited on the HMC. When the
ae23c9
+linux system is started, the AP bus will detect the AP devices assigned to the
ae23c9
+LPAR and create the following in sysfs:
ae23c9
+
ae23c9
+/sys/bus/ap
ae23c9
+... [devices]
ae23c9
+...... xx.yyyy
ae23c9
+...... ...
ae23c9
+...... cardxx
ae23c9
+...... ...
ae23c9
+
ae23c9
+Where:
ae23c9
+    cardxx     is AP adapter number xx (in hex)
ae23c9
+....xx.yyyy    is an APQN with xx specifying the APID and yyyy specifying the
ae23c9
+               APQI
ae23c9
+
ae23c9
+For example, if AP adapters 5 and 6 and domains 4, 71 (0x47), 171 (0xab) and
ae23c9
+255 (0xff) are configured for the LPAR, the sysfs representation on the linux
ae23c9
+host system would look like this:
ae23c9
+
ae23c9
+/sys/bus/ap
ae23c9
+... [devices]
ae23c9
+...... 05.0004
ae23c9
+...... 05.0047
ae23c9
+...... 05.00ab
ae23c9
+...... 05.00ff
ae23c9
+...... 06.0004
ae23c9
+...... 06.0047
ae23c9
+...... 06.00ab
ae23c9
+...... 06.00ff
ae23c9
+...... card05
ae23c9
+...... card06
ae23c9
+
ae23c9
+A set of default device drivers are also created to control each type of AP
ae23c9
+device that can be assigned to the LPAR on which a linux host is running:
ae23c9
+
ae23c9
+/sys/bus/ap
ae23c9
+... [drivers]
ae23c9
+...... [cex2acard]        for Crypto Express 2/3 accelerator cards
ae23c9
+...... [cex2aqueue]       for AP queues served by Crypto Express 2/3
ae23c9
+                          accelerator cards
ae23c9
+...... [cex4card]         for Crypto Express 4/5/6 accelerator and coprocessor
ae23c9
+                          cards
ae23c9
+...... [cex4queue]        for AP queues served by Crypto Express 4/5/6
ae23c9
+                          accelerator and coprocessor cards
ae23c9
+...... [pcixcccard]       for Crypto Express 2/3 coprocessor cards
ae23c9
+...... [pcixccqueue]      for AP queues served by Crypto Express 2/3
ae23c9
+                          coprocessor cards
ae23c9
+
ae23c9
+Binding AP devices to device drivers
ae23c9
+------------------------------------
ae23c9
+There are two sysfs files that specify bitmasks marking a subset of the APQN
ae23c9
+range as 'usable by the default AP queue device drivers' or 'not usable by the
ae23c9
+default device drivers' and thus available for use by the alternate device
ae23c9
+driver(s). The sysfs locations of the masks are:
ae23c9
+
ae23c9
+   /sys/bus/ap/apmask
ae23c9
+   /sys/bus/ap/aqmask
ae23c9
+
ae23c9
+   The 'apmask' is a 256-bit mask that identifies a set of AP adapter IDs
ae23c9
+   (APID). Each bit in the mask, from left to right (i.e., from most significant
ae23c9
+   to least significant bit in big endian order), corresponds to an APID from
ae23c9
+   0-255. If a bit is set, the APID is marked as usable only by the default AP
ae23c9
+   queue device drivers; otherwise, the APID is usable by the vfio_ap
ae23c9
+   device driver.
ae23c9
+
ae23c9
+   The 'aqmask' is a 256-bit mask that identifies a set of AP queue indexes
ae23c9
+   (APQI). Each bit in the mask, from left to right (i.e., from most significant
ae23c9
+   to least significant bit in big endian order), corresponds to an APQI from
ae23c9
+   0-255. If a bit is set, the APQI is marked as usable only by the default AP
ae23c9
+   queue device drivers; otherwise, the APQI is usable by the vfio_ap device
ae23c9
+   driver.
ae23c9
+
ae23c9
+   Take, for example, the following mask:
ae23c9
+
ae23c9
+      0x7dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ae23c9
+
ae23c9
+    It indicates:
ae23c9
+
ae23c9
+      1, 2, 3, 4, 5, and 7-255 belong to the default drivers' pool, and 0 and 6
ae23c9
+      belong to the vfio_ap device driver's pool.
ae23c9
+
ae23c9
+   The APQN of each AP queue device assigned to the linux host is checked by the
ae23c9
+   AP bus against the set of APQNs derived from the cross product of APIDs
ae23c9
+   and APQIs marked as usable only by the default AP queue device drivers. If a
ae23c9
+   match is detected,  only the default AP queue device drivers will be probed;
ae23c9
+   otherwise, the vfio_ap device driver will be probed.
ae23c9
+
ae23c9
+   By default, the two masks are set to reserve all APQNs for use by the default
ae23c9
+   AP queue device drivers. There are two ways the default masks can be changed:
ae23c9
+
ae23c9
+   1. The sysfs mask files can be edited by echoing a string into the
ae23c9
+      respective sysfs mask file in one of two formats:
ae23c9
+
ae23c9
+      * An absolute hex string starting with 0x - like "0x12345678" - sets
ae23c9
+        the mask. If the given string is shorter than the mask, it is padded
ae23c9
+        with 0s on the right; for example, specifying a mask value of 0x41 is
ae23c9
+        the same as specifying:
ae23c9
+
ae23c9
+           0x4100000000000000000000000000000000000000000000000000000000000000
ae23c9
+
ae23c9
+        Keep in mind that the mask reads from left to right (i.e., most
ae23c9
+        significant to least significant bit in big endian order), so the mask
ae23c9
+        above identifies device numbers 1 and 7 (01000001).
ae23c9
+
ae23c9
+        If the string is longer than the mask, the operation is terminated with
ae23c9
+        an error (EINVAL).
ae23c9
+
ae23c9
+      * Individual bits in the mask can be switched on and off by specifying
ae23c9
+        each bit number to be switched in a comma separated list. Each bit
ae23c9
+        number string must be prepended with a ('+') or minus ('-') to indicate
ae23c9
+        the corresponding bit is to be switched on ('+') or off ('-'). Some
ae23c9
+        valid values are:
ae23c9
+
ae23c9
+           "+0"    switches bit 0 on
ae23c9
+           "-13"   switches bit 13 off
ae23c9
+           "+0x41" switches bit 65 on
ae23c9
+           "-0xff" switches bit 255 off
ae23c9
+
ae23c9
+           The following example:
ae23c9
+              +0,-6,+0x47,-0xf0
ae23c9
+
ae23c9
+              Switches bits 0 and 71 (0x47) on
ae23c9
+              Switches bits 6 and 240 (0xf0) off
ae23c9
+
ae23c9
+        Note that the bits not specified in the list remain as they were before
ae23c9
+        the operation.
ae23c9
+
ae23c9
+   2. The masks can also be changed at boot time via parameters on the kernel
ae23c9
+      command line like this:
ae23c9
+
ae23c9
+         ap.apmask=0xffff ap.aqmask=0x40
ae23c9
+
ae23c9
+         This would create the following masks:
ae23c9
+
ae23c9
+            apmask:
ae23c9
+            0xffff000000000000000000000000000000000000000000000000000000000000
ae23c9
+
ae23c9
+            aqmask:
ae23c9
+            0x4000000000000000000000000000000000000000000000000000000000000000
ae23c9
+
ae23c9
+         Resulting in these two pools:
ae23c9
+
ae23c9
+            default drivers pool:    adapter 0-15, domain 1
ae23c9
+            alternate drivers pool:  adapter 16-255, domains 0, 2-255
ae23c9
+
ae23c9
+Configuring an AP matrix for a linux guest.
ae23c9
+------------------------------------------
ae23c9
+The sysfs interfaces for configuring an AP matrix for a guest are built on the
ae23c9
+VFIO mediated device framework. To configure an AP matrix for a guest, a
ae23c9
+mediated matrix device must first be created for the /sys/devices/vfio_ap/matrix
ae23c9
+device. When the vfio_ap device driver is loaded, it registers with the VFIO
ae23c9
+mediated device framework. When the driver registers, the sysfs interfaces for
ae23c9
+creating mediated matrix devices is created:
ae23c9
+
ae23c9
+/sys/devices
ae23c9
+... [vfio_ap]
ae23c9
+......[matrix]
ae23c9
+......... [mdev_supported_types]
ae23c9
+............ [vfio_ap-passthrough]
ae23c9
+............... create
ae23c9
+............... [devices]
ae23c9
+
ae23c9
+A mediated AP matrix device is created by writing a UUID to the attribute file
ae23c9
+named 'create', for example:
ae23c9
+
ae23c9
+   uuidgen > create
ae23c9
+
ae23c9
+   or
ae23c9
+
ae23c9
+   echo $uuid > create
ae23c9
+
ae23c9
+When a mediated AP matrix device is created, a sysfs directory named after
ae23c9
+the UUID is created in the 'devices' subdirectory:
ae23c9
+
ae23c9
+/sys/devices
ae23c9
+... [vfio_ap]
ae23c9
+......[matrix]
ae23c9
+......... [mdev_supported_types]
ae23c9
+............ [vfio_ap-passthrough]
ae23c9
+............... create
ae23c9
+............... [devices]
ae23c9
+.................. [$uuid]
ae23c9
+
ae23c9
+There will also be three sets of attribute files created in the mediated
ae23c9
+matrix device's sysfs directory to configure an AP matrix for the
ae23c9
+KVM guest:
ae23c9
+
ae23c9
+/sys/devices
ae23c9
+... [vfio_ap]
ae23c9
+......[matrix]
ae23c9
+......... [mdev_supported_types]
ae23c9
+............ [vfio_ap-passthrough]
ae23c9
+............... create
ae23c9
+............... [devices]
ae23c9
+.................. [$uuid]
ae23c9
+..................... assign_adapter
ae23c9
+..................... assign_control_domain
ae23c9
+..................... assign_domain
ae23c9
+..................... matrix
ae23c9
+..................... unassign_adapter
ae23c9
+..................... unassign_control_domain
ae23c9
+..................... unassign_domain
ae23c9
+
ae23c9
+assign_adapter
ae23c9
+   To assign an AP adapter to the mediated matrix device, its APID is written
ae23c9
+   to the 'assign_adapter' file. This may be done multiple times to assign more
ae23c9
+   than one adapter. The APID may be specified using conventional semantics
ae23c9
+   as a decimal, hexadecimal, or octal number. For example, to assign adapters
ae23c9
+   4, 5 and 16 to a mediated matrix device in decimal, hexadecimal and octal
ae23c9
+   respectively:
ae23c9
+
ae23c9
+       echo 4 > assign_adapter
ae23c9
+       echo 0x5 > assign_adapter
ae23c9
+       echo 020 > assign_adapter
ae23c9
+
ae23c9
+   In order to successfully assign an adapter:
ae23c9
+
ae23c9
+   * The adapter number specified must represent a value from 0 up to the
ae23c9
+     maximum adapter number allowed by the machine model. If an adapter number
ae23c9
+     higher than the maximum is specified, the operation will terminate with
ae23c9
+     an error (ENODEV).
ae23c9
+
ae23c9
+   * All APQNs that can be derived from the adapter ID being assigned and the
ae23c9
+     IDs of the previously assigned domains must be bound to the vfio_ap device
ae23c9
+     driver. If no domains have yet been assigned, then there must be at least
ae23c9
+     one APQN with the specified APID bound to the vfio_ap driver. If no such
ae23c9
+     APQNs are bound to the driver, the operation will terminate with an
ae23c9
+     error (EADDRNOTAVAIL).
ae23c9
+
ae23c9
+     No APQN that can be derived from the adapter ID and the IDs of the
ae23c9
+     previously assigned domains can be assigned to another mediated matrix
ae23c9
+     device. If an APQN is assigned to another mediated matrix device, the
ae23c9
+     operation will terminate with an error (EADDRINUSE).
ae23c9
+
ae23c9
+unassign_adapter
ae23c9
+   To unassign an AP adapter, its APID is written to the 'unassign_adapter'
ae23c9
+   file. This may also be done multiple times to unassign more than one adapter.
ae23c9
+
ae23c9
+assign_domain
ae23c9
+   To assign a usage domain, the domain number is written into the
ae23c9
+   'assign_domain' file. This may be done multiple times to assign more than one
ae23c9
+   usage domain. The domain number is specified using conventional semantics as
ae23c9
+   a decimal, hexadecimal, or octal number. For example, to assign usage domains
ae23c9
+   4, 8, and 71 to a mediated matrix device in decimal, hexadecimal and octal
ae23c9
+   respectively:
ae23c9
+
ae23c9
+      echo 4 > assign_domain
ae23c9
+      echo 0x8 > assign_domain
ae23c9
+      echo 0107 > assign_domain
ae23c9
+
ae23c9
+   In order to successfully assign a domain:
ae23c9
+
ae23c9
+   * The domain number specified must represent a value from 0 up to the
ae23c9
+     maximum domain number allowed by the machine model. If a domain number
ae23c9
+     higher than the maximum is specified, the operation will terminate with
ae23c9
+     an error (ENODEV).
ae23c9
+
ae23c9
+   * All APQNs that can be derived from the domain ID being assigned and the IDs
ae23c9
+     of the previously assigned adapters must be bound to the vfio_ap device
ae23c9
+     driver. If no domains have yet been assigned, then there must be at least
ae23c9
+     one APQN with the specified APQI bound to the vfio_ap driver. If no such
ae23c9
+     APQNs are bound to the driver, the operation will terminate with an
ae23c9
+     error (EADDRNOTAVAIL).
ae23c9
+
ae23c9
+     No APQN that can be derived from the domain ID being assigned and the IDs
ae23c9
+     of the previously assigned adapters can be assigned to another mediated
ae23c9
+     matrix device. If an APQN is assigned to another mediated matrix device,
ae23c9
+     the operation will terminate with an error (EADDRINUSE).
ae23c9
+
ae23c9
+unassign_domain
ae23c9
+   To unassign a usage domain, the domain number is written into the
ae23c9
+   'unassign_domain' file. This may be done multiple times to unassign more than
ae23c9
+   one usage domain.
ae23c9
+
ae23c9
+assign_control_domain
ae23c9
+   To assign a control domain, the domain number is written into the
ae23c9
+   'assign_control_domain' file. This may be done multiple times to
ae23c9
+   assign more than one control domain. The domain number may be specified using
ae23c9
+   conventional semantics as a decimal, hexadecimal, or octal number. For
ae23c9
+   example, to assign  control domains 4, 8, and 71 to  a mediated matrix device
ae23c9
+   in decimal, hexadecimal and octal respectively:
ae23c9
+
ae23c9
+      echo 4 > assign_domain
ae23c9
+      echo 0x8 > assign_domain
ae23c9
+      echo 0107 > assign_domain
ae23c9
+
ae23c9
+   In order to successfully assign a control domain, the domain number
ae23c9
+   specified must represent a value from 0 up to the maximum domain number
ae23c9
+   allowed by the machine model. If a control domain number higher than the
ae23c9
+   maximum is specified, the operation will terminate with an error (ENODEV).
ae23c9
+
ae23c9
+unassign_control_domain
ae23c9
+   To unassign a control domain, the domain number is written into the
ae23c9
+   'unassign_domain' file. This may be done multiple times to unassign more than
ae23c9
+   one control domain.
ae23c9
+
ae23c9
+Notes: Hot plug/unplug is not currently supported for mediated AP matrix
ae23c9
+devices, so no changes to the AP matrix will be allowed while a guest using
ae23c9
+the mediated matrix device is running. Attempts to assign an adapter,
ae23c9
+domain or control domain will be rejected and an error (EBUSY) returned.
ae23c9
+
ae23c9
+Starting a Linux Guest Configured with an AP Matrix:
ae23c9
+===================================================
ae23c9
+To provide a mediated matrix device for use by a guest, the following option
ae23c9
+must be specified on the QEMU command line:
ae23c9
+
ae23c9
+   -device vfio_ap,sysfsdev=$path-to-mdev
ae23c9
+
ae23c9
+The sysfsdev parameter specifies the path to the mediated matrix device.
ae23c9
+There are a number of ways to specify this path:
ae23c9
+
ae23c9
+/sys/devices/vfio_ap/matrix/$uuid
ae23c9
+/sys/bus/mdev/devices/$uuid
ae23c9
+/sys/bus/mdev/drivers/vfio_mdev/$uuid
ae23c9
+/sys/devices/vfio_ap/matrix/mdev_supported_types/vfio_ap-passthrough/devices/$uuid
ae23c9
+
ae23c9
+When the linux guest is started, the guest will open the mediated
ae23c9
+matrix device's file descriptor to get information about the mediated matrix
ae23c9
+device. The vfio_ap device driver will update the APM, AQM, and ADM fields in
ae23c9
+the guest's CRYCB with the adapter, usage domain and control domains assigned
ae23c9
+via the mediated matrix device's sysfs attribute files. Programs running on the
ae23c9
+linux guest will then:
ae23c9
+
ae23c9
+1. Have direct access to the APQNs derived from the cross product of the AP
ae23c9
+   adapter numbers (APID) and queue indexes (APQI) specified in the APM and AQM
ae23c9
+   fields of the guests's CRYCB respectively. These APQNs identify the AP queues
ae23c9
+   that are valid for use by the guest; meaning, AP commands can be sent by the
ae23c9
+   guest to any of these queues for processing.
ae23c9
+
ae23c9
+2. Have authorization to process AP commands to change a control domain
ae23c9
+   identified in the ADM field of the guest's CRYCB. The AP command must be sent
ae23c9
+   to a valid APQN (see 1 above).
ae23c9
+
ae23c9
+CPU model features:
ae23c9
+
ae23c9
+Three CPU model features are available for controlling guest access to AP
ae23c9
+facilities:
ae23c9
+
ae23c9
+1. AP facilities feature
ae23c9
+
ae23c9
+   The AP facilities feature indicates that AP facilities are installed on the
ae23c9
+   guest. This feature will be exposed for use only if the AP facilities
ae23c9
+   are installed on the host system. The feature is s390-specific and is
ae23c9
+   represented as a parameter of the -cpu option on the QEMU command line:
ae23c9
+
ae23c9
+      qemu-system-s390x -cpu $model,ap=on|off
ae23c9
+
ae23c9
+      Where:
ae23c9
+
ae23c9
+         $model is the CPU model defined for the guest (defaults to the model of
ae23c9
+                the host system if not specified).
ae23c9
+
ae23c9
+         ap=on|off indicates whether AP facilities are installed (on) or not
ae23c9
+                   (off). The default for CPU models zEC12 or newer
ae23c9
+                   is ap=on. AP facilities must be installed on the guest if a
ae23c9
+                   vfio-ap device (-device vfio-ap,sysfsdev=$path) is configured
ae23c9
+                   for the guest, or the guest will fail to start.
ae23c9
+
ae23c9
+2. Query Configuration Information (QCI) facility
ae23c9
+
ae23c9
+   The QCI facility is used by the AP bus running on the guest to query the
ae23c9
+   configuration of the AP facilities. This facility will be available
ae23c9
+   only if the QCI facility is installed on the host system. The feature is
ae23c9
+   s390-specific and is represented as a parameter of the -cpu option on the
ae23c9
+   QEMU command line:
ae23c9
+
ae23c9
+      qemu-system-s390x -cpu $model,apqci=on|off
ae23c9
+
ae23c9
+      Where:
ae23c9
+
ae23c9
+         $model is the CPU model defined for the guest
ae23c9
+
ae23c9
+         apqci=on|off indicates whether the QCI facility is installed (on) or
ae23c9
+                      not (off). The default for CPU models zEC12 or newer
ae23c9
+                      is apqci=on; for older models, QCI will not be installed.
ae23c9
+
ae23c9
+                      If QCI is installed (apqci=on) but AP facilities are not
ae23c9
+                      (ap=off), an error message will be logged, but the guest
ae23c9
+                      will be allowed to start. It makes no sense to have QCI
ae23c9
+                      installed if the AP facilities are not; this is considered
ae23c9
+                      an invalid configuration.
ae23c9
+
ae23c9
+                      If the QCI facility is not installed, APQNs with an APQI
ae23c9
+                      greater than 15 will not be detected by the AP bus
ae23c9
+                      running on the guest.
ae23c9
+
ae23c9
+3. Adjunct Process Facility Test (APFT) facility
ae23c9
+
ae23c9
+   The APFT facility is used by the AP bus running on the guest to test the
ae23c9
+   AP facilities available for a given AP queue. This facility will be available
ae23c9
+   only if the APFT facility is installed on the host system. The feature is
ae23c9
+   s390-specific and is represented as a parameter of the -cpu option on the
ae23c9
+   QEMU command line:
ae23c9
+
ae23c9
+      qemu-system-s390x -cpu $model,apft=on|off
ae23c9
+
ae23c9
+      Where:
ae23c9
+
ae23c9
+         $model is the CPU model defined for the guest (defaults to the model of
ae23c9
+                the host system if not specified).
ae23c9
+
ae23c9
+         apft=on|off indicates whether the APFT facility is installed (on) or
ae23c9
+                     not (off). The default for CPU models zEC12 and
ae23c9
+                     newer is apft=on for older models, APFT will not be
ae23c9
+                     installed.
ae23c9
+
ae23c9
+                     If APFT is installed (apft=on) but AP facilities are not
ae23c9
+                     (ap=off), an error message will be logged, but the guest
ae23c9
+                     will be allowed to start. It makes no sense to have APFT
ae23c9
+                     installed if the AP facilities are not; this is considered
ae23c9
+                     an invalid configuration.
ae23c9
+
ae23c9
+                     It also makes no sense to turn APFT off because the AP bus
ae23c9
+                     running on the guest will not detect CEX4 and newer devices
ae23c9
+                     without it. Since only CEX4 and newer devices are supported
ae23c9
+                     for guest usage, no AP devices can be made accessible to a
ae23c9
+                     guest started without APFT installed.
ae23c9
+
ae23c9
+Example: Configure AP Matrixes for Three Linux Guests:
ae23c9
+=====================================================
ae23c9
+Let's now provide an example to illustrate how KVM guests may be given
ae23c9
+access to AP facilities. For this example, we will show how to configure
ae23c9
+three guests such that executing the lszcrypt command on the guests would
ae23c9
+look like this:
ae23c9
+
ae23c9
+Guest1
ae23c9
+------
ae23c9
+CARD.DOMAIN TYPE  MODE
ae23c9
+------------------------------
ae23c9
+05          CEX5C CCA-Coproc
ae23c9
+05.0004     CEX5C CCA-Coproc
ae23c9
+05.00ab     CEX5C CCA-Coproc
ae23c9
+06          CEX5A Accelerator
ae23c9
+06.0004     CEX5A Accelerator
ae23c9
+06.00ab     CEX5C CCA-Coproc
ae23c9
+
ae23c9
+Guest2
ae23c9
+------
ae23c9
+CARD.DOMAIN TYPE  MODE
ae23c9
+------------------------------
ae23c9
+05          CEX5A Accelerator
ae23c9
+05.0047     CEX5A Accelerator
ae23c9
+05.00ff     CEX5A Accelerator (5,4), (5,171), (6,4), (6,171),
ae23c9
+
ae23c9
+Guest3
ae23c9
+------
ae23c9
+CARD.DOMAIN TYPE  MODE
ae23c9
+------------------------------
ae23c9
+06          CEX5A Accelerator
ae23c9
+06.0047     CEX5A Accelerator
ae23c9
+06.00ff     CEX5A Accelerator
ae23c9
+
ae23c9
+These are the steps:
ae23c9
+
ae23c9
+1. Install the vfio_ap module on the linux host. The dependency chain for the
ae23c9
+   vfio_ap module is:
ae23c9
+   * iommu
ae23c9
+   * s390
ae23c9
+   * zcrypt
ae23c9
+   * vfio
ae23c9
+   * vfio_mdev
ae23c9
+   * vfio_mdev_device
ae23c9
+   * KVM
ae23c9
+
ae23c9
+   To build the vfio_ap module, the kernel build must be configured with the
ae23c9
+   following Kconfig elements selected:
ae23c9
+   * IOMMU_SUPPORT
ae23c9
+   * S390
ae23c9
+   * ZCRYPT
ae23c9
+   * S390_AP_IOMMU
ae23c9
+   * VFIO
ae23c9
+   * VFIO_MDEV
ae23c9
+   * VFIO_MDEV_DEVICE
ae23c9
+   * KVM
ae23c9
+
ae23c9
+   If using make menuconfig select the following to build the vfio_ap module:
ae23c9
+   -> Device Drivers
ae23c9
+      -> IOMMU Hardware Support
ae23c9
+         select S390 AP IOMMU Support
ae23c9
+      -> VFIO Non-Privileged userspace driver framework
ae23c9
+         -> Mediated device driver frramework
ae23c9
+            -> VFIO driver for Mediated devices
ae23c9
+   -> I/O subsystem
ae23c9
+      -> VFIO support for AP devices
ae23c9
+
ae23c9
+2. Secure the AP queues to be used by the three guests so that the host can not
ae23c9
+   access them. To secure the AP queues 05.0004, 05.0047, 05.00ab, 05.00ff,
ae23c9
+   06.0004, 06.0047, 06.00ab, and 06.00ff for use by the vfio_ap device driver,
ae23c9
+   the corresponding APQNs must be removed from the default queue drivers pool
ae23c9
+   as follows:
ae23c9
+
ae23c9
+      echo -5,-6 > /sys/bus/ap/apmask
ae23c9
+
ae23c9
+      echo -4,-0x47,-0xab,-0xff > /sys/bus/ap/aqmask
ae23c9
+
ae23c9
+   This will result in AP queues 05.0004, 05.0047, 05.00ab, 05.00ff, 06.0004,
ae23c9
+   06.0047, 06.00ab, and 06.00ff getting bound to the vfio_ap device driver. The
ae23c9
+   sysfs directory for the vfio_ap device driver will now contain symbolic links
ae23c9
+   to the AP queue devices bound to it:
ae23c9
+
ae23c9
+   /sys/bus/ap
ae23c9
+   ... [drivers]
ae23c9
+   ...... [vfio_ap]
ae23c9
+   ......... [05.0004]
ae23c9
+   ......... [05.0047]
ae23c9
+   ......... [05.00ab]
ae23c9
+   ......... [05.00ff]
ae23c9
+   ......... [06.0004]
ae23c9
+   ......... [06.0047]
ae23c9
+   ......... [06.00ab]
ae23c9
+   ......... [06.00ff]
ae23c9
+
ae23c9
+   Keep in mind that only type 10 and newer adapters (i.e., CEX4 and later)
ae23c9
+   can be bound to the vfio_ap device driver. The reason for this is to
ae23c9
+   simplify the implementation by not needlessly complicating the design by
ae23c9
+   supporting older devices that will go out of service in the relatively near
ae23c9
+   future, and for which there are few older systems on which to test.
ae23c9
+
ae23c9
+   The administrator, therefore, must take care to secure only AP queues that
ae23c9
+   can be bound to the vfio_ap device driver. The device type for a given AP
ae23c9
+   queue device can be read from the parent card's sysfs directory. For example,
ae23c9
+   to see the hardware type of the queue 05.0004:
ae23c9
+
ae23c9
+   cat /sys/bus/ap/devices/card05/hwtype
ae23c9
+
ae23c9
+   The hwtype must be 10 or higher (CEX4 or newer) in order to be bound to the
ae23c9
+   vfio_ap device driver.
ae23c9
+
ae23c9
+3. Create the mediated devices needed to configure the AP matrixes for the
ae23c9
+   three guests and to provide an interface to the vfio_ap driver for
ae23c9
+   use by the guests:
ae23c9
+
ae23c9
+   /sys/devices/vfio_ap/matrix/
ae23c9
+   --- [mdev_supported_types]
ae23c9
+   ------ [vfio_ap-passthrough] (passthrough mediated matrix device type)
ae23c9
+   --------- create
ae23c9
+   --------- [devices]
ae23c9
+
ae23c9
+   To create the mediated devices for the three guests:
ae23c9
+
ae23c9
+       uuidgen > create
ae23c9
+       uuidgen > create
ae23c9
+       uuidgen > create
ae23c9
+
ae23c9
+        or
ae23c9
+
ae23c9
+        echo $uuid1 > create
ae23c9
+        echo $uuid2 > create
ae23c9
+        echo $uuid3 > create
ae23c9
+
ae23c9
+   This will create three mediated devices in the [devices] subdirectory named
ae23c9
+   after the UUID used to create the mediated device. We'll call them $uuid1,
ae23c9
+   $uuid2 and $uuid3 and this is the sysfs directory structure after creation:
ae23c9
+
ae23c9
+   /sys/devices/vfio_ap/matrix/
ae23c9
+   --- [mdev_supported_types]
ae23c9
+   ------ [vfio_ap-passthrough]
ae23c9
+   --------- [devices]
ae23c9
+   ------------ [$uuid1]
ae23c9
+   --------------- assign_adapter
ae23c9
+   --------------- assign_control_domain
ae23c9
+   --------------- assign_domain
ae23c9
+   --------------- matrix
ae23c9
+   --------------- unassign_adapter
ae23c9
+   --------------- unassign_control_domain
ae23c9
+   --------------- unassign_domain
ae23c9
+
ae23c9
+   ------------ [$uuid2]
ae23c9
+   --------------- assign_adapter
ae23c9
+   --------------- assign_control_domain
ae23c9
+   --------------- assign_domain
ae23c9
+   --------------- matrix
ae23c9
+   --------------- unassign_adapter
ae23c9
+   ----------------unassign_control_domain
ae23c9
+   ----------------unassign_domain
ae23c9
+
ae23c9
+   ------------ [$uuid3]
ae23c9
+   --------------- assign_adapter
ae23c9
+   --------------- assign_control_domain
ae23c9
+   --------------- assign_domain
ae23c9
+   --------------- matrix
ae23c9
+   --------------- unassign_adapter
ae23c9
+   ----------------unassign_control_domain
ae23c9
+   ----------------unassign_domain
ae23c9
+
ae23c9
+4. The administrator now needs to configure the matrixes for the mediated
ae23c9
+   devices $uuid1 (for Guest1), $uuid2 (for Guest2) and $uuid3 (for Guest3).
ae23c9
+
ae23c9
+   This is how the matrix is configured for Guest1:
ae23c9
+
ae23c9
+      echo 5 > assign_adapter
ae23c9
+      echo 6 > assign_adapter
ae23c9
+      echo 4 > assign_domain
ae23c9
+      echo 0xab > assign_domain
ae23c9
+
ae23c9
+      Control domains can similarly be assigned using the assign_control_domain
ae23c9
+      sysfs file.
ae23c9
+
ae23c9
+      If a mistake is made configuring an adapter, domain or control domain,
ae23c9
+      you can use the unassign_xxx interfaces to unassign the adapter, domain or
ae23c9
+      control domain.
ae23c9
+
ae23c9
+      To display the matrix configuration for Guest1:
ae23c9
+
ae23c9
+         cat matrix
ae23c9
+
ae23c9
+         The output will display the APQNs in the format xx.yyyy, where xx is
ae23c9
+         the adapter number and yyyy is the domain number. The output for Guest1
ae23c9
+         will look like this:
ae23c9
+
ae23c9
+         05.0004
ae23c9
+         05.00ab
ae23c9
+         06.0004
ae23c9
+         06.00ab
ae23c9
+
ae23c9
+   This is how the matrix is configured for Guest2:
ae23c9
+
ae23c9
+      echo 5 > assign_adapter
ae23c9
+      echo 0x47 > assign_domain
ae23c9
+      echo 0xff > assign_domain
ae23c9
+
ae23c9
+   This is how the matrix is configured for Guest3:
ae23c9
+
ae23c9
+      echo 6 > assign_adapter
ae23c9
+      echo 0x47 > assign_domain
ae23c9
+      echo 0xff > assign_domain
ae23c9
+
ae23c9
+5. Start Guest1:
ae23c9
+
ae23c9
+   /usr/bin/qemu-system-s390x ... -cpu host,ap=on,apqci=on,apft=on \
ae23c9
+      -device vfio-ap,sysfsdev=/sys/devices/vfio_ap/matrix/$uuid1 ...
ae23c9
+
ae23c9
+7. Start Guest2:
ae23c9
+
ae23c9
+   /usr/bin/qemu-system-s390x ... -cpu host,ap=on,apqci=on,apft=on \
ae23c9
+      -device vfio-ap,sysfsdev=/sys/devices/vfio_ap/matrix/$uuid2 ...
ae23c9
+
ae23c9
+7. Start Guest3:
ae23c9
+
ae23c9
+   /usr/bin/qemu-system-s390x ... -cpu host,ap=on,apqci=on,apft=on \
ae23c9
+      -device vfio-ap,sysfsdev=/sys/devices/vfio_ap/matrix/$uuid3 ...
ae23c9
+
ae23c9
+When the guest is shut down, the mediated matrix devices may be removed.
ae23c9
+
ae23c9
+Using our example again, to remove the mediated matrix device $uuid1:
ae23c9
+
ae23c9
+   /sys/devices/vfio_ap/matrix/
ae23c9
+      --- [mdev_supported_types]
ae23c9
+      ------ [vfio_ap-passthrough]
ae23c9
+      --------- [devices]
ae23c9
+      ------------ [$uuid1]
ae23c9
+      --------------- remove
ae23c9
+
ae23c9
+
ae23c9
+   echo 1 > remove
ae23c9
+
ae23c9
+   This will remove all of the mdev matrix device's sysfs structures including
ae23c9
+   the mdev device itself. To recreate and reconfigure the mdev matrix device,
ae23c9
+   all of the steps starting with step 3 will have to be performed again. Note
ae23c9
+   that the remove will fail if a guest using the mdev is still running.
ae23c9
+
ae23c9
+   It is not necessary to remove an mdev matrix device, but one may want to
ae23c9
+   remove it if no guest will use it during the remaining lifetime of the linux
ae23c9
+   host. If the mdev matrix device is removed, one may want to also reconfigure
ae23c9
+   the pool of adapters and queues reserved for use by the default drivers.
ae23c9
+
ae23c9
+Limitations
ae23c9
+===========
ae23c9
+* The KVM/kernel interfaces do not provide a way to prevent restoring an APQN
ae23c9
+  to the default drivers pool of a queue that is still assigned to a mediated
ae23c9
+  device in use by a guest. It is incumbent upon the administrator to
ae23c9
+  ensure there is no mediated device in use by a guest to which the APQN is
ae23c9
+  assigned lest the host be given access to the private data of the AP queue
ae23c9
+  device, such as a private key configured specifically for the guest.
ae23c9
+
ae23c9
+* Dynamically modifying the AP matrix for a running guest (which would amount to
ae23c9
+  hot(un)plug of AP devices for the guest) is currently not supported
ae23c9
+
ae23c9
+* Live guest migration is not supported for guests using AP devices.
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9