xref: /OK3568_Linux_fs/kernel/drivers/infiniband/sw/rxe/rxe_sysfs.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4*4882a593Smuzhiyun  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include "rxe.h"
8*4882a593Smuzhiyun #include "rxe_net.h"
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /* Copy argument and remove trailing CR. Return the new length. */
sanitize_arg(const char * val,char * intf,int intf_len)11*4882a593Smuzhiyun static int sanitize_arg(const char *val, char *intf, int intf_len)
12*4882a593Smuzhiyun {
13*4882a593Smuzhiyun 	int len;
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun 	if (!val)
16*4882a593Smuzhiyun 		return 0;
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun 	/* Remove newline. */
19*4882a593Smuzhiyun 	for (len = 0; len < intf_len - 1 && val[len] && val[len] != '\n'; len++)
20*4882a593Smuzhiyun 		intf[len] = val[len];
21*4882a593Smuzhiyun 	intf[len] = 0;
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun 	if (len == 0 || (val[len] != 0 && val[len] != '\n'))
24*4882a593Smuzhiyun 		return 0;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	return len;
27*4882a593Smuzhiyun }
28*4882a593Smuzhiyun 
rxe_param_set_add(const char * val,const struct kernel_param * kp)29*4882a593Smuzhiyun static int rxe_param_set_add(const char *val, const struct kernel_param *kp)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	int len;
32*4882a593Smuzhiyun 	int err = 0;
33*4882a593Smuzhiyun 	char intf[32];
34*4882a593Smuzhiyun 	struct net_device *ndev;
35*4882a593Smuzhiyun 	struct rxe_dev *exists;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	if (!rxe_initialized) {
38*4882a593Smuzhiyun 		pr_err("Module parameters are not supported, use rdma link add or rxe_cfg\n");
39*4882a593Smuzhiyun 		return -EAGAIN;
40*4882a593Smuzhiyun 	}
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	len = sanitize_arg(val, intf, sizeof(intf));
43*4882a593Smuzhiyun 	if (!len) {
44*4882a593Smuzhiyun 		pr_err("add: invalid interface name\n");
45*4882a593Smuzhiyun 		return -EINVAL;
46*4882a593Smuzhiyun 	}
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	ndev = dev_get_by_name(&init_net, intf);
49*4882a593Smuzhiyun 	if (!ndev) {
50*4882a593Smuzhiyun 		pr_err("interface %s not found\n", intf);
51*4882a593Smuzhiyun 		return -EINVAL;
52*4882a593Smuzhiyun 	}
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	if (is_vlan_dev(ndev)) {
55*4882a593Smuzhiyun 		pr_err("rxe creation allowed on top of a real device only\n");
56*4882a593Smuzhiyun 		err = -EPERM;
57*4882a593Smuzhiyun 		goto err;
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	exists = rxe_get_dev_from_net(ndev);
61*4882a593Smuzhiyun 	if (exists) {
62*4882a593Smuzhiyun 		ib_device_put(&exists->ib_dev);
63*4882a593Smuzhiyun 		pr_err("already configured on %s\n", intf);
64*4882a593Smuzhiyun 		err = -EINVAL;
65*4882a593Smuzhiyun 		goto err;
66*4882a593Smuzhiyun 	}
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	err = rxe_net_add("rxe%d", ndev);
69*4882a593Smuzhiyun 	if (err) {
70*4882a593Smuzhiyun 		pr_err("failed to add %s\n", intf);
71*4882a593Smuzhiyun 		goto err;
72*4882a593Smuzhiyun 	}
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun err:
75*4882a593Smuzhiyun 	dev_put(ndev);
76*4882a593Smuzhiyun 	return err;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
rxe_param_set_remove(const char * val,const struct kernel_param * kp)79*4882a593Smuzhiyun static int rxe_param_set_remove(const char *val, const struct kernel_param *kp)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	int len;
82*4882a593Smuzhiyun 	char intf[32];
83*4882a593Smuzhiyun 	struct ib_device *ib_dev;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	len = sanitize_arg(val, intf, sizeof(intf));
86*4882a593Smuzhiyun 	if (!len) {
87*4882a593Smuzhiyun 		pr_err("add: invalid interface name\n");
88*4882a593Smuzhiyun 		return -EINVAL;
89*4882a593Smuzhiyun 	}
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	if (strncmp("all", intf, len) == 0) {
92*4882a593Smuzhiyun 		pr_info("rxe_sys: remove all");
93*4882a593Smuzhiyun 		ib_unregister_driver(RDMA_DRIVER_RXE);
94*4882a593Smuzhiyun 		return 0;
95*4882a593Smuzhiyun 	}
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	ib_dev = ib_device_get_by_name(intf, RDMA_DRIVER_RXE);
98*4882a593Smuzhiyun 	if (!ib_dev) {
99*4882a593Smuzhiyun 		pr_err("not configured on %s\n", intf);
100*4882a593Smuzhiyun 		return -EINVAL;
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	ib_unregister_device_and_put(ib_dev);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	return 0;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun static const struct kernel_param_ops rxe_add_ops = {
109*4882a593Smuzhiyun 	.set = rxe_param_set_add,
110*4882a593Smuzhiyun };
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun static const struct kernel_param_ops rxe_remove_ops = {
113*4882a593Smuzhiyun 	.set = rxe_param_set_remove,
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun module_param_cb(add, &rxe_add_ops, NULL, 0200);
117*4882a593Smuzhiyun MODULE_PARM_DESC(add, "DEPRECATED.  Create RXE device over network interface");
118*4882a593Smuzhiyun module_param_cb(remove, &rxe_remove_ops, NULL, 0200);
119*4882a593Smuzhiyun MODULE_PARM_DESC(remove, "DEPRECATED.  Remove RXE device over network interface");
120