|
| 1 | +// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
| 2 | +/* |
| 3 | + * Copyright (c) 2019 Mellanox Technologies. All rights reserved. |
| 4 | + */ |
| 5 | +#include <rdma/ib_verbs.h> |
| 6 | +#include <rdma/rdma_counter.h> |
| 7 | + |
| 8 | +#include "core_priv.h" |
| 9 | +#include "restrack.h" |
| 10 | + |
| 11 | +#define ALL_AUTO_MODE_MASKS (RDMA_COUNTER_MASK_QP_TYPE) |
| 12 | + |
| 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) |
| 16 | +{ |
| 17 | + if ((new_mode == RDMA_COUNTER_MODE_AUTO) && |
| 18 | + ((new_mask & (~ALL_AUTO_MODE_MASKS)) || |
| 19 | + (curr->mode != RDMA_COUNTER_MODE_NONE))) |
| 20 | + return -EINVAL; |
| 21 | + |
| 22 | + curr->mode = new_mode; |
| 23 | + curr->mask = new_mask; |
| 24 | + return 0; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * rdma_counter_set_auto_mode() - Turn on/off per-port auto mode |
| 29 | + * |
| 30 | + * When @on is true, the @mask must be set |
| 31 | + */ |
| 32 | +int rdma_counter_set_auto_mode(struct ib_device *dev, u8 port, |
| 33 | + bool on, enum rdma_nl_counter_mask mask) |
| 34 | +{ |
| 35 | + struct rdma_port_counter *port_counter; |
| 36 | + int ret; |
| 37 | + |
| 38 | + port_counter = &dev->port_data[port].port_counter; |
| 39 | + mutex_lock(&port_counter->lock); |
| 40 | + if (on) { |
| 41 | + ret = __counter_set_mode(&port_counter->mode, |
| 42 | + RDMA_COUNTER_MODE_AUTO, mask); |
| 43 | + } else { |
| 44 | + if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO) { |
| 45 | + ret = -EINVAL; |
| 46 | + goto out; |
| 47 | + } |
| 48 | + ret = __counter_set_mode(&port_counter->mode, |
| 49 | + RDMA_COUNTER_MODE_NONE, 0); |
| 50 | + } |
| 51 | + |
| 52 | +out: |
| 53 | + mutex_unlock(&port_counter->lock); |
| 54 | + return ret; |
| 55 | +} |
| 56 | + |
| 57 | +void rdma_counter_init(struct ib_device *dev) |
| 58 | +{ |
| 59 | + struct rdma_port_counter *port_counter; |
| 60 | + u32 port; |
| 61 | + |
| 62 | + if (!dev->ops.alloc_hw_stats || !dev->port_data) |
| 63 | + return; |
| 64 | + |
| 65 | + rdma_for_each_port(dev, port) { |
| 66 | + port_counter = &dev->port_data[port].port_counter; |
| 67 | + port_counter->mode.mode = RDMA_COUNTER_MODE_NONE; |
| 68 | + mutex_init(&port_counter->lock); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +void rdma_counter_release(struct ib_device *dev) |
| 73 | +{ |
| 74 | +} |
0 commit comments