Linux Audio

Check our new training course

Loading...
v6.8
   1/*
   2 * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
   3 *
   4 * Redistribution and use in source and binary forms, with or without
   5 * modification, are permitted provided that the following conditions are met:
   6 *
   7 * 1. Redistributions of source code must retain the above copyright
   8 *    notice, this list of conditions and the following disclaimer.
   9 * 2. Redistributions in binary form must reproduce the above copyright
  10 *    notice, this list of conditions and the following disclaimer in the
  11 *    documentation and/or other materials provided with the distribution.
  12 * 3. Neither the names of the copyright holders nor the names of its
  13 *    contributors may be used to endorse or promote products derived from
  14 *    this software without specific prior written permission.
  15 *
  16 * Alternatively, this software may be distributed under the terms of the
  17 * GNU General Public License ("GPL") version 2 as published by the Free
  18 * Software Foundation.
  19 *
  20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30 * POSSIBILITY OF SUCH DAMAGE.
  31 */
  32
  33#include <linux/module.h>
  34#include <linux/pid.h>
  35#include <linux/pid_namespace.h>
  36#include <linux/mutex.h>
  37#include <net/netlink.h>
  38#include <rdma/rdma_cm.h>
  39#include <rdma/rdma_netlink.h>
  40
  41#include "core_priv.h"
  42#include "cma_priv.h"
  43#include "restrack.h"
  44#include "uverbs.h"
  45
  46/*
  47 * This determines whether a non-privileged user is allowed to specify a
  48 * controlled QKEY or not, when true non-privileged user is allowed to specify
  49 * a controlled QKEY.
  50 */
  51static bool privileged_qkey;
  52
  53typedef int (*res_fill_func_t)(struct sk_buff*, bool,
  54			       struct rdma_restrack_entry*, uint32_t);
  55
  56/*
  57 * Sort array elements by the netlink attribute name
  58 */
  59static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
  60	[RDMA_NLDEV_ATTR_CHARDEV]		= { .type = NLA_U64 },
  61	[RDMA_NLDEV_ATTR_CHARDEV_ABI]		= { .type = NLA_U64 },
  62	[RDMA_NLDEV_ATTR_CHARDEV_NAME]		= { .type = NLA_NUL_STRING,
  63					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
  64	[RDMA_NLDEV_ATTR_CHARDEV_TYPE]		= { .type = NLA_NUL_STRING,
  65					.len = RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE },
  66	[RDMA_NLDEV_ATTR_DEV_DIM]               = { .type = NLA_U8 },
  67	[RDMA_NLDEV_ATTR_DEV_INDEX]		= { .type = NLA_U32 },
  68	[RDMA_NLDEV_ATTR_DEV_NAME]		= { .type = NLA_NUL_STRING,
  69					.len = IB_DEVICE_NAME_MAX },
  70	[RDMA_NLDEV_ATTR_DEV_NODE_TYPE]		= { .type = NLA_U8 },
  71	[RDMA_NLDEV_ATTR_DEV_PROTOCOL]		= { .type = NLA_NUL_STRING,
  72					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
  73	[RDMA_NLDEV_ATTR_DRIVER]		= { .type = NLA_NESTED },
  74	[RDMA_NLDEV_ATTR_DRIVER_ENTRY]		= { .type = NLA_NESTED },
  75	[RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE]	= { .type = NLA_U8 },
  76	[RDMA_NLDEV_ATTR_DRIVER_STRING]		= { .type = NLA_NUL_STRING,
  77					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
  78	[RDMA_NLDEV_ATTR_DRIVER_S32]		= { .type = NLA_S32 },
  79	[RDMA_NLDEV_ATTR_DRIVER_S64]		= { .type = NLA_S64 },
  80	[RDMA_NLDEV_ATTR_DRIVER_U32]		= { .type = NLA_U32 },
  81	[RDMA_NLDEV_ATTR_DRIVER_U64]		= { .type = NLA_U64 },
  82	[RDMA_NLDEV_ATTR_FW_VERSION]		= { .type = NLA_NUL_STRING,
  83					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
  84	[RDMA_NLDEV_ATTR_LID]			= { .type = NLA_U32 },
  85	[RDMA_NLDEV_ATTR_LINK_TYPE]		= { .type = NLA_NUL_STRING,
  86					.len = IFNAMSIZ },
  87	[RDMA_NLDEV_ATTR_LMC]			= { .type = NLA_U8 },
  88	[RDMA_NLDEV_ATTR_NDEV_INDEX]		= { .type = NLA_U32 },
  89	[RDMA_NLDEV_ATTR_NDEV_NAME]		= { .type = NLA_NUL_STRING,
  90					.len = IFNAMSIZ },
  91	[RDMA_NLDEV_ATTR_NODE_GUID]		= { .type = NLA_U64 },
  92	[RDMA_NLDEV_ATTR_PORT_INDEX]		= { .type = NLA_U32 },
  93	[RDMA_NLDEV_ATTR_PORT_PHYS_STATE]	= { .type = NLA_U8 },
  94	[RDMA_NLDEV_ATTR_PORT_STATE]		= { .type = NLA_U8 },
  95	[RDMA_NLDEV_ATTR_RES_CM_ID]		= { .type = NLA_NESTED },
  96	[RDMA_NLDEV_ATTR_RES_CM_IDN]		= { .type = NLA_U32 },
  97	[RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY]	= { .type = NLA_NESTED },
  98	[RDMA_NLDEV_ATTR_RES_CQ]		= { .type = NLA_NESTED },
  99	[RDMA_NLDEV_ATTR_RES_CQE]		= { .type = NLA_U32 },
 100	[RDMA_NLDEV_ATTR_RES_CQN]		= { .type = NLA_U32 },
 101	[RDMA_NLDEV_ATTR_RES_CQ_ENTRY]		= { .type = NLA_NESTED },
 102	[RDMA_NLDEV_ATTR_RES_CTX]		= { .type = NLA_NESTED },
 103	[RDMA_NLDEV_ATTR_RES_CTXN]		= { .type = NLA_U32 },
 104	[RDMA_NLDEV_ATTR_RES_CTX_ENTRY]		= { .type = NLA_NESTED },
 105	[RDMA_NLDEV_ATTR_RES_DST_ADDR]		= {
 106			.len = sizeof(struct __kernel_sockaddr_storage) },
 107	[RDMA_NLDEV_ATTR_RES_IOVA]		= { .type = NLA_U64 },
 108	[RDMA_NLDEV_ATTR_RES_KERN_NAME]		= { .type = NLA_NUL_STRING,
 109					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
 110	[RDMA_NLDEV_ATTR_RES_LKEY]		= { .type = NLA_U32 },
 111	[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]	= { .type = NLA_U32 },
 112	[RDMA_NLDEV_ATTR_RES_LQPN]		= { .type = NLA_U32 },
 113	[RDMA_NLDEV_ATTR_RES_MR]		= { .type = NLA_NESTED },
 114	[RDMA_NLDEV_ATTR_RES_MRLEN]		= { .type = NLA_U64 },
 115	[RDMA_NLDEV_ATTR_RES_MRN]		= { .type = NLA_U32 },
 116	[RDMA_NLDEV_ATTR_RES_MR_ENTRY]		= { .type = NLA_NESTED },
 117	[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]	= { .type = NLA_U8 },
 118	[RDMA_NLDEV_ATTR_RES_PD]		= { .type = NLA_NESTED },
 119	[RDMA_NLDEV_ATTR_RES_PDN]		= { .type = NLA_U32 },
 120	[RDMA_NLDEV_ATTR_RES_PD_ENTRY]		= { .type = NLA_NESTED },
 121	[RDMA_NLDEV_ATTR_RES_PID]		= { .type = NLA_U32 },
 122	[RDMA_NLDEV_ATTR_RES_POLL_CTX]		= { .type = NLA_U8 },
 123	[RDMA_NLDEV_ATTR_RES_PS]		= { .type = NLA_U32 },
 124	[RDMA_NLDEV_ATTR_RES_QP]		= { .type = NLA_NESTED },
 125	[RDMA_NLDEV_ATTR_RES_QP_ENTRY]		= { .type = NLA_NESTED },
 126	[RDMA_NLDEV_ATTR_RES_RAW]		= { .type = NLA_BINARY },
 127	[RDMA_NLDEV_ATTR_RES_RKEY]		= { .type = NLA_U32 },
 128	[RDMA_NLDEV_ATTR_RES_RQPN]		= { .type = NLA_U32 },
 129	[RDMA_NLDEV_ATTR_RES_RQ_PSN]		= { .type = NLA_U32 },
 130	[RDMA_NLDEV_ATTR_RES_SQ_PSN]		= { .type = NLA_U32 },
 131	[RDMA_NLDEV_ATTR_RES_SRC_ADDR]		= {
 132			.len = sizeof(struct __kernel_sockaddr_storage) },
 133	[RDMA_NLDEV_ATTR_RES_STATE]		= { .type = NLA_U8 },
 134	[RDMA_NLDEV_ATTR_RES_SUMMARY]		= { .type = NLA_NESTED },
 135	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY]	= { .type = NLA_NESTED },
 136	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]= { .type = NLA_U64 },
 137	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]= { .type = NLA_NUL_STRING,
 138					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
 139	[RDMA_NLDEV_ATTR_RES_TYPE]		= { .type = NLA_U8 },
 140	[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]= { .type = NLA_U32 },
 141	[RDMA_NLDEV_ATTR_RES_USECNT]		= { .type = NLA_U64 },
 142	[RDMA_NLDEV_ATTR_RES_SRQ]		= { .type = NLA_NESTED },
 143	[RDMA_NLDEV_ATTR_RES_SRQN]		= { .type = NLA_U32 },
 144	[RDMA_NLDEV_ATTR_RES_SRQ_ENTRY]		= { .type = NLA_NESTED },
 145	[RDMA_NLDEV_ATTR_MIN_RANGE]		= { .type = NLA_U32 },
 146	[RDMA_NLDEV_ATTR_MAX_RANGE]		= { .type = NLA_U32 },
 147	[RDMA_NLDEV_ATTR_SM_LID]		= { .type = NLA_U32 },
 148	[RDMA_NLDEV_ATTR_SUBNET_PREFIX]		= { .type = NLA_U64 },
 149	[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]	= { .type = NLA_U32 },
 150	[RDMA_NLDEV_ATTR_STAT_MODE]		= { .type = NLA_U32 },
 151	[RDMA_NLDEV_ATTR_STAT_RES]		= { .type = NLA_U32 },
 152	[RDMA_NLDEV_ATTR_STAT_COUNTER]		= { .type = NLA_NESTED },
 153	[RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY]	= { .type = NLA_NESTED },
 154	[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]       = { .type = NLA_U32 },
 155	[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]       = { .type = NLA_NESTED },
 156	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY]  = { .type = NLA_NESTED },
 157	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME] = { .type = NLA_NUL_STRING },
 158	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE] = { .type = NLA_U64 },
 159	[RDMA_NLDEV_ATTR_SYS_IMAGE_GUID]	= { .type = NLA_U64 },
 160	[RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID]	= { .type = NLA_U32 },
 161	[RDMA_NLDEV_NET_NS_FD]			= { .type = NLA_U32 },
 162	[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]	= { .type = NLA_U8 },
 163	[RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK]	= { .type = NLA_U8 },
 164	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_INDEX]	= { .type = NLA_U32 },
 165	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_DYNAMIC] = { .type = NLA_U8 },
 166	[RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE] = { .type = NLA_U8 },
 167};
 168
 169static int put_driver_name_print_type(struct sk_buff *msg, const char *name,
 170				      enum rdma_nldev_print_type print_type)
 171{
 172	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name))
 173		return -EMSGSIZE;
 174	if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC &&
 175	    nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type))
 176		return -EMSGSIZE;
 177
 178	return 0;
 179}
 180
 181static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name,
 182				   enum rdma_nldev_print_type print_type,
 183				   u32 value)
 184{
 185	if (put_driver_name_print_type(msg, name, print_type))
 186		return -EMSGSIZE;
 187	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value))
 188		return -EMSGSIZE;
 189
 190	return 0;
 191}
 192
 193static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name,
 194				   enum rdma_nldev_print_type print_type,
 195				   u64 value)
 196{
 197	if (put_driver_name_print_type(msg, name, print_type))
 198		return -EMSGSIZE;
 199	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value,
 200			      RDMA_NLDEV_ATTR_PAD))
 201		return -EMSGSIZE;
 202
 203	return 0;
 204}
 205
 206int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
 207			      const char *str)
 208{
 209	if (put_driver_name_print_type(msg, name,
 210				       RDMA_NLDEV_PRINT_TYPE_UNSPEC))
 211		return -EMSGSIZE;
 212	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, str))
 213		return -EMSGSIZE;
 214
 215	return 0;
 216}
 217EXPORT_SYMBOL(rdma_nl_put_driver_string);
 218
 219int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value)
 220{
 221	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
 222				       value);
 223}
 224EXPORT_SYMBOL(rdma_nl_put_driver_u32);
 225
 226int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
 227			       u32 value)
 228{
 229	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
 230				       value);
 231}
 232EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex);
 233
 234int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value)
 235{
 236	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
 237				       value);
 238}
 239EXPORT_SYMBOL(rdma_nl_put_driver_u64);
 240
 241int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value)
 242{
 243	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
 244				       value);
 245}
 246EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex);
 247
 248bool rdma_nl_get_privileged_qkey(void)
 249{
 250	return privileged_qkey || capable(CAP_NET_RAW);
 251}
 252EXPORT_SYMBOL(rdma_nl_get_privileged_qkey);
 253
 254static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
 255{
 256	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
 257		return -EMSGSIZE;
 258	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME,
 259			   dev_name(&device->dev)))
 260		return -EMSGSIZE;
 261
 262	return 0;
 263}
 264
 265static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
 266{
 267	char fw[IB_FW_VERSION_NAME_MAX];
 268	int ret = 0;
 269	u32 port;
 270
 271	if (fill_nldev_handle(msg, device))
 272		return -EMSGSIZE;
 273
 274	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
 275		return -EMSGSIZE;
 276
 277	BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
 278	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
 279			      device->attrs.device_cap_flags,
 280			      RDMA_NLDEV_ATTR_PAD))
 281		return -EMSGSIZE;
 282
 283	ib_get_device_fw_str(device, fw);
 284	/* Device without FW has strlen(fw) = 0 */
 285	if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
 286		return -EMSGSIZE;
 287
 288	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
 289			      be64_to_cpu(device->node_guid),
 290			      RDMA_NLDEV_ATTR_PAD))
 291		return -EMSGSIZE;
 292	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
 293			      be64_to_cpu(device->attrs.sys_image_guid),
 294			      RDMA_NLDEV_ATTR_PAD))
 295		return -EMSGSIZE;
 296	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
 297		return -EMSGSIZE;
 298	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, device->use_cq_dim))
 299		return -EMSGSIZE;
 300
 301	/*
 302	 * Link type is determined on first port and mlx4 device
 303	 * which can potentially have two different link type for the same
 304	 * IB device is considered as better to be avoided in the future,
 305	 */
 306	port = rdma_start_port(device);
 307	if (rdma_cap_opa_mad(device, port))
 308		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "opa");
 309	else if (rdma_protocol_ib(device, port))
 310		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "ib");
 311	else if (rdma_protocol_iwarp(device, port))
 312		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "iw");
 313	else if (rdma_protocol_roce(device, port))
 314		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "roce");
 315	else if (rdma_protocol_usnic(device, port))
 316		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL,
 317				     "usnic");
 318	return ret;
 319}
 320
 321static int fill_port_info(struct sk_buff *msg,
 322			  struct ib_device *device, u32 port,
 323			  const struct net *net)
 324{
 325	struct net_device *netdev = NULL;
 326	struct ib_port_attr attr;
 327	int ret;
 328	u64 cap_flags = 0;
 329
 330	if (fill_nldev_handle(msg, device))
 331		return -EMSGSIZE;
 332
 333	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
 334		return -EMSGSIZE;
 335
 336	ret = ib_query_port(device, port, &attr);
 337	if (ret)
 338		return ret;
 339
 340	if (rdma_protocol_ib(device, port)) {
 341		BUILD_BUG_ON((sizeof(attr.port_cap_flags) +
 342				sizeof(attr.port_cap_flags2)) > sizeof(u64));
 343		cap_flags = attr.port_cap_flags |
 344			((u64)attr.port_cap_flags2 << 32);
 345		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
 346				      cap_flags, RDMA_NLDEV_ATTR_PAD))
 347			return -EMSGSIZE;
 348		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
 349				      attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD))
 350			return -EMSGSIZE;
 351		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
 352			return -EMSGSIZE;
 353		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
 354			return -EMSGSIZE;
 355		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
 356			return -EMSGSIZE;
 357	}
 358	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
 359		return -EMSGSIZE;
 360	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
 361		return -EMSGSIZE;
 362
 363	netdev = ib_device_get_netdev(device, port);
 364	if (netdev && net_eq(dev_net(netdev), net)) {
 365		ret = nla_put_u32(msg,
 366				  RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
 367		if (ret)
 368			goto out;
 369		ret = nla_put_string(msg,
 370				     RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
 371	}
 372
 373out:
 374	dev_put(netdev);
 
 375	return ret;
 376}
 377
 378static int fill_res_info_entry(struct sk_buff *msg,
 379			       const char *name, u64 curr)
 380{
 381	struct nlattr *entry_attr;
 382
 383	entry_attr = nla_nest_start_noflag(msg,
 384					   RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
 385	if (!entry_attr)
 386		return -EMSGSIZE;
 387
 388	if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
 389		goto err;
 390	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr,
 391			      RDMA_NLDEV_ATTR_PAD))
 392		goto err;
 393
 394	nla_nest_end(msg, entry_attr);
 395	return 0;
 396
 397err:
 398	nla_nest_cancel(msg, entry_attr);
 399	return -EMSGSIZE;
 400}
 401
 402static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
 403{
 404	static const char * const names[RDMA_RESTRACK_MAX] = {
 405		[RDMA_RESTRACK_PD] = "pd",
 406		[RDMA_RESTRACK_CQ] = "cq",
 407		[RDMA_RESTRACK_QP] = "qp",
 408		[RDMA_RESTRACK_CM_ID] = "cm_id",
 409		[RDMA_RESTRACK_MR] = "mr",
 410		[RDMA_RESTRACK_CTX] = "ctx",
 411		[RDMA_RESTRACK_SRQ] = "srq",
 412	};
 413
 414	struct nlattr *table_attr;
 415	int ret, i, curr;
 416
 417	if (fill_nldev_handle(msg, device))
 418		return -EMSGSIZE;
 419
 420	table_attr = nla_nest_start_noflag(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
 421	if (!table_attr)
 422		return -EMSGSIZE;
 423
 424	for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
 425		if (!names[i])
 426			continue;
 427		curr = rdma_restrack_count(device, i);
 428		ret = fill_res_info_entry(msg, names[i], curr);
 429		if (ret)
 430			goto err;
 431	}
 432
 433	nla_nest_end(msg, table_attr);
 434	return 0;
 435
 436err:
 437	nla_nest_cancel(msg, table_attr);
 438	return ret;
 439}
 440
 441static int fill_res_name_pid(struct sk_buff *msg,
 442			     struct rdma_restrack_entry *res)
 443{
 444	int err = 0;
 445
 446	/*
 447	 * For user resources, user is should read /proc/PID/comm to get the
 448	 * name of the task file.
 449	 */
 450	if (rdma_is_kernel_res(res)) {
 451		err = nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
 452				     res->kern_name);
 453	} else {
 454		pid_t pid;
 455
 456		pid = task_pid_vnr(res->task);
 457		/*
 458		 * Task is dead and in zombie state.
 459		 * There is no need to print PID anymore.
 460		 */
 461		if (pid)
 462			/*
 463			 * This part is racy, task can be killed and PID will
 464			 * be zero right here but it is ok, next query won't
 465			 * return PID. We don't promise real-time reflection
 466			 * of SW objects.
 467			 */
 468			err = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID, pid);
 469	}
 470
 471	return err ? -EMSGSIZE : 0;
 472}
 473
 474static int fill_res_qp_entry_query(struct sk_buff *msg,
 475				   struct rdma_restrack_entry *res,
 476				   struct ib_device *dev,
 477				   struct ib_qp *qp)
 478{
 479	struct ib_qp_init_attr qp_init_attr;
 480	struct ib_qp_attr qp_attr;
 481	int ret;
 482
 483	ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
 484	if (ret)
 485		return ret;
 486
 487	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
 488		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
 489				qp_attr.dest_qp_num))
 490			goto err;
 491		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
 492				qp_attr.rq_psn))
 493			goto err;
 494	}
 495
 496	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
 497		goto err;
 498
 499	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
 500	    qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
 501		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
 502			       qp_attr.path_mig_state))
 503			goto err;
 504	}
 505	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
 506		goto err;
 507	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
 508		goto err;
 509
 510	if (dev->ops.fill_res_qp_entry)
 511		return dev->ops.fill_res_qp_entry(msg, qp);
 512	return 0;
 513
 514err:	return -EMSGSIZE;
 515}
 516
 517static int fill_res_qp_entry(struct sk_buff *msg, bool has_cap_net_admin,
 518			     struct rdma_restrack_entry *res, uint32_t port)
 519{
 520	struct ib_qp *qp = container_of(res, struct ib_qp, res);
 521	struct ib_device *dev = qp->device;
 522	int ret;
 523
 524	if (port && port != qp->port)
 525		return -EAGAIN;
 526
 527	/* In create_qp() port is not set yet */
 528	if (qp->port && nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp->port))
 529		return -EMSGSIZE;
 530
 531	ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num);
 532	if (ret)
 533		return -EMSGSIZE;
 534
 535	if (!rdma_is_kernel_res(res) &&
 536	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, qp->pd->res.id))
 537		return -EMSGSIZE;
 538
 539	ret = fill_res_name_pid(msg, res);
 540	if (ret)
 541		return -EMSGSIZE;
 542
 543	return fill_res_qp_entry_query(msg, res, dev, qp);
 544}
 545
 546static int fill_res_qp_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
 547				 struct rdma_restrack_entry *res, uint32_t port)
 548{
 549	struct ib_qp *qp = container_of(res, struct ib_qp, res);
 550	struct ib_device *dev = qp->device;
 551
 552	if (port && port != qp->port)
 553		return -EAGAIN;
 554	if (!dev->ops.fill_res_qp_entry_raw)
 555		return -EINVAL;
 556	return dev->ops.fill_res_qp_entry_raw(msg, qp);
 557}
 558
 559static int fill_res_cm_id_entry(struct sk_buff *msg, bool has_cap_net_admin,
 560				struct rdma_restrack_entry *res, uint32_t port)
 561{
 562	struct rdma_id_private *id_priv =
 563				container_of(res, struct rdma_id_private, res);
 564	struct ib_device *dev = id_priv->id.device;
 565	struct rdma_cm_id *cm_id = &id_priv->id;
 566
 567	if (port && port != cm_id->port_num)
 568		return -EAGAIN;
 569
 570	if (cm_id->port_num &&
 571	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
 572		goto err;
 573
 574	if (id_priv->qp_num) {
 575		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
 576			goto err;
 577		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
 578			goto err;
 579	}
 580
 581	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
 582		goto err;
 583
 584	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
 585		goto err;
 586
 587	if (cm_id->route.addr.src_addr.ss_family &&
 588	    nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
 589		    sizeof(cm_id->route.addr.src_addr),
 590		    &cm_id->route.addr.src_addr))
 591		goto err;
 592	if (cm_id->route.addr.dst_addr.ss_family &&
 593	    nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
 594		    sizeof(cm_id->route.addr.dst_addr),
 595		    &cm_id->route.addr.dst_addr))
 596		goto err;
 597
 598	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CM_IDN, res->id))
 599		goto err;
 600
 601	if (fill_res_name_pid(msg, res))
 602		goto err;
 603
 604	if (dev->ops.fill_res_cm_id_entry)
 605		return dev->ops.fill_res_cm_id_entry(msg, cm_id);
 606	return 0;
 607
 608err: return -EMSGSIZE;
 609}
 610
 611static int fill_res_cq_entry(struct sk_buff *msg, bool has_cap_net_admin,
 612			     struct rdma_restrack_entry *res, uint32_t port)
 613{
 614	struct ib_cq *cq = container_of(res, struct ib_cq, res);
 615	struct ib_device *dev = cq->device;
 616
 617	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
 618		return -EMSGSIZE;
 619	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
 620			      atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD))
 621		return -EMSGSIZE;
 622
 623	/* Poll context is only valid for kernel CQs */
 624	if (rdma_is_kernel_res(res) &&
 625	    nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
 626		return -EMSGSIZE;
 627
 628	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, (cq->dim != NULL)))
 629		return -EMSGSIZE;
 630
 631	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN, res->id))
 632		return -EMSGSIZE;
 633	if (!rdma_is_kernel_res(res) &&
 634	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
 635			cq->uobject->uevent.uobject.context->res.id))
 636		return -EMSGSIZE;
 637
 638	if (fill_res_name_pid(msg, res))
 639		return -EMSGSIZE;
 640
 641	return (dev->ops.fill_res_cq_entry) ?
 642		dev->ops.fill_res_cq_entry(msg, cq) : 0;
 643}
 644
 645static int fill_res_cq_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
 646				 struct rdma_restrack_entry *res, uint32_t port)
 647{
 648	struct ib_cq *cq = container_of(res, struct ib_cq, res);
 649	struct ib_device *dev = cq->device;
 650
 651	if (!dev->ops.fill_res_cq_entry_raw)
 652		return -EINVAL;
 653	return dev->ops.fill_res_cq_entry_raw(msg, cq);
 654}
 655
 656static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
 657			     struct rdma_restrack_entry *res, uint32_t port)
 658{
 659	struct ib_mr *mr = container_of(res, struct ib_mr, res);
 660	struct ib_device *dev = mr->pd->device;
 661
 662	if (has_cap_net_admin) {
 663		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
 664			return -EMSGSIZE;
 665		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
 666			return -EMSGSIZE;
 667	}
 668
 669	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length,
 670			      RDMA_NLDEV_ATTR_PAD))
 671		return -EMSGSIZE;
 672
 673	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
 674		return -EMSGSIZE;
 675
 676	if (!rdma_is_kernel_res(res) &&
 677	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, mr->pd->res.id))
 678		return -EMSGSIZE;
 679
 680	if (fill_res_name_pid(msg, res))
 681		return -EMSGSIZE;
 682
 683	return (dev->ops.fill_res_mr_entry) ?
 684		       dev->ops.fill_res_mr_entry(msg, mr) :
 685		       0;
 686}
 687
 688static int fill_res_mr_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
 689				 struct rdma_restrack_entry *res, uint32_t port)
 690{
 691	struct ib_mr *mr = container_of(res, struct ib_mr, res);
 692	struct ib_device *dev = mr->pd->device;
 693
 694	if (!dev->ops.fill_res_mr_entry_raw)
 695		return -EINVAL;
 696	return dev->ops.fill_res_mr_entry_raw(msg, mr);
 697}
 698
 699static int fill_res_pd_entry(struct sk_buff *msg, bool has_cap_net_admin,
 700			     struct rdma_restrack_entry *res, uint32_t port)
 701{
 702	struct ib_pd *pd = container_of(res, struct ib_pd, res);
 703
 704	if (has_cap_net_admin) {
 705		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
 706				pd->local_dma_lkey))
 707			goto err;
 708		if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
 709		    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
 710				pd->unsafe_global_rkey))
 711			goto err;
 712	}
 713	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
 714			      atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD))
 715		goto err;
 716
 717	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, res->id))
 718		goto err;
 719
 720	if (!rdma_is_kernel_res(res) &&
 721	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
 722			pd->uobject->context->res.id))
 723		goto err;
 724
 725	return fill_res_name_pid(msg, res);
 726
 727err:	return -EMSGSIZE;
 728}
 729
 730static int fill_res_ctx_entry(struct sk_buff *msg, bool has_cap_net_admin,
 731			      struct rdma_restrack_entry *res, uint32_t port)
 732{
 733	struct ib_ucontext *ctx = container_of(res, struct ib_ucontext, res);
 734
 735	if (rdma_is_kernel_res(res))
 736		return 0;
 737
 738	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN, ctx->res.id))
 739		return -EMSGSIZE;
 740
 741	return fill_res_name_pid(msg, res);
 742}
 743
 744static int fill_res_range_qp_entry(struct sk_buff *msg, uint32_t min_range,
 745				   uint32_t max_range)
 746{
 747	struct nlattr *entry_attr;
 748
 749	if (!min_range)
 750		return 0;
 751
 752	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
 753	if (!entry_attr)
 754		return -EMSGSIZE;
 755
 756	if (min_range == max_range) {
 757		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, min_range))
 758			goto err;
 759	} else {
 760		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MIN_RANGE, min_range))
 761			goto err;
 762		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MAX_RANGE, max_range))
 763			goto err;
 764	}
 765	nla_nest_end(msg, entry_attr);
 766	return 0;
 767
 768err:
 769	nla_nest_cancel(msg, entry_attr);
 770	return -EMSGSIZE;
 771}
 772
 773static int fill_res_srq_qps(struct sk_buff *msg, struct ib_srq *srq)
 774{
 775	uint32_t min_range = 0, prev = 0;
 776	struct rdma_restrack_entry *res;
 777	struct rdma_restrack_root *rt;
 778	struct nlattr *table_attr;
 779	struct ib_qp *qp = NULL;
 780	unsigned long id = 0;
 781
 782	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
 783	if (!table_attr)
 784		return -EMSGSIZE;
 785
 786	rt = &srq->device->res[RDMA_RESTRACK_QP];
 787	xa_lock(&rt->xa);
 788	xa_for_each(&rt->xa, id, res) {
 789		if (!rdma_restrack_get(res))
 790			continue;
 791
 792		qp = container_of(res, struct ib_qp, res);
 793		if (!qp->srq || (qp->srq->res.id != srq->res.id)) {
 794			rdma_restrack_put(res);
 795			continue;
 796		}
 797
 798		if (qp->qp_num < prev)
 799			/* qp_num should be ascending */
 800			goto err_loop;
 801
 802		if (min_range == 0) {
 803			min_range = qp->qp_num;
 804		} else if (qp->qp_num > (prev + 1)) {
 805			if (fill_res_range_qp_entry(msg, min_range, prev))
 806				goto err_loop;
 807
 808			min_range = qp->qp_num;
 809		}
 810		prev = qp->qp_num;
 811		rdma_restrack_put(res);
 812	}
 813
 814	xa_unlock(&rt->xa);
 815
 816	if (fill_res_range_qp_entry(msg, min_range, prev))
 817		goto err;
 818
 819	nla_nest_end(msg, table_attr);
 820	return 0;
 821
 822err_loop:
 823	rdma_restrack_put(res);
 824	xa_unlock(&rt->xa);
 825err:
 826	nla_nest_cancel(msg, table_attr);
 827	return -EMSGSIZE;
 828}
 829
 830static int fill_res_srq_entry(struct sk_buff *msg, bool has_cap_net_admin,
 831			      struct rdma_restrack_entry *res, uint32_t port)
 832{
 833	struct ib_srq *srq = container_of(res, struct ib_srq, res);
 834	struct ib_device *dev = srq->device;
 835
 836	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SRQN, srq->res.id))
 837		goto err;
 838
 839	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, srq->srq_type))
 840		goto err;
 841
 842	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, srq->pd->res.id))
 843		goto err;
 844
 845	if (ib_srq_has_cq(srq->srq_type)) {
 846		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN,
 847				srq->ext.cq->res.id))
 848			goto err;
 849	}
 850
 851	if (fill_res_srq_qps(msg, srq))
 852		goto err;
 853
 854	if (fill_res_name_pid(msg, res))
 855		goto err;
 856
 857	if (dev->ops.fill_res_srq_entry)
 858		return dev->ops.fill_res_srq_entry(msg, srq);
 859
 860	return 0;
 861
 862err:
 863	return -EMSGSIZE;
 864}
 865
 866static int fill_res_srq_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
 867				 struct rdma_restrack_entry *res, uint32_t port)
 868{
 869	struct ib_srq *srq = container_of(res, struct ib_srq, res);
 870	struct ib_device *dev = srq->device;
 871
 872	if (!dev->ops.fill_res_srq_entry_raw)
 873		return -EINVAL;
 874	return dev->ops.fill_res_srq_entry_raw(msg, srq);
 875}
 876
 877static int fill_stat_counter_mode(struct sk_buff *msg,
 878				  struct rdma_counter *counter)
 879{
 880	struct rdma_counter_mode *m = &counter->mode;
 881
 882	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, m->mode))
 883		return -EMSGSIZE;
 884
 885	if (m->mode == RDMA_COUNTER_MODE_AUTO) {
 886		if ((m->mask & RDMA_COUNTER_MASK_QP_TYPE) &&
 887		    nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, m->param.qp_type))
 888			return -EMSGSIZE;
 889
 890		if ((m->mask & RDMA_COUNTER_MASK_PID) &&
 891		    fill_res_name_pid(msg, &counter->res))
 892			return -EMSGSIZE;
 893	}
 894
 895	return 0;
 896}
 897
 898static int fill_stat_counter_qp_entry(struct sk_buff *msg, u32 qpn)
 899{
 900	struct nlattr *entry_attr;
 901
 902	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
 903	if (!entry_attr)
 904		return -EMSGSIZE;
 905
 906	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn))
 907		goto err;
 908
 909	nla_nest_end(msg, entry_attr);
 910	return 0;
 911
 912err:
 913	nla_nest_cancel(msg, entry_attr);
 914	return -EMSGSIZE;
 915}
 916
 917static int fill_stat_counter_qps(struct sk_buff *msg,
 918				 struct rdma_counter *counter)
 919{
 920	struct rdma_restrack_entry *res;
 921	struct rdma_restrack_root *rt;
 922	struct nlattr *table_attr;
 923	struct ib_qp *qp = NULL;
 924	unsigned long id = 0;
 925	int ret = 0;
 926
 927	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
 928	if (!table_attr)
 929		return -EMSGSIZE;
 930
 931	rt = &counter->device->res[RDMA_RESTRACK_QP];
 932	xa_lock(&rt->xa);
 933	xa_for_each(&rt->xa, id, res) {
 934		qp = container_of(res, struct ib_qp, res);
 935		if (!qp->counter || (qp->counter->id != counter->id))
 936			continue;
 937
 938		ret = fill_stat_counter_qp_entry(msg, qp->qp_num);
 939		if (ret)
 940			goto err;
 941	}
 942
 943	xa_unlock(&rt->xa);
 944	nla_nest_end(msg, table_attr);
 945	return 0;
 946
 947err:
 948	xa_unlock(&rt->xa);
 949	nla_nest_cancel(msg, table_attr);
 950	return ret;
 951}
 952
 953int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
 954				 u64 value)
 955{
 956	struct nlattr *entry_attr;
 957
 958	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY);
 959	if (!entry_attr)
 960		return -EMSGSIZE;
 961
 962	if (nla_put_string(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME,
 963			   name))
 964		goto err;
 965	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE,
 966			      value, RDMA_NLDEV_ATTR_PAD))
 967		goto err;
 968
 969	nla_nest_end(msg, entry_attr);
 970	return 0;
 971
 972err:
 973	nla_nest_cancel(msg, entry_attr);
 974	return -EMSGSIZE;
 975}
 976EXPORT_SYMBOL(rdma_nl_stat_hwcounter_entry);
 977
 978static int fill_stat_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
 979			      struct rdma_restrack_entry *res, uint32_t port)
 980{
 981	struct ib_mr *mr = container_of(res, struct ib_mr, res);
 982	struct ib_device *dev = mr->pd->device;
 983
 984	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
 985		goto err;
 986
 987	if (dev->ops.fill_stat_mr_entry)
 988		return dev->ops.fill_stat_mr_entry(msg, mr);
 989	return 0;
 990
 991err:
 992	return -EMSGSIZE;
 993}
 994
 995static int fill_stat_counter_hwcounters(struct sk_buff *msg,
 996					struct rdma_counter *counter)
 997{
 998	struct rdma_hw_stats *st = counter->stats;
 999	struct nlattr *table_attr;
1000	int i;
1001
1002	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
1003	if (!table_attr)
1004		return -EMSGSIZE;
1005
1006	mutex_lock(&st->lock);
1007	for (i = 0; i < st->num_counters; i++) {
1008		if (test_bit(i, st->is_disabled))
1009			continue;
1010		if (rdma_nl_stat_hwcounter_entry(msg, st->descs[i].name,
1011						 st->value[i]))
1012			goto err;
1013	}
1014	mutex_unlock(&st->lock);
1015
1016	nla_nest_end(msg, table_attr);
1017	return 0;
1018
1019err:
1020	mutex_unlock(&st->lock);
1021	nla_nest_cancel(msg, table_attr);
1022	return -EMSGSIZE;
1023}
1024
1025static int fill_res_counter_entry(struct sk_buff *msg, bool has_cap_net_admin,
1026				  struct rdma_restrack_entry *res,
1027				  uint32_t port)
1028{
1029	struct rdma_counter *counter =
1030		container_of(res, struct rdma_counter, res);
1031
1032	if (port && port != counter->port)
1033		return -EAGAIN;
1034
1035	/* Dump it even query failed */
1036	rdma_counter_query_stats(counter);
1037
1038	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, counter->port) ||
1039	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, counter->id) ||
1040	    fill_stat_counter_mode(msg, counter) ||
1041	    fill_stat_counter_qps(msg, counter) ||
1042	    fill_stat_counter_hwcounters(msg, counter))
1043		return -EMSGSIZE;
1044
1045	return 0;
1046}
1047
1048static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1049			  struct netlink_ext_ack *extack)
1050{
1051	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1052	struct ib_device *device;
1053	struct sk_buff *msg;
1054	u32 index;
1055	int err;
1056
1057	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1058				     nldev_policy, extack);
1059	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1060		return -EINVAL;
1061
1062	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1063
1064	device = ib_device_get_by_index(sock_net(skb->sk), index);
1065	if (!device)
1066		return -EINVAL;
1067
1068	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1069	if (!msg) {
1070		err = -ENOMEM;
1071		goto err;
1072	}
1073
1074	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1075			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1076			0, 0);
1077	if (!nlh) {
1078		err = -EMSGSIZE;
1079		goto err_free;
1080	}
1081
1082	err = fill_dev_info(msg, device);
1083	if (err)
1084		goto err_free;
1085
1086	nlmsg_end(msg, nlh);
1087
1088	ib_device_put(device);
1089	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1090
1091err_free:
1092	nlmsg_free(msg);
1093err:
1094	ib_device_put(device);
1095	return err;
1096}
1097
1098static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1099			  struct netlink_ext_ack *extack)
1100{
1101	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1102	struct ib_device *device;
1103	u32 index;
1104	int err;
1105
1106	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1107				     nldev_policy, extack);
1108	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1109		return -EINVAL;
1110
1111	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1112	device = ib_device_get_by_index(sock_net(skb->sk), index);
1113	if (!device)
1114		return -EINVAL;
1115
1116	if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) {
1117		char name[IB_DEVICE_NAME_MAX] = {};
1118
1119		nla_strscpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1120			    IB_DEVICE_NAME_MAX);
1121		if (strlen(name) == 0) {
1122			err = -EINVAL;
1123			goto done;
1124		}
1125		err = ib_device_rename(device, name);
1126		goto done;
1127	}
1128
1129	if (tb[RDMA_NLDEV_NET_NS_FD]) {
1130		u32 ns_fd;
1131
1132		ns_fd = nla_get_u32(tb[RDMA_NLDEV_NET_NS_FD]);
1133		err = ib_device_set_netns_put(skb, device, ns_fd);
1134		goto put_done;
1135	}
1136
1137	if (tb[RDMA_NLDEV_ATTR_DEV_DIM]) {
1138		u8 use_dim;
1139
1140		use_dim = nla_get_u8(tb[RDMA_NLDEV_ATTR_DEV_DIM]);
1141		err = ib_device_set_dim(device,  use_dim);
1142		goto done;
1143	}
1144
1145done:
1146	ib_device_put(device);
1147put_done:
1148	return err;
1149}
1150
1151static int _nldev_get_dumpit(struct ib_device *device,
1152			     struct sk_buff *skb,
1153			     struct netlink_callback *cb,
1154			     unsigned int idx)
1155{
1156	int start = cb->args[0];
1157	struct nlmsghdr *nlh;
1158
1159	if (idx < start)
1160		return 0;
1161
1162	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1163			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1164			0, NLM_F_MULTI);
1165
1166	if (!nlh || fill_dev_info(skb, device)) {
1167		nlmsg_cancel(skb, nlh);
1168		goto out;
1169	}
1170
1171	nlmsg_end(skb, nlh);
1172
1173	idx++;
1174
1175out:	cb->args[0] = idx;
1176	return skb->len;
1177}
1178
1179static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
1180{
1181	/*
1182	 * There is no need to take lock, because
1183	 * we are relying on ib_core's locking.
1184	 */
1185	return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
1186}
1187
1188static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1189			       struct netlink_ext_ack *extack)
1190{
1191	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1192	struct ib_device *device;
1193	struct sk_buff *msg;
1194	u32 index;
1195	u32 port;
1196	int err;
1197
1198	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1199				     nldev_policy, extack);
1200	if (err ||
1201	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
1202	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
1203		return -EINVAL;
1204
1205	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1206	device = ib_device_get_by_index(sock_net(skb->sk), index);
1207	if (!device)
1208		return -EINVAL;
1209
1210	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1211	if (!rdma_is_port_valid(device, port)) {
1212		err = -EINVAL;
1213		goto err;
1214	}
1215
1216	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1217	if (!msg) {
1218		err = -ENOMEM;
1219		goto err;
1220	}
1221
1222	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1223			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1224			0, 0);
1225	if (!nlh) {
1226		err = -EMSGSIZE;
1227		goto err_free;
1228	}
1229
1230	err = fill_port_info(msg, device, port, sock_net(skb->sk));
1231	if (err)
1232		goto err_free;
1233
1234	nlmsg_end(msg, nlh);
1235	ib_device_put(device);
1236
1237	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1238
1239err_free:
1240	nlmsg_free(msg);
1241err:
1242	ib_device_put(device);
1243	return err;
1244}
1245
1246static int nldev_port_get_dumpit(struct sk_buff *skb,
1247				 struct netlink_callback *cb)
1248{
1249	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1250	struct ib_device *device;
1251	int start = cb->args[0];
1252	struct nlmsghdr *nlh;
1253	u32 idx = 0;
1254	u32 ifindex;
1255	int err;
1256	unsigned int p;
1257
1258	err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1259				     nldev_policy, NULL);
1260	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1261		return -EINVAL;
1262
1263	ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1264	device = ib_device_get_by_index(sock_net(skb->sk), ifindex);
1265	if (!device)
1266		return -EINVAL;
1267
1268	rdma_for_each_port (device, p) {
1269		/*
1270		 * The dumpit function returns all information from specific
1271		 * index. This specific index is taken from the netlink
1272		 * messages request sent by user and it is available
1273		 * in cb->args[0].
1274		 *
1275		 * Usually, the user doesn't fill this field and it causes
1276		 * to return everything.
1277		 *
1278		 */
1279		if (idx < start) {
1280			idx++;
1281			continue;
1282		}
1283
1284		nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
1285				cb->nlh->nlmsg_seq,
1286				RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1287						 RDMA_NLDEV_CMD_PORT_GET),
1288				0, NLM_F_MULTI);
1289
1290		if (!nlh || fill_port_info(skb, device, p, sock_net(skb->sk))) {
1291			nlmsg_cancel(skb, nlh);
1292			goto out;
1293		}
1294		idx++;
1295		nlmsg_end(skb, nlh);
1296	}
1297
1298out:
1299	ib_device_put(device);
1300	cb->args[0] = idx;
1301	return skb->len;
1302}
1303
1304static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1305			      struct netlink_ext_ack *extack)
1306{
1307	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1308	struct ib_device *device;
1309	struct sk_buff *msg;
1310	u32 index;
1311	int ret;
1312
1313	ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1314				     nldev_policy, extack);
1315	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1316		return -EINVAL;
1317
1318	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1319	device = ib_device_get_by_index(sock_net(skb->sk), index);
1320	if (!device)
1321		return -EINVAL;
1322
1323	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1324	if (!msg) {
1325		ret = -ENOMEM;
1326		goto err;
1327	}
1328
1329	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1330			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1331			0, 0);
1332	if (!nlh) {
1333		ret = -EMSGSIZE;
1334		goto err_free;
1335	}
1336
1337	ret = fill_res_info(msg, device);
1338	if (ret)
1339		goto err_free;
1340
1341	nlmsg_end(msg, nlh);
1342	ib_device_put(device);
1343	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1344
1345err_free:
1346	nlmsg_free(msg);
1347err:
1348	ib_device_put(device);
1349	return ret;
1350}
1351
1352static int _nldev_res_get_dumpit(struct ib_device *device,
1353				 struct sk_buff *skb,
1354				 struct netlink_callback *cb,
1355				 unsigned int idx)
1356{
1357	int start = cb->args[0];
1358	struct nlmsghdr *nlh;
1359
1360	if (idx < start)
1361		return 0;
1362
1363	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1364			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1365			0, NLM_F_MULTI);
1366
1367	if (!nlh || fill_res_info(skb, device)) {
1368		nlmsg_cancel(skb, nlh);
1369		goto out;
1370	}
1371	nlmsg_end(skb, nlh);
1372
1373	idx++;
1374
1375out:
1376	cb->args[0] = idx;
1377	return skb->len;
1378}
1379
1380static int nldev_res_get_dumpit(struct sk_buff *skb,
1381				struct netlink_callback *cb)
1382{
1383	return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
1384}
1385
1386struct nldev_fill_res_entry {
1387	enum rdma_nldev_attr nldev_attr;
1388	u8 flags;
1389	u32 entry;
1390	u32 id;
1391};
1392
1393enum nldev_res_flags {
1394	NLDEV_PER_DEV = 1 << 0,
1395};
1396
1397static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
1398	[RDMA_RESTRACK_QP] = {
1399		.nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
1400		.entry = RDMA_NLDEV_ATTR_RES_QP_ENTRY,
1401		.id = RDMA_NLDEV_ATTR_RES_LQPN,
1402	},
1403	[RDMA_RESTRACK_CM_ID] = {
1404		.nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
1405		.entry = RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY,
1406		.id = RDMA_NLDEV_ATTR_RES_CM_IDN,
1407	},
1408	[RDMA_RESTRACK_CQ] = {
1409		.nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
1410		.flags = NLDEV_PER_DEV,
1411		.entry = RDMA_NLDEV_ATTR_RES_CQ_ENTRY,
1412		.id = RDMA_NLDEV_ATTR_RES_CQN,
1413	},
1414	[RDMA_RESTRACK_MR] = {
1415		.nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
1416		.flags = NLDEV_PER_DEV,
1417		.entry = RDMA_NLDEV_ATTR_RES_MR_ENTRY,
1418		.id = RDMA_NLDEV_ATTR_RES_MRN,
1419	},
1420	[RDMA_RESTRACK_PD] = {
1421		.nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
1422		.flags = NLDEV_PER_DEV,
1423		.entry = RDMA_NLDEV_ATTR_RES_PD_ENTRY,
1424		.id = RDMA_NLDEV_ATTR_RES_PDN,
1425	},
1426	[RDMA_RESTRACK_COUNTER] = {
1427		.nldev_attr = RDMA_NLDEV_ATTR_STAT_COUNTER,
1428		.entry = RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY,
1429		.id = RDMA_NLDEV_ATTR_STAT_COUNTER_ID,
1430	},
1431	[RDMA_RESTRACK_CTX] = {
1432		.nldev_attr = RDMA_NLDEV_ATTR_RES_CTX,
1433		.flags = NLDEV_PER_DEV,
1434		.entry = RDMA_NLDEV_ATTR_RES_CTX_ENTRY,
1435		.id = RDMA_NLDEV_ATTR_RES_CTXN,
1436	},
1437	[RDMA_RESTRACK_SRQ] = {
1438		.nldev_attr = RDMA_NLDEV_ATTR_RES_SRQ,
1439		.flags = NLDEV_PER_DEV,
1440		.entry = RDMA_NLDEV_ATTR_RES_SRQ_ENTRY,
1441		.id = RDMA_NLDEV_ATTR_RES_SRQN,
1442	},
1443
1444};
1445
1446static int res_get_common_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1447			       struct netlink_ext_ack *extack,
1448			       enum rdma_restrack_type res_type,
1449			       res_fill_func_t fill_func)
1450{
1451	const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1452	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1453	struct rdma_restrack_entry *res;
1454	struct ib_device *device;
1455	u32 index, id, port = 0;
1456	bool has_cap_net_admin;
1457	struct sk_buff *msg;
1458	int ret;
1459
1460	ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1461				     nldev_policy, extack);
1462	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !fe->id || !tb[fe->id])
1463		return -EINVAL;
1464
1465	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1466	device = ib_device_get_by_index(sock_net(skb->sk), index);
1467	if (!device)
1468		return -EINVAL;
1469
1470	if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1471		port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1472		if (!rdma_is_port_valid(device, port)) {
1473			ret = -EINVAL;
1474			goto err;
1475		}
1476	}
1477
1478	if ((port && fe->flags & NLDEV_PER_DEV) ||
1479	    (!port && ~fe->flags & NLDEV_PER_DEV)) {
1480		ret = -EINVAL;
1481		goto err;
1482	}
1483
1484	id = nla_get_u32(tb[fe->id]);
1485	res = rdma_restrack_get_byid(device, res_type, id);
1486	if (IS_ERR(res)) {
1487		ret = PTR_ERR(res);
1488		goto err;
1489	}
1490
1491	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1492	if (!msg) {
1493		ret = -ENOMEM;
1494		goto err_get;
1495	}
1496
1497	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1498			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1499					 RDMA_NL_GET_OP(nlh->nlmsg_type)),
1500			0, 0);
1501
1502	if (!nlh || fill_nldev_handle(msg, device)) {
1503		ret = -EMSGSIZE;
1504		goto err_free;
1505	}
1506
1507	has_cap_net_admin = netlink_capable(skb, CAP_NET_ADMIN);
1508
1509	ret = fill_func(msg, has_cap_net_admin, res, port);
1510	if (ret)
1511		goto err_free;
1512
1513	rdma_restrack_put(res);
1514	nlmsg_end(msg, nlh);
1515	ib_device_put(device);
1516	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1517
1518err_free:
1519	nlmsg_free(msg);
1520err_get:
1521	rdma_restrack_put(res);
1522err:
1523	ib_device_put(device);
1524	return ret;
1525}
1526
1527static int res_get_common_dumpit(struct sk_buff *skb,
1528				 struct netlink_callback *cb,
1529				 enum rdma_restrack_type res_type,
1530				 res_fill_func_t fill_func)
1531{
1532	const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1533	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1534	struct rdma_restrack_entry *res;
1535	struct rdma_restrack_root *rt;
1536	int err, ret = 0, idx = 0;
1537	struct nlattr *table_attr;
1538	struct nlattr *entry_attr;
1539	struct ib_device *device;
1540	int start = cb->args[0];
1541	bool has_cap_net_admin;
1542	struct nlmsghdr *nlh;
1543	unsigned long id;
1544	u32 index, port = 0;
1545	bool filled = false;
1546
1547	err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1548				     nldev_policy, NULL);
1549	/*
1550	 * Right now, we are expecting the device index to get res information,
1551	 * but it is possible to extend this code to return all devices in
1552	 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
1553	 * if it doesn't exist, we will iterate over all devices.
1554	 *
1555	 * But it is not needed for now.
1556	 */
1557	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1558		return -EINVAL;
1559
1560	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1561	device = ib_device_get_by_index(sock_net(skb->sk), index);
1562	if (!device)
1563		return -EINVAL;
1564
1565	/*
1566	 * If no PORT_INDEX is supplied, we will return all QPs from that device
1567	 */
1568	if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1569		port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1570		if (!rdma_is_port_valid(device, port)) {
1571			ret = -EINVAL;
1572			goto err_index;
1573		}
1574	}
1575
1576	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1577			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1578					 RDMA_NL_GET_OP(cb->nlh->nlmsg_type)),
1579			0, NLM_F_MULTI);
1580
1581	if (!nlh || fill_nldev_handle(skb, device)) {
1582		ret = -EMSGSIZE;
1583		goto err;
1584	}
1585
1586	table_attr = nla_nest_start_noflag(skb, fe->nldev_attr);
1587	if (!table_attr) {
1588		ret = -EMSGSIZE;
1589		goto err;
1590	}
1591
1592	has_cap_net_admin = netlink_capable(cb->skb, CAP_NET_ADMIN);
1593
1594	rt = &device->res[res_type];
1595	xa_lock(&rt->xa);
1596	/*
1597	 * FIXME: if the skip ahead is something common this loop should
1598	 * use xas_for_each & xas_pause to optimize, we can have a lot of
1599	 * objects.
1600	 */
1601	xa_for_each(&rt->xa, id, res) {
1602		if (idx < start || !rdma_restrack_get(res))
1603			goto next;
1604
1605		xa_unlock(&rt->xa);
1606
1607		filled = true;
1608
1609		entry_attr = nla_nest_start_noflag(skb, fe->entry);
1610		if (!entry_attr) {
1611			ret = -EMSGSIZE;
1612			rdma_restrack_put(res);
1613			goto msg_full;
1614		}
1615
1616		ret = fill_func(skb, has_cap_net_admin, res, port);
1617
1618		rdma_restrack_put(res);
1619
1620		if (ret) {
1621			nla_nest_cancel(skb, entry_attr);
1622			if (ret == -EMSGSIZE)
1623				goto msg_full;
1624			if (ret == -EAGAIN)
1625				goto again;
1626			goto res_err;
1627		}
1628		nla_nest_end(skb, entry_attr);
1629again:		xa_lock(&rt->xa);
1630next:		idx++;
1631	}
1632	xa_unlock(&rt->xa);
1633
1634msg_full:
1635	nla_nest_end(skb, table_attr);
1636	nlmsg_end(skb, nlh);
1637	cb->args[0] = idx;
1638
1639	/*
1640	 * No more entries to fill, cancel the message and
1641	 * return 0 to mark end of dumpit.
1642	 */
1643	if (!filled)
1644		goto err;
1645
1646	ib_device_put(device);
1647	return skb->len;
1648
1649res_err:
1650	nla_nest_cancel(skb, table_attr);
1651
1652err:
1653	nlmsg_cancel(skb, nlh);
1654
1655err_index:
1656	ib_device_put(device);
1657	return ret;
1658}
1659
1660#define RES_GET_FUNCS(name, type)                                              \
1661	static int nldev_res_get_##name##_dumpit(struct sk_buff *skb,          \
1662						 struct netlink_callback *cb)  \
1663	{                                                                      \
1664		return res_get_common_dumpit(skb, cb, type,                    \
1665					     fill_res_##name##_entry);         \
1666	}                                                                      \
1667	static int nldev_res_get_##name##_doit(struct sk_buff *skb,            \
1668					       struct nlmsghdr *nlh,           \
1669					       struct netlink_ext_ack *extack) \
1670	{                                                                      \
1671		return res_get_common_doit(skb, nlh, extack, type,             \
1672					   fill_res_##name##_entry);           \
1673	}
1674
1675RES_GET_FUNCS(qp, RDMA_RESTRACK_QP);
1676RES_GET_FUNCS(qp_raw, RDMA_RESTRACK_QP);
1677RES_GET_FUNCS(cm_id, RDMA_RESTRACK_CM_ID);
1678RES_GET_FUNCS(cq, RDMA_RESTRACK_CQ);
1679RES_GET_FUNCS(cq_raw, RDMA_RESTRACK_CQ);
1680RES_GET_FUNCS(pd, RDMA_RESTRACK_PD);
1681RES_GET_FUNCS(mr, RDMA_RESTRACK_MR);
1682RES_GET_FUNCS(mr_raw, RDMA_RESTRACK_MR);
1683RES_GET_FUNCS(counter, RDMA_RESTRACK_COUNTER);
1684RES_GET_FUNCS(ctx, RDMA_RESTRACK_CTX);
1685RES_GET_FUNCS(srq, RDMA_RESTRACK_SRQ);
1686RES_GET_FUNCS(srq_raw, RDMA_RESTRACK_SRQ);
1687
1688static LIST_HEAD(link_ops);
1689static DECLARE_RWSEM(link_ops_rwsem);
1690
1691static const struct rdma_link_ops *link_ops_get(const char *type)
1692{
1693	const struct rdma_link_ops *ops;
1694
1695	list_for_each_entry(ops, &link_ops, list) {
1696		if (!strcmp(ops->type, type))
1697			goto out;
1698	}
1699	ops = NULL;
1700out:
1701	return ops;
1702}
1703
1704void rdma_link_register(struct rdma_link_ops *ops)
1705{
1706	down_write(&link_ops_rwsem);
1707	if (WARN_ON_ONCE(link_ops_get(ops->type)))
1708		goto out;
1709	list_add(&ops->list, &link_ops);
1710out:
1711	up_write(&link_ops_rwsem);
1712}
1713EXPORT_SYMBOL(rdma_link_register);
1714
1715void rdma_link_unregister(struct rdma_link_ops *ops)
1716{
1717	down_write(&link_ops_rwsem);
1718	list_del(&ops->list);
1719	up_write(&link_ops_rwsem);
1720}
1721EXPORT_SYMBOL(rdma_link_unregister);
1722
1723static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
1724			  struct netlink_ext_ack *extack)
1725{
1726	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1727	char ibdev_name[IB_DEVICE_NAME_MAX];
1728	const struct rdma_link_ops *ops;
1729	char ndev_name[IFNAMSIZ];
1730	struct net_device *ndev;
1731	char type[IFNAMSIZ];
1732	int err;
1733
1734	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1735				     nldev_policy, extack);
1736	if (err || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
1737	    !tb[RDMA_NLDEV_ATTR_LINK_TYPE] || !tb[RDMA_NLDEV_ATTR_NDEV_NAME])
1738		return -EINVAL;
1739
1740	nla_strscpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1741		    sizeof(ibdev_name));
1742	if (strchr(ibdev_name, '%') || strlen(ibdev_name) == 0)
1743		return -EINVAL;
1744
1745	nla_strscpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type));
1746	nla_strscpy(ndev_name, tb[RDMA_NLDEV_ATTR_NDEV_NAME],
1747		    sizeof(ndev_name));
1748
1749	ndev = dev_get_by_name(sock_net(skb->sk), ndev_name);
1750	if (!ndev)
1751		return -ENODEV;
1752
1753	down_read(&link_ops_rwsem);
1754	ops = link_ops_get(type);
1755#ifdef CONFIG_MODULES
1756	if (!ops) {
1757		up_read(&link_ops_rwsem);
1758		request_module("rdma-link-%s", type);
1759		down_read(&link_ops_rwsem);
1760		ops = link_ops_get(type);
1761	}
1762#endif
1763	err = ops ? ops->newlink(ibdev_name, ndev) : -EINVAL;
1764	up_read(&link_ops_rwsem);
1765	dev_put(ndev);
1766
1767	return err;
1768}
1769
1770static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
1771			  struct netlink_ext_ack *extack)
1772{
1773	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1774	struct ib_device *device;
1775	u32 index;
1776	int err;
1777
1778	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1779				     nldev_policy, extack);
1780	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1781		return -EINVAL;
1782
1783	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1784	device = ib_device_get_by_index(sock_net(skb->sk), index);
1785	if (!device)
1786		return -EINVAL;
1787
1788	if (!(device->attrs.kernel_cap_flags & IBK_ALLOW_USER_UNREG)) {
1789		ib_device_put(device);
1790		return -EINVAL;
1791	}
1792
1793	ib_unregister_device_and_put(device);
1794	return 0;
1795}
1796
1797static int nldev_get_chardev(struct sk_buff *skb, struct nlmsghdr *nlh,
1798			     struct netlink_ext_ack *extack)
1799{
1800	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1801	char client_name[RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE];
1802	struct ib_client_nl_info data = {};
1803	struct ib_device *ibdev = NULL;
1804	struct sk_buff *msg;
1805	u32 index;
1806	int err;
1807
1808	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
1809			  extack);
1810	if (err || !tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE])
1811		return -EINVAL;
1812
1813	nla_strscpy(client_name, tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE],
1814		    sizeof(client_name));
1815
1816	if (tb[RDMA_NLDEV_ATTR_DEV_INDEX]) {
1817		index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1818		ibdev = ib_device_get_by_index(sock_net(skb->sk), index);
1819		if (!ibdev)
1820			return -EINVAL;
1821
1822		if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1823			data.port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1824			if (!rdma_is_port_valid(ibdev, data.port)) {
1825				err = -EINVAL;
1826				goto out_put;
1827			}
1828		} else {
1829			data.port = -1;
1830		}
1831	} else if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1832		return -EINVAL;
1833	}
1834
1835	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1836	if (!msg) {
1837		err = -ENOMEM;
1838		goto out_put;
1839	}
1840	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1841			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1842					 RDMA_NLDEV_CMD_GET_CHARDEV),
1843			0, 0);
1844	if (!nlh) {
1845		err = -EMSGSIZE;
1846		goto out_nlmsg;
1847	}
1848
1849	data.nl_msg = msg;
1850	err = ib_get_client_nl_info(ibdev, client_name, &data);
1851	if (err)
1852		goto out_nlmsg;
1853
1854	err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV,
1855				huge_encode_dev(data.cdev->devt),
1856				RDMA_NLDEV_ATTR_PAD);
1857	if (err)
1858		goto out_data;
1859	err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV_ABI, data.abi,
1860				RDMA_NLDEV_ATTR_PAD);
1861	if (err)
1862		goto out_data;
1863	if (nla_put_string(msg, RDMA_NLDEV_ATTR_CHARDEV_NAME,
1864			   dev_name(data.cdev))) {
1865		err = -EMSGSIZE;
1866		goto out_data;
1867	}
1868
1869	nlmsg_end(msg, nlh);
1870	put_device(data.cdev);
1871	if (ibdev)
1872		ib_device_put(ibdev);
1873	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1874
1875out_data:
1876	put_device(data.cdev);
1877out_nlmsg:
1878	nlmsg_free(msg);
1879out_put:
1880	if (ibdev)
1881		ib_device_put(ibdev);
1882	return err;
1883}
1884
1885static int nldev_sys_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1886			      struct netlink_ext_ack *extack)
1887{
1888	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1889	struct sk_buff *msg;
1890	int err;
1891
1892	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1893			  nldev_policy, extack);
1894	if (err)
1895		return err;
1896
1897	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1898	if (!msg)
1899		return -ENOMEM;
1900
1901	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1902			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1903					 RDMA_NLDEV_CMD_SYS_GET),
1904			0, 0);
1905	if (!nlh) {
1906		nlmsg_free(msg);
1907		return -EMSGSIZE;
1908	}
1909
1910	err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_NETNS_MODE,
1911			 (u8)ib_devices_shared_netns);
1912	if (err) {
1913		nlmsg_free(msg);
1914		return err;
1915	}
1916
1917	err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE,
1918			 (u8)privileged_qkey);
1919	if (err) {
1920		nlmsg_free(msg);
1921		return err;
1922	}
1923	/*
1924	 * Copy-on-fork is supported.
1925	 * See commits:
1926	 * 70e806e4e645 ("mm: Do early cow for pinned pages during fork() for ptes")
1927	 * 4eae4efa2c29 ("hugetlb: do early cow when page pinned on src mm")
1928	 * for more details. Don't backport this without them.
1929	 *
1930	 * Return value ignored on purpose, assume copy-on-fork is not
1931	 * supported in case of failure.
1932	 */
1933	nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK, 1);
1934
1935	nlmsg_end(msg, nlh);
1936	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1937}
1938
1939static int nldev_set_sys_set_netns_doit(struct nlattr *tb[])
1940{
1941	u8 enable;
1942	int err;
1943
1944	enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]);
1945	/* Only 0 and 1 are supported */
1946	if (enable > 1)
1947		return -EINVAL;
1948
1949	err = rdma_compatdev_set(enable);
1950	return err;
1951}
1952
1953static int nldev_set_sys_set_pqkey_doit(struct nlattr *tb[])
1954{
1955	u8 enable;
1956
1957	enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE]);
1958	/* Only 0 and 1 are supported */
1959	if (enable > 1)
1960		return -EINVAL;
1961
1962	privileged_qkey = enable;
1963	return 0;
1964}
1965
1966static int nldev_set_sys_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1967				  struct netlink_ext_ack *extack)
1968{
1969	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
 
1970	int err;
1971
1972	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1973			  nldev_policy, extack);
1974	if (err)
1975		return -EINVAL;
1976
1977	if (tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE])
1978		return nldev_set_sys_set_netns_doit(tb);
1979
1980	if (tb[RDMA_NLDEV_SYS_ATTR_PRIVILEGED_QKEY_MODE])
1981		return nldev_set_sys_set_pqkey_doit(tb);
1982
1983	return -EINVAL;
1984}
1985
1986
1987static int nldev_stat_set_mode_doit(struct sk_buff *msg,
1988				    struct netlink_ext_ack *extack,
1989				    struct nlattr *tb[],
1990				    struct ib_device *device, u32 port)
1991{
1992	u32 mode, mask = 0, qpn, cntn = 0;
1993	int ret;
1994
1995	/* Currently only counter for QP is supported */
1996	if (!tb[RDMA_NLDEV_ATTR_STAT_RES] ||
1997	    nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
1998		return -EINVAL;
1999
2000	mode = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_MODE]);
2001	if (mode == RDMA_COUNTER_MODE_AUTO) {
2002		if (tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK])
2003			mask = nla_get_u32(
2004				tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]);
2005		return rdma_counter_set_auto_mode(device, port, mask, extack);
2006	}
2007
2008	if (!tb[RDMA_NLDEV_ATTR_RES_LQPN])
2009		return -EINVAL;
2010
2011	qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
2012	if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]) {
2013		cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
2014		ret = rdma_counter_bind_qpn(device, port, qpn, cntn);
2015		if (ret)
2016			return ret;
2017	} else {
2018		ret = rdma_counter_bind_qpn_alloc(device, port, qpn, &cntn);
2019		if (ret)
2020			return ret;
2021	}
2022
2023	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
2024	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
2025		ret = -EMSGSIZE;
2026		goto err_fill;
2027	}
2028
2029	return 0;
2030
2031err_fill:
2032	rdma_counter_unbind_qpn(device, port, qpn, cntn);
2033	return ret;
2034}
2035
2036static int nldev_stat_set_counter_dynamic_doit(struct nlattr *tb[],
2037					       struct ib_device *device,
2038					       u32 port)
2039{
2040	struct rdma_hw_stats *stats;
2041	struct nlattr *entry_attr;
2042	unsigned long *target;
2043	int rem, i, ret = 0;
2044	u32 index;
2045
2046	stats = ib_get_hw_stats_port(device, port);
2047	if (!stats)
2048		return -EINVAL;
2049
2050	target = kcalloc(BITS_TO_LONGS(stats->num_counters),
2051			 sizeof(*stats->is_disabled), GFP_KERNEL);
2052	if (!target)
2053		return -ENOMEM;
2054
2055	nla_for_each_nested(entry_attr, tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS],
2056			    rem) {
2057		index = nla_get_u32(entry_attr);
2058		if ((index >= stats->num_counters) ||
2059		    !(stats->descs[index].flags & IB_STAT_FLAG_OPTIONAL)) {
2060			ret = -EINVAL;
2061			goto out;
2062		}
2063
2064		set_bit(index, target);
2065	}
2066
2067	for (i = 0; i < stats->num_counters; i++) {
2068		if (!(stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL))
2069			continue;
2070
2071		ret = rdma_counter_modify(device, port, i, test_bit(i, target));
2072		if (ret)
2073			goto out;
2074	}
2075
2076out:
2077	kfree(target);
2078	return ret;
2079}
2080
2081static int nldev_stat_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2082			       struct netlink_ext_ack *extack)
2083{
 
2084	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2085	struct ib_device *device;
2086	struct sk_buff *msg;
2087	u32 index, port;
2088	int ret;
2089
2090	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
2091			  extack);
2092	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
2093	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
 
 
 
 
 
