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