2 * Copyright(c) 2002-2011 Exar Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification are permitted provided the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
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.
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.
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.
35 #ifndef _VXGE_OSDEP_H_
36 #define _VXGE_OSDEP_H_
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #if __FreeBSD_version >= 800000
42 #include <sys/buf_ring.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>
52 #include <sys/sockio.h>
54 #include <sys/mutex.h>
56 #include <sys/stddef.h>
58 #include <sys/endian.h>
59 #include <sys/sysctl.h>
64 #include <net/if_arp.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>
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>
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>
91 #include <dev/pci/pcivar.h>
92 #include <dev/pci/pcireg.h>
93 #include <dev/pci/pci_private.h>
95 #include <dev/vxge/include/vxge-defs.h>
98 * ------------------------- includes and defines -------------------------
101 #if BYTE_ORDER == BIG_ENDIAN
102 #define VXGE_OS_HOST_BIG_ENDIAN
104 #define VXGE_OS_HOST_LITTLE_ENDIAN
108 #define VXGE_OS_PLATFORM_64BIT
110 #define VXGE_OS_PLATFORM_32BIT
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
118 #define __DECONST(type, var) ((type)(uintrptr_t)(const void *)(var))
121 typedef struct ifnet *ifnet_t;
122 typedef struct mbuf *mbuf_t;
123 typedef struct mbuf *OS_NETSTACK_BUF;
125 typedef struct _vxge_bus_res_t {
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 */
134 typedef struct _vxge_dma_alloc_t {
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 */
146 typedef struct _vxge_pci_info {
148 device_t ndev; /* Device */
149 void *reg_map[3]; /* BAR Resource */
150 struct resource *bar_info[3]; /* BAR tag and handle */
155 * ---------------------- fixed size primitive types -----------------------
157 typedef size_t ptr_t;
160 typedef uint16_t u16;
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;
175 * -------------------------- "libc" functionality -------------------------
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)
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); \
195 #define vxge_os_printf(fmt...) { \
200 #define vxge_os_vaprintf(fmt...) \
203 #define vxge_os_vasprintf(fmt...) { \
204 vxge_os_printf(fmt); \
207 #define vxge_trace(trace, fmt, args...) \
208 vxge_debug_uld(VXGE_COMPONENT_ULD, \
209 trace, hldev, vpid, fmt, ## args)
212 * -------------------- synchronization primitives -------------------------
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); \
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); \
226 /* Destroy the lock */
227 #define vxge_os_spin_lock_destroy(lockp, ctxh) { \
228 if (mtx_initialized(lockp) != 0) \
229 mtx_destroy(lockp); \
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); \
238 /* Acquire the lock */
239 #define vxge_os_spin_lock(lockp) { \
240 if (mtx_owned(lockp) == 0) \
244 /* Release the lock */
245 #define vxge_os_spin_unlock(lockp) mtx_unlock(lockp)
247 /* Acquire the lock (IRQ version) */
248 #define vxge_os_spin_lock_irq(lockp, flags) { \
250 if (mtx_owned(lockp) == 0) \
251 mtx_lock_flags(lockp, flags); \
254 /* Release the lock (IRQ version) */
255 #define vxge_os_spin_unlock_irq(lockp, flags) { \
257 mtx_unlock_flags(lockp, flags); \
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")
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)
280 * ------------------------- misc primitives -------------------------------
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
288 #define vxge_os_ntohs ntohs
289 #define vxge_os_ntohl ntohl
290 #define vxge_os_ntohll be64toh
292 #define vxge_os_htons htons
293 #define vxge_os_htonl htonl
294 #define vxge_os_htonll htobe64
296 #define vxge_os_in_multicast IN_MULTICAST
297 #define VXGE_OS_INADDR_BROADCAST INADDR_BROADCAST
299 * -------------------------- compiler stuff ------------------------------
301 #define __vxge_os_cacheline_size CACHE_LINE_SIZE
302 #define __vxge_os_attr_cacheline_aligned __aligned(__vxge_os_cacheline_size)
305 * ---------------------- memory primitives --------------------------------
307 #if defined(VXGE_OS_MEMORY_CHECK)
309 typedef struct _vxge_os_malloc_t {
318 #define VXGE_OS_MALLOC_CNT_MAX 64*1024
320 extern u32 g_malloc_cnt;
321 extern vxge_os_malloc_t g_malloc_arr[VXGE_OS_MALLOC_CNT_MAX];
323 #define VXGE_OS_MEMORY_CHECK_MALLOC(_vaddr, _size, _file, _line) { \
326 for (i = 0; i < g_malloc_cnt; i++) { \
327 if (g_malloc_arr[i].ptr == NULL) \
330 if (i == 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); \
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; \
345 #define VXGE_OS_MEMORY_CHECK_FREE(_vaddr, _size, _file, _line) { \
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", \
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); \
366 #define VXGE_OS_MEMORY_CHECK_MALLOC(prt, size, file, line)
367 #define VXGE_OS_MEMORY_CHECK_FREE(vaddr, size, file, line)
371 vxge_mem_alloc_ex(u_long size, const char *file, int line)
374 vaddr = malloc(size, M_DEVBUF, M_ZERO | M_NOWAIT);
376 VXGE_OS_MEMORY_CHECK_MALLOC((void *)vaddr, size, file, line)
377 vxge_os_memzero(vaddr, size);
384 vxge_mem_free_ex(const void *vaddr, u_long size, const char *file, int line)
387 VXGE_OS_MEMORY_CHECK_FREE(vaddr, size, file, line)
388 free(__DECONST(void *, vaddr), M_DEVBUF);
392 #define vxge_os_malloc(pdev, size) \
393 vxge_mem_alloc_ex(size, __FILE__, __LINE__)
395 #define vxge_os_free(pdev, vaddr, size) \
396 vxge_mem_free_ex(vaddr, size, __FILE__, __LINE__)
398 #define vxge_mem_alloc(size) \
399 vxge_mem_alloc_ex(size, __FILE__, __LINE__)
401 #define vxge_mem_free(vaddr, size) \
402 vxge_mem_free_ex(vaddr, size, __FILE__, __LINE__)
404 #define vxge_free_packet(x) \
405 if (NULL != x) { m_freem(x); x = NULL; }
408 * --------------------------- pci primitives ------------------------------
410 #define vxge_os_pci_read8(pdev, cfgh, where, val) \
411 (*(val) = pci_read_config(pdev->ndev, where, 1))
413 #define vxge_os_pci_write8(pdev, cfgh, where, val) \
414 pci_write_config(pdev->ndev, where, val, 1)
416 #define vxge_os_pci_read16(pdev, cfgh, where, val) \
417 (*(val) = pci_read_config(pdev->ndev, where, 2))
419 #define vxge_os_pci_write16(pdev, cfgh, where, val) \
420 pci_write_config(pdev->ndev, where, val, 2)
422 #define vxge_os_pci_read32(pdev, cfgh, where, val) \
423 (*(val) = pci_read_config(pdev->ndev, where, 4))
425 #define vxge_os_pci_write32(pdev, cfgh, where, val) \
426 pci_write_config(pdev->ndev, where, val, 4)
429 vxge_os_pci_res_len(pci_dev_h pdev, pci_reg_h regh)
431 return (((vxge_bus_res_t *) regh)->bus_res_len);
435 vxge_os_pio_mem_read8(pci_dev_h pdev, pci_reg_h regh, void *addr)
438 (caddr_t) (((vxge_bus_res_t *) (regh))->bar_start_addr);
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));
446 vxge_os_pio_mem_read16(pci_dev_h pdev, pci_reg_h regh, void *addr)
449 (caddr_t) (((vxge_bus_res_t *) (regh))->bar_start_addr);
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));
457 vxge_os_pio_mem_read32(pci_dev_h pdev, pci_reg_h regh, void *addr)
460 (caddr_t) (((vxge_bus_res_t *) (regh))->bar_start_addr);
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));
468 vxge_os_pio_mem_read64(pci_dev_h pdev, pci_reg_h regh, void *addr)
470 u64 val, val_l, val_u;
473 (caddr_t) (((vxge_bus_res_t *) (regh))->bar_start_addr);
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));
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));
483 val = ((val_l << 32) | val_u);
488 vxge_os_pio_mem_write8(pci_dev_h pdev, pci_reg_h regh, u8 val, void *addr)
491 (caddr_t) (((vxge_bus_res_t *) regh)->bar_start_addr);
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);
499 vxge_os_pio_mem_write16(pci_dev_h pdev, pci_reg_h regh, u16 val, void *addr)
502 (caddr_t) (((vxge_bus_res_t *) (regh))->bar_start_addr);
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);
510 vxge_os_pio_mem_write32(pci_dev_h pdev, pci_reg_h regh, u32 val, void *addr)
513 (caddr_t) (((vxge_bus_res_t *) (regh))->bar_start_addr);
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);
521 vxge_os_pio_mem_write64(pci_dev_h pdev, pci_reg_h regh, u64 val, void *addr)
523 u32 val_l = (u32) (val & 0xffffffff);
524 u32 val_u = (u32) (val >> 32);
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);
530 #define vxge_os_flush_bridge vxge_os_pio_mem_read64
533 * --------------------------- dma primitives -----------------------------
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)
541 vxge_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
546 *(bus_addr_t *) arg = segs->ds_addr;
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)
554 bus_addr_t bus_addr = BUS_SPACE_MAXADDR;
555 bus_size_t boundary, max_size, alignment = PAGE_SIZE;
557 if (bytes > PAGE_SIZE) {
561 boundary = PAGE_SIZE;
562 max_size = PAGE_SIZE;
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 */
582 device_printf(pdev->ndev, "bus_dma_tag_create failed\n");
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),
591 device_printf(pdev->ndev, "bus_dmamem_alloc failed\n");
595 VXGE_OS_MEMORY_CHECK_MALLOC(p_dmah->dma_vaddr, p_dmah->dma_size,
598 return (p_dmah->dma_vaddr);
601 bus_dma_tag_destroy(p_dmah->dma_tag);
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)
611 VXGE_OS_MEMORY_CHECK_FREE(p_dmah->dma_vaddr, size, file, line)
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);
616 p_dmah->dma_map = NULL;
617 p_dmah->dma_tag = NULL;
618 p_dmah->dma_vaddr = NULL;
622 vxge_hal_blockpool_block_add(void *, void *, u32, pci_dma_h *, pci_dma_acc_h *);
625 vxge_os_dma_malloc_async(pci_dev_h pdev, void *devh,
626 u_long size, int dma_flags)
629 pci_dma_acc_h acc_handle;
631 void *block_addr = NULL;
633 block_addr = vxge_os_dma_malloc(pdev, size, dma_flags,
634 &dma_h, &acc_handle);
636 vxge_hal_blockpool_block_add(devh, block_addr, size,
637 &dma_h, &acc_handle);
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)
644 bus_dmasync_op_t dmasync_op;
647 case VXGE_OS_DMA_DIR_TODEVICE:
648 dmasync_op = BUS_DMASYNC_PREWRITE | BUS_DMASYNC_POSTWRITE;
651 case VXGE_OS_DMA_DIR_FROMDEVICE:
652 dmasync_op = BUS_DMASYNC_PREREAD | BUS_DMASYNC_POSTREAD;
656 case VXGE_OS_DMA_DIR_BIDIRECTIONAL:
657 dmasync_op = BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE;
661 bus_dmamap_sync(dmah.dma_tag, dmah.dma_map, dmasync_op);
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)
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);
674 return (VXGE_OS_INVALID_DMA_ADDR);
676 dmah.dma_size = size;
677 return (dmah.dma_paddr);
681 vxge_os_dma_unmap(pci_dev_h pdev, pci_dma_h dmah, dma_addr_t dma_paddr,
684 bus_dmamap_unload(dmah.dma_tag, dmah.dma_map);
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, \
692 vxge_os_is_my_packet(void *pdev, unsigned long addr)
697 #endif /* _VXGE_OSDEP_H_ */