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