cma_configfs.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 = kcalloc(ports_num, sizeof(*cma_dev_group->ports),
  170. GFP_KERNEL);
  171. if (!ports)
  172. return -ENOMEM;
  173. for (i = 0; i < ports_num; i++) {
  174. char port_str[11];
  175. ports[i].port_num = i + 1;
  176. snprintf(port_str, sizeof(port_str), "%u", i + 1);
  177. ports[i].cma_dev_group = cma_dev_group;
  178. config_group_init_type_name(&ports[i].group,
  179. port_str,
  180. &cma_port_group_type);
  181. configfs_add_default_group(&ports[i].group,
  182. &cma_dev_group->ports_group);
  183. }
  184. cma_dev_group->ports = ports;
  185. return 0;
  186. }
  187. static void release_cma_dev(struct config_item *item)
  188. {
  189. struct config_group *group = container_of(item, struct config_group,
  190. cg_item);
  191. struct cma_dev_group *cma_dev_group = container_of(group,
  192. struct cma_dev_group,
  193. device_group);
  194. kfree(cma_dev_group);
  195. };
  196. static void release_cma_ports_group(struct config_item *item)
  197. {
  198. struct config_group *group = container_of(item, struct config_group,
  199. cg_item);
  200. struct cma_dev_group *cma_dev_group = container_of(group,
  201. struct cma_dev_group,
  202. ports_group);
  203. kfree(cma_dev_group->ports);
  204. cma_dev_group->ports = NULL;
  205. };
  206. static struct configfs_item_operations cma_ports_item_ops = {
  207. .release = release_cma_ports_group
  208. };
  209. static const struct config_item_type cma_ports_group_type = {
  210. .ct_item_ops = &cma_ports_item_ops,
  211. .ct_owner = THIS_MODULE
  212. };
  213. static struct configfs_item_operations cma_device_item_ops = {
  214. .release = release_cma_dev
  215. };
  216. static const struct config_item_type cma_device_group_type = {
  217. .ct_item_ops = &cma_device_item_ops,
  218. .ct_owner = THIS_MODULE
  219. };
  220. static struct config_group *make_cma_dev(struct config_group *group,
  221. const char *name)
  222. {
  223. int err = -ENODEV;
  224. struct cma_device *cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
  225. (void *)name);
  226. struct cma_dev_group *cma_dev_group = NULL;
  227. if (!cma_dev)
  228. goto fail;
  229. cma_dev_group = kzalloc(sizeof(*cma_dev_group), GFP_KERNEL);
  230. if (!cma_dev_group) {
  231. err = -ENOMEM;
  232. goto fail;
  233. }
  234. strscpy(cma_dev_group->name, name, sizeof(cma_dev_group->name));
  235. config_group_init_type_name(&cma_dev_group->ports_group, "ports",
  236. &cma_ports_group_type);
  237. err = make_cma_ports(cma_dev_group, cma_dev);
  238. if (err)
  239. goto fail;
  240. config_group_init_type_name(&cma_dev_group->device_group, name,
  241. &cma_device_group_type);
  242. configfs_add_default_group(&cma_dev_group->ports_group,
  243. &cma_dev_group->device_group);
  244. cma_dev_put(cma_dev);
  245. return &cma_dev_group->device_group;
  246. fail:
  247. if (cma_dev)
  248. cma_dev_put(cma_dev);
  249. kfree(cma_dev_group);
  250. return ERR_PTR(err);
  251. }
  252. static void drop_cma_dev(struct config_group *cgroup, struct config_item *item)
  253. {
  254. struct config_group *group =
  255. container_of(item, struct config_group, cg_item);
  256. struct cma_dev_group *cma_dev_group =
  257. container_of(group, struct cma_dev_group, device_group);
  258. configfs_remove_default_groups(&cma_dev_group->ports_group);
  259. configfs_remove_default_groups(&cma_dev_group->device_group);
  260. config_item_put(item);
  261. }
  262. static struct configfs_group_operations cma_subsys_group_ops = {
  263. .make_group = make_cma_dev,
  264. .drop_item = drop_cma_dev,
  265. };
  266. static const struct config_item_type cma_subsys_type = {
  267. .ct_group_ops = &cma_subsys_group_ops,
  268. .ct_owner = THIS_MODULE,
  269. };
  270. static struct configfs_subsystem cma_subsys = {
  271. .su_group = {
  272. .cg_item = {
  273. .ci_namebuf = "rdma_cm",
  274. .ci_type = &cma_subsys_type,
  275. },
  276. },
  277. };
  278. int __init cma_configfs_init(void)
  279. {
  280. int ret;
  281. config_group_init(&cma_subsys.su_group);
  282. mutex_init(&cma_subsys.su_mutex);
  283. ret = configfs_register_subsystem(&cma_subsys);
  284. if (ret)
  285. mutex_destroy(&cma_subsys.su_mutex);
  286. return ret;
  287. }
  288. void __exit cma_configfs_exit(void)
  289. {
  290. configfs_unregister_subsystem(&cma_subsys);
  291. mutex_destroy(&cma_subsys.su_mutex);
  292. }