Skip to content

Commit

Permalink
IB/core: Add inline function to validate port
Browse files Browse the repository at this point in the history
Signed-off-by: Yuval Shaia <yuval.shaia@oracle.com>
Reviewed-by: Leon Romanovsky <leonro@mellanox.com>
Signed-off-by: Doug Ledford <dledford@redhat.com>
  • Loading branch information
Yuval Shaia authored and Doug Ledford committed Jan 27, 2017
1 parent 2bce1a6 commit 24dc831
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
18 changes: 8 additions & 10 deletions drivers/infiniband/core/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,7 @@ int ib_find_cached_gid_by_port(struct ib_device *ib_dev,
struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type};
unsigned long flags;

if (port < rdma_start_port(ib_dev) ||
port > rdma_end_port(ib_dev))
if (!rdma_is_port_valid(ib_dev, port))
return -ENOENT;

table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
Expand Down Expand Up @@ -562,8 +561,7 @@ static int ib_cache_gid_find_by_filter(struct ib_device *ib_dev,
bool found = false;


if (port < rdma_start_port(ib_dev) ||
port > rdma_end_port(ib_dev) ||
if (!rdma_is_port_valid(ib_dev, port) ||
!rdma_protocol_roce(ib_dev, port))
return -EPROTONOSUPPORT;

Expand Down Expand Up @@ -845,7 +843,7 @@ int ib_get_cached_gid(struct ib_device *device,
unsigned long flags;
struct ib_gid_table *table;

if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
if (!rdma_is_port_valid(device, port_num))
return -EINVAL;

table = device->cache.ports[port_num - rdma_start_port(device)].gid;
Expand Down Expand Up @@ -895,7 +893,7 @@ int ib_get_cached_pkey(struct ib_device *device,
unsigned long flags;
int ret = 0;

if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
if (!rdma_is_port_valid(device, port_num))
return -EINVAL;

read_lock_irqsave(&device->cache.lock, flags);
Expand Down Expand Up @@ -924,7 +922,7 @@ int ib_find_cached_pkey(struct ib_device *device,
int ret = -ENOENT;
int partial_ix = -1;

if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
if (!rdma_is_port_valid(device, port_num))
return -EINVAL;

read_lock_irqsave(&device->cache.lock, flags);
Expand Down Expand Up @@ -964,7 +962,7 @@ int ib_find_exact_cached_pkey(struct ib_device *device,
int i;
int ret = -ENOENT;

if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
if (!rdma_is_port_valid(device, port_num))
return -EINVAL;

read_lock_irqsave(&device->cache.lock, flags);
Expand Down Expand Up @@ -993,7 +991,7 @@ int ib_get_cached_lmc(struct ib_device *device,
unsigned long flags;
int ret = 0;

if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
if (!rdma_is_port_valid(device, port_num))
return -EINVAL;

read_lock_irqsave(&device->cache.lock, flags);
Expand Down Expand Up @@ -1038,7 +1036,7 @@ static void ib_cache_update(struct ib_device *device,
bool use_roce_gid_table =
rdma_cap_roce_gid_table(device, port);

if (port < rdma_start_port(device) || port > rdma_end_port(device))
if (!rdma_is_port_valid(device, port))
return;

table = device->cache.ports[port - rdma_start_port(device)].gid;
Expand Down
6 changes: 2 additions & 4 deletions drivers/infiniband/core/cma.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,7 @@ struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter filter,
int cma_get_default_gid_type(struct cma_device *cma_dev,
unsigned int port)
{
if (port < rdma_start_port(cma_dev->device) ||
port > rdma_end_port(cma_dev->device))
if (!rdma_is_port_valid(cma_dev->device, port))
return -EINVAL;

return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
Expand All @@ -282,8 +281,7 @@ int cma_set_default_gid_type(struct cma_device *cma_dev,
{
unsigned long supported_gids;

if (port < rdma_start_port(cma_dev->device) ||
port > rdma_end_port(cma_dev->device))
if (!rdma_is_port_valid(cma_dev->device, port))
return -EINVAL;

supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
Expand Down
4 changes: 2 additions & 2 deletions drivers/infiniband/core/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ int ib_query_port(struct ib_device *device,
union ib_gid gid;
int err;

if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
if (!rdma_is_port_valid(device, port_num))
return -EINVAL;

memset(port_attr, 0, sizeof(*port_attr));
Expand Down Expand Up @@ -825,7 +825,7 @@ int ib_modify_port(struct ib_device *device,
if (!device->modify_port)
return -ENOSYS;

if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
if (!rdma_is_port_valid(device, port_num))
return -EINVAL;

return device->modify_port(device, port_num, port_modify_mask,
Expand Down
3 changes: 1 addition & 2 deletions drivers/infiniband/core/verbs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1205,8 +1205,7 @@ int ib_resolve_eth_dmac(struct ib_device *device,
{
int ret = 0;

if (ah_attr->port_num < rdma_start_port(device) ||
ah_attr->port_num > rdma_end_port(device))
if (!rdma_is_port_valid(device, ah_attr->port_num))
return -EINVAL;

if (!rdma_cap_eth_ah(device, ah_attr->port_num))
Expand Down
7 changes: 7 additions & 0 deletions include/rdma/ib_verbs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,13 @@ static inline u8 rdma_end_port(const struct ib_device *device)
return rdma_cap_ib_switch(device) ? 0 : device->phys_port_cnt;
}

static inline int rdma_is_port_valid(const struct ib_device *device,
unsigned int port)
{
return (port >= rdma_start_port(device) &&
port <= rdma_end_port(device));
}

static inline bool rdma_protocol_ib(const struct ib_device *device, u8 port_num)
{
return device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_PROT_IB;
Expand Down

0 comments on commit 24dc831

Please sign in to comment.