Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2015, Mellanox Technologies inc.  All rights reserved.
0003  *
0004  * This software is available to you under a choice of one of two
0005  * licenses.  You may choose to be licensed under the terms of the GNU
0006  * General Public License (GPL) Version 2, available from the file
0007  * COPYING in the main directory of this source tree, or the
0008  * OpenIB.org BSD license below:
0009  *
0010  *     Redistribution and use in source and binary forms, with or
0011  *     without modification, are permitted provided that the following
0012  *     conditions are met:
0013  *
0014  *      - Redistributions of source code must retain the above
0015  *        copyright notice, this list of conditions and the following
0016  *        disclaimer.
0017  *
0018  *      - Redistributions in binary form must reproduce the above
0019  *        copyright notice, this list of conditions and the following
0020  *        disclaimer in the documentation and/or other materials
0021  *        provided with the distribution.
0022  *
0023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0024  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0025  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0026  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0027  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0028  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0029  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0030  * SOFTWARE.
0031  */
0032 
0033 #include <linux/configfs.h>
0034 #include <rdma/ib_verbs.h>
0035 #include <rdma/rdma_cm.h>
0036 
0037 #include "core_priv.h"
0038 #include "cma_priv.h"
0039 
0040 struct cma_device;
0041 
0042 struct cma_dev_group;
0043 
0044 struct cma_dev_port_group {
0045     u32         port_num;
0046     struct cma_dev_group    *cma_dev_group;
0047     struct config_group group;
0048 };
0049 
0050 struct cma_dev_group {
0051     char                name[IB_DEVICE_NAME_MAX];
0052     struct config_group     device_group;
0053     struct config_group     ports_group;
0054     struct cma_dev_port_group   *ports;
0055 };
0056 
0057 static struct cma_dev_port_group *to_dev_port_group(struct config_item *item)
0058 {
0059     struct config_group *group;
0060 
0061     if (!item)
0062         return NULL;
0063 
0064     group = container_of(item, struct config_group, cg_item);
0065     return container_of(group, struct cma_dev_port_group, group);
0066 }
0067 
0068 static bool filter_by_name(struct ib_device *ib_dev, void *cookie)
0069 {
0070     return !strcmp(dev_name(&ib_dev->dev), cookie);
0071 }
0072 
0073 static int cma_configfs_params_get(struct config_item *item,
0074                    struct cma_device **pcma_dev,
0075                    struct cma_dev_port_group **pgroup)
0076 {
0077     struct cma_dev_port_group *group = to_dev_port_group(item);
0078     struct cma_device *cma_dev;
0079 
0080     if (!group)
0081         return -ENODEV;
0082 
0083     cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
0084                         group->cma_dev_group->name);
0085     if (!cma_dev)
0086         return -ENODEV;
0087 
0088     *pcma_dev = cma_dev;
0089     *pgroup = group;
0090 
0091     return 0;
0092 }
0093 
0094 static void cma_configfs_params_put(struct cma_device *cma_dev)
0095 {
0096     cma_dev_put(cma_dev);
0097 }
0098 
0099 static ssize_t default_roce_mode_show(struct config_item *item,
0100                       char *buf)
0101 {
0102     struct cma_device *cma_dev;
0103     struct cma_dev_port_group *group;
0104     int gid_type;
0105     ssize_t ret;
0106 
0107     ret = cma_configfs_params_get(item, &cma_dev, &group);
0108     if (ret)
0109         return ret;
0110 
0111     gid_type = cma_get_default_gid_type(cma_dev, group->port_num);
0112     cma_configfs_params_put(cma_dev);
0113 
0114     if (gid_type < 0)
0115         return gid_type;
0116 
0117     return sysfs_emit(buf, "%s\n", ib_cache_gid_type_str(gid_type));
0118 }
0119 
0120 static ssize_t default_roce_mode_store(struct config_item *item,
0121                        const char *buf, size_t count)
0122 {
0123     struct cma_device *cma_dev;
0124     struct cma_dev_port_group *group;
0125     int gid_type;
0126     ssize_t ret;
0127 
0128     ret = cma_configfs_params_get(item, &cma_dev, &group);
0129     if (ret)
0130         return ret;
0131 
0132     gid_type = ib_cache_gid_parse_type_str(buf);
0133     if (gid_type < 0) {
0134         cma_configfs_params_put(cma_dev);
0135         return -EINVAL;
0136     }
0137 
0138     ret = cma_set_default_gid_type(cma_dev, group->port_num, gid_type);
0139 
0140     cma_configfs_params_put(cma_dev);
0141 
0142     return !ret ? strnlen(buf, count) : ret;
0143 }
0144 
0145 CONFIGFS_ATTR(, default_roce_mode);
0146 
0147 static ssize_t default_roce_tos_show(struct config_item *item, char *buf)
0148 {
0149     struct cma_device *cma_dev;
0150     struct cma_dev_port_group *group;
0151     ssize_t ret;
0152     u8 tos;
0153 
0154     ret = cma_configfs_params_get(item, &cma_dev, &group);
0155     if (ret)
0156         return ret;
0157 
0158     tos = cma_get_default_roce_tos(cma_dev, group->port_num);
0159     cma_configfs_params_put(cma_dev);
0160 
0161     return sysfs_emit(buf, "%u\n", tos);
0162 }
0163 
0164 static ssize_t default_roce_tos_store(struct config_item *item,
0165                       const char *buf, size_t count)
0166 {
0167     struct cma_device *cma_dev;
0168     struct cma_dev_port_group *group;
0169     ssize_t ret;
0170     u8 tos;
0171 
0172     ret = kstrtou8(buf, 0, &tos);
0173     if (ret)
0174         return ret;
0175 
0176     ret = cma_configfs_params_get(item, &cma_dev, &group);
0177     if (ret)
0178         return ret;
0179 
0180     ret = cma_set_default_roce_tos(cma_dev, group->port_num, tos);
0181     cma_configfs_params_put(cma_dev);
0182 
0183     return ret ? ret : strnlen(buf, count);
0184 }
0185 
0186 CONFIGFS_ATTR(, default_roce_tos);
0187 
0188 static struct configfs_attribute *cma_configfs_attributes[] = {
0189     &attr_default_roce_mode,
0190     &attr_default_roce_tos,
0191     NULL,
0192 };
0193 
0194 static const struct config_item_type cma_port_group_type = {
0195     .ct_attrs   = cma_configfs_attributes,
0196     .ct_owner   = THIS_MODULE
0197 };
0198 
0199 static int make_cma_ports(struct cma_dev_group *cma_dev_group,
0200               struct cma_device *cma_dev)
0201 {
0202     struct cma_dev_port_group *ports;
0203     struct ib_device *ibdev;
0204     u32 ports_num;
0205     u32 i;
0206 
0207     ibdev = cma_get_ib_dev(cma_dev);
0208 
0209     if (!ibdev)
0210         return -ENODEV;
0211 
0212     ports_num = ibdev->phys_port_cnt;
0213     ports = kcalloc(ports_num, sizeof(*cma_dev_group->ports),
0214             GFP_KERNEL);
0215 
0216     if (!ports)
0217         return -ENOMEM;
0218 
0219     for (i = 0; i < ports_num; i++) {
0220         char port_str[10];
0221 
0222         ports[i].port_num = i + 1;
0223         snprintf(port_str, sizeof(port_str), "%u", i + 1);
0224         ports[i].cma_dev_group = cma_dev_group;
0225         config_group_init_type_name(&ports[i].group,
0226                         port_str,
0227                         &cma_port_group_type);
0228         configfs_add_default_group(&ports[i].group,
0229                 &cma_dev_group->ports_group);
0230 
0231     }
0232     cma_dev_group->ports = ports;
0233     return 0;
0234 }
0235 
0236 static void release_cma_dev(struct config_item  *item)
0237 {
0238     struct config_group *group = container_of(item, struct config_group,
0239                           cg_item);
0240     struct cma_dev_group *cma_dev_group = container_of(group,
0241                                struct cma_dev_group,
0242                                device_group);
0243 
0244     kfree(cma_dev_group);
0245 };
0246 
0247 static void release_cma_ports_group(struct config_item  *item)
0248 {
0249     struct config_group *group = container_of(item, struct config_group,
0250                           cg_item);
0251     struct cma_dev_group *cma_dev_group = container_of(group,
0252                                struct cma_dev_group,
0253                                ports_group);
0254 
0255     kfree(cma_dev_group->ports);
0256     cma_dev_group->ports = NULL;
0257 };
0258 
0259 static struct configfs_item_operations cma_ports_item_ops = {
0260     .release = release_cma_ports_group
0261 };
0262 
0263 static const struct config_item_type cma_ports_group_type = {
0264     .ct_item_ops    = &cma_ports_item_ops,
0265     .ct_owner   = THIS_MODULE
0266 };
0267 
0268 static struct configfs_item_operations cma_device_item_ops = {
0269     .release = release_cma_dev
0270 };
0271 
0272 static const struct config_item_type cma_device_group_type = {
0273     .ct_item_ops    = &cma_device_item_ops,
0274     .ct_owner   = THIS_MODULE
0275 };
0276 
0277 static struct config_group *make_cma_dev(struct config_group *group,
0278                      const char *name)
0279 {
0280     int err = -ENODEV;
0281     struct cma_device *cma_dev = cma_enum_devices_by_ibdev(filter_by_name,
0282                                    (void *)name);
0283     struct cma_dev_group *cma_dev_group = NULL;
0284 
0285     if (!cma_dev)
0286         goto fail;
0287 
0288     cma_dev_group = kzalloc(sizeof(*cma_dev_group), GFP_KERNEL);
0289 
0290     if (!cma_dev_group) {
0291         err = -ENOMEM;
0292         goto fail;
0293     }
0294 
0295     strlcpy(cma_dev_group->name, name, sizeof(cma_dev_group->name));
0296 
0297     config_group_init_type_name(&cma_dev_group->ports_group, "ports",
0298                     &cma_ports_group_type);
0299 
0300     err = make_cma_ports(cma_dev_group, cma_dev);
0301     if (err)
0302         goto fail;
0303 
0304     config_group_init_type_name(&cma_dev_group->device_group, name,
0305                     &cma_device_group_type);
0306     configfs_add_default_group(&cma_dev_group->ports_group,
0307             &cma_dev_group->device_group);
0308 
0309     cma_dev_put(cma_dev);
0310     return &cma_dev_group->device_group;
0311 
0312 fail:
0313     if (cma_dev)
0314         cma_dev_put(cma_dev);
0315     kfree(cma_dev_group);
0316     return ERR_PTR(err);
0317 }
0318 
0319 static void drop_cma_dev(struct config_group *cgroup, struct config_item *item)
0320 {
0321     struct config_group *group =
0322         container_of(item, struct config_group, cg_item);
0323     struct cma_dev_group *cma_dev_group =
0324         container_of(group, struct cma_dev_group, device_group);
0325 
0326     configfs_remove_default_groups(&cma_dev_group->ports_group);
0327     configfs_remove_default_groups(&cma_dev_group->device_group);
0328     config_item_put(item);
0329 }
0330 
0331 static struct configfs_group_operations cma_subsys_group_ops = {
0332     .make_group = make_cma_dev,
0333     .drop_item  = drop_cma_dev,
0334 };
0335 
0336 static const struct config_item_type cma_subsys_type = {
0337     .ct_group_ops   = &cma_subsys_group_ops,
0338     .ct_owner   = THIS_MODULE,
0339 };
0340 
0341 static struct configfs_subsystem cma_subsys = {
0342     .su_group   = {
0343         .cg_item    = {
0344             .ci_namebuf = "rdma_cm",
0345             .ci_type    = &cma_subsys_type,
0346         },
0347     },
0348 };
0349 
0350 int __init cma_configfs_init(void)
0351 {
0352     int ret;
0353 
0354     config_group_init(&cma_subsys.su_group);
0355     mutex_init(&cma_subsys.su_mutex);
0356     ret = configfs_register_subsystem(&cma_subsys);
0357     if (ret)
0358         mutex_destroy(&cma_subsys.su_mutex);
0359     return ret;
0360 }
0361 
0362 void __exit cma_configfs_exit(void)
0363 {
0364     configfs_unregister_subsystem(&cma_subsys);
0365     mutex_destroy(&cma_subsys.su_mutex);
0366 }