cma_configfs.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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/configfs.h>
  33. #include <rdma/ib_verbs.h>
  34. #include <rdma/rdma_cm.h>
  35. #include "core_priv.h"
  36. #include "cma_priv.h"
  37. struct cma_device;
  38. struct cma_dev_group;
  39. struct cma_dev_port_group {
  40. u32 port_num;
  41. struct cma_dev_group *cma_dev_group;
  42. struct config_group group;
  43. };
  44. struct cma_dev_group {
  45. char name[IB_DEVICE_NAME_MAX];
  46. struct config_group device_group;
  47. struct config_group ports_group;
  48. struct cma_dev_port_group *ports;
  49. };
  50. static struct cma_dev_port_group *to_dev_port_group(struct config_item *item)
  51. {
  52. struct config_group *group;
  53. if (!item)
  54. return NULL;
  55. group = container_of(item, struct config_group, cg_item);
  56. return container_of(group, struct cma_dev_port_group, group);
  57. }
  58. static bool filter_by_name(struct ib_device *ib_dev, void *cookie)
  59. {
  60. return !strcmp(dev_name(&ib_dev->dev), cookie);
  61. }
  62. static int cma_configfs_params_get(struct config_item *item,
  63. struct cma_device **pcma_dev,
  64. struct cma_dev_port_group **pgroup)
  65. {
  66. struct cma_dev_port_group *group = to_dev_port_group(item);
  67. struct cma_device *cma_dev;
  68. if (!group)
  69. return -ENODEV;
  70. cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
  71. group->cma_dev_group->name);
  72. if (!cma_dev)
  73. return -ENODEV;
  74. *pcma_dev = cma_dev;
  75. *pgroup = group;
  76. return 0;
  77. }
  78. static void cma_configfs_params_put(struct cma_device *cma_dev)
  79. {
  80. cma_dev_put(cma_dev);
  81. }
  82. static ssize_t default_roce_mode_show(struct config_item *item,
  83. char *buf)
  84. {
  85. struct cma_device *cma_dev;
  86. struct cma_dev_port_group *group;
  87. int gid_type;
  88. ssize_t ret;
  89. ret = cma_configfs_params_get(item, &cma_dev, &group);
  90. if (ret)
  91. return ret;
  92. gid_type = cma_get_default_gid_type(cma_dev, group->port_num);
  93. cma_configfs_params_put(cma_dev);
  94. if (gid_type < 0)
  95. return gid_type;
  96. return sysfs_emit(buf, "%s\n", ib_cache_gid_type_str(gid_type));
  97. }
  98. static ssize_t default_roce_mode_store(struct config_item *item,
  99. const char *buf, size_t count)
  100. {
  101. struct cma_device *cma_dev;
  102. struct cma_dev_port_group *group;
  103. int gid_type;
  104. ssize_t ret;
  105. ret = cma_configfs_params_get(item, &cma_dev, &group);
  106. if (ret)
  107. return ret;
  108. gid_type = ib_cache_gid_parse_type_str(buf);
  109. if (gid_type < 0) {
  110. cma_configfs_params_put(cma_dev);
  111. return -EINVAL;
  112. }
  113. ret = cma_set_default_gid_type(cma_dev, group->port_num, gid_type);
  114. cma_configfs_params_put(cma_dev);
  115. return !ret ? strnlen(buf, count) : ret;
  116. }
  117. CONFIGFS_ATTR(, default_roce_mode);
  118. static ssize_t default_roce_tos_show(struct config_item *item, char *buf)
  119. {
  120. struct cma_device *cma_dev;
  121. struct cma_dev_port_group *group;
  122. ssize_t ret;
  123. u8 tos;
  124. ret = cma_configfs_params_get(item, &cma_dev, &group);
  125. if (ret)
  126. return ret;
  127. tos = cma_get_default_roce_tos(cma_dev, group->port_num);
  128. cma_configfs_params_put(cma_dev);
  129. return sysfs_emit(buf, "%u\n", tos);
  130. }
  131. static ssize_t default_roce_tos_store(struct config_item *item,
  132. const char *buf, size_t count)
  133. {
  134. struct cma_device *cma_dev;
  135. struct cma_dev_port_group *group;
  136. ssize_t ret;
  137. u8 tos;
  138. ret = kstrtou8(buf, 0, &tos);
  139. if (ret)
  140. return ret;
  141. ret = cma_configfs_params_get(item, &cma_dev, &group);
  142. if (ret)
  143. return ret;
  144. ret = cma_set_default_roce_tos(cma_dev, group->port_num, tos);
  145. cma_configfs_params_put(cma_dev);
  146. return ret ? ret : strnlen(buf, count);
  147. }
  148. CONFIGFS_ATTR(, default_roce_tos);
  149. static struct configfs_attribute *cma_configfs_attributes[] = {
  150. &attr_default_roce_mode,
  151. &attr_default_roce_tos,
  152. NULL,
  153. };
  154. static const struct config_item_type cma_port_group_type = {
  155. .ct_attrs = cma_configfs_attributes,
  156. .ct_owner = THIS_MODULE
  157. };
  158. static int make_cma_ports(struct cma_dev_group *cma_dev_group,
  159. struct cma_device *cma_dev)
  160. {
  161. struct cma_dev_port_group *ports;
  162. struct ib_device *ibdev;
  163. u32 ports_num;
  164. u32 i;
  165. ibdev = cma_get_ib_dev(cma_dev);
  166. if (!ibdev)
  167. return -ENODEV;
  168. ports_num = ibdev->phys_port_cnt;
  169. ports = kzalloc_objs(*cma_dev_group->ports, ports_num);
  170. if (!ports)
  171. return -ENOMEM;
  172. for (i = 0; i < ports_num; i++) {
  173. char port_str[11];
  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. }
  186. static void release_cma_dev(struct config_item *item)
  187. {
  188. struct config_group *group = container_of(item, struct config_group,
  189. cg_item);
  190. struct cma_dev_group *cma_dev_group = container_of(group,
  191. struct cma_dev_group,
  192. device_group);
  193. kfree(cma_dev_group);
  194. };
  195. static void release_cma_ports_group(struct config_item *item)
  196. {
  197. struct config_group *group = container_of(item, struct config_group,
  198. cg_item);
  199. struct cma_dev_group *cma_dev_group = container_of(group,
  200. struct cma_dev_group,
  201. ports_group);
  202. kfree(cma_dev_group->ports);
  203. cma_dev_group->ports = NULL;
  204. };
  205. static struct configfs_item_operations cma_ports_item_ops = {
  206. .release = release_cma_ports_group
  207. };
  208. static const struct config_item_type cma_ports_group_type = {
  209. .ct_item_ops = &cma_ports_item_ops,
  210. .ct_owner = THIS_MODULE
  211. };
  212. static struct configfs_item_operations cma_device_item_ops = {
  213. .release = release_cma_dev
  214. };
  215. static const struct config_item_type cma_device_group_type = {
  216. .ct_item_ops = &cma_device_item_ops,
  217. .ct_owner = THIS_MODULE
  218. };
  219. static struct config_group *make_cma_dev(struct config_group *group,
  220. const char *name)
  221. {
  222. int err = -ENODEV;
  223. struct cma_device *cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
  224. (void *)name);
  225. struct cma_dev_group *cma_dev_group = NULL;
  226. if (!cma_dev)
  227. goto fail;
  228. cma_dev_group = kzalloc_obj(*cma_dev_group);
  229. if (!cma_dev_group) {
  230. err = -ENOMEM;
  231. goto fail;
  232. }
  233. strscpy(cma_dev_group->name, name, sizeof(cma_dev_group->name));
  234. config_group_init_type_name(&cma_dev_group->ports_group, "ports",
  235. &cma_ports_group_type);
  236. err = make_cma_ports(cma_dev_group, cma_dev);
  237. if (err)
  238. goto fail;
  239. config_group_init_type_name(&cma_dev_group->device_group, name,
  240. &cma_device_group_type);
  241. configfs_add_default_group(&cma_dev_group->ports_group,
  242. &cma_dev_group->device_group);
  243. cma_dev_put(cma_dev);
  244. return &cma_dev_group->device_group;
  245. fail:
  246. if (cma_dev)
  247. cma_dev_put(cma_dev);
  248. kfree(cma_dev_group);
  249. return ERR_PTR(err);
  250. }
  251. static void drop_cma_dev(struct config_group *cgroup, struct config_item *item)
  252. {
  253. struct config_group *group =
  254. container_of(item, struct config_group, cg_item);
  255. struct cma_dev_group *cma_dev_group =
  256. container_of(group, struct cma_dev_group, device_group);
  257. configfs_remove_default_groups(&cma_dev_group->ports_group);
  258. configfs_remove_default_groups(&cma_dev_group->device_group);
  259. config_item_put(item);
  260. }
  261. static struct configfs_group_operations cma_subsys_group_ops = {
  262. .make_group = make_cma_dev,
  263. .drop_item = drop_cma_dev,
  264. };
  265. static const struct config_item_type cma_subsys_type = {
  266. .ct_group_ops = &cma_subsys_group_ops,
  267. .ct_owner = THIS_MODULE,
  268. };
  269. static struct configfs_subsystem cma_subsys = {
  270. .su_group = {
  271. .cg_item = {
  272. .ci_namebuf = "rdma_cm",
  273. .ci_type = &cma_subsys_type,
  274. },
  275. },
  276. };
  277. int __init cma_configfs_init(void)
  278. {
  279. int ret;
  280. config_group_init(&cma_subsys.su_group);
  281. mutex_init(&cma_subsys.su_mutex);
  282. ret = configfs_register_subsystem(&cma_subsys);
  283. if (ret)
  284. mutex_destroy(&cma_subsys.su_mutex);
  285. return ret;
  286. }
  287. void __exit cma_configfs_exit(void)
  288. {
  289. configfs_unregister_subsystem(&cma_subsys);
  290. mutex_destroy(&cma_subsys.su_mutex);
  291. }