1 // SPDX-License-Identifier: GPL-2.0
2 /**
3  * PCI Endpoint *Function* (EPF) library
4  *
5  * Copyright (C) 2017 Texas Instruments
6  * Author: Kishon Vijay Abraham I <kishon@ti.com>
7  */
8 
9 #include <linux/device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 
14 #include <linux/pci-epc.h>
15 #include <linux/pci-epf.h>
16 #include <linux/pci-ep-cfs.h>
17 
18 static DEFINE_MUTEX(pci_epf_mutex);
19 
20 static struct bus_type pci_epf_bus_type;
21 static const struct device_type pci_epf_type;
22 
23 /**
24  * pci_epf_linkup() - Notify the function driver that EPC device has
25  *		      established a connection with the Root Complex.
26  * @epf: the EPF device bound to the EPC device which has established
27  *	 the connection with the host
28  *
29  * Invoke to notify the function driver that EPC device has established
30  * a connection with the Root Complex.
31  */
32 void pci_epf_linkup(struct pci_epf *epf)
33 {
34 	if (!epf->driver) {
35 		dev_WARN(&epf->dev, "epf device not bound to driver\n");
36 		return;
37 	}
38 
39 	epf->driver->ops->linkup(epf);
40 }
41 EXPORT_SYMBOL_GPL(pci_epf_linkup);
42 
43 /**
44  * pci_epf_unbind() - Notify the function driver that the binding between the
45  *		      EPF device and EPC device has been lost
46  * @epf: the EPF device which has lost the binding with the EPC device
47  *
48  * Invoke to notify the function driver that the binding between the EPF device
49  * and EPC device has been lost.
50  */
51 void pci_epf_unbind(struct pci_epf *epf)
52 {
53 	if (!epf->driver) {
54 		dev_WARN(&epf->dev, "epf device not bound to driver\n");
55 		return;
56 	}
57 
58 	epf->driver->ops->unbind(epf);
59 	module_put(epf->driver->owner);
60 }
61 EXPORT_SYMBOL_GPL(pci_epf_unbind);
62 
63 /**
64  * pci_epf_bind() - Notify the function driver that the EPF device has been
65  *		    bound to a EPC device
66  * @epf: the EPF device which has been bound to the EPC device
67  *
68  * Invoke to notify the function driver that it has been bound to a EPC device
69  */
70 int pci_epf_bind(struct pci_epf *epf)
71 {
72 	if (!epf->driver) {
73 		dev_WARN(&epf->dev, "epf device not bound to driver\n");
74 		return -EINVAL;
75 	}
76 
77 	if (!try_module_get(epf->driver->owner))
78 		return -EAGAIN;
79 
80 	return epf->driver->ops->bind(epf);
81 }
82 EXPORT_SYMBOL_GPL(pci_epf_bind);
83 
84 /**
85  * pci_epf_free_space() - free the allocated PCI EPF register space
86  * @addr: the virtual address of the PCI EPF register space
87  * @bar: the BAR number corresponding to the register space
88  *
89  * Invoke to free the allocated PCI EPF register space.
90  */
91 void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar)
92 {
93 	struct device *dev = epf->epc->dev.parent;
94 
95 	if (!addr)
96 		return;
97 
98 	dma_free_coherent(dev, epf->bar[bar].size, addr,
99 			  epf->bar[bar].phys_addr);
100 
101 	epf->bar[bar].phys_addr = 0;
102 	epf->bar[bar].size = 0;
103 	epf->bar[bar].barno = 0;
104 	epf->bar[bar].flags = 0;
105 }
106 EXPORT_SYMBOL_GPL(pci_epf_free_space);
107 
108 /**
109  * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
110  * @size: the size of the memory that has to be allocated
111  * @bar: the BAR number corresponding to the allocated register space
112  *
113  * Invoke to allocate memory for the PCI EPF register space.
114  */
115 void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar)
116 {
117 	void *space;
118 	struct device *dev = epf->epc->dev.parent;
119 	dma_addr_t phys_addr;
120 
121 	if (size < 128)
122 		size = 128;
123 	size = roundup_pow_of_two(size);
124 
125 	space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
126 	if (!space) {
127 		dev_err(dev, "failed to allocate mem space\n");
128 		return NULL;
129 	}
130 
131 	epf->bar[bar].phys_addr = phys_addr;
132 	epf->bar[bar].size = size;
133 	epf->bar[bar].barno = bar;
134 	epf->bar[bar].flags |= upper_32_bits(size) ?
135 				PCI_BASE_ADDRESS_MEM_TYPE_64 :
136 				PCI_BASE_ADDRESS_MEM_TYPE_32;
137 
138 	return space;
139 }
140 EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
141 
142 static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
143 {
144 	struct config_group *group, *tmp;
145 
146 	if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
147 		return;
148 
149 	mutex_lock(&pci_epf_mutex);
150 	list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
151 		pci_ep_cfs_remove_epf_group(group);
152 	list_del(&driver->epf_group);
153 	mutex_unlock(&pci_epf_mutex);
154 }
155 
156 /**
157  * pci_epf_unregister_driver() - unregister the PCI EPF driver
158  * @driver: the PCI EPF driver that has to be unregistered
159  *
160  * Invoke to unregister the PCI EPF driver.
161  */
162 void pci_epf_unregister_driver(struct pci_epf_driver *driver)
163 {
164 	pci_epf_remove_cfs(driver);
165 	driver_unregister(&driver->driver);
166 }
167 EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
168 
169 static int pci_epf_add_cfs(struct pci_epf_driver *driver)
170 {
171 	struct config_group *group;
172 	const struct pci_epf_device_id *id;
173 
174 	if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
175 		return 0;
176 
177 	INIT_LIST_HEAD(&driver->epf_group);
178 
179 	id = driver->id_table;
180 	while (id->name[0]) {
181 		group = pci_ep_cfs_add_epf_group(id->name);
182 		if (IS_ERR(group)) {
183 			pci_epf_remove_cfs(driver);
184 			return PTR_ERR(group);
185 		}
186 
187 		mutex_lock(&pci_epf_mutex);
188 		list_add_tail(&group->group_entry, &driver->epf_group);
189 		mutex_unlock(&pci_epf_mutex);
190 		id++;
191 	}
192 
193 	return 0;
194 }
195 
196 /**
197  * __pci_epf_register_driver() - register a new PCI EPF driver
198  * @driver: structure representing PCI EPF driver
199  * @owner: the owner of the module that registers the PCI EPF driver
200  *
201  * Invoke to register a new PCI EPF driver.
202  */
203 int __pci_epf_register_driver(struct pci_epf_driver *driver,
204 			      struct module *owner)
205 {
206 	int ret;
207 
208 	if (!driver->ops)
209 		return -EINVAL;
210 
211 	if (!driver->ops->bind || !driver->ops->unbind || !driver->ops->linkup)
212 		return -EINVAL;
213 
214 	driver->driver.bus = &pci_epf_bus_type;
215 	driver->driver.owner = owner;
216 
217 	ret = driver_register(&driver->driver);
218 	if (ret)
219 		return ret;
220 
221 	pci_epf_add_cfs(driver);
222 
223 	return 0;
224 }
225 EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
226 
227 /**
228  * pci_epf_destroy() - destroy the created PCI EPF device
229  * @epf: the PCI EPF device that has to be destroyed.
230  *
231  * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
232  */
233 void pci_epf_destroy(struct pci_epf *epf)
234 {
235 	device_unregister(&epf->dev);
236 }
237 EXPORT_SYMBOL_GPL(pci_epf_destroy);
238 
239 /**
240  * pci_epf_create() - create a new PCI EPF device
241  * @name: the name of the PCI EPF device. This name will be used to bind the
242  *	  the EPF device to a EPF driver
243  *
244  * Invoke to create a new PCI EPF device by providing the name of the function
245  * device.
246  */
247 struct pci_epf *pci_epf_create(const char *name)
248 {
249 	int ret;
250 	struct pci_epf *epf;
251 	struct device *dev;
252 	int len;
253 
254 	epf = kzalloc(sizeof(*epf), GFP_KERNEL);
255 	if (!epf)
256 		return ERR_PTR(-ENOMEM);
257 
258 	len = strchrnul(name, '.') - name;
259 	epf->name = kstrndup(name, len, GFP_KERNEL);
260 	if (!epf->name) {
261 		kfree(epf);
262 		return ERR_PTR(-ENOMEM);
263 	}
264 
265 	dev = &epf->dev;
266 	device_initialize(dev);
267 	dev->bus = &pci_epf_bus_type;
268 	dev->type = &pci_epf_type;
269 
270 	ret = dev_set_name(dev, "%s", name);
271 	if (ret) {
272 		put_device(dev);
273 		return ERR_PTR(ret);
274 	}
275 
276 	ret = device_add(dev);
277 	if (ret) {
278 		put_device(dev);
279 		return ERR_PTR(ret);
280 	}
281 
282 	return epf;
283 }
284 EXPORT_SYMBOL_GPL(pci_epf_create);
285 
286 const struct pci_epf_device_id *
287 pci_epf_match_device(const struct pci_epf_device_id *id, struct pci_epf *epf)
288 {
289 	if (!id || !epf)
290 		return NULL;
291 
292 	while (*id->name) {
293 		if (strcmp(epf->name, id->name) == 0)
294 			return id;
295 		id++;
296 	}
297 
298 	return NULL;
299 }
300 EXPORT_SYMBOL_GPL(pci_epf_match_device);
301 
302 static void pci_epf_dev_release(struct device *dev)
303 {
304 	struct pci_epf *epf = to_pci_epf(dev);
305 
306 	kfree(epf->name);
307 	kfree(epf);
308 }
309 
310 static const struct device_type pci_epf_type = {
311 	.release	= pci_epf_dev_release,
312 };
313 
314 static int
315 pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
316 {
317 	while (id->name[0]) {
318 		if (strcmp(epf->name, id->name) == 0)
319 			return true;
320 		id++;
321 	}
322 
323 	return false;
324 }
325 
326 static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
327 {
328 	struct pci_epf *epf = to_pci_epf(dev);
329 	struct pci_epf_driver *driver = to_pci_epf_driver(drv);
330 
331 	if (driver->id_table)
332 		return pci_epf_match_id(driver->id_table, epf);
333 
334 	return !strcmp(epf->name, drv->name);
335 }
336 
337 static int pci_epf_device_probe(struct device *dev)
338 {
339 	struct pci_epf *epf = to_pci_epf(dev);
340 	struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
341 
342 	if (!driver->probe)
343 		return -ENODEV;
344 
345 	epf->driver = driver;
346 
347 	return driver->probe(epf);
348 }
349 
350 static int pci_epf_device_remove(struct device *dev)
351 {
352 	int ret = 0;
353 	struct pci_epf *epf = to_pci_epf(dev);
354 	struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
355 
356 	if (driver->remove)
357 		ret = driver->remove(epf);
358 	epf->driver = NULL;
359 
360 	return ret;
361 }
362 
363 static struct bus_type pci_epf_bus_type = {
364 	.name		= "pci-epf",
365 	.match		= pci_epf_device_match,
366 	.probe		= pci_epf_device_probe,
367 	.remove		= pci_epf_device_remove,
368 };
369 
370 static int __init pci_epf_init(void)
371 {
372 	int ret;
373 
374 	ret = bus_register(&pci_epf_bus_type);
375 	if (ret) {
376 		pr_err("failed to register pci epf bus --> %d\n", ret);
377 		return ret;
378 	}
379 
380 	return 0;
381 }
382 module_init(pci_epf_init);
383 
384 static void __exit pci_epf_exit(void)
385 {
386 	bus_unregister(&pci_epf_bus_type);
387 }
388 module_exit(pci_epf_exit);
389 
390 MODULE_DESCRIPTION("PCI EPF Library");
391 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
392 MODULE_LICENSE("GPL v2");
393