1d9a5a644SRaed Salem // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2d9a5a644SRaed Salem /*
3d9a5a644SRaed Salem  * Copyright (c) 2018, Mellanox Technologies inc.  All rights reserved.
4d9a5a644SRaed Salem  *
5d9a5a644SRaed Salem  * This software is available to you under a choice of one of two
6d9a5a644SRaed Salem  * licenses.  You may choose to be licensed under the terms of the GNU
7d9a5a644SRaed Salem  * General Public License (GPL) Version 2, available from the file
8d9a5a644SRaed Salem  * COPYING in the main directory of this source tree, or the
9d9a5a644SRaed Salem  * OpenIB.org BSD license below:
10d9a5a644SRaed Salem  *
11d9a5a644SRaed Salem  *     Redistribution and use in source and binary forms, with or
12d9a5a644SRaed Salem  *     without modification, are permitted provided that the following
13d9a5a644SRaed Salem  *     conditions are met:
14d9a5a644SRaed Salem  *
15d9a5a644SRaed Salem  *      - Redistributions of source code must retain the above
16d9a5a644SRaed Salem  *        copyright notice, this list of conditions and the following
17d9a5a644SRaed Salem  *        disclaimer.
18d9a5a644SRaed Salem  *
19d9a5a644SRaed Salem  *      - Redistributions in binary form must reproduce the above
20d9a5a644SRaed Salem  *        copyright notice, this list of conditions and the following
21d9a5a644SRaed Salem  *        disclaimer in the documentation and/or other materials
22d9a5a644SRaed Salem  *        provided with the distribution.
23d9a5a644SRaed Salem  *
24d9a5a644SRaed Salem  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25d9a5a644SRaed Salem  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26d9a5a644SRaed Salem  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27d9a5a644SRaed Salem  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28d9a5a644SRaed Salem  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29d9a5a644SRaed Salem  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30d9a5a644SRaed Salem  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31d9a5a644SRaed Salem  * SOFTWARE.
32d9a5a644SRaed Salem  */
33d9a5a644SRaed Salem 
340080aed4SBart Van Assche #include "rdma_core.h"
35d9a5a644SRaed Salem #include "uverbs.h"
36d9a5a644SRaed Salem #include <rdma/uverbs_std_types.h>
37d9a5a644SRaed Salem 
uverbs_free_counters(struct ib_uobject * uobject,enum rdma_remove_reason why,struct uverbs_attr_bundle * attrs)38d9a5a644SRaed Salem static int uverbs_free_counters(struct ib_uobject *uobject,
39a6a3797dSShamir Rabinovitch 				enum rdma_remove_reason why,
40a6a3797dSShamir Rabinovitch 				struct uverbs_attr_bundle *attrs)
41d9a5a644SRaed Salem {
42d9a5a644SRaed Salem 	struct ib_counters *counters = uobject->object;
431c77483eSYishai Hadas 	int ret;
44d9a5a644SRaed Salem 
45efa968eeSLeon Romanovsky 	if (atomic_read(&counters->usecnt))
46efa968eeSLeon Romanovsky 		return -EBUSY;
47d9a5a644SRaed Salem 
4871ff3f62SLeon Romanovsky 	ret = counters->device->ops.destroy_counters(counters);
4971ff3f62SLeon Romanovsky 	if (ret)
5071ff3f62SLeon Romanovsky 		return ret;
513b023e1bSLeon Romanovsky 	kfree(counters);
523b023e1bSLeon Romanovsky 	return 0;
53d9a5a644SRaed Salem }
54d9a5a644SRaed Salem 
UVERBS_HANDLER(UVERBS_METHOD_COUNTERS_CREATE)55e83f0ecdSJason Gunthorpe static int UVERBS_HANDLER(UVERBS_METHOD_COUNTERS_CREATE)(
5615a1b4beSJason Gunthorpe 	struct uverbs_attr_bundle *attrs)
57d9a5a644SRaed Salem {
58e83f0ecdSJason Gunthorpe 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
59e83f0ecdSJason Gunthorpe 		attrs, UVERBS_ATTR_CREATE_COUNTERS_HANDLE);
60feec576aSJason Gunthorpe 	struct ib_device *ib_dev = attrs->context->device;
61d9a5a644SRaed Salem 	struct ib_counters *counters;
62d9a5a644SRaed Salem 	int ret;
63d9a5a644SRaed Salem 
64d9a5a644SRaed Salem 	/*
65d9a5a644SRaed Salem 	 * This check should be removed once the infrastructure
66d9a5a644SRaed Salem 	 * have the ability to remove methods from parse tree once
67d9a5a644SRaed Salem 	 * such condition is met.
68d9a5a644SRaed Salem 	 */
693023a1e9SKamal Heib 	if (!ib_dev->ops.create_counters)
70d9a5a644SRaed Salem 		return -EOPNOTSUPP;
71d9a5a644SRaed Salem 
723b023e1bSLeon Romanovsky 	counters = rdma_zalloc_drv_obj(ib_dev, ib_counters);
733b023e1bSLeon Romanovsky 	if (!counters)
743b023e1bSLeon Romanovsky 		return -ENOMEM;
75d9a5a644SRaed Salem 
76d9a5a644SRaed Salem 	counters->device = ib_dev;
77d9a5a644SRaed Salem 	counters->uobject = uobj;
78d9a5a644SRaed Salem 	uobj->object = counters;
79d9a5a644SRaed Salem 	atomic_set(&counters->usecnt, 0);
80d9a5a644SRaed Salem 
813b023e1bSLeon Romanovsky 	ret = ib_dev->ops.create_counters(counters, attrs);
823b023e1bSLeon Romanovsky 	if (ret)
833b023e1bSLeon Romanovsky 		kfree(counters);
84d9a5a644SRaed Salem 
85d9a5a644SRaed Salem 	return ret;
86d9a5a644SRaed Salem }
87d9a5a644SRaed Salem 
UVERBS_HANDLER(UVERBS_METHOD_COUNTERS_READ)88e83f0ecdSJason Gunthorpe static int UVERBS_HANDLER(UVERBS_METHOD_COUNTERS_READ)(
8915a1b4beSJason Gunthorpe 	struct uverbs_attr_bundle *attrs)
90ebb6796bSRaed Salem {
91ebb6796bSRaed Salem 	struct ib_counters_read_attr read_attr = {};
92ebb6796bSRaed Salem 	const struct uverbs_attr *uattr;
93ebb6796bSRaed Salem 	struct ib_counters *counters =
94ebb6796bSRaed Salem 		uverbs_attr_get_obj(attrs, UVERBS_ATTR_READ_COUNTERS_HANDLE);
95ebb6796bSRaed Salem 	int ret;
96ebb6796bSRaed Salem 
973023a1e9SKamal Heib 	if (!counters->device->ops.read_counters)
98ebb6796bSRaed Salem 		return -EOPNOTSUPP;
99ebb6796bSRaed Salem 
100ebb6796bSRaed Salem 	if (!atomic_read(&counters->usecnt))
101ebb6796bSRaed Salem 		return -EINVAL;
102ebb6796bSRaed Salem 
103bccd0622SJason Gunthorpe 	ret = uverbs_get_flags32(&read_attr.flags, attrs,
104bccd0622SJason Gunthorpe 				 UVERBS_ATTR_READ_COUNTERS_FLAGS,
105bccd0622SJason Gunthorpe 				 IB_UVERBS_READ_COUNTERS_PREFER_CACHED);
106ebb6796bSRaed Salem 	if (ret)
107ebb6796bSRaed Salem 		return ret;
108ebb6796bSRaed Salem 
109ebb6796bSRaed Salem 	uattr = uverbs_attr_get(attrs, UVERBS_ATTR_READ_COUNTERS_BUFF);
110*26b7d1a2SXiang Yang 	if (IS_ERR(uattr))
111*26b7d1a2SXiang Yang 		return PTR_ERR(uattr);
112ebb6796bSRaed Salem 	read_attr.ncounters = uattr->ptr_attr.len / sizeof(u64);
113b61815e2SJason Gunthorpe 	read_attr.counters_buff = uverbs_zalloc(
114b61815e2SJason Gunthorpe 		attrs, array_size(read_attr.ncounters, sizeof(u64)));
115b61815e2SJason Gunthorpe 	if (IS_ERR(read_attr.counters_buff))
116b61815e2SJason Gunthorpe 		return PTR_ERR(read_attr.counters_buff);
117ebb6796bSRaed Salem 
1183023a1e9SKamal Heib 	ret = counters->device->ops.read_counters(counters, &read_attr, attrs);
119ebb6796bSRaed Salem 	if (ret)
120b61815e2SJason Gunthorpe 		return ret;
121ebb6796bSRaed Salem 
122b61815e2SJason Gunthorpe 	return uverbs_copy_to(attrs, UVERBS_ATTR_READ_COUNTERS_BUFF,
123ebb6796bSRaed Salem 			      read_attr.counters_buff,
124ebb6796bSRaed Salem 			      read_attr.ncounters * sizeof(u64));
125ebb6796bSRaed Salem }
126ebb6796bSRaed Salem 
1279a119cd5SJason Gunthorpe DECLARE_UVERBS_NAMED_METHOD(
1289a119cd5SJason Gunthorpe 	UVERBS_METHOD_COUNTERS_CREATE,
1299a119cd5SJason Gunthorpe 	UVERBS_ATTR_IDR(UVERBS_ATTR_CREATE_COUNTERS_HANDLE,
130d9a5a644SRaed Salem 			UVERBS_OBJECT_COUNTERS,
131d9a5a644SRaed Salem 			UVERBS_ACCESS_NEW,
13283bb4442SJason Gunthorpe 			UA_MANDATORY));
133d9a5a644SRaed Salem 
1349a119cd5SJason Gunthorpe DECLARE_UVERBS_NAMED_METHOD_DESTROY(
1359a119cd5SJason Gunthorpe 	UVERBS_METHOD_COUNTERS_DESTROY,
1369a119cd5SJason Gunthorpe 	UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_COUNTERS_HANDLE,
137d9a5a644SRaed Salem 			UVERBS_OBJECT_COUNTERS,
138d9a5a644SRaed Salem 			UVERBS_ACCESS_DESTROY,
13983bb4442SJason Gunthorpe 			UA_MANDATORY));
140d9a5a644SRaed Salem 
1419a119cd5SJason Gunthorpe DECLARE_UVERBS_NAMED_METHOD(
1429a119cd5SJason Gunthorpe 	UVERBS_METHOD_COUNTERS_READ,
1439a119cd5SJason Gunthorpe 	UVERBS_ATTR_IDR(UVERBS_ATTR_READ_COUNTERS_HANDLE,
144ebb6796bSRaed Salem 			UVERBS_OBJECT_COUNTERS,
145ebb6796bSRaed Salem 			UVERBS_ACCESS_READ,
14683bb4442SJason Gunthorpe 			UA_MANDATORY),
1479a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_READ_COUNTERS_BUFF,
148540cd692SJason Gunthorpe 			    UVERBS_ATTR_MIN_SIZE(0),
14983bb4442SJason Gunthorpe 			    UA_MANDATORY),
150bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(UVERBS_ATTR_READ_COUNTERS_FLAGS,
151bccd0622SJason Gunthorpe 			     enum ib_uverbs_read_counters_flags));
152ebb6796bSRaed Salem 
153d9a5a644SRaed Salem DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_COUNTERS,
1549a119cd5SJason Gunthorpe 			    UVERBS_TYPE_ALLOC_IDR(uverbs_free_counters),
155d9a5a644SRaed Salem 			    &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_CREATE),
156ebb6796bSRaed Salem 			    &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_DESTROY),
157ebb6796bSRaed Salem 			    &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_READ));
1580bd01f3dSJason Gunthorpe 
1590bd01f3dSJason Gunthorpe const struct uapi_definition uverbs_def_obj_counters[] = {
1600bd01f3dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_COUNTERS,
1610bd01f3dSJason Gunthorpe 				      UAPI_DEF_OBJ_NEEDS_FN(destroy_counters)),
1620bd01f3dSJason Gunthorpe 	{}
1630bd01f3dSJason Gunthorpe };
164