xref: /openbmc/linux/drivers/infiniband/core/nldev.c (revision ea5ef136)
1 /*
2  * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. Neither the names of the copyright holders nor the names of its
13  *    contributors may be used to endorse or promote products derived from
14  *    this software without specific prior written permission.
15  *
16  * Alternatively, this software may be distributed under the terms of the
17  * GNU General Public License ("GPL") version 2 as published by the Free
18  * Software Foundation.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <linux/module.h>
34 #include <linux/pid.h>
35 #include <linux/pid_namespace.h>
36 #include <linux/mutex.h>
37 #include <net/netlink.h>
38 #include <rdma/rdma_cm.h>
39 #include <rdma/rdma_netlink.h>
40 
41 #include "core_priv.h"
42 #include "cma_priv.h"
43 #include "restrack.h"
44 #include "uverbs.h"
45 
46 typedef int (*res_fill_func_t)(struct sk_buff*, bool,
47 			       struct rdma_restrack_entry*, uint32_t);
48 
49 /*
50  * Sort array elements by the netlink attribute name
51  */
52 static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
53 	[RDMA_NLDEV_ATTR_CHARDEV]		= { .type = NLA_U64 },
54 	[RDMA_NLDEV_ATTR_CHARDEV_ABI]		= { .type = NLA_U64 },
55 	[RDMA_NLDEV_ATTR_CHARDEV_NAME]		= { .type = NLA_NUL_STRING,
56 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
57 	[RDMA_NLDEV_ATTR_CHARDEV_TYPE]		= { .type = NLA_NUL_STRING,
58 					.len = RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE },
59 	[RDMA_NLDEV_ATTR_DEV_DIM]               = { .type = NLA_U8 },
60 	[RDMA_NLDEV_ATTR_DEV_INDEX]		= { .type = NLA_U32 },
61 	[RDMA_NLDEV_ATTR_DEV_NAME]		= { .type = NLA_NUL_STRING,
62 					.len = IB_DEVICE_NAME_MAX },
63 	[RDMA_NLDEV_ATTR_DEV_NODE_TYPE]		= { .type = NLA_U8 },
64 	[RDMA_NLDEV_ATTR_DEV_PROTOCOL]		= { .type = NLA_NUL_STRING,
65 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
66 	[RDMA_NLDEV_ATTR_DRIVER]		= { .type = NLA_NESTED },
67 	[RDMA_NLDEV_ATTR_DRIVER_ENTRY]		= { .type = NLA_NESTED },
68 	[RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE]	= { .type = NLA_U8 },
69 	[RDMA_NLDEV_ATTR_DRIVER_STRING]		= { .type = NLA_NUL_STRING,
70 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
71 	[RDMA_NLDEV_ATTR_DRIVER_S32]		= { .type = NLA_S32 },
72 	[RDMA_NLDEV_ATTR_DRIVER_S64]		= { .type = NLA_S64 },
73 	[RDMA_NLDEV_ATTR_DRIVER_U32]		= { .type = NLA_U32 },
74 	[RDMA_NLDEV_ATTR_DRIVER_U64]		= { .type = NLA_U64 },
75 	[RDMA_NLDEV_ATTR_FW_VERSION]		= { .type = NLA_NUL_STRING,
76 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
77 	[RDMA_NLDEV_ATTR_LID]			= { .type = NLA_U32 },
78 	[RDMA_NLDEV_ATTR_LINK_TYPE]		= { .type = NLA_NUL_STRING,
79 					.len = IFNAMSIZ },
80 	[RDMA_NLDEV_ATTR_LMC]			= { .type = NLA_U8 },
81 	[RDMA_NLDEV_ATTR_NDEV_INDEX]		= { .type = NLA_U32 },
82 	[RDMA_NLDEV_ATTR_NDEV_NAME]		= { .type = NLA_NUL_STRING,
83 					.len = IFNAMSIZ },
84 	[RDMA_NLDEV_ATTR_NODE_GUID]		= { .type = NLA_U64 },
85 	[RDMA_NLDEV_ATTR_PORT_INDEX]		= { .type = NLA_U32 },
86 	[RDMA_NLDEV_ATTR_PORT_PHYS_STATE]	= { .type = NLA_U8 },
87 	[RDMA_NLDEV_ATTR_PORT_STATE]		= { .type = NLA_U8 },
88 	[RDMA_NLDEV_ATTR_RES_CM_ID]		= { .type = NLA_NESTED },
89 	[RDMA_NLDEV_ATTR_RES_CM_IDN]		= { .type = NLA_U32 },
90 	[RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY]	= { .type = NLA_NESTED },
91 	[RDMA_NLDEV_ATTR_RES_CQ]		= { .type = NLA_NESTED },
92 	[RDMA_NLDEV_ATTR_RES_CQE]		= { .type = NLA_U32 },
93 	[RDMA_NLDEV_ATTR_RES_CQN]		= { .type = NLA_U32 },
94 	[RDMA_NLDEV_ATTR_RES_CQ_ENTRY]		= { .type = NLA_NESTED },
95 	[RDMA_NLDEV_ATTR_RES_CTX]		= { .type = NLA_NESTED },
96 	[RDMA_NLDEV_ATTR_RES_CTXN]		= { .type = NLA_U32 },
97 	[RDMA_NLDEV_ATTR_RES_CTX_ENTRY]		= { .type = NLA_NESTED },
98 	[RDMA_NLDEV_ATTR_RES_DST_ADDR]		= {
99 			.len = sizeof(struct __kernel_sockaddr_storage) },
100 	[RDMA_NLDEV_ATTR_RES_IOVA]		= { .type = NLA_U64 },
101 	[RDMA_NLDEV_ATTR_RES_KERN_NAME]		= { .type = NLA_NUL_STRING,
102 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
103 	[RDMA_NLDEV_ATTR_RES_LKEY]		= { .type = NLA_U32 },
104 	[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]	= { .type = NLA_U32 },
105 	[RDMA_NLDEV_ATTR_RES_LQPN]		= { .type = NLA_U32 },
106 	[RDMA_NLDEV_ATTR_RES_MR]		= { .type = NLA_NESTED },
107 	[RDMA_NLDEV_ATTR_RES_MRLEN]		= { .type = NLA_U64 },
108 	[RDMA_NLDEV_ATTR_RES_MRN]		= { .type = NLA_U32 },
109 	[RDMA_NLDEV_ATTR_RES_MR_ENTRY]		= { .type = NLA_NESTED },
110 	[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]	= { .type = NLA_U8 },
111 	[RDMA_NLDEV_ATTR_RES_PD]		= { .type = NLA_NESTED },
112 	[RDMA_NLDEV_ATTR_RES_PDN]		= { .type = NLA_U32 },
113 	[RDMA_NLDEV_ATTR_RES_PD_ENTRY]		= { .type = NLA_NESTED },
114 	[RDMA_NLDEV_ATTR_RES_PID]		= { .type = NLA_U32 },
115 	[RDMA_NLDEV_ATTR_RES_POLL_CTX]		= { .type = NLA_U8 },
116 	[RDMA_NLDEV_ATTR_RES_PS]		= { .type = NLA_U32 },
117 	[RDMA_NLDEV_ATTR_RES_QP]		= { .type = NLA_NESTED },
118 	[RDMA_NLDEV_ATTR_RES_QP_ENTRY]		= { .type = NLA_NESTED },
119 	[RDMA_NLDEV_ATTR_RES_RAW]		= { .type = NLA_BINARY },
120 	[RDMA_NLDEV_ATTR_RES_RKEY]		= { .type = NLA_U32 },
121 	[RDMA_NLDEV_ATTR_RES_RQPN]		= { .type = NLA_U32 },
122 	[RDMA_NLDEV_ATTR_RES_RQ_PSN]		= { .type = NLA_U32 },
123 	[RDMA_NLDEV_ATTR_RES_SQ_PSN]		= { .type = NLA_U32 },
124 	[RDMA_NLDEV_ATTR_RES_SRC_ADDR]		= {
125 			.len = sizeof(struct __kernel_sockaddr_storage) },
126 	[RDMA_NLDEV_ATTR_RES_STATE]		= { .type = NLA_U8 },
127 	[RDMA_NLDEV_ATTR_RES_SUMMARY]		= { .type = NLA_NESTED },
128 	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY]	= { .type = NLA_NESTED },
129 	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]= { .type = NLA_U64 },
130 	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]= { .type = NLA_NUL_STRING,
131 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
132 	[RDMA_NLDEV_ATTR_RES_TYPE]		= { .type = NLA_U8 },
133 	[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]= { .type = NLA_U32 },
134 	[RDMA_NLDEV_ATTR_RES_USECNT]		= { .type = NLA_U64 },
135 	[RDMA_NLDEV_ATTR_RES_SRQ]		= { .type = NLA_NESTED },
136 	[RDMA_NLDEV_ATTR_RES_SRQN]		= { .type = NLA_U32 },
137 	[RDMA_NLDEV_ATTR_RES_SRQ_ENTRY]		= { .type = NLA_NESTED },
138 	[RDMA_NLDEV_ATTR_MIN_RANGE]		= { .type = NLA_U32 },
139 	[RDMA_NLDEV_ATTR_MAX_RANGE]		= { .type = NLA_U32 },
140 	[RDMA_NLDEV_ATTR_SM_LID]		= { .type = NLA_U32 },
141 	[RDMA_NLDEV_ATTR_SUBNET_PREFIX]		= { .type = NLA_U64 },
142 	[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]	= { .type = NLA_U32 },
143 	[RDMA_NLDEV_ATTR_STAT_MODE]		= { .type = NLA_U32 },
144 	[RDMA_NLDEV_ATTR_STAT_RES]		= { .type = NLA_U32 },
145 	[RDMA_NLDEV_ATTR_STAT_COUNTER]		= { .type = NLA_NESTED },
146 	[RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY]	= { .type = NLA_NESTED },
147 	[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]       = { .type = NLA_U32 },
148 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]       = { .type = NLA_NESTED },
149 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY]  = { .type = NLA_NESTED },
150 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME] = { .type = NLA_NUL_STRING },
151 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE] = { .type = NLA_U64 },
152 	[RDMA_NLDEV_ATTR_SYS_IMAGE_GUID]	= { .type = NLA_U64 },
153 	[RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID]	= { .type = NLA_U32 },
154 	[RDMA_NLDEV_NET_NS_FD]			= { .type = NLA_U32 },
155 	[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]	= { .type = NLA_U8 },
156 	[RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK]	= { .type = NLA_U8 },
157 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_INDEX]	= { .type = NLA_U32 },
158 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_DYNAMIC] = { .type = NLA_U8 },
159 };
160 
161 static int put_driver_name_print_type(struct sk_buff *msg, const char *name,
162 				      enum rdma_nldev_print_type print_type)
163 {
164 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name))
165 		return -EMSGSIZE;
166 	if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC &&
167 	    nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type))
168 		return -EMSGSIZE;
169 
170 	return 0;
171 }
172 
173 static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name,
174 				   enum rdma_nldev_print_type print_type,
175 				   u32 value)
176 {
177 	if (put_driver_name_print_type(msg, name, print_type))
178 		return -EMSGSIZE;
179 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value))
180 		return -EMSGSIZE;
181 
182 	return 0;
183 }
184 
185 static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name,
186 				   enum rdma_nldev_print_type print_type,
187 				   u64 value)
188 {
189 	if (put_driver_name_print_type(msg, name, print_type))
190 		return -EMSGSIZE;
191 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value,
192 			      RDMA_NLDEV_ATTR_PAD))
193 		return -EMSGSIZE;
194 
195 	return 0;
196 }
197 
198 int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
199 			      const char *str)
200 {
201 	if (put_driver_name_print_type(msg, name,
202 				       RDMA_NLDEV_PRINT_TYPE_UNSPEC))
203 		return -EMSGSIZE;
204 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, str))
205 		return -EMSGSIZE;
206 
207 	return 0;
208 }
209 EXPORT_SYMBOL(rdma_nl_put_driver_string);
210 
211 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value)
212 {
213 	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
214 				       value);
215 }
216 EXPORT_SYMBOL(rdma_nl_put_driver_u32);
217 
218 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
219 			       u32 value)
220 {
221 	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
222 				       value);
223 }
224 EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex);
225 
226 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value)
227 {
228 	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
229 				       value);
230 }
231 EXPORT_SYMBOL(rdma_nl_put_driver_u64);
232 
233 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value)
234 {
235 	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
236 				       value);
237 }
238 EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex);
239 
240 static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
241 {
242 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
243 		return -EMSGSIZE;
244 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME,
245 			   dev_name(&device->dev)))
246 		return -EMSGSIZE;
247 
248 	return 0;
249 }
250 
251 static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
252 {
253 	char fw[IB_FW_VERSION_NAME_MAX];
254 	int ret = 0;
255 	u32 port;
256 
257 	if (fill_nldev_handle(msg, device))
258 		return -EMSGSIZE;
259 
260 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
261 		return -EMSGSIZE;
262 
263 	BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
264 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
265 			      device->attrs.device_cap_flags,
266 			      RDMA_NLDEV_ATTR_PAD))
267 		return -EMSGSIZE;
268 
269 	ib_get_device_fw_str(device, fw);
270 	/* Device without FW has strlen(fw) = 0 */
271 	if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
272 		return -EMSGSIZE;
273 
274 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
275 			      be64_to_cpu(device->node_guid),
276 			      RDMA_NLDEV_ATTR_PAD))
277 		return -EMSGSIZE;
278 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
279 			      be64_to_cpu(device->attrs.sys_image_guid),
280 			      RDMA_NLDEV_ATTR_PAD))
281 		return -EMSGSIZE;
282 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
283 		return -EMSGSIZE;
284 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, device->use_cq_dim))
285 		return -EMSGSIZE;
286 
287 	/*
288 	 * Link type is determined on first port and mlx4 device
289 	 * which can potentially have two different link type for the same
290 	 * IB device is considered as better to be avoided in the future,
291 	 */
292 	port = rdma_start_port(device);
293 	if (rdma_cap_opa_mad(device, port))
294 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "opa");
295 	else if (rdma_protocol_ib(device, port))
296 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "ib");
297 	else if (rdma_protocol_iwarp(device, port))
298 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "iw");
299 	else if (rdma_protocol_roce(device, port))
300 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "roce");
301 	else if (rdma_protocol_usnic(device, port))
302 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL,
303 				     "usnic");
304 	return ret;
305 }
306 
307 static int fill_port_info(struct sk_buff *msg,
308 			  struct ib_device *device, u32 port,
309 			  const struct net *net)
310 {
311 	struct net_device *netdev = NULL;
312 	struct ib_port_attr attr;
313 	int ret;
314 	u64 cap_flags = 0;
315 
316 	if (fill_nldev_handle(msg, device))
317 		return -EMSGSIZE;
318 
319 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
320 		return -EMSGSIZE;
321 
322 	ret = ib_query_port(device, port, &attr);
323 	if (ret)
324 		return ret;
325 
326 	if (rdma_protocol_ib(device, port)) {
327 		BUILD_BUG_ON((sizeof(attr.port_cap_flags) +
328 				sizeof(attr.port_cap_flags2)) > sizeof(u64));
329 		cap_flags = attr.port_cap_flags |
330 			((u64)attr.port_cap_flags2 << 32);
331 		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
332 				      cap_flags, RDMA_NLDEV_ATTR_PAD))
333 			return -EMSGSIZE;
334 		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
335 				      attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD))
336 			return -EMSGSIZE;
337 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
338 			return -EMSGSIZE;
339 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
340 			return -EMSGSIZE;
341 		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
342 			return -EMSGSIZE;
343 	}
344 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
345 		return -EMSGSIZE;
346 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
347 		return -EMSGSIZE;
348 
349 	netdev = ib_device_get_netdev(device, port);
350 	if (netdev && net_eq(dev_net(netdev), net)) {
351 		ret = nla_put_u32(msg,
352 				  RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
353 		if (ret)
354 			goto out;
355 		ret = nla_put_string(msg,
356 				     RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
357 	}
358 
359 out:
360 	if (netdev)
361 		dev_put(netdev);
362 	return ret;
363 }
364 
365 static int fill_res_info_entry(struct sk_buff *msg,
366 			       const char *name, u64 curr)
367 {
368 	struct nlattr *entry_attr;
369 
370 	entry_attr = nla_nest_start_noflag(msg,
371 					   RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
372 	if (!entry_attr)
373 		return -EMSGSIZE;
374 
375 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
376 		goto err;
377 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr,
378 			      RDMA_NLDEV_ATTR_PAD))
379 		goto err;
380 
381 	nla_nest_end(msg, entry_attr);
382 	return 0;
383 
384 err:
385 	nla_nest_cancel(msg, entry_attr);
386 	return -EMSGSIZE;
387 }
388 
389 static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
390 {
391 	static const char * const names[RDMA_RESTRACK_MAX] = {
392 		[RDMA_RESTRACK_PD] = "pd",
393 		[RDMA_RESTRACK_CQ] = "cq",
394 		[RDMA_RESTRACK_QP] = "qp",
395 		[RDMA_RESTRACK_CM_ID] = "cm_id",
396 		[RDMA_RESTRACK_MR] = "mr",
397 		[RDMA_RESTRACK_CTX] = "ctx",
398 		[RDMA_RESTRACK_SRQ] = "srq",
399 	};
400 
401 	struct nlattr *table_attr;
402 	int ret, i, curr;
403 
404 	if (fill_nldev_handle(msg, device))
405 		return -EMSGSIZE;
406 
407 	table_attr = nla_nest_start_noflag(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
408 	if (!table_attr)
409 		return -EMSGSIZE;
410 
411 	for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
412 		if (!names[i])
413 			continue;
414 		curr = rdma_restrack_count(device, i);
415 		ret = fill_res_info_entry(msg, names[i], curr);
416 		if (ret)
417 			goto err;
418 	}
419 
420 	nla_nest_end(msg, table_attr);
421 	return 0;
422 
423 err:
424 	nla_nest_cancel(msg, table_attr);
425 	return ret;
426 }
427 
428 static int fill_res_name_pid(struct sk_buff *msg,
429 			     struct rdma_restrack_entry *res)
430 {
431 	int err = 0;
432 
433 	/*
434 	 * For user resources, user is should read /proc/PID/comm to get the
435 	 * name of the task file.
436 	 */
437 	if (rdma_is_kernel_res(res)) {
438 		err = nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
439 				     res->kern_name);
440 	} else {
441 		pid_t pid;
442 
443 		pid = task_pid_vnr(res->task);
444 		/*
445 		 * Task is dead and in zombie state.
446 		 * There is no need to print PID anymore.
447 		 */
448 		if (pid)
449 			/*
450 			 * This part is racy, task can be killed and PID will
451 			 * be zero right here but it is ok, next query won't
452 			 * return PID. We don't promise real-time reflection
453 			 * of SW objects.
454 			 */
455 			err = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID, pid);
456 	}
457 
458 	return err ? -EMSGSIZE : 0;
459 }
460 
461 static int fill_res_qp_entry_query(struct sk_buff *msg,
462 				   struct rdma_restrack_entry *res,
463 				   struct ib_device *dev,
464 				   struct ib_qp *qp)
465 {
466 	struct ib_qp_init_attr qp_init_attr;
467 	struct ib_qp_attr qp_attr;
468 	int ret;
469 
470 	ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
471 	if (ret)
472 		return ret;
473 
474 	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
475 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
476 				qp_attr.dest_qp_num))
477 			goto err;
478 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
479 				qp_attr.rq_psn))
480 			goto err;
481 	}
482 
483 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
484 		goto err;
485 
486 	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
487 	    qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
488 		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
489 			       qp_attr.path_mig_state))
490 			goto err;
491 	}
492 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
493 		goto err;
494 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
495 		goto err;
496 
497 	if (dev->ops.fill_res_qp_entry)
498 		return dev->ops.fill_res_qp_entry(msg, qp);
499 	return 0;
500 
501 err:	return -EMSGSIZE;
502 }
503 
504 static int fill_res_qp_entry(struct sk_buff *msg, bool has_cap_net_admin,
505 			     struct rdma_restrack_entry *res, uint32_t port)
506 {
507 	struct ib_qp *qp = container_of(res, struct ib_qp, res);
508 	struct ib_device *dev = qp->device;
509 	int ret;
510 
511 	if (port && port != qp->port)
512 		return -EAGAIN;
513 
514 	/* In create_qp() port is not set yet */
515 	if (qp->port && nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp->port))
516 		return -EINVAL;
517 
518 	ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num);
519 	if (ret)
520 		return -EMSGSIZE;
521 
522 	if (!rdma_is_kernel_res(res) &&
523 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, qp->pd->res.id))
524 		return -EMSGSIZE;
525 
526 	ret = fill_res_name_pid(msg, res);
527 	if (ret)
528 		return -EMSGSIZE;
529 
530 	return fill_res_qp_entry_query(msg, res, dev, qp);
531 }
532 
533 static int fill_res_qp_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
534 				 struct rdma_restrack_entry *res, uint32_t port)
535 {
536 	struct ib_qp *qp = container_of(res, struct ib_qp, res);
537 	struct ib_device *dev = qp->device;
538 
539 	if (port && port != qp->port)
540 		return -EAGAIN;
541 	if (!dev->ops.fill_res_qp_entry_raw)
542 		return -EINVAL;
543 	return dev->ops.fill_res_qp_entry_raw(msg, qp);
544 }
545 
546 static int fill_res_cm_id_entry(struct sk_buff *msg, bool has_cap_net_admin,
547 				struct rdma_restrack_entry *res, uint32_t port)
548 {
549 	struct rdma_id_private *id_priv =
550 				container_of(res, struct rdma_id_private, res);
551 	struct ib_device *dev = id_priv->id.device;
552 	struct rdma_cm_id *cm_id = &id_priv->id;
553 
554 	if (port && port != cm_id->port_num)
555 		return -EAGAIN;
556 
557 	if (cm_id->port_num &&
558 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
559 		goto err;
560 
561 	if (id_priv->qp_num) {
562 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
563 			goto err;
564 		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
565 			goto err;
566 	}
567 
568 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
569 		goto err;
570 
571 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
572 		goto err;
573 
574 	if (cm_id->route.addr.src_addr.ss_family &&
575 	    nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
576 		    sizeof(cm_id->route.addr.src_addr),
577 		    &cm_id->route.addr.src_addr))
578 		goto err;
579 	if (cm_id->route.addr.dst_addr.ss_family &&
580 	    nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
581 		    sizeof(cm_id->route.addr.dst_addr),
582 		    &cm_id->route.addr.dst_addr))
583 		goto err;
584 
585 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CM_IDN, res->id))
586 		goto err;
587 
588 	if (fill_res_name_pid(msg, res))
589 		goto err;
590 
591 	if (dev->ops.fill_res_cm_id_entry)
592 		return dev->ops.fill_res_cm_id_entry(msg, cm_id);
593 	return 0;
594 
595 err: return -EMSGSIZE;
596 }
597 
598 static int fill_res_cq_entry(struct sk_buff *msg, bool has_cap_net_admin,
599 			     struct rdma_restrack_entry *res, uint32_t port)
600 {
601 	struct ib_cq *cq = container_of(res, struct ib_cq, res);
602 	struct ib_device *dev = cq->device;
603 
604 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
605 		return -EMSGSIZE;
606 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
607 			      atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD))
608 		return -EMSGSIZE;
609 
610 	/* Poll context is only valid for kernel CQs */
611 	if (rdma_is_kernel_res(res) &&
612 	    nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
613 		return -EMSGSIZE;
614 
615 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, (cq->dim != NULL)))
616 		return -EMSGSIZE;
617 
618 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN, res->id))
619 		return -EMSGSIZE;
620 	if (!rdma_is_kernel_res(res) &&
621 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
622 			cq->uobject->uevent.uobject.context->res.id))
623 		return -EMSGSIZE;
624 
625 	if (fill_res_name_pid(msg, res))
626 		return -EMSGSIZE;
627 
628 	return (dev->ops.fill_res_cq_entry) ?
629 		dev->ops.fill_res_cq_entry(msg, cq) : 0;
630 }
631 
632 static int fill_res_cq_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
633 				 struct rdma_restrack_entry *res, uint32_t port)
634 {
635 	struct ib_cq *cq = container_of(res, struct ib_cq, res);
636 	struct ib_device *dev = cq->device;
637 
638 	if (!dev->ops.fill_res_cq_entry_raw)
639 		return -EINVAL;
640 	return dev->ops.fill_res_cq_entry_raw(msg, cq);
641 }
642 
643 static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
644 			     struct rdma_restrack_entry *res, uint32_t port)
645 {
646 	struct ib_mr *mr = container_of(res, struct ib_mr, res);
647 	struct ib_device *dev = mr->pd->device;
648 
649 	if (has_cap_net_admin) {
650 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
651 			return -EMSGSIZE;
652 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
653 			return -EMSGSIZE;
654 	}
655 
656 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length,
657 			      RDMA_NLDEV_ATTR_PAD))
658 		return -EMSGSIZE;
659 
660 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
661 		return -EMSGSIZE;
662 
663 	if (!rdma_is_kernel_res(res) &&
664 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, mr->pd->res.id))
665 		return -EMSGSIZE;
666 
667 	if (fill_res_name_pid(msg, res))
668 		return -EMSGSIZE;
669 
670 	return (dev->ops.fill_res_mr_entry) ?
671 		       dev->ops.fill_res_mr_entry(msg, mr) :
672 		       0;
673 }
674 
675 static int fill_res_mr_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
676 				 struct rdma_restrack_entry *res, uint32_t port)
677 {
678 	struct ib_mr *mr = container_of(res, struct ib_mr, res);
679 	struct ib_device *dev = mr->pd->device;
680 
681 	if (!dev->ops.fill_res_mr_entry_raw)
682 		return -EINVAL;
683 	return dev->ops.fill_res_mr_entry_raw(msg, mr);
684 }
685 
686 static int fill_res_pd_entry(struct sk_buff *msg, bool has_cap_net_admin,
687 			     struct rdma_restrack_entry *res, uint32_t port)
688 {
689 	struct ib_pd *pd = container_of(res, struct ib_pd, res);
690 
691 	if (has_cap_net_admin) {
692 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
693 				pd->local_dma_lkey))
694 			goto err;
695 		if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
696 		    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
697 				pd->unsafe_global_rkey))
698 			goto err;
699 	}
700 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
701 			      atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD))
702 		goto err;
703 
704 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, res->id))
705 		goto err;
706 
707 	if (!rdma_is_kernel_res(res) &&
708 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
709 			pd->uobject->context->res.id))
710 		goto err;
711 
712 	return fill_res_name_pid(msg, res);
713 
714 err:	return -EMSGSIZE;
715 }
716 
717 static int fill_res_ctx_entry(struct sk_buff *msg, bool has_cap_net_admin,
718 			      struct rdma_restrack_entry *res, uint32_t port)
719 {
720 	struct ib_ucontext *ctx = container_of(res, struct ib_ucontext, res);
721 
722 	if (rdma_is_kernel_res(res))
723 		return 0;
724 
725 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN, ctx->res.id))
726 		return -EMSGSIZE;
727 
728 	return fill_res_name_pid(msg, res);
729 }
730 
731 static int fill_res_range_qp_entry(struct sk_buff *msg, uint32_t min_range,
732 				   uint32_t max_range)
733 {
734 	struct nlattr *entry_attr;
735 
736 	if (!min_range)
737 		return 0;
738 
739 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
740 	if (!entry_attr)
741 		return -EMSGSIZE;
742 
743 	if (min_range == max_range) {
744 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, min_range))
745 			goto err;
746 	} else {
747 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MIN_RANGE, min_range))
748 			goto err;
749 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MAX_RANGE, max_range))
750 			goto err;
751 	}
752 	nla_nest_end(msg, entry_attr);
753 	return 0;
754 
755 err:
756 	nla_nest_cancel(msg, entry_attr);
757 	return -EMSGSIZE;
758 }
759 
760 static int fill_res_srq_qps(struct sk_buff *msg, struct ib_srq *srq)
761 {
762 	uint32_t min_range = 0, prev = 0;
763 	struct rdma_restrack_entry *res;
764 	struct rdma_restrack_root *rt;
765 	struct nlattr *table_attr;
766 	struct ib_qp *qp = NULL;
767 	unsigned long id = 0;
768 
769 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
770 	if (!table_attr)
771 		return -EMSGSIZE;
772 
773 	rt = &srq->device->res[RDMA_RESTRACK_QP];
774 	xa_lock(&rt->xa);
775 	xa_for_each(&rt->xa, id, res) {
776 		if (!rdma_restrack_get(res))
777 			continue;
778 
779 		qp = container_of(res, struct ib_qp, res);
780 		if (!qp->srq || (qp->srq->res.id != srq->res.id)) {
781 			rdma_restrack_put(res);
782 			continue;
783 		}
784 
785 		if (qp->qp_num < prev)
786 			/* qp_num should be ascending */
787 			goto err_loop;
788 
789 		if (min_range == 0) {
790 			min_range = qp->qp_num;
791 		} else if (qp->qp_num > (prev + 1)) {
792 			if (fill_res_range_qp_entry(msg, min_range, prev))
793 				goto err_loop;
794 
795 			min_range = qp->qp_num;
796 		}
797 		prev = qp->qp_num;
798 		rdma_restrack_put(res);
799 	}
800 
801 	xa_unlock(&rt->xa);
802 
803 	if (fill_res_range_qp_entry(msg, min_range, prev))
804 		goto err;
805 
806 	nla_nest_end(msg, table_attr);
807 	return 0;
808 
809 err_loop:
810 	rdma_restrack_put(res);
811 	xa_unlock(&rt->xa);
812 err:
813 	nla_nest_cancel(msg, table_attr);
814 	return -EMSGSIZE;
815 }
816 
817 static int fill_res_srq_entry(struct sk_buff *msg, bool has_cap_net_admin,
818 			      struct rdma_restrack_entry *res, uint32_t port)
819 {
820 	struct ib_srq *srq = container_of(res, struct ib_srq, res);
821 
822 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SRQN, srq->res.id))
823 		goto err;
824 
825 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, srq->srq_type))
826 		goto err;
827 
828 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, srq->pd->res.id))
829 		goto err;
830 
831 	if (ib_srq_has_cq(srq->srq_type)) {
832 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN,
833 				srq->ext.cq->res.id))
834 			goto err;
835 	}
836 
837 	if (fill_res_srq_qps(msg, srq))
838 		goto err;
839 
840 	return fill_res_name_pid(msg, res);
841 
842 err:
843 	return -EMSGSIZE;
844 }
845 
846 static int fill_stat_counter_mode(struct sk_buff *msg,
847 				  struct rdma_counter *counter)
848 {
849 	struct rdma_counter_mode *m = &counter->mode;
850 
851 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, m->mode))
852 		return -EMSGSIZE;
853 
854 	if (m->mode == RDMA_COUNTER_MODE_AUTO) {
855 		if ((m->mask & RDMA_COUNTER_MASK_QP_TYPE) &&
856 		    nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, m->param.qp_type))
857 			return -EMSGSIZE;
858 
859 		if ((m->mask & RDMA_COUNTER_MASK_PID) &&
860 		    fill_res_name_pid(msg, &counter->res))
861 			return -EMSGSIZE;
862 	}
863 
864 	return 0;
865 }
866 
867 static int fill_stat_counter_qp_entry(struct sk_buff *msg, u32 qpn)
868 {
869 	struct nlattr *entry_attr;
870 
871 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
872 	if (!entry_attr)
873 		return -EMSGSIZE;
874 
875 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn))
876 		goto err;
877 
878 	nla_nest_end(msg, entry_attr);
879 	return 0;
880 
881 err:
882 	nla_nest_cancel(msg, entry_attr);
883 	return -EMSGSIZE;
884 }
885 
886 static int fill_stat_counter_qps(struct sk_buff *msg,
887 				 struct rdma_counter *counter)
888 {
889 	struct rdma_restrack_entry *res;
890 	struct rdma_restrack_root *rt;
891 	struct nlattr *table_attr;
892 	struct ib_qp *qp = NULL;
893 	unsigned long id = 0;
894 	int ret = 0;
895 
896 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
897 	if (!table_attr)
898 		return -EMSGSIZE;
899 
900 	rt = &counter->device->res[RDMA_RESTRACK_QP];
901 	xa_lock(&rt->xa);
902 	xa_for_each(&rt->xa, id, res) {
903 		qp = container_of(res, struct ib_qp, res);
904 		if (!qp->counter || (qp->counter->id != counter->id))
905 			continue;
906 
907 		ret = fill_stat_counter_qp_entry(msg, qp->qp_num);
908 		if (ret)
909 			goto err;
910 	}
911 
912 	xa_unlock(&rt->xa);
913 	nla_nest_end(msg, table_attr);
914 	return 0;
915 
916 err:
917 	xa_unlock(&rt->xa);
918 	nla_nest_cancel(msg, table_attr);
919 	return ret;
920 }
921 
922 int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
923 				 u64 value)
924 {
925 	struct nlattr *entry_attr;
926 
927 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY);
928 	if (!entry_attr)
929 		return -EMSGSIZE;
930 
931 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME,
932 			   name))
933 		goto err;
934 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE,
935 			      value, RDMA_NLDEV_ATTR_PAD))
936 		goto err;
937 
938 	nla_nest_end(msg, entry_attr);
939 	return 0;
940 
941 err:
942 	nla_nest_cancel(msg, entry_attr);
943 	return -EMSGSIZE;
944 }
945 EXPORT_SYMBOL(rdma_nl_stat_hwcounter_entry);
946 
947 static int fill_stat_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
948 			      struct rdma_restrack_entry *res, uint32_t port)
949 {
950 	struct ib_mr *mr = container_of(res, struct ib_mr, res);
951 	struct ib_device *dev = mr->pd->device;
952 
953 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
954 		goto err;
955 
956 	if (dev->ops.fill_stat_mr_entry)
957 		return dev->ops.fill_stat_mr_entry(msg, mr);
958 	return 0;
959 
960 err:
961 	return -EMSGSIZE;
962 }
963 
964 static int fill_stat_counter_hwcounters(struct sk_buff *msg,
965 					struct rdma_counter *counter)
966 {
967 	struct rdma_hw_stats *st = counter->stats;
968 	struct nlattr *table_attr;
969 	int i;
970 
971 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
972 	if (!table_attr)
973 		return -EMSGSIZE;
974 
975 	mutex_lock(&st->lock);
976 	for (i = 0; i < st->num_counters; i++) {
977 		if (test_bit(i, st->is_disabled))
978 			continue;
979 		if (rdma_nl_stat_hwcounter_entry(msg, st->descs[i].name,
980 						 st->value[i]))
981 			goto err;
982 	}
983 	mutex_unlock(&st->lock);
984 
985 	nla_nest_end(msg, table_attr);
986 	return 0;
987 
988 err:
989 	mutex_unlock(&st->lock);
990 	nla_nest_cancel(msg, table_attr);
991 	return -EMSGSIZE;
992 }
993 
994 static int fill_res_counter_entry(struct sk_buff *msg, bool has_cap_net_admin,
995 				  struct rdma_restrack_entry *res,
996 				  uint32_t port)
997 {
998 	struct rdma_counter *counter =
999 		container_of(res, struct rdma_counter, res);
1000 
1001 	if (port && port != counter->port)
1002 		return -EAGAIN;
1003 
1004 	/* Dump it even query failed */
1005 	rdma_counter_query_stats(counter);
1006 
1007 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, counter->port) ||
1008 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, counter->id) ||
1009 	    fill_stat_counter_mode(msg, counter) ||
1010 	    fill_stat_counter_qps(msg, counter) ||
1011 	    fill_stat_counter_hwcounters(msg, counter))
1012 		return -EMSGSIZE;
1013 
1014 	return 0;
1015 }
1016 
1017 static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1018 			  struct netlink_ext_ack *extack)
1019 {
1020 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1021 	struct ib_device *device;
1022 	struct sk_buff *msg;
1023 	u32 index;
1024 	int err;
1025 
1026 	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1027 				     nldev_policy, extack);
1028 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1029 		return -EINVAL;
1030 
1031 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1032 
1033 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1034 	if (!device)
1035 		return -EINVAL;
1036 
1037 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1038 	if (!msg) {
1039 		err = -ENOMEM;
1040 		goto err;
1041 	}
1042 
1043 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1044 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1045 			0, 0);
1046 
1047 	err = fill_dev_info(msg, device);
1048 	if (err)
1049 		goto err_free;
1050 
1051 	nlmsg_end(msg, nlh);
1052 
1053 	ib_device_put(device);
1054 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1055 
1056 err_free:
1057 	nlmsg_free(msg);
1058 err:
1059 	ib_device_put(device);
1060 	return err;
1061 }
1062 
1063 static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1064 			  struct netlink_ext_ack *extack)
1065 {
1066 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1067 	struct ib_device *device;
1068 	u32 index;
1069 	int err;
1070 
1071 	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1072 				     nldev_policy, extack);
1073 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1074 		return -EINVAL;
1075 
1076 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1077 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1078 	if (!device)
1079 		return -EINVAL;
1080 
1081 	if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) {
1082 		char name[IB_DEVICE_NAME_MAX] = {};
1083 
1084 		nla_strscpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1085 			    IB_DEVICE_NAME_MAX);
1086 		if (strlen(name) == 0) {
1087 			err = -EINVAL;
1088 			goto done;
1089 		}
1090 		err = ib_device_rename(device, name);
1091 		goto done;
1092 	}
1093 
1094 	if (tb[RDMA_NLDEV_NET_NS_FD]) {
1095 		u32 ns_fd;
1096 
1097 		ns_fd = nla_get_u32(tb[RDMA_NLDEV_NET_NS_FD]);
1098 		err = ib_device_set_netns_put(skb, device, ns_fd);
1099 		goto put_done;
1100 	}
1101 
1102 	if (tb[RDMA_NLDEV_ATTR_DEV_DIM]) {
1103 		u8 use_dim;
1104 
1105 		use_dim = nla_get_u8(tb[RDMA_NLDEV_ATTR_DEV_DIM]);
1106 		err = ib_device_set_dim(device,  use_dim);
1107 		goto done;
1108 	}
1109 
1110 done:
1111 	ib_device_put(device);
1112 put_done:
1113 	return err;
1114 }
1115 
1116 static int _nldev_get_dumpit(struct ib_device *device,
1117 			     struct sk_buff *skb,
1118 			     struct netlink_callback *cb,
1119 			     unsigned int idx)
1120 {
1121 	int start = cb->args[0];
1122 	struct nlmsghdr *nlh;
1123 
1124 	if (idx < start)
1125 		return 0;
1126 
1127 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1128 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1129 			0, NLM_F_MULTI);
1130 
1131 	if (fill_dev_info(skb, device)) {
1132 		nlmsg_cancel(skb, nlh);
1133 		goto out;
1134 	}
1135 
1136 	nlmsg_end(skb, nlh);
1137 
1138 	idx++;
1139 
1140 out:	cb->args[0] = idx;
1141 	return skb->len;
1142 }
1143 
1144 static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
1145 {
1146 	/*
1147 	 * There is no need to take lock, because
1148 	 * we are relying on ib_core's locking.
1149 	 */
1150 	return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
1151 }
1152 
1153 static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1154 			       struct netlink_ext_ack *extack)
1155 {
1156 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1157 	struct ib_device *device;
1158 	struct sk_buff *msg;
1159 	u32 index;
1160 	u32 port;
1161 	int err;
1162 
1163 	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1164 				     nldev_policy, extack);
1165 	if (err ||
1166 	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
1167 	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
1168 		return -EINVAL;
1169 
1170 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1171 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1172 	if (!device)
1173 		return -EINVAL;
1174 
1175 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1176 	if (!rdma_is_port_valid(device, port)) {
1177 		err = -EINVAL;
1178 		goto err;
1179 	}
1180 
1181 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1182 	if (!msg) {
1183 		err = -ENOMEM;
1184 		goto err;
1185 	}
1186 
1187 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1188 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1189 			0, 0);
1190 
1191 	err = fill_port_info(msg, device, port, sock_net(skb->sk));
1192 	if (err)
1193 		goto err_free;
1194 
1195 	nlmsg_end(msg, nlh);
1196 	ib_device_put(device);
1197 
1198 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1199 
1200 err_free:
1201 	nlmsg_free(msg);
1202 err:
1203 	ib_device_put(device);
1204 	return err;
1205 }
1206 
1207 static int nldev_port_get_dumpit(struct sk_buff *skb,
1208 				 struct netlink_callback *cb)
1209 {
1210 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1211 	struct ib_device *device;
1212 	int start = cb->args[0];
1213 	struct nlmsghdr *nlh;
1214 	u32 idx = 0;
1215 	u32 ifindex;
1216 	int err;
1217 	unsigned int p;
1218 
1219 	err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1220 				     nldev_policy, NULL);
1221 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1222 		return -EINVAL;
1223 
1224 	ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1225 	device = ib_device_get_by_index(sock_net(skb->sk), ifindex);
1226 	if (!device)
1227 		return -EINVAL;
1228 
1229 	rdma_for_each_port (device, p) {
1230 		/*
1231 		 * The dumpit function returns all information from specific
1232 		 * index. This specific index is taken from the netlink
1233 		 * messages request sent by user and it is available
1234 		 * in cb->args[0].
1235 		 *
1236 		 * Usually, the user doesn't fill this field and it causes
1237 		 * to return everything.
1238 		 *
1239 		 */
1240 		if (idx < start) {
1241 			idx++;
1242 			continue;
1243 		}
1244 
1245 		nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
1246 				cb->nlh->nlmsg_seq,
1247 				RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1248 						 RDMA_NLDEV_CMD_PORT_GET),
1249 				0, NLM_F_MULTI);
1250 
1251 		if (fill_port_info(skb, device, p, sock_net(skb->sk))) {
1252 			nlmsg_cancel(skb, nlh);
1253 			goto out;
1254 		}
1255 		idx++;
1256 		nlmsg_end(skb, nlh);
1257 	}
1258 
1259 out:
1260 	ib_device_put(device);
1261 	cb->args[0] = idx;
1262 	return skb->len;
1263 }
1264 
1265 static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1266 			      struct netlink_ext_ack *extack)
1267 {
1268 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1269 	struct ib_device *device;
1270 	struct sk_buff *msg;
1271 	u32 index;
1272 	int ret;
1273 
1274 	ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1275 				     nldev_policy, extack);
1276 	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1277 		return -EINVAL;
1278 
1279 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1280 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1281 	if (!device)
1282 		return -EINVAL;
1283 
1284 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1285 	if (!msg) {
1286 		ret = -ENOMEM;
1287 		goto err;
1288 	}
1289 
1290 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1291 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1292 			0, 0);
1293 
1294 	ret = fill_res_info(msg, device);
1295 	if (ret)
1296 		goto err_free;
1297 
1298 	nlmsg_end(msg, nlh);
1299 	ib_device_put(device);
1300 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1301 
1302 err_free:
1303 	nlmsg_free(msg);
1304 err:
1305 	ib_device_put(device);
1306 	return ret;
1307 }
1308 
1309 static int _nldev_res_get_dumpit(struct ib_device *device,
1310 				 struct sk_buff *skb,
1311 				 struct netlink_callback *cb,
1312 				 unsigned int idx)
1313 {
1314 	int start = cb->args[0];
1315 	struct nlmsghdr *nlh;
1316 
1317 	if (idx < start)
1318 		return 0;
1319 
1320 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1321 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1322 			0, NLM_F_MULTI);
1323 
1324 	if (fill_res_info(skb, device)) {
1325 		nlmsg_cancel(skb, nlh);
1326 		goto out;
1327 	}
1328 	nlmsg_end(skb, nlh);
1329 
1330 	idx++;
1331 
1332 out:
1333 	cb->args[0] = idx;
1334 	return skb->len;
1335 }
1336 
1337 static int nldev_res_get_dumpit(struct sk_buff *skb,
1338 				struct netlink_callback *cb)
1339 {
1340 	return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
1341 }
1342 
1343 struct nldev_fill_res_entry {
1344 	enum rdma_nldev_attr nldev_attr;
1345 	u8 flags;
1346 	u32 entry;
1347 	u32 id;
1348 };
1349 
1350 enum nldev_res_flags {
1351 	NLDEV_PER_DEV = 1 << 0,
1352 };
1353 
1354 static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
1355 	[RDMA_RESTRACK_QP] = {
1356 		.nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
1357 		.entry = RDMA_NLDEV_ATTR_RES_QP_ENTRY,
1358 		.id = RDMA_NLDEV_ATTR_RES_LQPN,
1359 	},
1360 	[RDMA_RESTRACK_CM_ID] = {
1361 		.nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
1362 		.entry = RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY,
1363 		.id = RDMA_NLDEV_ATTR_RES_CM_IDN,
1364 	},
1365 	[RDMA_RESTRACK_CQ] = {
1366 		.nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
1367 		.flags = NLDEV_PER_DEV,
1368 		.entry = RDMA_NLDEV_ATTR_RES_CQ_ENTRY,
1369 		.id = RDMA_NLDEV_ATTR_RES_CQN,
1370 	},
1371 	[RDMA_RESTRACK_MR] = {
1372 		.nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
1373 		.flags = NLDEV_PER_DEV,
1374 		.entry = RDMA_NLDEV_ATTR_RES_MR_ENTRY,
1375 		.id = RDMA_NLDEV_ATTR_RES_MRN,
1376 	},
1377 	[RDMA_RESTRACK_PD] = {
1378 		.nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
1379 		.flags = NLDEV_PER_DEV,
1380 		.entry = RDMA_NLDEV_ATTR_RES_PD_ENTRY,
1381 		.id = RDMA_NLDEV_ATTR_RES_PDN,
1382 	},
1383 	[RDMA_RESTRACK_COUNTER] = {
1384 		.nldev_attr = RDMA_NLDEV_ATTR_STAT_COUNTER,
1385 		.entry = RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY,
1386 		.id = RDMA_NLDEV_ATTR_STAT_COUNTER_ID,
1387 	},
1388 	[RDMA_RESTRACK_CTX] = {
1389 		.nldev_attr = RDMA_NLDEV_ATTR_RES_CTX,
1390 		.flags = NLDEV_PER_DEV,
1391 		.entry = RDMA_NLDEV_ATTR_RES_CTX_ENTRY,
1392 		.id = RDMA_NLDEV_ATTR_RES_CTXN,
1393 	},
1394 	[RDMA_RESTRACK_SRQ] = {
1395 		.nldev_attr = RDMA_NLDEV_ATTR_RES_SRQ,
1396 		.flags = NLDEV_PER_DEV,
1397 		.entry = RDMA_NLDEV_ATTR_RES_SRQ_ENTRY,
1398 		.id = RDMA_NLDEV_ATTR_RES_SRQN,
1399 	},
1400 
1401 };
1402 
1403 static int res_get_common_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1404 			       struct netlink_ext_ack *extack,
1405 			       enum rdma_restrack_type res_type,
1406 			       res_fill_func_t fill_func)
1407 {
1408 	const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1409 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1410 	struct rdma_restrack_entry *res;
1411 	struct ib_device *device;
1412 	u32 index, id, port = 0;
1413 	bool has_cap_net_admin;
1414 	struct sk_buff *msg;
1415 	int ret;
1416 
1417 	ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1418 				     nldev_policy, extack);
1419 	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !fe->id || !tb[fe->id])
1420 		return -EINVAL;
1421 
1422 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1423 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1424 	if (!device)
1425 		return -EINVAL;
1426 
1427 	if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1428 		port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1429 		if (!rdma_is_port_valid(device, port)) {
1430 			ret = -EINVAL;
1431 			goto err;
1432 		}
1433 	}
1434 
1435 	if ((port && fe->flags & NLDEV_PER_DEV) ||
1436 	    (!port && ~fe->flags & NLDEV_PER_DEV)) {
1437 		ret = -EINVAL;
1438 		goto err;
1439 	}
1440 
1441 	id = nla_get_u32(tb[fe->id]);
1442 	res = rdma_restrack_get_byid(device, res_type, id);
1443 	if (IS_ERR(res)) {
1444 		ret = PTR_ERR(res);
1445 		goto err;
1446 	}
1447 
1448 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1449 	if (!msg) {
1450 		ret = -ENOMEM;
1451 		goto err_get;
1452 	}
1453 
1454 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1455 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1456 					 RDMA_NL_GET_OP(nlh->nlmsg_type)),
1457 			0, 0);
1458 
1459 	if (fill_nldev_handle(msg, device)) {
1460 		ret = -EMSGSIZE;
1461 		goto err_free;
1462 	}
1463 
1464 	has_cap_net_admin = netlink_capable(skb, CAP_NET_ADMIN);
1465 
1466 	ret = fill_func(msg, has_cap_net_admin, res, port);
1467 	if (ret)
1468 		goto err_free;
1469 
1470 	rdma_restrack_put(res);
1471 	nlmsg_end(msg, nlh);
1472 	ib_device_put(device);
1473 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1474 
1475 err_free:
1476 	nlmsg_free(msg);
1477 err_get:
1478 	rdma_restrack_put(res);
1479 err:
1480 	ib_device_put(device);
1481 	return ret;
1482 }
1483 
1484 static int res_get_common_dumpit(struct sk_buff *skb,
1485 				 struct netlink_callback *cb,
1486 				 enum rdma_restrack_type res_type,
1487 				 res_fill_func_t fill_func)
1488 {
1489 	const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1490 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1491 	struct rdma_restrack_entry *res;
1492 	struct rdma_restrack_root *rt;
1493 	int err, ret = 0, idx = 0;
1494 	struct nlattr *table_attr;
1495 	struct nlattr *entry_attr;
1496 	struct ib_device *device;
1497 	int start = cb->args[0];
1498 	bool has_cap_net_admin;
1499 	struct nlmsghdr *nlh;
1500 	unsigned long id;
1501 	u32 index, port = 0;
1502 	bool filled = false;
1503 
1504 	err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1505 				     nldev_policy, NULL);
1506 	/*
1507 	 * Right now, we are expecting the device index to get res information,
1508 	 * but it is possible to extend this code to return all devices in
1509 	 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
1510 	 * if it doesn't exist, we will iterate over all devices.
1511 	 *
1512 	 * But it is not needed for now.
1513 	 */
1514 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1515 		return -EINVAL;
1516 
1517 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1518 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1519 	if (!device)
1520 		return -EINVAL;
1521 
1522 	/*
1523 	 * If no PORT_INDEX is supplied, we will return all QPs from that device
1524 	 */
1525 	if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1526 		port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1527 		if (!rdma_is_port_valid(device, port)) {
1528 			ret = -EINVAL;
1529 			goto err_index;
1530 		}
1531 	}
1532 
1533 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1534 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1535 					 RDMA_NL_GET_OP(cb->nlh->nlmsg_type)),
1536 			0, NLM_F_MULTI);
1537 
1538 	if (fill_nldev_handle(skb, device)) {
1539 		ret = -EMSGSIZE;
1540 		goto err;
1541 	}
1542 
1543 	table_attr = nla_nest_start_noflag(skb, fe->nldev_attr);
1544 	if (!table_attr) {
1545 		ret = -EMSGSIZE;
1546 		goto err;
1547 	}
1548 
1549 	has_cap_net_admin = netlink_capable(cb->skb, CAP_NET_ADMIN);
1550 
1551 	rt = &device->res[res_type];
1552 	xa_lock(&rt->xa);
1553 	/*
1554 	 * FIXME: if the skip ahead is something common this loop should
1555 	 * use xas_for_each & xas_pause to optimize, we can have a lot of
1556 	 * objects.
1557 	 */
1558 	xa_for_each(&rt->xa, id, res) {
1559 		if (idx < start || !rdma_restrack_get(res))
1560 			goto next;
1561 
1562 		xa_unlock(&rt->xa);
1563 
1564 		filled = true;
1565 
1566 		entry_attr = nla_nest_start_noflag(skb, fe->entry);
1567 		if (!entry_attr) {
1568 			ret = -EMSGSIZE;
1569 			rdma_restrack_put(res);
1570 			goto msg_full;
1571 		}
1572 
1573 		ret = fill_func(skb, has_cap_net_admin, res, port);
1574 
1575 		rdma_restrack_put(res);
1576 
1577 		if (ret) {
1578 			nla_nest_cancel(skb, entry_attr);
1579 			if (ret == -EMSGSIZE)
1580 				goto msg_full;
1581 			if (ret == -EAGAIN)
1582 				goto again;
1583 			goto res_err;
1584 		}
1585 		nla_nest_end(skb, entry_attr);
1586 again:		xa_lock(&rt->xa);
1587 next:		idx++;
1588 	}
1589 	xa_unlock(&rt->xa);
1590 
1591 msg_full:
1592 	nla_nest_end(skb, table_attr);
1593 	nlmsg_end(skb, nlh);
1594 	cb->args[0] = idx;
1595 
1596 	/*
1597 	 * No more entries to fill, cancel the message and
1598 	 * return 0 to mark end of dumpit.
1599 	 */
1600 	if (!filled)
1601 		goto err;
1602 
1603 	ib_device_put(device);
1604 	return skb->len;
1605 
1606 res_err:
1607 	nla_nest_cancel(skb, table_attr);
1608 
1609 err:
1610 	nlmsg_cancel(skb, nlh);
1611 
1612 err_index:
1613 	ib_device_put(device);
1614 	return ret;
1615 }
1616 
1617 #define RES_GET_FUNCS(name, type)                                              \
1618 	static int nldev_res_get_##name##_dumpit(struct sk_buff *skb,          \
1619 						 struct netlink_callback *cb)  \
1620 	{                                                                      \
1621 		return res_get_common_dumpit(skb, cb, type,                    \
1622 					     fill_res_##name##_entry);         \
1623 	}                                                                      \
1624 	static int nldev_res_get_##name##_doit(struct sk_buff *skb,            \
1625 					       struct nlmsghdr *nlh,           \
1626 					       struct netlink_ext_ack *extack) \
1627 	{                                                                      \
1628 		return res_get_common_doit(skb, nlh, extack, type,             \
1629 					   fill_res_##name##_entry);           \
1630 	}
1631 
1632 RES_GET_FUNCS(qp, RDMA_RESTRACK_QP);
1633 RES_GET_FUNCS(qp_raw, RDMA_RESTRACK_QP);
1634 RES_GET_FUNCS(cm_id, RDMA_RESTRACK_CM_ID);
1635 RES_GET_FUNCS(cq, RDMA_RESTRACK_CQ);
1636 RES_GET_FUNCS(cq_raw, RDMA_RESTRACK_CQ);
1637 RES_GET_FUNCS(pd, RDMA_RESTRACK_PD);
1638 RES_GET_FUNCS(mr, RDMA_RESTRACK_MR);
1639 RES_GET_FUNCS(mr_raw, RDMA_RESTRACK_MR);
1640 RES_GET_FUNCS(counter, RDMA_RESTRACK_COUNTER);
1641 RES_GET_FUNCS(ctx, RDMA_RESTRACK_CTX);
1642 RES_GET_FUNCS(srq, RDMA_RESTRACK_SRQ);
1643 
1644 static LIST_HEAD(link_ops);
1645 static DECLARE_RWSEM(link_ops_rwsem);
1646 
1647 static const struct rdma_link_ops *link_ops_get(const char *type)
1648 {
1649 	const struct rdma_link_ops *ops;
1650 
1651 	list_for_each_entry(ops, &link_ops, list) {
1652 		if (!strcmp(ops->type, type))
1653 			goto out;
1654 	}
1655 	ops = NULL;
1656 out:
1657 	return ops;
1658 }
1659 
1660 void rdma_link_register(struct rdma_link_ops *ops)
1661 {
1662 	down_write(&link_ops_rwsem);
1663 	if (WARN_ON_ONCE(link_ops_get(ops->type)))
1664 		goto out;
1665 	list_add(&ops->list, &link_ops);
1666 out:
1667 	up_write(&link_ops_rwsem);
1668 }
1669 EXPORT_SYMBOL(rdma_link_register);
1670 
1671 void rdma_link_unregister(struct rdma_link_ops *ops)
1672 {
1673 	down_write(&link_ops_rwsem);
1674 	list_del(&ops->list);
1675 	up_write(&link_ops_rwsem);
1676 }
1677 EXPORT_SYMBOL(rdma_link_unregister);
1678 
1679 static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
1680 			  struct netlink_ext_ack *extack)
1681 {
1682 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1683 	char ibdev_name[IB_DEVICE_NAME_MAX];
1684 	const struct rdma_link_ops *ops;
1685 	char ndev_name[IFNAMSIZ];
1686 	struct net_device *ndev;
1687 	char type[IFNAMSIZ];
1688 	int err;
1689 
1690 	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1691 				     nldev_policy, extack);
1692 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
1693 	    !tb[RDMA_NLDEV_ATTR_LINK_TYPE] || !tb[RDMA_NLDEV_ATTR_NDEV_NAME])
1694 		return -EINVAL;
1695 
1696 	nla_strscpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1697 		    sizeof(ibdev_name));
1698 	if (strchr(ibdev_name, '%') || strlen(ibdev_name) == 0)
1699 		return -EINVAL;
1700 
1701 	nla_strscpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type));
1702 	nla_strscpy(ndev_name, tb[RDMA_NLDEV_ATTR_NDEV_NAME],
1703 		    sizeof(ndev_name));
1704 
1705 	ndev = dev_get_by_name(sock_net(skb->sk), ndev_name);
1706 	if (!ndev)
1707 		return -ENODEV;
1708 
1709 	down_read(&link_ops_rwsem);
1710 	ops = link_ops_get(type);
1711 #ifdef CONFIG_MODULES
1712 	if (!ops) {
1713 		up_read(&link_ops_rwsem);
1714 		request_module("rdma-link-%s", type);
1715 		down_read(&link_ops_rwsem);
1716 		ops = link_ops_get(type);
1717 	}
1718 #endif
1719 	err = ops ? ops->newlink(ibdev_name, ndev) : -EINVAL;
1720 	up_read(&link_ops_rwsem);
1721 	dev_put(ndev);
1722 
1723 	return err;
1724 }
1725 
1726 static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
1727 			  struct netlink_ext_ack *extack)
1728 {
1729 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1730 	struct ib_device *device;
1731 	u32 index;
1732 	int err;
1733 
1734 	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1735 				     nldev_policy, extack);
1736 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1737 		return -EINVAL;
1738 
1739 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1740 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1741 	if (!device)
1742 		return -EINVAL;
1743 
1744 	if (!(device->attrs.kernel_cap_flags & IBK_ALLOW_USER_UNREG)) {
1745 		ib_device_put(device);
1746 		return -EINVAL;
1747 	}
1748 
1749 	ib_unregister_device_and_put(device);
1750 	return 0;
1751 }
1752 
1753 static int nldev_get_chardev(struct sk_buff *skb, struct nlmsghdr *nlh,
1754 			     struct netlink_ext_ack *extack)
1755 {
1756 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1757 	char client_name[RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE];
1758 	struct ib_client_nl_info data = {};
1759 	struct ib_device *ibdev = NULL;
1760 	struct sk_buff *msg;
1761 	u32 index;
1762 	int err;
1763 
1764 	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
1765 			  extack);
1766 	if (err || !tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE])
1767 		return -EINVAL;
1768 
1769 	nla_strscpy(client_name, tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE],
1770 		    sizeof(client_name));
1771 
1772 	if (tb[RDMA_NLDEV_ATTR_DEV_INDEX]) {
1773 		index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1774 		ibdev = ib_device_get_by_index(sock_net(skb->sk), index);
1775 		if (!ibdev)
1776 			return -EINVAL;
1777 
1778 		if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1779 			data.port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1780 			if (!rdma_is_port_valid(ibdev, data.port)) {
1781 				err = -EINVAL;
1782 				goto out_put;
1783 			}
1784 		} else {
1785 			data.port = -1;
1786 		}
1787 	} else if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1788 		return -EINVAL;
1789 	}
1790 
1791 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1792 	if (!msg) {
1793 		err = -ENOMEM;
1794 		goto out_put;
1795 	}
1796 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1797 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1798 					 RDMA_NLDEV_CMD_GET_CHARDEV),
1799 			0, 0);
1800 
1801 	data.nl_msg = msg;
1802 	err = ib_get_client_nl_info(ibdev, client_name, &data);
1803 	if (err)
1804 		goto out_nlmsg;
1805 
1806 	err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV,
1807 				huge_encode_dev(data.cdev->devt),
1808 				RDMA_NLDEV_ATTR_PAD);
1809 	if (err)
1810 		goto out_data;
1811 	err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV_ABI, data.abi,
1812 				RDMA_NLDEV_ATTR_PAD);
1813 	if (err)
1814 		goto out_data;
1815 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_CHARDEV_NAME,
1816 			   dev_name(data.cdev))) {
1817 		err = -EMSGSIZE;
1818 		goto out_data;
1819 	}
1820 
1821 	nlmsg_end(msg, nlh);
1822 	put_device(data.cdev);
1823 	if (ibdev)
1824 		ib_device_put(ibdev);
1825 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1826 
1827 out_data:
1828 	put_device(data.cdev);
1829 out_nlmsg:
1830 	nlmsg_free(msg);
1831 out_put:
1832 	if (ibdev)
1833 		ib_device_put(ibdev);
1834 	return err;
1835 }
1836 
1837 static int nldev_sys_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1838 			      struct netlink_ext_ack *extack)
1839 {
1840 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1841 	struct sk_buff *msg;
1842 	int err;
1843 
1844 	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1845 			  nldev_policy, extack);
1846 	if (err)
1847 		return err;
1848 
1849 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1850 	if (!msg)
1851 		return -ENOMEM;
1852 
1853 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1854 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1855 					 RDMA_NLDEV_CMD_SYS_GET),
1856 			0, 0);
1857 
1858 	err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_NETNS_MODE,
1859 			 (u8)ib_devices_shared_netns);
1860 	if (err) {
1861 		nlmsg_free(msg);
1862 		return err;
1863 	}
1864 
1865 	/*
1866 	 * Copy-on-fork is supported.
1867 	 * See commits:
1868 	 * 70e806e4e645 ("mm: Do early cow for pinned pages during fork() for ptes")
1869 	 * 4eae4efa2c29 ("hugetlb: do early cow when page pinned on src mm")
1870 	 * for more details. Don't backport this without them.
1871 	 *
1872 	 * Return value ignored on purpose, assume copy-on-fork is not
1873 	 * supported in case of failure.
1874 	 */
1875 	nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK, 1);
1876 
1877 	nlmsg_end(msg, nlh);
1878 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1879 }
1880 
1881 static int nldev_set_sys_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1882 				  struct netlink_ext_ack *extack)
1883 {
1884 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1885 	u8 enable;
1886 	int err;
1887 
1888 	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1889 			  nldev_policy, extack);
1890 	if (err || !tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE])
1891 		return -EINVAL;
1892 
1893 	enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]);
1894 	/* Only 0 and 1 are supported */
1895 	if (enable > 1)
1896 		return -EINVAL;
1897 
1898 	err = rdma_compatdev_set(enable);
1899 	return err;
1900 }
1901 
1902 static int nldev_stat_set_mode_doit(struct sk_buff *msg,
1903 				    struct netlink_ext_ack *extack,
1904 				    struct nlattr *tb[],
1905 				    struct ib_device *device, u32 port)
1906 {
1907 	u32 mode, mask = 0, qpn, cntn = 0;
1908 	int ret;
1909 
1910 	/* Currently only counter for QP is supported */
1911 	if (!tb[RDMA_NLDEV_ATTR_STAT_RES] ||
1912 	    nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
1913 		return -EINVAL;
1914 
1915 	mode = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_MODE]);
1916 	if (mode == RDMA_COUNTER_MODE_AUTO) {
1917 		if (tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK])
1918 			mask = nla_get_u32(
1919 				tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]);
1920 		return rdma_counter_set_auto_mode(device, port, mask, extack);
1921 	}
1922 
1923 	if (!tb[RDMA_NLDEV_ATTR_RES_LQPN])
1924 		return -EINVAL;
1925 
1926 	qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
1927 	if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]) {
1928 		cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
1929 		ret = rdma_counter_bind_qpn(device, port, qpn, cntn);
1930 		if (ret)
1931 			return ret;
1932 	} else {
1933 		ret = rdma_counter_bind_qpn_alloc(device, port, qpn, &cntn);
1934 		if (ret)
1935 			return ret;
1936 	}
1937 
1938 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
1939 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
1940 		ret = -EMSGSIZE;
1941 		goto err_fill;
1942 	}
1943 
1944 	return 0;
1945 
1946 err_fill:
1947 	rdma_counter_unbind_qpn(device, port, qpn, cntn);
1948 	return ret;
1949 }
1950 
1951 static int nldev_stat_set_counter_dynamic_doit(struct nlattr *tb[],
1952 					       struct ib_device *device,
1953 					       u32 port)
1954 {
1955 	struct rdma_hw_stats *stats;
1956 	struct nlattr *entry_attr;
1957 	unsigned long *target;
1958 	int rem, i, ret = 0;
1959 	u32 index;
1960 
1961 	stats = ib_get_hw_stats_port(device, port);
1962 	if (!stats)
1963 		return -EINVAL;
1964 
1965 	target = kcalloc(BITS_TO_LONGS(stats->num_counters),
1966 			 sizeof(*stats->is_disabled), GFP_KERNEL);
1967 	if (!target)
1968 		return -ENOMEM;
1969 
1970 	nla_for_each_nested(entry_attr, tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS],
1971 			    rem) {
1972 		index = nla_get_u32(entry_attr);
1973 		if ((index >= stats->num_counters) ||
1974 		    !(stats->descs[index].flags & IB_STAT_FLAG_OPTIONAL)) {
1975 			ret = -EINVAL;
1976 			goto out;
1977 		}
1978 
1979 		set_bit(index, target);
1980 	}
1981 
1982 	for (i = 0; i < stats->num_counters; i++) {
1983 		if (!(stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL))
1984 			continue;
1985 
1986 		ret = rdma_counter_modify(device, port, i, test_bit(i, target));
1987 		if (ret)
1988 			goto out;
1989 	}
1990 
1991 out:
1992 	kfree(target);
1993 	return ret;
1994 }
1995 
1996 static int nldev_stat_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1997 			       struct netlink_ext_ack *extack)
1998 {
1999 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2000 	struct ib_device *device;
2001 	struct sk_buff *msg;
2002 	u32 index, port;
2003 	int ret;
2004 
2005 	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
2006 			  extack);
2007 	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
2008 	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2009 		return -EINVAL;
2010 
2011 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2012 	device = ib_device_get_by_index(sock_net(skb->sk), index);
2013 	if (!device)
2014 		return -EINVAL;
2015 
2016 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2017 	if (!rdma_is_port_valid(device, port)) {
2018 		ret = -EINVAL;
2019 		goto err_put_device;
2020 	}
2021 
2022 	if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] &&
2023 	    !tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]) {
2024 		ret = -EINVAL;
2025 		goto err_put_device;
2026 	}
2027 
2028 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2029 	if (!msg) {
2030 		ret = -ENOMEM;
2031 		goto err_put_device;
2032 	}
2033 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2034 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2035 					 RDMA_NLDEV_CMD_STAT_SET),
2036 			0, 0);
2037 	if (fill_nldev_handle(msg, device) ||
2038 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) {
2039 		ret = -EMSGSIZE;
2040 		goto err_free_msg;
2041 	}
2042 
2043 	if (tb[RDMA_NLDEV_ATTR_STAT_MODE]) {
2044 		ret = nldev_stat_set_mode_doit(msg, extack, tb, device, port);
2045 		if (ret)
2046 			goto err_free_msg;
2047 	}
2048 
2049 	if (tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]) {
2050 		ret = nldev_stat_set_counter_dynamic_doit(tb, device, port);
2051 		if (ret)
2052 			goto err_free_msg;
2053 	}
2054 
2055 	nlmsg_end(msg, nlh);
2056 	ib_device_put(device);
2057 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2058 
2059 err_free_msg:
2060 	nlmsg_free(msg);
2061 err_put_device:
2062 	ib_device_put(device);
2063 	return ret;
2064 }
2065 
2066 static int nldev_stat_del_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2067 			       struct netlink_ext_ack *extack)
2068 {
2069 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2070 	struct ib_device *device;
2071 	struct sk_buff *msg;
2072 	u32 index, port, qpn, cntn;
2073 	int ret;
2074 
2075 	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2076 			  nldev_policy, extack);
2077 	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES] ||
2078 	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX] ||
2079 	    !tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID] ||
2080 	    !tb[RDMA_NLDEV_ATTR_RES_LQPN])
2081 		return -EINVAL;
2082 
2083 	if (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
2084 		return -EINVAL;
2085 
2086 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2087 	device = ib_device_get_by_index(sock_net(skb->sk), index);
2088 	if (!device)
2089 		return -EINVAL;
2090 
2091 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2092 	if (!rdma_is_port_valid(device, port)) {
2093 		ret = -EINVAL;
2094 		goto err;
2095 	}
2096 
2097 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2098 	if (!msg) {
2099 		ret = -ENOMEM;
2100 		goto err;
2101 	}
2102 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2103 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2104 					 RDMA_NLDEV_CMD_STAT_SET),
2105 			0, 0);
2106 
2107 	cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
2108 	qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
2109 	if (fill_nldev_handle(msg, device) ||
2110 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
2111 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
2112 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
2113 		ret = -EMSGSIZE;
2114 		goto err_fill;
2115 	}
2116 
2117 	ret = rdma_counter_unbind_qpn(device, port, qpn, cntn);
2118 	if (ret)
2119 		goto err_fill;
2120 
2121 	nlmsg_end(msg, nlh);
2122 	ib_device_put(device);
2123 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2124 
2125 err_fill:
2126 	nlmsg_free(msg);
2127 err:
2128 	ib_device_put(device);
2129 	return ret;
2130 }
2131 
2132 static int stat_get_doit_default_counter(struct sk_buff *skb,
2133 					 struct nlmsghdr *nlh,
2134 					 struct netlink_ext_ack *extack,
2135 					 struct nlattr *tb[])
2136 {
2137 	struct rdma_hw_stats *stats;
2138 	struct nlattr *table_attr;
2139 	struct ib_device *device;
2140 	int ret, num_cnts, i;
2141 	struct sk_buff *msg;
2142 	u32 index, port;
2143 	u64 v;
2144 
2145 	if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2146 		return -EINVAL;
2147 
2148 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2149 	device = ib_device_get_by_index(sock_net(skb->sk), index);
2150 	if (!device)
2151 		return -EINVAL;
2152 
2153 	if (!device->ops.alloc_hw_port_stats || !device->ops.get_hw_stats) {
2154 		ret = -EINVAL;
2155 		goto err;
2156 	}
2157 
2158 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2159 	stats = ib_get_hw_stats_port(device, port);
2160 	if (!stats) {
2161 		ret = -EINVAL;
2162 		goto err;
2163 	}
2164 
2165 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2166 	if (!msg) {
2167 		ret = -ENOMEM;
2168 		goto err;
2169 	}
2170 
2171 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2172 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2173 					 RDMA_NLDEV_CMD_STAT_GET),
2174 			0, 0);
2175 
2176 	if (fill_nldev_handle(msg, device) ||
2177 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) {
2178 		ret = -EMSGSIZE;
2179 		goto err_msg;
2180 	}
2181 
2182 	mutex_lock(&stats->lock);
2183 
2184 	num_cnts = device->ops.get_hw_stats(device, stats, port, 0);
2185 	if (num_cnts < 0) {
2186 		ret = -EINVAL;
2187 		goto err_stats;
2188 	}
2189 
2190 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
2191 	if (!table_attr) {
2192 		ret = -EMSGSIZE;
2193 		goto err_stats;
2194 	}
2195 	for (i = 0; i < num_cnts; i++) {
2196 		if (test_bit(i, stats->is_disabled))
2197 			continue;
2198 
2199 		v = stats->value[i] +
2200 			rdma_counter_get_hwstat_value(device, port, i);
2201 		if (rdma_nl_stat_hwcounter_entry(msg,
2202 						 stats->descs[i].name, v)) {
2203 			ret = -EMSGSIZE;
2204 			goto err_table;
2205 		}
2206 	}
2207 	nla_nest_end(msg, table_attr);
2208 
2209 	mutex_unlock(&stats->lock);
2210 	nlmsg_end(msg, nlh);
2211 	ib_device_put(device);
2212 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2213 
2214 err_table:
2215 	nla_nest_cancel(msg, table_attr);
2216 err_stats:
2217 	mutex_unlock(&stats->lock);
2218 err_msg:
2219 	nlmsg_free(msg);
2220 err:
2221 	ib_device_put(device);
2222 	return ret;
2223 }
2224 
2225 static int stat_get_doit_qp(struct sk_buff *skb, struct nlmsghdr *nlh,
2226 			    struct netlink_ext_ack *extack, struct nlattr *tb[])
2227 
2228 {
2229 	static enum rdma_nl_counter_mode mode;
2230 	static enum rdma_nl_counter_mask mask;
2231 	struct ib_device *device;
2232 	struct sk_buff *msg;
2233 	u32 index, port;
2234 	int ret;
2235 
2236 	if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID])
2237 		return nldev_res_get_counter_doit(skb, nlh, extack);
2238 
2239 	if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] ||
2240 	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2241 		return -EINVAL;
2242 
2243 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2244 	device = ib_device_get_by_index(sock_net(skb->sk), index);
2245 	if (!device)
2246 		return -EINVAL;
2247 
2248 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2249 	if (!rdma_is_port_valid(device, port)) {
2250 		ret = -EINVAL;
2251 		goto err;
2252 	}
2253 
2254 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2255 	if (!msg) {
2256 		ret = -ENOMEM;
2257 		goto err;
2258 	}
2259 
2260 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2261 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2262 					 RDMA_NLDEV_CMD_STAT_GET),
2263 			0, 0);
2264 
2265 	ret = rdma_counter_get_mode(device, port, &mode, &mask);
2266 	if (ret)
2267 		goto err_msg;
2268 
2269 	if (fill_nldev_handle(msg, device) ||
2270 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
2271 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, mode)) {
2272 		ret = -EMSGSIZE;
2273 		goto err_msg;
2274 	}
2275 
2276 	if ((mode == RDMA_COUNTER_MODE_AUTO) &&
2277 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK, mask)) {
2278 		ret = -EMSGSIZE;
2279 		goto err_msg;
2280 	}
2281 
2282 	nlmsg_end(msg, nlh);
2283 	ib_device_put(device);
2284 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2285 
2286 err_msg:
2287 	nlmsg_free(msg);
2288 err:
2289 	ib_device_put(device);
2290 	return ret;
2291 }
2292 
2293 static int nldev_stat_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2294 			       struct netlink_ext_ack *extack)
2295 {
2296 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2297 	int ret;
2298 
2299 	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2300 			  nldev_policy, extack);
2301 	if (ret)
2302 		return -EINVAL;
2303 
2304 	if (!tb[RDMA_NLDEV_ATTR_STAT_RES])
2305 		return stat_get_doit_default_counter(skb, nlh, extack, tb);
2306 
2307 	switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2308 	case RDMA_NLDEV_ATTR_RES_QP:
2309 		ret = stat_get_doit_qp(skb, nlh, extack, tb);
2310 		break;
2311 	case RDMA_NLDEV_ATTR_RES_MR:
2312 		ret = res_get_common_doit(skb, nlh, extack, RDMA_RESTRACK_MR,
2313 					  fill_stat_mr_entry);
2314 		break;
2315 	default:
2316 		ret = -EINVAL;
2317 		break;
2318 	}
2319 
2320 	return ret;
2321 }
2322 
2323 static int nldev_stat_get_dumpit(struct sk_buff *skb,
2324 				 struct netlink_callback *cb)
2325 {
2326 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2327 	int ret;
2328 
2329 	ret = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2330 			  nldev_policy, NULL);
2331 	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES])
2332 		return -EINVAL;
2333 
2334 	switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2335 	case RDMA_NLDEV_ATTR_RES_QP:
2336 		ret = nldev_res_get_counter_dumpit(skb, cb);
2337 		break;
2338 	case RDMA_NLDEV_ATTR_RES_MR:
2339 		ret = res_get_common_dumpit(skb, cb, RDMA_RESTRACK_MR,
2340 					    fill_stat_mr_entry);
2341 		break;
2342 	default:
2343 		ret = -EINVAL;
2344 		break;
2345 	}
2346 
2347 	return ret;
2348 }
2349 
2350 static int nldev_stat_get_counter_status_doit(struct sk_buff *skb,
2351 					      struct nlmsghdr *nlh,
2352 					      struct netlink_ext_ack *extack)
2353 {
2354 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX], *table, *entry;
2355 	struct rdma_hw_stats *stats;
2356 	struct ib_device *device;
2357 	struct sk_buff *msg;
2358 	u32 devid, port;
2359 	int ret, i;
2360 
2361 	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2362 			  nldev_policy, extack);
2363 	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
2364 	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2365 		return -EINVAL;
2366 
2367 	devid = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2368 	device = ib_device_get_by_index(sock_net(skb->sk), devid);
2369 	if (!device)
2370 		return -EINVAL;
2371 
2372 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2373 	if (!rdma_is_port_valid(device, port)) {
2374 		ret = -EINVAL;
2375 		goto err;
2376 	}
2377 
2378 	stats = ib_get_hw_stats_port(device, port);
2379 	if (!stats) {
2380 		ret = -EINVAL;
2381 		goto err;
2382 	}
2383 
2384 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2385 	if (!msg) {
2386 		ret = -ENOMEM;
2387 		goto err;
2388 	}
2389 
2390 	nlh = nlmsg_put(
2391 		msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2392 		RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_STAT_GET_STATUS),
2393 		0, 0);
2394 
2395 	ret = -EMSGSIZE;
2396 	if (fill_nldev_handle(msg, device) ||
2397 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
2398 		goto err_msg;
2399 
2400 	table = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
2401 	if (!table)
2402 		goto err_msg;
2403 
2404 	mutex_lock(&stats->lock);
2405 	for (i = 0; i < stats->num_counters; i++) {
2406 		entry = nla_nest_start(msg,
2407 				       RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY);
2408 		if (!entry)
2409 			goto err_msg_table;
2410 
2411 		if (nla_put_string(msg,
2412 				   RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME,
2413 				   stats->descs[i].name) ||
2414 		    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_INDEX, i))
2415 			goto err_msg_entry;
2416 
2417 		if ((stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL) &&
2418 		    (nla_put_u8(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_DYNAMIC,
2419 				!test_bit(i, stats->is_disabled))))
2420 			goto err_msg_entry;
2421 
2422 		nla_nest_end(msg, entry);
2423 	}
2424 	mutex_unlock(&stats->lock);
2425 
2426 	nla_nest_end(msg, table);
2427 	nlmsg_end(msg, nlh);
2428 	ib_device_put(device);
2429 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2430 
2431 err_msg_entry:
2432 	nla_nest_cancel(msg, entry);
2433 err_msg_table:
2434 	mutex_unlock(&stats->lock);
2435 	nla_nest_cancel(msg, table);
2436 err_msg:
2437 	nlmsg_free(msg);
2438 err:
2439 	ib_device_put(device);
2440 	return ret;
2441 }
2442 
2443 static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
2444 	[RDMA_NLDEV_CMD_GET] = {
2445 		.doit = nldev_get_doit,
2446 		.dump = nldev_get_dumpit,
2447 	},
2448 	[RDMA_NLDEV_CMD_GET_CHARDEV] = {
2449 		.doit = nldev_get_chardev,
2450 	},
2451 	[RDMA_NLDEV_CMD_SET] = {
2452 		.doit = nldev_set_doit,
2453 		.flags = RDMA_NL_ADMIN_PERM,
2454 	},
2455 	[RDMA_NLDEV_CMD_NEWLINK] = {
2456 		.doit = nldev_newlink,
2457 		.flags = RDMA_NL_ADMIN_PERM,
2458 	},
2459 	[RDMA_NLDEV_CMD_DELLINK] = {
2460 		.doit = nldev_dellink,
2461 		.flags = RDMA_NL_ADMIN_PERM,
2462 	},
2463 	[RDMA_NLDEV_CMD_PORT_GET] = {
2464 		.doit = nldev_port_get_doit,
2465 		.dump = nldev_port_get_dumpit,
2466 	},
2467 	[RDMA_NLDEV_CMD_RES_GET] = {
2468 		.doit = nldev_res_get_doit,
2469 		.dump = nldev_res_get_dumpit,
2470 	},
2471 	[RDMA_NLDEV_CMD_RES_QP_GET] = {
2472 		.doit = nldev_res_get_qp_doit,
2473 		.dump = nldev_res_get_qp_dumpit,
2474 	},
2475 	[RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
2476 		.doit = nldev_res_get_cm_id_doit,
2477 		.dump = nldev_res_get_cm_id_dumpit,
2478 	},
2479 	[RDMA_NLDEV_CMD_RES_CQ_GET] = {
2480 		.doit = nldev_res_get_cq_doit,
2481 		.dump = nldev_res_get_cq_dumpit,
2482 	},
2483 	[RDMA_NLDEV_CMD_RES_MR_GET] = {
2484 		.doit = nldev_res_get_mr_doit,
2485 		.dump = nldev_res_get_mr_dumpit,
2486 	},
2487 	[RDMA_NLDEV_CMD_RES_PD_GET] = {
2488 		.doit = nldev_res_get_pd_doit,
2489 		.dump = nldev_res_get_pd_dumpit,
2490 	},
2491 	[RDMA_NLDEV_CMD_RES_CTX_GET] = {
2492 		.doit = nldev_res_get_ctx_doit,
2493 		.dump = nldev_res_get_ctx_dumpit,
2494 	},
2495 	[RDMA_NLDEV_CMD_RES_SRQ_GET] = {
2496 		.doit = nldev_res_get_srq_doit,
2497 		.dump = nldev_res_get_srq_dumpit,
2498 	},
2499 	[RDMA_NLDEV_CMD_SYS_GET] = {
2500 		.doit = nldev_sys_get_doit,
2501 	},
2502 	[RDMA_NLDEV_CMD_SYS_SET] = {
2503 		.doit = nldev_set_sys_set_doit,
2504 	},
2505 	[RDMA_NLDEV_CMD_STAT_SET] = {
2506 		.doit = nldev_stat_set_doit,
2507 		.flags = RDMA_NL_ADMIN_PERM,
2508 	},
2509 	[RDMA_NLDEV_CMD_STAT_GET] = {
2510 		.doit = nldev_stat_get_doit,
2511 		.dump = nldev_stat_get_dumpit,
2512 	},
2513 	[RDMA_NLDEV_CMD_STAT_DEL] = {
2514 		.doit = nldev_stat_del_doit,
2515 		.flags = RDMA_NL_ADMIN_PERM,
2516 	},
2517 	[RDMA_NLDEV_CMD_RES_QP_GET_RAW] = {
2518 		.doit = nldev_res_get_qp_raw_doit,
2519 		.dump = nldev_res_get_qp_raw_dumpit,
2520 		.flags = RDMA_NL_ADMIN_PERM,
2521 	},
2522 	[RDMA_NLDEV_CMD_RES_CQ_GET_RAW] = {
2523 		.doit = nldev_res_get_cq_raw_doit,
2524 		.dump = nldev_res_get_cq_raw_dumpit,
2525 		.flags = RDMA_NL_ADMIN_PERM,
2526 	},
2527 	[RDMA_NLDEV_CMD_RES_MR_GET_RAW] = {
2528 		.doit = nldev_res_get_mr_raw_doit,
2529 		.dump = nldev_res_get_mr_raw_dumpit,
2530 		.flags = RDMA_NL_ADMIN_PERM,
2531 	},
2532 	[RDMA_NLDEV_CMD_STAT_GET_STATUS] = {
2533 		.doit = nldev_stat_get_counter_status_doit,
2534 	},
2535 };
2536 
2537 void __init nldev_init(void)
2538 {
2539 	rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
2540 }
2541 
2542 void __exit nldev_exit(void)
2543 {
2544 	rdma_nl_unregister(RDMA_NL_NLDEV);
2545 }
2546 
2547 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);
2548