3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
28 #include <linux/highmem.h>
29 #include <linux/vmalloc.h>
30 #include <linux/ioport.h>
31 #include <linux/irq.h>
32 #include <linux/interrupt.h>
33 #include <linux/sched.h>
34 #include <linux/wait.h>
35 #include <linux/spinlock.h>
36 #include <linux/workqueue.h>
37 #include <linux/kernel.h>
38 #include <linux/jiffies.h>
39 #include <linux/delay.h>
40 #include <linux/time.h>
42 #include <linux/bitops.h>
43 #include <linux/slab.h>
46 struct osd_callback_struct
{
47 struct work_struct work
;
48 void (*callback
)(void *);
52 void *osd_VirtualAllocExec(unsigned int size
)
55 return __vmalloc(size
, GFP_KERNEL
, PAGE_KERNEL_EXEC
);
57 return __vmalloc(size
, GFP_KERNEL
,
58 __pgprot(__PAGE_KERNEL
& (~_PAGE_NX
)));
63 * osd_PageAlloc() - Allocate pages
64 * @count: Total number of Kernel pages you want to allocate
66 * Tries to allocate @count number of consecutive free kernel pages.
67 * And if successful, it will set the pages to 0 before returning.
68 * If successfull it will return pointer to the @count pages.
69 * Mainly used by Hyper-V drivers.
71 void *osd_PageAlloc(unsigned int count
)
75 p
= (void *)__get_free_pages(GFP_KERNEL
, get_order(count
* PAGE_SIZE
));
77 memset(p
, 0, count
* PAGE_SIZE
);
80 /* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */
83 /* BUGBUG: We need to use kmap in case we are in HIMEM region */
84 /* p = page_address(page); */
85 /* if (p) memset(p, 0, PAGE_SIZE); */
88 EXPORT_SYMBOL_GPL(osd_PageAlloc
);
91 * osd_PageFree() - Free pages
92 * @page: Pointer to the first page to be freed
93 * @count: Total number of Kernel pages you free
95 * Frees the pages allocated by osd_PageAlloc()
96 * Mainly used by Hyper-V drivers.
98 void osd_PageFree(void *page
, unsigned int count
)
100 free_pages((unsigned long)page
, get_order(count
* PAGE_SIZE
));
101 /*struct page* p = virt_to_page(page);
104 EXPORT_SYMBOL_GPL(osd_PageFree
);
107 * osd_WaitEventCreate() - Create the event queue
109 * Allocates memory for a &struct osd_waitevent. And then calls
110 * init_waitqueue_head to set up the wait queue for the event.
111 * This structure is usually part of a another structure that contains
112 * the actual Hyper-V device driver structure.
114 * Returns pointer to &struct osd_waitevent
115 * Mainly used by Hyper-V drivers.
117 struct osd_waitevent
*osd_WaitEventCreate(void)
119 struct osd_waitevent
*wait
= kmalloc(sizeof(struct osd_waitevent
),
125 init_waitqueue_head(&wait
->event
);
128 EXPORT_SYMBOL_GPL(osd_WaitEventCreate
);
132 * osd_WaitEventSet() - Wake up the process
133 * @waitEvent: Structure to event to be woken up
135 * @waitevent is of type &struct osd_waitevent
137 * Wake up the sleeping process so it can do some work.
138 * And set condition indicator in &struct osd_waitevent to indicate
139 * the process is in a woken state.
141 * Only used by Network and Storage Hyper-V drivers.
143 void osd_WaitEventSet(struct osd_waitevent
*waitEvent
)
145 waitEvent
->condition
= 1;
146 wake_up_interruptible(&waitEvent
->event
);
148 EXPORT_SYMBOL_GPL(osd_WaitEventSet
);
151 * osd_WaitEventWait() - Wait for event till condition is true
152 * @waitEvent: Structure to event to be put to sleep
154 * @waitevent is of type &struct osd_waitevent
156 * Set up the process to sleep until waitEvent->condition get true.
157 * And set condition indicator in &struct osd_waitevent to indicate
158 * the process is in a sleeping state.
160 * Returns the status of 'wait_event_interruptible()' system call
162 * Mainly used by Hyper-V drivers.
164 int osd_WaitEventWait(struct osd_waitevent
*waitEvent
)
168 ret
= wait_event_interruptible(waitEvent
->event
,
169 waitEvent
->condition
);
170 waitEvent
->condition
= 0;
173 EXPORT_SYMBOL_GPL(osd_WaitEventWait
);
176 * osd_WaitEventWaitEx() - Wait for event or timeout for process wakeup
177 * @waitEvent: Structure to event to be put to sleep
178 * @TimeoutInMs: Total number of Milliseconds to wait before waking up
180 * @waitevent is of type &struct osd_waitevent
181 * Set up the process to sleep until @waitEvent->condition get true or
182 * @TimeoutInMs (Time out in Milliseconds) has been reached.
183 * And set condition indicator in &struct osd_waitevent to indicate
184 * the process is in a sleeping state.
186 * Returns the status of 'wait_event_interruptible_timeout()' system call
188 * Mainly used by Hyper-V drivers.
190 int osd_WaitEventWaitEx(struct osd_waitevent
*waitEvent
, u32 TimeoutInMs
)
194 ret
= wait_event_interruptible_timeout(waitEvent
->event
,
195 waitEvent
->condition
,
196 msecs_to_jiffies(TimeoutInMs
));
197 waitEvent
->condition
= 0;
200 EXPORT_SYMBOL_GPL(osd_WaitEventWaitEx
);
202 static void osd_callback_work(struct work_struct
*work
)
204 struct osd_callback_struct
*cb
= container_of(work
,
205 struct osd_callback_struct
,
207 (cb
->callback
)(cb
->data
);
211 int osd_schedule_callback(struct workqueue_struct
*wq
,
212 void (*func
)(void *),
215 struct osd_callback_struct
*cb
;
217 cb
= kmalloc(sizeof(*cb
), GFP_KERNEL
);
219 printk(KERN_ERR
"unable to allocate memory in osd_schedule_callback\n");
225 INIT_WORK(&cb
->work
, osd_callback_work
);
226 return queue_work(wq
, &cb
->work
);