IxOsalBufferMgt.c 22 KB

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