b38cf27b3a3df8a360dd449ae8bbfef31772d126
[openwrt/openwrt.git] / target / linux / generic / files / crypto / ocf / ocf-compat.h
1 #ifndef _BSD_COMPAT_H_
2 #define _BSD_COMPAT_H_ 1
3 /****************************************************************************/
4 /*
5 * Provide compat routines for older linux kernels and BSD kernels
6 *
7 * Written by David McCullough <david_mccullough@mcafee.com>
8 * Copyright (C) 2010 David McCullough <david_mccullough@mcafee.com>
9 *
10 * LICENSE TERMS
11 *
12 * The free distribution and use of this software in both source and binary
13 * form is allowed (with or without changes) provided that:
14 *
15 * 1. distributions of this source code include the above copyright
16 * notice, this list of conditions and the following disclaimer;
17 *
18 * 2. distributions in binary form include the above copyright
19 * notice, this list of conditions and the following disclaimer
20 * in the documentation and/or other associated materials;
21 *
22 * 3. the copyright holder's name is not used to endorse products
23 * built using this software without specific written permission.
24 *
25 * ALTERNATIVELY, provided that this notice is retained in full, this file
26 * may be distributed under the terms of the GNU General Public License (GPL),
27 * in which case the provisions of the GPL apply INSTEAD OF those given above.
28 *
29 * DISCLAIMER
30 *
31 * This software is provided 'as is' with no explicit or implied warranties
32 * in respect of its properties, including, but not limited to, correctness
33 * and/or fitness for purpose.
34 */
35 /****************************************************************************/
36 #ifdef __KERNEL__
37 #include <linux/version.h>
38 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
39 #include <linux/config.h>
40 #endif
41
42 /*
43 * fake some BSD driver interface stuff specifically for OCF use
44 */
45
46 typedef struct ocf_device *device_t;
47
48 typedef struct {
49 int (*cryptodev_newsession)(device_t dev, u_int32_t *sidp, struct cryptoini *cri);
50 int (*cryptodev_freesession)(device_t dev, u_int64_t tid);
51 int (*cryptodev_process)(device_t dev, struct cryptop *crp, int hint);
52 int (*cryptodev_kprocess)(device_t dev, struct cryptkop *krp, int hint);
53 } device_method_t;
54 #define DEVMETHOD(id, func) id: func
55
56 struct ocf_device {
57 char name[32]; /* the driver name */
58 char nameunit[32]; /* the driver name + HW instance */
59 int unit;
60 device_method_t methods;
61 void *softc;
62 };
63
64 #define CRYPTODEV_NEWSESSION(dev, sid, cri) \
65 ((*(dev)->methods.cryptodev_newsession)(dev,sid,cri))
66 #define CRYPTODEV_FREESESSION(dev, sid) \
67 ((*(dev)->methods.cryptodev_freesession)(dev, sid))
68 #define CRYPTODEV_PROCESS(dev, crp, hint) \
69 ((*(dev)->methods.cryptodev_process)(dev, crp, hint))
70 #define CRYPTODEV_KPROCESS(dev, krp, hint) \
71 ((*(dev)->methods.cryptodev_kprocess)(dev, krp, hint))
72
73 #define device_get_name(dev) ((dev)->name)
74 #define device_get_nameunit(dev) ((dev)->nameunit)
75 #define device_get_unit(dev) ((dev)->unit)
76 #define device_get_softc(dev) ((dev)->softc)
77
78 #define softc_device_decl \
79 struct ocf_device _device; \
80 device_t
81
82 #define softc_device_init(_sc, _name, _unit, _methods) \
83 if (1) {\
84 strncpy((_sc)->_device.name, _name, sizeof((_sc)->_device.name) - 1); \
85 snprintf((_sc)->_device.nameunit, sizeof((_sc)->_device.name), "%s%d", _name, _unit); \
86 (_sc)->_device.unit = _unit; \
87 (_sc)->_device.methods = _methods; \
88 (_sc)->_device.softc = (void *) _sc; \
89 *(device_t *)((softc_get_device(_sc))+1) = &(_sc)->_device; \
90 } else
91
92 #define softc_get_device(_sc) (&(_sc)->_device)
93
94 /*
95 * iomem support for 2.4 and 2.6 kernels
96 */
97 #include <linux/version.h>
98 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
99 #define ocf_iomem_t unsigned long
100
101 /*
102 * implement simple workqueue like support for older kernels
103 */
104
105 #include <linux/tqueue.h>
106
107 #define work_struct tq_struct
108
109 #define INIT_WORK(wp, fp, ap) \
110 do { \
111 (wp)->sync = 0; \
112 (wp)->routine = (fp); \
113 (wp)->data = (ap); \
114 } while (0)
115
116 #define schedule_work(wp) \
117 do { \
118 queue_task((wp), &tq_immediate); \
119 mark_bh(IMMEDIATE_BH); \
120 } while (0)
121
122 #define flush_scheduled_work() run_task_queue(&tq_immediate)
123
124 #else
125 #define ocf_iomem_t void __iomem *
126
127 #include <linux/workqueue.h>
128
129 #endif
130
131 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
132 #include <linux/fdtable.h>
133 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
134 #define files_fdtable(files) (files)
135 #endif
136
137 #ifdef MODULE_PARM
138 #undef module_param /* just in case */
139 #define module_param(a,b,c) MODULE_PARM(a,"i")
140 #endif
141
142 #define bzero(s,l) memset(s,0,l)
143 #define bcopy(s,d,l) memcpy(d,s,l)
144 #define bcmp(x, y, l) memcmp(x,y,l)
145
146 #define MIN(x,y) ((x) < (y) ? (x) : (y))
147
148 #define device_printf(dev, a...) ({ \
149 printk("%s: ", device_get_nameunit(dev)); printk(a); \
150 })
151
152 #undef printf
153 #define printf(fmt...) printk(fmt)
154
155 #define KASSERT(c,p) if (!(c)) { printk p ; } else
156
157 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
158 #define ocf_daemonize(str) \
159 daemonize(); \
160 spin_lock_irq(&current->sigmask_lock); \
161 sigemptyset(&current->blocked); \
162 recalc_sigpending(current); \
163 spin_unlock_irq(&current->sigmask_lock); \
164 sprintf(current->comm, str); \
165 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0) \
166 spin_lock_irq(&current->sigmask_lock); \
167 sigemptyset(&current->blocked); \
168 recalc_sigpending(current); \
169 spin_unlock_irq(&current->sigmask_lock); \
170 sprintf(current->comm, str); \
171 #else
172 #define ocf_daemonize(str) daemonize(str);
173 #endif
174
175 #define TAILQ_INSERT_TAIL(q,d,m) list_add_tail(&(d)->m, (q))
176 #define TAILQ_EMPTY(q) list_empty(q)
177 #define TAILQ_FOREACH(v, q, m) list_for_each_entry(v, q, m)
178
179 #define read_random(p,l) get_random_bytes(p,l)
180
181 #define DELAY(x) ((x) > 2000 ? mdelay((x)/1000) : udelay(x))
182 #define strtoul simple_strtoul
183
184 #define pci_get_vendor(dev) ((dev)->vendor)
185 #define pci_get_device(dev) ((dev)->device)
186
187 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
188 #define pci_set_consistent_dma_mask(dev, mask) (0)
189 #endif
190 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
191 #define pci_dma_sync_single_for_cpu pci_dma_sync_single
192 #endif
193
194 #ifndef DMA_32BIT_MASK
195 #define DMA_32BIT_MASK 0x00000000ffffffffULL
196 #endif
197
198 #ifndef htole32
199 #define htole32(x) cpu_to_le32(x)
200 #endif
201 #ifndef htobe32
202 #define htobe32(x) cpu_to_be32(x)
203 #endif
204 #ifndef htole16
205 #define htole16(x) cpu_to_le16(x)
206 #endif
207 #ifndef htobe16
208 #define htobe16(x) cpu_to_be16(x)
209 #endif
210
211 /* older kernels don't have these */
212
213 #include <asm/irq.h>
214 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
215 #if !defined(IRQ_NONE) && !defined(IRQ_RETVAL)
216 #define IRQ_NONE
217 #define IRQ_HANDLED
218 #define IRQ_WAKE_THREAD
219 #define IRQ_RETVAL
220 #define irqreturn_t void
221 typedef irqreturn_t (*irq_handler_t)(int irq, void *arg, struct pt_regs *regs);
222 #endif
223 #ifndef IRQF_SHARED
224 #define IRQF_SHARED SA_SHIRQ
225 #endif
226 #endif
227
228 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
229 # define strlcpy(dest,src,len) \
230 ({strncpy(dest,src,(len)-1); ((char *)dest)[(len)-1] = '\0'; })
231 #endif
232
233 #ifndef MAX_ERRNO
234 #define MAX_ERRNO 4095
235 #endif
236 #ifndef IS_ERR_VALUE
237 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,5)
238 #include <linux/err.h>
239 #endif
240 #ifndef IS_ERR_VALUE
241 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
242 #endif
243 #endif
244
245 /*
246 * common debug for all
247 */
248 #if 1
249 #define dprintk(a...) do { if (debug) printk(a); } while(0)
250 #else
251 #define dprintk(a...)
252 #endif
253
254 #ifndef SLAB_ATOMIC
255 /* Changed in 2.6.20, must use GFP_ATOMIC now */
256 #define SLAB_ATOMIC GFP_ATOMIC
257 #endif
258
259 /*
260 * need some additional support for older kernels */
261 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,2)
262 #define pci_register_driver_compat(driver, rc) \
263 do { \
264 if ((rc) > 0) { \
265 (rc) = 0; \
266 } else if (rc == 0) { \
267 (rc) = -ENODEV; \
268 } else { \
269 pci_unregister_driver(driver); \
270 } \
271 } while (0)
272 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
273 #define pci_register_driver_compat(driver,rc) ((rc) = (rc) < 0 ? (rc) : 0)
274 #else
275 #define pci_register_driver_compat(driver,rc)
276 #endif
277
278 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
279
280 #include <linux/mm.h>
281 #include <asm/scatterlist.h>
282
283 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
284 unsigned int len, unsigned int offset)
285 {
286 sg->page = page;
287 sg->offset = offset;
288 sg->length = len;
289 }
290
291 static inline void *sg_virt(struct scatterlist *sg)
292 {
293 return page_address(sg->page) + sg->offset;
294 }
295
296 #define sg_init_table(sg, n)
297
298 #define sg_mark_end(sg)
299
300 #endif
301
302 #ifndef late_initcall
303 #define late_initcall(init) module_init(init)
304 #endif
305
306 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4) || !defined(CONFIG_SMP)
307 #define ocf_for_each_cpu(cpu) for ((cpu) = 0; (cpu) == 0; (cpu)++)
308 #else
309 #define ocf_for_each_cpu(cpu) for_each_present_cpu(cpu)
310 #endif
311
312 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
313 #include <linux/sched.h>
314 #define kill_proc(p,s,v) send_sig(s,find_task_by_vpid(p),0)
315 #endif
316
317 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4)
318
319 struct ocf_thread {
320 struct task_struct *task;
321 int (*func)(void *arg);
322 void *arg;
323 };
324
325 /* thread startup helper func */
326 static inline int ocf_run_thread(void *arg)
327 {
328 struct ocf_thread *t = (struct ocf_thread *) arg;
329 if (!t)
330 return -1; /* very bad */
331 t->task = current;
332 daemonize();
333 spin_lock_irq(&current->sigmask_lock);
334 sigemptyset(&current->blocked);
335 recalc_sigpending(current);
336 spin_unlock_irq(&current->sigmask_lock);
337 return (*t->func)(t->arg);
338 }
339
340 #define kthread_create(f,a,fmt...) \
341 ({ \
342 struct ocf_thread t; \
343 pid_t p; \
344 t.task = NULL; \
345 t.func = (f); \
346 t.arg = (a); \
347 p = kernel_thread(ocf_run_thread, &t, CLONE_FS|CLONE_FILES); \
348 while (p != (pid_t) -1 && t.task == NULL) \
349 schedule(); \
350 if (t.task) \
351 snprintf(t.task->comm, sizeof(t.task->comm), fmt); \
352 (t.task); \
353 })
354
355 #define kthread_bind(t,cpu) /**/
356
357 #define kthread_should_stop() (strcmp(current->comm, "stopping") == 0)
358
359 #define kthread_stop(t) \
360 ({ \
361 strcpy((t)->comm, "stopping"); \
362 kill_proc((t)->pid, SIGTERM, 1); \
363 do { \
364 schedule(); \
365 } while (kill_proc((t)->pid, SIGTERM, 1) == 0); \
366 })
367
368 #else
369 #include <linux/kthread.h>
370 #endif
371
372
373 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0)
374 #define skb_frag_page(x) ((x)->page)
375 #endif
376
377 #endif /* __KERNEL__ */
378
379 /****************************************************************************/
380 #endif /* _BSD_COMPAT_H_ */