]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/vxge/vxge-osdep.h
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / dev / vxge / vxge-osdep.h
1 /*-
2  * Copyright(c) 2002-2011 Exar Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification are permitted provided the following conditions are met:
7  *
8  *    1. Redistributions of source code must retain the above copyright notice,
9  *       this list of conditions and the following disclaimer.
10  *
11  *    2. Redistributions in binary form must reproduce the above copyright
12  *       notice, this list of conditions and the following disclaimer in the
13  *       documentation and/or other materials provided with the distribution.
14  *
15  *    3. Neither the name of the Exar Corporation nor the names of its
16  *       contributors may be used to endorse or promote products derived from
17  *       this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*$FreeBSD$*/
32
33 /* LINTLIBRARY */
34
35 #ifndef _VXGE_OSDEP_H_
36 #define _VXGE_OSDEP_H_
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40
41 #if __FreeBSD_version >= 800000
42 #include <sys/buf_ring.h>
43 #endif
44
45 #include <sys/mbuf.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/malloc.h>
49 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/bus.h>
52 #include <sys/sockio.h>
53 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <sys/rman.h>
56 #include <sys/stddef.h>
57 #include <sys/proc.h>
58 #include <sys/endian.h>
59 #include <sys/sysctl.h>
60 #include <sys/pcpu.h>
61 #include <sys/smp.h>
62
63 #include <net/if.h>
64 #include <net/if_arp.h>
65 #include <net/bpf.h>
66 #include <net/ethernet.h>
67 #include <net/if_dl.h>
68 #include <net/if_media.h>
69 #include <net/if_types.h>
70 #include <net/if_var.h>
71 #include <net/if_vlan_var.h>
72
73 #include <netinet/in_systm.h>
74 #include <netinet/in.h>
75 #include <netinet/if_ether.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip6.h>
78 #include <netinet/tcp.h>
79 #include <netinet/tcp_lro.h>
80 #include <netinet/udp.h>
81
82 #include <machine/bus.h>
83 #include <machine/resource.h>
84 #include <machine/clock.h>
85 #include <machine/stdarg.h>
86 #include <machine/in_cksum.h>
87
88 #include <vm/vm.h>
89 #include <vm/pmap.h>
90
91 #include <dev/pci/pcivar.h>
92 #include <dev/pci/pcireg.h>
93 #include <dev/pci/pci_private.h>
94
95 #include <dev/vxge/include/vxge-defs.h>
96
97 /*
98  * ------------------------- includes and defines -------------------------
99  */
100
101 #if BYTE_ORDER == BIG_ENDIAN
102 #define VXGE_OS_HOST_BIG_ENDIAN
103 #else
104 #define VXGE_OS_HOST_LITTLE_ENDIAN
105 #endif
106
107 #if __LONG_BIT == 64
108 #define VXGE_OS_PLATFORM_64BIT
109 #else
110 #define VXGE_OS_PLATFORM_32BIT
111 #endif
112
113 #define VXGE_OS_PCI_CONFIG_SIZE         256
114 #define VXGE_OS_HOST_PAGE_SIZE          4096
115 #define VXGE_LL_IP_FAST_CSUM(hdr, len)  0
116
117 #ifndef __DECONST
118 #define __DECONST(type, var)    ((type)(uintrptr_t)(const void *)(var))
119 #endif
120
121 typedef struct ifnet *ifnet_t;
122 typedef struct mbuf *mbuf_t;
123 typedef struct mbuf *OS_NETSTACK_BUF;
124
125 typedef struct _vxge_bus_res_t {
126
127         u_long                  bus_res_len;
128         bus_space_tag_t         bus_space_tag;          /* DMA Tag */
129         bus_space_handle_t      bus_space_handle;       /* Bus handle */
130         struct resource         *bar_start_addr;        /* BAR address */
131
132 } vxge_bus_res_t;
133
134 typedef struct _vxge_dma_alloc_t {
135
136         bus_addr_t              dma_paddr;              /* Physical Address */
137         caddr_t                 dma_vaddr;              /* Virtual Address */
138         bus_dma_tag_t           dma_tag;                /* DMA Tag */
139         bus_dmamap_t            dma_map;                /* DMA Map */
140         bus_dma_segment_t       dma_segment;            /* DMA Segment */
141         bus_size_t              dma_size;               /* Size */
142         int                     dma_nseg;               /* scatter-gather */
143
144 } vxge_dma_alloc_t;
145
146 typedef struct _vxge_pci_info {
147
148         device_t        ndev;           /* Device */
149         void            *reg_map[3];    /* BAR Resource */
150         struct resource *bar_info[3];   /* BAR tag and handle */
151
152 } vxge_pci_info_t;
153
154 /*
155  * ---------------------- fixed size primitive types -----------------------
156  */
157 typedef size_t                  ptr_t;
158 typedef int8_t                  s8;
159 typedef uint8_t                 u8;
160 typedef uint16_t                u16;
161 typedef int32_t                 s32;
162 typedef uint32_t                u32;
163 typedef unsigned long long int  u64;
164 typedef boolean_t               bool;
165 typedef bus_addr_t              dma_addr_t;
166 typedef struct mtx              spinlock_t;
167 typedef struct resource         *pci_irq_h;
168 typedef vxge_pci_info_t         *pci_dev_h;
169 typedef vxge_pci_info_t         *pci_cfg_h;
170 typedef vxge_bus_res_t          *pci_reg_h;
171 typedef vxge_dma_alloc_t        pci_dma_h;
172 typedef vxge_dma_alloc_t        pci_dma_acc_h;
173
174 /*
175  * -------------------------- "libc" functionality -------------------------
176  */
177 #define vxge_os_curr_time               systime
178 #define vxge_os_strcpy                  strcpy
179 #define vxge_os_strlcpy                 strlcpy
180 #define vxge_os_strlen                  strlen
181 #define vxge_os_sprintf                 sprintf
182 #define vxge_os_snprintf                snprintf
183 #define vxge_os_println(buf)            printf("%s\n", buf)
184 #define vxge_os_memzero                 bzero
185 #define vxge_os_memcmp                  memcmp
186 #define vxge_os_memcpy(dst, src, size)  bcopy(src, dst, size)
187
188 #define vxge_os_timestamp(buff) {                       \
189         struct timeval cur_time;                        \
190         gettimeofday(&cur_time, 0);                     \
191         snprintf(buff, sizeof(buff), "%08li.%08li: ",   \
192                 cur_time.tv_sec, cur_time.tv_usec);     \
193 }
194
195 #define vxge_os_printf(fmt...) {                        \
196         printf(fmt);                                    \
197         printf("\n");                                   \
198 }
199
200 #define vxge_os_vaprintf(fmt...)                        \
201         vxge_os_printf(fmt);
202
203 #define vxge_os_vasprintf(fmt...) {                     \
204         vxge_os_printf(fmt);                            \
205 }
206
207 #define vxge_trace(trace, fmt, args...)                 \
208         vxge_debug_uld(VXGE_COMPONENT_ULD,              \
209                 trace, hldev, vpid, fmt, ## args)
210
211 /*
212  * -------------------- synchronization primitives -------------------------
213  */
214 /* Initialize the spin lock */
215 #define vxge_os_spin_lock_init(lockp, ctxh) {                   \
216         if (mtx_initialized(lockp) == 0)                        \
217                 mtx_init((lockp), "vxge", NULL, MTX_DEF);       \
218 }
219
220 /* Initialize the spin lock (IRQ version) */
221 #define vxge_os_spin_lock_init_irq(lockp, ctxh) {               \
222         if (mtx_initialized(lockp) == 0)                        \
223                 mtx_init((lockp), "vxge", NULL, MTX_DEF);       \
224 }
225
226 /* Destroy the lock */
227 #define vxge_os_spin_lock_destroy(lockp, ctxh) {                \
228         if (mtx_initialized(lockp) != 0)                        \
229                 mtx_destroy(lockp);                             \
230 }
231
232 /* Destroy the lock (IRQ version) */
233 #define vxge_os_spin_lock_destroy_irq(lockp, ctxh) {            \
234         if (mtx_initialized(lockp) != 0)                        \
235                 mtx_destroy(lockp);                             \
236 }
237
238 /* Acquire the lock */
239 #define vxge_os_spin_lock(lockp) {                              \
240         if (mtx_owned(lockp) == 0)                              \
241                 mtx_lock(lockp);                                \
242 }
243
244 /* Release the lock */
245 #define vxge_os_spin_unlock(lockp)      mtx_unlock(lockp)
246
247 /* Acquire the lock (IRQ version) */
248 #define vxge_os_spin_lock_irq(lockp, flags) {                   \
249         flags = MTX_QUIET;                                      \
250         if (mtx_owned(lockp) == 0)                              \
251                 mtx_lock_flags(lockp, flags);                   \
252 }
253
254 /* Release the lock (IRQ version) */
255 #define vxge_os_spin_unlock_irq(lockp, flags) {                 \
256         flags = MTX_QUIET;                                      \
257         mtx_unlock_flags(lockp, flags);                         \
258 }
259
260 /* Write memory barrier */
261 #if __FreeBSD_version < 800000
262 #if defined(__i386__) || defined(__amd64__)
263 #define mb()  __asm volatile("mfence" ::: "memory")
264 #define wmb() __asm volatile("sfence" ::: "memory")
265 #define rmb() __asm volatile("lfence" ::: "memory")
266 #else
267 #define mb()
268 #define rmb()
269 #define wmb()
270 #endif
271 #endif
272
273 #define vxge_os_wmb()           wmb()
274 #define vxge_os_udelay(x)       DELAY(x)
275 #define vxge_os_stall(x)        DELAY(x)
276 #define vxge_os_mdelay(x)       DELAY(x * 1000)
277 #define vxge_os_xchg            (targetp, newval)
278
279 /*
280  * ------------------------- misc primitives -------------------------------
281  */
282 #define vxge_os_be32            u32
283 #define vxge_os_unlikely(x)     (x)
284 #define vxge_os_prefetch(x)     (x = x)
285 #define vxge_os_prefetchw(x)    (x = x)
286 #define vxge_os_bug             vxge_os_printf
287
288 #define vxge_os_ntohs           ntohs
289 #define vxge_os_ntohl           ntohl
290 #define vxge_os_ntohll          be64toh
291
292 #define vxge_os_htons           htons
293 #define vxge_os_htonl           htonl
294 #define vxge_os_htonll          htobe64
295
296 #define vxge_os_in_multicast            IN_MULTICAST
297 #define VXGE_OS_INADDR_BROADCAST        INADDR_BROADCAST
298 /*
299  * -------------------------- compiler stuff ------------------------------
300  */
301 #define __vxge_os_cacheline_size        CACHE_LINE_SIZE
302 #define __vxge_os_attr_cacheline_aligned __aligned(__vxge_os_cacheline_size)
303
304 /*
305  * ---------------------- memory primitives --------------------------------
306  */
307 #if defined(VXGE_OS_MEMORY_CHECK)
308
309 typedef struct _vxge_os_malloc_t {
310
311         u_long          line;
312         u_long          size;
313         void            *ptr;
314         const char      *file;
315
316 } vxge_os_malloc_t;
317
318 #define VXGE_OS_MALLOC_CNT_MAX  64*1024
319
320 extern u32 g_malloc_cnt;
321 extern vxge_os_malloc_t g_malloc_arr[VXGE_OS_MALLOC_CNT_MAX];
322
323 #define VXGE_OS_MEMORY_CHECK_MALLOC(_vaddr, _size, _file, _line) {      \
324         if (_vaddr) {                                                   \
325                 u32 i;                                                  \
326                 for (i = 0; i < g_malloc_cnt; i++) {                    \
327                         if (g_malloc_arr[i].ptr == NULL)                \
328                                 break;                                  \
329                 }                                                       \
330                 if (i == g_malloc_cnt) {                                \
331                         g_malloc_cnt++;                                 \
332                         if (g_malloc_cnt >= VXGE_OS_MALLOC_CNT_MAX) {   \
333                                 vxge_os_bug("g_malloc_cnt exceed %d\n", \
334                                     VXGE_OS_MALLOC_CNT_MAX);            \
335                         } else {                                        \
336                                 g_malloc_arr[i].ptr = _vaddr;           \
337                                 g_malloc_arr[i].size = _size;           \
338                                 g_malloc_arr[i].file = _file;           \
339                                 g_malloc_arr[i].line = _line;           \
340                         }                                               \
341                 }                                                       \
342         }                                                               \
343 }
344
345 #define VXGE_OS_MEMORY_CHECK_FREE(_vaddr, _size, _file, _line) {        \
346         u32 i;                                                          \
347         for (i = 0; i < VXGE_OS_MALLOC_CNT_MAX; i++) {                  \
348                 if (g_malloc_arr[i].ptr == _vaddr) {                    \
349                         g_malloc_arr[i].ptr = NULL;                     \
350                         if (_size && g_malloc_arr[i].size !=  _size) {  \
351                                 vxge_os_printf("freeing wrong size "    \
352                                     "%lu allocated %s:%lu:"             \
353                                     VXGE_OS_LLXFMT":%lu\n",             \
354                                     _size,                              \
355                                     g_malloc_arr[i].file,               \
356                                     g_malloc_arr[i].line,               \
357                                     (u64)(u_long) g_malloc_arr[i].ptr,  \
358                                     g_malloc_arr[i].size);              \
359                         }                                               \
360                         break;                                          \
361                 }                                                       \
362         }                                                               \
363 }
364
365 #else
366 #define VXGE_OS_MEMORY_CHECK_MALLOC(prt, size, file, line)
367 #define VXGE_OS_MEMORY_CHECK_FREE(vaddr, size, file, line)
368 #endif
369
370 static inline void *
371 vxge_mem_alloc_ex(u_long size, const char *file, int line)
372 {
373         void *vaddr = NULL;
374         vaddr = malloc(size, M_DEVBUF, M_ZERO | M_NOWAIT);
375         if (NULL != vaddr) {
376                 VXGE_OS_MEMORY_CHECK_MALLOC((void *)vaddr, size, file, line)
377                 vxge_os_memzero(vaddr, size);
378         }
379
380         return (vaddr);
381 }
382
383 static inline void
384 vxge_mem_free_ex(const void *vaddr, u_long size, const char *file, int line)
385 {
386         if (NULL != vaddr) {
387                 VXGE_OS_MEMORY_CHECK_FREE(vaddr, size, file, line)
388                 free(__DECONST(void *, vaddr), M_DEVBUF);
389         }
390 }
391
392 #define vxge_os_malloc(pdev, size)                      \
393         vxge_mem_alloc_ex(size, __FILE__, __LINE__)
394
395 #define vxge_os_free(pdev, vaddr, size)                 \
396         vxge_mem_free_ex(vaddr, size, __FILE__, __LINE__)
397
398 #define vxge_mem_alloc(size)                            \
399         vxge_mem_alloc_ex(size, __FILE__, __LINE__)
400
401 #define vxge_mem_free(vaddr, size)                      \
402         vxge_mem_free_ex(vaddr, size, __FILE__, __LINE__)
403
404 #define vxge_free_packet(x)                             \
405         if (NULL != x) { m_freem(x); x = NULL; }
406
407 /*
408  * --------------------------- pci primitives ------------------------------
409  */
410 #define vxge_os_pci_read8(pdev, cfgh, where, val)       \
411         (*(val) = pci_read_config(pdev->ndev, where, 1))
412
413 #define vxge_os_pci_write8(pdev, cfgh, where, val)      \
414         pci_write_config(pdev->ndev, where, val, 1)
415
416 #define vxge_os_pci_read16(pdev, cfgh, where, val)      \
417         (*(val) = pci_read_config(pdev->ndev, where, 2))
418
419 #define vxge_os_pci_write16(pdev, cfgh, where, val)     \
420         pci_write_config(pdev->ndev, where, val, 2)
421
422 #define vxge_os_pci_read32(pdev, cfgh, where, val)      \
423         (*(val) = pci_read_config(pdev->ndev, where, 4))
424
425 #define vxge_os_pci_write32(pdev, cfgh, where, val)     \
426         pci_write_config(pdev->ndev, where, val, 4)
427
428 static inline u32
429 vxge_os_pci_res_len(pci_dev_h pdev, pci_reg_h regh)
430 {
431         return (((vxge_bus_res_t *) regh)->bus_res_len);
432 }
433
434 static inline u8
435 vxge_os_pio_mem_read8(pci_dev_h pdev, pci_reg_h regh, void *addr)
436 {
437         caddr_t vaddr =
438             (caddr_t) (((vxge_bus_res_t *) (regh))->bar_start_addr);
439
440         return bus_space_read_1(((vxge_bus_res_t *) regh)->bus_space_tag,
441                                 ((vxge_bus_res_t *) regh)->bus_space_handle,
442                                 (bus_size_t) ((caddr_t) (addr) - vaddr));
443 }
444
445 static inline u16
446 vxge_os_pio_mem_read16(pci_dev_h pdev, pci_reg_h regh, void *addr)
447 {
448         caddr_t vaddr =
449             (caddr_t) (((vxge_bus_res_t *) (regh))->bar_start_addr);
450
451         return bus_space_read_2(((vxge_bus_res_t *) regh)->bus_space_tag,
452                                 ((vxge_bus_res_t *) regh)->bus_space_handle,
453                                 (bus_size_t) ((caddr_t) (addr) - vaddr));
454 }
455
456 static inline u32
457 vxge_os_pio_mem_read32(pci_dev_h pdev, pci_reg_h regh, void *addr)
458 {
459         caddr_t vaddr =
460             (caddr_t) (((vxge_bus_res_t *) (regh))->bar_start_addr);
461
462         return bus_space_read_4(((vxge_bus_res_t *) regh)->bus_space_tag,
463                                 ((vxge_bus_res_t *) regh)->bus_space_handle,
464                                 (bus_size_t) ((caddr_t) (addr) - vaddr));
465 }
466
467 static inline u64
468 vxge_os_pio_mem_read64(pci_dev_h pdev, pci_reg_h regh, void *addr)
469 {
470         u64 val, val_l, val_u;
471
472         caddr_t vaddr =
473             (caddr_t) (((vxge_bus_res_t *) (regh))->bar_start_addr);
474
475         val_l = bus_space_read_4(((vxge_bus_res_t *) regh)->bus_space_tag,
476                                 ((vxge_bus_res_t *) regh)->bus_space_handle,
477                                 (bus_size_t) (((caddr_t) addr) + 4 - vaddr));
478
479         val_u = bus_space_read_4(((vxge_bus_res_t *) regh)->bus_space_tag,
480                                 ((vxge_bus_res_t *) regh)->bus_space_handle,
481                                 (bus_size_t) ((caddr_t) (addr) - vaddr));
482
483         val = ((val_l << 32) | val_u);
484         return (val);
485 }
486
487 static inline void
488 vxge_os_pio_mem_write8(pci_dev_h pdev, pci_reg_h regh, u8 val, void *addr)
489 {
490         caddr_t vaddr =
491             (caddr_t) (((vxge_bus_res_t *) regh)->bar_start_addr);
492
493         bus_space_write_1(((vxge_bus_res_t *) regh)->bus_space_tag,
494                         ((vxge_bus_res_t *) regh)->bus_space_handle,
495                         (bus_size_t) ((caddr_t) (addr) - vaddr), val);
496 }
497
498 static inline void
499 vxge_os_pio_mem_write16(pci_dev_h pdev, pci_reg_h regh, u16 val, void *addr)
500 {
501         caddr_t vaddr =
502             (caddr_t) (((vxge_bus_res_t *) (regh))->bar_start_addr);
503
504         bus_space_write_2(((vxge_bus_res_t *) regh)->bus_space_tag,
505                         ((vxge_bus_res_t *) regh)->bus_space_handle,
506                         (bus_size_t) ((caddr_t) (addr) - vaddr), val);
507 }
508
509 static inline void
510 vxge_os_pio_mem_write32(pci_dev_h pdev, pci_reg_h regh, u32 val, void *addr)
511 {
512         caddr_t vaddr =
513             (caddr_t) (((vxge_bus_res_t *) (regh))->bar_start_addr);
514
515         bus_space_write_4(((vxge_bus_res_t *) regh)->bus_space_tag,
516                         ((vxge_bus_res_t *) regh)->bus_space_handle,
517                         (bus_size_t) ((caddr_t) (addr) - vaddr), val);
518 }
519
520 static inline void
521 vxge_os_pio_mem_write64(pci_dev_h pdev, pci_reg_h regh, u64 val, void *addr)
522 {
523         u32 val_l = (u32) (val & 0xffffffff);
524         u32 val_u = (u32) (val >> 32);
525
526         vxge_os_pio_mem_write32(pdev, regh, val_l, addr);
527         vxge_os_pio_mem_write32(pdev, regh, val_u, (caddr_t) addr + 4);
528 }
529
530 #define vxge_os_flush_bridge    vxge_os_pio_mem_read64
531
532 /*
533  * --------------------------- dma primitives -----------------------------
534  */
535 #define VXGE_OS_DMA_DIR_TODEVICE        0
536 #define VXGE_OS_DMA_DIR_FROMDEVICE      1
537 #define VXGE_OS_DMA_DIR_BIDIRECTIONAL   2
538 #define VXGE_OS_INVALID_DMA_ADDR        ((bus_addr_t)0)
539
540 static void
541 vxge_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
542 {
543         if (error)
544                 return;
545
546         *(bus_addr_t *) arg = segs->ds_addr;
547 }
548
549 static inline void *
550 vxge_os_dma_malloc(pci_dev_h pdev, u_long bytes, int dma_flags,
551     pci_dma_h * p_dmah, pci_dma_acc_h * p_dma_acch)
552 {
553         int error = 0;
554         bus_addr_t bus_addr = BUS_SPACE_MAXADDR;
555         bus_size_t boundary, max_size, alignment = PAGE_SIZE;
556
557         if (bytes > PAGE_SIZE) {
558                 boundary = 0;
559                 max_size = bytes;
560         } else {
561                 boundary = PAGE_SIZE;
562                 max_size = PAGE_SIZE;
563         }
564
565         error = bus_dma_tag_create(
566             bus_get_dma_tag(pdev->ndev),        /* Parent */
567             alignment,                          /* Alignment */
568             boundary,                           /* Bounds */
569             bus_addr,                           /* Low Address */
570             bus_addr,                           /* High Address */
571             NULL,                               /* Filter Func */
572             NULL,                               /* Filter Func Argument */
573             bytes,                              /* Maximum Size */
574             1,                                  /* Number of Segments */
575             max_size,                           /* Maximum Segment Size */
576             BUS_DMA_ALLOCNOW,                   /* Flags */
577             NULL,                               /* Lock Func */
578             NULL,                               /* Lock Func Arguments */
579             &(p_dmah->dma_tag));                /* DMA Tag */
580
581         if (error != 0) {
582                 device_printf(pdev->ndev, "bus_dma_tag_create failed\n");
583                 goto _exit0;
584         }
585
586         p_dmah->dma_size = bytes;
587         error = bus_dmamem_alloc(p_dmah->dma_tag, (void **)&p_dmah->dma_vaddr,
588             (BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT),
589             &p_dmah->dma_map);
590         if (error != 0) {
591                 device_printf(pdev->ndev, "bus_dmamem_alloc failed\n");
592                 goto _exit1;
593         }
594
595         VXGE_OS_MEMORY_CHECK_MALLOC(p_dmah->dma_vaddr, p_dmah->dma_size,
596             __FILE__, __LINE__);
597
598         return (p_dmah->dma_vaddr);
599
600 _exit1:
601         bus_dma_tag_destroy(p_dmah->dma_tag);
602 _exit0:
603         return (NULL);
604 }
605
606 static inline void
607 vxge_dma_free(pci_dev_h pdev, const void *vaddr, u_long size,
608     pci_dma_h *p_dmah, pci_dma_acc_h *p_dma_acch,
609     const char *file, int line)
610 {
611         VXGE_OS_MEMORY_CHECK_FREE(p_dmah->dma_vaddr, size, file, line)
612
613         bus_dmamem_free(p_dmah->dma_tag, p_dmah->dma_vaddr, p_dmah->dma_map);
614         bus_dma_tag_destroy(p_dmah->dma_tag);
615
616         p_dmah->dma_map = NULL;
617         p_dmah->dma_tag = NULL;
618         p_dmah->dma_vaddr = NULL;
619 }
620
621 extern void
622 vxge_hal_blockpool_block_add(void *, void *, u32, pci_dma_h *, pci_dma_acc_h *);
623
624 static inline void
625 vxge_os_dma_malloc_async(pci_dev_h pdev, void *devh,
626     u_long size, int dma_flags)
627 {
628         pci_dma_h dma_h;
629         pci_dma_acc_h acc_handle;
630
631         void *block_addr = NULL;
632
633         block_addr = vxge_os_dma_malloc(pdev, size, dma_flags,
634             &dma_h, &acc_handle);
635
636         vxge_hal_blockpool_block_add(devh, block_addr, size,
637             &dma_h, &acc_handle);
638 }
639
640 static inline void
641 vxge_os_dma_sync(pci_dev_h pdev, pci_dma_h dmah, dma_addr_t dma_paddr,
642     u64 dma_offset, size_t length, int dir)
643 {
644         bus_dmasync_op_t dmasync_op;
645
646         switch (dir) {
647         case VXGE_OS_DMA_DIR_TODEVICE:
648                 dmasync_op = BUS_DMASYNC_PREWRITE | BUS_DMASYNC_POSTWRITE;
649                 break;
650
651         case VXGE_OS_DMA_DIR_FROMDEVICE:
652                 dmasync_op = BUS_DMASYNC_PREREAD | BUS_DMASYNC_POSTREAD;
653                 break;
654
655         default:
656         case VXGE_OS_DMA_DIR_BIDIRECTIONAL:
657                 dmasync_op = BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE;
658                 break;
659         }
660
661         bus_dmamap_sync(dmah.dma_tag, dmah.dma_map, dmasync_op);
662 }
663
664 static inline dma_addr_t
665 vxge_os_dma_map(pci_dev_h pdev, pci_dma_h dmah, void *vaddr, u_long size,
666                 int dir, int dma_flags)
667 {
668         int error;
669
670         error = bus_dmamap_load(dmah.dma_tag, dmah.dma_map, dmah.dma_vaddr,
671             dmah.dma_size, vxge_dmamap_cb, &(dmah.dma_paddr), BUS_DMA_NOWAIT);
672
673         if (error != 0)
674                 return (VXGE_OS_INVALID_DMA_ADDR);
675
676         dmah.dma_size = size;
677         return (dmah.dma_paddr);
678 }
679
680 static inline void
681 vxge_os_dma_unmap(pci_dev_h pdev, pci_dma_h dmah, dma_addr_t dma_paddr,
682     u32 size, int dir)
683 {
684         bus_dmamap_unload(dmah.dma_tag, dmah.dma_map);
685 }
686
687 #define vxge_os_dma_free(pdev, vaddr, size, dma_flags, p_dma_acch, p_dmah)  \
688         vxge_dma_free(pdev, vaddr, size, p_dma_acch, p_dmah,                \
689                 __FILE__, __LINE__)
690
691 static inline int
692 vxge_os_is_my_packet(void *pdev, unsigned long addr)
693 {
694         return (0);
695 }
696
697 #endif /* _VXGE_OSDEP_H_ */