pci-epf-core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 const struct bus_type pci_epf_bus_type;
  17. static const struct device_type pci_epf_type;
  18. /**
  19. * pci_epf_unbind() - Notify the function driver that the binding between the
  20. * EPF device and EPC device has been lost
  21. * @epf: the EPF device which has lost the binding with the EPC device
  22. *
  23. * Invoke to notify the function driver that the binding between the EPF device
  24. * and EPC device has been lost.
  25. */
  26. void pci_epf_unbind(struct pci_epf *epf)
  27. {
  28. struct pci_epf *epf_vf;
  29. if (!epf->driver) {
  30. dev_WARN(&epf->dev, "epf device not bound to driver\n");
  31. return;
  32. }
  33. mutex_lock(&epf->lock);
  34. list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
  35. if (epf_vf->is_bound)
  36. epf_vf->driver->ops->unbind(epf_vf);
  37. }
  38. if (epf->is_bound)
  39. epf->driver->ops->unbind(epf);
  40. mutex_unlock(&epf->lock);
  41. module_put(epf->driver->owner);
  42. }
  43. EXPORT_SYMBOL_GPL(pci_epf_unbind);
  44. /**
  45. * pci_epf_bind() - Notify the function driver that the EPF device has been
  46. * bound to a EPC device
  47. * @epf: the EPF device which has been bound to the EPC device
  48. *
  49. * Invoke to notify the function driver that it has been bound to a EPC device
  50. */
  51. int pci_epf_bind(struct pci_epf *epf)
  52. {
  53. struct device *dev = &epf->dev;
  54. struct pci_epf *epf_vf;
  55. u8 func_no, vfunc_no;
  56. struct pci_epc *epc;
  57. int ret;
  58. if (!epf->driver) {
  59. dev_WARN(dev, "epf device not bound to driver\n");
  60. return -EINVAL;
  61. }
  62. if (!try_module_get(epf->driver->owner))
  63. return -EAGAIN;
  64. mutex_lock(&epf->lock);
  65. list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
  66. vfunc_no = epf_vf->vfunc_no;
  67. if (vfunc_no < 1) {
  68. dev_err(dev, "Invalid virtual function number\n");
  69. ret = -EINVAL;
  70. goto ret;
  71. }
  72. epc = epf->epc;
  73. func_no = epf->func_no;
  74. if (!IS_ERR_OR_NULL(epc)) {
  75. if (!epc->max_vfs) {
  76. dev_err(dev, "No support for virt function\n");
  77. ret = -EINVAL;
  78. goto ret;
  79. }
  80. if (vfunc_no > epc->max_vfs[func_no]) {
  81. dev_err(dev, "PF%d: Exceeds max vfunc number\n",
  82. func_no);
  83. ret = -EINVAL;
  84. goto ret;
  85. }
  86. }
  87. epc = epf->sec_epc;
  88. func_no = epf->sec_epc_func_no;
  89. if (!IS_ERR_OR_NULL(epc)) {
  90. if (!epc->max_vfs) {
  91. dev_err(dev, "No support for virt function\n");
  92. ret = -EINVAL;
  93. goto ret;
  94. }
  95. if (vfunc_no > epc->max_vfs[func_no]) {
  96. dev_err(dev, "PF%d: Exceeds max vfunc number\n",
  97. func_no);
  98. ret = -EINVAL;
  99. goto ret;
  100. }
  101. }
  102. epf_vf->func_no = epf->func_no;
  103. epf_vf->sec_epc_func_no = epf->sec_epc_func_no;
  104. epf_vf->epc = epf->epc;
  105. epf_vf->sec_epc = epf->sec_epc;
  106. ret = epf_vf->driver->ops->bind(epf_vf);
  107. if (ret)
  108. goto ret;
  109. epf_vf->is_bound = true;
  110. }
  111. ret = epf->driver->ops->bind(epf);
  112. if (ret)
  113. goto ret;
  114. epf->is_bound = true;
  115. mutex_unlock(&epf->lock);
  116. return 0;
  117. ret:
  118. mutex_unlock(&epf->lock);
  119. pci_epf_unbind(epf);
  120. return ret;
  121. }
  122. EXPORT_SYMBOL_GPL(pci_epf_bind);
  123. /**
  124. * pci_epf_add_vepf() - associate virtual EP function to physical EP function
  125. * @epf_pf: the physical EP function to which the virtual EP function should be
  126. * associated
  127. * @epf_vf: the virtual EP function to be added
  128. *
  129. * A physical endpoint function can be associated with multiple virtual
  130. * endpoint functions. Invoke pci_epf_add_epf() to add a virtual PCI endpoint
  131. * function to a physical PCI endpoint function.
  132. */
  133. int pci_epf_add_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
  134. {
  135. u32 vfunc_no;
  136. if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
  137. return -EINVAL;
  138. if (epf_pf->epc || epf_vf->epc || epf_vf->epf_pf)
  139. return -EBUSY;
  140. if (epf_pf->sec_epc || epf_vf->sec_epc)
  141. return -EBUSY;
  142. mutex_lock(&epf_pf->lock);
  143. vfunc_no = find_first_zero_bit(&epf_pf->vfunction_num_map,
  144. BITS_PER_LONG);
  145. if (vfunc_no >= BITS_PER_LONG) {
  146. mutex_unlock(&epf_pf->lock);
  147. return -EINVAL;
  148. }
  149. set_bit(vfunc_no, &epf_pf->vfunction_num_map);
  150. epf_vf->vfunc_no = vfunc_no;
  151. epf_vf->epf_pf = epf_pf;
  152. epf_vf->is_vf = true;
  153. list_add_tail(&epf_vf->list, &epf_pf->pci_vepf);
  154. mutex_unlock(&epf_pf->lock);
  155. return 0;
  156. }
  157. EXPORT_SYMBOL_GPL(pci_epf_add_vepf);
  158. /**
  159. * pci_epf_remove_vepf() - remove virtual EP function from physical EP function
  160. * @epf_pf: the physical EP function from which the virtual EP function should
  161. * be removed
  162. * @epf_vf: the virtual EP function to be removed
  163. *
  164. * Invoke to remove a virtual endpoint function from the physical endpoint
  165. * function.
  166. */
  167. void pci_epf_remove_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
  168. {
  169. if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
  170. return;
  171. mutex_lock(&epf_pf->lock);
  172. clear_bit(epf_vf->vfunc_no, &epf_pf->vfunction_num_map);
  173. epf_vf->epf_pf = NULL;
  174. list_del(&epf_vf->list);
  175. mutex_unlock(&epf_pf->lock);
  176. }
  177. EXPORT_SYMBOL_GPL(pci_epf_remove_vepf);
  178. /**
  179. * pci_epf_free_space() - free the allocated PCI EPF register space
  180. * @epf: the EPF device from whom to free the memory
  181. * @addr: the virtual address of the PCI EPF register space
  182. * @bar: the BAR number corresponding to the register space
  183. * @type: Identifies if the allocated space is for primary EPC or secondary EPC
  184. *
  185. * Invoke to free the allocated PCI EPF register space.
  186. */
  187. void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar,
  188. enum pci_epc_interface_type type)
  189. {
  190. struct device *dev;
  191. struct pci_epf_bar *epf_bar;
  192. struct pci_epc *epc;
  193. if (!addr)
  194. return;
  195. if (type == PRIMARY_INTERFACE) {
  196. epc = epf->epc;
  197. epf_bar = epf->bar;
  198. } else {
  199. epc = epf->sec_epc;
  200. epf_bar = epf->sec_epc_bar;
  201. }
  202. dev = epc->dev.parent;
  203. dma_free_coherent(dev, epf_bar[bar].size, addr,
  204. epf_bar[bar].phys_addr);
  205. epf_bar[bar].phys_addr = 0;
  206. epf_bar[bar].addr = NULL;
  207. epf_bar[bar].size = 0;
  208. epf_bar[bar].barno = 0;
  209. epf_bar[bar].flags = 0;
  210. }
  211. EXPORT_SYMBOL_GPL(pci_epf_free_space);
  212. /**
  213. * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
  214. * @epf: the EPF device to whom allocate the memory
  215. * @size: the size of the memory that has to be allocated
  216. * @bar: the BAR number corresponding to the allocated register space
  217. * @epc_features: the features provided by the EPC specific to this EPF
  218. * @type: Identifies if the allocation is for primary EPC or secondary EPC
  219. *
  220. * Invoke to allocate memory for the PCI EPF register space.
  221. * Flag PCI_BASE_ADDRESS_MEM_TYPE_64 will automatically get set if the BAR
  222. * can only be a 64-bit BAR, or if the requested size is larger than 2 GB.
  223. */
  224. void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
  225. const struct pci_epc_features *epc_features,
  226. enum pci_epc_interface_type type)
  227. {
  228. u64 bar_fixed_size = epc_features->bar[bar].fixed_size;
  229. size_t align = epc_features->align;
  230. struct pci_epf_bar *epf_bar;
  231. dma_addr_t phys_addr;
  232. struct pci_epc *epc;
  233. struct device *dev;
  234. void *space;
  235. if (size < 128)
  236. size = 128;
  237. if (epc_features->bar[bar].type == BAR_FIXED && bar_fixed_size) {
  238. if (size > bar_fixed_size) {
  239. dev_err(&epf->dev,
  240. "requested BAR size is larger than fixed size\n");
  241. return NULL;
  242. }
  243. size = bar_fixed_size;
  244. }
  245. if (align)
  246. size = ALIGN(size, align);
  247. else
  248. size = roundup_pow_of_two(size);
  249. if (type == PRIMARY_INTERFACE) {
  250. epc = epf->epc;
  251. epf_bar = epf->bar;
  252. } else {
  253. epc = epf->sec_epc;
  254. epf_bar = epf->sec_epc_bar;
  255. }
  256. dev = epc->dev.parent;
  257. space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
  258. if (!space) {
  259. dev_err(dev, "failed to allocate mem space\n");
  260. return NULL;
  261. }
  262. epf_bar[bar].phys_addr = phys_addr;
  263. epf_bar[bar].addr = space;
  264. epf_bar[bar].size = size;
  265. epf_bar[bar].barno = bar;
  266. if (upper_32_bits(size) || epc_features->bar[bar].only_64bit)
  267. epf_bar[bar].flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
  268. else
  269. epf_bar[bar].flags |= PCI_BASE_ADDRESS_MEM_TYPE_32;
  270. return space;
  271. }
  272. EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
  273. static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
  274. {
  275. struct config_group *group, *tmp;
  276. if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
  277. return;
  278. mutex_lock(&pci_epf_mutex);
  279. list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
  280. pci_ep_cfs_remove_epf_group(group);
  281. list_del(&driver->epf_group);
  282. mutex_unlock(&pci_epf_mutex);
  283. }
  284. /**
  285. * pci_epf_unregister_driver() - unregister the PCI EPF driver
  286. * @driver: the PCI EPF driver that has to be unregistered
  287. *
  288. * Invoke to unregister the PCI EPF driver.
  289. */
  290. void pci_epf_unregister_driver(struct pci_epf_driver *driver)
  291. {
  292. pci_epf_remove_cfs(driver);
  293. driver_unregister(&driver->driver);
  294. }
  295. EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
  296. static int pci_epf_add_cfs(struct pci_epf_driver *driver)
  297. {
  298. struct config_group *group;
  299. const struct pci_epf_device_id *id;
  300. if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
  301. return 0;
  302. INIT_LIST_HEAD(&driver->epf_group);
  303. id = driver->id_table;
  304. while (id->name[0]) {
  305. group = pci_ep_cfs_add_epf_group(id->name);
  306. if (IS_ERR(group)) {
  307. pci_epf_remove_cfs(driver);
  308. return PTR_ERR(group);
  309. }
  310. mutex_lock(&pci_epf_mutex);
  311. list_add_tail(&group->group_entry, &driver->epf_group);
  312. mutex_unlock(&pci_epf_mutex);
  313. id++;
  314. }
  315. return 0;
  316. }
  317. /**
  318. * __pci_epf_register_driver() - register a new PCI EPF driver
  319. * @driver: structure representing PCI EPF driver
  320. * @owner: the owner of the module that registers the PCI EPF driver
  321. *
  322. * Invoke to register a new PCI EPF driver.
  323. */
  324. int __pci_epf_register_driver(struct pci_epf_driver *driver,
  325. struct module *owner)
  326. {
  327. int ret;
  328. if (!driver->ops)
  329. return -EINVAL;
  330. if (!driver->ops->bind || !driver->ops->unbind)
  331. return -EINVAL;
  332. driver->driver.bus = &pci_epf_bus_type;
  333. driver->driver.owner = owner;
  334. ret = driver_register(&driver->driver);
  335. if (ret)
  336. return ret;
  337. pci_epf_add_cfs(driver);
  338. return 0;
  339. }
  340. EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
  341. /**
  342. * pci_epf_destroy() - destroy the created PCI EPF device
  343. * @epf: the PCI EPF device that has to be destroyed.
  344. *
  345. * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
  346. */
  347. void pci_epf_destroy(struct pci_epf *epf)
  348. {
  349. device_unregister(&epf->dev);
  350. }
  351. EXPORT_SYMBOL_GPL(pci_epf_destroy);
  352. /**
  353. * pci_epf_create() - create a new PCI EPF device
  354. * @name: the name of the PCI EPF device. This name will be used to bind the
  355. * EPF device to a EPF driver
  356. *
  357. * Invoke to create a new PCI EPF device by providing the name of the function
  358. * device.
  359. */
  360. struct pci_epf *pci_epf_create(const char *name)
  361. {
  362. int ret;
  363. struct pci_epf *epf;
  364. struct device *dev;
  365. int len;
  366. epf = kzalloc(sizeof(*epf), GFP_KERNEL);
  367. if (!epf)
  368. return ERR_PTR(-ENOMEM);
  369. len = strchrnul(name, '.') - name;
  370. epf->name = kstrndup(name, len, GFP_KERNEL);
  371. if (!epf->name) {
  372. kfree(epf);
  373. return ERR_PTR(-ENOMEM);
  374. }
  375. /* VFs are numbered starting with 1. So set BIT(0) by default */
  376. epf->vfunction_num_map = 1;
  377. INIT_LIST_HEAD(&epf->pci_vepf);
  378. dev = &epf->dev;
  379. device_initialize(dev);
  380. dev->bus = &pci_epf_bus_type;
  381. dev->type = &pci_epf_type;
  382. mutex_init(&epf->lock);
  383. ret = dev_set_name(dev, "%s", name);
  384. if (ret) {
  385. put_device(dev);
  386. return ERR_PTR(ret);
  387. }
  388. ret = device_add(dev);
  389. if (ret) {
  390. put_device(dev);
  391. return ERR_PTR(ret);
  392. }
  393. return epf;
  394. }
  395. EXPORT_SYMBOL_GPL(pci_epf_create);
  396. static void pci_epf_dev_release(struct device *dev)
  397. {
  398. struct pci_epf *epf = to_pci_epf(dev);
  399. kfree(epf->name);
  400. kfree(epf);
  401. }
  402. static const struct device_type pci_epf_type = {
  403. .release = pci_epf_dev_release,
  404. };
  405. static const struct pci_epf_device_id *
  406. pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
  407. {
  408. while (id->name[0]) {
  409. if (strcmp(epf->name, id->name) == 0)
  410. return id;
  411. id++;
  412. }
  413. return NULL;
  414. }
  415. static int pci_epf_device_match(struct device *dev, const struct device_driver *drv)
  416. {
  417. struct pci_epf *epf = to_pci_epf(dev);
  418. const struct pci_epf_driver *driver = to_pci_epf_driver(drv);
  419. if (driver->id_table)
  420. return !!pci_epf_match_id(driver->id_table, epf);
  421. return !strcmp(epf->name, drv->name);
  422. }
  423. static int pci_epf_device_probe(struct device *dev)
  424. {
  425. struct pci_epf *epf = to_pci_epf(dev);
  426. struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
  427. if (!driver->probe)
  428. return -ENODEV;
  429. epf->driver = driver;
  430. return driver->probe(epf, pci_epf_match_id(driver->id_table, epf));
  431. }
  432. static void pci_epf_device_remove(struct device *dev)
  433. {
  434. struct pci_epf *epf = to_pci_epf(dev);
  435. struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
  436. if (driver->remove)
  437. driver->remove(epf);
  438. epf->driver = NULL;
  439. }
  440. static const struct bus_type pci_epf_bus_type = {
  441. .name = "pci-epf",
  442. .match = pci_epf_device_match,
  443. .probe = pci_epf_device_probe,
  444. .remove = pci_epf_device_remove,
  445. };
  446. static int __init pci_epf_init(void)
  447. {
  448. int ret;
  449. ret = bus_register(&pci_epf_bus_type);
  450. if (ret) {
  451. pr_err("failed to register pci epf bus --> %d\n", ret);
  452. return ret;
  453. }
  454. return 0;
  455. }
  456. module_init(pci_epf_init);
  457. static void __exit pci_epf_exit(void)
  458. {
  459. bus_unregister(&pci_epf_bus_type);
  460. }
  461. module_exit(pci_epf_exit);
  462. MODULE_DESCRIPTION("PCI EPF Library");
  463. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");