Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / pci / n8 / helper.c
blob89b5db73e32985c0d28cbb803ff8dd2bb8a36608
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: helper.c,v 1.2 2008/11/03 04:31:01 tls Exp $";
36 /*****************************************************************************/
37 /** @file helper.c *
38 * @brief BSDi System Call Abstraction. *
39 * *
40 * This file implements the BSDi versions of some standard system calls *
41 * that drivers commonly make, in a variety of platforms. It also ensures *
42 * that the necessary system includes are included as well. *
43 *****************************************************************************/
45 /*****************************************************************************
46 * Revision history:
47 * 02/12/04 bac Fixed n8_usleep to work when the tick interval changes.
48 * 05/05/03 brr Moved memory functions to n8_memory_bsd.c.
49 * 04/23/03 brr Remove warnings from n8_delay_ms.
50 * 03/12/03 jpw Added function n8_delay_ms - keep this spinlock safe!
51 * 12/10/02 brr Added function n8_gettime.
52 * 10/25/02 brr Clean up function prototypes & include files.
53 * 06/12/02 brr Added N8_WaitEventInt function.
54 * 04/02/02 spm Changed usec normalizer in n8_usleep from 1000 to 1000000.
55 * this assures that units of jiffies are passed to tsleep.
56 * Added case to sleep for minimum yield if usecs is less
57 * than minimum yield.
58 * 03/11/02 brr Update for 2.1.
59 * 10/12/01 mmd Renamed Atomic*() routines to N8_Atomic*().
60 * 10/04/01 mmd Original version.
61 ****************************************************************************/
62 /** @defgroup NSP2000Driver BSDi System Call Abstraction.
65 #include "helper.h"
66 #include "irq.h"
67 #include "n8_pub_errors.h"
68 #include "n8_driver_main.h"
70 #include <sys/proc.h>
72 /* Since BSD does not give us a mechanism to update the write pointer */
73 /* and ensure this process is asleep before the interrupt fires, this */
74 /* fuction attempts to solve the problem. It goes to sleep for an */
75 /* arbitrary long wait. If the ISR wakes this process up, we check */
76 /* the request immediately and return on realizing the event has */
77 /* completed. If the ISR fails to wake us because we are not */
78 /* completely suspended, the timeout guarentees we wake up eventually */
79 /* and check to see if the ISR has marked our request as complete. In */
80 /* which case we return succes. Otherwise we exceed out timeout count */
81 /* and return FAILURE. The count value may need adjustment based on */
82 /* traffic load on the system. */
84 int N8_WaitEventInt(n8_WaitQueue_t *waitQueue_p, API_Request_t *API_req_p)
86 int count = 0;
90 if (API_req_p->qr.requestStatus == N8_QUEUE_REQUEST_FINISHED)
92 /* The event has completed, return success. */
93 return 0;
95 tsleep(waitQueue_p, PWAIT, "QMQueue", (1*HZ));
96 count++;
98 while (count < 5);
100 return (1);
103 /*****************************************************************************
104 * N8_BlockWithTimeout
105 *****************************************************************************/
106 /** @ingroup NSP2000Driver
107 * @brief Block for notification or specified timeout.
109 * This routine abstracts the Linux system call to block until release or
110 * timeout.
112 * @param *WaitQueue RO: Specifies the block element
113 * @param timeout RO: Specifies the timeout
114 * @param debug RO: Specifies whether to display debug messages
116 * @par Externals:
117 * N/A
119 * @return
120 * 1 = success - was woken up by corresponding call to wakeup.
121 * 0 = failure - timed out.
123 * @par Errors:
124 * See return section for error information.
125 *****************************************************************************/
127 unsigned char N8_BlockWithTimeout( wait_queue_head_t *WaitQueue,
128 unsigned long timeout,
129 unsigned char debug )
131 int rc;
133 /* BLOCK UNTIL RELEASE, TIMEOUT, OR SIGNAL */
134 rc = tsleep(WaitQueue, (PWAIT | PCATCH), "N8_BlockWithTimeout", (timeout * hz));
135 if (!rc)
137 if (debug)
139 printf("NSP2000: Received desired interrupt.\n");
141 return 1;
143 if (debug && (rc == EWOULDBLOCK))
145 printf("NSP2000: Timed out while awaiting interrupt.\n");
147 else if (debug)
149 printf("NSP2000: An unexpected signal has interrupted N8_BlockWithTimeout.\n");
151 return 0;
155 #if 0
156 /*****************************************************************************
157 * n8_usleep
158 *****************************************************************************/
159 /** @ingroup NSP2000Driver
160 * @brief Sleep usec seconds.
162 * The minimum delay is N8_MINIMUM_YIELD_USECS, which is usually a system
163 * clock tick.
166 * @param usecs RO: Microseconds to sleep
168 * @par Externals:
169 * HZ is the CPU clock speed in Hertz (defined in Linux
170 * kernel headers; usually 100 Hz).
172 * @return
173 * N8_STATUS_OK Success
175 * @par Errors:
176 * See return section.
178 * @par Locks:
179 * None.
181 * @par Assumptions:
183 *****************************************************************************/
185 N8_Status_t
186 n8_usleep(unsigned int usecs)
188 char tmp;
190 /* the minimum amount to sleep is 1 tick. if requested sleep time is less
191 * than 1 tick, sleep for 1 tick. */
192 if (usecs < N8_USECS_PER_TICK)
194 tsleep(&tmp,PZERO,"NSP2000 udelay", 1 /* tick */);
196 else
198 tsleep(&tmp,PZERO,"NSP2000 udelay",((usecs)*hz/N8_USECS_PER_SEC));
201 return N8_STATUS_OK;
205 /*****************************************************************************
206 * n8_delay_ms
207 *****************************************************************************/
208 /** @ingroup NSP2000Driver
209 * @brief Sleep for given milliseconds.
212 * @param usecs RO: Microseconds to sleep
214 * @par Externals:
215 * HZ is the CPU clock speed in Hertz (defined in Linux
216 * kernel headers; usually 100 Hz).
218 * @return
219 * N8_STATUS_OK Success
221 * @par Errors:
222 * See return section.
224 * @par Locks:
225 * None.
227 * @par Assumptions:
229 *****************************************************************************/
231 N8_Status_t
232 n8_delay_ms(unsigned int milliseconds)
235 DELAY((milliseconds*1000));
237 return N8_STATUS_OK;
240 /*****************************************************************************
241 * n8_gettime
242 *****************************************************************************/
243 /** @ingroup NSP2000Driver
244 * @brief Get current timeval
246 * @param n8_timeResults_p WO: Returned time value in n8_timeval_t,
247 * sec and usec
249 * @par Externals:
250 * None.
252 * @return
253 * N8_STATUS_OK Success
255 * @par Errors:
256 * None.
258 * @par Locks:
259 * None.
261 * @par Assumptions:
262 * None.
264 *****************************************************************************/
266 N8_Status_t
267 n8_gettime( n8_timeval_t *n8_timeResults_p )
270 struct timespec ts;
271 N8_Status_t returnResults = N8_STATUS_OK;
273 getnanotime(&ts);
275 /* Timespec has a seconds portion and a nano seconds portion. */
276 /* Thus we need to divide to convert nanoseconds to microseconds. */
277 n8_timeResults_p->tv_sec = ts.tv_sec;
278 n8_timeResults_p->tv_usec = ts.tv_nsec / 1000;
280 return returnResults;
282 } /* n8_gettime */
284 #endif