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