Skip to content

Commit 24dc831

Browse files
Yuval Shaiadledford
authored andcommitted
IB/core: Add inline function to validate port
Signed-off-by: Yuval Shaia <[email protected]> Reviewed-by: Leon Romanovsky <[email protected]> Signed-off-by: Doug Ledford <[email protected]>
1 parent 2bce1a6 commit 24dc831

File tree

5 files changed

+20
-18
lines changed

5 files changed

+20
-18
lines changed

drivers/infiniband/core/cache.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,7 @@ int ib_find_cached_gid_by_port(struct ib_device *ib_dev,
504504
struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type};
505505
unsigned long flags;
506506

507-
if (port < rdma_start_port(ib_dev) ||
508-
port > rdma_end_port(ib_dev))
507+
if (!rdma_is_port_valid(ib_dev, port))
509508
return -ENOENT;
510509

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

564563

565-
if (port < rdma_start_port(ib_dev) ||
566-
port > rdma_end_port(ib_dev) ||
564+
if (!rdma_is_port_valid(ib_dev, port) ||
567565
!rdma_protocol_roce(ib_dev, port))
568566
return -EPROTONOSUPPORT;
569567

@@ -845,7 +843,7 @@ int ib_get_cached_gid(struct ib_device *device,
845843
unsigned long flags;
846844
struct ib_gid_table *table;
847845

848-
if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
846+
if (!rdma_is_port_valid(device, port_num))
849847
return -EINVAL;
850848

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

898-
if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
896+
if (!rdma_is_port_valid(device, port_num))
899897
return -EINVAL;
900898

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

927-
if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
925+
if (!rdma_is_port_valid(device, port_num))
928926
return -EINVAL;
929927

930928
read_lock_irqsave(&device->cache.lock, flags);
@@ -964,7 +962,7 @@ int ib_find_exact_cached_pkey(struct ib_device *device,
964962
int i;
965963
int ret = -ENOENT;
966964

967-
if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
965+
if (!rdma_is_port_valid(device, port_num))
968966
return -EINVAL;
969967

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

996-
if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
994+
if (!rdma_is_port_valid(device, port_num))
997995
return -EINVAL;
998996

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

1041-
if (port < rdma_start_port(device) || port > rdma_end_port(device))
1039+
if (!rdma_is_port_valid(device, port))
10421040
return;
10431041

10441042
table = device->cache.ports[port - rdma_start_port(device)].gid;

drivers/infiniband/core/cma.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter filter,
269269
int cma_get_default_gid_type(struct cma_device *cma_dev,
270270
unsigned int port)
271271
{
272-
if (port < rdma_start_port(cma_dev->device) ||
273-
port > rdma_end_port(cma_dev->device))
272+
if (!rdma_is_port_valid(cma_dev->device, port))
274273
return -EINVAL;
275274

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

285-
if (port < rdma_start_port(cma_dev->device) ||
286-
port > rdma_end_port(cma_dev->device))
284+
if (!rdma_is_port_valid(cma_dev->device, port))
287285
return -EINVAL;
288286

289287
supported_gids = roce_gid_type_mask_support(cma_dev->device, port);

drivers/infiniband/core/device.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ int ib_query_port(struct ib_device *device,
659659
union ib_gid gid;
660660
int err;
661661

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

665665
memset(port_attr, 0, sizeof(*port_attr));
@@ -825,7 +825,7 @@ int ib_modify_port(struct ib_device *device,
825825
if (!device->modify_port)
826826
return -ENOSYS;
827827

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

831831
return device->modify_port(device, port_num, port_modify_mask,

drivers/infiniband/core/verbs.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,8 +1205,7 @@ int ib_resolve_eth_dmac(struct ib_device *device,
12051205
{
12061206
int ret = 0;
12071207

1208-
if (ah_attr->port_num < rdma_start_port(device) ||
1209-
ah_attr->port_num > rdma_end_port(device))
1208+
if (!rdma_is_port_valid(device, ah_attr->port_num))
12101209
return -EINVAL;
12111210

12121211
if (!rdma_cap_eth_ah(device, ah_attr->port_num))

include/rdma/ib_verbs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,13 @@ static inline u8 rdma_end_port(const struct ib_device *device)
22802280
return rdma_cap_ib_switch(device) ? 0 : device->phys_port_cnt;
22812281
}
22822282

2283+
static inline int rdma_is_port_valid(const struct ib_device *device,
2284+
unsigned int port)
2285+
{
2286+
return (port >= rdma_start_port(device) &&
2287+
port <= rdma_end_port(device));
2288+
}
2289+
22832290
static inline bool rdma_protocol_ib(const struct ib_device *device, u8 port_num)
22842291
{
22852292
return device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_PROT_IB;

0 commit comments

Comments
 (0)