kernel: fix OCF for linux 3.2
[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 #else
166 #define ocf_daemonize(str) daemonize(str);
167 #endif
168
169 #define TAILQ_INSERT_TAIL(q,d,m) list_add_tail(&(d)->m, (q))
170 #define TAILQ_EMPTY(q)  list_empty(q)
171 #define TAILQ_FOREACH(v, q, m) list_for_each_entry(v, q, m)
172
173 #define read_random(p,l) get_random_bytes(p,l)
174
175 #define DELAY(x)        ((x) > 2000 ? mdelay((x)/1000) : udelay(x))
176 #define strtoul simple_strtoul
177
178 #define pci_get_vendor(dev)     ((dev)->vendor)
179 #define pci_get_device(dev)     ((dev)->device)
180
181 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
182 #define pci_set_consistent_dma_mask(dev, mask) (0)
183 #endif
184 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
185 #define pci_dma_sync_single_for_cpu pci_dma_sync_single
186 #endif
187
188 #ifndef DMA_32BIT_MASK
189 #define DMA_32BIT_MASK  0x00000000ffffffffULL
190 #endif
191
192 #ifndef htole32
193 #define htole32(x)      cpu_to_le32(x)
194 #endif
195 #ifndef htobe32
196 #define htobe32(x)      cpu_to_be32(x)
197 #endif
198 #ifndef htole16
199 #define htole16(x)      cpu_to_le16(x)
200 #endif
201 #ifndef htobe16
202 #define htobe16(x)      cpu_to_be16(x)
203 #endif
204
205 /* older kernels don't have these */
206
207 #include <asm/irq.h>
208 #if !defined(IRQ_NONE) && !defined(IRQ_RETVAL)
209 #define IRQ_NONE
210 #define IRQ_HANDLED
211 #define IRQ_WAKE_THREAD
212 #define IRQ_RETVAL
213 #define irqreturn_t void
214 typedef irqreturn_t (*irq_handler_t)(int irq, void *arg, struct pt_regs *regs);
215 #endif
216 #ifndef IRQF_SHARED
217 #define IRQF_SHARED     SA_SHIRQ
218 #endif
219
220 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
221 # define strlcpy(dest,src,len) \
222                 ({strncpy(dest,src,(len)-1); ((char *)dest)[(len)-1] = '\0'; })
223 #endif
224
225 #ifndef MAX_ERRNO
226 #define MAX_ERRNO       4095
227 #endif
228 #ifndef IS_ERR_VALUE
229 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,5)
230 #include <linux/err.h>
231 #endif
232 #ifndef IS_ERR_VALUE
233 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
234 #endif
235 #endif
236
237 /*
238  * common debug for all
239  */
240 #if 1
241 #define dprintk(a...)   do { if (debug) printk(a); } while(0)
242 #else
243 #define dprintk(a...)
244 #endif
245
246 #ifndef SLAB_ATOMIC
247 /* Changed in 2.6.20, must use GFP_ATOMIC now */
248 #define SLAB_ATOMIC     GFP_ATOMIC
249 #endif
250
251 /*
252  * need some additional support for older kernels */
253 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,2)
254 #define pci_register_driver_compat(driver, rc) \
255         do { \
256                 if ((rc) > 0) { \
257                         (rc) = 0; \
258                 } else if (rc == 0) { \
259                         (rc) = -ENODEV; \
260                 } else { \
261                         pci_unregister_driver(driver); \
262                 } \
263         } while (0)
264 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
265 #define pci_register_driver_compat(driver,rc) ((rc) = (rc) < 0 ? (rc) : 0)
266 #else
267 #define pci_register_driver_compat(driver,rc)
268 #endif
269
270 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
271
272 #include <linux/mm.h>
273 #include <asm/scatterlist.h>
274
275 static inline void sg_set_page(struct scatterlist *sg,  struct page *page,
276                                unsigned int len, unsigned int offset)
277 {
278         sg->page = page;
279         sg->offset = offset;
280         sg->length = len;
281 }
282
283 static inline void *sg_virt(struct scatterlist *sg)
284 {
285         return page_address(sg->page) + sg->offset;
286 }
287
288 #define sg_init_table(sg, n)
289
290 #endif
291
292 #ifndef late_initcall
293 #define late_initcall(init) module_init(init)
294 #endif
295
296 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4) || !defined(CONFIG_SMP)
297 #define ocf_for_each_cpu(cpu) for ((cpu) = 0; (cpu) == 0; (cpu)++)
298 #else
299 #define ocf_for_each_cpu(cpu) for_each_present_cpu(cpu)
300 #endif
301
302 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
303 #include <linux/sched.h>
304 #define kill_proc(p,s,v)        send_sig(s,find_task_by_vpid(p),0)
305 #endif
306
307 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4)
308
309 struct ocf_thread {
310         struct task_struct      *task;
311         int                                     (*func)(void *arg);
312         void                            *arg;
313 };
314
315 /* thread startup helper func */
316 static inline int ocf_run_thread(void *arg)
317 {
318         struct ocf_thread *t = (struct ocf_thread *) arg;
319         if (!t)
320                 return -1; /* very bad */
321         t->task = current;
322         daemonize();
323         spin_lock_irq(&current->sigmask_lock);
324         sigemptyset(&current->blocked);
325         recalc_sigpending(current);
326         spin_unlock_irq(&current->sigmask_lock);
327         return (*t->func)(t->arg);
328 }
329
330 #define kthread_create(f,a,fmt...) \
331         ({ \
332                 struct ocf_thread t; \
333                 pid_t p; \
334                 t.task = NULL; \
335                 t.func = (f); \
336                 t.arg = (a); \
337                 p = kernel_thread(ocf_run_thread, &t, CLONE_FS|CLONE_FILES); \
338                 while (p != (pid_t) -1 && t.task == NULL) \
339                         schedule(); \
340                 if (t.task) \
341                         snprintf(t.task->comm, sizeof(t.task->comm), fmt); \
342                 (t.task); \
343         })
344
345 #define kthread_bind(t,cpu)     /**/
346
347 #define kthread_should_stop()   (strcmp(current->comm, "stopping") == 0)
348
349 #define kthread_stop(t) \
350         ({ \
351                 strcpy((t)->comm, "stopping"); \
352                 kill_proc((t)->pid, SIGTERM, 1); \
353                 do { \
354                         schedule(); \
355                 } while (kill_proc((t)->pid, SIGTERM, 1) == 0); \
356         })
357
358 #else
359 #include <linux/kthread.h>
360 #endif
361
362 #include <linux/skbuff.h>
363
364 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0)
365 static inline struct page *skb_frag_page(const skb_frag_t *frag)
366 {
367         return frag->page;
368 }
369 #endif
370
371 #endif /* __KERNEL__ */
372
373 /****************************************************************************/
374 #endif /* _BSD_COMPAT_H_ */