blob: 4b8b0f5d534445e0de215bf05293877a9c628a46 [file] [log] [blame]
Leon Romanovsky6c80b412017-06-20 09:14:15 +03001/*
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
Leon Romanovskyb4c598a2017-06-20 09:59:14 +030033#include <net/netlink.h>
Leon Romanovsky6c80b412017-06-20 09:14:15 +030034#include <rdma/rdma_netlink.h>
35
36#include "core_priv.h"
37
Leon Romanovskyb4c598a2017-06-20 09:59:14 +030038static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
39 [RDMA_NLDEV_ATTR_DEV_INDEX] = { .type = NLA_U32 },
40 [RDMA_NLDEV_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING,
41 .len = IB_DEVICE_NAME_MAX - 1},
42 [RDMA_NLDEV_ATTR_PORT_INDEX] = { .type = NLA_U32 },
43};
44
45static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
46{
47 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
48 return -EMSGSIZE;
49 if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME, device->name))
50 return -EMSGSIZE;
51 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
52 return -EMSGSIZE;
53 return 0;
54}
55
56static int _nldev_get_dumpit(struct ib_device *device,
57 struct sk_buff *skb,
58 struct netlink_callback *cb,
59 unsigned int idx)
60{
61 int start = cb->args[0];
62 struct nlmsghdr *nlh;
63
64 if (idx < start)
65 return 0;
66
67 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
68 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
69 0, NLM_F_MULTI);
70
71 if (fill_dev_info(skb, device)) {
72 nlmsg_cancel(skb, nlh);
73 goto out;
74 }
75
76 nlmsg_end(skb, nlh);
77
78 idx++;
79
80out: cb->args[0] = idx;
81 return skb->len;
82}
83
84static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
85{
86 /*
87 * There is no need to take lock, because
88 * we are relying on ib_core's lists_rwsem
89 */
90 return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
91}
92
93static const struct rdma_nl_cbs nldev_cb_table[] = {
94 [RDMA_NLDEV_CMD_GET] = {
95 .dump = nldev_get_dumpit,
96 },
97};
98
Leon Romanovsky6c80b412017-06-20 09:14:15 +030099void __init nldev_init(void)
100{
Leon Romanovskyb4c598a2017-06-20 09:59:14 +0300101 rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
Leon Romanovsky6c80b412017-06-20 09:14:15 +0300102}
103
104void __exit nldev_exit(void)
105{
106 rdma_nl_unregister(RDMA_NL_NLDEV);
107}