1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel/userspace transport abstraction for Hyper-V util driver.
5 * Copyright (C) 2015, Vitaly Kuznetsov <vkuznets@redhat.com>
8 #include <linux/slab.h>
10 #include <linux/poll.h>
12 #include "hyperv_vmbus.h"
13 #include "hv_utils_transport.h"
15 static DEFINE_SPINLOCK(hvt_list_lock);
16 static struct list_head hvt_list = LIST_HEAD_INIT(hvt_list);
18 static void hvt_reset(struct hvutil_transport *hvt)
27 static ssize_t hvt_op_read(struct file *file, char __user *buf,
28 size_t count, loff_t *ppos)
30 struct hvutil_transport *hvt;
33 hvt = container_of(file->f_op, struct hvutil_transport, fops);
35 if (wait_event_interruptible(hvt->outmsg_q, hvt->outmsg_len > 0 ||
36 hvt->mode != HVUTIL_TRANSPORT_CHARDEV))
39 mutex_lock(&hvt->lock);
41 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
51 if (count < hvt->outmsg_len) {
56 if (!copy_to_user(buf, hvt->outmsg, hvt->outmsg_len))
57 ret = hvt->outmsg_len;
70 mutex_unlock(&hvt->lock);
74 static ssize_t hvt_op_write(struct file *file, const char __user *buf,
75 size_t count, loff_t *ppos)
77 struct hvutil_transport *hvt;
81 hvt = container_of(file->f_op, struct hvutil_transport, fops);
83 inmsg = memdup_user(buf, count);
85 return PTR_ERR(inmsg);
87 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
90 ret = hvt->on_msg(inmsg, count);
94 return ret ? ret : count;
97 static __poll_t hvt_op_poll(struct file *file, poll_table *wait)
99 struct hvutil_transport *hvt;
101 hvt = container_of(file->f_op, struct hvutil_transport, fops);
103 poll_wait(file, &hvt->outmsg_q, wait);
105 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
106 return EPOLLERR | EPOLLHUP;
108 if (hvt->outmsg_len > 0)
109 return EPOLLIN | EPOLLRDNORM;
114 static int hvt_op_open(struct inode *inode, struct file *file)
116 struct hvutil_transport *hvt;
118 bool issue_reset = false;
120 hvt = container_of(file->f_op, struct hvutil_transport, fops);
122 mutex_lock(&hvt->lock);
124 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
126 } else if (hvt->mode == HVUTIL_TRANSPORT_INIT) {
128 * Switching to CHARDEV mode. We switch bach to INIT when
129 * device gets released.
131 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
133 else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
135 * We're switching from netlink communication to using char
136 * device. Issue the reset first.
139 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
147 mutex_unlock(&hvt->lock);
152 static void hvt_transport_free(struct hvutil_transport *hvt)
154 misc_deregister(&hvt->mdev);
159 static int hvt_op_release(struct inode *inode, struct file *file)
161 struct hvutil_transport *hvt;
164 hvt = container_of(file->f_op, struct hvutil_transport, fops);
166 mutex_lock(&hvt->lock);
167 mode_old = hvt->mode;
168 if (hvt->mode != HVUTIL_TRANSPORT_DESTROY)
169 hvt->mode = HVUTIL_TRANSPORT_INIT;
171 * Cleanup message buffers to avoid spurious messages when the daemon
176 if (mode_old == HVUTIL_TRANSPORT_DESTROY)
177 complete(&hvt->release);
179 mutex_unlock(&hvt->lock);
184 static void hvt_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
186 struct hvutil_transport *hvt, *hvt_found = NULL;
188 spin_lock(&hvt_list_lock);
189 list_for_each_entry(hvt, &hvt_list, list) {
190 if (hvt->cn_id.idx == msg->id.idx &&
191 hvt->cn_id.val == msg->id.val) {
196 spin_unlock(&hvt_list_lock);
198 pr_warn("hvt_cn_callback: spurious message received!\n");
203 * Switching to NETLINK mode. Switching to CHARDEV happens when someone
206 mutex_lock(&hvt->lock);
207 if (hvt->mode == HVUTIL_TRANSPORT_INIT)
208 hvt->mode = HVUTIL_TRANSPORT_NETLINK;
210 if (hvt->mode == HVUTIL_TRANSPORT_NETLINK)
211 hvt_found->on_msg(msg->data, msg->len);
213 pr_warn("hvt_cn_callback: unexpected netlink message!\n");
214 mutex_unlock(&hvt->lock);
217 int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len,
218 void (*on_read_cb)(void))
220 struct cn_msg *cn_msg;
223 if (hvt->mode == HVUTIL_TRANSPORT_INIT ||
224 hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
226 } else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
227 cn_msg = kzalloc(sizeof(*cn_msg) + len, GFP_ATOMIC);
230 cn_msg->id.idx = hvt->cn_id.idx;
231 cn_msg->id.val = hvt->cn_id.val;
233 memcpy(cn_msg->data, msg, len);
234 ret = cn_netlink_send(cn_msg, 0, 0, GFP_ATOMIC);
237 * We don't know when netlink messages are delivered but unlike
238 * in CHARDEV mode we're not blocked and we can send next
239 * messages right away.
245 /* HVUTIL_TRANSPORT_CHARDEV */
246 mutex_lock(&hvt->lock);
247 if (hvt->mode != HVUTIL_TRANSPORT_CHARDEV) {
253 /* Previous message wasn't received */
257 hvt->outmsg = kzalloc(len, GFP_KERNEL);
259 memcpy(hvt->outmsg, msg, len);
260 hvt->outmsg_len = len;
261 hvt->on_read = on_read_cb;
262 wake_up_interruptible(&hvt->outmsg_q);
266 mutex_unlock(&hvt->lock);
270 struct hvutil_transport *hvutil_transport_init(const char *name,
271 u32 cn_idx, u32 cn_val,
272 int (*on_msg)(void *, int),
273 void (*on_reset)(void))
275 struct hvutil_transport *hvt;
277 hvt = kzalloc(sizeof(*hvt), GFP_KERNEL);
281 hvt->cn_id.idx = cn_idx;
282 hvt->cn_id.val = cn_val;
284 hvt->mdev.minor = MISC_DYNAMIC_MINOR;
285 hvt->mdev.name = name;
287 hvt->fops.owner = THIS_MODULE;
288 hvt->fops.read = hvt_op_read;
289 hvt->fops.write = hvt_op_write;
290 hvt->fops.poll = hvt_op_poll;
291 hvt->fops.open = hvt_op_open;
292 hvt->fops.release = hvt_op_release;
294 hvt->mdev.fops = &hvt->fops;
296 init_waitqueue_head(&hvt->outmsg_q);
297 mutex_init(&hvt->lock);
298 init_completion(&hvt->release);
300 spin_lock(&hvt_list_lock);
301 list_add(&hvt->list, &hvt_list);
302 spin_unlock(&hvt_list_lock);
304 hvt->on_msg = on_msg;
305 hvt->on_reset = on_reset;
307 if (misc_register(&hvt->mdev))
310 /* Use cn_id.idx/cn_id.val to determine if we need to setup netlink */
311 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0 &&
312 cn_add_callback(&hvt->cn_id, name, hvt_cn_callback))
318 spin_lock(&hvt_list_lock);
319 list_del(&hvt->list);
320 spin_unlock(&hvt_list_lock);
325 void hvutil_transport_destroy(struct hvutil_transport *hvt)
329 mutex_lock(&hvt->lock);
330 mode_old = hvt->mode;
331 hvt->mode = HVUTIL_TRANSPORT_DESTROY;
332 wake_up_interruptible(&hvt->outmsg_q);
333 mutex_unlock(&hvt->lock);
336 * In case we were in 'chardev' mode we still have an open fd so we
337 * have to defer freeing the device. Netlink interface can be freed
340 spin_lock(&hvt_list_lock);
341 list_del(&hvt->list);
342 spin_unlock(&hvt_list_lock);
343 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0)
344 cn_del_callback(&hvt->cn_id);
346 if (mode_old == HVUTIL_TRANSPORT_CHARDEV)
347 wait_for_completion(&hvt->release);
349 hvt_transport_free(hvt);