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)
37 static ssize_t hvt_op_read(struct file *file, char __user *buf,
38 size_t count, loff_t *ppos)
40 struct hvutil_transport *hvt;
43 hvt = container_of(file->f_op, struct hvutil_transport, fops);
45 if (wait_event_interruptible(hvt->outmsg_q, hvt->outmsg_len > 0 ||
46 hvt->mode != HVUTIL_TRANSPORT_CHARDEV))
49 mutex_lock(&hvt->lock);
51 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
61 if (count < hvt->outmsg_len) {
66 if (!copy_to_user(buf, hvt->outmsg, hvt->outmsg_len))
67 ret = hvt->outmsg_len;
76 mutex_unlock(&hvt->lock);
80 static ssize_t hvt_op_write(struct file *file, const char __user *buf,
81 size_t count, loff_t *ppos)
83 struct hvutil_transport *hvt;
87 hvt = container_of(file->f_op, struct hvutil_transport, fops);
89 inmsg = memdup_user(buf, count);
91 return PTR_ERR(inmsg);
93 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
96 ret = hvt->on_msg(inmsg, count);
100 return ret ? ret : count;
103 static unsigned int hvt_op_poll(struct file *file, poll_table *wait)
105 struct hvutil_transport *hvt;
107 hvt = container_of(file->f_op, struct hvutil_transport, fops);
109 poll_wait(file, &hvt->outmsg_q, wait);
111 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
114 if (hvt->outmsg_len > 0)
115 return POLLIN | POLLRDNORM;
120 static int hvt_op_open(struct inode *inode, struct file *file)
122 struct hvutil_transport *hvt;
124 bool issue_reset = false;
126 hvt = container_of(file->f_op, struct hvutil_transport, fops);
128 mutex_lock(&hvt->lock);
130 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
132 } else if (hvt->mode == HVUTIL_TRANSPORT_INIT) {
134 * Switching to CHARDEV mode. We switch bach to INIT when
135 * device gets released.
137 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
139 else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
141 * We're switching from netlink communication to using char
142 * device. Issue the reset first.
145 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
153 mutex_unlock(&hvt->lock);
158 static int hvt_op_release(struct inode *inode, struct file *file)
160 struct hvutil_transport *hvt;
162 hvt = container_of(file->f_op, struct hvutil_transport, fops);
164 mutex_lock(&hvt->lock);
165 if (hvt->mode != HVUTIL_TRANSPORT_DESTROY)
166 hvt->mode = HVUTIL_TRANSPORT_INIT;
168 * Cleanup message buffers to avoid spurious messages when the daemon
172 mutex_unlock(&hvt->lock);
177 static void hvt_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
179 struct hvutil_transport *hvt, *hvt_found = NULL;
181 spin_lock(&hvt_list_lock);
182 list_for_each_entry(hvt, &hvt_list, list) {
183 if (hvt->cn_id.idx == msg->id.idx &&
184 hvt->cn_id.val == msg->id.val) {
189 spin_unlock(&hvt_list_lock);
191 pr_warn("hvt_cn_callback: spurious message received!\n");
196 * Switching to NETLINK mode. Switching to CHARDEV happens when someone
199 mutex_lock(&hvt->lock);
200 if (hvt->mode == HVUTIL_TRANSPORT_INIT)
201 hvt->mode = HVUTIL_TRANSPORT_NETLINK;
203 if (hvt->mode == HVUTIL_TRANSPORT_NETLINK)
204 hvt_found->on_msg(msg->data, msg->len);
206 pr_warn("hvt_cn_callback: unexpected netlink message!\n");
207 mutex_unlock(&hvt->lock);
210 int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len)
212 struct cn_msg *cn_msg;
215 if (hvt->mode == HVUTIL_TRANSPORT_INIT ||
216 hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
218 } else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
219 cn_msg = kzalloc(sizeof(*cn_msg) + len, GFP_ATOMIC);
222 cn_msg->id.idx = hvt->cn_id.idx;
223 cn_msg->id.val = hvt->cn_id.val;
225 memcpy(cn_msg->data, msg, len);
226 ret = cn_netlink_send(cn_msg, 0, 0, GFP_ATOMIC);
230 /* HVUTIL_TRANSPORT_CHARDEV */
231 mutex_lock(&hvt->lock);
232 if (hvt->mode != HVUTIL_TRANSPORT_CHARDEV) {
238 /* Previous message wasn't received */
242 hvt->outmsg = kzalloc(len, GFP_KERNEL);
244 memcpy(hvt->outmsg, msg, len);
245 hvt->outmsg_len = len;
246 wake_up_interruptible(&hvt->outmsg_q);
250 mutex_unlock(&hvt->lock);
254 struct hvutil_transport *hvutil_transport_init(const char *name,
255 u32 cn_idx, u32 cn_val,
256 int (*on_msg)(void *, int),
257 void (*on_reset)(void))
259 struct hvutil_transport *hvt;
261 hvt = kzalloc(sizeof(*hvt), GFP_KERNEL);
265 hvt->cn_id.idx = cn_idx;
266 hvt->cn_id.val = cn_val;
268 hvt->mdev.minor = MISC_DYNAMIC_MINOR;
269 hvt->mdev.name = name;
271 hvt->fops.owner = THIS_MODULE;
272 hvt->fops.read = hvt_op_read;
273 hvt->fops.write = hvt_op_write;
274 hvt->fops.poll = hvt_op_poll;
275 hvt->fops.open = hvt_op_open;
276 hvt->fops.release = hvt_op_release;
278 hvt->mdev.fops = &hvt->fops;
280 init_waitqueue_head(&hvt->outmsg_q);
281 mutex_init(&hvt->lock);
283 spin_lock(&hvt_list_lock);
284 list_add(&hvt->list, &hvt_list);
285 spin_unlock(&hvt_list_lock);
287 hvt->on_msg = on_msg;
288 hvt->on_reset = on_reset;
290 if (misc_register(&hvt->mdev))
293 /* Use cn_id.idx/cn_id.val to determine if we need to setup netlink */
294 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0 &&
295 cn_add_callback(&hvt->cn_id, name, hvt_cn_callback))
305 void hvutil_transport_destroy(struct hvutil_transport *hvt)
307 mutex_lock(&hvt->lock);
308 hvt->mode = HVUTIL_TRANSPORT_DESTROY;
309 wake_up_interruptible(&hvt->outmsg_q);
310 mutex_unlock(&hvt->lock);
312 spin_lock(&hvt_list_lock);
313 list_del(&hvt->list);
314 spin_unlock(&hvt_list_lock);
315 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0)
316 cn_del_callback(&hvt->cn_id);
317 misc_deregister(&hvt->mdev);