xref: /openbmc/linux/drivers/infiniband/core/nldev.c (revision 9e2a187a)
16c80b41aSLeon Romanovsky /*
26c80b41aSLeon Romanovsky  * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
36c80b41aSLeon Romanovsky  *
46c80b41aSLeon Romanovsky  * Redistribution and use in source and binary forms, with or without
56c80b41aSLeon Romanovsky  * modification, are permitted provided that the following conditions are met:
66c80b41aSLeon Romanovsky  *
76c80b41aSLeon Romanovsky  * 1. Redistributions of source code must retain the above copyright
86c80b41aSLeon Romanovsky  *    notice, this list of conditions and the following disclaimer.
96c80b41aSLeon Romanovsky  * 2. Redistributions in binary form must reproduce the above copyright
106c80b41aSLeon Romanovsky  *    notice, this list of conditions and the following disclaimer in the
116c80b41aSLeon Romanovsky  *    documentation and/or other materials provided with the distribution.
126c80b41aSLeon Romanovsky  * 3. Neither the names of the copyright holders nor the names of its
136c80b41aSLeon Romanovsky  *    contributors may be used to endorse or promote products derived from
146c80b41aSLeon Romanovsky  *    this software without specific prior written permission.
156c80b41aSLeon Romanovsky  *
166c80b41aSLeon Romanovsky  * Alternatively, this software may be distributed under the terms of the
176c80b41aSLeon Romanovsky  * GNU General Public License ("GPL") version 2 as published by the Free
186c80b41aSLeon Romanovsky  * Software Foundation.
196c80b41aSLeon Romanovsky  *
206c80b41aSLeon Romanovsky  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
216c80b41aSLeon Romanovsky  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
226c80b41aSLeon Romanovsky  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236c80b41aSLeon Romanovsky  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
246c80b41aSLeon Romanovsky  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
256c80b41aSLeon Romanovsky  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
266c80b41aSLeon Romanovsky  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
276c80b41aSLeon Romanovsky  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
286c80b41aSLeon Romanovsky  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
296c80b41aSLeon Romanovsky  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
306c80b41aSLeon Romanovsky  * POSSIBILITY OF SUCH DAMAGE.
316c80b41aSLeon Romanovsky  */
326c80b41aSLeon Romanovsky 
33e3bf14bdSJason Gunthorpe #include <linux/module.h>
34bf3c5a93SLeon Romanovsky #include <linux/pid.h>
35bf3c5a93SLeon Romanovsky #include <linux/pid_namespace.h>
363856ec4bSSteve Wise #include <linux/mutex.h>
37b4c598a6SLeon Romanovsky #include <net/netlink.h>
3800313983SSteve Wise #include <rdma/rdma_cm.h>
396c80b41aSLeon Romanovsky #include <rdma/rdma_netlink.h>
406c80b41aSLeon Romanovsky 
416c80b41aSLeon Romanovsky #include "core_priv.h"
4200313983SSteve Wise #include "cma_priv.h"
4341eda65cSLeon Romanovsky #include "restrack.h"
445bd48c18SJason Gunthorpe #include "uverbs.h"
456c80b41aSLeon Romanovsky 
46fb910690SErez Alfasi typedef int (*res_fill_func_t)(struct sk_buff*, bool,
47fb910690SErez Alfasi 			       struct rdma_restrack_entry*, uint32_t);
48fb910690SErez Alfasi 
49696de2e9SDoug Ledford /*
50696de2e9SDoug Ledford  * Sort array elements by the netlink attribute name
51696de2e9SDoug Ledford  */
52b4c598a6SLeon Romanovsky static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
53696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_CHARDEV]		= { .type = NLA_U64 },
54696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_CHARDEV_ABI]		= { .type = NLA_U64 },
55696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_CHARDEV_NAME]		= { .type = NLA_NUL_STRING,
5634d65cd8SDoug Ledford 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
57696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_CHARDEV_TYPE]		= { .type = NLA_NUL_STRING,
5834d65cd8SDoug Ledford 					.len = RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE },
59f8fc8cd9SYamin Friedman 	[RDMA_NLDEV_ATTR_DEV_DIM]               = { .type = NLA_U8 },
60b4c598a6SLeon Romanovsky 	[RDMA_NLDEV_ATTR_DEV_INDEX]		= { .type = NLA_U32 },
61b4c598a6SLeon Romanovsky 	[RDMA_NLDEV_ATTR_DEV_NAME]		= { .type = NLA_NUL_STRING,
6234d65cd8SDoug Ledford 					.len = IB_DEVICE_NAME_MAX },
63696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_DEV_NODE_TYPE]		= { .type = NLA_U8 },
64696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_DEV_PROTOCOL]		= { .type = NLA_NUL_STRING,
6534d65cd8SDoug Ledford 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
66696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_DRIVER]		= { .type = NLA_NESTED },
67696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_DRIVER_ENTRY]		= { .type = NLA_NESTED },
68696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE]	= { .type = NLA_U8 },
69696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_DRIVER_STRING]		= { .type = NLA_NUL_STRING,
7034d65cd8SDoug Ledford 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
71696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_DRIVER_S32]		= { .type = NLA_S32 },
72696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_DRIVER_S64]		= { .type = NLA_S64 },
73696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_DRIVER_U32]		= { .type = NLA_U32 },
74696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_DRIVER_U64]		= { .type = NLA_U64 },
758621a7e3SLeon Romanovsky 	[RDMA_NLDEV_ATTR_FW_VERSION]		= { .type = NLA_NUL_STRING,
7634d65cd8SDoug Ledford 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
7780a06dd3SLeon Romanovsky 	[RDMA_NLDEV_ATTR_LID]			= { .type = NLA_U32 },
78696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_LINK_TYPE]		= { .type = NLA_NUL_STRING,
7934d65cd8SDoug Ledford 					.len = IFNAMSIZ },
8034840feaSLeon Romanovsky 	[RDMA_NLDEV_ATTR_LMC]			= { .type = NLA_U8 },
815b2cc79dSLeon Romanovsky 	[RDMA_NLDEV_ATTR_NDEV_INDEX]		= { .type = NLA_U32 },
825b2cc79dSLeon Romanovsky 	[RDMA_NLDEV_ATTR_NDEV_NAME]		= { .type = NLA_NUL_STRING,
835b2cc79dSLeon Romanovsky 					.len = IFNAMSIZ },
84696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_NODE_GUID]		= { .type = NLA_U64 },
85696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_PORT_INDEX]		= { .type = NLA_U32 },
86696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_PORT_PHYS_STATE]	= { .type = NLA_U8 },
87696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_PORT_STATE]		= { .type = NLA_U8 },
88696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_CM_ID]		= { .type = NLA_NESTED },
89517b773eSLeon Romanovsky 	[RDMA_NLDEV_ATTR_RES_CM_IDN]		= { .type = NLA_U32 },
90696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY]	= { .type = NLA_NESTED },
91696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_CQ]		= { .type = NLA_NESTED },
92696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_CQE]		= { .type = NLA_U32 },
93696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_CQN]		= { .type = NLA_U32 },
94696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_CQ_ENTRY]		= { .type = NLA_NESTED },
95c3d02788SLeon Romanovsky 	[RDMA_NLDEV_ATTR_RES_CTXN]		= { .type = NLA_U32 },
96696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_DST_ADDR]		= {
97696de2e9SDoug Ledford 			.len = sizeof(struct __kernel_sockaddr_storage) },
98696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_IOVA]		= { .type = NLA_U64 },
99696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_KERN_NAME]		= { .type = NLA_NUL_STRING,
10034d65cd8SDoug Ledford 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
101696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_LKEY]		= { .type = NLA_U32 },
102696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]	= { .type = NLA_U32 },
103696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_LQPN]		= { .type = NLA_U32 },
104696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_MR]		= { .type = NLA_NESTED },
105696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_MRLEN]		= { .type = NLA_U64 },
106696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_MRN]		= { .type = NLA_U32 },
107696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_MR_ENTRY]		= { .type = NLA_NESTED },
108696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]	= { .type = NLA_U8 },
109696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_PD]		= { .type = NLA_NESTED },
110696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_PDN]		= { .type = NLA_U32 },
111696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_PD_ENTRY]		= { .type = NLA_NESTED },
112696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_PID]		= { .type = NLA_U32 },
113696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_POLL_CTX]		= { .type = NLA_U8 },
114696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_PS]		= { .type = NLA_U32 },
115696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_QP]		= { .type = NLA_NESTED },
116696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_QP_ENTRY]		= { .type = NLA_NESTED },
117696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_RKEY]		= { .type = NLA_U32 },
118696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_RQPN]		= { .type = NLA_U32 },
119696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_RQ_PSN]		= { .type = NLA_U32 },
120696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_SQ_PSN]		= { .type = NLA_U32 },
121696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_SRC_ADDR]		= {
122696de2e9SDoug Ledford 			.len = sizeof(struct __kernel_sockaddr_storage) },
123696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_STATE]		= { .type = NLA_U8 },
124696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_SUMMARY]		= { .type = NLA_NESTED },
125696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY]	= { .type = NLA_NESTED },
126696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]= { .type = NLA_U64 },
127696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]= { .type = NLA_NUL_STRING,
12834d65cd8SDoug Ledford 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
129696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_TYPE]		= { .type = NLA_U8 },
130696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]= { .type = NLA_U32 },
131696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_RES_USECNT]		= { .type = NLA_U64 },
132696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_SM_LID]		= { .type = NLA_U32 },
133696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_SUBNET_PREFIX]		= { .type = NLA_U64 },
134b47ae6f8SMark Zhang 	[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]	= { .type = NLA_U32 },
135b47ae6f8SMark Zhang 	[RDMA_NLDEV_ATTR_STAT_MODE]		= { .type = NLA_U32 },
136b47ae6f8SMark Zhang 	[RDMA_NLDEV_ATTR_STAT_RES]		= { .type = NLA_U32 },
137c4ffee7cSMark Zhang 	[RDMA_NLDEV_ATTR_STAT_COUNTER]		= { .type = NLA_NESTED },
138c4ffee7cSMark Zhang 	[RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY]	= { .type = NLA_NESTED },
139c4ffee7cSMark Zhang 	[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]       = { .type = NLA_U32 },
140c4ffee7cSMark Zhang 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]       = { .type = NLA_NESTED },
141c4ffee7cSMark Zhang 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY]  = { .type = NLA_NESTED },
142c4ffee7cSMark Zhang 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME] = { .type = NLA_NUL_STRING },
143c4ffee7cSMark Zhang 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE] = { .type = NLA_U64 },
144696de2e9SDoug Ledford 	[RDMA_NLDEV_ATTR_SYS_IMAGE_GUID]	= { .type = NLA_U64 },
1458f71bb00SJason Gunthorpe 	[RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID]	= { .type = NLA_U32 },
146696de2e9SDoug Ledford 	[RDMA_NLDEV_NET_NS_FD]			= { .type = NLA_U32 },
147696de2e9SDoug Ledford 	[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]	= { .type = NLA_U8 },
148b4c598a6SLeon Romanovsky };
149b4c598a6SLeon Romanovsky 
15073937e8aSSteve Wise static int put_driver_name_print_type(struct sk_buff *msg, const char *name,
15173937e8aSSteve Wise 				      enum rdma_nldev_print_type print_type)
15273937e8aSSteve Wise {
15373937e8aSSteve Wise 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name))
15473937e8aSSteve Wise 		return -EMSGSIZE;
15573937e8aSSteve Wise 	if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC &&
15673937e8aSSteve Wise 	    nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type))
15773937e8aSSteve Wise 		return -EMSGSIZE;
15873937e8aSSteve Wise 
15973937e8aSSteve Wise 	return 0;
16073937e8aSSteve Wise }
16173937e8aSSteve Wise 
16273937e8aSSteve Wise static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name,
16373937e8aSSteve Wise 				   enum rdma_nldev_print_type print_type,
16473937e8aSSteve Wise 				   u32 value)
16573937e8aSSteve Wise {
16673937e8aSSteve Wise 	if (put_driver_name_print_type(msg, name, print_type))
16773937e8aSSteve Wise 		return -EMSGSIZE;
16873937e8aSSteve Wise 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value))
16973937e8aSSteve Wise 		return -EMSGSIZE;
17073937e8aSSteve Wise 
17173937e8aSSteve Wise 	return 0;
17273937e8aSSteve Wise }
17373937e8aSSteve Wise 
17473937e8aSSteve Wise static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name,
17573937e8aSSteve Wise 				   enum rdma_nldev_print_type print_type,
17673937e8aSSteve Wise 				   u64 value)
17773937e8aSSteve Wise {
17873937e8aSSteve Wise 	if (put_driver_name_print_type(msg, name, print_type))
17973937e8aSSteve Wise 		return -EMSGSIZE;
18073937e8aSSteve Wise 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value,
18173937e8aSSteve Wise 			      RDMA_NLDEV_ATTR_PAD))
18273937e8aSSteve Wise 		return -EMSGSIZE;
18373937e8aSSteve Wise 
18473937e8aSSteve Wise 	return 0;
18573937e8aSSteve Wise }
18673937e8aSSteve Wise 
187e1b95ae0SErez Alfasi int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
188e1b95ae0SErez Alfasi 			      const char *str)
189e1b95ae0SErez Alfasi {
190e1b95ae0SErez Alfasi 	if (put_driver_name_print_type(msg, name,
191e1b95ae0SErez Alfasi 				       RDMA_NLDEV_PRINT_TYPE_UNSPEC))
192e1b95ae0SErez Alfasi 		return -EMSGSIZE;
193e1b95ae0SErez Alfasi 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, str))
194e1b95ae0SErez Alfasi 		return -EMSGSIZE;
195e1b95ae0SErez Alfasi 
196e1b95ae0SErez Alfasi 	return 0;
197e1b95ae0SErez Alfasi }
198e1b95ae0SErez Alfasi EXPORT_SYMBOL(rdma_nl_put_driver_string);
199e1b95ae0SErez Alfasi 
20073937e8aSSteve Wise int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value)
20173937e8aSSteve Wise {
20273937e8aSSteve Wise 	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
20373937e8aSSteve Wise 				       value);
20473937e8aSSteve Wise }
20573937e8aSSteve Wise EXPORT_SYMBOL(rdma_nl_put_driver_u32);
20673937e8aSSteve Wise 
20773937e8aSSteve Wise int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
20873937e8aSSteve Wise 			       u32 value)
20973937e8aSSteve Wise {
21073937e8aSSteve Wise 	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
21173937e8aSSteve Wise 				       value);
21273937e8aSSteve Wise }
21373937e8aSSteve Wise EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex);
21473937e8aSSteve Wise 
21573937e8aSSteve Wise int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value)
21673937e8aSSteve Wise {
21773937e8aSSteve Wise 	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
21873937e8aSSteve Wise 				       value);
21973937e8aSSteve Wise }
22073937e8aSSteve Wise EXPORT_SYMBOL(rdma_nl_put_driver_u64);
22173937e8aSSteve Wise 
22273937e8aSSteve Wise int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value)
22373937e8aSSteve Wise {
22473937e8aSSteve Wise 	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
22573937e8aSSteve Wise 				       value);
22673937e8aSSteve Wise }
22773937e8aSSteve Wise EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex);
22873937e8aSSteve Wise 
229c2409810SLeon Romanovsky static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
230b4c598a6SLeon Romanovsky {
231b4c598a6SLeon Romanovsky 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
232b4c598a6SLeon Romanovsky 		return -EMSGSIZE;
233896de009SJason Gunthorpe 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME,
234896de009SJason Gunthorpe 			   dev_name(&device->dev)))
235b4c598a6SLeon Romanovsky 		return -EMSGSIZE;
236c2409810SLeon Romanovsky 
237c2409810SLeon Romanovsky 	return 0;
238c2409810SLeon Romanovsky }
239c2409810SLeon Romanovsky 
240c2409810SLeon Romanovsky static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
241c2409810SLeon Romanovsky {
242c2409810SLeon Romanovsky 	char fw[IB_FW_VERSION_NAME_MAX];
2439e886b39SLeon Romanovsky 	int ret = 0;
2449e886b39SLeon Romanovsky 	u8 port;
245c2409810SLeon Romanovsky 
246c2409810SLeon Romanovsky 	if (fill_nldev_handle(msg, device))
247c2409810SLeon Romanovsky 		return -EMSGSIZE;
248c2409810SLeon Romanovsky 
249b4c598a6SLeon Romanovsky 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
250b4c598a6SLeon Romanovsky 		return -EMSGSIZE;
251ac505253SLeon Romanovsky 
252ac505253SLeon Romanovsky 	BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
253ac505253SLeon Romanovsky 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
25425a0ad85SSteve Wise 			      device->attrs.device_cap_flags,
25525a0ad85SSteve Wise 			      RDMA_NLDEV_ATTR_PAD))
256ac505253SLeon Romanovsky 		return -EMSGSIZE;
257ac505253SLeon Romanovsky 
2588621a7e3SLeon Romanovsky 	ib_get_device_fw_str(device, fw);
2595b2cc79dSLeon Romanovsky 	/* Device without FW has strlen(fw) = 0 */
2608621a7e3SLeon Romanovsky 	if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
2618621a7e3SLeon Romanovsky 		return -EMSGSIZE;
2628621a7e3SLeon Romanovsky 
2631aaff896SLeon Romanovsky 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
26425a0ad85SSteve Wise 			      be64_to_cpu(device->node_guid),
26525a0ad85SSteve Wise 			      RDMA_NLDEV_ATTR_PAD))
2661aaff896SLeon Romanovsky 		return -EMSGSIZE;
2671aaff896SLeon Romanovsky 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
26825a0ad85SSteve Wise 			      be64_to_cpu(device->attrs.sys_image_guid),
26925a0ad85SSteve Wise 			      RDMA_NLDEV_ATTR_PAD))
2701aaff896SLeon Romanovsky 		return -EMSGSIZE;
2711bb77b8cSLeon Romanovsky 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
2721bb77b8cSLeon Romanovsky 		return -EMSGSIZE;
273f8fc8cd9SYamin Friedman 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, device->use_cq_dim))
274f8fc8cd9SYamin Friedman 		return -EMSGSIZE;
2759e886b39SLeon Romanovsky 
2769e886b39SLeon Romanovsky 	/*
2779e886b39SLeon Romanovsky 	 * Link type is determined on first port and mlx4 device
2789e886b39SLeon Romanovsky 	 * which can potentially have two different link type for the same
2799e886b39SLeon Romanovsky 	 * IB device is considered as better to be avoided in the future,
2809e886b39SLeon Romanovsky 	 */
2819e886b39SLeon Romanovsky 	port = rdma_start_port(device);
2829e886b39SLeon Romanovsky 	if (rdma_cap_opa_mad(device, port))
2839e886b39SLeon Romanovsky 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "opa");
2849e886b39SLeon Romanovsky 	else if (rdma_protocol_ib(device, port))
2859e886b39SLeon Romanovsky 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "ib");
2869e886b39SLeon Romanovsky 	else if (rdma_protocol_iwarp(device, port))
2879e886b39SLeon Romanovsky 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "iw");
2889e886b39SLeon Romanovsky 	else if (rdma_protocol_roce(device, port))
2899e886b39SLeon Romanovsky 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "roce");
2909e886b39SLeon Romanovsky 	else if (rdma_protocol_usnic(device, port))
2919e886b39SLeon Romanovsky 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL,
2929e886b39SLeon Romanovsky 				     "usnic");
2939e886b39SLeon Romanovsky 	return ret;
294b4c598a6SLeon Romanovsky }
295b4c598a6SLeon Romanovsky 
2967d02f605SLeon Romanovsky static int fill_port_info(struct sk_buff *msg,
2975b2cc79dSLeon Romanovsky 			  struct ib_device *device, u32 port,
2985b2cc79dSLeon Romanovsky 			  const struct net *net)
2997d02f605SLeon Romanovsky {
3005b2cc79dSLeon Romanovsky 	struct net_device *netdev = NULL;
301ac505253SLeon Romanovsky 	struct ib_port_attr attr;
302ac505253SLeon Romanovsky 	int ret;
3034fa2813dSMichael Guralnik 	u64 cap_flags = 0;
304ac505253SLeon Romanovsky 
305c2409810SLeon Romanovsky 	if (fill_nldev_handle(msg, device))
3067d02f605SLeon Romanovsky 		return -EMSGSIZE;
307c2409810SLeon Romanovsky 
3087d02f605SLeon Romanovsky 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
3097d02f605SLeon Romanovsky 		return -EMSGSIZE;
310ac505253SLeon Romanovsky 
311ac505253SLeon Romanovsky 	ret = ib_query_port(device, port, &attr);
312ac505253SLeon Romanovsky 	if (ret)
313ac505253SLeon Romanovsky 		return ret;
314ac505253SLeon Romanovsky 
315dd8028f1SLeon Romanovsky 	if (rdma_protocol_ib(device, port)) {
3164fa2813dSMichael Guralnik 		BUILD_BUG_ON((sizeof(attr.port_cap_flags) +
3174fa2813dSMichael Guralnik 				sizeof(attr.port_cap_flags2)) > sizeof(u64));
3184fa2813dSMichael Guralnik 		cap_flags = attr.port_cap_flags |
3194fa2813dSMichael Guralnik 			((u64)attr.port_cap_flags2 << 32);
320ac505253SLeon Romanovsky 		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
3214fa2813dSMichael Guralnik 				      cap_flags, RDMA_NLDEV_ATTR_PAD))
322ac505253SLeon Romanovsky 			return -EMSGSIZE;
323dd8028f1SLeon Romanovsky 		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
32425a0ad85SSteve Wise 				      attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD))
32512026fbbSLeon Romanovsky 			return -EMSGSIZE;
32680a06dd3SLeon Romanovsky 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
32780a06dd3SLeon Romanovsky 			return -EMSGSIZE;
32880a06dd3SLeon Romanovsky 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
32980a06dd3SLeon Romanovsky 			return -EMSGSIZE;
33034840feaSLeon Romanovsky 		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
33134840feaSLeon Romanovsky 			return -EMSGSIZE;
33280a06dd3SLeon Romanovsky 	}
3335654e49dSLeon Romanovsky 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
3345654e49dSLeon Romanovsky 		return -EMSGSIZE;
3355654e49dSLeon Romanovsky 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
3365654e49dSLeon Romanovsky 		return -EMSGSIZE;
3375b2cc79dSLeon Romanovsky 
338c2261dd7SJason Gunthorpe 	netdev = ib_device_get_netdev(device, port);
3395b2cc79dSLeon Romanovsky 	if (netdev && net_eq(dev_net(netdev), net)) {
3405b2cc79dSLeon Romanovsky 		ret = nla_put_u32(msg,
3415b2cc79dSLeon Romanovsky 				  RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
3425b2cc79dSLeon Romanovsky 		if (ret)
3435b2cc79dSLeon Romanovsky 			goto out;
3445b2cc79dSLeon Romanovsky 		ret = nla_put_string(msg,
3455b2cc79dSLeon Romanovsky 				     RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
3465b2cc79dSLeon Romanovsky 	}
3475b2cc79dSLeon Romanovsky 
3485b2cc79dSLeon Romanovsky out:
3495b2cc79dSLeon Romanovsky 	if (netdev)
3505b2cc79dSLeon Romanovsky 		dev_put(netdev);
3515b2cc79dSLeon Romanovsky 	return ret;
3527d02f605SLeon Romanovsky }
3537d02f605SLeon Romanovsky 
354bf3c5a93SLeon Romanovsky static int fill_res_info_entry(struct sk_buff *msg,
355bf3c5a93SLeon Romanovsky 			       const char *name, u64 curr)
356bf3c5a93SLeon Romanovsky {
357bf3c5a93SLeon Romanovsky 	struct nlattr *entry_attr;
358bf3c5a93SLeon Romanovsky 
359ae0be8deSMichal Kubecek 	entry_attr = nla_nest_start_noflag(msg,
360ae0be8deSMichal Kubecek 					   RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
361bf3c5a93SLeon Romanovsky 	if (!entry_attr)
362bf3c5a93SLeon Romanovsky 		return -EMSGSIZE;
363bf3c5a93SLeon Romanovsky 
364bf3c5a93SLeon Romanovsky 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
365bf3c5a93SLeon Romanovsky 		goto err;
36625a0ad85SSteve Wise 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr,
36725a0ad85SSteve Wise 			      RDMA_NLDEV_ATTR_PAD))
368bf3c5a93SLeon Romanovsky 		goto err;
369bf3c5a93SLeon Romanovsky 
370bf3c5a93SLeon Romanovsky 	nla_nest_end(msg, entry_attr);
371bf3c5a93SLeon Romanovsky 	return 0;
372bf3c5a93SLeon Romanovsky 
373bf3c5a93SLeon Romanovsky err:
374bf3c5a93SLeon Romanovsky 	nla_nest_cancel(msg, entry_attr);
375bf3c5a93SLeon Romanovsky 	return -EMSGSIZE;
376bf3c5a93SLeon Romanovsky }
377bf3c5a93SLeon Romanovsky 
378bf3c5a93SLeon Romanovsky static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
379bf3c5a93SLeon Romanovsky {
380bf3c5a93SLeon Romanovsky 	static const char * const names[RDMA_RESTRACK_MAX] = {
381bf3c5a93SLeon Romanovsky 		[RDMA_RESTRACK_PD] = "pd",
382bf3c5a93SLeon Romanovsky 		[RDMA_RESTRACK_CQ] = "cq",
383bf3c5a93SLeon Romanovsky 		[RDMA_RESTRACK_QP] = "qp",
38400313983SSteve Wise 		[RDMA_RESTRACK_CM_ID] = "cm_id",
385fccec5b8SSteve Wise 		[RDMA_RESTRACK_MR] = "mr",
386ffd321e4SLeon Romanovsky 		[RDMA_RESTRACK_CTX] = "ctx",
387bf3c5a93SLeon Romanovsky 	};
388bf3c5a93SLeon Romanovsky 
389bf3c5a93SLeon Romanovsky 	struct nlattr *table_attr;
390bf3c5a93SLeon Romanovsky 	int ret, i, curr;
391bf3c5a93SLeon Romanovsky 
392bf3c5a93SLeon Romanovsky 	if (fill_nldev_handle(msg, device))
393bf3c5a93SLeon Romanovsky 		return -EMSGSIZE;
394bf3c5a93SLeon Romanovsky 
395ae0be8deSMichal Kubecek 	table_attr = nla_nest_start_noflag(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
396bf3c5a93SLeon Romanovsky 	if (!table_attr)
397bf3c5a93SLeon Romanovsky 		return -EMSGSIZE;
398bf3c5a93SLeon Romanovsky 
399bf3c5a93SLeon Romanovsky 	for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
400bf3c5a93SLeon Romanovsky 		if (!names[i])
401bf3c5a93SLeon Romanovsky 			continue;
40260c78668SLeon Romanovsky 		curr = rdma_restrack_count(device, i);
403bf3c5a93SLeon Romanovsky 		ret = fill_res_info_entry(msg, names[i], curr);
404bf3c5a93SLeon Romanovsky 		if (ret)
405bf3c5a93SLeon Romanovsky 			goto err;
406bf3c5a93SLeon Romanovsky 	}
407bf3c5a93SLeon Romanovsky 
408bf3c5a93SLeon Romanovsky 	nla_nest_end(msg, table_attr);
409bf3c5a93SLeon Romanovsky 	return 0;
410bf3c5a93SLeon Romanovsky 
411bf3c5a93SLeon Romanovsky err:
412bf3c5a93SLeon Romanovsky 	nla_nest_cancel(msg, table_attr);
413bf3c5a93SLeon Romanovsky 	return ret;
414bf3c5a93SLeon Romanovsky }
415bf3c5a93SLeon Romanovsky 
41600313983SSteve Wise static int fill_res_name_pid(struct sk_buff *msg,
41700313983SSteve Wise 			     struct rdma_restrack_entry *res)
41800313983SSteve Wise {
419ac71ffcfSLeon Romanovsky 	int err = 0;
420ac71ffcfSLeon Romanovsky 
42100313983SSteve Wise 	/*
42200313983SSteve Wise 	 * For user resources, user is should read /proc/PID/comm to get the
42300313983SSteve Wise 	 * name of the task file.
42400313983SSteve Wise 	 */
42500313983SSteve Wise 	if (rdma_is_kernel_res(res)) {
426ac71ffcfSLeon Romanovsky 		err = nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
427ac71ffcfSLeon Romanovsky 				     res->kern_name);
42800313983SSteve Wise 	} else {
429ac71ffcfSLeon Romanovsky 		pid_t pid;
430ac71ffcfSLeon Romanovsky 
431ac71ffcfSLeon Romanovsky 		pid = task_pid_vnr(res->task);
432ac71ffcfSLeon Romanovsky 		/*
433ac71ffcfSLeon Romanovsky 		 * Task is dead and in zombie state.
434ac71ffcfSLeon Romanovsky 		 * There is no need to print PID anymore.
435ac71ffcfSLeon Romanovsky 		 */
436ac71ffcfSLeon Romanovsky 		if (pid)
437ac71ffcfSLeon Romanovsky 			/*
438ac71ffcfSLeon Romanovsky 			 * This part is racy, task can be killed and PID will
439ac71ffcfSLeon Romanovsky 			 * be zero right here but it is ok, next query won't
440ac71ffcfSLeon Romanovsky 			 * return PID. We don't promise real-time reflection
441ac71ffcfSLeon Romanovsky 			 * of SW objects.
442ac71ffcfSLeon Romanovsky 			 */
443ac71ffcfSLeon Romanovsky 			err = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID, pid);
44400313983SSteve Wise 	}
445ac71ffcfSLeon Romanovsky 
446ac71ffcfSLeon Romanovsky 	return err ? -EMSGSIZE : 0;
44700313983SSteve Wise }
44800313983SSteve Wise 
44902da3750SLeon Romanovsky static bool fill_res_entry(struct ib_device *dev, struct sk_buff *msg,
45002da3750SLeon Romanovsky 			   struct rdma_restrack_entry *res)
45102da3750SLeon Romanovsky {
45202da3750SLeon Romanovsky 	if (!dev->ops.fill_res_entry)
45302da3750SLeon Romanovsky 		return false;
45402da3750SLeon Romanovsky 	return dev->ops.fill_res_entry(msg, res);
45502da3750SLeon Romanovsky }
45602da3750SLeon Romanovsky 
457659067b0SLeon Romanovsky static int fill_res_qp_entry(struct sk_buff *msg, bool has_cap_net_admin,
458d12ff624SSteve Wise 			     struct rdma_restrack_entry *res, uint32_t port)
459b5fa635aSLeon Romanovsky {
460d12ff624SSteve Wise 	struct ib_qp *qp = container_of(res, struct ib_qp, res);
46102da3750SLeon Romanovsky 	struct ib_device *dev = qp->device;
462b5fa635aSLeon Romanovsky 	struct ib_qp_init_attr qp_init_attr;
463b5fa635aSLeon Romanovsky 	struct ib_qp_attr qp_attr;
464b5fa635aSLeon Romanovsky 	int ret;
465b5fa635aSLeon Romanovsky 
466b5fa635aSLeon Romanovsky 	ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
467b5fa635aSLeon Romanovsky 	if (ret)
468b5fa635aSLeon Romanovsky 		return ret;
469b5fa635aSLeon Romanovsky 
470b5fa635aSLeon Romanovsky 	if (port && port != qp_attr.port_num)
471c5dfe0eaSLeon Romanovsky 		return -EAGAIN;
472b5fa635aSLeon Romanovsky 
473b5fa635aSLeon Romanovsky 	/* In create_qp() port is not set yet */
474b5fa635aSLeon Romanovsky 	if (qp_attr.port_num &&
475b5fa635aSLeon Romanovsky 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp_attr.port_num))
476b5fa635aSLeon Romanovsky 		goto err;
477b5fa635aSLeon Romanovsky 
478b5fa635aSLeon Romanovsky 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num))
479b5fa635aSLeon Romanovsky 		goto err;
480b5fa635aSLeon Romanovsky 	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
481b5fa635aSLeon Romanovsky 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
482b5fa635aSLeon Romanovsky 				qp_attr.dest_qp_num))
483b5fa635aSLeon Romanovsky 			goto err;
484b5fa635aSLeon Romanovsky 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
485b5fa635aSLeon Romanovsky 				qp_attr.rq_psn))
486b5fa635aSLeon Romanovsky 			goto err;
487b5fa635aSLeon Romanovsky 	}
488b5fa635aSLeon Romanovsky 
489b5fa635aSLeon Romanovsky 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
490b5fa635aSLeon Romanovsky 		goto err;
491b5fa635aSLeon Romanovsky 
492b5fa635aSLeon Romanovsky 	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
493b5fa635aSLeon Romanovsky 	    qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
494b5fa635aSLeon Romanovsky 		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
495b5fa635aSLeon Romanovsky 			       qp_attr.path_mig_state))
496b5fa635aSLeon Romanovsky 			goto err;
497b5fa635aSLeon Romanovsky 	}
498b5fa635aSLeon Romanovsky 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
499b5fa635aSLeon Romanovsky 		goto err;
500b5fa635aSLeon Romanovsky 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
501b5fa635aSLeon Romanovsky 		goto err;
502b5fa635aSLeon Romanovsky 
503c3d02788SLeon Romanovsky 	if (!rdma_is_kernel_res(res) &&
504c3d02788SLeon Romanovsky 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, qp->pd->res.id))
505c3d02788SLeon Romanovsky 		goto err;
506c3d02788SLeon Romanovsky 
50700313983SSteve Wise 	if (fill_res_name_pid(msg, res))
508b5fa635aSLeon Romanovsky 		goto err;
50900313983SSteve Wise 
51002da3750SLeon Romanovsky 	if (fill_res_entry(dev, msg, res))
511da5c8507SSteve Wise 		goto err;
512da5c8507SSteve Wise 
51300313983SSteve Wise 	return 0;
51400313983SSteve Wise 
515c5dfe0eaSLeon Romanovsky err:	return -EMSGSIZE;
51600313983SSteve Wise }
51700313983SSteve Wise 
518659067b0SLeon Romanovsky static int fill_res_cm_id_entry(struct sk_buff *msg, bool has_cap_net_admin,
51900313983SSteve Wise 				struct rdma_restrack_entry *res, uint32_t port)
52000313983SSteve Wise {
52100313983SSteve Wise 	struct rdma_id_private *id_priv =
52200313983SSteve Wise 				container_of(res, struct rdma_id_private, res);
52302da3750SLeon Romanovsky 	struct ib_device *dev = id_priv->id.device;
52400313983SSteve Wise 	struct rdma_cm_id *cm_id = &id_priv->id;
52500313983SSteve Wise 
52600313983SSteve Wise 	if (port && port != cm_id->port_num)
52700313983SSteve Wise 		return 0;
52800313983SSteve Wise 
52900313983SSteve Wise 	if (cm_id->port_num &&
53000313983SSteve Wise 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
53100313983SSteve Wise 		goto err;
53200313983SSteve Wise 
53300313983SSteve Wise 	if (id_priv->qp_num) {
53400313983SSteve Wise 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
53500313983SSteve Wise 			goto err;
53600313983SSteve Wise 		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
537b5fa635aSLeon Romanovsky 			goto err;
538b5fa635aSLeon Romanovsky 	}
539b5fa635aSLeon Romanovsky 
54000313983SSteve Wise 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
54100313983SSteve Wise 		goto err;
54200313983SSteve Wise 
54300313983SSteve Wise 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
54400313983SSteve Wise 		goto err;
54500313983SSteve Wise 
54600313983SSteve Wise 	if (cm_id->route.addr.src_addr.ss_family &&
54700313983SSteve Wise 	    nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
54800313983SSteve Wise 		    sizeof(cm_id->route.addr.src_addr),
54900313983SSteve Wise 		    &cm_id->route.addr.src_addr))
55000313983SSteve Wise 		goto err;
55100313983SSteve Wise 	if (cm_id->route.addr.dst_addr.ss_family &&
55200313983SSteve Wise 	    nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
55300313983SSteve Wise 		    sizeof(cm_id->route.addr.dst_addr),
55400313983SSteve Wise 		    &cm_id->route.addr.dst_addr))
55500313983SSteve Wise 		goto err;
55600313983SSteve Wise 
557517b773eSLeon Romanovsky 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CM_IDN, res->id))
558517b773eSLeon Romanovsky 		goto err;
559517b773eSLeon Romanovsky 
56000313983SSteve Wise 	if (fill_res_name_pid(msg, res))
56100313983SSteve Wise 		goto err;
56200313983SSteve Wise 
56302da3750SLeon Romanovsky 	if (fill_res_entry(dev, msg, res))
564da5c8507SSteve Wise 		goto err;
565da5c8507SSteve Wise 
566b5fa635aSLeon Romanovsky 	return 0;
567b5fa635aSLeon Romanovsky 
568c5dfe0eaSLeon Romanovsky err: return -EMSGSIZE;
569b5fa635aSLeon Romanovsky }
570b5fa635aSLeon Romanovsky 
571659067b0SLeon Romanovsky static int fill_res_cq_entry(struct sk_buff *msg, bool has_cap_net_admin,
572a34fc089SSteve Wise 			     struct rdma_restrack_entry *res, uint32_t port)
573a34fc089SSteve Wise {
574a34fc089SSteve Wise 	struct ib_cq *cq = container_of(res, struct ib_cq, res);
57502da3750SLeon Romanovsky 	struct ib_device *dev = cq->device;
576a34fc089SSteve Wise 
577a34fc089SSteve Wise 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
578a34fc089SSteve Wise 		goto err;
579a34fc089SSteve Wise 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
58025a0ad85SSteve Wise 			      atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD))
581a34fc089SSteve Wise 		goto err;
582a34fc089SSteve Wise 
583a34fc089SSteve Wise 	/* Poll context is only valid for kernel CQs */
584a34fc089SSteve Wise 	if (rdma_is_kernel_res(res) &&
585a34fc089SSteve Wise 	    nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
586a34fc089SSteve Wise 		goto err;
587a34fc089SSteve Wise 
588f8fc8cd9SYamin Friedman 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, (cq->dim != NULL)))
589f8fc8cd9SYamin Friedman 		goto err;
590f8fc8cd9SYamin Friedman 
591517b773eSLeon Romanovsky 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN, res->id))
592517b773eSLeon Romanovsky 		goto err;
593c3d02788SLeon Romanovsky 	if (!rdma_is_kernel_res(res) &&
594c3d02788SLeon Romanovsky 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
5955bd48c18SJason Gunthorpe 			cq->uobject->uevent.uobject.context->res.id))
596c3d02788SLeon Romanovsky 		goto err;
597517b773eSLeon Romanovsky 
598a34fc089SSteve Wise 	if (fill_res_name_pid(msg, res))
599a34fc089SSteve Wise 		goto err;
600a34fc089SSteve Wise 
6019e2a187aSMaor Gottlieb 	if (dev->ops.fill_res_cq_entry)
6029e2a187aSMaor Gottlieb 		return dev->ops.fill_res_cq_entry(msg, cq);
603a34fc089SSteve Wise 	return 0;
604a34fc089SSteve Wise 
605c5dfe0eaSLeon Romanovsky err:	return -EMSGSIZE;
606a34fc089SSteve Wise }
607a34fc089SSteve Wise 
608659067b0SLeon Romanovsky static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
609fccec5b8SSteve Wise 			     struct rdma_restrack_entry *res, uint32_t port)
610fccec5b8SSteve Wise {
611fccec5b8SSteve Wise 	struct ib_mr *mr = container_of(res, struct ib_mr, res);
61202da3750SLeon Romanovsky 	struct ib_device *dev = mr->pd->device;
613fccec5b8SSteve Wise 
614659067b0SLeon Romanovsky 	if (has_cap_net_admin) {
615fccec5b8SSteve Wise 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
616fccec5b8SSteve Wise 			goto err;
617fccec5b8SSteve Wise 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
618fccec5b8SSteve Wise 			goto err;
619fccec5b8SSteve Wise 	}
620fccec5b8SSteve Wise 
62125a0ad85SSteve Wise 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length,
62225a0ad85SSteve Wise 			      RDMA_NLDEV_ATTR_PAD))
623fccec5b8SSteve Wise 		goto err;
624fccec5b8SSteve Wise 
625517b773eSLeon Romanovsky 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
626517b773eSLeon Romanovsky 		goto err;
627517b773eSLeon Romanovsky 
628c3d02788SLeon Romanovsky 	if (!rdma_is_kernel_res(res) &&
629c3d02788SLeon Romanovsky 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, mr->pd->res.id))
630c3d02788SLeon Romanovsky 		goto err;
631c3d02788SLeon Romanovsky 
632fccec5b8SSteve Wise 	if (fill_res_name_pid(msg, res))
633fccec5b8SSteve Wise 		goto err;
634fccec5b8SSteve Wise 
635f4434529SMaor Gottlieb 	if (dev->ops.fill_res_mr_entry)
636f4434529SMaor Gottlieb 		return dev->ops.fill_res_mr_entry(msg, mr);
637fccec5b8SSteve Wise 	return 0;
638fccec5b8SSteve Wise 
639c5dfe0eaSLeon Romanovsky err:	return -EMSGSIZE;
640fccec5b8SSteve Wise }
641fccec5b8SSteve Wise 
642659067b0SLeon Romanovsky static int fill_res_pd_entry(struct sk_buff *msg, bool has_cap_net_admin,
64329cf1351SSteve Wise 			     struct rdma_restrack_entry *res, uint32_t port)
64429cf1351SSteve Wise {
64529cf1351SSteve Wise 	struct ib_pd *pd = container_of(res, struct ib_pd, res);
64629cf1351SSteve Wise 
647659067b0SLeon Romanovsky 	if (has_cap_net_admin) {
64829cf1351SSteve Wise 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
64929cf1351SSteve Wise 				pd->local_dma_lkey))
65029cf1351SSteve Wise 			goto err;
65129cf1351SSteve Wise 		if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
65229cf1351SSteve Wise 		    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
65329cf1351SSteve Wise 				pd->unsafe_global_rkey))
65429cf1351SSteve Wise 			goto err;
65529cf1351SSteve Wise 	}
65629cf1351SSteve Wise 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
65725a0ad85SSteve Wise 			      atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD))
65829cf1351SSteve Wise 		goto err;
65929cf1351SSteve Wise 
660517b773eSLeon Romanovsky 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, res->id))
661517b773eSLeon Romanovsky 		goto err;
662517b773eSLeon Romanovsky 
663c3d02788SLeon Romanovsky 	if (!rdma_is_kernel_res(res) &&
664c3d02788SLeon Romanovsky 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
665c3d02788SLeon Romanovsky 			pd->uobject->context->res.id))
666c3d02788SLeon Romanovsky 		goto err;
667c3d02788SLeon Romanovsky 
66824fd6d6fSMaor Gottlieb 	return fill_res_name_pid(msg, res);
66929cf1351SSteve Wise 
670c5dfe0eaSLeon Romanovsky err:	return -EMSGSIZE;
67129cf1351SSteve Wise }
67229cf1351SSteve Wise 
673c4ffee7cSMark Zhang static int fill_stat_counter_mode(struct sk_buff *msg,
674c4ffee7cSMark Zhang 				  struct rdma_counter *counter)
675c4ffee7cSMark Zhang {
676c4ffee7cSMark Zhang 	struct rdma_counter_mode *m = &counter->mode;
677c4ffee7cSMark Zhang 
678c4ffee7cSMark Zhang 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, m->mode))
679c4ffee7cSMark Zhang 		return -EMSGSIZE;
680c4ffee7cSMark Zhang 
681c4ffee7cSMark Zhang 	if (m->mode == RDMA_COUNTER_MODE_AUTO)
682c4ffee7cSMark Zhang 		if ((m->mask & RDMA_COUNTER_MASK_QP_TYPE) &&
683c4ffee7cSMark Zhang 		    nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, m->param.qp_type))
684c4ffee7cSMark Zhang 			return -EMSGSIZE;
685c4ffee7cSMark Zhang 
686c4ffee7cSMark Zhang 	return 0;
687c4ffee7cSMark Zhang }
688c4ffee7cSMark Zhang 
689c4ffee7cSMark Zhang static int fill_stat_counter_qp_entry(struct sk_buff *msg, u32 qpn)
690c4ffee7cSMark Zhang {
691c4ffee7cSMark Zhang 	struct nlattr *entry_attr;
692c4ffee7cSMark Zhang 
693c4ffee7cSMark Zhang 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
694c4ffee7cSMark Zhang 	if (!entry_attr)
695c4ffee7cSMark Zhang 		return -EMSGSIZE;
696c4ffee7cSMark Zhang 
697c4ffee7cSMark Zhang 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn))
698c4ffee7cSMark Zhang 		goto err;
699c4ffee7cSMark Zhang 
700c4ffee7cSMark Zhang 	nla_nest_end(msg, entry_attr);
701c4ffee7cSMark Zhang 	return 0;
702c4ffee7cSMark Zhang 
703c4ffee7cSMark Zhang err:
704c4ffee7cSMark Zhang 	nla_nest_cancel(msg, entry_attr);
705c4ffee7cSMark Zhang 	return -EMSGSIZE;
706c4ffee7cSMark Zhang }
707c4ffee7cSMark Zhang 
708c4ffee7cSMark Zhang static int fill_stat_counter_qps(struct sk_buff *msg,
709c4ffee7cSMark Zhang 				 struct rdma_counter *counter)
710c4ffee7cSMark Zhang {
711c4ffee7cSMark Zhang 	struct rdma_restrack_entry *res;
712c4ffee7cSMark Zhang 	struct rdma_restrack_root *rt;
713c4ffee7cSMark Zhang 	struct nlattr *table_attr;
714c4ffee7cSMark Zhang 	struct ib_qp *qp = NULL;
715c4ffee7cSMark Zhang 	unsigned long id = 0;
716c4ffee7cSMark Zhang 	int ret = 0;
717c4ffee7cSMark Zhang 
718c4ffee7cSMark Zhang 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
719c4ffee7cSMark Zhang 
720c4ffee7cSMark Zhang 	rt = &counter->device->res[RDMA_RESTRACK_QP];
721c4ffee7cSMark Zhang 	xa_lock(&rt->xa);
722c4ffee7cSMark Zhang 	xa_for_each(&rt->xa, id, res) {
723c4ffee7cSMark Zhang 		qp = container_of(res, struct ib_qp, res);
724c4ffee7cSMark Zhang 		if (qp->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
725c4ffee7cSMark Zhang 			continue;
726c4ffee7cSMark Zhang 
727c4ffee7cSMark Zhang 		if (!qp->counter || (qp->counter->id != counter->id))
728c4ffee7cSMark Zhang 			continue;
729c4ffee7cSMark Zhang 
730c4ffee7cSMark Zhang 		ret = fill_stat_counter_qp_entry(msg, qp->qp_num);
731c4ffee7cSMark Zhang 		if (ret)
732c4ffee7cSMark Zhang 			goto err;
733c4ffee7cSMark Zhang 	}
734c4ffee7cSMark Zhang 
735c4ffee7cSMark Zhang 	xa_unlock(&rt->xa);
736c4ffee7cSMark Zhang 	nla_nest_end(msg, table_attr);
737c4ffee7cSMark Zhang 	return 0;
738c4ffee7cSMark Zhang 
739c4ffee7cSMark Zhang err:
740c4ffee7cSMark Zhang 	xa_unlock(&rt->xa);
741c4ffee7cSMark Zhang 	nla_nest_cancel(msg, table_attr);
742c4ffee7cSMark Zhang 	return ret;
743c4ffee7cSMark Zhang }
744c4ffee7cSMark Zhang 
7454061ff7aSErez Alfasi int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
7464061ff7aSErez Alfasi 				 u64 value)
747c4ffee7cSMark Zhang {
748c4ffee7cSMark Zhang 	struct nlattr *entry_attr;
749c4ffee7cSMark Zhang 
750c4ffee7cSMark Zhang 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY);
751c4ffee7cSMark Zhang 	if (!entry_attr)
752c4ffee7cSMark Zhang 		return -EMSGSIZE;
753c4ffee7cSMark Zhang 
754c4ffee7cSMark Zhang 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME,
755c4ffee7cSMark Zhang 			   name))
756c4ffee7cSMark Zhang 		goto err;
757c4ffee7cSMark Zhang 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE,
758c4ffee7cSMark Zhang 			      value, RDMA_NLDEV_ATTR_PAD))
759c4ffee7cSMark Zhang 		goto err;
760c4ffee7cSMark Zhang 
761c4ffee7cSMark Zhang 	nla_nest_end(msg, entry_attr);
762c4ffee7cSMark Zhang 	return 0;
763c4ffee7cSMark Zhang 
764c4ffee7cSMark Zhang err:
765c4ffee7cSMark Zhang 	nla_nest_cancel(msg, entry_attr);
766c4ffee7cSMark Zhang 	return -EMSGSIZE;
767c4ffee7cSMark Zhang }
7684061ff7aSErez Alfasi EXPORT_SYMBOL(rdma_nl_stat_hwcounter_entry);
7694061ff7aSErez Alfasi 
7704061ff7aSErez Alfasi static int fill_stat_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
7714061ff7aSErez Alfasi 			      struct rdma_restrack_entry *res, uint32_t port)
7724061ff7aSErez Alfasi {
7734061ff7aSErez Alfasi 	struct ib_mr *mr = container_of(res, struct ib_mr, res);
7744061ff7aSErez Alfasi 	struct ib_device *dev = mr->pd->device;
7754061ff7aSErez Alfasi 
7764061ff7aSErez Alfasi 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
7774061ff7aSErez Alfasi 		goto err;
7784061ff7aSErez Alfasi 
779f4434529SMaor Gottlieb 	if (dev->ops.fill_stat_mr_entry)
780f4434529SMaor Gottlieb 		return dev->ops.fill_stat_mr_entry(msg, mr);
7814061ff7aSErez Alfasi 	return 0;
7824061ff7aSErez Alfasi 
7834061ff7aSErez Alfasi err:
7844061ff7aSErez Alfasi 	return -EMSGSIZE;
7854061ff7aSErez Alfasi }
786c4ffee7cSMark Zhang 
787c4ffee7cSMark Zhang static int fill_stat_counter_hwcounters(struct sk_buff *msg,
788c4ffee7cSMark Zhang 					struct rdma_counter *counter)
789c4ffee7cSMark Zhang {
790c4ffee7cSMark Zhang 	struct rdma_hw_stats *st = counter->stats;
791c4ffee7cSMark Zhang 	struct nlattr *table_attr;
792c4ffee7cSMark Zhang 	int i;
793c4ffee7cSMark Zhang 
794c4ffee7cSMark Zhang 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
795c4ffee7cSMark Zhang 	if (!table_attr)
796c4ffee7cSMark Zhang 		return -EMSGSIZE;
797c4ffee7cSMark Zhang 
798c4ffee7cSMark Zhang 	for (i = 0; i < st->num_counters; i++)
7994061ff7aSErez Alfasi 		if (rdma_nl_stat_hwcounter_entry(msg, st->names[i], st->value[i]))
800c4ffee7cSMark Zhang 			goto err;
801c4ffee7cSMark Zhang 
802c4ffee7cSMark Zhang 	nla_nest_end(msg, table_attr);
803c4ffee7cSMark Zhang 	return 0;
804c4ffee7cSMark Zhang 
805c4ffee7cSMark Zhang err:
806c4ffee7cSMark Zhang 	nla_nest_cancel(msg, table_attr);
807c4ffee7cSMark Zhang 	return -EMSGSIZE;
808c4ffee7cSMark Zhang }
809c4ffee7cSMark Zhang 
810c4ffee7cSMark Zhang static int fill_res_counter_entry(struct sk_buff *msg, bool has_cap_net_admin,
811c4ffee7cSMark Zhang 				  struct rdma_restrack_entry *res,
812c4ffee7cSMark Zhang 				  uint32_t port)
813c4ffee7cSMark Zhang {
814c4ffee7cSMark Zhang 	struct rdma_counter *counter =
815c4ffee7cSMark Zhang 		container_of(res, struct rdma_counter, res);
816c4ffee7cSMark Zhang 
817c4ffee7cSMark Zhang 	if (port && port != counter->port)
818a15542bbSMark Zhang 		return -EAGAIN;
819c4ffee7cSMark Zhang 
820c4ffee7cSMark Zhang 	/* Dump it even query failed */
821c4ffee7cSMark Zhang 	rdma_counter_query_stats(counter);
822c4ffee7cSMark Zhang 
823c4ffee7cSMark Zhang 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, counter->port) ||
824c4ffee7cSMark Zhang 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, counter->id) ||
825c4ffee7cSMark Zhang 	    fill_res_name_pid(msg, &counter->res) ||
826c4ffee7cSMark Zhang 	    fill_stat_counter_mode(msg, counter) ||
827c4ffee7cSMark Zhang 	    fill_stat_counter_qps(msg, counter) ||
828c4ffee7cSMark Zhang 	    fill_stat_counter_hwcounters(msg, counter))
829c4ffee7cSMark Zhang 		return -EMSGSIZE;
830c4ffee7cSMark Zhang 
831c4ffee7cSMark Zhang 	return 0;
832c4ffee7cSMark Zhang }
833c4ffee7cSMark Zhang 
834e5c9469eSLeon Romanovsky static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
835e5c9469eSLeon Romanovsky 			  struct netlink_ext_ack *extack)
836e5c9469eSLeon Romanovsky {
837e5c9469eSLeon Romanovsky 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
838e5c9469eSLeon Romanovsky 	struct ib_device *device;
839e5c9469eSLeon Romanovsky 	struct sk_buff *msg;
840e5c9469eSLeon Romanovsky 	u32 index;
841e5c9469eSLeon Romanovsky 	int err;
842e5c9469eSLeon Romanovsky 
8438cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
844e5c9469eSLeon Romanovsky 				     nldev_policy, extack);
845e5c9469eSLeon Romanovsky 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
846e5c9469eSLeon Romanovsky 		return -EINVAL;
847e5c9469eSLeon Romanovsky 
848e5c9469eSLeon Romanovsky 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
849e5c9469eSLeon Romanovsky 
85037eeab55SParav Pandit 	device = ib_device_get_by_index(sock_net(skb->sk), index);
851e5c9469eSLeon Romanovsky 	if (!device)
852e5c9469eSLeon Romanovsky 		return -EINVAL;
853e5c9469eSLeon Romanovsky 
854e5c9469eSLeon Romanovsky 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
855f8978bd9SLeon Romanovsky 	if (!msg) {
856f8978bd9SLeon Romanovsky 		err = -ENOMEM;
857f8978bd9SLeon Romanovsky 		goto err;
858f8978bd9SLeon Romanovsky 	}
859e5c9469eSLeon Romanovsky 
860e5c9469eSLeon Romanovsky 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
861e5c9469eSLeon Romanovsky 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
862e5c9469eSLeon Romanovsky 			0, 0);
863e5c9469eSLeon Romanovsky 
864e5c9469eSLeon Romanovsky 	err = fill_dev_info(msg, device);
865f8978bd9SLeon Romanovsky 	if (err)
866f8978bd9SLeon Romanovsky 		goto err_free;
867e5c9469eSLeon Romanovsky 
868e5c9469eSLeon Romanovsky 	nlmsg_end(msg, nlh);
869e5c9469eSLeon Romanovsky 
87001b67117SParav Pandit 	ib_device_put(device);
8711d2fedd8SParav Pandit 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
872f8978bd9SLeon Romanovsky 
873f8978bd9SLeon Romanovsky err_free:
874f8978bd9SLeon Romanovsky 	nlmsg_free(msg);
875f8978bd9SLeon Romanovsky err:
87601b67117SParav Pandit 	ib_device_put(device);
877f8978bd9SLeon Romanovsky 	return err;
878e5c9469eSLeon Romanovsky }
879e5c9469eSLeon Romanovsky 
88005d940d3SLeon Romanovsky static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
88105d940d3SLeon Romanovsky 			  struct netlink_ext_ack *extack)
88205d940d3SLeon Romanovsky {
88305d940d3SLeon Romanovsky 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
88405d940d3SLeon Romanovsky 	struct ib_device *device;
88505d940d3SLeon Romanovsky 	u32 index;
88605d940d3SLeon Romanovsky 	int err;
88705d940d3SLeon Romanovsky 
8888cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
8898cb08174SJohannes Berg 				     nldev_policy, extack);
89005d940d3SLeon Romanovsky 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
89105d940d3SLeon Romanovsky 		return -EINVAL;
89205d940d3SLeon Romanovsky 
89305d940d3SLeon Romanovsky 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
89437eeab55SParav Pandit 	device = ib_device_get_by_index(sock_net(skb->sk), index);
89505d940d3SLeon Romanovsky 	if (!device)
89605d940d3SLeon Romanovsky 		return -EINVAL;
89705d940d3SLeon Romanovsky 
89805d940d3SLeon Romanovsky 	if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) {
89905d940d3SLeon Romanovsky 		char name[IB_DEVICE_NAME_MAX] = {};
90005d940d3SLeon Romanovsky 
90105d940d3SLeon Romanovsky 		nla_strlcpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
90205d940d3SLeon Romanovsky 			    IB_DEVICE_NAME_MAX);
9037aefa623SJason Gunthorpe 		if (strlen(name) == 0) {
9047aefa623SJason Gunthorpe 			err = -EINVAL;
9057aefa623SJason Gunthorpe 			goto done;
9067aefa623SJason Gunthorpe 		}
90705d940d3SLeon Romanovsky 		err = ib_device_rename(device, name);
9082e5b8a01SParav Pandit 		goto done;
90905d940d3SLeon Romanovsky 	}
91005d940d3SLeon Romanovsky 
9112e5b8a01SParav Pandit 	if (tb[RDMA_NLDEV_NET_NS_FD]) {
9122e5b8a01SParav Pandit 		u32 ns_fd;
9132e5b8a01SParav Pandit 
9142e5b8a01SParav Pandit 		ns_fd = nla_get_u32(tb[RDMA_NLDEV_NET_NS_FD]);
9152e5b8a01SParav Pandit 		err = ib_device_set_netns_put(skb, device, ns_fd);
9162e5b8a01SParav Pandit 		goto put_done;
9172e5b8a01SParav Pandit 	}
9182e5b8a01SParav Pandit 
919f8fc8cd9SYamin Friedman 	if (tb[RDMA_NLDEV_ATTR_DEV_DIM]) {
920f8fc8cd9SYamin Friedman 		u8 use_dim;
921f8fc8cd9SYamin Friedman 
922f8fc8cd9SYamin Friedman 		use_dim = nla_get_u8(tb[RDMA_NLDEV_ATTR_DEV_DIM]);
923f8fc8cd9SYamin Friedman 		err = ib_device_set_dim(device,  use_dim);
924f8fc8cd9SYamin Friedman 		goto done;
925f8fc8cd9SYamin Friedman 	}
926f8fc8cd9SYamin Friedman 
9272e5b8a01SParav Pandit done:
92801b67117SParav Pandit 	ib_device_put(device);
9292e5b8a01SParav Pandit put_done:
93005d940d3SLeon Romanovsky 	return err;
93105d940d3SLeon Romanovsky }
93205d940d3SLeon Romanovsky 
933b4c598a6SLeon Romanovsky static int _nldev_get_dumpit(struct ib_device *device,
934b4c598a6SLeon Romanovsky 			     struct sk_buff *skb,
935b4c598a6SLeon Romanovsky 			     struct netlink_callback *cb,
936b4c598a6SLeon Romanovsky 			     unsigned int idx)
937b4c598a6SLeon Romanovsky {
938b4c598a6SLeon Romanovsky 	int start = cb->args[0];
939b4c598a6SLeon Romanovsky 	struct nlmsghdr *nlh;
940b4c598a6SLeon Romanovsky 
941b4c598a6SLeon Romanovsky 	if (idx < start)
942b4c598a6SLeon Romanovsky 		return 0;
943b4c598a6SLeon Romanovsky 
944b4c598a6SLeon Romanovsky 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
945b4c598a6SLeon Romanovsky 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
946b4c598a6SLeon Romanovsky 			0, NLM_F_MULTI);
947b4c598a6SLeon Romanovsky 
948b4c598a6SLeon Romanovsky 	if (fill_dev_info(skb, device)) {
949b4c598a6SLeon Romanovsky 		nlmsg_cancel(skb, nlh);
950b4c598a6SLeon Romanovsky 		goto out;
951b4c598a6SLeon Romanovsky 	}
952b4c598a6SLeon Romanovsky 
953b4c598a6SLeon Romanovsky 	nlmsg_end(skb, nlh);
954b4c598a6SLeon Romanovsky 
955b4c598a6SLeon Romanovsky 	idx++;
956b4c598a6SLeon Romanovsky 
957b4c598a6SLeon Romanovsky out:	cb->args[0] = idx;
958b4c598a6SLeon Romanovsky 	return skb->len;
959b4c598a6SLeon Romanovsky }
960b4c598a6SLeon Romanovsky 
961b4c598a6SLeon Romanovsky static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
962b4c598a6SLeon Romanovsky {
963b4c598a6SLeon Romanovsky 	/*
964b4c598a6SLeon Romanovsky 	 * There is no need to take lock, because
96537eeab55SParav Pandit 	 * we are relying on ib_core's locking.
966b4c598a6SLeon Romanovsky 	 */
967b4c598a6SLeon Romanovsky 	return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
968b4c598a6SLeon Romanovsky }
969b4c598a6SLeon Romanovsky 
970c3f66f7bSLeon Romanovsky static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
971c3f66f7bSLeon Romanovsky 			       struct netlink_ext_ack *extack)
972c3f66f7bSLeon Romanovsky {
973c3f66f7bSLeon Romanovsky 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
974c3f66f7bSLeon Romanovsky 	struct ib_device *device;
975c3f66f7bSLeon Romanovsky 	struct sk_buff *msg;
976c3f66f7bSLeon Romanovsky 	u32 index;
977c3f66f7bSLeon Romanovsky 	u32 port;
978c3f66f7bSLeon Romanovsky 	int err;
979c3f66f7bSLeon Romanovsky 
9808cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
981c3f66f7bSLeon Romanovsky 				     nldev_policy, extack);
982287683d0SLeon Romanovsky 	if (err ||
983287683d0SLeon Romanovsky 	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
984287683d0SLeon Romanovsky 	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
985c3f66f7bSLeon Romanovsky 		return -EINVAL;
986c3f66f7bSLeon Romanovsky 
987c3f66f7bSLeon Romanovsky 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
98837eeab55SParav Pandit 	device = ib_device_get_by_index(sock_net(skb->sk), index);
989c3f66f7bSLeon Romanovsky 	if (!device)
990c3f66f7bSLeon Romanovsky 		return -EINVAL;
991c3f66f7bSLeon Romanovsky 
992c3f66f7bSLeon Romanovsky 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
993f8978bd9SLeon Romanovsky 	if (!rdma_is_port_valid(device, port)) {
994f8978bd9SLeon Romanovsky 		err = -EINVAL;
995f8978bd9SLeon Romanovsky 		goto err;
996f8978bd9SLeon Romanovsky 	}
997c3f66f7bSLeon Romanovsky 
998c3f66f7bSLeon Romanovsky 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
999f8978bd9SLeon Romanovsky 	if (!msg) {
1000f8978bd9SLeon Romanovsky 		err = -ENOMEM;
1001f8978bd9SLeon Romanovsky 		goto err;
1002f8978bd9SLeon Romanovsky 	}
1003c3f66f7bSLeon Romanovsky 
1004c3f66f7bSLeon Romanovsky 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1005c3f66f7bSLeon Romanovsky 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1006c3f66f7bSLeon Romanovsky 			0, 0);
1007c3f66f7bSLeon Romanovsky 
10085b2cc79dSLeon Romanovsky 	err = fill_port_info(msg, device, port, sock_net(skb->sk));
1009f8978bd9SLeon Romanovsky 	if (err)
1010f8978bd9SLeon Romanovsky 		goto err_free;
1011c3f66f7bSLeon Romanovsky 
1012c3f66f7bSLeon Romanovsky 	nlmsg_end(msg, nlh);
101301b67117SParav Pandit 	ib_device_put(device);
1014c3f66f7bSLeon Romanovsky 
10151d2fedd8SParav Pandit 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1016f8978bd9SLeon Romanovsky 
1017f8978bd9SLeon Romanovsky err_free:
1018f8978bd9SLeon Romanovsky 	nlmsg_free(msg);
1019f8978bd9SLeon Romanovsky err:
102001b67117SParav Pandit 	ib_device_put(device);
1021f8978bd9SLeon Romanovsky 	return err;
1022c3f66f7bSLeon Romanovsky }
1023c3f66f7bSLeon Romanovsky 
10247d02f605SLeon Romanovsky static int nldev_port_get_dumpit(struct sk_buff *skb,
10257d02f605SLeon Romanovsky 				 struct netlink_callback *cb)
10267d02f605SLeon Romanovsky {
10277d02f605SLeon Romanovsky 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
10287d02f605SLeon Romanovsky 	struct ib_device *device;
10297d02f605SLeon Romanovsky 	int start = cb->args[0];
10307d02f605SLeon Romanovsky 	struct nlmsghdr *nlh;
10317d02f605SLeon Romanovsky 	u32 idx = 0;
10327d02f605SLeon Romanovsky 	u32 ifindex;
10337d02f605SLeon Romanovsky 	int err;
1034ea1075edSJason Gunthorpe 	unsigned int p;
10357d02f605SLeon Romanovsky 
10368cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
10377d02f605SLeon Romanovsky 				     nldev_policy, NULL);
10387d02f605SLeon Romanovsky 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
10397d02f605SLeon Romanovsky 		return -EINVAL;
10407d02f605SLeon Romanovsky 
10417d02f605SLeon Romanovsky 	ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
104237eeab55SParav Pandit 	device = ib_device_get_by_index(sock_net(skb->sk), ifindex);
10437d02f605SLeon Romanovsky 	if (!device)
10447d02f605SLeon Romanovsky 		return -EINVAL;
10457d02f605SLeon Romanovsky 
1046ea1075edSJason Gunthorpe 	rdma_for_each_port (device, p) {
10477d02f605SLeon Romanovsky 		/*
10487d02f605SLeon Romanovsky 		 * The dumpit function returns all information from specific
10497d02f605SLeon Romanovsky 		 * index. This specific index is taken from the netlink
10507d02f605SLeon Romanovsky 		 * messages request sent by user and it is available
10517d02f605SLeon Romanovsky 		 * in cb->args[0].
10527d02f605SLeon Romanovsky 		 *
10537d02f605SLeon Romanovsky 		 * Usually, the user doesn't fill this field and it causes
10547d02f605SLeon Romanovsky 		 * to return everything.
10557d02f605SLeon Romanovsky 		 *
10567d02f605SLeon Romanovsky 		 */
10577d02f605SLeon Romanovsky 		if (idx < start) {
10587d02f605SLeon Romanovsky 			idx++;
10597d02f605SLeon Romanovsky 			continue;
10607d02f605SLeon Romanovsky 		}
10617d02f605SLeon Romanovsky 
10627d02f605SLeon Romanovsky 		nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
10637d02f605SLeon Romanovsky 				cb->nlh->nlmsg_seq,
10647d02f605SLeon Romanovsky 				RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
10657d02f605SLeon Romanovsky 						 RDMA_NLDEV_CMD_PORT_GET),
10667d02f605SLeon Romanovsky 				0, NLM_F_MULTI);
10677d02f605SLeon Romanovsky 
10685b2cc79dSLeon Romanovsky 		if (fill_port_info(skb, device, p, sock_net(skb->sk))) {
10697d02f605SLeon Romanovsky 			nlmsg_cancel(skb, nlh);
10707d02f605SLeon Romanovsky 			goto out;
10717d02f605SLeon Romanovsky 		}
10727d02f605SLeon Romanovsky 		idx++;
10737d02f605SLeon Romanovsky 		nlmsg_end(skb, nlh);
10747d02f605SLeon Romanovsky 	}
10757d02f605SLeon Romanovsky 
1076f8978bd9SLeon Romanovsky out:
107701b67117SParav Pandit 	ib_device_put(device);
1078f8978bd9SLeon Romanovsky 	cb->args[0] = idx;
10797d02f605SLeon Romanovsky 	return skb->len;
10807d02f605SLeon Romanovsky }
10817d02f605SLeon Romanovsky 
1082bf3c5a93SLeon Romanovsky static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1083bf3c5a93SLeon Romanovsky 			      struct netlink_ext_ack *extack)
1084bf3c5a93SLeon Romanovsky {
1085bf3c5a93SLeon Romanovsky 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1086bf3c5a93SLeon Romanovsky 	struct ib_device *device;
1087bf3c5a93SLeon Romanovsky 	struct sk_buff *msg;
1088bf3c5a93SLeon Romanovsky 	u32 index;
1089bf3c5a93SLeon Romanovsky 	int ret;
1090bf3c5a93SLeon Romanovsky 
10918cb08174SJohannes Berg 	ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1092bf3c5a93SLeon Romanovsky 				     nldev_policy, extack);
1093bf3c5a93SLeon Romanovsky 	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1094bf3c5a93SLeon Romanovsky 		return -EINVAL;
1095bf3c5a93SLeon Romanovsky 
1096bf3c5a93SLeon Romanovsky 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
109737eeab55SParav Pandit 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1098bf3c5a93SLeon Romanovsky 	if (!device)
1099bf3c5a93SLeon Romanovsky 		return -EINVAL;
1100bf3c5a93SLeon Romanovsky 
1101bf3c5a93SLeon Romanovsky 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1102f34727a1SDan Carpenter 	if (!msg) {
1103f34727a1SDan Carpenter 		ret = -ENOMEM;
1104bf3c5a93SLeon Romanovsky 		goto err;
1105f34727a1SDan Carpenter 	}
1106bf3c5a93SLeon Romanovsky 
1107bf3c5a93SLeon Romanovsky 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1108bf3c5a93SLeon Romanovsky 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1109bf3c5a93SLeon Romanovsky 			0, 0);
1110bf3c5a93SLeon Romanovsky 
1111bf3c5a93SLeon Romanovsky 	ret = fill_res_info(msg, device);
1112bf3c5a93SLeon Romanovsky 	if (ret)
1113bf3c5a93SLeon Romanovsky 		goto err_free;
1114bf3c5a93SLeon Romanovsky 
1115bf3c5a93SLeon Romanovsky 	nlmsg_end(msg, nlh);
111601b67117SParav Pandit 	ib_device_put(device);
11171d2fedd8SParav Pandit 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1118bf3c5a93SLeon Romanovsky 
1119bf3c5a93SLeon Romanovsky err_free:
1120bf3c5a93SLeon Romanovsky 	nlmsg_free(msg);
1121bf3c5a93SLeon Romanovsky err:
112201b67117SParav Pandit 	ib_device_put(device);
1123bf3c5a93SLeon Romanovsky 	return ret;
1124bf3c5a93SLeon Romanovsky }
1125bf3c5a93SLeon Romanovsky 
1126bf3c5a93SLeon Romanovsky static int _nldev_res_get_dumpit(struct ib_device *device,
1127bf3c5a93SLeon Romanovsky 				 struct sk_buff *skb,
1128bf3c5a93SLeon Romanovsky 				 struct netlink_callback *cb,
1129bf3c5a93SLeon Romanovsky 				 unsigned int idx)
1130bf3c5a93SLeon Romanovsky {
1131bf3c5a93SLeon Romanovsky 	int start = cb->args[0];
1132bf3c5a93SLeon Romanovsky 	struct nlmsghdr *nlh;
1133bf3c5a93SLeon Romanovsky 
1134bf3c5a93SLeon Romanovsky 	if (idx < start)
1135bf3c5a93SLeon Romanovsky 		return 0;
1136bf3c5a93SLeon Romanovsky 
1137bf3c5a93SLeon Romanovsky 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1138bf3c5a93SLeon Romanovsky 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1139bf3c5a93SLeon Romanovsky 			0, NLM_F_MULTI);
1140bf3c5a93SLeon Romanovsky 
1141bf3c5a93SLeon Romanovsky 	if (fill_res_info(skb, device)) {
1142bf3c5a93SLeon Romanovsky 		nlmsg_cancel(skb, nlh);
1143bf3c5a93SLeon Romanovsky 		goto out;
1144bf3c5a93SLeon Romanovsky 	}
1145bf3c5a93SLeon Romanovsky 	nlmsg_end(skb, nlh);
1146bf3c5a93SLeon Romanovsky 
1147bf3c5a93SLeon Romanovsky 	idx++;
1148bf3c5a93SLeon Romanovsky 
1149bf3c5a93SLeon Romanovsky out:
1150bf3c5a93SLeon Romanovsky 	cb->args[0] = idx;
1151bf3c5a93SLeon Romanovsky 	return skb->len;
1152bf3c5a93SLeon Romanovsky }
1153bf3c5a93SLeon Romanovsky 
1154bf3c5a93SLeon Romanovsky static int nldev_res_get_dumpit(struct sk_buff *skb,
1155bf3c5a93SLeon Romanovsky 				struct netlink_callback *cb)
1156bf3c5a93SLeon Romanovsky {
1157bf3c5a93SLeon Romanovsky 	return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
1158bf3c5a93SLeon Romanovsky }
1159bf3c5a93SLeon Romanovsky 
1160d12ff624SSteve Wise struct nldev_fill_res_entry {
1161d12ff624SSteve Wise 	enum rdma_nldev_attr nldev_attr;
1162d12ff624SSteve Wise 	enum rdma_nldev_command nldev_cmd;
1163c5dfe0eaSLeon Romanovsky 	u8 flags;
1164c5dfe0eaSLeon Romanovsky 	u32 entry;
1165c5dfe0eaSLeon Romanovsky 	u32 id;
1166c5dfe0eaSLeon Romanovsky };
1167c5dfe0eaSLeon Romanovsky 
1168c5dfe0eaSLeon Romanovsky enum nldev_res_flags {
1169c5dfe0eaSLeon Romanovsky 	NLDEV_PER_DEV = 1 << 0,
1170d12ff624SSteve Wise };
1171d12ff624SSteve Wise 
1172d12ff624SSteve Wise static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
1173d12ff624SSteve Wise 	[RDMA_RESTRACK_QP] = {
1174d12ff624SSteve Wise 		.nldev_cmd = RDMA_NLDEV_CMD_RES_QP_GET,
1175d12ff624SSteve Wise 		.nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
1176c5dfe0eaSLeon Romanovsky 		.entry = RDMA_NLDEV_ATTR_RES_QP_ENTRY,
11771b8b7788SLeon Romanovsky 		.id = RDMA_NLDEV_ATTR_RES_LQPN,
1178d12ff624SSteve Wise 	},
117900313983SSteve Wise 	[RDMA_RESTRACK_CM_ID] = {
118000313983SSteve Wise 		.nldev_cmd = RDMA_NLDEV_CMD_RES_CM_ID_GET,
118100313983SSteve Wise 		.nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
1182c5dfe0eaSLeon Romanovsky 		.entry = RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY,
1183517b773eSLeon Romanovsky 		.id = RDMA_NLDEV_ATTR_RES_CM_IDN,
118400313983SSteve Wise 	},
1185a34fc089SSteve Wise 	[RDMA_RESTRACK_CQ] = {
1186a34fc089SSteve Wise 		.nldev_cmd = RDMA_NLDEV_CMD_RES_CQ_GET,
1187a34fc089SSteve Wise 		.nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
1188c5dfe0eaSLeon Romanovsky 		.flags = NLDEV_PER_DEV,
1189c5dfe0eaSLeon Romanovsky 		.entry = RDMA_NLDEV_ATTR_RES_CQ_ENTRY,
1190517b773eSLeon Romanovsky 		.id = RDMA_NLDEV_ATTR_RES_CQN,
1191a34fc089SSteve Wise 	},
1192fccec5b8SSteve Wise 	[RDMA_RESTRACK_MR] = {
1193fccec5b8SSteve Wise 		.nldev_cmd = RDMA_NLDEV_CMD_RES_MR_GET,
1194fccec5b8SSteve Wise 		.nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
1195c5dfe0eaSLeon Romanovsky 		.flags = NLDEV_PER_DEV,
1196c5dfe0eaSLeon Romanovsky 		.entry = RDMA_NLDEV_ATTR_RES_MR_ENTRY,
1197517b773eSLeon Romanovsky 		.id = RDMA_NLDEV_ATTR_RES_MRN,
1198fccec5b8SSteve Wise 	},
119929cf1351SSteve Wise 	[RDMA_RESTRACK_PD] = {
120029cf1351SSteve Wise 		.nldev_cmd = RDMA_NLDEV_CMD_RES_PD_GET,
120129cf1351SSteve Wise 		.nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
1202c5dfe0eaSLeon Romanovsky 		.flags = NLDEV_PER_DEV,
1203c5dfe0eaSLeon Romanovsky 		.entry = RDMA_NLDEV_ATTR_RES_PD_ENTRY,
1204517b773eSLeon Romanovsky 		.id = RDMA_NLDEV_ATTR_RES_PDN,
120529cf1351SSteve Wise 	},
1206c4ffee7cSMark Zhang 	[RDMA_RESTRACK_COUNTER] = {
1207c4ffee7cSMark Zhang 		.nldev_cmd = RDMA_NLDEV_CMD_STAT_GET,
1208c4ffee7cSMark Zhang 		.nldev_attr = RDMA_NLDEV_ATTR_STAT_COUNTER,
1209c4ffee7cSMark Zhang 		.entry = RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY,
1210c4ffee7cSMark Zhang 		.id = RDMA_NLDEV_ATTR_STAT_COUNTER_ID,
1211c4ffee7cSMark Zhang 	},
1212d12ff624SSteve Wise };
1213d12ff624SSteve Wise 
1214c5dfe0eaSLeon Romanovsky static int res_get_common_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1215c5dfe0eaSLeon Romanovsky 			       struct netlink_ext_ack *extack,
1216fb910690SErez Alfasi 			       enum rdma_restrack_type res_type,
1217fb910690SErez Alfasi 			       res_fill_func_t fill_func)
1218c5dfe0eaSLeon Romanovsky {
1219c5dfe0eaSLeon Romanovsky 	const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1220c5dfe0eaSLeon Romanovsky 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1221c5dfe0eaSLeon Romanovsky 	struct rdma_restrack_entry *res;
1222c5dfe0eaSLeon Romanovsky 	struct ib_device *device;
1223c5dfe0eaSLeon Romanovsky 	u32 index, id, port = 0;
1224c5dfe0eaSLeon Romanovsky 	bool has_cap_net_admin;
1225c5dfe0eaSLeon Romanovsky 	struct sk_buff *msg;
1226c5dfe0eaSLeon Romanovsky 	int ret;
1227c5dfe0eaSLeon Romanovsky 
12288cb08174SJohannes Berg 	ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1229c5dfe0eaSLeon Romanovsky 				     nldev_policy, extack);
1230c5dfe0eaSLeon Romanovsky 	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !fe->id || !tb[fe->id])
1231c5dfe0eaSLeon Romanovsky 		return -EINVAL;
1232c5dfe0eaSLeon Romanovsky 
1233c5dfe0eaSLeon Romanovsky 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
123437eeab55SParav Pandit 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1235c5dfe0eaSLeon Romanovsky 	if (!device)
1236c5dfe0eaSLeon Romanovsky 		return -EINVAL;
1237c5dfe0eaSLeon Romanovsky 
1238c5dfe0eaSLeon Romanovsky 	if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1239c5dfe0eaSLeon Romanovsky 		port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1240c5dfe0eaSLeon Romanovsky 		if (!rdma_is_port_valid(device, port)) {
1241c5dfe0eaSLeon Romanovsky 			ret = -EINVAL;
1242c5dfe0eaSLeon Romanovsky 			goto err;
1243c5dfe0eaSLeon Romanovsky 		}
1244c5dfe0eaSLeon Romanovsky 	}
1245c5dfe0eaSLeon Romanovsky 
1246c5dfe0eaSLeon Romanovsky 	if ((port && fe->flags & NLDEV_PER_DEV) ||
1247c5dfe0eaSLeon Romanovsky 	    (!port && ~fe->flags & NLDEV_PER_DEV)) {
1248c5dfe0eaSLeon Romanovsky 		ret = -EINVAL;
1249c5dfe0eaSLeon Romanovsky 		goto err;
1250c5dfe0eaSLeon Romanovsky 	}
1251c5dfe0eaSLeon Romanovsky 
1252c5dfe0eaSLeon Romanovsky 	id = nla_get_u32(tb[fe->id]);
1253c5dfe0eaSLeon Romanovsky 	res = rdma_restrack_get_byid(device, res_type, id);
1254c5dfe0eaSLeon Romanovsky 	if (IS_ERR(res)) {
1255c5dfe0eaSLeon Romanovsky 		ret = PTR_ERR(res);
1256c5dfe0eaSLeon Romanovsky 		goto err;
1257c5dfe0eaSLeon Romanovsky 	}
1258c5dfe0eaSLeon Romanovsky 
1259c5dfe0eaSLeon Romanovsky 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1260c5dfe0eaSLeon Romanovsky 	if (!msg) {
1261c5dfe0eaSLeon Romanovsky 		ret = -ENOMEM;
1262ab59ca3eSChristophe JAILLET 		goto err_get;
1263c5dfe0eaSLeon Romanovsky 	}
1264c5dfe0eaSLeon Romanovsky 
1265c5dfe0eaSLeon Romanovsky 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1266c5dfe0eaSLeon Romanovsky 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, fe->nldev_cmd),
1267c5dfe0eaSLeon Romanovsky 			0, 0);
1268c5dfe0eaSLeon Romanovsky 
1269c5dfe0eaSLeon Romanovsky 	if (fill_nldev_handle(msg, device)) {
1270c5dfe0eaSLeon Romanovsky 		ret = -EMSGSIZE;
1271c5dfe0eaSLeon Romanovsky 		goto err_free;
1272c5dfe0eaSLeon Romanovsky 	}
1273c5dfe0eaSLeon Romanovsky 
1274c5dfe0eaSLeon Romanovsky 	has_cap_net_admin = netlink_capable(skb, CAP_NET_ADMIN);
1275fb910690SErez Alfasi 
1276fb910690SErez Alfasi 	ret = fill_func(msg, has_cap_net_admin, res, port);
1277c5dfe0eaSLeon Romanovsky 	if (ret)
1278c5dfe0eaSLeon Romanovsky 		goto err_free;
1279c5dfe0eaSLeon Romanovsky 
128050bbe3d3SMaor Gottlieb 	rdma_restrack_put(res);
1281c5dfe0eaSLeon Romanovsky 	nlmsg_end(msg, nlh);
1282c5dfe0eaSLeon Romanovsky 	ib_device_put(device);
12831d2fedd8SParav Pandit 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1284c5dfe0eaSLeon Romanovsky 
1285c5dfe0eaSLeon Romanovsky err_free:
1286c5dfe0eaSLeon Romanovsky 	nlmsg_free(msg);
1287c5dfe0eaSLeon Romanovsky err_get:
1288c5dfe0eaSLeon Romanovsky 	rdma_restrack_put(res);
1289c5dfe0eaSLeon Romanovsky err:
1290c5dfe0eaSLeon Romanovsky 	ib_device_put(device);
1291c5dfe0eaSLeon Romanovsky 	return ret;
1292c5dfe0eaSLeon Romanovsky }
1293c5dfe0eaSLeon Romanovsky 
1294d12ff624SSteve Wise static int res_get_common_dumpit(struct sk_buff *skb,
1295d12ff624SSteve Wise 				 struct netlink_callback *cb,
1296fb910690SErez Alfasi 				 enum rdma_restrack_type res_type,
1297fb910690SErez Alfasi 				 res_fill_func_t fill_func)
1298b5fa635aSLeon Romanovsky {
1299d12ff624SSteve Wise 	const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1300b5fa635aSLeon Romanovsky 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1301b5fa635aSLeon Romanovsky 	struct rdma_restrack_entry *res;
13027c77c6a9SLeon Romanovsky 	struct rdma_restrack_root *rt;
1303b5fa635aSLeon Romanovsky 	int err, ret = 0, idx = 0;
1304b5fa635aSLeon Romanovsky 	struct nlattr *table_attr;
1305c5dfe0eaSLeon Romanovsky 	struct nlattr *entry_attr;
1306b5fa635aSLeon Romanovsky 	struct ib_device *device;
1307b5fa635aSLeon Romanovsky 	int start = cb->args[0];
1308659067b0SLeon Romanovsky 	bool has_cap_net_admin;
1309b5fa635aSLeon Romanovsky 	struct nlmsghdr *nlh;
1310fd47c2f9SLeon Romanovsky 	unsigned long id;
1311b5fa635aSLeon Romanovsky 	u32 index, port = 0;
1312d12ff624SSteve Wise 	bool filled = false;
1313b5fa635aSLeon Romanovsky 
13148cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1315b5fa635aSLeon Romanovsky 				     nldev_policy, NULL);
1316b5fa635aSLeon Romanovsky 	/*
1317d12ff624SSteve Wise 	 * Right now, we are expecting the device index to get res information,
1318b5fa635aSLeon Romanovsky 	 * but it is possible to extend this code to return all devices in
1319b5fa635aSLeon Romanovsky 	 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
1320b5fa635aSLeon Romanovsky 	 * if it doesn't exist, we will iterate over all devices.
1321b5fa635aSLeon Romanovsky 	 *
1322b5fa635aSLeon Romanovsky 	 * But it is not needed for now.
1323b5fa635aSLeon Romanovsky 	 */
1324b5fa635aSLeon Romanovsky 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1325b5fa635aSLeon Romanovsky 		return -EINVAL;
1326b5fa635aSLeon Romanovsky 
1327b5fa635aSLeon Romanovsky 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
132837eeab55SParav Pandit 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1329b5fa635aSLeon Romanovsky 	if (!device)
1330b5fa635aSLeon Romanovsky 		return -EINVAL;
1331b5fa635aSLeon Romanovsky 
1332b5fa635aSLeon Romanovsky 	/*
1333b5fa635aSLeon Romanovsky 	 * If no PORT_INDEX is supplied, we will return all QPs from that device
1334b5fa635aSLeon Romanovsky 	 */
1335b5fa635aSLeon Romanovsky 	if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1336b5fa635aSLeon Romanovsky 		port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1337b5fa635aSLeon Romanovsky 		if (!rdma_is_port_valid(device, port)) {
1338b5fa635aSLeon Romanovsky 			ret = -EINVAL;
1339b5fa635aSLeon Romanovsky 			goto err_index;
1340b5fa635aSLeon Romanovsky 		}
1341b5fa635aSLeon Romanovsky 	}
1342b5fa635aSLeon Romanovsky 
1343b5fa635aSLeon Romanovsky 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1344d12ff624SSteve Wise 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, fe->nldev_cmd),
1345b5fa635aSLeon Romanovsky 			0, NLM_F_MULTI);
1346b5fa635aSLeon Romanovsky 
1347b5fa635aSLeon Romanovsky 	if (fill_nldev_handle(skb, device)) {
1348b5fa635aSLeon Romanovsky 		ret = -EMSGSIZE;
1349b5fa635aSLeon Romanovsky 		goto err;
1350b5fa635aSLeon Romanovsky 	}
1351b5fa635aSLeon Romanovsky 
1352ae0be8deSMichal Kubecek 	table_attr = nla_nest_start_noflag(skb, fe->nldev_attr);
1353b5fa635aSLeon Romanovsky 	if (!table_attr) {
1354b5fa635aSLeon Romanovsky 		ret = -EMSGSIZE;
1355b5fa635aSLeon Romanovsky 		goto err;
1356b5fa635aSLeon Romanovsky 	}
1357b5fa635aSLeon Romanovsky 
1358659067b0SLeon Romanovsky 	has_cap_net_admin = netlink_capable(cb->skb, CAP_NET_ADMIN);
1359659067b0SLeon Romanovsky 
13607c77c6a9SLeon Romanovsky 	rt = &device->res[res_type];
13617c77c6a9SLeon Romanovsky 	xa_lock(&rt->xa);
1362fd47c2f9SLeon Romanovsky 	/*
1363fd47c2f9SLeon Romanovsky 	 * FIXME: if the skip ahead is something common this loop should
1364fd47c2f9SLeon Romanovsky 	 * use xas_for_each & xas_pause to optimize, we can have a lot of
1365fd47c2f9SLeon Romanovsky 	 * objects.
1366fd47c2f9SLeon Romanovsky 	 */
13677c77c6a9SLeon Romanovsky 	xa_for_each(&rt->xa, id, res) {
1368f2a0e45fSLeon Romanovsky 		if (idx < start || !rdma_restrack_get(res))
1369b5fa635aSLeon Romanovsky 			goto next;
1370b5fa635aSLeon Romanovsky 
13717c77c6a9SLeon Romanovsky 		xa_unlock(&rt->xa);
13727c77c6a9SLeon Romanovsky 
1373d12ff624SSteve Wise 		filled = true;
1374b5fa635aSLeon Romanovsky 
1375ae0be8deSMichal Kubecek 		entry_attr = nla_nest_start_noflag(skb, fe->entry);
1376c5dfe0eaSLeon Romanovsky 		if (!entry_attr) {
1377c5dfe0eaSLeon Romanovsky 			ret = -EMSGSIZE;
1378c5dfe0eaSLeon Romanovsky 			rdma_restrack_put(res);
13797c77c6a9SLeon Romanovsky 			goto msg_full;
1380c5dfe0eaSLeon Romanovsky 		}
1381c5dfe0eaSLeon Romanovsky 
1382fb910690SErez Alfasi 		ret = fill_func(skb, has_cap_net_admin, res, port);
1383fb910690SErez Alfasi 
1384b5fa635aSLeon Romanovsky 		rdma_restrack_put(res);
1385b5fa635aSLeon Romanovsky 
13867c77c6a9SLeon Romanovsky 		if (ret) {
1387c5dfe0eaSLeon Romanovsky 			nla_nest_cancel(skb, entry_attr);
1388b5fa635aSLeon Romanovsky 			if (ret == -EMSGSIZE)
13897c77c6a9SLeon Romanovsky 				goto msg_full;
1390c5dfe0eaSLeon Romanovsky 			if (ret == -EAGAIN)
13917c77c6a9SLeon Romanovsky 				goto again;
1392b5fa635aSLeon Romanovsky 			goto res_err;
13937c77c6a9SLeon Romanovsky 		}
1394c5dfe0eaSLeon Romanovsky 		nla_nest_end(skb, entry_attr);
13957c77c6a9SLeon Romanovsky again:		xa_lock(&rt->xa);
1396b5fa635aSLeon Romanovsky next:		idx++;
1397b5fa635aSLeon Romanovsky 	}
13987c77c6a9SLeon Romanovsky 	xa_unlock(&rt->xa);
1399b5fa635aSLeon Romanovsky 
14007c77c6a9SLeon Romanovsky msg_full:
1401b5fa635aSLeon Romanovsky 	nla_nest_end(skb, table_attr);
1402b5fa635aSLeon Romanovsky 	nlmsg_end(skb, nlh);
1403b5fa635aSLeon Romanovsky 	cb->args[0] = idx;
1404b5fa635aSLeon Romanovsky 
1405b5fa635aSLeon Romanovsky 	/*
1406d12ff624SSteve Wise 	 * No more entries to fill, cancel the message and
1407b5fa635aSLeon Romanovsky 	 * return 0 to mark end of dumpit.
1408b5fa635aSLeon Romanovsky 	 */
1409d12ff624SSteve Wise 	if (!filled)
1410b5fa635aSLeon Romanovsky 		goto err;
1411b5fa635aSLeon Romanovsky 
141201b67117SParav Pandit 	ib_device_put(device);
1413b5fa635aSLeon Romanovsky 	return skb->len;
1414b5fa635aSLeon Romanovsky 
1415b5fa635aSLeon Romanovsky res_err:
1416b5fa635aSLeon Romanovsky 	nla_nest_cancel(skb, table_attr);
1417b5fa635aSLeon Romanovsky 
1418b5fa635aSLeon Romanovsky err:
1419b5fa635aSLeon Romanovsky 	nlmsg_cancel(skb, nlh);
1420b5fa635aSLeon Romanovsky 
1421b5fa635aSLeon Romanovsky err_index:
142201b67117SParav Pandit 	ib_device_put(device);
1423b5fa635aSLeon Romanovsky 	return ret;
1424b5fa635aSLeon Romanovsky }
1425b5fa635aSLeon Romanovsky 
1426f732e713SLeon Romanovsky #define RES_GET_FUNCS(name, type)					       \
1427f732e713SLeon Romanovsky 	static int nldev_res_get_##name##_dumpit(struct sk_buff *skb,	       \
1428f732e713SLeon Romanovsky 						 struct netlink_callback *cb)  \
1429f732e713SLeon Romanovsky 	{								       \
1430fb910690SErez Alfasi 		return res_get_common_dumpit(skb, cb, type,		       \
1431fb910690SErez Alfasi 					     fill_res_##name##_entry);	       \
1432c5dfe0eaSLeon Romanovsky 	}								       \
1433c5dfe0eaSLeon Romanovsky 	static int nldev_res_get_##name##_doit(struct sk_buff *skb,	       \
1434c5dfe0eaSLeon Romanovsky 					       struct nlmsghdr *nlh,	       \
1435c5dfe0eaSLeon Romanovsky 					       struct netlink_ext_ack *extack) \
1436c5dfe0eaSLeon Romanovsky 	{								       \
1437fb910690SErez Alfasi 		return res_get_common_doit(skb, nlh, extack, type,	       \
1438fb910690SErez Alfasi 					   fill_res_##name##_entry);	       \
1439d12ff624SSteve Wise 	}
1440d12ff624SSteve Wise 
1441f732e713SLeon Romanovsky RES_GET_FUNCS(qp, RDMA_RESTRACK_QP);
1442f732e713SLeon Romanovsky RES_GET_FUNCS(cm_id, RDMA_RESTRACK_CM_ID);
1443f732e713SLeon Romanovsky RES_GET_FUNCS(cq, RDMA_RESTRACK_CQ);
1444f732e713SLeon Romanovsky RES_GET_FUNCS(pd, RDMA_RESTRACK_PD);
1445f732e713SLeon Romanovsky RES_GET_FUNCS(mr, RDMA_RESTRACK_MR);
1446c4ffee7cSMark Zhang RES_GET_FUNCS(counter, RDMA_RESTRACK_COUNTER);
144729cf1351SSteve Wise 
14483856ec4bSSteve Wise static LIST_HEAD(link_ops);
14493856ec4bSSteve Wise static DECLARE_RWSEM(link_ops_rwsem);
14503856ec4bSSteve Wise 
14513856ec4bSSteve Wise static const struct rdma_link_ops *link_ops_get(const char *type)
14523856ec4bSSteve Wise {
14533856ec4bSSteve Wise 	const struct rdma_link_ops *ops;
14543856ec4bSSteve Wise 
14553856ec4bSSteve Wise 	list_for_each_entry(ops, &link_ops, list) {
14563856ec4bSSteve Wise 		if (!strcmp(ops->type, type))
14573856ec4bSSteve Wise 			goto out;
14583856ec4bSSteve Wise 	}
14593856ec4bSSteve Wise 	ops = NULL;
14603856ec4bSSteve Wise out:
14613856ec4bSSteve Wise 	return ops;
14623856ec4bSSteve Wise }
14633856ec4bSSteve Wise 
14643856ec4bSSteve Wise void rdma_link_register(struct rdma_link_ops *ops)
14653856ec4bSSteve Wise {
14663856ec4bSSteve Wise 	down_write(&link_ops_rwsem);
1467afc1990eSDan Carpenter 	if (WARN_ON_ONCE(link_ops_get(ops->type)))
14683856ec4bSSteve Wise 		goto out;
14693856ec4bSSteve Wise 	list_add(&ops->list, &link_ops);
14703856ec4bSSteve Wise out:
14713856ec4bSSteve Wise 	up_write(&link_ops_rwsem);
14723856ec4bSSteve Wise }
14733856ec4bSSteve Wise EXPORT_SYMBOL(rdma_link_register);
14743856ec4bSSteve Wise 
14753856ec4bSSteve Wise void rdma_link_unregister(struct rdma_link_ops *ops)
14763856ec4bSSteve Wise {
14773856ec4bSSteve Wise 	down_write(&link_ops_rwsem);
14783856ec4bSSteve Wise 	list_del(&ops->list);
14793856ec4bSSteve Wise 	up_write(&link_ops_rwsem);
14803856ec4bSSteve Wise }
14813856ec4bSSteve Wise EXPORT_SYMBOL(rdma_link_unregister);
14823856ec4bSSteve Wise 
14833856ec4bSSteve Wise static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
14843856ec4bSSteve Wise 			  struct netlink_ext_ack *extack)
14853856ec4bSSteve Wise {
14863856ec4bSSteve Wise 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
14873856ec4bSSteve Wise 	char ibdev_name[IB_DEVICE_NAME_MAX];
14883856ec4bSSteve Wise 	const struct rdma_link_ops *ops;
14893856ec4bSSteve Wise 	char ndev_name[IFNAMSIZ];
14903856ec4bSSteve Wise 	struct net_device *ndev;
14913856ec4bSSteve Wise 	char type[IFNAMSIZ];
14923856ec4bSSteve Wise 	int err;
14933856ec4bSSteve Wise 
14948cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
14953856ec4bSSteve Wise 				     nldev_policy, extack);
14963856ec4bSSteve Wise 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
14973856ec4bSSteve Wise 	    !tb[RDMA_NLDEV_ATTR_LINK_TYPE] || !tb[RDMA_NLDEV_ATTR_NDEV_NAME])
14983856ec4bSSteve Wise 		return -EINVAL;
14993856ec4bSSteve Wise 
15003856ec4bSSteve Wise 	nla_strlcpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
15013856ec4bSSteve Wise 		    sizeof(ibdev_name));
15027aefa623SJason Gunthorpe 	if (strchr(ibdev_name, '%') || strlen(ibdev_name) == 0)
15033856ec4bSSteve Wise 		return -EINVAL;
15043856ec4bSSteve Wise 
15053856ec4bSSteve Wise 	nla_strlcpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type));
15063856ec4bSSteve Wise 	nla_strlcpy(ndev_name, tb[RDMA_NLDEV_ATTR_NDEV_NAME],
15073856ec4bSSteve Wise 		    sizeof(ndev_name));
15083856ec4bSSteve Wise 
15097a54f78dSParav Pandit 	ndev = dev_get_by_name(sock_net(skb->sk), ndev_name);
15103856ec4bSSteve Wise 	if (!ndev)
15113856ec4bSSteve Wise 		return -ENODEV;
15123856ec4bSSteve Wise 
15133856ec4bSSteve Wise 	down_read(&link_ops_rwsem);
15143856ec4bSSteve Wise 	ops = link_ops_get(type);
15153856ec4bSSteve Wise #ifdef CONFIG_MODULES
15163856ec4bSSteve Wise 	if (!ops) {
15173856ec4bSSteve Wise 		up_read(&link_ops_rwsem);
15183856ec4bSSteve Wise 		request_module("rdma-link-%s", type);
15193856ec4bSSteve Wise 		down_read(&link_ops_rwsem);
15203856ec4bSSteve Wise 		ops = link_ops_get(type);
15213856ec4bSSteve Wise 	}
15223856ec4bSSteve Wise #endif
15233856ec4bSSteve Wise 	err = ops ? ops->newlink(ibdev_name, ndev) : -EINVAL;
15243856ec4bSSteve Wise 	up_read(&link_ops_rwsem);
15253856ec4bSSteve Wise 	dev_put(ndev);
15263856ec4bSSteve Wise 
15273856ec4bSSteve Wise 	return err;
15283856ec4bSSteve Wise }
15293856ec4bSSteve Wise 
15303856ec4bSSteve Wise static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
15313856ec4bSSteve Wise 			  struct netlink_ext_ack *extack)
15323856ec4bSSteve Wise {
15333856ec4bSSteve Wise 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
15343856ec4bSSteve Wise 	struct ib_device *device;
15353856ec4bSSteve Wise 	u32 index;
15363856ec4bSSteve Wise 	int err;
15373856ec4bSSteve Wise 
15388cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
15393856ec4bSSteve Wise 				     nldev_policy, extack);
15403856ec4bSSteve Wise 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
15413856ec4bSSteve Wise 		return -EINVAL;
15423856ec4bSSteve Wise 
15433856ec4bSSteve Wise 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
154437eeab55SParav Pandit 	device = ib_device_get_by_index(sock_net(skb->sk), index);
15453856ec4bSSteve Wise 	if (!device)
15463856ec4bSSteve Wise 		return -EINVAL;
15473856ec4bSSteve Wise 
15483856ec4bSSteve Wise 	if (!(device->attrs.device_cap_flags & IB_DEVICE_ALLOW_USER_UNREG)) {
15493856ec4bSSteve Wise 		ib_device_put(device);
15503856ec4bSSteve Wise 		return -EINVAL;
15513856ec4bSSteve Wise 	}
15523856ec4bSSteve Wise 
15533856ec4bSSteve Wise 	ib_unregister_device_and_put(device);
15543856ec4bSSteve Wise 	return 0;
15553856ec4bSSteve Wise }
15563856ec4bSSteve Wise 
15570e2d00ebSJason Gunthorpe static int nldev_get_chardev(struct sk_buff *skb, struct nlmsghdr *nlh,
15580e2d00ebSJason Gunthorpe 			     struct netlink_ext_ack *extack)
15590e2d00ebSJason Gunthorpe {
15600e2d00ebSJason Gunthorpe 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
156134d65cd8SDoug Ledford 	char client_name[RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE];
15620e2d00ebSJason Gunthorpe 	struct ib_client_nl_info data = {};
15630e2d00ebSJason Gunthorpe 	struct ib_device *ibdev = NULL;
15640e2d00ebSJason Gunthorpe 	struct sk_buff *msg;
15650e2d00ebSJason Gunthorpe 	u32 index;
15660e2d00ebSJason Gunthorpe 	int err;
15670e2d00ebSJason Gunthorpe 
15680e2d00ebSJason Gunthorpe 	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
15690e2d00ebSJason Gunthorpe 			  extack);
15700e2d00ebSJason Gunthorpe 	if (err || !tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE])
15710e2d00ebSJason Gunthorpe 		return -EINVAL;
15720e2d00ebSJason Gunthorpe 
157334d65cd8SDoug Ledford 	nla_strlcpy(client_name, tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE],
157434d65cd8SDoug Ledford 		    sizeof(client_name));
15750e2d00ebSJason Gunthorpe 
15760e2d00ebSJason Gunthorpe 	if (tb[RDMA_NLDEV_ATTR_DEV_INDEX]) {
15770e2d00ebSJason Gunthorpe 		index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
15780e2d00ebSJason Gunthorpe 		ibdev = ib_device_get_by_index(sock_net(skb->sk), index);
15790e2d00ebSJason Gunthorpe 		if (!ibdev)
15800e2d00ebSJason Gunthorpe 			return -EINVAL;
15810e2d00ebSJason Gunthorpe 
15820e2d00ebSJason Gunthorpe 		if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
15830e2d00ebSJason Gunthorpe 			data.port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
15840e2d00ebSJason Gunthorpe 			if (!rdma_is_port_valid(ibdev, data.port)) {
15850e2d00ebSJason Gunthorpe 				err = -EINVAL;
15860e2d00ebSJason Gunthorpe 				goto out_put;
15870e2d00ebSJason Gunthorpe 			}
15880e2d00ebSJason Gunthorpe 		} else {
15890e2d00ebSJason Gunthorpe 			data.port = -1;
15900e2d00ebSJason Gunthorpe 		}
15910e2d00ebSJason Gunthorpe 	} else if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
15920e2d00ebSJason Gunthorpe 		return -EINVAL;
15930e2d00ebSJason Gunthorpe 	}
15940e2d00ebSJason Gunthorpe 
15950e2d00ebSJason Gunthorpe 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15960e2d00ebSJason Gunthorpe 	if (!msg) {
15970e2d00ebSJason Gunthorpe 		err = -ENOMEM;
15980e2d00ebSJason Gunthorpe 		goto out_put;
15990e2d00ebSJason Gunthorpe 	}
16000e2d00ebSJason Gunthorpe 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
16010e2d00ebSJason Gunthorpe 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
16020e2d00ebSJason Gunthorpe 					 RDMA_NLDEV_CMD_GET_CHARDEV),
16030e2d00ebSJason Gunthorpe 			0, 0);
16040e2d00ebSJason Gunthorpe 
16050e2d00ebSJason Gunthorpe 	data.nl_msg = msg;
16060e2d00ebSJason Gunthorpe 	err = ib_get_client_nl_info(ibdev, client_name, &data);
16070e2d00ebSJason Gunthorpe 	if (err)
16080e2d00ebSJason Gunthorpe 		goto out_nlmsg;
16090e2d00ebSJason Gunthorpe 
16100e2d00ebSJason Gunthorpe 	err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV,
16110e2d00ebSJason Gunthorpe 				huge_encode_dev(data.cdev->devt),
16120e2d00ebSJason Gunthorpe 				RDMA_NLDEV_ATTR_PAD);
16130e2d00ebSJason Gunthorpe 	if (err)
16140e2d00ebSJason Gunthorpe 		goto out_data;
16150e2d00ebSJason Gunthorpe 	err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV_ABI, data.abi,
16160e2d00ebSJason Gunthorpe 				RDMA_NLDEV_ATTR_PAD);
16170e2d00ebSJason Gunthorpe 	if (err)
16180e2d00ebSJason Gunthorpe 		goto out_data;
16190e2d00ebSJason Gunthorpe 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_CHARDEV_NAME,
16200e2d00ebSJason Gunthorpe 			   dev_name(data.cdev))) {
16210e2d00ebSJason Gunthorpe 		err = -EMSGSIZE;
16220e2d00ebSJason Gunthorpe 		goto out_data;
16230e2d00ebSJason Gunthorpe 	}
16240e2d00ebSJason Gunthorpe 
16250e2d00ebSJason Gunthorpe 	nlmsg_end(msg, nlh);
16260e2d00ebSJason Gunthorpe 	put_device(data.cdev);
16270e2d00ebSJason Gunthorpe 	if (ibdev)
16280e2d00ebSJason Gunthorpe 		ib_device_put(ibdev);
16291d2fedd8SParav Pandit 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
16300e2d00ebSJason Gunthorpe 
16310e2d00ebSJason Gunthorpe out_data:
16320e2d00ebSJason Gunthorpe 	put_device(data.cdev);
16330e2d00ebSJason Gunthorpe out_nlmsg:
16340e2d00ebSJason Gunthorpe 	nlmsg_free(msg);
16350e2d00ebSJason Gunthorpe out_put:
16360e2d00ebSJason Gunthorpe 	if (ibdev)
16370e2d00ebSJason Gunthorpe 		ib_device_put(ibdev);
16380e2d00ebSJason Gunthorpe 	return err;
16390e2d00ebSJason Gunthorpe }
16400e2d00ebSJason Gunthorpe 
16414d7ba8ceSParav Pandit static int nldev_sys_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
16424d7ba8ceSParav Pandit 			      struct netlink_ext_ack *extack)
1643cb7e0e13SParav Pandit {
1644cb7e0e13SParav Pandit 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
16454d7ba8ceSParav Pandit 	struct sk_buff *msg;
1646cb7e0e13SParav Pandit 	int err;
1647cb7e0e13SParav Pandit 
16484d7ba8ceSParav Pandit 	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
16494d7ba8ceSParav Pandit 			  nldev_policy, extack);
1650cb7e0e13SParav Pandit 	if (err)
1651cb7e0e13SParav Pandit 		return err;
1652cb7e0e13SParav Pandit 
16534d7ba8ceSParav Pandit 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16544d7ba8ceSParav Pandit 	if (!msg)
16554d7ba8ceSParav Pandit 		return -ENOMEM;
16564d7ba8ceSParav Pandit 
16574d7ba8ceSParav Pandit 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1658cb7e0e13SParav Pandit 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1659cb7e0e13SParav Pandit 					 RDMA_NLDEV_CMD_SYS_GET),
1660cb7e0e13SParav Pandit 			0, 0);
1661cb7e0e13SParav Pandit 
16624d7ba8ceSParav Pandit 	err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_NETNS_MODE,
1663cb7e0e13SParav Pandit 			 (u8)ib_devices_shared_netns);
1664cb7e0e13SParav Pandit 	if (err) {
16654d7ba8ceSParav Pandit 		nlmsg_free(msg);
1666cb7e0e13SParav Pandit 		return err;
1667cb7e0e13SParav Pandit 	}
16684d7ba8ceSParav Pandit 	nlmsg_end(msg, nlh);
16691d2fedd8SParav Pandit 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1670cb7e0e13SParav Pandit }
1671cb7e0e13SParav Pandit 
16722b34c558SParav Pandit static int nldev_set_sys_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
16732b34c558SParav Pandit 				  struct netlink_ext_ack *extack)
16742b34c558SParav Pandit {
16752b34c558SParav Pandit 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
16762b34c558SParav Pandit 	u8 enable;
16772b34c558SParav Pandit 	int err;
16782b34c558SParav Pandit 
16792b34c558SParav Pandit 	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
16802b34c558SParav Pandit 			  nldev_policy, extack);
16812b34c558SParav Pandit 	if (err || !tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE])
16822b34c558SParav Pandit 		return -EINVAL;
16832b34c558SParav Pandit 
16842b34c558SParav Pandit 	enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]);
16852b34c558SParav Pandit 	/* Only 0 and 1 are supported */
16862b34c558SParav Pandit 	if (enable > 1)
16872b34c558SParav Pandit 		return -EINVAL;
16882b34c558SParav Pandit 
16892b34c558SParav Pandit 	err = rdma_compatdev_set(enable);
16902b34c558SParav Pandit 	return err;
16912b34c558SParav Pandit }
16922b34c558SParav Pandit 
1693b47ae6f8SMark Zhang static int nldev_stat_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1694b47ae6f8SMark Zhang 			       struct netlink_ext_ack *extack)
1695b47ae6f8SMark Zhang {
1696b389327dSMark Zhang 	u32 index, port, mode, mask = 0, qpn, cntn = 0;
1697b47ae6f8SMark Zhang 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1698b47ae6f8SMark Zhang 	struct ib_device *device;
1699b47ae6f8SMark Zhang 	struct sk_buff *msg;
1700b47ae6f8SMark Zhang 	int ret;
1701b47ae6f8SMark Zhang 
1702b47ae6f8SMark Zhang 	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1703b47ae6f8SMark Zhang 			  nldev_policy, extack);
1704b47ae6f8SMark Zhang 	/* Currently only counter for QP is supported */
1705b47ae6f8SMark Zhang 	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES] ||
1706b47ae6f8SMark Zhang 	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
1707b47ae6f8SMark Zhang 	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX] || !tb[RDMA_NLDEV_ATTR_STAT_MODE])
1708b47ae6f8SMark Zhang 		return -EINVAL;
1709b47ae6f8SMark Zhang 
1710b47ae6f8SMark Zhang 	if (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
1711b47ae6f8SMark Zhang 		return -EINVAL;
1712b47ae6f8SMark Zhang 
1713b47ae6f8SMark Zhang 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1714b47ae6f8SMark Zhang 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1715b47ae6f8SMark Zhang 	if (!device)
1716b47ae6f8SMark Zhang 		return -EINVAL;
1717b47ae6f8SMark Zhang 
1718b47ae6f8SMark Zhang 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1719b47ae6f8SMark Zhang 	if (!rdma_is_port_valid(device, port)) {
1720b47ae6f8SMark Zhang 		ret = -EINVAL;
1721b47ae6f8SMark Zhang 		goto err;
1722b47ae6f8SMark Zhang 	}
1723b47ae6f8SMark Zhang 
1724b47ae6f8SMark Zhang 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1725b47ae6f8SMark Zhang 	if (!msg) {
1726b47ae6f8SMark Zhang 		ret = -ENOMEM;
1727b47ae6f8SMark Zhang 		goto err;
1728b47ae6f8SMark Zhang 	}
1729b47ae6f8SMark Zhang 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1730b47ae6f8SMark Zhang 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1731b47ae6f8SMark Zhang 					 RDMA_NLDEV_CMD_STAT_SET),
1732b47ae6f8SMark Zhang 			0, 0);
1733b47ae6f8SMark Zhang 
1734b47ae6f8SMark Zhang 	mode = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_MODE]);
1735b389327dSMark Zhang 	if (mode == RDMA_COUNTER_MODE_AUTO) {
1736b47ae6f8SMark Zhang 		if (tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK])
1737b389327dSMark Zhang 			mask = nla_get_u32(
1738b389327dSMark Zhang 				tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]);
1739b47ae6f8SMark Zhang 
1740b47ae6f8SMark Zhang 		ret = rdma_counter_set_auto_mode(device, port,
1741b47ae6f8SMark Zhang 						 mask ? true : false, mask);
1742b47ae6f8SMark Zhang 		if (ret)
1743b47ae6f8SMark Zhang 			goto err_msg;
1744b389327dSMark Zhang 	} else {
174578f34a16SMark Zhang 		if (!tb[RDMA_NLDEV_ATTR_RES_LQPN])
174678f34a16SMark Zhang 			goto err_msg;
1747b389327dSMark Zhang 		qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
1748b389327dSMark Zhang 		if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]) {
1749b389327dSMark Zhang 			cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
1750b389327dSMark Zhang 			ret = rdma_counter_bind_qpn(device, port, qpn, cntn);
1751b389327dSMark Zhang 		} else {
1752b389327dSMark Zhang 			ret = rdma_counter_bind_qpn_alloc(device, port,
1753b389327dSMark Zhang 							  qpn, &cntn);
1754b389327dSMark Zhang 		}
1755b389327dSMark Zhang 		if (ret)
1756b47ae6f8SMark Zhang 			goto err_msg;
1757b389327dSMark Zhang 
1758b389327dSMark Zhang 		if (fill_nldev_handle(msg, device) ||
1759b389327dSMark Zhang 		    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
1760b389327dSMark Zhang 		    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
1761b389327dSMark Zhang 		    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
1762b389327dSMark Zhang 			ret = -EMSGSIZE;
1763b389327dSMark Zhang 			goto err_fill;
1764b389327dSMark Zhang 		}
1765b47ae6f8SMark Zhang 	}
1766b47ae6f8SMark Zhang 
1767b47ae6f8SMark Zhang 	nlmsg_end(msg, nlh);
1768b47ae6f8SMark Zhang 	ib_device_put(device);
17691d2fedd8SParav Pandit 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1770b47ae6f8SMark Zhang 
1771b389327dSMark Zhang err_fill:
1772b389327dSMark Zhang 	rdma_counter_unbind_qpn(device, port, qpn, cntn);
1773b47ae6f8SMark Zhang err_msg:
1774b47ae6f8SMark Zhang 	nlmsg_free(msg);
1775b47ae6f8SMark Zhang err:
1776b47ae6f8SMark Zhang 	ib_device_put(device);
1777b47ae6f8SMark Zhang 	return ret;
1778b47ae6f8SMark Zhang }
1779b47ae6f8SMark Zhang 
1780b389327dSMark Zhang static int nldev_stat_del_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1781b389327dSMark Zhang 			       struct netlink_ext_ack *extack)
1782b389327dSMark Zhang {
1783b389327dSMark Zhang 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1784b389327dSMark Zhang 	struct ib_device *device;
1785b389327dSMark Zhang 	struct sk_buff *msg;
1786b389327dSMark Zhang 	u32 index, port, qpn, cntn;
1787b389327dSMark Zhang 	int ret;
1788b389327dSMark Zhang 
1789b389327dSMark Zhang 	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1790b389327dSMark Zhang 			  nldev_policy, extack);
1791b389327dSMark Zhang 	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES] ||
1792b389327dSMark Zhang 	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX] ||
1793b389327dSMark Zhang 	    !tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID] ||
1794b389327dSMark Zhang 	    !tb[RDMA_NLDEV_ATTR_RES_LQPN])
1795b389327dSMark Zhang 		return -EINVAL;
1796b389327dSMark Zhang 
1797b389327dSMark Zhang 	if (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
1798b389327dSMark Zhang 		return -EINVAL;
1799b389327dSMark Zhang 
1800b389327dSMark Zhang 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1801b389327dSMark Zhang 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1802b389327dSMark Zhang 	if (!device)
1803b389327dSMark Zhang 		return -EINVAL;
1804b389327dSMark Zhang 
1805b389327dSMark Zhang 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1806b389327dSMark Zhang 	if (!rdma_is_port_valid(device, port)) {
1807b389327dSMark Zhang 		ret = -EINVAL;
1808b389327dSMark Zhang 		goto err;
1809b389327dSMark Zhang 	}
1810b389327dSMark Zhang 
1811b389327dSMark Zhang 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1812b389327dSMark Zhang 	if (!msg) {
1813b389327dSMark Zhang 		ret = -ENOMEM;
1814b389327dSMark Zhang 		goto err;
1815b389327dSMark Zhang 	}
1816b389327dSMark Zhang 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1817b389327dSMark Zhang 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1818b389327dSMark Zhang 					 RDMA_NLDEV_CMD_STAT_SET),
1819b389327dSMark Zhang 			0, 0);
1820b389327dSMark Zhang 
1821b389327dSMark Zhang 	cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
1822b389327dSMark Zhang 	qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
1823b389327dSMark Zhang 	if (fill_nldev_handle(msg, device) ||
1824b389327dSMark Zhang 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
1825b389327dSMark Zhang 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
1826b389327dSMark Zhang 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
1827b389327dSMark Zhang 		ret = -EMSGSIZE;
1828b389327dSMark Zhang 		goto err_fill;
1829b389327dSMark Zhang 	}
1830b389327dSMark Zhang 
1831594e6c5dSLeon Romanovsky 	ret = rdma_counter_unbind_qpn(device, port, qpn, cntn);
1832594e6c5dSLeon Romanovsky 	if (ret)
1833594e6c5dSLeon Romanovsky 		goto err_fill;
1834594e6c5dSLeon Romanovsky 
1835b389327dSMark Zhang 	nlmsg_end(msg, nlh);
1836b389327dSMark Zhang 	ib_device_put(device);
18371d2fedd8SParav Pandit 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1838b389327dSMark Zhang 
1839b389327dSMark Zhang err_fill:
1840b389327dSMark Zhang 	nlmsg_free(msg);
1841b389327dSMark Zhang err:
1842b389327dSMark Zhang 	ib_device_put(device);
1843b389327dSMark Zhang 	return ret;
1844b389327dSMark Zhang }
1845b389327dSMark Zhang 
18466e7be47aSMark Zhang static int stat_get_doit_default_counter(struct sk_buff *skb,
18476e7be47aSMark Zhang 					 struct nlmsghdr *nlh,
18486e7be47aSMark Zhang 					 struct netlink_ext_ack *extack,
18496e7be47aSMark Zhang 					 struct nlattr *tb[])
18506e7be47aSMark Zhang {
18516e7be47aSMark Zhang 	struct rdma_hw_stats *stats;
18526e7be47aSMark Zhang 	struct nlattr *table_attr;
18536e7be47aSMark Zhang 	struct ib_device *device;
18546e7be47aSMark Zhang 	int ret, num_cnts, i;
18556e7be47aSMark Zhang 	struct sk_buff *msg;
18566e7be47aSMark Zhang 	u32 index, port;
18576e7be47aSMark Zhang 	u64 v;
18586e7be47aSMark Zhang 
18596e7be47aSMark Zhang 	if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
18606e7be47aSMark Zhang 		return -EINVAL;
18616e7be47aSMark Zhang 
18626e7be47aSMark Zhang 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
18636e7be47aSMark Zhang 	device = ib_device_get_by_index(sock_net(skb->sk), index);
18646e7be47aSMark Zhang 	if (!device)
18656e7be47aSMark Zhang 		return -EINVAL;
18666e7be47aSMark Zhang 
18676e7be47aSMark Zhang 	if (!device->ops.alloc_hw_stats || !device->ops.get_hw_stats) {
18686e7be47aSMark Zhang 		ret = -EINVAL;
18696e7be47aSMark Zhang 		goto err;
18706e7be47aSMark Zhang 	}
18716e7be47aSMark Zhang 
18726e7be47aSMark Zhang 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
18736e7be47aSMark Zhang 	if (!rdma_is_port_valid(device, port)) {
18746e7be47aSMark Zhang 		ret = -EINVAL;
18756e7be47aSMark Zhang 		goto err;
18766e7be47aSMark Zhang 	}
18776e7be47aSMark Zhang 
18786e7be47aSMark Zhang 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18796e7be47aSMark Zhang 	if (!msg) {
18806e7be47aSMark Zhang 		ret = -ENOMEM;
18816e7be47aSMark Zhang 		goto err;
18826e7be47aSMark Zhang 	}
18836e7be47aSMark Zhang 
18846e7be47aSMark Zhang 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
18856e7be47aSMark Zhang 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
18866e7be47aSMark Zhang 					 RDMA_NLDEV_CMD_STAT_GET),
18876e7be47aSMark Zhang 			0, 0);
18886e7be47aSMark Zhang 
18896e7be47aSMark Zhang 	if (fill_nldev_handle(msg, device) ||
18906e7be47aSMark Zhang 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) {
18916e7be47aSMark Zhang 		ret = -EMSGSIZE;
18926e7be47aSMark Zhang 		goto err_msg;
18936e7be47aSMark Zhang 	}
18946e7be47aSMark Zhang 
18956e7be47aSMark Zhang 	stats = device->port_data ? device->port_data[port].hw_stats : NULL;
18966e7be47aSMark Zhang 	if (stats == NULL) {
18976e7be47aSMark Zhang 		ret = -EINVAL;
18986e7be47aSMark Zhang 		goto err_msg;
18996e7be47aSMark Zhang 	}
19006e7be47aSMark Zhang 	mutex_lock(&stats->lock);
19016e7be47aSMark Zhang 
19026e7be47aSMark Zhang 	num_cnts = device->ops.get_hw_stats(device, stats, port, 0);
19036e7be47aSMark Zhang 	if (num_cnts < 0) {
19046e7be47aSMark Zhang 		ret = -EINVAL;
19056e7be47aSMark Zhang 		goto err_stats;
19066e7be47aSMark Zhang 	}
19076e7be47aSMark Zhang 
19086e7be47aSMark Zhang 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
19096e7be47aSMark Zhang 	if (!table_attr) {
19106e7be47aSMark Zhang 		ret = -EMSGSIZE;
19116e7be47aSMark Zhang 		goto err_stats;
19126e7be47aSMark Zhang 	}
19136e7be47aSMark Zhang 	for (i = 0; i < num_cnts; i++) {
19146e7be47aSMark Zhang 		v = stats->value[i] +
19156e7be47aSMark Zhang 			rdma_counter_get_hwstat_value(device, port, i);
19164061ff7aSErez Alfasi 		if (rdma_nl_stat_hwcounter_entry(msg, stats->names[i], v)) {
19176e7be47aSMark Zhang 			ret = -EMSGSIZE;
19186e7be47aSMark Zhang 			goto err_table;
19196e7be47aSMark Zhang 		}
19206e7be47aSMark Zhang 	}
19216e7be47aSMark Zhang 	nla_nest_end(msg, table_attr);
19226e7be47aSMark Zhang 
19236e7be47aSMark Zhang 	mutex_unlock(&stats->lock);
19246e7be47aSMark Zhang 	nlmsg_end(msg, nlh);
19256e7be47aSMark Zhang 	ib_device_put(device);
19261d2fedd8SParav Pandit 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
19276e7be47aSMark Zhang 
19286e7be47aSMark Zhang err_table:
19296e7be47aSMark Zhang 	nla_nest_cancel(msg, table_attr);
19306e7be47aSMark Zhang err_stats:
19316e7be47aSMark Zhang 	mutex_unlock(&stats->lock);
19326e7be47aSMark Zhang err_msg:
19336e7be47aSMark Zhang 	nlmsg_free(msg);
19346e7be47aSMark Zhang err:
19356e7be47aSMark Zhang 	ib_device_put(device);
19366e7be47aSMark Zhang 	return ret;
19376e7be47aSMark Zhang }
19386e7be47aSMark Zhang 
193983c2c1fcSMark Zhang static int stat_get_doit_qp(struct sk_buff *skb, struct nlmsghdr *nlh,
194083c2c1fcSMark Zhang 			    struct netlink_ext_ack *extack, struct nlattr *tb[])
194183c2c1fcSMark Zhang 
194283c2c1fcSMark Zhang {
194383c2c1fcSMark Zhang 	static enum rdma_nl_counter_mode mode;
194483c2c1fcSMark Zhang 	static enum rdma_nl_counter_mask mask;
194583c2c1fcSMark Zhang 	struct ib_device *device;
194683c2c1fcSMark Zhang 	struct sk_buff *msg;
194783c2c1fcSMark Zhang 	u32 index, port;
194883c2c1fcSMark Zhang 	int ret;
194983c2c1fcSMark Zhang 
195083c2c1fcSMark Zhang 	if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID])
195183c2c1fcSMark Zhang 		return nldev_res_get_counter_doit(skb, nlh, extack);
195283c2c1fcSMark Zhang 
195383c2c1fcSMark Zhang 	if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] ||
195483c2c1fcSMark Zhang 	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
195583c2c1fcSMark Zhang 		return -EINVAL;
195683c2c1fcSMark Zhang 
195783c2c1fcSMark Zhang 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
195883c2c1fcSMark Zhang 	device = ib_device_get_by_index(sock_net(skb->sk), index);
195983c2c1fcSMark Zhang 	if (!device)
196083c2c1fcSMark Zhang 		return -EINVAL;
196183c2c1fcSMark Zhang 
196283c2c1fcSMark Zhang 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
196383c2c1fcSMark Zhang 	if (!rdma_is_port_valid(device, port)) {
196483c2c1fcSMark Zhang 		ret = -EINVAL;
196583c2c1fcSMark Zhang 		goto err;
196683c2c1fcSMark Zhang 	}
196783c2c1fcSMark Zhang 
196883c2c1fcSMark Zhang 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
196983c2c1fcSMark Zhang 	if (!msg) {
197083c2c1fcSMark Zhang 		ret = -ENOMEM;
197183c2c1fcSMark Zhang 		goto err;
197283c2c1fcSMark Zhang 	}
197383c2c1fcSMark Zhang 
197483c2c1fcSMark Zhang 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
197583c2c1fcSMark Zhang 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
197683c2c1fcSMark Zhang 					 RDMA_NLDEV_CMD_STAT_GET),
197783c2c1fcSMark Zhang 			0, 0);
197883c2c1fcSMark Zhang 
197983c2c1fcSMark Zhang 	ret = rdma_counter_get_mode(device, port, &mode, &mask);
198083c2c1fcSMark Zhang 	if (ret)
198183c2c1fcSMark Zhang 		goto err_msg;
198283c2c1fcSMark Zhang 
198383c2c1fcSMark Zhang 	if (fill_nldev_handle(msg, device) ||
198483c2c1fcSMark Zhang 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
1985932727c5SDan Carpenter 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, mode)) {
1986932727c5SDan Carpenter 		ret = -EMSGSIZE;
198783c2c1fcSMark Zhang 		goto err_msg;
1988932727c5SDan Carpenter 	}
198983c2c1fcSMark Zhang 
199083c2c1fcSMark Zhang 	if ((mode == RDMA_COUNTER_MODE_AUTO) &&
1991932727c5SDan Carpenter 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK, mask)) {
1992932727c5SDan Carpenter 		ret = -EMSGSIZE;
199383c2c1fcSMark Zhang 		goto err_msg;
1994932727c5SDan Carpenter 	}
199583c2c1fcSMark Zhang 
199683c2c1fcSMark Zhang 	nlmsg_end(msg, nlh);
199783c2c1fcSMark Zhang 	ib_device_put(device);
19981d2fedd8SParav Pandit 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
199983c2c1fcSMark Zhang 
200083c2c1fcSMark Zhang err_msg:
200183c2c1fcSMark Zhang 	nlmsg_free(msg);
200283c2c1fcSMark Zhang err:
200383c2c1fcSMark Zhang 	ib_device_put(device);
200483c2c1fcSMark Zhang 	return ret;
200583c2c1fcSMark Zhang }
200683c2c1fcSMark Zhang 
2007c4ffee7cSMark Zhang static int nldev_stat_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2008c4ffee7cSMark Zhang 			       struct netlink_ext_ack *extack)
2009c4ffee7cSMark Zhang {
2010c4ffee7cSMark Zhang 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2011c4ffee7cSMark Zhang 	int ret;
2012c4ffee7cSMark Zhang 
2013c4ffee7cSMark Zhang 	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2014c4ffee7cSMark Zhang 			  nldev_policy, extack);
20156e7be47aSMark Zhang 	if (ret)
2016c4ffee7cSMark Zhang 		return -EINVAL;
2017c4ffee7cSMark Zhang 
20186e7be47aSMark Zhang 	if (!tb[RDMA_NLDEV_ATTR_STAT_RES])
20196e7be47aSMark Zhang 		return stat_get_doit_default_counter(skb, nlh, extack, tb);
20206e7be47aSMark Zhang 
2021c4ffee7cSMark Zhang 	switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2022c4ffee7cSMark Zhang 	case RDMA_NLDEV_ATTR_RES_QP:
202383c2c1fcSMark Zhang 		ret = stat_get_doit_qp(skb, nlh, extack, tb);
2024c4ffee7cSMark Zhang 		break;
20254061ff7aSErez Alfasi 	case RDMA_NLDEV_ATTR_RES_MR:
20264061ff7aSErez Alfasi 		ret = res_get_common_doit(skb, nlh, extack, RDMA_RESTRACK_MR,
20274061ff7aSErez Alfasi 					  fill_stat_mr_entry);
20284061ff7aSErez Alfasi 		break;
2029c4ffee7cSMark Zhang 	default:
2030c4ffee7cSMark Zhang 		ret = -EINVAL;
2031c4ffee7cSMark Zhang 		break;
2032c4ffee7cSMark Zhang 	}
2033c4ffee7cSMark Zhang 
2034c4ffee7cSMark Zhang 	return ret;
2035c4ffee7cSMark Zhang }
2036c4ffee7cSMark Zhang 
2037c4ffee7cSMark Zhang static int nldev_stat_get_dumpit(struct sk_buff *skb,
2038c4ffee7cSMark Zhang 				 struct netlink_callback *cb)
2039c4ffee7cSMark Zhang {
2040c4ffee7cSMark Zhang 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2041c4ffee7cSMark Zhang 	int ret;
2042c4ffee7cSMark Zhang 
2043c4ffee7cSMark Zhang 	ret = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2044c4ffee7cSMark Zhang 			  nldev_policy, NULL);
2045c4ffee7cSMark Zhang 	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES])
2046c4ffee7cSMark Zhang 		return -EINVAL;
2047c4ffee7cSMark Zhang 
2048c4ffee7cSMark Zhang 	switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2049c4ffee7cSMark Zhang 	case RDMA_NLDEV_ATTR_RES_QP:
2050c4ffee7cSMark Zhang 		ret = nldev_res_get_counter_dumpit(skb, cb);
2051c4ffee7cSMark Zhang 		break;
20524061ff7aSErez Alfasi 	case RDMA_NLDEV_ATTR_RES_MR:
20534061ff7aSErez Alfasi 		ret = res_get_common_dumpit(skb, cb, RDMA_RESTRACK_MR,
20544061ff7aSErez Alfasi 					    fill_stat_mr_entry);
20554061ff7aSErez Alfasi 		break;
2056c4ffee7cSMark Zhang 	default:
2057c4ffee7cSMark Zhang 		ret = -EINVAL;
2058c4ffee7cSMark Zhang 		break;
2059c4ffee7cSMark Zhang 	}
2060c4ffee7cSMark Zhang 
2061c4ffee7cSMark Zhang 	return ret;
2062c4ffee7cSMark Zhang }
2063c4ffee7cSMark Zhang 
2064d0e312feSLeon Romanovsky static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
2065b4c598a6SLeon Romanovsky 	[RDMA_NLDEV_CMD_GET] = {
2066e5c9469eSLeon Romanovsky 		.doit = nldev_get_doit,
2067b4c598a6SLeon Romanovsky 		.dump = nldev_get_dumpit,
2068b4c598a6SLeon Romanovsky 	},
20690e2d00ebSJason Gunthorpe 	[RDMA_NLDEV_CMD_GET_CHARDEV] = {
20700e2d00ebSJason Gunthorpe 		.doit = nldev_get_chardev,
20710e2d00ebSJason Gunthorpe 	},
207205d940d3SLeon Romanovsky 	[RDMA_NLDEV_CMD_SET] = {
207305d940d3SLeon Romanovsky 		.doit = nldev_set_doit,
207405d940d3SLeon Romanovsky 		.flags = RDMA_NL_ADMIN_PERM,
207505d940d3SLeon Romanovsky 	},
20763856ec4bSSteve Wise 	[RDMA_NLDEV_CMD_NEWLINK] = {
20773856ec4bSSteve Wise 		.doit = nldev_newlink,
20783856ec4bSSteve Wise 		.flags = RDMA_NL_ADMIN_PERM,
20793856ec4bSSteve Wise 	},
20803856ec4bSSteve Wise 	[RDMA_NLDEV_CMD_DELLINK] = {
20813856ec4bSSteve Wise 		.doit = nldev_dellink,
20823856ec4bSSteve Wise 		.flags = RDMA_NL_ADMIN_PERM,
20833856ec4bSSteve Wise 	},
20847d02f605SLeon Romanovsky 	[RDMA_NLDEV_CMD_PORT_GET] = {
2085c3f66f7bSLeon Romanovsky 		.doit = nldev_port_get_doit,
20867d02f605SLeon Romanovsky 		.dump = nldev_port_get_dumpit,
20877d02f605SLeon Romanovsky 	},
2088bf3c5a93SLeon Romanovsky 	[RDMA_NLDEV_CMD_RES_GET] = {
2089bf3c5a93SLeon Romanovsky 		.doit = nldev_res_get_doit,
2090bf3c5a93SLeon Romanovsky 		.dump = nldev_res_get_dumpit,
2091bf3c5a93SLeon Romanovsky 	},
2092b5fa635aSLeon Romanovsky 	[RDMA_NLDEV_CMD_RES_QP_GET] = {
2093c5dfe0eaSLeon Romanovsky 		.doit = nldev_res_get_qp_doit,
2094b5fa635aSLeon Romanovsky 		.dump = nldev_res_get_qp_dumpit,
2095b5fa635aSLeon Romanovsky 	},
209600313983SSteve Wise 	[RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
2097c5dfe0eaSLeon Romanovsky 		.doit = nldev_res_get_cm_id_doit,
209800313983SSteve Wise 		.dump = nldev_res_get_cm_id_dumpit,
209900313983SSteve Wise 	},
2100a34fc089SSteve Wise 	[RDMA_NLDEV_CMD_RES_CQ_GET] = {
2101c5dfe0eaSLeon Romanovsky 		.doit = nldev_res_get_cq_doit,
2102a34fc089SSteve Wise 		.dump = nldev_res_get_cq_dumpit,
2103a34fc089SSteve Wise 	},
2104fccec5b8SSteve Wise 	[RDMA_NLDEV_CMD_RES_MR_GET] = {
2105c5dfe0eaSLeon Romanovsky 		.doit = nldev_res_get_mr_doit,
2106fccec5b8SSteve Wise 		.dump = nldev_res_get_mr_dumpit,
2107fccec5b8SSteve Wise 	},
210829cf1351SSteve Wise 	[RDMA_NLDEV_CMD_RES_PD_GET] = {
2109c5dfe0eaSLeon Romanovsky 		.doit = nldev_res_get_pd_doit,
211029cf1351SSteve Wise 		.dump = nldev_res_get_pd_dumpit,
211129cf1351SSteve Wise 	},
2112cb7e0e13SParav Pandit 	[RDMA_NLDEV_CMD_SYS_GET] = {
21134d7ba8ceSParav Pandit 		.doit = nldev_sys_get_doit,
2114cb7e0e13SParav Pandit 	},
21152b34c558SParav Pandit 	[RDMA_NLDEV_CMD_SYS_SET] = {
21162b34c558SParav Pandit 		.doit = nldev_set_sys_set_doit,
2117b47ae6f8SMark Zhang 	},
2118b47ae6f8SMark Zhang 	[RDMA_NLDEV_CMD_STAT_SET] = {
2119b47ae6f8SMark Zhang 		.doit = nldev_stat_set_doit,
21202b34c558SParav Pandit 		.flags = RDMA_NL_ADMIN_PERM,
21212b34c558SParav Pandit 	},
2122c4ffee7cSMark Zhang 	[RDMA_NLDEV_CMD_STAT_GET] = {
2123c4ffee7cSMark Zhang 		.doit = nldev_stat_get_doit,
2124c4ffee7cSMark Zhang 		.dump = nldev_stat_get_dumpit,
2125c4ffee7cSMark Zhang 	},
2126b389327dSMark Zhang 	[RDMA_NLDEV_CMD_STAT_DEL] = {
2127b389327dSMark Zhang 		.doit = nldev_stat_del_doit,
2128b389327dSMark Zhang 		.flags = RDMA_NL_ADMIN_PERM,
2129b389327dSMark Zhang 	},
2130b4c598a6SLeon Romanovsky };
2131b4c598a6SLeon Romanovsky 
21326c80b41aSLeon Romanovsky void __init nldev_init(void)
21336c80b41aSLeon Romanovsky {
2134b4c598a6SLeon Romanovsky 	rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
21356c80b41aSLeon Romanovsky }
21366c80b41aSLeon Romanovsky 
21376c80b41aSLeon Romanovsky void __exit nldev_exit(void)
21386c80b41aSLeon Romanovsky {
21396c80b41aSLeon Romanovsky 	rdma_nl_unregister(RDMA_NL_NLDEV);
21406c80b41aSLeon Romanovsky }
2141e3bf14bdSJason Gunthorpe 
2142e3bf14bdSJason Gunthorpe MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);
2143