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