|
3 | 3 | * Copyright (c) 2018, Mellanox Technologies inc. All rights reserved. |
4 | 4 | */ |
5 | 5 |
|
| 6 | +#include <linux/overflow.h> |
6 | 7 | #include <rdma/uverbs_std_types.h> |
7 | 8 | #include "rdma_core.h" |
8 | 9 | #include "uverbs.h" |
9 | 10 | #include <rdma/uverbs_ioctl.h> |
10 | 11 | #include <rdma/opa_addr.h> |
| 12 | +#include <rdma/ib_cache.h> |
11 | 13 |
|
12 | 14 | /* |
13 | 15 | * This ioctl method allows calling any defined write or write_ex |
@@ -266,6 +268,172 @@ static int UVERBS_HANDLER(UVERBS_METHOD_QUERY_CONTEXT)( |
266 | 268 | return ucontext->device->ops.query_ucontext(ucontext, attrs); |
267 | 269 | } |
268 | 270 |
|
| 271 | +static int copy_gid_entries_to_user(struct uverbs_attr_bundle *attrs, |
| 272 | + struct ib_uverbs_gid_entry *entries, |
| 273 | + size_t num_entries, size_t user_entry_size) |
| 274 | +{ |
| 275 | + const struct uverbs_attr *attr; |
| 276 | + void __user *user_entries; |
| 277 | + size_t copy_len; |
| 278 | + int ret; |
| 279 | + int i; |
| 280 | + |
| 281 | + if (user_entry_size == sizeof(*entries)) { |
| 282 | + ret = uverbs_copy_to(attrs, |
| 283 | + UVERBS_ATTR_QUERY_GID_TABLE_RESP_ENTRIES, |
| 284 | + entries, sizeof(*entries) * num_entries); |
| 285 | + return ret; |
| 286 | + } |
| 287 | + |
| 288 | + copy_len = min_t(size_t, user_entry_size, sizeof(*entries)); |
| 289 | + attr = uverbs_attr_get(attrs, UVERBS_ATTR_QUERY_GID_TABLE_RESP_ENTRIES); |
| 290 | + if (IS_ERR(attr)) |
| 291 | + return PTR_ERR(attr); |
| 292 | + |
| 293 | + user_entries = u64_to_user_ptr(attr->ptr_attr.data); |
| 294 | + for (i = 0; i < num_entries; i++) { |
| 295 | + if (copy_to_user(user_entries, entries, copy_len)) |
| 296 | + return -EFAULT; |
| 297 | + |
| 298 | + if (user_entry_size > sizeof(*entries)) { |
| 299 | + if (clear_user(user_entries + sizeof(*entries), |
| 300 | + user_entry_size - sizeof(*entries))) |
| 301 | + return -EFAULT; |
| 302 | + } |
| 303 | + |
| 304 | + entries++; |
| 305 | + user_entries += user_entry_size; |
| 306 | + } |
| 307 | + |
| 308 | + return uverbs_output_written(attrs, |
| 309 | + UVERBS_ATTR_QUERY_GID_TABLE_RESP_ENTRIES); |
| 310 | +} |
| 311 | + |
| 312 | +static int UVERBS_HANDLER(UVERBS_METHOD_QUERY_GID_TABLE)( |
| 313 | + struct uverbs_attr_bundle *attrs) |
| 314 | +{ |
| 315 | + struct ib_uverbs_gid_entry *entries; |
| 316 | + struct ib_ucontext *ucontext; |
| 317 | + struct ib_device *ib_dev; |
| 318 | + size_t user_entry_size; |
| 319 | + ssize_t num_entries; |
| 320 | + size_t max_entries; |
| 321 | + size_t num_bytes; |
| 322 | + u32 flags; |
| 323 | + int ret; |
| 324 | + |
| 325 | + ret = uverbs_get_flags32(&flags, attrs, |
| 326 | + UVERBS_ATTR_QUERY_GID_TABLE_FLAGS, 0); |
| 327 | + if (ret) |
| 328 | + return ret; |
| 329 | + |
| 330 | + ret = uverbs_get_const(&user_entry_size, attrs, |
| 331 | + UVERBS_ATTR_QUERY_GID_TABLE_ENTRY_SIZE); |
| 332 | + if (ret) |
| 333 | + return ret; |
| 334 | + |
| 335 | + max_entries = uverbs_attr_ptr_get_array_size( |
| 336 | + attrs, UVERBS_ATTR_QUERY_GID_TABLE_RESP_ENTRIES, |
| 337 | + user_entry_size); |
| 338 | + if (max_entries <= 0) |
| 339 | + return -EINVAL; |
| 340 | + |
| 341 | + ucontext = ib_uverbs_get_ucontext(attrs); |
| 342 | + if (IS_ERR(ucontext)) |
| 343 | + return PTR_ERR(ucontext); |
| 344 | + ib_dev = ucontext->device; |
| 345 | + |
| 346 | + if (check_mul_overflow(max_entries, sizeof(*entries), &num_bytes)) |
| 347 | + return -EINVAL; |
| 348 | + |
| 349 | + entries = uverbs_zalloc(attrs, num_bytes); |
| 350 | + if (!entries) |
| 351 | + return -ENOMEM; |
| 352 | + |
| 353 | + num_entries = rdma_query_gid_table(ib_dev, entries, max_entries); |
| 354 | + if (num_entries < 0) |
| 355 | + return -EINVAL; |
| 356 | + |
| 357 | + ret = copy_gid_entries_to_user(attrs, entries, num_entries, |
| 358 | + user_entry_size); |
| 359 | + if (ret) |
| 360 | + return ret; |
| 361 | + |
| 362 | + ret = uverbs_copy_to(attrs, |
| 363 | + UVERBS_ATTR_QUERY_GID_TABLE_RESP_NUM_ENTRIES, |
| 364 | + &num_entries, sizeof(num_entries)); |
| 365 | + return ret; |
| 366 | +} |
| 367 | + |
| 368 | +static int UVERBS_HANDLER(UVERBS_METHOD_QUERY_GID_ENTRY)( |
| 369 | + struct uverbs_attr_bundle *attrs) |
| 370 | +{ |
| 371 | + struct ib_uverbs_gid_entry entry = {}; |
| 372 | + const struct ib_gid_attr *gid_attr; |
| 373 | + struct ib_ucontext *ucontext; |
| 374 | + struct ib_device *ib_dev; |
| 375 | + struct net_device *ndev; |
| 376 | + u32 gid_index; |
| 377 | + u32 port_num; |
| 378 | + u32 flags; |
| 379 | + int ret; |
| 380 | + |
| 381 | + ret = uverbs_get_flags32(&flags, attrs, |
| 382 | + UVERBS_ATTR_QUERY_GID_ENTRY_FLAGS, 0); |
| 383 | + if (ret) |
| 384 | + return ret; |
| 385 | + |
| 386 | + ret = uverbs_get_const(&port_num, attrs, |
| 387 | + UVERBS_ATTR_QUERY_GID_ENTRY_PORT); |
| 388 | + if (ret) |
| 389 | + return ret; |
| 390 | + |
| 391 | + ret = uverbs_get_const(&gid_index, attrs, |
| 392 | + UVERBS_ATTR_QUERY_GID_ENTRY_GID_INDEX); |
| 393 | + if (ret) |
| 394 | + return ret; |
| 395 | + |
| 396 | + ucontext = ib_uverbs_get_ucontext(attrs); |
| 397 | + if (IS_ERR(ucontext)) |
| 398 | + return PTR_ERR(ucontext); |
| 399 | + ib_dev = ucontext->device; |
| 400 | + |
| 401 | + if (!rdma_is_port_valid(ib_dev, port_num)) |
| 402 | + return -EINVAL; |
| 403 | + |
| 404 | + if (!rdma_ib_or_roce(ib_dev, port_num)) |
| 405 | + return -EOPNOTSUPP; |
| 406 | + |
| 407 | + gid_attr = rdma_get_gid_attr(ib_dev, port_num, gid_index); |
| 408 | + if (IS_ERR(gid_attr)) |
| 409 | + return PTR_ERR(gid_attr); |
| 410 | + |
| 411 | + memcpy(&entry.gid, &gid_attr->gid, sizeof(gid_attr->gid)); |
| 412 | + entry.gid_index = gid_attr->index; |
| 413 | + entry.port_num = gid_attr->port_num; |
| 414 | + entry.gid_type = gid_attr->gid_type; |
| 415 | + |
| 416 | + rcu_read_lock(); |
| 417 | + ndev = rdma_read_gid_attr_ndev_rcu(gid_attr); |
| 418 | + if (IS_ERR(ndev)) { |
| 419 | + if (PTR_ERR(ndev) != -ENODEV) { |
| 420 | + ret = PTR_ERR(ndev); |
| 421 | + rcu_read_unlock(); |
| 422 | + goto out; |
| 423 | + } |
| 424 | + } else { |
| 425 | + entry.netdev_ifindex = ndev->ifindex; |
| 426 | + } |
| 427 | + rcu_read_unlock(); |
| 428 | + |
| 429 | + ret = uverbs_copy_to_struct_or_zero( |
| 430 | + attrs, UVERBS_ATTR_QUERY_GID_ENTRY_RESP_ENTRY, &entry, |
| 431 | + sizeof(entry)); |
| 432 | +out: |
| 433 | + rdma_put_gid_attr(gid_attr); |
| 434 | + return ret; |
| 435 | +} |
| 436 | + |
269 | 437 | DECLARE_UVERBS_NAMED_METHOD( |
270 | 438 | UVERBS_METHOD_GET_CONTEXT, |
271 | 439 | UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_GET_CONTEXT_NUM_COMP_VECTORS, |
@@ -300,12 +468,38 @@ DECLARE_UVERBS_NAMED_METHOD( |
300 | 468 | reserved), |
301 | 469 | UA_MANDATORY)); |
302 | 470 |
|
| 471 | +DECLARE_UVERBS_NAMED_METHOD( |
| 472 | + UVERBS_METHOD_QUERY_GID_TABLE, |
| 473 | + UVERBS_ATTR_CONST_IN(UVERBS_ATTR_QUERY_GID_TABLE_ENTRY_SIZE, u64, |
| 474 | + UA_MANDATORY), |
| 475 | + UVERBS_ATTR_FLAGS_IN(UVERBS_ATTR_QUERY_GID_TABLE_FLAGS, u32, |
| 476 | + UA_OPTIONAL), |
| 477 | + UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_QUERY_GID_TABLE_RESP_ENTRIES, |
| 478 | + UVERBS_ATTR_MIN_SIZE(0), UA_MANDATORY), |
| 479 | + UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_QUERY_GID_TABLE_RESP_NUM_ENTRIES, |
| 480 | + UVERBS_ATTR_TYPE(u64), UA_MANDATORY)); |
| 481 | + |
| 482 | +DECLARE_UVERBS_NAMED_METHOD( |
| 483 | + UVERBS_METHOD_QUERY_GID_ENTRY, |
| 484 | + UVERBS_ATTR_CONST_IN(UVERBS_ATTR_QUERY_GID_ENTRY_PORT, u32, |
| 485 | + UA_MANDATORY), |
| 486 | + UVERBS_ATTR_CONST_IN(UVERBS_ATTR_QUERY_GID_ENTRY_GID_INDEX, u32, |
| 487 | + UA_MANDATORY), |
| 488 | + UVERBS_ATTR_FLAGS_IN(UVERBS_ATTR_QUERY_GID_ENTRY_FLAGS, u32, |
| 489 | + UA_MANDATORY), |
| 490 | + UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_QUERY_GID_ENTRY_RESP_ENTRY, |
| 491 | + UVERBS_ATTR_STRUCT(struct ib_uverbs_gid_entry, |
| 492 | + netdev_ifindex), |
| 493 | + UA_MANDATORY)); |
| 494 | + |
303 | 495 | DECLARE_UVERBS_GLOBAL_METHODS(UVERBS_OBJECT_DEVICE, |
304 | 496 | &UVERBS_METHOD(UVERBS_METHOD_GET_CONTEXT), |
305 | 497 | &UVERBS_METHOD(UVERBS_METHOD_INVOKE_WRITE), |
306 | 498 | &UVERBS_METHOD(UVERBS_METHOD_INFO_HANDLES), |
307 | 499 | &UVERBS_METHOD(UVERBS_METHOD_QUERY_PORT), |
308 | | - &UVERBS_METHOD(UVERBS_METHOD_QUERY_CONTEXT)); |
| 500 | + &UVERBS_METHOD(UVERBS_METHOD_QUERY_CONTEXT), |
| 501 | + &UVERBS_METHOD(UVERBS_METHOD_QUERY_GID_TABLE), |
| 502 | + &UVERBS_METHOD(UVERBS_METHOD_QUERY_GID_ENTRY)); |
309 | 503 |
|
310 | 504 | const struct uapi_definition uverbs_def_obj_device[] = { |
311 | 505 | UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_DEVICE), |
|
0 commit comments