Loading...
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
18static DEFINE_MUTEX(pci_epf_mutex);
19
20static struct bus_type pci_epf_bus_type;
21static 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 */
32void 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}
41EXPORT_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 */
51void 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}
61EXPORT_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 */
70int 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}
82EXPORT_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 */
91void 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}
106EXPORT_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 * @align: alignment size for the allocation region
113 *
114 * Invoke to allocate memory for the PCI EPF register space.
115 */
116void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
117 size_t align)
118{
119 void *space;
120 struct device *dev = epf->epc->dev.parent;
121 dma_addr_t phys_addr;
122
123 if (size < 128)
124 size = 128;
125
126 if (align)
127 size = ALIGN(size, align);
128 else
129 size = roundup_pow_of_two(size);
130
131 space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
132 if (!space) {
133 dev_err(dev, "failed to allocate mem space\n");
134 return NULL;
135 }
136
137 epf->bar[bar].phys_addr = phys_addr;
138 epf->bar[bar].size = size;
139 epf->bar[bar].barno = bar;
140 epf->bar[bar].flags |= upper_32_bits(size) ?
141 PCI_BASE_ADDRESS_MEM_TYPE_64 :
142 PCI_BASE_ADDRESS_MEM_TYPE_32;
143
144 return space;
145}
146EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
147
148static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
149{
150 struct config_group *group, *tmp;
151
152 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
153 return;
154
155 mutex_lock(&pci_epf_mutex);
156 list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
157 pci_ep_cfs_remove_epf_group(group);
158 list_del(&driver->epf_group);
159 mutex_unlock(&pci_epf_mutex);
160}
161
162/**
163 * pci_epf_unregister_driver() - unregister the PCI EPF driver
164 * @driver: the PCI EPF driver that has to be unregistered
165 *
166 * Invoke to unregister the PCI EPF driver.
167 */
168void pci_epf_unregister_driver(struct pci_epf_driver *driver)
169{
170 pci_epf_remove_cfs(driver);
171 driver_unregister(&driver->driver);
172}
173EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
174
175static int pci_epf_add_cfs(struct pci_epf_driver *driver)
176{
177 struct config_group *group;
178 const struct pci_epf_device_id *id;
179
180 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
181 return 0;
182
183 INIT_LIST_HEAD(&driver->epf_group);
184
185 id = driver->id_table;
186 while (id->name[0]) {
187 group = pci_ep_cfs_add_epf_group(id->name);
188 if (IS_ERR(group)) {
189 pci_epf_remove_cfs(driver);
190 return PTR_ERR(group);
191 }
192
193 mutex_lock(&pci_epf_mutex);
194 list_add_tail(&group->group_entry, &driver->epf_group);
195 mutex_unlock(&pci_epf_mutex);
196 id++;
197 }
198
199 return 0;
200}
201
202/**
203 * __pci_epf_register_driver() - register a new PCI EPF driver
204 * @driver: structure representing PCI EPF driver
205 * @owner: the owner of the module that registers the PCI EPF driver
206 *
207 * Invoke to register a new PCI EPF driver.
208 */
209int __pci_epf_register_driver(struct pci_epf_driver *driver,
210 struct module *owner)
211{
212 int ret;
213
214 if (!driver->ops)
215 return -EINVAL;
216
217 if (!driver->ops->bind || !driver->ops->unbind || !driver->ops->linkup)
218 return -EINVAL;
219
220 driver->driver.bus = &pci_epf_bus_type;
221 driver->driver.owner = owner;
222
223 ret = driver_register(&driver->driver);
224 if (ret)
225 return ret;
226
227 pci_epf_add_cfs(driver);
228
229 return 0;
230}
231EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
232
233/**
234 * pci_epf_destroy() - destroy the created PCI EPF device
235 * @epf: the PCI EPF device that has to be destroyed.
236 *
237 * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
238 */
239void pci_epf_destroy(struct pci_epf *epf)
240{
241 device_unregister(&epf->dev);
242}
243EXPORT_SYMBOL_GPL(pci_epf_destroy);
244
245/**
246 * pci_epf_create() - create a new PCI EPF device
247 * @name: the name of the PCI EPF device. This name will be used to bind the
248 * the EPF device to a EPF driver
249 *
250 * Invoke to create a new PCI EPF device by providing the name of the function
251 * device.
252 */
253struct pci_epf *pci_epf_create(const char *name)
254{
255 int ret;
256 struct pci_epf *epf;
257 struct device *dev;
258 int len;
259
260 epf = kzalloc(sizeof(*epf), GFP_KERNEL);
261 if (!epf)
262 return ERR_PTR(-ENOMEM);
263
264 len = strchrnul(name, '.') - name;
265 epf->name = kstrndup(name, len, GFP_KERNEL);
266 if (!epf->name) {
267 kfree(epf);
268 return ERR_PTR(-ENOMEM);
269 }
270
271 dev = &epf->dev;
272 device_initialize(dev);
273 dev->bus = &pci_epf_bus_type;
274 dev->type = &pci_epf_type;
275
276 ret = dev_set_name(dev, "%s", name);
277 if (ret) {
278 put_device(dev);
279 return ERR_PTR(ret);
280 }
281
282 ret = device_add(dev);
283 if (ret) {
284 put_device(dev);
285 return ERR_PTR(ret);
286 }
287
288 return epf;
289}
290EXPORT_SYMBOL_GPL(pci_epf_create);
291
292const struct pci_epf_device_id *
293pci_epf_match_device(const struct pci_epf_device_id *id, struct pci_epf *epf)
294{
295 if (!id || !epf)
296 return NULL;
297
298 while (*id->name) {
299 if (strcmp(epf->name, id->name) == 0)
300 return id;
301 id++;
302 }
303
304 return NULL;
305}
306EXPORT_SYMBOL_GPL(pci_epf_match_device);
307
308static void pci_epf_dev_release(struct device *dev)
309{
310 struct pci_epf *epf = to_pci_epf(dev);
311
312 kfree(epf->name);
313 kfree(epf);
314}
315
316static const struct device_type pci_epf_type = {
317 .release = pci_epf_dev_release,
318};
319
320static int
321pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
322{
323 while (id->name[0]) {
324 if (strcmp(epf->name, id->name) == 0)
325 return true;
326 id++;
327 }
328
329 return false;
330}
331
332static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
333{
334 struct pci_epf *epf = to_pci_epf(dev);
335 struct pci_epf_driver *driver = to_pci_epf_driver(drv);
336
337 if (driver->id_table)
338 return pci_epf_match_id(driver->id_table, epf);
339
340 return !strcmp(epf->name, drv->name);
341}
342
343static int pci_epf_device_probe(struct device *dev)
344{
345 struct pci_epf *epf = to_pci_epf(dev);
346 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
347
348 if (!driver->probe)
349 return -ENODEV;
350
351 epf->driver = driver;
352
353 return driver->probe(epf);
354}
355
356static int pci_epf_device_remove(struct device *dev)
357{
358 int ret = 0;
359 struct pci_epf *epf = to_pci_epf(dev);
360 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
361
362 if (driver->remove)
363 ret = driver->remove(epf);
364 epf->driver = NULL;
365
366 return ret;
367}
368
369static struct bus_type pci_epf_bus_type = {
370 .name = "pci-epf",
371 .match = pci_epf_device_match,
372 .probe = pci_epf_device_probe,
373 .remove = pci_epf_device_remove,
374};
375
376static int __init pci_epf_init(void)
377{
378 int ret;
379
380 ret = bus_register(&pci_epf_bus_type);
381 if (ret) {
382 pr_err("failed to register pci epf bus --> %d\n", ret);
383 return ret;
384 }
385
386 return 0;
387}
388module_init(pci_epf_init);
389
390static void __exit pci_epf_exit(void)
391{
392 bus_unregister(&pci_epf_bus_type);
393}
394module_exit(pci_epf_exit);
395
396MODULE_DESCRIPTION("PCI EPF Library");
397MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
398MODULE_LICENSE("GPL v2");
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
18static DEFINE_MUTEX(pci_epf_mutex);
19
20static const struct bus_type pci_epf_bus_type;
21static const struct device_type pci_epf_type;
22
23/**
24 * pci_epf_unbind() - Notify the function driver that the binding between the
25 * EPF device and EPC device has been lost
26 * @epf: the EPF device which has lost the binding with the EPC device
27 *
28 * Invoke to notify the function driver that the binding between the EPF device
29 * and EPC device has been lost.
30 */
31void pci_epf_unbind(struct pci_epf *epf)
32{
33 struct pci_epf *epf_vf;
34
35 if (!epf->driver) {
36 dev_WARN(&epf->dev, "epf device not bound to driver\n");
37 return;
38 }
39
40 mutex_lock(&epf->lock);
41 list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
42 if (epf_vf->is_bound)
43 epf_vf->driver->ops->unbind(epf_vf);
44 }
45 if (epf->is_bound)
46 epf->driver->ops->unbind(epf);
47 mutex_unlock(&epf->lock);
48 module_put(epf->driver->owner);
49}
50EXPORT_SYMBOL_GPL(pci_epf_unbind);
51
52/**
53 * pci_epf_bind() - Notify the function driver that the EPF device has been
54 * bound to a EPC device
55 * @epf: the EPF device which has been bound to the EPC device
56 *
57 * Invoke to notify the function driver that it has been bound to a EPC device
58 */
59int pci_epf_bind(struct pci_epf *epf)
60{
61 struct device *dev = &epf->dev;
62 struct pci_epf *epf_vf;
63 u8 func_no, vfunc_no;
64 struct pci_epc *epc;
65 int ret;
66
67 if (!epf->driver) {
68 dev_WARN(dev, "epf device not bound to driver\n");
69 return -EINVAL;
70 }
71
72 if (!try_module_get(epf->driver->owner))
73 return -EAGAIN;
74
75 mutex_lock(&epf->lock);
76 list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
77 vfunc_no = epf_vf->vfunc_no;
78
79 if (vfunc_no < 1) {
80 dev_err(dev, "Invalid virtual function number\n");
81 ret = -EINVAL;
82 goto ret;
83 }
84
85 epc = epf->epc;
86 func_no = epf->func_no;
87 if (!IS_ERR_OR_NULL(epc)) {
88 if (!epc->max_vfs) {
89 dev_err(dev, "No support for virt function\n");
90 ret = -EINVAL;
91 goto ret;
92 }
93
94 if (vfunc_no > epc->max_vfs[func_no]) {
95 dev_err(dev, "PF%d: Exceeds max vfunc number\n",
96 func_no);
97 ret = -EINVAL;
98 goto ret;
99 }
100 }
101
102 epc = epf->sec_epc;
103 func_no = epf->sec_epc_func_no;
104 if (!IS_ERR_OR_NULL(epc)) {
105 if (!epc->max_vfs) {
106 dev_err(dev, "No support for virt function\n");
107 ret = -EINVAL;
108 goto ret;
109 }
110
111 if (vfunc_no > epc->max_vfs[func_no]) {
112 dev_err(dev, "PF%d: Exceeds max vfunc number\n",
113 func_no);
114 ret = -EINVAL;
115 goto ret;
116 }
117 }
118
119 epf_vf->func_no = epf->func_no;
120 epf_vf->sec_epc_func_no = epf->sec_epc_func_no;
121 epf_vf->epc = epf->epc;
122 epf_vf->sec_epc = epf->sec_epc;
123 ret = epf_vf->driver->ops->bind(epf_vf);
124 if (ret)
125 goto ret;
126 epf_vf->is_bound = true;
127 }
128
129 ret = epf->driver->ops->bind(epf);
130 if (ret)
131 goto ret;
132 epf->is_bound = true;
133
134 mutex_unlock(&epf->lock);
135 return 0;
136
137ret:
138 mutex_unlock(&epf->lock);
139 pci_epf_unbind(epf);
140
141 return ret;
142}
143EXPORT_SYMBOL_GPL(pci_epf_bind);
144
145/**
146 * pci_epf_add_vepf() - associate virtual EP function to physical EP function
147 * @epf_pf: the physical EP function to which the virtual EP function should be
148 * associated
149 * @epf_vf: the virtual EP function to be added
150 *
151 * A physical endpoint function can be associated with multiple virtual
152 * endpoint functions. Invoke pci_epf_add_epf() to add a virtual PCI endpoint
153 * function to a physical PCI endpoint function.
154 */
155int pci_epf_add_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
156{
157 u32 vfunc_no;
158
159 if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
160 return -EINVAL;
161
162 if (epf_pf->epc || epf_vf->epc || epf_vf->epf_pf)
163 return -EBUSY;
164
165 if (epf_pf->sec_epc || epf_vf->sec_epc)
166 return -EBUSY;
167
168 mutex_lock(&epf_pf->lock);
169 vfunc_no = find_first_zero_bit(&epf_pf->vfunction_num_map,
170 BITS_PER_LONG);
171 if (vfunc_no >= BITS_PER_LONG) {
172 mutex_unlock(&epf_pf->lock);
173 return -EINVAL;
174 }
175
176 set_bit(vfunc_no, &epf_pf->vfunction_num_map);
177 epf_vf->vfunc_no = vfunc_no;
178
179 epf_vf->epf_pf = epf_pf;
180 epf_vf->is_vf = true;
181
182 list_add_tail(&epf_vf->list, &epf_pf->pci_vepf);
183 mutex_unlock(&epf_pf->lock);
184
185 return 0;
186}
187EXPORT_SYMBOL_GPL(pci_epf_add_vepf);
188
189/**
190 * pci_epf_remove_vepf() - remove virtual EP function from physical EP function
191 * @epf_pf: the physical EP function from which the virtual EP function should
192 * be removed
193 * @epf_vf: the virtual EP function to be removed
194 *
195 * Invoke to remove a virtual endpoint function from the physical endpoint
196 * function.
197 */
198void pci_epf_remove_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
199{
200 if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
201 return;
202
203 mutex_lock(&epf_pf->lock);
204 clear_bit(epf_vf->vfunc_no, &epf_pf->vfunction_num_map);
205 epf_vf->epf_pf = NULL;
206 list_del(&epf_vf->list);
207 mutex_unlock(&epf_pf->lock);
208}
209EXPORT_SYMBOL_GPL(pci_epf_remove_vepf);
210
211/**
212 * pci_epf_free_space() - free the allocated PCI EPF register space
213 * @epf: the EPF device from whom to free the memory
214 * @addr: the virtual address of the PCI EPF register space
215 * @bar: the BAR number corresponding to the register space
216 * @type: Identifies if the allocated space is for primary EPC or secondary EPC
217 *
218 * Invoke to free the allocated PCI EPF register space.
219 */
220void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar,
221 enum pci_epc_interface_type type)
222{
223 struct device *dev;
224 struct pci_epf_bar *epf_bar;
225 struct pci_epc *epc;
226
227 if (!addr)
228 return;
229
230 if (type == PRIMARY_INTERFACE) {
231 epc = epf->epc;
232 epf_bar = epf->bar;
233 } else {
234 epc = epf->sec_epc;
235 epf_bar = epf->sec_epc_bar;
236 }
237
238 dev = epc->dev.parent;
239 dma_free_coherent(dev, epf_bar[bar].size, addr,
240 epf_bar[bar].phys_addr);
241
242 epf_bar[bar].phys_addr = 0;
243 epf_bar[bar].addr = NULL;
244 epf_bar[bar].size = 0;
245 epf_bar[bar].barno = 0;
246 epf_bar[bar].flags = 0;
247}
248EXPORT_SYMBOL_GPL(pci_epf_free_space);
249
250/**
251 * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
252 * @epf: the EPF device to whom allocate the memory
253 * @size: the size of the memory that has to be allocated
254 * @bar: the BAR number corresponding to the allocated register space
255 * @epc_features: the features provided by the EPC specific to this EPF
256 * @type: Identifies if the allocation is for primary EPC or secondary EPC
257 *
258 * Invoke to allocate memory for the PCI EPF register space.
259 * Flag PCI_BASE_ADDRESS_MEM_TYPE_64 will automatically get set if the BAR
260 * can only be a 64-bit BAR, or if the requested size is larger than 2 GB.
261 */
262void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
263 const struct pci_epc_features *epc_features,
264 enum pci_epc_interface_type type)
265{
266 u64 bar_fixed_size = epc_features->bar[bar].fixed_size;
267 size_t align = epc_features->align;
268 struct pci_epf_bar *epf_bar;
269 dma_addr_t phys_addr;
270 struct pci_epc *epc;
271 struct device *dev;
272 void *space;
273
274 if (size < 128)
275 size = 128;
276
277 if (epc_features->bar[bar].type == BAR_FIXED && bar_fixed_size) {
278 if (size > bar_fixed_size) {
279 dev_err(&epf->dev,
280 "requested BAR size is larger than fixed size\n");
281 return NULL;
282 }
283 size = bar_fixed_size;
284 }
285
286 if (align)
287 size = ALIGN(size, align);
288 else
289 size = roundup_pow_of_two(size);
290
291 if (type == PRIMARY_INTERFACE) {
292 epc = epf->epc;
293 epf_bar = epf->bar;
294 } else {
295 epc = epf->sec_epc;
296 epf_bar = epf->sec_epc_bar;
297 }
298
299 dev = epc->dev.parent;
300 space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
301 if (!space) {
302 dev_err(dev, "failed to allocate mem space\n");
303 return NULL;
304 }
305
306 epf_bar[bar].phys_addr = phys_addr;
307 epf_bar[bar].addr = space;
308 epf_bar[bar].size = size;
309 epf_bar[bar].barno = bar;
310 if (upper_32_bits(size) || epc_features->bar[bar].only_64bit)
311 epf_bar[bar].flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
312 else
313 epf_bar[bar].flags |= PCI_BASE_ADDRESS_MEM_TYPE_32;
314
315 return space;
316}
317EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
318
319static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
320{
321 struct config_group *group, *tmp;
322
323 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
324 return;
325
326 mutex_lock(&pci_epf_mutex);
327 list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
328 pci_ep_cfs_remove_epf_group(group);
329 list_del(&driver->epf_group);
330 mutex_unlock(&pci_epf_mutex);
331}
332
333/**
334 * pci_epf_unregister_driver() - unregister the PCI EPF driver
335 * @driver: the PCI EPF driver that has to be unregistered
336 *
337 * Invoke to unregister the PCI EPF driver.
338 */
339void pci_epf_unregister_driver(struct pci_epf_driver *driver)
340{
341 pci_epf_remove_cfs(driver);
342 driver_unregister(&driver->driver);
343}
344EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
345
346static int pci_epf_add_cfs(struct pci_epf_driver *driver)
347{
348 struct config_group *group;
349 const struct pci_epf_device_id *id;
350
351 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
352 return 0;
353
354 INIT_LIST_HEAD(&driver->epf_group);
355
356 id = driver->id_table;
357 while (id->name[0]) {
358 group = pci_ep_cfs_add_epf_group(id->name);
359 if (IS_ERR(group)) {
360 pci_epf_remove_cfs(driver);
361 return PTR_ERR(group);
362 }
363
364 mutex_lock(&pci_epf_mutex);
365 list_add_tail(&group->group_entry, &driver->epf_group);
366 mutex_unlock(&pci_epf_mutex);
367 id++;
368 }
369
370 return 0;
371}
372
373/**
374 * __pci_epf_register_driver() - register a new PCI EPF driver
375 * @driver: structure representing PCI EPF driver
376 * @owner: the owner of the module that registers the PCI EPF driver
377 *
378 * Invoke to register a new PCI EPF driver.
379 */
380int __pci_epf_register_driver(struct pci_epf_driver *driver,
381 struct module *owner)
382{
383 int ret;
384
385 if (!driver->ops)
386 return -EINVAL;
387
388 if (!driver->ops->bind || !driver->ops->unbind)
389 return -EINVAL;
390
391 driver->driver.bus = &pci_epf_bus_type;
392 driver->driver.owner = owner;
393
394 ret = driver_register(&driver->driver);
395 if (ret)
396 return ret;
397
398 pci_epf_add_cfs(driver);
399
400 return 0;
401}
402EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
403
404/**
405 * pci_epf_destroy() - destroy the created PCI EPF device
406 * @epf: the PCI EPF device that has to be destroyed.
407 *
408 * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
409 */
410void pci_epf_destroy(struct pci_epf *epf)
411{
412 device_unregister(&epf->dev);
413}
414EXPORT_SYMBOL_GPL(pci_epf_destroy);
415
416/**
417 * pci_epf_create() - create a new PCI EPF device
418 * @name: the name of the PCI EPF device. This name will be used to bind the
419 * EPF device to a EPF driver
420 *
421 * Invoke to create a new PCI EPF device by providing the name of the function
422 * device.
423 */
424struct pci_epf *pci_epf_create(const char *name)
425{
426 int ret;
427 struct pci_epf *epf;
428 struct device *dev;
429 int len;
430
431 epf = kzalloc(sizeof(*epf), GFP_KERNEL);
432 if (!epf)
433 return ERR_PTR(-ENOMEM);
434
435 len = strchrnul(name, '.') - name;
436 epf->name = kstrndup(name, len, GFP_KERNEL);
437 if (!epf->name) {
438 kfree(epf);
439 return ERR_PTR(-ENOMEM);
440 }
441
442 /* VFs are numbered starting with 1. So set BIT(0) by default */
443 epf->vfunction_num_map = 1;
444 INIT_LIST_HEAD(&epf->pci_vepf);
445
446 dev = &epf->dev;
447 device_initialize(dev);
448 dev->bus = &pci_epf_bus_type;
449 dev->type = &pci_epf_type;
450 mutex_init(&epf->lock);
451
452 ret = dev_set_name(dev, "%s", name);
453 if (ret) {
454 put_device(dev);
455 return ERR_PTR(ret);
456 }
457
458 ret = device_add(dev);
459 if (ret) {
460 put_device(dev);
461 return ERR_PTR(ret);
462 }
463
464 return epf;
465}
466EXPORT_SYMBOL_GPL(pci_epf_create);
467
468static void pci_epf_dev_release(struct device *dev)
469{
470 struct pci_epf *epf = to_pci_epf(dev);
471
472 kfree(epf->name);
473 kfree(epf);
474}
475
476static const struct device_type pci_epf_type = {
477 .release = pci_epf_dev_release,
478};
479
480static const struct pci_epf_device_id *
481pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
482{
483 while (id->name[0]) {
484 if (strcmp(epf->name, id->name) == 0)
485 return id;
486 id++;
487 }
488
489 return NULL;
490}
491
492static int pci_epf_device_match(struct device *dev, const struct device_driver *drv)
493{
494 struct pci_epf *epf = to_pci_epf(dev);
495 const struct pci_epf_driver *driver = to_pci_epf_driver(drv);
496
497 if (driver->id_table)
498 return !!pci_epf_match_id(driver->id_table, epf);
499
500 return !strcmp(epf->name, drv->name);
501}
502
503static int pci_epf_device_probe(struct device *dev)
504{
505 struct pci_epf *epf = to_pci_epf(dev);
506 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
507
508 if (!driver->probe)
509 return -ENODEV;
510
511 epf->driver = driver;
512
513 return driver->probe(epf, pci_epf_match_id(driver->id_table, epf));
514}
515
516static void pci_epf_device_remove(struct device *dev)
517{
518 struct pci_epf *epf = to_pci_epf(dev);
519 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
520
521 if (driver->remove)
522 driver->remove(epf);
523 epf->driver = NULL;
524}
525
526static const struct bus_type pci_epf_bus_type = {
527 .name = "pci-epf",
528 .match = pci_epf_device_match,
529 .probe = pci_epf_device_probe,
530 .remove = pci_epf_device_remove,
531};
532
533static int __init pci_epf_init(void)
534{
535 int ret;
536
537 ret = bus_register(&pci_epf_bus_type);
538 if (ret) {
539 pr_err("failed to register pci epf bus --> %d\n", ret);
540 return ret;
541 }
542
543 return 0;
544}
545module_init(pci_epf_init);
546
547static void __exit pci_epf_exit(void)
548{
549 bus_unregister(&pci_epf_bus_type);
550}
551module_exit(pci_epf_exit);
552
553MODULE_DESCRIPTION("PCI EPF Library");
554MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");