1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2019 Mellanox Technologies. All rights reserved.
5 #include <rdma/ib_verbs.h>
6 #include <rdma/rdma_counter.h>
11 #define ALL_AUTO_MODE_MASKS (RDMA_COUNTER_MASK_QP_TYPE)
13 static int __counter_set_mode(struct rdma_counter_mode *curr,
14 enum rdma_nl_counter_mode new_mode,
15 enum rdma_nl_counter_mask new_mask)
17 if ((new_mode == RDMA_COUNTER_MODE_AUTO) &&
18 ((new_mask & (~ALL_AUTO_MODE_MASKS)) ||
19 (curr->mode != RDMA_COUNTER_MODE_NONE)))
22 curr->mode = new_mode;
23 curr->mask = new_mask;
28 * rdma_counter_set_auto_mode() - Turn on/off per-port auto mode
30 * When @on is true, the @mask must be set; When @on is false, it goes
31 * into manual mode if there's any counter, so that the user is able to
32 * manually access them.
34 int rdma_counter_set_auto_mode(struct ib_device *dev, u8 port,
35 bool on, enum rdma_nl_counter_mask mask)
37 struct rdma_port_counter *port_counter;
40 port_counter = &dev->port_data[port].port_counter;
41 if (!port_counter->hstats)
44 mutex_lock(&port_counter->lock);
46 ret = __counter_set_mode(&port_counter->mode,
47 RDMA_COUNTER_MODE_AUTO, mask);
49 if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO) {
54 if (port_counter->num_counters)
55 ret = __counter_set_mode(&port_counter->mode,
56 RDMA_COUNTER_MODE_MANUAL, 0);
58 ret = __counter_set_mode(&port_counter->mode,
59 RDMA_COUNTER_MODE_NONE, 0);
63 mutex_unlock(&port_counter->lock);
67 static struct rdma_counter *rdma_counter_alloc(struct ib_device *dev, u8 port,
68 enum rdma_nl_counter_mode mode)
70 struct rdma_port_counter *port_counter;
71 struct rdma_counter *counter;
74 if (!dev->ops.counter_dealloc || !dev->ops.counter_alloc_stats)
77 counter = kzalloc(sizeof(*counter), GFP_KERNEL);
81 counter->device = dev;
83 counter->res.type = RDMA_RESTRACK_COUNTER;
84 counter->stats = dev->ops.counter_alloc_stats(counter);
88 port_counter = &dev->port_data[port].port_counter;
89 mutex_lock(&port_counter->lock);
90 if (mode == RDMA_COUNTER_MODE_MANUAL) {
91 ret = __counter_set_mode(&port_counter->mode,
92 RDMA_COUNTER_MODE_MANUAL, 0);
97 port_counter->num_counters++;
98 mutex_unlock(&port_counter->lock);
100 counter->mode.mode = mode;
101 kref_init(&counter->kref);
102 mutex_init(&counter->lock);
107 mutex_unlock(&port_counter->lock);
108 kfree(counter->stats);
114 static void rdma_counter_free(struct rdma_counter *counter)
116 struct rdma_port_counter *port_counter;
118 port_counter = &counter->device->port_data[counter->port].port_counter;
119 mutex_lock(&port_counter->lock);
120 port_counter->num_counters--;
121 if (!port_counter->num_counters &&
122 (port_counter->mode.mode == RDMA_COUNTER_MODE_MANUAL))
123 __counter_set_mode(&port_counter->mode, RDMA_COUNTER_MODE_NONE,
126 mutex_unlock(&port_counter->lock);
128 rdma_restrack_del(&counter->res);
129 kfree(counter->stats);
133 static void auto_mode_init_counter(struct rdma_counter *counter,
134 const struct ib_qp *qp,
135 enum rdma_nl_counter_mask new_mask)
137 struct auto_mode_param *param = &counter->mode.param;
139 counter->mode.mode = RDMA_COUNTER_MODE_AUTO;
140 counter->mode.mask = new_mask;
142 if (new_mask & RDMA_COUNTER_MASK_QP_TYPE)
143 param->qp_type = qp->qp_type;
146 static bool auto_mode_match(struct ib_qp *qp, struct rdma_counter *counter,
147 enum rdma_nl_counter_mask auto_mask)
149 struct auto_mode_param *param = &counter->mode.param;
152 if (!rdma_is_visible_in_pid_ns(&qp->res))
155 /* Ensure that counter belongs to the right PID */
156 if (task_pid_nr(counter->res.task) != task_pid_nr(qp->res.task))
159 if (auto_mask & RDMA_COUNTER_MASK_QP_TYPE)
160 match &= (param->qp_type == qp->qp_type);
165 static int __rdma_counter_bind_qp(struct rdma_counter *counter,
173 if (!qp->device->ops.counter_bind_qp)
176 mutex_lock(&counter->lock);
177 ret = qp->device->ops.counter_bind_qp(counter, qp);
178 mutex_unlock(&counter->lock);
183 static int __rdma_counter_unbind_qp(struct ib_qp *qp)
185 struct rdma_counter *counter = qp->counter;
188 if (!qp->device->ops.counter_unbind_qp)
191 mutex_lock(&counter->lock);
192 ret = qp->device->ops.counter_unbind_qp(qp);
193 mutex_unlock(&counter->lock);
198 static void counter_history_stat_update(const struct rdma_counter *counter)
200 struct ib_device *dev = counter->device;
201 struct rdma_port_counter *port_counter;
204 port_counter = &dev->port_data[counter->port].port_counter;
205 if (!port_counter->hstats)
208 for (i = 0; i < counter->stats->num_counters; i++)
209 port_counter->hstats->value[i] += counter->stats->value[i];
213 * rdma_get_counter_auto_mode - Find the counter that @qp should be bound
216 * Return: The counter (with ref-count increased) if found
218 static struct rdma_counter *rdma_get_counter_auto_mode(struct ib_qp *qp,
221 struct rdma_port_counter *port_counter;
222 struct rdma_counter *counter = NULL;
223 struct ib_device *dev = qp->device;
224 struct rdma_restrack_entry *res;
225 struct rdma_restrack_root *rt;
226 unsigned long id = 0;
228 port_counter = &dev->port_data[port].port_counter;
229 rt = &dev->res[RDMA_RESTRACK_COUNTER];
231 xa_for_each(&rt->xa, id, res) {
232 if (!rdma_is_visible_in_pid_ns(res))
235 counter = container_of(res, struct rdma_counter, res);
236 if ((counter->device != qp->device) || (counter->port != port))
239 if (auto_mode_match(qp, counter, port_counter->mode.mask))
245 if (counter && !kref_get_unless_zero(&counter->kref))
252 static void rdma_counter_res_add(struct rdma_counter *counter,
255 if (rdma_is_kernel_res(&qp->res)) {
256 rdma_restrack_set_task(&counter->res, qp->res.kern_name);
257 rdma_restrack_kadd(&counter->res);
259 rdma_restrack_attach_task(&counter->res, qp->res.task);
260 rdma_restrack_uadd(&counter->res);
264 static void counter_release(struct kref *kref)
266 struct rdma_counter *counter;
268 counter = container_of(kref, struct rdma_counter, kref);
269 counter_history_stat_update(counter);
270 counter->device->ops.counter_dealloc(counter);
271 rdma_counter_free(counter);
275 * rdma_counter_bind_qp_auto - Check and bind the QP to a counter base on
278 int rdma_counter_bind_qp_auto(struct ib_qp *qp, u8 port)
280 struct rdma_port_counter *port_counter;
281 struct ib_device *dev = qp->device;
282 struct rdma_counter *counter;
285 if (!rdma_is_port_valid(dev, port))
288 port_counter = &dev->port_data[port].port_counter;
289 if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO)
292 counter = rdma_get_counter_auto_mode(qp, port);
294 ret = __rdma_counter_bind_qp(counter, qp);
296 kref_put(&counter->kref, counter_release);
300 counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_AUTO);
304 auto_mode_init_counter(counter, qp, port_counter->mode.mask);
306 ret = __rdma_counter_bind_qp(counter, qp);
308 rdma_counter_free(counter);
312 rdma_counter_res_add(counter, qp);
319 * rdma_counter_unbind_qp - Unbind a qp from a counter
321 * true - Decrease the counter ref-count anyway (e.g., qp destroy)
323 int rdma_counter_unbind_qp(struct ib_qp *qp, bool force)
325 struct rdma_counter *counter = qp->counter;
331 ret = __rdma_counter_unbind_qp(qp);
335 kref_put(&counter->kref, counter_release);
339 int rdma_counter_query_stats(struct rdma_counter *counter)
341 struct ib_device *dev = counter->device;
344 if (!dev->ops.counter_update_stats)
347 mutex_lock(&counter->lock);
348 ret = dev->ops.counter_update_stats(counter);
349 mutex_unlock(&counter->lock);
354 static u64 get_running_counters_hwstat_sum(struct ib_device *dev,
357 struct rdma_restrack_entry *res;
358 struct rdma_restrack_root *rt;
359 struct rdma_counter *counter;
360 unsigned long id = 0;
363 rt = &dev->res[RDMA_RESTRACK_COUNTER];
365 xa_for_each(&rt->xa, id, res) {
366 if (!rdma_restrack_get(res))
371 counter = container_of(res, struct rdma_counter, res);
372 if ((counter->device != dev) || (counter->port != port) ||
373 rdma_counter_query_stats(counter))
376 sum += counter->stats->value[index];
380 rdma_restrack_put(res);
388 * rdma_counter_get_hwstat_value() - Get the sum value of all counters on a
389 * specific port, including the running ones and history data
391 u64 rdma_counter_get_hwstat_value(struct ib_device *dev, u8 port, u32 index)
393 struct rdma_port_counter *port_counter;
396 port_counter = &dev->port_data[port].port_counter;
397 if (!port_counter->hstats)
400 sum = get_running_counters_hwstat_sum(dev, port, index);
401 sum += port_counter->hstats->value[index];
406 static struct ib_qp *rdma_counter_get_qp(struct ib_device *dev, u32 qp_num)
408 struct rdma_restrack_entry *res = NULL;
409 struct ib_qp *qp = NULL;
411 res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_QP, qp_num);
415 if (!rdma_is_visible_in_pid_ns(res))
418 qp = container_of(res, struct ib_qp, res);
419 if (qp->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
425 rdma_restrack_put(res);
429 static int rdma_counter_bind_qp_manual(struct rdma_counter *counter,
432 if ((counter->device != qp->device) || (counter->port != qp->port))
435 return __rdma_counter_bind_qp(counter, qp);
438 static struct rdma_counter *rdma_get_counter_by_id(struct ib_device *dev,
441 struct rdma_restrack_entry *res;
442 struct rdma_counter *counter;
444 res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_COUNTER, counter_id);
448 if (!rdma_is_visible_in_pid_ns(res)) {
449 rdma_restrack_put(res);
453 counter = container_of(res, struct rdma_counter, res);
454 kref_get(&counter->kref);
455 rdma_restrack_put(res);
461 * rdma_counter_bind_qpn() - Bind QP @qp_num to counter @counter_id
463 int rdma_counter_bind_qpn(struct ib_device *dev, u8 port,
464 u32 qp_num, u32 counter_id)
466 struct rdma_counter *counter;
470 qp = rdma_counter_get_qp(dev, qp_num);
474 counter = rdma_get_counter_by_id(dev, counter_id);
480 if (counter->res.task != qp->res.task) {
485 ret = rdma_counter_bind_qp_manual(counter, qp);
489 rdma_restrack_put(&qp->res);
493 kref_put(&counter->kref, counter_release);
495 rdma_restrack_put(&qp->res);
500 * rdma_counter_bind_qpn_alloc() - Alloc a counter and bind QP @qp_num to it
501 * The id of new counter is returned in @counter_id
503 int rdma_counter_bind_qpn_alloc(struct ib_device *dev, u8 port,
504 u32 qp_num, u32 *counter_id)
506 struct rdma_counter *counter;
510 if (!rdma_is_port_valid(dev, port))
513 if (!dev->port_data[port].port_counter.hstats)
516 qp = rdma_counter_get_qp(dev, qp_num);
520 if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
525 counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_MANUAL);
531 ret = rdma_counter_bind_qp_manual(counter, qp);
536 *counter_id = counter->id;
538 rdma_counter_res_add(counter, qp);
540 rdma_restrack_put(&qp->res);
544 rdma_counter_free(counter);
546 rdma_restrack_put(&qp->res);
551 * rdma_counter_unbind_qpn() - Unbind QP @qp_num from a counter
553 int rdma_counter_unbind_qpn(struct ib_device *dev, u8 port,
554 u32 qp_num, u32 counter_id)
556 struct rdma_port_counter *port_counter;
560 if (!rdma_is_port_valid(dev, port))
563 qp = rdma_counter_get_qp(dev, qp_num);
567 if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
572 port_counter = &dev->port_data[port].port_counter;
573 if (!qp->counter || qp->counter->id != counter_id ||
574 port_counter->mode.mode != RDMA_COUNTER_MODE_MANUAL) {
579 ret = rdma_counter_unbind_qp(qp, false);
582 rdma_restrack_put(&qp->res);
586 int rdma_counter_get_mode(struct ib_device *dev, u8 port,
587 enum rdma_nl_counter_mode *mode,
588 enum rdma_nl_counter_mask *mask)
590 struct rdma_port_counter *port_counter;
592 port_counter = &dev->port_data[port].port_counter;
593 *mode = port_counter->mode.mode;
594 *mask = port_counter->mode.mask;
599 void rdma_counter_init(struct ib_device *dev)
601 struct rdma_port_counter *port_counter;
607 rdma_for_each_port(dev, port) {
608 port_counter = &dev->port_data[port].port_counter;
609 port_counter->mode.mode = RDMA_COUNTER_MODE_NONE;
610 mutex_init(&port_counter->lock);
612 if (!dev->ops.alloc_hw_stats)
615 port_counter->hstats = dev->ops.alloc_hw_stats(dev, port);
616 if (!port_counter->hstats)
623 rdma_for_each_port(dev, port) {
624 port_counter = &dev->port_data[port].port_counter;
625 kfree(port_counter->hstats);
626 port_counter->hstats = NULL;
632 void rdma_counter_release(struct ib_device *dev)
634 struct rdma_port_counter *port_counter;
637 rdma_for_each_port(dev, port) {
638 port_counter = &dev->port_data[port].port_counter;
639 kfree(port_counter->hstats);