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
)
30 mutex_lock(&hvt
->outmsg_lock
);
34 mutex_unlock(&hvt
->outmsg_lock
);
39 static ssize_t
hvt_op_read(struct file
*file
, char __user
*buf
,
40 size_t count
, loff_t
*ppos
)
42 struct hvutil_transport
*hvt
;
45 hvt
= container_of(file
->f_op
, struct hvutil_transport
, fops
);
47 if (wait_event_interruptible(hvt
->outmsg_q
, hvt
->outmsg_len
> 0))
50 mutex_lock(&hvt
->outmsg_lock
);
56 if (count
< hvt
->outmsg_len
) {
61 if (!copy_to_user(buf
, hvt
->outmsg
, hvt
->outmsg_len
))
62 ret
= hvt
->outmsg_len
;
71 mutex_unlock(&hvt
->outmsg_lock
);
75 static ssize_t
hvt_op_write(struct file
*file
, const char __user
*buf
,
76 size_t count
, loff_t
*ppos
)
78 struct hvutil_transport
*hvt
;
81 hvt
= container_of(file
->f_op
, struct hvutil_transport
, fops
);
83 inmsg
= kzalloc(count
, GFP_KERNEL
);
84 if (copy_from_user(inmsg
, buf
, count
)) {
88 if (hvt
->on_msg(inmsg
, count
))
95 static unsigned int hvt_op_poll(struct file
*file
, poll_table
*wait
)
97 struct hvutil_transport
*hvt
;
99 hvt
= container_of(file
->f_op
, struct hvutil_transport
, fops
);
101 poll_wait(file
, &hvt
->outmsg_q
, wait
);
102 if (hvt
->outmsg_len
> 0)
103 return POLLIN
| POLLRDNORM
;
108 static int hvt_op_open(struct inode
*inode
, struct file
*file
)
110 struct hvutil_transport
*hvt
;
112 hvt
= container_of(file
->f_op
, struct hvutil_transport
, fops
);
115 * Switching to CHARDEV mode. We switch bach to INIT when device
118 if (hvt
->mode
== HVUTIL_TRANSPORT_INIT
)
119 hvt
->mode
= HVUTIL_TRANSPORT_CHARDEV
;
120 else if (hvt
->mode
== HVUTIL_TRANSPORT_NETLINK
) {
122 * We're switching from netlink communication to using char
123 * device. Issue the reset first.
126 hvt
->mode
= HVUTIL_TRANSPORT_CHARDEV
;
133 static int hvt_op_release(struct inode
*inode
, struct file
*file
)
135 struct hvutil_transport
*hvt
;
137 hvt
= container_of(file
->f_op
, struct hvutil_transport
, fops
);
139 hvt
->mode
= HVUTIL_TRANSPORT_INIT
;
141 * Cleanup message buffers to avoid spurious messages when the daemon
149 static void hvt_cn_callback(struct cn_msg
*msg
, struct netlink_skb_parms
*nsp
)
151 struct hvutil_transport
*hvt
, *hvt_found
= NULL
;
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
) {
161 spin_unlock(&hvt_list_lock
);
163 pr_warn("hvt_cn_callback: spurious message received!\n");
168 * Switching to NETLINK mode. Switching to CHARDEV happens when someone
171 if (hvt
->mode
== HVUTIL_TRANSPORT_INIT
)
172 hvt
->mode
= HVUTIL_TRANSPORT_NETLINK
;
174 if (hvt
->mode
== HVUTIL_TRANSPORT_NETLINK
)
175 hvt_found
->on_msg(msg
->data
, msg
->len
);
177 pr_warn("hvt_cn_callback: unexpected netlink message!\n");
180 int hvutil_transport_send(struct hvutil_transport
*hvt
, void *msg
, int len
)
182 struct cn_msg
*cn_msg
;
185 if (hvt
->mode
== HVUTIL_TRANSPORT_INIT
) {
187 } else if (hvt
->mode
== HVUTIL_TRANSPORT_NETLINK
) {
188 cn_msg
= kzalloc(sizeof(*cn_msg
) + len
, GFP_ATOMIC
);
191 cn_msg
->id
.idx
= hvt
->cn_id
.idx
;
192 cn_msg
->id
.val
= hvt
->cn_id
.val
;
194 memcpy(cn_msg
->data
, msg
, len
);
195 ret
= cn_netlink_send(cn_msg
, 0, 0, GFP_ATOMIC
);
199 /* HVUTIL_TRANSPORT_CHARDEV */
200 mutex_lock(&hvt
->outmsg_lock
);
202 /* Previous message wasn't received */
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
);
211 mutex_unlock(&hvt
->outmsg_lock
);
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))
220 struct hvutil_transport
*hvt
;
222 hvt
= kzalloc(sizeof(*hvt
), GFP_KERNEL
);
226 hvt
->cn_id
.idx
= cn_idx
;
227 hvt
->cn_id
.val
= cn_val
;
229 hvt
->mdev
.minor
= MISC_DYNAMIC_MINOR
;
230 hvt
->mdev
.name
= name
;
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
;
239 hvt
->mdev
.fops
= &hvt
->fops
;
241 init_waitqueue_head(&hvt
->outmsg_q
);
242 mutex_init(&hvt
->outmsg_lock
);
244 spin_lock(&hvt_list_lock
);
245 list_add(&hvt
->list
, &hvt_list
);
246 spin_unlock(&hvt_list_lock
);
248 hvt
->on_msg
= on_msg
;
249 hvt
->on_reset
= on_reset
;
251 if (misc_register(&hvt
->mdev
))
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
))
266 void hvutil_transport_destroy(struct hvutil_transport
*hvt
)
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
);