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