1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2018, Mellanox Technologies inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <rdma/uverbs_std_types.h>
37 static int uverbs_free_counters(struct ib_uobject *uobject,
38 enum rdma_remove_reason why)
40 struct ib_counters *counters = uobject->object;
43 ret = ib_destroy_usecnt(&counters->usecnt, why, uobject);
47 return counters->device->destroy_counters(counters);
50 static int UVERBS_HANDLER(UVERBS_METHOD_COUNTERS_CREATE)(
51 struct ib_uverbs_file *file, struct uverbs_attr_bundle *attrs)
53 struct ib_uobject *uobj = uverbs_attr_get_uobject(
54 attrs, UVERBS_ATTR_CREATE_COUNTERS_HANDLE);
55 struct ib_device *ib_dev = uobj->context->device;
56 struct ib_counters *counters;
60 * This check should be removed once the infrastructure
61 * have the ability to remove methods from parse tree once
62 * such condition is met.
64 if (!ib_dev->create_counters)
67 counters = ib_dev->create_counters(ib_dev, attrs);
68 if (IS_ERR(counters)) {
69 ret = PTR_ERR(counters);
70 goto err_create_counters;
73 counters->device = ib_dev;
74 counters->uobject = uobj;
75 uobj->object = counters;
76 atomic_set(&counters->usecnt, 0);
84 static int UVERBS_HANDLER(UVERBS_METHOD_COUNTERS_READ)(
85 struct ib_uverbs_file *file, struct uverbs_attr_bundle *attrs)
87 struct ib_counters_read_attr read_attr = {};
88 const struct uverbs_attr *uattr;
89 struct ib_counters *counters =
90 uverbs_attr_get_obj(attrs, UVERBS_ATTR_READ_COUNTERS_HANDLE);
93 if (!counters->device->read_counters)
96 if (!atomic_read(&counters->usecnt))
99 ret = uverbs_get_flags32(&read_attr.flags, attrs,
100 UVERBS_ATTR_READ_COUNTERS_FLAGS,
101 IB_UVERBS_READ_COUNTERS_PREFER_CACHED);
105 uattr = uverbs_attr_get(attrs, UVERBS_ATTR_READ_COUNTERS_BUFF);
106 read_attr.ncounters = uattr->ptr_attr.len / sizeof(u64);
107 read_attr.counters_buff = uverbs_zalloc(
108 attrs, array_size(read_attr.ncounters, sizeof(u64)));
109 if (IS_ERR(read_attr.counters_buff))
110 return PTR_ERR(read_attr.counters_buff);
112 ret = counters->device->read_counters(counters, &read_attr, attrs);
116 return uverbs_copy_to(attrs, UVERBS_ATTR_READ_COUNTERS_BUFF,
117 read_attr.counters_buff,
118 read_attr.ncounters * sizeof(u64));
121 DECLARE_UVERBS_NAMED_METHOD(
122 UVERBS_METHOD_COUNTERS_CREATE,
123 UVERBS_ATTR_IDR(UVERBS_ATTR_CREATE_COUNTERS_HANDLE,
124 UVERBS_OBJECT_COUNTERS,
128 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
129 UVERBS_METHOD_COUNTERS_DESTROY,
130 UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_COUNTERS_HANDLE,
131 UVERBS_OBJECT_COUNTERS,
132 UVERBS_ACCESS_DESTROY,
135 DECLARE_UVERBS_NAMED_METHOD(
136 UVERBS_METHOD_COUNTERS_READ,
137 UVERBS_ATTR_IDR(UVERBS_ATTR_READ_COUNTERS_HANDLE,
138 UVERBS_OBJECT_COUNTERS,
141 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_READ_COUNTERS_BUFF,
142 UVERBS_ATTR_MIN_SIZE(0),
144 UVERBS_ATTR_FLAGS_IN(UVERBS_ATTR_READ_COUNTERS_FLAGS,
145 enum ib_uverbs_read_counters_flags));
147 DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_COUNTERS,
148 UVERBS_TYPE_ALLOC_IDR(uverbs_free_counters),
149 &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_CREATE),
150 &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_DESTROY),
151 &UVERBS_METHOD(UVERBS_METHOD_COUNTERS_READ));