IxOsalBufferMgt.c 23 KB

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