Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file IxOsalBufferMgt.c |
| 3 | * |
| 4 | * @brief Default buffer pool management and buffer management |
| 5 | * Implementation. |
| 6 | * |
| 7 | * Design Notes: |
| 8 | * |
| 9 | * @par |
| 10 | * IXP400 SW Release version 2.0 |
| 11 | * |
| 12 | * -- Copyright Notice -- |
| 13 | * |
| 14 | * @par |
| 15 | * Copyright 2001-2005, Intel Corporation. |
| 16 | * All rights reserved. |
| 17 | * |
| 18 | * @par |
Wolfgang Denk | cb3761e | 2013-07-28 22:12:47 +0200 | [diff] [blame] | 19 | * SPDX-License-Identifier: BSD-3-Clause |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 20 | * @par |
| 21 | * -- End of Copyright Notice -- |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * OS may choose to use default bufferMgt by defining |
| 26 | * IX_OSAL_USE_DEFAULT_BUFFER_MGT in IxOsalOsBufferMgt.h |
| 27 | */ |
| 28 | |
| 29 | #include "IxOsal.h" |
| 30 | |
| 31 | #define IX_OSAL_BUFFER_FREE_PROTECTION /* Define this to enable Illegal MBuf Freed Protection*/ |
| 32 | |
| 33 | /* |
| 34 | * The implementation is only used when the following |
| 35 | * is defined. |
| 36 | */ |
| 37 | #ifdef IX_OSAL_USE_DEFAULT_BUFFER_MGT |
| 38 | |
| 39 | |
| 40 | #define IX_OSAL_MBUF_SYS_SIGNATURE (0x8BADF00D) |
| 41 | #define IX_OSAL_MBUF_SYS_SIGNATURE_MASK (0xEFFFFFFF) |
| 42 | #define IX_OSAL_MBUF_USED_FLAG (0x10000000) |
| 43 | #define IX_OSAL_MBUF_SYS_SIGNATURE_INIT(bufPtr) IX_OSAL_MBUF_SIGNATURE (bufPtr) = (UINT32)IX_OSAL_MBUF_SYS_SIGNATURE |
| 44 | |
| 45 | /* |
| 46 | * This implementation is protect, the buffer pool management's ixOsalMBufFree |
| 47 | * against an invalid MBUF pointer argument that already has been freed earlier |
| 48 | * or in other words resides in the free pool of MBUFs. This added feature, |
| 49 | * checks the MBUF "USED" FLAG. The Flag tells if the MBUF is still not freed |
| 50 | * back to the Buffer Pool. |
| 51 | * Disable this feature for performance reasons by undef |
| 52 | * IX_OSAL_BUFFER_FREE_PROTECTION macro. |
| 53 | */ |
| 54 | #ifdef IX_OSAL_BUFFER_FREE_PROTECTION /*IX_OSAL_BUFFER_FREE_PROTECTION With Buffer Free protection*/ |
| 55 | |
| 56 | #define IX_OSAL_MBUF_GET_SYS_SIGNATURE(bufPtr) (IX_OSAL_MBUF_SIGNATURE (bufPtr)&(IX_OSAL_MBUF_SYS_SIGNATURE_MASK) ) |
| 57 | #define IX_OSAL_MBUF_SET_SYS_SIGNATURE(bufPtr) do { \ |
| 58 | IX_OSAL_MBUF_SIGNATURE (bufPtr)&(~IX_OSAL_MBUF_SYS_SIGNATURE_MASK);\ |
| 59 | IX_OSAL_MBUF_SIGNATURE (bufPtr)|=IX_OSAL_MBUF_SYS_SIGNATURE; \ |
| 60 | }while(0) |
| 61 | |
| 62 | #define IX_OSAL_MBUF_SET_USED_FLAG(bufPtr) IX_OSAL_MBUF_SIGNATURE (bufPtr)|=IX_OSAL_MBUF_USED_FLAG |
| 63 | #define IX_OSAL_MBUF_CLEAR_USED_FLAG(bufPtr) IX_OSAL_MBUF_SIGNATURE (bufPtr)&=~IX_OSAL_MBUF_USED_FLAG |
| 64 | #define IX_OSAL_MBUF_ISSET_USED_FLAG(bufPtr) (IX_OSAL_MBUF_SIGNATURE (bufPtr)&IX_OSAL_MBUF_USED_FLAG) |
| 65 | |
| 66 | #else |
| 67 | |
| 68 | #define IX_OSAL_MBUF_GET_SYS_SIGNATURE(bufPtr) IX_OSAL_MBUF_SIGNATURE (bufPtr) |
| 69 | #define IX_OSAL_MBUF_SET_SYS_SIGNATURE(bufPtr) IX_OSAL_MBUF_SIGNATURE (bufPtr) = IX_OSAL_MBUF_SYS_SIGNATURE |
| 70 | |
| 71 | #endif /*IX_OSAL_BUFFER_FREE_PROTECTION With Buffer Free protection*/ |
| 72 | /* |
| 73 | * Variable declarations global to this file only. Externs are followed by |
| 74 | * static variables. |
| 75 | */ |
| 76 | |
| 77 | /* |
| 78 | * A unit of 32, used to provide bit-shift for pool |
| 79 | * management. Needs some work if users want more than 32 pools. |
| 80 | */ |
| 81 | #define IX_OSAL_BUFF_FREE_BITS 32 |
| 82 | |
| 83 | PRIVATE UINT32 ixOsalBuffFreePools[IX_OSAL_MBUF_MAX_POOLS / |
| 84 | IX_OSAL_BUFF_FREE_BITS]; |
| 85 | |
| 86 | PUBLIC IX_OSAL_MBUF_POOL ixOsalBuffPools[IX_OSAL_MBUF_MAX_POOLS]; |
| 87 | |
| 88 | static int ixOsalBuffPoolsInUse = 0; |
| 89 | |
| 90 | #ifdef IX_OSAL_BUFFER_ALLOC_SEPARATELY |
| 91 | PRIVATE IX_OSAL_MBUF * |
| 92 | ixOsalBuffPoolMbufInit (UINT32 mbufSizeAligned, |
| 93 | UINT32 dataSizeAligned, |
| 94 | IX_OSAL_MBUF_POOL *poolPtr); |
| 95 | #endif |
| 96 | |
| 97 | PRIVATE IX_OSAL_MBUF_POOL * ixOsalPoolAlloc (void); |
| 98 | |
| 99 | /* |
| 100 | * Function definition: ixOsalPoolAlloc |
| 101 | */ |
| 102 | |
| 103 | /****************************/ |
| 104 | |
| 105 | PRIVATE IX_OSAL_MBUF_POOL * |
| 106 | ixOsalPoolAlloc (void) |
| 107 | { |
| 108 | register unsigned int i = 0; |
| 109 | |
| 110 | /* |
| 111 | * Scan for the first free buffer. Free buffers are indicated by 0 |
| 112 | * on the corrsponding bit in ixOsalBuffFreePools. |
| 113 | */ |
| 114 | if (ixOsalBuffPoolsInUse >= IX_OSAL_MBUF_MAX_POOLS) |
| 115 | { |
| 116 | /* |
| 117 | * Fail to grab a ptr this time |
| 118 | */ |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | while (ixOsalBuffFreePools[i / IX_OSAL_BUFF_FREE_BITS] & |
| 123 | (1 << (i % IX_OSAL_BUFF_FREE_BITS))) |
| 124 | i++; |
| 125 | /* |
| 126 | * Free buffer found. Mark it as busy and initialize. |
| 127 | */ |
| 128 | ixOsalBuffFreePools[i / IX_OSAL_BUFF_FREE_BITS] |= |
| 129 | (1 << (i % IX_OSAL_BUFF_FREE_BITS)); |
| 130 | |
| 131 | memset (&ixOsalBuffPools[i], 0, sizeof (IX_OSAL_MBUF_POOL)); |
| 132 | |
| 133 | ixOsalBuffPools[i].poolIdx = i; |
| 134 | ixOsalBuffPoolsInUse++; |
| 135 | |
| 136 | return &ixOsalBuffPools[i]; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | #ifdef IX_OSAL_BUFFER_ALLOC_SEPARATELY |
| 141 | PRIVATE IX_OSAL_MBUF * |
| 142 | ixOsalBuffPoolMbufInit (UINT32 mbufSizeAligned, |
| 143 | UINT32 dataSizeAligned, |
| 144 | IX_OSAL_MBUF_POOL *poolPtr) |
| 145 | { |
| 146 | UINT8 *dataPtr; |
| 147 | IX_OSAL_MBUF *realMbufPtr; |
| 148 | /* Allocate cache-aligned memory for mbuf header */ |
| 149 | realMbufPtr = (IX_OSAL_MBUF *) IX_OSAL_CACHE_DMA_MALLOC (mbufSizeAligned); |
| 150 | IX_OSAL_ASSERT (realMbufPtr != NULL); |
| 151 | memset (realMbufPtr, 0, mbufSizeAligned); |
| 152 | |
| 153 | /* Allocate cache-aligned memory for mbuf data */ |
| 154 | dataPtr = (UINT8 *) IX_OSAL_CACHE_DMA_MALLOC (dataSizeAligned); |
| 155 | IX_OSAL_ASSERT (dataPtr != NULL); |
| 156 | memset (dataPtr, 0, dataSizeAligned); |
| 157 | |
| 158 | /* Fill in mbuf header fields */ |
| 159 | IX_OSAL_MBUF_MDATA (realMbufPtr) = dataPtr; |
| 160 | IX_OSAL_MBUF_ALLOCATED_BUFF_DATA (realMbufPtr) = (UINT32)dataPtr; |
| 161 | |
| 162 | IX_OSAL_MBUF_MLEN (realMbufPtr) = dataSizeAligned; |
| 163 | IX_OSAL_MBUF_ALLOCATED_BUFF_LEN (realMbufPtr) = dataSizeAligned; |
| 164 | |
| 165 | IX_OSAL_MBUF_NET_POOL (realMbufPtr) = (IX_OSAL_MBUF_POOL *) poolPtr; |
| 166 | |
| 167 | IX_OSAL_MBUF_SYS_SIGNATURE_INIT(realMbufPtr); |
| 168 | |
| 169 | /* update some statistical information */ |
| 170 | poolPtr->mbufMemSize += mbufSizeAligned; |
| 171 | poolPtr->dataMemSize += dataSizeAligned; |
| 172 | |
| 173 | return realMbufPtr; |
| 174 | } |
| 175 | #endif /* #ifdef IX_OSAL_BUFFER_ALLOC_SEPARATELY */ |
| 176 | |
| 177 | /* |
| 178 | * Function definition: ixOsalBuffPoolInit |
| 179 | */ |
| 180 | |
| 181 | PUBLIC IX_OSAL_MBUF_POOL * |
| 182 | ixOsalPoolInit (UINT32 count, UINT32 size, const char *name) |
| 183 | { |
| 184 | |
| 185 | /* These variables are only used if UX_OSAL_BUFFER_ALLOC_SEPERATELY |
| 186 | * is defined . |
| 187 | */ |
| 188 | #ifdef IX_OSAL_BUFFER_ALLOC_SEPARATELY |
| 189 | UINT32 i, mbufSizeAligned, dataSizeAligned; |
| 190 | IX_OSAL_MBUF *currentMbufPtr = NULL; |
| 191 | #else |
| 192 | void *poolBufPtr; |
| 193 | void *poolDataPtr; |
| 194 | int mbufMemSize; |
| 195 | int dataMemSize; |
| 196 | #endif |
| 197 | |
| 198 | IX_OSAL_MBUF_POOL *poolPtr = NULL; |
| 199 | |
| 200 | if (count <= 0) |
| 201 | { |
| 202 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, |
| 203 | IX_OSAL_LOG_DEV_STDOUT, |
| 204 | "ixOsalPoolInit(): " "count = 0 \n", 0, 0, 0, 0, 0, 0); |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | if (name == NULL) |
| 209 | { |
| 210 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, |
| 211 | IX_OSAL_LOG_DEV_STDOUT, |
| 212 | "ixOsalPoolInit(): " "NULL name \n", 0, 0, 0, 0, 0, 0); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | if (strlen (name) > IX_OSAL_MBUF_POOL_NAME_LEN) |
| 217 | { |
| 218 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, |
| 219 | IX_OSAL_LOG_DEV_STDOUT, |
| 220 | "ixOsalPoolInit(): " |
| 221 | "ERROR - name length should be no greater than %d \n", |
| 222 | IX_OSAL_MBUF_POOL_NAME_LEN, 0, 0, 0, 0, 0); |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | /* OS can choose whether to allocate all buffers all together (if it |
| 227 | * can handle a huge single alloc request), or to allocate buffers |
| 228 | * separately by the defining IX_OSAL_BUFFER_ALLOC_SEPARATELY. |
| 229 | */ |
| 230 | #ifdef IX_OSAL_BUFFER_ALLOC_SEPARATELY |
| 231 | /* Get a pool Ptr */ |
| 232 | poolPtr = ixOsalPoolAlloc (); |
| 233 | |
| 234 | if (poolPtr == NULL) |
| 235 | { |
| 236 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, |
| 237 | IX_OSAL_LOG_DEV_STDOUT, |
| 238 | "ixOsalPoolInit(): " "Fail to Get PoolPtr \n", 0, 0, 0, 0, 0, 0); |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | mbufSizeAligned = IX_OSAL_MBUF_POOL_SIZE_ALIGN (sizeof (IX_OSAL_MBUF)); |
| 243 | dataSizeAligned = IX_OSAL_MBUF_POOL_SIZE_ALIGN(size); |
| 244 | |
| 245 | poolPtr->nextFreeBuf = NULL; |
| 246 | poolPtr->mbufMemPtr = NULL; |
| 247 | poolPtr->dataMemPtr = NULL; |
| 248 | poolPtr->bufDataSize = dataSizeAligned; |
| 249 | poolPtr->totalBufsInPool = count; |
| 250 | poolPtr->poolAllocType = IX_OSAL_MBUF_POOL_TYPE_SYS_ALLOC; |
| 251 | strcpy (poolPtr->name, name); |
| 252 | |
| 253 | |
| 254 | for (i = 0; i < count; i++) |
| 255 | { |
| 256 | /* create an mbuf */ |
| 257 | currentMbufPtr = ixOsalBuffPoolMbufInit (mbufSizeAligned, |
| 258 | dataSizeAligned, |
| 259 | poolPtr); |
| 260 | |
| 261 | #ifdef IX_OSAL_BUFFER_FREE_PROTECTION |
| 262 | /* Set the Buffer USED Flag. If not, ixOsalMBufFree will fail. |
| 263 | ixOsalMbufFree used here is in a special case whereby, it's |
| 264 | used to add MBUF to the Pool. By specification, ixOsalMbufFree |
| 265 | deallocates an allocated MBUF from Pool. |
| 266 | */ |
| 267 | IX_OSAL_MBUF_SET_USED_FLAG(currentMbufPtr); |
| 268 | #endif |
| 269 | /* Add it to the pool */ |
| 270 | ixOsalMbufFree (currentMbufPtr); |
| 271 | |
| 272 | /* flush the pool information to RAM */ |
| 273 | IX_OSAL_CACHE_FLUSH (currentMbufPtr, mbufSizeAligned); |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * update the number of free buffers in the pool |
| 278 | */ |
| 279 | poolPtr->freeBufsInPool = count; |
| 280 | |
| 281 | #else |
| 282 | /* Otherwise allocate buffers in a continuous block fashion */ |
| 283 | poolBufPtr = IX_OSAL_MBUF_POOL_MBUF_AREA_ALLOC (count, mbufMemSize); |
| 284 | IX_OSAL_ASSERT (poolBufPtr != NULL); |
| 285 | poolDataPtr = |
| 286 | IX_OSAL_MBUF_POOL_DATA_AREA_ALLOC (count, size, dataMemSize); |
| 287 | IX_OSAL_ASSERT (poolDataPtr != NULL); |
| 288 | |
| 289 | poolPtr = ixOsalNoAllocPoolInit (poolBufPtr, poolDataPtr, |
| 290 | count, size, name); |
| 291 | if (poolPtr == NULL) |
| 292 | { |
| 293 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, |
| 294 | IX_OSAL_LOG_DEV_STDOUT, |
| 295 | "ixOsalPoolInit(): " "Fail to get pool ptr \n", 0, 0, 0, 0, 0, 0); |
| 296 | return NULL; |
| 297 | } |
| 298 | |
| 299 | poolPtr->poolAllocType = IX_OSAL_MBUF_POOL_TYPE_SYS_ALLOC; |
| 300 | |
| 301 | #endif /* IX_OSAL_BUFFER_ALLOC_SEPARATELY */ |
| 302 | return poolPtr; |
| 303 | } |
| 304 | |
| 305 | PUBLIC IX_OSAL_MBUF_POOL * |
| 306 | ixOsalNoAllocPoolInit (void *poolBufPtr, |
| 307 | void *poolDataPtr, UINT32 count, UINT32 size, const char *name) |
| 308 | { |
| 309 | UINT32 i, mbufSizeAligned, sizeAligned; |
| 310 | IX_OSAL_MBUF *currentMbufPtr = NULL; |
| 311 | IX_OSAL_MBUF *nextMbufPtr = NULL; |
| 312 | IX_OSAL_MBUF_POOL *poolPtr = NULL; |
| 313 | |
| 314 | /* |
| 315 | * check parameters |
| 316 | */ |
| 317 | if (poolBufPtr == NULL) |
| 318 | { |
| 319 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, |
| 320 | IX_OSAL_LOG_DEV_STDOUT, |
| 321 | "ixOsalNoAllocPoolInit(): " |
| 322 | "ERROR - NULL poolBufPtr \n", 0, 0, 0, 0, 0, 0); |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | if (count <= 0) |
| 327 | { |
| 328 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, |
| 329 | IX_OSAL_LOG_DEV_STDOUT, |
| 330 | "ixOsalNoAllocPoolInit(): " |
| 331 | "ERROR - count must > 0 \n", 0, 0, 0, 0, 0, 0); |
| 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | if (name == NULL) |
| 336 | { |
| 337 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, |
| 338 | IX_OSAL_LOG_DEV_STDOUT, |
| 339 | "ixOsalNoAllocPoolInit(): " |
| 340 | "ERROR - NULL name ptr \n", 0, 0, 0, 0, 0, 0); |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | if (strlen (name) > IX_OSAL_MBUF_POOL_NAME_LEN) |
| 345 | { |
| 346 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, |
| 347 | IX_OSAL_LOG_DEV_STDOUT, |
| 348 | "ixOsalNoAllocPoolInit(): " |
| 349 | "ERROR - name length should be no greater than %d \n", |
| 350 | IX_OSAL_MBUF_POOL_NAME_LEN, 0, 0, 0, 0, 0); |
| 351 | return NULL; |
| 352 | } |
| 353 | |
| 354 | poolPtr = ixOsalPoolAlloc (); |
| 355 | |
| 356 | if (poolPtr == NULL) |
| 357 | { |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Adjust sizes to ensure alignment on cache line boundaries |
| 363 | */ |
| 364 | mbufSizeAligned = |
| 365 | IX_OSAL_MBUF_POOL_SIZE_ALIGN (sizeof (IX_OSAL_MBUF)); |
| 366 | /* |
| 367 | * clear the mbuf memory area |
| 368 | */ |
| 369 | memset (poolBufPtr, 0, mbufSizeAligned * count); |
| 370 | |
| 371 | if (poolDataPtr != NULL) |
| 372 | { |
| 373 | /* |
| 374 | * Adjust sizes to ensure alignment on cache line boundaries |
| 375 | */ |
| 376 | sizeAligned = IX_OSAL_MBUF_POOL_SIZE_ALIGN (size); |
| 377 | /* |
| 378 | * clear the data memory area |
| 379 | */ |
| 380 | memset (poolDataPtr, 0, sizeAligned * count); |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | sizeAligned = 0; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * initialise pool fields |
| 389 | */ |
| 390 | strcpy ((poolPtr)->name, name); |
| 391 | |
| 392 | poolPtr->dataMemPtr = poolDataPtr; |
| 393 | poolPtr->mbufMemPtr = poolBufPtr; |
| 394 | poolPtr->bufDataSize = sizeAligned; |
| 395 | poolPtr->totalBufsInPool = count; |
| 396 | poolPtr->mbufMemSize = mbufSizeAligned * count; |
| 397 | poolPtr->dataMemSize = sizeAligned * count; |
| 398 | |
| 399 | currentMbufPtr = (IX_OSAL_MBUF *) poolBufPtr; |
| 400 | |
| 401 | poolPtr->nextFreeBuf = currentMbufPtr; |
| 402 | |
| 403 | for (i = 0; i < count; i++) |
| 404 | { |
| 405 | if (i < (count - 1)) |
| 406 | { |
| 407 | nextMbufPtr = |
| 408 | (IX_OSAL_MBUF *) ((unsigned) currentMbufPtr + |
| 409 | mbufSizeAligned); |
| 410 | } |
| 411 | else |
| 412 | { /* last mbuf in chain */ |
| 413 | nextMbufPtr = NULL; |
| 414 | } |
| 415 | IX_OSAL_MBUF_NEXT_BUFFER_IN_PKT_PTR (currentMbufPtr) = nextMbufPtr; |
| 416 | IX_OSAL_MBUF_NET_POOL (currentMbufPtr) = poolPtr; |
| 417 | |
| 418 | IX_OSAL_MBUF_SYS_SIGNATURE_INIT(currentMbufPtr); |
| 419 | |
| 420 | if (poolDataPtr != NULL) |
| 421 | { |
| 422 | IX_OSAL_MBUF_MDATA (currentMbufPtr) = poolDataPtr; |
| 423 | IX_OSAL_MBUF_ALLOCATED_BUFF_DATA(currentMbufPtr) = (UINT32) poolDataPtr; |
| 424 | |
| 425 | IX_OSAL_MBUF_MLEN (currentMbufPtr) = sizeAligned; |
| 426 | IX_OSAL_MBUF_ALLOCATED_BUFF_LEN(currentMbufPtr) = sizeAligned; |
| 427 | |
| 428 | poolDataPtr = (void *) ((unsigned) poolDataPtr + sizeAligned); |
| 429 | } |
| 430 | |
| 431 | currentMbufPtr = nextMbufPtr; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * update the number of free buffers in the pool |
| 436 | */ |
| 437 | poolPtr->freeBufsInPool = count; |
| 438 | |
| 439 | poolPtr->poolAllocType = IX_OSAL_MBUF_POOL_TYPE_USER_ALLOC; |
| 440 | |
| 441 | return poolPtr; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Get a mbuf ptr from the pool |
| 446 | */ |
| 447 | PUBLIC IX_OSAL_MBUF * |
| 448 | ixOsalMbufAlloc (IX_OSAL_MBUF_POOL * poolPtr) |
| 449 | { |
| 450 | int lock; |
| 451 | IX_OSAL_MBUF *newBufPtr = NULL; |
| 452 | |
| 453 | /* |
| 454 | * check parameters |
| 455 | */ |
| 456 | if (poolPtr == NULL) |
| 457 | { |
| 458 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, |
| 459 | IX_OSAL_LOG_DEV_STDOUT, |
| 460 | "ixOsalMbufAlloc(): " |
| 461 | "ERROR - Invalid Parameter\n", 0, 0, 0, 0, 0, 0); |
| 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | lock = ixOsalIrqLock (); |
| 466 | |
| 467 | newBufPtr = poolPtr->nextFreeBuf; |
| 468 | if (newBufPtr) |
| 469 | { |
| 470 | poolPtr->nextFreeBuf = |
| 471 | IX_OSAL_MBUF_NEXT_BUFFER_IN_PKT_PTR (newBufPtr); |
| 472 | IX_OSAL_MBUF_NEXT_BUFFER_IN_PKT_PTR (newBufPtr) = NULL; |
| 473 | |
| 474 | /* |
| 475 | * update the number of free buffers in the pool |
| 476 | */ |
| 477 | poolPtr->freeBufsInPool--; |
| 478 | } |
| 479 | else |
| 480 | { |
| 481 | /* Return NULL to indicate to caller that request is denied. */ |
| 482 | ixOsalIrqUnlock (lock); |
| 483 | |
| 484 | return NULL; |
| 485 | } |
| 486 | |
| 487 | #ifdef IX_OSAL_BUFFER_FREE_PROTECTION |
| 488 | /* Set Buffer Used Flag to indicate state.*/ |
| 489 | IX_OSAL_MBUF_SET_USED_FLAG(newBufPtr); |
| 490 | #endif |
| 491 | |
| 492 | ixOsalIrqUnlock (lock); |
| 493 | |
| 494 | return newBufPtr; |
| 495 | } |
| 496 | |
| 497 | PUBLIC IX_OSAL_MBUF * |
| 498 | ixOsalMbufFree (IX_OSAL_MBUF * bufPtr) |
| 499 | { |
| 500 | int lock; |
| 501 | IX_OSAL_MBUF_POOL *poolPtr; |
| 502 | |
| 503 | IX_OSAL_MBUF *nextBufPtr = NULL; |
| 504 | |
| 505 | /* |
| 506 | * check parameters |
| 507 | */ |
| 508 | if (bufPtr == NULL) |
| 509 | { |
| 510 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, |
| 511 | IX_OSAL_LOG_DEV_STDOUT, |
| 512 | "ixOsalMbufFree(): " |
| 513 | "ERROR - Invalid Parameter\n", 0, 0, 0, 0, 0, 0); |
| 514 | return NULL; |
| 515 | } |
| 516 | |
| 517 | |
| 518 | |
| 519 | lock = ixOsalIrqLock (); |
| 520 | |
| 521 | #ifdef IX_OSAL_BUFFER_FREE_PROTECTION |
| 522 | |
| 523 | /* Prevention for Buffer freed more than once*/ |
| 524 | if(!IX_OSAL_MBUF_ISSET_USED_FLAG(bufPtr)) |
| 525 | { |
| 526 | return NULL; |
| 527 | } |
| 528 | IX_OSAL_MBUF_CLEAR_USED_FLAG(bufPtr); |
| 529 | #endif |
| 530 | |
| 531 | poolPtr = IX_OSAL_MBUF_NET_POOL (bufPtr); |
| 532 | |
| 533 | /* |
| 534 | * check the mbuf wrapper signature (if mbuf wrapper was used) |
| 535 | */ |
| 536 | if (poolPtr->poolAllocType == IX_OSAL_MBUF_POOL_TYPE_SYS_ALLOC) |
| 537 | { |
| 538 | IX_OSAL_ENSURE ( (IX_OSAL_MBUF_GET_SYS_SIGNATURE(bufPtr) == IX_OSAL_MBUF_SYS_SIGNATURE), |
| 539 | "ixOsalBuffPoolBufFree: ERROR - Invalid mbuf signature."); |
| 540 | } |
| 541 | |
| 542 | nextBufPtr = IX_OSAL_MBUF_NEXT_BUFFER_IN_PKT_PTR (bufPtr); |
| 543 | |
| 544 | IX_OSAL_MBUF_NEXT_BUFFER_IN_PKT_PTR (bufPtr) = poolPtr->nextFreeBuf; |
| 545 | poolPtr->nextFreeBuf = bufPtr; |
| 546 | |
| 547 | /* |
| 548 | * update the number of free buffers in the pool |
| 549 | */ |
| 550 | poolPtr->freeBufsInPool++; |
| 551 | |
| 552 | ixOsalIrqUnlock (lock); |
| 553 | |
| 554 | return nextBufPtr; |
| 555 | } |
| 556 | |
| 557 | PUBLIC void |
| 558 | ixOsalMbufChainFree (IX_OSAL_MBUF * bufPtr) |
| 559 | { |
| 560 | while ((bufPtr = ixOsalMbufFree (bufPtr))); |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * Function definition: ixOsalBuffPoolShow |
| 565 | */ |
| 566 | PUBLIC void |
| 567 | ixOsalMbufPoolShow (IX_OSAL_MBUF_POOL * poolPtr) |
| 568 | { |
| 569 | IX_OSAL_MBUF *nextBufPtr; |
| 570 | int count = 0; |
| 571 | int lock; |
| 572 | |
| 573 | /* |
| 574 | * check parameters |
| 575 | */ |
| 576 | if (poolPtr == NULL) |
| 577 | { |
| 578 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, |
| 579 | IX_OSAL_LOG_DEV_STDOUT, |
| 580 | "ixOsalBuffPoolShow(): " |
| 581 | "ERROR - Invalid Parameter", 0, 0, 0, 0, 0, 0); |
| 582 | /* |
| 583 | * return IX_FAIL; |
| 584 | */ |
| 585 | return; |
| 586 | } |
| 587 | |
| 588 | lock = ixOsalIrqLock (); |
| 589 | count = poolPtr->freeBufsInPool; |
| 590 | nextBufPtr = poolPtr->nextFreeBuf; |
| 591 | ixOsalIrqUnlock (lock); |
| 592 | |
| 593 | ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, |
| 594 | IX_OSAL_LOG_DEV_STDOUT, "=== POOL INFORMATION ===\n", 0, 0, 0, |
| 595 | 0, 0, 0); |
| 596 | ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT, |
| 597 | "Pool Name: %s\n", |
| 598 | (unsigned int) poolPtr->name, 0, 0, 0, 0, 0); |
| 599 | ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT, |
| 600 | "Pool Allocation Type: %d\n", |
| 601 | (unsigned int) poolPtr->poolAllocType, 0, 0, 0, 0, 0); |
| 602 | ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT, |
| 603 | "Pool Mbuf Mem Usage (bytes): %d\n", |
| 604 | (unsigned int) poolPtr->mbufMemSize, 0, 0, 0, 0, 0); |
| 605 | ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT, |
| 606 | "Pool Data Mem Usage (bytes): %d\n", |
| 607 | (unsigned int) poolPtr->dataMemSize, 0, 0, 0, 0, 0); |
| 608 | ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT, |
| 609 | "Mbuf Data Capacity (bytes): %d\n", |
| 610 | (unsigned int) poolPtr->bufDataSize, 0, 0, 0, 0, 0); |
| 611 | ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT, |
| 612 | "Total Mbufs in Pool: %d\n", |
| 613 | (unsigned int) poolPtr->totalBufsInPool, 0, 0, 0, 0, 0); |
| 614 | ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT, |
| 615 | "Available Mbufs: %d\n", (unsigned int) count, 0, |
| 616 | 0, 0, 0, 0); |
| 617 | ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT, |
| 618 | "Next Available Mbuf: %p\n", (unsigned int) nextBufPtr, |
| 619 | 0, 0, 0, 0, 0); |
| 620 | |
| 621 | if (poolPtr->poolAllocType == IX_OSAL_MBUF_POOL_TYPE_USER_ALLOC) |
| 622 | { |
| 623 | ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, |
| 624 | IX_OSAL_LOG_DEV_STDOUT, |
| 625 | "Mbuf Mem Area Start address: %p\n", |
| 626 | (unsigned int) poolPtr->mbufMemPtr, 0, 0, 0, 0, 0); |
| 627 | ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT, |
| 628 | "Data Mem Area Start address: %p\n", |
| 629 | (unsigned int) poolPtr->dataMemPtr, 0, 0, 0, 0, 0); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | PUBLIC void |
| 634 | ixOsalMbufDataPtrReset (IX_OSAL_MBUF * bufPtr) |
| 635 | { |
| 636 | IX_OSAL_MBUF_POOL *poolPtr; |
| 637 | UINT8 *poolDataPtr; |
| 638 | |
| 639 | if (bufPtr == NULL) |
| 640 | { |
| 641 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, |
| 642 | "ixOsalBuffPoolBufDataPtrReset" |
| 643 | ": ERROR - Invalid Parameter\n", 0, 0, 0, 0, 0, 0); |
| 644 | return; |
| 645 | } |
| 646 | |
| 647 | poolPtr = (IX_OSAL_MBUF_POOL *) IX_OSAL_MBUF_NET_POOL (bufPtr); |
| 648 | poolDataPtr = poolPtr->dataMemPtr; |
| 649 | |
| 650 | if (poolPtr->poolAllocType == IX_OSAL_MBUF_POOL_TYPE_SYS_ALLOC) |
| 651 | { |
| 652 | if (IX_OSAL_MBUF_GET_SYS_SIGNATURE(bufPtr) != IX_OSAL_MBUF_SYS_SIGNATURE) |
| 653 | { |
| 654 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, |
| 655 | "ixOsalBuffPoolBufDataPtrReset" |
| 656 | ": invalid mbuf, cannot reset mData pointer\n", 0, 0, |
| 657 | 0, 0, 0, 0); |
| 658 | return; |
| 659 | } |
| 660 | IX_OSAL_MBUF_MDATA (bufPtr) = (UINT8*)IX_OSAL_MBUF_ALLOCATED_BUFF_DATA (bufPtr); |
| 661 | } |
| 662 | else |
| 663 | { |
| 664 | if (poolDataPtr) |
| 665 | { |
| 666 | unsigned int bufSize = poolPtr->bufDataSize; |
| 667 | unsigned int bufDataAddr = |
| 668 | (unsigned int) IX_OSAL_MBUF_MDATA (bufPtr); |
| 669 | unsigned int poolDataAddr = (unsigned int) poolDataPtr; |
| 670 | |
| 671 | /* |
| 672 | * the pointer is still pointing somewhere in the mbuf payload. |
| 673 | * This operation moves the pointer to the beginning of the |
| 674 | * mbuf payload |
| 675 | */ |
| 676 | bufDataAddr = ((bufDataAddr - poolDataAddr) / bufSize) * bufSize; |
| 677 | IX_OSAL_MBUF_MDATA (bufPtr) = &poolDataPtr[bufDataAddr]; |
| 678 | } |
| 679 | else |
| 680 | { |
| 681 | ixOsalLog (IX_OSAL_LOG_LVL_WARNING, IX_OSAL_LOG_DEV_STDOUT, |
| 682 | "ixOsalBuffPoolBufDataPtrReset" |
| 683 | ": cannot be used if user supplied NULL pointer for pool data area " |
| 684 | "when pool was created\n", 0, 0, 0, 0, 0, 0); |
| 685 | return; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Function definition: ixOsalBuffPoolUninit |
| 693 | */ |
| 694 | PUBLIC IX_STATUS |
| 695 | ixOsalBuffPoolUninit (IX_OSAL_MBUF_POOL * pool) |
| 696 | { |
| 697 | if (!pool) |
| 698 | { |
| 699 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, |
| 700 | "ixOsalBuffPoolUninit: NULL ptr \n", 0, 0, 0, 0, 0, 0); |
| 701 | return IX_FAIL; |
| 702 | } |
| 703 | |
| 704 | if (pool->freeBufsInPool != pool->totalBufsInPool) |
| 705 | { |
| 706 | ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, |
| 707 | "ixOsalBuffPoolUninit: need to return all ptrs to the pool first! \n", |
| 708 | 0, 0, 0, 0, 0, 0); |
| 709 | return IX_FAIL; |
| 710 | } |
| 711 | |
| 712 | if (pool->poolAllocType == IX_OSAL_MBUF_POOL_TYPE_SYS_ALLOC) |
| 713 | { |
| 714 | #ifdef IX_OSAL_BUFFER_ALLOC_SEPARATELY |
| 715 | UINT32 i; |
| 716 | IX_OSAL_MBUF* pBuf; |
| 717 | |
| 718 | pBuf = pool->nextFreeBuf; |
| 719 | /* Freed the Buffer one by one till all the Memory is freed*/ |
| 720 | for (i= pool->freeBufsInPool; i >0 && pBuf!=NULL ;i--){ |
| 721 | IX_OSAL_MBUF* pBufTemp; |
| 722 | pBufTemp = IX_OSAL_MBUF_NEXT_BUFFER_IN_PKT_PTR(pBuf); |
| 723 | /* Freed MBUF Data Memory area*/ |
| 724 | IX_OSAL_CACHE_DMA_FREE( (void *) (IX_OSAL_MBUF_ALLOCATED_BUFF_DATA(pBuf)) ); |
| 725 | /* Freed MBUF Struct Memory area*/ |
| 726 | IX_OSAL_CACHE_DMA_FREE(pBuf); |
| 727 | pBuf = pBufTemp; |
| 728 | } |
| 729 | |
| 730 | #else |
| 731 | IX_OSAL_CACHE_DMA_FREE (pool->mbufMemPtr); |
| 732 | IX_OSAL_CACHE_DMA_FREE (pool->dataMemPtr); |
| 733 | #endif |
| 734 | } |
| 735 | |
| 736 | ixOsalBuffFreePools[pool->poolIdx / IX_OSAL_BUFF_FREE_BITS] &= |
| 737 | ~(1 << (pool->poolIdx % IX_OSAL_BUFF_FREE_BITS)); |
| 738 | ixOsalBuffPoolsInUse--; |
| 739 | return IX_SUCCESS; |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Function definition: ixOsalBuffPoolDataAreaSizeGet |
| 744 | */ |
| 745 | PUBLIC UINT32 |
| 746 | ixOsalBuffPoolDataAreaSizeGet (int count, int size) |
| 747 | { |
| 748 | UINT32 memorySize; |
| 749 | memorySize = count * IX_OSAL_MBUF_POOL_SIZE_ALIGN (size); |
| 750 | return memorySize; |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * Function definition: ixOsalBuffPoolMbufAreaSizeGet |
| 755 | */ |
| 756 | PUBLIC UINT32 |
| 757 | ixOsalBuffPoolMbufAreaSizeGet (int count) |
| 758 | { |
| 759 | UINT32 memorySize; |
| 760 | memorySize = |
| 761 | count * IX_OSAL_MBUF_POOL_SIZE_ALIGN (sizeof (IX_OSAL_MBUF)); |
| 762 | return memorySize; |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * Function definition: ixOsalBuffPoolFreeCountGet |
| 767 | */ |
| 768 | PUBLIC UINT32 ixOsalBuffPoolFreeCountGet(IX_OSAL_MBUF_POOL * poolPtr) |
| 769 | |
| 770 | { |
| 771 | |
| 772 | return poolPtr->freeBufsInPool; |
| 773 | |
| 774 | } |
| 775 | |
| 776 | #endif /* IX_OSAL_USE_DEFAULT_BUFFER_MGT */ |