1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Private data and functions for adjunct processor VFIO matrix driver. 4 * 5 * Author(s): Tony Krowiak <akrowiak@linux.ibm.com> 6 * Halil Pasic <pasic@linux.ibm.com> 7 * Pierre Morel <pmorel@linux.ibm.com> 8 * 9 * Copyright IBM Corp. 2018 10 */ 11 12 #ifndef _VFIO_AP_PRIVATE_H_ 13 #define _VFIO_AP_PRIVATE_H_ 14 15 #include <linux/types.h> 16 #include <linux/mdev.h> 17 #include <linux/delay.h> 18 #include <linux/mutex.h> 19 #include <linux/kvm_host.h> 20 #include <linux/vfio.h> 21 #include <linux/hashtable.h> 22 23 #include "ap_bus.h" 24 25 #define VFIO_AP_MODULE_NAME "vfio_ap" 26 #define VFIO_AP_DRV_NAME "vfio_ap" 27 28 /** 29 * struct ap_matrix_dev - Contains the data for the matrix device. 30 * 31 * @device: generic device structure associated with the AP matrix device 32 * @available_instances: number of mediated matrix devices that can be created 33 * @info: the struct containing the output from the PQAP(QCI) instruction 34 * @mdev_list: the list of mediated matrix devices created 35 * @mdevs_lock: mutex for locking the AP matrix device. This lock will be 36 * taken every time we fiddle with state managed by the vfio_ap 37 * driver, be it using @mdev_list or writing the state of a 38 * single ap_matrix_mdev device. It's quite coarse but we don't 39 * expect much contention. 40 * @vfio_ap_drv: the vfio_ap device driver 41 * @guests_lock: mutex for controlling access to a guest that is using AP 42 * devices passed through by the vfio_ap device driver. This lock 43 * will be taken when the AP devices are plugged into or unplugged 44 * from a guest, and when an ap_matrix_mdev device is added to or 45 * removed from @mdev_list or the list is iterated. 46 */ 47 struct ap_matrix_dev { 48 struct device device; 49 atomic_t available_instances; 50 struct ap_config_info info; 51 struct list_head mdev_list; 52 struct mutex mdevs_lock; /* serializes access to each ap_matrix_mdev */ 53 struct ap_driver *vfio_ap_drv; 54 struct mutex guests_lock; /* serializes access to each KVM guest */ 55 struct mdev_parent parent; 56 struct mdev_type mdev_type; 57 struct mdev_type *mdev_types[]; 58 }; 59 60 extern struct ap_matrix_dev *matrix_dev; 61 62 /** 63 * struct ap_matrix - matrix of adapters, domains and control domains 64 * 65 * @apm_max: max adapter number in @apm 66 * @apm: identifies the AP adapters in the matrix 67 * @aqm_max: max domain number in @aqm 68 * @aqm: identifies the AP queues (domains) in the matrix 69 * @adm_max: max domain number in @adm 70 * @adm: identifies the AP control domains in the matrix 71 * 72 * The AP matrix is comprised of three bit masks identifying the adapters, 73 * queues (domains) and control domains that belong to an AP matrix. The bits in 74 * each mask, from left to right, correspond to IDs 0 to 255. When a bit is set 75 * the corresponding ID belongs to the matrix. 76 */ 77 struct ap_matrix { 78 unsigned long apm_max; 79 DECLARE_BITMAP(apm, 256); 80 unsigned long aqm_max; 81 DECLARE_BITMAP(aqm, 256); 82 unsigned long adm_max; 83 DECLARE_BITMAP(adm, 256); 84 }; 85 86 /** 87 * struct ap_queue_table - a table of queue objects. 88 * 89 * @queues: a hashtable of queues (struct vfio_ap_queue). 90 */ 91 struct ap_queue_table { 92 DECLARE_HASHTABLE(queues, 8); 93 }; 94 95 /** 96 * struct ap_matrix_mdev - Contains the data associated with a matrix mediated 97 * device. 98 * @vdev: the vfio device 99 * @node: allows the ap_matrix_mdev struct to be added to a list 100 * @matrix: the adapters, usage domains and control domains assigned to the 101 * mediated matrix device. 102 * @shadow_apcb: the shadow copy of the APCB field of the KVM guest's CRYCB 103 * @kvm: the struct holding guest's state 104 * @pqap_hook: the function pointer to the interception handler for the 105 * PQAP(AQIC) instruction. 106 * @mdev: the mediated device 107 * @qtable: table of queues (struct vfio_ap_queue) assigned to the mdev 108 * @apm_add: bitmap of APIDs added to the host's AP configuration 109 * @aqm_add: bitmap of APQIs added to the host's AP configuration 110 * @adm_add: bitmap of control domain numbers added to the host's AP 111 * configuration 112 */ 113 struct ap_matrix_mdev { 114 struct vfio_device vdev; 115 struct list_head node; 116 struct ap_matrix matrix; 117 struct ap_matrix shadow_apcb; 118 struct kvm *kvm; 119 crypto_hook pqap_hook; 120 struct mdev_device *mdev; 121 struct ap_queue_table qtable; 122 DECLARE_BITMAP(apm_add, AP_DEVICES); 123 DECLARE_BITMAP(aqm_add, AP_DOMAINS); 124 DECLARE_BITMAP(adm_add, AP_DOMAINS); 125 }; 126 127 /** 128 * struct vfio_ap_queue - contains the data associated with a queue bound to the 129 * vfio_ap device driver 130 * @matrix_mdev: the matrix mediated device 131 * @saved_iova: the notification indicator byte (nib) address 132 * @apqn: the APQN of the AP queue device 133 * @saved_isc: the guest ISC registered with the GIB interface 134 * @mdev_qnode: allows the vfio_ap_queue struct to be added to a hashtable 135 * @reset_rc: the status response code from the last reset of the queue 136 */ 137 struct vfio_ap_queue { 138 struct ap_matrix_mdev *matrix_mdev; 139 dma_addr_t saved_iova; 140 int apqn; 141 #define VFIO_AP_ISC_INVALID 0xff 142 unsigned char saved_isc; 143 struct hlist_node mdev_qnode; 144 unsigned int reset_rc; 145 }; 146 147 int vfio_ap_mdev_register(void); 148 void vfio_ap_mdev_unregister(void); 149 150 int vfio_ap_mdev_probe_queue(struct ap_device *queue); 151 void vfio_ap_mdev_remove_queue(struct ap_device *queue); 152 153 int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm); 154 155 void vfio_ap_on_cfg_changed(struct ap_config_info *new_config_info, 156 struct ap_config_info *old_config_info); 157 void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info, 158 struct ap_config_info *old_config_info); 159 160 #endif /* _VFIO_AP_PRIVATE_H_ */ 161