ethernet-mem.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**********************************************************************
  2. * Author: Cavium Networks
  3. *
  4. * Contact: support@caviumnetworks.com
  5. * This file is part of the OCTEON SDK
  6. *
  7. * Copyright (c) 2003-2010 Cavium Networks
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this file; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. * or visit http://www.gnu.org/licenses/.
  23. *
  24. * This file may also be available under a different license from Cavium.
  25. * Contact Cavium Networks for more information
  26. **********************************************************************/
  27. #include <linux/kernel.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/slab.h>
  30. #include <asm/octeon/octeon.h>
  31. #include "ethernet-defines.h"
  32. #include "cvmx-fpa.h"
  33. /**
  34. * cvm_oct_fill_hw_skbuff - fill the supplied hardware pool with skbuffs
  35. * @pool: Pool to allocate an skbuff for
  36. * @size: Size of the buffer needed for the pool
  37. * @elements: Number of buffers to allocate
  38. *
  39. * Returns the actual number of buffers allocated.
  40. */
  41. static int cvm_oct_fill_hw_skbuff(int pool, int size, int elements)
  42. {
  43. int freed = elements;
  44. while (freed) {
  45. struct sk_buff *skb = dev_alloc_skb(size + 256);
  46. if (unlikely(skb == NULL)) {
  47. pr_warning
  48. ("Failed to allocate skb for hardware pool %d\n",
  49. pool);
  50. break;
  51. }
  52. skb_reserve(skb, 256 - (((unsigned long)skb->data) & 0x7f));
  53. *(struct sk_buff **)(skb->data - sizeof(void *)) = skb;
  54. cvmx_fpa_free(skb->data, pool, DONT_WRITEBACK(size / 128));
  55. freed--;
  56. }
  57. return elements - freed;
  58. }
  59. /**
  60. * cvm_oct_free_hw_skbuff- free hardware pool skbuffs
  61. * @pool: Pool to allocate an skbuff for
  62. * @size: Size of the buffer needed for the pool
  63. * @elements: Number of buffers to allocate
  64. */
  65. static void cvm_oct_free_hw_skbuff(int pool, int size, int elements)
  66. {
  67. char *memory;
  68. do {
  69. memory = cvmx_fpa_alloc(pool);
  70. if (memory) {
  71. struct sk_buff *skb =
  72. *(struct sk_buff **)(memory - sizeof(void *));
  73. elements--;
  74. dev_kfree_skb(skb);
  75. }
  76. } while (memory);
  77. if (elements < 0)
  78. pr_warning("Freeing of pool %u had too many skbuffs (%d)\n",
  79. pool, elements);
  80. else if (elements > 0)
  81. pr_warning("Freeing of pool %u is missing %d skbuffs\n",
  82. pool, elements);
  83. }
  84. /**
  85. * cvm_oct_fill_hw_memory - fill a hardware pool with memory.
  86. * @pool: Pool to populate
  87. * @size: Size of each buffer in the pool
  88. * @elements: Number of buffers to allocate
  89. *
  90. * Returns the actual number of buffers allocated.
  91. */
  92. static int cvm_oct_fill_hw_memory(int pool, int size, int elements)
  93. {
  94. char *memory;
  95. char *fpa;
  96. int freed = elements;
  97. while (freed) {
  98. /*
  99. * FPA memory must be 128 byte aligned. Since we are
  100. * aligning we need to save the original pointer so we
  101. * can feed it to kfree when the memory is returned to
  102. * the kernel.
  103. *
  104. * We allocate an extra 256 bytes to allow for
  105. * alignment and space for the original pointer saved
  106. * just before the block.
  107. */
  108. memory = kmalloc(size + 256, GFP_ATOMIC);
  109. if (unlikely(memory == NULL)) {
  110. pr_warning("Unable to allocate %u bytes for FPA pool %d\n",
  111. elements * size, pool);
  112. break;
  113. }
  114. fpa = (char *)(((unsigned long)memory + 256) & ~0x7fUL);
  115. *((char **)fpa - 1) = memory;
  116. cvmx_fpa_free(fpa, pool, 0);
  117. freed--;
  118. }
  119. return elements - freed;
  120. }
  121. /**
  122. * cvm_oct_free_hw_memory - Free memory allocated by cvm_oct_fill_hw_memory
  123. * @pool: FPA pool to free
  124. * @size: Size of each buffer in the pool
  125. * @elements: Number of buffers that should be in the pool
  126. */
  127. static void cvm_oct_free_hw_memory(int pool, int size, int elements)
  128. {
  129. char *memory;
  130. char *fpa;
  131. do {
  132. fpa = cvmx_fpa_alloc(pool);
  133. if (fpa) {
  134. elements--;
  135. fpa = (char *)phys_to_virt(cvmx_ptr_to_phys(fpa));
  136. memory = *((char **)fpa - 1);
  137. kfree(memory);
  138. }
  139. } while (fpa);
  140. if (elements < 0)
  141. pr_warning("Freeing of pool %u had too many buffers (%d)\n",
  142. pool, elements);
  143. else if (elements > 0)
  144. pr_warning("Warning: Freeing of pool %u is missing %d buffers\n",
  145. pool, elements);
  146. }
  147. int cvm_oct_mem_fill_fpa(int pool, int size, int elements)
  148. {
  149. int freed;
  150. if (USE_SKBUFFS_IN_HW && pool == CVMX_FPA_PACKET_POOL)
  151. freed = cvm_oct_fill_hw_skbuff(pool, size, elements);
  152. else
  153. freed = cvm_oct_fill_hw_memory(pool, size, elements);
  154. return freed;
  155. }
  156. void cvm_oct_mem_empty_fpa(int pool, int size, int elements)
  157. {
  158. if (USE_SKBUFFS_IN_HW && pool == CVMX_FPA_PACKET_POOL)
  159. cvm_oct_free_hw_skbuff(pool, size, elements);
  160. else
  161. cvm_oct_free_hw_memory(pool, size, elements);
  162. }