cma_configfs.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 struct configfs_attribute *cma_configfs_attributes[] = {
  115. &attr_default_roce_mode,
  116. NULL,
  117. };
  118. static struct config_item_type cma_port_group_type = {
  119. .ct_attrs = cma_configfs_attributes,
  120. .ct_owner = THIS_MODULE
  121. };
  122. static int make_cma_ports(struct cma_dev_group *cma_dev_group,
  123. struct cma_device *cma_dev)
  124. {
  125. struct ib_device *ibdev;
  126. unsigned int i;
  127. unsigned int ports_num;
  128. struct cma_dev_port_group *ports;
  129. int err;
  130. ibdev = cma_get_ib_dev(cma_dev);
  131. if (!ibdev)
  132. return -ENODEV;
  133. ports_num = ibdev->phys_port_cnt;
  134. ports = kcalloc(ports_num, sizeof(*cma_dev_group->ports),
  135. GFP_KERNEL);
  136. if (!ports) {
  137. err = -ENOMEM;
  138. goto free;
  139. }
  140. for (i = 0; i < ports_num; i++) {
  141. char port_str[10];
  142. ports[i].port_num = i + 1;
  143. snprintf(port_str, sizeof(port_str), "%u", i + 1);
  144. ports[i].cma_dev_group = cma_dev_group;
  145. config_group_init_type_name(&ports[i].group,
  146. port_str,
  147. &cma_port_group_type);
  148. configfs_add_default_group(&ports[i].group,
  149. &cma_dev_group->ports_group);
  150. }
  151. cma_dev_group->ports = ports;
  152. return 0;
  153. free:
  154. kfree(ports);
  155. cma_dev_group->ports = NULL;
  156. return err;
  157. }
  158. static void release_cma_dev(struct config_item *item)
  159. {
  160. struct config_group *group = container_of(item, struct config_group,
  161. cg_item);
  162. struct cma_dev_group *cma_dev_group = container_of(group,
  163. struct cma_dev_group,
  164. device_group);
  165. kfree(cma_dev_group);
  166. };
  167. static void release_cma_ports_group(struct config_item *item)
  168. {
  169. struct config_group *group = container_of(item, struct config_group,
  170. cg_item);
  171. struct cma_dev_group *cma_dev_group = container_of(group,
  172. struct cma_dev_group,
  173. ports_group);
  174. kfree(cma_dev_group->ports);
  175. cma_dev_group->ports = NULL;
  176. };
  177. static struct configfs_item_operations cma_ports_item_ops = {
  178. .release = release_cma_ports_group
  179. };
  180. static struct config_item_type cma_ports_group_type = {
  181. .ct_item_ops = &cma_ports_item_ops,
  182. .ct_owner = THIS_MODULE
  183. };
  184. static struct configfs_item_operations cma_device_item_ops = {
  185. .release = release_cma_dev
  186. };
  187. static struct config_item_type cma_device_group_type = {
  188. .ct_item_ops = &cma_device_item_ops,
  189. .ct_owner = THIS_MODULE
  190. };
  191. static struct config_group *make_cma_dev(struct config_group *group,
  192. const char *name)
  193. {
  194. int err = -ENODEV;
  195. struct cma_device *cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
  196. (void *)name);
  197. struct cma_dev_group *cma_dev_group = NULL;
  198. if (!cma_dev)
  199. goto fail;
  200. cma_dev_group = kzalloc(sizeof(*cma_dev_group), GFP_KERNEL);
  201. if (!cma_dev_group) {
  202. err = -ENOMEM;
  203. goto fail;
  204. }
  205. strncpy(cma_dev_group->name, name, sizeof(cma_dev_group->name));
  206. config_group_init_type_name(&cma_dev_group->ports_group, "ports",
  207. &cma_ports_group_type);
  208. err = make_cma_ports(cma_dev_group, cma_dev);
  209. if (err)
  210. goto fail;
  211. config_group_init_type_name(&cma_dev_group->device_group, name,
  212. &cma_device_group_type);
  213. configfs_add_default_group(&cma_dev_group->ports_group,
  214. &cma_dev_group->device_group);
  215. cma_deref_dev(cma_dev);
  216. return &cma_dev_group->device_group;
  217. fail:
  218. if (cma_dev)
  219. cma_deref_dev(cma_dev);
  220. kfree(cma_dev_group);
  221. return ERR_PTR(err);
  222. }
  223. static struct configfs_group_operations cma_subsys_group_ops = {
  224. .make_group = make_cma_dev,
  225. };
  226. static struct config_item_type cma_subsys_type = {
  227. .ct_group_ops = &cma_subsys_group_ops,
  228. .ct_owner = THIS_MODULE,
  229. };
  230. static struct configfs_subsystem cma_subsys = {
  231. .su_group = {
  232. .cg_item = {
  233. .ci_namebuf = "rdma_cm",
  234. .ci_type = &cma_subsys_type,
  235. },
  236. },
  237. };
  238. int __init cma_configfs_init(void)
  239. {
  240. config_group_init(&cma_subsys.su_group);
  241. mutex_init(&cma_subsys.su_mutex);
  242. return configfs_register_subsystem(&cma_subsys);
  243. }
  244. void __exit cma_configfs_exit(void)
  245. {
  246. configfs_unregister_subsystem(&cma_subsys);
  247. }