blob: e8ef0e3e52ea415fc3b85a597bc3b2772171947f [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001#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
wdenke65527f2004-02-12 00:47:09 +000016static u32 addressTableHashMode[GAL_ETH_DEVS] = { 0, };
17static u32 addressTableHashSize[GAL_ETH_DEVS] = { 0, };
18static addrTblEntry *addressTableBase[GAL_ETH_DEVS] = { 0, };
19static void *realAddrTableBase[GAL_ETH_DEVS] = { 0, };
wdenkc6097192002-11-03 00:24:07 +000020
wdenke65527f2004-02-12 00:47:09 +000021static const u32 hashLength[2] = {
22 (0x8000), /* 8K * 4 entries */
23 (0x8000 / 16), /* 512 * 4 entries */
wdenkc6097192002-11-03 00:24:07 +000024};
25
26/* Initialize the address table for a port, if needed */
wdenke65527f2004-02-12 00:47:09 +000027unsigned int initAddressTable (u32 port, u32 hashMode, u32 hashSizeSelector)
wdenkc6097192002-11-03 00:24:07 +000028{
wdenke65527f2004-02-12 00:47:09 +000029 unsigned int tableBase;
wdenkc6097192002-11-03 00:24:07 +000030
wdenke65527f2004-02-12 00:47:09 +000031 if (port < 0 || port >= GAL_ETH_DEVS) {
32 printf ("%s: Invalid port number %d\n", __FUNCTION__, port);
wdenkc6097192002-11-03 00:24:07 +000033 return 0;
34 }
35
36 if (hashMode > 1) {
wdenke65527f2004-02-12 00:47:09 +000037 printf ("%s: Invalid Hash Mode %d\n", __FUNCTION__, port);
wdenkc6097192002-11-03 00:24:07 +000038 return 0;
39 }
40
wdenke65527f2004-02-12 00:47:09 +000041 if (realAddrTableBase[port] &&
42 (addressTableHashSize[port] != hashSizeSelector)) {
wdenkc6097192002-11-03 00:24:07 +000043 /* we have been here before,
44 * but now we want a different sized table
45 */
wdenke65527f2004-02-12 00:47:09 +000046 free (realAddrTableBase[port]);
wdenkc6097192002-11-03 00:24:07 +000047 realAddrTableBase[port] = 0;
48 addressTableBase[port] = 0;
49
50 }
51
wdenke65527f2004-02-12 00:47:09 +000052 tableBase = (unsigned int) addressTableBase[port];
wdenkc6097192002-11-03 00:24:07 +000053 /* we get called for every probe, so only do this once */
wdenke65527f2004-02-12 00:47:09 +000054 if (!tableBase) {
55 int bytes =
56 hashLength[hashSizeSelector] * sizeof (addrTblEntry);
wdenkc6097192002-11-03 00:24:07 +000057
Wolfgang Denk7fb52662005-10-13 16:45:02 +020058 realAddrTableBase[port] =
wdenke65527f2004-02-12 00:47:09 +000059 malloc (bytes + 64);
Wolfgang Denkf6a692b2005-12-04 00:40:34 +010060 tableBase = (unsigned int)realAddrTableBase;
wdenkc6097192002-11-03 00:24:07 +000061
wdenke65527f2004-02-12 00:47:09 +000062 if (!tableBase) {
63 printf ("%s: alloc memory failed \n", __FUNCTION__);
wdenkc6097192002-11-03 00:24:07 +000064 return 0;
65 }
66
wdenke65527f2004-02-12 00:47:09 +000067 /* align to octal byte */
68 if (tableBase & 63)
69 tableBase = (tableBase + 63) & ~63;
wdenkc6097192002-11-03 00:24:07 +000070
wdenke65527f2004-02-12 00:47:09 +000071 addressTableHashMode[port] = hashMode;
72 addressTableHashSize[port] = hashSizeSelector;
73 addressTableBase[port] = (addrTblEntry *) tableBase;
wdenkc6097192002-11-03 00:24:07 +000074
wdenke65527f2004-02-12 00:47:09 +000075 memset ((void *) tableBase, 0, bytes);
wdenkc6097192002-11-03 00:24:07 +000076 }
77
wdenke65527f2004-02-12 00:47:09 +000078 return tableBase;
wdenkc6097192002-11-03 00:24:07 +000079}
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 */
wdenke65527f2004-02-12 00:47:09 +000093u32 hashTableFunction (u32 macH, u32 macL, u32 HashSize, u32 hash_mode)
wdenkc6097192002-11-03 00:24:07 +000094{
wdenke65527f2004-02-12 00:47:09 +000095 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;
wdenkc6097192002-11-03 00:24:07 +0000104
105
wdenke65527f2004-02-12 00:47:09 +0000106 addrH = NIBBLE_SWAPPING_16_BIT (macH);
107 addrL = NIBBLE_SWAPPING_32_BIT (macL);
wdenkc6097192002-11-03 00:24:07 +0000108
wdenke65527f2004-02-12 00:47:09 +0000109 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);
wdenkc6097192002-11-03 00:24:07 +0000113
wdenke65527f2004-02-12 00:47:09 +0000114 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);
wdenkc6097192002-11-03 00:24:07 +0000122
wdenke65527f2004-02-12 00:47:09 +0000123 addrH = addrHSwapped;
124 addrL = addrLSwapped;
wdenkc6097192002-11-03 00:24:07 +0000125
wdenke65527f2004-02-12 00:47:09 +0000126 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 }
wdenkc6097192002-11-03 00:24:07 +0000138
wdenke65527f2004-02-12 00:47:09 +0000139 hashResult = (addr0 << 9) | (addr1 ^ addr2 ^ addr3);
wdenkc6097192002-11-03 00:24:07 +0000140
wdenke65527f2004-02-12 00:47:09 +0000141 if (HashSize == _8K_TABLE) {
142 hashResult = hashResult & 0xffff;
143 } else {
144 hashResult = hashResult & 0x07ff;
145 }
wdenkc6097192002-11-03 00:24:07 +0000146
wdenke65527f2004-02-12 00:47:09 +0000147 return (hashResult);
wdenkc6097192002-11-03 00:24:07 +0000148}
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 */
wdenke65527f2004-02-12 00:47:09 +0000166int addAddressTableEntry (u32 port, u32 macH, u32 macL, u32 rd, u32 skip)
wdenkc6097192002-11-03 00:24:07 +0000167{
wdenke65527f2004-02-12 00:47:09 +0000168 addrTblEntry *entry;
169 u32 newHi;
170 u32 newLo;
171 u32 i;
wdenkc6097192002-11-03 00:24:07 +0000172
wdenke65527f2004-02-12 00:47:09 +0000173 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;
wdenkc6097192002-11-03 00:24:07 +0000182
wdenke65527f2004-02-12 00:47:09 +0000183 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);
wdenkc6097192002-11-03 00:24:07 +0000188
wdenke65527f2004-02-12 00:47:09 +0000189 /*
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 }
wdenk57b2d802003-06-27 21:31:46 +0000205 }
wdenkc6097192002-11-03 00:24:07 +0000206
wdenke65527f2004-02-12 00:47:09 +0000207 if (i == HOP_NUMBER) {
208 PRINTF ("addGT64260addressTableEntry: table section is full\n");
209 return (FALSE);
210 }
wdenkc6097192002-11-03 00:24:07 +0000211
wdenke65527f2004-02-12 00:47:09 +0000212 /*
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);
wdenkc6097192002-11-03 00:24:07 +0000219}
220
221#endif /* CONFIG_GT_USE_MAC_HASH_TABLE */