NSCL DDAS  1.0
Support for XIA DDAS at the NSCL
 All Classes Namespaces Files Functions Variables Macros Pages
Plx_sysdep.h
1 #ifndef _PLX_SYSDEP_H_
2 #define _PLX_SYSDEP_H_
3 
4 /*******************************************************************************
5  * Copyright (c) PLX Technology, Inc.
6  *
7  * PLX Technology Inc. licenses this source file under the GNU Lesser General Public
8  * License (LGPL) version 2. This source file may be modified or redistributed
9  * under the terms of the LGPL and without express permission from PLX Technology.
10  *
11  * PLX Technology, Inc. provides this software AS IS, WITHOUT ANY WARRANTY,
12  * EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTY OF
13  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. PLX makes no guarantee
14  * or representations regarding the use of, or the results of the use of,
15  * the software and documentation in terms of correctness, accuracy,
16  * reliability, currentness, or otherwise; and you rely on the software,
17  * documentation and results solely at your own risk.
18  *
19  * IN NO EVENT SHALL PLX BE LIABLE FOR ANY LOSS OF USE, LOSS OF BUSINESS,
20  * LOSS OF PROFITS, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES
21  * OF ANY KIND.
22  *
23  ******************************************************************************/
24 
25 /******************************************************************************
26  *
27  * File Name:
28  *
29  * Plx_sysdep.h
30  *
31  * Description:
32  *
33  * This file is provided to support compatible code between different
34  * Linux kernel versions.
35  *
36  * Revision History:
37  *
38  * 05-01-13 : PLX SDK v7.10
39  *
40  *****************************************************************************/
41 
42 
43 #ifndef LINUX_VERSION_CODE
44  #include <linux/version.h>
45 #endif
46 
47 
48 // Only allow 2.6 and higher kernels
49 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
50  #error "ERROR: Linux kernel versions prior to v2.6 not supported"
51 #endif
52 
53 
54 
55 
56 /***********************************************************
57  * IORESOURCE_MEM_64
58  *
59  * This flag specifies whether a PCI BAR space is 64-bit. The
60  * definition wasn't added until 2.6.31.
61  **********************************************************/
62 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31))
63  #define IORESOURCE_MEM_64 0x00100000
64 #endif
65 
66 
67 
68 
69 /***********************************************************
70  * VM_RESERVED
71  *
72  * This flag was removed starting with 3.7. The recommended
73  * replacement is a combination of two flags.
74  **********************************************************/
75 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0))
76  #define VM_RESERVED (VM_DONTEXPAND | VM_DONTDUMP)
77 #endif
78 
79 
80 
81 
82 /***********************************************************
83  * INIT_WORK & INIT_DELAYED_WORK
84  *
85  * This macro initializes a work structure with the function
86  * to call. In kernel 2.6.20, the 3rd parameter was removed.
87  * It used to be the parameter to the function, but now, the
88  * function is called with a pointer to the work_struct itself.
89  * INIT_DELAYED_WORK was introduced to init the delayed_work struct.
90  **********************************************************/
91 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
92  #define PLX_INIT_WORK INIT_WORK
93  // Red Hat pre-defines this
94  #if !defined(RED_HAT_LINUX_KERNEL)
95  #define INIT_DELAYED_WORK INIT_WORK
96  #endif
97 #else
98  #define PLX_INIT_WORK(work, func, data) INIT_WORK((work), (func))
99 #endif
100 
101 
102 
103 
104 /***********************************************************
105  * ioremap_prot
106  *
107  * This function is supported after 2.6.27. PLX drivers only
108  * used it for probing ACPI tables. In newer kernels, calls
109  * to ioremap() for ACPI locations report errors since the
110  * default flags conflict with kernel mappings.
111  **********************************************************/
112 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27))
113  #define ioremap_prot(addr,size,flags) ioremap((addr), (size))
114 #endif
115 
116 
117 
118 
119 /***********************************************************
120  * sema_init / init_MUTEX
121  *
122  * init_MUTEX replaced by sema_init starting with 2.6.26
123  **********************************************************/
124 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26))
125  #define Plx_sema_init(sem, n) init_MUTEX( (sem) )
126 #else
127  #define Plx_sema_init sema_init
128 #endif
129 
130 
131 
132 
133 /***********************************************************
134  * flush_work
135  *
136  * Flush work queue function not added until 2.6.27
137  **********************************************************/
138 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27))
139  #define Plx_flush_work(x)
140 #else
141  #define Plx_flush_work flush_work
142 #endif
143 
144 
145 
146 
147 /***********************************************************
148  * PLX_DPC_PARAM
149  *
150  * In kernel 2.6.20, the parameter to a work queue function
151  * was made to always be a pointer to the work_struct itself.
152  * In previous kernels, this was always a VOID*. Since
153  * PLX drivers use work queue functions for the DPC/bottom-half
154  * processing, the parameter had to be changed. For cleaner
155  * source code, the definition PLX_DPC_PARAM is used and is
156  * defined below. This also allows 2.4.x compatible source code.
157  **********************************************************/
158 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
159  #define PLX_DPC_PARAM VOID
160 #else
161  #define PLX_DPC_PARAM struct work_struct
162 #endif
163 
164 
165 
166 
167 /***********************************************************
168  * SA_SHIRQ / IRQF_SHARED
169  *
170  * In kernel 2.6.18, the IRQ flag SA_SHIRQ was renamed to
171  * IRQF_SHARED. SA_SHIRQ was deprecated but remained in the
172  * the kernel headers to support older drivers until 2.6.24.
173  **********************************************************/
174 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
175  #define PLX_IRQF_SHARED SA_SHIRQ
176 #else
177  #define PLX_IRQF_SHARED IRQF_SHARED
178 #endif
179 
180 
181 
182 
183 /***********************************************************
184  * pci_find_device / pci_get_device
185  * pci_find_slot / pci_get_bus_and_slot
186  *
187  * In kernel 2.6, pci_find_* was deprecated and replaced
188  * with pci_get_*. The new functions were official in 2.6.19.
189  * If pci_get_* functions are used, the kernel increments the
190  * reference count to the device. To decement the count, the
191  * function pci_dev_put() must be called.
192  **********************************************************/
193 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
194  #define Plx_pci_get_device pci_find_device
195  #define Plx_pci_get_bus_and_slot pci_find_slot
196  #define Plx_pci_dev_put( pPciDev )
197 #else
198  #define Plx_pci_get_device pci_get_device
199  #define Plx_pci_get_bus_and_slot pci_get_bus_and_slot
200  #define Plx_pci_dev_put pci_dev_put
201 #endif
202 
203 
204 
205 
206 /***********************************************************
207  * pci_enable_msi - Added in 2.6.1
208  * pci_disable_msi - Added in 2.6.8
209  **********************************************************/
210 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,1))
211  #define Plx_pci_enable_msi(pDev) (-1)
212 #else
213  #define Plx_pci_enable_msi pci_enable_msi
214 #endif
215 
216 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8))
217  #define Plx_pci_disable_msi(pDev)
218 #else
219  #define Plx_pci_disable_msi pci_disable_msi
220 #endif
221 
222 
223 
224 
225 /***********************************************************
226  * is_virtfn field in pci_dev - Added in 2.6.30
227  *
228  * For SR-IOV devices, there is an 'is_virtfn' field in the
229  * pci_dev structure to report whether the device is a VF. This
230  * field is was also added to updated RedHat kernels.
231  **********************************************************/
232 #if (defined(CONFIG_PCI_IOV) || \
233  (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)) || \
234  ((LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,18)) && defined(RED_HAT_LINUX_KERNEL)))
235  #define Plx_pcie_is_virtfn(pDev) (pDev->is_virtfn)
236 #else
237  #define Plx_pcie_is_virtfn(pDev) (FALSE)
238 #endif
239 
240 
241 
242 
243 /***********************************************************
244  * readq / writeq
245  *
246  * These functions are used to perform 64-bit accesses to
247  * I/O memory. They are not defined for all architectures.
248  * For x86 32-bit, they were added in 2.6.29.
249  *
250  * NOTE: This is still incomplete for non-x86 32-bit architectures
251  * where readq/writeq are not yet defined.
252  **********************************************************/
253 #if ((LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)) && defined(CONFIG_X86_32))
254  // x86 64-bit I/O access functions
255  static inline __u64 readq(const volatile void __iomem *addr)
256  {
257  const volatile u32 __iomem *p = addr;
258  u64 value;
259 
260  value = readl(p);
261  value += ((u64)readl(p + 1) << 32);
262 
263  return value;
264  }
265 
266  static inline void writeq(__u64 val, volatile void __iomem *addr)
267  {
268  writel(val, addr);
269  writel(val >> 32, addr+4);
270  }
271 
272 #elif !defined(CONFIG_64BIT) && !defined(CONFIG_X86_32)
273  // Revert to 32-bit accesses for non-x86/x64 platforms
274  #define readq readl
275  #define writeq writel
276 #endif
277 
278 
279 
280 
281 /***********************************************************
282  * dma_set_mask / pci_set_dma_mask
283  *
284  * This function is used to set the mask for DMA addresses
285  * for the device. In 2.4 kernels, the function is pci_set_dma_mask
286  * and in 2.6 kernels, it has been change to dma_set_mask.
287  * In addition to the name change, the first parameter has
288  * been changed from the PCI device structure in 2.4, to
289  * the device structure found in the PCI device structure.
290  **********************************************************/
291 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
292  #define Plx_dma_set_mask(pdx, mask) \
293  ( \
294  pci_set_dma_mask( \
295  (pdx)->pPciDevice, \
296  (mask) \
297  ) \
298  )
299 #else
300  #define Plx_dma_set_mask(pdx, mask) \
301  ( \
302  dma_set_mask( \
303  &((pdx)->pPciDevice->dev), \
304  (mask) \
305  ) \
306  )
307 #endif
308 
309 
310 
311 
312 /***********************************************************
313  * dma_set_coherent_mask / pci_set_consistent_dma_mask
314  *
315  * This function is used to set the mask for coherent/consistent
316  * DMA buffer allocations. Prior to 2.6.34, the function is
317  * pci_set_consistent_dma_mask. It is now dma_set_coherent_mask.
318  * The first parameter has also been changed from pci_dev
319  * structure to the dev structure found in pci_dev.
320  **********************************************************/
321 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34)
322  #define Plx_dma_set_coherent_mask(pdx, mask) \
323  ( \
324  pci_set_consistent_dma_mask( \
325  (pdx)->pPciDevice, \
326  (mask) \
327  ) \
328  )
329 #else
330  #define Plx_dma_set_coherent_mask(pdx, mask) \
331  ( \
332  dma_set_coherent_mask( \
333  &((pdx)->pPciDevice->dev), \
334  (mask) \
335  ) \
336  )
337 #endif
338 
339 
340 
341 
342 /***********************************************************
343  * DMA_BIT_MASK
344  *
345  * This macro is used to specify bit masks (e.g dma_set_mask).
346  * It was introduced in 2.6.24
347  **********************************************************/
348 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
349  #define PLX_DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
350 #else
351  #define PLX_DMA_BIT_MASK DMA_BIT_MASK
352 #endif
353 
354 
355 
356 
357 /***********************************************************
358  * dma_alloc_coherent & dma_free_coherent
359  *
360  * These functions are used to allocate and map DMA buffers.
361  * In 2.4 kernels, the functions are pci_alloc/free_consistent
362  * and in 2.6 kernels, they have been changed to
363  * dma_alloc/free_coherent. In addition to the name changes,
364  * the first parameter has been changed from the PCI device
365  * structure in 2.4, to the device structure found in the PCI
366  * device structure.
367  **********************************************************/
368 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
369  #define Plx_dma_alloc_coherent(pdx, size, dma_handle, flag) \
370  pci_alloc_consistent( \
371  (pdx)->pPciDevice, \
372  (size), \
373  (dma_handle) \
374  )
375 
376  #define Plx_dma_free_coherent(pdx, size, cpu_addr, dma_handle) \
377  pci_free_consistent( \
378  (pdx)->pPciDevice, \
379  (size), \
380  (cpu_addr), \
381  (dma_handle) \
382  )
383 #else
384  #define Plx_dma_alloc_coherent(pdx, size, dma_handle, flag) \
385  dma_alloc_coherent( \
386  &((pdx)->pPciDevice->dev), \
387  (size), \
388  (dma_handle), \
389  (flag) \
390  )
391 
392  #define Plx_dma_free_coherent(pdx, size, cpu_addr, dma_handle) \
393  dma_free_coherent( \
394  &((pdx)->pPciDevice->dev), \
395  (size), \
396  (cpu_addr), \
397  (dma_handle) \
398  )
399 #endif
400 
401 
402 
403 
404 /***********************************************************
405  * dma_map_page & dma_unmap_page
406  *
407  * These functions are used to map a single user buffer page
408  * in order to get a valid bus address for the page. In 2.4
409  * kernels, the functions are pci_map/unmap_page and in 2.6
410  * kernels, they have been changed to dma_map/unmap_page.
411  * In addition to the name changes, the first parameter has
412  * been changed from the PCI device structure in 2.4, to the
413  * device structure found in the PCI device structure.
414  **********************************************************/
415 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
416  #define Plx_dma_map_page(pdx, page, offset, size, direction) \
417  pci_map_page( \
418  (pdx)->pPciDevice, \
419  (page), \
420  (offset), \
421  (size), \
422  (direction) \
423  )
424 
425  #define Plx_dma_unmap_page(pdx, dma_address, size, direction) \
426  pci_unmap_page( \
427  (pdx)->pPciDevice, \
428  (dma_address), \
429  (size), \
430  (direction) \
431  )
432 #else
433  #define Plx_dma_map_page(pdx, page, offset, size, direction) \
434  dma_map_page( \
435  &((pdx)->pPciDevice->dev), \
436  (page), \
437  (offset), \
438  (size), \
439  (direction) \
440  )
441 
442  #define Plx_dma_unmap_page(pdx, dma_address, size, direction) \
443  dma_unmap_page( \
444  &((pdx)->pPciDevice->dev), \
445  (dma_address), \
446  (size), \
447  (direction) \
448  )
449 #endif
450 
451 
452 
453 
454 /***********************************************************
455  * remap_pfn_range & remap_page_range
456  *
457  * The remap_pfn_range() function was added in kernel 2.6 and
458  * does not exist in previous kernels. For older kernels,
459  * remap_page_range can be used, as it is the same function
460  * except the Page Frame Number (pfn) parameter should
461  * actually be a physical address. For that case, the
462  * pfn is simply shifted by PAGE_SHIFT to obtain the
463  * corresponding physical address.
464  *
465  * remap_pfn_range, however, does not seem to exist in all
466  * kernel 2.6 distributions. remap_page_range was officially
467  * removed in 2.6.11 but declared deprecated in 2.6.10. To keep
468  * things simple, this driver will default to remap_page_range
469  * unless the kernel version is 2.6.10 or greater.
470  *
471  * For 2.4 kernels, remap_pfn_range obviously does not exist.
472  * Although remap_page_range() may be used instead, there
473  * was a parameter added in kernel version 2.5.3. The new
474  * parameter is a pointer to the VMA structure. To make
475  * matters even more complicated, the kernel source in
476  * RedHat 9.0 (v2.4.20-8), however, also uses the new
477  * parameter. As a result, another #define is added if
478  * RedHat 9.0 kernel source is used.
479  *
480  * The #defines below should result in the following usage table:
481  *
482  * kernel function
483  * ====================================================
484  * 2.4.0 -> 2.4.19 remap_page_range (no VMA param)
485  * 2.4.20 -> 2.5.2 (non-RedHat) remap_page_range (no VMA param)
486  * 2.4.20 -> 2.5.2 (RedHat) remap_page_range (with VMA param)
487  * 2.5.3 -> 2.6.9 remap_page_range (with VMA param)
488  * 2.6.10 & up remap_pfn_range
489  *
490  **********************************************************/
491 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10))
492  #if ( (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,3)) || \
493  ((LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,20)) && defined(RED_HAT_LINUX_KERNEL)) )
494 
495  // Revert to remap_page_range
496  #define Plx_remap_pfn_range(vma, virt_addr, pfn, size, prot) \
497  ( \
498  remap_page_range( \
499  (vma), \
500  (virt_addr), \
501  (pfn) << PAGE_SHIFT,\
502  (size), \
503  (prot) \
504  ) \
505  )
506  #else
507  // VMA parameter must be removed
508  #define Plx_remap_pfn_range(vma, virt_addr, pfn, size, prot) \
509  ( \
510  remap_page_range( \
511  (virt_addr), \
512  (pfn) << PAGE_SHIFT,\
513  (size), \
514  (prot) \
515  ) \
516  )
517  #endif
518 #else
519  // Function already defined
520  #define Plx_remap_pfn_range remap_pfn_range
521 #endif
522 
523 
524 
525 
526 /***********************************************************
527  * io_remap_pfn_range & io_remap_page_range
528  *
529  * The io_remap_page_range() function is used to map I/O space
530  * into user mode. Generally, it defaults to remap_page_range,
531  * but on some architectures it performs platform-specific code.
532  *
533  * In kernel 2.6.12, io_remap_page_range was deprecated and
534  * replaced with io_remap_pfn_range.
535  *
536  * Since io_remap_xxx_range usually reverts to remap_xxx_range,
537  * the same issues regarding kernel version apply. Refer to
538  * the explanation above regarding remap_page/pfn_range.
539  *
540  * The #defines below should result in the following usage table:
541  *
542  * kernel function
543  * ====================================================
544  * 2.4.0 -> 2.4.19 io_remap_page_range (no VMA param)
545  * 2.4.20 -> 2.5.2 (non-RedHat) io_remap_page_range (no VMA param)
546  * 2.4.20 -> 2.5.2 (RedHat) io_remap_page_range (with VMA param)
547  * 2.5.3 -> 2.6.11 io_remap_page_range (with VMA param)
548  * 2.6.12 & up io_remap_pfn_range
549  *
550  **********************************************************/
551 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12))
552  #if ( (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,3)) || \
553  ((LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,20)) && defined(RED_HAT_LINUX_KERNEL)) )
554 
555  // Revert to io_remap_page_range (with VMA)
556  #define Plx_io_remap_pfn_range(vma, virt_addr, pfn, size, prot) \
557  ( \
558  io_remap_page_range( \
559  (vma), \
560  (virt_addr), \
561  (pfn) << PAGE_SHIFT,\
562  (size), \
563  (prot) \
564  ) \
565  )
566  #else
567  // Revert to io_remap_page_range (without VMA)
568  #define Plx_io_remap_pfn_range(vma, virt_addr, pfn, size, prot) \
569  ( \
570  io_remap_page_range( \
571  (virt_addr), \
572  (pfn) << PAGE_SHIFT,\
573  (size), \
574  (prot) \
575  ) \
576  )
577  #endif
578 #else
579  // Function already defined
580  #define Plx_io_remap_pfn_range io_remap_pfn_range
581 #endif
582 
583 
584 
585 #endif // _PLX_SYSDEP_H_