1 /*
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 
25 static DEFINE_SPINLOCK(hvt_list_lock);
26 static struct list_head hvt_list = LIST_HEAD_INIT(hvt_list);
27 
28 static void hvt_reset(struct hvutil_transport *hvt)
29 {
30 	mutex_lock(&hvt->outmsg_lock);
31 	kfree(hvt->outmsg);
32 	hvt->outmsg = NULL;
33 	hvt->outmsg_len = 0;
34 	mutex_unlock(&hvt->outmsg_lock);
35 	if (hvt->on_reset)
36 		hvt->on_reset();
37 }
38 
39 static ssize_t hvt_op_read(struct file *file, char __user *buf,
40 			   size_t count, loff_t *ppos)
41 {
42 	struct hvutil_transport *hvt;
43 	int ret;
44 
45 	hvt = container_of(file->f_op, struct hvutil_transport, fops);
46 
47 	if (wait_event_interruptible(hvt->outmsg_q, hvt->outmsg_len > 0))
48 		return -EINTR;
49 
50 	mutex_lock(&hvt->outmsg_lock);
51 	if (!hvt->outmsg) {
52 		ret = -EAGAIN;
53 		goto out_unlock;
54 	}
55 
56 	if (count < hvt->outmsg_len) {
57 		ret = -EINVAL;
58 		goto out_unlock;
59 	}
60 
61 	if (!copy_to_user(buf, hvt->outmsg, hvt->outmsg_len))
62 		ret = hvt->outmsg_len;
63 	else
64 		ret = -EFAULT;
65 
66 	kfree(hvt->outmsg);
67 	hvt->outmsg = NULL;
68 	hvt->outmsg_len = 0;
69 
70 out_unlock:
71 	mutex_unlock(&hvt->outmsg_lock);
72 	return ret;
73 }
74 
75 static ssize_t hvt_op_write(struct file *file, const char __user *buf,
76 			    size_t count, loff_t *ppos)
77 {
78 	struct hvutil_transport *hvt;
79 	u8 *inmsg;
80 
81 	hvt = container_of(file->f_op, struct hvutil_transport, fops);
82 
83 	inmsg = kzalloc(count, GFP_KERNEL);
84 	if (copy_from_user(inmsg, buf, count)) {
85 		kfree(inmsg);
86 		return -EFAULT;
87 	}
88 	if (hvt->on_msg(inmsg, count))
89 		return -EFAULT;
90 	kfree(inmsg);
91 
92 	return count;
93 }
94 
95 static unsigned int hvt_op_poll(struct file *file, poll_table *wait)
96 {
97 	struct hvutil_transport *hvt;
98 
99 	hvt = container_of(file->f_op, struct hvutil_transport, fops);
100 
101 	poll_wait(file, &hvt->outmsg_q, wait);
102 	if (hvt->outmsg_len > 0)
103 		return POLLIN | POLLRDNORM;
104 
105 	return 0;
106 }
107 
108 static int hvt_op_open(struct inode *inode, struct file *file)
109 {
110 	struct hvutil_transport *hvt;
111 
112 	hvt = container_of(file->f_op, struct hvutil_transport, fops);
113 
114 	/*
115 	 * Switching to CHARDEV mode. We switch bach to INIT when device
116 	 * gets released.
117 	 */
118 	if (hvt->mode == HVUTIL_TRANSPORT_INIT)
119 		hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
120 	else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
121 		/*
122 		 * We're switching from netlink communication to using char
123 		 * device. Issue the reset first.
124 		 */
125 		hvt_reset(hvt);
126 		hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
127 	} else
128 		return -EBUSY;
129 
130 	return 0;
131 }
132 
133 static int hvt_op_release(struct inode *inode, struct file *file)
134 {
135 	struct hvutil_transport *hvt;
136 
137 	hvt = container_of(file->f_op, struct hvutil_transport, fops);
138 
139 	hvt->mode = HVUTIL_TRANSPORT_INIT;
140 	/*
141 	 * Cleanup message buffers to avoid spurious messages when the daemon
142 	 * connects back.
143 	 */
144 	hvt_reset(hvt);
145 
146 	return 0;
147 }
148 
149 static void hvt_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
150 {
151 	struct hvutil_transport *hvt, *hvt_found = NULL;
152 
153 	spin_lock(&hvt_list_lock);
154 	list_for_each_entry(hvt, &hvt_list, list) {
155 		if (hvt->cn_id.idx == msg->id.idx &&
156 		    hvt->cn_id.val == msg->id.val) {
157 			hvt_found = hvt;
158 			break;
159 		}
160 	}
161 	spin_unlock(&hvt_list_lock);
162 	if (!hvt_found) {
163 		pr_warn("hvt_cn_callback: spurious message received!\n");
164 		return;
165 	}
166 
167 	/*
168 	 * Switching to NETLINK mode. Switching to CHARDEV happens when someone
169 	 * opens the device.
170 	 */
171 	if (hvt->mode == HVUTIL_TRANSPORT_INIT)
172 		hvt->mode = HVUTIL_TRANSPORT_NETLINK;
173 
174 	if (hvt->mode == HVUTIL_TRANSPORT_NETLINK)
175 		hvt_found->on_msg(msg->data, msg->len);
176 	else
177 		pr_warn("hvt_cn_callback: unexpected netlink message!\n");
178 }
179 
180 int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len)
181 {
182 	struct cn_msg *cn_msg;
183 	int ret = 0;
184 
185 	if (hvt->mode == HVUTIL_TRANSPORT_INIT) {
186 		return -EINVAL;
187 	} else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
188 		cn_msg = kzalloc(sizeof(*cn_msg) + len, GFP_ATOMIC);
189 		if (!cn_msg)
190 			return -ENOMEM;
191 		cn_msg->id.idx = hvt->cn_id.idx;
192 		cn_msg->id.val = hvt->cn_id.val;
193 		cn_msg->len = len;
194 		memcpy(cn_msg->data, msg, len);
195 		ret = cn_netlink_send(cn_msg, 0, 0, GFP_ATOMIC);
196 		kfree(cn_msg);
197 		return ret;
198 	}
199 	/* HVUTIL_TRANSPORT_CHARDEV */
200 	mutex_lock(&hvt->outmsg_lock);
201 	if (hvt->outmsg) {
202 		/* Previous message wasn't received */
203 		ret = -EFAULT;
204 		goto out_unlock;
205 	}
206 	hvt->outmsg = kzalloc(len, GFP_KERNEL);
207 	memcpy(hvt->outmsg, msg, len);
208 	hvt->outmsg_len = len;
209 	wake_up_interruptible(&hvt->outmsg_q);
210 out_unlock:
211 	mutex_unlock(&hvt->outmsg_lock);
212 	return ret;
213 }
214 
215 struct hvutil_transport *hvutil_transport_init(const char *name,
216 					       u32 cn_idx, u32 cn_val,
217 					       int (*on_msg)(void *, int),
218 					       void (*on_reset)(void))
219 {
220 	struct hvutil_transport *hvt;
221 
222 	hvt = kzalloc(sizeof(*hvt), GFP_KERNEL);
223 	if (!hvt)
224 		return NULL;
225 
226 	hvt->cn_id.idx = cn_idx;
227 	hvt->cn_id.val = cn_val;
228 
229 	hvt->mdev.minor = MISC_DYNAMIC_MINOR;
230 	hvt->mdev.name = name;
231 
232 	hvt->fops.owner = THIS_MODULE;
233 	hvt->fops.read = hvt_op_read;
234 	hvt->fops.write = hvt_op_write;
235 	hvt->fops.poll = hvt_op_poll;
236 	hvt->fops.open = hvt_op_open;
237 	hvt->fops.release = hvt_op_release;
238 
239 	hvt->mdev.fops = &hvt->fops;
240 
241 	init_waitqueue_head(&hvt->outmsg_q);
242 	mutex_init(&hvt->outmsg_lock);
243 
244 	spin_lock(&hvt_list_lock);
245 	list_add(&hvt->list, &hvt_list);
246 	spin_unlock(&hvt_list_lock);
247 
248 	hvt->on_msg = on_msg;
249 	hvt->on_reset = on_reset;
250 
251 	if (misc_register(&hvt->mdev))
252 		goto err_free_hvt;
253 
254 	/* Use cn_id.idx/cn_id.val to determine if we need to setup netlink */
255 	if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0 &&
256 	    cn_add_callback(&hvt->cn_id, name, hvt_cn_callback))
257 		goto err_free_hvt;
258 
259 	return hvt;
260 
261 err_free_hvt:
262 	kfree(hvt);
263 	return NULL;
264 }
265 
266 void hvutil_transport_destroy(struct hvutil_transport *hvt)
267 {
268 	spin_lock(&hvt_list_lock);
269 	list_del(&hvt->list);
270 	spin_unlock(&hvt_list_lock);
271 	if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0)
272 		cn_del_callback(&hvt->cn_id);
273 	misc_deregister(&hvt->mdev);
274 	kfree(hvt->outmsg);
275 	kfree(hvt);
276 }
277