Loading...
1/*
2 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
20 */
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23#include <linux/kernel.h>
24#include <linux/sched.h>
25#include <linux/wait.h>
26#include <linux/mm.h>
27#include <linux/slab.h>
28#include <linux/module.h>
29#include <linux/hyperv.h>
30#include <linux/uio.h>
31#include <linux/interrupt.h>
32
33#include "hyperv_vmbus.h"
34
35#define NUM_PAGES_SPANNED(addr, len) \
36((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
37
38/*
39 * vmbus_setevent- Trigger an event notification on the specified
40 * channel.
41 */
42static void vmbus_setevent(struct vmbus_channel *channel)
43{
44 struct hv_monitor_page *monitorpage;
45
46 if (channel->offermsg.monitor_allocated) {
47 /* Each u32 represents 32 channels */
48 sync_set_bit(channel->offermsg.child_relid & 31,
49 (unsigned long *) vmbus_connection.send_int_page +
50 (channel->offermsg.child_relid >> 5));
51
52 /* Get the child to parent monitor page */
53 monitorpage = vmbus_connection.monitor_pages[1];
54
55 sync_set_bit(channel->monitor_bit,
56 (unsigned long *)&monitorpage->trigger_group
57 [channel->monitor_grp].pending);
58
59 } else {
60 vmbus_set_event(channel);
61 }
62}
63
64/*
65 * vmbus_open - Open the specified channel.
66 */
67int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
68 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
69 void (*onchannelcallback)(void *context), void *context)
70{
71 struct vmbus_channel_open_channel *open_msg;
72 struct vmbus_channel_msginfo *open_info = NULL;
73 void *in, *out;
74 unsigned long flags;
75 int ret, err = 0;
76 unsigned long t;
77 struct page *page;
78
79 spin_lock_irqsave(&newchannel->lock, flags);
80 if (newchannel->state == CHANNEL_OPEN_STATE) {
81 newchannel->state = CHANNEL_OPENING_STATE;
82 } else {
83 spin_unlock_irqrestore(&newchannel->lock, flags);
84 return -EINVAL;
85 }
86 spin_unlock_irqrestore(&newchannel->lock, flags);
87
88 newchannel->onchannel_callback = onchannelcallback;
89 newchannel->channel_callback_context = context;
90
91 /* Allocate the ring buffer */
92 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
93 GFP_KERNEL|__GFP_ZERO,
94 get_order(send_ringbuffer_size +
95 recv_ringbuffer_size));
96
97 if (!page)
98 out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
99 get_order(send_ringbuffer_size +
100 recv_ringbuffer_size));
101 else
102 out = (void *)page_address(page);
103
104 if (!out) {
105 err = -ENOMEM;
106 goto error0;
107 }
108
109 in = (void *)((unsigned long)out + send_ringbuffer_size);
110
111 newchannel->ringbuffer_pages = out;
112 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
113 recv_ringbuffer_size) >> PAGE_SHIFT;
114
115 ret = hv_ringbuffer_init(
116 &newchannel->outbound, out, send_ringbuffer_size);
117
118 if (ret != 0) {
119 err = ret;
120 goto error0;
121 }
122
123 ret = hv_ringbuffer_init(
124 &newchannel->inbound, in, recv_ringbuffer_size);
125 if (ret != 0) {
126 err = ret;
127 goto error0;
128 }
129
130
131 /* Establish the gpadl for the ring buffer */
132 newchannel->ringbuffer_gpadlhandle = 0;
133
134 ret = vmbus_establish_gpadl(newchannel,
135 newchannel->outbound.ring_buffer,
136 send_ringbuffer_size +
137 recv_ringbuffer_size,
138 &newchannel->ringbuffer_gpadlhandle);
139
140 if (ret != 0) {
141 err = ret;
142 goto error0;
143 }
144
145 /* Create and init the channel open message */
146 open_info = kmalloc(sizeof(*open_info) +
147 sizeof(struct vmbus_channel_open_channel),
148 GFP_KERNEL);
149 if (!open_info) {
150 err = -ENOMEM;
151 goto error_gpadl;
152 }
153
154 init_completion(&open_info->waitevent);
155
156 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
157 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
158 open_msg->openid = newchannel->offermsg.child_relid;
159 open_msg->child_relid = newchannel->offermsg.child_relid;
160 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
161 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
162 PAGE_SHIFT;
163 open_msg->target_vp = newchannel->target_vp;
164
165 if (userdatalen > MAX_USER_DEFINED_BYTES) {
166 err = -EINVAL;
167 goto error_gpadl;
168 }
169
170 if (userdatalen)
171 memcpy(open_msg->userdata, userdata, userdatalen);
172
173 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
174 list_add_tail(&open_info->msglistentry,
175 &vmbus_connection.chn_msg_list);
176 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
177
178 ret = vmbus_post_msg(open_msg,
179 sizeof(struct vmbus_channel_open_channel));
180
181 if (ret != 0) {
182 err = ret;
183 goto error1;
184 }
185
186 t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
187 if (t == 0) {
188 err = -ETIMEDOUT;
189 goto error1;
190 }
191
192 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
193 list_del(&open_info->msglistentry);
194 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
195
196 if (open_info->response.open_result.status) {
197 err = -EAGAIN;
198 goto error_gpadl;
199 }
200
201 newchannel->state = CHANNEL_OPENED_STATE;
202 kfree(open_info);
203 return 0;
204
205error1:
206 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
207 list_del(&open_info->msglistentry);
208 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
209
210error_gpadl:
211 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
212
213error0:
214 free_pages((unsigned long)out,
215 get_order(send_ringbuffer_size + recv_ringbuffer_size));
216 kfree(open_info);
217 newchannel->state = CHANNEL_OPEN_STATE;
218 return err;
219}
220EXPORT_SYMBOL_GPL(vmbus_open);
221
222/* Used for Hyper-V Socket: a guest client's connect() to the host */
223int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
224 const uuid_le *shv_host_servie_id)
225{
226 struct vmbus_channel_tl_connect_request conn_msg;
227
228 memset(&conn_msg, 0, sizeof(conn_msg));
229 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
230 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
231 conn_msg.host_service_id = *shv_host_servie_id;
232
233 return vmbus_post_msg(&conn_msg, sizeof(conn_msg));
234}
235EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
236
237/*
238 * create_gpadl_header - Creates a gpadl for the specified buffer
239 */
240static int create_gpadl_header(void *kbuffer, u32 size,
241 struct vmbus_channel_msginfo **msginfo,
242 u32 *messagecount)
243{
244 int i;
245 int pagecount;
246 struct vmbus_channel_gpadl_header *gpadl_header;
247 struct vmbus_channel_gpadl_body *gpadl_body;
248 struct vmbus_channel_msginfo *msgheader;
249 struct vmbus_channel_msginfo *msgbody = NULL;
250 u32 msgsize;
251
252 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
253
254 pagecount = size >> PAGE_SHIFT;
255
256 /* do we need a gpadl body msg */
257 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
258 sizeof(struct vmbus_channel_gpadl_header) -
259 sizeof(struct gpa_range);
260 pfncount = pfnsize / sizeof(u64);
261
262 if (pagecount > pfncount) {
263 /* we need a gpadl body */
264 /* fill in the header */
265 msgsize = sizeof(struct vmbus_channel_msginfo) +
266 sizeof(struct vmbus_channel_gpadl_header) +
267 sizeof(struct gpa_range) + pfncount * sizeof(u64);
268 msgheader = kzalloc(msgsize, GFP_KERNEL);
269 if (!msgheader)
270 goto nomem;
271
272 INIT_LIST_HEAD(&msgheader->submsglist);
273 msgheader->msgsize = msgsize;
274
275 gpadl_header = (struct vmbus_channel_gpadl_header *)
276 msgheader->msg;
277 gpadl_header->rangecount = 1;
278 gpadl_header->range_buflen = sizeof(struct gpa_range) +
279 pagecount * sizeof(u64);
280 gpadl_header->range[0].byte_offset = 0;
281 gpadl_header->range[0].byte_count = size;
282 for (i = 0; i < pfncount; i++)
283 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
284 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
285 *msginfo = msgheader;
286 *messagecount = 1;
287
288 pfnsum = pfncount;
289 pfnleft = pagecount - pfncount;
290
291 /* how many pfns can we fit */
292 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
293 sizeof(struct vmbus_channel_gpadl_body);
294 pfncount = pfnsize / sizeof(u64);
295
296 /* fill in the body */
297 while (pfnleft) {
298 if (pfnleft > pfncount)
299 pfncurr = pfncount;
300 else
301 pfncurr = pfnleft;
302
303 msgsize = sizeof(struct vmbus_channel_msginfo) +
304 sizeof(struct vmbus_channel_gpadl_body) +
305 pfncurr * sizeof(u64);
306 msgbody = kzalloc(msgsize, GFP_KERNEL);
307
308 if (!msgbody) {
309 struct vmbus_channel_msginfo *pos = NULL;
310 struct vmbus_channel_msginfo *tmp = NULL;
311 /*
312 * Free up all the allocated messages.
313 */
314 list_for_each_entry_safe(pos, tmp,
315 &msgheader->submsglist,
316 msglistentry) {
317
318 list_del(&pos->msglistentry);
319 kfree(pos);
320 }
321
322 goto nomem;
323 }
324
325 msgbody->msgsize = msgsize;
326 (*messagecount)++;
327 gpadl_body =
328 (struct vmbus_channel_gpadl_body *)msgbody->msg;
329
330 /*
331 * Gpadl is u32 and we are using a pointer which could
332 * be 64-bit
333 * This is governed by the guest/host protocol and
334 * so the hypervisor gurantees that this is ok.
335 */
336 for (i = 0; i < pfncurr; i++)
337 gpadl_body->pfn[i] = slow_virt_to_phys(
338 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
339 PAGE_SHIFT;
340
341 /* add to msg header */
342 list_add_tail(&msgbody->msglistentry,
343 &msgheader->submsglist);
344 pfnsum += pfncurr;
345 pfnleft -= pfncurr;
346 }
347 } else {
348 /* everything fits in a header */
349 msgsize = sizeof(struct vmbus_channel_msginfo) +
350 sizeof(struct vmbus_channel_gpadl_header) +
351 sizeof(struct gpa_range) + pagecount * sizeof(u64);
352 msgheader = kzalloc(msgsize, GFP_KERNEL);
353 if (msgheader == NULL)
354 goto nomem;
355 msgheader->msgsize = msgsize;
356
357 gpadl_header = (struct vmbus_channel_gpadl_header *)
358 msgheader->msg;
359 gpadl_header->rangecount = 1;
360 gpadl_header->range_buflen = sizeof(struct gpa_range) +
361 pagecount * sizeof(u64);
362 gpadl_header->range[0].byte_offset = 0;
363 gpadl_header->range[0].byte_count = size;
364 for (i = 0; i < pagecount; i++)
365 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
366 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
367
368 *msginfo = msgheader;
369 *messagecount = 1;
370 }
371
372 return 0;
373nomem:
374 kfree(msgheader);
375 kfree(msgbody);
376 return -ENOMEM;
377}
378
379/*
380 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
381 *
382 * @channel: a channel
383 * @kbuffer: from kmalloc or vmalloc
384 * @size: page-size multiple
385 * @gpadl_handle: some funky thing
386 */
387int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
388 u32 size, u32 *gpadl_handle)
389{
390 struct vmbus_channel_gpadl_header *gpadlmsg;
391 struct vmbus_channel_gpadl_body *gpadl_body;
392 struct vmbus_channel_msginfo *msginfo = NULL;
393 struct vmbus_channel_msginfo *submsginfo;
394 u32 msgcount;
395 struct list_head *curr;
396 u32 next_gpadl_handle;
397 unsigned long flags;
398 int ret = 0;
399
400 next_gpadl_handle =
401 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
402
403 ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
404 if (ret)
405 return ret;
406
407 init_completion(&msginfo->waitevent);
408
409 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
410 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
411 gpadlmsg->child_relid = channel->offermsg.child_relid;
412 gpadlmsg->gpadl = next_gpadl_handle;
413
414
415 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
416 list_add_tail(&msginfo->msglistentry,
417 &vmbus_connection.chn_msg_list);
418
419 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
420
421 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
422 sizeof(*msginfo));
423 if (ret != 0)
424 goto cleanup;
425
426 if (msgcount > 1) {
427 list_for_each(curr, &msginfo->submsglist) {
428
429 submsginfo = (struct vmbus_channel_msginfo *)curr;
430 gpadl_body =
431 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
432
433 gpadl_body->header.msgtype =
434 CHANNELMSG_GPADL_BODY;
435 gpadl_body->gpadl = next_gpadl_handle;
436
437 ret = vmbus_post_msg(gpadl_body,
438 submsginfo->msgsize -
439 sizeof(*submsginfo));
440 if (ret != 0)
441 goto cleanup;
442
443 }
444 }
445 wait_for_completion(&msginfo->waitevent);
446
447 /* At this point, we received the gpadl created msg */
448 *gpadl_handle = gpadlmsg->gpadl;
449
450cleanup:
451 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
452 list_del(&msginfo->msglistentry);
453 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
454
455 kfree(msginfo);
456 return ret;
457}
458EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
459
460/*
461 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
462 */
463int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
464{
465 struct vmbus_channel_gpadl_teardown *msg;
466 struct vmbus_channel_msginfo *info;
467 unsigned long flags;
468 int ret;
469
470 info = kmalloc(sizeof(*info) +
471 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
472 if (!info)
473 return -ENOMEM;
474
475 init_completion(&info->waitevent);
476
477 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
478
479 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
480 msg->child_relid = channel->offermsg.child_relid;
481 msg->gpadl = gpadl_handle;
482
483 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
484 list_add_tail(&info->msglistentry,
485 &vmbus_connection.chn_msg_list);
486 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
487 ret = vmbus_post_msg(msg,
488 sizeof(struct vmbus_channel_gpadl_teardown));
489
490 if (ret)
491 goto post_msg_err;
492
493 wait_for_completion(&info->waitevent);
494
495post_msg_err:
496 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
497 list_del(&info->msglistentry);
498 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
499
500 kfree(info);
501 return ret;
502}
503EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
504
505static void reset_channel_cb(void *arg)
506{
507 struct vmbus_channel *channel = arg;
508
509 channel->onchannel_callback = NULL;
510}
511
512static int vmbus_close_internal(struct vmbus_channel *channel)
513{
514 struct vmbus_channel_close_channel *msg;
515 struct tasklet_struct *tasklet;
516 int ret;
517
518 /*
519 * process_chn_event(), running in the tasklet, can race
520 * with vmbus_close_internal() in the case of SMP guest, e.g., when
521 * the former is accessing channel->inbound.ring_buffer, the latter
522 * could be freeing the ring_buffer pages.
523 *
524 * To resolve the race, we can serialize them by disabling the
525 * tasklet when the latter is running here.
526 */
527 tasklet = hv_context.event_dpc[channel->target_cpu];
528 tasklet_disable(tasklet);
529
530 /*
531 * In case a device driver's probe() fails (e.g.,
532 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
533 * rescinded later (e.g., we dynamically disble an Integrated Service
534 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
535 * here we should skip most of the below cleanup work.
536 */
537 if (channel->state != CHANNEL_OPENED_STATE) {
538 ret = -EINVAL;
539 goto out;
540 }
541
542 channel->state = CHANNEL_OPEN_STATE;
543 channel->sc_creation_callback = NULL;
544 /* Stop callback and cancel the timer asap */
545 if (channel->target_cpu != get_cpu()) {
546 put_cpu();
547 smp_call_function_single(channel->target_cpu, reset_channel_cb,
548 channel, true);
549 } else {
550 reset_channel_cb(channel);
551 put_cpu();
552 }
553
554 /* Send a closing message */
555
556 msg = &channel->close_msg.msg;
557
558 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
559 msg->child_relid = channel->offermsg.child_relid;
560
561 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
562
563 if (ret) {
564 pr_err("Close failed: close post msg return is %d\n", ret);
565 /*
566 * If we failed to post the close msg,
567 * it is perhaps better to leak memory.
568 */
569 goto out;
570 }
571
572 /* Tear down the gpadl for the channel's ring buffer */
573 if (channel->ringbuffer_gpadlhandle) {
574 ret = vmbus_teardown_gpadl(channel,
575 channel->ringbuffer_gpadlhandle);
576 if (ret) {
577 pr_err("Close failed: teardown gpadl return %d\n", ret);
578 /*
579 * If we failed to teardown gpadl,
580 * it is perhaps better to leak memory.
581 */
582 goto out;
583 }
584 }
585
586 /* Cleanup the ring buffers for this channel */
587 hv_ringbuffer_cleanup(&channel->outbound);
588 hv_ringbuffer_cleanup(&channel->inbound);
589
590 free_pages((unsigned long)channel->ringbuffer_pages,
591 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
592
593out:
594 tasklet_enable(tasklet);
595
596 return ret;
597}
598
599/*
600 * vmbus_close - Close the specified channel
601 */
602void vmbus_close(struct vmbus_channel *channel)
603{
604 struct list_head *cur, *tmp;
605 struct vmbus_channel *cur_channel;
606
607 if (channel->primary_channel != NULL) {
608 /*
609 * We will only close sub-channels when
610 * the primary is closed.
611 */
612 return;
613 }
614 /*
615 * Close all the sub-channels first and then close the
616 * primary channel.
617 */
618 list_for_each_safe(cur, tmp, &channel->sc_list) {
619 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
620 if (cur_channel->state != CHANNEL_OPENED_STATE)
621 continue;
622 vmbus_close_internal(cur_channel);
623 }
624 /*
625 * Now close the primary.
626 */
627 vmbus_close_internal(channel);
628}
629EXPORT_SYMBOL_GPL(vmbus_close);
630
631int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
632 u32 bufferlen, u64 requestid,
633 enum vmbus_packet_type type, u32 flags, bool kick_q)
634{
635 struct vmpacket_descriptor desc;
636 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
637 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
638 struct kvec bufferlist[3];
639 u64 aligned_data = 0;
640 int ret;
641 bool signal = false;
642 bool lock = channel->acquire_ring_lock;
643 int num_vecs = ((bufferlen != 0) ? 3 : 1);
644
645
646 /* Setup the descriptor */
647 desc.type = type; /* VmbusPacketTypeDataInBand; */
648 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
649 /* in 8-bytes granularity */
650 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
651 desc.len8 = (u16)(packetlen_aligned >> 3);
652 desc.trans_id = requestid;
653
654 bufferlist[0].iov_base = &desc;
655 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
656 bufferlist[1].iov_base = buffer;
657 bufferlist[1].iov_len = bufferlen;
658 bufferlist[2].iov_base = &aligned_data;
659 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
660
661 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, num_vecs,
662 &signal, lock);
663
664 /*
665 * Signalling the host is conditional on many factors:
666 * 1. The ring state changed from being empty to non-empty.
667 * This is tracked by the variable "signal".
668 * 2. The variable kick_q tracks if more data will be placed
669 * on the ring. We will not signal if more data is
670 * to be placed.
671 *
672 * Based on the channel signal state, we will decide
673 * which signaling policy will be applied.
674 *
675 * If we cannot write to the ring-buffer; signal the host
676 * even if we may not have written anything. This is a rare
677 * enough condition that it should not matter.
678 * NOTE: in this case, the hvsock channel is an exception, because
679 * it looks the host side's hvsock implementation has a throttling
680 * mechanism which can hurt the performance otherwise.
681 */
682
683 if (channel->signal_policy)
684 signal = true;
685 else
686 kick_q = true;
687
688 if (((ret == 0) && kick_q && signal) ||
689 (ret && !is_hvsock_channel(channel)))
690 vmbus_setevent(channel);
691
692 return ret;
693}
694EXPORT_SYMBOL(vmbus_sendpacket_ctl);
695
696/**
697 * vmbus_sendpacket() - Send the specified buffer on the given channel
698 * @channel: Pointer to vmbus_channel structure.
699 * @buffer: Pointer to the buffer you want to receive the data into.
700 * @bufferlen: Maximum size of what the the buffer will hold
701 * @requestid: Identifier of the request
702 * @type: Type of packet that is being send e.g. negotiate, time
703 * packet etc.
704 *
705 * Sends data in @buffer directly to hyper-v via the vmbus
706 * This will send the data unparsed to hyper-v.
707 *
708 * Mainly used by Hyper-V drivers.
709 */
710int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
711 u32 bufferlen, u64 requestid,
712 enum vmbus_packet_type type, u32 flags)
713{
714 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
715 type, flags, true);
716}
717EXPORT_SYMBOL(vmbus_sendpacket);
718
719/*
720 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
721 * packets using a GPADL Direct packet type. This interface allows you
722 * to control notifying the host. This will be useful for sending
723 * batched data. Also the sender can control the send flags
724 * explicitly.
725 */
726int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
727 struct hv_page_buffer pagebuffers[],
728 u32 pagecount, void *buffer, u32 bufferlen,
729 u64 requestid,
730 u32 flags,
731 bool kick_q)
732{
733 int ret;
734 int i;
735 struct vmbus_channel_packet_page_buffer desc;
736 u32 descsize;
737 u32 packetlen;
738 u32 packetlen_aligned;
739 struct kvec bufferlist[3];
740 u64 aligned_data = 0;
741 bool signal = false;
742 bool lock = channel->acquire_ring_lock;
743
744 if (pagecount > MAX_PAGE_BUFFER_COUNT)
745 return -EINVAL;
746
747
748 /*
749 * Adjust the size down since vmbus_channel_packet_page_buffer is the
750 * largest size we support
751 */
752 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
753 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
754 sizeof(struct hv_page_buffer));
755 packetlen = descsize + bufferlen;
756 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
757
758 /* Setup the descriptor */
759 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
760 desc.flags = flags;
761 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
762 desc.length8 = (u16)(packetlen_aligned >> 3);
763 desc.transactionid = requestid;
764 desc.rangecount = pagecount;
765
766 for (i = 0; i < pagecount; i++) {
767 desc.range[i].len = pagebuffers[i].len;
768 desc.range[i].offset = pagebuffers[i].offset;
769 desc.range[i].pfn = pagebuffers[i].pfn;
770 }
771
772 bufferlist[0].iov_base = &desc;
773 bufferlist[0].iov_len = descsize;
774 bufferlist[1].iov_base = buffer;
775 bufferlist[1].iov_len = bufferlen;
776 bufferlist[2].iov_base = &aligned_data;
777 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
778
779 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
780 &signal, lock);
781
782 /*
783 * Signalling the host is conditional on many factors:
784 * 1. The ring state changed from being empty to non-empty.
785 * This is tracked by the variable "signal".
786 * 2. The variable kick_q tracks if more data will be placed
787 * on the ring. We will not signal if more data is
788 * to be placed.
789 *
790 * Based on the channel signal state, we will decide
791 * which signaling policy will be applied.
792 *
793 * If we cannot write to the ring-buffer; signal the host
794 * even if we may not have written anything. This is a rare
795 * enough condition that it should not matter.
796 */
797
798 if (channel->signal_policy)
799 signal = true;
800 else
801 kick_q = true;
802
803 if (((ret == 0) && kick_q && signal) || (ret))
804 vmbus_setevent(channel);
805
806 return ret;
807}
808EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
809
810/*
811 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
812 * packets using a GPADL Direct packet type.
813 */
814int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
815 struct hv_page_buffer pagebuffers[],
816 u32 pagecount, void *buffer, u32 bufferlen,
817 u64 requestid)
818{
819 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
820 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
821 buffer, bufferlen, requestid,
822 flags, true);
823
824}
825EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
826
827/*
828 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
829 * using a GPADL Direct packet type.
830 * The buffer includes the vmbus descriptor.
831 */
832int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
833 struct vmbus_packet_mpb_array *desc,
834 u32 desc_size,
835 void *buffer, u32 bufferlen, u64 requestid)
836{
837 int ret;
838 u32 packetlen;
839 u32 packetlen_aligned;
840 struct kvec bufferlist[3];
841 u64 aligned_data = 0;
842 bool signal = false;
843 bool lock = channel->acquire_ring_lock;
844
845 packetlen = desc_size + bufferlen;
846 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
847
848 /* Setup the descriptor */
849 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
850 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
851 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
852 desc->length8 = (u16)(packetlen_aligned >> 3);
853 desc->transactionid = requestid;
854 desc->rangecount = 1;
855
856 bufferlist[0].iov_base = desc;
857 bufferlist[0].iov_len = desc_size;
858 bufferlist[1].iov_base = buffer;
859 bufferlist[1].iov_len = bufferlen;
860 bufferlist[2].iov_base = &aligned_data;
861 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
862
863 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
864 &signal, lock);
865
866 if (ret == 0 && signal)
867 vmbus_setevent(channel);
868
869 return ret;
870}
871EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
872
873/*
874 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
875 * using a GPADL Direct packet type.
876 */
877int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
878 struct hv_multipage_buffer *multi_pagebuffer,
879 void *buffer, u32 bufferlen, u64 requestid)
880{
881 int ret;
882 struct vmbus_channel_packet_multipage_buffer desc;
883 u32 descsize;
884 u32 packetlen;
885 u32 packetlen_aligned;
886 struct kvec bufferlist[3];
887 u64 aligned_data = 0;
888 bool signal = false;
889 bool lock = channel->acquire_ring_lock;
890 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
891 multi_pagebuffer->len);
892
893 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
894 return -EINVAL;
895
896 /*
897 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
898 * the largest size we support
899 */
900 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
901 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
902 sizeof(u64));
903 packetlen = descsize + bufferlen;
904 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
905
906
907 /* Setup the descriptor */
908 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
909 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
910 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
911 desc.length8 = (u16)(packetlen_aligned >> 3);
912 desc.transactionid = requestid;
913 desc.rangecount = 1;
914
915 desc.range.len = multi_pagebuffer->len;
916 desc.range.offset = multi_pagebuffer->offset;
917
918 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
919 pfncount * sizeof(u64));
920
921 bufferlist[0].iov_base = &desc;
922 bufferlist[0].iov_len = descsize;
923 bufferlist[1].iov_base = buffer;
924 bufferlist[1].iov_len = bufferlen;
925 bufferlist[2].iov_base = &aligned_data;
926 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
927
928 ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3,
929 &signal, lock);
930
931 if (ret == 0 && signal)
932 vmbus_setevent(channel);
933
934 return ret;
935}
936EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
937
938/**
939 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
940 * @channel: Pointer to vmbus_channel structure.
941 * @buffer: Pointer to the buffer you want to receive the data into.
942 * @bufferlen: Maximum size of what the the buffer will hold
943 * @buffer_actual_len: The actual size of the data after it was received
944 * @requestid: Identifier of the request
945 *
946 * Receives directly from the hyper-v vmbus and puts the data it received
947 * into Buffer. This will receive the data unparsed from hyper-v.
948 *
949 * Mainly used by Hyper-V drivers.
950 */
951static inline int
952__vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
953 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
954 bool raw)
955{
956 int ret;
957 bool signal = false;
958
959 ret = hv_ringbuffer_read(&channel->inbound, buffer, bufferlen,
960 buffer_actual_len, requestid, &signal, raw);
961
962 if (signal)
963 vmbus_setevent(channel);
964
965 return ret;
966}
967
968int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
969 u32 bufferlen, u32 *buffer_actual_len,
970 u64 *requestid)
971{
972 return __vmbus_recvpacket(channel, buffer, bufferlen,
973 buffer_actual_len, requestid, false);
974}
975EXPORT_SYMBOL(vmbus_recvpacket);
976
977/*
978 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
979 */
980int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
981 u32 bufferlen, u32 *buffer_actual_len,
982 u64 *requestid)
983{
984 return __vmbus_recvpacket(channel, buffer, bufferlen,
985 buffer_actual_len, requestid, true);
986}
987EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
1/*
2 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
20 */
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23#include <linux/kernel.h>
24#include <linux/sched.h>
25#include <linux/wait.h>
26#include <linux/mm.h>
27#include <linux/slab.h>
28#include <linux/module.h>
29#include <linux/hyperv.h>
30#include <linux/uio.h>
31#include <linux/interrupt.h>
32
33#include "hyperv_vmbus.h"
34
35#define NUM_PAGES_SPANNED(addr, len) \
36((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
37
38/*
39 * vmbus_setevent- Trigger an event notification on the specified
40 * channel.
41 */
42void vmbus_setevent(struct vmbus_channel *channel)
43{
44 struct hv_monitor_page *monitorpage;
45
46 /*
47 * For channels marked as in "low latency" mode
48 * bypass the monitor page mechanism.
49 */
50 if ((channel->offermsg.monitor_allocated) &&
51 (!channel->low_latency)) {
52 /* Each u32 represents 32 channels */
53 sync_set_bit(channel->offermsg.child_relid & 31,
54 (unsigned long *) vmbus_connection.send_int_page +
55 (channel->offermsg.child_relid >> 5));
56
57 /* Get the child to parent monitor page */
58 monitorpage = vmbus_connection.monitor_pages[1];
59
60 sync_set_bit(channel->monitor_bit,
61 (unsigned long *)&monitorpage->trigger_group
62 [channel->monitor_grp].pending);
63
64 } else {
65 vmbus_set_event(channel);
66 }
67}
68EXPORT_SYMBOL_GPL(vmbus_setevent);
69
70/*
71 * vmbus_open - Open the specified channel.
72 */
73int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
74 u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
75 void (*onchannelcallback)(void *context), void *context)
76{
77 struct vmbus_channel_open_channel *open_msg;
78 struct vmbus_channel_msginfo *open_info = NULL;
79 unsigned long flags;
80 int ret, err = 0;
81 struct page *page;
82
83 if (send_ringbuffer_size % PAGE_SIZE ||
84 recv_ringbuffer_size % PAGE_SIZE)
85 return -EINVAL;
86
87 spin_lock_irqsave(&newchannel->lock, flags);
88 if (newchannel->state == CHANNEL_OPEN_STATE) {
89 newchannel->state = CHANNEL_OPENING_STATE;
90 } else {
91 spin_unlock_irqrestore(&newchannel->lock, flags);
92 return -EINVAL;
93 }
94 spin_unlock_irqrestore(&newchannel->lock, flags);
95
96 newchannel->onchannel_callback = onchannelcallback;
97 newchannel->channel_callback_context = context;
98
99 /* Allocate the ring buffer */
100 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
101 GFP_KERNEL|__GFP_ZERO,
102 get_order(send_ringbuffer_size +
103 recv_ringbuffer_size));
104
105 if (!page)
106 page = alloc_pages(GFP_KERNEL|__GFP_ZERO,
107 get_order(send_ringbuffer_size +
108 recv_ringbuffer_size));
109
110 if (!page) {
111 err = -ENOMEM;
112 goto error_set_chnstate;
113 }
114
115 newchannel->ringbuffer_pages = page_address(page);
116 newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
117 recv_ringbuffer_size) >> PAGE_SHIFT;
118
119 ret = hv_ringbuffer_init(&newchannel->outbound, page,
120 send_ringbuffer_size >> PAGE_SHIFT);
121
122 if (ret != 0) {
123 err = ret;
124 goto error_free_pages;
125 }
126
127 ret = hv_ringbuffer_init(&newchannel->inbound,
128 &page[send_ringbuffer_size >> PAGE_SHIFT],
129 recv_ringbuffer_size >> PAGE_SHIFT);
130 if (ret != 0) {
131 err = ret;
132 goto error_free_pages;
133 }
134
135
136 /* Establish the gpadl for the ring buffer */
137 newchannel->ringbuffer_gpadlhandle = 0;
138
139 ret = vmbus_establish_gpadl(newchannel,
140 page_address(page),
141 send_ringbuffer_size +
142 recv_ringbuffer_size,
143 &newchannel->ringbuffer_gpadlhandle);
144
145 if (ret != 0) {
146 err = ret;
147 goto error_free_pages;
148 }
149
150 /* Create and init the channel open message */
151 open_info = kmalloc(sizeof(*open_info) +
152 sizeof(struct vmbus_channel_open_channel),
153 GFP_KERNEL);
154 if (!open_info) {
155 err = -ENOMEM;
156 goto error_free_gpadl;
157 }
158
159 init_completion(&open_info->waitevent);
160 open_info->waiting_channel = newchannel;
161
162 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
163 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
164 open_msg->openid = newchannel->offermsg.child_relid;
165 open_msg->child_relid = newchannel->offermsg.child_relid;
166 open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
167 open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
168 PAGE_SHIFT;
169 open_msg->target_vp = newchannel->target_vp;
170
171 if (userdatalen > MAX_USER_DEFINED_BYTES) {
172 err = -EINVAL;
173 goto error_free_gpadl;
174 }
175
176 if (userdatalen)
177 memcpy(open_msg->userdata, userdata, userdatalen);
178
179 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
180 list_add_tail(&open_info->msglistentry,
181 &vmbus_connection.chn_msg_list);
182 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
183
184 ret = vmbus_post_msg(open_msg,
185 sizeof(struct vmbus_channel_open_channel), true);
186
187 if (ret != 0) {
188 err = ret;
189 goto error_clean_msglist;
190 }
191
192 wait_for_completion(&open_info->waitevent);
193
194 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
195 list_del(&open_info->msglistentry);
196 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
197
198 if (newchannel->rescind) {
199 err = -ENODEV;
200 goto error_free_gpadl;
201 }
202
203 if (open_info->response.open_result.status) {
204 err = -EAGAIN;
205 goto error_free_gpadl;
206 }
207
208 newchannel->state = CHANNEL_OPENED_STATE;
209 kfree(open_info);
210 return 0;
211
212error_clean_msglist:
213 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
214 list_del(&open_info->msglistentry);
215 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
216
217error_free_gpadl:
218 vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
219 kfree(open_info);
220error_free_pages:
221 hv_ringbuffer_cleanup(&newchannel->outbound);
222 hv_ringbuffer_cleanup(&newchannel->inbound);
223 __free_pages(page,
224 get_order(send_ringbuffer_size + recv_ringbuffer_size));
225error_set_chnstate:
226 newchannel->state = CHANNEL_OPEN_STATE;
227 return err;
228}
229EXPORT_SYMBOL_GPL(vmbus_open);
230
231/* Used for Hyper-V Socket: a guest client's connect() to the host */
232int vmbus_send_tl_connect_request(const uuid_le *shv_guest_servie_id,
233 const uuid_le *shv_host_servie_id)
234{
235 struct vmbus_channel_tl_connect_request conn_msg;
236
237 memset(&conn_msg, 0, sizeof(conn_msg));
238 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
239 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
240 conn_msg.host_service_id = *shv_host_servie_id;
241
242 return vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
243}
244EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
245
246/*
247 * create_gpadl_header - Creates a gpadl for the specified buffer
248 */
249static int create_gpadl_header(void *kbuffer, u32 size,
250 struct vmbus_channel_msginfo **msginfo)
251{
252 int i;
253 int pagecount;
254 struct vmbus_channel_gpadl_header *gpadl_header;
255 struct vmbus_channel_gpadl_body *gpadl_body;
256 struct vmbus_channel_msginfo *msgheader;
257 struct vmbus_channel_msginfo *msgbody = NULL;
258 u32 msgsize;
259
260 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
261
262 pagecount = size >> PAGE_SHIFT;
263
264 /* do we need a gpadl body msg */
265 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
266 sizeof(struct vmbus_channel_gpadl_header) -
267 sizeof(struct gpa_range);
268 pfncount = pfnsize / sizeof(u64);
269
270 if (pagecount > pfncount) {
271 /* we need a gpadl body */
272 /* fill in the header */
273 msgsize = sizeof(struct vmbus_channel_msginfo) +
274 sizeof(struct vmbus_channel_gpadl_header) +
275 sizeof(struct gpa_range) + pfncount * sizeof(u64);
276 msgheader = kzalloc(msgsize, GFP_KERNEL);
277 if (!msgheader)
278 goto nomem;
279
280 INIT_LIST_HEAD(&msgheader->submsglist);
281 msgheader->msgsize = msgsize;
282
283 gpadl_header = (struct vmbus_channel_gpadl_header *)
284 msgheader->msg;
285 gpadl_header->rangecount = 1;
286 gpadl_header->range_buflen = sizeof(struct gpa_range) +
287 pagecount * sizeof(u64);
288 gpadl_header->range[0].byte_offset = 0;
289 gpadl_header->range[0].byte_count = size;
290 for (i = 0; i < pfncount; i++)
291 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
292 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
293 *msginfo = msgheader;
294
295 pfnsum = pfncount;
296 pfnleft = pagecount - pfncount;
297
298 /* how many pfns can we fit */
299 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
300 sizeof(struct vmbus_channel_gpadl_body);
301 pfncount = pfnsize / sizeof(u64);
302
303 /* fill in the body */
304 while (pfnleft) {
305 if (pfnleft > pfncount)
306 pfncurr = pfncount;
307 else
308 pfncurr = pfnleft;
309
310 msgsize = sizeof(struct vmbus_channel_msginfo) +
311 sizeof(struct vmbus_channel_gpadl_body) +
312 pfncurr * sizeof(u64);
313 msgbody = kzalloc(msgsize, GFP_KERNEL);
314
315 if (!msgbody) {
316 struct vmbus_channel_msginfo *pos = NULL;
317 struct vmbus_channel_msginfo *tmp = NULL;
318 /*
319 * Free up all the allocated messages.
320 */
321 list_for_each_entry_safe(pos, tmp,
322 &msgheader->submsglist,
323 msglistentry) {
324
325 list_del(&pos->msglistentry);
326 kfree(pos);
327 }
328
329 goto nomem;
330 }
331
332 msgbody->msgsize = msgsize;
333 gpadl_body =
334 (struct vmbus_channel_gpadl_body *)msgbody->msg;
335
336 /*
337 * Gpadl is u32 and we are using a pointer which could
338 * be 64-bit
339 * This is governed by the guest/host protocol and
340 * so the hypervisor gurantees that this is ok.
341 */
342 for (i = 0; i < pfncurr; i++)
343 gpadl_body->pfn[i] = slow_virt_to_phys(
344 kbuffer + PAGE_SIZE * (pfnsum + i)) >>
345 PAGE_SHIFT;
346
347 /* add to msg header */
348 list_add_tail(&msgbody->msglistentry,
349 &msgheader->submsglist);
350 pfnsum += pfncurr;
351 pfnleft -= pfncurr;
352 }
353 } else {
354 /* everything fits in a header */
355 msgsize = sizeof(struct vmbus_channel_msginfo) +
356 sizeof(struct vmbus_channel_gpadl_header) +
357 sizeof(struct gpa_range) + pagecount * sizeof(u64);
358 msgheader = kzalloc(msgsize, GFP_KERNEL);
359 if (msgheader == NULL)
360 goto nomem;
361
362 INIT_LIST_HEAD(&msgheader->submsglist);
363 msgheader->msgsize = msgsize;
364
365 gpadl_header = (struct vmbus_channel_gpadl_header *)
366 msgheader->msg;
367 gpadl_header->rangecount = 1;
368 gpadl_header->range_buflen = sizeof(struct gpa_range) +
369 pagecount * sizeof(u64);
370 gpadl_header->range[0].byte_offset = 0;
371 gpadl_header->range[0].byte_count = size;
372 for (i = 0; i < pagecount; i++)
373 gpadl_header->range[0].pfn_array[i] = slow_virt_to_phys(
374 kbuffer + PAGE_SIZE * i) >> PAGE_SHIFT;
375
376 *msginfo = msgheader;
377 }
378
379 return 0;
380nomem:
381 kfree(msgheader);
382 kfree(msgbody);
383 return -ENOMEM;
384}
385
386/*
387 * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
388 *
389 * @channel: a channel
390 * @kbuffer: from kmalloc or vmalloc
391 * @size: page-size multiple
392 * @gpadl_handle: some funky thing
393 */
394int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
395 u32 size, u32 *gpadl_handle)
396{
397 struct vmbus_channel_gpadl_header *gpadlmsg;
398 struct vmbus_channel_gpadl_body *gpadl_body;
399 struct vmbus_channel_msginfo *msginfo = NULL;
400 struct vmbus_channel_msginfo *submsginfo, *tmp;
401 struct list_head *curr;
402 u32 next_gpadl_handle;
403 unsigned long flags;
404 int ret = 0;
405
406 next_gpadl_handle =
407 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
408
409 ret = create_gpadl_header(kbuffer, size, &msginfo);
410 if (ret)
411 return ret;
412
413 init_completion(&msginfo->waitevent);
414 msginfo->waiting_channel = channel;
415
416 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
417 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
418 gpadlmsg->child_relid = channel->offermsg.child_relid;
419 gpadlmsg->gpadl = next_gpadl_handle;
420
421
422 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
423 list_add_tail(&msginfo->msglistentry,
424 &vmbus_connection.chn_msg_list);
425
426 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
427
428 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
429 sizeof(*msginfo), true);
430 if (ret != 0)
431 goto cleanup;
432
433 list_for_each(curr, &msginfo->submsglist) {
434 submsginfo = (struct vmbus_channel_msginfo *)curr;
435 gpadl_body =
436 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
437
438 gpadl_body->header.msgtype =
439 CHANNELMSG_GPADL_BODY;
440 gpadl_body->gpadl = next_gpadl_handle;
441
442 ret = vmbus_post_msg(gpadl_body,
443 submsginfo->msgsize - sizeof(*submsginfo),
444 true);
445 if (ret != 0)
446 goto cleanup;
447
448 }
449 wait_for_completion(&msginfo->waitevent);
450
451 if (channel->rescind) {
452 ret = -ENODEV;
453 goto cleanup;
454 }
455
456 /* At this point, we received the gpadl created msg */
457 *gpadl_handle = gpadlmsg->gpadl;
458
459cleanup:
460 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
461 list_del(&msginfo->msglistentry);
462 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
463 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
464 msglistentry) {
465 kfree(submsginfo);
466 }
467
468 kfree(msginfo);
469 return ret;
470}
471EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
472
473/*
474 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
475 */
476int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
477{
478 struct vmbus_channel_gpadl_teardown *msg;
479 struct vmbus_channel_msginfo *info;
480 unsigned long flags;
481 int ret;
482
483 info = kmalloc(sizeof(*info) +
484 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
485 if (!info)
486 return -ENOMEM;
487
488 init_completion(&info->waitevent);
489 info->waiting_channel = channel;
490
491 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
492
493 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
494 msg->child_relid = channel->offermsg.child_relid;
495 msg->gpadl = gpadl_handle;
496
497 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
498 list_add_tail(&info->msglistentry,
499 &vmbus_connection.chn_msg_list);
500 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
501 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
502 true);
503
504 if (ret)
505 goto post_msg_err;
506
507 wait_for_completion(&info->waitevent);
508
509post_msg_err:
510 /*
511 * If the channel has been rescinded;
512 * we will be awakened by the rescind
513 * handler; set the error code to zero so we don't leak memory.
514 */
515 if (channel->rescind)
516 ret = 0;
517
518 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
519 list_del(&info->msglistentry);
520 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
521
522 kfree(info);
523 return ret;
524}
525EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
526
527static void reset_channel_cb(void *arg)
528{
529 struct vmbus_channel *channel = arg;
530
531 channel->onchannel_callback = NULL;
532}
533
534static int vmbus_close_internal(struct vmbus_channel *channel)
535{
536 struct vmbus_channel_close_channel *msg;
537 int ret;
538
539 /*
540 * process_chn_event(), running in the tasklet, can race
541 * with vmbus_close_internal() in the case of SMP guest, e.g., when
542 * the former is accessing channel->inbound.ring_buffer, the latter
543 * could be freeing the ring_buffer pages.
544 *
545 * To resolve the race, we can serialize them by disabling the
546 * tasklet when the latter is running here.
547 */
548 hv_event_tasklet_disable(channel);
549
550 /*
551 * In case a device driver's probe() fails (e.g.,
552 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
553 * rescinded later (e.g., we dynamically disble an Integrated Service
554 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
555 * here we should skip most of the below cleanup work.
556 */
557 if (channel->state != CHANNEL_OPENED_STATE) {
558 ret = -EINVAL;
559 goto out;
560 }
561
562 channel->state = CHANNEL_OPEN_STATE;
563 channel->sc_creation_callback = NULL;
564 /* Stop callback and cancel the timer asap */
565 if (channel->target_cpu != get_cpu()) {
566 put_cpu();
567 smp_call_function_single(channel->target_cpu, reset_channel_cb,
568 channel, true);
569 } else {
570 reset_channel_cb(channel);
571 put_cpu();
572 }
573
574 /* Send a closing message */
575
576 msg = &channel->close_msg.msg;
577
578 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
579 msg->child_relid = channel->offermsg.child_relid;
580
581 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
582 true);
583
584 if (ret) {
585 pr_err("Close failed: close post msg return is %d\n", ret);
586 /*
587 * If we failed to post the close msg,
588 * it is perhaps better to leak memory.
589 */
590 goto out;
591 }
592
593 /* Tear down the gpadl for the channel's ring buffer */
594 if (channel->ringbuffer_gpadlhandle) {
595 ret = vmbus_teardown_gpadl(channel,
596 channel->ringbuffer_gpadlhandle);
597 if (ret) {
598 pr_err("Close failed: teardown gpadl return %d\n", ret);
599 /*
600 * If we failed to teardown gpadl,
601 * it is perhaps better to leak memory.
602 */
603 goto out;
604 }
605 }
606
607 /* Cleanup the ring buffers for this channel */
608 hv_ringbuffer_cleanup(&channel->outbound);
609 hv_ringbuffer_cleanup(&channel->inbound);
610
611 free_pages((unsigned long)channel->ringbuffer_pages,
612 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
613
614out:
615 hv_event_tasklet_enable(channel);
616
617 return ret;
618}
619
620/*
621 * vmbus_close - Close the specified channel
622 */
623void vmbus_close(struct vmbus_channel *channel)
624{
625 struct list_head *cur, *tmp;
626 struct vmbus_channel *cur_channel;
627
628 if (channel->primary_channel != NULL) {
629 /*
630 * We will only close sub-channels when
631 * the primary is closed.
632 */
633 return;
634 }
635 /*
636 * Close all the sub-channels first and then close the
637 * primary channel.
638 */
639 list_for_each_safe(cur, tmp, &channel->sc_list) {
640 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
641 if (cur_channel->state != CHANNEL_OPENED_STATE)
642 continue;
643 vmbus_close_internal(cur_channel);
644 }
645 /*
646 * Now close the primary.
647 */
648 vmbus_close_internal(channel);
649}
650EXPORT_SYMBOL_GPL(vmbus_close);
651
652int vmbus_sendpacket_ctl(struct vmbus_channel *channel, void *buffer,
653 u32 bufferlen, u64 requestid,
654 enum vmbus_packet_type type, u32 flags, bool kick_q)
655{
656 struct vmpacket_descriptor desc;
657 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
658 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
659 struct kvec bufferlist[3];
660 u64 aligned_data = 0;
661 bool lock = channel->acquire_ring_lock;
662 int num_vecs = ((bufferlen != 0) ? 3 : 1);
663
664
665 /* Setup the descriptor */
666 desc.type = type; /* VmbusPacketTypeDataInBand; */
667 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
668 /* in 8-bytes granularity */
669 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
670 desc.len8 = (u16)(packetlen_aligned >> 3);
671 desc.trans_id = requestid;
672
673 bufferlist[0].iov_base = &desc;
674 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
675 bufferlist[1].iov_base = buffer;
676 bufferlist[1].iov_len = bufferlen;
677 bufferlist[2].iov_base = &aligned_data;
678 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
679
680 return hv_ringbuffer_write(channel, bufferlist, num_vecs,
681 lock, kick_q);
682
683}
684EXPORT_SYMBOL(vmbus_sendpacket_ctl);
685
686/**
687 * vmbus_sendpacket() - Send the specified buffer on the given channel
688 * @channel: Pointer to vmbus_channel structure.
689 * @buffer: Pointer to the buffer you want to receive the data into.
690 * @bufferlen: Maximum size of what the the buffer will hold
691 * @requestid: Identifier of the request
692 * @type: Type of packet that is being send e.g. negotiate, time
693 * packet etc.
694 *
695 * Sends data in @buffer directly to hyper-v via the vmbus
696 * This will send the data unparsed to hyper-v.
697 *
698 * Mainly used by Hyper-V drivers.
699 */
700int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
701 u32 bufferlen, u64 requestid,
702 enum vmbus_packet_type type, u32 flags)
703{
704 return vmbus_sendpacket_ctl(channel, buffer, bufferlen, requestid,
705 type, flags, true);
706}
707EXPORT_SYMBOL(vmbus_sendpacket);
708
709/*
710 * vmbus_sendpacket_pagebuffer_ctl - Send a range of single-page buffer
711 * packets using a GPADL Direct packet type. This interface allows you
712 * to control notifying the host. This will be useful for sending
713 * batched data. Also the sender can control the send flags
714 * explicitly.
715 */
716int vmbus_sendpacket_pagebuffer_ctl(struct vmbus_channel *channel,
717 struct hv_page_buffer pagebuffers[],
718 u32 pagecount, void *buffer, u32 bufferlen,
719 u64 requestid,
720 u32 flags,
721 bool kick_q)
722{
723 int i;
724 struct vmbus_channel_packet_page_buffer desc;
725 u32 descsize;
726 u32 packetlen;
727 u32 packetlen_aligned;
728 struct kvec bufferlist[3];
729 u64 aligned_data = 0;
730 bool lock = channel->acquire_ring_lock;
731
732 if (pagecount > MAX_PAGE_BUFFER_COUNT)
733 return -EINVAL;
734
735
736 /*
737 * Adjust the size down since vmbus_channel_packet_page_buffer is the
738 * largest size we support
739 */
740 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
741 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
742 sizeof(struct hv_page_buffer));
743 packetlen = descsize + bufferlen;
744 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
745
746 /* Setup the descriptor */
747 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
748 desc.flags = flags;
749 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
750 desc.length8 = (u16)(packetlen_aligned >> 3);
751 desc.transactionid = requestid;
752 desc.rangecount = pagecount;
753
754 for (i = 0; i < pagecount; i++) {
755 desc.range[i].len = pagebuffers[i].len;
756 desc.range[i].offset = pagebuffers[i].offset;
757 desc.range[i].pfn = pagebuffers[i].pfn;
758 }
759
760 bufferlist[0].iov_base = &desc;
761 bufferlist[0].iov_len = descsize;
762 bufferlist[1].iov_base = buffer;
763 bufferlist[1].iov_len = bufferlen;
764 bufferlist[2].iov_base = &aligned_data;
765 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
766
767 return hv_ringbuffer_write(channel, bufferlist, 3,
768 lock, kick_q);
769}
770EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer_ctl);
771
772/*
773 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
774 * packets using a GPADL Direct packet type.
775 */
776int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
777 struct hv_page_buffer pagebuffers[],
778 u32 pagecount, void *buffer, u32 bufferlen,
779 u64 requestid)
780{
781 u32 flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
782 return vmbus_sendpacket_pagebuffer_ctl(channel, pagebuffers, pagecount,
783 buffer, bufferlen, requestid,
784 flags, true);
785
786}
787EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
788
789/*
790 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
791 * using a GPADL Direct packet type.
792 * The buffer includes the vmbus descriptor.
793 */
794int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
795 struct vmbus_packet_mpb_array *desc,
796 u32 desc_size,
797 void *buffer, u32 bufferlen, u64 requestid)
798{
799 u32 packetlen;
800 u32 packetlen_aligned;
801 struct kvec bufferlist[3];
802 u64 aligned_data = 0;
803 bool lock = channel->acquire_ring_lock;
804
805 packetlen = desc_size + bufferlen;
806 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
807
808 /* Setup the descriptor */
809 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
810 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
811 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes grandularity */
812 desc->length8 = (u16)(packetlen_aligned >> 3);
813 desc->transactionid = requestid;
814 desc->rangecount = 1;
815
816 bufferlist[0].iov_base = desc;
817 bufferlist[0].iov_len = desc_size;
818 bufferlist[1].iov_base = buffer;
819 bufferlist[1].iov_len = bufferlen;
820 bufferlist[2].iov_base = &aligned_data;
821 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
822
823 return hv_ringbuffer_write(channel, bufferlist, 3,
824 lock, true);
825}
826EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
827
828/*
829 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
830 * using a GPADL Direct packet type.
831 */
832int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
833 struct hv_multipage_buffer *multi_pagebuffer,
834 void *buffer, u32 bufferlen, u64 requestid)
835{
836 struct vmbus_channel_packet_multipage_buffer desc;
837 u32 descsize;
838 u32 packetlen;
839 u32 packetlen_aligned;
840 struct kvec bufferlist[3];
841 u64 aligned_data = 0;
842 bool lock = channel->acquire_ring_lock;
843 u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
844 multi_pagebuffer->len);
845
846 if (pfncount > MAX_MULTIPAGE_BUFFER_COUNT)
847 return -EINVAL;
848
849 /*
850 * Adjust the size down since vmbus_channel_packet_multipage_buffer is
851 * the largest size we support
852 */
853 descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
854 ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
855 sizeof(u64));
856 packetlen = descsize + bufferlen;
857 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
858
859
860 /* Setup the descriptor */
861 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
862 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
863 desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
864 desc.length8 = (u16)(packetlen_aligned >> 3);
865 desc.transactionid = requestid;
866 desc.rangecount = 1;
867
868 desc.range.len = multi_pagebuffer->len;
869 desc.range.offset = multi_pagebuffer->offset;
870
871 memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
872 pfncount * sizeof(u64));
873
874 bufferlist[0].iov_base = &desc;
875 bufferlist[0].iov_len = descsize;
876 bufferlist[1].iov_base = buffer;
877 bufferlist[1].iov_len = bufferlen;
878 bufferlist[2].iov_base = &aligned_data;
879 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
880
881 return hv_ringbuffer_write(channel, bufferlist, 3,
882 lock, true);
883}
884EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
885
886/**
887 * vmbus_recvpacket() - Retrieve the user packet on the specified channel
888 * @channel: Pointer to vmbus_channel structure.
889 * @buffer: Pointer to the buffer you want to receive the data into.
890 * @bufferlen: Maximum size of what the the buffer will hold
891 * @buffer_actual_len: The actual size of the data after it was received
892 * @requestid: Identifier of the request
893 *
894 * Receives directly from the hyper-v vmbus and puts the data it received
895 * into Buffer. This will receive the data unparsed from hyper-v.
896 *
897 * Mainly used by Hyper-V drivers.
898 */
899static inline int
900__vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
901 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
902 bool raw)
903{
904 return hv_ringbuffer_read(channel, buffer, bufferlen,
905 buffer_actual_len, requestid, raw);
906
907}
908
909int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
910 u32 bufferlen, u32 *buffer_actual_len,
911 u64 *requestid)
912{
913 return __vmbus_recvpacket(channel, buffer, bufferlen,
914 buffer_actual_len, requestid, false);
915}
916EXPORT_SYMBOL(vmbus_recvpacket);
917
918/*
919 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
920 */
921int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
922 u32 bufferlen, u32 *buffer_actual_len,
923 u64 *requestid)
924{
925 return __vmbus_recvpacket(channel, buffer, bufferlen,
926 buffer_actual_len, requestid, true);
927}
928EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);