eth_addrtbl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include <common.h>
  2. #include <malloc.h>
  3. #include <galileo/gt64260R.h>
  4. #include <galileo/core.h>
  5. #include <asm/cache.h>
  6. #include "eth.h"
  7. #include "eth_addrtbl.h"
  8. #define PRINTF printf
  9. #ifdef CONFIG_GT_USE_MAC_HASH_TABLE
  10. static u32 addressTableHashMode[GAL_ETH_DEVS] = { 0, };
  11. static u32 addressTableHashSize[GAL_ETH_DEVS] = { 0, };
  12. static addrTblEntry *addressTableBase[GAL_ETH_DEVS] = { 0, };
  13. static void *realAddrTableBase[GAL_ETH_DEVS] = { 0, };
  14. static const u32 hashLength[2] = {
  15. (0x8000), /* 8K * 4 entries */
  16. (0x8000 / 16), /* 512 * 4 entries */
  17. };
  18. /* Initialize the address table for a port, if needed */
  19. unsigned int initAddressTable (u32 port, u32 hashMode, u32 hashSizeSelector)
  20. {
  21. unsigned int tableBase;
  22. if (port < 0 || port >= GAL_ETH_DEVS) {
  23. printf ("%s: Invalid port number %d\n", __FUNCTION__, port);
  24. return 0;
  25. }
  26. if (hashMode > 1) {
  27. printf ("%s: Invalid Hash Mode %d\n", __FUNCTION__, port);
  28. return 0;
  29. }
  30. if (realAddrTableBase[port] &&
  31. (addressTableHashSize[port] != hashSizeSelector)) {
  32. /* we have been here before,
  33. * but now we want a different sized table
  34. */
  35. free (realAddrTableBase[port]);
  36. realAddrTableBase[port] = 0;
  37. addressTableBase[port] = 0;
  38. }
  39. tableBase = (unsigned int) addressTableBase[port];
  40. /* we get called for every probe, so only do this once */
  41. if (!tableBase) {
  42. int bytes =
  43. hashLength[hashSizeSelector] * sizeof (addrTblEntry);
  44. realAddrTableBase[port] =
  45. malloc (bytes + 64);
  46. tableBase = (unsigned int)realAddrTableBase;
  47. if (!tableBase) {
  48. printf ("%s: alloc memory failed \n", __FUNCTION__);
  49. return 0;
  50. }
  51. /* align to octal byte */
  52. if (tableBase & 63)
  53. tableBase = (tableBase + 63) & ~63;
  54. addressTableHashMode[port] = hashMode;
  55. addressTableHashSize[port] = hashSizeSelector;
  56. addressTableBase[port] = (addrTblEntry *) tableBase;
  57. memset ((void *) tableBase, 0, bytes);
  58. }
  59. return tableBase;
  60. }
  61. /*
  62. * ----------------------------------------------------------------------------
  63. * This function will calculate the hash function of the address.
  64. * depends on the hash mode and hash size.
  65. * Inputs
  66. * macH - the 2 most significant bytes of the MAC address.
  67. * macL - the 4 least significant bytes of the MAC address.
  68. * hashMode - hash mode 0 or hash mode 1.
  69. * hashSizeSelector - indicates number of hash table entries (0=0x8000,1=0x800)
  70. * Outputs
  71. * return the calculated entry.
  72. */
  73. u32 hashTableFunction (u32 macH, u32 macL, u32 HashSize, u32 hash_mode)
  74. {
  75. u32 hashResult;
  76. u32 addrH;
  77. u32 addrL;
  78. u32 addr0;
  79. u32 addr1;
  80. u32 addr2;
  81. u32 addr3;
  82. u32 addrHSwapped;
  83. u32 addrLSwapped;
  84. addrH = NIBBLE_SWAPPING_16_BIT (macH);
  85. addrL = NIBBLE_SWAPPING_32_BIT (macL);
  86. addrHSwapped = FLIP_4_BITS (addrH & 0xf)
  87. + ((FLIP_4_BITS ((addrH >> 4) & 0xf)) << 4)
  88. + ((FLIP_4_BITS ((addrH >> 8) & 0xf)) << 8)
  89. + ((FLIP_4_BITS ((addrH >> 12) & 0xf)) << 12);
  90. addrLSwapped = FLIP_4_BITS (addrL & 0xf)
  91. + ((FLIP_4_BITS ((addrL >> 4) & 0xf)) << 4)
  92. + ((FLIP_4_BITS ((addrL >> 8) & 0xf)) << 8)
  93. + ((FLIP_4_BITS ((addrL >> 12) & 0xf)) << 12)
  94. + ((FLIP_4_BITS ((addrL >> 16) & 0xf)) << 16)
  95. + ((FLIP_4_BITS ((addrL >> 20) & 0xf)) << 20)
  96. + ((FLIP_4_BITS ((addrL >> 24) & 0xf)) << 24)
  97. + ((FLIP_4_BITS ((addrL >> 28) & 0xf)) << 28);
  98. addrH = addrHSwapped;
  99. addrL = addrLSwapped;
  100. if (hash_mode == 0) {
  101. addr0 = (addrL >> 2) & 0x03f;
  102. addr1 = (addrL & 0x003) | ((addrL >> 8) & 0x7f) << 2;
  103. addr2 = (addrL >> 15) & 0x1ff;
  104. addr3 = ((addrL >> 24) & 0x0ff) | ((addrH & 1) << 8);
  105. } else {
  106. addr0 = FLIP_6_BITS (addrL & 0x03f);
  107. addr1 = FLIP_9_BITS (((addrL >> 6) & 0x1ff));
  108. addr2 = FLIP_9_BITS ((addrL >> 15) & 0x1ff);
  109. addr3 = FLIP_9_BITS ((((addrL >> 24) & 0x0ff) |
  110. ((addrH & 0x1) << 8)));
  111. }
  112. hashResult = (addr0 << 9) | (addr1 ^ addr2 ^ addr3);
  113. if (HashSize == _8K_TABLE) {
  114. hashResult = hashResult & 0xffff;
  115. } else {
  116. hashResult = hashResult & 0x07ff;
  117. }
  118. return (hashResult);
  119. }
  120. /*
  121. * ----------------------------------------------------------------------------
  122. * This function will add an entry to the address table.
  123. * depends on the hash mode and hash size that was initialized.
  124. * Inputs
  125. * port - ETHERNET port number.
  126. * macH - the 2 most significant bytes of the MAC address.
  127. * macL - the 4 least significant bytes of the MAC address.
  128. * skip - if 1, skip this address.
  129. * rd - the RD field in the address table.
  130. * Outputs
  131. * address table entry is added.
  132. * true if success.
  133. * false if table full
  134. */
  135. int addAddressTableEntry (u32 port, u32 macH, u32 macL, u32 rd, u32 skip)
  136. {
  137. addrTblEntry *entry;
  138. u32 newHi;
  139. u32 newLo;
  140. u32 i;
  141. newLo = (((macH >> 4) & 0xf) << 15)
  142. | (((macH >> 0) & 0xf) << 11)
  143. | (((macH >> 12) & 0xf) << 7)
  144. | (((macH >> 8) & 0xf) << 3)
  145. | (((macL >> 20) & 0x1) << 31)
  146. | (((macL >> 16) & 0xf) << 27)
  147. | (((macL >> 28) & 0xf) << 23)
  148. | (((macL >> 24) & 0xf) << 19)
  149. | (skip << SKIP_BIT) | (rd << 2) | VALID;
  150. newHi = (((macL >> 4) & 0xf) << 15)
  151. | (((macL >> 0) & 0xf) << 11)
  152. | (((macL >> 12) & 0xf) << 7)
  153. | (((macL >> 8) & 0xf) << 3)
  154. | (((macL >> 21) & 0x7) << 0);
  155. /*
  156. * Pick the appropriate table, start scanning for free/reusable
  157. * entries at the index obtained by hashing the specified MAC address
  158. */
  159. entry = addressTableBase[port];
  160. entry += hashTableFunction (macH, macL, addressTableHashSize[port],
  161. addressTableHashMode[port]);
  162. for (i = 0; i < HOP_NUMBER; i++, entry++) {
  163. if (!(entry->lo & VALID) /*|| (entry->lo & SKIP) */ ) {
  164. break;
  165. } else { /* if same address put in same position */
  166. if (((entry->lo & 0xfffffff8) == (newLo & 0xfffffff8))
  167. && (entry->hi == newHi)) {
  168. break;
  169. }
  170. }
  171. }
  172. if (i == HOP_NUMBER) {
  173. PRINTF ("addGT64260addressTableEntry: table section is full\n");
  174. return false;
  175. }
  176. /*
  177. * Update the selected entry
  178. */
  179. entry->hi = newHi;
  180. entry->lo = newLo;
  181. DCACHE_FLUSH_N_SYNC ((u32) entry, MAC_ENTRY_SIZE);
  182. return true;
  183. }
  184. #endif /* CONFIG_GT_USE_MAC_HASH_TABLE */