0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/slab.h>
0009 #include <linux/fs.h>
0010 #include <linux/poll.h>
0011
0012 #include "hyperv_vmbus.h"
0013 #include "hv_utils_transport.h"
0014
0015 static DEFINE_SPINLOCK(hvt_list_lock);
0016 static LIST_HEAD(hvt_list);
0017
0018 static void hvt_reset(struct hvutil_transport *hvt)
0019 {
0020 kfree(hvt->outmsg);
0021 hvt->outmsg = NULL;
0022 hvt->outmsg_len = 0;
0023 if (hvt->on_reset)
0024 hvt->on_reset();
0025 }
0026
0027 static ssize_t hvt_op_read(struct file *file, char __user *buf,
0028 size_t count, loff_t *ppos)
0029 {
0030 struct hvutil_transport *hvt;
0031 int ret;
0032
0033 hvt = container_of(file->f_op, struct hvutil_transport, fops);
0034
0035 if (wait_event_interruptible(hvt->outmsg_q, hvt->outmsg_len > 0 ||
0036 hvt->mode != HVUTIL_TRANSPORT_CHARDEV))
0037 return -EINTR;
0038
0039 mutex_lock(&hvt->lock);
0040
0041 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
0042 ret = -EBADF;
0043 goto out_unlock;
0044 }
0045
0046 if (!hvt->outmsg) {
0047 ret = -EAGAIN;
0048 goto out_unlock;
0049 }
0050
0051 if (count < hvt->outmsg_len) {
0052 ret = -EINVAL;
0053 goto out_unlock;
0054 }
0055
0056 if (!copy_to_user(buf, hvt->outmsg, hvt->outmsg_len))
0057 ret = hvt->outmsg_len;
0058 else
0059 ret = -EFAULT;
0060
0061 kfree(hvt->outmsg);
0062 hvt->outmsg = NULL;
0063 hvt->outmsg_len = 0;
0064
0065 if (hvt->on_read)
0066 hvt->on_read();
0067 hvt->on_read = NULL;
0068
0069 out_unlock:
0070 mutex_unlock(&hvt->lock);
0071 return ret;
0072 }
0073
0074 static ssize_t hvt_op_write(struct file *file, const char __user *buf,
0075 size_t count, loff_t *ppos)
0076 {
0077 struct hvutil_transport *hvt;
0078 u8 *inmsg;
0079 int ret;
0080
0081 hvt = container_of(file->f_op, struct hvutil_transport, fops);
0082
0083 inmsg = memdup_user(buf, count);
0084 if (IS_ERR(inmsg))
0085 return PTR_ERR(inmsg);
0086
0087 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
0088 ret = -EBADF;
0089 else
0090 ret = hvt->on_msg(inmsg, count);
0091
0092 kfree(inmsg);
0093
0094 return ret ? ret : count;
0095 }
0096
0097 static __poll_t hvt_op_poll(struct file *file, poll_table *wait)
0098 {
0099 struct hvutil_transport *hvt;
0100
0101 hvt = container_of(file->f_op, struct hvutil_transport, fops);
0102
0103 poll_wait(file, &hvt->outmsg_q, wait);
0104
0105 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
0106 return EPOLLERR | EPOLLHUP;
0107
0108 if (hvt->outmsg_len > 0)
0109 return EPOLLIN | EPOLLRDNORM;
0110
0111 return 0;
0112 }
0113
0114 static int hvt_op_open(struct inode *inode, struct file *file)
0115 {
0116 struct hvutil_transport *hvt;
0117 int ret = 0;
0118 bool issue_reset = false;
0119
0120 hvt = container_of(file->f_op, struct hvutil_transport, fops);
0121
0122 mutex_lock(&hvt->lock);
0123
0124 if (hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
0125 ret = -EBADF;
0126 } else if (hvt->mode == HVUTIL_TRANSPORT_INIT) {
0127
0128
0129
0130
0131 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
0132 }
0133 else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
0134
0135
0136
0137
0138 issue_reset = true;
0139 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
0140 } else {
0141 ret = -EBUSY;
0142 }
0143
0144 if (issue_reset)
0145 hvt_reset(hvt);
0146
0147 mutex_unlock(&hvt->lock);
0148
0149 return ret;
0150 }
0151
0152 static void hvt_transport_free(struct hvutil_transport *hvt)
0153 {
0154 misc_deregister(&hvt->mdev);
0155 kfree(hvt->outmsg);
0156 kfree(hvt);
0157 }
0158
0159 static int hvt_op_release(struct inode *inode, struct file *file)
0160 {
0161 struct hvutil_transport *hvt;
0162 int mode_old;
0163
0164 hvt = container_of(file->f_op, struct hvutil_transport, fops);
0165
0166 mutex_lock(&hvt->lock);
0167 mode_old = hvt->mode;
0168 if (hvt->mode != HVUTIL_TRANSPORT_DESTROY)
0169 hvt->mode = HVUTIL_TRANSPORT_INIT;
0170
0171
0172
0173
0174 hvt_reset(hvt);
0175
0176 if (mode_old == HVUTIL_TRANSPORT_DESTROY)
0177 complete(&hvt->release);
0178
0179 mutex_unlock(&hvt->lock);
0180
0181 return 0;
0182 }
0183
0184 static void hvt_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
0185 {
0186 struct hvutil_transport *hvt, *hvt_found = NULL;
0187
0188 spin_lock(&hvt_list_lock);
0189 list_for_each_entry(hvt, &hvt_list, list) {
0190 if (hvt->cn_id.idx == msg->id.idx &&
0191 hvt->cn_id.val == msg->id.val) {
0192 hvt_found = hvt;
0193 break;
0194 }
0195 }
0196 spin_unlock(&hvt_list_lock);
0197 if (!hvt_found) {
0198 pr_warn("hvt_cn_callback: spurious message received!\n");
0199 return;
0200 }
0201
0202
0203
0204
0205
0206 mutex_lock(&hvt->lock);
0207 if (hvt->mode == HVUTIL_TRANSPORT_INIT)
0208 hvt->mode = HVUTIL_TRANSPORT_NETLINK;
0209
0210 if (hvt->mode == HVUTIL_TRANSPORT_NETLINK)
0211 hvt_found->on_msg(msg->data, msg->len);
0212 else
0213 pr_warn("hvt_cn_callback: unexpected netlink message!\n");
0214 mutex_unlock(&hvt->lock);
0215 }
0216
0217 int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len,
0218 void (*on_read_cb)(void))
0219 {
0220 struct cn_msg *cn_msg;
0221 int ret = 0;
0222
0223 if (hvt->mode == HVUTIL_TRANSPORT_INIT ||
0224 hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
0225 return -EINVAL;
0226 } else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
0227 cn_msg = kzalloc(sizeof(*cn_msg) + len, GFP_ATOMIC);
0228 if (!cn_msg)
0229 return -ENOMEM;
0230 cn_msg->id.idx = hvt->cn_id.idx;
0231 cn_msg->id.val = hvt->cn_id.val;
0232 cn_msg->len = len;
0233 memcpy(cn_msg->data, msg, len);
0234 ret = cn_netlink_send(cn_msg, 0, 0, GFP_ATOMIC);
0235 kfree(cn_msg);
0236
0237
0238
0239
0240
0241 if (on_read_cb)
0242 on_read_cb();
0243 return ret;
0244 }
0245
0246 mutex_lock(&hvt->lock);
0247 if (hvt->mode != HVUTIL_TRANSPORT_CHARDEV) {
0248 ret = -EINVAL;
0249 goto out_unlock;
0250 }
0251
0252 if (hvt->outmsg) {
0253
0254 ret = -EFAULT;
0255 goto out_unlock;
0256 }
0257 hvt->outmsg = kzalloc(len, GFP_KERNEL);
0258 if (hvt->outmsg) {
0259 memcpy(hvt->outmsg, msg, len);
0260 hvt->outmsg_len = len;
0261 hvt->on_read = on_read_cb;
0262 wake_up_interruptible(&hvt->outmsg_q);
0263 } else
0264 ret = -ENOMEM;
0265 out_unlock:
0266 mutex_unlock(&hvt->lock);
0267 return ret;
0268 }
0269
0270 struct hvutil_transport *hvutil_transport_init(const char *name,
0271 u32 cn_idx, u32 cn_val,
0272 int (*on_msg)(void *, int),
0273 void (*on_reset)(void))
0274 {
0275 struct hvutil_transport *hvt;
0276
0277 hvt = kzalloc(sizeof(*hvt), GFP_KERNEL);
0278 if (!hvt)
0279 return NULL;
0280
0281 hvt->cn_id.idx = cn_idx;
0282 hvt->cn_id.val = cn_val;
0283
0284 hvt->mdev.minor = MISC_DYNAMIC_MINOR;
0285 hvt->mdev.name = name;
0286
0287 hvt->fops.owner = THIS_MODULE;
0288 hvt->fops.read = hvt_op_read;
0289 hvt->fops.write = hvt_op_write;
0290 hvt->fops.poll = hvt_op_poll;
0291 hvt->fops.open = hvt_op_open;
0292 hvt->fops.release = hvt_op_release;
0293
0294 hvt->mdev.fops = &hvt->fops;
0295
0296 init_waitqueue_head(&hvt->outmsg_q);
0297 mutex_init(&hvt->lock);
0298 init_completion(&hvt->release);
0299
0300 spin_lock(&hvt_list_lock);
0301 list_add(&hvt->list, &hvt_list);
0302 spin_unlock(&hvt_list_lock);
0303
0304 hvt->on_msg = on_msg;
0305 hvt->on_reset = on_reset;
0306
0307 if (misc_register(&hvt->mdev))
0308 goto err_free_hvt;
0309
0310
0311 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0 &&
0312 cn_add_callback(&hvt->cn_id, name, hvt_cn_callback))
0313 goto err_free_hvt;
0314
0315 return hvt;
0316
0317 err_free_hvt:
0318 spin_lock(&hvt_list_lock);
0319 list_del(&hvt->list);
0320 spin_unlock(&hvt_list_lock);
0321 kfree(hvt);
0322 return NULL;
0323 }
0324
0325 void hvutil_transport_destroy(struct hvutil_transport *hvt)
0326 {
0327 int mode_old;
0328
0329 mutex_lock(&hvt->lock);
0330 mode_old = hvt->mode;
0331 hvt->mode = HVUTIL_TRANSPORT_DESTROY;
0332 wake_up_interruptible(&hvt->outmsg_q);
0333 mutex_unlock(&hvt->lock);
0334
0335
0336
0337
0338
0339
0340 spin_lock(&hvt_list_lock);
0341 list_del(&hvt->list);
0342 spin_unlock(&hvt_list_lock);
0343 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0)
0344 cn_del_callback(&hvt->cn_id);
0345
0346 if (mode_old == HVUTIL_TRANSPORT_CHARDEV)
0347 wait_for_completion(&hvt->release);
0348
0349 hvt_transport_free(hvt);
0350 }