blob: f8fc99600de8d99872eb8a41d16939b0f266d07f [file] [log] [blame]
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001/*
2 * Copyright (c) 2012, Microsoft Corporation.
3 *
4 * Author:
5 * K. Y. Srinivasan <kys@microsoft.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
15 * details.
16 *
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/kernel.h>
22#include <linux/mman.h>
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/kthread.h>
28#include <linux/completion.h>
29#include <linux/memory_hotplug.h>
30#include <linux/memory.h>
31#include <linux/notifier.h>
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -080032#include <linux/percpu_counter.h>
33
34#include <linux/hyperv.h>
35
36/*
37 * We begin with definitions supporting the Dynamic Memory protocol
38 * with the host.
39 *
40 * Begin protocol definitions.
41 */
42
43
44
45/*
46 * Protocol versions. The low word is the minor version, the high word the major
47 * version.
48 *
49 * History:
50 * Initial version 1.0
51 * Changed to 0.1 on 2009/03/25
52 * Changes to 0.2 on 2009/05/14
53 * Changes to 0.3 on 2009/12/03
54 * Changed to 1.0 on 2011/04/05
55 */
56
57#define DYNMEM_MAKE_VERSION(Major, Minor) ((__u32)(((Major) << 16) | (Minor)))
58#define DYNMEM_MAJOR_VERSION(Version) ((__u32)(Version) >> 16)
59#define DYNMEM_MINOR_VERSION(Version) ((__u32)(Version) & 0xff)
60
61enum {
62 DYNMEM_PROTOCOL_VERSION_1 = DYNMEM_MAKE_VERSION(0, 3),
63 DYNMEM_PROTOCOL_VERSION_2 = DYNMEM_MAKE_VERSION(1, 0),
64
65 DYNMEM_PROTOCOL_VERSION_WIN7 = DYNMEM_PROTOCOL_VERSION_1,
66 DYNMEM_PROTOCOL_VERSION_WIN8 = DYNMEM_PROTOCOL_VERSION_2,
67
68 DYNMEM_PROTOCOL_VERSION_CURRENT = DYNMEM_PROTOCOL_VERSION_WIN8
69};
70
71
72
73/*
74 * Message Types
75 */
76
77enum dm_message_type {
78 /*
79 * Version 0.3
80 */
81 DM_ERROR = 0,
82 DM_VERSION_REQUEST = 1,
83 DM_VERSION_RESPONSE = 2,
84 DM_CAPABILITIES_REPORT = 3,
85 DM_CAPABILITIES_RESPONSE = 4,
86 DM_STATUS_REPORT = 5,
87 DM_BALLOON_REQUEST = 6,
88 DM_BALLOON_RESPONSE = 7,
89 DM_UNBALLOON_REQUEST = 8,
90 DM_UNBALLOON_RESPONSE = 9,
91 DM_MEM_HOT_ADD_REQUEST = 10,
92 DM_MEM_HOT_ADD_RESPONSE = 11,
93 DM_VERSION_03_MAX = 11,
94 /*
95 * Version 1.0.
96 */
97 DM_INFO_MESSAGE = 12,
98 DM_VERSION_1_MAX = 12
99};
100
101
102/*
103 * Structures defining the dynamic memory management
104 * protocol.
105 */
106
107union dm_version {
108 struct {
109 __u16 minor_version;
110 __u16 major_version;
111 };
112 __u32 version;
113} __packed;
114
115
116union dm_caps {
117 struct {
118 __u64 balloon:1;
119 __u64 hot_add:1;
120 __u64 reservedz:62;
121 } cap_bits;
122 __u64 caps;
123} __packed;
124
125union dm_mem_page_range {
126 struct {
127 /*
128 * The PFN number of the first page in the range.
129 * 40 bits is the architectural limit of a PFN
130 * number for AMD64.
131 */
132 __u64 start_page:40;
133 /*
134 * The number of pages in the range.
135 */
136 __u64 page_cnt:24;
137 } finfo;
138 __u64 page_range;
139} __packed;
140
141
142
143/*
144 * The header for all dynamic memory messages:
145 *
146 * type: Type of the message.
147 * size: Size of the message in bytes; including the header.
148 * trans_id: The guest is responsible for manufacturing this ID.
149 */
150
151struct dm_header {
152 __u16 type;
153 __u16 size;
154 __u32 trans_id;
155} __packed;
156
157/*
158 * A generic message format for dynamic memory.
159 * Specific message formats are defined later in the file.
160 */
161
162struct dm_message {
163 struct dm_header hdr;
164 __u8 data[]; /* enclosed message */
165} __packed;
166
167
168/*
169 * Specific message types supporting the dynamic memory protocol.
170 */
171
172/*
173 * Version negotiation message. Sent from the guest to the host.
174 * The guest is free to try different versions until the host
175 * accepts the version.
176 *
177 * dm_version: The protocol version requested.
178 * is_last_attempt: If TRUE, this is the last version guest will request.
179 * reservedz: Reserved field, set to zero.
180 */
181
182struct dm_version_request {
183 struct dm_header hdr;
184 union dm_version version;
185 __u32 is_last_attempt:1;
186 __u32 reservedz:31;
187} __packed;
188
189/*
190 * Version response message; Host to Guest and indicates
191 * if the host has accepted the version sent by the guest.
192 *
193 * is_accepted: If TRUE, host has accepted the version and the guest
194 * should proceed to the next stage of the protocol. FALSE indicates that
195 * guest should re-try with a different version.
196 *
197 * reservedz: Reserved field, set to zero.
198 */
199
200struct dm_version_response {
201 struct dm_header hdr;
202 __u64 is_accepted:1;
203 __u64 reservedz:63;
204} __packed;
205
206/*
207 * Message reporting capabilities. This is sent from the guest to the
208 * host.
209 */
210
211struct dm_capabilities {
212 struct dm_header hdr;
213 union dm_caps caps;
214 __u64 min_page_cnt;
215 __u64 max_page_number;
216} __packed;
217
218/*
219 * Response to the capabilities message. This is sent from the host to the
220 * guest. This message notifies if the host has accepted the guest's
221 * capabilities. If the host has not accepted, the guest must shutdown
222 * the service.
223 *
224 * is_accepted: Indicates if the host has accepted guest's capabilities.
225 * reservedz: Must be 0.
226 */
227
228struct dm_capabilities_resp_msg {
229 struct dm_header hdr;
230 __u64 is_accepted:1;
231 __u64 reservedz:63;
232} __packed;
233
234/*
235 * This message is used to report memory pressure from the guest.
236 * This message is not part of any transaction and there is no
237 * response to this message.
238 *
239 * num_avail: Available memory in pages.
240 * num_committed: Committed memory in pages.
241 * page_file_size: The accumulated size of all page files
242 * in the system in pages.
243 * zero_free: The nunber of zero and free pages.
244 * page_file_writes: The writes to the page file in pages.
245 * io_diff: An indicator of file cache efficiency or page file activity,
246 * calculated as File Cache Page Fault Count - Page Read Count.
247 * This value is in pages.
248 *
249 * Some of these metrics are Windows specific and fortunately
250 * the algorithm on the host side that computes the guest memory
251 * pressure only uses num_committed value.
252 */
253
254struct dm_status {
255 struct dm_header hdr;
256 __u64 num_avail;
257 __u64 num_committed;
258 __u64 page_file_size;
259 __u64 zero_free;
260 __u32 page_file_writes;
261 __u32 io_diff;
262} __packed;
263
264
265/*
266 * Message to ask the guest to allocate memory - balloon up message.
267 * This message is sent from the host to the guest. The guest may not be
268 * able to allocate as much memory as requested.
269 *
270 * num_pages: number of pages to allocate.
271 */
272
273struct dm_balloon {
274 struct dm_header hdr;
275 __u32 num_pages;
276 __u32 reservedz;
277} __packed;
278
279
280/*
281 * Balloon response message; this message is sent from the guest
282 * to the host in response to the balloon message.
283 *
284 * reservedz: Reserved; must be set to zero.
285 * more_pages: If FALSE, this is the last message of the transaction.
286 * if TRUE there will atleast one more message from the guest.
287 *
288 * range_count: The number of ranges in the range array.
289 *
290 * range_array: An array of page ranges returned to the host.
291 *
292 */
293
294struct dm_balloon_response {
295 struct dm_header hdr;
296 __u32 reservedz;
297 __u32 more_pages:1;
298 __u32 range_count:31;
299 union dm_mem_page_range range_array[];
300} __packed;
301
302/*
303 * Un-balloon message; this message is sent from the host
304 * to the guest to give guest more memory.
305 *
306 * more_pages: If FALSE, this is the last message of the transaction.
307 * if TRUE there will atleast one more message from the guest.
308 *
309 * reservedz: Reserved; must be set to zero.
310 *
311 * range_count: The number of ranges in the range array.
312 *
313 * range_array: An array of page ranges returned to the host.
314 *
315 */
316
317struct dm_unballoon_request {
318 struct dm_header hdr;
319 __u32 more_pages:1;
320 __u32 reservedz:31;
321 __u32 range_count;
322 union dm_mem_page_range range_array[];
323} __packed;
324
325/*
326 * Un-balloon response message; this message is sent from the guest
327 * to the host in response to an unballoon request.
328 *
329 */
330
331struct dm_unballoon_response {
332 struct dm_header hdr;
333} __packed;
334
335
336/*
337 * Hot add request message. Message sent from the host to the guest.
338 *
339 * mem_range: Memory range to hot add.
340 *
341 * On Linux we currently don't support this since we cannot hot add
342 * arbitrary granularity of memory.
343 */
344
345struct dm_hot_add {
346 struct dm_header hdr;
347 union dm_mem_page_range range;
348} __packed;
349
350/*
351 * Hot add response message.
352 * This message is sent by the guest to report the status of a hot add request.
353 * If page_count is less than the requested page count, then the host should
354 * assume all further hot add requests will fail, since this indicates that
355 * the guest has hit an upper physical memory barrier.
356 *
357 * Hot adds may also fail due to low resources; in this case, the guest must
358 * not complete this message until the hot add can succeed, and the host must
359 * not send a new hot add request until the response is sent.
360 * If VSC fails to hot add memory DYNMEM_NUMBER_OF_UNSUCCESSFUL_HOTADD_ATTEMPTS
361 * times it fails the request.
362 *
363 *
364 * page_count: number of pages that were successfully hot added.
365 *
366 * result: result of the operation 1: success, 0: failure.
367 *
368 */
369
370struct dm_hot_add_response {
371 struct dm_header hdr;
372 __u32 page_count;
373 __u32 result;
374} __packed;
375
376/*
377 * Types of information sent from host to the guest.
378 */
379
380enum dm_info_type {
381 INFO_TYPE_MAX_PAGE_CNT = 0,
382 MAX_INFO_TYPE
383};
384
385
386/*
387 * Header for the information message.
388 */
389
390struct dm_info_header {
391 enum dm_info_type type;
392 __u32 data_size;
393} __packed;
394
395/*
396 * This message is sent from the host to the guest to pass
397 * some relevant information (win8 addition).
398 *
399 * reserved: no used.
400 * info_size: size of the information blob.
401 * info: information blob.
402 */
403
404struct dm_info_msg {
K. Y. Srinivasan6427a0d2012-12-06 11:06:54 -0800405 struct dm_header hdr;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800406 __u32 reserved;
407 __u32 info_size;
408 __u8 info[];
409};
410
411/*
412 * End protocol definitions.
413 */
414
415static bool hot_add;
416static bool do_hot_add;
K. Y. Srinivasane500d152013-02-08 15:57:15 -0800417/*
418 * Delay reporting memory pressure by
419 * the specified number of seconds.
420 */
421static uint pressure_report_delay = 30;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800422
423module_param(hot_add, bool, (S_IRUGO | S_IWUSR));
424MODULE_PARM_DESC(hot_add, "If set attempt memory hot_add");
425
K. Y. Srinivasane500d152013-02-08 15:57:15 -0800426module_param(pressure_report_delay, uint, (S_IRUGO | S_IWUSR));
427MODULE_PARM_DESC(pressure_report_delay, "Delay in secs in reporting pressure");
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800428static atomic_t trans_id = ATOMIC_INIT(0);
429
430static int dm_ring_size = (5 * PAGE_SIZE);
431
432/*
433 * Driver specific state.
434 */
435
436enum hv_dm_state {
437 DM_INITIALIZING = 0,
438 DM_INITIALIZED,
439 DM_BALLOON_UP,
440 DM_BALLOON_DOWN,
441 DM_HOT_ADD,
442 DM_INIT_ERROR
443};
444
445
446static __u8 recv_buffer[PAGE_SIZE];
447static __u8 *send_buffer;
448#define PAGES_IN_2M 512
449
450struct hv_dynmem_device {
451 struct hv_device *dev;
452 enum hv_dm_state state;
453 struct completion host_event;
454 struct completion config_event;
455
456 /*
457 * Number of pages we have currently ballooned out.
458 */
459 unsigned int num_pages_ballooned;
460
461 /*
462 * This thread handles both balloon/hot-add
463 * requests from the host as well as notifying
464 * the host with regards to memory pressure in
465 * the guest.
466 */
467 struct task_struct *thread;
468
469 /*
470 * We start with the highest version we can support
471 * and downgrade based on the host; we save here the
472 * next version to try.
473 */
474 __u32 next_version;
475};
476
477static struct hv_dynmem_device dm_device;
478
479static void hot_add_req(struct hv_dynmem_device *dm, struct dm_hot_add *msg)
480{
481
482 struct dm_hot_add_response resp;
483
484 if (do_hot_add) {
485
486 pr_info("Memory hot add not supported\n");
487
488 /*
489 * Currently we do not support hot add.
490 * Just fail the request.
491 */
492 }
493
494 memset(&resp, 0, sizeof(struct dm_hot_add_response));
495 resp.hdr.type = DM_MEM_HOT_ADD_RESPONSE;
496 resp.hdr.size = sizeof(struct dm_hot_add_response);
497 resp.hdr.trans_id = atomic_inc_return(&trans_id);
498
499 resp.page_count = 0;
500 resp.result = 0;
501
502 dm->state = DM_INITIALIZED;
503 vmbus_sendpacket(dm->dev->channel, &resp,
504 sizeof(struct dm_hot_add_response),
505 (unsigned long)NULL,
506 VM_PKT_DATA_INBAND, 0);
507
508}
509
510static void process_info(struct hv_dynmem_device *dm, struct dm_info_msg *msg)
511{
K. Y. Srinivasan6427a0d2012-12-06 11:06:54 -0800512 struct dm_info_header *info_hdr;
513
514 info_hdr = (struct dm_info_header *)msg->info;
515
516 switch (info_hdr->type) {
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800517 case INFO_TYPE_MAX_PAGE_CNT:
518 pr_info("Received INFO_TYPE_MAX_PAGE_CNT\n");
K. Y. Srinivasan6427a0d2012-12-06 11:06:54 -0800519 pr_info("Data Size is %d\n", info_hdr->data_size);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800520 break;
521 default:
K. Y. Srinivasan6427a0d2012-12-06 11:06:54 -0800522 pr_info("Received Unknown type: %d\n", info_hdr->type);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800523 }
524}
525
526/*
527 * Post our status as it relates memory pressure to the
528 * host. Host expects the guests to post this status
529 * periodically at 1 second intervals.
530 *
531 * The metrics specified in this protocol are very Windows
532 * specific and so we cook up numbers here to convey our memory
533 * pressure.
534 */
535
536static void post_status(struct hv_dynmem_device *dm)
537{
538 struct dm_status status;
K. Y. Srinivasan07315722013-01-25 16:18:47 -0800539 struct sysinfo val;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800540
K. Y. Srinivasane500d152013-02-08 15:57:15 -0800541 if (pressure_report_delay > 0) {
542 --pressure_report_delay;
543 return;
544 }
K. Y. Srinivasan07315722013-01-25 16:18:47 -0800545 si_meminfo(&val);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800546 memset(&status, 0, sizeof(struct dm_status));
547 status.hdr.type = DM_STATUS_REPORT;
548 status.hdr.size = sizeof(struct dm_status);
549 status.hdr.trans_id = atomic_inc_return(&trans_id);
550
K. Y. Srinivasan07315722013-01-25 16:18:47 -0800551 /*
552 * The host expects the guest to report free memory.
553 * Further, the host expects the pressure information to
554 * include the ballooned out pages.
555 */
556 status.num_avail = val.freeram;
557 status.num_committed = vm_memory_committed() + dm->num_pages_ballooned;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800558
559 vmbus_sendpacket(dm->dev->channel, &status,
560 sizeof(struct dm_status),
561 (unsigned long)NULL,
562 VM_PKT_DATA_INBAND, 0);
563
564}
565
Greg Kroah-Hartman989623c2012-11-21 12:46:40 -0800566static void free_balloon_pages(struct hv_dynmem_device *dm,
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800567 union dm_mem_page_range *range_array)
568{
569 int num_pages = range_array->finfo.page_cnt;
570 __u64 start_frame = range_array->finfo.start_page;
571 struct page *pg;
572 int i;
573
574 for (i = 0; i < num_pages; i++) {
575 pg = pfn_to_page(i + start_frame);
576 __free_page(pg);
577 dm->num_pages_ballooned--;
578 }
579}
580
581
582
583static int alloc_balloon_pages(struct hv_dynmem_device *dm, int num_pages,
584 struct dm_balloon_response *bl_resp, int alloc_unit,
585 bool *alloc_error)
586{
587 int i = 0;
588 struct page *pg;
589
590 if (num_pages < alloc_unit)
591 return 0;
592
593 for (i = 0; (i * alloc_unit) < num_pages; i++) {
594 if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) >
595 PAGE_SIZE)
596 return i * alloc_unit;
597
598 /*
599 * We execute this code in a thread context. Furthermore,
600 * we don't want the kernel to try too hard.
601 */
602 pg = alloc_pages(GFP_HIGHUSER | __GFP_NORETRY |
603 __GFP_NOMEMALLOC | __GFP_NOWARN,
604 get_order(alloc_unit << PAGE_SHIFT));
605
606 if (!pg) {
607 *alloc_error = true;
608 return i * alloc_unit;
609 }
610
611
612 dm->num_pages_ballooned += alloc_unit;
613
614 bl_resp->range_count++;
615 bl_resp->range_array[i].finfo.start_page =
616 page_to_pfn(pg);
617 bl_resp->range_array[i].finfo.page_cnt = alloc_unit;
618 bl_resp->hdr.size += sizeof(union dm_mem_page_range);
619
620 }
621
622 return num_pages;
623}
624
625
626
627static void balloon_up(struct hv_dynmem_device *dm, struct dm_balloon *req)
628{
629 int num_pages = req->num_pages;
630 int num_ballooned = 0;
631 struct dm_balloon_response *bl_resp;
632 int alloc_unit;
633 int ret;
634 bool alloc_error = false;
635 bool done = false;
636 int i;
637
638
639 /*
640 * Currently, we only support 4k allocations.
641 */
642 alloc_unit = 1;
643
644 while (!done) {
645 bl_resp = (struct dm_balloon_response *)send_buffer;
646 memset(send_buffer, 0, PAGE_SIZE);
647 bl_resp->hdr.type = DM_BALLOON_RESPONSE;
648 bl_resp->hdr.trans_id = atomic_inc_return(&trans_id);
649 bl_resp->hdr.size = sizeof(struct dm_balloon_response);
650 bl_resp->more_pages = 1;
651
652
653 num_pages -= num_ballooned;
654 num_ballooned = alloc_balloon_pages(dm, num_pages,
655 bl_resp, alloc_unit,
656 &alloc_error);
657
658 if ((alloc_error) || (num_ballooned == num_pages)) {
659 bl_resp->more_pages = 0;
660 done = true;
661 dm->state = DM_INITIALIZED;
662 }
663
664 /*
665 * We are pushing a lot of data through the channel;
666 * deal with transient failures caused because of the
667 * lack of space in the ring buffer.
668 */
669
670 do {
671 ret = vmbus_sendpacket(dm_device.dev->channel,
672 bl_resp,
673 bl_resp->hdr.size,
674 (unsigned long)NULL,
675 VM_PKT_DATA_INBAND, 0);
676
677 if (ret == -EAGAIN)
678 msleep(20);
679
680 } while (ret == -EAGAIN);
681
682 if (ret) {
683 /*
684 * Free up the memory we allocatted.
685 */
686 pr_info("Balloon response failed\n");
687
688 for (i = 0; i < bl_resp->range_count; i++)
689 free_balloon_pages(dm,
690 &bl_resp->range_array[i]);
691
692 done = true;
693 }
694 }
695
696}
697
698static void balloon_down(struct hv_dynmem_device *dm,
699 struct dm_unballoon_request *req)
700{
701 union dm_mem_page_range *range_array = req->range_array;
702 int range_count = req->range_count;
703 struct dm_unballoon_response resp;
704 int i;
705
706 for (i = 0; i < range_count; i++)
707 free_balloon_pages(dm, &range_array[i]);
708
709 if (req->more_pages == 1)
710 return;
711
712 memset(&resp, 0, sizeof(struct dm_unballoon_response));
713 resp.hdr.type = DM_UNBALLOON_RESPONSE;
714 resp.hdr.trans_id = atomic_inc_return(&trans_id);
715 resp.hdr.size = sizeof(struct dm_unballoon_response);
716
717 vmbus_sendpacket(dm_device.dev->channel, &resp,
718 sizeof(struct dm_unballoon_response),
719 (unsigned long)NULL,
720 VM_PKT_DATA_INBAND, 0);
721
722 dm->state = DM_INITIALIZED;
723}
724
725static void balloon_onchannelcallback(void *context);
726
727static int dm_thread_func(void *dm_dev)
728{
729 struct hv_dynmem_device *dm = dm_dev;
730 int t;
731 unsigned long scan_start;
732
733 while (!kthread_should_stop()) {
734 t = wait_for_completion_timeout(&dm_device.config_event, 1*HZ);
735 /*
736 * The host expects us to post information on the memory
737 * pressure every second.
738 */
739
740 if (t == 0)
741 post_status(dm);
742
743 scan_start = jiffies;
744 switch (dm->state) {
745 case DM_BALLOON_UP:
746 balloon_up(dm, (struct dm_balloon *)recv_buffer);
747 break;
748
749 case DM_HOT_ADD:
750 hot_add_req(dm, (struct dm_hot_add *)recv_buffer);
751 break;
752 default:
753 break;
754 }
755
756 if (!time_in_range(jiffies, scan_start, scan_start + HZ))
757 post_status(dm);
758
759 }
760
761 return 0;
762}
763
764
765static void version_resp(struct hv_dynmem_device *dm,
766 struct dm_version_response *vresp)
767{
768 struct dm_version_request version_req;
769 int ret;
770
771 if (vresp->is_accepted) {
772 /*
773 * We are done; wakeup the
774 * context waiting for version
775 * negotiation.
776 */
777 complete(&dm->host_event);
778 return;
779 }
780 /*
781 * If there are more versions to try, continue
782 * with negotiations; if not
783 * shutdown the service since we are not able
784 * to negotiate a suitable version number
785 * with the host.
786 */
787 if (dm->next_version == 0)
788 goto version_error;
789
790 dm->next_version = 0;
791 memset(&version_req, 0, sizeof(struct dm_version_request));
792 version_req.hdr.type = DM_VERSION_REQUEST;
793 version_req.hdr.size = sizeof(struct dm_version_request);
794 version_req.hdr.trans_id = atomic_inc_return(&trans_id);
795 version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN7;
796 version_req.is_last_attempt = 1;
797
798 ret = vmbus_sendpacket(dm->dev->channel, &version_req,
799 sizeof(struct dm_version_request),
800 (unsigned long)NULL,
801 VM_PKT_DATA_INBAND, 0);
802
803 if (ret)
804 goto version_error;
805
806 return;
807
808version_error:
809 dm->state = DM_INIT_ERROR;
810 complete(&dm->host_event);
811}
812
813static void cap_resp(struct hv_dynmem_device *dm,
814 struct dm_capabilities_resp_msg *cap_resp)
815{
816 if (!cap_resp->is_accepted) {
817 pr_info("Capabilities not accepted by host\n");
818 dm->state = DM_INIT_ERROR;
819 }
820 complete(&dm->host_event);
821}
822
823static void balloon_onchannelcallback(void *context)
824{
825 struct hv_device *dev = context;
826 u32 recvlen;
827 u64 requestid;
828 struct dm_message *dm_msg;
829 struct dm_header *dm_hdr;
830 struct hv_dynmem_device *dm = hv_get_drvdata(dev);
831
832 memset(recv_buffer, 0, sizeof(recv_buffer));
833 vmbus_recvpacket(dev->channel, recv_buffer,
834 PAGE_SIZE, &recvlen, &requestid);
835
836 if (recvlen > 0) {
837 dm_msg = (struct dm_message *)recv_buffer;
838 dm_hdr = &dm_msg->hdr;
839
840 switch (dm_hdr->type) {
841 case DM_VERSION_RESPONSE:
842 version_resp(dm,
843 (struct dm_version_response *)dm_msg);
844 break;
845
846 case DM_CAPABILITIES_RESPONSE:
847 cap_resp(dm,
848 (struct dm_capabilities_resp_msg *)dm_msg);
849 break;
850
851 case DM_BALLOON_REQUEST:
852 dm->state = DM_BALLOON_UP;
853 complete(&dm->config_event);
854 break;
855
856 case DM_UNBALLOON_REQUEST:
857 dm->state = DM_BALLOON_DOWN;
858 balloon_down(dm,
859 (struct dm_unballoon_request *)recv_buffer);
860 break;
861
862 case DM_MEM_HOT_ADD_REQUEST:
863 dm->state = DM_HOT_ADD;
864 complete(&dm->config_event);
865 break;
866
867 case DM_INFO_MESSAGE:
868 process_info(dm, (struct dm_info_msg *)dm_msg);
869 break;
870
871 default:
872 pr_err("Unhandled message: type: %d\n", dm_hdr->type);
873
874 }
875 }
876
877}
878
879static int balloon_probe(struct hv_device *dev,
880 const struct hv_vmbus_device_id *dev_id)
881{
882 int ret, t;
883 struct dm_version_request version_req;
884 struct dm_capabilities cap_msg;
885
886 do_hot_add = hot_add;
887
888 /*
889 * First allocate a send buffer.
890 */
891
892 send_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
893 if (!send_buffer)
894 return -ENOMEM;
895
896 ret = vmbus_open(dev->channel, dm_ring_size, dm_ring_size, NULL, 0,
897 balloon_onchannelcallback, dev);
898
899 if (ret)
K. Y. Srinivasan33080c12012-12-11 11:07:17 -0800900 goto probe_error0;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800901
902 dm_device.dev = dev;
903 dm_device.state = DM_INITIALIZING;
904 dm_device.next_version = DYNMEM_PROTOCOL_VERSION_WIN7;
905 init_completion(&dm_device.host_event);
906 init_completion(&dm_device.config_event);
907
908 dm_device.thread =
909 kthread_run(dm_thread_func, &dm_device, "hv_balloon");
910 if (IS_ERR(dm_device.thread)) {
911 ret = PTR_ERR(dm_device.thread);
K. Y. Srinivasan33080c12012-12-11 11:07:17 -0800912 goto probe_error1;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800913 }
914
915 hv_set_drvdata(dev, &dm_device);
916 /*
917 * Initiate the hand shake with the host and negotiate
918 * a version that the host can support. We start with the
919 * highest version number and go down if the host cannot
920 * support it.
921 */
922 memset(&version_req, 0, sizeof(struct dm_version_request));
923 version_req.hdr.type = DM_VERSION_REQUEST;
924 version_req.hdr.size = sizeof(struct dm_version_request);
925 version_req.hdr.trans_id = atomic_inc_return(&trans_id);
926 version_req.version.version = DYNMEM_PROTOCOL_VERSION_WIN8;
927 version_req.is_last_attempt = 0;
928
929 ret = vmbus_sendpacket(dev->channel, &version_req,
930 sizeof(struct dm_version_request),
931 (unsigned long)NULL,
932 VM_PKT_DATA_INBAND,
933 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
934 if (ret)
K. Y. Srinivasan33080c12012-12-11 11:07:17 -0800935 goto probe_error2;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800936
937 t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
938 if (t == 0) {
939 ret = -ETIMEDOUT;
K. Y. Srinivasan33080c12012-12-11 11:07:17 -0800940 goto probe_error2;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800941 }
942
943 /*
944 * If we could not negotiate a compatible version with the host
945 * fail the probe function.
946 */
947 if (dm_device.state == DM_INIT_ERROR) {
948 ret = -ETIMEDOUT;
K. Y. Srinivasan33080c12012-12-11 11:07:17 -0800949 goto probe_error2;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800950 }
951 /*
952 * Now submit our capabilities to the host.
953 */
954 memset(&cap_msg, 0, sizeof(struct dm_capabilities));
955 cap_msg.hdr.type = DM_CAPABILITIES_REPORT;
956 cap_msg.hdr.size = sizeof(struct dm_capabilities);
957 cap_msg.hdr.trans_id = atomic_inc_return(&trans_id);
958
959 cap_msg.caps.cap_bits.balloon = 1;
960 /*
961 * While we currently don't support hot-add,
962 * we still advertise this capability since the
963 * host requires that guests partcipating in the
964 * dynamic memory protocol support hot add.
965 */
966 cap_msg.caps.cap_bits.hot_add = 1;
967
968 /*
969 * Currently the host does not use these
970 * values and we set them to what is done in the
971 * Windows driver.
972 */
973 cap_msg.min_page_cnt = 0;
974 cap_msg.max_page_number = -1;
975
976 ret = vmbus_sendpacket(dev->channel, &cap_msg,
977 sizeof(struct dm_capabilities),
978 (unsigned long)NULL,
979 VM_PKT_DATA_INBAND,
980 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
981 if (ret)
K. Y. Srinivasan33080c12012-12-11 11:07:17 -0800982 goto probe_error2;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800983
984 t = wait_for_completion_timeout(&dm_device.host_event, 5*HZ);
985 if (t == 0) {
986 ret = -ETIMEDOUT;
K. Y. Srinivasan33080c12012-12-11 11:07:17 -0800987 goto probe_error2;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800988 }
989
990 /*
991 * If the host does not like our capabilities,
992 * fail the probe function.
993 */
994 if (dm_device.state == DM_INIT_ERROR) {
995 ret = -ETIMEDOUT;
K. Y. Srinivasan33080c12012-12-11 11:07:17 -0800996 goto probe_error2;
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -0800997 }
998
999 dm_device.state = DM_INITIALIZED;
1000
1001 return 0;
1002
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001003probe_error2:
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001004 kthread_stop(dm_device.thread);
1005
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001006probe_error1:
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001007 vmbus_close(dev->channel);
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001008probe_error0:
1009 kfree(send_buffer);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001010 return ret;
1011}
1012
1013static int balloon_remove(struct hv_device *dev)
1014{
1015 struct hv_dynmem_device *dm = hv_get_drvdata(dev);
1016
1017 if (dm->num_pages_ballooned != 0)
1018 pr_warn("Ballooned pages: %d\n", dm->num_pages_ballooned);
1019
1020 vmbus_close(dev->channel);
1021 kthread_stop(dm->thread);
K. Y. Srinivasan33080c12012-12-11 11:07:17 -08001022 kfree(send_buffer);
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001023
1024 return 0;
1025}
1026
1027static const struct hv_vmbus_device_id id_table[] = {
1028 /* Dynamic Memory Class ID */
1029 /* 525074DC-8985-46e2-8057-A307DC18A502 */
K. Y. Srinivasand13984e2013-01-23 17:42:41 -08001030 { HV_DM_GUID, },
K. Y. Srinivasan9aa8b502012-11-14 01:09:02 -08001031 { },
1032};
1033
1034MODULE_DEVICE_TABLE(vmbus, id_table);
1035
1036static struct hv_driver balloon_drv = {
1037 .name = "hv_balloon",
1038 .id_table = id_table,
1039 .probe = balloon_probe,
1040 .remove = balloon_remove,
1041};
1042
1043static int __init init_balloon_drv(void)
1044{
1045
1046 return vmbus_driver_register(&balloon_drv);
1047}
1048
1049static void exit_balloon_drv(void)
1050{
1051
1052 vmbus_driver_unregister(&balloon_drv);
1053}
1054
1055module_init(init_balloon_drv);
1056module_exit(exit_balloon_drv);
1057
1058MODULE_DESCRIPTION("Hyper-V Balloon");
1059MODULE_VERSION(HV_DRV_VERSION);
1060MODULE_LICENSE("GPL");