Commit | Line | Data |
---|---|---|
3e7ee490 HJ |
1 | /* |
2 | * | |
3 | * Copyright (c) 2009, Microsoft Corporation. | |
4 | * | |
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. | |
8 | * | |
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 | |
12 | * more details. | |
13 | * | |
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. | |
17 | * | |
18 | * Authors: | |
19 | * Haiyang Zhang <haiyangz@microsoft.com> | |
20 | * Hank Janssen <hjanssen@microsoft.com> | |
21 | * | |
22 | */ | |
23 | ||
3e7ee490 HJ |
24 | #include <linux/module.h> |
25 | #include <linux/init.h> | |
26 | #include <linux/types.h> | |
27 | #include <linux/mm.h> | |
28 | #include <linux/highmem.h> | |
29 | #include <linux/vmalloc.h> | |
3e7ee490 HJ |
30 | #include <linux/ioport.h> |
31 | #include <linux/irq.h> | |
32 | #include <linux/interrupt.h> | |
4439c935 | 33 | #include <linux/sched.h> |
3e7ee490 HJ |
34 | #include <linux/wait.h> |
35 | #include <linux/spinlock.h> | |
36 | #include <linux/workqueue.h> | |
37 | #include <linux/kernel.h> | |
3e7ee490 HJ |
38 | #include <linux/jiffies.h> |
39 | #include <linux/delay.h> | |
40 | #include <linux/time.h> | |
93b5c9e1 GKH |
41 | #include <linux/io.h> |
42 | #include <linux/bitops.h> | |
5a0e3ad6 | 43 | #include <linux/slab.h> |
4983b39a | 44 | #include "osd.h" |
3e7ee490 | 45 | |
de65a384 | 46 | struct osd_callback_struct { |
3e7ee490 | 47 | struct work_struct work; |
de65a384 BP |
48 | void (*callback)(void *); |
49 | void *data; | |
50 | }; | |
3e7ee490 | 51 | |
bfc30aae | 52 | void *osd_VirtualAllocExec(unsigned int size) |
3e7ee490 HJ |
53 | { |
54 | #ifdef __x86_64__ | |
55 | return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL_EXEC); | |
56 | #else | |
93b5c9e1 GKH |
57 | return __vmalloc(size, GFP_KERNEL, |
58 | __pgprot(__PAGE_KERNEL & (~_PAGE_NX))); | |
3e7ee490 HJ |
59 | #endif |
60 | } | |
61 | ||
3e189519 HJ |
62 | /** |
63 | * osd_PageAlloc() - Allocate pages | |
64 | * @count: Total number of Kernel pages you want to allocate | |
65 | * | |
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. | |
70 | */ | |
bfc30aae | 71 | void *osd_PageAlloc(unsigned int count) |
3e7ee490 HJ |
72 | { |
73 | void *p; | |
93b5c9e1 | 74 | |
3e7ee490 | 75 | p = (void *)__get_free_pages(GFP_KERNEL, get_order(count * PAGE_SIZE)); |
93b5c9e1 GKH |
76 | if (p) |
77 | memset(p, 0, count * PAGE_SIZE); | |
3e7ee490 HJ |
78 | return p; |
79 | ||
454f18a9 BP |
80 | /* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */ |
81 | /* void *p; */ | |
3e7ee490 | 82 | |
454f18a9 BP |
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); */ | |
86 | /* return p; */ | |
3e7ee490 | 87 | } |
45dcfb38 | 88 | EXPORT_SYMBOL_GPL(osd_PageAlloc); |
3e7ee490 | 89 | |
3e189519 HJ |
90 | /** |
91 | * osd_PageFree() - Free pages | |
92 | * @page: Pointer to the first page to be freed | |
93 | * @count: Total number of Kernel pages you free | |
94 | * | |
95 | * Frees the pages allocated by osd_PageAlloc() | |
96 | * Mainly used by Hyper-V drivers. | |
97 | */ | |
93b5c9e1 | 98 | void osd_PageFree(void *page, unsigned int count) |
3e7ee490 HJ |
99 | { |
100 | free_pages((unsigned long)page, get_order(count * PAGE_SIZE)); | |
101 | /*struct page* p = virt_to_page(page); | |
102 | __free_page(p);*/ | |
103 | } | |
45dcfb38 | 104 | EXPORT_SYMBOL_GPL(osd_PageFree); |
3e7ee490 | 105 | |
3e189519 HJ |
106 | /** |
107 | * osd_WaitEventCreate() - Create the event queue | |
108 | * | |
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. | |
113 | * | |
114 | * Returns pointer to &struct osd_waitevent | |
115 | * Mainly used by Hyper-V drivers. | |
116 | */ | |
bfc30aae | 117 | struct osd_waitevent *osd_WaitEventCreate(void) |
3e7ee490 | 118 | { |
93b5c9e1 GKH |
119 | struct osd_waitevent *wait = kmalloc(sizeof(struct osd_waitevent), |
120 | GFP_KERNEL); | |
3e7ee490 | 121 | if (!wait) |
3e7ee490 | 122 | return NULL; |
3e7ee490 HJ |
123 | |
124 | wait->condition = 0; | |
125 | init_waitqueue_head(&wait->event); | |
126 | return wait; | |
127 | } | |
45dcfb38 | 128 | EXPORT_SYMBOL_GPL(osd_WaitEventCreate); |
3e7ee490 | 129 | |
3e189519 HJ |
130 | |
131 | /** | |
132 | * osd_WaitEventSet() - Wake up the process | |
133 | * @waitEvent: Structure to event to be woken up | |
134 | * | |
135 | * @waitevent is of type &struct osd_waitevent | |
136 | * | |
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. | |
140 | * | |
141 | * Only used by Network and Storage Hyper-V drivers. | |
142 | */ | |
bfc30aae | 143 | void osd_WaitEventSet(struct osd_waitevent *waitEvent) |
3e7ee490 | 144 | { |
3e7ee490 HJ |
145 | waitEvent->condition = 1; |
146 | wake_up_interruptible(&waitEvent->event); | |
147 | } | |
45dcfb38 | 148 | EXPORT_SYMBOL_GPL(osd_WaitEventSet); |
3e7ee490 | 149 | |
3e189519 HJ |
150 | /** |
151 | * osd_WaitEventWait() - Wait for event till condition is true | |
152 | * @waitEvent: Structure to event to be put to sleep | |
153 | * | |
154 | * @waitevent is of type &struct osd_waitevent | |
155 | * | |
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. | |
159 | * | |
160 | * Returns the status of 'wait_event_interruptible()' system call | |
161 | * | |
162 | * Mainly used by Hyper-V drivers. | |
163 | */ | |
bfc30aae | 164 | int osd_WaitEventWait(struct osd_waitevent *waitEvent) |
3e7ee490 | 165 | { |
93b5c9e1 | 166 | int ret = 0; |
3e7ee490 | 167 | |
aedb444a BP |
168 | ret = wait_event_interruptible(waitEvent->event, |
169 | waitEvent->condition); | |
3e7ee490 HJ |
170 | waitEvent->condition = 0; |
171 | return ret; | |
172 | } | |
45dcfb38 | 173 | EXPORT_SYMBOL_GPL(osd_WaitEventWait); |
3e7ee490 | 174 | |
3e189519 HJ |
175 | /** |
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 | |
179 | * | |
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. | |
185 | * | |
186 | * Returns the status of 'wait_event_interruptible_timeout()' system call | |
187 | * | |
188 | * Mainly used by Hyper-V drivers. | |
189 | */ | |
bfc30aae | 190 | int osd_WaitEventWaitEx(struct osd_waitevent *waitEvent, u32 TimeoutInMs) |
3e7ee490 | 191 | { |
93b5c9e1 | 192 | int ret = 0; |
3e7ee490 | 193 | |
aedb444a BP |
194 | ret = wait_event_interruptible_timeout(waitEvent->event, |
195 | waitEvent->condition, | |
196 | msecs_to_jiffies(TimeoutInMs)); | |
3e7ee490 HJ |
197 | waitEvent->condition = 0; |
198 | return ret; | |
199 | } | |
45dcfb38 | 200 | EXPORT_SYMBOL_GPL(osd_WaitEventWaitEx); |
3e7ee490 | 201 | |
de65a384 | 202 | static void osd_callback_work(struct work_struct *work) |
3e7ee490 | 203 | { |
de65a384 | 204 | struct osd_callback_struct *cb = container_of(work, |
93b5c9e1 GKH |
205 | struct osd_callback_struct, |
206 | work); | |
de65a384 | 207 | (cb->callback)(cb->data); |
de65a384 | 208 | kfree(cb); |
3e7ee490 HJ |
209 | } |
210 | ||
de65a384 BP |
211 | int osd_schedule_callback(struct workqueue_struct *wq, |
212 | void (*func)(void *), | |
213 | void *data) | |
3e7ee490 | 214 | { |
de65a384 | 215 | struct osd_callback_struct *cb; |
3e7ee490 | 216 | |
de65a384 | 217 | cb = kmalloc(sizeof(*cb), GFP_KERNEL); |
93b5c9e1 GKH |
218 | if (!cb) { |
219 | printk(KERN_ERR "unable to allocate memory in osd_schedule_callback\n"); | |
3e7ee490 HJ |
220 | return -1; |
221 | } | |
222 | ||
de65a384 BP |
223 | cb->callback = func; |
224 | cb->data = data; | |
225 | INIT_WORK(&cb->work, osd_callback_work); | |
226 | return queue_work(wq, &cb->work); | |
3e7ee490 HJ |
227 | } |
228 |