2 * ADM5120 built-in ethernet switch driver
4 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
6 * This code was based on a driver for Linux 2.6.xx by Jeroen Vreeken.
7 * Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2005
8 * NAPI extension for the Jeroen's driver
9 * Copyright Thomas Langer (Thomas.Langer@infineon.com), 2007
10 * Copyright Friedrich Beckmann (Friedrich.Beckmann@infineon.com), 2007
11 * Inspiration for the Jeroen's driver came from the ADMtek 2.4 driver.
12 * Copyright ADMtek Inc.
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License version 2 as published
16 * by the Free Software Foundation.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <linux/interrupt.h>
24 #include <linux/ioport.h>
25 #include <linux/spinlock.h>
26 #include <linux/platform_device.h>
28 #include <linux/irq.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/skbuff.h>
34 #include <asm/mipsregs.h>
36 #include <asm/mach-adm5120/adm5120_info.h>
37 #include <asm/mach-adm5120/adm5120_defs.h>
38 #include <asm/mach-adm5120/adm5120_switch.h>
40 #include "adm5120sw.h"
42 #define DRV_NAME "adm5120-switch"
43 #define DRV_DESC "ADM5120 built-in ethernet switch driver"
44 #define DRV_VERSION "0.1.1"
46 #define CONFIG_ADM5120_SWITCH_NAPI 1
47 #undef CONFIG_ADM5120_SWITCH_DEBUG
49 /* ------------------------------------------------------------------------ */
51 #ifdef CONFIG_ADM5120_SWITCH_DEBUG
52 #define SW_DBG(f, a...) printk(KERN_DBG "%s: " f, DRV_NAME , ## a)
54 #define SW_DBG(f, a...) do {} while (0)
56 #define SW_ERR(f, a...) printk(KERN_ERR "%s: " f, DRV_NAME , ## a)
57 #define SW_INFO(f, a...) printk(KERN_INFO "%s: " f, DRV_NAME , ## a)
59 #define SWITCH_NUM_PORTS 6
60 #define ETH_CSUM_LEN 4
62 #define RX_MAX_PKTLEN 1550
63 #define RX_RING_SIZE 64
65 #define TX_RING_SIZE 32
66 #define TX_QUEUE_LEN 28 /* Limit ring entries actually used. */
67 #define TX_TIMEOUT HZ*400
69 #define RX_DESCS_SIZE (RX_RING_SIZE * sizeof(struct dma_desc *))
70 #define RX_SKBS_SIZE (RX_RING_SIZE * sizeof(struct sk_buff *))
71 #define TX_DESCS_SIZE (TX_RING_SIZE * sizeof(struct dma_desc *))
72 #define TX_SKBS_SIZE (TX_RING_SIZE * sizeof(struct sk_buff *))
74 #define SKB_ALLOC_LEN (RX_MAX_PKTLEN + 32)
75 #define SKB_RESERVE_LEN (NET_IP_ALIGN + NET_SKB_PAD)
77 #define SWITCH_INTS_HIGH (SWITCH_INT_SHD | SWITCH_INT_RHD | SWITCH_INT_HDF)
78 #define SWITCH_INTS_LOW (SWITCH_INT_SLD | SWITCH_INT_RLD | SWITCH_INT_LDF)
79 #define SWITCH_INTS_ERR (SWITCH_INT_RDE | SWITCH_INT_SDE | SWITCH_INT_CPUH)
80 #define SWITCH_INTS_Q (SWITCH_INT_P0QF | SWITCH_INT_P1QF | SWITCH_INT_P2QF | \
81 SWITCH_INT_P3QF | SWITCH_INT_P4QF | SWITCH_INT_P5QF | \
82 SWITCH_INT_CPQF | SWITCH_INT_GQF)
84 #define SWITCH_INTS_ALL (SWITCH_INTS_HIGH | SWITCH_INTS_LOW | \
85 SWITCH_INTS_ERR | SWITCH_INTS_Q | \
86 SWITCH_INT_MD | SWITCH_INT_PSC)
88 #define SWITCH_INTS_USED (SWITCH_INTS_LOW | SWITCH_INT_PSC)
89 #define SWITCH_INTS_POLL (SWITCH_INT_RLD | SWITCH_INT_LDF | SWITCH_INT_SLD)
91 /* ------------------------------------------------------------------------ */
93 struct adm5120_if_priv
{
94 struct net_device
*dev
;
97 unsigned int port_mask
;
99 #ifdef CONFIG_ADM5120_SWITCH_NAPI
100 struct napi_struct napi
;
106 #define DESC_OWN (1UL << 31) /* Owned by the switch */
107 #define DESC_EOR (1UL << 28) /* End of Ring */
108 #define DESC_ADDR_MASK 0x1FFFFFF
109 #define DESC_ADDR(x) ((__u32)(x) & DESC_ADDR_MASK)
111 #define DESC_BUF2_EN (1UL << 31) /* Buffer 2 enable */
114 /* definitions for tx/rx descriptors */
115 #define DESC_PKTLEN_SHIFT 16
116 #define DESC_PKTLEN_MASK 0x7FF
117 /* tx descriptor specific part */
118 #define DESC_CSUM (1UL << 31) /* Append checksum */
119 #define DESC_DSTPORT_SHIFT 8
120 #define DESC_DSTPORT_MASK 0x3F
121 #define DESC_VLAN_MASK 0x3F
122 /* rx descriptor specific part */
123 #define DESC_SRCPORT_SHIFT 12
124 #define DESC_SRCPORT_MASK 0x7
125 #define DESC_DA_MASK 0x3
126 #define DESC_DA_SHIFT 4
127 #define DESC_IPCSUM_FAIL (1UL << 3) /* IP checksum fail */
128 #define DESC_VLAN_TAG (1UL << 2) /* VLAN tag present */
129 #define DESC_TYPE_MASK 0x3 /* mask for Packet type */
130 #define DESC_TYPE_IP 0x0 /* IP packet */
131 #define DESC_TYPE_PPPoE 0x1 /* PPPoE packet */
132 } __attribute__ ((aligned(16)));
134 /* ------------------------------------------------------------------------ */
136 static int adm5120_nrdevs
;
138 static struct net_device
*adm5120_devs
[SWITCH_NUM_PORTS
];
139 /* Lookup table port -> device */
140 static struct net_device
*adm5120_port
[SWITCH_NUM_PORTS
];
142 static struct dma_desc
*txl_descs
;
143 static struct dma_desc
*rxl_descs
;
145 static dma_addr_t txl_descs_dma
;
146 static dma_addr_t rxl_descs_dma
;
148 static struct sk_buff
**txl_skbuff
;
149 static struct sk_buff
**rxl_skbuff
;
151 static unsigned int cur_rxl
, dirty_rxl
; /* producer/consumer ring indices */
152 static unsigned int cur_txl
, dirty_txl
;
154 static unsigned int sw_used
;
156 static spinlock_t tx_lock
= SPIN_LOCK_UNLOCKED
;
158 /* ------------------------------------------------------------------------ */
160 static inline u32
sw_read_reg(u32 reg
)
162 return __raw_readl((void __iomem
*)KSEG1ADDR(ADM5120_SWITCH_BASE
)+reg
);
165 static inline void sw_write_reg(u32 reg
, u32 val
)
167 __raw_writel(val
, (void __iomem
*)KSEG1ADDR(ADM5120_SWITCH_BASE
)+reg
);
170 static inline void sw_int_mask(u32 mask
)
174 t
= sw_read_reg(SWITCH_REG_INT_MASK
);
176 sw_write_reg(SWITCH_REG_INT_MASK
, t
);
179 static inline void sw_int_unmask(u32 mask
)
183 t
= sw_read_reg(SWITCH_REG_INT_MASK
);
185 sw_write_reg(SWITCH_REG_INT_MASK
, t
);
188 static inline void sw_int_ack(u32 mask
)
190 sw_write_reg(SWITCH_REG_INT_STATUS
, mask
);
193 static inline u32
sw_int_status(void)
197 t
= sw_read_reg(SWITCH_REG_INT_STATUS
);
198 t
&= ~sw_read_reg(SWITCH_REG_INT_MASK
);
202 static inline u32
desc_get_srcport(struct dma_desc
*desc
)
204 return (desc
->misc
>> DESC_SRCPORT_SHIFT
) & DESC_SRCPORT_MASK
;
207 static inline u32
desc_get_pktlen(struct dma_desc
*desc
)
209 return (desc
->misc
>> DESC_PKTLEN_SHIFT
) & DESC_PKTLEN_MASK
;
212 static inline int desc_ipcsum_fail(struct dma_desc
*desc
)
214 return ((desc
->misc
& DESC_IPCSUM_FAIL
) != 0);
217 /* ------------------------------------------------------------------------ */
219 static void sw_dump_desc(char *label
, struct dma_desc
*desc
, int tx
)
223 SW_DBG("%s %s desc/%p\n", label
, tx
? "tx" : "rx", desc
);
226 SW_DBG(" buf1 %08X addr=%08X; len=%08X %s%s\n", t
,
229 (t
& DESC_OWN
) ? "SWITCH" : "CPU",
230 (t
& DESC_EOR
) ? " RE" : "");
233 SW_DBG(" buf2 %08X addr=%08X%s\n", desc
->buf2
,
235 (t
& DESC_BUF2_EN
) ? " EN" : "" );
239 SW_DBG(" misc %08X%s pktlen=%04X ports=%02X vlan=%02X\n", t
,
240 (t
& DESC_CSUM
) ? " CSUM" : "",
241 (t
>> DESC_PKTLEN_SHIFT
) & DESC_PKTLEN_MASK
,
242 (t
>> DESC_DSTPORT_SHIFT
) & DESC_DSTPORT_MASK
,
245 SW_DBG(" misc %08X pktlen=%04X port=%d DA=%d%s%s type=%d\n",
247 (t
>> DESC_PKTLEN_SHIFT
) & DESC_PKTLEN_MASK
,
248 (t
>> DESC_SRCPORT_SHIFT
) & DESC_SRCPORT_MASK
,
249 (t
>> DESC_DA_SHIFT
) & DESC_DA_MASK
,
250 (t
& DESC_IPCSUM_FAIL
) ? " IPCF" : "",
251 (t
& DESC_VLAN_TAG
) ? " VLAN" : "",
252 (t
& DESC_TYPE_MASK
));
255 static void sw_dump_intr_mask(char *label
, u32 mask
)
257 SW_DBG("%s %08X%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
259 (mask
& SWITCH_INT_SHD
) ? " SHD" : "",
260 (mask
& SWITCH_INT_SLD
) ? " SLD" : "",
261 (mask
& SWITCH_INT_RHD
) ? " RHD" : "",
262 (mask
& SWITCH_INT_RLD
) ? " RLD" : "",
263 (mask
& SWITCH_INT_HDF
) ? " HDF" : "",
264 (mask
& SWITCH_INT_LDF
) ? " LDF" : "",
265 (mask
& SWITCH_INT_P0QF
) ? " P0QF" : "",
266 (mask
& SWITCH_INT_P1QF
) ? " P1QF" : "",
267 (mask
& SWITCH_INT_P2QF
) ? " P2QF" : "",
268 (mask
& SWITCH_INT_P3QF
) ? " P3QF" : "",
269 (mask
& SWITCH_INT_P4QF
) ? " P4QF" : "",
270 (mask
& SWITCH_INT_CPQF
) ? " CPQF" : "",
271 (mask
& SWITCH_INT_GQF
) ? " GQF" : "",
272 (mask
& SWITCH_INT_MD
) ? " MD" : "",
273 (mask
& SWITCH_INT_BCS
) ? " BCS" : "",
274 (mask
& SWITCH_INT_PSC
) ? " PSC" : "",
275 (mask
& SWITCH_INT_ID
) ? " ID" : "",
276 (mask
& SWITCH_INT_W0TE
) ? " W0TE" : "",
277 (mask
& SWITCH_INT_W1TE
) ? " W1TE" : "",
278 (mask
& SWITCH_INT_RDE
) ? " RDE" : "",
279 (mask
& SWITCH_INT_SDE
) ? " SDE" : "",
280 (mask
& SWITCH_INT_CPUH
) ? " CPUH" : "");
283 static void sw_dump_regs(void)
287 t
= sw_read_reg(SWITCH_REG_PHY_STATUS
);
288 SW_DBG("phy_status: %08X\n", t
);
290 t
= sw_read_reg(SWITCH_REG_CPUP_CONF
);
291 SW_DBG("cpup_conf: %08X%s%s%s\n", t
,
292 (t
& CPUP_CONF_DCPUP
) ? " DCPUP" : "",
293 (t
& CPUP_CONF_CRCP
) ? " CRCP" : "",
294 (t
& CPUP_CONF_BTM
) ? " BTM" : "");
296 t
= sw_read_reg(SWITCH_REG_PORT_CONF0
);
297 SW_DBG("port_conf0: %08X\n", t
);
298 t
= sw_read_reg(SWITCH_REG_PORT_CONF1
);
299 SW_DBG("port_conf1: %08X\n", t
);
300 t
= sw_read_reg(SWITCH_REG_PORT_CONF2
);
301 SW_DBG("port_conf2: %08X\n", t
);
303 t
= sw_read_reg(SWITCH_REG_VLAN_G1
);
304 SW_DBG("vlan g1: %08X\n", t
);
305 t
= sw_read_reg(SWITCH_REG_VLAN_G2
);
306 SW_DBG("vlan g2: %08X\n", t
);
308 t
= sw_read_reg(SWITCH_REG_BW_CNTL0
);
309 SW_DBG("bw_cntl0: %08X\n", t
);
310 t
= sw_read_reg(SWITCH_REG_BW_CNTL1
);
311 SW_DBG("bw_cntl1: %08X\n", t
);
313 t
= sw_read_reg(SWITCH_REG_PHY_CNTL0
);
314 SW_DBG("phy_cntl0: %08X\n", t
);
315 t
= sw_read_reg(SWITCH_REG_PHY_CNTL1
);
316 SW_DBG("phy_cntl1: %08X\n", t
);
317 t
= sw_read_reg(SWITCH_REG_PHY_CNTL2
);
318 SW_DBG("phy_cntl2: %08X\n", t
);
319 t
= sw_read_reg(SWITCH_REG_PHY_CNTL3
);
320 SW_DBG("phy_cntl3: %08X\n", t
);
321 t
= sw_read_reg(SWITCH_REG_PHY_CNTL4
);
322 SW_DBG("phy_cntl4: %08X\n", t
);
324 t
= sw_read_reg(SWITCH_REG_INT_STATUS
);
325 sw_dump_intr_mask("int_status: ", t
);
327 t
= sw_read_reg(SWITCH_REG_INT_MASK
);
328 sw_dump_intr_mask("int_mask: ", t
);
330 t
= sw_read_reg(SWITCH_REG_SHDA
);
331 SW_DBG("shda: %08X\n", t
);
332 t
= sw_read_reg(SWITCH_REG_SLDA
);
333 SW_DBG("slda: %08X\n", t
);
334 t
= sw_read_reg(SWITCH_REG_RHDA
);
335 SW_DBG("rhda: %08X\n", t
);
336 t
= sw_read_reg(SWITCH_REG_RLDA
);
337 SW_DBG("rlda: %08X\n", t
);
340 /* ------------------------------------------------------------------------ */
342 static inline void adm5120_rx_dma_update(struct dma_desc
*desc
,
343 struct sk_buff
*skb
, int end
)
347 desc
->buflen
= RX_MAX_PKTLEN
;
348 desc
->buf1
= DESC_ADDR(skb
->data
) |
349 DESC_OWN
| (end
? DESC_EOR
: 0);
352 static void adm5120_switch_rx_refill(void)
356 for (; cur_rxl
- dirty_rxl
> 0; dirty_rxl
++) {
357 struct dma_desc
*desc
;
360 entry
= dirty_rxl
% RX_RING_SIZE
;
361 desc
= &rxl_descs
[entry
];
363 skb
= rxl_skbuff
[entry
];
365 skb
= alloc_skb(SKB_ALLOC_LEN
, GFP_ATOMIC
);
367 skb_reserve(skb
, SKB_RESERVE_LEN
);
368 rxl_skbuff
[entry
] = skb
;
370 SW_ERR("no memory for skb\n");
374 desc
->buf1
= (desc
->buf1
& DESC_EOR
) | DESC_OWN
;
380 desc
->buflen
= RX_MAX_PKTLEN
;
382 desc
->buf1
= (desc
->buf1
& DESC_EOR
) | DESC_OWN
|
383 DESC_ADDR(skb
->data
);
387 static int adm5120_switch_rx(int limit
)
389 unsigned int done
= 0;
391 SW_DBG("rx start, limit=%d, cur_rxl=%u, dirty_rxl=%u\n",
392 limit
, cur_rxl
, dirty_rxl
);
394 while (done
< limit
) {
395 int entry
= cur_rxl
% RX_RING_SIZE
;
396 struct dma_desc
*desc
= &rxl_descs
[entry
];
397 struct net_device
*rdev
;
400 if (desc
->buf1
& DESC_OWN
)
403 if (dirty_rxl
+ RX_RING_SIZE
== cur_rxl
)
406 port
= desc_get_srcport(desc
);
407 rdev
= adm5120_port
[port
];
409 SW_DBG("rx descriptor %u, desc=%p, skb=%p\n", entry
, desc
,
412 if ((rdev
) && netif_running(rdev
)) {
413 struct sk_buff
*skb
= rxl_skbuff
[entry
];
416 pktlen
= desc_get_pktlen(desc
);
417 pktlen
-= ETH_CSUM_LEN
;
419 if ((pktlen
== 0) || desc_ipcsum_fail(desc
)) {
420 rdev
->stats
.rx_errors
++;
422 rdev
->stats
.rx_length_errors
++;
423 if (desc_ipcsum_fail(desc
))
424 rdev
->stats
.rx_crc_errors
++;
425 SW_DBG("rx error, recycling skb %u\n", entry
);
427 skb_put(skb
, pktlen
);
430 skb
->protocol
= eth_type_trans(skb
, rdev
);
431 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
433 dma_cache_wback_inv((unsigned long)skb
->data
,
436 #ifdef CONFIG_ADM5120_SWITCH_NAPI
437 netif_receive_skb(skb
);
442 rdev
->last_rx
= jiffies
;
443 rdev
->stats
.rx_packets
++;
444 rdev
->stats
.rx_bytes
+= pktlen
;
446 rxl_skbuff
[entry
] = NULL
;
450 SW_DBG("no rx device, recycling skb %u\n", entry
);
454 if (cur_rxl
- dirty_rxl
> RX_RING_SIZE
/ 4)
455 adm5120_switch_rx_refill();
458 adm5120_switch_rx_refill();
460 SW_DBG("rx finished, cur_rxl=%u, dirty_rxl=%u, processed %d\n",
461 cur_rxl
, dirty_rxl
, done
);
466 static void adm5120_switch_tx(void)
471 entry
= dirty_txl
% TX_RING_SIZE
;
472 while (dirty_txl
!= cur_txl
) {
473 struct dma_desc
*desc
= &txl_descs
[entry
];
474 struct sk_buff
*skb
= txl_skbuff
[entry
];
476 if (desc
->buf1
& DESC_OWN
)
479 if (netif_running(skb
->dev
)) {
480 skb
->dev
->stats
.tx_bytes
+= skb
->len
;
481 skb
->dev
->stats
.tx_packets
++;
484 dev_kfree_skb_irq(skb
);
485 txl_skbuff
[entry
] = NULL
;
486 entry
= (++dirty_txl
) % TX_RING_SIZE
;
489 if ((cur_txl
- dirty_txl
) < TX_QUEUE_LEN
- 4) {
491 for (i
= 0; i
< SWITCH_NUM_PORTS
; i
++) {
492 if (!adm5120_devs
[i
])
494 netif_wake_queue(adm5120_devs
[i
]);
497 spin_unlock(&tx_lock
);
500 #ifdef CONFIG_ADM5120_SWITCH_NAPI
501 static int adm5120_if_poll(struct napi_struct
*napi
, int limit
)
503 struct adm5120_if_priv
*priv
= container_of(napi
,
504 struct adm5120_if_priv
, napi
);
505 struct net_device
*dev
= priv
->dev
;
509 sw_int_ack(SWITCH_INTS_POLL
);
511 SW_DBG("%s: processing TX ring\n", dev
->name
);
514 SW_DBG("%s: processing RX ring\n", dev
->name
);
515 done
= adm5120_switch_rx(limit
);
517 status
= sw_int_status() & SWITCH_INTS_POLL
;
518 if ((done
< limit
) && (!status
)) {
519 SW_DBG("disable polling mode for %s\n", dev
->name
);
520 netif_rx_complete(dev
, napi
);
521 sw_int_unmask(SWITCH_INTS_POLL
);
525 SW_DBG("%s still in polling mode, done=%d, status=%x\n",
526 dev
->name
, done
, status
);
529 #endif /* CONFIG_ADM5120_SWITCH_NAPI */
532 static irqreturn_t
adm5120_switch_irq(int irq
, void *dev_id
)
536 status
= sw_int_status();
537 status
&= SWITCH_INTS_ALL
;
541 #ifdef CONFIG_ADM5120_SWITCH_NAPI
542 sw_int_ack(status
& ~SWITCH_INTS_POLL
);
544 if (status
& SWITCH_INTS_POLL
) {
545 struct net_device
*dev
= dev_id
;
546 struct adm5120_if_priv
*priv
= netdev_priv(dev
);
548 sw_dump_intr_mask("poll ints", status
);
549 SW_DBG("enable polling mode for %s\n", dev
->name
);
550 sw_int_mask(SWITCH_INTS_POLL
);
551 netif_rx_schedule(dev
, &priv
->napi
);
556 if (status
& (SWITCH_INT_RLD
| SWITCH_INT_LDF
)) {
557 adm5120_switch_rx(RX_RING_SIZE
);
560 if (status
& SWITCH_INT_SLD
) {
568 static void adm5120_set_bw(char *matrix
)
572 /* Port 0 to 3 are set using the bandwidth control 0 register */
573 val
= matrix
[0] + (matrix
[1]<<8) + (matrix
[2]<<16) + (matrix
[3]<<24);
574 sw_write_reg(SWITCH_REG_BW_CNTL0
, val
);
576 /* Port 4 and 5 are set using the bandwidth control 1 register */
579 sw_write_reg(SWITCH_REG_BW_CNTL1
, val
| 0x80000000);
581 sw_write_reg(SWITCH_REG_BW_CNTL1
, val
& ~0x8000000);
583 SW_DBG("D: ctl0 0x%ux, ctl1 0x%ux\n", sw_read_reg(SWITCH_REG_BW_CNTL0
),
584 sw_read_reg(SWITCH_REG_BW_CNTL1
));
587 static void adm5120_switch_tx_ring_reset(struct dma_desc
*desc
,
588 struct sk_buff
**skbl
, int num
)
590 memset(desc
, 0, num
* sizeof(*desc
));
591 desc
[num
-1].buf1
|= DESC_EOR
;
592 memset(skbl
, 0, sizeof(struct skb
*)*num
);
598 static void adm5120_switch_rx_ring_reset(struct dma_desc
*desc
,
599 struct sk_buff
**skbl
, int num
)
603 memset(desc
, 0, num
* sizeof(*desc
));
604 for (i
= 0; i
< num
; i
++) {
605 skbl
[i
] = dev_alloc_skb(SKB_ALLOC_LEN
);
610 skb_reserve(skbl
[i
], SKB_RESERVE_LEN
);
611 adm5120_rx_dma_update(&desc
[i
], skbl
[i
], (num
-1==i
));
618 static int adm5120_switch_tx_ring_alloc(void)
622 txl_descs
= dma_alloc_coherent(NULL
, TX_DESCS_SIZE
, &txl_descs_dma
,
629 txl_skbuff
= kzalloc(TX_SKBS_SIZE
, GFP_KERNEL
);
641 static void adm5120_switch_tx_ring_free(void)
646 for (i
= 0; i
< TX_RING_SIZE
; i
++)
648 kfree_skb(txl_skbuff
[i
]);
653 dma_free_coherent(NULL
, TX_DESCS_SIZE
, txl_descs
,
657 static int adm5120_switch_rx_ring_alloc(void)
663 rxl_descs
= dma_alloc_coherent(NULL
, RX_DESCS_SIZE
, &rxl_descs_dma
,
670 rxl_skbuff
= kzalloc(RX_SKBS_SIZE
, GFP_KERNEL
);
676 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
678 skb
= alloc_skb(SKB_ALLOC_LEN
, GFP_ATOMIC
);
684 skb_reserve(skb
, SKB_RESERVE_LEN
);
693 static void adm5120_switch_rx_ring_free(void)
698 for (i
= 0; i
< RX_RING_SIZE
; i
++)
700 kfree_skb(rxl_skbuff
[i
]);
705 dma_free_coherent(NULL
, RX_DESCS_SIZE
, rxl_descs
,
709 static void adm5120_write_mac(struct net_device
*dev
)
711 struct adm5120_if_priv
*priv
= netdev_priv(dev
);
712 unsigned char *mac
= dev
->dev_addr
;
715 t
= mac
[2] | (mac
[3] << MAC_WT1_MAC3_SHIFT
) |
716 (mac
[4] << MAC_WT1_MAC4_SHIFT
) | (mac
[5] << MAC_WT1_MAC5_SHIFT
);
717 sw_write_reg(SWITCH_REG_MAC_WT1
, t
);
719 t
= (mac
[0] << MAC_WT0_MAC0_SHIFT
) | (mac
[1] << MAC_WT0_MAC1_SHIFT
) |
720 MAC_WT0_MAWC
| MAC_WT0_WVE
| (priv
->vlan_no
<<3);
722 sw_write_reg(SWITCH_REG_MAC_WT0
, t
);
724 while (!(sw_read_reg(SWITCH_REG_MAC_WT0
) & MAC_WT0_MWD
));
727 static void adm5120_set_vlan(char *matrix
)
732 val
= matrix
[0] + (matrix
[1]<<8) + (matrix
[2]<<16) + (matrix
[3]<<24);
733 sw_write_reg(SWITCH_REG_VLAN_G1
, val
);
734 val
= matrix
[4] + (matrix
[5]<<8);
735 sw_write_reg(SWITCH_REG_VLAN_G2
, val
);
737 /* Now set/update the port vs. device lookup table */
738 for (port
=0; port
<SWITCH_NUM_PORTS
; port
++) {
739 for (vlan_port
=0; vlan_port
<SWITCH_NUM_PORTS
&& !(matrix
[vlan_port
] & (0x00000001 << port
)); vlan_port
++);
740 if (vlan_port
<SWITCH_NUM_PORTS
)
741 adm5120_port
[port
] = adm5120_devs
[vlan_port
];
743 adm5120_port
[port
] = NULL
;
747 static void adm5120_switch_set_vlan_mac(unsigned int vlan
, unsigned char *mac
)
751 t
= mac
[2] | (mac
[3] << MAC_WT1_MAC3_SHIFT
)
752 | (mac
[4] << MAC_WT1_MAC4_SHIFT
)
753 | (mac
[5] << MAC_WT1_MAC5_SHIFT
);
754 sw_write_reg(SWITCH_REG_MAC_WT1
, t
);
756 t
= (mac
[0] << MAC_WT0_MAC0_SHIFT
) | (mac
[1] << MAC_WT0_MAC1_SHIFT
) |
757 MAC_WT0_MAWC
| MAC_WT0_WVE
| (vlan
<< MAC_WT0_WVN_SHIFT
) |
758 (MAC_WT0_WAF_STATIC
<< MAC_WT0_WAF_SHIFT
);
759 sw_write_reg(SWITCH_REG_MAC_WT0
, t
);
762 t
= sw_read_reg(SWITCH_REG_MAC_WT0
);
763 } while ((t
& MAC_WT0_MWD
) == 0);
766 static void adm5120_switch_set_vlan_ports(unsigned int vlan
, u32 ports
)
772 reg
= SWITCH_REG_VLAN_G1
;
775 reg
= SWITCH_REG_VLAN_G2
;
778 t
= sw_read_reg(reg
);
779 t
&= ~(0xFF << (vlan
*8));
780 t
|= (ports
<< (vlan
*8));
781 sw_write_reg(reg
, t
);
784 /* ------------------------------------------------------------------------ */
786 #ifdef CONFIG_ADM5120_SWITCH_NAPI
787 static inline void adm5120_if_napi_enable(struct net_device
*dev
)
789 struct adm5120_if_priv
*priv
= netdev_priv(dev
);
790 napi_enable(&priv
->napi
);
793 static inline void adm5120_if_napi_disable(struct net_device
*dev
)
795 struct adm5120_if_priv
*priv
= netdev_priv(dev
);
796 napi_disable(&priv
->napi
);
799 static inline void adm5120_if_napi_enable(struct net_device
*dev
) {}
800 static inline void adm5120_if_napi_disable(struct net_device
*dev
) {}
801 #endif /* CONFIG_ADM5120_SWITCH_NAPI */
803 static int adm5120_if_open(struct net_device
*dev
)
809 adm5120_if_napi_enable(dev
);
811 err
= request_irq(dev
->irq
, adm5120_switch_irq
,
812 (IRQF_SHARED
| IRQF_DISABLED
), dev
->name
, dev
);
814 SW_ERR("unable to get irq for %s\n", dev
->name
);
819 /* enable interrupts on first open */
820 sw_int_unmask(SWITCH_INTS_USED
);
822 /* enable (additional) port */
823 t
= sw_read_reg(SWITCH_REG_PORT_CONF0
);
824 for (i
= 0; i
< SWITCH_NUM_PORTS
; i
++) {
825 if (dev
== adm5120_devs
[i
])
826 t
&= ~adm5120_eth_vlans
[i
];
828 sw_write_reg(SWITCH_REG_PORT_CONF0
, t
);
830 netif_start_queue(dev
);
835 adm5120_if_napi_disable(dev
);
839 static int adm5120_if_stop(struct net_device
*dev
)
844 netif_stop_queue(dev
);
845 adm5120_if_napi_disable(dev
);
847 /* disable port if not assigned to other devices */
848 t
= sw_read_reg(SWITCH_REG_PORT_CONF0
);
849 t
|= SWITCH_PORTS_NOCPU
;
850 for (i
= 0; i
< SWITCH_NUM_PORTS
; i
++) {
851 if ((dev
!= adm5120_devs
[i
]) && netif_running(adm5120_devs
[i
]))
852 t
&= ~adm5120_eth_vlans
[i
];
854 sw_write_reg(SWITCH_REG_PORT_CONF0
, t
);
857 sw_int_mask(SWITCH_INTS_USED
);
859 free_irq(dev
->irq
, dev
);
864 static int adm5120_if_hard_start_xmit(struct sk_buff
*skb
,
865 struct net_device
*dev
)
867 struct dma_desc
*desc
;
868 struct adm5120_if_priv
*priv
= netdev_priv(dev
);
873 /* lock switch irq */
874 spin_lock_irq(&tx_lock
);
876 /* calculate the next TX descriptor entry. */
877 entry
= cur_txl
% TX_RING_SIZE
;
879 desc
= &txl_descs
[entry
];
880 if (desc
->buf1
& DESC_OWN
) {
881 /* We want to write a packet but the TX queue is still
882 * occupied by the DMA. We are faster than the DMA... */
883 SW_DBG("%s unable to transmit, packet dopped\n", dev
->name
);
885 dev
->stats
.tx_dropped
++;
889 txl_skbuff
[entry
] = skb
;
890 data
= (desc
->buf1
& DESC_EOR
);
891 data
|= DESC_ADDR(skb
->data
);
894 ((skb
->len
<ETH_ZLEN
?ETH_ZLEN
:skb
->len
) << DESC_PKTLEN_SHIFT
) |
895 (0x1 << priv
->vlan_no
);
897 desc
->buflen
= skb
->len
< ETH_ZLEN
? ETH_ZLEN
: skb
->len
;
899 desc
->buf1
= data
| DESC_OWN
;
900 sw_write_reg(SWITCH_REG_SEND_TRIG
, SEND_TRIG_STL
);
903 if (cur_txl
== dirty_txl
+ TX_QUEUE_LEN
) {
904 for (i
= 0; i
< SWITCH_NUM_PORTS
; i
++) {
905 if (!adm5120_devs
[i
])
907 netif_stop_queue(adm5120_devs
[i
]);
911 dev
->trans_start
= jiffies
;
913 spin_unlock_irq(&tx_lock
);
918 static void adm5120_if_tx_timeout(struct net_device
*dev
)
920 SW_INFO("TX timeout on %s\n",dev
->name
);
923 static void adm5120_if_set_multicast_list(struct net_device
*dev
)
925 struct adm5120_if_priv
*priv
= netdev_priv(dev
);
929 ports
= adm5120_eth_vlans
[priv
->vlan_no
] & SWITCH_PORTS_NOCPU
;
931 t
= sw_read_reg(SWITCH_REG_CPUP_CONF
);
932 if (dev
->flags
& IFF_PROMISC
)
933 /* enable unknown packets */
934 t
&= ~(ports
<< CPUP_CONF_DUNP_SHIFT
);
936 /* disable unknown packets */
937 t
|= (ports
<< CPUP_CONF_DUNP_SHIFT
);
939 if (dev
->flags
& IFF_PROMISC
|| dev
->flags
& IFF_ALLMULTI
||
941 /* enable multicast packets */
942 t
&= ~(ports
<< CPUP_CONF_DMCP_SHIFT
);
944 /* disable multicast packets */
945 t
|= (ports
<< CPUP_CONF_DMCP_SHIFT
);
947 /* If there is any port configured to be in promiscuous mode, then the */
948 /* Bridge Test Mode has to be activated. This will result in */
949 /* transporting also packets learned in another VLAN to be forwarded */
951 /* The difficult scenario is when we want to build a bridge on the CPU.*/
952 /* Assume we have port0 and the CPU port in VLAN0 and port1 and the */
953 /* CPU port in VLAN1. Now we build a bridge on the CPU between */
954 /* VLAN0 and VLAN1. Both ports of the VLANs are set in promisc mode. */
955 /* Now assume a packet with ethernet source address 99 enters port 0 */
956 /* It will be forwarded to the CPU because it is unknown. Then the */
957 /* bridge in the CPU will send it to VLAN1 and it goes out at port 1. */
958 /* When now a packet with ethernet destination address 99 comes in at */
959 /* port 1 in VLAN1, then the switch has learned that this address is */
960 /* located at port 0 in VLAN0. Therefore the switch will drop */
961 /* this packet. In order to avoid this and to send the packet still */
962 /* to the CPU, the Bridge Test Mode has to be activated. */
964 /* Check if there is any vlan in promisc mode. */
965 if (t
& (SWITCH_PORTS_NOCPU
<< CPUP_CONF_DUNP_SHIFT
))
966 t
&= ~CPUP_CONF_BTM
; /* Disable Bridge Testing Mode */
968 t
|= CPUP_CONF_BTM
; /* Enable Bridge Testing Mode */
970 sw_write_reg(SWITCH_REG_CPUP_CONF
, t
);
974 static int adm5120_if_set_mac_address(struct net_device
*dev
, void *p
)
976 struct sockaddr
*addr
= p
;
978 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
979 adm5120_write_mac(dev
);
983 static int adm5120_if_do_ioctl(struct net_device
*dev
, struct ifreq
*rq
,
987 struct adm5120_sw_info info
;
988 struct adm5120_if_priv
*priv
= netdev_priv(dev
);
993 info
.ports
= adm5120_nrdevs
;
994 info
.vlan
= priv
->vlan_no
;
995 err
= copy_to_user(rq
->ifr_data
, &info
, sizeof(info
));
1000 if (!capable(CAP_NET_ADMIN
))
1002 err
= copy_from_user(adm5120_eth_vlans
, rq
->ifr_data
,
1003 sizeof(adm5120_eth_vlans
));
1006 adm5120_set_vlan(adm5120_eth_vlans
);
1009 err
= copy_to_user(rq
->ifr_data
, adm5120_eth_vlans
,
1010 sizeof(adm5120_eth_vlans
));
1020 static struct net_device
*adm5120_if_alloc(void)
1022 struct net_device
*dev
;
1023 struct adm5120_if_priv
*priv
;
1025 dev
= alloc_etherdev(sizeof(*priv
));
1029 priv
= netdev_priv(dev
);
1032 dev
->irq
= ADM5120_IRQ_SWITCH
;
1033 dev
->open
= adm5120_if_open
;
1034 dev
->hard_start_xmit
= adm5120_if_hard_start_xmit
;
1035 dev
->stop
= adm5120_if_stop
;
1036 dev
->set_multicast_list
= adm5120_if_set_multicast_list
;
1037 dev
->do_ioctl
= adm5120_if_do_ioctl
;
1038 dev
->tx_timeout
= adm5120_if_tx_timeout
;
1039 dev
->watchdog_timeo
= TX_TIMEOUT
;
1040 dev
->set_mac_address
= adm5120_if_set_mac_address
;
1042 #ifdef CONFIG_ADM5120_SWITCH_NAPI
1043 netif_napi_add(dev
, &priv
->napi
, adm5120_if_poll
, 64);
1049 /* ------------------------------------------------------------------------ */
1051 static void adm5120_switch_cleanup(void)
1055 /* disable interrupts */
1056 sw_int_mask(SWITCH_INTS_ALL
);
1058 for (i
= 0; i
< SWITCH_NUM_PORTS
; i
++) {
1059 struct net_device
*dev
= adm5120_devs
[i
];
1061 unregister_netdev(dev
);
1066 adm5120_switch_tx_ring_free();
1067 adm5120_switch_rx_ring_free();
1070 static int __init
adm5120_switch_probe(struct platform_device
*pdev
)
1075 adm5120_nrdevs
= adm5120_eth_num_ports
;
1077 t
= CPUP_CONF_DCPUP
| CPUP_CONF_CRCP
|
1078 SWITCH_PORTS_NOCPU
<< CPUP_CONF_DUNP_SHIFT
|
1079 SWITCH_PORTS_NOCPU
<< CPUP_CONF_DMCP_SHIFT
;
1080 sw_write_reg(SWITCH_REG_CPUP_CONF
, t
);
1082 t
= (SWITCH_PORTS_NOCPU
<< PORT_CONF0_EMCP_SHIFT
) |
1083 (SWITCH_PORTS_NOCPU
<< PORT_CONF0_BP_SHIFT
) |
1084 (SWITCH_PORTS_NOCPU
);
1085 sw_write_reg(SWITCH_REG_PORT_CONF0
, t
);
1087 /* setup ports to Autoneg/100M/Full duplex/Auto MDIX */
1088 t
= SWITCH_PORTS_PHY
|
1089 (SWITCH_PORTS_PHY
<< PHY_CNTL2_SC_SHIFT
) |
1090 (SWITCH_PORTS_PHY
<< PHY_CNTL2_DC_SHIFT
) |
1091 (SWITCH_PORTS_PHY
<< PHY_CNTL2_PHYR_SHIFT
) |
1092 (SWITCH_PORTS_PHY
<< PHY_CNTL2_AMDIX_SHIFT
) |
1094 sw_write_reg(SWITCH_REG_PHY_CNTL2
, t
);
1096 t
= sw_read_reg(SWITCH_REG_PHY_CNTL3
);
1098 sw_write_reg(SWITCH_REG_PHY_CNTL3
, t
);
1100 /* Force all the packets from all ports are low priority */
1101 sw_write_reg(SWITCH_REG_PRI_CNTL
, 0);
1103 sw_int_mask(SWITCH_INTS_ALL
);
1104 sw_int_ack(SWITCH_INTS_ALL
);
1106 err
= adm5120_switch_rx_ring_alloc();
1110 err
= adm5120_switch_tx_ring_alloc();
1114 adm5120_switch_tx_ring_reset(txl_descs
, txl_skbuff
, TX_RING_SIZE
);
1115 adm5120_switch_rx_ring_reset(rxl_descs
, rxl_skbuff
, RX_RING_SIZE
);
1117 sw_write_reg(SWITCH_REG_SHDA
, 0);
1118 sw_write_reg(SWITCH_REG_SLDA
, KSEG1ADDR(txl_descs
));
1119 sw_write_reg(SWITCH_REG_RHDA
, 0);
1120 sw_write_reg(SWITCH_REG_RLDA
, KSEG1ADDR(rxl_descs
));
1122 for (i
= 0; i
< SWITCH_NUM_PORTS
; i
++) {
1123 struct net_device
*dev
;
1124 struct adm5120_if_priv
*priv
;
1126 dev
= adm5120_if_alloc();
1132 adm5120_devs
[i
] = dev
;
1133 priv
= netdev_priv(dev
);
1136 priv
->port_mask
= adm5120_eth_vlans
[i
];
1138 memcpy(dev
->dev_addr
, adm5120_eth_macs
[i
], 6);
1139 adm5120_write_mac(dev
);
1141 err
= register_netdev(dev
);
1143 SW_INFO("%s register failed, error=%d\n",
1149 /* setup vlan/port mapping after devs are filled up */
1150 adm5120_set_vlan(adm5120_eth_vlans
);
1152 /* enable CPU port */
1153 t
= sw_read_reg(SWITCH_REG_CPUP_CONF
);
1154 t
&= ~CPUP_CONF_DCPUP
;
1155 sw_write_reg(SWITCH_REG_CPUP_CONF
, t
);
1160 adm5120_switch_cleanup();
1162 SW_ERR("init failed\n");
1166 static int adm5120_switch_remove(struct platform_device
*dev
)
1168 adm5120_switch_cleanup();
1172 static struct platform_driver adm5120_switch_driver
= {
1173 .probe
= adm5120_switch_probe
,
1174 .remove
= adm5120_switch_remove
,
1180 /* -------------------------------------------------------------------------- */
1182 static int __init
adm5120_switch_mod_init(void)
1186 pr_info(DRV_DESC
" version " DRV_VERSION
"\n");
1187 err
= platform_driver_register(&adm5120_switch_driver
);
1192 static void __exit
adm5120_switch_mod_exit(void)
1194 platform_driver_unregister(&adm5120_switch_driver
);
1197 module_init(adm5120_switch_mod_init
);
1198 module_exit(adm5120_switch_mod_exit
);
1200 MODULE_LICENSE("GPL v2");
1201 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1202 MODULE_DESCRIPTION(DRV_DESC
);
1203 MODULE_VERSION(DRV_VERSION
);