pci-epf-core.c 8.0 KB

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