2094		return -EINVAL;
2095
2096	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2097	device = ib_device_get_by_index(sock_net(skb->sk), index);
2098	if (!device)
2099		return -EINVAL;
2100
2101	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2102	if (!rdma_is_port_valid(device, port)) {
2103		ret = -EINVAL;
2104		goto err_put_device;
2105	}
2106
2107	if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] &&
2108	    !tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]) {
2109		ret = -EINVAL;
2110		goto err_put_device;
2111	}
2112
2113	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2114	if (!msg) {
2115		ret = -ENOMEM;
2116		goto err_put_device;
2117	}
2118	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2119			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2120					 RDMA_NLDEV_CMD_STAT_SET),
2121			0, 0);
2122	if (!nlh || fill_nldev_handle(msg, device) ||
2123	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) {
2124		ret = -EMSGSIZE;
2125		goto err_free_msg;
2126	}
2127
2128	if (tb[RDMA_NLDEV_ATTR_STAT_MODE]) {
2129		ret = nldev_stat_set_mode_doit(msg, extack, tb, device, port);
 
 
 
 
2130		if (ret)
2131			goto err_free_msg;
2132	}
2133
2134	if (tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]) {
2135		ret = nldev_stat_set_counter_dynamic_doit(tb, device, port);
 
 
 
 
 
 
 
2136		if (ret)
2137			goto err_free_msg;
 
 
 
 
 
 
 
 
2138	}
2139
2140	nlmsg_end(msg, nlh);
2141	ib_device_put(device);
2142	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2143
2144err_free_msg:
 
 
2145	nlmsg_free(msg);
2146err_put_device:
2147	ib_device_put(device);
2148	return ret;
2149}
2150
2151static int nldev_stat_del_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2152			       struct netlink_ext_ack *extack)
2153{
2154	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2155	struct ib_device *device;
2156	struct sk_buff *msg;
2157	u32 index, port, qpn, cntn;
2158	int ret;
2159
2160	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2161			  nldev_policy, extack);
2162	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES] ||
2163	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX] ||
2164	    !tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID] ||
2165	    !tb[RDMA_NLDEV_ATTR_RES_LQPN])
2166		return -EINVAL;
2167
2168	if (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
2169		return -EINVAL;
2170
2171	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2172	device = ib_device_get_by_index(sock_net(skb->sk), index);
2173	if (!device)
2174		return -EINVAL;
2175
2176	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2177	if (!rdma_is_port_valid(device, port)) {
2178		ret = -EINVAL;
2179		goto err;
2180	}
2181
2182	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2183	if (!msg) {
2184		ret = -ENOMEM;
2185		goto err;
2186	}
2187	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2188			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2189					 RDMA_NLDEV_CMD_STAT_SET),
2190			0, 0);
2191	if (!nlh) {
2192		ret = -EMSGSIZE;
2193		goto err_fill;
2194	}
2195
2196	cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
2197	qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
2198	if (fill_nldev_handle(msg, device) ||
2199	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
2200	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
2201	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
2202		ret = -EMSGSIZE;
2203		goto err_fill;
2204	}
2205
2206	ret = rdma_counter_unbind_qpn(device, port, qpn, cntn);
2207	if (ret)
2208		goto err_fill;
2209
2210	nlmsg_end(msg, nlh);
2211	ib_device_put(device);
2212	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2213
2214err_fill:
2215	nlmsg_free(msg);
2216err:
2217	ib_device_put(device);
2218	return ret;
2219}
2220
2221static int stat_get_doit_default_counter(struct sk_buff *skb,
2222					 struct nlmsghdr *nlh,
2223					 struct netlink_ext_ack *extack,
2224					 struct nlattr *tb[])
2225{
2226	struct rdma_hw_stats *stats;
2227	struct nlattr *table_attr;
2228	struct ib_device *device;
2229	int ret, num_cnts, i;
2230	struct sk_buff *msg;
2231	u32 index, port;
2232	u64 v;
2233
2234	if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2235		return -EINVAL;
2236
2237	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2238	device = ib_device_get_by_index(sock_net(skb->sk), index);
2239	if (!device)
2240		return -EINVAL;
2241
2242	if (!device->ops.alloc_hw_port_stats || !device->ops.get_hw_stats) {
2243		ret = -EINVAL;
2244		goto err;
2245	}
2246
2247	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2248	stats = ib_get_hw_stats_port(device, port);
2249	if (!stats) {
2250		ret = -EINVAL;
2251		goto err;
2252	}
2253
2254	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2255	if (!msg) {
2256		ret = -ENOMEM;
2257		goto err;
2258	}
2259
2260	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2261			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2262					 RDMA_NLDEV_CMD_STAT_GET),
2263			0, 0);
2264
2265	if (!nlh || fill_nldev_handle(msg, device) ||
2266	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) {
2267		ret = -EMSGSIZE;
2268		goto err_msg;
2269	}
2270
2271	mutex_lock(&stats->lock);
2272
2273	num_cnts = device->ops.get_hw_stats(device, stats, port, 0);
2274	if (num_cnts < 0) {
2275		ret = -EINVAL;
2276		goto err_stats;
2277	}
2278
2279	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
2280	if (!table_attr) {
2281		ret = -EMSGSIZE;
2282		goto err_stats;
2283	}
2284	for (i = 0; i < num_cnts; i++) {
2285		if (test_bit(i, stats->is_disabled))
2286			continue;
2287
2288		v = stats->value[i] +
2289			rdma_counter_get_hwstat_value(device, port, i);
2290		if (rdma_nl_stat_hwcounter_entry(msg,
2291						 stats->descs[i].name, v)) {
2292			ret = -EMSGSIZE;
2293			goto err_table;
2294		}
2295	}
2296	nla_nest_end(msg, table_attr);
2297
2298	mutex_unlock(&stats->lock);
2299	nlmsg_end(msg, nlh);
2300	ib_device_put(device);
2301	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2302
2303err_table:
2304	nla_nest_cancel(msg, table_attr);
2305err_stats:
2306	mutex_unlock(&stats->lock);
2307err_msg:
2308	nlmsg_free(msg);
2309err:
2310	ib_device_put(device);
2311	return ret;
2312}
2313
2314static int stat_get_doit_qp(struct sk_buff *skb, struct nlmsghdr *nlh,
2315			    struct netlink_ext_ack *extack, struct nlattr *tb[])
2316
2317{
2318	static enum rdma_nl_counter_mode mode;
2319	static enum rdma_nl_counter_mask mask;
2320	struct ib_device *device;
2321	struct sk_buff *msg;
2322	u32 index, port;
2323	int ret;
2324
2325	if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID])
2326		return nldev_res_get_counter_doit(skb, nlh, extack);
2327
2328	if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] ||
2329	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2330		return -EINVAL;
2331
2332	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2333	device = ib_device_get_by_index(sock_net(skb->sk), index);
2334	if (!device)
2335		return -EINVAL;
2336
2337	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2338	if (!rdma_is_port_valid(device, port)) {
2339		ret = -EINVAL;
2340		goto err;
2341	}
2342
2343	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2344	if (!msg) {
2345		ret = -ENOMEM;
2346		goto err;
2347	}
2348
2349	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2350			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2351					 RDMA_NLDEV_CMD_STAT_GET),
2352			0, 0);
2353	if (!nlh) {
2354		ret = -EMSGSIZE;
2355		goto err_msg;
2356	}
2357
2358	ret = rdma_counter_get_mode(device, port, &mode, &mask);
2359	if (ret)
2360		goto err_msg;
2361
2362	if (fill_nldev_handle(msg, device) ||
2363	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
2364	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, mode)) {
2365		ret = -EMSGSIZE;
2366		goto err_msg;
2367	}
2368
2369	if ((mode == RDMA_COUNTER_MODE_AUTO) &&
2370	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK, mask)) {
2371		ret = -EMSGSIZE;
2372		goto err_msg;
2373	}
2374
2375	nlmsg_end(msg, nlh);
2376	ib_device_put(device);
2377	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2378
2379err_msg:
2380	nlmsg_free(msg);
2381err:
2382	ib_device_put(device);
2383	return ret;
2384}
2385
2386static int nldev_stat_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2387			       struct netlink_ext_ack *extack)
2388{
2389	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2390	int ret;
2391
2392	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2393			  nldev_policy, extack);
2394	if (ret)
2395		return -EINVAL;
2396
2397	if (!tb[RDMA_NLDEV_ATTR_STAT_RES])
2398		return stat_get_doit_default_counter(skb, nlh, extack, tb);
2399
2400	switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2401	case RDMA_NLDEV_ATTR_RES_QP:
2402		ret = stat_get_doit_qp(skb, nlh, extack, tb);
2403		break;
2404	case RDMA_NLDEV_ATTR_RES_MR:
2405		ret = res_get_common_doit(skb, nlh, extack, RDMA_RESTRACK_MR,
2406					  fill_stat_mr_entry);
2407		break;
2408	default:
2409		ret = -EINVAL;
2410		break;
2411	}
2412
2413	return ret;
2414}
2415
2416static int nldev_stat_get_dumpit(struct sk_buff *skb,
2417				 struct netlink_callback *cb)
2418{
2419	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2420	int ret;
2421
2422	ret = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2423			  nldev_policy, NULL);
2424	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES])
2425		return -EINVAL;
2426
2427	switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2428	case RDMA_NLDEV_ATTR_RES_QP:
2429		ret = nldev_res_get_counter_dumpit(skb, cb);
2430		break;
2431	case RDMA_NLDEV_ATTR_RES_MR:
2432		ret = res_get_common_dumpit(skb, cb, RDMA_RESTRACK_MR,
2433					    fill_stat_mr_entry);
2434		break;
2435	default:
2436		ret = -EINVAL;
2437		break;
2438	}
2439
2440	return ret;
2441}
2442
2443static int nldev_stat_get_counter_status_doit(struct sk_buff *skb,
2444					      struct nlmsghdr *nlh,
2445					      struct netlink_ext_ack *extack)
2446{
2447	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX], *table, *entry;
2448	struct rdma_hw_stats *stats;
2449	struct ib_device *device;
2450	struct sk_buff *msg;
2451	u32 devid, port;
2452	int ret, i;
2453
2454	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2455			  nldev_policy, extack);
2456	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
2457	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2458		return -EINVAL;
2459
2460	devid = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2461	device = ib_device_get_by_index(sock_net(skb->sk), devid);
2462	if (!device)
2463		return -EINVAL;
2464
2465	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2466	if (!rdma_is_port_valid(device, port)) {
2467		ret = -EINVAL;
2468		goto err;
2469	}
2470
2471	stats = ib_get_hw_stats_port(device, port);
2472	if (!stats) {
2473		ret = -EINVAL;
2474		goto err;
2475	}
2476
2477	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2478	if (!msg) {
2479		ret = -ENOMEM;
2480		goto err;
2481	}
2482
2483	nlh = nlmsg_put(
2484		msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2485		RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_STAT_GET_STATUS),
2486		0, 0);
2487
2488	ret = -EMSGSIZE;
2489	if (!nlh || fill_nldev_handle(msg, device) ||
2490	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
2491		goto err_msg;
2492
2493	table = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
2494	if (!table)
2495		goto err_msg;
2496
2497	mutex_lock(&stats->lock);
2498	for (i = 0; i < stats->num_counters; i++) {
2499		entry = nla_nest_start(msg,
2500				       RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY);
2501		if (!entry)
2502			goto err_msg_table;
2503
2504		if (nla_put_string(msg,
2505				   RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME,
2506				   stats->descs[i].name) ||
2507		    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_INDEX, i))
2508			goto err_msg_entry;
2509
2510		if ((stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL) &&
2511		    (nla_put_u8(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_DYNAMIC,
2512				!test_bit(i, stats->is_disabled))))
2513			goto err_msg_entry;
2514
2515		nla_nest_end(msg, entry);
2516	}
2517	mutex_unlock(&stats->lock);
2518
2519	nla_nest_end(msg, table);
2520	nlmsg_end(msg, nlh);
2521	ib_device_put(device);
2522	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2523
2524err_msg_entry:
2525	nla_nest_cancel(msg, entry);
2526err_msg_table:
2527	mutex_unlock(&stats->lock);
2528	nla_nest_cancel(msg, table);
2529err_msg:
2530	nlmsg_free(msg);
2531err:
2532	ib_device_put(device);
2533	return ret;
2534}
2535
2536static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
2537	[RDMA_NLDEV_CMD_GET] = {
2538		.doit = nldev_get_doit,
2539		.dump = nldev_get_dumpit,
2540	},
2541	[RDMA_NLDEV_CMD_GET_CHARDEV] = {
2542		.doit = nldev_get_chardev,
2543	},
2544	[RDMA_NLDEV_CMD_SET] = {
2545		.doit = nldev_set_doit,
2546		.flags = RDMA_NL_ADMIN_PERM,
2547	},
2548	[RDMA_NLDEV_CMD_NEWLINK] = {
2549		.doit = nldev_newlink,
2550		.flags = RDMA_NL_ADMIN_PERM,
2551	},
2552	[RDMA_NLDEV_CMD_DELLINK] = {
2553		.doit = nldev_dellink,
2554		.flags = RDMA_NL_ADMIN_PERM,
2555	},
2556	[RDMA_NLDEV_CMD_PORT_GET] = {
2557		.doit = nldev_port_get_doit,
2558		.dump = nldev_port_get_dumpit,
2559	},
2560	[RDMA_NLDEV_CMD_RES_GET] = {
2561		.doit = nldev_res_get_doit,
2562		.dump = nldev_res_get_dumpit,
2563	},
2564	[RDMA_NLDEV_CMD_RES_QP_GET] = {
2565		.doit = nldev_res_get_qp_doit,
2566		.dump = nldev_res_get_qp_dumpit,
2567	},
2568	[RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
2569		.doit = nldev_res_get_cm_id_doit,
2570		.dump = nldev_res_get_cm_id_dumpit,
2571	},
2572	[RDMA_NLDEV_CMD_RES_CQ_GET] = {
2573		.doit = nldev_res_get_cq_doit,
2574		.dump = nldev_res_get_cq_dumpit,
2575	},
2576	[RDMA_NLDEV_CMD_RES_MR_GET] = {
2577		.doit = nldev_res_get_mr_doit,
2578		.dump = nldev_res_get_mr_dumpit,
2579	},
2580	[RDMA_NLDEV_CMD_RES_PD_GET] = {
2581		.doit = nldev_res_get_pd_doit,
2582		.dump = nldev_res_get_pd_dumpit,
2583	},
2584	[RDMA_NLDEV_CMD_RES_CTX_GET] = {
2585		.doit = nldev_res_get_ctx_doit,
2586		.dump = nldev_res_get_ctx_dumpit,
2587	},
2588	[RDMA_NLDEV_CMD_RES_SRQ_GET] = {
2589		.doit = nldev_res_get_srq_doit,
2590		.dump = nldev_res_get_srq_dumpit,
2591	},
2592	[RDMA_NLDEV_CMD_SYS_GET] = {
2593		.doit = nldev_sys_get_doit,
2594	},
2595	[RDMA_NLDEV_CMD_SYS_SET] = {
2596		.doit = nldev_set_sys_set_doit,
2597		.flags = RDMA_NL_ADMIN_PERM,
2598	},
2599	[RDMA_NLDEV_CMD_STAT_SET] = {
2600		.doit = nldev_stat_set_doit,
2601		.flags = RDMA_NL_ADMIN_PERM,
2602	},
2603	[RDMA_NLDEV_CMD_STAT_GET] = {
2604		.doit = nldev_stat_get_doit,
2605		.dump = nldev_stat_get_dumpit,
2606	},
2607	[RDMA_NLDEV_CMD_STAT_DEL] = {
2608		.doit = nldev_stat_del_doit,
2609		.flags = RDMA_NL_ADMIN_PERM,
2610	},
2611	[RDMA_NLDEV_CMD_RES_QP_GET_RAW] = {
2612		.doit = nldev_res_get_qp_raw_doit,
2613		.dump = nldev_res_get_qp_raw_dumpit,
2614		.flags = RDMA_NL_ADMIN_PERM,
2615	},
2616	[RDMA_NLDEV_CMD_RES_CQ_GET_RAW] = {
2617		.doit = nldev_res_get_cq_raw_doit,
2618		.dump = nldev_res_get_cq_raw_dumpit,
2619		.flags = RDMA_NL_ADMIN_PERM,
2620	},
2621	[RDMA_NLDEV_CMD_RES_MR_GET_RAW] = {
2622		.doit = nldev_res_get_mr_raw_doit,
2623		.dump = nldev_res_get_mr_raw_dumpit,
2624		.flags = RDMA_NL_ADMIN_PERM,
2625	},
2626	[RDMA_NLDEV_CMD_RES_SRQ_GET_RAW] = {
2627		.doit = nldev_res_get_srq_raw_doit,
2628		.dump = nldev_res_get_srq_raw_dumpit,
2629		.flags = RDMA_NL_ADMIN_PERM,
2630	},
2631	[RDMA_NLDEV_CMD_STAT_GET_STATUS] = {
2632		.doit = nldev_stat_get_counter_status_doit,
2633	},
2634};
2635
2636void __init nldev_init(void)
2637{
2638	rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
2639}
2640
2641void nldev_exit(void)
2642{
2643	rdma_nl_unregister(RDMA_NL_NLDEV);
2644}
2645
2646MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);
v5.14.15
   1/*
   2 * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
   3 *
   4 * Redistribution and use in source and binary forms, with or without
   5 * modification, are permitted provided that the following conditions are met:
   6 *
   7 * 1. Redistributions of source code must retain the above copyright
   8 *    notice, this list of conditions and the following disclaimer.
   9 * 2. Redistributions in binary form must reproduce the above copyright
  10 *    notice, this list of conditions and the following disclaimer in the
  11 *    documentation and/or other materials provided with the distribution.
  12 * 3. Neither the names of the copyright holders nor the names of its
  13 *    contributors may be used to endorse or promote products derived from
  14 *    this software without specific prior written permission.
  15 *
  16 * Alternatively, this software may be distributed under the terms of the
  17 * GNU General Public License ("GPL") version 2 as published by the Free
  18 * Software Foundation.
  19 *
  20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30 * POSSIBILITY OF SUCH DAMAGE.
  31 */
  32
  33#include <linux/module.h>
  34#include <linux/pid.h>
  35#include <linux/pid_namespace.h>
  36#include <linux/mutex.h>
  37#include <net/netlink.h>
  38#include <rdma/rdma_cm.h>
  39#include <rdma/rdma_netlink.h>
  40
  41#include "core_priv.h"
  42#include "cma_priv.h"
  43#include "restrack.h"
  44#include "uverbs.h"
  45
 
 
 
 
 
 
 
  46typedef int (*res_fill_func_t)(struct sk_buff*, bool,
  47			       struct rdma_restrack_entry*, uint32_t);
  48
  49/*
  50 * Sort array elements by the netlink attribute name
  51 */
  52static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
  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,
  56					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
  57	[RDMA_NLDEV_ATTR_CHARDEV_TYPE]		= { .type = NLA_NUL_STRING,
  58					.len = RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE },
  59	[RDMA_NLDEV_ATTR_DEV_DIM]               = { .type = NLA_U8 },
  60	[RDMA_NLDEV_ATTR_DEV_INDEX]		= { .type = NLA_U32 },
  61	[RDMA_NLDEV_ATTR_DEV_NAME]		= { .type = NLA_NUL_STRING,
  62					.len = IB_DEVICE_NAME_MAX },
  63	[RDMA_NLDEV_ATTR_DEV_NODE_TYPE]		= { .type = NLA_U8 },
  64	[RDMA_NLDEV_ATTR_DEV_PROTOCOL]		= { .type = NLA_NUL_STRING,
  65					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
  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,
  70					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
  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,
  76					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
  77	[RDMA_NLDEV_ATTR_LID]			= { .type = NLA_U32 },
  78	[RDMA_NLDEV_ATTR_LINK_TYPE]		= { .type = NLA_NUL_STRING,
  79					.len = IFNAMSIZ },
  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 },
  88	[RDMA_NLDEV_ATTR_RES_CM_ID]		= { .type = NLA_NESTED },
  89	[RDMA_NLDEV_ATTR_RES_CM_IDN]		= { .type = NLA_U32 },
  90	[RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY]	= { .type = NLA_NESTED },
  91	[RDMA_NLDEV_ATTR_RES_CQ]		= { .type = NLA_NESTED },
  92	[RDMA_NLDEV_ATTR_RES_CQE]		= { .type = NLA_U32 },
  93	[RDMA_NLDEV_ATTR_RES_CQN]		= { .type = NLA_U32 },
  94	[RDMA_NLDEV_ATTR_RES_CQ_ENTRY]		= { .type = NLA_NESTED },
  95	[RDMA_NLDEV_ATTR_RES_CTX]		= { .type = NLA_NESTED },
  96	[RDMA_NLDEV_ATTR_RES_CTXN]		= { .type = NLA_U32 },
  97	[RDMA_NLDEV_ATTR_RES_CTX_ENTRY]		= { .type = NLA_NESTED },
  98	[RDMA_NLDEV_ATTR_RES_DST_ADDR]		= {
  99			.len = sizeof(struct __kernel_sockaddr_storage) },
 100	[RDMA_NLDEV_ATTR_RES_IOVA]		= { .type = NLA_U64 },
 101	[RDMA_NLDEV_ATTR_RES_KERN_NAME]		= { .type = NLA_NUL_STRING,
 102					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
 103	[RDMA_NLDEV_ATTR_RES_LKEY]		= { .type = NLA_U32 },
 104	[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]	= { .type = NLA_U32 },
 105	[RDMA_NLDEV_ATTR_RES_LQPN]		= { .type = NLA_U32 },
 106	[RDMA_NLDEV_ATTR_RES_MR]		= { .type = NLA_NESTED },
 107	[RDMA_NLDEV_ATTR_RES_MRLEN]		= { .type = NLA_U64 },
 108	[RDMA_NLDEV_ATTR_RES_MRN]		= { .type = NLA_U32 },
 109	[RDMA_NLDEV_ATTR_RES_MR_ENTRY]		= { .type = NLA_NESTED },
 110	[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]	= { .type = NLA_U8 },
 111	[RDMA_NLDEV_ATTR_RES_PD]		= { .type = NLA_NESTED },
 112	[RDMA_NLDEV_ATTR_RES_PDN]		= { .type = NLA_U32 },
 113	[RDMA_NLDEV_ATTR_RES_PD_ENTRY]		= { .type = NLA_NESTED },
 114	[RDMA_NLDEV_ATTR_RES_PID]		= { .type = NLA_U32 },
 115	[RDMA_NLDEV_ATTR_RES_POLL_CTX]		= { .type = NLA_U8 },
 116	[RDMA_NLDEV_ATTR_RES_PS]		= { .type = NLA_U32 },
 117	[RDMA_NLDEV_ATTR_RES_QP]		= { .type = NLA_NESTED },
 118	[RDMA_NLDEV_ATTR_RES_QP_ENTRY]		= { .type = NLA_NESTED },
 119	[RDMA_NLDEV_ATTR_RES_RAW]		= { .type = NLA_BINARY },
 120	[RDMA_NLDEV_ATTR_RES_RKEY]		= { .type = NLA_U32 },
 121	[RDMA_NLDEV_ATTR_RES_RQPN]		= { .type = NLA_U32 },
 122	[RDMA_NLDEV_ATTR_RES_RQ_PSN]		= { .type = NLA_U32 },
 123	[RDMA_NLDEV_ATTR_RES_SQ_PSN]		= { .type = NLA_U32 },
 124	[RDMA_NLDEV_ATTR_RES_SRC_ADDR]		= {
 125			.len = sizeof(struct __kernel_sockaddr_storage) },
 126	[RDMA_NLDEV_ATTR_RES_STATE]		= { .type = NLA_U8 },
 127	[RDMA_NLDEV_ATTR_RES_SUMMARY]		= { .type = NLA_NESTED },
 128	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY]	= { .type = NLA_NESTED },
 129	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]= { .type = NLA_U64 },
 130	[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]= { .type = NLA_NUL_STRING,
 131					.len = RDMA_NLDEV_ATTR_EMPTY_STRING },
 132	[RDMA_NLDEV_ATTR_RES_TYPE]		= { .type = NLA_U8 },
 133	[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]= { .type = NLA_U32 },
 134	[RDMA_NLDEV_ATTR_RES_USECNT]		= { .type = NLA_U64 },
 135	[RDMA_NLDEV_ATTR_RES_SRQ]		= { .type = NLA_NESTED },
 136	[RDMA_NLDEV_ATTR_RES_SRQN]		= { .type = NLA_U32 },
 137	[RDMA_NLDEV_ATTR_RES_SRQ_ENTRY]		= { .type = NLA_NESTED },
 138	[RDMA_NLDEV_ATTR_MIN_RANGE]		= { .type = NLA_U32 },
 139	[RDMA_NLDEV_ATTR_MAX_RANGE]		= { .type = NLA_U32 },
 140	[RDMA_NLDEV_ATTR_SM_LID]		= { .type = NLA_U32 },
 141	[RDMA_NLDEV_ATTR_SUBNET_PREFIX]		= { .type = NLA_U64 },
 142	[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]	= { .type = NLA_U32 },
 143	[RDMA_NLDEV_ATTR_STAT_MODE]		= { .type = NLA_U32 },
 144	[RDMA_NLDEV_ATTR_STAT_RES]		= { .type = NLA_U32 },
 145	[RDMA_NLDEV_ATTR_STAT_COUNTER]		= { .type = NLA_NESTED },
 146	[RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY]	= { .type = NLA_NESTED },
 147	[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]       = { .type = NLA_U32 },
 148	[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]       = { .type = NLA_NESTED },
 149	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY]  = { .type = NLA_NESTED },
 150	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME] = { .type = NLA_NUL_STRING },
 151	[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE] = { .type = NLA_U64 },
 152	[RDMA_NLDEV_ATTR_SYS_IMAGE_GUID]	= { .type = NLA_U64 },
 153	[RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID]	= { .type = NLA_U32 },
 154	[RDMA_NLDEV_NET_NS_FD]			= { .type = NLA_U32 },
 155	[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]	= { .type = NLA_U8 },
 156	[RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK]	= { .type = NLA_U8 },
 
 
 
 157};
 158
 159static int put_driver_name_print_type(struct sk_buff *msg, const char *name,
 160				      enum rdma_nldev_print_type print_type)
 161{
 162	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name))
 163		return -EMSGSIZE;
 164	if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC &&
 165	    nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type))
 166		return -EMSGSIZE;
 167
 168	return 0;
 169}
 170
 171static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name,
 172				   enum rdma_nldev_print_type print_type,
 173				   u32 value)
 174{
 175	if (put_driver_name_print_type(msg, name, print_type))
 176		return -EMSGSIZE;
 177	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value))
 178		return -EMSGSIZE;
 179
 180	return 0;
 181}
 182
 183static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name,
 184				   enum rdma_nldev_print_type print_type,
 185				   u64 value)
 186{
 187	if (put_driver_name_print_type(msg, name, print_type))
 188		return -EMSGSIZE;
 189	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value,
 190			      RDMA_NLDEV_ATTR_PAD))
 191		return -EMSGSIZE;
 192
 193	return 0;
 194}
 195
 196int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
 197			      const char *str)
 198{
 199	if (put_driver_name_print_type(msg, name,
 200				       RDMA_NLDEV_PRINT_TYPE_UNSPEC))
 201		return -EMSGSIZE;
 202	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, str))
 203		return -EMSGSIZE;
 204
 205	return 0;
 206}
 207EXPORT_SYMBOL(rdma_nl_put_driver_string);
 208
 209int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value)
 210{
 211	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
 212				       value);
 213}
 214EXPORT_SYMBOL(rdma_nl_put_driver_u32);
 215
 216int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
 217			       u32 value)
 218{
 219	return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
 220				       value);
 221}
 222EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex);
 223
 224int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value)
 225{
 226	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
 227				       value);
 228}
 229EXPORT_SYMBOL(rdma_nl_put_driver_u64);
 230
 231int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value)
 232{
 233	return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
 234				       value);
 235}
 236EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex);
 237
 
 
 
 
 
 
 238static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
 239{
 240	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
 241		return -EMSGSIZE;
 242	if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME,
 243			   dev_name(&device->dev)))
 244		return -EMSGSIZE;
 245
 246	return 0;
 247}
 248
 249static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
 250{
 251	char fw[IB_FW_VERSION_NAME_MAX];
 252	int ret = 0;
 253	u32 port;
 254
 255	if (fill_nldev_handle(msg, device))
 256		return -EMSGSIZE;
 257
 258	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
 259		return -EMSGSIZE;
 260
 261	BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
 262	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
 263			      device->attrs.device_cap_flags,
 264			      RDMA_NLDEV_ATTR_PAD))
 265		return -EMSGSIZE;
 266
 267	ib_get_device_fw_str(device, fw);
 268	/* Device without FW has strlen(fw) = 0 */
 269	if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
 270		return -EMSGSIZE;
 271
 272	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
 273			      be64_to_cpu(device->node_guid),
 274			      RDMA_NLDEV_ATTR_PAD))
 275		return -EMSGSIZE;
 276	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
 277			      be64_to_cpu(device->attrs.sys_image_guid),
 278			      RDMA_NLDEV_ATTR_PAD))
 279		return -EMSGSIZE;
 280	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
 281		return -EMSGSIZE;
 282	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, device->use_cq_dim))
 283		return -EMSGSIZE;
 284
 285	/*
 286	 * Link type is determined on first port and mlx4 device
 287	 * which can potentially have two different link type for the same
 288	 * IB device is considered as better to be avoided in the future,
 289	 */
 290	port = rdma_start_port(device);
 291	if (rdma_cap_opa_mad(device, port))
 292		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "opa");
 293	else if (rdma_protocol_ib(device, port))
 294		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "ib");
 295	else if (rdma_protocol_iwarp(device, port))
 296		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "iw");
 297	else if (rdma_protocol_roce(device, port))
 298		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "roce");
 299	else if (rdma_protocol_usnic(device, port))
 300		ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL,
 301				     "usnic");
 302	return ret;
 303}
 304
 305static int fill_port_info(struct sk_buff *msg,
 306			  struct ib_device *device, u32 port,
 307			  const struct net *net)
 308{
 309	struct net_device *netdev = NULL;
 310	struct ib_port_attr attr;
 311	int ret;
 312	u64 cap_flags = 0;
 313
 314	if (fill_nldev_handle(msg, device))
 315		return -EMSGSIZE;
 316
 317	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
 318		return -EMSGSIZE;
 319
 320	ret = ib_query_port(device, port, &attr);
 321	if (ret)
 322		return ret;
 323
 324	if (rdma_protocol_ib(device, port)) {
 325		BUILD_BUG_ON((sizeof(attr.port_cap_flags) +
 326				sizeof(attr.port_cap_flags2)) > sizeof(u64));
 327		cap_flags = attr.port_cap_flags |
 328			((u64)attr.port_cap_flags2 << 32);
 329		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
 330				      cap_flags, RDMA_NLDEV_ATTR_PAD))
 331			return -EMSGSIZE;
 332		if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
 333				      attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD))
 334			return -EMSGSIZE;
 335		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
 336			return -EMSGSIZE;
 337		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
 338			return -EMSGSIZE;
 339		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
 340			return -EMSGSIZE;
 341	}
 342	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
 343		return -EMSGSIZE;
 344	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
 345		return -EMSGSIZE;
 346
 347	netdev = ib_device_get_netdev(device, port);
 348	if (netdev && net_eq(dev_net(netdev), net)) {
 349		ret = nla_put_u32(msg,
 350				  RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
 351		if (ret)
 352			goto out;
 353		ret = nla_put_string(msg,
 354				     RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
 355	}
 356
 357out:
 358	if (netdev)
 359		dev_put(netdev);
 360	return ret;
 361}
 362
 363static int fill_res_info_entry(struct sk_buff *msg,
 364			       const char *name, u64 curr)
 365{
 366	struct nlattr *entry_attr;
 367
 368	entry_attr = nla_nest_start_noflag(msg,
 369					   RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
 370	if (!entry_attr)
 371		return -EMSGSIZE;
 372
 373	if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
 374		goto err;
 375	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr,
 376			      RDMA_NLDEV_ATTR_PAD))
 377		goto err;
 378
 379	nla_nest_end(msg, entry_attr);
 380	return 0;
 381
 382err:
 383	nla_nest_cancel(msg, entry_attr);
 384	return -EMSGSIZE;
 385}
 386
 387static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
 388{
 389	static const char * const names[RDMA_RESTRACK_MAX] = {
 390		[RDMA_RESTRACK_PD] = "pd",
 391		[RDMA_RESTRACK_CQ] = "cq",
 392		[RDMA_RESTRACK_QP] = "qp",
 393		[RDMA_RESTRACK_CM_ID] = "cm_id",
 394		[RDMA_RESTRACK_MR] = "mr",
 395		[RDMA_RESTRACK_CTX] = "ctx",
 396		[RDMA_RESTRACK_SRQ] = "srq",
 397	};
 398
 399	struct nlattr *table_attr;
 400	int ret, i, curr;
 401
 402	if (fill_nldev_handle(msg, device))
 403		return -EMSGSIZE;
 404
 405	table_attr = nla_nest_start_noflag(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
 406	if (!table_attr)
 407		return -EMSGSIZE;
 408
 409	for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
 410		if (!names[i])
 411			continue;
 412		curr = rdma_restrack_count(device, i);
 413		ret = fill_res_info_entry(msg, names[i], curr);
 414		if (ret)
 415			goto err;
 416	}
 417
 418	nla_nest_end(msg, table_attr);
 419	return 0;
 420
 421err:
 422	nla_nest_cancel(msg, table_attr);
 423	return ret;
 424}
 425
 426static int fill_res_name_pid(struct sk_buff *msg,
 427			     struct rdma_restrack_entry *res)
 428{
 429	int err = 0;
 430
 431	/*
 432	 * For user resources, user is should read /proc/PID/comm to get the
 433	 * name of the task file.
 434	 */
 435	if (rdma_is_kernel_res(res)) {
 436		err = nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
 437				     res->kern_name);
 438	} else {
 439		pid_t pid;
 440
 441		pid = task_pid_vnr(res->task);
 442		/*
 443		 * Task is dead and in zombie state.
 444		 * There is no need to print PID anymore.
 445		 */
 446		if (pid)
 447			/*
 448			 * This part is racy, task can be killed and PID will
 449			 * be zero right here but it is ok, next query won't
 450			 * return PID. We don't promise real-time reflection
 451			 * of SW objects.
 452			 */
 453			err = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID, pid);
 454	}
 455
 456	return err ? -EMSGSIZE : 0;
 457}
 458
 459static int fill_res_qp_entry_query(struct sk_buff *msg,
 460				   struct rdma_restrack_entry *res,
 461				   struct ib_device *dev,
 462				   struct ib_qp *qp)
 463{
 464	struct ib_qp_init_attr qp_init_attr;
 465	struct ib_qp_attr qp_attr;
 466	int ret;
 467
 468	ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
 469	if (ret)
 470		return ret;
 471
 472	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
 473		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
 474				qp_attr.dest_qp_num))
 475			goto err;
 476		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
 477				qp_attr.rq_psn))
 478			goto err;
 479	}
 480
 481	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
 482		goto err;
 483
 484	if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
 485	    qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
 486		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
 487			       qp_attr.path_mig_state))
 488			goto err;
 489	}
 490	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
 491		goto err;
 492	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
 493		goto err;
 494
 495	if (dev->ops.fill_res_qp_entry)
 496		return dev->ops.fill_res_qp_entry(msg, qp);
 497	return 0;
 498
 499err:	return -EMSGSIZE;
 500}
 501
 502static int fill_res_qp_entry(struct sk_buff *msg, bool has_cap_net_admin,
 503			     struct rdma_restrack_entry *res, uint32_t port)
 504{
 505	struct ib_qp *qp = container_of(res, struct ib_qp, res);
 506	struct ib_device *dev = qp->device;
 507	int ret;
 508
 509	if (port && port != qp->port)
 510		return -EAGAIN;
 511
 512	/* In create_qp() port is not set yet */
 513	if (qp->port && nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp->port))
 514		return -EINVAL;
 515
 516	ret = nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num);
 517	if (ret)
 518		return -EMSGSIZE;
 519
 520	if (!rdma_is_kernel_res(res) &&
 521	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, qp->pd->res.id))
 522		return -EMSGSIZE;
 523
 524	ret = fill_res_name_pid(msg, res);
 525	if (ret)
 526		return -EMSGSIZE;
 527
 528	return fill_res_qp_entry_query(msg, res, dev, qp);
 529}
 530
 531static int fill_res_qp_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
 532				 struct rdma_restrack_entry *res, uint32_t port)
 533{
 534	struct ib_qp *qp = container_of(res, struct ib_qp, res);
 535	struct ib_device *dev = qp->device;
 536
 537	if (port && port != qp->port)
 538		return -EAGAIN;
 539	if (!dev->ops.fill_res_qp_entry_raw)
 540		return -EINVAL;
 541	return dev->ops.fill_res_qp_entry_raw(msg, qp);
 542}
 543
 544static int fill_res_cm_id_entry(struct sk_buff *msg, bool has_cap_net_admin,
 545				struct rdma_restrack_entry *res, uint32_t port)
 546{
 547	struct rdma_id_private *id_priv =
 548				container_of(res, struct rdma_id_private, res);
 549	struct ib_device *dev = id_priv->id.device;
 550	struct rdma_cm_id *cm_id = &id_priv->id;
 551
 552	if (port && port != cm_id->port_num)
 553		return 0;
 554
 555	if (cm_id->port_num &&
 556	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
 557		goto err;
 558
 559	if (id_priv->qp_num) {
 560		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
 561			goto err;
 562		if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
 563			goto err;
 564	}
 565
 566	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
 567		goto err;
 568
 569	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
 570		goto err;
 571
 572	if (cm_id->route.addr.src_addr.ss_family &&
 573	    nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
 574		    sizeof(cm_id->route.addr.src_addr),
 575		    &cm_id->route.addr.src_addr))
 576		goto err;
 577	if (cm_id->route.addr.dst_addr.ss_family &&
 578	    nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
 579		    sizeof(cm_id->route.addr.dst_addr),
 580		    &cm_id->route.addr.dst_addr))
 581		goto err;
 582
 583	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CM_IDN, res->id))
 584		goto err;
 585
 586	if (fill_res_name_pid(msg, res))
 587		goto err;
 588
 589	if (dev->ops.fill_res_cm_id_entry)
 590		return dev->ops.fill_res_cm_id_entry(msg, cm_id);
 591	return 0;
 592
 593err: return -EMSGSIZE;
 594}
 595
 596static int fill_res_cq_entry(struct sk_buff *msg, bool has_cap_net_admin,
 597			     struct rdma_restrack_entry *res, uint32_t port)
 598{
 599	struct ib_cq *cq = container_of(res, struct ib_cq, res);
 600	struct ib_device *dev = cq->device;
 601
 602	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
 603		return -EMSGSIZE;
 604	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
 605			      atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD))
 606		return -EMSGSIZE;
 607
 608	/* Poll context is only valid for kernel CQs */
 609	if (rdma_is_kernel_res(res) &&
 610	    nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
 611		return -EMSGSIZE;
 612
 613	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_DIM, (cq->dim != NULL)))
 614		return -EMSGSIZE;
 615
 616	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN, res->id))
 617		return -EMSGSIZE;
 618	if (!rdma_is_kernel_res(res) &&
 619	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
 620			cq->uobject->uevent.uobject.context->res.id))
 621		return -EMSGSIZE;
 622
 623	if (fill_res_name_pid(msg, res))
 624		return -EMSGSIZE;
 625
 626	return (dev->ops.fill_res_cq_entry) ?
 627		dev->ops.fill_res_cq_entry(msg, cq) : 0;
 628}
 629
 630static int fill_res_cq_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
 631				 struct rdma_restrack_entry *res, uint32_t port)
 632{
 633	struct ib_cq *cq = container_of(res, struct ib_cq, res);
 634	struct ib_device *dev = cq->device;
 635
 636	if (!dev->ops.fill_res_cq_entry_raw)
 637		return -EINVAL;
 638	return dev->ops.fill_res_cq_entry_raw(msg, cq);
 639}
 640
 641static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
 642			     struct rdma_restrack_entry *res, uint32_t port)
 643{
 644	struct ib_mr *mr = container_of(res, struct ib_mr, res);
 645	struct ib_device *dev = mr->pd->device;
 646
 647	if (has_cap_net_admin) {
 648		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
 649			return -EMSGSIZE;
 650		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
 651			return -EMSGSIZE;
 652	}
 653
 654	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length,
 655			      RDMA_NLDEV_ATTR_PAD))
 656		return -EMSGSIZE;
 657
 658	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
 659		return -EMSGSIZE;
 660
 661	if (!rdma_is_kernel_res(res) &&
 662	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, mr->pd->res.id))
 663		return -EMSGSIZE;
 664
 665	if (fill_res_name_pid(msg, res))
 666		return -EMSGSIZE;
 667
 668	return (dev->ops.fill_res_mr_entry) ?
 669		       dev->ops.fill_res_mr_entry(msg, mr) :
 670		       0;
 671}
 672
 673static int fill_res_mr_raw_entry(struct sk_buff *msg, bool has_cap_net_admin,
 674				 struct rdma_restrack_entry *res, uint32_t port)
 675{
 676	struct ib_mr *mr = container_of(res, struct ib_mr, res);
 677	struct ib_device *dev = mr->pd->device;
 678
 679	if (!dev->ops.fill_res_mr_entry_raw)
 680		return -EINVAL;
 681	return dev->ops.fill_res_mr_entry_raw(msg, mr);
 682}
 683
 684static int fill_res_pd_entry(struct sk_buff *msg, bool has_cap_net_admin,
 685			     struct rdma_restrack_entry *res, uint32_t port)
 686{
 687	struct ib_pd *pd = container_of(res, struct ib_pd, res);
 688
 689	if (has_cap_net_admin) {
 690		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
 691				pd->local_dma_lkey))
 692			goto err;
 693		if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
 694		    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
 695				pd->unsafe_global_rkey))
 696			goto err;
 697	}
 698	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
 699			      atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD))
 700		goto err;
 701
 702	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, res->id))
 703		goto err;
 704
 705	if (!rdma_is_kernel_res(res) &&
 706	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
 707			pd->uobject->context->res.id))
 708		goto err;
 709
 710	return fill_res_name_pid(msg, res);
 711
 712err:	return -EMSGSIZE;
 713}
 714
 715static int fill_res_ctx_entry(struct sk_buff *msg, bool has_cap_net_admin,
 716			      struct rdma_restrack_entry *res, uint32_t port)
 717{
 718	struct ib_ucontext *ctx = container_of(res, struct ib_ucontext, res);
 719
 720	if (rdma_is_kernel_res(res))
 721		return 0;
 722
 723	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN, ctx->res.id))
 724		return -EMSGSIZE;
 725
 726	return fill_res_name_pid(msg, res);
 727}
 728
 729static int fill_res_range_qp_entry(struct sk_buff *msg, uint32_t min_range,
 730				   uint32_t max_range)
 731{
 732	struct nlattr *entry_attr;
 733
 734	if (!min_range)
 735		return 0;
 736
 737	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
 738	if (!entry_attr)
 739		return -EMSGSIZE;
 740
 741	if (min_range == max_range) {
 742		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, min_range))
 743			goto err;
 744	} else {
 745		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MIN_RANGE, min_range))
 746			goto err;
 747		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_MAX_RANGE, max_range))
 748			goto err;
 749	}
 750	nla_nest_end(msg, entry_attr);
 751	return 0;
 752
 753err:
 754	nla_nest_cancel(msg, entry_attr);
 755	return -EMSGSIZE;
 756}
 757
 758static int fill_res_srq_qps(struct sk_buff *msg, struct ib_srq *srq)
 759{
 760	uint32_t min_range = 0, prev = 0;
 761	struct rdma_restrack_entry *res;
 762	struct rdma_restrack_root *rt;
 763	struct nlattr *table_attr;
 764	struct ib_qp *qp = NULL;
 765	unsigned long id = 0;
 766
 767	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
 768	if (!table_attr)
 769		return -EMSGSIZE;
 770
 771	rt = &srq->device->res[RDMA_RESTRACK_QP];
 772	xa_lock(&rt->xa);
 773	xa_for_each(&rt->xa, id, res) {
 774		if (!rdma_restrack_get(res))
 775			continue;
 776
 777		qp = container_of(res, struct ib_qp, res);
 778		if (!qp->srq || (qp->srq->res.id != srq->res.id)) {
 779			rdma_restrack_put(res);
 780			continue;
 781		}
 782
 783		if (qp->qp_num < prev)
 784			/* qp_num should be ascending */
 785			goto err_loop;
 786
 787		if (min_range == 0) {
 788			min_range = qp->qp_num;
 789		} else if (qp->qp_num > (prev + 1)) {
 790			if (fill_res_range_qp_entry(msg, min_range, prev))
 791				goto err_loop;
 792
 793			min_range = qp->qp_num;
 794		}
 795		prev = qp->qp_num;
 796		rdma_restrack_put(res);
 797	}
 798
 799	xa_unlock(&rt->xa);
 800
 801	if (fill_res_range_qp_entry(msg, min_range, prev))
 802		goto err;
 803
 804	nla_nest_end(msg, table_attr);
 805	return 0;
 806
 807err_loop:
 808	rdma_restrack_put(res);
 809	xa_unlock(&rt->xa);
 810err:
 811	nla_nest_cancel(msg, table_attr);
 812	return -EMSGSIZE;
 813}
 814
 815static int fill_res_srq_entry(struct sk_buff *msg, bool has_cap_net_admin,
 816			      struct rdma_restrack_entry *res, uint32_t port)
 817{
 818	struct ib_srq *srq = container_of(res, struct ib_srq, res);
 
 819
 820	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SRQN, srq->res.id))
 821		goto err;
 822
 823	if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, srq->srq_type))
 824		goto err;
 825
 826	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, srq->pd->res.id))
 827		goto err;
 828
 829	if (ib_srq_has_cq(srq->srq_type)) {
 830		if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN,
 831				srq->ext.cq->res.id))
 832			goto err;
 833	}
 834
 835	if (fill_res_srq_qps(msg, srq))
 836		goto err;
 837
 838	return fill_res_name_pid(msg, res);
 
 
 
 
 
 
 839
 840err:
 841	return -EMSGSIZE;
 842}
 843
 
 
 
 
 
 
 
 
 
 
 
 844static int fill_stat_counter_mode(struct sk_buff *msg,
 845				  struct rdma_counter *counter)
 846{
 847	struct rdma_counter_mode *m = &counter->mode;
 848
 849	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, m->mode))
 850		return -EMSGSIZE;
 851
 852	if (m->mode == RDMA_COUNTER_MODE_AUTO) {
 853		if ((m->mask & RDMA_COUNTER_MASK_QP_TYPE) &&
 854		    nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, m->param.qp_type))
 855			return -EMSGSIZE;
 856
 857		if ((m->mask & RDMA_COUNTER_MASK_PID) &&
 858		    fill_res_name_pid(msg, &counter->res))
 859			return -EMSGSIZE;
 860	}
 861
 862	return 0;
 863}
 864
 865static int fill_stat_counter_qp_entry(struct sk_buff *msg, u32 qpn)
 866{
 867	struct nlattr *entry_attr;
 868
 869	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
 870	if (!entry_attr)
 871		return -EMSGSIZE;
 872
 873	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn))
 874		goto err;
 875
 876	nla_nest_end(msg, entry_attr);
 877	return 0;
 878
 879err:
 880	nla_nest_cancel(msg, entry_attr);
 881	return -EMSGSIZE;
 882}
 883
 884static int fill_stat_counter_qps(struct sk_buff *msg,
 885				 struct rdma_counter *counter)
 886{
 887	struct rdma_restrack_entry *res;
 888	struct rdma_restrack_root *rt;
 889	struct nlattr *table_attr;
 890	struct ib_qp *qp = NULL;
 891	unsigned long id = 0;
 892	int ret = 0;
 893
 894	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP);
 
 
 895
 896	rt = &counter->device->res[RDMA_RESTRACK_QP];
 897	xa_lock(&rt->xa);
 898	xa_for_each(&rt->xa, id, res) {
 899		qp = container_of(res, struct ib_qp, res);
 900		if (!qp->counter || (qp->counter->id != counter->id))
 901			continue;
 902
 903		ret = fill_stat_counter_qp_entry(msg, qp->qp_num);
 904		if (ret)
 905			goto err;
 906	}
 907
 908	xa_unlock(&rt->xa);
 909	nla_nest_end(msg, table_attr);
 910	return 0;
 911
 912err:
 913	xa_unlock(&rt->xa);
 914	nla_nest_cancel(msg, table_attr);
 915	return ret;
 916}
 917
 918int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
 919				 u64 value)
 920{
 921	struct nlattr *entry_attr;
 922
 923	entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY);
 924	if (!entry_attr)
 925		return -EMSGSIZE;
 926
 927	if (nla_put_string(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME,
 928			   name))
 929		goto err;
 930	if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE,
 931			      value, RDMA_NLDEV_ATTR_PAD))
 932		goto err;
 933
 934	nla_nest_end(msg, entry_attr);
 935	return 0;
 936
 937err:
 938	nla_nest_cancel(msg, entry_attr);
 939	return -EMSGSIZE;
 940}
 941EXPORT_SYMBOL(rdma_nl_stat_hwcounter_entry);
 942
 943static int fill_stat_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
 944			      struct rdma_restrack_entry *res, uint32_t port)
 945{
 946	struct ib_mr *mr = container_of(res, struct ib_mr, res);
 947	struct ib_device *dev = mr->pd->device;
 948
 949	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
 950		goto err;
 951
 952	if (dev->ops.fill_stat_mr_entry)
 953		return dev->ops.fill_stat_mr_entry(msg, mr);
 954	return 0;
 955
 956err:
 957	return -EMSGSIZE;
 958}
 959
 960static int fill_stat_counter_hwcounters(struct sk_buff *msg,
 961					struct rdma_counter *counter)
 962{
 963	struct rdma_hw_stats *st = counter->stats;
 964	struct nlattr *table_attr;
 965	int i;
 966
 967	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
 968	if (!table_attr)
 969		return -EMSGSIZE;
 970
 971	for (i = 0; i < st->num_counters; i++)
 972		if (rdma_nl_stat_hwcounter_entry(msg, st->names[i], st->value[i]))
 
 
 
 
 973			goto err;
 
 
 974
 975	nla_nest_end(msg, table_attr);
 976	return 0;
 977
 978err:
 
 979	nla_nest_cancel(msg, table_attr);
 980	return -EMSGSIZE;
 981}
 982
 983static int fill_res_counter_entry(struct sk_buff *msg, bool has_cap_net_admin,
 984				  struct rdma_restrack_entry *res,
 985				  uint32_t port)
 986{
 987	struct rdma_counter *counter =
 988		container_of(res, struct rdma_counter, res);
 989
 990	if (port && port != counter->port)
 991		return -EAGAIN;
 992
 993	/* Dump it even query failed */
 994	rdma_counter_query_stats(counter);
 995
 996	if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, counter->port) ||
 997	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, counter->id) ||
 998	    fill_stat_counter_mode(msg, counter) ||
 999	    fill_stat_counter_qps(msg, counter) ||
