blob: 31c2f8649271fb8e98baa545e9c13fc267923866 [file] [log] [blame]
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -07001/*
2 * Kernel/userspace transport abstraction for Hyper-V util driver.
3 *
4 * Copyright (C) 2015, Vitaly Kuznetsov <vkuznets@redhat.com>
5 *
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.
9 *
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
14 * details.
15 *
16 */
17
18#include <linux/slab.h>
19#include <linux/fs.h>
20#include <linux/poll.h>
21
22#include "hyperv_vmbus.h"
23#include "hv_utils_transport.h"
24
25static DEFINE_SPINLOCK(hvt_list_lock);
26static struct list_head hvt_list = LIST_HEAD_INIT(hvt_list);
27
28static void hvt_reset(struct hvutil_transport *hvt)
29{
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -070030 kfree(hvt->outmsg);
31 hvt->outmsg = NULL;
32 hvt->outmsg_len = 0;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -070033 if (hvt->on_reset)
34 hvt->on_reset();
35}
36
37static ssize_t hvt_op_read(struct file *file, char __user *buf,
38 size_t count, loff_t *ppos)
39{
40 struct hvutil_transport *hvt;
41 int ret;
42
43 hvt = container_of(file->f_op, struct hvutil_transport, fops);
44
Vitaly Kuznetsova1502562015-12-14 19:01:55 -080045 if (wait_event_interruptible(hvt->outmsg_q, hvt->outmsg_len > 0 ||
46 hvt->mode != HVUTIL_TRANSPORT_CHARDEV))
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -070047 return -EINTR;
48
Vitaly Kuznetsova72f3a42015-12-14 19:01:54 -080049 mutex_lock(&hvt->lock);
Vitaly Kuznetsova1502562015-12-14 19:01:55 -080050
51 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
52 ret = -EBADF;
53 goto out_unlock;
54 }
55
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -070056 if (!hvt->outmsg) {
57 ret = -EAGAIN;
58 goto out_unlock;
59 }
60
61 if (count < hvt->outmsg_len) {
62 ret = -EINVAL;
63 goto out_unlock;
64 }
65
66 if (!copy_to_user(buf, hvt->outmsg, hvt->outmsg_len))
67 ret = hvt->outmsg_len;
68 else
69 ret = -EFAULT;
70
71 kfree(hvt->outmsg);
72 hvt->outmsg = NULL;
73 hvt->outmsg_len = 0;
74
75out_unlock:
Vitaly Kuznetsova72f3a42015-12-14 19:01:54 -080076 mutex_unlock(&hvt->lock);
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -070077 return ret;
78}
79
80static ssize_t hvt_op_write(struct file *file, const char __user *buf,
81 size_t count, loff_t *ppos)
82{
83 struct hvutil_transport *hvt;
84 u8 *inmsg;
Vitaly Kuznetsov1f753382015-12-14 19:01:53 -080085 int ret;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -070086
87 hvt = container_of(file->f_op, struct hvutil_transport, fops);
88
Olaf Heringb0035962015-12-14 16:01:37 -080089 inmsg = memdup_user(buf, count);
90 if (IS_ERR(inmsg))
91 return PTR_ERR(inmsg);
92
Vitaly Kuznetsova1502562015-12-14 19:01:55 -080093 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
94 ret = -EBADF;
95 else
96 ret = hvt->on_msg(inmsg, count);
Vitaly Kuznetsov1f753382015-12-14 19:01:53 -080097
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -070098 kfree(inmsg);
99
Vitaly Kuznetsov1f753382015-12-14 19:01:53 -0800100 return ret ? ret : count;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700101}
102
103static unsigned int hvt_op_poll(struct file *file, poll_table *wait)
104{
105 struct hvutil_transport *hvt;
106
107 hvt = container_of(file->f_op, struct hvutil_transport, fops);
108
109 poll_wait(file, &hvt->outmsg_q, wait);
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800110
111 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
112 return -EBADF;
113
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700114 if (hvt->outmsg_len > 0)
115 return POLLIN | POLLRDNORM;
116
117 return 0;
118}
119
120static int hvt_op_open(struct inode *inode, struct file *file)
121{
122 struct hvutil_transport *hvt;
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800123 int ret = 0;
124 bool issue_reset = false;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700125
126 hvt = container_of(file->f_op, struct hvutil_transport, fops);
127
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800128 mutex_lock(&hvt->lock);
129
130 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
131 ret = -EBADF;
132 } else if (hvt->mode == HVUTIL_TRANSPORT_INIT) {
133 /*
134 * Switching to CHARDEV mode. We switch bach to INIT when
135 * device gets released.
136 */
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700137 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800138 }
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700139 else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
140 /*
141 * We're switching from netlink communication to using char
142 * device. Issue the reset first.
143 */
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800144 issue_reset = true;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700145 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800146 } else {
147 ret = -EBUSY;
148 }
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700149
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800150 if (issue_reset)
151 hvt_reset(hvt);
152
153 mutex_unlock(&hvt->lock);
154
155 return ret;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700156}
157
158static int hvt_op_release(struct inode *inode, struct file *file)
159{
160 struct hvutil_transport *hvt;
161
162 hvt = container_of(file->f_op, struct hvutil_transport, fops);
163
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800164 mutex_lock(&hvt->lock);
165 if (hvt->mode != HVUTIL_TRANSPORT_DESTROY)
166 hvt->mode = HVUTIL_TRANSPORT_INIT;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700167 /*
168 * Cleanup message buffers to avoid spurious messages when the daemon
169 * connects back.
170 */
171 hvt_reset(hvt);
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800172 mutex_unlock(&hvt->lock);
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700173
174 return 0;
175}
176
177static void hvt_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
178{
179 struct hvutil_transport *hvt, *hvt_found = NULL;
180
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) {
185 hvt_found = hvt;
186 break;
187 }
188 }
189 spin_unlock(&hvt_list_lock);
190 if (!hvt_found) {
191 pr_warn("hvt_cn_callback: spurious message received!\n");
192 return;
193 }
194
195 /*
196 * Switching to NETLINK mode. Switching to CHARDEV happens when someone
197 * opens the device.
198 */
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800199 mutex_lock(&hvt->lock);
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700200 if (hvt->mode == HVUTIL_TRANSPORT_INIT)
201 hvt->mode = HVUTIL_TRANSPORT_NETLINK;
202
203 if (hvt->mode == HVUTIL_TRANSPORT_NETLINK)
204 hvt_found->on_msg(msg->data, msg->len);
205 else
206 pr_warn("hvt_cn_callback: unexpected netlink message!\n");
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800207 mutex_unlock(&hvt->lock);
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700208}
209
210int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len)
211{
212 struct cn_msg *cn_msg;
213 int ret = 0;
214
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800215 if (hvt->mode == HVUTIL_TRANSPORT_INIT ||
216 hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700217 return -EINVAL;
218 } else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
219 cn_msg = kzalloc(sizeof(*cn_msg) + len, GFP_ATOMIC);
Dan Carpenter9dd6a062015-08-01 16:08:17 -0700220 if (!cn_msg)
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700221 return -ENOMEM;
222 cn_msg->id.idx = hvt->cn_id.idx;
223 cn_msg->id.val = hvt->cn_id.val;
224 cn_msg->len = len;
225 memcpy(cn_msg->data, msg, len);
226 ret = cn_netlink_send(cn_msg, 0, 0, GFP_ATOMIC);
227 kfree(cn_msg);
228 return ret;
229 }
230 /* HVUTIL_TRANSPORT_CHARDEV */
Vitaly Kuznetsova72f3a42015-12-14 19:01:54 -0800231 mutex_lock(&hvt->lock);
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800232 if (hvt->mode != HVUTIL_TRANSPORT_CHARDEV) {
233 ret = -EINVAL;
234 goto out_unlock;
235 }
236
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700237 if (hvt->outmsg) {
238 /* Previous message wasn't received */
239 ret = -EFAULT;
240 goto out_unlock;
241 }
242 hvt->outmsg = kzalloc(len, GFP_KERNEL);
Olaf Heringcdc0c0c2015-12-14 16:01:36 -0800243 if (hvt->outmsg) {
244 memcpy(hvt->outmsg, msg, len);
245 hvt->outmsg_len = len;
246 wake_up_interruptible(&hvt->outmsg_q);
247 } else
248 ret = -ENOMEM;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700249out_unlock:
Vitaly Kuznetsova72f3a42015-12-14 19:01:54 -0800250 mutex_unlock(&hvt->lock);
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700251 return ret;
252}
253
254struct 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))
258{
259 struct hvutil_transport *hvt;
260
261 hvt = kzalloc(sizeof(*hvt), GFP_KERNEL);
262 if (!hvt)
263 return NULL;
264
265 hvt->cn_id.idx = cn_idx;
266 hvt->cn_id.val = cn_val;
267
268 hvt->mdev.minor = MISC_DYNAMIC_MINOR;
269 hvt->mdev.name = name;
270
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;
277
278 hvt->mdev.fops = &hvt->fops;
279
280 init_waitqueue_head(&hvt->outmsg_q);
Vitaly Kuznetsova72f3a42015-12-14 19:01:54 -0800281 mutex_init(&hvt->lock);
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700282
283 spin_lock(&hvt_list_lock);
284 list_add(&hvt->list, &hvt_list);
285 spin_unlock(&hvt_list_lock);
286
287 hvt->on_msg = on_msg;
288 hvt->on_reset = on_reset;
289
290 if (misc_register(&hvt->mdev))
291 goto err_free_hvt;
292
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))
296 goto err_free_hvt;
297
298 return hvt;
299
300err_free_hvt:
301 kfree(hvt);
302 return NULL;
303}
304
305void hvutil_transport_destroy(struct hvutil_transport *hvt)
306{
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800307 mutex_lock(&hvt->lock);
308 hvt->mode = HVUTIL_TRANSPORT_DESTROY;
309 wake_up_interruptible(&hvt->outmsg_q);
310 mutex_unlock(&hvt->lock);
311
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700312 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);
318 kfree(hvt->outmsg);
319 kfree(hvt);
320}