ocf-compat.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. * fake some BSD driver interface stuff specifically for OCF use
  43. */
  44. typedef struct ocf_device *device_t;
  45. typedef struct {
  46. int (*cryptodev_newsession)(device_t dev, u_int32_t *sidp, struct cryptoini *cri);
  47. int (*cryptodev_freesession)(device_t dev, u_int64_t tid);
  48. int (*cryptodev_process)(device_t dev, struct cryptop *crp, int hint);
  49. int (*cryptodev_kprocess)(device_t dev, struct cryptkop *krp, int hint);
  50. } device_method_t;
  51. #define DEVMETHOD(id, func) id: func
  52. struct ocf_device {
  53. char name[32]; /* the driver name */
  54. char nameunit[32]; /* the driver name + HW instance */
  55. int unit;
  56. device_method_t methods;
  57. void *softc;
  58. };
  59. #define CRYPTODEV_NEWSESSION(dev, sid, cri) \
  60. ((*(dev)->methods.cryptodev_newsession)(dev,sid,cri))
  61. #define CRYPTODEV_FREESESSION(dev, sid) \
  62. ((*(dev)->methods.cryptodev_freesession)(dev, sid))
  63. #define CRYPTODEV_PROCESS(dev, crp, hint) \
  64. ((*(dev)->methods.cryptodev_process)(dev, crp, hint))
  65. #define CRYPTODEV_KPROCESS(dev, krp, hint) \
  66. ((*(dev)->methods.cryptodev_kprocess)(dev, krp, hint))
  67. #define device_get_name(dev) ((dev)->name)
  68. #define device_get_nameunit(dev) ((dev)->nameunit)
  69. #define device_get_unit(dev) ((dev)->unit)
  70. #define device_get_softc(dev) ((dev)->softc)
  71. #define softc_device_decl \
  72. struct ocf_device _device; \
  73. device_t
  74. #define softc_device_init(_sc, _name, _unit, _methods) \
  75. if (1) {\
  76. strncpy((_sc)->_device.name, _name, sizeof((_sc)->_device.name) - 1); \
  77. snprintf((_sc)->_device.nameunit, sizeof((_sc)->_device.name), "%s%d", _name, _unit); \
  78. (_sc)->_device.unit = _unit; \
  79. (_sc)->_device.methods = _methods; \
  80. (_sc)->_device.softc = (void *) _sc; \
  81. *(device_t *)((softc_get_device(_sc))+1) = &(_sc)->_device; \
  82. } else
  83. #define softc_get_device(_sc) (&(_sc)->_device)
  84. /*
  85. * iomem support for 2.4 and 2.6 kernels
  86. */
  87. #include <linux/version.h>
  88. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  89. #define ocf_iomem_t unsigned long
  90. /*
  91. * implement simple workqueue like support for older kernels
  92. */
  93. #include <linux/tqueue.h>
  94. #define work_struct tq_struct
  95. #define INIT_WORK(wp, fp, ap) \
  96. do { \
  97. (wp)->sync = 0; \
  98. (wp)->routine = (fp); \
  99. (wp)->data = (ap); \
  100. } while (0)
  101. #define schedule_work(wp) \
  102. do { \
  103. queue_task((wp), &tq_immediate); \
  104. mark_bh(IMMEDIATE_BH); \
  105. } while (0)
  106. #define flush_scheduled_work() run_task_queue(&tq_immediate)
  107. #else
  108. #define ocf_iomem_t void __iomem *
  109. #include <linux/workqueue.h>
  110. #endif
  111. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
  112. #include <linux/fdtable.h>
  113. #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
  114. #define files_fdtable(files) (files)
  115. #endif
  116. #ifdef MODULE_PARM
  117. #undef module_param /* just in case */
  118. #define module_param(a,b,c) MODULE_PARM(a,"i")
  119. #endif
  120. #define bzero(s,l) memset(s,0,l)
  121. #define bcopy(s,d,l) memcpy(d,s,l)
  122. #define bcmp(x, y, l) memcmp(x,y,l)
  123. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  124. #define device_printf(dev, a...) ({ \
  125. printk("%s: ", device_get_nameunit(dev)); printk(a); \
  126. })
  127. #undef printf
  128. #define printf(fmt...) printk(fmt)
  129. #define KASSERT(c,p) if (!(c)) { printk p ; } else
  130. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  131. #define ocf_daemonize(str) \
  132. daemonize(); \
  133. spin_lock_irq(&current->sigmask_lock); \
  134. sigemptyset(&current->blocked); \
  135. recalc_sigpending(current); \
  136. spin_unlock_irq(&current->sigmask_lock); \
  137. sprintf(current->comm, str); \
  138. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0) \
  139. spin_lock_irq(&current->sigmask_lock); \
  140. sigemptyset(&current->blocked); \
  141. recalc_sigpending(current); \
  142. spin_unlock_irq(&current->sigmask_lock); \
  143. sprintf(current->comm, str); \
  144. #else
  145. #define ocf_daemonize(str) daemonize(str);
  146. #endif
  147. #define TAILQ_INSERT_TAIL(q,d,m) list_add_tail(&(d)->m, (q))
  148. #define TAILQ_EMPTY(q) list_empty(q)
  149. #define TAILQ_FOREACH(v, q, m) list_for_each_entry(v, q, m)
  150. #define read_random(p,l) get_random_bytes(p,l)
  151. #define DELAY(x) ((x) > 2000 ? mdelay((x)/1000) : udelay(x))
  152. #define strtoul simple_strtoul
  153. #define pci_get_vendor(dev) ((dev)->vendor)
  154. #define pci_get_device(dev) ((dev)->device)
  155. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  156. #define pci_set_consistent_dma_mask(dev, mask) (0)
  157. #endif
  158. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
  159. #define pci_dma_sync_single_for_cpu pci_dma_sync_single
  160. #endif
  161. #ifndef DMA_32BIT_MASK
  162. #define DMA_32BIT_MASK 0x00000000ffffffffULL
  163. #endif
  164. #ifndef htole32
  165. #define htole32(x) cpu_to_le32(x)
  166. #endif
  167. #ifndef htobe32
  168. #define htobe32(x) cpu_to_be32(x)
  169. #endif
  170. #ifndef htole16
  171. #define htole16(x) cpu_to_le16(x)
  172. #endif
  173. #ifndef htobe16
  174. #define htobe16(x) cpu_to_be16(x)
  175. #endif
  176. /* older kernels don't have these */
  177. #include <asm/irq.h>
  178. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  179. #if !defined(IRQ_NONE) && !defined(IRQ_RETVAL)
  180. #define IRQ_NONE
  181. #define IRQ_HANDLED
  182. #define IRQ_WAKE_THREAD
  183. #define IRQ_RETVAL
  184. #define irqreturn_t void
  185. typedef irqreturn_t (*irq_handler_t)(int irq, void *arg, struct pt_regs *regs);
  186. #endif
  187. #ifndef IRQF_SHARED
  188. #define IRQF_SHARED SA_SHIRQ
  189. #endif
  190. #endif
  191. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
  192. # define strlcpy(dest,src,len) \
  193. ({strncpy(dest,src,(len)-1); ((char *)dest)[(len)-1] = '\0'; })
  194. #endif
  195. #ifndef MAX_ERRNO
  196. #define MAX_ERRNO 4095
  197. #endif
  198. #ifndef IS_ERR_VALUE
  199. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,5)
  200. #include <linux/err.h>
  201. #endif
  202. #ifndef IS_ERR_VALUE
  203. #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
  204. #endif
  205. #endif
  206. /*
  207. * common debug for all
  208. */
  209. #if 1
  210. #define dprintk(a...) do { if (debug) printk(a); } while(0)
  211. #else
  212. #define dprintk(a...)
  213. #endif
  214. #ifndef SLAB_ATOMIC
  215. /* Changed in 2.6.20, must use GFP_ATOMIC now */
  216. #define SLAB_ATOMIC GFP_ATOMIC
  217. #endif
  218. /*
  219. * need some additional support for older kernels */
  220. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,2)
  221. #define pci_register_driver_compat(driver, rc) \
  222. do { \
  223. if ((rc) > 0) { \
  224. (rc) = 0; \
  225. } else if (rc == 0) { \
  226. (rc) = -ENODEV; \
  227. } else { \
  228. pci_unregister_driver(driver); \
  229. } \
  230. } while (0)
  231. #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
  232. #define pci_register_driver_compat(driver,rc) ((rc) = (rc) < 0 ? (rc) : 0)
  233. #else
  234. #define pci_register_driver_compat(driver,rc)
  235. #endif
  236. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
  237. #include <linux/mm.h>
  238. #include <asm/scatterlist.h>
  239. static inline void sg_set_page(struct scatterlist *sg, struct page *page,
  240. unsigned int len, unsigned int offset)
  241. {
  242. sg->page = page;
  243. sg->offset = offset;
  244. sg->length = len;
  245. }
  246. static inline void *sg_virt(struct scatterlist *sg)
  247. {
  248. return page_address(sg->page) + sg->offset;
  249. }
  250. #define sg_init_table(sg, n)
  251. #define sg_mark_end(sg)
  252. #endif
  253. #ifndef late_initcall
  254. #define late_initcall(init) module_init(init)
  255. #endif
  256. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4) || !defined(CONFIG_SMP)
  257. #define ocf_for_each_cpu(cpu) for ((cpu) = 0; (cpu) == 0; (cpu)++)
  258. #else
  259. #define ocf_for_each_cpu(cpu) for_each_present_cpu(cpu)
  260. #endif
  261. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
  262. #include <linux/sched.h>
  263. #define kill_proc(p,s,v) send_sig(s,find_task_by_vpid(p),0)
  264. #endif
  265. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4)
  266. struct ocf_thread {
  267. struct task_struct *task;
  268. int (*func)(void *arg);
  269. void *arg;
  270. };
  271. /* thread startup helper func */
  272. static inline int ocf_run_thread(void *arg)
  273. {
  274. struct ocf_thread *t = (struct ocf_thread *) arg;
  275. if (!t)
  276. return -1; /* very bad */
  277. t->task = current;
  278. daemonize();
  279. spin_lock_irq(&current->sigmask_lock);
  280. sigemptyset(&current->blocked);
  281. recalc_sigpending(current);
  282. spin_unlock_irq(&current->sigmask_lock);
  283. return (*t->func)(t->arg);
  284. }
  285. #define kthread_create(f,a,fmt...) \
  286. ({ \
  287. struct ocf_thread t; \
  288. pid_t p; \
  289. t.task = NULL; \
  290. t.func = (f); \
  291. t.arg = (a); \
  292. p = kernel_thread(ocf_run_thread, &t, CLONE_FS|CLONE_FILES); \
  293. while (p != (pid_t) -1 && t.task == NULL) \
  294. schedule(); \
  295. if (t.task) \
  296. snprintf(t.task->comm, sizeof(t.task->comm), fmt); \
  297. (t.task); \
  298. })
  299. #define kthread_bind(t,cpu) /**/
  300. #define kthread_should_stop() (strcmp(current->comm, "stopping") == 0)
  301. #define kthread_stop(t) \
  302. ({ \
  303. strcpy((t)->comm, "stopping"); \
  304. kill_proc((t)->pid, SIGTERM, 1); \
  305. do { \
  306. schedule(); \
  307. } while (kill_proc((t)->pid, SIGTERM, 1) == 0); \
  308. })
  309. #else
  310. #include <linux/kthread.h>
  311. #endif
  312. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0)
  313. #define skb_frag_page(x) ((x)->page)
  314. #endif
  315. #endif /* __KERNEL__ */
  316. /****************************************************************************/
  317. #endif /* _BSD_COMPAT_H_ */