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;
84 rdma_restrack_new(&counter->res, RDMA_RESTRACK_COUNTER);
85 counter->stats = dev->ops.counter_alloc_stats(counter);
89 port_counter = &dev->port_data[port].port_counter;
90 mutex_lock(&port_counter->lock);
91 if (mode == RDMA_COUNTER_MODE_MANUAL) {
92 ret = __counter_set_mode(&port_counter->mode,
93 RDMA_COUNTER_MODE_MANUAL, 0);
98 port_counter->num_counters++;
99 mutex_unlock(&port_counter->lock);
101 counter->mode.mode = mode;
102 kref_init(&counter->kref);
103 mutex_init(&counter->lock);
108 mutex_unlock(&port_counter->lock);
109 kfree(counter->stats);
111 rdma_restrack_put(&counter->res);
116 static void rdma_counter_free(struct rdma_counter *counter)
118 struct rdma_port_counter *port_counter;
120 port_counter = &counter->device->port_data[counter->port].port_counter;
121 mutex_lock(&port_counter->lock);
122 port_counter->num_counters--;
123 if (!port_counter->num_counters &&
124 (port_counter->mode.mode == RDMA_COUNTER_MODE_MANUAL))
125 __counter_set_mode(&port_counter->mode, RDMA_COUNTER_MODE_NONE,
128 mutex_unlock(&port_counter->lock);
130 rdma_restrack_del(&counter->res);
131 kfree(counter->stats);
135 static void auto_mode_init_counter(struct rdma_counter *counter,
136 const struct ib_qp *qp,
137 enum rdma_nl_counter_mask new_mask)
139 struct auto_mode_param *param = &counter->mode.param;
141 counter->mode.mode = RDMA_COUNTER_MODE_AUTO;
142 counter->mode.mask = new_mask;
144 if (new_mask & RDMA_COUNTER_MASK_QP_TYPE)
145 param->qp_type = qp->qp_type;
148 static bool auto_mode_match(struct ib_qp *qp, struct rdma_counter *counter,
149 enum rdma_nl_counter_mask auto_mask)
151 struct auto_mode_param *param = &counter->mode.param;
154 if (auto_mask & RDMA_COUNTER_MASK_QP_TYPE)
155 match &= (param->qp_type == qp->qp_type);
157 if (auto_mask & RDMA_COUNTER_MASK_PID)
158 match &= (task_pid_nr(counter->res.task) ==
159 task_pid_nr(qp->res.task));
164 static int __rdma_counter_bind_qp(struct rdma_counter *counter,
172 if (!qp->device->ops.counter_bind_qp)
175 mutex_lock(&counter->lock);
176 ret = qp->device->ops.counter_bind_qp(counter, qp);
177 mutex_unlock(&counter->lock);
182 static int __rdma_counter_unbind_qp(struct ib_qp *qp)
184 struct rdma_counter *counter = qp->counter;
187 if (!qp->device->ops.counter_unbind_qp)
190 mutex_lock(&counter->lock);
191 ret = qp->device->ops.counter_unbind_qp(qp);
192 mutex_unlock(&counter->lock);
197 static void counter_history_stat_update(struct rdma_counter *counter)
199 struct ib_device *dev = counter->device;
200 struct rdma_port_counter *port_counter;
203 port_counter = &dev->port_data[counter->port].port_counter;
204 if (!port_counter->hstats)
207 rdma_counter_query_stats(counter);
209 for (i = 0; i < counter->stats->num_counters; i++)
210 port_counter->hstats->value[i] += counter->stats->value[i];
214 * rdma_get_counter_auto_mode - Find the counter that @qp should be bound
217 * Return: The counter (with ref-count increased) if found
219 static struct rdma_counter *rdma_get_counter_auto_mode(struct ib_qp *qp,
222 struct rdma_port_counter *port_counter;
223 struct rdma_counter *counter = NULL;
224 struct ib_device *dev = qp->device;
225 struct rdma_restrack_entry *res;
226 struct rdma_restrack_root *rt;
227 unsigned long id = 0;
229 port_counter = &dev->port_data[port].port_counter;
230 rt = &dev->res[RDMA_RESTRACK_COUNTER];
232 xa_for_each(&rt->xa, id, res) {
233 counter = container_of(res, struct rdma_counter, res);
234 if ((counter->device != qp->device) || (counter->port != port))
237 if (auto_mode_match(qp, counter, port_counter->mode.mask))
243 if (counter && !kref_get_unless_zero(&counter->kref))
250 static void rdma_counter_res_add(struct rdma_counter *counter,
253 rdma_restrack_parent_name(&counter->res, &qp->res);
254 rdma_restrack_add(&counter->res);
257 static void counter_release(struct kref *kref)
259 struct rdma_counter *counter;
261 counter = container_of(kref, struct rdma_counter, kref);
262 counter_history_stat_update(counter);
263 counter->device->ops.counter_dealloc(counter);
264 rdma_counter_free(counter);
268 * rdma_counter_bind_qp_auto - Check and bind the QP to a counter base on
271 int rdma_counter_bind_qp_auto(struct ib_qp *qp, u8 port)
273 struct rdma_port_counter *port_counter;
274 struct ib_device *dev = qp->device;
275 struct rdma_counter *counter;
278 if (!qp->res.valid || rdma_is_kernel_res(&qp->res))
281 if (!rdma_is_port_valid(dev, port))
284 port_counter = &dev->port_data[port].port_counter;
285 if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO)
288 counter = rdma_get_counter_auto_mode(qp, port);
290 ret = __rdma_counter_bind_qp(counter, qp);
292 kref_put(&counter->kref, counter_release);
296 counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_AUTO);
300 auto_mode_init_counter(counter, qp, port_counter->mode.mask);
302 ret = __rdma_counter_bind_qp(counter, qp);
304 rdma_counter_free(counter);
308 rdma_counter_res_add(counter, qp);
315 * rdma_counter_unbind_qp - Unbind a qp from a counter
317 * true - Decrease the counter ref-count anyway (e.g., qp destroy)
319 int rdma_counter_unbind_qp(struct ib_qp *qp, bool force)
321 struct rdma_counter *counter = qp->counter;
327 ret = __rdma_counter_unbind_qp(qp);
331 kref_put(&counter->kref, counter_release);
335 int rdma_counter_query_stats(struct rdma_counter *counter)
337 struct ib_device *dev = counter->device;
340 if (!dev->ops.counter_update_stats)
343 mutex_lock(&counter->lock);
344 ret = dev->ops.counter_update_stats(counter);
345 mutex_unlock(&counter->lock);
350 static u64 get_running_counters_hwstat_sum(struct ib_device *dev,
353 struct rdma_restrack_entry *res;
354 struct rdma_restrack_root *rt;
355 struct rdma_counter *counter;
356 unsigned long id = 0;
359 rt = &dev->res[RDMA_RESTRACK_COUNTER];
361 xa_for_each(&rt->xa, id, res) {
362 if (!rdma_restrack_get(res))
367 counter = container_of(res, struct rdma_counter, res);
368 if ((counter->device != dev) || (counter->port != port) ||
369 rdma_counter_query_stats(counter))
372 sum += counter->stats->value[index];
376 rdma_restrack_put(res);
384 * rdma_counter_get_hwstat_value() - Get the sum value of all counters on a
385 * specific port, including the running ones and history data
387 u64 rdma_counter_get_hwstat_value(struct ib_device *dev, u8 port, u32 index)
389 struct rdma_port_counter *port_counter;
392 port_counter = &dev->port_data[port].port_counter;
393 if (!port_counter->hstats)
396 sum = get_running_counters_hwstat_sum(dev, port, index);
397 sum += port_counter->hstats->value[index];
402 static struct ib_qp *rdma_counter_get_qp(struct ib_device *dev, u32 qp_num)
404 struct rdma_restrack_entry *res = NULL;
405 struct ib_qp *qp = NULL;
407 res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_QP, qp_num);
411 qp = container_of(res, struct ib_qp, res);
412 if (qp->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
418 rdma_restrack_put(res);
422 static int rdma_counter_bind_qp_manual(struct rdma_counter *counter,
425 if ((counter->device != qp->device) || (counter->port != qp->port))
428 return __rdma_counter_bind_qp(counter, qp);
431 static struct rdma_counter *rdma_get_counter_by_id(struct ib_device *dev,
434 struct rdma_restrack_entry *res;
435 struct rdma_counter *counter;
437 res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_COUNTER, counter_id);
441 counter = container_of(res, struct rdma_counter, res);
442 kref_get(&counter->kref);
443 rdma_restrack_put(res);
449 * rdma_counter_bind_qpn() - Bind QP @qp_num to counter @counter_id
451 int rdma_counter_bind_qpn(struct ib_device *dev, u8 port,
452 u32 qp_num, u32 counter_id)
454 struct rdma_port_counter *port_counter;
455 struct rdma_counter *counter;
459 port_counter = &dev->port_data[port].port_counter;
460 if (port_counter->mode.mode == RDMA_COUNTER_MODE_AUTO)
463 qp = rdma_counter_get_qp(dev, qp_num);
467 counter = rdma_get_counter_by_id(dev, counter_id);
473 if (rdma_is_kernel_res(&counter->res) != rdma_is_kernel_res(&qp->res)) {
478 ret = rdma_counter_bind_qp_manual(counter, qp);
482 rdma_restrack_put(&qp->res);
486 kref_put(&counter->kref, counter_release);
488 rdma_restrack_put(&qp->res);
493 * rdma_counter_bind_qpn_alloc() - Alloc a counter and bind QP @qp_num to it
494 * The id of new counter is returned in @counter_id
496 int rdma_counter_bind_qpn_alloc(struct ib_device *dev, u8 port,
497 u32 qp_num, u32 *counter_id)
499 struct rdma_port_counter *port_counter;
500 struct rdma_counter *counter;
504 if (!rdma_is_port_valid(dev, port))
507 port_counter = &dev->port_data[port].port_counter;
508 if (!port_counter->hstats)
511 if (port_counter->mode.mode == RDMA_COUNTER_MODE_AUTO)
514 qp = rdma_counter_get_qp(dev, qp_num);
518 if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
523 counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_MANUAL);
529 ret = rdma_counter_bind_qp_manual(counter, qp);
534 *counter_id = counter->id;
536 rdma_counter_res_add(counter, qp);
538 rdma_restrack_put(&qp->res);
542 rdma_counter_free(counter);
544 rdma_restrack_put(&qp->res);
549 * rdma_counter_unbind_qpn() - Unbind QP @qp_num from a counter
551 int rdma_counter_unbind_qpn(struct ib_device *dev, u8 port,
552 u32 qp_num, u32 counter_id)
554 struct rdma_port_counter *port_counter;
558 if (!rdma_is_port_valid(dev, port))
561 qp = rdma_counter_get_qp(dev, qp_num);
565 if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
570 port_counter = &dev->port_data[port].port_counter;
571 if (!qp->counter || qp->counter->id != counter_id ||
572 port_counter->mode.mode != RDMA_COUNTER_MODE_MANUAL) {
577 ret = rdma_counter_unbind_qp(qp, false);
580 rdma_restrack_put(&qp->res);
584 int rdma_counter_get_mode(struct ib_device *dev, u8 port,
585 enum rdma_nl_counter_mode *mode,
586 enum rdma_nl_counter_mask *mask)
588 struct rdma_port_counter *port_counter;
590 port_counter = &dev->port_data[port].port_counter;
591 *mode = port_counter->mode.mode;
592 *mask = port_counter->mode.mask;
597 void rdma_counter_init(struct ib_device *dev)
599 struct rdma_port_counter *port_counter;
605 rdma_for_each_port(dev, port) {
606 port_counter = &dev->port_data[port].port_counter;
607 port_counter->mode.mode = RDMA_COUNTER_MODE_NONE;
608 mutex_init(&port_counter->lock);
610 if (!dev->ops.alloc_hw_stats)
613 port_counter->hstats = dev->ops.alloc_hw_stats(dev, port);
614 if (!port_counter->hstats)
621 for (i = port; i >= rdma_start_port(dev); i--) {
622 port_counter = &dev->port_data[port].port_counter;
623 kfree(port_counter->hstats);
624 port_counter->hstats = NULL;
625 mutex_destroy(&port_counter->lock);
629 void rdma_counter_release(struct ib_device *dev)
631 struct rdma_port_counter *port_counter;
634 rdma_for_each_port(dev, port) {
635 port_counter = &dev->port_data[port].port_counter;
636 kfree(port_counter->hstats);
637 mutex_destroy(&port_counter->lock);