ocf-compat.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 <[email protected]>
  8. * Copyright (C) 2010 David McCullough <[email protected]>
  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 !defined(IRQ_NONE) && !defined(IRQ_RETVAL)
  179. #define IRQ_NONE
  180. #define IRQ_HANDLED
  181. #define IRQ_WAKE_THREAD
  182. #define IRQ_RETVAL
  183. #define irqreturn_t void
  184. typedef irqreturn_t (*irq_handler_t)(int irq, void *arg, struct pt_regs *regs);
  185. #endif
  186. #ifndef IRQF_SHARED
  187. #define IRQF_SHARED SA_SHIRQ
  188. #endif
  189. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
  190. # define strlcpy(dest,src,len) \
  191. ({strncpy(dest,src,(len)-1); ((char *)dest)[(len)-1] = '\0'; })
  192. #endif
  193. #ifndef MAX_ERRNO
  194. #define MAX_ERRNO 4095
  195. #endif
  196. #ifndef IS_ERR_VALUE
  197. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,5)
  198. #include <linux/err.h>
  199. #endif
  200. #ifndef IS_ERR_VALUE
  201. #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
  202. #endif
  203. #endif
  204. /*
  205. * common debug for all
  206. */
  207. #if 1
  208. #define dprintk(a...) do { if (debug) printk(a); } while(0)
  209. #else
  210. #define dprintk(a...)
  211. #endif
  212. #ifndef SLAB_ATOMIC
  213. /* Changed in 2.6.20, must use GFP_ATOMIC now */
  214. #define SLAB_ATOMIC GFP_ATOMIC
  215. #endif
  216. /*
  217. * need some additional support for older kernels */
  218. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,2)
  219. #define pci_register_driver_compat(driver, rc) \
  220. do { \
  221. if ((rc) > 0) { \
  222. (rc) = 0; \
  223. } else if (rc == 0) { \
  224. (rc) = -ENODEV; \
  225. } else { \
  226. pci_unregister_driver(driver); \
  227. } \
  228. } while (0)
  229. #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
  230. #define pci_register_driver_compat(driver,rc) ((rc) = (rc) < 0 ? (rc) : 0)
  231. #else
  232. #define pci_register_driver_compat(driver,rc)
  233. #endif
  234. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
  235. #include <linux/mm.h>
  236. #include <asm/scatterlist.h>
  237. static inline void sg_set_page(struct scatterlist *sg, struct page *page,
  238. unsigned int len, unsigned int offset)
  239. {
  240. sg->page = page;
  241. sg->offset = offset;
  242. sg->length = len;
  243. }
  244. static inline void *sg_virt(struct scatterlist *sg)
  245. {
  246. return page_address(sg->page) + sg->offset;
  247. }
  248. #define sg_init_table(sg, n)
  249. #define sg_mark_end(sg)
  250. #endif
  251. #ifndef late_initcall
  252. #define late_initcall(init) module_init(init)
  253. #endif
  254. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4) || !defined(CONFIG_SMP)
  255. #define ocf_for_each_cpu(cpu) for ((cpu) = 0; (cpu) == 0; (cpu)++)
  256. #else
  257. #define ocf_for_each_cpu(cpu) for_each_present_cpu(cpu)
  258. #endif
  259. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
  260. #include <linux/sched.h>
  261. #define kill_proc(p,s,v) send_sig(s,find_task_by_vpid(p),0)
  262. #endif
  263. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,4)
  264. struct ocf_thread {
  265. struct task_struct *task;
  266. int (*func)(void *arg);
  267. void *arg;
  268. };
  269. /* thread startup helper func */
  270. static inline int ocf_run_thread(void *arg)
  271. {
  272. struct ocf_thread *t = (struct ocf_thread *) arg;
  273. if (!t)
  274. return -1; /* very bad */
  275. t->task = current;
  276. daemonize();
  277. spin_lock_irq(&current->sigmask_lock);
  278. sigemptyset(&current->blocked);
  279. recalc_sigpending(current);
  280. spin_unlock_irq(&current->sigmask_lock);
  281. return (*t->func)(t->arg);
  282. }
  283. #define kthread_create(f,a,fmt...) \
  284. ({ \
  285. struct ocf_thread t; \
  286. pid_t p; \
  287. t.task = NULL; \
  288. t.func = (f); \
  289. t.arg = (a); \
  290. p = kernel_thread(ocf_run_thread, &t, CLONE_FS|CLONE_FILES); \
  291. while (p != (pid_t) -1 && t.task == NULL) \
  292. schedule(); \
  293. if (t.task) \
  294. snprintf(t.task->comm, sizeof(t.task->comm), fmt); \
  295. (t.task); \
  296. })
  297. #define kthread_bind(t,cpu) /**/
  298. #define kthread_should_stop() (strcmp(current->comm, "stopping") == 0)
  299. #define kthread_stop(t) \
  300. ({ \
  301. strcpy((t)->comm, "stopping"); \
  302. kill_proc((t)->pid, SIGTERM, 1); \
  303. do { \
  304. schedule(); \
  305. } while (kill_proc((t)->pid, SIGTERM, 1) == 0); \
  306. })
  307. #else
  308. #include <linux/kthread.h>
  309. #endif
  310. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0)
  311. #define skb_frag_page(x) ((x)->page)
  312. #endif
  313. #endif /* __KERNEL__ */
  314. /****************************************************************************/
  315. #endif /* _BSD_COMPAT_H_ */