blob: 4402a71e23f7f7277c6d1561c4a8db5d46489b8d [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
Vitaly Kuznetsove0fa3e52016-06-09 17:08:57 -070075 if (hvt->on_read)
76 hvt->on_read();
77 hvt->on_read = NULL;
78
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -070079out_unlock:
Vitaly Kuznetsova72f3a42015-12-14 19:01:54 -080080 mutex_unlock(&hvt->lock);
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -070081 return ret;
82}
83
84static ssize_t hvt_op_write(struct file *file, const char __user *buf,
85 size_t count, loff_t *ppos)
86{
87 struct hvutil_transport *hvt;
88 u8 *inmsg;
Vitaly Kuznetsov1f753382015-12-14 19:01:53 -080089 int ret;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -070090
91 hvt = container_of(file->f_op, struct hvutil_transport, fops);
92
Olaf Heringb0035962015-12-14 16:01:37 -080093 inmsg = memdup_user(buf, count);
94 if (IS_ERR(inmsg))
95 return PTR_ERR(inmsg);
96
Vitaly Kuznetsova1502562015-12-14 19:01:55 -080097 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
98 ret = -EBADF;
99 else
100 ret = hvt->on_msg(inmsg, count);
Vitaly Kuznetsov1f753382015-12-14 19:01:53 -0800101
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700102 kfree(inmsg);
103
Vitaly Kuznetsov1f753382015-12-14 19:01:53 -0800104 return ret ? ret : count;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700105}
106
107static unsigned int hvt_op_poll(struct file *file, poll_table *wait)
108{
109 struct hvutil_transport *hvt;
110
111 hvt = container_of(file->f_op, struct hvutil_transport, fops);
112
113 poll_wait(file, &hvt->outmsg_q, wait);
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800114
115 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
Vitaly Kuznetsov77b744a2015-12-15 16:27:26 -0800116 return POLLERR | POLLHUP;
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800117
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700118 if (hvt->outmsg_len > 0)
119 return POLLIN | POLLRDNORM;
120
121 return 0;
122}
123
124static int hvt_op_open(struct inode *inode, struct file *file)
125{
126 struct hvutil_transport *hvt;
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800127 int ret = 0;
128 bool issue_reset = false;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700129
130 hvt = container_of(file->f_op, struct hvutil_transport, fops);
131
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800132 mutex_lock(&hvt->lock);
133
134 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
135 ret = -EBADF;
136 } else if (hvt->mode == HVUTIL_TRANSPORT_INIT) {
137 /*
138 * Switching to CHARDEV mode. We switch bach to INIT when
139 * device gets released.
140 */
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700141 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800142 }
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700143 else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
144 /*
145 * We're switching from netlink communication to using char
146 * device. Issue the reset first.
147 */
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800148 issue_reset = true;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700149 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800150 } else {
151 ret = -EBUSY;
152 }
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700153
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800154 if (issue_reset)
155 hvt_reset(hvt);
156
157 mutex_unlock(&hvt->lock);
158
159 return ret;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700160}
161
Vitaly Kuznetsov94200982015-12-14 19:01:56 -0800162static void hvt_transport_free(struct hvutil_transport *hvt)
163{
164 misc_deregister(&hvt->mdev);
165 kfree(hvt->outmsg);
166 kfree(hvt);
167}
168
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700169static int hvt_op_release(struct inode *inode, struct file *file)
170{
171 struct hvutil_transport *hvt;
Vitaly Kuznetsov94200982015-12-14 19:01:56 -0800172 int mode_old;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700173
174 hvt = container_of(file->f_op, struct hvutil_transport, fops);
175
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800176 mutex_lock(&hvt->lock);
Vitaly Kuznetsov94200982015-12-14 19:01:56 -0800177 mode_old = hvt->mode;
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800178 if (hvt->mode != HVUTIL_TRANSPORT_DESTROY)
179 hvt->mode = HVUTIL_TRANSPORT_INIT;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700180 /*
181 * Cleanup message buffers to avoid spurious messages when the daemon
182 * connects back.
183 */
184 hvt_reset(hvt);
185
Vitaly Kuznetsov94200982015-12-14 19:01:56 -0800186 if (mode_old == HVUTIL_TRANSPORT_DESTROY)
Vitaly Kuznetsove9c18ae2017-03-04 18:13:59 -0700187 complete(&hvt->release);
188
189 mutex_unlock(&hvt->lock);
Vitaly Kuznetsov94200982015-12-14 19:01:56 -0800190
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700191 return 0;
192}
193
194static void hvt_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
195{
196 struct hvutil_transport *hvt, *hvt_found = NULL;
197
198 spin_lock(&hvt_list_lock);
199 list_for_each_entry(hvt, &hvt_list, list) {
200 if (hvt->cn_id.idx == msg->id.idx &&
201 hvt->cn_id.val == msg->id.val) {
202 hvt_found = hvt;
203 break;
204 }
205 }
206 spin_unlock(&hvt_list_lock);
207 if (!hvt_found) {
208 pr_warn("hvt_cn_callback: spurious message received!\n");
209 return;
210 }
211
212 /*
213 * Switching to NETLINK mode. Switching to CHARDEV happens when someone
214 * opens the device.
215 */
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800216 mutex_lock(&hvt->lock);
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700217 if (hvt->mode == HVUTIL_TRANSPORT_INIT)
218 hvt->mode = HVUTIL_TRANSPORT_NETLINK;
219
220 if (hvt->mode == HVUTIL_TRANSPORT_NETLINK)
221 hvt_found->on_msg(msg->data, msg->len);
222 else
223 pr_warn("hvt_cn_callback: unexpected netlink message!\n");
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800224 mutex_unlock(&hvt->lock);
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700225}
226
Vitaly Kuznetsove0fa3e52016-06-09 17:08:57 -0700227int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len,
228 void (*on_read_cb)(void))
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700229{
230 struct cn_msg *cn_msg;
231 int ret = 0;
232
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800233 if (hvt->mode == HVUTIL_TRANSPORT_INIT ||
234 hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700235 return -EINVAL;
236 } else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
237 cn_msg = kzalloc(sizeof(*cn_msg) + len, GFP_ATOMIC);
Dan Carpenter9dd6a062015-08-01 16:08:17 -0700238 if (!cn_msg)
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700239 return -ENOMEM;
240 cn_msg->id.idx = hvt->cn_id.idx;
241 cn_msg->id.val = hvt->cn_id.val;
242 cn_msg->len = len;
243 memcpy(cn_msg->data, msg, len);
244 ret = cn_netlink_send(cn_msg, 0, 0, GFP_ATOMIC);
245 kfree(cn_msg);
Vitaly Kuznetsove0fa3e52016-06-09 17:08:57 -0700246 /*
247 * We don't know when netlink messages are delivered but unlike
248 * in CHARDEV mode we're not blocked and we can send next
249 * messages right away.
250 */
251 if (on_read_cb)
252 on_read_cb();
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700253 return ret;
254 }
255 /* HVUTIL_TRANSPORT_CHARDEV */
Vitaly Kuznetsova72f3a42015-12-14 19:01:54 -0800256 mutex_lock(&hvt->lock);
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800257 if (hvt->mode != HVUTIL_TRANSPORT_CHARDEV) {
258 ret = -EINVAL;
259 goto out_unlock;
260 }
261
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700262 if (hvt->outmsg) {
263 /* Previous message wasn't received */
264 ret = -EFAULT;
265 goto out_unlock;
266 }
267 hvt->outmsg = kzalloc(len, GFP_KERNEL);
Olaf Heringcdc0c0c2015-12-14 16:01:36 -0800268 if (hvt->outmsg) {
269 memcpy(hvt->outmsg, msg, len);
270 hvt->outmsg_len = len;
Vitaly Kuznetsove0fa3e52016-06-09 17:08:57 -0700271 hvt->on_read = on_read_cb;
Olaf Heringcdc0c0c2015-12-14 16:01:36 -0800272 wake_up_interruptible(&hvt->outmsg_q);
273 } else
274 ret = -ENOMEM;
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700275out_unlock:
Vitaly Kuznetsova72f3a42015-12-14 19:01:54 -0800276 mutex_unlock(&hvt->lock);
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700277 return ret;
278}
279
280struct hvutil_transport *hvutil_transport_init(const char *name,
281 u32 cn_idx, u32 cn_val,
282 int (*on_msg)(void *, int),
283 void (*on_reset)(void))
284{
285 struct hvutil_transport *hvt;
286
287 hvt = kzalloc(sizeof(*hvt), GFP_KERNEL);
288 if (!hvt)
289 return NULL;
290
291 hvt->cn_id.idx = cn_idx;
292 hvt->cn_id.val = cn_val;
293
294 hvt->mdev.minor = MISC_DYNAMIC_MINOR;
295 hvt->mdev.name = name;
296
297 hvt->fops.owner = THIS_MODULE;
298 hvt->fops.read = hvt_op_read;
299 hvt->fops.write = hvt_op_write;
300 hvt->fops.poll = hvt_op_poll;
301 hvt->fops.open = hvt_op_open;
302 hvt->fops.release = hvt_op_release;
303
304 hvt->mdev.fops = &hvt->fops;
305
306 init_waitqueue_head(&hvt->outmsg_q);
Vitaly Kuznetsova72f3a42015-12-14 19:01:54 -0800307 mutex_init(&hvt->lock);
Vitaly Kuznetsove9c18ae2017-03-04 18:13:59 -0700308 init_completion(&hvt->release);
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700309
310 spin_lock(&hvt_list_lock);
311 list_add(&hvt->list, &hvt_list);
312 spin_unlock(&hvt_list_lock);
313
314 hvt->on_msg = on_msg;
315 hvt->on_reset = on_reset;
316
317 if (misc_register(&hvt->mdev))
318 goto err_free_hvt;
319
320 /* Use cn_id.idx/cn_id.val to determine if we need to setup netlink */
321 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0 &&
322 cn_add_callback(&hvt->cn_id, name, hvt_cn_callback))
323 goto err_free_hvt;
324
325 return hvt;
326
327err_free_hvt:
Alex Nge66853b2016-02-26 15:13:20 -0800328 spin_lock(&hvt_list_lock);
329 list_del(&hvt->list);
330 spin_unlock(&hvt_list_lock);
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700331 kfree(hvt);
332 return NULL;
333}
334
335void hvutil_transport_destroy(struct hvutil_transport *hvt)
336{
Vitaly Kuznetsov94200982015-12-14 19:01:56 -0800337 int mode_old;
338
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800339 mutex_lock(&hvt->lock);
Vitaly Kuznetsov94200982015-12-14 19:01:56 -0800340 mode_old = hvt->mode;
Vitaly Kuznetsova1502562015-12-14 19:01:55 -0800341 hvt->mode = HVUTIL_TRANSPORT_DESTROY;
342 wake_up_interruptible(&hvt->outmsg_q);
343 mutex_unlock(&hvt->lock);
344
Vitaly Kuznetsov94200982015-12-14 19:01:56 -0800345 /*
346 * In case we were in 'chardev' mode we still have an open fd so we
347 * have to defer freeing the device. Netlink interface can be freed
348 * now.
349 */
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700350 spin_lock(&hvt_list_lock);
351 list_del(&hvt->list);
352 spin_unlock(&hvt_list_lock);
353 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0)
354 cn_del_callback(&hvt->cn_id);
Vitaly Kuznetsov94200982015-12-14 19:01:56 -0800355
Vitaly Kuznetsove9c18ae2017-03-04 18:13:59 -0700356 if (mode_old == HVUTIL_TRANSPORT_CHARDEV)
357 wait_for_completion(&hvt->release);
358
359 hvt_transport_free(hvt);
Vitaly Kuznetsov14b50f82015-04-11 18:07:51 -0700360}