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 | RDMA_COUNTER_MASK_PID)
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 (auto_mask & RDMA_COUNTER_MASK_QP_TYPE)
153 match &= (param->qp_type == qp->qp_type);
155 if (auto_mask & RDMA_COUNTER_MASK_PID)
156 match &= (task_pid_nr(counter->res.task) ==
157 task_pid_nr(qp->res.task));
162 static int __rdma_counter_bind_qp(struct rdma_counter *counter,
170 if (!qp->device->ops.counter_bind_qp)
173 mutex_lock(&counter->lock);
174 ret = qp->device->ops.counter_bind_qp(counter, qp);
175 mutex_unlock(&counter->lock);
180 static int __rdma_counter_unbind_qp(struct ib_qp *qp)
182 struct rdma_counter *counter = qp->counter;
185 if (!qp->device->ops.counter_unbind_qp)
188 mutex_lock(&counter->lock);
189 ret = qp->device->ops.counter_unbind_qp(qp);
190 mutex_unlock(&counter->lock);
195 static void counter_history_stat_update(struct rdma_counter *counter)
197 struct ib_device *dev = counter->device;
198 struct rdma_port_counter *port_counter;
201 port_counter = &dev->port_data[counter->port].port_counter;
202 if (!port_counter->hstats)
205 rdma_counter_query_stats(counter);
207 for (i = 0; i < counter->stats->num_counters; i++)
208 port_counter->hstats->value[i] += counter->stats->value[i];
212 * rdma_get_counter_auto_mode - Find the counter that @qp should be bound
215 * Return: The counter (with ref-count increased) if found
217 static struct rdma_counter *rdma_get_counter_auto_mode(struct ib_qp *qp,
220 struct rdma_port_counter *port_counter;
221 struct rdma_counter *counter = NULL;
222 struct ib_device *dev = qp->device;
223 struct rdma_restrack_entry *res;
224 struct rdma_restrack_root *rt;
225 unsigned long id = 0;
227 port_counter = &dev->port_data[port].port_counter;
228 rt = &dev->res[RDMA_RESTRACK_COUNTER];
230 xa_for_each(&rt->xa, id, res) {
231 counter = container_of(res, struct rdma_counter, res);
232 if ((counter->device != qp->device) || (counter->port != port))
235 if (auto_mode_match(qp, counter, port_counter->mode.mask))
241 if (counter && !kref_get_unless_zero(&counter->kref))
248 static void rdma_counter_res_add(struct rdma_counter *counter,
251 if (rdma_is_kernel_res(&qp->res)) {
252 rdma_restrack_set_task(&counter->res, qp->res.kern_name);
253 rdma_restrack_kadd(&counter->res);
255 rdma_restrack_attach_task(&counter->res, qp->res.task);
256 rdma_restrack_uadd(&counter->res);
260 static void counter_release(struct kref *kref)
262 struct rdma_counter *counter;
264 counter = container_of(kref, struct rdma_counter, kref);
265 counter_history_stat_update(counter);
266 counter->device->ops.counter_dealloc(counter);
267 rdma_counter_free(counter);
271 * rdma_counter_bind_qp_auto - Check and bind the QP to a counter base on
274 int rdma_counter_bind_qp_auto(struct ib_qp *qp, u8 port)
276 struct rdma_port_counter *port_counter;
277 struct ib_device *dev = qp->device;
278 struct rdma_counter *counter;
281 if (!qp->res.valid || rdma_is_kernel_res(&qp->res))
284 if (!rdma_is_port_valid(dev, port))
287 port_counter = &dev->port_data[port].port_counter;
288 if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO)
291 counter = rdma_get_counter_auto_mode(qp, port);
293 ret = __rdma_counter_bind_qp(counter, qp);
295 kref_put(&counter->kref, counter_release);
299 counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_AUTO);
303 auto_mode_init_counter(counter, qp, port_counter->mode.mask);
305 ret = __rdma_counter_bind_qp(counter, qp);
307 rdma_counter_free(counter);
311 rdma_counter_res_add(counter, qp);
318 * rdma_counter_unbind_qp - Unbind a qp from a counter
320 * true - Decrease the counter ref-count anyway (e.g., qp destroy)
322 int rdma_counter_unbind_qp(struct ib_qp *qp, bool force)
324 struct rdma_counter *counter = qp->counter;
330 ret = __rdma_counter_unbind_qp(qp);
334 kref_put(&counter->kref, counter_release);
338 int rdma_counter_query_stats(struct rdma_counter *counter)
340 struct ib_device *dev = counter->device;
343 if (!dev->ops.counter_update_stats)
346 mutex_lock(&counter->lock);
347 ret = dev->ops.counter_update_stats(counter);
348 mutex_unlock(&counter->lock);
353 static u64 get_running_counters_hwstat_sum(struct ib_device *dev,
356 struct rdma_restrack_entry *res;
357 struct rdma_restrack_root *rt;
358 struct rdma_counter *counter;
359 unsigned long id = 0;
362 rt = &dev->res[RDMA_RESTRACK_COUNTER];
364 xa_for_each(&rt->xa, id, res) {
365 if (!rdma_restrack_get(res))
370 counter = container_of(res, struct rdma_counter, res);
371 if ((counter->device != dev) || (counter->port != port) ||
372 rdma_counter_query_stats(counter))
375 sum += counter->stats->value[index];
379 rdma_restrack_put(res);
387 * rdma_counter_get_hwstat_value() - Get the sum value of all counters on a
388 * specific port, including the running ones and history data
390 u64 rdma_counter_get_hwstat_value(struct ib_device *dev, u8 port, u32 index)
392 struct rdma_port_counter *port_counter;
395 port_counter = &dev->port_data[port].port_counter;
396 if (!port_counter->hstats)
399 sum = get_running_counters_hwstat_sum(dev, port, index);
400 sum += port_counter->hstats->value[index];
405 static struct ib_qp *rdma_counter_get_qp(struct ib_device *dev, u32 qp_num)
407 struct rdma_restrack_entry *res = NULL;
408 struct ib_qp *qp = NULL;
410 res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_QP, qp_num);
414 qp = container_of(res, struct ib_qp, res);
415 if (qp->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
421 rdma_restrack_put(res);
425 static int rdma_counter_bind_qp_manual(struct rdma_counter *counter,
428 if ((counter->device != qp->device) || (counter->port != qp->port))
431 return __rdma_counter_bind_qp(counter, qp);
434 static struct rdma_counter *rdma_get_counter_by_id(struct ib_device *dev,
437 struct rdma_restrack_entry *res;
438 struct rdma_counter *counter;
440 res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_COUNTER, counter_id);
444 counter = container_of(res, struct rdma_counter, res);
445 kref_get(&counter->kref);
446 rdma_restrack_put(res);
452 * rdma_counter_bind_qpn() - Bind QP @qp_num to counter @counter_id
454 int rdma_counter_bind_qpn(struct ib_device *dev, u8 port,
455 u32 qp_num, u32 counter_id)
457 struct rdma_port_counter *port_counter;
458 struct rdma_counter *counter;
462 port_counter = &dev->port_data[port].port_counter;
463 if (port_counter->mode.mode == RDMA_COUNTER_MODE_AUTO)
466 qp = rdma_counter_get_qp(dev, qp_num);
470 counter = rdma_get_counter_by_id(dev, counter_id);
476 if (rdma_is_kernel_res(&counter->res) != rdma_is_kernel_res(&qp->res)) {
481 ret = rdma_counter_bind_qp_manual(counter, qp);
485 rdma_restrack_put(&qp->res);
489 kref_put(&counter->kref, counter_release);
491 rdma_restrack_put(&qp->res);
496 * rdma_counter_bind_qpn_alloc() - Alloc a counter and bind QP @qp_num to it
497 * The id of new counter is returned in @counter_id
499 int rdma_counter_bind_qpn_alloc(struct ib_device *dev, u8 port,
500 u32 qp_num, u32 *counter_id)
502 struct rdma_port_counter *port_counter;
503 struct rdma_counter *counter;
507 if (!rdma_is_port_valid(dev, port))
510 port_counter = &dev->port_data[port].port_counter;
511 if (!port_counter->hstats)
514 if (port_counter->mode.mode == RDMA_COUNTER_MODE_AUTO)
517 qp = rdma_counter_get_qp(dev, qp_num);
521 if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
526 counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_MANUAL);
532 ret = rdma_counter_bind_qp_manual(counter, qp);
537 *counter_id = counter->id;
539 rdma_counter_res_add(counter, qp);
541 rdma_restrack_put(&qp->res);
545 rdma_counter_free(counter);
547 rdma_restrack_put(&qp->res);
552 * rdma_counter_unbind_qpn() - Unbind QP @qp_num from a counter
554 int rdma_counter_unbind_qpn(struct ib_device *dev, u8 port,
555 u32 qp_num, u32 counter_id)
557 struct rdma_port_counter *port_counter;
561 if (!rdma_is_port_valid(dev, port))
564 qp = rdma_counter_get_qp(dev, qp_num);
568 if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
573 port_counter = &dev->port_data[port].port_counter;
574 if (!qp->counter || qp->counter->id != counter_id ||
575 port_counter->mode.mode != RDMA_COUNTER_MODE_MANUAL) {
580 ret = rdma_counter_unbind_qp(qp, false);
583 rdma_restrack_put(&qp->res);
587 int rdma_counter_get_mode(struct ib_device *dev, u8 port,
588 enum rdma_nl_counter_mode *mode,
589 enum rdma_nl_counter_mask *mask)
591 struct rdma_port_counter *port_counter;
593 port_counter = &dev->port_data[port].port_counter;
594 *mode = port_counter->mode.mode;
595 *mask = port_counter->mode.mask;
600 void rdma_counter_init(struct ib_device *dev)
602 struct rdma_port_counter *port_counter;
608 rdma_for_each_port(dev, port) {
609 port_counter = &dev->port_data[port].port_counter;
610 port_counter->mode.mode = RDMA_COUNTER_MODE_NONE;
611 mutex_init(&port_counter->lock);
613 if (!dev->ops.alloc_hw_stats)
616 port_counter->hstats = dev->ops.alloc_hw_stats(dev, port);
617 if (!port_counter->hstats)
624 for (i = port; i >= rdma_start_port(dev); i--) {
625 port_counter = &dev->port_data[port].port_counter;
626 kfree(port_counter->hstats);
627 port_counter->hstats = NULL;
628 mutex_destroy(&port_counter->lock);
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);
640 mutex_destroy(&port_counter->lock);