1 /*
2 * Texas Instruments 3-Port Ethernet Switch Address Lookup Engine
3 *
4 * Copyright (C) 2010 Texas Instruments
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
9 *
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
17 #include <linux/seq_file.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/io.h>
21 #include <linux/stat.h>
22 #include <linux/sysfs.h>
24 #include "cpsw_ale.h"
26 #define BITMASK(bits) (BIT(bits) - 1)
27 #define ADDR_FMT_STR "%02x:%02x:%02x:%02x:%02x:%02x"
28 #define ADDR_FMT_ARGS(addr) (addr)[0], (addr)[1], (addr)[2], \
29 (addr)[3], (addr)[4], (addr)[5]
30 #define ALE_ENTRY_BITS 68
31 #define ALE_ENTRY_WORDS DIV_ROUND_UP(ALE_ENTRY_BITS, 32)
33 /* ALE Registers */
34 #define ALE_IDVER 0x00
35 #define ALE_CONTROL 0x08
36 #define ALE_PRESCALE 0x10
37 #define ALE_UNKNOWNVLAN 0x18
38 #define ALE_TABLE_CONTROL 0x20
39 #define ALE_TABLE 0x34
40 #define ALE_PORTCTL 0x40
42 #define ALE_TABLE_WRITE BIT(31)
44 #define ALE_TYPE_FREE 0
45 #define ALE_TYPE_ADDR 1
46 #define ALE_TYPE_VLAN 2
47 #define ALE_TYPE_VLAN_ADDR 3
49 #define ALE_UCAST_PERSISTANT 0
50 #define ALE_UCAST_UNTOUCHED 1
51 #define ALE_UCAST_OUI 2
52 #define ALE_UCAST_TOUCHED 3
54 #define ALE_MCAST_FWD 0
55 #define ALE_MCAST_BLOCK_LEARN_FWD 1
56 #define ALE_MCAST_FWD_LEARN 2
57 #define ALE_MCAST_FWD_2 3
59 /* the following remap params into members of cpsw_ale */
60 #define ale_regs params.ale_regs
61 #define ale_entries params.ale_entries
62 #define ale_ports params.ale_ports
64 static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
65 {
66 int idx;
68 idx = start / 32;
69 start -= idx * 32;
70 idx = 2 - idx; /* flip */
71 return (ale_entry[idx] >> start) & BITMASK(bits);
72 }
74 static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
75 u32 value)
76 {
77 int idx;
79 value &= BITMASK(bits);
80 idx = start / 32;
81 start -= idx * 32;
82 idx = 2 - idx; /* flip */
83 ale_entry[idx] &= ~(BITMASK(bits) << start);
84 ale_entry[idx] |= (value << start);
85 }
87 #define DEFINE_ALE_FIELD(name, start, bits) \
88 static inline int cpsw_ale_get_##name(u32 *ale_entry) \
89 { \
90 return cpsw_ale_get_field(ale_entry, start, bits); \
91 } \
92 static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value) \
93 { \
94 cpsw_ale_set_field(ale_entry, start, bits, value); \
95 }
97 DEFINE_ALE_FIELD(entry_type, 60, 2)
98 DEFINE_ALE_FIELD(vlan_id, 48, 12)
99 DEFINE_ALE_FIELD(mcast_state, 62, 2)
100 DEFINE_ALE_FIELD(port_mask, 66, 3)
101 DEFINE_ALE_FIELD(super, 65, 1)
102 DEFINE_ALE_FIELD(ucast_type, 62, 2)
103 DEFINE_ALE_FIELD(port_num, 66, 2)
104 DEFINE_ALE_FIELD(blocked, 65, 1)
105 DEFINE_ALE_FIELD(secure, 64, 1)
106 DEFINE_ALE_FIELD(vlan_untag_force, 24, 3)
107 DEFINE_ALE_FIELD(vlan_reg_mcast, 16, 3)
108 DEFINE_ALE_FIELD(vlan_unreg_mcast, 8, 3)
109 DEFINE_ALE_FIELD(vlan_member_list, 0, 3)
110 DEFINE_ALE_FIELD(mcast, 40, 1)
112 /* The MAC address field in the ALE entry cannot be macroized as above */
113 static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
114 {
115 int i;
117 for (i = 0; i < 6; i++)
118 addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
119 }
121 static inline void cpsw_ale_set_addr(u32 *ale_entry, u8 *addr)
122 {
123 int i;
125 for (i = 0; i < 6; i++)
126 cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
127 }
129 static int cpsw_ale_read(struct cpsw_ale *ale, int idx, u32 *ale_entry)
130 {
131 int i;
133 WARN_ON(idx > ale->ale_entries);
135 __raw_writel(idx, ale->ale_regs + ALE_TABLE_CONTROL);
137 for (i = 0; i < ALE_ENTRY_WORDS; i++)
138 ale_entry[i] = __raw_readl(ale->ale_regs + ALE_TABLE + 4 * i);
140 return idx;
141 }
143 static int cpsw_ale_write(struct cpsw_ale *ale, int idx, u32 *ale_entry)
144 {
145 int i;
147 WARN_ON(idx > ale->ale_entries);
149 for (i = 0; i < ALE_ENTRY_WORDS; i++)
150 __raw_writel(ale_entry[i], ale->ale_regs + ALE_TABLE + 4 * i);
152 __raw_writel(idx | ALE_TABLE_WRITE, ale->ale_regs + ALE_TABLE_CONTROL);
154 return idx;
155 }
157 static int cpsw_ale_match_addr(struct cpsw_ale *ale, u8* addr)
158 {
159 u32 ale_entry[ALE_ENTRY_WORDS];
160 int type, idx;
162 for (idx = 0; idx < ale->ale_entries; idx++) {
163 u8 entry_addr[6];
165 cpsw_ale_read(ale, idx, ale_entry);
166 type = cpsw_ale_get_entry_type(ale_entry);
167 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
168 continue;
169 cpsw_ale_get_addr(ale_entry, entry_addr);
170 if (memcmp(entry_addr, addr, 6) == 0)
171 return idx;
172 }
173 return -ENOENT;
174 }
176 static int cpsw_ale_match_free(struct cpsw_ale *ale)
177 {
178 u32 ale_entry[ALE_ENTRY_WORDS];
179 int type, idx;
181 for (idx = 0; idx < ale->ale_entries; idx++) {
182 cpsw_ale_read(ale, idx, ale_entry);
183 type = cpsw_ale_get_entry_type(ale_entry);
184 if (type == ALE_TYPE_FREE)
185 return idx;
186 }
187 return -ENOENT;
188 }
190 static int cpsw_ale_find_ageable(struct cpsw_ale *ale)
191 {
192 u32 ale_entry[ALE_ENTRY_WORDS];
193 int type, idx;
195 for (idx = 0; idx < ale->ale_entries; idx++) {
196 cpsw_ale_read(ale, idx, ale_entry);
197 type = cpsw_ale_get_entry_type(ale_entry);
198 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
199 continue;
200 if (cpsw_ale_get_mcast(ale_entry))
201 continue;
202 type = cpsw_ale_get_ucast_type(ale_entry);
203 if (type != ALE_UCAST_PERSISTANT &&
204 type != ALE_UCAST_OUI)
205 return idx;
206 }
207 return -ENOENT;
208 }
210 static void cpsw_ale_flush_mcast(struct cpsw_ale *ale, u32 *ale_entry,
211 int port_mask)
212 {
213 int mask;
215 mask = cpsw_ale_get_port_mask(ale_entry);
216 if ((mask & port_mask) == 0)
217 return; /* ports dont intersect, not interested */
218 mask &= ~port_mask;
220 /* free if only remaining port is host port */
221 if (mask == BIT(ale->ale_ports))
222 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
223 else
224 cpsw_ale_set_port_mask(ale_entry, mask);
225 }
227 static void cpsw_ale_flush_ucast(struct cpsw_ale *ale, u32 *ale_entry,
228 int port_mask)
229 {
230 int port;
232 port = cpsw_ale_get_port_num(ale_entry);
233 if ((BIT(port) & port_mask) == 0)
234 return; /* ports dont intersect, not interested */
235 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
236 }
238 int cpsw_ale_flush(struct cpsw_ale *ale, int port_mask)
239 {
240 u32 ale_entry[ALE_ENTRY_WORDS];
241 int ret, idx;
243 for (idx = 0; idx < ale->ale_entries; idx++) {
244 cpsw_ale_read(ale, idx, ale_entry);
245 ret = cpsw_ale_get_entry_type(ale_entry);
246 if (ret != ALE_TYPE_ADDR && ret != ALE_TYPE_VLAN_ADDR)
247 continue;
249 if (cpsw_ale_get_mcast(ale_entry))
250 cpsw_ale_flush_mcast(ale, ale_entry, port_mask);
251 else
252 cpsw_ale_flush_ucast(ale, ale_entry, port_mask);
254 cpsw_ale_write(ale, idx, ale_entry);
255 }
256 return 0;
257 }
259 static int cpsw_ale_dump_mcast(u32 *ale_entry, char *buf, int len)
260 {
261 int outlen = 0;
262 static const char *str_mcast_state[] = {"f", "blf", "lf", "f"};
263 int mcast_state = cpsw_ale_get_mcast_state(ale_entry);
264 int port_mask = cpsw_ale_get_port_mask(ale_entry);
265 int super = cpsw_ale_get_super(ale_entry);
267 outlen += snprintf(buf + outlen, len - outlen,
268 "mcstate: %s(%d), ", str_mcast_state[mcast_state],
269 mcast_state);
270 outlen += snprintf(buf + outlen, len - outlen,
271 "port mask: %x, %ssuper\n", port_mask,
272 super ? "" : "no ");
273 return outlen;
274 }
276 static int cpsw_ale_dump_ucast(u32 *ale_entry, char *buf, int len)
277 {
278 int outlen = 0;
279 static const char *str_ucast_type[] = {"persistant", "untouched",
280 "oui", "touched"};
281 int ucast_type = cpsw_ale_get_ucast_type(ale_entry);
282 int port_num = cpsw_ale_get_port_num(ale_entry);
283 int secure = cpsw_ale_get_secure(ale_entry);
284 int blocked = cpsw_ale_get_blocked(ale_entry);
286 outlen += snprintf(buf + outlen, len - outlen,
287 "uctype: %s(%d), ", str_ucast_type[ucast_type],
288 ucast_type);
289 outlen += snprintf(buf + outlen, len - outlen,
290 "port: %d%s%s\n", port_num, secure ? ", Secure" : "",
291 blocked ? ", Blocked" : "");
292 return outlen;
293 }
295 static int cpsw_ale_dump_entry(int idx, u32 *ale_entry, char *buf, int len)
296 {
297 int type, outlen = 0;
298 u8 addr[6];
299 static const char *str_type[] = {"free", "addr", "vlan", "vlan+addr"};
301 type = cpsw_ale_get_entry_type(ale_entry);
302 if (type == ALE_TYPE_FREE)
303 return outlen;
305 if (idx >= 0) {
306 outlen += snprintf(buf + outlen, len - outlen,
307 "index %d, ", idx);
308 }
310 outlen += snprintf(buf + outlen, len - outlen, "raw: %08x %08x %08x, ",
311 ale_entry[0], ale_entry[1], ale_entry[2]);
313 outlen += snprintf(buf + outlen, len - outlen,
314 "type: %s(%d), ", str_type[type], type);
316 cpsw_ale_get_addr(ale_entry, addr);
317 outlen += snprintf(buf + outlen, len - outlen,
318 "addr: " ADDR_FMT_STR ", ", ADDR_FMT_ARGS(addr));
320 if (type == ALE_TYPE_VLAN || type == ALE_TYPE_VLAN_ADDR) {
321 outlen += snprintf(buf + outlen, len - outlen, "vlan: %d, ",
322 cpsw_ale_get_vlan_id(ale_entry));
323 }
325 outlen += cpsw_ale_get_mcast(ale_entry) ?
326 cpsw_ale_dump_mcast(ale_entry, buf + outlen, len - outlen) :
327 cpsw_ale_dump_ucast(ale_entry, buf + outlen, len - outlen);
329 return outlen;
330 }
332 int cpsw_ale_add_ucast(struct cpsw_ale *ale, u8 *addr, int port, int flags)
333 {
334 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
335 int idx;
337 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
338 cpsw_ale_set_addr(ale_entry, addr);
339 cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
340 cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
341 cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
342 cpsw_ale_set_port_num(ale_entry, port);
344 idx = cpsw_ale_match_addr(ale, addr);
345 if (idx < 0)
346 idx = cpsw_ale_match_free(ale);
347 if (idx < 0)
348 idx = cpsw_ale_find_ageable(ale);
349 if (idx < 0)
350 return -ENOMEM;
352 cpsw_ale_write(ale, idx, ale_entry);
353 return 0;
354 }
356 int cpsw_ale_del_ucast(struct cpsw_ale *ale, u8 *addr, int port)
357 {
358 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
359 int idx;
361 idx = cpsw_ale_match_addr(ale, addr);
362 if (idx < 0)
363 return -ENOENT;
365 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
366 cpsw_ale_write(ale, idx, ale_entry);
367 return 0;
368 }
370 int cpsw_ale_add_mcast(struct cpsw_ale *ale, u8 *addr, int port_mask)
371 {
372 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
373 int idx, mask;
375 idx = cpsw_ale_match_addr(ale, addr);
376 if (idx >= 0)
377 cpsw_ale_read(ale, idx, ale_entry);
379 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
380 cpsw_ale_set_addr(ale_entry, addr);
381 cpsw_ale_set_mcast_state(ale_entry, ALE_MCAST_FWD_2);
383 mask = cpsw_ale_get_port_mask(ale_entry);
384 port_mask |= mask;
385 cpsw_ale_set_port_mask(ale_entry, port_mask);
387 if (idx < 0)
388 idx = cpsw_ale_match_free(ale);
389 if (idx < 0)
390 idx = cpsw_ale_find_ageable(ale);
391 if (idx < 0)
392 return -ENOMEM;
394 cpsw_ale_write(ale, idx, ale_entry);
395 return 0;
396 }
398 int cpsw_ale_del_mcast(struct cpsw_ale *ale, u8 *addr, int port_mask)
399 {
400 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
401 int idx, mask;
403 idx = cpsw_ale_match_addr(ale, addr);
404 if (idx < 0)
405 return -EINVAL;
407 cpsw_ale_read(ale, idx, ale_entry);
408 mask = cpsw_ale_get_port_mask(ale_entry);
409 port_mask = mask & ~port_mask;
411 if (port_mask == BIT(ale->ale_ports))
412 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
413 else
414 cpsw_ale_set_port_mask(ale_entry, port_mask);
416 cpsw_ale_write(ale, idx, ale_entry);
417 return 0;
418 }
420 struct ale_control_info {
421 const char *name;
422 int offset, port_offset;
423 int shift, port_shift;
424 int bits;
425 };
427 #define CTRL_GLOBAL(name, bit) {#name, ALE_CONTROL, 0, bit, 0, 1}
428 #define CTRL_UNK(name, bit) {#name, ALE_UNKNOWNVLAN, 0, bit, 1, 1}
429 #define CTRL_PORTCTL(name, start, bits) {#name, ALE_PORTCTL, 4, start, 0, bits}
431 static struct ale_control_info ale_controls[] = {
432 [ALE_ENABLE] = CTRL_GLOBAL(enable, 31),
433 [ALE_CLEAR] = CTRL_GLOBAL(clear, 30),
434 [ALE_AGEOUT] = CTRL_GLOBAL(ageout, 29),
435 [ALE_VLAN_NOLEARN] = CTRL_GLOBAL(vlan_nolearn, 7),
436 [ALE_NO_PORT_VLAN] = CTRL_GLOBAL(no_port_vlan, 6),
437 [ALE_OUI_DENY] = CTRL_GLOBAL(oui_deny, 5),
438 [ALE_BYPASS] = CTRL_GLOBAL(bypass, 4),
439 [ALE_RATE_LIMIT_TX] = CTRL_GLOBAL(rate_limit_tx, 3),
440 [ALE_VLAN_AWARE] = CTRL_GLOBAL(vlan_aware, 2),
441 [ALE_AUTH_ENABLE] = CTRL_GLOBAL(auth_enable, 1),
442 [ALE_RATE_LIMIT] = CTRL_GLOBAL(rate_limit, 0),
444 [ALE_PORT_STATE] = CTRL_PORTCTL(port_state, 0, 2),
445 [ALE_PORT_DROP_UNTAGGED] = CTRL_PORTCTL(drop_untagged, 2, 1),
446 [ALE_PORT_DROP_UNKNOWN_VLAN] = CTRL_PORTCTL(drop_unknown, 3, 1),
447 [ALE_PORT_NOLEARN] = CTRL_PORTCTL(nolearn, 4, 1),
448 [ALE_PORT_MCAST_LIMIT] = CTRL_PORTCTL(mcast_limit, 16, 8),
449 [ALE_PORT_BCAST_LIMIT] = CTRL_PORTCTL(bcast_limit, 24, 8),
451 [ALE_PORT_UNKNOWN_VLAN_MEMBER] = CTRL_UNK(unknown_vlan_member, 0),
452 [ALE_PORT_UNKNOWN_MCAST_FLOOD] = CTRL_UNK(unknown_mcast_flood, 8),
453 [ALE_PORT_UNKNOWN_REG_MCAST_FLOOD] = CTRL_UNK(unknown_reg_flood, 16),
454 [ALE_PORT_UNTAGGED_EGRESS] = CTRL_UNK(untagged_egress, 24),
455 };
457 int cpsw_ale_control_set(struct cpsw_ale *ale, int port, int control,
458 int value)
459 {
460 struct ale_control_info *info = &ale_controls[control];
461 int offset, shift;
462 u32 tmp, mask;
464 if (control < 0 || control >= ARRAY_SIZE(ale_controls))
465 return -EINVAL;
467 if (info->port_offset == 0 && info->port_shift == 0)
468 port = 0; /* global, port is a dont care */
470 if (port < 0 || port > ale->ale_ports)
471 return -EINVAL;
473 mask = BITMASK(info->bits);
474 if (value & ~mask)
475 return -EINVAL;
477 offset = info->offset + (port * info->port_offset);
478 shift = info->shift + (port * info->port_shift);
480 tmp = __raw_readl(ale->ale_regs + offset);
481 tmp = (tmp & ~(mask << shift)) | (value << shift);
482 __raw_writel(tmp, ale->ale_regs + offset);
484 {
485 volatile u32 dly = 10000;
486 while (dly--)
487 ;
488 }
489 return 0;
490 }
492 int cpsw_ale_control_get(struct cpsw_ale *ale, int port, int control)
493 {
494 struct ale_control_info *info = &ale_controls[control];
495 int offset, shift;
496 u32 tmp;
498 if (control < 0 || control >= ARRAY_SIZE(ale_controls))
499 return -EINVAL;
501 if (info->port_offset == 0 && info->port_shift == 0)
502 port = 0; /* global, port is a dont care */
504 if (port < 0 || port > ale->ale_ports)
505 return -EINVAL;
507 offset = info->offset + (port * info->port_offset);
508 shift = info->shift + (port * info->port_shift);
510 tmp = __raw_readl(ale->ale_regs + offset) >> shift;
511 return tmp & BITMASK(info->bits);
512 }
514 static ssize_t cpsw_ale_control_show(struct device *dev,
515 struct device_attribute *attr,
516 char *buf)
517 {
518 int i, port, len = 0;
519 struct ale_control_info *info;
520 struct cpsw_ale *ale = control_attr_to_ale(attr);
522 for (i = 0, info = ale_controls; i < ALE_NUM_CONTROLS; i++, info++) {
523 /* global controls */
524 if (info->port_shift == 0 && info->port_offset == 0) {
525 len += snprintf(buf + len, SZ_4K - len,
526 "%s=%d\n", info->name,
527 cpsw_ale_control_get(ale, 0, i));
528 continue;
529 }
530 /* port specific controls */
531 for (port = 0; port < ale->ale_ports; port++) {
532 len += snprintf(buf + len, SZ_4K - len,
533 "%s.%d=%d\n", info->name, port,
534 cpsw_ale_control_get(ale, port, i));
535 }
536 }
537 return len;
538 }
540 static ssize_t cpsw_ale_control_store(struct device *dev,
541 struct device_attribute *attr,
542 const char *buf, size_t count)
543 {
544 char ctrl_str[33], *end;
545 int port = 0, value, len, ret, control;
546 struct cpsw_ale *ale = control_attr_to_ale(attr);
548 len = strcspn(buf, ".=");
549 if (len >= 32)
550 return -ENOMEM;
551 strncpy(ctrl_str, buf, len);
552 ctrl_str[len] = '\0';
553 buf += len;
555 if (*buf == '.') {
556 port = simple_strtoul(buf + 1, &end, 0);
557 buf = end;
558 }
560 if (*buf != '=')
561 return -EINVAL;
563 value = simple_strtoul(buf + 1, NULL, 0);
565 for (control = 0; control < ALE_NUM_CONTROLS; control++)
566 if (strcmp(ctrl_str, ale_controls[control].name) == 0)
567 break;
569 if (control >= ALE_NUM_CONTROLS)
570 return -ENOENT;
572 dev_dbg(ale->params.dev, "processing command %s.%d=%d\n",
573 ale_controls[control].name, port, value);
575 ret = cpsw_ale_control_set(ale, port, control, value);
576 if (ret < 0)
577 return ret;
578 return count;
579 }
581 DEVICE_ATTR(ale_control, S_IRUGO | S_IWUSR, cpsw_ale_control_show,
582 cpsw_ale_control_store);
584 static ssize_t cpsw_ale_table_show(struct device *dev,
585 struct device_attribute *attr,
586 char *buf)
587 {
588 int len = SZ_4K, outlen = 0, idx;
589 u32 ale_entry[ALE_ENTRY_WORDS];
590 struct cpsw_ale *ale = table_attr_to_ale(attr);
592 for (idx = 0; idx < ale->ale_entries; idx++) {
593 cpsw_ale_read(ale, idx, ale_entry);
594 outlen += cpsw_ale_dump_entry(idx, ale_entry, buf + outlen,
595 len - outlen);
596 }
597 return outlen;
598 }
599 DEVICE_ATTR(ale_table, S_IRUGO, cpsw_ale_table_show, NULL);
601 static void cpsw_ale_timer(unsigned long arg)
602 {
603 struct cpsw_ale *ale = (struct cpsw_ale *)arg;
605 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
607 if (ale->ageout) {
608 ale->timer.expires = jiffies + ale->ageout;
609 add_timer(&ale->timer);
610 }
611 }
613 int cpsw_ale_set_ageout(struct cpsw_ale *ale, int ageout)
614 {
615 del_timer_sync(&ale->timer);
616 ale->ageout = ageout * HZ;
617 if (ale->ageout) {
618 ale->timer.expires = jiffies + ale->ageout;
619 add_timer(&ale->timer);
620 }
621 return 0;
622 }
624 void cpsw_ale_start(struct cpsw_ale *ale)
625 {
626 u32 rev;
627 int ret;
629 rev = __raw_readl(ale->ale_regs + ALE_IDVER);
630 dev_dbg(ale->params.dev, "initialized cpsw ale revision %d.%d\n",
631 (rev >> 8) & 0xff, rev & 0xff);
632 cpsw_ale_control_set(ale, 0, ALE_ENABLE, 1);
633 cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
635 ale->ale_control_attr = dev_attr_ale_control;
636 sysfs_attr_init(&ale->ale_control_attr.attr);
637 ret = device_create_file(ale->params.dev, &ale->ale_control_attr);
638 WARN_ON(ret < 0);
640 ale->ale_table_attr = dev_attr_ale_table;
641 sysfs_attr_init(&ale->ale_table_attr.attr);
642 ret = device_create_file(ale->params.dev, &ale->ale_table_attr);
643 WARN_ON(ret < 0);
645 init_timer(&ale->timer);
646 ale->timer.data = (unsigned long)ale;
647 ale->timer.function = cpsw_ale_timer;
648 if (ale->ageout) {
649 ale->timer.expires = jiffies + ale->ageout;
650 add_timer(&ale->timer);
651 }
652 }
654 void cpsw_ale_stop(struct cpsw_ale *ale)
655 {
656 del_timer_sync(&ale->timer);
657 device_remove_file(ale->params.dev, &ale->ale_table_attr);
658 device_remove_file(ale->params.dev, &ale->ale_control_attr);
659 }
661 struct cpsw_ale *cpsw_ale_create(struct cpsw_ale_params *params)
662 {
663 struct cpsw_ale *ale;
664 int ret;
666 ret = -ENOMEM;
667 ale = kzalloc(sizeof(*ale), GFP_KERNEL);
668 if (WARN_ON(!ale))
669 return NULL;
671 ale->params = *params;
672 ale->ageout = ale->params.ale_ageout * HZ;
674 return ale;
675 }
677 int cpsw_ale_destroy(struct cpsw_ale *ale)
678 {
679 if (!ale)
680 return -EINVAL;
681 cpsw_ale_stop(ale);
682 cpsw_ale_control_set(ale, 0, ALE_ENABLE, 0);
683 kfree(ale);
684 return 0;
685 }