diff options
author | Parav Pandit <parav@mellanox.com> | 2018-10-17 06:20:20 -0400 |
---|---|---|
committer | Doug Ledford <dledford@redhat.com> | 2018-10-17 11:43:07 -0400 |
commit | 548cb4fbe80d68b9d1b8b30aca179636e74bec36 (patch) | |
tree | b24c338b65d8f19aeeb8d178432f9e896f5eaf35 | |
parent | 67fecaf8e9cc28812042f61194ac0e0a9737f897 (diff) |
RDMA/core: Refactor ib_register_device() function
ib_register_device() does several allocation and initialization
steps. Split it into smaller more readable functions for easy
review and maintenance.
Signed-off-by: Parav Pandit <parav@mellanox.com>
Reviewed-by: Daniel Jurgens <danielj@mellanox.com>
Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
Signed-off-by: Doug Ledford <dledford@redhat.com>
-rw-r--r-- | drivers/infiniband/core/device.c | 126 |
1 files changed, 75 insertions, 51 deletions
diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c index d175b94ae952..87eb4f2cdd7d 100644 --- a/drivers/infiniband/core/device.c +++ b/drivers/infiniband/core/device.c | |||
@@ -465,22 +465,8 @@ static u32 __dev_new_index(void) | |||
465 | } | 465 | } |
466 | } | 466 | } |
467 | 467 | ||
468 | /** | 468 | static void setup_dma_device(struct ib_device *device) |
469 | * ib_register_device - Register an IB device with IB core | ||
470 | * @device:Device to register | ||
471 | * | ||
472 | * Low-level drivers use ib_register_device() to register their | ||
473 | * devices with the IB core. All registered clients will receive a | ||
474 | * callback for each device that is added. @device must be allocated | ||
475 | * with ib_alloc_device(). | ||
476 | */ | ||
477 | int ib_register_device(struct ib_device *device, const char *name, | ||
478 | int (*port_callback)(struct ib_device *, u8, | ||
479 | struct kobject *)) | ||
480 | { | 469 | { |
481 | int ret; | ||
482 | struct ib_client *client; | ||
483 | struct ib_udata uhw = {.outlen = 0, .inlen = 0}; | ||
484 | struct device *parent = device->dev.parent; | 470 | struct device *parent = device->dev.parent; |
485 | 471 | ||
486 | WARN_ON_ONCE(device->dma_device); | 472 | WARN_ON_ONCE(device->dma_device); |
@@ -512,34 +498,38 @@ int ib_register_device(struct ib_device *device, const char *name, | |||
512 | WARN_ON_ONCE(!parent); | 498 | WARN_ON_ONCE(!parent); |
513 | device->dma_device = parent; | 499 | device->dma_device = parent; |
514 | } | 500 | } |
501 | } | ||
515 | 502 | ||
516 | mutex_lock(&device_mutex); | 503 | static void cleanup_device(struct ib_device *device) |
504 | { | ||
505 | ib_cache_cleanup_one(device); | ||
506 | ib_cache_release_one(device); | ||
507 | kfree(device->port_pkey_list); | ||
508 | kfree(device->port_immutable); | ||
509 | } | ||
517 | 510 | ||
518 | if (strchr(name, '%')) { | 511 | static int setup_device(struct ib_device *device) |
519 | ret = alloc_name(device, name); | 512 | { |
520 | if (ret) | 513 | struct ib_udata uhw = {.outlen = 0, .inlen = 0}; |
521 | goto out; | 514 | int ret; |
522 | } else { | ||
523 | ret = dev_set_name(&device->dev, name); | ||
524 | if (ret) | ||
525 | goto out; | ||
526 | } | ||
527 | if (__ib_device_get_by_name(dev_name(&device->dev))) { | ||
528 | ret = -ENFILE; | ||
529 | goto out; | ||
530 | } | ||
531 | strlcpy(device->name, dev_name(&device->dev), IB_DEVICE_NAME_MAX); | ||
532 | 515 | ||
533 | if (ib_device_check_mandatory(device)) { | 516 | ret = ib_device_check_mandatory(device); |
534 | ret = -EINVAL; | 517 | if (ret) |
535 | goto out; | 518 | return ret; |
536 | } | ||
537 | 519 | ||
538 | ret = read_port_immutable(device); | 520 | ret = read_port_immutable(device); |
539 | if (ret) { | 521 | if (ret) { |
540 | dev_warn(&device->dev, | 522 | dev_warn(&device->dev, |
541 | "Couldn't create per port immutable data\n"); | 523 | "Couldn't create per port immutable data\n"); |
542 | goto out; | 524 | return ret; |
525 | } | ||
526 | |||
527 | memset(&device->attrs, 0, sizeof(device->attrs)); | ||
528 | ret = device->query_device(device, &device->attrs, &uhw); | ||
529 | if (ret) { | ||
530 | dev_warn(&device->dev, | ||
531 | "Couldn't query the device attributes\n"); | ||
532 | goto port_cleanup; | ||
543 | } | 533 | } |
544 | 534 | ||
545 | ret = setup_port_pkey_list(device); | 535 | ret = setup_port_pkey_list(device); |
@@ -554,6 +544,53 @@ int ib_register_device(struct ib_device *device, const char *name, | |||
554 | "Couldn't set up InfiniBand P_Key/GID cache\n"); | 544 | "Couldn't set up InfiniBand P_Key/GID cache\n"); |
555 | goto pkey_cleanup; | 545 | goto pkey_cleanup; |
556 | } | 546 | } |
547 | return 0; | ||
548 | |||
549 | pkey_cleanup: | ||
550 | kfree(device->port_pkey_list); | ||
551 | port_cleanup: | ||
552 | kfree(device->port_immutable); | ||
553 | return ret; | ||
554 | } | ||
555 | |||
556 | /** | ||
557 | * ib_register_device - Register an IB device with IB core | ||
558 | * @device:Device to register | ||
559 | * | ||
560 | * Low-level drivers use ib_register_device() to register their | ||
561 | * devices with the IB core. All registered clients will receive a | ||
562 | * callback for each device that is added. @device must be allocated | ||
563 | * with ib_alloc_device(). | ||
564 | */ | ||
565 | int ib_register_device(struct ib_device *device, const char *name, | ||
566 | int (*port_callback)(struct ib_device *, u8, | ||
567 | struct kobject *)) | ||
568 | { | ||
569 | int ret; | ||
570 | struct ib_client *client; | ||
571 | |||
572 | setup_dma_device(device); | ||
573 | |||
574 | mutex_lock(&device_mutex); | ||
575 | |||
576 | if (strchr(name, '%')) { | ||
577 | ret = alloc_name(device, name); | ||
578 | if (ret) | ||
579 | goto out; | ||
580 | } else { | ||
581 | ret = dev_set_name(&device->dev, name); | ||
582 | if (ret) | ||
583 | goto out; | ||
584 | } | ||
585 | if (__ib_device_get_by_name(dev_name(&device->dev))) { | ||
586 | ret = -ENFILE; | ||
587 | goto out; | ||
588 | } | ||
589 | strlcpy(device->name, dev_name(&device->dev), IB_DEVICE_NAME_MAX); | ||
590 | |||
591 | ret = setup_device(device); | ||
592 | if (ret) | ||
593 | goto out; | ||
557 | 594 | ||
558 | device->index = __dev_new_index(); | 595 | device->index = __dev_new_index(); |
559 | 596 | ||
@@ -561,15 +598,7 @@ int ib_register_device(struct ib_device *device, const char *name, | |||
561 | if (ret) { | 598 | if (ret) { |
562 | dev_warn(&device->dev, | 599 | dev_warn(&device->dev, |
563 | "Couldn't register device with rdma cgroup\n"); | 600 | "Couldn't register device with rdma cgroup\n"); |
564 | goto cache_cleanup; | 601 | goto dev_cleanup; |
565 | } | ||
566 | |||
567 | memset(&device->attrs, 0, sizeof(device->attrs)); | ||
568 | ret = device->query_device(device, &device->attrs, &uhw); | ||
569 | if (ret) { | ||
570 | dev_warn(&device->dev, | ||
571 | "Couldn't query the device attributes\n"); | ||
572 | goto cg_cleanup; | ||
573 | } | 602 | } |
574 | 603 | ||
575 | ret = ib_device_register_sysfs(device, port_callback); | 604 | ret = ib_device_register_sysfs(device, port_callback); |
@@ -593,13 +622,8 @@ int ib_register_device(struct ib_device *device, const char *name, | |||
593 | 622 | ||
594 | cg_cleanup: | 623 | cg_cleanup: |
595 | ib_device_unregister_rdmacg(device); | 624 | ib_device_unregister_rdmacg(device); |
596 | cache_cleanup: | 625 | dev_cleanup: |
597 | ib_cache_cleanup_one(device); | 626 | cleanup_device(device); |
598 | ib_cache_release_one(device); | ||
599 | pkey_cleanup: | ||
600 | kfree(device->port_pkey_list); | ||
601 | port_cleanup: | ||
602 | kfree(device->port_immutable); | ||
603 | out: | 627 | out: |
604 | mutex_unlock(&device_mutex); | 628 | mutex_unlock(&device_mutex); |
605 | return ret; | 629 | return ret; |