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