0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/device.h>
0010 #include <linux/dma-mapping.h>
0011 #include <linux/slab.h>
0012 #include <linux/module.h>
0013
0014 #include <linux/pci-epc.h>
0015 #include <linux/pci-epf.h>
0016 #include <linux/pci-ep-cfs.h>
0017
0018 static DEFINE_MUTEX(pci_epf_mutex);
0019
0020 static struct bus_type pci_epf_bus_type;
0021 static const struct device_type pci_epf_type;
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 struct config_group *pci_epf_type_add_cfs(struct pci_epf *epf,
0035 struct config_group *group)
0036 {
0037 struct config_group *epf_type_group;
0038
0039 if (!epf->driver) {
0040 dev_err(&epf->dev, "epf device not bound to driver\n");
0041 return NULL;
0042 }
0043
0044 if (!epf->driver->ops->add_cfs)
0045 return NULL;
0046
0047 mutex_lock(&epf->lock);
0048 epf_type_group = epf->driver->ops->add_cfs(epf, group);
0049 mutex_unlock(&epf->lock);
0050
0051 return epf_type_group;
0052 }
0053 EXPORT_SYMBOL_GPL(pci_epf_type_add_cfs);
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 void pci_epf_unbind(struct pci_epf *epf)
0064 {
0065 struct pci_epf *epf_vf;
0066
0067 if (!epf->driver) {
0068 dev_WARN(&epf->dev, "epf device not bound to driver\n");
0069 return;
0070 }
0071
0072 mutex_lock(&epf->lock);
0073 list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
0074 if (epf_vf->is_bound)
0075 epf_vf->driver->ops->unbind(epf_vf);
0076 }
0077 if (epf->is_bound)
0078 epf->driver->ops->unbind(epf);
0079 mutex_unlock(&epf->lock);
0080 module_put(epf->driver->owner);
0081 }
0082 EXPORT_SYMBOL_GPL(pci_epf_unbind);
0083
0084
0085
0086
0087
0088
0089
0090
0091 int pci_epf_bind(struct pci_epf *epf)
0092 {
0093 struct device *dev = &epf->dev;
0094 struct pci_epf *epf_vf;
0095 u8 func_no, vfunc_no;
0096 struct pci_epc *epc;
0097 int ret;
0098
0099 if (!epf->driver) {
0100 dev_WARN(dev, "epf device not bound to driver\n");
0101 return -EINVAL;
0102 }
0103
0104 if (!try_module_get(epf->driver->owner))
0105 return -EAGAIN;
0106
0107 mutex_lock(&epf->lock);
0108 list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
0109 vfunc_no = epf_vf->vfunc_no;
0110
0111 if (vfunc_no < 1) {
0112 dev_err(dev, "Invalid virtual function number\n");
0113 ret = -EINVAL;
0114 goto ret;
0115 }
0116
0117 epc = epf->epc;
0118 func_no = epf->func_no;
0119 if (!IS_ERR_OR_NULL(epc)) {
0120 if (!epc->max_vfs) {
0121 dev_err(dev, "No support for virt function\n");
0122 ret = -EINVAL;
0123 goto ret;
0124 }
0125
0126 if (vfunc_no > epc->max_vfs[func_no]) {
0127 dev_err(dev, "PF%d: Exceeds max vfunc number\n",
0128 func_no);
0129 ret = -EINVAL;
0130 goto ret;
0131 }
0132 }
0133
0134 epc = epf->sec_epc;
0135 func_no = epf->sec_epc_func_no;
0136 if (!IS_ERR_OR_NULL(epc)) {
0137 if (!epc->max_vfs) {
0138 dev_err(dev, "No support for virt function\n");
0139 ret = -EINVAL;
0140 goto ret;
0141 }
0142
0143 if (vfunc_no > epc->max_vfs[func_no]) {
0144 dev_err(dev, "PF%d: Exceeds max vfunc number\n",
0145 func_no);
0146 ret = -EINVAL;
0147 goto ret;
0148 }
0149 }
0150
0151 epf_vf->func_no = epf->func_no;
0152 epf_vf->sec_epc_func_no = epf->sec_epc_func_no;
0153 epf_vf->epc = epf->epc;
0154 epf_vf->sec_epc = epf->sec_epc;
0155 ret = epf_vf->driver->ops->bind(epf_vf);
0156 if (ret)
0157 goto ret;
0158 epf_vf->is_bound = true;
0159 }
0160
0161 ret = epf->driver->ops->bind(epf);
0162 if (ret)
0163 goto ret;
0164 epf->is_bound = true;
0165
0166 mutex_unlock(&epf->lock);
0167 return 0;
0168
0169 ret:
0170 mutex_unlock(&epf->lock);
0171 pci_epf_unbind(epf);
0172
0173 return ret;
0174 }
0175 EXPORT_SYMBOL_GPL(pci_epf_bind);
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187 int pci_epf_add_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
0188 {
0189 u32 vfunc_no;
0190
0191 if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
0192 return -EINVAL;
0193
0194 if (epf_pf->epc || epf_vf->epc || epf_vf->epf_pf)
0195 return -EBUSY;
0196
0197 if (epf_pf->sec_epc || epf_vf->sec_epc)
0198 return -EBUSY;
0199
0200 mutex_lock(&epf_pf->lock);
0201 vfunc_no = find_first_zero_bit(&epf_pf->vfunction_num_map,
0202 BITS_PER_LONG);
0203 if (vfunc_no >= BITS_PER_LONG) {
0204 mutex_unlock(&epf_pf->lock);
0205 return -EINVAL;
0206 }
0207
0208 set_bit(vfunc_no, &epf_pf->vfunction_num_map);
0209 epf_vf->vfunc_no = vfunc_no;
0210
0211 epf_vf->epf_pf = epf_pf;
0212 epf_vf->is_vf = true;
0213
0214 list_add_tail(&epf_vf->list, &epf_pf->pci_vepf);
0215 mutex_unlock(&epf_pf->lock);
0216
0217 return 0;
0218 }
0219 EXPORT_SYMBOL_GPL(pci_epf_add_vepf);
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230 void pci_epf_remove_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
0231 {
0232 if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
0233 return;
0234
0235 mutex_lock(&epf_pf->lock);
0236 clear_bit(epf_vf->vfunc_no, &epf_pf->vfunction_num_map);
0237 list_del(&epf_vf->list);
0238 mutex_unlock(&epf_pf->lock);
0239 }
0240 EXPORT_SYMBOL_GPL(pci_epf_remove_vepf);
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251 void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar,
0252 enum pci_epc_interface_type type)
0253 {
0254 struct device *dev;
0255 struct pci_epf_bar *epf_bar;
0256 struct pci_epc *epc;
0257
0258 if (!addr)
0259 return;
0260
0261 if (type == PRIMARY_INTERFACE) {
0262 epc = epf->epc;
0263 epf_bar = epf->bar;
0264 } else {
0265 epc = epf->sec_epc;
0266 epf_bar = epf->sec_epc_bar;
0267 }
0268
0269 dev = epc->dev.parent;
0270 dma_free_coherent(dev, epf_bar[bar].size, addr,
0271 epf_bar[bar].phys_addr);
0272
0273 epf_bar[bar].phys_addr = 0;
0274 epf_bar[bar].addr = NULL;
0275 epf_bar[bar].size = 0;
0276 epf_bar[bar].barno = 0;
0277 epf_bar[bar].flags = 0;
0278 }
0279 EXPORT_SYMBOL_GPL(pci_epf_free_space);
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291 void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
0292 size_t align, enum pci_epc_interface_type type)
0293 {
0294 struct pci_epf_bar *epf_bar;
0295 dma_addr_t phys_addr;
0296 struct pci_epc *epc;
0297 struct device *dev;
0298 void *space;
0299
0300 if (size < 128)
0301 size = 128;
0302
0303 if (align)
0304 size = ALIGN(size, align);
0305 else
0306 size = roundup_pow_of_two(size);
0307
0308 if (type == PRIMARY_INTERFACE) {
0309 epc = epf->epc;
0310 epf_bar = epf->bar;
0311 } else {
0312 epc = epf->sec_epc;
0313 epf_bar = epf->sec_epc_bar;
0314 }
0315
0316 dev = epc->dev.parent;
0317 space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
0318 if (!space) {
0319 dev_err(dev, "failed to allocate mem space\n");
0320 return NULL;
0321 }
0322
0323 epf_bar[bar].phys_addr = phys_addr;
0324 epf_bar[bar].addr = space;
0325 epf_bar[bar].size = size;
0326 epf_bar[bar].barno = bar;
0327 epf_bar[bar].flags |= upper_32_bits(size) ?
0328 PCI_BASE_ADDRESS_MEM_TYPE_64 :
0329 PCI_BASE_ADDRESS_MEM_TYPE_32;
0330
0331 return space;
0332 }
0333 EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
0334
0335 static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
0336 {
0337 struct config_group *group, *tmp;
0338
0339 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
0340 return;
0341
0342 mutex_lock(&pci_epf_mutex);
0343 list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
0344 pci_ep_cfs_remove_epf_group(group);
0345 list_del(&driver->epf_group);
0346 mutex_unlock(&pci_epf_mutex);
0347 }
0348
0349
0350
0351
0352
0353
0354
0355 void pci_epf_unregister_driver(struct pci_epf_driver *driver)
0356 {
0357 pci_epf_remove_cfs(driver);
0358 driver_unregister(&driver->driver);
0359 }
0360 EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
0361
0362 static int pci_epf_add_cfs(struct pci_epf_driver *driver)
0363 {
0364 struct config_group *group;
0365 const struct pci_epf_device_id *id;
0366
0367 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
0368 return 0;
0369
0370 INIT_LIST_HEAD(&driver->epf_group);
0371
0372 id = driver->id_table;
0373 while (id->name[0]) {
0374 group = pci_ep_cfs_add_epf_group(id->name);
0375 if (IS_ERR(group)) {
0376 pci_epf_remove_cfs(driver);
0377 return PTR_ERR(group);
0378 }
0379
0380 mutex_lock(&pci_epf_mutex);
0381 list_add_tail(&group->group_entry, &driver->epf_group);
0382 mutex_unlock(&pci_epf_mutex);
0383 id++;
0384 }
0385
0386 return 0;
0387 }
0388
0389
0390
0391
0392
0393
0394
0395
0396 int __pci_epf_register_driver(struct pci_epf_driver *driver,
0397 struct module *owner)
0398 {
0399 int ret;
0400
0401 if (!driver->ops)
0402 return -EINVAL;
0403
0404 if (!driver->ops->bind || !driver->ops->unbind)
0405 return -EINVAL;
0406
0407 driver->driver.bus = &pci_epf_bus_type;
0408 driver->driver.owner = owner;
0409
0410 ret = driver_register(&driver->driver);
0411 if (ret)
0412 return ret;
0413
0414 pci_epf_add_cfs(driver);
0415
0416 return 0;
0417 }
0418 EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
0419
0420
0421
0422
0423
0424
0425
0426 void pci_epf_destroy(struct pci_epf *epf)
0427 {
0428 device_unregister(&epf->dev);
0429 }
0430 EXPORT_SYMBOL_GPL(pci_epf_destroy);
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440 struct pci_epf *pci_epf_create(const char *name)
0441 {
0442 int ret;
0443 struct pci_epf *epf;
0444 struct device *dev;
0445 int len;
0446
0447 epf = kzalloc(sizeof(*epf), GFP_KERNEL);
0448 if (!epf)
0449 return ERR_PTR(-ENOMEM);
0450
0451 len = strchrnul(name, '.') - name;
0452 epf->name = kstrndup(name, len, GFP_KERNEL);
0453 if (!epf->name) {
0454 kfree(epf);
0455 return ERR_PTR(-ENOMEM);
0456 }
0457
0458
0459 epf->vfunction_num_map = 1;
0460 INIT_LIST_HEAD(&epf->pci_vepf);
0461
0462 dev = &epf->dev;
0463 device_initialize(dev);
0464 dev->bus = &pci_epf_bus_type;
0465 dev->type = &pci_epf_type;
0466 mutex_init(&epf->lock);
0467
0468 ret = dev_set_name(dev, "%s", name);
0469 if (ret) {
0470 put_device(dev);
0471 return ERR_PTR(ret);
0472 }
0473
0474 ret = device_add(dev);
0475 if (ret) {
0476 put_device(dev);
0477 return ERR_PTR(ret);
0478 }
0479
0480 return epf;
0481 }
0482 EXPORT_SYMBOL_GPL(pci_epf_create);
0483
0484 static void pci_epf_dev_release(struct device *dev)
0485 {
0486 struct pci_epf *epf = to_pci_epf(dev);
0487
0488 kfree(epf->name);
0489 kfree(epf);
0490 }
0491
0492 static const struct device_type pci_epf_type = {
0493 .release = pci_epf_dev_release,
0494 };
0495
0496 static int
0497 pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
0498 {
0499 while (id->name[0]) {
0500 if (strcmp(epf->name, id->name) == 0)
0501 return true;
0502 id++;
0503 }
0504
0505 return false;
0506 }
0507
0508 static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
0509 {
0510 struct pci_epf *epf = to_pci_epf(dev);
0511 struct pci_epf_driver *driver = to_pci_epf_driver(drv);
0512
0513 if (driver->id_table)
0514 return pci_epf_match_id(driver->id_table, epf);
0515
0516 return !strcmp(epf->name, drv->name);
0517 }
0518
0519 static int pci_epf_device_probe(struct device *dev)
0520 {
0521 struct pci_epf *epf = to_pci_epf(dev);
0522 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
0523
0524 if (!driver->probe)
0525 return -ENODEV;
0526
0527 epf->driver = driver;
0528
0529 return driver->probe(epf);
0530 }
0531
0532 static void pci_epf_device_remove(struct device *dev)
0533 {
0534 struct pci_epf *epf = to_pci_epf(dev);
0535 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
0536
0537 if (driver->remove)
0538 driver->remove(epf);
0539 epf->driver = NULL;
0540 }
0541
0542 static struct bus_type pci_epf_bus_type = {
0543 .name = "pci-epf",
0544 .match = pci_epf_device_match,
0545 .probe = pci_epf_device_probe,
0546 .remove = pci_epf_device_remove,
0547 };
0548
0549 static int __init pci_epf_init(void)
0550 {
0551 int ret;
0552
0553 ret = bus_register(&pci_epf_bus_type);
0554 if (ret) {
0555 pr_err("failed to register pci epf bus --> %d\n", ret);
0556 return ret;
0557 }
0558
0559 return 0;
0560 }
0561 module_init(pci_epf_init);
0562
0563 static void __exit pci_epf_exit(void)
0564 {
0565 bus_unregister(&pci_epf_bus_type);
0566 }
0567 module_exit(pci_epf_exit);
0568
0569 MODULE_DESCRIPTION("PCI EPF Library");
0570 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
0571 MODULE_LICENSE("GPL v2");