• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/staging/hv/
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
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>
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>
41#include <linux/io.h>
42#include <linux/bitops.h>
43#include <linux/slab.h>
44#include "osd.h"
45
46struct osd_callback_struct {
47	struct work_struct work;
48	void (*callback)(void *);
49	void *data;
50};
51
52void *osd_VirtualAllocExec(unsigned int size)
53{
54#ifdef __x86_64__
55	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL_EXEC);
56#else
57	return __vmalloc(size, GFP_KERNEL,
58			 __pgprot(__PAGE_KERNEL & (~_PAGE_NX)));
59#endif
60}
61
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 */
71void *osd_PageAlloc(unsigned int count)
72{
73	void *p;
74
75	p = (void *)__get_free_pages(GFP_KERNEL, get_order(count * PAGE_SIZE));
76	if (p)
77		memset(p, 0, count * PAGE_SIZE);
78	return p;
79
80	/* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */
81	/* void *p; */
82
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; */
87}
88EXPORT_SYMBOL_GPL(osd_PageAlloc);
89
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 */
98void osd_PageFree(void *page, unsigned int count)
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}
104EXPORT_SYMBOL_GPL(osd_PageFree);
105
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 */
117struct osd_waitevent *osd_WaitEventCreate(void)
118{
119	struct osd_waitevent *wait = kmalloc(sizeof(struct osd_waitevent),
120					     GFP_KERNEL);
121	if (!wait)
122		return NULL;
123
124	wait->condition = 0;
125	init_waitqueue_head(&wait->event);
126	return wait;
127}
128EXPORT_SYMBOL_GPL(osd_WaitEventCreate);
129
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 */
143void osd_WaitEventSet(struct osd_waitevent *waitEvent)
144{
145	waitEvent->condition = 1;
146	wake_up_interruptible(&waitEvent->event);
147}
148EXPORT_SYMBOL_GPL(osd_WaitEventSet);
149
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 */
164int osd_WaitEventWait(struct osd_waitevent *waitEvent)
165{
166	int ret = 0;
167
168	ret = wait_event_interruptible(waitEvent->event,
169				       waitEvent->condition);
170	waitEvent->condition = 0;
171	return ret;
172}
173EXPORT_SYMBOL_GPL(osd_WaitEventWait);
174
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 */
190int osd_WaitEventWaitEx(struct osd_waitevent *waitEvent, u32 TimeoutInMs)
191{
192	int ret = 0;
193
194	ret = wait_event_interruptible_timeout(waitEvent->event,
195					       waitEvent->condition,
196					       msecs_to_jiffies(TimeoutInMs));
197	waitEvent->condition = 0;
198	return ret;
199}
200EXPORT_SYMBOL_GPL(osd_WaitEventWaitEx);
201
202static void osd_callback_work(struct work_struct *work)
203{
204	struct osd_callback_struct *cb = container_of(work,
205						struct osd_callback_struct,
206						work);
207	(cb->callback)(cb->data);
208	kfree(cb);
209}
210
211int osd_schedule_callback(struct workqueue_struct *wq,
212			  void (*func)(void *),
213			  void *data)
214{
215	struct osd_callback_struct *cb;
216
217	cb = kmalloc(sizeof(*cb), GFP_KERNEL);
218	if (!cb) {
219		printk(KERN_ERR "unable to allocate memory in osd_schedule_callback\n");
220		return -1;
221	}
222
223	cb->callback = func;
224	cb->data = data;
225	INIT_WORK(&cb->work, osd_callback_work);
226	return queue_work(wq, &cb->work);
227}
228