xref: /linux/drivers/infiniband/core/nldev.c (revision fbf03a08679ea02764f25f26564252e879cf14a4)
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 #include <rdma/frmr_pools.h>
41 
42 #include "core_priv.h"
43 #include "cma_priv.h"
44 #include "restrack.h"
45 #include "uverbs.h"
46 #include "frmr_pools.h"
47 
48 /*
49  * This determines whether a non-privileged user is allowed to specify a
50  * controlled QKEY or not, when true non-privileged user is allowed to specify
51  * a controlled QKEY.
52  */
53 static bool privileged_qkey;
54 static DEFINE_MUTEX(nldev_dellink_mutex);
55 
56 typedef int (*res_fill_func_t)(struct sk_buff*, bool,
57 			       struct rdma_restrack_entry*, uint32_t);
58 
59 /*
60  * Sort array elements by the netlink attribute name
61  */
62 static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
63 	[RDMA_NLDEV_ATTR_CHARDEV]		= { .type = NLA_U64 },
64 	[RDMA_NLDEV_ATTR_CHARDEV_ABI]		= { .type = NLA_U64 },
65 	[RDMA_NLDEV_ATTR_CHARDEV_NAME]		= { .type = NLA_NUL_STRING,
66 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
67 	[RDMA_NLDEV_ATTR_CHARDEV_TYPE]		= { .type = NLA_NUL_STRING,
68 					.len = RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE },
69 	[RDMA_NLDEV_ATTR_DEV_DIM]               = { .type = NLA_U8 },
70 	[RDMA_NLDEV_ATTR_DEV_INDEX]		= { .type = NLA_U32 },
71 	[RDMA_NLDEV_ATTR_DEV_NAME]		= { .type = NLA_NUL_STRING,
72 					.len = IB_DEVICE_NAME_MAX },
73 	[RDMA_NLDEV_ATTR_DEV_NODE_TYPE]		= { .type = NLA_U8 },
74 	[RDMA_NLDEV_ATTR_DEV_PROTOCOL]		= { .type = NLA_NUL_STRING,
75 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
76 	[RDMA_NLDEV_ATTR_DRIVER]		= { .type = NLA_NESTED },
77 	[RDMA_NLDEV_ATTR_DRIVER_ENTRY]		= { .type = NLA_NESTED },
78 	[RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE]	= { .type = NLA_U8 },
79 	[RDMA_NLDEV_ATTR_DRIVER_STRING]		= { .type = NLA_NUL_STRING,
80 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
81 	[RDMA_NLDEV_ATTR_DRIVER_S32]		= { .type = NLA_S32 },
82 	[RDMA_NLDEV_ATTR_DRIVER_S64]		= { .type = NLA_S64 },
83 	[RDMA_NLDEV_ATTR_DRIVER_U32]		= { .type = NLA_U32 },
84 	[RDMA_NLDEV_ATTR_DRIVER_U64]		= { .type = NLA_U64 },
85 	[RDMA_NLDEV_ATTR_FW_VERSION]		= { .type = NLA_NUL_STRING,
86 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
87 	[RDMA_NLDEV_ATTR_LID]			= { .type = NLA_U32 },
88 	[RDMA_NLDEV_ATTR_LINK_TYPE]		= { .type = NLA_NUL_STRING,
89 					.len = IFNAMSIZ },
90 	[RDMA_NLDEV_ATTR_LMC]			= { .type = NLA_U8 },
91 	[RDMA_NLDEV_ATTR_NDEV_INDEX]		= { .type = NLA_U32 },
92 	[RDMA_NLDEV_ATTR_NDEV_NAME]		= { .type = NLA_NUL_STRING,
93 					.len = IFNAMSIZ },
94 	[RDMA_NLDEV_ATTR_NODE_GUID]		= { .type = NLA_U64 },
95 	[RDMA_NLDEV_ATTR_PORT_INDEX]		= { .type = NLA_U32 },
96 	[RDMA_NLDEV_ATTR_PORT_PHYS_STATE]	= { .type = NLA_U8 },
97 	[RDMA_NLDEV_ATTR_PORT_STATE]		= { .type = NLA_U8 },
98 	[RDMA_NLDEV_ATTR_RES_CM_ID]		= { .type = NLA_NESTED },
99 	[RDMA_NLDEV_ATTR_RES_CM_IDN]		= { .type = NLA_U32 },
100 	[RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY]	= { .type = NLA_NESTED },
101 	[RDMA_NLDEV_ATTR_RES_CQ]		= { .type = NLA_NESTED },
102 	[RDMA_NLDEV_ATTR_RES_CQE]		= { .type = NLA_U32 },
103 	[RDMA_NLDEV_ATTR_RES_CQN]		= { .type = NLA_U32 },
104 	[RDMA_NLDEV_ATTR_RES_CQ_ENTRY]		= { .type = NLA_NESTED },
105 	[RDMA_NLDEV_ATTR_RES_CTX]		= { .type = NLA_NESTED },
106 	[RDMA_NLDEV_ATTR_RES_CTXN]		= { .type = NLA_U32 },
107 	[RDMA_NLDEV_ATTR_RES_CTX_ENTRY]		= { .type = NLA_NESTED },
108 	[RDMA_NLDEV_ATTR_RES_DST_ADDR]		= {
109 			.len = sizeof(struct __kernel_sockaddr_storage) },
110 	[RDMA_NLDEV_ATTR_RES_IOVA]		= { .type = NLA_U64 },
111 	[RDMA_NLDEV_ATTR_RES_KERN_NAME]		= { .type = NLA_NUL_STRING,
112 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
113 	[RDMA_NLDEV_ATTR_RES_LKEY]		= { .type = NLA_U32 },
114 	[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]	= { .type = NLA_U32 },
115 	[RDMA_NLDEV_ATTR_RES_LQPN]		= { .type = NLA_U32 },
116 	[RDMA_NLDEV_ATTR_RES_MR]		= { .type = NLA_NESTED },
117 	[RDMA_NLDEV_ATTR_RES_MRLEN]		= { .type = NLA_U64 },
118 	[RDMA_NLDEV_ATTR_RES_MRN]		= { .type = NLA_U32 },
119 	[RDMA_NLDEV_ATTR_RES_MR_ENTRY]		= { .type = NLA_NESTED },
120 	[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]	= { .type = NLA_U8 },
121 	[RDMA_NLDEV_ATTR_RES_PD]		= { .type = NLA_NESTED },
122 	[RDMA_NLDEV_ATTR_RES_PDN]		= { .type = NLA_U32 },
123 	[RDMA_NLDEV_ATTR_RES_PD_ENTRY]		= { .type = NLA_NESTED },
124 	[RDMA_NLDEV_ATTR_RES_PID]		= { .type = NLA_U32 },
125 	[RDMA_NLDEV_ATTR_RES_POLL_CTX]		= { .type = NLA_U8 },
126 	[RDMA_NLDEV_ATTR_RES_PS]		= { .type = NLA_U32 },
127 	[RDMA_NLDEV_ATTR_RES_QP]		= { .type = NLA_NESTED },
128 	[RDMA_NLDEV_ATTR_RES_QP_ENTRY]		= { .type = NLA_NESTED },
129 	[RDMA_NLDEV_ATTR_RES_RAW]		= { .type = NLA_BINARY },
130 	[RDMA_NLDEV_ATTR_RES_RKEY]		= { .type = NLA_U32 },
131 	[RDMA_NLDEV_ATTR_RES_RQPN]		= { .type = NLA_U32 },
132 	[RDMA_NLDEV_ATTR_RES_RQ_PSN]		= { .type = NLA_U32 },
133 	[RDMA_NLDEV_ATTR_RES_SQ_PSN]		= { .type = NLA_U32 },
134 	[RDMA_NLDEV_ATTR_RES_SRC_ADDR]		= {
135 			.len = sizeof(struct __kernel_sockaddr_storage) },
136 	[RDMA_NLDEV_ATTR_RES_STATE]		= { .type = NLA_U8 },
137 	[RDMA_NLDEV_ATTR_RES_SUMMARY]		= { .type = NLA_NESTED },
138 	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY]	= { .type = NLA_NESTED },
139 	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]= { .type = NLA_U64 },
140 	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]= { .type = NLA_NUL_STRING,
141 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
142 	[RDMA_NLDEV_ATTR_RES_TYPE]		= { .type = NLA_U8 },
143 	[RDMA_NLDEV_ATTR_RES_SUBTYPE]		= { .type = NLA_NUL_STRING,
144 					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
145 	[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]= { .type = NLA_U32 },
146 	[RDMA_NLDEV_ATTR_RES_USECNT]		= { .type = NLA_U64 },
147 	[RDMA_NLDEV_ATTR_RES_SRQ]		= { .type = NLA_NESTED },
148 	[RDMA_NLDEV_ATTR_RES_SRQN]		= { .type = NLA_U32 },
149 	[RDMA_NLDEV_ATTR_RES_SRQ_ENTRY]		= { .type = NLA_NESTED },
150 	[RDMA_NLDEV_ATTR_MIN_RANGE]		= { .type = NLA_U32 },
151 	[RDMA_NLDEV_ATTR_MAX_RANGE]		= { .type = NLA_U32 },
152 	[RDMA_NLDEV_ATTR_SM_LID]		= { .type = NLA_U32 },
153 	[RDMA_NLDEV_ATTR_SUBNET_PREFIX]		= { .type = NLA_U64 },
154 	[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]	= { .type = NLA_U32 },
155 	[RDMA_NLDEV_ATTR_STAT_MODE]		= { .type = NLA_U32 },
156 	[RDMA_NLDEV_ATTR_STAT_RES]		= { .type = NLA_U32 },
157 	[RDMA_NLDEV_ATTR_STAT_COUNTER]		= { .type = NLA_NESTED },
158 	[RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY]	= { .type = NLA_NESTED },
159 	[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]       = { .type = NLA_U32 },
160 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]       = { .type = NLA_NESTED },
161 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY]  = { .type = NLA_NESTED },
162 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME] = { .type = NLA_NUL_STRING },
163 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE] = { .type = NLA_U64 },
164 	[RDMA_NLDEV_ATTR_SYS_IMAGE_GUID]	= { .type = NLA_U64 },
165 	[RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID]	= { .type = NLA_U32 },
166 	[RDMA_NLDEV_NET_NS_FD]			= { .type = NLA_U32 },
167 	[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]	= { .type = NLA_U8 },
168 	[RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK]	= { .type = NLA_U8 },
169 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_INDEX]	= { .type = NLA_U32 },
170 	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_DYNAMIC] = { .type = NLA_U8 },
171 	[RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE] = { .type = NLA_U8 },
172 	[RDMA_NLDEV_ATTR_DRIVER_DETAILS]	= { .type = NLA_U8 },
173 	[RDMA_NLDEV_ATTR_DEV_TYPE]		= { .type = NLA_U8 },
174 	[RDMA_NLDEV_ATTR_PARENT_NAME]		= { .type = NLA_NUL_STRING },
175 	[RDMA_NLDEV_ATTR_NAME_ASSIGN_TYPE]	= { .type = NLA_U8 },
176 	[RDMA_NLDEV_ATTR_EVENT_TYPE]		= { .type = NLA_U8 },
177 	[RDMA_NLDEV_ATTR_STAT_OPCOUNTER_ENABLED] = { .type = NLA_U8 },
178 	[RDMA_NLDEV_ATTR_FRMR_POOLS]		= { .type = NLA_NESTED },
179 	[RDMA_NLDEV_ATTR_FRMR_POOL_ENTRY]	= { .type = NLA_NESTED },
180 	[RDMA_NLDEV_ATTR_FRMR_POOL_KEY]		= { .type = NLA_NESTED },
181 	[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ATS]	= { .type = NLA_U8 },
182 	[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ACCESS_FLAGS] = { .type = NLA_U32 },
183 	[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_VENDOR_KEY] = { .type = NLA_U64 },
184 	[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_NUM_DMA_BLOCKS] = { .type = NLA_U64 },
185 	[RDMA_NLDEV_ATTR_FRMR_POOL_QUEUE_HANDLES] = { .type = NLA_U32 },
186 	[RDMA_NLDEV_ATTR_FRMR_POOL_MAX_IN_USE]	= { .type = NLA_U64 },
187 	[RDMA_NLDEV_ATTR_FRMR_POOL_IN_USE]	= { .type = NLA_U64 },
188 	[RDMA_NLDEV_ATTR_FRMR_POOLS_AGING_PERIOD] = { .type = NLA_U32 },
189 	[RDMA_NLDEV_ATTR_FRMR_POOL_PINNED_HANDLES] = { .type = NLA_U32 },
190 	[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_KERNEL_VENDOR_KEY] = { .type = NLA_U64 },
191 	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_MAX]	= { .type = NLA_U64 },
192 };
193 
194 static int put_driver_name_print_type(struct sk_buff *msg, const char *name,
195 				      enum rdma_nldev_print_type print_type)
196 {
197 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name))
198 		return -EMSGSIZE;
199 	if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC &&
200 	    nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type))
201 		return -EMSGSIZE;
202 
203 	return 0;
204 }
205 
206 static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name,
207 				   enum rdma_nldev_print_type print_type,
208 				   u32 value)
209 {
210 	if (put_driver_name_print_type(msg, name, print_type))
211 		return -EMSGSIZE;
212 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value))
213 		return -EMSGSIZE;
214 
215 	return 0;
216 }
217 
218 static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name,
219 				   enum rdma_nldev_print_type print_type,
220 				   u64 value)
221 {
222 	if (put_driver_name_print_type(msg, name, print_type))
223 		return -EMSGSIZE;
224 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value,
225 			      RDMA_NLDEV_ATTR_PAD))
226 		return -EMSGSIZE;
227 
228 	return 0;
229 }
230 
231 int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
232 			      const char *str)
233 {
234 	if (put_driver_name_print_type(msg, name,
235 				       RDMA_NLDEV_PRINT_TYPE_UNSPEC))
236 		return -EMSGSIZE;
237 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, str))
238 		return -EMSGSIZE;
239 
240 	return 0;
241 }
242 EXPORT_SYMBOL(rdma_nl_put_driver_string);
243 
244 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value)
245 {
246 	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
247 				       value);
248 }
249 EXPORT_SYMBOL(rdma_nl_put_driver_u32);
250 
251 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
252 			       u32 value)
253 {
254 	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
255 				       value);
256 }
257 EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex);
258 
259 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value)
260 {
261 	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
262 				       value);
263 }
264 EXPORT_SYMBOL(rdma_nl_put_driver_u64);
265 
266 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value)
267 {
268 	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
269 				       value);
270 }
271 EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex);
272 
273 bool rdma_nl_get_privileged_qkey(void)
274 {
275 	return privileged_qkey;
276 }
277 EXPORT_SYMBOL(rdma_nl_get_privileged_qkey);
278 
279 static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
280 {
281 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
282 		return -EMSGSIZE;
283 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME,
284 			   dev_name(&device->dev)))
285 		return -EMSGSIZE;
286 
287 	return 0;
288 }
289 
290 static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
291 {
292 	char fw[IB_FW_VERSION_NAME_MAX];
293 	int ret = 0;
294 	u32 port;
295 
296 	if (fill_nldev_handle(msg, device))
297 		return -EMSGSIZE;
298 
299 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
300 		return -EMSGSIZE;
301 
302 	BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
303 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
304 			      device->attrs.device_cap_flags,
305 			      RDMA_NLDEV_ATTR_PAD))
306 		return -EMSGSIZE;
307 
308 	ib_get_device_fw_str(device, fw);
309 	/* Device without FW has strlen(fw) = 0 */
310 	if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
311 		return -EMSGSIZE;
312 
313 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
314 			      be64_to_cpu(device->node_guid),
315 			      RDMA_NLDEV_ATTR_PAD))
316 		return -EMSGSIZE;
317 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
318 			      be64_to_cpu(device->attrs.sys_image_guid),
319 			      RDMA_NLDEV_ATTR_PAD))
320 		return -EMSGSIZE;
321 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
322 		return -EMSGSIZE;
323 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, device->use_cq_dim))
324 		return -EMSGSIZE;
325 
326 	if (device->type &&
327 	    nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_TYPE, device->type))
328 		return -EMSGSIZE;
329 
330 	if (device->parent &&
331 	    nla_put_string(msg, RDMA_NLDEV_ATTR_PARENT_NAME,
332 			   dev_name(&device->parent->dev)))
333 		return -EMSGSIZE;
334 
335 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_NAME_ASSIGN_TYPE,
336 		       device->name_assign_type))
337 		return -EMSGSIZE;
338 
339 	/*
340 	 * Link type is determined on first port and mlx4 device
341 	 * which can potentially have two different link type for the same
342 	 * IB device is considered as better to be avoided in the future,
343 	 */
344 	port = rdma_start_port(device);
345 	if (rdma_cap_opa_mad(device, port))
346 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "opa");
347 	else if (rdma_protocol_ib(device, port))
348 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "ib");
349 	else if (rdma_protocol_iwarp(device, port))
350 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "iw");
351 	else if (rdma_protocol_roce(device, port))
352 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "roce");
353 	else if (rdma_protocol_usnic(device, port))
354 		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL,
355 				     "usnic");
356 	return ret;
357 }
358 
359 static int fill_port_info(struct sk_buff *msg,
360 			  struct ib_device *device, u32 port,
361 			  const struct net *net)
362 {
363 	struct net_device *netdev = NULL;
364 	struct ib_port_attr attr;
365 	int ret;
366 	u64 cap_flags = 0;
367 
368 	if (fill_nldev_handle(msg, device))
369 		return -EMSGSIZE;
370 
371 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
372 		return -EMSGSIZE;
373 
374 	ret = ib_query_port(device, port, &attr);
375 	if (ret)
376 		return ret;
377 
378 	if (rdma_protocol_ib(device, port)) {
379 		BUILD_BUG_ON((sizeof(attr.port_cap_flags) +
380 				sizeof(attr.port_cap_flags2)) > sizeof(u64));
381 		cap_flags = attr.port_cap_flags |
382 			((u64)attr.port_cap_flags2 << 32);
383 		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
384 				      cap_flags, RDMA_NLDEV_ATTR_PAD))
385 			return -EMSGSIZE;
386 		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
387 				      attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD))
388 			return -EMSGSIZE;
389 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
390 			return -EMSGSIZE;
391 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
392 			return -EMSGSIZE;
393 		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
394 			return -EMSGSIZE;
395 	}
396 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
397 		return -EMSGSIZE;
398 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
399 		return -EMSGSIZE;
400 
401 	netdev = ib_device_get_netdev(device, port);
402 	if (netdev && net_eq(dev_net(netdev), net)) {
403 		ret = nla_put_u32(msg,
404 				  RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
405 		if (ret)
406 			goto out;
407 		ret = nla_put_string(msg,
408 				     RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
409 	}
410 
411 out:
412 	dev_put(netdev);
413 	return ret;
414 }
415 
416 static int fill_res_info_entry(struct sk_buff *msg,
417 			       const char *name, u64 curr, u64 max)
418 {
419 	struct nlattr *entry_attr;
420 
421 	entry_attr = nla_nest_start_noflag(msg,
422 					   RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
423 	if (!entry_attr)
424 		return -EMSGSIZE;
425 
426 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
427 		goto err;
428 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr,
429 			      RDMA_NLDEV_ATTR_PAD))
430 		goto err;
431 	if (max && nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_MAX,
432 				     max, RDMA_NLDEV_ATTR_PAD))
433 		goto err;
434 
435 	nla_nest_end(msg, entry_attr);
436 	return 0;
437 
438 err:
439 	nla_nest_cancel(msg, entry_attr);
440 	return -EMSGSIZE;
441 }
442 
443 static int fill_res_info(struct sk_buff *msg, struct ib_device *device,
444 			 bool show_details)
445 {
446 	static const char * const names[RDMA_RESTRACK_MAX] = {
447 		[RDMA_RESTRACK_PD] = "pd",
448 		[RDMA_RESTRACK_CQ] = "cq",
449 		[RDMA_RESTRACK_QP] = "qp",
450 		[RDMA_RESTRACK_CM_ID] = "cm_id",
451 		[RDMA_RESTRACK_MR] = "mr",
452 		[RDMA_RESTRACK_CTX] = "ctx",
453 		[RDMA_RESTRACK_SRQ] = "srq",
454 	};
455 
456 	struct nlattr *table_attr;
457 	u64 curr, max;
458 	int ret, i;
459 
460 	if (fill_nldev_handle(msg, device))
461 		return -EMSGSIZE;
462 
463 	table_attr = nla_nest_start_noflag(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
464 	if (!table_attr)
465 		return -EMSGSIZE;
466 
467 	for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
468 		if (!names[i])
469 			continue;
470 		curr = rdma_restrack_count(device, i, show_details);
471 		switch (i) {
472 		case RDMA_RESTRACK_QP:
473 			max = device->attrs.max_qp;
474 			break;
475 		case RDMA_RESTRACK_CQ:
476 			max = device->attrs.max_cq;
477 			break;
478 		case RDMA_RESTRACK_MR:
479 			max = device->attrs.max_mr;
480 			break;
481 		case RDMA_RESTRACK_PD:
482 			max = device->attrs.max_pd;
483 			break;
484 		case RDMA_RESTRACK_SRQ:
485 			max = device->attrs.max_srq;
486 			break;
487 		default:
488 			max = 0;
489 		}
490 		ret = fill_res_info_entry(msg, names[i], curr, max);
491 		if (ret)
492 			goto err;
493 	}
494 
495 	nla_nest_end(msg, table_attr);
496 	return 0;
497 
498 err:
499 	nla_nest_cancel(msg, table_attr);
500 	return ret;
501 }
502 
503 static int fill_res_name_pid(struct sk_buff *msg,
504 			     struct rdma_restrack_entry *res)
505 {
506 	int err = 0;
507 
508 	/*
509 	 * For user resources, user is should read /proc/PID/comm to get the
510 	 * name of the task file.
511 	 */
512 	if (rdma_is_kernel_res(res)) {
513 		err = nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
514 				     res->kern_name);
515 	} else {
516 		pid_t pid;
517 
518 		pid = task_pid_vnr(res->task);
519 		/*
520 		 * Task is dead and in zombie state.
521 		 * There is no need to print PID anymore.
522 		 */
523 		if (pid)
524 			/*
525 			 * This part is racy, task can be killed and PID will
526 			 * be zero right here but it is ok, next query won't
527 			 * return PID. We don't promise real-time reflection
528 			 * of SW objects.
529 			 */
530 			err = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID, pid);
531 	}
532 
533 	return err ? -EMSGSIZE : 0;
534 }
535 
536 static int fill_res_qp_entry_query(struct sk_buff *msg,
537 				   struct rdma_restrack_entry *res,
538 				   struct ib_device *dev,
539 				   struct ib_qp *qp)
540 {
541 	struct ib_qp_init_attr qp_init_attr;
542 	struct ib_qp_attr qp_attr;
543 	int ret;
544 
545 	ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
546 	if (ret)
547 		return ret;
548 
549 	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
550 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
551 				qp_attr.dest_qp_num))
552 			goto err;
553 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
554 				qp_attr.rq_psn))
555 			goto err;
556 	}
557 
558 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
559 		goto err;
560 
561 	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
562 	    qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
563 		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
564 			       qp_attr.path_mig_state))
565 			goto err;
566 	}
567 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
568 		goto err;
569 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
570 		goto err;
571 
572 	if (dev->ops.fill_res_qp_entry)
573 		return dev->ops.fill_res_qp_entry(msg, qp);
574 	return 0;
575 
576 err:	return -EMSGSIZE;
577 }
578 
579 static int fill_res_qp_entry(struct sk_buff *msg, bool has_cap_net_admin,
580 			     struct rdma_restrack_entry *res, uint32_t port)
581 {
582 	struct ib_qp *qp = container_of(res, struct ib_qp, res);
583 	struct ib_device *dev = qp->device;
584 	int ret;
585 
586 	if (port && port != qp->port)
587 		return -EAGAIN;
588 
589 	/* In create_qp() port is not set yet */
590 	if (qp->port && nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp->port))
591 		return -EMSGSIZE;
592 
593 	ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num);
594 	if (ret)
595 		return -EMSGSIZE;
596 
597 	if (!rdma_is_kernel_res(res) &&
598 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, qp->pd->res.id))
599 		return -EMSGSIZE;
600 
601 	ret = fill_res_name_pid(msg, res);
602 	if (ret)
603 		return -EMSGSIZE;
604 
605 	return fill_res_qp_entry_query(msg, res, dev, qp);
606 }
607 
608 static int fill_res_qp_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
609 				 struct rdma_restrack_entry *res, uint32_t port)
610 {
611 	struct ib_qp *qp = container_of(res, struct ib_qp, res);
612 	struct ib_device *dev = qp->device;
613 
614 	if (port && port != qp->port)
615 		return -EAGAIN;
616 	if (!dev->ops.fill_res_qp_entry_raw)
617 		return -EINVAL;
618 	return dev->ops.fill_res_qp_entry_raw(msg, qp);
619 }
620 
621 static int fill_res_cm_id_entry(struct sk_buff *msg, bool has_cap_net_admin,
622 				struct rdma_restrack_entry *res, uint32_t port)
623 {
624 	struct rdma_id_private *id_priv =
625 				container_of(res, struct rdma_id_private, res);
626 	struct ib_device *dev = id_priv->id.device;
627 	struct rdma_cm_id *cm_id = &id_priv->id;
628 
629 	if (port && port != cm_id->port_num)
630 		return -EAGAIN;
631 
632 	if (cm_id->port_num &&
633 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
634 		goto err;
635 
636 	if (id_priv->qp_num) {
637 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
638 			goto err;
639 		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
640 			goto err;
641 	}
642 
643 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
644 		goto err;
645 
646 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
647 		goto err;
648 
649 	if (cm_id->route.addr.src_addr.ss_family &&
650 	    nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
651 		    sizeof(cm_id->route.addr.src_addr),
652 		    &cm_id->route.addr.src_addr))
653 		goto err;
654 	if (cm_id->route.addr.dst_addr.ss_family &&
655 	    nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
656 		    sizeof(cm_id->route.addr.dst_addr),
657 		    &cm_id->route.addr.dst_addr))
658 		goto err;
659 
660 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CM_IDN, res->id))
661 		goto err;
662 
663 	if (fill_res_name_pid(msg, res))
664 		goto err;
665 
666 	if (dev->ops.fill_res_cm_id_entry)
667 		return dev->ops.fill_res_cm_id_entry(msg, cm_id);
668 	return 0;
669 
670 err: return -EMSGSIZE;
671 }
672 
673 static int fill_res_cq_entry(struct sk_buff *msg, bool has_cap_net_admin,
674 			     struct rdma_restrack_entry *res, uint32_t port)
675 {
676 	struct ib_cq *cq = container_of(res, struct ib_cq, res);
677 	struct ib_device *dev = cq->device;
678 
679 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
680 		return -EMSGSIZE;
681 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
682 			      atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD))
683 		return -EMSGSIZE;
684 
685 	/* Poll context is only valid for kernel CQs */
686 	if (rdma_is_kernel_res(res) &&
687 	    nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
688 		return -EMSGSIZE;
689 
690 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, (cq->dim != NULL)))
691 		return -EMSGSIZE;
692 
693 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN, res->id))
694 		return -EMSGSIZE;
695 	if (!rdma_is_kernel_res(res) &&
696 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
697 			cq->uobject->uevent.uobject.context->res.id))
698 		return -EMSGSIZE;
699 
700 	if (fill_res_name_pid(msg, res))
701 		return -EMSGSIZE;
702 
703 	return (dev->ops.fill_res_cq_entry) ?
704 		dev->ops.fill_res_cq_entry(msg, cq) : 0;
705 }
706 
707 static int fill_res_cq_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
708 				 struct rdma_restrack_entry *res, uint32_t port)
709 {
710 	struct ib_cq *cq = container_of(res, struct ib_cq, res);
711 	struct ib_device *dev = cq->device;
712 
713 	if (!dev->ops.fill_res_cq_entry_raw)
714 		return -EINVAL;
715 	return dev->ops.fill_res_cq_entry_raw(msg, cq);
716 }
717 
718 static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
719 			     struct rdma_restrack_entry *res, uint32_t port)
720 {
721 	struct ib_mr *mr = container_of(res, struct ib_mr, res);
722 	struct ib_device *dev = mr->device;
723 
724 	if (has_cap_net_admin) {
725 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
726 			return -EMSGSIZE;
727 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
728 			return -EMSGSIZE;
729 	}
730 
731 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length,
732 			      RDMA_NLDEV_ATTR_PAD))
733 		return -EMSGSIZE;
734 
735 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
736 		return -EMSGSIZE;
737 
738 	if (!rdma_is_kernel_res(res)) {
739 		struct ib_pd *pd = READ_ONCE(mr->pd);
740 
741 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, pd->res.id))
742 			return -EMSGSIZE;
743 	}
744 
745 	if (fill_res_name_pid(msg, res))
746 		return -EMSGSIZE;
747 
748 	return (dev->ops.fill_res_mr_entry) ?
749 		       dev->ops.fill_res_mr_entry(msg, mr) :
750 		       0;
751 }
752 
753 static int fill_res_mr_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
754 				 struct rdma_restrack_entry *res, uint32_t port)
755 {
756 	struct ib_mr *mr = container_of(res, struct ib_mr, res);
757 	struct ib_device *dev = mr->device;
758 
759 	if (!dev->ops.fill_res_mr_entry_raw)
760 		return -EINVAL;
761 	return dev->ops.fill_res_mr_entry_raw(msg, mr);
762 }
763 
764 static int fill_res_pd_entry(struct sk_buff *msg, bool has_cap_net_admin,
765 			     struct rdma_restrack_entry *res, uint32_t port)
766 {
767 	struct ib_pd *pd = container_of(res, struct ib_pd, res);
768 
769 	if (has_cap_net_admin) {
770 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
771 				pd->local_dma_lkey))
772 			goto err;
773 		if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
774 		    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
775 				pd->unsafe_global_rkey))
776 			goto err;
777 	}
778 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
779 			      atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD))
780 		goto err;
781 
782 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, res->id))
783 		goto err;
784 
785 	if (!rdma_is_kernel_res(res) &&
786 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
787 			pd->uobject->context->res.id))
788 		goto err;
789 
790 	return fill_res_name_pid(msg, res);
791 
792 err:	return -EMSGSIZE;
793 }
794 
795 static int fill_res_ctx_entry(struct sk_buff *msg, bool has_cap_net_admin,
796 			      struct rdma_restrack_entry *res, uint32_t port)
797 {
798 	struct ib_ucontext *ctx = container_of(res, struct ib_ucontext, res);
799 
800 	if (rdma_is_kernel_res(res))
801 		return 0;
802 
803 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN, ctx->res.id))
804 		return -EMSGSIZE;
805 
806 	return fill_res_name_pid(msg, res);
807 }
808 
809 static int fill_res_range_qp_entry(struct sk_buff *msg, uint32_t min_range,
810 				   uint32_t max_range)
811 {
812 	struct nlattr *entry_attr;
813 
814 	if (!min_range)
815 		return 0;
816 
817 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
818 	if (!entry_attr)
819 		return -EMSGSIZE;
820 
821 	if (min_range == max_range) {
822 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, min_range))
823 			goto err;
824 	} else {
825 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MIN_RANGE, min_range))
826 			goto err;
827 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MAX_RANGE, max_range))
828 			goto err;
829 	}
830 	nla_nest_end(msg, entry_attr);
831 	return 0;
832 
833 err:
834 	nla_nest_cancel(msg, entry_attr);
835 	return -EMSGSIZE;
836 }
837 
838 static int fill_res_srq_qps(struct sk_buff *msg, struct ib_srq *srq)
839 {
840 	uint32_t min_range = 0, prev = 0;
841 	struct rdma_restrack_entry *res;
842 	struct rdma_restrack_root *rt;
843 	struct nlattr *table_attr;
844 	struct ib_qp *qp = NULL;
845 	unsigned long id = 0;
846 
847 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
848 	if (!table_attr)
849 		return -EMSGSIZE;
850 
851 	rt = &srq->device->res[RDMA_RESTRACK_QP];
852 	xa_lock(&rt->xa);
853 	xa_for_each(&rt->xa, id, res) {
854 		if (!rdma_restrack_get(res))
855 			continue;
856 
857 		qp = container_of(res, struct ib_qp, res);
858 		if (!qp->srq || (qp->srq->res.id != srq->res.id)) {
859 			rdma_restrack_put(res);
860 			continue;
861 		}
862 
863 		if (qp->qp_num < prev)
864 			/* qp_num should be ascending */
865 			goto err_loop;
866 
867 		if (min_range == 0) {
868 			min_range = qp->qp_num;
869 		} else if (qp->qp_num > (prev + 1)) {
870 			if (fill_res_range_qp_entry(msg, min_range, prev))
871 				goto err_loop;
872 
873 			min_range = qp->qp_num;
874 		}
875 		prev = qp->qp_num;
876 		rdma_restrack_put(res);
877 	}
878 
879 	xa_unlock(&rt->xa);
880 
881 	if (fill_res_range_qp_entry(msg, min_range, prev))
882 		goto err;
883 
884 	nla_nest_end(msg, table_attr);
885 	return 0;
886 
887 err_loop:
888 	rdma_restrack_put(res);
889 	xa_unlock(&rt->xa);
890 err:
891 	nla_nest_cancel(msg, table_attr);
892 	return -EMSGSIZE;
893 }
894 
895 static int fill_res_srq_entry(struct sk_buff *msg, bool has_cap_net_admin,
896 			      struct rdma_restrack_entry *res, uint32_t port)
897 {
898 	struct ib_srq *srq = container_of(res, struct ib_srq, res);
899 	struct ib_device *dev = srq->device;
900 
901 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SRQN, srq->res.id))
902 		goto err;
903 
904 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, srq->srq_type))
905 		goto err;
906 
907 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, srq->pd->res.id))
908 		goto err;
909 
910 	if (ib_srq_has_cq(srq->srq_type)) {
911 		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN,
912 				srq->ext.cq->res.id))
913 			goto err;
914 	}
915 
916 	if (fill_res_srq_qps(msg, srq))
917 		goto err;
918 
919 	if (fill_res_name_pid(msg, res))
920 		goto err;
921 
922 	if (dev->ops.fill_res_srq_entry)
923 		return dev->ops.fill_res_srq_entry(msg, srq);
924 
925 	return 0;
926 
927 err:
928 	return -EMSGSIZE;
929 }
930 
931 static int fill_res_srq_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
932 				 struct rdma_restrack_entry *res, uint32_t port)
933 {
934 	struct ib_srq *srq = container_of(res, struct ib_srq, res);
935 	struct ib_device *dev = srq->device;
936 
937 	if (!dev->ops.fill_res_srq_entry_raw)
938 		return -EINVAL;
939 	return dev->ops.fill_res_srq_entry_raw(msg, srq);
940 }
941 
942 static int fill_stat_counter_mode(struct sk_buff *msg,
943 				  struct rdma_counter *counter)
944 {
945 	struct rdma_counter_mode *m = &counter->mode;
946 
947 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, m->mode))
948 		return -EMSGSIZE;
949 
950 	if (m->mode == RDMA_COUNTER_MODE_AUTO) {
951 		if ((m->mask & RDMA_COUNTER_MASK_QP_TYPE) &&
952 		    nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, m->param.qp_type))
953 			return -EMSGSIZE;
954 
955 		if ((m->mask & RDMA_COUNTER_MASK_PID) &&
956 		    fill_res_name_pid(msg, &counter->res))
957 			return -EMSGSIZE;
958 	}
959 
960 	return 0;
961 }
962 
963 static int fill_stat_counter_qp_entry(struct sk_buff *msg, u32 qpn)
964 {
965 	struct nlattr *entry_attr;
966 
967 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
968 	if (!entry_attr)
969 		return -EMSGSIZE;
970 
971 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn))
972 		goto err;
973 
974 	nla_nest_end(msg, entry_attr);
975 	return 0;
976 
977 err:
978 	nla_nest_cancel(msg, entry_attr);
979 	return -EMSGSIZE;
980 }
981 
982 static int fill_stat_counter_qps(struct sk_buff *msg,
983 				 struct rdma_counter *counter)
984 {
985 	struct rdma_restrack_entry *res;
986 	struct rdma_restrack_root *rt;
987 	struct nlattr *table_attr;
988 	struct ib_qp *qp = NULL;
989 	unsigned long id = 0;
990 	int ret = 0;
991 
992 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
993 	if (!table_attr)
994 		return -EMSGSIZE;
995 
996 	rt = &counter->device->res[RDMA_RESTRACK_QP];
997 	xa_lock(&rt->xa);
998 	xa_for_each(&rt->xa, id, res) {
999 		qp = container_of(res, struct ib_qp, res);
1000 		if (!qp->counter || (qp->counter->id != counter->id))
1001 			continue;
1002 
1003 		ret = fill_stat_counter_qp_entry(msg, qp->qp_num);
1004 		if (ret)
1005 			goto err;
1006 	}
1007 
1008 	xa_unlock(&rt->xa);
1009 	nla_nest_end(msg, table_attr);
1010 	return 0;
1011 
1012 err:
1013 	xa_unlock(&rt->xa);
1014 	nla_nest_cancel(msg, table_attr);
1015 	return ret;
1016 }
1017 
1018 int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
1019 				 u64 value)
1020 {
1021 	struct nlattr *entry_attr;
1022 
1023 	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY);
1024 	if (!entry_attr)
1025 		return -EMSGSIZE;
1026 
1027 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME,
1028 			   name))
1029 		goto err;
1030 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE,
1031 			      value, RDMA_NLDEV_ATTR_PAD))
1032 		goto err;
1033 
1034 	nla_nest_end(msg, entry_attr);
1035 	return 0;
1036 
1037 err:
1038 	nla_nest_cancel(msg, entry_attr);
1039 	return -EMSGSIZE;
1040 }
1041 EXPORT_SYMBOL(rdma_nl_stat_hwcounter_entry);
1042 
1043 static int fill_stat_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
1044 			      struct rdma_restrack_entry *res, uint32_t port)
1045 {
1046 	struct ib_mr *mr = container_of(res, struct ib_mr, res);
1047 	struct ib_device *dev = mr->device;
1048 
1049 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
1050 		goto err;
1051 
1052 	if (dev->ops.fill_stat_mr_entry)
1053 		return dev->ops.fill_stat_mr_entry(msg, mr);
1054 	return 0;
1055 
1056 err:
1057 	return -EMSGSIZE;
1058 }
1059 
1060 static int fill_stat_counter_hwcounters(struct sk_buff *msg,
1061 					struct rdma_counter *counter)
1062 {
1063 	struct rdma_hw_stats *st = counter->stats;
1064 	struct nlattr *table_attr;
1065 	int i;
1066 
1067 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
1068 	if (!table_attr)
1069 		return -EMSGSIZE;
1070 
1071 	mutex_lock(&st->lock);
1072 	for (i = 0; i < st->num_counters; i++) {
1073 		if (test_bit(i, st->is_disabled))
1074 			continue;
1075 		if (rdma_nl_stat_hwcounter_entry(msg, st->descs[i].name,
1076 						 st->value[i]))
1077 			goto err;
1078 	}
1079 	mutex_unlock(&st->lock);
1080 
1081 	nla_nest_end(msg, table_attr);
1082 	return 0;
1083 
1084 err:
1085 	mutex_unlock(&st->lock);
1086 	nla_nest_cancel(msg, table_attr);
1087 	return -EMSGSIZE;
1088 }
1089 
1090 static int fill_res_counter_entry(struct sk_buff *msg, bool has_cap_net_admin,
1091 				  struct rdma_restrack_entry *res,
1092 				  uint32_t port)
1093 {
1094 	struct rdma_counter *counter =
1095 		container_of(res, struct rdma_counter, res);
1096 
1097 	if (port && port != counter->port)
1098 		return -EAGAIN;
1099 
1100 	/* Dump it even query failed */
1101 	rdma_counter_query_stats(counter);
1102 
1103 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, counter->port) ||
1104 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, counter->id) ||
1105 	    fill_stat_counter_mode(msg, counter) ||
1106 	    fill_stat_counter_qps(msg, counter) ||
1107 	    fill_stat_counter_hwcounters(msg, counter))
1108 		return -EMSGSIZE;
1109 
1110 	return 0;
1111 }
1112 
1113 static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1114 			  struct netlink_ext_ack *extack)
1115 {
1116 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1117 	struct ib_device *device;
1118 	struct sk_buff *msg;
1119 	u32 index;
1120 	int err;
1121 
1122 	err = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1123 			    nldev_policy, NL_VALIDATE_LIBERAL, extack);
1124 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1125 		return -EINVAL;
1126 
1127 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1128 
1129 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1130 	if (!device)
1131 		return -EINVAL;
1132 
1133 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1134 	if (!msg) {
1135 		err = -ENOMEM;
1136 		goto err;
1137 	}
1138 
1139 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1140 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1141 			0, 0);
1142 	if (!nlh) {
1143 		err = -EMSGSIZE;
1144 		goto err_free;
1145 	}
1146 
1147 	err = fill_dev_info(msg, device);
1148 	if (err)
1149 		goto err_free;
1150 
1151 	nlmsg_end(msg, nlh);
1152 
1153 	ib_device_put(device);
1154 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1155 
1156 err_free:
1157 	nlmsg_free(msg);
1158 err:
1159 	ib_device_put(device);
1160 	return err;
1161 }
1162 
1163 static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1164 			  struct netlink_ext_ack *extack)
1165 {
1166 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1167 	struct ib_device *device;
1168 	u32 index;
1169 	int err;
1170 
1171 	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1172 			    nldev_policy, extack);
1173 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1174 		return -EINVAL;
1175 
1176 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1177 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1178 	if (!device)
1179 		return -EINVAL;
1180 
1181 	if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) {
1182 		char name[IB_DEVICE_NAME_MAX] = {};
1183 
1184 		nla_strscpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1185 			    IB_DEVICE_NAME_MAX);
1186 		if (strlen(name) == 0) {
1187 			err = -EINVAL;
1188 			goto done;
1189 		}
1190 		err = ib_device_rename(device, name);
1191 		goto done;
1192 	}
1193 
1194 	if (tb[RDMA_NLDEV_NET_NS_FD]) {
1195 		u32 ns_fd;
1196 
1197 		ns_fd = nla_get_u32(tb[RDMA_NLDEV_NET_NS_FD]);
1198 		err = ib_device_set_netns_put(skb, device, ns_fd);
1199 		goto put_done;
1200 	}
1201 
1202 	if (tb[RDMA_NLDEV_ATTR_DEV_DIM]) {
1203 		u8 use_dim;
1204 
1205 		use_dim = nla_get_u8(tb[RDMA_NLDEV_ATTR_DEV_DIM]);
1206 		err = ib_device_set_dim(device,  use_dim);
1207 		goto done;
1208 	}
1209 
1210 done:
1211 	ib_device_put(device);
1212 put_done:
1213 	return err;
1214 }
1215 
1216 static int _nldev_get_dumpit(struct ib_device *device,
1217 			     struct sk_buff *skb,
1218 			     struct netlink_callback *cb,
1219 			     unsigned int idx)
1220 {
1221 	int start = cb->args[0];
1222 	struct nlmsghdr *nlh;
1223 
1224 	if (idx < start)
1225 		return 0;
1226 
1227 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1228 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1229 			0, NLM_F_MULTI);
1230 
1231 	if (!nlh || fill_dev_info(skb, device)) {
1232 		nlmsg_cancel(skb, nlh);
1233 		goto out;
1234 	}
1235 
1236 	nlmsg_end(skb, nlh);
1237 
1238 	idx++;
1239 
1240 out:	cb->args[0] = idx;
1241 	return skb->len;
1242 }
1243 
1244 static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
1245 {
1246 	/*
1247 	 * There is no need to take lock, because
1248 	 * we are relying on ib_core's locking.
1249 	 */
1250 	return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
1251 }
1252 
1253 static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1254 			       struct netlink_ext_ack *extack)
1255 {
1256 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1257 	struct ib_device *device;
1258 	struct sk_buff *msg;
1259 	u32 index;
1260 	u32 port;
1261 	int err;
1262 
1263 	err = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1264 			    nldev_policy, NL_VALIDATE_LIBERAL, extack);
1265 	if (err ||
1266 	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
1267 	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
1268 		return -EINVAL;
1269 
1270 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1271 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1272 	if (!device)
1273 		return -EINVAL;
1274 
1275 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1276 	if (!rdma_is_port_valid(device, port)) {
1277 		err = -EINVAL;
1278 		goto err;
1279 	}
1280 
1281 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1282 	if (!msg) {
1283 		err = -ENOMEM;
1284 		goto err;
1285 	}
1286 
1287 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1288 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1289 			0, 0);
1290 	if (!nlh) {
1291 		err = -EMSGSIZE;
1292 		goto err_free;
1293 	}
1294 
1295 	err = fill_port_info(msg, device, port, sock_net(skb->sk));
1296 	if (err)
1297 		goto err_free;
1298 
1299 	nlmsg_end(msg, nlh);
1300 	ib_device_put(device);
1301 
1302 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1303 
1304 err_free:
1305 	nlmsg_free(msg);
1306 err:
1307 	ib_device_put(device);
1308 	return err;
1309 }
1310 
1311 static int nldev_port_get_dumpit(struct sk_buff *skb,
1312 				 struct netlink_callback *cb)
1313 {
1314 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1315 	struct ib_device *device;
1316 	int start = cb->args[0];
1317 	struct nlmsghdr *nlh;
1318 	u32 idx = 0;
1319 	u32 ifindex;
1320 	int err;
1321 	unsigned int p;
1322 
1323 	err = __nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1324 			    nldev_policy, NL_VALIDATE_LIBERAL, NULL);
1325 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1326 		return -EINVAL;
1327 
1328 	ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1329 	device = ib_device_get_by_index(sock_net(skb->sk), ifindex);
1330 	if (!device)
1331 		return -EINVAL;
1332 
1333 	rdma_for_each_port (device, p) {
1334 		/*
1335 		 * The dumpit function returns all information from specific
1336 		 * index. This specific index is taken from the netlink
1337 		 * messages request sent by user and it is available
1338 		 * in cb->args[0].
1339 		 *
1340 		 * Usually, the user doesn't fill this field and it causes
1341 		 * to return everything.
1342 		 *
1343 		 */
1344 		if (idx < start) {
1345 			idx++;
1346 			continue;
1347 		}
1348 
1349 		nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
1350 				cb->nlh->nlmsg_seq,
1351 				RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1352 						 RDMA_NLDEV_CMD_PORT_GET),
1353 				0, NLM_F_MULTI);
1354 
1355 		if (!nlh || fill_port_info(skb, device, p, sock_net(skb->sk))) {
1356 			nlmsg_cancel(skb, nlh);
1357 			goto out;
1358 		}
1359 		idx++;
1360 		nlmsg_end(skb, nlh);
1361 	}
1362 
1363 out:
1364 	ib_device_put(device);
1365 	cb->args[0] = idx;
1366 	return skb->len;
1367 }
1368 
1369 static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1370 			      struct netlink_ext_ack *extack)
1371 {
1372 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1373 	bool show_details = false;
1374 	struct ib_device *device;
1375 	struct sk_buff *msg;
1376 	u32 index;
1377 	int ret;
1378 
1379 	ret = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1380 			    nldev_policy, NL_VALIDATE_LIBERAL, extack);
1381 	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1382 		return -EINVAL;
1383 
1384 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1385 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1386 	if (!device)
1387 		return -EINVAL;
1388 
1389 	if (tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS])
1390 		show_details = nla_get_u8(tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS]);
1391 
1392 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1393 	if (!msg) {
1394 		ret = -ENOMEM;
1395 		goto err;
1396 	}
1397 
1398 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1399 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1400 			0, 0);
1401 	if (!nlh) {
1402 		ret = -EMSGSIZE;
1403 		goto err_free;
1404 	}
1405 
1406 	ret = fill_res_info(msg, device, show_details);
1407 	if (ret)
1408 		goto err_free;
1409 
1410 	nlmsg_end(msg, nlh);
1411 	ib_device_put(device);
1412 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1413 
1414 err_free:
1415 	nlmsg_free(msg);
1416 err:
1417 	ib_device_put(device);
1418 	return ret;
1419 }
1420 
1421 static int _nldev_res_get_dumpit(struct ib_device *device,
1422 				 struct sk_buff *skb,
1423 				 struct netlink_callback *cb,
1424 				 unsigned int idx)
1425 {
1426 	int start = cb->args[0];
1427 	struct nlmsghdr *nlh;
1428 
1429 	if (idx < start)
1430 		return 0;
1431 
1432 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1433 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1434 			0, NLM_F_MULTI);
1435 
1436 	if (!nlh || fill_res_info(skb, device, false)) {
1437 		nlmsg_cancel(skb, nlh);
1438 		goto out;
1439 	}
1440 	nlmsg_end(skb, nlh);
1441 
1442 	idx++;
1443 
1444 out:
1445 	cb->args[0] = idx;
1446 	return skb->len;
1447 }
1448 
1449 static int nldev_res_get_dumpit(struct sk_buff *skb,
1450 				struct netlink_callback *cb)
1451 {
1452 	return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
1453 }
1454 
1455 struct nldev_fill_res_entry {
1456 	enum rdma_nldev_attr nldev_attr;
1457 	u8 flags;
1458 	u32 entry;
1459 	u32 id;
1460 };
1461 
1462 enum nldev_res_flags {
1463 	NLDEV_PER_DEV = 1 << 0,
1464 };
1465 
1466 static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
1467 	[RDMA_RESTRACK_QP] = {
1468 		.nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
1469 		.entry = RDMA_NLDEV_ATTR_RES_QP_ENTRY,
1470 		.id = RDMA_NLDEV_ATTR_RES_LQPN,
1471 	},
1472 	[RDMA_RESTRACK_CM_ID] = {
1473 		.nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
1474 		.entry = RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY,
1475 		.id = RDMA_NLDEV_ATTR_RES_CM_IDN,
1476 	},
1477 	[RDMA_RESTRACK_CQ] = {
1478 		.nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
1479 		.flags = NLDEV_PER_DEV,
1480 		.entry = RDMA_NLDEV_ATTR_RES_CQ_ENTRY,
1481 		.id = RDMA_NLDEV_ATTR_RES_CQN,
1482 	},
1483 	[RDMA_RESTRACK_MR] = {
1484 		.nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
1485 		.flags = NLDEV_PER_DEV,
1486 		.entry = RDMA_NLDEV_ATTR_RES_MR_ENTRY,
1487 		.id = RDMA_NLDEV_ATTR_RES_MRN,
1488 	},
1489 	[RDMA_RESTRACK_PD] = {
1490 		.nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
1491 		.flags = NLDEV_PER_DEV,
1492 		.entry = RDMA_NLDEV_ATTR_RES_PD_ENTRY,
1493 		.id = RDMA_NLDEV_ATTR_RES_PDN,
1494 	},
1495 	[RDMA_RESTRACK_COUNTER] = {
1496 		.nldev_attr = RDMA_NLDEV_ATTR_STAT_COUNTER,
1497 		.entry = RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY,
1498 		.id = RDMA_NLDEV_ATTR_STAT_COUNTER_ID,
1499 	},
1500 	[RDMA_RESTRACK_CTX] = {
1501 		.nldev_attr = RDMA_NLDEV_ATTR_RES_CTX,
1502 		.flags = NLDEV_PER_DEV,
1503 		.entry = RDMA_NLDEV_ATTR_RES_CTX_ENTRY,
1504 		.id = RDMA_NLDEV_ATTR_RES_CTXN,
1505 	},
1506 	[RDMA_RESTRACK_SRQ] = {
1507 		.nldev_attr = RDMA_NLDEV_ATTR_RES_SRQ,
1508 		.flags = NLDEV_PER_DEV,
1509 		.entry = RDMA_NLDEV_ATTR_RES_SRQ_ENTRY,
1510 		.id = RDMA_NLDEV_ATTR_RES_SRQN,
1511 	},
1512 
1513 };
1514 
1515 static noinline_for_stack int
1516 res_get_common_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1517 		    struct netlink_ext_ack *extack,
1518 		    enum rdma_restrack_type res_type,
1519 		    res_fill_func_t fill_func)
1520 {
1521 	const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1522 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1523 	struct rdma_restrack_entry *res;
1524 	struct ib_device *device;
1525 	u32 index, id, port = 0;
1526 	bool has_cap_net_admin;
1527 	struct sk_buff *msg;
1528 	int ret;
1529 
1530 	ret = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1531 			    nldev_policy, NL_VALIDATE_LIBERAL, extack);
1532 	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !fe->id || !tb[fe->id])
1533 		return -EINVAL;
1534 
1535 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1536 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1537 	if (!device)
1538 		return -EINVAL;
1539 
1540 	if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1541 		port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1542 		if (!rdma_is_port_valid(device, port)) {
1543 			ret = -EINVAL;
1544 			goto err;
1545 		}
1546 	}
1547 
1548 	if ((port && fe->flags & NLDEV_PER_DEV) ||
1549 	    (!port && ~fe->flags & NLDEV_PER_DEV)) {
1550 		ret = -EINVAL;
1551 		goto err;
1552 	}
1553 
1554 	id = nla_get_u32(tb[fe->id]);
1555 	res = rdma_restrack_get_byid(device, res_type, id);
1556 	if (IS_ERR(res)) {
1557 		ret = PTR_ERR(res);
1558 		goto err;
1559 	}
1560 
1561 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1562 	if (!msg) {
1563 		ret = -ENOMEM;
1564 		goto err_get;
1565 	}
1566 
1567 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1568 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1569 					 RDMA_NL_GET_OP(nlh->nlmsg_type)),
1570 			0, 0);
1571 
1572 	if (!nlh || fill_nldev_handle(msg, device)) {
1573 		ret = -EMSGSIZE;
1574 		goto err_free;
1575 	}
1576 
1577 	has_cap_net_admin = netlink_capable(skb, CAP_NET_ADMIN);
1578 
1579 	ret = fill_func(msg, has_cap_net_admin, res, port);
1580 	if (ret)
1581 		goto err_free;
1582 
1583 	rdma_restrack_put(res);
1584 	nlmsg_end(msg, nlh);
1585 	ib_device_put(device);
1586 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1587 
1588 err_free:
1589 	nlmsg_free(msg);
1590 err_get:
1591 	rdma_restrack_put(res);
1592 err:
1593 	ib_device_put(device);
1594 	return ret;
1595 }
1596 
1597 static int res_get_common_dumpit(struct sk_buff *skb,
1598 				 struct netlink_callback *cb,
1599 				 enum rdma_restrack_type res_type,
1600 				 res_fill_func_t fill_func)
1601 {
1602 	const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1603 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1604 	struct rdma_restrack_entry *res;
1605 	struct rdma_restrack_root *rt;
1606 	int err, ret = 0, idx = 0;
1607 	bool show_details = false;
1608 	struct nlattr *table_attr;
1609 	struct nlattr *entry_attr;
1610 	struct ib_device *device;
1611 	int start = cb->args[0];
1612 	bool has_cap_net_admin;
1613 	struct nlmsghdr *nlh;
1614 	unsigned long id;
1615 	u32 index, port = 0;
1616 	bool filled = false;
1617 
1618 	err = __nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1619 			    nldev_policy, NL_VALIDATE_LIBERAL, NULL);
1620 	/*
1621 	 * Right now, we are expecting the device index to get res information,
1622 	 * but it is possible to extend this code to return all devices in
1623 	 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
1624 	 * if it doesn't exist, we will iterate over all devices.
1625 	 *
1626 	 * But it is not needed for now.
1627 	 */
1628 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1629 		return -EINVAL;
1630 
1631 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1632 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1633 	if (!device)
1634 		return -EINVAL;
1635 
1636 	if (tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS])
1637 		show_details = nla_get_u8(tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS]);
1638 
1639 	/*
1640 	 * If no PORT_INDEX is supplied, we will return all QPs from that device
1641 	 */
1642 	if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1643 		port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1644 		if (!rdma_is_port_valid(device, port)) {
1645 			ret = -EINVAL;
1646 			goto err_index;
1647 		}
1648 	}
1649 
1650 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1651 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1652 					 RDMA_NL_GET_OP(cb->nlh->nlmsg_type)),
1653 			0, NLM_F_MULTI);
1654 
1655 	if (!nlh || fill_nldev_handle(skb, device)) {
1656 		ret = -EMSGSIZE;
1657 		goto err;
1658 	}
1659 
1660 	table_attr = nla_nest_start_noflag(skb, fe->nldev_attr);
1661 	if (!table_attr) {
1662 		ret = -EMSGSIZE;
1663 		goto err;
1664 	}
1665 
1666 	has_cap_net_admin = netlink_capable(cb->skb, CAP_NET_ADMIN);
1667 
1668 	rt = &device->res[res_type];
1669 	xa_lock(&rt->xa);
1670 	/*
1671 	 * FIXME: if the skip ahead is something common this loop should
1672 	 * use xas_for_each & xas_pause to optimize, we can have a lot of
1673 	 * objects.
1674 	 */
1675 	xa_for_each(&rt->xa, id, res) {
1676 		if (xa_get_mark(&rt->xa, res->id, RESTRACK_DD) && !show_details)
1677 			goto next;
1678 
1679 		if (idx < start || !rdma_restrack_get(res))
1680 			goto next;
1681 
1682 		xa_unlock(&rt->xa);
1683 
1684 		filled = true;
1685 
1686 		entry_attr = nla_nest_start_noflag(skb, fe->entry);
1687 		if (!entry_attr) {
1688 			ret = -EMSGSIZE;
1689 			rdma_restrack_put(res);
1690 			goto msg_full;
1691 		}
1692 
1693 		ret = fill_func(skb, has_cap_net_admin, res, port);
1694 
1695 		rdma_restrack_put(res);
1696 
1697 		if (ret) {
1698 			nla_nest_cancel(skb, entry_attr);
1699 			if (ret == -EMSGSIZE)
1700 				goto msg_full;
1701 			if (ret == -EAGAIN)
1702 				goto again;
1703 			goto res_err;
1704 		}
1705 		nla_nest_end(skb, entry_attr);
1706 again:		xa_lock(&rt->xa);
1707 next:		idx++;
1708 	}
1709 	xa_unlock(&rt->xa);
1710 
1711 msg_full:
1712 	nla_nest_end(skb, table_attr);
1713 	nlmsg_end(skb, nlh);
1714 	cb->args[0] = idx;
1715 
1716 	/*
1717 	 * No more entries to fill, cancel the message and
1718 	 * return 0 to mark end of dumpit.
1719 	 */
1720 	if (!filled)
1721 		goto err;
1722 
1723 	ib_device_put(device);
1724 	return skb->len;
1725 
1726 res_err:
1727 	nla_nest_cancel(skb, table_attr);
1728 
1729 err:
1730 	nlmsg_cancel(skb, nlh);
1731 
1732 err_index:
1733 	ib_device_put(device);
1734 	return ret;
1735 }
1736 
1737 #define RES_GET_FUNCS(name, type)                                              \
1738 	static int nldev_res_get_##name##_dumpit(struct sk_buff *skb,          \
1739 						 struct netlink_callback *cb)  \
1740 	{                                                                      \
1741 		return res_get_common_dumpit(skb, cb, type,                    \
1742 					     fill_res_##name##_entry);         \
1743 	}                                                                      \
1744 	static int nldev_res_get_##name##_doit(struct sk_buff *skb,            \
1745 					       struct nlmsghdr *nlh,           \
1746 					       struct netlink_ext_ack *extack) \
1747 	{                                                                      \
1748 		return res_get_common_doit(skb, nlh, extack, type,             \
1749 					   fill_res_##name##_entry);           \
1750 	}
1751 
1752 RES_GET_FUNCS(qp, RDMA_RESTRACK_QP);
1753 RES_GET_FUNCS(qp_raw, RDMA_RESTRACK_QP);
1754 RES_GET_FUNCS(cm_id, RDMA_RESTRACK_CM_ID);
1755 RES_GET_FUNCS(cq, RDMA_RESTRACK_CQ);
1756 RES_GET_FUNCS(cq_raw, RDMA_RESTRACK_CQ);
1757 RES_GET_FUNCS(pd, RDMA_RESTRACK_PD);
1758 RES_GET_FUNCS(mr, RDMA_RESTRACK_MR);
1759 RES_GET_FUNCS(mr_raw, RDMA_RESTRACK_MR);
1760 RES_GET_FUNCS(counter, RDMA_RESTRACK_COUNTER);
1761 RES_GET_FUNCS(ctx, RDMA_RESTRACK_CTX);
1762 RES_GET_FUNCS(srq, RDMA_RESTRACK_SRQ);
1763 RES_GET_FUNCS(srq_raw, RDMA_RESTRACK_SRQ);
1764 
1765 static LIST_HEAD(link_ops);
1766 static DECLARE_RWSEM(link_ops_rwsem);
1767 
1768 static const struct rdma_link_ops *link_ops_get(const char *type)
1769 {
1770 	const struct rdma_link_ops *ops;
1771 
1772 	list_for_each_entry(ops, &link_ops, list) {
1773 		if (!strcmp(ops->type, type))
1774 			goto out;
1775 	}
1776 	ops = NULL;
1777 out:
1778 	return ops;
1779 }
1780 
1781 void rdma_link_register(struct rdma_link_ops *ops)
1782 {
1783 	down_write(&link_ops_rwsem);
1784 	if (WARN_ON_ONCE(link_ops_get(ops->type)))
1785 		goto out;
1786 	list_add(&ops->list, &link_ops);
1787 out:
1788 	up_write(&link_ops_rwsem);
1789 }
1790 EXPORT_SYMBOL(rdma_link_register);
1791 
1792 void rdma_link_unregister(struct rdma_link_ops *ops)
1793 {
1794 	down_write(&link_ops_rwsem);
1795 	list_del(&ops->list);
1796 	up_write(&link_ops_rwsem);
1797 }
1798 EXPORT_SYMBOL(rdma_link_unregister);
1799 
1800 static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
1801 			  struct netlink_ext_ack *extack)
1802 {
1803 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1804 	char ibdev_name[IB_DEVICE_NAME_MAX];
1805 	const struct rdma_link_ops *ops;
1806 	char ndev_name[IFNAMSIZ];
1807 	struct net_device *ndev;
1808 	char type[IFNAMSIZ];
1809 	int err;
1810 
1811 	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1812 			    nldev_policy, extack);
1813 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
1814 	    !tb[RDMA_NLDEV_ATTR_LINK_TYPE] || !tb[RDMA_NLDEV_ATTR_NDEV_NAME])
1815 		return -EINVAL;
1816 
1817 	nla_strscpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1818 		    sizeof(ibdev_name));
1819 	if (strchr(ibdev_name, '%') || strlen(ibdev_name) == 0)
1820 		return -EINVAL;
1821 
1822 	nla_strscpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type));
1823 	nla_strscpy(ndev_name, tb[RDMA_NLDEV_ATTR_NDEV_NAME],
1824 		    sizeof(ndev_name));
1825 
1826 	ndev = dev_get_by_name(sock_net(skb->sk), ndev_name);
1827 	if (!ndev)
1828 		return -ENODEV;
1829 
1830 	down_read(&link_ops_rwsem);
1831 	ops = link_ops_get(type);
1832 #ifdef CONFIG_MODULES
1833 	if (!ops) {
1834 		up_read(&link_ops_rwsem);
1835 		request_module("rdma-link-%s", type);
1836 		down_read(&link_ops_rwsem);
1837 		ops = link_ops_get(type);
1838 	}
1839 #endif
1840 	err = ops ? ops->newlink(ibdev_name, ndev) : -EINVAL;
1841 	up_read(&link_ops_rwsem);
1842 	dev_put(ndev);
1843 
1844 	return err;
1845 }
1846 
1847 static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
1848 			  struct netlink_ext_ack *extack)
1849 {
1850 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1851 	struct ib_device *device;
1852 	u32 index;
1853 	int err;
1854 
1855 	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1856 			    nldev_policy, extack);
1857 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1858 		return -EINVAL;
1859 
1860 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1861 	device = ib_device_get_by_index(sock_net(skb->sk), index);
1862 	if (!device)
1863 		return -EINVAL;
1864 
1865 	if (!(device->attrs.kernel_cap_flags & IBK_ALLOW_USER_UNREG)) {
1866 		ib_device_put(device);
1867 		return -EINVAL;
1868 	}
1869 
1870 	/*
1871 	 * This path is triggered by the 'rdma link delete' administrative command.
1872 	 * For Soft-RoCE (RXE), we ensure that transport sockets are closed here.
1873 	 * Note: iWARP driver does not implement .dellink, so this logic is
1874 	 * implicitly scoped to the driver supporting dynamic link deletion like RXE.
1875 	 */
1876 	if (device->link_ops && device->link_ops->dellink) {
1877 		mutex_lock(&nldev_dellink_mutex);
1878 		err = device->link_ops->dellink(device);
1879 		mutex_unlock(&nldev_dellink_mutex);
1880 		if (err)
1881 			return err;
1882 	}
1883 
1884 	ib_unregister_device_and_put(device);
1885 	return 0;
1886 }
1887 
1888 static int nldev_get_chardev(struct sk_buff *skb, struct nlmsghdr *nlh,
1889 			     struct netlink_ext_ack *extack)
1890 {
1891 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1892 	char client_name[RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE];
1893 	struct ib_client_nl_info data = {};
1894 	struct ib_device *ibdev = NULL;
1895 	struct sk_buff *msg;
1896 	u32 index;
1897 	int err;
1898 
1899 	err = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
1900 			    NL_VALIDATE_LIBERAL, extack);
1901 	if (err || !tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE])
1902 		return -EINVAL;
1903 
1904 	nla_strscpy(client_name, tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE],
1905 		    sizeof(client_name));
1906 
1907 	if (tb[RDMA_NLDEV_ATTR_DEV_INDEX]) {
1908 		index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1909 		ibdev = ib_device_get_by_index(sock_net(skb->sk), index);
1910 		if (!ibdev)
1911 			return -EINVAL;
1912 
1913 		if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1914 			data.port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1915 			if (!rdma_is_port_valid(ibdev, data.port)) {
1916 				err = -EINVAL;
1917 				goto out_put;
1918 			}
1919 		} else {
1920 			data.port = -1;
1921 		}
1922 	} else if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1923 		return -EINVAL;
1924 	}
1925 
1926 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1927 	if (!msg) {
1928 		err = -ENOMEM;
1929 		goto out_put;
1930 	}
1931 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1932 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1933 					 RDMA_NLDEV_CMD_GET_CHARDEV),
1934 			0, 0);
1935 	if (!nlh) {
1936 		err = -EMSGSIZE;
1937 		goto out_nlmsg;
1938 	}
1939 
1940 	data.nl_msg = msg;
1941 	err = ib_get_client_nl_info(ibdev, client_name, &data);
1942 	if (err)
1943 		goto out_nlmsg;
1944 
1945 	err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV,
1946 				huge_encode_dev(data.cdev->devt),
1947 				RDMA_NLDEV_ATTR_PAD);
1948 	if (err)
1949 		goto out_data;
1950 	err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV_ABI, data.abi,
1951 				RDMA_NLDEV_ATTR_PAD);
1952 	if (err)
1953 		goto out_data;
1954 	if (nla_put_string(msg, RDMA_NLDEV_ATTR_CHARDEV_NAME,
1955 			   dev_name(data.cdev))) {
1956 		err = -EMSGSIZE;
1957 		goto out_data;
1958 	}
1959 
1960 	nlmsg_end(msg, nlh);
1961 	put_device(data.cdev);
1962 	if (ibdev)
1963 		ib_device_put(ibdev);
1964 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1965 
1966 out_data:
1967 	put_device(data.cdev);
1968 out_nlmsg:
1969 	nlmsg_free(msg);
1970 out_put:
1971 	if (ibdev)
1972 		ib_device_put(ibdev);
1973 	return err;
1974 }
1975 
1976 static int nldev_sys_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1977 			      struct netlink_ext_ack *extack)
1978 {
1979 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1980 	struct sk_buff *msg;
1981 	int err;
1982 
1983 	err = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1984 			    nldev_policy, NL_VALIDATE_LIBERAL, extack);
1985 	if (err)
1986 		return err;
1987 
1988 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1989 	if (!msg)
1990 		return -ENOMEM;
1991 
1992 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1993 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1994 					 RDMA_NLDEV_CMD_SYS_GET),
1995 			0, 0);
1996 	if (!nlh) {
1997 		nlmsg_free(msg);
1998 		return -EMSGSIZE;
1999 	}
2000 
2001 	err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_NETNS_MODE,
2002 			 (u8)ib_devices_shared_netns);
2003 	if (err) {
2004 		nlmsg_free(msg);
2005 		return err;
2006 	}
2007 
2008 	err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE,
2009 			 (u8)privileged_qkey);
2010 	if (err) {
2011 		nlmsg_free(msg);
2012 		return err;
2013 	}
2014 
2015 	err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_MONITOR_MODE, 1);
2016 	if (err) {
2017 		nlmsg_free(msg);
2018 		return err;
2019 	}
2020 	/*
2021 	 * Copy-on-fork is supported.
2022 	 * See commits:
2023 	 * 70e806e4e645 ("mm: Do early cow for pinned pages during fork() for ptes")
2024 	 * 4eae4efa2c29 ("hugetlb: do early cow when page pinned on src mm")
2025 	 * for more details. Don't backport this without them.
2026 	 *
2027 	 * Return value ignored on purpose, assume copy-on-fork is not
2028 	 * supported in case of failure.
2029 	 */
2030 	nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK, 1);
2031 
2032 	nlmsg_end(msg, nlh);
2033 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2034 }
2035 
2036 static int nldev_set_sys_set_netns_doit(struct nlattr *tb[])
2037 {
2038 	u8 enable;
2039 	int err;
2040 
2041 	enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]);
2042 	/* Only 0 and 1 are supported */
2043 	if (enable > 1)
2044 		return -EINVAL;
2045 
2046 	err = rdma_compatdev_set(enable);
2047 	return err;
2048 }
2049 
2050 static int nldev_set_sys_set_pqkey_doit(struct nlattr *tb[])
2051 {
2052 	u8 enable;
2053 
2054 	enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE]);
2055 	/* Only 0 and 1 are supported */
2056 	if (enable > 1)
2057 		return -EINVAL;
2058 
2059 	privileged_qkey = enable;
2060 	return 0;
2061 }
2062 
2063 static int nldev_set_sys_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2064 				  struct netlink_ext_ack *extack)
2065 {
2066 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2067 	int err;
2068 
2069 	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2070 			  nldev_policy, extack);
2071 	if (err)
2072 		return -EINVAL;
2073 
2074 	if (tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE])
2075 		return nldev_set_sys_set_netns_doit(tb);
2076 
2077 	if (tb[RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE])
2078 		return nldev_set_sys_set_pqkey_doit(tb);
2079 
2080 	return -EINVAL;
2081 }
2082 
2083 
2084 static int nldev_stat_set_mode_doit(struct sk_buff *msg,
2085 				    struct netlink_ext_ack *extack,
2086 				    struct nlattr *tb[],
2087 				    struct ib_device *device, u32 port)
2088 {
2089 	u32 mode, mask = 0, qpn, cntn = 0;
2090 	bool opcnt = false;
2091 	int ret;
2092 
2093 	/* Currently only counter for QP is supported */
2094 	if (!tb[RDMA_NLDEV_ATTR_STAT_RES] ||
2095 	    nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
2096 		return -EINVAL;
2097 
2098 	if (tb[RDMA_NLDEV_ATTR_STAT_OPCOUNTER_ENABLED])
2099 		opcnt = !!nla_get_u8(
2100 			tb[RDMA_NLDEV_ATTR_STAT_OPCOUNTER_ENABLED]);
2101 
2102 	mode = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_MODE]);
2103 	if (mode == RDMA_COUNTER_MODE_AUTO) {
2104 		if (tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK])
2105 			mask = nla_get_u32(
2106 				tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]);
2107 		return rdma_counter_set_auto_mode(device, port, mask, opcnt,
2108 						  extack);
2109 	}
2110 
2111 	if (!tb[RDMA_NLDEV_ATTR_RES_LQPN])
2112 		return -EINVAL;
2113 
2114 	qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
2115 	if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]) {
2116 		cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
2117 		ret = rdma_counter_bind_qpn(device, port, qpn, cntn);
2118 		if (ret)
2119 			return ret;
2120 	} else {
2121 		ret = rdma_counter_bind_qpn_alloc(device, port, qpn, &cntn);
2122 		if (ret)
2123 			return ret;
2124 	}
2125 
2126 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
2127 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
2128 		ret = -EMSGSIZE;
2129 		goto err_fill;
2130 	}
2131 
2132 	return 0;
2133 
2134 err_fill:
2135 	rdma_counter_unbind_qpn(device, port, qpn, cntn);
2136 	return ret;
2137 }
2138 
2139 static int nldev_stat_set_counter_dynamic_doit(struct nlattr *tb[],
2140 					       struct ib_device *device,
2141 					       u32 port)
2142 {
2143 	struct rdma_hw_stats *stats;
2144 	struct nlattr *entry_attr;
2145 	unsigned long *target;
2146 	int rem, i, ret = 0;
2147 	u32 index;
2148 
2149 	stats = ib_get_hw_stats_port(device, port);
2150 	if (!stats)
2151 		return -EINVAL;
2152 
2153 	target = kcalloc(BITS_TO_LONGS(stats->num_counters),
2154 			 sizeof(*stats->is_disabled), GFP_KERNEL);
2155 	if (!target)
2156 		return -ENOMEM;
2157 
2158 	nla_for_each_nested(entry_attr, tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS],
2159 			    rem) {
2160 		index = nla_get_u32(entry_attr);
2161 		if ((index >= stats->num_counters) ||
2162 		    !(stats->descs[index].flags & IB_STAT_FLAG_OPTIONAL)) {
2163 			ret = -EINVAL;
2164 			goto out;
2165 		}
2166 
2167 		set_bit(index, target);
2168 	}
2169 
2170 	for (i = 0; i < stats->num_counters; i++) {
2171 		if (!(stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL))
2172 			continue;
2173 
2174 		ret = rdma_counter_modify(device, port, i, test_bit(i, target));
2175 		if (ret)
2176 			goto out;
2177 	}
2178 
2179 out:
2180 	kfree(target);
2181 	return ret;
2182 }
2183 
2184 static int nldev_stat_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2185 			       struct netlink_ext_ack *extack)
2186 {
2187 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2188 	struct ib_device *device;
2189 	struct sk_buff *msg;
2190 	u32 index, port;
2191 	int ret;
2192 
2193 	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
2194 			  extack);
2195 	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
2196 	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2197 		return -EINVAL;
2198 
2199 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2200 	device = ib_device_get_by_index(sock_net(skb->sk), index);
2201 	if (!device)
2202 		return -EINVAL;
2203 
2204 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2205 	if (!rdma_is_port_valid(device, port)) {
2206 		ret = -EINVAL;
2207 		goto err_put_device;
2208 	}
2209 
2210 	if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] &&
2211 	    !tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]) {
2212 		ret = -EINVAL;
2213 		goto err_put_device;
2214 	}
2215 
2216 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2217 	if (!msg) {
2218 		ret = -ENOMEM;
2219 		goto err_put_device;
2220 	}
2221 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2222 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2223 					 RDMA_NLDEV_CMD_STAT_SET),
2224 			0, 0);
2225 	if (!nlh || fill_nldev_handle(msg, device) ||
2226 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) {
2227 		ret = -EMSGSIZE;
2228 		goto err_free_msg;
2229 	}
2230 
2231 	if (tb[RDMA_NLDEV_ATTR_STAT_MODE]) {
2232 		ret = nldev_stat_set_mode_doit(msg, extack, tb, device, port);
2233 		if (ret)
2234 			goto err_free_msg;
2235 	}
2236 
2237 	if (tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]) {
2238 		ret = nldev_stat_set_counter_dynamic_doit(tb, device, port);
2239 		if (ret)
2240 			goto err_free_msg;
2241 	}
2242 
2243 	nlmsg_end(msg, nlh);
2244 	ib_device_put(device);
2245 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2246 
2247 err_free_msg:
2248 	nlmsg_free(msg);
2249 err_put_device:
2250 	ib_device_put(device);
2251 	return ret;
2252 }
2253 
2254 static int nldev_stat_del_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2255 			       struct netlink_ext_ack *extack)
2256 {
2257 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2258 	struct ib_device *device;
2259 	struct sk_buff *msg;
2260 	u32 index, port, qpn, cntn;
2261 	int ret;
2262 
2263 	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2264 			  nldev_policy, extack);
2265 	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES] ||
2266 	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX] ||
2267 	    !tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID] ||
2268 	    !tb[RDMA_NLDEV_ATTR_RES_LQPN])
2269 		return -EINVAL;
2270 
2271 	if (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
2272 		return -EINVAL;
2273 
2274 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2275 	device = ib_device_get_by_index(sock_net(skb->sk), index);
2276 	if (!device)
2277 		return -EINVAL;
2278 
2279 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2280 	if (!rdma_is_port_valid(device, port)) {
2281 		ret = -EINVAL;
2282 		goto err;
2283 	}
2284 
2285 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2286 	if (!msg) {
2287 		ret = -ENOMEM;
2288 		goto err;
2289 	}
2290 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2291 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2292 					 RDMA_NLDEV_CMD_STAT_SET),
2293 			0, 0);
2294 	if (!nlh) {
2295 		ret = -EMSGSIZE;
2296 		goto err_fill;
2297 	}
2298 
2299 	cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
2300 	qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
2301 	if (fill_nldev_handle(msg, device) ||
2302 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
2303 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
2304 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
2305 		ret = -EMSGSIZE;
2306 		goto err_fill;
2307 	}
2308 
2309 	ret = rdma_counter_unbind_qpn(device, port, qpn, cntn);
2310 	if (ret)
2311 		goto err_fill;
2312 
2313 	nlmsg_end(msg, nlh);
2314 	ib_device_put(device);
2315 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2316 
2317 err_fill:
2318 	nlmsg_free(msg);
2319 err:
2320 	ib_device_put(device);
2321 	return ret;
2322 }
2323 
2324 static noinline_for_stack int
2325 stat_get_doit_default_counter(struct sk_buff *skb, struct nlmsghdr *nlh,
2326 			      struct netlink_ext_ack *extack,
2327 			      struct nlattr *tb[])
2328 {
2329 	struct rdma_hw_stats *stats;
2330 	struct nlattr *table_attr;
2331 	struct ib_device *device;
2332 	int ret, num_cnts, i;
2333 	struct sk_buff *msg;
2334 	u32 index, port;
2335 	u64 v;
2336 
2337 	if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2338 		return -EINVAL;
2339 
2340 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2341 	device = ib_device_get_by_index(sock_net(skb->sk), index);
2342 	if (!device)
2343 		return -EINVAL;
2344 
2345 	if (!device->ops.alloc_hw_port_stats || !device->ops.get_hw_stats) {
2346 		ret = -EINVAL;
2347 		goto err;
2348 	}
2349 
2350 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2351 	stats = ib_get_hw_stats_port(device, port);
2352 	if (!stats) {
2353 		ret = -EINVAL;
2354 		goto err;
2355 	}
2356 
2357 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2358 	if (!msg) {
2359 		ret = -ENOMEM;
2360 		goto err;
2361 	}
2362 
2363 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2364 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2365 					 RDMA_NLDEV_CMD_STAT_GET),
2366 			0, 0);
2367 
2368 	if (!nlh || fill_nldev_handle(msg, device) ||
2369 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) {
2370 		ret = -EMSGSIZE;
2371 		goto err_msg;
2372 	}
2373 
2374 	mutex_lock(&stats->lock);
2375 
2376 	num_cnts = device->ops.get_hw_stats(device, stats, port, 0);
2377 	if (num_cnts < 0) {
2378 		ret = -EINVAL;
2379 		goto err_stats;
2380 	}
2381 
2382 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
2383 	if (!table_attr) {
2384 		ret = -EMSGSIZE;
2385 		goto err_stats;
2386 	}
2387 	for (i = 0; i < num_cnts; i++) {
2388 		if (test_bit(i, stats->is_disabled))
2389 			continue;
2390 
2391 		v = stats->value[i] +
2392 			rdma_counter_get_hwstat_value(device, port, i);
2393 		if (rdma_nl_stat_hwcounter_entry(msg,
2394 						 stats->descs[i].name, v)) {
2395 			ret = -EMSGSIZE;
2396 			goto err_table;
2397 		}
2398 	}
2399 	nla_nest_end(msg, table_attr);
2400 
2401 	mutex_unlock(&stats->lock);
2402 	nlmsg_end(msg, nlh);
2403 	ib_device_put(device);
2404 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2405 
2406 err_table:
2407 	nla_nest_cancel(msg, table_attr);
2408 err_stats:
2409 	mutex_unlock(&stats->lock);
2410 err_msg:
2411 	nlmsg_free(msg);
2412 err:
2413 	ib_device_put(device);
2414 	return ret;
2415 }
2416 
2417 static noinline_for_stack int
2418 stat_get_doit_qp(struct sk_buff *skb, struct nlmsghdr *nlh,
2419 		 struct netlink_ext_ack *extack, struct nlattr *tb[])
2420 
2421 {
2422 	static enum rdma_nl_counter_mode mode;
2423 	static enum rdma_nl_counter_mask mask;
2424 	struct ib_device *device;
2425 	struct sk_buff *msg;
2426 	u32 index, port;
2427 	bool opcnt;
2428 	int ret;
2429 
2430 	if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID])
2431 		return nldev_res_get_counter_doit(skb, nlh, extack);
2432 
2433 	if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] ||
2434 	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2435 		return -EINVAL;
2436 
2437 	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2438 	device = ib_device_get_by_index(sock_net(skb->sk), index);
2439 	if (!device)
2440 		return -EINVAL;
2441 
2442 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2443 	if (!rdma_is_port_valid(device, port)) {
2444 		ret = -EINVAL;
2445 		goto err;
2446 	}
2447 
2448 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2449 	if (!msg) {
2450 		ret = -ENOMEM;
2451 		goto err;
2452 	}
2453 
2454 	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2455 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2456 					 RDMA_NLDEV_CMD_STAT_GET),
2457 			0, 0);
2458 	if (!nlh) {
2459 		ret = -EMSGSIZE;
2460 		goto err_msg;
2461 	}
2462 
2463 	ret = rdma_counter_get_mode(device, port, &mode, &mask, &opcnt);
2464 	if (ret)
2465 		goto err_msg;
2466 
2467 	if (fill_nldev_handle(msg, device) ||
2468 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
2469 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, mode)) {
2470 		ret = -EMSGSIZE;
2471 		goto err_msg;
2472 	}
2473 
2474 	if ((mode == RDMA_COUNTER_MODE_AUTO) &&
2475 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK, mask)) {
2476 		ret = -EMSGSIZE;
2477 		goto err_msg;
2478 	}
2479 
2480 	if ((mode == RDMA_COUNTER_MODE_AUTO) &&
2481 	    nla_put_u8(msg, RDMA_NLDEV_ATTR_STAT_OPCOUNTER_ENABLED, opcnt)) {
2482 		ret = -EMSGSIZE;
2483 		goto err_msg;
2484 	}
2485 
2486 	nlmsg_end(msg, nlh);
2487 	ib_device_put(device);
2488 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2489 
2490 err_msg:
2491 	nlmsg_free(msg);
2492 err:
2493 	ib_device_put(device);
2494 	return ret;
2495 }
2496 
2497 static int nldev_stat_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2498 			       struct netlink_ext_ack *extack)
2499 {
2500 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2501 	int ret;
2502 
2503 	ret = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2504 			    nldev_policy, NL_VALIDATE_LIBERAL, extack);
2505 	if (ret)
2506 		return -EINVAL;
2507 
2508 	if (!tb[RDMA_NLDEV_ATTR_STAT_RES])
2509 		return stat_get_doit_default_counter(skb, nlh, extack, tb);
2510 
2511 	switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2512 	case RDMA_NLDEV_ATTR_RES_QP:
2513 		ret = stat_get_doit_qp(skb, nlh, extack, tb);
2514 		break;
2515 	case RDMA_NLDEV_ATTR_RES_MR:
2516 		ret = res_get_common_doit(skb, nlh, extack, RDMA_RESTRACK_MR,
2517 					  fill_stat_mr_entry);
2518 		break;
2519 	default:
2520 		ret = -EINVAL;
2521 		break;
2522 	}
2523 
2524 	return ret;
2525 }
2526 
2527 static int nldev_stat_get_dumpit(struct sk_buff *skb,
2528 				 struct netlink_callback *cb)
2529 {
2530 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2531 	int ret;
2532 
2533 	ret = __nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2534 			    nldev_policy, NL_VALIDATE_LIBERAL, NULL);
2535 	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES])
2536 		return -EINVAL;
2537 
2538 	switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2539 	case RDMA_NLDEV_ATTR_RES_QP:
2540 		ret = nldev_res_get_counter_dumpit(skb, cb);
2541 		break;
2542 	case RDMA_NLDEV_ATTR_RES_MR:
2543 		ret = res_get_common_dumpit(skb, cb, RDMA_RESTRACK_MR,
2544 					    fill_stat_mr_entry);
2545 		break;
2546 	default:
2547 		ret = -EINVAL;
2548 		break;
2549 	}
2550 
2551 	return ret;
2552 }
2553 
2554 static int nldev_stat_get_counter_status_doit(struct sk_buff *skb,
2555 					      struct nlmsghdr *nlh,
2556 					      struct netlink_ext_ack *extack)
2557 {
2558 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX], *table, *entry;
2559 	struct rdma_hw_stats *stats;
2560 	struct ib_device *device;
2561 	struct sk_buff *msg;
2562 	u32 devid, port;
2563 	int ret, i;
2564 
2565 	ret = __nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2566 			    nldev_policy, NL_VALIDATE_LIBERAL, extack);
2567 	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
2568 	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2569 		return -EINVAL;
2570 
2571 	devid = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2572 	device = ib_device_get_by_index(sock_net(skb->sk), devid);
2573 	if (!device)
2574 		return -EINVAL;
2575 
2576 	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2577 	if (!rdma_is_port_valid(device, port)) {
2578 		ret = -EINVAL;
2579 		goto err;
2580 	}
2581 
2582 	stats = ib_get_hw_stats_port(device, port);
2583 	if (!stats) {
2584 		ret = -EINVAL;
2585 		goto err;
2586 	}
2587 
2588 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2589 	if (!msg) {
2590 		ret = -ENOMEM;
2591 		goto err;
2592 	}
2593 
2594 	nlh = nlmsg_put(
2595 		msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2596 		RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_STAT_GET_STATUS),
2597 		0, 0);
2598 
2599 	ret = -EMSGSIZE;
2600 	if (!nlh || fill_nldev_handle(msg, device) ||
2601 	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
2602 		goto err_msg;
2603 
2604 	table = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
2605 	if (!table)
2606 		goto err_msg;
2607 
2608 	mutex_lock(&stats->lock);
2609 	for (i = 0; i < stats->num_counters; i++) {
2610 		entry = nla_nest_start(msg,
2611 				       RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY);
2612 		if (!entry)
2613 			goto err_msg_table;
2614 
2615 		if (nla_put_string(msg,
2616 				   RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME,
2617 				   stats->descs[i].name) ||
2618 		    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_INDEX, i))
2619 			goto err_msg_entry;
2620 
2621 		if ((stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL) &&
2622 		    (nla_put_u8(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_DYNAMIC,
2623 				!test_bit(i, stats->is_disabled))))
2624 			goto err_msg_entry;
2625 
2626 		nla_nest_end(msg, entry);
2627 	}
2628 	mutex_unlock(&stats->lock);
2629 
2630 	nla_nest_end(msg, table);
2631 	nlmsg_end(msg, nlh);
2632 	ib_device_put(device);
2633 	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2634 
2635 err_msg_entry:
2636 	nla_nest_cancel(msg, entry);
2637 err_msg_table:
2638 	mutex_unlock(&stats->lock);
2639 	nla_nest_cancel(msg, table);
2640 err_msg:
2641 	nlmsg_free(msg);
2642 err:
2643 	ib_device_put(device);
2644 	return ret;
2645 }
2646 
2647 static int nldev_newdev(struct sk_buff *skb, struct nlmsghdr *nlh,
2648 			struct netlink_ext_ack *extack)
2649 {
2650 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2651 	enum rdma_nl_dev_type type;
2652 	struct ib_device *parent;
2653 	char name[IFNAMSIZ] = {};
2654 	u32 parentid;
2655 	int ret;
2656 
2657 	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2658 			  nldev_policy, extack);
2659 	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
2660 		!tb[RDMA_NLDEV_ATTR_DEV_NAME] || !tb[RDMA_NLDEV_ATTR_DEV_TYPE])
2661 		return -EINVAL;
2662 
2663 	nla_strscpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME], sizeof(name));
2664 	type = nla_get_u8(tb[RDMA_NLDEV_ATTR_DEV_TYPE]);
2665 	parentid = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2666 	parent = ib_device_get_by_index(sock_net(skb->sk), parentid);
2667 	if (!parent)
2668 		return -EINVAL;
2669 
2670 	ret = ib_add_sub_device(parent, type, name);
2671 	ib_device_put(parent);
2672 
2673 	return ret;
2674 }
2675 
2676 static int nldev_deldev(struct sk_buff *skb, struct nlmsghdr *nlh,
2677 			struct netlink_ext_ack *extack)
2678 {
2679 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2680 	struct ib_device *device;
2681 	u32 devid;
2682 	int ret;
2683 
2684 	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2685 			  nldev_policy, extack);
2686 	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
2687 		return -EINVAL;
2688 
2689 	devid = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2690 	device = ib_device_get_by_index(sock_net(skb->sk), devid);
2691 	if (!device)
2692 		return -EINVAL;
2693 
2694 	return ib_del_sub_device_and_put(device);
2695 }
2696 
2697 static int fill_frmr_pool_key(struct sk_buff *msg, struct ib_frmr_key *key)
2698 {
2699 	struct nlattr *key_attr;
2700 
2701 	key_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_FRMR_POOL_KEY);
2702 	if (!key_attr)
2703 		return -EMSGSIZE;
2704 
2705 	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ATS, key->ats))
2706 		goto err;
2707 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ACCESS_FLAGS,
2708 			key->access_flags))
2709 		goto err;
2710 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_FRMR_POOL_KEY_VENDOR_KEY,
2711 			      key->vendor_key, RDMA_NLDEV_ATTR_PAD))
2712 		goto err;
2713 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_FRMR_POOL_KEY_NUM_DMA_BLOCKS,
2714 			      key->num_dma_blocks, RDMA_NLDEV_ATTR_PAD))
2715 		goto err;
2716 
2717 	if (key->kernel_vendor_key &&
2718 	    nla_put_u64_64bit(msg,
2719 			      RDMA_NLDEV_ATTR_FRMR_POOL_KEY_KERNEL_VENDOR_KEY,
2720 			      key->kernel_vendor_key, RDMA_NLDEV_ATTR_PAD))
2721 		goto err;
2722 
2723 	nla_nest_end(msg, key_attr);
2724 	return 0;
2725 
2726 err:
2727 	return -EMSGSIZE;
2728 }
2729 
2730 static int fill_frmr_pool_entry(struct sk_buff *msg, struct ib_frmr_pool *pool)
2731 {
2732 	if (fill_frmr_pool_key(msg, &pool->key))
2733 		return -EMSGSIZE;
2734 
2735 	spin_lock(&pool->lock);
2736 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_FRMR_POOL_QUEUE_HANDLES,
2737 			pool->queue.ci + pool->inactive_queue.ci))
2738 		goto err_unlock;
2739 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_FRMR_POOL_MAX_IN_USE,
2740 			      pool->max_in_use, RDMA_NLDEV_ATTR_PAD))
2741 		goto err_unlock;
2742 	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_FRMR_POOL_IN_USE,
2743 			      pool->in_use, RDMA_NLDEV_ATTR_PAD))
2744 		goto err_unlock;
2745 	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_FRMR_POOL_PINNED_HANDLES,
2746 			pool->pinned_handles))
2747 		goto err_unlock;
2748 	spin_unlock(&pool->lock);
2749 
2750 	return 0;
2751 
2752 err_unlock:
2753 	spin_unlock(&pool->lock);
2754 	return -EMSGSIZE;
2755 }
2756 
2757 static int nldev_frmr_pools_parse_key(struct nlattr *tb[],
2758 				      struct ib_frmr_key *key,
2759 				      struct netlink_ext_ack *extack)
2760 {
2761 	if (tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ATS])
2762 		key->ats = nla_get_u8(tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ATS]);
2763 
2764 	if (tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ACCESS_FLAGS])
2765 		key->access_flags = nla_get_u32(
2766 			tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_ACCESS_FLAGS]);
2767 
2768 	if (tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_VENDOR_KEY])
2769 		key->vendor_key = nla_get_u64(
2770 			tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_VENDOR_KEY]);
2771 
2772 	if (tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_NUM_DMA_BLOCKS])
2773 		key->num_dma_blocks = nla_get_u64(
2774 			tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_NUM_DMA_BLOCKS]);
2775 
2776 	if (tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY_KERNEL_VENDOR_KEY])
2777 		return -EINVAL;
2778 
2779 	return 0;
2780 }
2781 
2782 static int nldev_frmr_pools_set_pinned(struct ib_device *device,
2783 				       struct nlattr *tb[],
2784 				       struct netlink_ext_ack *extack)
2785 {
2786 	struct nlattr *key_tb[RDMA_NLDEV_ATTR_MAX];
2787 	struct ib_frmr_key key = { 0 };
2788 	u32 pinned_handles = 0;
2789 	int err = 0;
2790 
2791 	pinned_handles =
2792 		nla_get_u32(tb[RDMA_NLDEV_ATTR_FRMR_POOL_PINNED_HANDLES]);
2793 
2794 	if (!tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY])
2795 		return -EINVAL;
2796 
2797 	err = nla_parse_nested(key_tb, RDMA_NLDEV_ATTR_MAX - 1,
2798 			       tb[RDMA_NLDEV_ATTR_FRMR_POOL_KEY], nldev_policy,
2799 			       extack);
2800 	if (err)
2801 		return err;
2802 
2803 	err = nldev_frmr_pools_parse_key(key_tb, &key, extack);
2804 	if (err)
2805 		return err;
2806 
2807 	err = ib_frmr_pools_set_pinned(device, &key, pinned_handles);
2808 
2809 	return err;
2810 }
2811 
2812 static int nldev_frmr_pools_get_dumpit(struct sk_buff *skb,
2813 				       struct netlink_callback *cb)
2814 {
2815 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2816 	struct ib_frmr_pools *pools;
2817 	int err, ret = 0, idx = 0;
2818 	struct ib_frmr_pool *pool;
2819 	struct nlattr *table_attr;
2820 	struct nlattr *entry_attr;
2821 	bool show_details = false;
2822 	struct ib_device *device;
2823 	int start = cb->args[0];
2824 	struct rb_node *node;
2825 	struct nlmsghdr *nlh;
2826 	bool filled = false;
2827 
2828 	err = __nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2829 			    nldev_policy, NL_VALIDATE_LIBERAL, NULL);
2830 	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
2831 		return -EINVAL;
2832 
2833 	device = ib_device_get_by_index(
2834 		sock_net(skb->sk), nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]));
2835 	if (!device)
2836 		return -EINVAL;
2837 
2838 	if (tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS])
2839 		show_details = nla_get_u8(tb[RDMA_NLDEV_ATTR_DRIVER_DETAILS]);
2840 
2841 	pools = device->frmr_pools;
2842 	if (!pools) {
2843 		ib_device_put(device);
2844 		return 0;
2845 	}
2846 
2847 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2848 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2849 					 RDMA_NLDEV_CMD_FRMR_POOLS_GET),
2850 			0, NLM_F_MULTI);
2851 
2852 	if (!nlh || fill_nldev_handle(skb, device)) {
2853 		ret = -EMSGSIZE;
2854 		goto err;
2855 	}
2856 
2857 	table_attr = nla_nest_start_noflag(skb, RDMA_NLDEV_ATTR_FRMR_POOLS);
2858 	if (!table_attr) {
2859 		ret = -EMSGSIZE;
2860 		goto err;
2861 	}
2862 
2863 	read_lock(&pools->rb_lock);
2864 	for (node = rb_first(&pools->rb_root); node; node = rb_next(node)) {
2865 		pool = rb_entry(node, struct ib_frmr_pool, node);
2866 		if (pool->key.kernel_vendor_key && !show_details)
2867 			continue;
2868 
2869 		if (idx < start) {
2870 			idx++;
2871 			continue;
2872 		}
2873 
2874 		filled = true;
2875 
2876 		entry_attr = nla_nest_start_noflag(
2877 			skb, RDMA_NLDEV_ATTR_FRMR_POOL_ENTRY);
2878 		if (!entry_attr) {
2879 			ret = -EMSGSIZE;
2880 			goto end_msg;
2881 		}
2882 
2883 		if (fill_frmr_pool_entry(skb, pool)) {
2884 			nla_nest_cancel(skb, entry_attr);
2885 			ret = -EMSGSIZE;
2886 			goto end_msg;
2887 		}
2888 
2889 		nla_nest_end(skb, entry_attr);
2890 		idx++;
2891 	}
2892 end_msg:
2893 	read_unlock(&pools->rb_lock);
2894 
2895 	nla_nest_end(skb, table_attr);
2896 	nlmsg_end(skb, nlh);
2897 	cb->args[0] = idx;
2898 
2899 	/*
2900 	 * No more entries to fill, cancel the message and
2901 	 * return 0 to mark end of dumpit.
2902 	 */
2903 	if (!filled)
2904 		goto err;
2905 
2906 	ib_device_put(device);
2907 	return skb->len;
2908 
2909 err:
2910 	nlmsg_cancel(skb, nlh);
2911 	ib_device_put(device);
2912 	return ret;
2913 }
2914 
2915 static int nldev_frmr_pools_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2916 				     struct netlink_ext_ack *extack)
2917 {
2918 	struct ib_device *device;
2919 	struct nlattr **tb;
2920 	u32 aging_period;
2921 	int err;
2922 
2923 	tb = kzalloc_objs(*tb, RDMA_NLDEV_ATTR_MAX, GFP_KERNEL);
2924 	if (!tb)
2925 		return -ENOMEM;
2926 
2927 	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
2928 			  extack);
2929 	if (err)
2930 		goto free_tb;
2931 
2932 	if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX]) {
2933 		err = -EINVAL;
2934 		goto free_tb;
2935 	}
2936 
2937 	device = ib_device_get_by_index(
2938 		sock_net(skb->sk), nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]));
2939 	if (!device) {
2940 		err = -EINVAL;
2941 		goto free_tb;
2942 	}
2943 
2944 	if (tb[RDMA_NLDEV_ATTR_FRMR_POOLS_AGING_PERIOD]) {
2945 		aging_period = nla_get_u32(
2946 			tb[RDMA_NLDEV_ATTR_FRMR_POOLS_AGING_PERIOD]);
2947 		err = ib_frmr_pools_set_aging_period(device, aging_period);
2948 		goto done;
2949 	}
2950 
2951 	if (tb[RDMA_NLDEV_ATTR_FRMR_POOL_PINNED_HANDLES])
2952 		err = nldev_frmr_pools_set_pinned(device, tb, extack);
2953 
2954 done:
2955 	ib_device_put(device);
2956 free_tb:
2957 	kfree(tb);
2958 	return err;
2959 }
2960 
2961 static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
2962 	[RDMA_NLDEV_CMD_GET] = {
2963 		.doit = nldev_get_doit,
2964 		.dump = nldev_get_dumpit,
2965 	},
2966 	[RDMA_NLDEV_CMD_GET_CHARDEV] = {
2967 		.doit = nldev_get_chardev,
2968 	},
2969 	[RDMA_NLDEV_CMD_SET] = {
2970 		.doit = nldev_set_doit,
2971 		.flags = RDMA_NL_ADMIN_PERM,
2972 	},
2973 	[RDMA_NLDEV_CMD_NEWLINK] = {
2974 		.doit = nldev_newlink,
2975 		.flags = RDMA_NL_ADMIN_PERM,
2976 	},
2977 	[RDMA_NLDEV_CMD_DELLINK] = {
2978 		.doit = nldev_dellink,
2979 		.flags = RDMA_NL_ADMIN_PERM,
2980 	},
2981 	[RDMA_NLDEV_CMD_PORT_GET] = {
2982 		.doit = nldev_port_get_doit,
2983 		.dump = nldev_port_get_dumpit,
2984 	},
2985 	[RDMA_NLDEV_CMD_RES_GET] = {
2986 		.doit = nldev_res_get_doit,
2987 		.dump = nldev_res_get_dumpit,
2988 	},
2989 	[RDMA_NLDEV_CMD_RES_QP_GET] = {
2990 		.doit = nldev_res_get_qp_doit,
2991 		.dump = nldev_res_get_qp_dumpit,
2992 	},
2993 	[RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
2994 		.doit = nldev_res_get_cm_id_doit,
2995 		.dump = nldev_res_get_cm_id_dumpit,
2996 	},
2997 	[RDMA_NLDEV_CMD_RES_CQ_GET] = {
2998 		.doit = nldev_res_get_cq_doit,
2999 		.dump = nldev_res_get_cq_dumpit,
3000 	},
3001 	[RDMA_NLDEV_CMD_RES_MR_GET] = {
3002 		.doit = nldev_res_get_mr_doit,
3003 		.dump = nldev_res_get_mr_dumpit,
3004 	},
3005 	[RDMA_NLDEV_CMD_RES_PD_GET] = {
3006 		.doit = nldev_res_get_pd_doit,
3007 		.dump = nldev_res_get_pd_dumpit,
3008 	},
3009 	[RDMA_NLDEV_CMD_RES_CTX_GET] = {
3010 		.doit = nldev_res_get_ctx_doit,
3011 		.dump = nldev_res_get_ctx_dumpit,
3012 	},
3013 	[RDMA_NLDEV_CMD_RES_SRQ_GET] = {
3014 		.doit = nldev_res_get_srq_doit,
3015 		.dump = nldev_res_get_srq_dumpit,
3016 	},
3017 	[RDMA_NLDEV_CMD_SYS_GET] = {
3018 		.doit = nldev_sys_get_doit,
3019 	},
3020 	[RDMA_NLDEV_CMD_SYS_SET] = {
3021 		.doit = nldev_set_sys_set_doit,
3022 		.flags = RDMA_NL_ADMIN_PERM,
3023 	},
3024 	[RDMA_NLDEV_CMD_STAT_SET] = {
3025 		.doit = nldev_stat_set_doit,
3026 		.flags = RDMA_NL_ADMIN_PERM,
3027 	},
3028 	[RDMA_NLDEV_CMD_STAT_GET] = {
3029 		.doit = nldev_stat_get_doit,
3030 		.dump = nldev_stat_get_dumpit,
3031 	},
3032 	[RDMA_NLDEV_CMD_STAT_DEL] = {
3033 		.doit = nldev_stat_del_doit,
3034 		.flags = RDMA_NL_ADMIN_PERM,
3035 	},
3036 	[RDMA_NLDEV_CMD_RES_QP_GET_RAW] = {
3037 		.doit = nldev_res_get_qp_raw_doit,
3038 		.dump = nldev_res_get_qp_raw_dumpit,
3039 		.flags = RDMA_NL_ADMIN_PERM,
3040 	},
3041 	[RDMA_NLDEV_CMD_RES_CQ_GET_RAW] = {
3042 		.doit = nldev_res_get_cq_raw_doit,
3043 		.dump = nldev_res_get_cq_raw_dumpit,
3044 		.flags = RDMA_NL_ADMIN_PERM,
3045 	},
3046 	[RDMA_NLDEV_CMD_RES_MR_GET_RAW] = {
3047 		.doit = nldev_res_get_mr_raw_doit,
3048 		.dump = nldev_res_get_mr_raw_dumpit,
3049 		.flags = RDMA_NL_ADMIN_PERM,
3050 	},
3051 	[RDMA_NLDEV_CMD_RES_SRQ_GET_RAW] = {
3052 		.doit = nldev_res_get_srq_raw_doit,
3053 		.dump = nldev_res_get_srq_raw_dumpit,
3054 		.flags = RDMA_NL_ADMIN_PERM,
3055 	},
3056 	[RDMA_NLDEV_CMD_STAT_GET_STATUS] = {
3057 		.doit = nldev_stat_get_counter_status_doit,
3058 	},
3059 	[RDMA_NLDEV_CMD_NEWDEV] = {
3060 		.doit = nldev_newdev,
3061 		.flags = RDMA_NL_ADMIN_PERM,
3062 	},
3063 	[RDMA_NLDEV_CMD_DELDEV] = {
3064 		.doit = nldev_deldev,
3065 		.flags = RDMA_NL_ADMIN_PERM,
3066 	},
3067 	[RDMA_NLDEV_CMD_FRMR_POOLS_GET] = {
3068 		.dump = nldev_frmr_pools_get_dumpit,
3069 	},
3070 	[RDMA_NLDEV_CMD_FRMR_POOLS_SET] = {
3071 		.doit = nldev_frmr_pools_set_doit,
3072 		.flags = RDMA_NL_ADMIN_PERM,
3073 	},
3074 };
3075 
3076 static int fill_mon_netdev_rename(struct sk_buff *msg,
3077 				  struct ib_device *device, u32 port,
3078 				  const struct net *net)
3079 {
3080 	struct net_device *netdev = ib_device_get_netdev(device, port);
3081 	int ret = 0;
3082 
3083 	if (!netdev || !net_eq(dev_net(netdev), net))
3084 		goto out;
3085 
3086 	ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
3087 	if (ret)
3088 		goto out;
3089 	ret = nla_put_string(msg, RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
3090 out:
3091 	dev_put(netdev);
3092 	return ret;
3093 }
3094 
3095 static int fill_mon_netdev_association(struct sk_buff *msg,
3096 				       struct ib_device *device, u32 port,
3097 				       const struct net *net)
3098 {
3099 	struct net_device *netdev = ib_device_get_netdev(device, port);
3100 	int ret = 0;
3101 
3102 	if (netdev && !net_eq(dev_net(netdev), net))
3103 		goto out;
3104 
3105 	ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index);
3106 	if (ret)
3107 		goto out;
3108 
3109 	ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME,
3110 			     dev_name(&device->dev));
3111 	if (ret)
3112 		goto out;
3113 
3114 	ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port);
3115 	if (ret)
3116 		goto out;
3117 
3118 	if (netdev) {
3119 		ret = nla_put_u32(msg,
3120 				  RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
3121 		if (ret)
3122 			goto out;
3123 
3124 		ret = nla_put_string(msg,
3125 				     RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
3126 	}
3127 
3128 out:
3129 	dev_put(netdev);
3130 	return ret;
3131 }
3132 
3133 static void rdma_nl_notify_err_msg(struct ib_device *device, u32 port_num,
3134 				    enum rdma_nl_notify_event_type type)
3135 {
3136 	struct net_device *netdev;
3137 
3138 	switch (type) {
3139 	case RDMA_REGISTER_EVENT:
3140 		dev_warn_ratelimited(&device->dev,
3141 				     "Failed to send RDMA monitor register device event\n");
3142 		break;
3143 	case RDMA_UNREGISTER_EVENT:
3144 		dev_warn_ratelimited(&device->dev,
3145 				     "Failed to send RDMA monitor unregister device event\n");
3146 		break;
3147 	case RDMA_NETDEV_ATTACH_EVENT:
3148 		netdev = ib_device_get_netdev(device, port_num);
3149 		dev_warn_ratelimited(&device->dev,
3150 				     "Failed to send RDMA monitor netdev attach event: port %d netdev %d\n",
3151 				     port_num, netdev->ifindex);
3152 		dev_put(netdev);
3153 		break;
3154 	case RDMA_NETDEV_DETACH_EVENT:
3155 		dev_warn_ratelimited(&device->dev,
3156 				     "Failed to send RDMA monitor netdev detach event: port %d\n",
3157 				     port_num);
3158 		break;
3159 	case RDMA_RENAME_EVENT:
3160 		dev_warn_ratelimited(&device->dev,
3161 				     "Failed to send RDMA monitor rename device event\n");
3162 		break;
3163 
3164 	case RDMA_NETDEV_RENAME_EVENT:
3165 		netdev = ib_device_get_netdev(device, port_num);
3166 		dev_warn_ratelimited(&device->dev,
3167 				     "Failed to send RDMA monitor netdev rename event: port %d netdev %d\n",
3168 				     port_num, netdev->ifindex);
3169 		dev_put(netdev);
3170 		break;
3171 	default:
3172 		break;
3173 	}
3174 }
3175 
3176 int rdma_nl_notify_event(struct ib_device *device, u32 port_num,
3177 			  enum rdma_nl_notify_event_type type)
3178 {
3179 	struct sk_buff *skb;
3180 	int ret = -EMSGSIZE;
3181 	struct net *net;
3182 	void *nlh;
3183 
3184 	net = read_pnet(&device->coredev.rdma_net);
3185 	if (!net)
3186 		return -EINVAL;
3187 
3188 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3189 	if (!skb)
3190 		return -ENOMEM;
3191 	nlh = nlmsg_put(skb, 0, 0,
3192 			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_MONITOR),
3193 			0, 0);
3194 	if (!nlh)
3195 		goto err_free;
3196 
3197 	switch (type) {
3198 	case RDMA_REGISTER_EVENT:
3199 	case RDMA_UNREGISTER_EVENT:
3200 	case RDMA_RENAME_EVENT:
3201 		ret = fill_nldev_handle(skb, device);
3202 		if (ret)
3203 			goto err_free;
3204 		break;
3205 	case RDMA_NETDEV_ATTACH_EVENT:
3206 	case RDMA_NETDEV_DETACH_EVENT:
3207 		ret = fill_mon_netdev_association(skb, device, port_num, net);
3208 		if (ret)
3209 			goto err_free;
3210 		break;
3211 	case RDMA_NETDEV_RENAME_EVENT:
3212 		ret = fill_mon_netdev_rename(skb, device, port_num, net);
3213 		if (ret)
3214 			goto err_free;
3215 		break;
3216 	default:
3217 		break;
3218 	}
3219 
3220 	ret = nla_put_u8(skb, RDMA_NLDEV_ATTR_EVENT_TYPE, type);
3221 	if (ret)
3222 		goto err_free;
3223 
3224 	nlmsg_end(skb, nlh);
3225 	ret = rdma_nl_multicast(net, skb, RDMA_NL_GROUP_NOTIFY, GFP_KERNEL);
3226 	if (ret && ret != -ESRCH) {
3227 		skb = NULL; /* skb is freed in the netlink send-op handling */
3228 		goto err_free;
3229 	}
3230 	return 0;
3231 
3232 err_free:
3233 	rdma_nl_notify_err_msg(device, port_num, type);
3234 	nlmsg_free(skb);
3235 	return ret;
3236 }
3237 
3238 void __init nldev_init(void)
3239 {
3240 	rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
3241 }
3242 
3243 void nldev_exit(void)
3244 {
3245 	rdma_nl_unregister(RDMA_NL_NLDEV);
3246 }
3247 
3248 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);
3249