eth_addrtbl.c 5.8 KB

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