1000	    fill_stat_counter_hwcounters(msg, counter))
1001		return -EMSGSIZE;
1002
1003	return 0;
1004}
1005
1006static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1007			  struct netlink_ext_ack *extack)
1008{
1009	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1010	struct ib_device *device;
1011	struct sk_buff *msg;
1012	u32 index;
1013	int err;
1014
1015	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1016				     nldev_policy, extack);
1017	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1018		return -EINVAL;
1019
1020	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1021
1022	device = ib_device_get_by_index(sock_net(skb->sk), index);
1023	if (!device)
1024		return -EINVAL;
1025
1026	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1027	if (!msg) {
1028		err = -ENOMEM;
1029		goto err;
1030	}
1031
1032	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1033			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1034			0, 0);
 
 
 
 
1035
1036	err = fill_dev_info(msg, device);
1037	if (err)
1038		goto err_free;
1039
1040	nlmsg_end(msg, nlh);
1041
1042	ib_device_put(device);
1043	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1044
1045err_free:
1046	nlmsg_free(msg);
1047err:
1048	ib_device_put(device);
1049	return err;
1050}
1051
1052static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1053			  struct netlink_ext_ack *extack)
1054{
1055	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1056	struct ib_device *device;
1057	u32 index;
1058	int err;
1059
1060	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1061				     nldev_policy, extack);
1062	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1063		return -EINVAL;
1064
1065	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1066	device = ib_device_get_by_index(sock_net(skb->sk), index);
1067	if (!device)
1068		return -EINVAL;
1069
1070	if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) {
1071		char name[IB_DEVICE_NAME_MAX] = {};
1072
1073		nla_strscpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1074			    IB_DEVICE_NAME_MAX);
1075		if (strlen(name) == 0) {
1076			err = -EINVAL;
1077			goto done;
1078		}
1079		err = ib_device_rename(device, name);
1080		goto done;
1081	}
1082
1083	if (tb[RDMA_NLDEV_NET_NS_FD]) {
1084		u32 ns_fd;
1085
1086		ns_fd = nla_get_u32(tb[RDMA_NLDEV_NET_NS_FD]);
1087		err = ib_device_set_netns_put(skb, device, ns_fd);
1088		goto put_done;
1089	}
1090
1091	if (tb[RDMA_NLDEV_ATTR_DEV_DIM]) {
1092		u8 use_dim;
1093
1094		use_dim = nla_get_u8(tb[RDMA_NLDEV_ATTR_DEV_DIM]);
1095		err = ib_device_set_dim(device,  use_dim);
1096		goto done;
1097	}
1098
1099done:
1100	ib_device_put(device);
1101put_done:
1102	return err;
1103}
1104
1105static int _nldev_get_dumpit(struct ib_device *device,
1106			     struct sk_buff *skb,
1107			     struct netlink_callback *cb,
1108			     unsigned int idx)
1109{
1110	int start = cb->args[0];
1111	struct nlmsghdr *nlh;
1112
1113	if (idx < start)
1114		return 0;
1115
1116	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1117			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1118			0, NLM_F_MULTI);
1119
1120	if (fill_dev_info(skb, device)) {
1121		nlmsg_cancel(skb, nlh);
1122		goto out;
1123	}
1124
1125	nlmsg_end(skb, nlh);
1126
1127	idx++;
1128
1129out:	cb->args[0] = idx;
1130	return skb->len;
1131}
1132
1133static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
1134{
1135	/*
1136	 * There is no need to take lock, because
1137	 * we are relying on ib_core's locking.
1138	 */
1139	return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
1140}
1141
1142static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1143			       struct netlink_ext_ack *extack)
1144{
1145	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1146	struct ib_device *device;
1147	struct sk_buff *msg;
1148	u32 index;
1149	u32 port;
1150	int err;
1151
1152	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1153				     nldev_policy, extack);
1154	if (err ||
1155	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
1156	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
1157		return -EINVAL;
1158
1159	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1160	device = ib_device_get_by_index(sock_net(skb->sk), index);
1161	if (!device)
1162		return -EINVAL;
1163
1164	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1165	if (!rdma_is_port_valid(device, port)) {
1166		err = -EINVAL;
1167		goto err;
1168	}
1169
1170	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1171	if (!msg) {
1172		err = -ENOMEM;
1173		goto err;
1174	}
1175
1176	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1177			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
1178			0, 0);
 
 
 
 
1179
1180	err = fill_port_info(msg, device, port, sock_net(skb->sk));
1181	if (err)
1182		goto err_free;
1183
1184	nlmsg_end(msg, nlh);
1185	ib_device_put(device);
1186
1187	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1188
1189err_free:
1190	nlmsg_free(msg);
1191err:
1192	ib_device_put(device);
1193	return err;
1194}
1195
1196static int nldev_port_get_dumpit(struct sk_buff *skb,
1197				 struct netlink_callback *cb)
1198{
1199	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1200	struct ib_device *device;
1201	int start = cb->args[0];
1202	struct nlmsghdr *nlh;
1203	u32 idx = 0;
1204	u32 ifindex;
1205	int err;
1206	unsigned int p;
1207
1208	err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1209				     nldev_policy, NULL);
1210	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1211		return -EINVAL;
1212
1213	ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1214	device = ib_device_get_by_index(sock_net(skb->sk), ifindex);
1215	if (!device)
1216		return -EINVAL;
1217
1218	rdma_for_each_port (device, p) {
1219		/*
1220		 * The dumpit function returns all information from specific
1221		 * index. This specific index is taken from the netlink
1222		 * messages request sent by user and it is available
1223		 * in cb->args[0].
1224		 *
1225		 * Usually, the user doesn't fill this field and it causes
1226		 * to return everything.
1227		 *
1228		 */
1229		if (idx < start) {
1230			idx++;
1231			continue;
1232		}
1233
1234		nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
1235				cb->nlh->nlmsg_seq,
1236				RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1237						 RDMA_NLDEV_CMD_PORT_GET),
1238				0, NLM_F_MULTI);
1239
1240		if (fill_port_info(skb, device, p, sock_net(skb->sk))) {
1241			nlmsg_cancel(skb, nlh);
1242			goto out;
1243		}
1244		idx++;
1245		nlmsg_end(skb, nlh);
1246	}
1247
1248out:
1249	ib_device_put(device);
1250	cb->args[0] = idx;
1251	return skb->len;
1252}
1253
1254static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1255			      struct netlink_ext_ack *extack)
1256{
1257	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1258	struct ib_device *device;
1259	struct sk_buff *msg;
1260	u32 index;
1261	int ret;
1262
1263	ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1264				     nldev_policy, extack);
1265	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1266		return -EINVAL;
1267
1268	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1269	device = ib_device_get_by_index(sock_net(skb->sk), index);
1270	if (!device)
1271		return -EINVAL;
1272
1273	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1274	if (!msg) {
1275		ret = -ENOMEM;
1276		goto err;
1277	}
1278
1279	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1280			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1281			0, 0);
 
 
 
 
1282
1283	ret = fill_res_info(msg, device);
1284	if (ret)
1285		goto err_free;
1286
1287	nlmsg_end(msg, nlh);
1288	ib_device_put(device);
1289	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1290
1291err_free:
1292	nlmsg_free(msg);
1293err:
1294	ib_device_put(device);
1295	return ret;
1296}
1297
1298static int _nldev_res_get_dumpit(struct ib_device *device,
1299				 struct sk_buff *skb,
1300				 struct netlink_callback *cb,
1301				 unsigned int idx)
1302{
1303	int start = cb->args[0];
1304	struct nlmsghdr *nlh;
1305
1306	if (idx < start)
1307		return 0;
1308
1309	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1310			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
1311			0, NLM_F_MULTI);
1312
1313	if (fill_res_info(skb, device)) {
1314		nlmsg_cancel(skb, nlh);
1315		goto out;
1316	}
1317	nlmsg_end(skb, nlh);
1318
1319	idx++;
1320
1321out:
1322	cb->args[0] = idx;
1323	return skb->len;
1324}
1325
1326static int nldev_res_get_dumpit(struct sk_buff *skb,
1327				struct netlink_callback *cb)
1328{
1329	return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
1330}
1331
1332struct nldev_fill_res_entry {
1333	enum rdma_nldev_attr nldev_attr;
1334	u8 flags;
1335	u32 entry;
1336	u32 id;
1337};
1338
1339enum nldev_res_flags {
1340	NLDEV_PER_DEV = 1 << 0,
1341};
1342
1343static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
1344	[RDMA_RESTRACK_QP] = {
1345		.nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
1346		.entry = RDMA_NLDEV_ATTR_RES_QP_ENTRY,
1347		.id = RDMA_NLDEV_ATTR_RES_LQPN,
1348	},
1349	[RDMA_RESTRACK_CM_ID] = {
1350		.nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
1351		.entry = RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY,
1352		.id = RDMA_NLDEV_ATTR_RES_CM_IDN,
1353	},
1354	[RDMA_RESTRACK_CQ] = {
1355		.nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
1356		.flags = NLDEV_PER_DEV,
1357		.entry = RDMA_NLDEV_ATTR_RES_CQ_ENTRY,
1358		.id = RDMA_NLDEV_ATTR_RES_CQN,
1359	},
1360	[RDMA_RESTRACK_MR] = {
1361		.nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
1362		.flags = NLDEV_PER_DEV,
1363		.entry = RDMA_NLDEV_ATTR_RES_MR_ENTRY,
1364		.id = RDMA_NLDEV_ATTR_RES_MRN,
1365	},
1366	[RDMA_RESTRACK_PD] = {
1367		.nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
1368		.flags = NLDEV_PER_DEV,
1369		.entry = RDMA_NLDEV_ATTR_RES_PD_ENTRY,
1370		.id = RDMA_NLDEV_ATTR_RES_PDN,
1371	},
1372	[RDMA_RESTRACK_COUNTER] = {
1373		.nldev_attr = RDMA_NLDEV_ATTR_STAT_COUNTER,
1374		.entry = RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY,
1375		.id = RDMA_NLDEV_ATTR_STAT_COUNTER_ID,
1376	},
1377	[RDMA_RESTRACK_CTX] = {
1378		.nldev_attr = RDMA_NLDEV_ATTR_RES_CTX,
1379		.flags = NLDEV_PER_DEV,
1380		.entry = RDMA_NLDEV_ATTR_RES_CTX_ENTRY,
1381		.id = RDMA_NLDEV_ATTR_RES_CTXN,
1382	},
1383	[RDMA_RESTRACK_SRQ] = {
1384		.nldev_attr = RDMA_NLDEV_ATTR_RES_SRQ,
1385		.flags = NLDEV_PER_DEV,
1386		.entry = RDMA_NLDEV_ATTR_RES_SRQ_ENTRY,
1387		.id = RDMA_NLDEV_ATTR_RES_SRQN,
1388	},
1389
1390};
1391
1392static int res_get_common_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1393			       struct netlink_ext_ack *extack,
1394			       enum rdma_restrack_type res_type,
1395			       res_fill_func_t fill_func)
1396{
1397	const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1398	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1399	struct rdma_restrack_entry *res;
1400	struct ib_device *device;
1401	u32 index, id, port = 0;
1402	bool has_cap_net_admin;
1403	struct sk_buff *msg;
1404	int ret;
1405
1406	ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1407				     nldev_policy, extack);
1408	if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !fe->id || !tb[fe->id])
1409		return -EINVAL;
1410
1411	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1412	device = ib_device_get_by_index(sock_net(skb->sk), index);
1413	if (!device)
1414		return -EINVAL;
1415
1416	if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1417		port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1418		if (!rdma_is_port_valid(device, port)) {
1419			ret = -EINVAL;
1420			goto err;
1421		}
1422	}
1423
1424	if ((port && fe->flags & NLDEV_PER_DEV) ||
1425	    (!port && ~fe->flags & NLDEV_PER_DEV)) {
1426		ret = -EINVAL;
1427		goto err;
1428	}
1429
1430	id = nla_get_u32(tb[fe->id]);
1431	res = rdma_restrack_get_byid(device, res_type, id);
1432	if (IS_ERR(res)) {
1433		ret = PTR_ERR(res);
1434		goto err;
1435	}
1436
1437	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1438	if (!msg) {
1439		ret = -ENOMEM;
1440		goto err_get;
1441	}
1442
1443	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1444			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1445					 RDMA_NL_GET_OP(nlh->nlmsg_type)),
1446			0, 0);
1447
1448	if (fill_nldev_handle(msg, device)) {
1449		ret = -EMSGSIZE;
1450		goto err_free;
1451	}
1452
1453	has_cap_net_admin = netlink_capable(skb, CAP_NET_ADMIN);
1454
1455	ret = fill_func(msg, has_cap_net_admin, res, port);
1456	if (ret)
1457		goto err_free;
1458
1459	rdma_restrack_put(res);
1460	nlmsg_end(msg, nlh);
1461	ib_device_put(device);
1462	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1463
1464err_free:
1465	nlmsg_free(msg);
1466err_get:
1467	rdma_restrack_put(res);
1468err:
1469	ib_device_put(device);
1470	return ret;
1471}
1472
1473static int res_get_common_dumpit(struct sk_buff *skb,
1474				 struct netlink_callback *cb,
1475				 enum rdma_restrack_type res_type,
1476				 res_fill_func_t fill_func)
1477{
1478	const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1479	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1480	struct rdma_restrack_entry *res;
1481	struct rdma_restrack_root *rt;
1482	int err, ret = 0, idx = 0;
1483	struct nlattr *table_attr;
1484	struct nlattr *entry_attr;
1485	struct ib_device *device;
1486	int start = cb->args[0];
1487	bool has_cap_net_admin;
1488	struct nlmsghdr *nlh;
1489	unsigned long id;
1490	u32 index, port = 0;
1491	bool filled = false;
1492
1493	err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1494				     nldev_policy, NULL);
1495	/*
1496	 * Right now, we are expecting the device index to get res information,
1497	 * but it is possible to extend this code to return all devices in
1498	 * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
1499	 * if it doesn't exist, we will iterate over all devices.
1500	 *
1501	 * But it is not needed for now.
1502	 */
1503	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1504		return -EINVAL;
1505
1506	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1507	device = ib_device_get_by_index(sock_net(skb->sk), index);
1508	if (!device)
1509		return -EINVAL;
1510
1511	/*
1512	 * If no PORT_INDEX is supplied, we will return all QPs from that device
1513	 */
1514	if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1515		port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1516		if (!rdma_is_port_valid(device, port)) {
1517			ret = -EINVAL;
1518			goto err_index;
1519		}
1520	}
1521
1522	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1523			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1524					 RDMA_NL_GET_OP(cb->nlh->nlmsg_type)),
1525			0, NLM_F_MULTI);
1526
1527	if (fill_nldev_handle(skb, device)) {
1528		ret = -EMSGSIZE;
1529		goto err;
1530	}
1531
1532	table_attr = nla_nest_start_noflag(skb, fe->nldev_attr);
1533	if (!table_attr) {
1534		ret = -EMSGSIZE;
1535		goto err;
1536	}
1537
1538	has_cap_net_admin = netlink_capable(cb->skb, CAP_NET_ADMIN);
1539
1540	rt = &device->res[res_type];
1541	xa_lock(&rt->xa);
1542	/*
1543	 * FIXME: if the skip ahead is something common this loop should
1544	 * use xas_for_each & xas_pause to optimize, we can have a lot of
1545	 * objects.
1546	 */
1547	xa_for_each(&rt->xa, id, res) {
1548		if (idx < start || !rdma_restrack_get(res))
1549			goto next;
1550
1551		xa_unlock(&rt->xa);
1552
1553		filled = true;
1554
1555		entry_attr = nla_nest_start_noflag(skb, fe->entry);
1556		if (!entry_attr) {
1557			ret = -EMSGSIZE;
1558			rdma_restrack_put(res);
1559			goto msg_full;
1560		}
1561
1562		ret = fill_func(skb, has_cap_net_admin, res, port);
1563
1564		rdma_restrack_put(res);
1565
1566		if (ret) {
1567			nla_nest_cancel(skb, entry_attr);
1568			if (ret == -EMSGSIZE)
1569				goto msg_full;
1570			if (ret == -EAGAIN)
1571				goto again;
1572			goto res_err;
1573		}
1574		nla_nest_end(skb, entry_attr);
1575again:		xa_lock(&rt->xa);
1576next:		idx++;
1577	}
1578	xa_unlock(&rt->xa);
1579
1580msg_full:
1581	nla_nest_end(skb, table_attr);
1582	nlmsg_end(skb, nlh);
1583	cb->args[0] = idx;
1584
1585	/*
1586	 * No more entries to fill, cancel the message and
1587	 * return 0 to mark end of dumpit.
1588	 */
1589	if (!filled)
1590		goto err;
1591
1592	ib_device_put(device);
1593	return skb->len;
1594
1595res_err:
1596	nla_nest_cancel(skb, table_attr);
1597
1598err:
1599	nlmsg_cancel(skb, nlh);
1600
1601err_index:
1602	ib_device_put(device);
1603	return ret;
1604}
1605
1606#define RES_GET_FUNCS(name, type)                                              \
1607	static int nldev_res_get_##name##_dumpit(struct sk_buff *skb,          \
1608						 struct netlink_callback *cb)  \
1609	{                                                                      \
1610		return res_get_common_dumpit(skb, cb, type,                    \
1611					     fill_res_##name##_entry);         \
1612	}                                                                      \
1613	static int nldev_res_get_##name##_doit(struct sk_buff *skb,            \
1614					       struct nlmsghdr *nlh,           \
1615					       struct netlink_ext_ack *extack) \
1616	{                                                                      \
1617		return res_get_common_doit(skb, nlh, extack, type,             \
1618					   fill_res_##name##_entry);           \
1619	}
1620
1621RES_GET_FUNCS(qp, RDMA_RESTRACK_QP);
1622RES_GET_FUNCS(qp_raw, RDMA_RESTRACK_QP);
1623RES_GET_FUNCS(cm_id, RDMA_RESTRACK_CM_ID);
1624RES_GET_FUNCS(cq, RDMA_RESTRACK_CQ);
1625RES_GET_FUNCS(cq_raw, RDMA_RESTRACK_CQ);
1626RES_GET_FUNCS(pd, RDMA_RESTRACK_PD);
1627RES_GET_FUNCS(mr, RDMA_RESTRACK_MR);
1628RES_GET_FUNCS(mr_raw, RDMA_RESTRACK_MR);
1629RES_GET_FUNCS(counter, RDMA_RESTRACK_COUNTER);
1630RES_GET_FUNCS(ctx, RDMA_RESTRACK_CTX);
1631RES_GET_FUNCS(srq, RDMA_RESTRACK_SRQ);
 
1632
1633static LIST_HEAD(link_ops);
1634static DECLARE_RWSEM(link_ops_rwsem);
1635
1636static const struct rdma_link_ops *link_ops_get(const char *type)
1637{
1638	const struct rdma_link_ops *ops;
1639
1640	list_for_each_entry(ops, &link_ops, list) {
1641		if (!strcmp(ops->type, type))
1642			goto out;
1643	}
1644	ops = NULL;
1645out:
1646	return ops;
1647}
1648
1649void rdma_link_register(struct rdma_link_ops *ops)
1650{
1651	down_write(&link_ops_rwsem);
1652	if (WARN_ON_ONCE(link_ops_get(ops->type)))
1653		goto out;
1654	list_add(&ops->list, &link_ops);
1655out:
1656	up_write(&link_ops_rwsem);
1657}
1658EXPORT_SYMBOL(rdma_link_register);
1659
1660void rdma_link_unregister(struct rdma_link_ops *ops)
1661{
1662	down_write(&link_ops_rwsem);
1663	list_del(&ops->list);
1664	up_write(&link_ops_rwsem);
1665}
1666EXPORT_SYMBOL(rdma_link_unregister);
1667
1668static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
1669			  struct netlink_ext_ack *extack)
1670{
1671	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1672	char ibdev_name[IB_DEVICE_NAME_MAX];
1673	const struct rdma_link_ops *ops;
1674	char ndev_name[IFNAMSIZ];
1675	struct net_device *ndev;
1676	char type[IFNAMSIZ];
1677	int err;
1678
1679	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1680				     nldev_policy, extack);
1681	if (err || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
1682	    !tb[RDMA_NLDEV_ATTR_LINK_TYPE] || !tb[RDMA_NLDEV_ATTR_NDEV_NAME])
1683		return -EINVAL;
1684
1685	nla_strscpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1686		    sizeof(ibdev_name));
1687	if (strchr(ibdev_name, '%') || strlen(ibdev_name) == 0)
1688		return -EINVAL;
1689
1690	nla_strscpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type));
1691	nla_strscpy(ndev_name, tb[RDMA_NLDEV_ATTR_NDEV_NAME],
1692		    sizeof(ndev_name));
1693
1694	ndev = dev_get_by_name(sock_net(skb->sk), ndev_name);
1695	if (!ndev)
1696		return -ENODEV;
1697
1698	down_read(&link_ops_rwsem);
1699	ops = link_ops_get(type);
1700#ifdef CONFIG_MODULES
1701	if (!ops) {
1702		up_read(&link_ops_rwsem);
1703		request_module("rdma-link-%s", type);
1704		down_read(&link_ops_rwsem);
1705		ops = link_ops_get(type);
1706	}
1707#endif
1708	err = ops ? ops->newlink(ibdev_name, ndev) : -EINVAL;
1709	up_read(&link_ops_rwsem);
1710	dev_put(ndev);
1711
1712	return err;
1713}
1714
1715static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
1716			  struct netlink_ext_ack *extack)
1717{
1718	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1719	struct ib_device *device;
1720	u32 index;
1721	int err;
1722
1723	err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1724				     nldev_policy, extack);
1725	if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1726		return -EINVAL;
1727
1728	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1729	device = ib_device_get_by_index(sock_net(skb->sk), index);
1730	if (!device)
1731		return -EINVAL;
1732
1733	if (!(device->attrs.device_cap_flags & IB_DEVICE_ALLOW_USER_UNREG)) {
1734		ib_device_put(device);
1735		return -EINVAL;
1736	}
1737
1738	ib_unregister_device_and_put(device);
1739	return 0;
1740}
1741
1742static int nldev_get_chardev(struct sk_buff *skb, struct nlmsghdr *nlh,
1743			     struct netlink_ext_ack *extack)
1744{
1745	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1746	char client_name[RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE];
1747	struct ib_client_nl_info data = {};
1748	struct ib_device *ibdev = NULL;
1749	struct sk_buff *msg;
1750	u32 index;
1751	int err;
1752
1753	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
1754			  extack);
1755	if (err || !tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE])
1756		return -EINVAL;
1757
1758	nla_strscpy(client_name, tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE],
1759		    sizeof(client_name));
1760
1761	if (tb[RDMA_NLDEV_ATTR_DEV_INDEX]) {
1762		index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1763		ibdev = ib_device_get_by_index(sock_net(skb->sk), index);
1764		if (!ibdev)
1765			return -EINVAL;
1766
1767		if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1768			data.port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1769			if (!rdma_is_port_valid(ibdev, data.port)) {
1770				err = -EINVAL;
1771				goto out_put;
1772			}
1773		} else {
1774			data.port = -1;
1775		}
1776	} else if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1777		return -EINVAL;
1778	}
1779
1780	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1781	if (!msg) {
1782		err = -ENOMEM;
1783		goto out_put;
1784	}
1785	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1786			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1787					 RDMA_NLDEV_CMD_GET_CHARDEV),
1788			0, 0);
 
 
 
 
1789
1790	data.nl_msg = msg;
1791	err = ib_get_client_nl_info(ibdev, client_name, &data);
1792	if (err)
1793		goto out_nlmsg;
1794
1795	err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV,
1796				huge_encode_dev(data.cdev->devt),
1797				RDMA_NLDEV_ATTR_PAD);
1798	if (err)
1799		goto out_data;
1800	err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV_ABI, data.abi,
1801				RDMA_NLDEV_ATTR_PAD);
1802	if (err)
1803		goto out_data;
1804	if (nla_put_string(msg, RDMA_NLDEV_ATTR_CHARDEV_NAME,
1805			   dev_name(data.cdev))) {
1806		err = -EMSGSIZE;
1807		goto out_data;
1808	}
1809
1810	nlmsg_end(msg, nlh);
1811	put_device(data.cdev);
1812	if (ibdev)
1813		ib_device_put(ibdev);
1814	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1815
1816out_data:
1817	put_device(data.cdev);
1818out_nlmsg:
1819	nlmsg_free(msg);
1820out_put:
1821	if (ibdev)
1822		ib_device_put(ibdev);
1823	return err;
1824}
1825
1826static int nldev_sys_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1827			      struct netlink_ext_ack *extack)
1828{
1829	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1830	struct sk_buff *msg;
1831	int err;
1832
1833	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1834			  nldev_policy, extack);
1835	if (err)
1836		return err;
1837
1838	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1839	if (!msg)
1840		return -ENOMEM;
1841
1842	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1843			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1844					 RDMA_NLDEV_CMD_SYS_GET),
1845			0, 0);
 
 
 
 
1846
1847	err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_NETNS_MODE,
1848			 (u8)ib_devices_shared_netns);
1849	if (err) {
1850		nlmsg_free(msg);
1851		return err;
1852	}
1853
 
 
 
 
 
 
1854	/*
1855	 * Copy-on-fork is supported.
1856	 * See commits:
1857	 * 70e806e4e645 ("mm: Do early cow for pinned pages during fork() for ptes")
1858	 * 4eae4efa2c29 ("hugetlb: do early cow when page pinned on src mm")
1859	 * for more details. Don't backport this without them.
1860	 *
1861	 * Return value ignored on purpose, assume copy-on-fork is not
1862	 * supported in case of failure.
1863	 */
1864	nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK, 1);
1865
1866	nlmsg_end(msg, nlh);
1867	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1868}
1869
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1870static int nldev_set_sys_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1871				  struct netlink_ext_ack *extack)
1872{
1873	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1874	u8 enable;
1875	int err;
1876
1877	err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1878			  nldev_policy, extack);
1879	if (err || !tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1880		return -EINVAL;
1881
1882	enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]);
1883	/* Only 0 and 1 are supported */
1884	if (enable > 1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1885		return -EINVAL;
1886
1887	err = rdma_compatdev_set(enable);
1888	return err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1889}
1890
1891static int nldev_stat_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1892			       struct netlink_ext_ack *extack)
1893{
1894	u32 index, port, mode, mask = 0, qpn, cntn = 0;
1895	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1896	struct ib_device *device;
1897	struct sk_buff *msg;
 
1898	int ret;
1899
1900	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1901			  nldev_policy, extack);
1902	/* Currently only counter for QP is supported */
1903	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES] ||
1904	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
1905	    !tb[RDMA_NLDEV_ATTR_PORT_INDEX] || !tb[RDMA_NLDEV_ATTR_STAT_MODE])
1906		return -EINVAL;
1907
1908	if (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
1909		return -EINVAL;
1910
1911	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1912	device = ib_device_get_by_index(sock_net(skb->sk), index);
1913	if (!device)
1914		return -EINVAL;
1915
1916	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1917	if (!rdma_is_port_valid(device, port)) {
1918		ret = -EINVAL;
1919		goto err;
 
 
 
 
 
 
1920	}
1921
1922	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1923	if (!msg) {
1924		ret = -ENOMEM;
1925		goto err;
1926	}
1927	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1928			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1929					 RDMA_NLDEV_CMD_STAT_SET),
1930			0, 0);
 
 
 
 
 
