/* Copyright (c) 2008-2012 Freescale Semiconductor, Inc1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions are met:5* * Redistributions of source code must retain the above copyright6* notice, this list of conditions and the following disclaimer.7* * Redistributions in binary form must reproduce the above copyright8* notice, this list of conditions and the following disclaimer in the9* documentation and/or other materials provided with the distribution.10* * Neither the name of Freescale Semiconductor nor the11* names of its contributors may be used to endorse or promote products12* derived from this software without specific prior written permission.13*14*15* ALTERNATIVELY, this software may be distributed under the terms of the16* GNU General Public License ("GPL") as published by the Free Software17* Foundation, either version 2 of that License or (at your option) any18* later version.19*20* THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY21* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23* DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY24* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES25* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;26* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND27* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT28* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233/**************************************************************************//**34@File error_ext.h3536@Description Error definitions.37*//***************************************************************************/3839#ifndef __ERROR_EXT_H40#define __ERROR_EXT_H4142#if defined(NCSW_FREEBSD)43#include <sys/param.h>44#include <sys/errno.h>45#include <sys/pcpu.h>46#endif4748#include "std_ext.h"49#include "xx_ext.h"50#include "core_ext.h"5152535455/**************************************************************************//**56@Group gen_id General Drivers Utilities5758@Description External routines.5960@{61*//***************************************************************************/6263/**************************************************************************//**64@Group gen_error_id Errors, Events and Debug6566@Description External routines.6768@{69*//***************************************************************************/7071/******************************************************************************72The scheme below provides the bits description for error codes:73740 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1575| Reserved (should be zero) | Module ID |767716 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3178| Error Type |79******************************************************************************/8081#define ERROR_CODE(_err) ((((uint32_t)_err) & 0x0000FFFF) | __ERR_MODULE__)8283#define GET_ERROR_TYPE(_errcode) ((_errcode) & 0x0000FFFF)84/**< Extract module code from error code (#t_Error) */8586#define GET_ERROR_MODULE(_errcode) ((_errcode) & 0x00FF0000)87/**< Extract error type (#e_ErrorType) from88error code (#t_Error) */899091/**************************************************************************//**92@Description Error Type Enumeration93*//***************************************************************************/94typedef enum e_ErrorType /* Comments / Associated Message Strings */95{ /* ------------------------------------------------------------ */96E_OK = 0 /* Never use "RETURN_ERROR" with E_OK; Use "return E_OK;" */97,E_WRITE_FAILED = EIO /**< Write access failed on memory/device. */98/* String: none, or device name. */99,E_NO_DEVICE = ENXIO /**< The associated device is not initialized. */100/* String: none. */101,E_NOT_AVAILABLE = EAGAIN102/**< Resource is unavailable. */103/* String: none, unless the operation is not the main goal104of the function (in this case add resource description). */105,E_NO_MEMORY = ENOMEM /**< External memory allocation failed. */106/* String: description of item for which allocation failed. */107,E_INVALID_ADDRESS = EFAULT108/**< Invalid address. */109/* String: description of the specific violation. */110,E_BUSY = EBUSY /**< Resource or module is busy. */111/* String: none, unless the operation is not the main goal112of the function (in this case add resource description). */113,E_ALREADY_EXISTS = EEXIST114/**< Requested resource or item already exists. */115/* Use when resource duplication or sharing are not allowed.116String: none, unless the operation is not the main goal117of the function (in this case add item description). */118,E_INVALID_OPERATION = ENODEV119/**< The operation/command is invalid (unrecognized). */120/* String: none. */121,E_INVALID_VALUE = EDOM /**< Invalid value. */122/* Use for non-enumeration parameters, and123only when other error types are not suitable.124String: parameter description + "(should be <attribute>)",125e.g: "Maximum Rx buffer length (should be divisible by 8)",126"Channel number (should be even)". */127,E_NOT_IN_RANGE = ERANGE/**< Parameter value is out of range. */128/* Don't use this error for enumeration parameters.129String: parameter description + "(should be %d-%d)",130e.g: "Number of pad characters (should be 0-15)". */131,E_NOT_SUPPORTED = ENOSYS132/**< The function is not supported or not implemented. */133/* String: none. */134,E_INVALID_STATE /**< The operation is not allowed in current module state. */135/* String: none. */136,E_INVALID_HANDLE /**< Invalid handle of module or object. */137/* String: none, unless the function takes in more than one138handle (in this case add the handle description) */139,E_INVALID_ID /**< Invalid module ID (usually enumeration or index). */140/* String: none, unless the function takes in more than one141ID (in this case add the ID description) */142,E_NULL_POINTER /**< Unexpected NULL pointer. */143/* String: pointer description. */144,E_INVALID_SELECTION /**< Invalid selection or mode. */145/* Use for enumeration values, only when other error types146are not suitable.147String: parameter description. */148,E_INVALID_COMM_MODE /**< Invalid communication mode. */149/* String: none, unless the function takes in more than one150communication mode indications (in this case add151parameter description). */152,E_INVALID_MEMORY_TYPE /**< Invalid memory type. */153/* String: none, unless the function takes in more than one154memory types (in this case add memory description,155e.g: "Data memory", "Buffer descriptors memory"). */156,E_INVALID_CLOCK /**< Invalid clock. */157/* String: none, unless the function takes in more than one158clocks (in this case add clock description,159e.g: "Rx clock", "Tx clock"). */160,E_CONFLICT /**< Some setting conflicts with another setting. */161/* String: description of the conflicting settings. */162,E_NOT_ALIGNED /**< Non-aligned address. */163/* String: parameter description + "(should be %d-bytes aligned)",164e.g: "Rx data buffer (should be 32-bytes aligned)". */165,E_NOT_FOUND /**< Requested resource or item was not found. */166/* Use only when the resource/item is uniquely identified.167String: none, unless the operation is not the main goal168of the function (in this case add item description). */169,E_FULL /**< Resource is full. */170/* String: none, unless the operation is not the main goal171of the function (in this case add resource description). */172,E_EMPTY /**< Resource is empty. */173/* String: none, unless the operation is not the main goal174of the function (in this case add resource description). */175,E_ALREADY_FREE /**< Specified resource or item is already free or deleted. */176/* String: none, unless the operation is not the main goal177of the function (in this case add item description). */178,E_READ_FAILED /**< Read access failed on memory/device. */179/* String: none, or device name. */180,E_INVALID_FRAME /**< Invalid frame object (NULL handle or missing buffers). */181/* String: none. */182,E_SEND_FAILED /**< Send operation failed on device. */183/* String: none, or device name. */184,E_RECEIVE_FAILED /**< Receive operation failed on device. */185/* String: none, or device name. */186,E_TIMEOUT/* = ETIMEDOUT*/ /**< The operation timed out. */187/* String: none. */188189,E_DUMMY_LAST /* NEVER USED */190191} e_ErrorType;192193/**************************************************************************//**194@Description Event Type Enumeration195*//***************************************************************************/196typedef enum e_Event /* Comments / Associated Flags and Message Strings */197{ /* ------------------------------------------------------------ */198EV_NO_EVENT = 0 /**< No event; Never used. */199200,EV_RX_DISCARD /**< Received packet discarded (by the driver, and only for201complete packets);202Flags: error flags in case of error, zero otherwise. */203/* String: reason for discard, e.g: "Error in frame",204"Disordered frame", "Incomplete frame", "No frame object". */205,EV_RX_ERROR /**< Receive error (by hardware/firmware);206Flags: usually status flags from the buffer descriptor. */207/* String: none. */208,EV_TX_ERROR /**< Transmit error (by hardware/firmware);209Flags: usually status flags from the buffer descriptor. */210/* String: none. */211,EV_NO_BUFFERS /**< System ran out of buffer objects;212Flags: zero. */213/* String: none. */214,EV_NO_MB_FRAMES /**< System ran out of multi-buffer frame objects;215Flags: zero. */216/* String: none. */217,EV_NO_SB_FRAMES /**< System ran out of single-buffer frame objects;218Flags: zero. */219/* String: none. */220,EV_TX_QUEUE_FULL /**< Transmit queue is full;221Flags: zero. */222/* String: none. */223,EV_RX_QUEUE_FULL /**< Receive queue is full;224Flags: zero. */225/* String: none. */226,EV_INTR_QUEUE_FULL /**< Interrupt queue overflow;227Flags: zero. */228/* String: none. */229,EV_NO_DATA_BUFFER /**< Data buffer allocation (from higher layer) failed;230Flags: zero. */231/* String: none. */232,EV_OBJ_POOL_EMPTY /**< Objects pool is empty;233Flags: zero. */234/* String: object description (name). */235,EV_BUS_ERROR /**< Illegal access on bus;236Flags: the address (if available) or bus identifier */237/* String: bus/address/module description. */238,EV_PTP_TXTS_QUEUE_FULL /**< PTP Tx timestamps queue is full;239Flags: zero. */240/* String: none. */241,EV_PTP_RXTS_QUEUE_FULL /**< PTP Rx timestamps queue is full;242Flags: zero. */243/* String: none. */244,EV_DUMMY_LAST245246} e_Event;247248249/**************************************************************************//**250@Collection Debug Levels for Errors and Events251252The level description refers to errors only.253For events, classification is done by the user.254255The TRACE, INFO and WARNING levels are allowed only when using256the DBG macro, and are not allowed when using the error macros257(RETURN_ERROR or REPORT_ERROR).258@{259*//***************************************************************************/260#define REPORT_LEVEL_CRITICAL 1 /**< Crasher: Incorrect flow, NULL pointers/handles. */261#define REPORT_LEVEL_MAJOR 2 /**< Cannot proceed: Invalid operation, parameters or262configuration. */263#define REPORT_LEVEL_MINOR 3 /**< Recoverable problem: a repeating call with the same264parameters may be successful. */265#define REPORT_LEVEL_WARNING 4 /**< Something is not exactly right, yet it is not an error. */266#define REPORT_LEVEL_INFO 5 /**< Messages which may be of interest to user/programmer. */267#define REPORT_LEVEL_TRACE 6 /**< Program flow messages. */268269#define EVENT_DISABLED 0xFF /**< Disabled event (not reported at all) */270271/* @} */272273274275#define NO_MSG ("")276277#ifndef DEBUG_GLOBAL_LEVEL278#define DEBUG_GLOBAL_LEVEL REPORT_LEVEL_WARNING279#endif /* DEBUG_GLOBAL_LEVEL */280281#ifndef ERROR_GLOBAL_LEVEL282#define ERROR_GLOBAL_LEVEL DEBUG_GLOBAL_LEVEL283#endif /* ERROR_GLOBAL_LEVEL */284285#ifndef EVENT_GLOBAL_LEVEL286#define EVENT_GLOBAL_LEVEL REPORT_LEVEL_MINOR287#endif /* EVENT_GLOBAL_LEVEL */288289#ifdef EVENT_LOCAL_LEVEL290#define EVENT_DYNAMIC_LEVEL EVENT_LOCAL_LEVEL291#else292#define EVENT_DYNAMIC_LEVEL EVENT_GLOBAL_LEVEL293#endif /* EVENT_LOCAL_LEVEL */294295296#ifndef DEBUG_DYNAMIC_LEVEL297#define DEBUG_USING_STATIC_LEVEL298299#ifdef DEBUG_STATIC_LEVEL300#define DEBUG_DYNAMIC_LEVEL DEBUG_STATIC_LEVEL301#else302#define DEBUG_DYNAMIC_LEVEL DEBUG_GLOBAL_LEVEL303#endif /* DEBUG_STATIC_LEVEL */304305#else /* DEBUG_DYNAMIC_LEVEL */306#ifdef DEBUG_STATIC_LEVEL307#error "Please use either DEBUG_STATIC_LEVEL or DEBUG_DYNAMIC_LEVEL (not both)"308#else309int DEBUG_DYNAMIC_LEVEL = DEBUG_GLOBAL_LEVEL;310#endif /* DEBUG_STATIC_LEVEL */311#endif /* !DEBUG_DYNAMIC_LEVEL */312313314#ifndef ERROR_DYNAMIC_LEVEL315316#ifdef ERROR_STATIC_LEVEL317#define ERROR_DYNAMIC_LEVEL ERROR_STATIC_LEVEL318#else319#define ERROR_DYNAMIC_LEVEL ERROR_GLOBAL_LEVEL320#endif /* ERROR_STATIC_LEVEL */321322#else /* ERROR_DYNAMIC_LEVEL */323#ifdef ERROR_STATIC_LEVEL324#error "Please use either ERROR_STATIC_LEVEL or ERROR_DYNAMIC_LEVEL (not both)"325#else326int ERROR_DYNAMIC_LEVEL = ERROR_GLOBAL_LEVEL;327#endif /* ERROR_STATIC_LEVEL */328#endif /* !ERROR_DYNAMIC_LEVEL */329330#define PRINT_FORMAT "[CPU%02d, %s:%d %s]"331#define PRINT_FMT_PARAMS PCPU_GET(cpuid), __FILE__, __LINE__, __FUNCTION__332333#if (!(defined(DEBUG_ERRORS)) || (DEBUG_ERRORS == 0))334/* No debug/error/event messages at all */335#define DBG(_level, _vmsg)336337#define REPORT_ERROR(_level, _err, _vmsg)338339#define RETURN_ERROR(_level, _err, _vmsg) \340return ERROR_CODE(_err)341342#if (REPORT_EVENTS > 0)343344#define REPORT_EVENT(_ev, _appId, _flg, _vmsg) \345do { \346if (_ev##_LEVEL <= EVENT_DYNAMIC_LEVEL) { \347XX_EventById((uint32_t)(_ev), (t_Handle)(_appId), (uint16_t)(_flg), NO_MSG); \348} \349} while (0)350351#else352353#define REPORT_EVENT(_ev, _appId, _flg, _vmsg)354355#endif /* (REPORT_EVENTS > 0) */356357358#else /* DEBUG_ERRORS > 0 */359360extern const char *dbgLevelStrings[];361#if (REPORT_EVENTS > 0)362extern const char *eventStrings[];363#endif /* (REPORT_EVENTS > 0) */364365char * ErrTypeStrings (e_ErrorType err);366367368#if ((defined(DEBUG_USING_STATIC_LEVEL)) && (DEBUG_DYNAMIC_LEVEL < REPORT_LEVEL_WARNING))369/* No need for DBG macro - debug level is higher anyway */370#define DBG(_level, _vmsg)371#else372#define DBG(_level, _vmsg) \373do { \374if (REPORT_LEVEL_##_level <= DEBUG_DYNAMIC_LEVEL) { \375XX_Print("> %s (%s) " PRINT_FORMAT ": ", \376dbgLevelStrings[REPORT_LEVEL_##_level - 1], \377__STRING(__ERR_MODULE__), \378PRINT_FMT_PARAMS); \379XX_Print _vmsg; \380XX_Print("\r\n"); \381} \382} while (0)383#endif /* (defined(DEBUG_USING_STATIC_LEVEL) && (DEBUG_DYNAMIC_LEVEL < WARNING)) */384385386#define REPORT_ERROR(_level, _err, _vmsg) \387do { \388if (REPORT_LEVEL_##_level <= ERROR_DYNAMIC_LEVEL) { \389XX_Print("! %s %s Error " PRINT_FORMAT ": %s; ", \390dbgLevelStrings[REPORT_LEVEL_##_level - 1], \391__STRING(__ERR_MODULE__), \392PRINT_FMT_PARAMS, \393ErrTypeStrings((e_ErrorType)GET_ERROR_TYPE(_err))); \394XX_Print _vmsg; \395XX_Print("\r\n"); \396} \397} while (0)398399400#define RETURN_ERROR(_level, _err, _vmsg) \401do { \402REPORT_ERROR(_level, (_err), _vmsg); \403return ERROR_CODE(_err); \404} while (0)405406407#if (REPORT_EVENTS > 0)408409#define REPORT_EVENT(_ev, _appId, _flg, _vmsg) \410do { \411if (_ev##_LEVEL <= EVENT_DYNAMIC_LEVEL) { \412XX_Print("~ %s %s Event " PRINT_FORMAT ": %s (flags: 0x%04x); ", \413dbgLevelStrings[_ev##_LEVEL - 1], \414__STRING(__ERR_MODULE__), \415PRINT_FMT_PARAMS, \416eventStrings[((_ev) - EV_NO_EVENT - 1)], \417(uint16_t)(_flg)); \418XX_Print _vmsg; \419XX_Print("\r\n"); \420XX_EventById((uint32_t)(_ev), (t_Handle)(_appId), (uint16_t)(_flg), NO_MSG); \421} \422} while (0)423424#else /* not REPORT_EVENTS */425426#define REPORT_EVENT(_ev, _appId, _flg, _vmsg)427428#endif /* (REPORT_EVENTS > 0) */429430#endif /* (DEBUG_ERRORS > 0) */431432433/**************************************************************************//**434@Function ASSERT_COND435436@Description Assertion macro.437438@Param[in] _cond - The condition being checked, in positive form;439Failure of the condition triggers the assert.440*//***************************************************************************/441#ifdef DISABLE_ASSERTIONS442#define ASSERT_COND(_cond)443#else444#define ASSERT_COND(_cond) \445do { \446if (!(_cond)) { \447XX_Print("*** ASSERT_COND failed " PRINT_FORMAT "\r\n", \448PRINT_FMT_PARAMS); \449XX_Exit(1); \450} \451} while (0)452#endif /* DISABLE_ASSERTIONS */453454455#ifdef DISABLE_INIT_PARAMETERS_CHECK456457#define CHECK_INIT_PARAMETERS(handle, f_check)458#define CHECK_INIT_PARAMETERS_RETURN_VALUE(handle, f_check, retval)459460#else461462#define CHECK_INIT_PARAMETERS(handle, f_check) \463do { \464t_Error err = f_check(handle); \465if (err != E_OK) { \466RETURN_ERROR(MAJOR, err, NO_MSG); \467} \468} while (0)469470#define CHECK_INIT_PARAMETERS_RETURN_VALUE(handle, f_check, retval) \471do { \472t_Error err = f_check(handle); \473if (err != E_OK) { \474REPORT_ERROR(MAJOR, err, NO_MSG); \475return (retval); \476} \477} while (0)478479#endif /* DISABLE_INIT_PARAMETERS_CHECK */480481#ifdef DISABLE_SANITY_CHECKS482483#define SANITY_CHECK_RETURN_ERROR(_cond, _err)484#define SANITY_CHECK_RETURN_VALUE(_cond, _err, retval)485#define SANITY_CHECK_RETURN(_cond, _err)486#define SANITY_CHECK_EXIT(_cond, _err)487488#else /* DISABLE_SANITY_CHECKS */489490#define SANITY_CHECK_RETURN_ERROR(_cond, _err) \491do { \492if (!(_cond)) { \493RETURN_ERROR(CRITICAL, (_err), NO_MSG); \494} \495} while (0)496497#define SANITY_CHECK_RETURN_VALUE(_cond, _err, retval) \498do { \499if (!(_cond)) { \500REPORT_ERROR(CRITICAL, (_err), NO_MSG); \501return (retval); \502} \503} while (0)504505#define SANITY_CHECK_RETURN(_cond, _err) \506do { \507if (!(_cond)) { \508REPORT_ERROR(CRITICAL, (_err), NO_MSG); \509return; \510} \511} while (0)512513#define SANITY_CHECK_EXIT(_cond, _err) \514do { \515if (!(_cond)) { \516REPORT_ERROR(CRITICAL, (_err), NO_MSG); \517XX_Exit(1); \518} \519} while (0)520521#endif /* DISABLE_SANITY_CHECKS */522523/** @} */ /* end of Debug/error Utils group */524525/** @} */ /* end of General Utils group */526527#endif /* __ERROR_EXT_H */528529530531532