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