eth_addrtbl.c 5.9 KB

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