Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / pci / n8 / common / api / n8_util.h
blob45b0879a688b69039170ac0ade39bbfe489684be
1 /*-
2 * Copyright (C) 2001-2003 by NBMK Encryption Technologies.
3 * All rights reserved.
5 * NBMK Encryption Technologies provides no support of any kind for
6 * this software. Questions or concerns about it may be addressed to
7 * the members of the relevant open-source community at
8 * <tech-crypto@netbsd.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
12 * met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 /*****************************************************************************
36 * @(#) n8_util.h 1.65@(#)
37 *****************************************************************************/
39 /*****************************************************************************/
40 /** @file n8_util.h
41 * @brief Header file for utility functions
43 * Utility function prototypes
45 *****************************************************************************/
47 /*****************************************************************************
48 * Revision history:
49 * 08/18/03 brr Eliminate unused parameter passage.
50 * 06/06/03 brr Move n8_enums to public include as n8_pub_enums.
51 * 05/19/03 brr Remove obsolete prototypes, conver freeRequest to a macro.
52 * 03/10/03 brr Added support for API callbacks.
53 * 07/14/02 bac Corrected chip type to be the standard N8_Unit_t.
54 * 06/10/02 hml Added proto for initializeEARequestBuffer.
55 * 05/07/02 msz Mark synchronous requests so QMgr will pend until they are
56 * done.
57 * 05/07/02 bac Changed output of RESULT_HANDLER_WARNING macro to print the
58 * error code in hex.
59 * 03/28/02 hml Updated QUEUE_AND_CHECK, since N8_QMgrQueue now returns
60 * an N8_Status_t.
61 * 03/26/02 hml Added proto for n8_validateUnit.
62 * 03/26/02 brr Allocate the data buffer as part of the API request.
63 * 02/28/02 brr Do not include any QMgr include files.
64 * 02/25/02 brr Updated RESULT_HANDLER_WARNING to use N8_PRINT.
65 * 11/27/01 bac Added prototype for n8_handleEvent.
66 * 11/13/01 dkm Removed unused DELETE_OBJECT and CREATE_KOBJECT macros.
67 * 11/14/01 bac Added error message to QUEUE_AND_CHECK.
68 * 11/11/01 bac Added n8_sizedBufferCmp prototype.
69 * 11/06/01 hml Added the CHECK_STRUCTURE macro and cleaned up a little.
70 * 10/30/01 bac Added functions n8_decrLength64 and n8_incrLength64.
71 * 10/11/01 brr Modified QUEUE_AND_CHECK macros to pass back different error
72 * code. This fixed a memory leak when queue operation failed.
73 * 10/11/01 hml Added n8_strdup prototype.
74 * 10/08/01 hml Added printHWError
75 * 10/02/01 bac Added support for n8_enums. Created RESULT_HANDLER_WARNING.
76 * 09/20/01 bac Tightened up some signatures to have unsigned ints and const
77 * modifiers.
78 * 09/18/01 bac Added CEIL macro, changed signature to createEARequestBuffer
79 * to add numCmds.
80 * 09/14/01 bac Moved debug command block printing macros and prototypes to
81 * this common location.
82 * 08/30/01 msz Include "QMQueue.h" for QMgrQueue to prevent a warning.
83 * 08/16/01 mmd Now including n8_driver_api.h instead of simon.h.
84 * 08/20/01 msz Fixed REQUEST_SUCCESSFULLY_ENQUEUEDs to be N8_QUEUE_SUCCESS,
85 * as these were equivalent,
86 * and ENQUEUE_REQUEST_SUCCESSFULLY_ENQUEUED was removed.
87 * Also go directly to QM rather than EA or PK.
88 * 07/30/01 bac Fixed HANDLE_EVENT to check return code in the event.
89 * 07/20/01 bac Added chip id to calls to create[PK|EA]RequestBuffer.
90 * 07/05/01 hml Copy unit type from request to event in HANDLE_EVENT
91 * 06/25/01 bac Added CREATE_UOBJECTSET macro
92 * 06/19/01 bac Added CREATE_UOBJECT macro
93 * 06/19/01 bac Added prototype for addMemoryStructToFree and updated
94 * freeRequest to handle N8_MemoryHandle_t entries
95 * 06/17/01 bac Added removeBlockFromFreelist prototype.
96 * 05/18/01 bac Removed unnecessary check for null in DELETE_OBJECT and
97 * redundant memset in CREATE_OBJECT.
98 * 05/04/01 bac Added macro CHECK_RETURN
99 * 04/26/01 bac Added prototypes for create[PK|EA]RequestBuffer
100 * 04/24/01 bac Original version.
101 ****************************************************************************/
103 #ifndef N8_UTIL_H
104 #define N8_UTIL_H
106 #include "n8_common.h"
107 #include "n8_pub_errors.h"
108 #include "n8_enqueue_common.h"
109 #include "n8_malloc_common.h"
110 #include "n8_driver_api.h"
111 #include "n8_pub_enums.h"
113 /* macros */
114 /* round up to the next word boundary */
115 #define NEXT_WORD_SIZE(_D) ((((_D)+3)>>2)<<2)
117 #define CEIL(__A,__B) (((__A)+(__B)-1)/(__B))
119 #define HANDLE_EVENT(__EV_P, __REQ_P, __RTN) \
120 (__RTN) = n8_handleEvent((__EV_P), (void *) (__REQ_P), \
121 (__REQ_P)->qr.unit, \
122 (__REQ_P)->qr.requestStatus);
125 #define N8_SET_EVENT_FINISHED(__EP, __UNIT) \
126 (__EP)->unit = (__UNIT); \
127 (__EP)->state = NULL; \
128 (__EP)->status = N8_QUEUE_REQUEST_FINISHED;
131 #define CHECK_RETURN(RET) \
132 if ((RET) != N8_STATUS_OK) \
134 break; \
137 #define CHECK_OBJECT(OBJ, RETRN) \
138 if ((OBJ) == NULL) \
140 (RETRN) = N8_INVALID_OBJECT; \
141 break; \
144 #define CHECK_STRUCTURE(STRUCT, ID, RETRN) \
145 if ((STRUCT) != (ID)) \
147 (RETRN) = N8_INVALID_OBJECT; \
148 break; \
151 #define CREATE_UOBJECT(OBJ, _TYPE, RETRN) \
152 OBJ = (_TYPE *) N8_UMALLOC(sizeof(_TYPE)); \
153 if (OBJ == NULL) \
155 RETRN = N8_MALLOC_FAILED; \
156 break; \
159 #define CREATE_UOBJECTSET(OBJ, _TYPE, _NUM, RETRN) \
160 OBJ = (_TYPE *) N8_UMALLOC(sizeof(_TYPE) * (_NUM)); \
161 if (OBJ == NULL) \
163 RETRN = N8_MALLOC_FAILED; \
164 break; \
167 #define CREATE_UOBJECT_SIZE(OBJ, SIZE, RETRN) \
168 OBJ = N8_UMALLOC(SIZE); \
169 if (OBJ == NULL) \
171 RETRN = N8_MALLOC_FAILED; \
172 break; \
175 /* Note RNG requests currently do not use QUEUE_AND_CHECK */
176 #ifdef SUPPORT_CALLBACKS
177 #define QUEUE_AND_CHECK(__EV_P,__CMD_P, _RTN) \
179 if (__EV_P == NULL) \
181 __CMD_P->qr.synchronous = N8_TRUE; \
183 else \
185 __CMD_P->usrCallback = __EV_P->usrCallback; \
186 __CMD_P->usrData = __EV_P->usrData; \
188 _RTN = N8_QMgrQueue((__CMD_P)); \
189 if (_RTN != N8_STATUS_OK) \
191 break; \
194 #else
195 #define QUEUE_AND_CHECK(__EV_P,__CMD_P, _RTN) \
197 if (__EV_P == NULL) \
199 __CMD_P->qr.synchronous = N8_TRUE; \
201 _RTN = N8_QMgrQueue((__CMD_P)); \
202 if (_RTN != N8_STATUS_OK) \
204 break; \
207 #endif
209 #define freeRequest(_REQ_P) \
210 if (_REQ_P != NULL && ((API_Request_t *)_REQ_P)->userRequest != N8_TRUE) \
212 N8_KFREE((N8_MemoryHandle_t *)((int)_REQ_P-N8_BUFFER_HEADER_SIZE)); \
216 #define RESULT_HANDLER_WARNING(__TITLE__, __REQ_P__) \
217 N8_PRINT("%s called with error.\n" \
218 "simon error %x on command %d of %d\n", \
219 (__TITLE__), \
220 (__REQ_P__)->err_rpt_bfr.errorReturnedFromSimon, \
221 (__REQ_P__)->err_rpt_bfr.indexOfCommandThatCausedError, \
222 (__REQ_P__)->numNewCmds); \
223 printHWError((__REQ_P__)->err_rpt_bfr.errorReturnedFromSimon, \
224 (__REQ_P__)->qr.unit);
226 /* debugging macros */
227 #ifdef N8DEBUG
228 #define DBG_PRINT_PK_CMD_BLOCKS(A,B,C) \
229 printPKCommandBlocks((A),(B),(C))
231 #define DBG_PRINT_EA_CMD_BLOCKS(A,B,C) \
232 printEACommandBlocks((A),(B),(C))
233 #else
234 #define DBG_PRINT_PK_CMD_BLOCKS(A,B,C)
235 #define DBG_PRINT_EA_CMD_BLOCKS(A,B,C)
236 #endif
238 /* Prototypes */
239 void printN8Buffer(N8_Buffer_t *p, const unsigned int size);
240 void n8_displayBuffer(N8_Buffer_t *buf_p, const uint32_t length,
241 const char *name);
242 void initializeEARequestBuffer(API_Request_t *req_p,
243 N8_MemoryHandle_t *kmem_p,
244 const N8_Unit_t chip,
245 const unsigned int numCmds,
246 const void *callbackFcn,
247 N8_Boolean_t userRequest);
249 N8_Status_t createEARequestBuffer(API_Request_t **req_pp,
250 const N8_Unit_t chip,
251 const unsigned int numCmds,
252 const void *callbackFcn,
253 const unsigned int dataBytes);
255 N8_Status_t createPKRequestBuffer(API_Request_t **req_pp,
256 const N8_Unit_t chip,
257 const unsigned int numCmds,
258 const void *callbackFcn,
259 const unsigned int dataBytes);
261 void printCommandBlock(uint32_t *u, unsigned int size);
262 void printPKCommandBlocks(const char *title, PK_CMD_BLOCK_t *block_p, uint32_t num_blocks);
263 void printEACommandBlocks(const char *title, EA_CMD_BLOCK_t *block_p, uint32_t num_blocks);
264 void printHWError(uint32_t errCode, N8_Component_t unit);
265 char *n8_strdup(const char *str_p);
266 void n8_decrLength64(uint32_t *hi_word_p,
267 uint32_t *lo_word_p,
268 const uint32_t length);
269 void n8_incrLength64(uint32_t *hi_word_p,
270 uint32_t *lo_word_p,
271 const uint32_t length);
273 int n8_sizedBufferCmp(const N8_SizedBuffer_t *a_p,
274 const N8_SizedBuffer_t *b_p);
275 void resultHandlerGeneric(API_Request_t* req_p);
276 N8_Status_t n8_handleEvent(N8_Event_t *e_p, void *req_p,
277 N8_Unit_t unit,
278 N8_QueueStatusCodes_t requestStatus);
279 N8_Boolean_t n8_validateUnit(N8_Unit_t chip);
281 N8_Status_t queueEvent(N8_Event_t *event_p);
282 N8_Status_t callbackInit(uint32_t numEvents, uint32_t timeout);
283 N8_Status_t callbackShutdown(void);
284 N8_Status_t n8_getConfigInfo(N8_ConfigAPI_t *config_p);
286 #endif /* N8_UTIL_H */