xref: /linux/drivers/s390/crypto/vfio_ap_private.h (revision 8fb456bc9f9b136d5369f8fdd54597edd692d36d)
11fde5734STony Krowiak /* SPDX-License-Identifier: GPL-2.0 */
21fde5734STony Krowiak /*
31fde5734STony Krowiak  * Private data and functions for adjunct processor VFIO matrix driver.
41fde5734STony Krowiak  *
51fde5734STony Krowiak  * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
665f06713STony Krowiak  *	      Halil Pasic <pasic@linux.ibm.com>
7ec89b55eSPierre Morel  *	      Pierre Morel <pmorel@linux.ibm.com>
81fde5734STony Krowiak  *
91fde5734STony Krowiak  * Copyright IBM Corp. 2018
101fde5734STony Krowiak  */
111fde5734STony Krowiak 
121fde5734STony Krowiak #ifndef _VFIO_AP_PRIVATE_H_
131fde5734STony Krowiak #define _VFIO_AP_PRIVATE_H_
141fde5734STony Krowiak 
151fde5734STony Krowiak #include <linux/types.h>
161fde5734STony Krowiak #include <linux/mdev.h>
171fde5734STony Krowiak #include <linux/delay.h>
18bf48961fSTony Krowiak #include <linux/eventfd.h>
191fde5734STony Krowiak #include <linux/mutex.h>
20e5282de9SPierre Morel #include <linux/kvm_host.h>
21eb0feefdSJason Gunthorpe #include <linux/vfio.h>
2211cb2419STony Krowiak #include <linux/hashtable.h>
231fde5734STony Krowiak 
241fde5734STony Krowiak #include "ap_bus.h"
251fde5734STony Krowiak 
261fde5734STony Krowiak #define VFIO_AP_MODULE_NAME "vfio_ap"
271fde5734STony Krowiak #define VFIO_AP_DRV_NAME "vfio_ap"
281fde5734STony Krowiak 
291fde5734STony Krowiak /**
305ef4f710STony Krowiak  * struct ap_matrix_dev - Contains the data for the matrix device.
315ef4f710STony Krowiak  *
321fde5734STony Krowiak  * @device:	generic device structure associated with the AP matrix device
3365f06713STony Krowiak  * @info:	the struct containing the output from the PQAP(QCI) instruction
345ef4f710STony Krowiak  * @mdev_list:	the list of mediated matrix devices created
35d0786556STony Krowiak  * @mdevs_lock: mutex for locking the AP matrix device. This lock will be
3665f06713STony Krowiak  *		taken every time we fiddle with state managed by the vfio_ap
3765f06713STony Krowiak  *		driver, be it using @mdev_list or writing the state of a
3865f06713STony Krowiak  *		single ap_matrix_mdev device. It's quite coarse but we don't
3965f06713STony Krowiak  *		expect much contention.
405ef4f710STony Krowiak  * @vfio_ap_drv: the vfio_ap device driver
4121195eb0STony Krowiak  * @guests_lock: mutex for controlling access to a guest that is using AP
4221195eb0STony Krowiak  *		 devices passed through by the vfio_ap device driver. This lock
4321195eb0STony Krowiak  *		 will be taken when the AP devices are plugged into or unplugged
4421195eb0STony Krowiak  *		 from a guest, and when an ap_matrix_mdev device is added to or
4521195eb0STony Krowiak  *		 removed from @mdev_list or the list is iterated.
461fde5734STony Krowiak  */
471fde5734STony Krowiak struct ap_matrix_dev {
481fde5734STony Krowiak 	struct device device;
4965f06713STony Krowiak 	struct ap_config_info info;
5065f06713STony Krowiak 	struct list_head mdev_list;
51d0786556STony Krowiak 	struct mutex mdevs_lock; /* serializes access to each ap_matrix_mdev */
5236360658SPierre Morel 	struct ap_driver  *vfio_ap_drv;
5321195eb0STony Krowiak 	struct mutex guests_lock; /* serializes access to each KVM guest */
5489345d51SChristoph Hellwig 	struct mdev_parent parent;
55da44c340SChristoph Hellwig 	struct mdev_type mdev_type;
56e38de480SJason J. Herne 	struct mdev_type *mdev_types[1];
571fde5734STony Krowiak };
581fde5734STony Krowiak 
591fde5734STony Krowiak extern struct ap_matrix_dev *matrix_dev;
601fde5734STony Krowiak 
6165f06713STony Krowiak /**
625ef4f710STony Krowiak  * struct ap_matrix - matrix of adapters, domains and control domains
6365f06713STony Krowiak  *
6465f06713STony Krowiak  * @apm_max: max adapter number in @apm
655ef4f710STony Krowiak  * @apm: identifies the AP adapters in the matrix
6665f06713STony Krowiak  * @aqm_max: max domain number in @aqm
675ef4f710STony Krowiak  * @aqm: identifies the AP queues (domains) in the matrix
6865f06713STony Krowiak  * @adm_max: max domain number in @adm
695ef4f710STony Krowiak  * @adm: identifies the AP control domains in the matrix
705ef4f710STony Krowiak  *
715ef4f710STony Krowiak  * The AP matrix is comprised of three bit masks identifying the adapters,
725ef4f710STony Krowiak  * queues (domains) and control domains that belong to an AP matrix. The bits in
735ef4f710STony Krowiak  * each mask, from left to right, correspond to IDs 0 to 255. When a bit is set
745ef4f710STony Krowiak  * the corresponding ID belongs to the matrix.
7565f06713STony Krowiak  */
7665f06713STony Krowiak struct ap_matrix {
7765f06713STony Krowiak 	unsigned long apm_max;
78*8fb456bcSJason J. Herne 	DECLARE_BITMAP(apm, AP_DEVICES);
7965f06713STony Krowiak 	unsigned long aqm_max;
80*8fb456bcSJason J. Herne 	DECLARE_BITMAP(aqm, AP_DOMAINS);
8165f06713STony Krowiak 	unsigned long adm_max;
82*8fb456bcSJason J. Herne 	DECLARE_BITMAP(adm, AP_DOMAINS);
8365f06713STony Krowiak };
8465f06713STony Krowiak 
8565f06713STony Krowiak /**
8611cb2419STony Krowiak  * struct ap_queue_table - a table of queue objects.
8711cb2419STony Krowiak  *
8811cb2419STony Krowiak  * @queues: a hashtable of queues (struct vfio_ap_queue).
8911cb2419STony Krowiak  */
9011cb2419STony Krowiak struct ap_queue_table {
9111cb2419STony Krowiak 	DECLARE_HASHTABLE(queues, 8);
9211cb2419STony Krowiak };
9311cb2419STony Krowiak 
9411cb2419STony Krowiak /**
955ef4f710STony Krowiak  * struct ap_matrix_mdev - Contains the data associated with a matrix mediated
965ef4f710STony Krowiak  *			   device.
975ef4f710STony Krowiak  * @vdev:	the vfio device
985ef4f710STony Krowiak  * @node:	allows the ap_matrix_mdev struct to be added to a list
9965f06713STony Krowiak  * @matrix:	the adapters, usage domains and control domains assigned to the
10065f06713STony Krowiak  *		mediated matrix device.
10149b0109fSTony Krowiak  * @shadow_apcb:    the shadow copy of the APCB field of the KVM guest's CRYCB
102258287c9STony Krowiak  * @kvm:	the struct holding guest's state
1035ef4f710STony Krowiak  * @pqap_hook:	the function pointer to the interception handler for the
1045ef4f710STony Krowiak  *		PQAP(AQIC) instruction.
1055ef4f710STony Krowiak  * @mdev:	the mediated device
10611cb2419STony Krowiak  * @qtable:	table of queues (struct vfio_ap_queue) assigned to the mdev
107bf48961fSTony Krowiak  * @req_trigger eventfd ctx for signaling userspace to return a device
108eeb386aeSTony Krowiak  * @apm_add:	bitmap of APIDs added to the host's AP configuration
109eeb386aeSTony Krowiak  * @aqm_add:	bitmap of APQIs added to the host's AP configuration
110eeb386aeSTony Krowiak  * @adm_add:	bitmap of control domain numbers added to the host's AP
111eeb386aeSTony Krowiak  *		configuration
11265f06713STony Krowiak  */
11365f06713STony Krowiak struct ap_matrix_mdev {
114eb0feefdSJason Gunthorpe 	struct vfio_device vdev;
11565f06713STony Krowiak 	struct list_head node;
11665f06713STony Krowiak 	struct ap_matrix matrix;
11749b0109fSTony Krowiak 	struct ap_matrix shadow_apcb;
118258287c9STony Krowiak 	struct kvm *kvm;
1191e753732STony Krowiak 	crypto_hook pqap_hook;
12062e358ceSPierre Morel 	struct mdev_device *mdev;
12111cb2419STony Krowiak 	struct ap_queue_table qtable;
122bf48961fSTony Krowiak 	struct eventfd_ctx *req_trigger;
123eeb386aeSTony Krowiak 	DECLARE_BITMAP(apm_add, AP_DEVICES);
124eeb386aeSTony Krowiak 	DECLARE_BITMAP(aqm_add, AP_DOMAINS);
125eeb386aeSTony Krowiak 	DECLARE_BITMAP(adm_add, AP_DOMAINS);
12665f06713STony Krowiak };
12765f06713STony Krowiak 
1285ef4f710STony Krowiak /**
1295ef4f710STony Krowiak  * struct vfio_ap_queue - contains the data associated with a queue bound to the
1305ef4f710STony Krowiak  *			  vfio_ap device driver
1315ef4f710STony Krowiak  * @matrix_mdev: the matrix mediated device
1323fad3a26SNicolin Chen  * @saved_iova: the notification indicator byte (nib) address
1335ef4f710STony Krowiak  * @apqn: the APQN of the AP queue device
1345ef4f710STony Krowiak  * @saved_isc: the guest ISC registered with the GIB interface
13511cb2419STony Krowiak  * @mdev_qnode: allows the vfio_ap_queue struct to be added to a hashtable
136f848cba7STony Krowiak  * @reset_qnode: allows the vfio_ap_queue struct to be added to a list of queues
137f848cba7STony Krowiak  *		 that need to be reset
13862aab082STony Krowiak  * @reset_status: the status from the last reset of the queue
1399261f043STony Krowiak  * @reset_work: work to wait for queue reset to complete
1405ef4f710STony Krowiak  */
141ec89b55eSPierre Morel struct vfio_ap_queue {
142ec89b55eSPierre Morel 	struct ap_matrix_mdev *matrix_mdev;
1433fad3a26SNicolin Chen 	dma_addr_t saved_iova;
144ec89b55eSPierre Morel 	int	apqn;
145ec89b55eSPierre Morel #define VFIO_AP_ISC_INVALID 0xff
146ec89b55eSPierre Morel 	unsigned char saved_isc;
14711cb2419STony Krowiak 	struct hlist_node mdev_qnode;
148f848cba7STony Krowiak 	struct list_head reset_qnode;
14962aab082STony Krowiak 	struct ap_queue_status reset_status;
1509261f043STony Krowiak 	struct work_struct reset_work;
151ec89b55eSPierre Morel };
1526c12a638STony Krowiak 
1536c12a638STony Krowiak int vfio_ap_mdev_register(void);
1546c12a638STony Krowiak void vfio_ap_mdev_unregister(void);
155260f3ea1STony Krowiak 
156260f3ea1STony Krowiak int vfio_ap_mdev_probe_queue(struct ap_device *queue);
157260f3ea1STony Krowiak void vfio_ap_mdev_remove_queue(struct ap_device *queue);
1586c12a638STony Krowiak 
1593f85d1dfSTony Krowiak int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm);
1603f85d1dfSTony Krowiak 
161eeb386aeSTony Krowiak void vfio_ap_on_cfg_changed(struct ap_config_info *new_config_info,
162eeb386aeSTony Krowiak 			    struct ap_config_info *old_config_info);
163eeb386aeSTony Krowiak void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
164eeb386aeSTony Krowiak 			      struct ap_config_info *old_config_info);
165ec89b55eSPierre Morel 
1661fde5734STony Krowiak #endif /* _VFIO_AP_PRIVATE_H_ */
167