pci-ep-cfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /**
  2. * configfs to configure the PCI endpoint
  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/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/pci-epc.h>
  22. #include <linux/pci-epf.h>
  23. #include <linux/pci-ep-cfs.h>
  24. static struct config_group *functions_group;
  25. static struct config_group *controllers_group;
  26. struct pci_epf_group {
  27. struct config_group group;
  28. struct pci_epf *epf;
  29. };
  30. struct pci_epc_group {
  31. struct config_group group;
  32. struct pci_epc *epc;
  33. bool start;
  34. unsigned long function_num_map;
  35. };
  36. static inline struct pci_epf_group *to_pci_epf_group(struct config_item *item)
  37. {
  38. return container_of(to_config_group(item), struct pci_epf_group, group);
  39. }
  40. static inline struct pci_epc_group *to_pci_epc_group(struct config_item *item)
  41. {
  42. return container_of(to_config_group(item), struct pci_epc_group, group);
  43. }
  44. static ssize_t pci_epc_start_store(struct config_item *item, const char *page,
  45. size_t len)
  46. {
  47. int ret;
  48. bool start;
  49. struct pci_epc *epc;
  50. struct pci_epc_group *epc_group = to_pci_epc_group(item);
  51. epc = epc_group->epc;
  52. ret = kstrtobool(page, &start);
  53. if (ret)
  54. return ret;
  55. if (!start) {
  56. pci_epc_stop(epc);
  57. return len;
  58. }
  59. ret = pci_epc_start(epc);
  60. if (ret) {
  61. dev_err(&epc->dev, "failed to start endpoint controller\n");
  62. return -EINVAL;
  63. }
  64. epc_group->start = start;
  65. return len;
  66. }
  67. static ssize_t pci_epc_start_show(struct config_item *item, char *page)
  68. {
  69. return sprintf(page, "%d\n",
  70. to_pci_epc_group(item)->start);
  71. }
  72. CONFIGFS_ATTR(pci_epc_, start);
  73. static struct configfs_attribute *pci_epc_attrs[] = {
  74. &pci_epc_attr_start,
  75. NULL,
  76. };
  77. static int pci_epc_epf_link(struct config_item *epc_item,
  78. struct config_item *epf_item)
  79. {
  80. int ret;
  81. u32 func_no = 0;
  82. struct pci_epf_group *epf_group = to_pci_epf_group(epf_item);
  83. struct pci_epc_group *epc_group = to_pci_epc_group(epc_item);
  84. struct pci_epc *epc = epc_group->epc;
  85. struct pci_epf *epf = epf_group->epf;
  86. func_no = find_first_zero_bit(&epc_group->function_num_map,
  87. BITS_PER_LONG);
  88. if (func_no >= BITS_PER_LONG)
  89. return -EINVAL;
  90. set_bit(func_no, &epc_group->function_num_map);
  91. epf->func_no = func_no;
  92. ret = pci_epc_add_epf(epc, epf);
  93. if (ret)
  94. goto err_add_epf;
  95. ret = pci_epf_bind(epf);
  96. if (ret)
  97. goto err_epf_bind;
  98. return 0;
  99. err_epf_bind:
  100. pci_epc_remove_epf(epc, epf);
  101. err_add_epf:
  102. clear_bit(func_no, &epc_group->function_num_map);
  103. return ret;
  104. }
  105. static void pci_epc_epf_unlink(struct config_item *epc_item,
  106. struct config_item *epf_item)
  107. {
  108. struct pci_epc *epc;
  109. struct pci_epf *epf;
  110. struct pci_epf_group *epf_group = to_pci_epf_group(epf_item);
  111. struct pci_epc_group *epc_group = to_pci_epc_group(epc_item);
  112. WARN_ON_ONCE(epc_group->start);
  113. epc = epc_group->epc;
  114. epf = epf_group->epf;
  115. clear_bit(epf->func_no, &epc_group->function_num_map);
  116. pci_epf_unbind(epf);
  117. pci_epc_remove_epf(epc, epf);
  118. }
  119. static struct configfs_item_operations pci_epc_item_ops = {
  120. .allow_link = pci_epc_epf_link,
  121. .drop_link = pci_epc_epf_unlink,
  122. };
  123. static struct config_item_type pci_epc_type = {
  124. .ct_item_ops = &pci_epc_item_ops,
  125. .ct_attrs = pci_epc_attrs,
  126. .ct_owner = THIS_MODULE,
  127. };
  128. struct config_group *pci_ep_cfs_add_epc_group(const char *name)
  129. {
  130. int ret;
  131. struct pci_epc *epc;
  132. struct config_group *group;
  133. struct pci_epc_group *epc_group;
  134. epc_group = kzalloc(sizeof(*epc_group), GFP_KERNEL);
  135. if (!epc_group) {
  136. ret = -ENOMEM;
  137. goto err;
  138. }
  139. group = &epc_group->group;
  140. config_group_init_type_name(group, name, &pci_epc_type);
  141. ret = configfs_register_group(controllers_group, group);
  142. if (ret) {
  143. pr_err("failed to register configfs group for %s\n", name);
  144. goto err_register_group;
  145. }
  146. epc = pci_epc_get(name);
  147. if (IS_ERR(epc)) {
  148. ret = PTR_ERR(epc);
  149. goto err_epc_get;
  150. }
  151. epc_group->epc = epc;
  152. return group;
  153. err_epc_get:
  154. configfs_unregister_group(group);
  155. err_register_group:
  156. kfree(epc_group);
  157. err:
  158. return ERR_PTR(ret);
  159. }
  160. EXPORT_SYMBOL(pci_ep_cfs_add_epc_group);
  161. void pci_ep_cfs_remove_epc_group(struct config_group *group)
  162. {
  163. struct pci_epc_group *epc_group;
  164. if (!group)
  165. return;
  166. epc_group = container_of(group, struct pci_epc_group, group);
  167. pci_epc_put(epc_group->epc);
  168. configfs_unregister_group(&epc_group->group);
  169. kfree(epc_group);
  170. }
  171. EXPORT_SYMBOL(pci_ep_cfs_remove_epc_group);
  172. #define PCI_EPF_HEADER_R(_name) \
  173. static ssize_t pci_epf_##_name##_show(struct config_item *item, char *page) \
  174. { \
  175. struct pci_epf *epf = to_pci_epf_group(item)->epf; \
  176. if (WARN_ON_ONCE(!epf->header)) \
  177. return -EINVAL; \
  178. return sprintf(page, "0x%04x\n", epf->header->_name); \
  179. }
  180. #define PCI_EPF_HEADER_W_u32(_name) \
  181. static ssize_t pci_epf_##_name##_store(struct config_item *item, \
  182. const char *page, size_t len) \
  183. { \
  184. u32 val; \
  185. int ret; \
  186. struct pci_epf *epf = to_pci_epf_group(item)->epf; \
  187. if (WARN_ON_ONCE(!epf->header)) \
  188. return -EINVAL; \
  189. ret = kstrtou32(page, 0, &val); \
  190. if (ret) \
  191. return ret; \
  192. epf->header->_name = val; \
  193. return len; \
  194. }
  195. #define PCI_EPF_HEADER_W_u16(_name) \
  196. static ssize_t pci_epf_##_name##_store(struct config_item *item, \
  197. const char *page, size_t len) \
  198. { \
  199. u16 val; \
  200. int ret; \
  201. struct pci_epf *epf = to_pci_epf_group(item)->epf; \
  202. if (WARN_ON_ONCE(!epf->header)) \
  203. return -EINVAL; \
  204. ret = kstrtou16(page, 0, &val); \
  205. if (ret) \
  206. return ret; \
  207. epf->header->_name = val; \
  208. return len; \
  209. }
  210. #define PCI_EPF_HEADER_W_u8(_name) \
  211. static ssize_t pci_epf_##_name##_store(struct config_item *item, \
  212. const char *page, size_t len) \
  213. { \
  214. u8 val; \
  215. int ret; \
  216. struct pci_epf *epf = to_pci_epf_group(item)->epf; \
  217. if (WARN_ON_ONCE(!epf->header)) \
  218. return -EINVAL; \
  219. ret = kstrtou8(page, 0, &val); \
  220. if (ret) \
  221. return ret; \
  222. epf->header->_name = val; \
  223. return len; \
  224. }
  225. static ssize_t pci_epf_msi_interrupts_store(struct config_item *item,
  226. const char *page, size_t len)
  227. {
  228. u8 val;
  229. int ret;
  230. ret = kstrtou8(page, 0, &val);
  231. if (ret)
  232. return ret;
  233. to_pci_epf_group(item)->epf->msi_interrupts = val;
  234. return len;
  235. }
  236. static ssize_t pci_epf_msi_interrupts_show(struct config_item *item,
  237. char *page)
  238. {
  239. return sprintf(page, "%d\n",
  240. to_pci_epf_group(item)->epf->msi_interrupts);
  241. }
  242. PCI_EPF_HEADER_R(vendorid)
  243. PCI_EPF_HEADER_W_u16(vendorid)
  244. PCI_EPF_HEADER_R(deviceid)
  245. PCI_EPF_HEADER_W_u16(deviceid)
  246. PCI_EPF_HEADER_R(revid)
  247. PCI_EPF_HEADER_W_u8(revid)
  248. PCI_EPF_HEADER_R(progif_code)
  249. PCI_EPF_HEADER_W_u8(progif_code)
  250. PCI_EPF_HEADER_R(subclass_code)
  251. PCI_EPF_HEADER_W_u8(subclass_code)
  252. PCI_EPF_HEADER_R(baseclass_code)
  253. PCI_EPF_HEADER_W_u8(baseclass_code)
  254. PCI_EPF_HEADER_R(cache_line_size)
  255. PCI_EPF_HEADER_W_u8(cache_line_size)
  256. PCI_EPF_HEADER_R(subsys_vendor_id)
  257. PCI_EPF_HEADER_W_u16(subsys_vendor_id)
  258. PCI_EPF_HEADER_R(subsys_id)
  259. PCI_EPF_HEADER_W_u16(subsys_id)
  260. PCI_EPF_HEADER_R(interrupt_pin)
  261. PCI_EPF_HEADER_W_u8(interrupt_pin)
  262. CONFIGFS_ATTR(pci_epf_, vendorid);
  263. CONFIGFS_ATTR(pci_epf_, deviceid);
  264. CONFIGFS_ATTR(pci_epf_, revid);
  265. CONFIGFS_ATTR(pci_epf_, progif_code);
  266. CONFIGFS_ATTR(pci_epf_, subclass_code);
  267. CONFIGFS_ATTR(pci_epf_, baseclass_code);
  268. CONFIGFS_ATTR(pci_epf_, cache_line_size);
  269. CONFIGFS_ATTR(pci_epf_, subsys_vendor_id);
  270. CONFIGFS_ATTR(pci_epf_, subsys_id);
  271. CONFIGFS_ATTR(pci_epf_, interrupt_pin);
  272. CONFIGFS_ATTR(pci_epf_, msi_interrupts);
  273. static struct configfs_attribute *pci_epf_attrs[] = {
  274. &pci_epf_attr_vendorid,
  275. &pci_epf_attr_deviceid,
  276. &pci_epf_attr_revid,
  277. &pci_epf_attr_progif_code,
  278. &pci_epf_attr_subclass_code,
  279. &pci_epf_attr_baseclass_code,
  280. &pci_epf_attr_cache_line_size,
  281. &pci_epf_attr_subsys_vendor_id,
  282. &pci_epf_attr_subsys_id,
  283. &pci_epf_attr_interrupt_pin,
  284. &pci_epf_attr_msi_interrupts,
  285. NULL,
  286. };
  287. static void pci_epf_release(struct config_item *item)
  288. {
  289. struct pci_epf_group *epf_group = to_pci_epf_group(item);
  290. pci_epf_destroy(epf_group->epf);
  291. kfree(epf_group);
  292. }
  293. static struct configfs_item_operations pci_epf_ops = {
  294. .release = pci_epf_release,
  295. };
  296. static struct config_item_type pci_epf_type = {
  297. .ct_item_ops = &pci_epf_ops,
  298. .ct_attrs = pci_epf_attrs,
  299. .ct_owner = THIS_MODULE,
  300. };
  301. static struct config_group *pci_epf_make(struct config_group *group,
  302. const char *name)
  303. {
  304. struct pci_epf_group *epf_group;
  305. struct pci_epf *epf;
  306. epf_group = kzalloc(sizeof(*epf_group), GFP_KERNEL);
  307. if (!epf_group)
  308. return ERR_PTR(-ENOMEM);
  309. config_group_init_type_name(&epf_group->group, name, &pci_epf_type);
  310. epf = pci_epf_create(group->cg_item.ci_name);
  311. if (IS_ERR(epf)) {
  312. pr_err("failed to create endpoint function device\n");
  313. return ERR_PTR(-EINVAL);
  314. }
  315. epf_group->epf = epf;
  316. return &epf_group->group;
  317. }
  318. static void pci_epf_drop(struct config_group *group, struct config_item *item)
  319. {
  320. config_item_put(item);
  321. }
  322. static struct configfs_group_operations pci_epf_group_ops = {
  323. .make_group = &pci_epf_make,
  324. .drop_item = &pci_epf_drop,
  325. };
  326. static struct config_item_type pci_epf_group_type = {
  327. .ct_group_ops = &pci_epf_group_ops,
  328. .ct_owner = THIS_MODULE,
  329. };
  330. struct config_group *pci_ep_cfs_add_epf_group(const char *name)
  331. {
  332. struct config_group *group;
  333. group = configfs_register_default_group(functions_group, name,
  334. &pci_epf_group_type);
  335. if (IS_ERR(group))
  336. pr_err("failed to register configfs group for %s function\n",
  337. name);
  338. return group;
  339. }
  340. EXPORT_SYMBOL(pci_ep_cfs_add_epf_group);
  341. void pci_ep_cfs_remove_epf_group(struct config_group *group)
  342. {
  343. if (IS_ERR_OR_NULL(group))
  344. return;
  345. configfs_unregister_default_group(group);
  346. }
  347. EXPORT_SYMBOL(pci_ep_cfs_remove_epf_group);
  348. static struct config_item_type pci_functions_type = {
  349. .ct_owner = THIS_MODULE,
  350. };
  351. static struct config_item_type pci_controllers_type = {
  352. .ct_owner = THIS_MODULE,
  353. };
  354. static struct config_item_type pci_ep_type = {
  355. .ct_owner = THIS_MODULE,
  356. };
  357. static struct configfs_subsystem pci_ep_cfs_subsys = {
  358. .su_group = {
  359. .cg_item = {
  360. .ci_namebuf = "pci_ep",
  361. .ci_type = &pci_ep_type,
  362. },
  363. },
  364. .su_mutex = __MUTEX_INITIALIZER(pci_ep_cfs_subsys.su_mutex),
  365. };
  366. static int __init pci_ep_cfs_init(void)
  367. {
  368. int ret;
  369. struct config_group *root = &pci_ep_cfs_subsys.su_group;
  370. config_group_init(root);
  371. ret = configfs_register_subsystem(&pci_ep_cfs_subsys);
  372. if (ret) {
  373. pr_err("Error %d while registering subsystem %s\n",
  374. ret, root->cg_item.ci_namebuf);
  375. goto err;
  376. }
  377. functions_group = configfs_register_default_group(root, "functions",
  378. &pci_functions_type);
  379. if (IS_ERR(functions_group)) {
  380. ret = PTR_ERR(functions_group);
  381. pr_err("Error %d while registering functions group\n",
  382. ret);
  383. goto err_functions_group;
  384. }
  385. controllers_group =
  386. configfs_register_default_group(root, "controllers",
  387. &pci_controllers_type);
  388. if (IS_ERR(controllers_group)) {
  389. ret = PTR_ERR(controllers_group);
  390. pr_err("Error %d while registering controllers group\n",
  391. ret);
  392. goto err_controllers_group;
  393. }
  394. return 0;
  395. err_controllers_group:
  396. configfs_unregister_default_group(functions_group);
  397. err_functions_group:
  398. configfs_unregister_subsystem(&pci_ep_cfs_subsys);
  399. err:
  400. return ret;
  401. }
  402. module_init(pci_ep_cfs_init);
  403. static void __exit pci_ep_cfs_exit(void)
  404. {
  405. configfs_unregister_default_group(controllers_group);
  406. configfs_unregister_default_group(functions_group);
  407. configfs_unregister_subsystem(&pci_ep_cfs_subsys);
  408. }
  409. module_exit(pci_ep_cfs_exit);
  410. MODULE_DESCRIPTION("PCI EP CONFIGFS");
  411. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
  412. MODULE_LICENSE("GPL v2");