1139823Simp/*-
21541Srgrimes * Copyright (c) 2002-2007 Neterion, Inc.
31541Srgrimes * All rights reserved.
4137668Smlaier *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes *
141541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241541Srgrimes * SUCH DAMAGE.
251541Srgrimes *
261541Srgrimes * $FreeBSD$
271541Srgrimes */
281541Srgrimes
291541Srgrimes#ifndef XGE_OS_PAL_H
3010939Swollman#define XGE_OS_PAL_H
311541Srgrimes
321541Srgrimes#include <dev/nxge/include/xge-defs.h>
33172467Ssilby
34172467Ssilby__EXTERN_BEGIN_DECLS
35172467Ssilby
36204902Sqingli/*--------------------------- platform switch ------------------------------*/
37143868Sglebius
381541Srgrimes/* platform specific header */
391549Srgrimes#include <dev/nxge/xge-osdep.h>
4024204Sbde
411541Srgrimes#if !defined(XGE_OS_PLATFORM_64BIT) && !defined(XGE_OS_PLATFORM_32BIT)
42164033Srwatson#error "either 32bit or 64bit switch must be defined!"
431541Srgrimes#endif
44186948Sbz
4512704Sphk#if !defined(XGE_OS_HOST_BIG_ENDIAN) && !defined(XGE_OS_HOST_LITTLE_ENDIAN)
46186948Sbz#error "either little endian or big endian switch must be defined!"
4712704Sphk#endif
48192011Sqingli
491541Srgrimes#if defined(XGE_OS_PLATFORM_64BIT)
501541Srgrimes#define XGE_OS_MEMORY_DEADCODE_PAT  0x5a5a5a5a5a5a5a5a
51195914Sqingli#else
52215207Sgnn#define XGE_OS_MEMORY_DEADCODE_PAT  0x5a5a5a5a
53192011Sqingli#endif
54186119Sqingli
5555009Sshin#define XGE_OS_TRACE_MSGBUF_MAX     512
561541Srgrimestypedef struct xge_os_tracebuf_t {
57192011Sqingli	int     wrapped_once;     /* circular buffer been wrapped */
581541Srgrimes	int     timestamp;        /* whether timestamps are enabled */
591541Srgrimes	volatile int    offset;           /* offset within the tracebuf */
601541Srgrimes	int     size;             /* total size of trace buffer */
6181127Sume	char        msg[XGE_OS_TRACE_MSGBUF_MAX]; /* each individual buffer */
62170613Sbms	int     msgbuf_max;   /* actual size of msg buffer */
63189592Sbms	char        *data;            /* pointer to data buffer */
64195699Srwatson} xge_os_tracebuf_t;
65195699Srwatsonextern xge_os_tracebuf_t *g_xge_os_tracebuf;
661541Srgrimes
6792723Salfred#ifdef XGE_TRACE_INTO_CIRCULAR_ARR
6892723Salfredextern xge_os_tracebuf_t *g_xge_os_tracebuf;
6992723Salfredextern char *dmesg_start;
7092723Salfred
7155009Sshin/* Calculate the size of the msg and copy it into the global buffer */
72137628Smlaier#define __xge_trace(tb) { \
73222143Sqingli	int msgsize = xge_os_strlen(tb->msg) + 2; \
7492723Salfred	int offset = tb->offset; \
7592723Salfred	if (msgsize != 2 && msgsize < tb->msgbuf_max) { \
7692723Salfred	    int leftsize =  tb->size - offset; \
77167729Sbms	    if ((msgsize + tb->msgbuf_max) > leftsize) { \
781541Srgrimes	        xge_os_memzero(tb->data + offset, leftsize); \
79215701Sdim	        offset = 0; \
80195727Srwatson	        tb->wrapped_once = 1; \
81195699Srwatson	    } \
82195699Srwatson	    xge_os_memcpy(tb->data + offset, tb->msg, msgsize-1); \
83183550Szec	    *(tb->data + offset + msgsize-1) = '\n'; \
84215701Sdim	    *(tb->data + offset + msgsize) = 0; \
85207369Sbz	    offset += msgsize; \
86195699Srwatson	    tb->offset = offset; \
87195699Srwatson	    dmesg_start = tb->data + offset; \
88149221Sglebius	    *tb->msg = 0; \
8921666Swollman	} \
90207369Sbz}
91207369Sbz
92207369Sbz#define xge_os_vatrace(tb, fmt) { \
93215207Sgnn	if (tb != NULL) { \
94215207Sgnn	    char *_p = tb->msg; \
95215207Sgnn	    if (tb->timestamp) { \
961541Srgrimes	        xge_os_timestamp(tb->msg); \
971541Srgrimes	        _p = tb->msg + xge_os_strlen(tb->msg); \
981541Srgrimes	    } \
991541Srgrimes	    xge_os_vasprintf(_p, fmt); \
1001541Srgrimes	    __xge_trace(tb); \
1011541Srgrimes	} \
1021549Srgrimes}
103169454Srwatson
1041541Srgrimes#ifdef __GNUC__
1051541Srgrimes#define xge_os_trace(tb, fmt...) { \
1061541Srgrimes	if (tb != NULL) { \
1071541Srgrimes	    if (tb->timestamp) { \
108194951Srwatson	        xge_os_timestamp(tb->msg); \
109181803Sbz	    } \
110194951Srwatson	    xge_os_sprintf(tb->msg + xge_os_strlen(tb->msg), fmt); \
111194951Srwatson	    __xge_trace(tb); \
112194951Srwatson	} \
1131541Srgrimes}
114194951Srwatson#endif /* __GNUC__ */
115194951Srwatson
1161541Srgrimes#else
117194951Srwatson#define xge_os_vatrace(tb, fmt)
118194951Srwatson#ifdef __GNUC__
119194951Srwatson#define xge_os_trace(tb, fmt...)
1201541Srgrimes#endif /* __GNUC__ */
121194951Srwatson#endif
122194951Srwatson
1231541Srgrimes__EXTERN_END_DECLS
124194951Srwatson
1251541Srgrimes#endif /* XGE_OS_PAL_H */
1261541Srgrimes