2 * Kernel/userspace transport abstraction for Hyper-V util driver.
4 * Copyright (C) 2015, Vitaly Kuznetsov <vkuznets@redhat.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
18 #include <linux/slab.h>
20 #include <linux/poll.h>
22 #include "hyperv_vmbus.h"
23 #include "hv_utils_transport.h"
25 static DEFINE_SPINLOCK(hvt_list_lock);
26 static struct list_head hvt_list = LIST_HEAD_INIT(hvt_list);
28 static void hvt_reset(struct hvutil_transport *hvt)
30 mutex_lock(&hvt->outmsg_lock);
34 mutex_unlock(&hvt->outmsg_lock);
39 static ssize_t hvt_op_read(struct file *file, char __user *buf,
40 size_t count, loff_t *ppos)
42 struct hvutil_transport *hvt;
45 hvt = container_of(file->f_op, struct hvutil_transport, fops);
47 if (wait_event_interruptible(hvt->outmsg_q, hvt->outmsg_len > 0))
50 mutex_lock(&hvt->outmsg_lock);
56 if (count < hvt->outmsg_len) {
61 if (!copy_to_user(buf, hvt->outmsg, hvt->outmsg_len))
62 ret = hvt->outmsg_len;
71 mutex_unlock(&hvt->outmsg_lock);
75 static ssize_t hvt_op_write(struct file *file, const char __user *buf,
76 size_t count, loff_t *ppos)
78 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->on_msg(inmsg, count))
94 static unsigned int hvt_op_poll(struct file *file, poll_table *wait)
96 struct hvutil_transport *hvt;
98 hvt = container_of(file->f_op, struct hvutil_transport, fops);
100 poll_wait(file, &hvt->outmsg_q, wait);
101 if (hvt->outmsg_len > 0)
102 return POLLIN | POLLRDNORM;
107 static int hvt_op_open(struct inode *inode, struct file *file)
109 struct hvutil_transport *hvt;
111 hvt = container_of(file->f_op, struct hvutil_transport, fops);
114 * Switching to CHARDEV mode. We switch bach to INIT when device
117 if (hvt->mode == HVUTIL_TRANSPORT_INIT)
118 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
119 else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
121 * We're switching from netlink communication to using char
122 * device. Issue the reset first.
125 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
132 static int hvt_op_release(struct inode *inode, struct file *file)
134 struct hvutil_transport *hvt;
136 hvt = container_of(file->f_op, struct hvutil_transport, fops);
138 hvt->mode = HVUTIL_TRANSPORT_INIT;
140 * Cleanup message buffers to avoid spurious messages when the daemon
148 static void hvt_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
150 struct hvutil_transport *hvt, *hvt_found = NULL;
152 spin_lock(&hvt_list_lock);
153 list_for_each_entry(hvt, &hvt_list, list) {
154 if (hvt->cn_id.idx == msg->id.idx &&
155 hvt->cn_id.val == msg->id.val) {
160 spin_unlock(&hvt_list_lock);
162 pr_warn("hvt_cn_callback: spurious message received!\n");
167 * Switching to NETLINK mode. Switching to CHARDEV happens when someone
170 if (hvt->mode == HVUTIL_TRANSPORT_INIT)
171 hvt->mode = HVUTIL_TRANSPORT_NETLINK;
173 if (hvt->mode == HVUTIL_TRANSPORT_NETLINK)
174 hvt_found->on_msg(msg->data, msg->len);
176 pr_warn("hvt_cn_callback: unexpected netlink message!\n");
179 int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len)
181 struct cn_msg *cn_msg;
184 if (hvt->mode == HVUTIL_TRANSPORT_INIT) {
186 } else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
187 cn_msg = kzalloc(sizeof(*cn_msg) + len, GFP_ATOMIC);
190 cn_msg->id.idx = hvt->cn_id.idx;
191 cn_msg->id.val = hvt->cn_id.val;
193 memcpy(cn_msg->data, msg, len);
194 ret = cn_netlink_send(cn_msg, 0, 0, GFP_ATOMIC);
198 /* HVUTIL_TRANSPORT_CHARDEV */
199 mutex_lock(&hvt->outmsg_lock);
201 /* Previous message wasn't received */
205 hvt->outmsg = kzalloc(len, GFP_KERNEL);
207 memcpy(hvt->outmsg, msg, len);
208 hvt->outmsg_len = len;
209 wake_up_interruptible(&hvt->outmsg_q);
213 mutex_unlock(&hvt->outmsg_lock);
217 struct hvutil_transport *hvutil_transport_init(const char *name,
218 u32 cn_idx, u32 cn_val,
219 int (*on_msg)(void *, int),
220 void (*on_reset)(void))
222 struct hvutil_transport *hvt;
224 hvt = kzalloc(sizeof(*hvt), GFP_KERNEL);
228 hvt->cn_id.idx = cn_idx;
229 hvt->cn_id.val = cn_val;
231 hvt->mdev.minor = MISC_DYNAMIC_MINOR;
232 hvt->mdev.name = name;
234 hvt->fops.owner = THIS_MODULE;
235 hvt->fops.read = hvt_op_read;
236 hvt->fops.write = hvt_op_write;
237 hvt->fops.poll = hvt_op_poll;
238 hvt->fops.open = hvt_op_open;
239 hvt->fops.release = hvt_op_release;
241 hvt->mdev.fops = &hvt->fops;
243 init_waitqueue_head(&hvt->outmsg_q);
244 mutex_init(&hvt->outmsg_lock);
246 spin_lock(&hvt_list_lock);
247 list_add(&hvt->list, &hvt_list);
248 spin_unlock(&hvt_list_lock);
250 hvt->on_msg = on_msg;
251 hvt->on_reset = on_reset;
253 if (misc_register(&hvt->mdev))
256 /* Use cn_id.idx/cn_id.val to determine if we need to setup netlink */
257 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0 &&
258 cn_add_callback(&hvt->cn_id, name, hvt_cn_callback))
268 void hvutil_transport_destroy(struct hvutil_transport *hvt)
270 spin_lock(&hvt_list_lock);
271 list_del(&hvt->list);
272 spin_unlock(&hvt_list_lock);
273 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0)
274 cn_del_callback(&hvt->cn_id);
275 misc_deregister(&hvt->mdev);