2 #define _BSD_COMPAT_H_ 1
3 /****************************************************************************/
5 * Provide compat routines for older linux kernels and BSD kernels
7 * Written by David McCullough <david_mccullough@mcafee.com>
8 * Copyright (C) 2010 David McCullough <david_mccullough@mcafee.com>
12 * The free distribution and use of this software in both source and binary
13 * form is allowed (with or without changes) provided that:
15 * 1. distributions of this source code include the above copyright
16 * notice, this list of conditions and the following disclaimer;
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;
22 * 3. the copyright holder's name is not used to endorse products
23 * built using this software without specific written permission.
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.
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.
35 /****************************************************************************/
37 #include <linux/version.h>
38 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
39 #include <linux/config.h>
43 * fake some BSD driver interface stuff specifically for OCF use
46 typedef struct ocf_device *device_t;
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);
54 #define DEVMETHOD(id, func) id: func
57 char name[32]; /* the driver name */
58 char nameunit[32]; /* the driver name + HW instance */
60 device_method_t methods;
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))
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)
78 #define softc_device_decl \
79 struct ocf_device _device; \
82 #define softc_device_init(_sc, _name, _unit, _methods) \
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; \
92 #define softc_get_device(_sc) (&(_sc)->_device)
95 * iomem support for 2.4 and 2.6 kernels
97 #include <linux/version.h>
98 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
99 #define ocf_iomem_t unsigned long
102 * implement simple workqueue like support for older kernels
105 #include <linux/tqueue.h>
107 #define work_struct tq_struct
109 #define INIT_WORK(wp, fp, ap) \
112 (wp)->routine = (fp); \
116 #define schedule_work(wp) \
118 queue_task((wp), &tq_immediate); \
119 mark_bh(IMMEDIATE_BH); \
122 #define flush_scheduled_work() run_task_queue(&tq_immediate)
125 #define ocf_iomem_t void __iomem *
127 #include <linux/workqueue.h>
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)
138 #undef module_param /* just in case */
139 #define module_param(a,b,c) MODULE_PARM(a,"i")
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)
146 #define MIN(x,y) ((x) < (y) ? (x) : (y))
148 #define device_printf(dev, a...) ({ \
149 printk("%s: ", device_get_nameunit(dev)); printk(a); \
153 #define printf(fmt...) printk(fmt)
155 #define KASSERT(c,p) if (!(c)) { printk p ; } else
157 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
158 #define ocf_daemonize(str) \
160 spin_lock_irq(¤t->sigmask_lock); \
161 sigemptyset(¤t->blocked); \
162 recalc_sigpending(current); \
163 spin_unlock_irq(¤t->sigmask_lock); \
164 sprintf(current->comm, str); \
165 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0) \
166 spin_lock_irq(¤t->sigmask_lock); \
167 sigemptyset(¤t->blocked); \
168 recalc_sigpending(current); \
169 spin_unlock_irq(¤t->sigmask_lock); \
170 sprintf(current->comm, str); \
172 #define ocf_daemonize(str) daemonize(str);
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)
179 #define read_random(p,l) get_random_bytes(p,l)
181 #define DELAY(x) ((x) > 2000 ? mdelay((x)/1000) : udelay(x))
182 #define strtoul simple_strtoul
184 #define pci_get_vendor(dev) ((dev)->vendor)
185 #define pci_get_device(dev) ((dev)->device)
187 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
188 #define pci_set_consistent_dma_mask(dev, mask) (0)
190 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
191 #define pci_dma_sync_single_for_cpu pci_dma_sync_single
194 #ifndef DMA_32BIT_MASK
195 #define DMA_32BIT_MASK 0x00000000ffffffffULL
199 #define htole32(x) cpu_to_le32(x)
202 #define htobe32(x) cpu_to_be32(x)
205 #define htole16(x) cpu_to_le16(x)
208 #define htobe16(x) cpu_to_be16(x)
211 /* older kernels don't have these */
214 #if !defined(IRQ_NONE) && !defined(IRQ_RETVAL)
217 #define IRQ_WAKE_THREAD
219 #define irqreturn_t void
220 typedef irqreturn_t (*irq_handler_t)(int irq, void *arg, struct pt_regs *regs);
223 #define IRQF_SHARED SA_SHIRQ
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'; })
232 #define MAX_ERRNO 4095
235 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,5)
236 #include <linux/err.h>
239 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
244 * common debug for all
247 #define dprintk(a...) do { if (debug) printk(a); } while(0)
249 #define dprintk(a...)
253 /* Changed in 2.6.20, must use GFP_ATOMIC now */
254 #define SLAB_ATOMIC GFP_ATOMIC
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) \
264 } else if (rc == 0) { \
267 pci_unregister_driver(driver); \
270 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
271 #define pci_register_driver_compat(driver,rc) ((rc) = (rc) < 0 ? (rc) : 0)
273 #define pci_register_driver_compat(driver,rc)
276 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
278 #include <linux/mm.h>
279 #include <asm/scatterlist.h>
281 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
282 unsigned int len, unsigned int offset)
289 static inline void *sg_virt(struct scatterlist *sg)
291 return page_address(sg->page) + sg->offset;
294 #define sg_init_table(sg, n)
296 #define sg_mark_end(sg)
300 #ifndef late_initcall
301 #define late_initcall(init) module_init(init)
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)++)
307 #define ocf_for_each_cpu(cpu) for_each_present_cpu(cpu)
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)
315 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4)
318 struct task_struct *task;
319 int (*func)(void *arg);
323 /* thread startup helper func */
324 static inline int ocf_run_thread(void *arg)
326 struct ocf_thread *t = (struct ocf_thread *) arg;
328 return -1; /* very bad */
331 spin_lock_irq(¤t->sigmask_lock);
332 sigemptyset(¤t->blocked);
333 recalc_sigpending(current);
334 spin_unlock_irq(¤t->sigmask_lock);
335 return (*t->func)(t->arg);
338 #define kthread_create(f,a,fmt...) \
340 struct ocf_thread t; \
345 p = kernel_thread(ocf_run_thread, &t, CLONE_FS|CLONE_FILES); \
346 while (p != (pid_t) -1 && t.task == NULL) \
349 snprintf(t.task->comm, sizeof(t.task->comm), fmt); \
353 #define kthread_bind(t,cpu) /**/
355 #define kthread_should_stop() (strcmp(current->comm, "stopping") == 0)
357 #define kthread_stop(t) \
359 strcpy((t)->comm, "stopping"); \
360 kill_proc((t)->pid, SIGTERM, 1); \
363 } while (kill_proc((t)->pid, SIGTERM, 1) == 0); \
367 #include <linux/kthread.h>
371 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0)
372 #define skb_frag_page(x) ((x)->page)
375 #endif /* __KERNEL__ */
377 /****************************************************************************/
378 #endif /* _BSD_COMPAT_H_ */