2 * Copyright (c) 2002-2007 Neterion, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Includes and defines
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/protosw.h>
39 #include <sys/socket.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
45 #include <sys/mutex.h>
47 #include <sys/stddef.h>
48 #include <sys/types.h>
49 #include <sys/sockio.h>
51 #include <sys/mutex.h>
52 #include <sys/types.h>
53 #include <sys/endian.h>
54 #include <sys/sysctl.h>
55 #include <sys/endian.h>
56 #include <sys/socket.h>
57 #include <machine/bus.h>
58 #include <machine/resource.h>
59 #include <machine/clock.h>
62 #include <dev/pci/pcivar.h>
63 #include <dev/pci/pcireg.h>
64 #include <dev/pci/pci_private.h>
66 #include <net/if_arp.h>
67 #include <net/ethernet.h>
68 #include <net/if_dl.h>
69 #include <net/if_media.h>
70 #include <net/if_var.h>
72 #include <net/if_types.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/in.h>
75 #include <netinet/ip.h>
76 #include <netinet/tcp.h>
78 #define XGE_OS_PLATFORM_64BIT
80 #if BYTE_ORDER == BIG_ENDIAN
81 #define XGE_OS_HOST_BIG_ENDIAN
82 #elif BYTE_ORDER == LITTLE_ENDIAN
83 #define XGE_OS_HOST_LITTLE_ENDIAN
86 #define XGE_HAL_USE_5B_MODE
88 #ifdef XGE_TRACE_ASSERT
89 #undef XGE_TRACE_ASSERT
92 #define OS_NETSTACK_BUF struct mbuf *
93 #define XGE_LL_IP_FAST_CSUM(hdr, len) 0
96 #define __DECONST(type, var) ((type)(uintrptr_t)(const void *)(var))
99 #define xge_os_ntohs ntohs
100 #define xge_os_ntohl ntohl
101 #define xge_os_htons htons
102 #define xge_os_htonl htonl
104 typedef struct xge_bus_resource_t {
105 bus_space_tag_t bus_tag; /* DMA Tag */
106 bus_space_handle_t bus_handle; /* Bus handle */
107 struct resource *bar_start_addr;/* BAR start address */
108 } xge_bus_resource_t;
110 typedef struct xge_dma_alloc_t {
111 bus_addr_t dma_phyaddr; /* Physical Address */
112 caddr_t dma_viraddr; /* Virtual Address */
113 bus_dma_tag_t dma_tag; /* DMA Tag */
114 bus_dmamap_t dma_map; /* DMA Map */
115 bus_dma_segment_t dma_segment; /* DMA Segment */
116 bus_size_t dma_size; /* Size */
117 int dma_nseg; /* Maximum scatter-gather segs. */
120 typedef struct xge_dma_mbuf_t {
121 bus_addr_t dma_phyaddr; /* Physical Address */
122 bus_dmamap_t dma_map; /* DMA Map */
125 typedef struct xge_pci_info {
126 device_t device; /* Device */
127 struct resource *regmap0; /* Resource for BAR0 */
128 struct resource *regmap1; /* Resource for BAR1 */
129 void *bar0resource; /* BAR0 tag and handle */
130 void *bar1resource; /* BAR1 tag and handle */
135 * Fixed size primitive types
141 #define ulong_t unsigned long
142 #define uint unsigned int
143 #define ptrdiff_t ptrdiff_t
144 typedef bus_addr_t dma_addr_t;
145 typedef struct mtx spinlock_t;
146 typedef xge_pci_info_t *pci_dev_h;
147 typedef xge_bus_resource_t *pci_reg_h;
148 typedef xge_dma_alloc_t pci_dma_h;
149 typedef xge_dma_alloc_t pci_dma_acc_h;
150 typedef struct resource *pci_irq_h;
151 typedef xge_pci_info_t *pci_cfg_h;
154 * "libc" functionality
156 #define xge_os_memzero(addr, size) bzero(addr, size)
157 #define xge_os_memcpy(dst, src, size) bcopy(src, dst, size)
158 #define xge_os_memcmp memcmp
159 #define xge_os_strcpy strcpy
160 #define xge_os_strlen strlen
161 #define xge_os_snprintf snprintf
162 #define xge_os_sprintf sprintf
163 #define xge_os_printf(fmt...) { \
168 #define xge_os_vaprintf(fmt) { \
169 sprintf(fmt, fmt, "\n"); \
176 #define xge_os_vasprintf(buf, fmt) { \
179 (void) vaprintf(buf, fmt, va); \
183 #define xge_os_timestamp(buf) { \
184 struct timeval current_time; \
185 gettimeofday(¤t_time, 0); \
186 sprintf(buf, "%08li.%08li: ", current_time.tv_sec, \
187 current_time.tv_usec); \
190 #define xge_os_println xge_os_printf
193 * Synchronization Primitives
195 /* Initialize the spin lock */
196 #define xge_os_spin_lock_init(lockp, ctxh) { \
197 if(mtx_initialized(lockp) == 0) { \
198 mtx_init((lockp), "xge", NULL, MTX_DEF); \
202 /* Initialize the spin lock (IRQ version) */
203 #define xge_os_spin_lock_init_irq(lockp, ctxh) { \
204 if(mtx_initialized(lockp) == 0) { \
205 mtx_init((lockp), "xge", NULL, MTX_DEF); \
209 /* Destroy the lock */
210 #define xge_os_spin_lock_destroy(lockp, ctxh) { \
211 if(mtx_initialized(lockp) != 0) { \
212 mtx_destroy(lockp); \
216 /* Destroy the lock (IRQ version) */
217 #define xge_os_spin_lock_destroy_irq(lockp, ctxh) { \
218 if(mtx_initialized(lockp) != 0) { \
219 mtx_destroy(lockp); \
223 /* Acquire the lock */
224 #define xge_os_spin_lock(lockp) { \
225 if(mtx_owned(lockp) == 0) mtx_lock(lockp); \
228 /* Release the lock */
229 #define xge_os_spin_unlock(lockp) { \
233 /* Acquire the lock (IRQ version) */
234 #define xge_os_spin_lock_irq(lockp, flags) { \
236 if(mtx_owned(lockp) == 0) mtx_lock_flags(lockp, flags); \
239 /* Release the lock (IRQ version) */
240 #define xge_os_spin_unlock_irq(lockp, flags) { \
242 mtx_unlock_flags(lockp, flags); \
245 /* Write memory barrier */
248 /* Delay (in micro seconds) */
249 #define xge_os_udelay(us) DELAY(us)
251 /* Delay (in milli seconds) */
252 #define xge_os_mdelay(ms) DELAY(ms * 1000)
254 /* Compare and exchange */
255 //#define xge_os_cmpxchg(targetp, cmd, newval)
260 #define xge_os_unlikely(x) (x)
261 #define xge_os_prefetch(x) (x=x)
262 #define xge_os_prefetchw(x) (x=x)
263 #define xge_os_bug(fmt...) printf(fmt)
264 #define xge_os_htohs ntohs
265 #define xge_os_ntohl ntohl
266 #define xge_os_htons htons
267 #define xge_os_htonl htonl
272 #define __xge_os_attr_cacheline_aligned
273 #define __xge_os_cacheline_size 32
278 #define XGE_OS_INVALID_DMA_ADDR ((dma_addr_t)0)
282 * Allocate non DMA-able memory.
283 * @pdev: Device context.
284 * @size: Size to allocate.
286 * Allocate @size bytes of memory. This allocation can sleep, and therefore,
287 * and therefore it requires process context. In other words, xge_os_malloc()
288 * cannot be called from the interrupt context. Use xge_os_free() to free the
291 * Returns: Pointer to allocated memory, NULL - on failure.
293 * See also: xge_os_free().
296 xge_os_malloc(pci_dev_h pdev, unsigned long size) {
297 void *vaddr = malloc((size), M_DEVBUF, M_NOWAIT | M_ZERO);
299 XGE_OS_MEMORY_CHECK_MALLOC(vaddr, size, __FILE__, __LINE__);
300 xge_os_memzero(vaddr, size);
307 * Free non DMA-able memory.
308 * @pdev: Device context.
309 * @vaddr: Address of the allocated memory block.
310 * @size: Some OS's require to provide size on free
312 * Free the memory area obtained via xge_os_malloc(). This call may also sleep,
313 * and therefore it cannot be used inside interrupt.
315 * See also: xge_os_malloc().
318 xge_os_free(pci_dev_h pdev, const void *vaddr, unsigned long size) {
319 XGE_OS_MEMORY_CHECK_FREE(vaddr, size);
320 free(__DECONST(void *, vaddr), M_DEVBUF);
324 xge_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) {
326 *(bus_addr_t *) arg = segs->ds_addr;
332 * Allocate DMA-able memory.
333 * @pdev: Device context. Used to allocate/pin/map/unmap DMA-able memory.
334 * @size: Size (in bytes) to allocate.
335 * @dma_flags: XGE_OS_DMA_CACHELINE_ALIGNED, XGE_OS_DMA_STREAMING,
336 * XGE_OS_DMA_CONSISTENT (Note that the last two flags are mutually exclusive.)
337 * @p_dmah: Handle used to map the memory onto the corresponding device memory
338 * space. See xge_os_dma_map(). The handle is an out-parameter returned by the
340 * @p_dma_acch: One more DMA handle used subsequently to free the DMA object
341 * (via xge_os_dma_free()).
343 * Allocate DMA-able contiguous memory block of the specified @size. This memory
344 * can be subsequently freed using xge_os_dma_free().
345 * Note: can be used inside interrupt context.
347 * Returns: Pointer to allocated memory(DMA-able), NULL on failure.
350 xge_os_dma_malloc(pci_dev_h pdev, unsigned long size, int dma_flags,
351 pci_dma_h *p_dmah, pci_dma_acc_h *p_dma_acch) {
352 int retValue = bus_dma_tag_create(
353 bus_get_dma_tag(pdev->device), /* Parent */
354 PAGE_SIZE, /* Alignment no specific alignment */
356 BUS_SPACE_MAXADDR, /* Low Address */
357 BUS_SPACE_MAXADDR, /* High Address */
359 NULL, /* Filter arg */
362 size, /* max segment size */
363 BUS_DMA_ALLOCNOW, /* Flags */
364 NULL, /* lockfunction */
366 &p_dmah->dma_tag); /* DMA tag */
368 xge_os_printf("bus_dma_tag_create failed\n")
371 p_dmah->dma_size = size;
372 retValue = bus_dmamem_alloc(p_dmah->dma_tag,
373 (void **)&p_dmah->dma_viraddr, BUS_DMA_NOWAIT, &p_dmah->dma_map);
375 xge_os_printf("bus_dmamem_alloc failed\n")
378 XGE_OS_MEMORY_CHECK_MALLOC(p_dmah->dma_viraddr, p_dmah->dma_size,
380 return(p_dmah->dma_viraddr);
382 fail_2: bus_dma_tag_destroy(p_dmah->dma_tag);
383 fail_1: return(NULL);
388 * Free previously allocated DMA-able memory.
389 * @pdev: Device context. Used to allocate/pin/map/unmap DMA-able memory.
390 * @vaddr: Virtual address of the DMA-able memory.
391 * @p_dma_acch: DMA handle used to free the resource.
392 * @p_dmah: DMA handle used for mapping. See xge_os_dma_malloc().
394 * Free DMA-able memory originally allocated by xge_os_dma_malloc().
395 * Note: can be used inside interrupt.
396 * See also: xge_os_dma_malloc().
399 xge_os_dma_free(pci_dev_h pdev, const void *vaddr, int size,
400 pci_dma_acc_h *p_dma_acch, pci_dma_h *p_dmah)
402 XGE_OS_MEMORY_CHECK_FREE(p_dmah->dma_viraddr, size);
403 bus_dmamem_free(p_dmah->dma_tag, p_dmah->dma_viraddr, p_dmah->dma_map);
404 bus_dma_tag_destroy(p_dmah->dma_tag);
405 p_dmah->dma_map = NULL;
406 p_dmah->dma_tag = NULL;
407 p_dmah->dma_viraddr = NULL;
412 * IO/PCI/DMA Primitives
414 #define XGE_OS_DMA_DIR_TODEVICE 0
415 #define XGE_OS_DMA_DIR_FROMDEVICE 1
416 #define XGE_OS_DMA_DIR_BIDIRECTIONAL 2
420 * Read one byte from device PCI configuration.
421 * @pdev: Device context. Some OSs require device context to perform PIO and/or
423 * @cfgh: PCI configuration space handle.
424 * @where: Offset in the PCI configuration space.
425 * @val: Address of the result.
427 * Read byte value from the specified @regh PCI configuration space at the
428 * specified offset = @where.
429 * Returns: 0 - success, non-zero - failure.
431 #define xge_os_pci_read8(pdev, cfgh, where, val) \
432 (*(val) = pci_read_config(pdev->device, where, 1))
436 * Write one byte into device PCI configuration.
437 * @pdev: Device context. Some OSs require device context to perform PIO and/or
439 * @cfgh: PCI configuration space handle.
440 * @where: Offset in the PCI configuration space.
441 * @val: Value to write.
443 * Write byte value into the specified PCI configuration space
444 * Returns: 0 - success, non-zero - failure.
446 #define xge_os_pci_write8(pdev, cfgh, where, val) \
447 pci_write_config(pdev->device, where, val, 1)
451 * Read 16bit word from device PCI configuration.
452 * @pdev: Device context.
453 * @cfgh: PCI configuration space handle.
454 * @where: Offset in the PCI configuration space.
455 * @val: Address of the 16bit result.
457 * Read 16bit value from the specified PCI configuration space at the
459 * Returns: 0 - success, non-zero - failure.
461 #define xge_os_pci_read16(pdev, cfgh, where, val) \
462 (*(val) = pci_read_config(pdev->device, where, 2))
466 * Write 16bit word into device PCI configuration.
467 * @pdev: Device context.
468 * @cfgh: PCI configuration space handle.
469 * @where: Offset in the PCI configuration space.
470 * @val: Value to write.
472 * Write 16bit value into the specified @offset in PCI configuration space.
473 * Returns: 0 - success, non-zero - failure.
475 #define xge_os_pci_write16(pdev, cfgh, where, val) \
476 pci_write_config(pdev->device, where, val, 2)
480 * Read 32bit word from device PCI configuration.
481 * @pdev: Device context.
482 * @cfgh: PCI configuration space handle.
483 * @where: Offset in the PCI configuration space.
484 * @val: Address of 32bit result.
486 * Read 32bit value from the specified PCI configuration space at the
488 * Returns: 0 - success, non-zero - failure.
490 #define xge_os_pci_read32(pdev, cfgh, where, val) \
491 (*(val) = pci_read_config(pdev->device, where, 4))
495 * Write 32bit word into device PCI configuration.
496 * @pdev: Device context.
497 * @cfgh: PCI configuration space handle.
498 * @where: Offset in the PCI configuration space.
499 * @val: Value to write.
501 * Write 32bit value into the specified @offset in PCI configuration space.
502 * Returns: 0 - success, non-zero - failure.
504 #define xge_os_pci_write32(pdev, cfgh, where, val) \
505 pci_write_config(pdev->device, where, val, 4)
508 * xge_os_pio_mem_read8
509 * Read 1 byte from device memory mapped space.
510 * @pdev: Device context.
511 * @regh: PCI configuration space handle.
512 * @addr: Address in device memory space.
514 * Returns: 1 byte value read from the specified (mapped) memory space address.
517 xge_os_pio_mem_read8(pci_dev_h pdev, pci_reg_h regh, void *addr)
519 bus_space_tag_t tag =
520 (bus_space_tag_t)(((xge_bus_resource_t *)regh)->bus_tag);
521 bus_space_handle_t handle =
522 (bus_space_handle_t)(((xge_bus_resource_t *)regh)->bus_handle);
523 caddr_t addrss = (caddr_t)
524 (((xge_bus_resource_t *)(regh))->bar_start_addr);
526 return bus_space_read_1(tag, handle, (caddr_t)(addr) - addrss);
530 * xge_os_pio_mem_write8
531 * Write 1 byte into device memory mapped space.
532 * @pdev: Device context.
533 * @regh: PCI configuration space handle.
534 * @val: Value to write.
535 * @addr: Address in device memory space.
537 * Write byte value into the specified (mapped) device memory space.
540 xge_os_pio_mem_write8(pci_dev_h pdev, pci_reg_h regh, u8 val, void *addr)
542 bus_space_tag_t tag =
543 (bus_space_tag_t)(((xge_bus_resource_t *)regh)->bus_tag);
544 bus_space_handle_t handle =
545 (bus_space_handle_t)(((xge_bus_resource_t *)regh)->bus_handle);
546 caddr_t addrss = (caddr_t)
547 (((xge_bus_resource_t *)(regh))->bar_start_addr);
549 bus_space_write_1(tag, handle, (caddr_t)(addr) - addrss, val);
553 * xge_os_pio_mem_read16
554 * Read 16bit from device memory mapped space.
555 * @pdev: Device context.
556 * @regh: PCI configuration space handle.
557 * @addr: Address in device memory space.
559 * Returns: 16bit value read from the specified (mapped) memory space address.
562 xge_os_pio_mem_read16(pci_dev_h pdev, pci_reg_h regh, void *addr)
564 bus_space_tag_t tag =
565 (bus_space_tag_t)(((xge_bus_resource_t *)regh)->bus_tag);
566 bus_space_handle_t handle =
567 (bus_space_handle_t)(((xge_bus_resource_t *)regh)->bus_handle);
568 caddr_t addrss = (caddr_t)
569 (((xge_bus_resource_t *)(regh))->bar_start_addr);
571 return bus_space_read_2(tag, handle, (caddr_t)(addr) - addrss);
575 * xge_os_pio_mem_write16
576 * Write 16bit into device memory mapped space.
577 * @pdev: Device context.
578 * @regh: PCI configuration space handle.
579 * @val: Value to write.
580 * @addr: Address in device memory space.
582 * Write 16bit value into the specified (mapped) device memory space.
585 xge_os_pio_mem_write16(pci_dev_h pdev, pci_reg_h regh, u16 val, void *addr)
587 bus_space_tag_t tag =
588 (bus_space_tag_t)(((xge_bus_resource_t *)regh)->bus_tag);
589 bus_space_handle_t handle =
590 (bus_space_handle_t)(((xge_bus_resource_t *)regh)->bus_handle);
591 caddr_t addrss = (caddr_t)(((xge_bus_resource_t *)(regh))->bar_start_addr);
593 bus_space_write_2(tag, handle, (caddr_t)(addr) - addrss, val);
597 * xge_os_pio_mem_read32
598 * Read 32bit from device memory mapped space.
599 * @pdev: Device context.
600 * @regh: PCI configuration space handle.
601 * @addr: Address in device memory space.
603 * Returns: 32bit value read from the specified (mapped) memory space address.
606 xge_os_pio_mem_read32(pci_dev_h pdev, pci_reg_h regh, void *addr)
608 bus_space_tag_t tag =
609 (bus_space_tag_t)(((xge_bus_resource_t *)regh)->bus_tag);
610 bus_space_handle_t handle =
611 (bus_space_handle_t)(((xge_bus_resource_t *)regh)->bus_handle);
612 caddr_t addrss = (caddr_t)
613 (((xge_bus_resource_t *)(regh))->bar_start_addr);
615 return bus_space_read_4(tag, handle, (caddr_t)(addr) - addrss);
619 * xge_os_pio_mem_write32
620 * Write 32bit into device memory space.
621 * @pdev: Device context.
622 * @regh: PCI configuration space handle.
623 * @val: Value to write.
624 * @addr: Address in device memory space.
626 * Write 32bit value into the specified (mapped) device memory space.
629 xge_os_pio_mem_write32(pci_dev_h pdev, pci_reg_h regh, u32 val, void *addr)
631 bus_space_tag_t tag =
632 (bus_space_tag_t)(((xge_bus_resource_t *)regh)->bus_tag);
633 bus_space_handle_t handle =
634 (bus_space_handle_t)(((xge_bus_resource_t *)regh)->bus_handle);
635 caddr_t addrss = (caddr_t)(((xge_bus_resource_t *)(regh))->bar_start_addr);
636 bus_space_write_4(tag, handle, (caddr_t)(addr) - addrss, val);
640 * xge_os_pio_mem_read64
641 * Read 64bit from device memory mapped space.
642 * @pdev: Device context.
643 * @regh: PCI configuration space handle.
644 * @addr: Address in device memory space.
646 * Returns: 64bit value read from the specified (mapped) memory space address.
649 xge_os_pio_mem_read64(pci_dev_h pdev, pci_reg_h regh, void *addr)
653 bus_space_tag_t tag =
654 (bus_space_tag_t)(((xge_bus_resource_t *)regh)->bus_tag);
655 bus_space_handle_t handle =
656 (bus_space_handle_t)(((xge_bus_resource_t *)regh)->bus_handle);
657 caddr_t addrss = (caddr_t)
658 (((xge_bus_resource_t *)(regh))->bar_start_addr);
660 value1 = bus_space_read_4(tag, handle, (caddr_t)(addr) + 4 - addrss);
662 value2 = bus_space_read_4(tag, handle, (caddr_t)(addr) - addrss);
668 * xge_os_pio_mem_write64
669 * Write 32bit into device memory space.
670 * @pdev: Device context.
671 * @regh: PCI configuration space handle.
672 * @val: Value to write.
673 * @addr: Address in device memory space.
675 * Write 64bit value into the specified (mapped) device memory space.
678 xge_os_pio_mem_write64(pci_dev_h pdev, pci_reg_h regh, u64 val, void *addr)
680 u32 vall = val & 0xffffffff;
681 xge_os_pio_mem_write32(pdev, regh, vall, addr);
682 xge_os_pio_mem_write32(pdev, regh, val >> 32, ((caddr_t)(addr) + 4));
688 #define xge_os_flush_bridge xge_os_pio_mem_read64
692 * Map DMA-able memory block to, or from, or to-and-from device.
693 * @pdev: Device context. Used to allocate/pin/map/unmap DMA-able memory.
694 * @dmah: DMA handle used to map the memory block. Obtained via
695 * xge_os_dma_malloc().
696 * @vaddr: Virtual address of the DMA-able memory.
697 * @size: Size (in bytes) to be mapped.
698 * @dir: Direction of this operation (XGE_OS_DMA_DIR_TODEVICE, etc.)
699 * @dma_flags: XGE_OS_DMA_CACHELINE_ALIGNED, XGE_OS_DMA_STREAMING,
700 * XGE_OS_DMA_CONSISTENT (Note that the last two flags are mutually exclusive).
702 * Map a single memory block.
704 * Returns: DMA address of the memory block, XGE_OS_INVALID_DMA_ADDR on failure.
706 * See also: xge_os_dma_malloc(), xge_os_dma_unmap(), xge_os_dma_sync().
708 static inline dma_addr_t
709 xge_os_dma_map(pci_dev_h pdev, pci_dma_h dmah, void *vaddr, size_t size,
710 int dir, int dma_flags)
713 bus_dmamap_load(dmah.dma_tag, dmah.dma_map, dmah.dma_viraddr,
714 dmah.dma_size, xge_dmamap_cb, &dmah.dma_phyaddr, BUS_DMA_NOWAIT);
716 xge_os_printf("bus_dmamap_load_ failed\n")
717 return XGE_OS_INVALID_DMA_ADDR;
719 dmah.dma_size = size;
720 return dmah.dma_phyaddr;
724 * xge_os_dma_unmap - Unmap DMA-able memory.
725 * @pdev: Device context. Used to allocate/pin/map/unmap DMA-able memory.
726 * @dmah: DMA handle used to map the memory block. Obtained via
727 * xge_os_dma_malloc().
728 * @dma_addr: DMA address of the block. Obtained via xge_os_dma_map().
729 * @size: Size (in bytes) to be unmapped.
730 * @dir: Direction of this operation (XGE_OS_DMA_DIR_TODEVICE, etc.)
732 * Unmap a single DMA-able memory block that was previously mapped using
734 * See also: xge_os_dma_malloc(), xge_os_dma_map().
737 xge_os_dma_unmap(pci_dev_h pdev, pci_dma_h dmah, dma_addr_t dma_addr,
738 size_t size, int dir)
740 bus_dmamap_unload(dmah.dma_tag, dmah.dma_map);
745 * xge_os_dma_sync - Synchronize mapped memory.
746 * @pdev: Device context. Used to allocate/pin/map/unmap DMA-able memory.
747 * @dmah: DMA handle used to map the memory block. Obtained via
748 * xge_os_dma_malloc().
749 * @dma_addr: DMA address of the block. Obtained via xge_os_dma_map().
750 * @dma_offset: Offset from start of the blocke. Used by Solaris only.
751 * @length: Size of the block.
752 * @dir: Direction of this operation (XGE_OS_DMA_DIR_TODEVICE, etc.)
754 * Make physical and CPU memory consistent for a single streaming mode DMA
755 * translation. This API compiles to NOP on cache-coherent platforms. On
756 * non cache-coherent platforms, depending on the direction of the "sync"
757 * operation, this API will effectively either invalidate CPU cache (that might
758 * contain old data), or flush CPU cache to update physical memory.
759 * See also: xge_os_dma_malloc(), xge_os_dma_map(),
760 * xge_os_dma_unmap().
763 xge_os_dma_sync(pci_dev_h pdev, pci_dma_h dmah, dma_addr_t dma_addr,
764 u64 dma_offset, size_t length, int dir)
766 bus_dmasync_op_t syncop;
768 case XGE_OS_DMA_DIR_TODEVICE:
769 syncop = BUS_DMASYNC_PREWRITE | BUS_DMASYNC_POSTWRITE;
772 case XGE_OS_DMA_DIR_FROMDEVICE:
773 syncop = BUS_DMASYNC_PREREAD | BUS_DMASYNC_POSTREAD;
777 syncop = BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREWRITE;
780 bus_dmamap_sync(dmah.dma_tag, dmah.dma_map, syncop);
784 #endif /* XGE_OSDEP_H */