]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/hv/hv_utils_transport.c
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
;
80 mutex_unlock(&hvt
->lock
);
84 static ssize_t
hvt_op_write(struct file
*file
, const char __user
*buf
,
85 size_t count
, loff_t
*ppos
)
87 struct hvutil_transport
*hvt
;
91 hvt
= container_of(file
->f_op
, struct hvutil_transport
, fops
);
93 inmsg
= memdup_user(buf
, count
);
95 return PTR_ERR(inmsg
);
97 if (hvt
->mode
== HVUTIL_TRANSPORT_DESTROY
)
100 ret
= hvt
->on_msg(inmsg
, count
);
104 return ret
? ret
: count
;
107 static unsigned int hvt_op_poll(struct file
*file
, poll_table
*wait
)
109 struct hvutil_transport
*hvt
;
111 hvt
= container_of(file
->f_op
, struct hvutil_transport
, fops
);
113 poll_wait(file
, &hvt
->outmsg_q
, wait
);
115 if (hvt
->mode
== HVUTIL_TRANSPORT_DESTROY
)
116 return POLLERR
| POLLHUP
;
118 if (hvt
->outmsg_len
> 0)
119 return POLLIN
| POLLRDNORM
;
124 static int hvt_op_open(struct inode
*inode
, struct file
*file
)
126 struct hvutil_transport
*hvt
;
128 bool issue_reset
= false;
130 hvt
= container_of(file
->f_op
, struct hvutil_transport
, fops
);
132 mutex_lock(&hvt
->lock
);
134 if (hvt
->mode
== HVUTIL_TRANSPORT_DESTROY
) {
136 } else if (hvt
->mode
== HVUTIL_TRANSPORT_INIT
) {
138 * Switching to CHARDEV mode. We switch bach to INIT when
139 * device gets released.
141 hvt
->mode
= HVUTIL_TRANSPORT_CHARDEV
;
143 else if (hvt
->mode
== HVUTIL_TRANSPORT_NETLINK
) {
145 * We're switching from netlink communication to using char
146 * device. Issue the reset first.
149 hvt
->mode
= HVUTIL_TRANSPORT_CHARDEV
;
157 mutex_unlock(&hvt
->lock
);
162 static void hvt_transport_free(struct hvutil_transport
*hvt
)
164 misc_deregister(&hvt
->mdev
);
169 static int hvt_op_release(struct inode
*inode
, struct file
*file
)
171 struct hvutil_transport
*hvt
;
174 hvt
= container_of(file
->f_op
, struct hvutil_transport
, fops
);
176 mutex_lock(&hvt
->lock
);
177 mode_old
= hvt
->mode
;
178 if (hvt
->mode
!= HVUTIL_TRANSPORT_DESTROY
)
179 hvt
->mode
= HVUTIL_TRANSPORT_INIT
;
181 * Cleanup message buffers to avoid spurious messages when the daemon
186 if (mode_old
== HVUTIL_TRANSPORT_DESTROY
)
187 complete(&hvt
->release
);
189 mutex_unlock(&hvt
->lock
);
194 static void hvt_cn_callback(struct cn_msg
*msg
, struct netlink_skb_parms
*nsp
)
196 struct hvutil_transport
*hvt
, *hvt_found
= NULL
;
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
) {
206 spin_unlock(&hvt_list_lock
);
208 pr_warn("hvt_cn_callback: spurious message received!\n");
213 * Switching to NETLINK mode. Switching to CHARDEV happens when someone
216 mutex_lock(&hvt
->lock
);
217 if (hvt
->mode
== HVUTIL_TRANSPORT_INIT
)
218 hvt
->mode
= HVUTIL_TRANSPORT_NETLINK
;
220 if (hvt
->mode
== HVUTIL_TRANSPORT_NETLINK
)
221 hvt_found
->on_msg(msg
->data
, msg
->len
);
223 pr_warn("hvt_cn_callback: unexpected netlink message!\n");
224 mutex_unlock(&hvt
->lock
);
227 int hvutil_transport_send(struct hvutil_transport
*hvt
, void *msg
, int len
,
228 void (*on_read_cb
)(void))
230 struct cn_msg
*cn_msg
;
233 if (hvt
->mode
== HVUTIL_TRANSPORT_INIT
||
234 hvt
->mode
== HVUTIL_TRANSPORT_DESTROY
) {
236 } else if (hvt
->mode
== HVUTIL_TRANSPORT_NETLINK
) {
237 cn_msg
= kzalloc(sizeof(*cn_msg
) + len
, GFP_ATOMIC
);
240 cn_msg
->id
.idx
= hvt
->cn_id
.idx
;
241 cn_msg
->id
.val
= hvt
->cn_id
.val
;
243 memcpy(cn_msg
->data
, msg
, len
);
244 ret
= cn_netlink_send(cn_msg
, 0, 0, GFP_ATOMIC
);
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.
255 /* HVUTIL_TRANSPORT_CHARDEV */
256 mutex_lock(&hvt
->lock
);
257 if (hvt
->mode
!= HVUTIL_TRANSPORT_CHARDEV
) {
263 /* Previous message wasn't received */
267 hvt
->outmsg
= kzalloc(len
, GFP_KERNEL
);
269 memcpy(hvt
->outmsg
, msg
, len
);
270 hvt
->outmsg_len
= len
;
271 hvt
->on_read
= on_read_cb
;
272 wake_up_interruptible(&hvt
->outmsg_q
);
276 mutex_unlock(&hvt
->lock
);
280 struct 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))
285 struct hvutil_transport
*hvt
;
287 hvt
= kzalloc(sizeof(*hvt
), GFP_KERNEL
);
291 hvt
->cn_id
.idx
= cn_idx
;
292 hvt
->cn_id
.val
= cn_val
;
294 hvt
->mdev
.minor
= MISC_DYNAMIC_MINOR
;
295 hvt
->mdev
.name
= name
;
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
;
304 hvt
->mdev
.fops
= &hvt
->fops
;
306 init_waitqueue_head(&hvt
->outmsg_q
);
307 mutex_init(&hvt
->lock
);
308 init_completion(&hvt
->release
);
310 spin_lock(&hvt_list_lock
);
311 list_add(&hvt
->list
, &hvt_list
);
312 spin_unlock(&hvt_list_lock
);
314 hvt
->on_msg
= on_msg
;
315 hvt
->on_reset
= on_reset
;
317 if (misc_register(&hvt
->mdev
))
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
))
328 spin_lock(&hvt_list_lock
);
329 list_del(&hvt
->list
);
330 spin_unlock(&hvt_list_lock
);
335 void hvutil_transport_destroy(struct hvutil_transport
*hvt
)
339 mutex_lock(&hvt
->lock
);
340 mode_old
= hvt
->mode
;
341 hvt
->mode
= HVUTIL_TRANSPORT_DESTROY
;
342 wake_up_interruptible(&hvt
->outmsg_q
);
343 mutex_unlock(&hvt
->lock
);
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
350 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
);
356 if (mode_old
== HVUTIL_TRANSPORT_CHARDEV
)
357 wait_for_completion(&hvt
->release
);
359 hvt_transport_free(hvt
);