1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/*-
* Copyright (C) 2001-2003 by NBMK Encryption Technologies.
* All rights reserved.
*
* NBMK Encryption Technologies provides no support of any kind for
* this software. Questions or concerns about it may be addressed to
* the members of the relevant open-source community at
* <tech-crypto@netbsd.org>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*****************************************************************************
* @(#) userPool.c 1.10@(#)
*****************************************************************************/
/*****************************************************************************/
/** @file userPool.c *
* @brief Memory Services - Cross-Platform *
* *
* This file implements the kernel onlu portions of the user pool allocation *
* and management service for the NSP2000 devices. *
* *
* *
* userPoolInit - *
* Obtains the large allocated space. It allocates the large pool *
* with n8_GetLargeAllocation (platform-specific). *
* userPoolRelease - *
* Purges the allocation map and deallocates the large allocation space.*
* *
*****************************************************************************/
/*****************************************************************************
* Revision history:
* 05/07/03 brr Modified user pools to take advantage of the support for
* multiple memory banks.
* 04/23/03 brr Removed redundant parameter from n8_FreeLargeAllocation.
* 04/21/03 brr Added support for multiple memory banks.
* 07/15/02 brr Do not take userPoolSem in userPoolAlloc if not initialzed.
* 07/11/02 brr Do not fail userPoolInit if banks is 0.
* 07/02/02 brr Added userPoolStats.
* 06/26/02 brr Remove bank parameter from calls to N8_PhysToVirt.
* 06/24/02 brr Original version.
****************************************************************************/
/** @defgroup NSP2000Driver Memory Allocation Services - Cross-Platform.
*/
#include "n8_OS_intf.h"
#include "helper.h"
#include "n8_driver_parms.h"
#include "n8_malloc_common.h"
#include "n8_memory.h"
#include "userPool.h"
static int userPoolBanks = 0;
static ATOMICLOCK_t userPoolSem;
static unsigned long userPoolBase[DEF_USER_POOL_BANKS];
static n8_UserPoolState_t userPoolState[DEF_USER_POOL_BANKS];
static int userPoolID[DEF_USER_POOL_BANKS];
/*****************************************************************************
* userPoolDisplay
*****************************************************************************/
/** @ingroup NSP2000Driver
* @brief Displays the current state of the user pool.
*
* This function displays the current state of the user pool.
*
* @par Externals:
*
* @return
*
* @par Errors:
* See return section for error information.
*****************************************************************************/
void userPoolDisplay(void)
{
int ctr;
if (userPoolBanks == 0)
{
N8_PRINT( "userPool: <************* USER POOL UNINITIALIZED ***********>\n");
return;
}
N8_PRINT( "userPool: <***************** USER POOL MAP *****************>\n");
N8_PRINT( "userPool: banks = %d \n", userPoolBanks);
for (ctr = 0; ctr < userPoolBanks; ctr++)
{
N8_PRINT( "userPool: <*************** USER POOL BANK #%d ***************>\n", ctr);
N8_PRINT( "userPool: base address = %x, bank state = %d, session ID = %d, \n",
(int)(userPoolBase[ctr]),
userPoolState[ctr], userPoolID[ctr]);
if (userPoolState[ctr] != POOL_FREE)
{
n8_memoryDisplay(N8_MEMBANK_USERPOOL + ctr);
}
}
N8_PRINT( "userPool: <********* USER POOL ALLOCATION MAP END **********>\n");
}
/*****************************************************************************
* userPoolCount
*****************************************************************************/
/** @ingroup NSP2000Driver
* @brief Returns the number of allocated user pools.
*
* This function returns the number of allocated user pools.
*
* @par Externals:
*
* @return
*
* @par Errors:
* See return section for error information.
*****************************************************************************/
int userPoolCount(void)
{
return(userPoolBanks);
}
/*****************************************************************************
* userPoolInit
*****************************************************************************/
/** @ingroup NSP2000Driver
* @brief Allocate and setup management for user pool allocations.
*
* This routine should be called when a driver is loaded and initialized.
*
* @param bank RO: The number of user pool banks to allocate
* @param size RO: Specifies the desired allocation size(MB) per bank
* @param granularity RO: The size of the smallest allocation.
*
* @par Externals:
*
* @return
* N/A
*
* @par Errors:
* See return section for error information.
*****************************************************************************/
void userPoolInit(unsigned int banks,
unsigned long size,
unsigned long granularity)
{
int ctr;
/* Initialize each bank */
for (ctr = 0; ctr < banks; ctr++)
{
userPoolBase[ctr] = (unsigned long)n8_memoryInit(N8_MEMBANK_USERPOOL + ctr,
size * N8_ONE_MEGABYTE,
granularity);
if (!userPoolBase[ctr])
{
break;
}
}
/* Setup user pool globals */
userPoolBanks = ctr;
N8_AtomicLockInit(userPoolSem);
}
/*****************************************************************************
* userPoolRelease
*****************************************************************************/
/** @ingroup NSP2000Driver
* @brief Release memory resources allocated for user pools.
*
* This routine deallocates and releases the large space allocated for the
* user request pool.
*
*
* @par Externals:
* N/A
*
* @return
* N/A
*
* @par Errors:
* See return section for error information.
*****************************************************************************/
void userPoolRelease(void)
{
int ctr;
N8_AtomicLockDel(userPoolSem);
for (ctr = 0; ctr < userPoolBanks; ctr++)
{
n8_memoryRelease(N8_MEMBANK_USERPOOL+ctr);
userPoolBase[ctr] = 0;
}
userPoolBanks = 0;
return;
}
/*****************************************************************************
* userPoolAlloc
*****************************************************************************/
/** @ingroup NSP2000Driver
* @brief Allocate a user pool for a process.
*
* This routine should be called when a process opens the device
*
* @par Externals:
*
* @return
* NULL pointer if process failed
* non-NULL void physical pointer to the base of the user pool
*
* @par Errors:
* See return section for error information.
*****************************************************************************/
unsigned long userPoolAlloc(int sessID)
{
int ctr;
unsigned long addr = 0;
MemBankCtl_t *memBankCtl_p;
N8_AtomicLock(userPoolSem);
for (ctr = 0; ctr < userPoolBanks; ctr++)
{
if (userPoolState[ctr] == POOL_FREE)
{
userPoolState[ctr] = POOL_ALLOCATED;
userPoolID[ctr] = sessID;
addr = userPoolBase[ctr];
/* Reset memory control variables for new allocation */
memBankCtl_p = memBankCtl_gp[N8_MEMBANK_USERPOOL+ctr];
memBankCtl_p->maxAllocated = 0;
memBankCtl_p->failedAllocs = 0;
break;
}
}
N8_AtomicUnlock(userPoolSem);
return (addr);
}
/*****************************************************************************
* userPoolFree
*****************************************************************************/
/** @ingroup NSP2000Driver
* @brief Deallocate a user pool for a process.
*
* This routine should be called when a process exits
*
* @param sessID RO: The session ID of the exiting process.
*
* @par Externals:
*
* @return
*
* @par Errors:
* See return section for error information.
*
* @par Assumptions:
* It is assume the caller has disabled interrupts to ensure there is no
* conflict with the ISR accessing the currAllocated value.
*****************************************************************************/
void userPoolFree(int sessID)
{
int ctr;
MemBankCtl_t *memBankCtl_p;
for (ctr = 0; ctr < userPoolBanks; ctr++)
{
if (userPoolID[ctr] == sessID)
{
/* If there is memory still allocated, the command blocks on the */
/* queue may still be pointing to memory in this pool. Thus it */
/* cannot be marked as free until everything has been returned. */
n8_pfreeSessID(N8_MEMBANK_USERPOOL+ctr, sessID);
memBankCtl_p = memBankCtl_gp[N8_MEMBANK_USERPOOL+ctr];
if (n8_atomic_read(memBankCtl_p->curAllocated))
{
userPoolState[ctr] = POOL_DELETED;
}
else
{
userPoolState[ctr] = POOL_FREE;
}
}
}
}
/*****************************************************************************
* userPoolFreePool
*****************************************************************************/
/** @ingroup NSP2000Driver
* @brief Mark a user pool as free
*
* This routine should be called after the last buffer has been
* freed from a pool
*
* @param addr RO: The physical address of the user pool.
*
* @par Externals:
*
* @return
*
* @par Errors:
* See return section for error information.
*
*****************************************************************************/
void userPoolFreePool(int bankIndex)
{
/* Validate bank index */
if ((bankIndex >= N8_MEMBANK_USERPOOL) &&
(bankIndex <=N8_MEMBANK_USERPOOL+userPoolBanks))
{
/* Only mark the bank as free once all the buffers have been freed */
if (n8_atomic_read(memBankCtl_gp[bankIndex]->curAllocated) == 0)
{
userPoolState[bankIndex - N8_MEMBANK_USERPOOL] = POOL_FREE;
}
}
}
/*****************************************************************************
* userPoolStats
*****************************************************************************/
/** @ingroup NSP2000Driver
* @brief Returns the current state of the user pool.
*
* This function return the current state of the user pool.
*
* @par Externals:
*
* @return
*
* @par Errors:
* See return section for error information.
*****************************************************************************/
void userPoolStats(MemStats_t *memStatsPtr)
{
int ctr;
MemBankCtl_t *memBankCtl_p;
for (ctr = 0; ctr < userPoolBanks; ctr++)
{
if (userPoolState[ctr] != POOL_FREE)
{
memBankCtl_p = memBankCtl_gp[N8_MEMBANK_USERPOOL+ctr];
memStatsPtr->sessionID = userPoolID[ctr];
memStatsPtr->curAllocated = n8_atomic_read(memBankCtl_p->curAllocated);
memStatsPtr->maxAllocated = memBankCtl_p->maxAllocated;
memStatsPtr->maxEntries = memBankCtl_p->maxEntries;
memStatsPtr->failedAllocs = memBankCtl_p->failedAllocs;
}
else
{
memStatsPtr->sessionID = 0;
}
memStatsPtr++;
}
}
|