1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * VFIO based AP device driver
4  *
5  * Copyright IBM Corp. 2018
6  *
7  * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
8  *	      Pierre Morel <pmorel@linux.ibm.com>
9  */
10 
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <asm/facility.h>
16 #include "vfio_ap_private.h"
17 
18 #define VFIO_AP_ROOT_NAME "vfio_ap"
19 #define VFIO_AP_DEV_NAME "matrix"
20 
21 MODULE_AUTHOR("IBM Corporation");
22 MODULE_DESCRIPTION("VFIO AP device driver, Copyright IBM Corp. 2018");
23 MODULE_LICENSE("GPL v2");
24 
25 static struct ap_driver vfio_ap_drv;
26 
27 struct ap_matrix_dev *matrix_dev;
28 
29 /* Only type 10 adapters (CEX4 and later) are supported
30  * by the AP matrix device driver
31  */
32 static struct ap_device_id ap_queue_ids[] = {
33 	{ .dev_type = AP_DEVICE_TYPE_CEX4,
34 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
35 	{ .dev_type = AP_DEVICE_TYPE_CEX5,
36 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
37 	{ .dev_type = AP_DEVICE_TYPE_CEX6,
38 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
39 	{ .dev_type = AP_DEVICE_TYPE_CEX7,
40 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
41 	{ /* end of sibling */ },
42 };
43 
44 MODULE_DEVICE_TABLE(vfio_ap, ap_queue_ids);
45 
46 /**
47  * vfio_ap_queue_dev_probe:
48  *
49  * Allocate a vfio_ap_queue structure and associate it
50  * with the device as driver_data.
51  */
52 static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
53 {
54 	struct vfio_ap_queue *q;
55 
56 	q = kzalloc(sizeof(*q), GFP_KERNEL);
57 	if (!q)
58 		return -ENOMEM;
59 	dev_set_drvdata(&apdev->device, q);
60 	q->apqn = to_ap_queue(&apdev->device)->qid;
61 	q->saved_isc = VFIO_AP_ISC_INVALID;
62 	return 0;
63 }
64 
65 /**
66  * vfio_ap_queue_dev_remove:
67  *
68  * Takes the matrix lock to avoid actions on this device while removing
69  * Free the associated vfio_ap_queue structure
70  */
71 static void vfio_ap_queue_dev_remove(struct ap_device *apdev)
72 {
73 	struct vfio_ap_queue *q;
74 
75 	mutex_lock(&matrix_dev->lock);
76 	q = dev_get_drvdata(&apdev->device);
77 	vfio_ap_mdev_reset_queue(q, 1);
78 	dev_set_drvdata(&apdev->device, NULL);
79 	kfree(q);
80 	mutex_unlock(&matrix_dev->lock);
81 }
82 
83 static void vfio_ap_matrix_dev_release(struct device *dev)
84 {
85 	struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev);
86 
87 	kfree(matrix_dev);
88 }
89 
90 static int matrix_bus_match(struct device *dev, struct device_driver *drv)
91 {
92 	return 1;
93 }
94 
95 static struct bus_type matrix_bus = {
96 	.name = "matrix",
97 	.match = &matrix_bus_match,
98 };
99 
100 static struct device_driver matrix_driver = {
101 	.name = "vfio_ap",
102 	.bus = &matrix_bus,
103 	.suppress_bind_attrs = true,
104 };
105 
106 static int vfio_ap_matrix_dev_create(void)
107 {
108 	int ret;
109 	struct device *root_device;
110 
111 	root_device = root_device_register(VFIO_AP_ROOT_NAME);
112 	if (IS_ERR(root_device))
113 		return PTR_ERR(root_device);
114 
115 	ret = bus_register(&matrix_bus);
116 	if (ret)
117 		goto bus_register_err;
118 
119 	matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
120 	if (!matrix_dev) {
121 		ret = -ENOMEM;
122 		goto matrix_alloc_err;
123 	}
124 
125 	/* Fill in config info via PQAP(QCI), if available */
126 	if (test_facility(12)) {
127 		ret = ap_qci(&matrix_dev->info);
128 		if (ret)
129 			goto matrix_alloc_err;
130 	}
131 
132 	mutex_init(&matrix_dev->lock);
133 	INIT_LIST_HEAD(&matrix_dev->mdev_list);
134 
135 	dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME);
136 	matrix_dev->device.parent = root_device;
137 	matrix_dev->device.bus = &matrix_bus;
138 	matrix_dev->device.release = vfio_ap_matrix_dev_release;
139 	matrix_dev->vfio_ap_drv = &vfio_ap_drv;
140 
141 	ret = device_register(&matrix_dev->device);
142 	if (ret)
143 		goto matrix_reg_err;
144 
145 	ret = driver_register(&matrix_driver);
146 	if (ret)
147 		goto matrix_drv_err;
148 
149 	return 0;
150 
151 matrix_drv_err:
152 	device_unregister(&matrix_dev->device);
153 matrix_reg_err:
154 	put_device(&matrix_dev->device);
155 matrix_alloc_err:
156 	bus_unregister(&matrix_bus);
157 bus_register_err:
158 	root_device_unregister(root_device);
159 	return ret;
160 }
161 
162 static void vfio_ap_matrix_dev_destroy(void)
163 {
164 	struct device *root_device = matrix_dev->device.parent;
165 
166 	driver_unregister(&matrix_driver);
167 	device_unregister(&matrix_dev->device);
168 	bus_unregister(&matrix_bus);
169 	root_device_unregister(root_device);
170 }
171 
172 static int __init vfio_ap_init(void)
173 {
174 	int ret;
175 
176 	/* If there are no AP instructions, there is nothing to pass through. */
177 	if (!ap_instructions_available())
178 		return -ENODEV;
179 
180 	ret = vfio_ap_matrix_dev_create();
181 	if (ret)
182 		return ret;
183 
184 	memset(&vfio_ap_drv, 0, sizeof(vfio_ap_drv));
185 	vfio_ap_drv.probe = vfio_ap_queue_dev_probe;
186 	vfio_ap_drv.remove = vfio_ap_queue_dev_remove;
187 	vfio_ap_drv.ids = ap_queue_ids;
188 
189 	ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);
190 	if (ret) {
191 		vfio_ap_matrix_dev_destroy();
192 		return ret;
193 	}
194 
195 	ret = vfio_ap_mdev_register();
196 	if (ret) {
197 		ap_driver_unregister(&vfio_ap_drv);
198 		vfio_ap_matrix_dev_destroy();
199 
200 		return ret;
201 	}
202 
203 	return 0;
204 }
205 
206 static void __exit vfio_ap_exit(void)
207 {
208 	vfio_ap_mdev_unregister();
209 	ap_driver_unregister(&vfio_ap_drv);
210 	vfio_ap_matrix_dev_destroy();
211 }
212 
213 module_init(vfio_ap_init);
214 module_exit(vfio_ap_exit);
215