kernel: add support for 3.8-rc2
[15.05/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 !defined(IRQ_NONE) && !defined(IRQ_RETVAL)
215 #define IRQ_NONE
216 #define IRQ_HANDLED
217 #define IRQ_WAKE_THREAD
218 #define IRQ_RETVAL
219 #define irqreturn_t void
220 typedef irqreturn_t (*irq_handler_t)(int irq, void *arg, struct pt_regs *regs);
221 #endif
222 #ifndef IRQF_SHARED
223 #define IRQF_SHARED     SA_SHIRQ
224 #endif
225
226 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
227 # define strlcpy(dest,src,len) \
228                 ({strncpy(dest,src,(len)-1); ((char *)dest)[(len)-1] = '\0'; })
229 #endif
230
231 #ifndef MAX_ERRNO
232 #define MAX_ERRNO       4095
233 #endif
234 #ifndef IS_ERR_VALUE
235 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,5)
236 #include <linux/err.h>
237 #endif
238 #ifndef IS_ERR_VALUE
239 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
240 #endif
241 #endif
242
243 /*
244  * common debug for all
245  */
246 #if 1
247 #define dprintk(a...)   do { if (debug) printk(a); } while(0)
248 #else
249 #define dprintk(a...)
250 #endif
251
252 #ifndef SLAB_ATOMIC
253 /* Changed in 2.6.20, must use GFP_ATOMIC now */
254 #define SLAB_ATOMIC     GFP_ATOMIC
255 #endif
256
257 /*
258  * need some additional support for older kernels */
259 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,2)
260 #define pci_register_driver_compat(driver, rc) \
261         do { \
262                 if ((rc) > 0) { \
263                         (rc) = 0; \
264                 } else if (rc == 0) { \
265                         (rc) = -ENODEV; \
266                 } else { \
267                         pci_unregister_driver(driver); \
268                 } \
269         } while (0)
270 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
271 #define pci_register_driver_compat(driver,rc) ((rc) = (rc) < 0 ? (rc) : 0)
272 #else
273 #define pci_register_driver_compat(driver,rc)
274 #endif
275
276 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
277
278 #include <linux/mm.h>
279 #include <asm/scatterlist.h>
280
281 static inline void sg_set_page(struct scatterlist *sg,  struct page *page,
282                                unsigned int len, unsigned int offset)
283 {
284         sg->page = page;
285         sg->offset = offset;
286         sg->length = len;
287 }
288
289 static inline void *sg_virt(struct scatterlist *sg)
290 {
291         return page_address(sg->page) + sg->offset;
292 }
293
294 #define sg_init_table(sg, n)
295
296 #define sg_mark_end(sg)
297
298 #endif
299
300 #ifndef late_initcall
301 #define late_initcall(init) module_init(init)
302 #endif
303
304 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4) || !defined(CONFIG_SMP)
305 #define ocf_for_each_cpu(cpu) for ((cpu) = 0; (cpu) == 0; (cpu)++)
306 #else
307 #define ocf_for_each_cpu(cpu) for_each_present_cpu(cpu)
308 #endif
309
310 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
311 #include <linux/sched.h>
312 #define kill_proc(p,s,v)        send_sig(s,find_task_by_vpid(p),0)
313 #endif
314
315 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4)
316
317 struct ocf_thread {
318         struct task_struct      *task;
319         int                                     (*func)(void *arg);
320         void                            *arg;
321 };
322
323 /* thread startup helper func */
324 static inline int ocf_run_thread(void *arg)
325 {
326         struct ocf_thread *t = (struct ocf_thread *) arg;
327         if (!t)
328                 return -1; /* very bad */
329         t->task = current;
330         daemonize();
331         spin_lock_irq(&current->sigmask_lock);
332         sigemptyset(&current->blocked);
333         recalc_sigpending(current);
334         spin_unlock_irq(&current->sigmask_lock);
335         return (*t->func)(t->arg);
336 }
337
338 #define kthread_create(f,a,fmt...) \
339         ({ \
340                 struct ocf_thread t; \
341                 pid_t p; \
342                 t.task = NULL; \
343                 t.func = (f); \
344                 t.arg = (a); \
345                 p = kernel_thread(ocf_run_thread, &t, CLONE_FS|CLONE_FILES); \
346                 while (p != (pid_t) -1 && t.task == NULL) \
347                         schedule(); \
348                 if (t.task) \
349                         snprintf(t.task->comm, sizeof(t.task->comm), fmt); \
350                 (t.task); \
351         })
352
353 #define kthread_bind(t,cpu)     /**/
354
355 #define kthread_should_stop()   (strcmp(current->comm, "stopping") == 0)
356
357 #define kthread_stop(t) \
358         ({ \
359                 strcpy((t)->comm, "stopping"); \
360                 kill_proc((t)->pid, SIGTERM, 1); \
361                 do { \
362                         schedule(); \
363                 } while (kill_proc((t)->pid, SIGTERM, 1) == 0); \
364         })
365
366 #else
367 #include <linux/kthread.h>
368 #endif
369
370
371 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0)
372 #define skb_frag_page(x)        ((x)->page)
373 #endif
374
375 #endif /* __KERNEL__ */
376
377 /****************************************************************************/
378 #endif /* _BSD_COMPAT_H_ */