drivers/infiniband/core/ucaps.c

Source file repositories/reference/linux-study-clean/drivers/infiniband/core/ucaps.c

File Facts

System
Linux kernel
Corpus path
drivers/infiniband/core/ucaps.c
Extension
.c
Size
5778 bytes
Lines
264
Domain
Driver Families
Bucket
drivers/infiniband
Inferred role
Driver Families: operation-table or driver-model contract
Status
pattern implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

static const struct file_operations ucaps_cdev_fops = {
	.owner = THIS_MODULE,
	.open = simple_open,
};

static __exit void ib_cleanup_ucaps(void)
{
	mutex_lock(&ucaps_mutex);
	if (!ucaps_class_is_registered) {
		mutex_unlock(&ucaps_mutex);
		return;
	}

	for (int i = RDMA_UCAP_FIRST; i < RDMA_UCAP_MAX; i++)
		WARN_ON(ucaps_list[i]);

	class_unregister(&ucaps_class);
	ucaps_class_is_registered = false;
	unregister_chrdev_region(ucaps_base_dev, RDMA_UCAP_MAX);
	mutex_unlock(&ucaps_mutex);
}

static int get_ucap_from_devt(dev_t devt, u64 *idx_mask)
{
	for (int type = RDMA_UCAP_FIRST; type < RDMA_UCAP_MAX; type++) {
		if (ucaps_list[type] && ucaps_list[type]->dev.devt == devt) {
			*idx_mask |= 1 << type;
			return 0;
		}
	}

	return -EINVAL;
}

static int get_devt_from_fd(unsigned int fd, dev_t *ret_dev)
{
	CLASS(fd, f)(fd);

	if (fd_empty(f) || fd_file(f)->f_op != &ucaps_cdev_fops)
		return -EBADF;

	*ret_dev = file_inode(fd_file(f))->i_rdev;
	return 0;
}

/**
 * ib_ucaps_init - Initialization required before ucap creation.
 *
 * Return: 0 on success, or a negative errno value on failure
 */
static int ib_ucaps_init(void)
{
	int ret = 0;

	if (ucaps_class_is_registered)
		return ret;

	ret = class_register(&ucaps_class);
	if (ret)
		return ret;

	ret = alloc_chrdev_region(&ucaps_base_dev, 0, RDMA_UCAP_MAX,
				  ucaps_class.name);
	if (ret < 0) {
		class_unregister(&ucaps_class);
		return ret;
	}

	ucaps_class_is_registered = true;

	return 0;
}

static void ucap_dev_release(struct device *device)
{
	struct ib_ucap *ucap = container_of(device, struct ib_ucap, dev);

	kfree(ucap);
}

/**
 * ib_create_ucap - Add a ucap character device
 * @type: UCAP type
 *
 * Creates a ucap character device in the /dev/infiniband directory. By default,
 * the device has root-only read-write access.
 *
 * A driver may call this multiple times with the same UCAP type. A reference
 * count tracks creations and deletions.
 *

Annotation

Implementation Notes