vfio-ap.rst 43 KB

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