2 * PCI Endpoint *Function* (EPF) library
4 * Copyright (C) 2017 Texas Instruments
5 * Author: Kishon Vijay Abraham I <kishon@ti.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 of
9 * the License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/device.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
25 #include <linux/pci-epc.h>
26 #include <linux/pci-epf.h>
27 #include <linux/pci-ep-cfs.h>
29 static struct bus_type pci_epf_bus_type
;
30 static const struct device_type pci_epf_type
;
33 * pci_epf_linkup() - Notify the function driver that EPC device has
34 * established a connection with the Root Complex.
35 * @epf: the EPF device bound to the EPC device which has established
36 * the connection with the host
38 * Invoke to notify the function driver that EPC device has established
39 * a connection with the Root Complex.
41 void pci_epf_linkup(struct pci_epf
*epf
)
44 dev_WARN(&epf
->dev
, "epf device not bound to driver\n");
48 epf
->driver
->ops
->linkup(epf
);
50 EXPORT_SYMBOL_GPL(pci_epf_linkup
);
53 * pci_epf_unbind() - Notify the function driver that the binding between the
54 * EPF device and EPC device has been lost
55 * @epf: the EPF device which has lost the binding with the EPC device
57 * Invoke to notify the function driver that the binding between the EPF device
58 * and EPC device has been lost.
60 void pci_epf_unbind(struct pci_epf
*epf
)
63 dev_WARN(&epf
->dev
, "epf device not bound to driver\n");
67 epf
->driver
->ops
->unbind(epf
);
68 module_put(epf
->driver
->owner
);
70 EXPORT_SYMBOL_GPL(pci_epf_unbind
);
73 * pci_epf_bind() - Notify the function driver that the EPF device has been
74 * bound to a EPC device
75 * @epf: the EPF device which has been bound to the EPC device
77 * Invoke to notify the function driver that it has been bound to a EPC device
79 int pci_epf_bind(struct pci_epf
*epf
)
82 dev_WARN(&epf
->dev
, "epf device not bound to driver\n");
86 if (!try_module_get(epf
->driver
->owner
))
89 return epf
->driver
->ops
->bind(epf
);
91 EXPORT_SYMBOL_GPL(pci_epf_bind
);
94 * pci_epf_free_space() - free the allocated PCI EPF register space
95 * @addr: the virtual address of the PCI EPF register space
96 * @bar: the BAR number corresponding to the register space
98 * Invoke to free the allocated PCI EPF register space.
100 void pci_epf_free_space(struct pci_epf
*epf
, void *addr
, enum pci_barno bar
)
102 struct device
*dev
= &epf
->dev
;
107 dma_free_coherent(dev
, epf
->bar
[bar
].size
, addr
,
108 epf
->bar
[bar
].phys_addr
);
110 epf
->bar
[bar
].phys_addr
= 0;
111 epf
->bar
[bar
].size
= 0;
113 EXPORT_SYMBOL_GPL(pci_epf_free_space
);
116 * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
117 * @size: the size of the memory that has to be allocated
118 * @bar: the BAR number corresponding to the allocated register space
120 * Invoke to allocate memory for the PCI EPF register space.
122 void *pci_epf_alloc_space(struct pci_epf
*epf
, size_t size
, enum pci_barno bar
)
125 struct device
*dev
= &epf
->dev
;
126 dma_addr_t phys_addr
;
130 size
= roundup_pow_of_two(size
);
132 space
= dma_alloc_coherent(dev
, size
, &phys_addr
, GFP_KERNEL
);
134 dev_err(dev
, "failed to allocate mem space\n");
138 epf
->bar
[bar
].phys_addr
= phys_addr
;
139 epf
->bar
[bar
].size
= size
;
143 EXPORT_SYMBOL_GPL(pci_epf_alloc_space
);
146 * pci_epf_unregister_driver() - unregister the PCI EPF driver
147 * @driver: the PCI EPF driver that has to be unregistered
149 * Invoke to unregister the PCI EPF driver.
151 void pci_epf_unregister_driver(struct pci_epf_driver
*driver
)
153 pci_ep_cfs_remove_epf_group(driver
->group
);
154 driver_unregister(&driver
->driver
);
156 EXPORT_SYMBOL_GPL(pci_epf_unregister_driver
);
159 * __pci_epf_register_driver() - register a new PCI EPF driver
160 * @driver: structure representing PCI EPF driver
161 * @owner: the owner of the module that registers the PCI EPF driver
163 * Invoke to register a new PCI EPF driver.
165 int __pci_epf_register_driver(struct pci_epf_driver
*driver
,
166 struct module
*owner
)
173 if (!driver
->ops
->bind
|| !driver
->ops
->unbind
|| !driver
->ops
->linkup
)
176 driver
->driver
.bus
= &pci_epf_bus_type
;
177 driver
->driver
.owner
= owner
;
179 ret
= driver_register(&driver
->driver
);
183 driver
->group
= pci_ep_cfs_add_epf_group(driver
->driver
.name
);
187 EXPORT_SYMBOL_GPL(__pci_epf_register_driver
);
190 * pci_epf_destroy() - destroy the created PCI EPF device
191 * @epf: the PCI EPF device that has to be destroyed.
193 * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
195 void pci_epf_destroy(struct pci_epf
*epf
)
197 device_unregister(&epf
->dev
);
199 EXPORT_SYMBOL_GPL(pci_epf_destroy
);
202 * pci_epf_create() - create a new PCI EPF device
203 * @name: the name of the PCI EPF device. This name will be used to bind the
204 * the EPF device to a EPF driver
206 * Invoke to create a new PCI EPF device by providing the name of the function
209 struct pci_epf
*pci_epf_create(const char *name
)
217 epf
= kzalloc(sizeof(*epf
), GFP_KERNEL
);
223 buf
= kstrdup(name
, GFP_KERNEL
);
230 buf
= strchrnul(buf
, '.');
233 epf
->name
= kstrdup(func_name
, GFP_KERNEL
);
240 device_initialize(dev
);
241 dev
->bus
= &pci_epf_bus_type
;
242 dev
->type
= &pci_epf_type
;
244 ret
= dev_set_name(dev
, "%s", name
);
248 ret
= device_add(dev
);
268 EXPORT_SYMBOL_GPL(pci_epf_create
);
270 const struct pci_epf_device_id
*
271 pci_epf_match_device(const struct pci_epf_device_id
*id
, struct pci_epf
*epf
)
277 if (strcmp(epf
->name
, id
->name
) == 0)
284 EXPORT_SYMBOL_GPL(pci_epf_match_device
);
286 static void pci_epf_dev_release(struct device
*dev
)
288 struct pci_epf
*epf
= to_pci_epf(dev
);
294 static const struct device_type pci_epf_type
= {
295 .release
= pci_epf_dev_release
,
299 pci_epf_match_id(const struct pci_epf_device_id
*id
, const struct pci_epf
*epf
)
301 while (id
->name
[0]) {
302 if (strcmp(epf
->name
, id
->name
) == 0)
310 static int pci_epf_device_match(struct device
*dev
, struct device_driver
*drv
)
312 struct pci_epf
*epf
= to_pci_epf(dev
);
313 struct pci_epf_driver
*driver
= to_pci_epf_driver(drv
);
315 if (driver
->id_table
)
316 return pci_epf_match_id(driver
->id_table
, epf
);
318 return !strcmp(epf
->name
, drv
->name
);
321 static int pci_epf_device_probe(struct device
*dev
)
323 struct pci_epf
*epf
= to_pci_epf(dev
);
324 struct pci_epf_driver
*driver
= to_pci_epf_driver(dev
->driver
);
329 epf
->driver
= driver
;
331 return driver
->probe(epf
);
334 static int pci_epf_device_remove(struct device
*dev
)
337 struct pci_epf
*epf
= to_pci_epf(dev
);
338 struct pci_epf_driver
*driver
= to_pci_epf_driver(dev
->driver
);
341 ret
= driver
->remove(epf
);
347 static struct bus_type pci_epf_bus_type
= {
349 .match
= pci_epf_device_match
,
350 .probe
= pci_epf_device_probe
,
351 .remove
= pci_epf_device_remove
,
354 static int __init
pci_epf_init(void)
358 ret
= bus_register(&pci_epf_bus_type
);
360 pr_err("failed to register pci epf bus --> %d\n", ret
);
366 module_init(pci_epf_init
);
368 static void __exit
pci_epf_exit(void)
370 bus_unregister(&pci_epf_bus_type
);
372 module_exit(pci_epf_exit
);
374 MODULE_DESCRIPTION("PCI EPF Library");
375 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
376 MODULE_LICENSE("GPL v2");