Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / pci / n8 / common / api / n8_callback.c
blob312cb80b25569a6f8aba86810cf1fbcc8ad18260
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 static char const n8_id[] = "$Id: n8_callback.c,v 1.1 2008/10/30 12:02:14 darran Exp $";
36 /*****************************************************************************/
37 /** @file n8_callback.c
38 * @brief Routines for implementing the API callbacks.
40 *****************************************************************************/
42 /*****************************************************************************
43 * Revision history:
45 * 06/26/03 brr Send signal to kernel callback thread when terminated.
46 * 06/25/03 brr Moved all thread data into the data structure allocated by
47 * callbackInit so that a new callback thread can be started
48 * immediately after terminating one.
49 * 06/23/03 brr Verify previous callback thread has terminated completely
50 * before attempting to start another. (Bug 934)
51 * 04/24/03 brr Deallocate resources in callbackShutdown when no thread was
52 * started. Modified N8_EventPoll for efficiency and to return
53 * the number of completed events.
54 * 04/08/03 brr Fixed warnings.
55 * 04/03/03 brr Removed overhead of calling N8_EventCheck from the callback
56 * thread to improve performance.
57 * 04/01/03 brr Include n8_sdk_config instead of n8_driver_parms.
58 * 04/01/03 brr Updated for changes to N8_WaitOnRequest.
59 * 03/24/03 brr Modified thread to wait on AMBA interrupt when no requests
60 * are queued.
61 * 03/20/03 brr Updated for changes to N8_WaitOnRequest.
62 * 02/25/03 brr Original version.
63 ****************************************************************************/
64 /** @defgroup n8_event Callback processing
67 #include "n8_common.h"
68 #include "n8_pub_service.h"
69 #include "n8_OS_intf.h"
70 #include "n8_malloc_common.h"
71 #include "n8_semaphore.h"
72 #include "n8_sdk_config.h"
73 #include "n8_driver_api.h"
74 #include "n8_util.h"
76 #ifdef SUPPORT_CALLBACK_THREAD
78 typedef struct
80 int n8_thread;
81 int timeout;
82 volatile int eventReadIndex;
83 volatile int eventWriteIndex;
84 int eventMax;
85 n8_Lock_t eventLock;
86 N8_Event_t n8_Events[0];
87 } N8_CallbackData_t;
89 static N8_CallbackData_t *n8_callbackData_gp;
90 static N8_Thread_t n8_callbackThread_g;
92 #ifdef __KERNEL__
93 struct task_struct *threadStruct;
94 #endif
95 /*****************************************************************************
96 * queueEvent
97 *****************************************************************************/
98 /** @ingroup n8_event
99 * @brief This function inserts an event into the SDK's event queue.
101 * This function inserts an event into the SDK's event queue. This function
102 * takes the eventLock_g to ensure mutual exclusion.
104 * @par Externals
105 * n8_callback_gp - Pointer to the callback threads data structure.
107 * @return
108 * N8_STATUS_OK - all's well.<br>
109 * N8_EVENT_QUEUE_FULL - No entries are available in the event queue.<br>
111 * @par Errors
112 * <description of possible errors><br>
114 * @par Assumptions
115 * This is the only function that should increment eventWriteIndex,
116 * the write index in the SDK's list of Events.
117 *****************************************************************************/
118 N8_Status_t queueEvent(N8_Event_t *event_p)
120 int nextIndex;
121 N8_Status_t ret = N8_EVENT_QUEUE_FULL;
123 if (n8_callbackData_gp->eventMax != 0)
125 /* Acquire eventLock_g to update eventWriteIndex_g */
126 N8_acquireProcessSem(&n8_callbackData_gp->eventLock);
128 /* Calculate the next index */
129 nextIndex = n8_callbackData_gp->eventWriteIndex + 1;
130 if (nextIndex == n8_callbackData_gp->eventMax)
132 nextIndex = 0;
135 if (nextIndex != n8_callbackData_gp->eventReadIndex)
137 /* The queue is not full, queue the event */
138 n8_callbackData_gp->n8_Events[n8_callbackData_gp->eventWriteIndex] = *event_p;
139 n8_callbackData_gp->eventWriteIndex = nextIndex;
140 ret = N8_STATUS_OK;
143 /* Release the eventLock_g */
144 N8_releaseProcessSem(&n8_callbackData_gp->eventLock);
147 return ret;
151 /*****************************************************************************
152 * n8_callbackThread
153 *****************************************************************************/
154 /** @ingroup n8_event
155 * @brief Runs as a seperate thread polling the list of events and completing
156 * them.
158 * This function is started as a seperate thread. It continually runs through
159 * the SDK's internal list of events, checking each one for its
160 * completion status. If the event is completed, the user's callback is
161 * invoked and the event freed.
163 * @par Externals
165 * @return
166 * N8_STATUS_OK - all's well.<br>
168 * @par Errors
169 * <description of possible errors><br>
171 * @par Assumptions
172 * This is the only function that should increment eventReadIndex,
173 * the read index in the SDK's list of Events.
174 * N8_EventPoll should not be called if this thread is running.
175 *****************************************************************************/
176 N8_Status_t n8_callbackThread(N8_CallbackData_t *callbackData_p)
178 int nextRead;
179 N8_Event_t *events_p;
180 QMgrRequest_t *qreq_p = NULL;
181 N8_Status_t usrStatus;
183 /* Perform Linux Kernal specific initialization */
184 #ifdef __KERNEL__
185 threadStruct = current;
186 /* Mask out the unwanted signals */
187 current->blocked.sig[0] |= sigmask(SIGINT) | sigmask(SIGTERM);
188 recalc_sigpending(current);
190 /* set name of this process (max 15 chars + 0 !) */
191 sprintf(current->comm, "NSP2000 Thread");
192 #endif
194 while (callbackData_p->n8_thread == TRUE)
196 if (callbackData_p->eventReadIndex != callbackData_p->eventWriteIndex)
198 events_p = &callbackData_p->n8_Events[callbackData_p->eventReadIndex];
199 qreq_p = (QMgrRequest_t *) events_p->state;
201 if ( qreq_p->requestStatus == N8_QUEUE_REQUEST_FINISHED )
203 if ( qreq_p->requestError != N8_QUEUE_REQUEST_ERROR )
205 /* Do the callback if needed. */
206 if ( qreq_p->callback != NULL )
208 qreq_p->callback( qreq_p );
210 /* events_p->status = N8_QUEUE_REQUEST_FINISHED; */
211 usrStatus = N8_STATUS_OK;
213 else
215 /* events_p->status = N8_QUEUE_REQUEST_COMMAND_ERROR; */
216 usrStatus = N8_HARDWARE_ERROR;
219 /* free the request. */
220 freeRequest((API_Request_t *)qreq_p);
222 /* Perform the user's callback. */
223 if (events_p->usrCallback)
225 events_p->usrCallback( events_p->usrData, usrStatus );
228 /* Free the position in the Event queue */
229 nextRead = callbackData_p->eventReadIndex + 1;
230 if (nextRead == callbackData_p->eventMax)
232 nextRead = 0;
234 callbackData_p->eventReadIndex = nextRead;
236 else
238 N8_WaitOnRequest(callbackData_p->timeout);
241 else
243 N8_WaitOnRequest(callbackData_p->timeout);
247 /* Free the resources allocated by callback initialization. */
248 /* This is done here instead of in callbackShutdown to avoid freeing */
249 /* resources while the callback thread continues to reference them. */
250 callbackData_p->eventMax = 0;
251 N8_deleteProcessSem(&callbackData_p->eventLock);
252 N8_UFREE(callbackData_p);
254 return N8_STATUS_OK;
257 /*****************************************************************************
258 * n8_callbackInit
259 *****************************************************************************/
260 /** @ingroup n8_event
261 * @brief Allocates resources for the event queue and starts callback thread.
263 * This function is called by N8_InitializeAPI. It allocates resources used
264 * by callback functions and starts a seperate thread if the timeout value is
265 * non-zero. If the timeout is zero, the resources are allocated, but it is
266 * assume the user will be calling N8_EventPoll to complete the events.
268 * @par Externals
269 * n8_callback_gp - Pointer to the callback threads data structure.
271 * @return
272 * N8_STATUS_OK - all's well.<br>
273 * N8_EVENT_ALLOC_FAILED - insufficient resources to allocate event array.<br>
275 * @par Errors
276 * <description of possible errors><br>
278 * @par Assumptions
279 *****************************************************************************/
280 N8_Status_t callbackInit(uint32_t numEvents, uint32_t timeout)
282 int dataSize = sizeof(N8_CallbackData_t) +
283 ((numEvents + 1) * sizeof(N8_Event_t));
285 n8_callbackData_gp = N8_UMALLOC(dataSize);
287 if (n8_callbackData_gp == NULL)
289 return (N8_EVENT_ALLOC_FAILED);
292 memset(n8_callbackData_gp, 0, dataSize);
293 N8_initProcessSem(&n8_callbackData_gp->eventLock);
294 n8_callbackData_gp->eventMax = numEvents;
295 if (timeout)
297 n8_callbackData_gp->n8_thread = TRUE;
298 n8_callbackData_gp->timeout = timeout;
299 N8_THREAD_INIT(n8_callbackThread, n8_callbackData_gp, n8_callbackThread_g);
302 return(N8_STATUS_OK);
305 /*****************************************************************************
306 * n8_callbackShutdown
307 *****************************************************************************/
308 /** @ingroup n8_event
309 * @brief Sets the global flag to alert the callback thread to shutdown.
311 * This function is called by N8_TerminateAPI. It Sets the flag n8_thread
312 * flag to alert the callback thread to shutdown or frees resources if
313 * polling was configured.
315 * @par Externals
316 * n8_callback_gp - Pointer to the callback threads data structure.
318 * @return
319 * N8_STATUS_OK - all's well.<br>
321 * @par Errors
322 * <description of possible errors><br>
324 * @par Assumptions
325 *****************************************************************************/
326 N8_Status_t callbackShutdown(void)
329 if (n8_callbackData_gp->n8_thread == FALSE)
331 /* There was no callback thread started, so it is safe to deallocate */
332 /* resources immediately. */
333 n8_callbackData_gp->eventMax = 0;
334 N8_deleteProcessSem(&n8_callbackData_gp->eventLock);
335 N8_UFREE(n8_callbackData_gp);
337 else
339 /* If a callback thread was started, just set the n8_thread */
340 /* flag so the thread terminates, then deallocate resources. */
341 n8_callbackData_gp->n8_thread = FALSE;
343 #ifdef __KERNEL__
344 send_sig(SIGKILL, threadStruct, 0);
345 #endif
349 return(N8_STATUS_OK);
353 /*****************************************************************************
354 * N8_EventPoll
355 *****************************************************************************/
356 /** @ingroup n8_event
357 * @brief Run through the list of events and complete them.
359 * Run through the SDK's internal list of events, checking each one for its
360 * completion status. If the event is completed, the user's callback is
361 * invoked and the event freed.
363 * @par Externals
364 * n8_callback_gp - Pointer to the callback threads data structure.
366 * @return
367 * N8_STATUS_OK - all's well.<br>
369 * @par Errors
370 * <description of possible errors><br>
372 * @par Assumptions
373 * This is the only function that should increment eventReadIndex,
374 * the read index in the SDK's list of Events.
375 * This function should not be called if the callback thread is running.
376 *****************************************************************************/
377 int N8_EventPoll(void)
379 int nextRead;
380 N8_Event_t *events_p;
381 int eventsComplete = 0;
382 QMgrRequest_t *qreq_p = NULL;
383 N8_Status_t usrStatus;
385 while (n8_callbackData_gp->eventReadIndex != n8_callbackData_gp->eventWriteIndex)
387 events_p = &n8_callbackData_gp->n8_Events[n8_callbackData_gp->eventReadIndex];
388 qreq_p = (QMgrRequest_t *) events_p->state;
390 if ( qreq_p->requestStatus == N8_QUEUE_REQUEST_FINISHED )
392 if ( qreq_p->requestError != N8_QUEUE_REQUEST_ERROR )
394 /* Do the callback if needed. */
395 if ( qreq_p->callback != NULL )
397 qreq_p->callback( qreq_p );
399 /* events_p->status = N8_QUEUE_REQUEST_FINISHED; */
400 usrStatus = N8_STATUS_OK;
402 else
404 /* events_p->status = N8_QUEUE_REQUEST_COMMAND_ERROR; */
405 usrStatus = N8_HARDWARE_ERROR;
408 /* free the request. */
409 freeRequest((API_Request_t *)qreq_p);
411 /* Perform the user's callback. */
412 if (events_p->usrCallback)
414 events_p->usrCallback( events_p->usrData, usrStatus );
417 /* Free the position in the Event queue */
418 nextRead = n8_callbackData_gp->eventReadIndex + 1;
419 if (nextRead == n8_callbackData_gp->eventMax)
421 nextRead = 0;
423 n8_callbackData_gp->eventReadIndex = nextRead;
424 eventsComplete++;
428 return eventsComplete;
430 #endif