1931
1932	mode = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_MODE]);
1933	if (mode == RDMA_COUNTER_MODE_AUTO) {
1934		if (tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK])
1935			mask = nla_get_u32(
1936				tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]);
1937		ret = rdma_counter_set_auto_mode(device, port, mask, extack);
1938		if (ret)
1939			goto err_msg;
1940	} else {
1941		if (!tb[RDMA_NLDEV_ATTR_RES_LQPN])
1942			goto err_msg;
1943		qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
1944		if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]) {
1945			cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
1946			ret = rdma_counter_bind_qpn(device, port, qpn, cntn);
1947		} else {
1948			ret = rdma_counter_bind_qpn_alloc(device, port,
1949							  qpn, &cntn);
1950		}
1951		if (ret)
1952			goto err_msg;
1953
1954		if (fill_nldev_handle(msg, device) ||
1955		    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
1956		    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
1957		    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
1958			ret = -EMSGSIZE;
1959			goto err_fill;
1960		}
1961	}
1962
1963	nlmsg_end(msg, nlh);
1964	ib_device_put(device);
1965	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
1966
1967err_fill:
1968	rdma_counter_unbind_qpn(device, port, qpn, cntn);
1969err_msg:
1970	nlmsg_free(msg);
1971err:
1972	ib_device_put(device);
1973	return ret;
1974}
1975
1976static int nldev_stat_del_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1977			       struct netlink_ext_ack *extack)
1978{
1979	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1980	struct ib_device *device;
1981	struct sk_buff *msg;
1982	u32 index, port, qpn, cntn;
1983	int ret;
1984
1985	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1986			  nldev_policy, extack);
1987	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES] ||
1988	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX] ||
1989	    !tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID] ||
1990	    !tb[RDMA_NLDEV_ATTR_RES_LQPN])
1991		return -EINVAL;
1992
1993	if (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES]) != RDMA_NLDEV_ATTR_RES_QP)
1994		return -EINVAL;
1995
1996	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1997	device = ib_device_get_by_index(sock_net(skb->sk), index);
1998	if (!device)
1999		return -EINVAL;
2000
2001	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2002	if (!rdma_is_port_valid(device, port)) {
2003		ret = -EINVAL;
2004		goto err;
2005	}
2006
2007	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2008	if (!msg) {
2009		ret = -ENOMEM;
2010		goto err;
2011	}
2012	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2013			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2014					 RDMA_NLDEV_CMD_STAT_SET),
2015			0, 0);
 
 
 
 
2016
2017	cntn = nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
2018	qpn = nla_get_u32(tb[RDMA_NLDEV_ATTR_RES_LQPN]);
2019	if (fill_nldev_handle(msg, device) ||
2020	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
2021	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn) ||
2022	    nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qpn)) {
2023		ret = -EMSGSIZE;
2024		goto err_fill;
2025	}
2026
2027	ret = rdma_counter_unbind_qpn(device, port, qpn, cntn);
2028	if (ret)
2029		goto err_fill;
2030
2031	nlmsg_end(msg, nlh);
2032	ib_device_put(device);
2033	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2034
2035err_fill:
2036	nlmsg_free(msg);
2037err:
2038	ib_device_put(device);
2039	return ret;
2040}
2041
2042static int stat_get_doit_default_counter(struct sk_buff *skb,
2043					 struct nlmsghdr *nlh,
2044					 struct netlink_ext_ack *extack,
2045					 struct nlattr *tb[])
2046{
2047	struct rdma_hw_stats *stats;
2048	struct nlattr *table_attr;
2049	struct ib_device *device;
2050	int ret, num_cnts, i;
2051	struct sk_buff *msg;
2052	u32 index, port;
2053	u64 v;
2054
2055	if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2056		return -EINVAL;
2057
2058	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2059	device = ib_device_get_by_index(sock_net(skb->sk), index);
2060	if (!device)
2061		return -EINVAL;
2062
2063	if (!device->ops.alloc_hw_port_stats || !device->ops.get_hw_stats) {
2064		ret = -EINVAL;
2065		goto err;
2066	}
2067
2068	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2069	stats = ib_get_hw_stats_port(device, port);
2070	if (!stats) {
2071		ret = -EINVAL;
2072		goto err;
2073	}
2074
2075	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2076	if (!msg) {
2077		ret = -ENOMEM;
2078		goto err;
2079	}
2080
2081	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2082			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2083					 RDMA_NLDEV_CMD_STAT_GET),
2084			0, 0);
2085
2086	if (fill_nldev_handle(msg, device) ||
2087	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port)) {
2088		ret = -EMSGSIZE;
2089		goto err_msg;
2090	}
2091
2092	mutex_lock(&stats->lock);
2093
2094	num_cnts = device->ops.get_hw_stats(device, stats, port, 0);
2095	if (num_cnts < 0) {
2096		ret = -EINVAL;
2097		goto err_stats;
2098	}
2099
2100	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_STAT_HWCOUNTERS);
2101	if (!table_attr) {
2102		ret = -EMSGSIZE;
2103		goto err_stats;
2104	}
2105	for (i = 0; i < num_cnts; i++) {
 
 
 
2106		v = stats->value[i] +
2107			rdma_counter_get_hwstat_value(device, port, i);
2108		if (rdma_nl_stat_hwcounter_entry(msg, stats->names[i], v)) {
 
2109			ret = -EMSGSIZE;
2110			goto err_table;
2111		}
2112	}
2113	nla_nest_end(msg, table_attr);
2114
2115	mutex_unlock(&stats->lock);
2116	nlmsg_end(msg, nlh);
2117	ib_device_put(device);
2118	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2119
2120err_table:
2121	nla_nest_cancel(msg, table_attr);
2122err_stats:
2123	mutex_unlock(&stats->lock);
2124err_msg:
2125	nlmsg_free(msg);
2126err:
2127	ib_device_put(device);
2128	return ret;
2129}
2130
2131static int stat_get_doit_qp(struct sk_buff *skb, struct nlmsghdr *nlh,
2132			    struct netlink_ext_ack *extack, struct nlattr *tb[])
2133
2134{
2135	static enum rdma_nl_counter_mode mode;
2136	static enum rdma_nl_counter_mask mask;
2137	struct ib_device *device;
2138	struct sk_buff *msg;
2139	u32 index, port;
2140	int ret;
2141
2142	if (tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID])
2143		return nldev_res_get_counter_doit(skb, nlh, extack);
2144
2145	if (!tb[RDMA_NLDEV_ATTR_STAT_MODE] ||
2146	    !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
2147		return -EINVAL;
2148
2149	index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
2150	device = ib_device_get_by_index(sock_net(skb->sk), index);
2151	if (!device)
2152		return -EINVAL;
2153
2154	port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
2155	if (!rdma_is_port_valid(device, port)) {
2156		ret = -EINVAL;
2157		goto err;
2158	}
2159
2160	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2161	if (!msg) {
2162		ret = -ENOMEM;
2163		goto err;
2164	}
2165
2166	nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
2167			RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
2168					 RDMA_NLDEV_CMD_STAT_GET),
2169			0, 0);
 
 
 
 
2170
2171	ret = rdma_counter_get_mode(device, port, &mode, &mask);
2172	if (ret)
2173		goto err_msg;
2174
2175	if (fill_nldev_handle(msg, device) ||
2176	    nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port) ||
2177	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_MODE, mode)) {
2178		ret = -EMSGSIZE;
2179		goto err_msg;
2180	}
2181
2182	if ((mode == RDMA_COUNTER_MODE_AUTO) &&
2183	    nla_put_u32(msg, RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK, mask)) {
2184		ret = -EMSGSIZE;
2185		goto err_msg;
2186	}
2187
2188	nlmsg_end(msg, nlh);
2189	ib_device_put(device);
2190	return rdma_nl_unicast(sock_net(skb->sk), msg, NETLINK_CB(skb).portid);
2191
2192err_msg:
2193	nlmsg_free(msg);
2194err:
2195	ib_device_put(device);
2196	return ret;
2197}
2198
2199static int nldev_stat_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
2200			       struct netlink_ext_ack *extack)
2201{
2202	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2203	int ret;
2204
2205	ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2206			  nldev_policy, extack);
2207	if (ret)
2208		return -EINVAL;
2209
2210	if (!tb[RDMA_NLDEV_ATTR_STAT_RES])
2211		return stat_get_doit_default_counter(skb, nlh, extack, tb);
2212
2213	switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2214	case RDMA_NLDEV_ATTR_RES_QP:
2215		ret = stat_get_doit_qp(skb, nlh, extack, tb);
2216		break;
2217	case RDMA_NLDEV_ATTR_RES_MR:
2218		ret = res_get_common_doit(skb, nlh, extack, RDMA_RESTRACK_MR,
2219					  fill_stat_mr_entry);
2220		break;
2221	default:
2222		ret = -EINVAL;
2223		break;
2224	}
2225
2226	return ret;
2227}
2228
2229static int nldev_stat_get_dumpit(struct sk_buff *skb,
2230				 struct netlink_callback *cb)
2231{
2232	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
2233	int ret;
2234
2235	ret = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
2236			  nldev_policy, NULL);
2237	if (ret || !tb[RDMA_NLDEV_ATTR_STAT_RES])
2238		return -EINVAL;
2239
2240	switch (nla_get_u32(tb[RDMA_NLDEV_ATTR_STAT_RES])) {
2241	case RDMA_NLDEV_ATTR_RES_QP:
2242		ret = nldev_res_get_counter_dumpit(skb, cb);
2243		break;
2244	case RDMA_NLDEV_ATTR_RES_MR:
2245		ret = res_get_common_dumpit(skb, cb, RDMA_RESTRACK_MR,
2246					    fill_stat_mr_entry);
2247		break;
2248	default:
2249		ret = -EINVAL;
2250		break;
2251	}
2252
2253	return ret;
2254}
2255
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2256static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
2257	[RDMA_NLDEV_CMD_GET] = {
2258		.doit = nldev_get_doit,
2259		.dump = nldev_get_dumpit,
2260	},
2261	[RDMA_NLDEV_CMD_GET_CHARDEV] = {
2262		.doit = nldev_get_chardev,
2263	},
2264	[RDMA_NLDEV_CMD_SET] = {
2265		.doit = nldev_set_doit,
2266		.flags = RDMA_NL_ADMIN_PERM,
2267	},
2268	[RDMA_NLDEV_CMD_NEWLINK] = {
2269		.doit = nldev_newlink,
2270		.flags = RDMA_NL_ADMIN_PERM,
2271	},
2272	[RDMA_NLDEV_CMD_DELLINK] = {
2273		.doit = nldev_dellink,
2274		.flags = RDMA_NL_ADMIN_PERM,
2275	},
2276	[RDMA_NLDEV_CMD_PORT_GET] = {
2277		.doit = nldev_port_get_doit,
2278		.dump = nldev_port_get_dumpit,
2279	},
2280	[RDMA_NLDEV_CMD_RES_GET] = {
2281		.doit = nldev_res_get_doit,
2282		.dump = nldev_res_get_dumpit,
2283	},
2284	[RDMA_NLDEV_CMD_RES_QP_GET] = {
2285		.doit = nldev_res_get_qp_doit,
2286		.dump = nldev_res_get_qp_dumpit,
2287	},
2288	[RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
2289		.doit = nldev_res_get_cm_id_doit,
2290		.dump = nldev_res_get_cm_id_dumpit,
2291	},
2292	[RDMA_NLDEV_CMD_RES_CQ_GET] = {
2293		.doit = nldev_res_get_cq_doit,
2294		.dump = nldev_res_get_cq_dumpit,
2295	},
2296	[RDMA_NLDEV_CMD_RES_MR_GET] = {
2297		.doit = nldev_res_get_mr_doit,
2298		.dump = nldev_res_get_mr_dumpit,
2299	},
2300	[RDMA_NLDEV_CMD_RES_PD_GET] = {
2301		.doit = nldev_res_get_pd_doit,
2302		.dump = nldev_res_get_pd_dumpit,
2303	},
2304	[RDMA_NLDEV_CMD_RES_CTX_GET] = {
2305		.doit = nldev_res_get_ctx_doit,
2306		.dump = nldev_res_get_ctx_dumpit,
2307	},
2308	[RDMA_NLDEV_CMD_RES_SRQ_GET] = {
2309		.doit = nldev_res_get_srq_doit,
2310		.dump = nldev_res_get_srq_dumpit,
2311	},
2312	[RDMA_NLDEV_CMD_SYS_GET] = {
2313		.doit = nldev_sys_get_doit,
2314	},
2315	[RDMA_NLDEV_CMD_SYS_SET] = {
2316		.doit = nldev_set_sys_set_doit,
 
2317	},
2318	[RDMA_NLDEV_CMD_STAT_SET] = {
2319		.doit = nldev_stat_set_doit,
2320		.flags = RDMA_NL_ADMIN_PERM,
2321	},
2322	[RDMA_NLDEV_CMD_STAT_GET] = {
2323		.doit = nldev_stat_get_doit,
2324		.dump = nldev_stat_get_dumpit,
2325	},
2326	[RDMA_NLDEV_CMD_STAT_DEL] = {
2327		.doit = nldev_stat_del_doit,
2328		.flags = RDMA_NL_ADMIN_PERM,
2329	},
2330	[RDMA_NLDEV_CMD_RES_QP_GET_RAW] = {
2331		.doit = nldev_res_get_qp_raw_doit,
2332		.dump = nldev_res_get_qp_raw_dumpit,
2333		.flags = RDMA_NL_ADMIN_PERM,
2334	},
2335	[RDMA_NLDEV_CMD_RES_CQ_GET_RAW] = {
2336		.doit = nldev_res_get_cq_raw_doit,
2337		.dump = nldev_res_get_cq_raw_dumpit,
2338		.flags = RDMA_NL_ADMIN_PERM,
2339	},
2340	[RDMA_NLDEV_CMD_RES_MR_GET_RAW] = {
2341		.doit = nldev_res_get_mr_raw_doit,
2342		.dump = nldev_res_get_mr_raw_dumpit,
2343		.flags = RDMA_NL_ADMIN_PERM,
2344	},
 
 
 
 
 
 
 
 
2345};
2346
2347void __init nldev_init(void)
2348{
2349	rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
2350}
2351
2352void __exit nldev_exit(void)
2353{
2354	rdma_nl_unregister(RDMA_NL_NLDEV);
2355}
2356
2357MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);