cma_configfs.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (c) 2015, Mellanox Technologies inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/configfs.h>
  34. #include <rdma/ib_verbs.h>
  35. #include "core_priv.h"
  36. struct cma_device;
  37. struct cma_dev_group;
  38. struct cma_dev_port_group {
  39. unsigned int port_num;
  40. struct cma_dev_group *cma_dev_group;
  41. struct config_group group;
  42. };
  43. struct cma_dev_group {
  44. char name[IB_DEVICE_NAME_MAX];
  45. struct config_group device_group;
  46. struct config_group ports_group;
  47. struct cma_dev_port_group *ports;
  48. };
  49. static struct cma_dev_port_group *to_dev_port_group(struct config_item *item)
  50. {
  51. struct config_group *group;
  52. if (!item)
  53. return NULL;
  54. group = container_of(item, struct config_group, cg_item);
  55. return container_of(group, struct cma_dev_port_group, group);
  56. }
  57. static bool filter_by_name(struct ib_device *ib_dev, void *cookie)
  58. {
  59. return !strcmp(ib_dev->name, cookie);
  60. }
  61. static int cma_configfs_params_get(struct config_item *item,
  62. struct cma_device **pcma_dev,
  63. struct cma_dev_port_group **pgroup)
  64. {
  65. struct cma_dev_port_group *group = to_dev_port_group(item);
  66. struct cma_device *cma_dev;
  67. if (!group)
  68. return -ENODEV;
  69. cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
  70. group->cma_dev_group->name);
  71. if (!cma_dev)
  72. return -ENODEV;
  73. *pcma_dev = cma_dev;
  74. *pgroup = group;
  75. return 0;
  76. }
  77. static void cma_configfs_params_put(struct cma_device *cma_dev)
  78. {
  79. cma_deref_dev(cma_dev);
  80. }
  81. static ssize_t default_roce_mode_show(struct config_item *item,
  82. char *buf)
  83. {
  84. struct cma_device *cma_dev;
  85. struct cma_dev_port_group *group;
  86. int gid_type;
  87. ssize_t ret;
  88. ret = cma_configfs_params_get(item, &cma_dev, &group);
  89. if (ret)
  90. return ret;
  91. gid_type = cma_get_default_gid_type(cma_dev, group->port_num);
  92. cma_configfs_params_put(cma_dev);
  93. if (gid_type < 0)
  94. return gid_type;
  95. return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_type));
  96. }
  97. static ssize_t default_roce_mode_store(struct config_item *item,
  98. const char *buf, size_t count)
  99. {
  100. struct cma_device *cma_dev;
  101. struct cma_dev_port_group *group;
  102. int gid_type = ib_cache_gid_parse_type_str(buf);
  103. ssize_t ret;
  104. if (gid_type < 0)
  105. return -EINVAL;
  106. ret = cma_configfs_params_get(item, &cma_dev, &group);
  107. if (ret)
  108. return ret;
  109. ret = cma_set_default_gid_type(cma_dev, group->port_num, gid_type);
  110. cma_configfs_params_put(cma_dev);
  111. return !ret ? strnlen(buf, count) : ret;
  112. }
  113. CONFIGFS_ATTR(, default_roce_mode);
  114. static ssize_t default_roce_tos_show(struct config_item *item, char *buf)
  115. {
  116. struct cma_device *cma_dev;
  117. struct cma_dev_port_group *group;
  118. ssize_t ret;
  119. u8 tos;
  120. ret = cma_configfs_params_get(item, &cma_dev, &group);
  121. if (ret)
  122. return ret;
  123. tos = cma_get_default_roce_tos(cma_dev, group->port_num);
  124. cma_configfs_params_put(cma_dev);
  125. return sprintf(buf, "%u\n", tos);
  126. }
  127. static ssize_t default_roce_tos_store(struct config_item *item,
  128. const char *buf, size_t count)
  129. {
  130. struct cma_device *cma_dev;
  131. struct cma_dev_port_group *group;
  132. ssize_t ret;
  133. u8 tos;
  134. ret = kstrtou8(buf, 0, &tos);
  135. if (ret)
  136. return ret;
  137. ret = cma_configfs_params_get(item, &cma_dev, &group);
  138. if (ret)
  139. return ret;
  140. ret = cma_set_default_roce_tos(cma_dev, group->port_num, tos);
  141. cma_configfs_params_put(cma_dev);
  142. return ret ? ret : strnlen(buf, count);
  143. }
  144. CONFIGFS_ATTR(, default_roce_tos);
  145. static struct configfs_attribute *cma_configfs_attributes[] = {
  146. &attr_default_roce_mode,
  147. &attr_default_roce_tos,
  148. NULL,
  149. };
  150. static const struct config_item_type cma_port_group_type = {
  151. .ct_attrs = cma_configfs_attributes,
  152. .ct_owner = THIS_MODULE
  153. };
  154. static int make_cma_ports(struct cma_dev_group *cma_dev_group,
  155. struct cma_device *cma_dev)
  156. {
  157. struct ib_device *ibdev;
  158. unsigned int i;
  159. unsigned int ports_num;
  160. struct cma_dev_port_group *ports;
  161. int err;
  162. ibdev = cma_get_ib_dev(cma_dev);
  163. if (!ibdev)
  164. return -ENODEV;
  165. ports_num = ibdev->phys_port_cnt;
  166. ports = kcalloc(ports_num, sizeof(*cma_dev_group->ports),
  167. GFP_KERNEL);
  168. if (!ports) {
  169. err = -ENOMEM;
  170. goto free;
  171. }
  172. for (i = 0; i < ports_num; i++) {
  173. char port_str[10];
  174. ports[i].port_num = i + 1;
  175. snprintf(port_str, sizeof(port_str), "%u", i + 1);
  176. ports[i].cma_dev_group = cma_dev_group;
  177. config_group_init_type_name(&ports[i].group,
  178. port_str,
  179. &cma_port_group_type);
  180. configfs_add_default_group(&ports[i].group,
  181. &cma_dev_group->ports_group);
  182. }
  183. cma_dev_group->ports = ports;
  184. return 0;
  185. free:
  186. kfree(ports);
  187. cma_dev_group->ports = NULL;
  188. return err;
  189. }
  190. static void release_cma_dev(struct config_item *item)
  191. {
  192. struct config_group *group = container_of(item, struct config_group,
  193. cg_item);
  194. struct cma_dev_group *cma_dev_group = container_of(group,
  195. struct cma_dev_group,
  196. device_group);
  197. kfree(cma_dev_group);
  198. };
  199. static void release_cma_ports_group(struct config_item *item)
  200. {
  201. struct config_group *group = container_of(item, struct config_group,
  202. cg_item);
  203. struct cma_dev_group *cma_dev_group = container_of(group,
  204. struct cma_dev_group,
  205. ports_group);
  206. kfree(cma_dev_group->ports);
  207. cma_dev_group->ports = NULL;
  208. };
  209. static struct configfs_item_operations cma_ports_item_ops = {
  210. .release = release_cma_ports_group
  211. };
  212. static const struct config_item_type cma_ports_group_type = {
  213. .ct_item_ops = &cma_ports_item_ops,
  214. .ct_owner = THIS_MODULE
  215. };
  216. static struct configfs_item_operations cma_device_item_ops = {
  217. .release = release_cma_dev
  218. };
  219. static const struct config_item_type cma_device_group_type = {
  220. .ct_item_ops = &cma_device_item_ops,
  221. .ct_owner = THIS_MODULE
  222. };
  223. static struct config_group *make_cma_dev(struct config_group *group,
  224. const char *name)
  225. {
  226. int err = -ENODEV;
  227. struct cma_device *cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
  228. (void *)name);
  229. struct cma_dev_group *cma_dev_group = NULL;
  230. if (!cma_dev)
  231. goto fail;
  232. cma_dev_group = kzalloc(sizeof(*cma_dev_group), GFP_KERNEL);
  233. if (!cma_dev_group) {
  234. err = -ENOMEM;
  235. goto fail;
  236. }
  237. strlcpy(cma_dev_group->name, name, sizeof(cma_dev_group->name));
  238. config_group_init_type_name(&cma_dev_group->ports_group, "ports",
  239. &cma_ports_group_type);
  240. err = make_cma_ports(cma_dev_group, cma_dev);
  241. if (err)
  242. goto fail;
  243. config_group_init_type_name(&cma_dev_group->device_group, name,
  244. &cma_device_group_type);
  245. configfs_add_default_group(&cma_dev_group->ports_group,
  246. &cma_dev_group->device_group);
  247. cma_deref_dev(cma_dev);
  248. return &cma_dev_group->device_group;
  249. fail:
  250. if (cma_dev)
  251. cma_deref_dev(cma_dev);
  252. kfree(cma_dev_group);
  253. return ERR_PTR(err);
  254. }
  255. static struct configfs_group_operations cma_subsys_group_ops = {
  256. .make_group = make_cma_dev,
  257. };
  258. static const struct config_item_type cma_subsys_type = {
  259. .ct_group_ops = &cma_subsys_group_ops,
  260. .ct_owner = THIS_MODULE,
  261. };
  262. static struct configfs_subsystem cma_subsys = {
  263. .su_group = {
  264. .cg_item = {
  265. .ci_namebuf = "rdma_cm",
  266. .ci_type = &cma_subsys_type,
  267. },
  268. },
  269. };
  270. int __init cma_configfs_init(void)
  271. {
  272. config_group_init(&cma_subsys.su_group);
  273. mutex_init(&cma_subsys.su_mutex);
  274. return configfs_register_subsystem(&cma_subsys);
  275. }
  276. void __exit cma_configfs_exit(void)
  277. {
  278. configfs_unregister_subsystem(&cma_subsys);
  279. }