pci-epf-core.c 8.8 KB

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