pci-epf-core.c 8.6 KB

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