• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
46  *
47  * Allocate a vfio_ap_queue structure and associate it
48  * with the device as driver_data.
49  */
vfio_ap_queue_dev_probe(struct ap_device * apdev)50 static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
51 {
52 	struct vfio_ap_queue *q;
53 
54 	q = kzalloc(sizeof(*q), GFP_KERNEL);
55 	if (!q)
56 		return -ENOMEM;
57 	dev_set_drvdata(&apdev->device, q);
58 	q->apqn = to_ap_queue(&apdev->device)->qid;
59 	q->saved_isc = VFIO_AP_ISC_INVALID;
60 	return 0;
61 }
62 
63 /**
64  * vfio_ap_queue_dev_remove:
65  *
66  * Takes the matrix lock to avoid actions on this device while removing
67  * Free the associated vfio_ap_queue structure
68  */
vfio_ap_queue_dev_remove(struct ap_device * apdev)69 static void vfio_ap_queue_dev_remove(struct ap_device *apdev)
70 {
71 	struct vfio_ap_queue *q;
72 
73 	mutex_lock(&matrix_dev->lock);
74 	q = dev_get_drvdata(&apdev->device);
75 	vfio_ap_mdev_reset_queue(q, 1);
76 	dev_set_drvdata(&apdev->device, NULL);
77 	kfree(q);
78 	mutex_unlock(&matrix_dev->lock);
79 }
80 
81 static struct ap_driver vfio_ap_drv = {
82 	.probe = vfio_ap_queue_dev_probe,
83 	.remove = vfio_ap_queue_dev_remove,
84 	.ids = ap_queue_ids,
85 };
86 
vfio_ap_matrix_dev_release(struct device * dev)87 static void vfio_ap_matrix_dev_release(struct device *dev)
88 {
89 	struct ap_matrix_dev *matrix_dev;
90 
91 	matrix_dev = container_of(dev, struct ap_matrix_dev, device);
92 	kfree(matrix_dev);
93 }
94 
matrix_bus_match(struct device * dev,struct device_driver * drv)95 static int matrix_bus_match(struct device *dev, struct device_driver *drv)
96 {
97 	return 1;
98 }
99 
100 static struct bus_type matrix_bus = {
101 	.name = "matrix",
102 	.match = &matrix_bus_match,
103 };
104 
105 static struct device_driver matrix_driver = {
106 	.name = "vfio_ap",
107 	.bus = &matrix_bus,
108 	.suppress_bind_attrs = true,
109 };
110 
vfio_ap_matrix_dev_create(void)111 static int vfio_ap_matrix_dev_create(void)
112 {
113 	int ret;
114 	struct device *root_device;
115 
116 	root_device = root_device_register(VFIO_AP_ROOT_NAME);
117 	if (IS_ERR(root_device))
118 		return PTR_ERR(root_device);
119 
120 	ret = bus_register(&matrix_bus);
121 	if (ret)
122 		goto bus_register_err;
123 
124 	matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
125 	if (!matrix_dev) {
126 		ret = -ENOMEM;
127 		goto matrix_alloc_err;
128 	}
129 
130 	/* Fill in config info via PQAP(QCI), if available */
131 	if (test_facility(12)) {
132 		ret = ap_qci(&matrix_dev->info);
133 		if (ret)
134 			goto matrix_alloc_err;
135 	}
136 
137 	mutex_init(&matrix_dev->lock);
138 	INIT_LIST_HEAD(&matrix_dev->mdev_list);
139 
140 	dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME);
141 	matrix_dev->device.parent = root_device;
142 	matrix_dev->device.bus = &matrix_bus;
143 	matrix_dev->device.release = vfio_ap_matrix_dev_release;
144 	matrix_dev->vfio_ap_drv = &vfio_ap_drv;
145 
146 	ret = device_register(&matrix_dev->device);
147 	if (ret)
148 		goto matrix_reg_err;
149 
150 	ret = driver_register(&matrix_driver);
151 	if (ret)
152 		goto matrix_drv_err;
153 
154 	return 0;
155 
156 matrix_drv_err:
157 	device_unregister(&matrix_dev->device);
158 matrix_reg_err:
159 	put_device(&matrix_dev->device);
160 matrix_alloc_err:
161 	bus_unregister(&matrix_bus);
162 bus_register_err:
163 	root_device_unregister(root_device);
164 	return ret;
165 }
166 
vfio_ap_matrix_dev_destroy(void)167 static void vfio_ap_matrix_dev_destroy(void)
168 {
169 	struct device *root_device = matrix_dev->device.parent;
170 
171 	driver_unregister(&matrix_driver);
172 	device_unregister(&matrix_dev->device);
173 	bus_unregister(&matrix_bus);
174 	root_device_unregister(root_device);
175 }
176 
vfio_ap_init(void)177 static int __init vfio_ap_init(void)
178 {
179 	int ret;
180 
181 	/* If there are no AP instructions, there is nothing to pass through. */
182 	if (!ap_instructions_available())
183 		return -ENODEV;
184 
185 	ret = vfio_ap_matrix_dev_create();
186 	if (ret)
187 		return ret;
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 
vfio_ap_exit(void)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