RDMA/nldev: provide detailed PD information
[platform/kernel/linux-starfive.git] / drivers / infiniband / core / nldev.c
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 <net/netlink.h>
37 #include <rdma/rdma_cm.h>
38 #include <rdma/rdma_netlink.h>
39
40 #include "core_priv.h"
41 #include "cma_priv.h"
42
43 static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
44         [RDMA_NLDEV_ATTR_DEV_INDEX]     = { .type = NLA_U32 },
45         [RDMA_NLDEV_ATTR_DEV_NAME]      = { .type = NLA_NUL_STRING,
46                                             .len = IB_DEVICE_NAME_MAX - 1},
47         [RDMA_NLDEV_ATTR_PORT_INDEX]    = { .type = NLA_U32 },
48         [RDMA_NLDEV_ATTR_FW_VERSION]    = { .type = NLA_NUL_STRING,
49                                             .len = IB_FW_VERSION_NAME_MAX - 1},
50         [RDMA_NLDEV_ATTR_NODE_GUID]     = { .type = NLA_U64 },
51         [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = { .type = NLA_U64 },
52         [RDMA_NLDEV_ATTR_SUBNET_PREFIX] = { .type = NLA_U64 },
53         [RDMA_NLDEV_ATTR_LID]           = { .type = NLA_U32 },
54         [RDMA_NLDEV_ATTR_SM_LID]        = { .type = NLA_U32 },
55         [RDMA_NLDEV_ATTR_LMC]           = { .type = NLA_U8 },
56         [RDMA_NLDEV_ATTR_PORT_STATE]    = { .type = NLA_U8 },
57         [RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = { .type = NLA_U8 },
58         [RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = { .type = NLA_U8 },
59         [RDMA_NLDEV_ATTR_RES_SUMMARY]   = { .type = NLA_NESTED },
60         [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY]     = { .type = NLA_NESTED },
61         [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] = { .type = NLA_NUL_STRING,
62                                              .len = 16 },
63         [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR] = { .type = NLA_U64 },
64         [RDMA_NLDEV_ATTR_RES_QP]                = { .type = NLA_NESTED },
65         [RDMA_NLDEV_ATTR_RES_QP_ENTRY]          = { .type = NLA_NESTED },
66         [RDMA_NLDEV_ATTR_RES_LQPN]              = { .type = NLA_U32 },
67         [RDMA_NLDEV_ATTR_RES_RQPN]              = { .type = NLA_U32 },
68         [RDMA_NLDEV_ATTR_RES_RQ_PSN]            = { .type = NLA_U32 },
69         [RDMA_NLDEV_ATTR_RES_SQ_PSN]            = { .type = NLA_U32 },
70         [RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE] = { .type = NLA_U8 },
71         [RDMA_NLDEV_ATTR_RES_TYPE]              = { .type = NLA_U8 },
72         [RDMA_NLDEV_ATTR_RES_STATE]             = { .type = NLA_U8 },
73         [RDMA_NLDEV_ATTR_RES_PID]               = { .type = NLA_U32 },
74         [RDMA_NLDEV_ATTR_RES_KERN_NAME]         = { .type = NLA_NUL_STRING,
75                                                     .len = TASK_COMM_LEN },
76         [RDMA_NLDEV_ATTR_RES_CM_ID]             = { .type = NLA_NESTED },
77         [RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY]       = { .type = NLA_NESTED },
78         [RDMA_NLDEV_ATTR_RES_PS]                = { .type = NLA_U32 },
79         [RDMA_NLDEV_ATTR_RES_SRC_ADDR]  = {
80                         .len = sizeof(struct __kernel_sockaddr_storage) },
81         [RDMA_NLDEV_ATTR_RES_DST_ADDR]  = {
82                         .len = sizeof(struct __kernel_sockaddr_storage) },
83         [RDMA_NLDEV_ATTR_RES_CQ]                = { .type = NLA_NESTED },
84         [RDMA_NLDEV_ATTR_RES_CQ_ENTRY]          = { .type = NLA_NESTED },
85         [RDMA_NLDEV_ATTR_RES_CQE]               = { .type = NLA_U32 },
86         [RDMA_NLDEV_ATTR_RES_USECNT]            = { .type = NLA_U64 },
87         [RDMA_NLDEV_ATTR_RES_POLL_CTX]          = { .type = NLA_U8 },
88         [RDMA_NLDEV_ATTR_RES_MR]                = { .type = NLA_NESTED },
89         [RDMA_NLDEV_ATTR_RES_MR_ENTRY]          = { .type = NLA_NESTED },
90         [RDMA_NLDEV_ATTR_RES_RKEY]              = { .type = NLA_U32 },
91         [RDMA_NLDEV_ATTR_RES_LKEY]              = { .type = NLA_U32 },
92         [RDMA_NLDEV_ATTR_RES_IOVA]              = { .type = NLA_U64 },
93         [RDMA_NLDEV_ATTR_RES_MRLEN]             = { .type = NLA_U64 },
94         [RDMA_NLDEV_ATTR_RES_PD]                = { .type = NLA_NESTED },
95         [RDMA_NLDEV_ATTR_RES_PD_ENTRY]          = { .type = NLA_NESTED },
96         [RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]    = { .type = NLA_U32 },
97         [RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY] = { .type = NLA_U32 },
98 };
99
100 static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
101 {
102         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
103                 return -EMSGSIZE;
104         if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME, device->name))
105                 return -EMSGSIZE;
106
107         return 0;
108 }
109
110 static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
111 {
112         char fw[IB_FW_VERSION_NAME_MAX];
113
114         if (fill_nldev_handle(msg, device))
115                 return -EMSGSIZE;
116
117         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
118                 return -EMSGSIZE;
119
120         BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
121         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
122                               device->attrs.device_cap_flags, 0))
123                 return -EMSGSIZE;
124
125         ib_get_device_fw_str(device, fw);
126         /* Device without FW has strlen(fw) */
127         if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
128                 return -EMSGSIZE;
129
130         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
131                               be64_to_cpu(device->node_guid), 0))
132                 return -EMSGSIZE;
133         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
134                               be64_to_cpu(device->attrs.sys_image_guid), 0))
135                 return -EMSGSIZE;
136         if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
137                 return -EMSGSIZE;
138         return 0;
139 }
140
141 static int fill_port_info(struct sk_buff *msg,
142                           struct ib_device *device, u32 port)
143 {
144         struct ib_port_attr attr;
145         int ret;
146
147         if (fill_nldev_handle(msg, device))
148                 return -EMSGSIZE;
149
150         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
151                 return -EMSGSIZE;
152
153         ret = ib_query_port(device, port, &attr);
154         if (ret)
155                 return ret;
156
157         BUILD_BUG_ON(sizeof(attr.port_cap_flags) > sizeof(u64));
158         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
159                               (u64)attr.port_cap_flags, 0))
160                 return -EMSGSIZE;
161         if (rdma_protocol_ib(device, port) &&
162             nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
163                               attr.subnet_prefix, 0))
164                 return -EMSGSIZE;
165         if (rdma_protocol_ib(device, port)) {
166                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
167                         return -EMSGSIZE;
168                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
169                         return -EMSGSIZE;
170                 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
171                         return -EMSGSIZE;
172         }
173         if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
174                 return -EMSGSIZE;
175         if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
176                 return -EMSGSIZE;
177         return 0;
178 }
179
180 static int fill_res_info_entry(struct sk_buff *msg,
181                                const char *name, u64 curr)
182 {
183         struct nlattr *entry_attr;
184
185         entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
186         if (!entry_attr)
187                 return -EMSGSIZE;
188
189         if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
190                 goto err;
191         if (nla_put_u64_64bit(msg,
192                               RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr, 0))
193                 goto err;
194
195         nla_nest_end(msg, entry_attr);
196         return 0;
197
198 err:
199         nla_nest_cancel(msg, entry_attr);
200         return -EMSGSIZE;
201 }
202
203 static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
204 {
205         static const char * const names[RDMA_RESTRACK_MAX] = {
206                 [RDMA_RESTRACK_PD] = "pd",
207                 [RDMA_RESTRACK_CQ] = "cq",
208                 [RDMA_RESTRACK_QP] = "qp",
209                 [RDMA_RESTRACK_CM_ID] = "cm_id",
210                 [RDMA_RESTRACK_MR] = "mr",
211         };
212
213         struct rdma_restrack_root *res = &device->res;
214         struct nlattr *table_attr;
215         int ret, i, curr;
216
217         if (fill_nldev_handle(msg, device))
218                 return -EMSGSIZE;
219
220         table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
221         if (!table_attr)
222                 return -EMSGSIZE;
223
224         for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
225                 if (!names[i])
226                         continue;
227                 curr = rdma_restrack_count(res, i, task_active_pid_ns(current));
228                 ret = fill_res_info_entry(msg, names[i], curr);
229                 if (ret)
230                         goto err;
231         }
232
233         nla_nest_end(msg, table_attr);
234         return 0;
235
236 err:
237         nla_nest_cancel(msg, table_attr);
238         return ret;
239 }
240
241 static int fill_res_name_pid(struct sk_buff *msg,
242                              struct rdma_restrack_entry *res)
243 {
244         /*
245          * For user resources, user is should read /proc/PID/comm to get the
246          * name of the task file.
247          */
248         if (rdma_is_kernel_res(res)) {
249                 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
250                     res->kern_name))
251                         return -EMSGSIZE;
252         } else {
253                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID,
254                     task_pid_vnr(res->task)))
255                         return -EMSGSIZE;
256         }
257         return 0;
258 }
259
260 static int fill_res_qp_entry(struct sk_buff *msg, struct netlink_callback *cb,
261                              struct rdma_restrack_entry *res, uint32_t port)
262 {
263         struct ib_qp *qp = container_of(res, struct ib_qp, res);
264         struct ib_qp_init_attr qp_init_attr;
265         struct nlattr *entry_attr;
266         struct ib_qp_attr qp_attr;
267         int ret;
268
269         ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
270         if (ret)
271                 return ret;
272
273         if (port && port != qp_attr.port_num)
274                 return 0;
275
276         entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
277         if (!entry_attr)
278                 goto out;
279
280         /* In create_qp() port is not set yet */
281         if (qp_attr.port_num &&
282             nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp_attr.port_num))
283                 goto err;
284
285         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num))
286                 goto err;
287         if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
288                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
289                                 qp_attr.dest_qp_num))
290                         goto err;
291                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
292                                 qp_attr.rq_psn))
293                         goto err;
294         }
295
296         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
297                 goto err;
298
299         if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
300             qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
301                 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
302                                qp_attr.path_mig_state))
303                         goto err;
304         }
305         if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
306                 goto err;
307         if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
308                 goto err;
309
310         if (fill_res_name_pid(msg, res))
311                 goto err;
312
313         nla_nest_end(msg, entry_attr);
314         return 0;
315
316 err:
317         nla_nest_cancel(msg, entry_attr);
318 out:
319         return -EMSGSIZE;
320 }
321
322 static int fill_res_cm_id_entry(struct sk_buff *msg,
323                                 struct netlink_callback *cb,
324                                 struct rdma_restrack_entry *res, uint32_t port)
325 {
326         struct rdma_id_private *id_priv =
327                                 container_of(res, struct rdma_id_private, res);
328         struct rdma_cm_id *cm_id = &id_priv->id;
329         struct nlattr *entry_attr;
330
331         if (port && port != cm_id->port_num)
332                 return 0;
333
334         entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY);
335         if (!entry_attr)
336                 goto out;
337
338         if (cm_id->port_num &&
339             nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
340                 goto err;
341
342         if (id_priv->qp_num) {
343                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
344                         goto err;
345                 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
346                         goto err;
347         }
348
349         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
350                 goto err;
351
352         if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
353                 goto err;
354
355         if (cm_id->route.addr.src_addr.ss_family &&
356             nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
357                     sizeof(cm_id->route.addr.src_addr),
358                     &cm_id->route.addr.src_addr))
359                 goto err;
360         if (cm_id->route.addr.dst_addr.ss_family &&
361             nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
362                     sizeof(cm_id->route.addr.dst_addr),
363                     &cm_id->route.addr.dst_addr))
364                 goto err;
365
366         if (fill_res_name_pid(msg, res))
367                 goto err;
368
369         nla_nest_end(msg, entry_attr);
370         return 0;
371
372 err:
373         nla_nest_cancel(msg, entry_attr);
374 out:
375         return -EMSGSIZE;
376 }
377
378 static int fill_res_cq_entry(struct sk_buff *msg, struct netlink_callback *cb,
379                              struct rdma_restrack_entry *res, uint32_t port)
380 {
381         struct ib_cq *cq = container_of(res, struct ib_cq, res);
382         struct nlattr *entry_attr;
383
384         entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_CQ_ENTRY);
385         if (!entry_attr)
386                 goto out;
387
388         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
389                 goto err;
390         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
391                               atomic_read(&cq->usecnt), 0))
392                 goto err;
393
394         /* Poll context is only valid for kernel CQs */
395         if (rdma_is_kernel_res(res) &&
396             nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
397                 goto err;
398
399         if (fill_res_name_pid(msg, res))
400                 goto err;
401
402         nla_nest_end(msg, entry_attr);
403         return 0;
404
405 err:
406         nla_nest_cancel(msg, entry_attr);
407 out:
408         return -EMSGSIZE;
409 }
410
411 static int fill_res_mr_entry(struct sk_buff *msg, struct netlink_callback *cb,
412                              struct rdma_restrack_entry *res, uint32_t port)
413 {
414         struct ib_mr *mr = container_of(res, struct ib_mr, res);
415         struct nlattr *entry_attr;
416
417         entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_MR_ENTRY);
418         if (!entry_attr)
419                 goto out;
420
421         if (netlink_capable(cb->skb, CAP_NET_ADMIN)) {
422                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
423                         goto err;
424                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
425                         goto err;
426                 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_IOVA,
427                                       mr->iova, 0))
428                         goto err;
429         }
430
431         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length, 0))
432                 goto err;
433
434         if (fill_res_name_pid(msg, res))
435                 goto err;
436
437         nla_nest_end(msg, entry_attr);
438         return 0;
439
440 err:
441         nla_nest_cancel(msg, entry_attr);
442 out:
443         return -EMSGSIZE;
444 }
445
446 static int fill_res_pd_entry(struct sk_buff *msg, struct netlink_callback *cb,
447                              struct rdma_restrack_entry *res, uint32_t port)
448 {
449         struct ib_pd *pd = container_of(res, struct ib_pd, res);
450         struct nlattr *entry_attr;
451
452         entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_PD_ENTRY);
453         if (!entry_attr)
454                 goto out;
455
456         if (netlink_capable(cb->skb, CAP_NET_ADMIN)) {
457                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
458                                 pd->local_dma_lkey))
459                         goto err;
460                 if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
461                     nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
462                                 pd->unsafe_global_rkey))
463                         goto err;
464         }
465         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
466                               atomic_read(&pd->usecnt), 0))
467                 goto err;
468         if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
469             nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
470                         pd->unsafe_global_rkey))
471                 goto err;
472
473         if (fill_res_name_pid(msg, res))
474                 goto err;
475
476         nla_nest_end(msg, entry_attr);
477         return 0;
478
479 err:
480         nla_nest_cancel(msg, entry_attr);
481 out:
482         return -EMSGSIZE;
483 }
484
485 static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
486                           struct netlink_ext_ack *extack)
487 {
488         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
489         struct ib_device *device;
490         struct sk_buff *msg;
491         u32 index;
492         int err;
493
494         err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
495                           nldev_policy, extack);
496         if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
497                 return -EINVAL;
498
499         index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
500
501         device = ib_device_get_by_index(index);
502         if (!device)
503                 return -EINVAL;
504
505         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
506         if (!msg) {
507                 err = -ENOMEM;
508                 goto err;
509         }
510
511         nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
512                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
513                         0, 0);
514
515         err = fill_dev_info(msg, device);
516         if (err)
517                 goto err_free;
518
519         nlmsg_end(msg, nlh);
520
521         put_device(&device->dev);
522         return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
523
524 err_free:
525         nlmsg_free(msg);
526 err:
527         put_device(&device->dev);
528         return err;
529 }
530
531 static int _nldev_get_dumpit(struct ib_device *device,
532                              struct sk_buff *skb,
533                              struct netlink_callback *cb,
534                              unsigned int idx)
535 {
536         int start = cb->args[0];
537         struct nlmsghdr *nlh;
538
539         if (idx < start)
540                 return 0;
541
542         nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
543                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
544                         0, NLM_F_MULTI);
545
546         if (fill_dev_info(skb, device)) {
547                 nlmsg_cancel(skb, nlh);
548                 goto out;
549         }
550
551         nlmsg_end(skb, nlh);
552
553         idx++;
554
555 out:    cb->args[0] = idx;
556         return skb->len;
557 }
558
559 static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
560 {
561         /*
562          * There is no need to take lock, because
563          * we are relying on ib_core's lists_rwsem
564          */
565         return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
566 }
567
568 static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
569                                struct netlink_ext_ack *extack)
570 {
571         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
572         struct ib_device *device;
573         struct sk_buff *msg;
574         u32 index;
575         u32 port;
576         int err;
577
578         err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
579                           nldev_policy, extack);
580         if (err ||
581             !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
582             !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
583                 return -EINVAL;
584
585         index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
586         device = ib_device_get_by_index(index);
587         if (!device)
588                 return -EINVAL;
589
590         port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
591         if (!rdma_is_port_valid(device, port)) {
592                 err = -EINVAL;
593                 goto err;
594         }
595
596         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
597         if (!msg) {
598                 err = -ENOMEM;
599                 goto err;
600         }
601
602         nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
603                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
604                         0, 0);
605
606         err = fill_port_info(msg, device, port);
607         if (err)
608                 goto err_free;
609
610         nlmsg_end(msg, nlh);
611         put_device(&device->dev);
612
613         return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
614
615 err_free:
616         nlmsg_free(msg);
617 err:
618         put_device(&device->dev);
619         return err;
620 }
621
622 static int nldev_port_get_dumpit(struct sk_buff *skb,
623                                  struct netlink_callback *cb)
624 {
625         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
626         struct ib_device *device;
627         int start = cb->args[0];
628         struct nlmsghdr *nlh;
629         u32 idx = 0;
630         u32 ifindex;
631         int err;
632         u32 p;
633
634         err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
635                           nldev_policy, NULL);
636         if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
637                 return -EINVAL;
638
639         ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
640         device = ib_device_get_by_index(ifindex);
641         if (!device)
642                 return -EINVAL;
643
644         for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
645                 /*
646                  * The dumpit function returns all information from specific
647                  * index. This specific index is taken from the netlink
648                  * messages request sent by user and it is available
649                  * in cb->args[0].
650                  *
651                  * Usually, the user doesn't fill this field and it causes
652                  * to return everything.
653                  *
654                  */
655                 if (idx < start) {
656                         idx++;
657                         continue;
658                 }
659
660                 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
661                                 cb->nlh->nlmsg_seq,
662                                 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
663                                                  RDMA_NLDEV_CMD_PORT_GET),
664                                 0, NLM_F_MULTI);
665
666                 if (fill_port_info(skb, device, p)) {
667                         nlmsg_cancel(skb, nlh);
668                         goto out;
669                 }
670                 idx++;
671                 nlmsg_end(skb, nlh);
672         }
673
674 out:
675         put_device(&device->dev);
676         cb->args[0] = idx;
677         return skb->len;
678 }
679
680 static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
681                               struct netlink_ext_ack *extack)
682 {
683         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
684         struct ib_device *device;
685         struct sk_buff *msg;
686         u32 index;
687         int ret;
688
689         ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
690                           nldev_policy, extack);
691         if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
692                 return -EINVAL;
693
694         index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
695         device = ib_device_get_by_index(index);
696         if (!device)
697                 return -EINVAL;
698
699         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
700         if (!msg) {
701                 ret = -ENOMEM;
702                 goto err;
703         }
704
705         nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
706                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
707                         0, 0);
708
709         ret = fill_res_info(msg, device);
710         if (ret)
711                 goto err_free;
712
713         nlmsg_end(msg, nlh);
714         put_device(&device->dev);
715         return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
716
717 err_free:
718         nlmsg_free(msg);
719 err:
720         put_device(&device->dev);
721         return ret;
722 }
723
724 static int _nldev_res_get_dumpit(struct ib_device *device,
725                                  struct sk_buff *skb,
726                                  struct netlink_callback *cb,
727                                  unsigned int idx)
728 {
729         int start = cb->args[0];
730         struct nlmsghdr *nlh;
731
732         if (idx < start)
733                 return 0;
734
735         nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
736                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
737                         0, NLM_F_MULTI);
738
739         if (fill_res_info(skb, device)) {
740                 nlmsg_cancel(skb, nlh);
741                 goto out;
742         }
743
744         nlmsg_end(skb, nlh);
745
746         idx++;
747
748 out:
749         cb->args[0] = idx;
750         return skb->len;
751 }
752
753 static int nldev_res_get_dumpit(struct sk_buff *skb,
754                                 struct netlink_callback *cb)
755 {
756         return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
757 }
758
759 struct nldev_fill_res_entry {
760         int (*fill_res_func)(struct sk_buff *msg, struct netlink_callback *cb,
761                              struct rdma_restrack_entry *res, u32 port);
762         enum rdma_nldev_attr nldev_attr;
763         enum rdma_nldev_command nldev_cmd;
764 };
765
766 static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
767         [RDMA_RESTRACK_QP] = {
768                 .fill_res_func = fill_res_qp_entry,
769                 .nldev_cmd = RDMA_NLDEV_CMD_RES_QP_GET,
770                 .nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
771         },
772         [RDMA_RESTRACK_CM_ID] = {
773                 .fill_res_func = fill_res_cm_id_entry,
774                 .nldev_cmd = RDMA_NLDEV_CMD_RES_CM_ID_GET,
775                 .nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
776         },
777         [RDMA_RESTRACK_CQ] = {
778                 .fill_res_func = fill_res_cq_entry,
779                 .nldev_cmd = RDMA_NLDEV_CMD_RES_CQ_GET,
780                 .nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
781         },
782         [RDMA_RESTRACK_MR] = {
783                 .fill_res_func = fill_res_mr_entry,
784                 .nldev_cmd = RDMA_NLDEV_CMD_RES_MR_GET,
785                 .nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
786         },
787         [RDMA_RESTRACK_PD] = {
788                 .fill_res_func = fill_res_pd_entry,
789                 .nldev_cmd = RDMA_NLDEV_CMD_RES_PD_GET,
790                 .nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
791         },
792 };
793
794 static int res_get_common_dumpit(struct sk_buff *skb,
795                                  struct netlink_callback *cb,
796                                  enum rdma_restrack_type res_type)
797 {
798         const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
799         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
800         struct rdma_restrack_entry *res;
801         int err, ret = 0, idx = 0;
802         struct nlattr *table_attr;
803         struct ib_device *device;
804         int start = cb->args[0];
805         struct nlmsghdr *nlh;
806         u32 index, port = 0;
807         bool filled = false;
808
809         err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
810                           nldev_policy, NULL);
811         /*
812          * Right now, we are expecting the device index to get res information,
813          * but it is possible to extend this code to return all devices in
814          * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
815          * if it doesn't exist, we will iterate over all devices.
816          *
817          * But it is not needed for now.
818          */
819         if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
820                 return -EINVAL;
821
822         index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
823         device = ib_device_get_by_index(index);
824         if (!device)
825                 return -EINVAL;
826
827         /*
828          * If no PORT_INDEX is supplied, we will return all QPs from that device
829          */
830         if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
831                 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
832                 if (!rdma_is_port_valid(device, port)) {
833                         ret = -EINVAL;
834                         goto err_index;
835                 }
836         }
837
838         nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
839                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, fe->nldev_cmd),
840                         0, NLM_F_MULTI);
841
842         if (fill_nldev_handle(skb, device)) {
843                 ret = -EMSGSIZE;
844                 goto err;
845         }
846
847         table_attr = nla_nest_start(skb, fe->nldev_attr);
848         if (!table_attr) {
849                 ret = -EMSGSIZE;
850                 goto err;
851         }
852
853         down_read(&device->res.rwsem);
854         hash_for_each_possible(device->res.hash, res, node, res_type) {
855                 if (idx < start)
856                         goto next;
857
858                 if ((rdma_is_kernel_res(res) &&
859                      task_active_pid_ns(current) != &init_pid_ns) ||
860                     (!rdma_is_kernel_res(res) && task_active_pid_ns(current) !=
861                      task_active_pid_ns(res->task)))
862                         /*
863                          * 1. Kern resources should be visible in init
864                          *    namspace only
865                          * 2. Present only resources visible in the current
866                          *    namespace
867                          */
868                         goto next;
869
870                 if (!rdma_restrack_get(res))
871                         /*
872                          * Resource is under release now, but we are not
873                          * relesing lock now, so it will be released in
874                          * our next pass, once we will get ->next pointer.
875                          */
876                         goto next;
877
878                 filled = true;
879
880                 up_read(&device->res.rwsem);
881                 ret = fe->fill_res_func(skb, cb, res, port);
882                 down_read(&device->res.rwsem);
883                 /*
884                  * Return resource back, but it won't be released till
885                  * the &device->res.rwsem will be released for write.
886                  */
887                 rdma_restrack_put(res);
888
889                 if (ret == -EMSGSIZE)
890                         /*
891                          * There is a chance to optimize here.
892                          * It can be done by using list_prepare_entry
893                          * and list_for_each_entry_continue afterwards.
894                          */
895                         break;
896                 if (ret)
897                         goto res_err;
898 next:           idx++;
899         }
900         up_read(&device->res.rwsem);
901
902         nla_nest_end(skb, table_attr);
903         nlmsg_end(skb, nlh);
904         cb->args[0] = idx;
905
906         /*
907          * No more entries to fill, cancel the message and
908          * return 0 to mark end of dumpit.
909          */
910         if (!filled)
911                 goto err;
912
913         put_device(&device->dev);
914         return skb->len;
915
916 res_err:
917         nla_nest_cancel(skb, table_attr);
918         up_read(&device->res.rwsem);
919
920 err:
921         nlmsg_cancel(skb, nlh);
922
923 err_index:
924         put_device(&device->dev);
925         return ret;
926 }
927
928 static int nldev_res_get_qp_dumpit(struct sk_buff *skb,
929                                    struct netlink_callback *cb)
930 {
931         return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_QP);
932 }
933
934 static int nldev_res_get_cm_id_dumpit(struct sk_buff *skb,
935                                       struct netlink_callback *cb)
936 {
937         return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_CM_ID);
938 }
939
940 static int nldev_res_get_cq_dumpit(struct sk_buff *skb,
941                                    struct netlink_callback *cb)
942 {
943         return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_CQ);
944 }
945
946 static int nldev_res_get_mr_dumpit(struct sk_buff *skb,
947                                    struct netlink_callback *cb)
948 {
949         return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_MR);
950 }
951
952 static int nldev_res_get_pd_dumpit(struct sk_buff *skb,
953                                    struct netlink_callback *cb)
954 {
955         return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_PD);
956 }
957
958 static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
959         [RDMA_NLDEV_CMD_GET] = {
960                 .doit = nldev_get_doit,
961                 .dump = nldev_get_dumpit,
962         },
963         [RDMA_NLDEV_CMD_PORT_GET] = {
964                 .doit = nldev_port_get_doit,
965                 .dump = nldev_port_get_dumpit,
966         },
967         [RDMA_NLDEV_CMD_RES_GET] = {
968                 .doit = nldev_res_get_doit,
969                 .dump = nldev_res_get_dumpit,
970         },
971         [RDMA_NLDEV_CMD_RES_QP_GET] = {
972                 .dump = nldev_res_get_qp_dumpit,
973                 /*
974                  * .doit is not implemented yet for two reasons:
975                  * 1. It is not needed yet.
976                  * 2. There is a need to provide identifier, while it is easy
977                  * for the QPs (device index + port index + LQPN), it is not
978                  * the case for the rest of resources (PD and CQ). Because it
979                  * is better to provide similar interface for all resources,
980                  * let's wait till we will have other resources implemented
981                  * too.
982                  */
983         },
984         [RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
985                 .dump = nldev_res_get_cm_id_dumpit,
986         },
987         [RDMA_NLDEV_CMD_RES_CQ_GET] = {
988                 .dump = nldev_res_get_cq_dumpit,
989         },
990         [RDMA_NLDEV_CMD_RES_MR_GET] = {
991                 .dump = nldev_res_get_mr_dumpit,
992         },
993         [RDMA_NLDEV_CMD_RES_PD_GET] = {
994                 .dump = nldev_res_get_pd_dumpit,
995         },
996 };
997
998 void __init nldev_init(void)
999 {
1000         rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
1001 }
1002
1003 void __exit nldev_exit(void)
1004 {
1005         rdma_nl_unregister(RDMA_NL_NLDEV);
1006 }
1007
1008 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);