Commit 49695e95 authored by Patrisious Haddad's avatar Patrisious Haddad Committed by Jason Gunthorpe

RDMA/uverbs: Refactor rdma_counter_set_auto_mode and __counter_set_mode

Success is returned in the following flows:
 * New mode is the same as the current one.
 * Switched to new mode and there are no bound counters yet.

Link: https://lore.kernel.org/r/20210318110502.673676-1-leon@kernel.orgSigned-off-by: default avatarPatrisious Haddad <phaddad@nvidia.com>
Reviewed-by: default avatarMark Zhang <markzhang@nvidia.com>
Reviewed-by: default avatarMaor Gottlieb <maorg@nvidia.com>
Signed-off-by: default avatarLeon Romanovsky <leonro@nvidia.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
parent 6845485f
...@@ -14,10 +14,12 @@ static int __counter_set_mode(struct rdma_port_counter *port_counter, ...@@ -14,10 +14,12 @@ static int __counter_set_mode(struct rdma_port_counter *port_counter,
enum rdma_nl_counter_mode new_mode, enum rdma_nl_counter_mode new_mode,
enum rdma_nl_counter_mask new_mask) enum rdma_nl_counter_mask new_mask)
{ {
if (new_mode == RDMA_COUNTER_MODE_AUTO && port_counter->num_counters) if (new_mode == RDMA_COUNTER_MODE_AUTO) {
if (new_mask & ~ALL_AUTO_MODE_MASKS || if (new_mask & (~ALL_AUTO_MODE_MASKS))
port_counter->mode.mode != RDMA_COUNTER_MODE_NONE)
return -EINVAL; return -EINVAL;
if (port_counter->num_counters)
return -EBUSY;
}
port_counter->mode.mode = new_mode; port_counter->mode.mode = new_mode;
port_counter->mode.mask = new_mask; port_counter->mode.mask = new_mask;
...@@ -32,14 +34,17 @@ static int __counter_set_mode(struct rdma_port_counter *port_counter, ...@@ -32,14 +34,17 @@ static int __counter_set_mode(struct rdma_port_counter *port_counter,
* @mask: Mask to configure * @mask: Mask to configure
* @extack: Message to the user * @extack: Message to the user
* *
* Return 0 on success. * Return 0 on success. If counter mode wasn't changed then it is considered
* as success as well.
* Return -EBUSY when changing to auto mode while there are bounded counters.
*
*/ */
int rdma_counter_set_auto_mode(struct ib_device *dev, u32 port, int rdma_counter_set_auto_mode(struct ib_device *dev, u32 port,
enum rdma_nl_counter_mask mask, enum rdma_nl_counter_mask mask,
struct netlink_ext_ack *extack) struct netlink_ext_ack *extack)
{ {
enum rdma_nl_counter_mode mode = RDMA_COUNTER_MODE_AUTO;
struct rdma_port_counter *port_counter; struct rdma_port_counter *port_counter;
enum rdma_nl_counter_mode mode;
int ret; int ret;
port_counter = &dev->port_data[port].port_counter; port_counter = &dev->port_data[port].port_counter;
...@@ -47,25 +52,26 @@ int rdma_counter_set_auto_mode(struct ib_device *dev, u32 port, ...@@ -47,25 +52,26 @@ int rdma_counter_set_auto_mode(struct ib_device *dev, u32 port,
return -EOPNOTSUPP; return -EOPNOTSUPP;
mutex_lock(&port_counter->lock); mutex_lock(&port_counter->lock);
if (mask) { if (mask)
ret = __counter_set_mode(port_counter, mode, mask); mode = RDMA_COUNTER_MODE_AUTO;
if (ret) else
NL_SET_ERR_MSG( mode = (port_counter->num_counters) ? RDMA_COUNTER_MODE_MANUAL :
extack, RDMA_COUNTER_MODE_NONE;
"Turning on auto mode is not allowed when there is bound QP");
if (port_counter->mode.mode == mode &&
port_counter->mode.mask == mask) {
ret = 0;
goto out; goto out;
} }
if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO) { ret = __counter_set_mode(port_counter, mode, mask);
ret = -EINVAL;
goto out;
}
mode = (port_counter->num_counters) ? RDMA_COUNTER_MODE_MANUAL :
RDMA_COUNTER_MODE_NONE;
ret = __counter_set_mode(port_counter, mode, 0);
out: out:
mutex_unlock(&port_counter->lock); mutex_unlock(&port_counter->lock);
if (ret == -EBUSY)
NL_SET_ERR_MSG(
extack,
"Modifying auto mode is not allowed when there is a bound QP");
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment