12e4986cfSJason Gunthorpe // SPDX-License-Identifier: GPL-2.0-only
22e4986cfSJason Gunthorpe /*
32e4986cfSJason Gunthorpe * Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES
42e4986cfSJason Gunthorpe */
52e4986cfSJason Gunthorpe #define pr_fmt(fmt) "fwctl: " fmt
62e4986cfSJason Gunthorpe #include <linux/fwctl.h>
72e4986cfSJason Gunthorpe
82e4986cfSJason Gunthorpe #include <linux/container_of.h>
92e4986cfSJason Gunthorpe #include <linux/fs.h>
102e4986cfSJason Gunthorpe #include <linux/module.h>
11840cfb7cSJason Gunthorpe #include <linux/sizes.h>
122e4986cfSJason Gunthorpe #include <linux/slab.h>
132e4986cfSJason Gunthorpe
140e79a47fSJason Gunthorpe #include <uapi/fwctl/fwctl.h>
150e79a47fSJason Gunthorpe
162e4986cfSJason Gunthorpe enum {
172e4986cfSJason Gunthorpe FWCTL_MAX_DEVICES = 4096,
18840cfb7cSJason Gunthorpe MAX_RPC_LEN = SZ_2M,
192e4986cfSJason Gunthorpe };
202e4986cfSJason Gunthorpe static_assert(FWCTL_MAX_DEVICES < (1U << MINORBITS));
212e4986cfSJason Gunthorpe
222e4986cfSJason Gunthorpe static dev_t fwctl_dev;
232e4986cfSJason Gunthorpe static DEFINE_IDA(fwctl_ida);
24840cfb7cSJason Gunthorpe static unsigned long fwctl_tainted;
252e4986cfSJason Gunthorpe
260e79a47fSJason Gunthorpe struct fwctl_ucmd {
270e79a47fSJason Gunthorpe struct fwctl_uctx *uctx;
280e79a47fSJason Gunthorpe void __user *ubuffer;
290e79a47fSJason Gunthorpe void *cmd;
300e79a47fSJason Gunthorpe u32 user_size;
310e79a47fSJason Gunthorpe };
320e79a47fSJason Gunthorpe
ucmd_respond(struct fwctl_ucmd * ucmd,size_t cmd_len)33fb39e909SJason Gunthorpe static int ucmd_respond(struct fwctl_ucmd *ucmd, size_t cmd_len)
34fb39e909SJason Gunthorpe {
35fb39e909SJason Gunthorpe if (copy_to_user(ucmd->ubuffer, ucmd->cmd,
36fb39e909SJason Gunthorpe min_t(size_t, ucmd->user_size, cmd_len)))
37fb39e909SJason Gunthorpe return -EFAULT;
38fb39e909SJason Gunthorpe return 0;
39fb39e909SJason Gunthorpe }
40fb39e909SJason Gunthorpe
copy_to_user_zero_pad(void __user * to,const void * from,size_t from_len,size_t user_len)41fb39e909SJason Gunthorpe static int copy_to_user_zero_pad(void __user *to, const void *from,
42fb39e909SJason Gunthorpe size_t from_len, size_t user_len)
43fb39e909SJason Gunthorpe {
44fb39e909SJason Gunthorpe size_t copy_len;
45fb39e909SJason Gunthorpe
46fb39e909SJason Gunthorpe copy_len = min(from_len, user_len);
47fb39e909SJason Gunthorpe if (copy_to_user(to, from, copy_len))
48fb39e909SJason Gunthorpe return -EFAULT;
49fb39e909SJason Gunthorpe if (copy_len < user_len) {
50fb39e909SJason Gunthorpe if (clear_user(to + copy_len, user_len - copy_len))
51fb39e909SJason Gunthorpe return -EFAULT;
52fb39e909SJason Gunthorpe }
53fb39e909SJason Gunthorpe return 0;
54fb39e909SJason Gunthorpe }
55fb39e909SJason Gunthorpe
fwctl_cmd_info(struct fwctl_ucmd * ucmd)56fb39e909SJason Gunthorpe static int fwctl_cmd_info(struct fwctl_ucmd *ucmd)
57fb39e909SJason Gunthorpe {
58fb39e909SJason Gunthorpe struct fwctl_device *fwctl = ucmd->uctx->fwctl;
59fb39e909SJason Gunthorpe struct fwctl_info *cmd = ucmd->cmd;
60fb39e909SJason Gunthorpe size_t driver_info_len = 0;
61fb39e909SJason Gunthorpe
62fb39e909SJason Gunthorpe if (cmd->flags)
63fb39e909SJason Gunthorpe return -EOPNOTSUPP;
64fb39e909SJason Gunthorpe
65fb39e909SJason Gunthorpe if (!fwctl->ops->info && cmd->device_data_len) {
66fb39e909SJason Gunthorpe if (clear_user(u64_to_user_ptr(cmd->out_device_data),
67fb39e909SJason Gunthorpe cmd->device_data_len))
68fb39e909SJason Gunthorpe return -EFAULT;
69fb39e909SJason Gunthorpe } else if (cmd->device_data_len) {
70fb39e909SJason Gunthorpe void *driver_info __free(kfree) =
71fb39e909SJason Gunthorpe fwctl->ops->info(ucmd->uctx, &driver_info_len);
72fb39e909SJason Gunthorpe if (IS_ERR(driver_info))
73fb39e909SJason Gunthorpe return PTR_ERR(driver_info);
74fb39e909SJason Gunthorpe
75fb39e909SJason Gunthorpe if (copy_to_user_zero_pad(u64_to_user_ptr(cmd->out_device_data),
76fb39e909SJason Gunthorpe driver_info, driver_info_len,
77fb39e909SJason Gunthorpe cmd->device_data_len))
78fb39e909SJason Gunthorpe return -EFAULT;
79fb39e909SJason Gunthorpe }
80fb39e909SJason Gunthorpe
81fb39e909SJason Gunthorpe cmd->out_device_type = fwctl->ops->device_type;
82fb39e909SJason Gunthorpe cmd->device_data_len = driver_info_len;
83fb39e909SJason Gunthorpe return ucmd_respond(ucmd, sizeof(*cmd));
84fb39e909SJason Gunthorpe }
85fb39e909SJason Gunthorpe
fwctl_cmd_rpc(struct fwctl_ucmd * ucmd)86840cfb7cSJason Gunthorpe static int fwctl_cmd_rpc(struct fwctl_ucmd *ucmd)
87840cfb7cSJason Gunthorpe {
88840cfb7cSJason Gunthorpe struct fwctl_device *fwctl = ucmd->uctx->fwctl;
89840cfb7cSJason Gunthorpe struct fwctl_rpc *cmd = ucmd->cmd;
90840cfb7cSJason Gunthorpe size_t out_len;
91840cfb7cSJason Gunthorpe
92840cfb7cSJason Gunthorpe if (cmd->in_len > MAX_RPC_LEN || cmd->out_len > MAX_RPC_LEN)
93840cfb7cSJason Gunthorpe return -EMSGSIZE;
94840cfb7cSJason Gunthorpe
95840cfb7cSJason Gunthorpe switch (cmd->scope) {
96840cfb7cSJason Gunthorpe case FWCTL_RPC_CONFIGURATION:
97840cfb7cSJason Gunthorpe case FWCTL_RPC_DEBUG_READ_ONLY:
98840cfb7cSJason Gunthorpe break;
99840cfb7cSJason Gunthorpe
100840cfb7cSJason Gunthorpe case FWCTL_RPC_DEBUG_WRITE_FULL:
101840cfb7cSJason Gunthorpe if (!capable(CAP_SYS_RAWIO))
102840cfb7cSJason Gunthorpe return -EPERM;
103840cfb7cSJason Gunthorpe fallthrough;
104840cfb7cSJason Gunthorpe case FWCTL_RPC_DEBUG_WRITE:
105840cfb7cSJason Gunthorpe if (!test_and_set_bit(0, &fwctl_tainted)) {
106840cfb7cSJason Gunthorpe dev_warn(
107840cfb7cSJason Gunthorpe &fwctl->dev,
108*c92ae5d4SShannon Nelson "%s(%d): has requested full access to the physical device",
109840cfb7cSJason Gunthorpe current->comm, task_pid_nr(current));
110840cfb7cSJason Gunthorpe add_taint(TAINT_FWCTL, LOCKDEP_STILL_OK);
111840cfb7cSJason Gunthorpe }
112840cfb7cSJason Gunthorpe break;
113840cfb7cSJason Gunthorpe default:
114840cfb7cSJason Gunthorpe return -EOPNOTSUPP;
115840cfb7cSJason Gunthorpe }
116840cfb7cSJason Gunthorpe
117840cfb7cSJason Gunthorpe void *inbuf __free(kvfree) = kvzalloc(cmd->in_len, GFP_KERNEL_ACCOUNT);
118840cfb7cSJason Gunthorpe if (!inbuf)
119840cfb7cSJason Gunthorpe return -ENOMEM;
120840cfb7cSJason Gunthorpe if (copy_from_user(inbuf, u64_to_user_ptr(cmd->in), cmd->in_len))
121840cfb7cSJason Gunthorpe return -EFAULT;
122840cfb7cSJason Gunthorpe
123840cfb7cSJason Gunthorpe out_len = cmd->out_len;
124840cfb7cSJason Gunthorpe void *outbuf __free(kvfree) = fwctl->ops->fw_rpc(
125840cfb7cSJason Gunthorpe ucmd->uctx, cmd->scope, inbuf, cmd->in_len, &out_len);
126840cfb7cSJason Gunthorpe if (IS_ERR(outbuf))
127840cfb7cSJason Gunthorpe return PTR_ERR(outbuf);
128840cfb7cSJason Gunthorpe if (outbuf == inbuf) {
129840cfb7cSJason Gunthorpe /* The driver can re-use inbuf as outbuf */
130840cfb7cSJason Gunthorpe inbuf = NULL;
131840cfb7cSJason Gunthorpe }
132840cfb7cSJason Gunthorpe
133840cfb7cSJason Gunthorpe if (copy_to_user(u64_to_user_ptr(cmd->out), outbuf,
134840cfb7cSJason Gunthorpe min(cmd->out_len, out_len)))
135840cfb7cSJason Gunthorpe return -EFAULT;
136840cfb7cSJason Gunthorpe
137840cfb7cSJason Gunthorpe cmd->out_len = out_len;
138840cfb7cSJason Gunthorpe return ucmd_respond(ucmd, sizeof(*cmd));
139840cfb7cSJason Gunthorpe }
140840cfb7cSJason Gunthorpe
1410e79a47fSJason Gunthorpe /* On stack memory for the ioctl structs */
1420e79a47fSJason Gunthorpe union fwctl_ucmd_buffer {
143fb39e909SJason Gunthorpe struct fwctl_info info;
144840cfb7cSJason Gunthorpe struct fwctl_rpc rpc;
1450e79a47fSJason Gunthorpe };
1460e79a47fSJason Gunthorpe
1470e79a47fSJason Gunthorpe struct fwctl_ioctl_op {
1480e79a47fSJason Gunthorpe unsigned int size;
1490e79a47fSJason Gunthorpe unsigned int min_size;
1500e79a47fSJason Gunthorpe unsigned int ioctl_num;
1510e79a47fSJason Gunthorpe int (*execute)(struct fwctl_ucmd *ucmd);
1520e79a47fSJason Gunthorpe };
1530e79a47fSJason Gunthorpe
1540e79a47fSJason Gunthorpe #define IOCTL_OP(_ioctl, _fn, _struct, _last) \
1550e79a47fSJason Gunthorpe [_IOC_NR(_ioctl) - FWCTL_CMD_BASE] = { \
1560e79a47fSJason Gunthorpe .size = sizeof(_struct) + \
1570e79a47fSJason Gunthorpe BUILD_BUG_ON_ZERO(sizeof(union fwctl_ucmd_buffer) < \
1580e79a47fSJason Gunthorpe sizeof(_struct)), \
1590e79a47fSJason Gunthorpe .min_size = offsetofend(_struct, _last), \
1600e79a47fSJason Gunthorpe .ioctl_num = _ioctl, \
1610e79a47fSJason Gunthorpe .execute = _fn, \
1620e79a47fSJason Gunthorpe }
1630e79a47fSJason Gunthorpe static const struct fwctl_ioctl_op fwctl_ioctl_ops[] = {
164fb39e909SJason Gunthorpe IOCTL_OP(FWCTL_INFO, fwctl_cmd_info, struct fwctl_info, out_device_data),
165840cfb7cSJason Gunthorpe IOCTL_OP(FWCTL_RPC, fwctl_cmd_rpc, struct fwctl_rpc, out),
1660e79a47fSJason Gunthorpe };
1670e79a47fSJason Gunthorpe
fwctl_fops_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1680e79a47fSJason Gunthorpe static long fwctl_fops_ioctl(struct file *filp, unsigned int cmd,
1690e79a47fSJason Gunthorpe unsigned long arg)
1700e79a47fSJason Gunthorpe {
1710e79a47fSJason Gunthorpe struct fwctl_uctx *uctx = filp->private_data;
1720e79a47fSJason Gunthorpe const struct fwctl_ioctl_op *op;
1730e79a47fSJason Gunthorpe struct fwctl_ucmd ucmd = {};
1740e79a47fSJason Gunthorpe union fwctl_ucmd_buffer buf;
1750e79a47fSJason Gunthorpe unsigned int nr;
1760e79a47fSJason Gunthorpe int ret;
1770e79a47fSJason Gunthorpe
1780e79a47fSJason Gunthorpe nr = _IOC_NR(cmd);
1790e79a47fSJason Gunthorpe if ((nr - FWCTL_CMD_BASE) >= ARRAY_SIZE(fwctl_ioctl_ops))
1800e79a47fSJason Gunthorpe return -ENOIOCTLCMD;
1810e79a47fSJason Gunthorpe
1820e79a47fSJason Gunthorpe op = &fwctl_ioctl_ops[nr - FWCTL_CMD_BASE];
1830e79a47fSJason Gunthorpe if (op->ioctl_num != cmd)
1840e79a47fSJason Gunthorpe return -ENOIOCTLCMD;
1850e79a47fSJason Gunthorpe
1860e79a47fSJason Gunthorpe ucmd.uctx = uctx;
1870e79a47fSJason Gunthorpe ucmd.cmd = &buf;
1880e79a47fSJason Gunthorpe ucmd.ubuffer = (void __user *)arg;
1890e79a47fSJason Gunthorpe ret = get_user(ucmd.user_size, (u32 __user *)ucmd.ubuffer);
1900e79a47fSJason Gunthorpe if (ret)
1910e79a47fSJason Gunthorpe return ret;
1920e79a47fSJason Gunthorpe
1930e79a47fSJason Gunthorpe if (ucmd.user_size < op->min_size)
1940e79a47fSJason Gunthorpe return -EINVAL;
1950e79a47fSJason Gunthorpe
1960e79a47fSJason Gunthorpe ret = copy_struct_from_user(ucmd.cmd, op->size, ucmd.ubuffer,
1970e79a47fSJason Gunthorpe ucmd.user_size);
1980e79a47fSJason Gunthorpe if (ret)
1990e79a47fSJason Gunthorpe return ret;
2000e79a47fSJason Gunthorpe
2010e79a47fSJason Gunthorpe guard(rwsem_read)(&uctx->fwctl->registration_lock);
2020e79a47fSJason Gunthorpe if (!uctx->fwctl->ops)
2030e79a47fSJason Gunthorpe return -ENODEV;
2040e79a47fSJason Gunthorpe return op->execute(&ucmd);
2050e79a47fSJason Gunthorpe }
2060e79a47fSJason Gunthorpe
fwctl_fops_open(struct inode * inode,struct file * filp)2072e4986cfSJason Gunthorpe static int fwctl_fops_open(struct inode *inode, struct file *filp)
2082e4986cfSJason Gunthorpe {
2092e4986cfSJason Gunthorpe struct fwctl_device *fwctl =
2102e4986cfSJason Gunthorpe container_of(inode->i_cdev, struct fwctl_device, cdev);
2110e79a47fSJason Gunthorpe int ret;
2120e79a47fSJason Gunthorpe
2130e79a47fSJason Gunthorpe guard(rwsem_read)(&fwctl->registration_lock);
2140e79a47fSJason Gunthorpe if (!fwctl->ops)
2150e79a47fSJason Gunthorpe return -ENODEV;
2160e79a47fSJason Gunthorpe
2170e79a47fSJason Gunthorpe struct fwctl_uctx *uctx __free(kfree) =
2180e79a47fSJason Gunthorpe kzalloc(fwctl->ops->uctx_size, GFP_KERNEL_ACCOUNT);
2190e79a47fSJason Gunthorpe if (!uctx)
2200e79a47fSJason Gunthorpe return -ENOMEM;
2210e79a47fSJason Gunthorpe
2220e79a47fSJason Gunthorpe uctx->fwctl = fwctl;
2230e79a47fSJason Gunthorpe ret = fwctl->ops->open_uctx(uctx);
2240e79a47fSJason Gunthorpe if (ret)
2250e79a47fSJason Gunthorpe return ret;
2260e79a47fSJason Gunthorpe
2270e79a47fSJason Gunthorpe scoped_guard(mutex, &fwctl->uctx_list_lock) {
2280e79a47fSJason Gunthorpe list_add_tail(&uctx->uctx_list_entry, &fwctl->uctx_list);
2290e79a47fSJason Gunthorpe }
2302e4986cfSJason Gunthorpe
2312e4986cfSJason Gunthorpe get_device(&fwctl->dev);
2320e79a47fSJason Gunthorpe filp->private_data = no_free_ptr(uctx);
2332e4986cfSJason Gunthorpe return 0;
2342e4986cfSJason Gunthorpe }
2352e4986cfSJason Gunthorpe
fwctl_destroy_uctx(struct fwctl_uctx * uctx)2360e79a47fSJason Gunthorpe static void fwctl_destroy_uctx(struct fwctl_uctx *uctx)
2370e79a47fSJason Gunthorpe {
2380e79a47fSJason Gunthorpe lockdep_assert_held(&uctx->fwctl->uctx_list_lock);
2390e79a47fSJason Gunthorpe list_del(&uctx->uctx_list_entry);
2400e79a47fSJason Gunthorpe uctx->fwctl->ops->close_uctx(uctx);
2410e79a47fSJason Gunthorpe }
2420e79a47fSJason Gunthorpe
fwctl_fops_release(struct inode * inode,struct file * filp)2432e4986cfSJason Gunthorpe static int fwctl_fops_release(struct inode *inode, struct file *filp)
2442e4986cfSJason Gunthorpe {
2450e79a47fSJason Gunthorpe struct fwctl_uctx *uctx = filp->private_data;
2460e79a47fSJason Gunthorpe struct fwctl_device *fwctl = uctx->fwctl;
2472e4986cfSJason Gunthorpe
2480e79a47fSJason Gunthorpe scoped_guard(rwsem_read, &fwctl->registration_lock) {
2490e79a47fSJason Gunthorpe /*
2500e79a47fSJason Gunthorpe * NULL ops means fwctl_unregister() has already removed the
2510e79a47fSJason Gunthorpe * driver and destroyed the uctx.
2520e79a47fSJason Gunthorpe */
2530e79a47fSJason Gunthorpe if (fwctl->ops) {
2540e79a47fSJason Gunthorpe guard(mutex)(&fwctl->uctx_list_lock);
2550e79a47fSJason Gunthorpe fwctl_destroy_uctx(uctx);
2560e79a47fSJason Gunthorpe }
2570e79a47fSJason Gunthorpe }
2580e79a47fSJason Gunthorpe
2590e79a47fSJason Gunthorpe kfree(uctx);
2602e4986cfSJason Gunthorpe fwctl_put(fwctl);
2612e4986cfSJason Gunthorpe return 0;
2622e4986cfSJason Gunthorpe }
2632e4986cfSJason Gunthorpe
2642e4986cfSJason Gunthorpe static const struct file_operations fwctl_fops = {
2652e4986cfSJason Gunthorpe .owner = THIS_MODULE,
2662e4986cfSJason Gunthorpe .open = fwctl_fops_open,
2672e4986cfSJason Gunthorpe .release = fwctl_fops_release,
2680e79a47fSJason Gunthorpe .unlocked_ioctl = fwctl_fops_ioctl,
2692e4986cfSJason Gunthorpe };
2702e4986cfSJason Gunthorpe
fwctl_device_release(struct device * device)2712e4986cfSJason Gunthorpe static void fwctl_device_release(struct device *device)
2722e4986cfSJason Gunthorpe {
2732e4986cfSJason Gunthorpe struct fwctl_device *fwctl =
2742e4986cfSJason Gunthorpe container_of(device, struct fwctl_device, dev);
2752e4986cfSJason Gunthorpe
2762e4986cfSJason Gunthorpe ida_free(&fwctl_ida, fwctl->dev.devt - fwctl_dev);
2770e79a47fSJason Gunthorpe mutex_destroy(&fwctl->uctx_list_lock);
2782e4986cfSJason Gunthorpe kfree(fwctl);
2792e4986cfSJason Gunthorpe }
2802e4986cfSJason Gunthorpe
fwctl_devnode(const struct device * dev,umode_t * mode)2812e4986cfSJason Gunthorpe static char *fwctl_devnode(const struct device *dev, umode_t *mode)
2822e4986cfSJason Gunthorpe {
2832e4986cfSJason Gunthorpe return kasprintf(GFP_KERNEL, "fwctl/%s", dev_name(dev));
2842e4986cfSJason Gunthorpe }
2852e4986cfSJason Gunthorpe
2862e4986cfSJason Gunthorpe static struct class fwctl_class = {
2872e4986cfSJason Gunthorpe .name = "fwctl",
2882e4986cfSJason Gunthorpe .dev_release = fwctl_device_release,
2892e4986cfSJason Gunthorpe .devnode = fwctl_devnode,
2902e4986cfSJason Gunthorpe };
2912e4986cfSJason Gunthorpe
2922e4986cfSJason Gunthorpe static struct fwctl_device *
_alloc_device(struct device * parent,const struct fwctl_ops * ops,size_t size)2932e4986cfSJason Gunthorpe _alloc_device(struct device *parent, const struct fwctl_ops *ops, size_t size)
2942e4986cfSJason Gunthorpe {
2952e4986cfSJason Gunthorpe struct fwctl_device *fwctl __free(kfree) = kzalloc(size, GFP_KERNEL);
2962e4986cfSJason Gunthorpe int devnum;
2972e4986cfSJason Gunthorpe
2982e4986cfSJason Gunthorpe if (!fwctl)
2992e4986cfSJason Gunthorpe return NULL;
3002e4986cfSJason Gunthorpe
3012e4986cfSJason Gunthorpe devnum = ida_alloc_max(&fwctl_ida, FWCTL_MAX_DEVICES - 1, GFP_KERNEL);
3022e4986cfSJason Gunthorpe if (devnum < 0)
3032e4986cfSJason Gunthorpe return NULL;
3042e4986cfSJason Gunthorpe
3052e4986cfSJason Gunthorpe fwctl->dev.devt = fwctl_dev + devnum;
3062e4986cfSJason Gunthorpe fwctl->dev.class = &fwctl_class;
3072e4986cfSJason Gunthorpe fwctl->dev.parent = parent;
3082e4986cfSJason Gunthorpe
3090e79a47fSJason Gunthorpe init_rwsem(&fwctl->registration_lock);
3100e79a47fSJason Gunthorpe mutex_init(&fwctl->uctx_list_lock);
3110e79a47fSJason Gunthorpe INIT_LIST_HEAD(&fwctl->uctx_list);
3120e79a47fSJason Gunthorpe
3132e4986cfSJason Gunthorpe device_initialize(&fwctl->dev);
3142e4986cfSJason Gunthorpe return_ptr(fwctl);
3152e4986cfSJason Gunthorpe }
3162e4986cfSJason Gunthorpe
3172e4986cfSJason Gunthorpe /* Drivers use the fwctl_alloc_device() wrapper */
_fwctl_alloc_device(struct device * parent,const struct fwctl_ops * ops,size_t size)3182e4986cfSJason Gunthorpe struct fwctl_device *_fwctl_alloc_device(struct device *parent,
3192e4986cfSJason Gunthorpe const struct fwctl_ops *ops,
3202e4986cfSJason Gunthorpe size_t size)
3212e4986cfSJason Gunthorpe {
3222e4986cfSJason Gunthorpe struct fwctl_device *fwctl __free(fwctl) =
3232e4986cfSJason Gunthorpe _alloc_device(parent, ops, size);
3242e4986cfSJason Gunthorpe
3252e4986cfSJason Gunthorpe if (!fwctl)
3262e4986cfSJason Gunthorpe return NULL;
3272e4986cfSJason Gunthorpe
3282e4986cfSJason Gunthorpe cdev_init(&fwctl->cdev, &fwctl_fops);
3292e4986cfSJason Gunthorpe /*
3302e4986cfSJason Gunthorpe * The driver module is protected by fwctl_register/unregister(),
3312e4986cfSJason Gunthorpe * unregister won't complete until we are done with the driver's module.
3322e4986cfSJason Gunthorpe */
3332e4986cfSJason Gunthorpe fwctl->cdev.owner = THIS_MODULE;
3342e4986cfSJason Gunthorpe
3352e4986cfSJason Gunthorpe if (dev_set_name(&fwctl->dev, "fwctl%d", fwctl->dev.devt - fwctl_dev))
3362e4986cfSJason Gunthorpe return NULL;
3372e4986cfSJason Gunthorpe
3382e4986cfSJason Gunthorpe fwctl->ops = ops;
3392e4986cfSJason Gunthorpe return_ptr(fwctl);
3402e4986cfSJason Gunthorpe }
3412e4986cfSJason Gunthorpe EXPORT_SYMBOL_NS_GPL(_fwctl_alloc_device, "FWCTL");
3422e4986cfSJason Gunthorpe
3432e4986cfSJason Gunthorpe /**
3442e4986cfSJason Gunthorpe * fwctl_register - Register a new device to the subsystem
3452e4986cfSJason Gunthorpe * @fwctl: Previously allocated fwctl_device
3462e4986cfSJason Gunthorpe *
3472e4986cfSJason Gunthorpe * On return the device is visible through sysfs and /dev, driver ops may be
3482e4986cfSJason Gunthorpe * called.
3492e4986cfSJason Gunthorpe */
fwctl_register(struct fwctl_device * fwctl)3502e4986cfSJason Gunthorpe int fwctl_register(struct fwctl_device *fwctl)
3512e4986cfSJason Gunthorpe {
3522e4986cfSJason Gunthorpe return cdev_device_add(&fwctl->cdev, &fwctl->dev);
3532e4986cfSJason Gunthorpe }
3542e4986cfSJason Gunthorpe EXPORT_SYMBOL_NS_GPL(fwctl_register, "FWCTL");
3552e4986cfSJason Gunthorpe
3562e4986cfSJason Gunthorpe /**
3572e4986cfSJason Gunthorpe * fwctl_unregister - Unregister a device from the subsystem
3582e4986cfSJason Gunthorpe * @fwctl: Previously allocated and registered fwctl_device
3592e4986cfSJason Gunthorpe *
3602e4986cfSJason Gunthorpe * Undoes fwctl_register(). On return no driver ops will be called. The
3612e4986cfSJason Gunthorpe * caller must still call fwctl_put() to free the fwctl.
3622e4986cfSJason Gunthorpe *
3630e79a47fSJason Gunthorpe * Unregister will return even if userspace still has file descriptors open.
3640e79a47fSJason Gunthorpe * This will call ops->close_uctx() on any open FDs and after return no driver
3650e79a47fSJason Gunthorpe * op will be called. The FDs remain open but all fops will return -ENODEV.
3660e79a47fSJason Gunthorpe *
3672e4986cfSJason Gunthorpe * The design of fwctl allows this sort of disassociation of the driver from the
3682e4986cfSJason Gunthorpe * subsystem primarily by keeping memory allocations owned by the core subsytem.
3692e4986cfSJason Gunthorpe * The fwctl_device and fwctl_uctx can both be freed without requiring a driver
3702e4986cfSJason Gunthorpe * callback. This allows the module to remain unlocked while FDs are open.
3712e4986cfSJason Gunthorpe */
fwctl_unregister(struct fwctl_device * fwctl)3722e4986cfSJason Gunthorpe void fwctl_unregister(struct fwctl_device *fwctl)
3732e4986cfSJason Gunthorpe {
3740e79a47fSJason Gunthorpe struct fwctl_uctx *uctx;
3750e79a47fSJason Gunthorpe
3762e4986cfSJason Gunthorpe cdev_device_del(&fwctl->cdev, &fwctl->dev);
3770e79a47fSJason Gunthorpe
3780e79a47fSJason Gunthorpe /* Disable and free the driver's resources for any still open FDs. */
3790e79a47fSJason Gunthorpe guard(rwsem_write)(&fwctl->registration_lock);
3800e79a47fSJason Gunthorpe guard(mutex)(&fwctl->uctx_list_lock);
3810e79a47fSJason Gunthorpe while ((uctx = list_first_entry_or_null(&fwctl->uctx_list,
3820e79a47fSJason Gunthorpe struct fwctl_uctx,
3830e79a47fSJason Gunthorpe uctx_list_entry)))
3840e79a47fSJason Gunthorpe fwctl_destroy_uctx(uctx);
3850e79a47fSJason Gunthorpe
3860e79a47fSJason Gunthorpe /*
3870e79a47fSJason Gunthorpe * The driver module may unload after this returns, the op pointer will
3880e79a47fSJason Gunthorpe * not be valid.
3890e79a47fSJason Gunthorpe */
3900e79a47fSJason Gunthorpe fwctl->ops = NULL;
3912e4986cfSJason Gunthorpe }
3922e4986cfSJason Gunthorpe EXPORT_SYMBOL_NS_GPL(fwctl_unregister, "FWCTL");
3932e4986cfSJason Gunthorpe
fwctl_init(void)3942e4986cfSJason Gunthorpe static int __init fwctl_init(void)
3952e4986cfSJason Gunthorpe {
3962e4986cfSJason Gunthorpe int ret;
3972e4986cfSJason Gunthorpe
3982e4986cfSJason Gunthorpe ret = alloc_chrdev_region(&fwctl_dev, 0, FWCTL_MAX_DEVICES, "fwctl");
3992e4986cfSJason Gunthorpe if (ret)
4002e4986cfSJason Gunthorpe return ret;
4012e4986cfSJason Gunthorpe
4022e4986cfSJason Gunthorpe ret = class_register(&fwctl_class);
4032e4986cfSJason Gunthorpe if (ret)
4042e4986cfSJason Gunthorpe goto err_chrdev;
4052e4986cfSJason Gunthorpe return 0;
4062e4986cfSJason Gunthorpe
4072e4986cfSJason Gunthorpe err_chrdev:
4082e4986cfSJason Gunthorpe unregister_chrdev_region(fwctl_dev, FWCTL_MAX_DEVICES);
4092e4986cfSJason Gunthorpe return ret;
4102e4986cfSJason Gunthorpe }
4112e4986cfSJason Gunthorpe
fwctl_exit(void)4122e4986cfSJason Gunthorpe static void __exit fwctl_exit(void)
4132e4986cfSJason Gunthorpe {
4142e4986cfSJason Gunthorpe class_unregister(&fwctl_class);
4152e4986cfSJason Gunthorpe unregister_chrdev_region(fwctl_dev, FWCTL_MAX_DEVICES);
4162e4986cfSJason Gunthorpe }
4172e4986cfSJason Gunthorpe
4182e4986cfSJason Gunthorpe module_init(fwctl_init);
4192e4986cfSJason Gunthorpe module_exit(fwctl_exit);
4202e4986cfSJason Gunthorpe MODULE_DESCRIPTION("fwctl device firmware access framework");
4212e4986cfSJason Gunthorpe MODULE_LICENSE("GPL");
422