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