ocf-compat.h 7.8 KB

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