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 33 #include <linux/configfs.h> 34 #include <rdma/ib_verbs.h> 35 #include <rdma/rdma_cm.h> 36 37 #include "core_priv.h" 38 #include "cma_priv.h" 39 40 struct cma_device; 41 42 struct cma_dev_group; 43 44 struct cma_dev_port_group { 45 u32 port_num; 46 struct cma_dev_group *cma_dev_group; 47 struct config_group group; 48 }; 49 50 struct cma_dev_group { 51 char name[IB_DEVICE_NAME_MAX]; 52 struct config_group device_group; 53 struct config_group ports_group; 54 struct cma_dev_port_group *ports; 55 }; 56 57 static struct cma_dev_port_group *to_dev_port_group(struct config_item *item) 58 { 59 struct config_group *group; 60 61 if (!item) 62 return NULL; 63 64 group = container_of(item, struct config_group, cg_item); 65 return container_of(group, struct cma_dev_port_group, group); 66 } 67 68 /* 69 * configfs is not net namespace aware, so a name shared by devices in 70 * different namespaces resolves to the first match here. 71 */ 72 static bool filter_by_name(struct ib_device *ib_dev, void *cookie) 73 { 74 return !strcmp(dev_name(&ib_dev->dev), cookie); 75 } 76 77 static int cma_configfs_params_get(struct config_item *item, 78 struct cma_device **pcma_dev, 79 struct cma_dev_port_group **pgroup) 80 { 81 struct cma_dev_port_group *group = to_dev_port_group(item); 82 struct cma_device *cma_dev; 83 84 if (!group) 85 return -ENODEV; 86 87 cma_dev = cma_enum_devices_by_ibdev(filter_by_name, 88 group->cma_dev_group->name); 89 if (!cma_dev) 90 return -ENODEV; 91 92 *pcma_dev = cma_dev; 93 *pgroup = group; 94 95 return 0; 96 } 97 98 static void cma_configfs_params_put(struct cma_device *cma_dev) 99 { 100 cma_dev_put(cma_dev); 101 } 102 103 static ssize_t default_roce_mode_show(struct config_item *item, 104 char *buf) 105 { 106 struct cma_device *cma_dev; 107 struct cma_dev_port_group *group; 108 int gid_type; 109 ssize_t ret; 110 111 ret = cma_configfs_params_get(item, &cma_dev, &group); 112 if (ret) 113 return ret; 114 115 gid_type = cma_get_default_gid_type(cma_dev, group->port_num); 116 cma_configfs_params_put(cma_dev); 117 118 if (gid_type < 0) 119 return gid_type; 120 121 return sysfs_emit(buf, "%s\n", ib_cache_gid_type_str(gid_type)); 122 } 123 124 static ssize_t default_roce_mode_store(struct config_item *item, 125 const char *buf, size_t count) 126 { 127 struct cma_device *cma_dev; 128 struct cma_dev_port_group *group; 129 int gid_type; 130 ssize_t ret; 131 132 ret = cma_configfs_params_get(item, &cma_dev, &group); 133 if (ret) 134 return ret; 135 136 gid_type = ib_cache_gid_parse_type_str(buf); 137 if (gid_type < 0) { 138 cma_configfs_params_put(cma_dev); 139 return -EINVAL; 140 } 141 142 ret = cma_set_default_gid_type(cma_dev, group->port_num, gid_type); 143 144 cma_configfs_params_put(cma_dev); 145 146 return !ret ? strnlen(buf, count) : ret; 147 } 148 149 CONFIGFS_ATTR(, default_roce_mode); 150 151 static ssize_t default_roce_tos_show(struct config_item *item, char *buf) 152 { 153 struct cma_device *cma_dev; 154 struct cma_dev_port_group *group; 155 ssize_t ret; 156 u8 tos; 157 158 ret = cma_configfs_params_get(item, &cma_dev, &group); 159 if (ret) 160 return ret; 161 162 tos = cma_get_default_roce_tos(cma_dev, group->port_num); 163 cma_configfs_params_put(cma_dev); 164 165 return sysfs_emit(buf, "%u\n", tos); 166 } 167 168 static ssize_t default_roce_tos_store(struct config_item *item, 169 const char *buf, size_t count) 170 { 171 struct cma_device *cma_dev; 172 struct cma_dev_port_group *group; 173 ssize_t ret; 174 u8 tos; 175 176 ret = kstrtou8(buf, 0, &tos); 177 if (ret) 178 return ret; 179 180 ret = cma_configfs_params_get(item, &cma_dev, &group); 181 if (ret) 182 return ret; 183 184 ret = cma_set_default_roce_tos(cma_dev, group->port_num, tos); 185 cma_configfs_params_put(cma_dev); 186 187 return ret ? ret : strnlen(buf, count); 188 } 189 190 CONFIGFS_ATTR(, default_roce_tos); 191 192 static struct configfs_attribute *cma_configfs_attributes[] = { 193 &attr_default_roce_mode, 194 &attr_default_roce_tos, 195 NULL, 196 }; 197 198 static const struct config_item_type cma_port_group_type = { 199 .ct_attrs = cma_configfs_attributes, 200 .ct_owner = THIS_MODULE 201 }; 202 203 static int make_cma_ports(struct cma_dev_group *cma_dev_group, 204 struct cma_device *cma_dev) 205 { 206 struct cma_dev_port_group *ports; 207 struct ib_device *ibdev; 208 u32 ports_num; 209 u32 i; 210 211 ibdev = cma_get_ib_dev(cma_dev); 212 213 if (!ibdev) 214 return -ENODEV; 215 216 ports_num = ibdev->phys_port_cnt; 217 ports = kzalloc_objs(*cma_dev_group->ports, ports_num); 218 219 if (!ports) 220 return -ENOMEM; 221 222 for (i = 0; i < ports_num; i++) { 223 char port_str[11]; 224 225 ports[i].port_num = i + 1; 226 snprintf(port_str, sizeof(port_str), "%u", i + 1); 227 ports[i].cma_dev_group = cma_dev_group; 228 config_group_init_type_name(&ports[i].group, 229 port_str, 230 &cma_port_group_type); 231 configfs_add_default_group(&ports[i].group, 232 &cma_dev_group->ports_group); 233 234 } 235 cma_dev_group->ports = ports; 236 return 0; 237 } 238 239 static void release_cma_dev(struct config_item *item) 240 { 241 struct config_group *group = container_of(item, struct config_group, 242 cg_item); 243 struct cma_dev_group *cma_dev_group = container_of(group, 244 struct cma_dev_group, 245 device_group); 246 247 kfree(cma_dev_group); 248 }; 249 250 static void release_cma_ports_group(struct config_item *item) 251 { 252 struct config_group *group = container_of(item, struct config_group, 253 cg_item); 254 struct cma_dev_group *cma_dev_group = container_of(group, 255 struct cma_dev_group, 256 ports_group); 257 258 kfree(cma_dev_group->ports); 259 cma_dev_group->ports = NULL; 260 }; 261 262 static const struct configfs_item_operations cma_ports_item_ops = { 263 .release = release_cma_ports_group 264 }; 265 266 static const struct config_item_type cma_ports_group_type = { 267 .ct_item_ops = &cma_ports_item_ops, 268 .ct_owner = THIS_MODULE 269 }; 270 271 static const struct configfs_item_operations cma_device_item_ops = { 272 .release = release_cma_dev 273 }; 274 275 static const struct config_item_type cma_device_group_type = { 276 .ct_item_ops = &cma_device_item_ops, 277 .ct_owner = THIS_MODULE 278 }; 279 280 static struct config_group *make_cma_dev(struct config_group *group, 281 const char *name) 282 { 283 int err = -ENODEV; 284 struct cma_device *cma_dev = cma_enum_devices_by_ibdev(filter_by_name, 285 (void *)name); 286 struct cma_dev_group *cma_dev_group = NULL; 287 288 if (!cma_dev) 289 goto fail; 290 291 cma_dev_group = kzalloc_obj(*cma_dev_group); 292 293 if (!cma_dev_group) { 294 err = -ENOMEM; 295 goto fail; 296 } 297 298 strscpy(cma_dev_group->name, name, sizeof(cma_dev_group->name)); 299 300 config_group_init_type_name(&cma_dev_group->ports_group, "ports", 301 &cma_ports_group_type); 302 303 err = make_cma_ports(cma_dev_group, cma_dev); 304 if (err) 305 goto fail; 306 307 config_group_init_type_name(&cma_dev_group->device_group, name, 308 &cma_device_group_type); 309 configfs_add_default_group(&cma_dev_group->ports_group, 310 &cma_dev_group->device_group); 311 312 cma_dev_put(cma_dev); 313 return &cma_dev_group->device_group; 314 315 fail: 316 if (cma_dev) 317 cma_dev_put(cma_dev); 318 kfree(cma_dev_group); 319 return ERR_PTR(err); 320 } 321 322 static void drop_cma_dev(struct config_group *cgroup, struct config_item *item) 323 { 324 struct config_group *group = 325 container_of(item, struct config_group, cg_item); 326 struct cma_dev_group *cma_dev_group = 327 container_of(group, struct cma_dev_group, device_group); 328 329 configfs_remove_default_groups(&cma_dev_group->ports_group); 330 configfs_remove_default_groups(&cma_dev_group->device_group); 331 config_item_put(item); 332 } 333 334 static const struct configfs_group_operations cma_subsys_group_ops = { 335 .make_group = make_cma_dev, 336 .drop_item = drop_cma_dev, 337 }; 338 339 static const struct config_item_type cma_subsys_type = { 340 .ct_group_ops = &cma_subsys_group_ops, 341 .ct_owner = THIS_MODULE, 342 }; 343 344 static struct configfs_subsystem cma_subsys = { 345 .su_group = { 346 .cg_item = { 347 .ci_namebuf = "rdma_cm", 348 .ci_type = &cma_subsys_type, 349 }, 350 }, 351 }; 352 353 int __init cma_configfs_init(void) 354 { 355 int ret; 356 357 config_group_init(&cma_subsys.su_group); 358 mutex_init(&cma_subsys.su_mutex); 359 ret = configfs_register_subsystem(&cma_subsys); 360 if (ret) 361 mutex_destroy(&cma_subsys.su_mutex); 362 return ret; 363 } 364 365 void __exit cma_configfs_exit(void) 366 { 367 configfs_unregister_subsystem(&cma_subsys); 368 mutex_destroy(&cma_subsys.su_mutex); 369 } 370