4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2002-2005 Neterion, Inc.
31 * FileName : xge_osdep.h
33 * Description: OSPAL - Solaris
37 #ifndef _SYS_XGE_OSDEP_H
38 #define _SYS_XGE_OSDEP_H
41 #include <sys/sunddi.h>
42 #include <sys/varargs.h>
43 #include <sys/atomic.h>
44 #include <sys/policy.h>
45 #include <sys/int_fmtio.h>
46 #include <sys/thread.h>
47 #include <sys/cpuvar.h>
49 #include <inet/common.h>
58 /* ------------------------- includes and defines ------------------------- */
60 #define XGE_HAL_TX_MULTI_POST_IRQ 1
61 #define XGE_HAL_TX_MULTI_RESERVE_IRQ 1
62 #define XGE_HAL_TX_MULTI_FREE_IRQ 1
63 #define XGE_HAL_DMA_DTR_CONSISTENT 1
64 #define XGE_HAL_DMA_STATS_STREAMING 1
67 #define XGE_OS_DMA_REQUIRES_SYNC 1
70 #define XGE_HAL_ALIGN_XMIT 1
73 #define XGE_OS_HOST_BIG_ENDIAN 1
75 #define XGE_OS_HOST_LITTLE_ENDIAN 1
79 #define XGE_OS_HOST_PAGE_SIZE 8192
81 #define XGE_OS_HOST_PAGE_SIZE 4096
85 #define XGE_OS_PLATFORM_64BIT 1
87 #define XGE_OS_PLATFORM_32BIT 1
90 #define XGE_OS_HAS_SNPRINTF 1
93 #define XGE_LL_IP_FAST_CSUM(hdr, len) 0 /* ip_ocsum(hdr, len>>1, 0); */
95 /* ---------------------- fixed size primitive types ----------------------- */
101 typedef u64 dma_addr_t
;
102 #define ulong_t ulong_t
103 #define ptrdiff_t ptrdiff_t
104 typedef kmutex_t spinlock_t
;
105 typedef dev_info_t
*pci_dev_h
;
106 typedef ddi_acc_handle_t pci_reg_h
;
107 typedef ddi_acc_handle_t pci_cfg_h
;
108 typedef uint_t pci_irq_h
;
109 typedef ddi_dma_handle_t pci_dma_h
;
110 typedef ddi_acc_handle_t pci_dma_acc_h
;
113 #define OS_NETSTACK_BUF mblk_t *
114 #define OS_LL_HEADER uint8_t *
115 #define OS_IP_HEADER uint8_t *
116 #define OS_TL_HEADER uint8_t *
118 /* -------------------------- "libc" functionality ------------------------- */
120 #define xge_os_strlcpy (void) strlcpy
121 #define xge_os_strlen strlen
122 #define xge_os_snprintf snprintf
123 #define xge_os_memzero(addr, size) bzero(addr, size)
124 #define xge_os_memcpy(dst, src, size) bcopy(src, dst, size)
125 #define xge_os_memcmp(src1, src2, size) bcmp(src1, src2, size)
126 #define xge_os_ntohl ntohl
127 #define xge_os_htons htons
128 #define xge_os_ntohs ntohs
131 #define xge_os_printf(fmt...) cmn_err(CE_CONT, fmt)
132 #define xge_os_sprintf(buf, fmt...) strlen(sprintf(buf, fmt))
134 #define xge_os_vaprintf(fmt) { \
137 vcmn_err(CE_CONT, fmt, va); \
141 static inline void xge_os_printf(char *fmt
, ...) {
142 xge_os_vaprintf(fmt
);
145 #define xge_os_vasprintf(buf, fmt) { \
148 (void) vsprintf(buf, fmt, va); \
152 static inline int xge_os_sprintf(char *buf
, char *fmt
, ...) {
153 xge_os_vasprintf(buf
, fmt
);
154 return (strlen(buf
));
158 #define xge_os_timestamp(buf) { \
159 todinfo_t todinfo = utc_to_tod(ddi_get_time()); \
160 (void) xge_os_sprintf(buf, "%02d/%02d/%02d.%02d:%02d:%02d: ", \
161 todinfo.tod_day, todinfo.tod_month, \
162 (1970 + todinfo.tod_year - 70), \
163 todinfo.tod_hour, todinfo.tod_min, todinfo.tod_sec); \
166 #define xge_os_println xge_os_printf
168 /* -------------------- synchronization primitives ------------------------- */
170 #define xge_os_spin_lock_init(lockp, ctxh) \
171 mutex_init(lockp, NULL, MUTEX_DRIVER, NULL)
172 #define xge_os_spin_lock_init_irq(lockp, irqh) \
173 mutex_init(lockp, NULL, MUTEX_DRIVER, DDI_INTR_PRI(irqh))
174 #define xge_os_spin_lock_destroy(lockp, cthx) \
175 (cthx = cthx, mutex_destroy(lockp))
176 #define xge_os_spin_lock_destroy_irq(lockp, cthx) \
177 (cthx = cthx, mutex_destroy(lockp))
178 #define xge_os_spin_lock(lockp) mutex_enter(lockp)
179 #define xge_os_spin_unlock(lockp) mutex_exit(lockp)
180 #define xge_os_spin_lock_irq(lockp, flags) (flags = flags, mutex_enter(lockp))
181 #define xge_os_spin_unlock_irq(lockp, flags) mutex_exit(lockp)
183 /* x86 arch will never re-order writes, Sparc can */
184 #define xge_os_wmb() membar_producer()
186 #define xge_os_udelay(us) drv_usecwait(us)
187 #define xge_os_mdelay(ms) drv_usecwait(ms * 1000)
189 #define xge_os_cmpxchg(targetp, cmp, newval) \
190 sizeof (*(targetp)) == 4 ? \
191 atomic_cas_32((uint32_t *)targetp, cmp, newval) : \
192 atomic_cas_64((uint64_t *)targetp, cmp, newval)
194 /* ------------------------- misc primitives ------------------------------- */
196 #define xge_os_unlikely(x) (x)
197 #define xge_os_prefetch(a) (a = a)
198 #define xge_os_prefetchw
200 #define xge_os_bug(fmt...) cmn_err(CE_PANIC, fmt)
202 static inline void xge_os_bug(char *fmt
, ...) {
206 vcmn_err(CE_PANIC
, fmt
, ap
);
211 /* -------------------------- compiler stuffs ------------------------------ */
214 #define __xge_os_cacheline_size 64 /* L1-cache line size: x86_64 */
216 #define __xge_os_cacheline_size 64 /* L1-cache line size: sparcv9 */
220 #define __xge_os_attr_cacheline_aligned \
221 __attribute__((__aligned__(__xge_os_cacheline_size)))
223 #define __xge_os_attr_cacheline_aligned
226 /* ---------------------- memory primitives -------------------------------- */
228 static inline void *__xge_os_malloc(pci_dev_h pdev
, unsigned long size
,
229 char *file
, int line
)
231 void *vaddr
= kmem_alloc(size
, KM_SLEEP
);
233 XGE_OS_MEMORY_CHECK_MALLOC(vaddr
, size
, file
, line
);
237 static inline void xge_os_free(pci_dev_h pdev
, const void *vaddr
,
240 XGE_OS_MEMORY_CHECK_FREE(vaddr
, size
);
241 kmem_free((void*)vaddr
, size
);
244 #define xge_os_malloc(pdev, size) \
245 __xge_os_malloc(pdev, size, __FILE__, __LINE__)
247 static inline void *__xge_os_dma_malloc(pci_dev_h pdev
, unsigned long size
,
248 int dma_flags
, pci_dma_h
*p_dmah
, pci_dma_acc_h
*p_dma_acch
, char *file
,
254 extern ddi_device_acc_attr_t
*p_xge_dev_attr
;
255 extern struct ddi_dma_attr
*p_hal_dma_attr
;
257 ret
= ddi_dma_alloc_handle(pdev
, p_hal_dma_attr
,
258 DDI_DMA_DONTWAIT
, 0, p_dmah
);
259 if (ret
!= DDI_SUCCESS
) {
263 ret
= ddi_dma_mem_alloc(*p_dmah
, size
, p_xge_dev_attr
,
264 (dma_flags
& XGE_OS_DMA_CONSISTENT
?
265 DDI_DMA_CONSISTENT
: DDI_DMA_STREAMING
), DDI_DMA_DONTWAIT
, 0,
266 (caddr_t
*)&vaddr
, &real_size
, p_dma_acch
);
267 if (ret
!= DDI_SUCCESS
) {
268 ddi_dma_free_handle(p_dmah
);
272 if (size
> real_size
) {
273 ddi_dma_mem_free(p_dma_acch
);
274 ddi_dma_free_handle(p_dmah
);
278 XGE_OS_MEMORY_CHECK_MALLOC(vaddr
, size
, file
, line
);
283 #define xge_os_dma_malloc(pdev, size, dma_flags, p_dmah, p_dma_acch) \
284 __xge_os_dma_malloc(pdev, size, dma_flags, p_dmah, p_dma_acch, \
287 static inline void xge_os_dma_free(pci_dev_h pdev
, const void *vaddr
, int size
,
288 pci_dma_acc_h
*p_dma_acch
, pci_dma_h
*p_dmah
)
290 XGE_OS_MEMORY_CHECK_FREE(vaddr
, 0);
291 ddi_dma_mem_free(p_dma_acch
);
292 ddi_dma_free_handle(p_dmah
);
296 /* --------------------------- pci primitives ------------------------------ */
298 #define xge_os_pci_read8(pdev, cfgh, where, val) \
299 (*(val) = pci_config_get8(cfgh, where))
301 #define xge_os_pci_write8(pdev, cfgh, where, val) \
302 pci_config_put8(cfgh, where, val)
304 #define xge_os_pci_read16(pdev, cfgh, where, val) \
305 (*(val) = pci_config_get16(cfgh, where))
307 #define xge_os_pci_write16(pdev, cfgh, where, val) \
308 pci_config_put16(cfgh, where, val)
310 #define xge_os_pci_read32(pdev, cfgh, where, val) \
311 (*(val) = pci_config_get32(cfgh, where))
313 #define xge_os_pci_write32(pdev, cfgh, where, val) \
314 pci_config_put32(cfgh, where, val)
316 /* --------------------------- io primitives ------------------------------- */
318 #define xge_os_pio_mem_read8(pdev, regh, addr) \
319 (ddi_get8(regh, (uint8_t *)(addr)))
321 #define xge_os_pio_mem_write8(pdev, regh, val, addr) \
322 (ddi_put8(regh, (uint8_t *)(addr), val))
324 #define xge_os_pio_mem_read16(pdev, regh, addr) \
325 (ddi_get16(regh, (uint16_t *)(addr)))
327 #define xge_os_pio_mem_write16(pdev, regh, val, addr) \
328 (ddi_put16(regh, (uint16_t *)(addr), val))
330 #define xge_os_pio_mem_read32(pdev, regh, addr) \
331 (ddi_get32(regh, (uint32_t *)(addr)))
333 #define xge_os_pio_mem_write32(pdev, regh, val, addr) \
334 (ddi_put32(regh, (uint32_t *)(addr), val))
336 #define xge_os_pio_mem_read64(pdev, regh, addr) \
337 (ddi_get64(regh, (uint64_t *)(addr)))
339 #define xge_os_pio_mem_write64(pdev, regh, val, addr) \
340 (ddi_put64(regh, (uint64_t *)(addr), val))
342 #define xge_os_flush_bridge xge_os_pio_mem_read64
344 /* --------------------------- dma primitives ----------------------------- */
346 #define XGE_OS_DMA_DIR_TODEVICE DDI_DMA_SYNC_FORDEV
347 #define XGE_OS_DMA_DIR_FROMDEVICE DDI_DMA_SYNC_FORKERNEL
348 #define XGE_OS_DMA_DIR_BIDIRECTIONAL -1
350 #define XGE_OS_DMA_USES_IOMMU 0
352 #define XGE_OS_DMA_USES_IOMMU 1
355 #define XGE_OS_INVALID_DMA_ADDR ((dma_addr_t)0)
357 static inline dma_addr_t
xge_os_dma_map(pci_dev_h pdev
, pci_dma_h dmah
,
358 void *vaddr
, size_t size
, int dir
, int dma_flags
) {
362 ddi_dma_cookie_t dma_cookie
;
365 case XGE_OS_DMA_DIR_TODEVICE
:
366 flags
= DDI_DMA_WRITE
;
368 case XGE_OS_DMA_DIR_FROMDEVICE
:
369 flags
= DDI_DMA_READ
;
371 case XGE_OS_DMA_DIR_BIDIRECTIONAL
:
372 flags
= DDI_DMA_RDWR
;
378 flags
|= (dma_flags
& XGE_OS_DMA_CONSISTENT
) ?
379 DDI_DMA_CONSISTENT
: DDI_DMA_STREAMING
;
381 ret
= ddi_dma_addr_bind_handle(dmah
, NULL
, vaddr
, size
, flags
,
382 DDI_DMA_SLEEP
, 0, &dma_cookie
, &ncookies
);
383 if (ret
!= DDI_SUCCESS
) {
387 if (ncookies
!= 1 || dma_cookie
.dmac_size
< size
) {
388 (void) ddi_dma_unbind_handle(dmah
);
392 return (dma_cookie
.dmac_laddress
);
395 static inline void xge_os_dma_unmap(pci_dev_h pdev
, pci_dma_h dmah
,
396 dma_addr_t dma_addr
, size_t size
, int dir
)
398 (void) ddi_dma_unbind_handle(dmah
);
401 static inline void xge_os_dma_sync(pci_dev_h pdev
, pci_dma_h dmah
,
402 dma_addr_t dma_addr
, u64 dma_offset
, size_t length
, int dir
)
404 (void) ddi_dma_sync(dmah
, dma_offset
, length
, dir
);
411 #endif /* _SYS_XGE_OSDEP_H */