Skip to content

Commit

Permalink
RDMA/core: Refactor ib_register_device() function
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Parav Pandit authored and Doug Ledford committed Oct 17, 2018
1 parent 67fecaf commit 548cb4f
Showing 1 changed file with 75 additions and 51 deletions.
126 changes: 75 additions & 51 deletions drivers/infiniband/core/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,22 +465,8 @@ static u32 __dev_new_index(void)
}
}

/**
* ib_register_device - Register an IB device with IB core
* @device:Device to register
*
* Low-level drivers use ib_register_device() to register their
* devices with the IB core. All registered clients will receive a
* callback for each device that is added. @device must be allocated
* with ib_alloc_device().
*/
int ib_register_device(struct ib_device *device, const char *name,
int (*port_callback)(struct ib_device *, u8,
struct kobject *))
static void setup_dma_device(struct ib_device *device)
{
int ret;
struct ib_client *client;
struct ib_udata uhw = {.outlen = 0, .inlen = 0};
struct device *parent = device->dev.parent;

WARN_ON_ONCE(device->dma_device);
Expand Down Expand Up @@ -512,34 +498,38 @@ int ib_register_device(struct ib_device *device, const char *name,
WARN_ON_ONCE(!parent);
device->dma_device = parent;
}
}

mutex_lock(&device_mutex);
static void cleanup_device(struct ib_device *device)
{
ib_cache_cleanup_one(device);
ib_cache_release_one(device);
kfree(device->port_pkey_list);
kfree(device->port_immutable);
}

if (strchr(name, '%')) {
ret = alloc_name(device, name);
if (ret)
goto out;
} else {
ret = dev_set_name(&device->dev, name);
if (ret)
goto out;
}
if (__ib_device_get_by_name(dev_name(&device->dev))) {
ret = -ENFILE;
goto out;
}
strlcpy(device->name, dev_name(&device->dev), IB_DEVICE_NAME_MAX);
static int setup_device(struct ib_device *device)
{
struct ib_udata uhw = {.outlen = 0, .inlen = 0};
int ret;

if (ib_device_check_mandatory(device)) {
ret = -EINVAL;
goto out;
}
ret = ib_device_check_mandatory(device);
if (ret)
return ret;

ret = read_port_immutable(device);
if (ret) {
dev_warn(&device->dev,
"Couldn't create per port immutable data\n");
goto out;
return ret;
}

memset(&device->attrs, 0, sizeof(device->attrs));
ret = device->query_device(device, &device->attrs, &uhw);
if (ret) {
dev_warn(&device->dev,
"Couldn't query the device attributes\n");
goto port_cleanup;
}

ret = setup_port_pkey_list(device);
Expand All @@ -554,22 +544,61 @@ int ib_register_device(struct ib_device *device, const char *name,
"Couldn't set up InfiniBand P_Key/GID cache\n");
goto pkey_cleanup;
}
return 0;

pkey_cleanup:
kfree(device->port_pkey_list);
port_cleanup:
kfree(device->port_immutable);
return ret;
}

/**
* ib_register_device - Register an IB device with IB core
* @device:Device to register
*
* Low-level drivers use ib_register_device() to register their
* devices with the IB core. All registered clients will receive a
* callback for each device that is added. @device must be allocated
* with ib_alloc_device().
*/
int ib_register_device(struct ib_device *device, const char *name,
int (*port_callback)(struct ib_device *, u8,
struct kobject *))
{
int ret;
struct ib_client *client;

setup_dma_device(device);

mutex_lock(&device_mutex);

if (strchr(name, '%')) {
ret = alloc_name(device, name);
if (ret)
goto out;
} else {
ret = dev_set_name(&device->dev, name);
if (ret)
goto out;
}
if (__ib_device_get_by_name(dev_name(&device->dev))) {
ret = -ENFILE;
goto out;
}
strlcpy(device->name, dev_name(&device->dev), IB_DEVICE_NAME_MAX);

ret = setup_device(device);
if (ret)
goto out;

device->index = __dev_new_index();

ret = ib_device_register_rdmacg(device);
if (ret) {
dev_warn(&device->dev,
"Couldn't register device with rdma cgroup\n");
goto cache_cleanup;
}

memset(&device->attrs, 0, sizeof(device->attrs));
ret = device->query_device(device, &device->attrs, &uhw);
if (ret) {
dev_warn(&device->dev,
"Couldn't query the device attributes\n");
goto cg_cleanup;
goto dev_cleanup;
}

ret = ib_device_register_sysfs(device, port_callback);
Expand All @@ -593,13 +622,8 @@ int ib_register_device(struct ib_device *device, const char *name,

cg_cleanup:
ib_device_unregister_rdmacg(device);
cache_cleanup:
ib_cache_cleanup_one(device);
ib_cache_release_one(device);
pkey_cleanup:
kfree(device->port_pkey_list);
port_cleanup:
kfree(device->port_immutable);
dev_cleanup:
cleanup_device(device);
out:
mutex_unlock(&device_mutex);
return ret;
Expand Down

0 comments on commit 548cb4f

Please sign in to comment.