[PATCH 3.16 088/192] IB/core: Add inline function to validate port

Ben Hutchings
2017-10-09T12:46:46Z

From: Yuval Shaia <yuval.shaia@oracle.com>

commit 24dc831b77eca9361cf835be59fa69ea0e471afc upstream.

Signed-off-by: Yuval Shaia <yuval.shaia@oracle.com>
Reviewed-by: Leon Romanovsky <leonro@mellanox.com>
Signed-off-by: Doug Ledford <dledford@redhat.com>
[bwh: Backported to 3.16:
- Drop inapplicable changes
- Adjust context]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
---
--- a/drivers/infiniband/core/cache.c
+++ b/drivers/infiniband/core/cache.c
@@ -67,7 +67,7 @@ int ib_get_cached_gid(struct ib_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);
@@ -129,7 +129,7 @@ int ib_get_cached_pkey(struct ib_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);
@@ -158,7 +158,7 @@ int ib_find_cached_pkey(struct ib_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);
@@ -198,7 +198,7 @@ int ib_find_exact_cached_pkey(struct ib_
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);
@@ -227,7 +227,7 @@ int ib_get_cached_lmc(struct ib_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);
--- a/drivers/infiniband/core/device.c
+++ b/drivers/infiniband/core/device.c
@@ -563,7 +563,7 @@ int ib_query_port(struct ib_device *devi
u8 port_num,
struct ib_port_attr *port_attr)
{
- 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->query_port(device, port_num, port_attr);
@@ -641,7 +641,7 @@ int ib_modify_port(struct ib_device *dev
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,
--- a/include/rdma/ib_verbs.h
+++ b/include/rdma/ib_verbs.h
@@ -1721,6 +1721,13 @@ static inline u8 rdma_end_port(const str
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));
+}
+
int ib_query_gid(struct ib_device *device,
u8 port_num, int index, union ib_gid *gid);