2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org>
5 * Copyright (c) 2016 Rubicon Communications, LLC (Netgate)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * TI Common Platform Ethernet Switch (CPSW) Driver
32 * Found in TI8148 "DaVinci" and AM335x "Sitara" SoCs.
34 * This controller is documented in the AM335x Technical Reference
35 * Manual, in the TMS320DM814x DaVinci Digital Video Processors TRM
36 * and in the TMS320C6452 3 Port Switch Ethernet Subsystem TRM.
38 * It is basically a single Ethernet port (port 0) wired internally to
39 * a 3-port store-and-forward switch connected to two independent
40 * "sliver" controllers (port 1 and port 2). You can operate the
41 * controller in a variety of different ways by suitably configuring
42 * the slivers and the Address Lookup Engine (ALE) that routes packets
45 * This code was developed and tested on a BeagleBone with
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
54 #include <sys/param.h>
56 #include <sys/kernel.h>
59 #include <sys/module.h>
60 #include <sys/mutex.h>
62 #include <sys/socket.h>
63 #include <sys/sockio.h>
64 #include <sys/sysctl.h>
66 #include <machine/bus.h>
67 #include <machine/resource.h>
68 #include <machine/stdarg.h>
70 #include <net/ethernet.h>
73 #include <net/if_dl.h>
74 #include <net/if_media.h>
75 #include <net/if_types.h>
77 #include <arm/ti/ti_scm.h>
78 #include <arm/ti/am335x/am335x_scm.h>
80 #include <dev/mii/mii.h>
81 #include <dev/mii/miivar.h>
83 #include <dev/ofw/ofw_bus.h>
84 #include <dev/ofw/ofw_bus_subr.h>
86 #include <dev/fdt/fdt_common.h>
88 #ifdef CPSW_ETHERSWITCH
89 #include <dev/etherswitch/etherswitch.h>
90 #include "etherswitch_if.h"
93 #include "if_cpswreg.h"
94 #include "if_cpswvar.h"
96 #include "miibus_if.h"
98 /* Device probe/attach/detach. */
99 static int cpsw_probe(device_t);
100 static int cpsw_attach(device_t);
101 static int cpsw_detach(device_t);
102 static int cpswp_probe(device_t);
103 static int cpswp_attach(device_t);
104 static int cpswp_detach(device_t);
106 static phandle_t cpsw_get_node(device_t, device_t);
108 /* Device Init/shutdown. */
109 static int cpsw_shutdown(device_t);
110 static void cpswp_init(void *);
111 static void cpswp_init_locked(void *);
112 static void cpswp_stop_locked(struct cpswp_softc *);
114 /* Device Suspend/Resume. */
115 static int cpsw_suspend(device_t);
116 static int cpsw_resume(device_t);
119 static int cpswp_ioctl(struct ifnet *, u_long command, caddr_t data);
121 static int cpswp_miibus_readreg(device_t, int phy, int reg);
122 static int cpswp_miibus_writereg(device_t, int phy, int reg, int value);
123 static void cpswp_miibus_statchg(device_t);
125 /* Send/Receive packets. */
126 static void cpsw_intr_rx(void *arg);
127 static struct mbuf *cpsw_rx_dequeue(struct cpsw_softc *);
128 static void cpsw_rx_enqueue(struct cpsw_softc *);
129 static void cpswp_start(struct ifnet *);
130 static void cpsw_intr_tx(void *);
131 static void cpswp_tx_enqueue(struct cpswp_softc *);
132 static int cpsw_tx_dequeue(struct cpsw_softc *);
134 /* Misc interrupts and watchdog. */
135 static void cpsw_intr_rx_thresh(void *);
136 static void cpsw_intr_misc(void *);
137 static void cpswp_tick(void *);
138 static void cpswp_ifmedia_sts(struct ifnet *, struct ifmediareq *);
139 static int cpswp_ifmedia_upd(struct ifnet *);
140 static void cpsw_tx_watchdog(void *);
143 static void cpsw_ale_read_entry(struct cpsw_softc *, uint16_t, uint32_t *);
144 static void cpsw_ale_write_entry(struct cpsw_softc *, uint16_t, uint32_t *);
145 static int cpsw_ale_mc_entry_set(struct cpsw_softc *, uint8_t, int, uint8_t *);
146 static void cpsw_ale_dump_table(struct cpsw_softc *);
147 static int cpsw_ale_update_vlan_table(struct cpsw_softc *, int, int, int, int,
149 static int cpswp_ale_update_addresses(struct cpswp_softc *, int);
151 /* Statistics and sysctls. */
152 static void cpsw_add_sysctls(struct cpsw_softc *);
153 static void cpsw_stats_collect(struct cpsw_softc *);
154 static int cpsw_stats_sysctl(SYSCTL_HANDLER_ARGS);
156 #ifdef CPSW_ETHERSWITCH
157 static etherswitch_info_t *cpsw_getinfo(device_t);
158 static int cpsw_getport(device_t, etherswitch_port_t *);
159 static int cpsw_setport(device_t, etherswitch_port_t *);
160 static int cpsw_getconf(device_t, etherswitch_conf_t *);
161 static int cpsw_getvgroup(device_t, etherswitch_vlangroup_t *);
162 static int cpsw_setvgroup(device_t, etherswitch_vlangroup_t *);
163 static int cpsw_readreg(device_t, int);
164 static int cpsw_writereg(device_t, int, int);
165 static int cpsw_readphy(device_t, int, int);
166 static int cpsw_writephy(device_t, int, int, int);
170 * Arbitrary limit on number of segments in an mbuf to be transmitted.
171 * Packets with more segments than this will be defragmented before
174 #define CPSW_TXFRAGS 16
176 /* Shared resources. */
177 static device_method_t cpsw_methods[] = {
178 /* Device interface */
179 DEVMETHOD(device_probe, cpsw_probe),
180 DEVMETHOD(device_attach, cpsw_attach),
181 DEVMETHOD(device_detach, cpsw_detach),
182 DEVMETHOD(device_shutdown, cpsw_shutdown),
183 DEVMETHOD(device_suspend, cpsw_suspend),
184 DEVMETHOD(device_resume, cpsw_resume),
186 DEVMETHOD(bus_add_child, device_add_child_ordered),
188 DEVMETHOD(ofw_bus_get_node, cpsw_get_node),
189 #ifdef CPSW_ETHERSWITCH
190 /* etherswitch interface */
191 DEVMETHOD(etherswitch_getinfo, cpsw_getinfo),
192 DEVMETHOD(etherswitch_readreg, cpsw_readreg),
193 DEVMETHOD(etherswitch_writereg, cpsw_writereg),
194 DEVMETHOD(etherswitch_readphyreg, cpsw_readphy),
195 DEVMETHOD(etherswitch_writephyreg, cpsw_writephy),
196 DEVMETHOD(etherswitch_getport, cpsw_getport),
197 DEVMETHOD(etherswitch_setport, cpsw_setport),
198 DEVMETHOD(etherswitch_getvgroup, cpsw_getvgroup),
199 DEVMETHOD(etherswitch_setvgroup, cpsw_setvgroup),
200 DEVMETHOD(etherswitch_getconf, cpsw_getconf),
205 static driver_t cpsw_driver = {
208 sizeof(struct cpsw_softc),
211 static devclass_t cpsw_devclass;
213 DRIVER_MODULE(cpswss, simplebus, cpsw_driver, cpsw_devclass, 0, 0);
215 /* Port/Slave resources. */
216 static device_method_t cpswp_methods[] = {
217 /* Device interface */
218 DEVMETHOD(device_probe, cpswp_probe),
219 DEVMETHOD(device_attach, cpswp_attach),
220 DEVMETHOD(device_detach, cpswp_detach),
222 DEVMETHOD(miibus_readreg, cpswp_miibus_readreg),
223 DEVMETHOD(miibus_writereg, cpswp_miibus_writereg),
224 DEVMETHOD(miibus_statchg, cpswp_miibus_statchg),
228 static driver_t cpswp_driver = {
231 sizeof(struct cpswp_softc),
234 static devclass_t cpswp_devclass;
236 #ifdef CPSW_ETHERSWITCH
237 DRIVER_MODULE(etherswitch, cpswss, etherswitch_driver, etherswitch_devclass, 0, 0);
238 MODULE_DEPEND(cpswss, etherswitch, 1, 1, 1);
241 DRIVER_MODULE(cpsw, cpswss, cpswp_driver, cpswp_devclass, 0, 0);
242 DRIVER_MODULE(miibus, cpsw, miibus_driver, miibus_devclass, 0, 0);
243 MODULE_DEPEND(cpsw, ether, 1, 1, 1);
244 MODULE_DEPEND(cpsw, miibus, 1, 1, 1);
246 #ifdef CPSW_ETHERSWITCH
247 static struct cpsw_vlangroups cpsw_vgroups[CPSW_VLANS];
250 static uint32_t slave_mdio_addr[] = { 0x4a100200, 0x4a100300 };
252 static struct resource_spec irq_res_spec[] = {
253 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE },
254 { SYS_RES_IRQ, 1, RF_ACTIVE | RF_SHAREABLE },
255 { SYS_RES_IRQ, 2, RF_ACTIVE | RF_SHAREABLE },
256 { SYS_RES_IRQ, 3, RF_ACTIVE | RF_SHAREABLE },
263 { cpsw_intr_rx_thresh },
269 /* Number of entries here must match size of stats
270 * array in struct cpswp_softc. */
271 static struct cpsw_stat {
274 } cpsw_stat_sysctls[CPSW_SYSCTL_COUNT] = {
275 {0x00, "GoodRxFrames"},
276 {0x04, "BroadcastRxFrames"},
277 {0x08, "MulticastRxFrames"},
278 {0x0C, "PauseRxFrames"},
279 {0x10, "RxCrcErrors"},
280 {0x14, "RxAlignErrors"},
281 {0x18, "OversizeRxFrames"},
283 {0x20, "ShortRxFrames"},
284 {0x24, "RxFragments"},
286 {0x34, "GoodTxFrames"},
287 {0x38, "BroadcastTxFrames"},
288 {0x3c, "MulticastTxFrames"},
289 {0x40, "PauseTxFrames"},
290 {0x44, "DeferredTxFrames"},
291 {0x48, "CollisionsTxFrames"},
292 {0x4c, "SingleCollisionTxFrames"},
293 {0x50, "MultipleCollisionTxFrames"},
294 {0x54, "ExcessiveCollisions"},
295 {0x58, "LateCollisions"},
296 {0x5c, "TxUnderrun"},
297 {0x60, "CarrierSenseErrors"},
299 {0x68, "RxTx64OctetFrames"},
300 {0x6c, "RxTx65to127OctetFrames"},
301 {0x70, "RxTx128to255OctetFrames"},
302 {0x74, "RxTx256to511OctetFrames"},
303 {0x78, "RxTx512to1024OctetFrames"},
304 {0x7c, "RxTx1024upOctetFrames"},
306 {0x84, "RxStartOfFrameOverruns"},
307 {0x88, "RxMiddleOfFrameOverruns"},
308 {0x8c, "RxDmaOverruns"}
312 * Basic debug support.
316 cpsw_debugf_head(const char *funcname)
318 int t = (int)(time_second % (24 * 60 * 60));
320 printf("%02d:%02d:%02d %s ", t / (60 * 60), (t / 60) % 60, t % 60, funcname);
324 cpsw_debugf(const char *fmt, ...)
335 #define CPSW_DEBUGF(_sc, a) do { \
336 if ((_sc)->debug) { \
337 cpsw_debugf_head(__func__); \
345 #define CPSW_TX_LOCK(sc) do { \
346 mtx_assert(&(sc)->rx.lock, MA_NOTOWNED); \
347 mtx_lock(&(sc)->tx.lock); \
350 #define CPSW_TX_UNLOCK(sc) mtx_unlock(&(sc)->tx.lock)
351 #define CPSW_TX_LOCK_ASSERT(sc) mtx_assert(&(sc)->tx.lock, MA_OWNED)
353 #define CPSW_RX_LOCK(sc) do { \
354 mtx_assert(&(sc)->tx.lock, MA_NOTOWNED); \
355 mtx_lock(&(sc)->rx.lock); \
358 #define CPSW_RX_UNLOCK(sc) mtx_unlock(&(sc)->rx.lock)
359 #define CPSW_RX_LOCK_ASSERT(sc) mtx_assert(&(sc)->rx.lock, MA_OWNED)
361 #define CPSW_PORT_LOCK(_sc) do { \
362 mtx_assert(&(_sc)->lock, MA_NOTOWNED); \
363 mtx_lock(&(_sc)->lock); \
366 #define CPSW_PORT_UNLOCK(_sc) mtx_unlock(&(_sc)->lock)
367 #define CPSW_PORT_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->lock, MA_OWNED)
372 #define cpsw_read_4(_sc, _reg) bus_read_4((_sc)->mem_res, (_reg))
373 #define cpsw_write_4(_sc, _reg, _val) \
374 bus_write_4((_sc)->mem_res, (_reg), (_val))
376 #define cpsw_cpdma_bd_offset(i) (CPSW_CPPI_RAM_OFFSET + ((i)*16))
378 #define cpsw_cpdma_bd_paddr(sc, slot) \
379 BUS_SPACE_PHYSADDR(sc->mem_res, slot->bd_offset)
380 #define cpsw_cpdma_read_bd(sc, slot, val) \
381 bus_read_region_4(sc->mem_res, slot->bd_offset, (uint32_t *) val, 4)
382 #define cpsw_cpdma_write_bd(sc, slot, val) \
383 bus_write_region_4(sc->mem_res, slot->bd_offset, (uint32_t *) val, 4)
384 #define cpsw_cpdma_write_bd_next(sc, slot, next_slot) \
385 cpsw_write_4(sc, slot->bd_offset, cpsw_cpdma_bd_paddr(sc, next_slot))
386 #define cpsw_cpdma_write_bd_flags(sc, slot, val) \
387 bus_write_2(sc->mem_res, slot->bd_offset + 14, val)
388 #define cpsw_cpdma_read_bd_flags(sc, slot) \
389 bus_read_2(sc->mem_res, slot->bd_offset + 14)
390 #define cpsw_write_hdp_slot(sc, queue, slot) \
391 cpsw_write_4(sc, (queue)->hdp_offset, cpsw_cpdma_bd_paddr(sc, slot))
392 #define CP_OFFSET (CPSW_CPDMA_TX_CP(0) - CPSW_CPDMA_TX_HDP(0))
393 #define cpsw_read_cp(sc, queue) \
394 cpsw_read_4(sc, (queue)->hdp_offset + CP_OFFSET)
395 #define cpsw_write_cp(sc, queue, val) \
396 cpsw_write_4(sc, (queue)->hdp_offset + CP_OFFSET, (val))
397 #define cpsw_write_cp_slot(sc, queue, slot) \
398 cpsw_write_cp(sc, queue, cpsw_cpdma_bd_paddr(sc, slot))
401 /* XXX temporary function versions for debugging. */
403 cpsw_write_hdp_slotX(struct cpsw_softc *sc, struct cpsw_queue *queue, struct cpsw_slot *slot)
405 uint32_t reg = queue->hdp_offset;
406 uint32_t v = cpsw_cpdma_bd_paddr(sc, slot);
407 CPSW_DEBUGF(("HDP <=== 0x%08x (was 0x%08x)", v, cpsw_read_4(sc, reg)));
408 cpsw_write_4(sc, reg, v);
412 cpsw_write_cp_slotX(struct cpsw_softc *sc, struct cpsw_queue *queue, struct cpsw_slot *slot)
414 uint32_t v = cpsw_cpdma_bd_paddr(sc, slot);
415 CPSW_DEBUGF(("CP <=== 0x%08x (expecting 0x%08x)", v, cpsw_read_cp(sc, queue)));
416 cpsw_write_cp(sc, queue, v);
421 * Expanded dump routines for verbose debugging.
424 cpsw_dump_slot(struct cpsw_softc *sc, struct cpsw_slot *slot)
426 static const char *flags[] = {"SOP", "EOP", "Owner", "EOQ",
427 "TDownCmplt", "PassCRC", "Long", "Short", "MacCtl", "Overrun",
428 "PktErr1", "PortEn/PktErr0", "RxVlanEncap", "Port2", "Port1",
430 struct cpsw_cpdma_bd bd;
434 cpsw_cpdma_read_bd(sc, slot, &bd);
435 printf("BD Addr : 0x%08x Next : 0x%08x\n",
436 cpsw_cpdma_bd_paddr(sc, slot), bd.next);
437 printf(" BufPtr: 0x%08x BufLen: 0x%08x\n", bd.bufptr, bd.buflen);
438 printf(" BufOff: 0x%08x PktLen: 0x%08x\n", bd.bufoff, bd.pktlen);
441 for (i = 0; i < 16; ++i) {
442 if (bd.flags & (1 << (15 - i))) {
443 printf("%s%s", sep, flags[i]);
449 printf(" Ether: %14D\n",
450 (char *)(slot->mbuf->m_data), " ");
451 printf(" Packet: %16D\n",
452 (char *)(slot->mbuf->m_data) + 14, " ");
456 #define CPSW_DUMP_SLOT(cs, slot) do { \
458 cpsw_dump_slot(sc, slot); \
463 cpsw_dump_queue(struct cpsw_softc *sc, struct cpsw_slots *q)
465 struct cpsw_slot *slot;
469 STAILQ_FOREACH(slot, q, next) {
470 if (i > CPSW_TXFRAGS)
473 cpsw_dump_slot(sc, slot);
477 printf(" ... and %d more.\n", others);
481 #define CPSW_DUMP_QUEUE(sc, q) do { \
483 cpsw_dump_queue(sc, q); \
488 cpsw_init_slots(struct cpsw_softc *sc)
490 struct cpsw_slot *slot;
493 STAILQ_INIT(&sc->avail);
495 /* Put the slot descriptors onto the global avail list. */
496 for (i = 0; i < nitems(sc->_slots); i++) {
497 slot = &sc->_slots[i];
498 slot->bd_offset = cpsw_cpdma_bd_offset(i);
499 STAILQ_INSERT_TAIL(&sc->avail, slot, next);
504 cpsw_add_slots(struct cpsw_softc *sc, struct cpsw_queue *queue, int requested)
506 const int max_slots = nitems(sc->_slots);
507 struct cpsw_slot *slot;
511 requested = max_slots;
513 for (i = 0; i < requested; ++i) {
514 slot = STAILQ_FIRST(&sc->avail);
517 if (bus_dmamap_create(sc->mbuf_dtag, 0, &slot->dmamap)) {
518 device_printf(sc->dev, "failed to create dmamap\n");
521 STAILQ_REMOVE_HEAD(&sc->avail, next);
522 STAILQ_INSERT_TAIL(&queue->avail, slot, next);
523 ++queue->avail_queue_len;
524 ++queue->queue_slots;
530 cpsw_free_slot(struct cpsw_softc *sc, struct cpsw_slot *slot)
536 bus_dmamap_unload(sc->mbuf_dtag, slot->dmamap);
537 error = bus_dmamap_destroy(sc->mbuf_dtag, slot->dmamap);
538 KASSERT(error == 0, ("Mapping still active"));
548 cpsw_reset(struct cpsw_softc *sc)
552 callout_stop(&sc->watchdog.callout);
554 /* Reset RMII/RGMII wrapper. */
555 cpsw_write_4(sc, CPSW_WR_SOFT_RESET, 1);
556 while (cpsw_read_4(sc, CPSW_WR_SOFT_RESET) & 1)
559 /* Disable TX and RX interrupts for all cores. */
560 for (i = 0; i < 3; ++i) {
561 cpsw_write_4(sc, CPSW_WR_C_RX_THRESH_EN(i), 0x00);
562 cpsw_write_4(sc, CPSW_WR_C_TX_EN(i), 0x00);
563 cpsw_write_4(sc, CPSW_WR_C_RX_EN(i), 0x00);
564 cpsw_write_4(sc, CPSW_WR_C_MISC_EN(i), 0x00);
567 /* Reset CPSW subsystem. */
568 cpsw_write_4(sc, CPSW_SS_SOFT_RESET, 1);
569 while (cpsw_read_4(sc, CPSW_SS_SOFT_RESET) & 1)
572 /* Reset Sliver port 1 and 2 */
573 for (i = 0; i < 2; i++) {
575 cpsw_write_4(sc, CPSW_SL_SOFT_RESET(i), 1);
576 while (cpsw_read_4(sc, CPSW_SL_SOFT_RESET(i)) & 1)
580 /* Reset DMA controller. */
581 cpsw_write_4(sc, CPSW_CPDMA_SOFT_RESET, 1);
582 while (cpsw_read_4(sc, CPSW_CPDMA_SOFT_RESET) & 1)
585 /* Disable TX & RX DMA */
586 cpsw_write_4(sc, CPSW_CPDMA_TX_CONTROL, 0);
587 cpsw_write_4(sc, CPSW_CPDMA_RX_CONTROL, 0);
589 /* Clear all queues. */
590 for (i = 0; i < 8; i++) {
591 cpsw_write_4(sc, CPSW_CPDMA_TX_HDP(i), 0);
592 cpsw_write_4(sc, CPSW_CPDMA_RX_HDP(i), 0);
593 cpsw_write_4(sc, CPSW_CPDMA_TX_CP(i), 0);
594 cpsw_write_4(sc, CPSW_CPDMA_RX_CP(i), 0);
597 /* Clear all interrupt Masks */
598 cpsw_write_4(sc, CPSW_CPDMA_RX_INTMASK_CLEAR, 0xFFFFFFFF);
599 cpsw_write_4(sc, CPSW_CPDMA_TX_INTMASK_CLEAR, 0xFFFFFFFF);
603 cpsw_init(struct cpsw_softc *sc)
605 struct cpsw_slot *slot;
608 /* Disable the interrupt pacing. */
609 reg = cpsw_read_4(sc, CPSW_WR_INT_CONTROL);
610 reg &= ~(CPSW_WR_INT_PACE_EN | CPSW_WR_INT_PRESCALE_MASK);
611 cpsw_write_4(sc, CPSW_WR_INT_CONTROL, reg);
614 cpsw_write_4(sc, CPSW_ALE_CONTROL, CPSW_ALE_CTL_CLEAR_TBL);
617 reg = CPSW_ALE_CTL_ENABLE;
619 reg |= CPSW_ALE_CTL_VLAN_AWARE;
620 cpsw_write_4(sc, CPSW_ALE_CONTROL, reg);
622 /* Set Host Port Mapping. */
623 cpsw_write_4(sc, CPSW_PORT_P0_CPDMA_TX_PRI_MAP, 0x76543210);
624 cpsw_write_4(sc, CPSW_PORT_P0_CPDMA_RX_CH_MAP, 0);
626 /* Initialize ALE: set host port to forwarding(3). */
627 cpsw_write_4(sc, CPSW_ALE_PORTCTL(0),
628 ALE_PORTCTL_INGRESS | ALE_PORTCTL_FORWARD);
630 cpsw_write_4(sc, CPSW_SS_PTYPE, 0);
632 /* Enable statistics for ports 0, 1 and 2 */
633 cpsw_write_4(sc, CPSW_SS_STAT_PORT_EN, 7);
635 /* Turn off flow control. */
636 cpsw_write_4(sc, CPSW_SS_FLOW_CONTROL, 0);
638 /* Make IP hdr aligned with 4 */
639 cpsw_write_4(sc, CPSW_CPDMA_RX_BUFFER_OFFSET, 2);
641 /* Initialize RX Buffer Descriptors */
642 cpsw_write_4(sc, CPSW_CPDMA_RX_PENDTHRESH(0), 0);
643 cpsw_write_4(sc, CPSW_CPDMA_RX_FREEBUFFER(0), 0);
645 /* Enable TX & RX DMA */
646 cpsw_write_4(sc, CPSW_CPDMA_TX_CONTROL, 1);
647 cpsw_write_4(sc, CPSW_CPDMA_RX_CONTROL, 1);
649 /* Enable Interrupts for core 0 */
650 cpsw_write_4(sc, CPSW_WR_C_RX_THRESH_EN(0), 0xFF);
651 cpsw_write_4(sc, CPSW_WR_C_RX_EN(0), 0xFF);
652 cpsw_write_4(sc, CPSW_WR_C_TX_EN(0), 0xFF);
653 cpsw_write_4(sc, CPSW_WR_C_MISC_EN(0), 0x1F);
655 /* Enable host Error Interrupt */
656 cpsw_write_4(sc, CPSW_CPDMA_DMA_INTMASK_SET, 3);
658 /* Enable interrupts for RX and TX on Channel 0 */
659 cpsw_write_4(sc, CPSW_CPDMA_RX_INTMASK_SET,
660 CPSW_CPDMA_RX_INT(0) | CPSW_CPDMA_RX_INT_THRESH(0));
661 cpsw_write_4(sc, CPSW_CPDMA_TX_INTMASK_SET, 1);
663 /* Initialze MDIO - ENABLE, PREAMBLE=0, FAULTENB, CLKDIV=0xFF */
664 /* TODO Calculate MDCLK=CLK/(CLKDIV+1) */
665 cpsw_write_4(sc, MDIOCONTROL, MDIOCTL_ENABLE | MDIOCTL_FAULTENB | 0xff);
667 /* Select MII in GMII_SEL, Internal Delay mode */
668 //ti_scm_reg_write_4(0x650, 0);
670 /* Initialize active queues. */
671 slot = STAILQ_FIRST(&sc->tx.active);
673 cpsw_write_hdp_slot(sc, &sc->tx, slot);
674 slot = STAILQ_FIRST(&sc->rx.active);
676 cpsw_write_hdp_slot(sc, &sc->rx, slot);
678 cpsw_write_4(sc, CPSW_CPDMA_RX_FREEBUFFER(0), sc->rx.active_queue_len);
679 cpsw_write_4(sc, CPSW_CPDMA_RX_PENDTHRESH(0), CPSW_TXFRAGS);
681 /* Activate network interface. */
684 sc->watchdog.timer = 0;
685 callout_init(&sc->watchdog.callout, 0);
686 callout_reset(&sc->watchdog.callout, hz, cpsw_tx_watchdog, sc);
691 * Device Probe, Attach, Detach.
696 cpsw_probe(device_t dev)
699 if (!ofw_bus_status_okay(dev))
702 if (!ofw_bus_is_compatible(dev, "ti,cpsw"))
705 device_set_desc(dev, "3-port Switch Ethernet Subsystem");
706 return (BUS_PROBE_DEFAULT);
710 cpsw_intr_attach(struct cpsw_softc *sc)
714 for (i = 0; i < CPSW_INTR_COUNT; i++) {
715 if (bus_setup_intr(sc->dev, sc->irq_res[i],
716 INTR_TYPE_NET | INTR_MPSAFE, NULL,
717 cpsw_intr_cb[i].cb, sc, &sc->ih_cookie[i]) != 0) {
726 cpsw_intr_detach(struct cpsw_softc *sc)
730 for (i = 0; i < CPSW_INTR_COUNT; i++) {
731 if (sc->ih_cookie[i]) {
732 bus_teardown_intr(sc->dev, sc->irq_res[i],
739 cpsw_get_fdt_data(struct cpsw_softc *sc, int port)
743 pcell_t phy_id[3], vlan_id;
745 unsigned long mdio_child_addr;
747 /* Find any slave with phy-handle/phy_id */
750 for (child = OF_child(sc->node); child != 0; child = OF_peer(child)) {
751 if (OF_getprop_alloc(child, "name", (void **)&name) < 0)
753 if (sscanf(name, "slave@%lx", &mdio_child_addr) != 1) {
758 if (mdio_child_addr != slave_mdio_addr[port])
761 if (fdt_get_phyaddr(child, NULL, &phy, NULL) != 0){
762 /* Users with old DTB will have phy_id instead */
764 len = OF_getproplen(child, "phy_id");
765 if (len / sizeof(pcell_t) == 2) {
766 /* Get phy address from fdt */
767 if (OF_getencprop(child, "phy_id", phy_id, len) > 0)
772 len = OF_getproplen(child, "dual_emac_res_vlan");
773 if (len / sizeof(pcell_t) == 1) {
774 /* Get phy address from fdt */
775 if (OF_getencprop(child, "dual_emac_res_vlan",
776 &vlan_id, len) > 0) {
785 sc->port[port].phy = phy;
786 sc->port[port].vlan = vlan;
792 cpsw_attach(device_t dev)
795 struct cpsw_softc *sc;
798 sc = device_get_softc(dev);
800 sc->node = ofw_bus_get_node(dev);
801 getbinuptime(&sc->attach_uptime);
803 if (OF_getencprop(sc->node, "active_slave", &sc->active_slave,
804 sizeof(sc->active_slave)) <= 0) {
805 sc->active_slave = 0;
807 if (sc->active_slave > 1)
808 sc->active_slave = 1;
810 if (OF_hasprop(sc->node, "dual_emac"))
813 for (i = 0; i < CPSW_PORTS; i++) {
814 if (!sc->dualemac && i != sc->active_slave)
816 if (cpsw_get_fdt_data(sc, i) != 0) {
818 "failed to get PHY address from FDT\n");
823 /* Initialize mutexes */
824 mtx_init(&sc->tx.lock, device_get_nameunit(dev),
825 "cpsw TX lock", MTX_DEF);
826 mtx_init(&sc->rx.lock, device_get_nameunit(dev),
827 "cpsw RX lock", MTX_DEF);
829 /* Allocate IRQ resources */
830 error = bus_alloc_resources(dev, irq_res_spec, sc->irq_res);
832 device_printf(dev, "could not allocate IRQ resources\n");
838 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
839 &sc->mem_rid, RF_ACTIVE);
840 if (sc->mem_res == NULL) {
841 device_printf(sc->dev, "failed to allocate memory resource\n");
846 reg = cpsw_read_4(sc, CPSW_SS_IDVER);
847 device_printf(dev, "CPSW SS Version %d.%d (%d)\n", (reg >> 8 & 0x7),
848 reg & 0xFF, (reg >> 11) & 0x1F);
850 cpsw_add_sysctls(sc);
852 /* Allocate a busdma tag and DMA safe memory for mbufs. */
853 error = bus_dma_tag_create(
854 bus_get_dma_tag(sc->dev), /* parent */
855 1, 0, /* alignment, boundary */
856 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
857 BUS_SPACE_MAXADDR, /* highaddr */
858 NULL, NULL, /* filtfunc, filtfuncarg */
859 MCLBYTES, CPSW_TXFRAGS, /* maxsize, nsegments */
860 MCLBYTES, 0, /* maxsegsz, flags */
861 NULL, NULL, /* lockfunc, lockfuncarg */
862 &sc->mbuf_dtag); /* dmatag */
864 device_printf(dev, "bus_dma_tag_create failed\n");
869 /* Allocate a NULL buffer for padding. */
870 sc->nullpad = malloc(ETHER_MIN_LEN, M_DEVBUF, M_WAITOK | M_ZERO);
874 /* Allocate slots to TX and RX queues. */
875 STAILQ_INIT(&sc->rx.avail);
876 STAILQ_INIT(&sc->rx.active);
877 STAILQ_INIT(&sc->tx.avail);
878 STAILQ_INIT(&sc->tx.active);
879 // For now: 128 slots to TX, rest to RX.
880 // XXX TODO: start with 32/64 and grow dynamically based on demand.
881 if (cpsw_add_slots(sc, &sc->tx, 128) ||
882 cpsw_add_slots(sc, &sc->rx, -1)) {
883 device_printf(dev, "failed to allocate dmamaps\n");
887 device_printf(dev, "Initial queue size TX=%d RX=%d\n",
888 sc->tx.queue_slots, sc->rx.queue_slots);
890 sc->tx.hdp_offset = CPSW_CPDMA_TX_HDP(0);
891 sc->rx.hdp_offset = CPSW_CPDMA_RX_HDP(0);
893 if (cpsw_intr_attach(sc) == -1) {
894 device_printf(dev, "failed to setup interrupts\n");
899 #ifdef CPSW_ETHERSWITCH
900 for (i = 0; i < CPSW_VLANS; i++)
901 cpsw_vgroups[i].vid = -1;
904 /* Reset the controller. */
908 for (i = 0; i < CPSW_PORTS; i++) {
909 if (!sc->dualemac && i != sc->active_slave)
911 sc->port[i].dev = device_add_child(dev, "cpsw", i);
912 if (sc->port[i].dev == NULL) {
917 bus_generic_probe(dev);
918 bus_generic_attach(dev);
924 cpsw_detach(device_t dev)
926 struct cpsw_softc *sc;
929 bus_generic_detach(dev);
930 sc = device_get_softc(dev);
932 for (i = 0; i < CPSW_PORTS; i++) {
934 device_delete_child(dev, sc->port[i].dev);
937 if (device_is_attached(dev)) {
938 callout_stop(&sc->watchdog.callout);
939 callout_drain(&sc->watchdog.callout);
942 /* Stop and release all interrupts */
943 cpsw_intr_detach(sc);
945 /* Free dmamaps and mbufs */
946 for (i = 0; i < nitems(sc->_slots); ++i)
947 cpsw_free_slot(sc, &sc->_slots[i]);
949 /* Free null padding buffer. */
951 free(sc->nullpad, M_DEVBUF);
955 error = bus_dma_tag_destroy(sc->mbuf_dtag);
956 KASSERT(error == 0, ("Unable to destroy DMA tag"));
959 /* Free IO memory handler */
960 if (sc->mem_res != NULL)
961 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem_res);
962 bus_release_resources(dev, irq_res_spec, sc->irq_res);
964 /* Destroy mutexes */
965 mtx_destroy(&sc->rx.lock);
966 mtx_destroy(&sc->tx.lock);
968 /* Detach the switch device, if present. */
969 error = bus_generic_detach(dev);
973 return (device_delete_children(dev));
977 cpsw_get_node(device_t bus, device_t dev)
980 /* Share controller node with port device. */
981 return (ofw_bus_get_node(bus));
985 cpswp_probe(device_t dev)
988 if (device_get_unit(dev) > 1) {
989 device_printf(dev, "Only two ports are supported.\n");
992 device_set_desc(dev, "Ethernet Switch Port");
994 return (BUS_PROBE_DEFAULT);
998 cpswp_attach(device_t dev)
1002 struct cpswp_softc *sc;
1004 uint8_t mac_addr[ETHER_ADDR_LEN];
1006 sc = device_get_softc(dev);
1008 sc->pdev = device_get_parent(dev);
1009 sc->swsc = device_get_softc(sc->pdev);
1010 sc->unit = device_get_unit(dev);
1011 sc->phy = sc->swsc->port[sc->unit].phy;
1012 sc->vlan = sc->swsc->port[sc->unit].vlan;
1013 if (sc->swsc->dualemac && sc->vlan == -1)
1014 sc->vlan = sc->unit + 1;
1016 if (sc->unit == 0) {
1017 sc->physel = MDIOUSERPHYSEL0;
1018 sc->phyaccess = MDIOUSERACCESS0;
1020 sc->physel = MDIOUSERPHYSEL1;
1021 sc->phyaccess = MDIOUSERACCESS1;
1024 mtx_init(&sc->lock, device_get_nameunit(dev), "cpsw port lock",
1027 /* Allocate network interface */
1028 ifp = sc->ifp = if_alloc(IFT_ETHER);
1034 if_initname(ifp, device_get_name(sc->dev), sc->unit);
1036 ifp->if_flags = IFF_SIMPLEX | IFF_MULTICAST | IFF_BROADCAST;
1037 ifp->if_capabilities = IFCAP_VLAN_MTU | IFCAP_HWCSUM; //FIXME VLAN?
1038 ifp->if_capenable = ifp->if_capabilities;
1040 ifp->if_init = cpswp_init;
1041 ifp->if_start = cpswp_start;
1042 ifp->if_ioctl = cpswp_ioctl;
1044 ifp->if_snd.ifq_drv_maxlen = sc->swsc->tx.queue_slots;
1045 IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
1046 IFQ_SET_READY(&ifp->if_snd);
1048 /* Get high part of MAC address from control module (mac_id[0|1]_hi) */
1049 ti_scm_reg_read_4(SCM_MAC_ID0_HI + sc->unit * 8, ®);
1050 mac_addr[0] = reg & 0xFF;
1051 mac_addr[1] = (reg >> 8) & 0xFF;
1052 mac_addr[2] = (reg >> 16) & 0xFF;
1053 mac_addr[3] = (reg >> 24) & 0xFF;
1055 /* Get low part of MAC address from control module (mac_id[0|1]_lo) */
1056 ti_scm_reg_read_4(SCM_MAC_ID0_LO + sc->unit * 8, ®);
1057 mac_addr[4] = reg & 0xFF;
1058 mac_addr[5] = (reg >> 8) & 0xFF;
1060 error = mii_attach(dev, &sc->miibus, ifp, cpswp_ifmedia_upd,
1061 cpswp_ifmedia_sts, BMSR_DEFCAPMASK, sc->phy, MII_OFFSET_ANY, 0);
1063 device_printf(dev, "attaching PHYs failed\n");
1067 sc->mii = device_get_softc(sc->miibus);
1069 /* Select PHY and enable interrupts */
1070 cpsw_write_4(sc->swsc, sc->physel,
1071 MDIO_PHYSEL_LINKINTENB | (sc->phy & 0x1F));
1073 ether_ifattach(sc->ifp, mac_addr);
1074 callout_init(&sc->mii_callout, 0);
1080 cpswp_detach(device_t dev)
1082 struct cpswp_softc *sc;
1084 sc = device_get_softc(dev);
1085 CPSW_DEBUGF(sc->swsc, (""));
1086 if (device_is_attached(dev)) {
1087 ether_ifdetach(sc->ifp);
1089 cpswp_stop_locked(sc);
1090 CPSW_PORT_UNLOCK(sc);
1091 callout_drain(&sc->mii_callout);
1094 bus_generic_detach(dev);
1097 mtx_destroy(&sc->lock);
1109 cpsw_ports_down(struct cpsw_softc *sc)
1111 struct cpswp_softc *psc;
1112 struct ifnet *ifp1, *ifp2;
1116 psc = device_get_softc(sc->port[0].dev);
1118 psc = device_get_softc(sc->port[1].dev);
1120 if ((ifp1->if_flags & IFF_UP) == 0 && (ifp2->if_flags & IFF_UP) == 0)
1127 cpswp_init(void *arg)
1129 struct cpswp_softc *sc = arg;
1131 CPSW_DEBUGF(sc->swsc, (""));
1133 cpswp_init_locked(arg);
1134 CPSW_PORT_UNLOCK(sc);
1138 cpswp_init_locked(void *arg)
1140 #ifdef CPSW_ETHERSWITCH
1143 struct cpswp_softc *sc = arg;
1147 CPSW_DEBUGF(sc->swsc, (""));
1148 CPSW_PORT_LOCK_ASSERT(sc);
1150 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1153 getbinuptime(&sc->init_uptime);
1155 if (!sc->swsc->rx.running && !sc->swsc->tx.running) {
1156 /* Reset the controller. */
1157 cpsw_reset(sc->swsc);
1158 cpsw_init(sc->swsc);
1161 /* Set Slave Mapping. */
1162 cpsw_write_4(sc->swsc, CPSW_SL_RX_PRI_MAP(sc->unit), 0x76543210);
1163 cpsw_write_4(sc->swsc, CPSW_PORT_P_TX_PRI_MAP(sc->unit + 1),
1165 cpsw_write_4(sc->swsc, CPSW_SL_RX_MAXLEN(sc->unit), 0x5f2);
1166 /* Enable MAC RX/TX modules. */
1167 /* TODO: Docs claim that IFCTL_B and IFCTL_A do the same thing? */
1168 /* Huh? Docs call bit 0 "Loopback" some places, "FullDuplex" others. */
1169 reg = cpsw_read_4(sc->swsc, CPSW_SL_MACCONTROL(sc->unit));
1170 reg |= CPSW_SL_MACTL_GMII_ENABLE;
1171 cpsw_write_4(sc->swsc, CPSW_SL_MACCONTROL(sc->unit), reg);
1173 /* Initialize ALE: set port to forwarding, initialize addrs */
1174 cpsw_write_4(sc->swsc, CPSW_ALE_PORTCTL(sc->unit + 1),
1175 ALE_PORTCTL_INGRESS | ALE_PORTCTL_FORWARD);
1176 cpswp_ale_update_addresses(sc, 1);
1178 if (sc->swsc->dualemac) {
1180 cpsw_write_4(sc->swsc, CPSW_PORT_P_VLAN(sc->unit + 1),
1182 cpsw_ale_update_vlan_table(sc->swsc, sc->vlan,
1183 (1 << (sc->unit + 1)) | (1 << 0), /* Member list */
1184 (1 << (sc->unit + 1)) | (1 << 0), /* Untagged egress */
1185 (1 << (sc->unit + 1)) | (1 << 0), 0); /* mcast reg flood */
1186 #ifdef CPSW_ETHERSWITCH
1187 for (i = 0; i < CPSW_VLANS; i++) {
1188 if (cpsw_vgroups[i].vid != -1)
1190 cpsw_vgroups[i].vid = sc->vlan;
1196 mii_mediachg(sc->mii);
1197 callout_reset(&sc->mii_callout, hz, cpswp_tick, sc);
1198 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1199 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1203 cpsw_shutdown(device_t dev)
1205 struct cpsw_softc *sc;
1206 struct cpswp_softc *psc;
1209 sc = device_get_softc(dev);
1210 CPSW_DEBUGF(sc, (""));
1211 for (i = 0; i < CPSW_PORTS; i++) {
1212 if (!sc->dualemac && i != sc->active_slave)
1214 psc = device_get_softc(sc->port[i].dev);
1215 CPSW_PORT_LOCK(psc);
1216 cpswp_stop_locked(psc);
1217 CPSW_PORT_UNLOCK(psc);
1224 cpsw_rx_teardown(struct cpsw_softc *sc)
1229 CPSW_DEBUGF(sc, ("starting RX teardown"));
1230 sc->rx.teardown = 1;
1231 cpsw_write_4(sc, CPSW_CPDMA_RX_TEARDOWN, 0);
1233 while (sc->rx.running) {
1235 device_printf(sc->dev,
1236 "Unable to cleanly shutdown receiver\n");
1241 if (!sc->rx.running)
1242 CPSW_DEBUGF(sc, ("finished RX teardown (%d retries)", i));
1246 cpsw_tx_teardown(struct cpsw_softc *sc)
1251 CPSW_DEBUGF(sc, ("starting TX teardown"));
1252 /* Start the TX queue teardown if queue is not empty. */
1253 if (STAILQ_FIRST(&sc->tx.active) != NULL)
1254 cpsw_write_4(sc, CPSW_CPDMA_TX_TEARDOWN, 0);
1256 sc->tx.teardown = 1;
1257 cpsw_tx_dequeue(sc);
1258 while (sc->tx.running && ++i < 10) {
1260 cpsw_tx_dequeue(sc);
1262 if (sc->tx.running) {
1263 device_printf(sc->dev,
1264 "Unable to cleanly shutdown transmitter\n");
1267 ("finished TX teardown (%d retries, %d idle buffers)", i,
1268 sc->tx.active_queue_len));
1273 cpswp_stop_locked(struct cpswp_softc *sc)
1279 CPSW_DEBUGF(sc->swsc, (""));
1280 CPSW_PORT_LOCK_ASSERT(sc);
1282 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1285 /* Disable interface */
1286 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1287 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1290 callout_stop(&sc->mii_callout);
1292 /* Tear down the RX/TX queues. */
1293 if (cpsw_ports_down(sc->swsc)) {
1294 cpsw_rx_teardown(sc->swsc);
1295 cpsw_tx_teardown(sc->swsc);
1298 /* Stop MAC RX/TX modules. */
1299 reg = cpsw_read_4(sc->swsc, CPSW_SL_MACCONTROL(sc->unit));
1300 reg &= ~CPSW_SL_MACTL_GMII_ENABLE;
1301 cpsw_write_4(sc->swsc, CPSW_SL_MACCONTROL(sc->unit), reg);
1303 if (cpsw_ports_down(sc->swsc)) {
1304 /* Capture stats before we reset controller. */
1305 cpsw_stats_collect(sc->swsc);
1307 cpsw_reset(sc->swsc);
1308 cpsw_init(sc->swsc);
1317 cpsw_suspend(device_t dev)
1319 struct cpsw_softc *sc;
1320 struct cpswp_softc *psc;
1323 sc = device_get_softc(dev);
1324 CPSW_DEBUGF(sc, (""));
1325 for (i = 0; i < CPSW_PORTS; i++) {
1326 if (!sc->dualemac && i != sc->active_slave)
1328 psc = device_get_softc(sc->port[i].dev);
1329 CPSW_PORT_LOCK(psc);
1330 cpswp_stop_locked(psc);
1331 CPSW_PORT_UNLOCK(psc);
1338 cpsw_resume(device_t dev)
1340 struct cpsw_softc *sc;
1342 sc = device_get_softc(dev);
1343 CPSW_DEBUGF(sc, ("UNIMPLEMENTED"));
1355 cpsw_set_promisc(struct cpswp_softc *sc, int set)
1360 * Enabling promiscuous mode requires ALE_BYPASS to be enabled.
1361 * That disables the ALE forwarding logic and causes every
1362 * packet to be sent only to the host port. In bypass mode,
1363 * the ALE processes host port transmit packets the same as in
1366 reg = cpsw_read_4(sc->swsc, CPSW_ALE_CONTROL);
1367 reg &= ~CPSW_ALE_CTL_BYPASS;
1369 reg |= CPSW_ALE_CTL_BYPASS;
1370 cpsw_write_4(sc->swsc, CPSW_ALE_CONTROL, reg);
1374 cpsw_set_allmulti(struct cpswp_softc *sc, int set)
1377 printf("All-multicast mode unimplemented\n");
1382 cpswp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1384 struct cpswp_softc *sc;
1391 ifr = (struct ifreq *)data;
1395 changed = ifp->if_capenable ^ ifr->ifr_reqcap;
1396 if (changed & IFCAP_HWCSUM) {
1397 if ((ifr->ifr_reqcap & changed) & IFCAP_HWCSUM)
1398 ifp->if_capenable |= IFCAP_HWCSUM;
1400 ifp->if_capenable &= ~IFCAP_HWCSUM;
1406 if (ifp->if_flags & IFF_UP) {
1407 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1408 changed = ifp->if_flags ^ sc->if_flags;
1409 CPSW_DEBUGF(sc->swsc,
1410 ("SIOCSIFFLAGS: UP & RUNNING (changed=0x%x)",
1412 if (changed & IFF_PROMISC)
1413 cpsw_set_promisc(sc,
1414 ifp->if_flags & IFF_PROMISC);
1415 if (changed & IFF_ALLMULTI)
1416 cpsw_set_allmulti(sc,
1417 ifp->if_flags & IFF_ALLMULTI);
1419 CPSW_DEBUGF(sc->swsc,
1420 ("SIOCSIFFLAGS: starting up"));
1421 cpswp_init_locked(sc);
1423 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1424 CPSW_DEBUGF(sc->swsc, ("SIOCSIFFLAGS: shutting down"));
1425 cpswp_stop_locked(sc);
1428 sc->if_flags = ifp->if_flags;
1429 CPSW_PORT_UNLOCK(sc);
1432 cpswp_ale_update_addresses(sc, 0);
1435 /* Ugh. DELMULTI doesn't provide the specific address
1436 being removed, so the best we can do is remove
1437 everything and rebuild it all. */
1438 cpswp_ale_update_addresses(sc, 1);
1442 error = ifmedia_ioctl(ifp, ifr, &sc->mii->mii_media, command);
1445 error = ether_ioctl(ifp, command, data);
1456 cpswp_miibus_ready(struct cpsw_softc *sc, uint32_t reg)
1458 uint32_t r, retries = CPSW_MIIBUS_RETRIES;
1461 r = cpsw_read_4(sc, reg);
1462 if ((r & MDIO_PHYACCESS_GO) == 0)
1464 DELAY(CPSW_MIIBUS_DELAY);
1471 cpswp_miibus_readreg(device_t dev, int phy, int reg)
1473 struct cpswp_softc *sc;
1476 sc = device_get_softc(dev);
1477 if (!cpswp_miibus_ready(sc->swsc, sc->phyaccess)) {
1478 device_printf(dev, "MDIO not ready to read\n");
1482 /* Set GO, reg, phy */
1483 cmd = MDIO_PHYACCESS_GO | (reg & 0x1F) << 21 | (phy & 0x1F) << 16;
1484 cpsw_write_4(sc->swsc, sc->phyaccess, cmd);
1486 if (!cpswp_miibus_ready(sc->swsc, sc->phyaccess)) {
1487 device_printf(dev, "MDIO timed out during read\n");
1491 r = cpsw_read_4(sc->swsc, sc->phyaccess);
1492 if ((r & MDIO_PHYACCESS_ACK) == 0) {
1493 device_printf(dev, "Failed to read from PHY.\n");
1496 return (r & 0xFFFF);
1500 cpswp_miibus_writereg(device_t dev, int phy, int reg, int value)
1502 struct cpswp_softc *sc;
1505 sc = device_get_softc(dev);
1506 if (!cpswp_miibus_ready(sc->swsc, sc->phyaccess)) {
1507 device_printf(dev, "MDIO not ready to write\n");
1511 /* Set GO, WRITE, reg, phy, and value */
1512 cmd = MDIO_PHYACCESS_GO | MDIO_PHYACCESS_WRITE |
1513 (reg & 0x1F) << 21 | (phy & 0x1F) << 16 | (value & 0xFFFF);
1514 cpsw_write_4(sc->swsc, sc->phyaccess, cmd);
1516 if (!cpswp_miibus_ready(sc->swsc, sc->phyaccess)) {
1517 device_printf(dev, "MDIO timed out during write\n");
1525 cpswp_miibus_statchg(device_t dev)
1527 struct cpswp_softc *sc;
1528 uint32_t mac_control, reg;
1530 sc = device_get_softc(dev);
1531 CPSW_DEBUGF(sc->swsc, (""));
1533 reg = CPSW_SL_MACCONTROL(sc->unit);
1534 mac_control = cpsw_read_4(sc->swsc, reg);
1535 mac_control &= ~(CPSW_SL_MACTL_GIG | CPSW_SL_MACTL_IFCTL_A |
1536 CPSW_SL_MACTL_IFCTL_B | CPSW_SL_MACTL_FULLDUPLEX);
1538 switch(IFM_SUBTYPE(sc->mii->mii_media_active)) {
1543 mac_control |= CPSW_SL_MACTL_GIG;
1547 mac_control |= CPSW_SL_MACTL_IFCTL_A;
1550 if (sc->mii->mii_media_active & IFM_FDX)
1551 mac_control |= CPSW_SL_MACTL_FULLDUPLEX;
1553 cpsw_write_4(sc->swsc, reg, mac_control);
1558 * Transmit/Receive Packets.
1562 cpsw_intr_rx(void *arg)
1564 struct cpsw_softc *sc;
1566 struct mbuf *received, *next;
1568 sc = (struct cpsw_softc *)arg;
1570 if (sc->rx.teardown) {
1572 sc->rx.teardown = 0;
1573 cpsw_write_cp(sc, &sc->rx, 0xfffffffc);
1575 received = cpsw_rx_dequeue(sc);
1576 cpsw_rx_enqueue(sc);
1577 cpsw_write_4(sc, CPSW_CPDMA_CPDMA_EOI_VECTOR, 1);
1580 while (received != NULL) {
1581 next = received->m_nextpkt;
1582 received->m_nextpkt = NULL;
1583 ifp = received->m_pkthdr.rcvif;
1584 (*ifp->if_input)(ifp, received);
1585 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1590 static struct mbuf *
1591 cpsw_rx_dequeue(struct cpsw_softc *sc)
1593 int nsegs, port, removed;
1594 struct cpsw_cpdma_bd bd;
1595 struct cpsw_slot *last, *slot;
1596 struct cpswp_softc *psc;
1597 struct mbuf *m, *m0, *mb_head, *mb_tail;
1607 /* Pull completed packets off hardware RX queue. */
1608 while ((slot = STAILQ_FIRST(&sc->rx.active)) != NULL) {
1609 cpsw_cpdma_read_bd(sc, slot, &bd);
1612 * Stop on packets still in use by hardware, but do not stop
1613 * on packets with the teardown complete flag, they will be
1616 if ((bd.flags & (CPDMA_BD_OWNER | CPDMA_BD_TDOWNCMPLT)) ==
1622 STAILQ_REMOVE_HEAD(&sc->rx.active, next);
1623 STAILQ_INSERT_TAIL(&sc->rx.avail, slot, next);
1625 bus_dmamap_sync(sc->mbuf_dtag, slot->dmamap, BUS_DMASYNC_POSTREAD);
1626 bus_dmamap_unload(sc->mbuf_dtag, slot->dmamap);
1631 if (bd.flags & CPDMA_BD_TDOWNCMPLT) {
1632 CPSW_DEBUGF(sc, ("RX teardown is complete"));
1635 sc->rx.teardown = 0;
1639 port = (bd.flags & CPDMA_BD_PORT_MASK) - 1;
1640 KASSERT(port >= 0 && port <= 1,
1641 ("patcket received with invalid port: %d", port));
1642 psc = device_get_softc(sc->port[port].dev);
1645 m->m_data += bd.bufoff;
1646 m->m_len = bd.buflen;
1647 if (bd.flags & CPDMA_BD_SOP) {
1648 m->m_pkthdr.len = bd.pktlen;
1649 m->m_pkthdr.rcvif = psc->ifp;
1650 m->m_flags |= M_PKTHDR;
1651 m0_flags = bd.flags;
1656 m->m_nextpkt = NULL;
1657 if (bd.flags & CPDMA_BD_EOP && m0 != NULL) {
1658 if (m0_flags & CPDMA_BD_PASS_CRC)
1659 m_adj(m0, -ETHER_CRC_LEN);
1662 if (nsegs > sc->rx.longest_chain)
1663 sc->rx.longest_chain = nsegs;
1667 if ((psc->ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1668 /* check for valid CRC by looking into pkt_err[5:4] */
1670 (CPDMA_BD_SOP | CPDMA_BD_PKT_ERR_MASK)) ==
1672 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1673 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1674 m->m_pkthdr.csum_data = 0xffff;
1678 if (STAILQ_FIRST(&sc->rx.active) != NULL &&
1679 (bd.flags & (CPDMA_BD_EOP | CPDMA_BD_EOQ)) ==
1680 (CPDMA_BD_EOP | CPDMA_BD_EOQ)) {
1681 cpsw_write_hdp_slot(sc, &sc->rx,
1682 STAILQ_FIRST(&sc->rx.active));
1683 sc->rx.queue_restart++;
1686 /* Add mbuf to packet list to be returned. */
1687 if (mb_tail != NULL && (bd.flags & CPDMA_BD_SOP)) {
1688 mb_tail->m_nextpkt = m;
1689 } else if (mb_tail != NULL) {
1690 mb_tail->m_next = m;
1691 } else if (mb_tail == NULL && (bd.flags & CPDMA_BD_SOP) == 0) {
1694 "%s: %s: discanding fragment packet w/o header\n",
1695 __func__, psc->ifp->if_xname);
1705 cpsw_write_cp_slot(sc, &sc->rx, last);
1706 sc->rx.queue_removes += removed;
1707 sc->rx.avail_queue_len += removed;
1708 sc->rx.active_queue_len -= removed;
1709 if (sc->rx.avail_queue_len > sc->rx.max_avail_queue_len)
1710 sc->rx.max_avail_queue_len = sc->rx.avail_queue_len;
1711 CPSW_DEBUGF(sc, ("Removed %d received packet(s) from RX queue", removed));
1718 cpsw_rx_enqueue(struct cpsw_softc *sc)
1720 bus_dma_segment_t seg[1];
1721 struct cpsw_cpdma_bd bd;
1722 struct cpsw_slot *first_new_slot, *last_old_slot, *next, *slot;
1723 int error, nsegs, added = 0;
1725 /* Register new mbufs with hardware. */
1726 first_new_slot = NULL;
1727 last_old_slot = STAILQ_LAST(&sc->rx.active, cpsw_slot, next);
1728 while ((slot = STAILQ_FIRST(&sc->rx.avail)) != NULL) {
1729 if (first_new_slot == NULL)
1730 first_new_slot = slot;
1731 if (slot->mbuf == NULL) {
1732 slot->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1733 if (slot->mbuf == NULL) {
1734 device_printf(sc->dev,
1735 "Unable to fill RX queue\n");
1739 slot->mbuf->m_pkthdr.len =
1740 slot->mbuf->m_ext.ext_size;
1743 error = bus_dmamap_load_mbuf_sg(sc->mbuf_dtag, slot->dmamap,
1744 slot->mbuf, seg, &nsegs, BUS_DMA_NOWAIT);
1746 KASSERT(nsegs == 1, ("More than one segment (nsegs=%d)", nsegs));
1747 KASSERT(error == 0, ("DMA error (error=%d)", error));
1748 if (error != 0 || nsegs != 1) {
1749 device_printf(sc->dev,
1750 "%s: Can't prep RX buf for DMA (nsegs=%d, error=%d)\n",
1751 __func__, nsegs, error);
1752 bus_dmamap_unload(sc->mbuf_dtag, slot->dmamap);
1753 m_freem(slot->mbuf);
1758 bus_dmamap_sync(sc->mbuf_dtag, slot->dmamap, BUS_DMASYNC_PREREAD);
1760 /* Create and submit new rx descriptor. */
1761 if ((next = STAILQ_NEXT(slot, next)) != NULL)
1762 bd.next = cpsw_cpdma_bd_paddr(sc, next);
1765 bd.bufptr = seg->ds_addr;
1767 bd.buflen = MCLBYTES - 1;
1768 bd.pktlen = bd.buflen;
1769 bd.flags = CPDMA_BD_OWNER;
1770 cpsw_cpdma_write_bd(sc, slot, &bd);
1773 STAILQ_REMOVE_HEAD(&sc->rx.avail, next);
1774 STAILQ_INSERT_TAIL(&sc->rx.active, slot, next);
1777 if (added == 0 || first_new_slot == NULL)
1780 CPSW_DEBUGF(sc, ("Adding %d buffers to RX queue", added));
1782 /* Link new entries to hardware RX queue. */
1783 if (last_old_slot == NULL) {
1784 /* Start a fresh queue. */
1785 cpsw_write_hdp_slot(sc, &sc->rx, first_new_slot);
1787 /* Add buffers to end of current queue. */
1788 cpsw_cpdma_write_bd_next(sc, last_old_slot, first_new_slot);
1790 sc->rx.queue_adds += added;
1791 sc->rx.avail_queue_len -= added;
1792 sc->rx.active_queue_len += added;
1793 cpsw_write_4(sc, CPSW_CPDMA_RX_FREEBUFFER(0), added);
1794 if (sc->rx.active_queue_len > sc->rx.max_active_queue_len)
1795 sc->rx.max_active_queue_len = sc->rx.active_queue_len;
1799 cpswp_start(struct ifnet *ifp)
1801 struct cpswp_softc *sc;
1804 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1805 sc->swsc->tx.running == 0) {
1808 CPSW_TX_LOCK(sc->swsc);
1809 cpswp_tx_enqueue(sc);
1810 cpsw_tx_dequeue(sc->swsc);
1811 CPSW_TX_UNLOCK(sc->swsc);
1815 cpsw_intr_tx(void *arg)
1817 struct cpsw_softc *sc;
1819 sc = (struct cpsw_softc *)arg;
1821 if (cpsw_read_4(sc, CPSW_CPDMA_TX_CP(0)) == 0xfffffffc)
1822 cpsw_write_cp(sc, &sc->tx, 0xfffffffc);
1823 cpsw_tx_dequeue(sc);
1824 cpsw_write_4(sc, CPSW_CPDMA_CPDMA_EOI_VECTOR, 2);
1829 cpswp_tx_enqueue(struct cpswp_softc *sc)
1831 bus_dma_segment_t segs[CPSW_TXFRAGS];
1832 struct cpsw_cpdma_bd bd;
1833 struct cpsw_slot *first_new_slot, *last, *last_old_slot, *next, *slot;
1835 int error, nsegs, seg, added = 0, padlen;
1837 /* Pull pending packets from IF queue and prep them for DMA. */
1839 first_new_slot = NULL;
1840 last_old_slot = STAILQ_LAST(&sc->swsc->tx.active, cpsw_slot, next);
1841 while ((slot = STAILQ_FIRST(&sc->swsc->tx.avail)) != NULL) {
1842 IF_DEQUEUE(&sc->ifp->if_snd, m0);
1847 padlen = ETHER_MIN_LEN - ETHER_CRC_LEN - m0->m_pkthdr.len;
1850 else if (padlen > 0)
1851 m_append(slot->mbuf, padlen, sc->swsc->nullpad);
1853 /* Create mapping in DMA memory */
1854 error = bus_dmamap_load_mbuf_sg(sc->swsc->mbuf_dtag,
1855 slot->dmamap, slot->mbuf, segs, &nsegs, BUS_DMA_NOWAIT);
1856 /* If the packet is too fragmented, try to simplify. */
1857 if (error == EFBIG ||
1858 (error == 0 && nsegs > sc->swsc->tx.avail_queue_len)) {
1859 bus_dmamap_unload(sc->swsc->mbuf_dtag, slot->dmamap);
1860 m0 = m_defrag(slot->mbuf, M_NOWAIT);
1862 device_printf(sc->dev,
1863 "Can't defragment packet; dropping\n");
1864 m_freem(slot->mbuf);
1866 CPSW_DEBUGF(sc->swsc,
1867 ("Requeueing defragmented packet"));
1868 IF_PREPEND(&sc->ifp->if_snd, m0);
1874 device_printf(sc->dev,
1875 "%s: Can't setup DMA (error=%d), dropping packet\n",
1877 bus_dmamap_unload(sc->swsc->mbuf_dtag, slot->dmamap);
1878 m_freem(slot->mbuf);
1883 bus_dmamap_sync(sc->swsc->mbuf_dtag, slot->dmamap,
1884 BUS_DMASYNC_PREWRITE);
1886 CPSW_DEBUGF(sc->swsc,
1887 ("Queueing TX packet: %d segments + %d pad bytes",
1890 if (first_new_slot == NULL)
1891 first_new_slot = slot;
1893 /* Link from the previous descriptor. */
1895 cpsw_cpdma_write_bd_next(sc->swsc, last, slot);
1897 slot->ifp = sc->ifp;
1899 /* If there is only one segment, the for() loop
1900 * gets skipped and the single buffer gets set up
1901 * as both SOP and EOP. */
1903 next = STAILQ_NEXT(slot, next);
1904 bd.next = cpsw_cpdma_bd_paddr(sc->swsc, next);
1907 /* Start by setting up the first buffer. */
1908 bd.bufptr = segs[0].ds_addr;
1910 bd.buflen = segs[0].ds_len;
1911 bd.pktlen = m_length(slot->mbuf, NULL);
1912 bd.flags = CPDMA_BD_SOP | CPDMA_BD_OWNER;
1913 if (sc->swsc->dualemac) {
1914 bd.flags |= CPDMA_BD_TO_PORT;
1915 bd.flags |= ((sc->unit + 1) & CPDMA_BD_PORT_MASK);
1917 for (seg = 1; seg < nsegs; ++seg) {
1918 /* Save the previous buffer (which isn't EOP) */
1919 cpsw_cpdma_write_bd(sc->swsc, slot, &bd);
1920 STAILQ_REMOVE_HEAD(&sc->swsc->tx.avail, next);
1921 STAILQ_INSERT_TAIL(&sc->swsc->tx.active, slot, next);
1922 slot = STAILQ_FIRST(&sc->swsc->tx.avail);
1924 /* Setup next buffer (which isn't SOP) */
1925 if (nsegs > seg + 1) {
1926 next = STAILQ_NEXT(slot, next);
1927 bd.next = cpsw_cpdma_bd_paddr(sc->swsc, next);
1930 bd.bufptr = segs[seg].ds_addr;
1932 bd.buflen = segs[seg].ds_len;
1934 bd.flags = CPDMA_BD_OWNER;
1937 /* Save the final buffer. */
1938 bd.flags |= CPDMA_BD_EOP;
1939 cpsw_cpdma_write_bd(sc->swsc, slot, &bd);
1940 STAILQ_REMOVE_HEAD(&sc->swsc->tx.avail, next);
1941 STAILQ_INSERT_TAIL(&sc->swsc->tx.active, slot, next);
1945 if (nsegs > sc->swsc->tx.longest_chain)
1946 sc->swsc->tx.longest_chain = nsegs;
1948 BPF_MTAP(sc->ifp, m0);
1951 if (first_new_slot == NULL)
1954 /* Attach the list of new buffers to the hardware TX queue. */
1955 if (last_old_slot != NULL &&
1956 (cpsw_cpdma_read_bd_flags(sc->swsc, last_old_slot) &
1957 CPDMA_BD_EOQ) == 0) {
1958 /* Add buffers to end of current queue. */
1959 cpsw_cpdma_write_bd_next(sc->swsc, last_old_slot,
1962 /* Start a fresh queue. */
1963 cpsw_write_hdp_slot(sc->swsc, &sc->swsc->tx, first_new_slot);
1965 sc->swsc->tx.queue_adds += added;
1966 sc->swsc->tx.avail_queue_len -= added;
1967 sc->swsc->tx.active_queue_len += added;
1968 if (sc->swsc->tx.active_queue_len > sc->swsc->tx.max_active_queue_len) {
1969 sc->swsc->tx.max_active_queue_len = sc->swsc->tx.active_queue_len;
1971 CPSW_DEBUGF(sc->swsc, ("Queued %d TX packet(s)", added));
1975 cpsw_tx_dequeue(struct cpsw_softc *sc)
1977 struct cpsw_slot *slot, *last_removed_slot = NULL;
1978 struct cpsw_cpdma_bd bd;
1979 uint32_t flags, removed = 0;
1981 /* Pull completed buffers off the hardware TX queue. */
1982 slot = STAILQ_FIRST(&sc->tx.active);
1983 while (slot != NULL) {
1984 flags = cpsw_cpdma_read_bd_flags(sc, slot);
1986 /* TearDown complete is only marked on the SOP for the packet. */
1987 if ((flags & (CPDMA_BD_SOP | CPDMA_BD_TDOWNCMPLT)) ==
1988 (CPDMA_BD_SOP | CPDMA_BD_TDOWNCMPLT)) {
1989 sc->tx.teardown = 1;
1992 if ((flags & (CPDMA_BD_SOP | CPDMA_BD_OWNER)) ==
1993 (CPDMA_BD_SOP | CPDMA_BD_OWNER) && sc->tx.teardown == 0)
1994 break; /* Hardware is still using this packet. */
1996 bus_dmamap_sync(sc->mbuf_dtag, slot->dmamap, BUS_DMASYNC_POSTWRITE);
1997 bus_dmamap_unload(sc->mbuf_dtag, slot->dmamap);
1998 m_freem(slot->mbuf);
2002 if (sc->tx.teardown == 0)
2003 if_inc_counter(slot->ifp, IFCOUNTER_OPACKETS, 1);
2005 if_inc_counter(slot->ifp, IFCOUNTER_OQDROPS, 1);
2008 /* Dequeue any additional buffers used by this packet. */
2009 while (slot != NULL && slot->mbuf == NULL) {
2010 STAILQ_REMOVE_HEAD(&sc->tx.active, next);
2011 STAILQ_INSERT_TAIL(&sc->tx.avail, slot, next);
2013 last_removed_slot = slot;
2014 slot = STAILQ_FIRST(&sc->tx.active);
2017 cpsw_write_cp_slot(sc, &sc->tx, last_removed_slot);
2019 /* Restart the TX queue if necessary. */
2020 cpsw_cpdma_read_bd(sc, last_removed_slot, &bd);
2021 if (slot != NULL && bd.next != 0 && (bd.flags &
2022 (CPDMA_BD_EOP | CPDMA_BD_OWNER | CPDMA_BD_EOQ)) ==
2023 (CPDMA_BD_EOP | CPDMA_BD_EOQ)) {
2024 cpsw_write_hdp_slot(sc, &sc->tx, slot);
2025 sc->tx.queue_restart++;
2031 sc->tx.queue_removes += removed;
2032 sc->tx.active_queue_len -= removed;
2033 sc->tx.avail_queue_len += removed;
2034 if (sc->tx.avail_queue_len > sc->tx.max_avail_queue_len)
2035 sc->tx.max_avail_queue_len = sc->tx.avail_queue_len;
2036 CPSW_DEBUGF(sc, ("TX removed %d completed packet(s)", removed));
2039 if (sc->tx.teardown && STAILQ_EMPTY(&sc->tx.active)) {
2040 CPSW_DEBUGF(sc, ("TX teardown is complete"));
2041 sc->tx.teardown = 0;
2050 * Miscellaneous interrupts.
2055 cpsw_intr_rx_thresh(void *arg)
2057 struct cpsw_softc *sc;
2059 struct mbuf *received, *next;
2061 sc = (struct cpsw_softc *)arg;
2063 received = cpsw_rx_dequeue(sc);
2064 cpsw_rx_enqueue(sc);
2065 cpsw_write_4(sc, CPSW_CPDMA_CPDMA_EOI_VECTOR, 0);
2068 while (received != NULL) {
2069 next = received->m_nextpkt;
2070 received->m_nextpkt = NULL;
2071 ifp = received->m_pkthdr.rcvif;
2072 (*ifp->if_input)(ifp, received);
2073 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
2079 cpsw_intr_misc_host_error(struct cpsw_softc *sc)
2083 int txerr, rxerr, txchan, rxchan;
2086 device_printf(sc->dev,
2087 "HOST ERROR: PROGRAMMING ERROR DETECTED BY HARDWARE\n");
2089 intstat = cpsw_read_4(sc, CPSW_CPDMA_DMA_INTSTAT_MASKED);
2090 device_printf(sc->dev, "CPSW_CPDMA_DMA_INTSTAT_MASKED=0x%x\n", intstat);
2091 dmastat = cpsw_read_4(sc, CPSW_CPDMA_DMASTATUS);
2092 device_printf(sc->dev, "CPSW_CPDMA_DMASTATUS=0x%x\n", dmastat);
2094 txerr = (dmastat >> 20) & 15;
2095 txchan = (dmastat >> 16) & 7;
2096 rxerr = (dmastat >> 12) & 15;
2097 rxchan = (dmastat >> 8) & 7;
2101 case 1: printf("SOP error on TX channel %d\n", txchan);
2103 case 2: printf("Ownership bit not set on SOP buffer on TX channel %d\n", txchan);
2105 case 3: printf("Zero Next Buffer but not EOP on TX channel %d\n", txchan);
2107 case 4: printf("Zero Buffer Pointer on TX channel %d\n", txchan);
2109 case 5: printf("Zero Buffer Length on TX channel %d\n", txchan);
2111 case 6: printf("Packet length error on TX channel %d\n", txchan);
2113 default: printf("Unknown error on TX channel %d\n", txchan);
2118 printf("CPSW_CPDMA_TX%d_HDP=0x%x\n",
2119 txchan, cpsw_read_4(sc, CPSW_CPDMA_TX_HDP(txchan)));
2120 printf("CPSW_CPDMA_TX%d_CP=0x%x\n",
2121 txchan, cpsw_read_4(sc, CPSW_CPDMA_TX_CP(txchan)));
2122 cpsw_dump_queue(sc, &sc->tx.active);
2127 case 2: printf("Ownership bit not set on RX channel %d\n", rxchan);
2129 case 4: printf("Zero Buffer Pointer on RX channel %d\n", rxchan);
2131 case 5: printf("Zero Buffer Length on RX channel %d\n", rxchan);
2133 case 6: printf("Buffer offset too big on RX channel %d\n", rxchan);
2135 default: printf("Unknown RX error on RX channel %d\n", rxchan);
2140 printf("CPSW_CPDMA_RX%d_HDP=0x%x\n",
2141 rxchan, cpsw_read_4(sc,CPSW_CPDMA_RX_HDP(rxchan)));
2142 printf("CPSW_CPDMA_RX%d_CP=0x%x\n",
2143 rxchan, cpsw_read_4(sc, CPSW_CPDMA_RX_CP(rxchan)));
2144 cpsw_dump_queue(sc, &sc->rx.active);
2147 printf("\nALE Table\n");
2148 cpsw_ale_dump_table(sc);
2150 // XXX do something useful here??
2151 panic("CPSW HOST ERROR INTERRUPT");
2153 // Suppress this interrupt in the future.
2154 cpsw_write_4(sc, CPSW_CPDMA_DMA_INTMASK_CLEAR, intstat);
2155 printf("XXX HOST ERROR INTERRUPT SUPPRESSED\n");
2156 // The watchdog will probably reset the controller
2157 // in a little while. It will probably fail again.
2161 cpsw_intr_misc(void *arg)
2163 struct cpsw_softc *sc = arg;
2164 uint32_t stat = cpsw_read_4(sc, CPSW_WR_C_MISC_STAT(0));
2166 if (stat & CPSW_WR_C_MISC_EVNT_PEND)
2167 CPSW_DEBUGF(sc, ("Time sync event interrupt unimplemented"));
2168 if (stat & CPSW_WR_C_MISC_STAT_PEND)
2169 cpsw_stats_collect(sc);
2170 if (stat & CPSW_WR_C_MISC_HOST_PEND)
2171 cpsw_intr_misc_host_error(sc);
2172 if (stat & CPSW_WR_C_MISC_MDIOLINK) {
2173 cpsw_write_4(sc, MDIOLINKINTMASKED,
2174 cpsw_read_4(sc, MDIOLINKINTMASKED));
2176 if (stat & CPSW_WR_C_MISC_MDIOUSER) {
2178 ("MDIO operation completed interrupt unimplemented"));
2180 cpsw_write_4(sc, CPSW_CPDMA_CPDMA_EOI_VECTOR, 3);
2185 * Periodic Checks and Watchdog.
2190 cpswp_tick(void *msc)
2192 struct cpswp_softc *sc = msc;
2194 /* Check for media type change */
2196 if (sc->media_status != sc->mii->mii_media.ifm_media) {
2197 printf("%s: media type changed (ifm_media=%x)\n", __func__,
2198 sc->mii->mii_media.ifm_media);
2199 cpswp_ifmedia_upd(sc->ifp);
2202 /* Schedule another timeout one second from now */
2203 callout_reset(&sc->mii_callout, hz, cpswp_tick, sc);
2207 cpswp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2209 struct cpswp_softc *sc;
2210 struct mii_data *mii;
2213 CPSW_DEBUGF(sc->swsc, (""));
2219 ifmr->ifm_active = mii->mii_media_active;
2220 ifmr->ifm_status = mii->mii_media_status;
2221 CPSW_PORT_UNLOCK(sc);
2225 cpswp_ifmedia_upd(struct ifnet *ifp)
2227 struct cpswp_softc *sc;
2230 CPSW_DEBUGF(sc->swsc, (""));
2232 mii_mediachg(sc->mii);
2233 sc->media_status = sc->mii->mii_media.ifm_media;
2234 CPSW_PORT_UNLOCK(sc);
2240 cpsw_tx_watchdog_full_reset(struct cpsw_softc *sc)
2242 struct cpswp_softc *psc;
2245 cpsw_debugf_head("CPSW watchdog");
2246 device_printf(sc->dev, "watchdog timeout\n");
2247 printf("CPSW_CPDMA_TX%d_HDP=0x%x\n", 0,
2248 cpsw_read_4(sc, CPSW_CPDMA_TX_HDP(0)));
2249 printf("CPSW_CPDMA_TX%d_CP=0x%x\n", 0,
2250 cpsw_read_4(sc, CPSW_CPDMA_TX_CP(0)));
2251 cpsw_dump_queue(sc, &sc->tx.active);
2252 for (i = 0; i < CPSW_PORTS; i++) {
2253 if (!sc->dualemac && i != sc->active_slave)
2255 psc = device_get_softc(sc->port[i].dev);
2256 CPSW_PORT_LOCK(psc);
2257 cpswp_stop_locked(psc);
2258 CPSW_PORT_UNLOCK(psc);
2263 cpsw_tx_watchdog(void *msc)
2265 struct cpsw_softc *sc;
2269 if (sc->tx.active_queue_len == 0 || !sc->tx.running) {
2270 sc->watchdog.timer = 0; /* Nothing to do. */
2271 } else if (sc->tx.queue_removes > sc->tx.queue_removes_at_last_tick) {
2272 sc->watchdog.timer = 0; /* Stuff done while we weren't looking. */
2273 } else if (cpsw_tx_dequeue(sc) > 0) {
2274 sc->watchdog.timer = 0; /* We just did something. */
2276 /* There was something to do but it didn't get done. */
2277 ++sc->watchdog.timer;
2278 if (sc->watchdog.timer > 5) {
2279 sc->watchdog.timer = 0;
2280 ++sc->watchdog.resets;
2281 cpsw_tx_watchdog_full_reset(sc);
2284 sc->tx.queue_removes_at_last_tick = sc->tx.queue_removes;
2287 /* Schedule another timeout one second from now */
2288 callout_reset(&sc->watchdog.callout, hz, cpsw_tx_watchdog, sc);
2293 * ALE support routines.
2298 cpsw_ale_read_entry(struct cpsw_softc *sc, uint16_t idx, uint32_t *ale_entry)
2300 cpsw_write_4(sc, CPSW_ALE_TBLCTL, idx & 1023);
2301 ale_entry[0] = cpsw_read_4(sc, CPSW_ALE_TBLW0);
2302 ale_entry[1] = cpsw_read_4(sc, CPSW_ALE_TBLW1);
2303 ale_entry[2] = cpsw_read_4(sc, CPSW_ALE_TBLW2);
2307 cpsw_ale_write_entry(struct cpsw_softc *sc, uint16_t idx, uint32_t *ale_entry)
2309 cpsw_write_4(sc, CPSW_ALE_TBLW0, ale_entry[0]);
2310 cpsw_write_4(sc, CPSW_ALE_TBLW1, ale_entry[1]);
2311 cpsw_write_4(sc, CPSW_ALE_TBLW2, ale_entry[2]);
2312 cpsw_write_4(sc, CPSW_ALE_TBLCTL, 1 << 31 | (idx & 1023));
2316 cpsw_ale_remove_all_mc_entries(struct cpsw_softc *sc)
2319 uint32_t ale_entry[3];
2321 /* First four entries are link address and broadcast. */
2322 for (i = 10; i < CPSW_MAX_ALE_ENTRIES; i++) {
2323 cpsw_ale_read_entry(sc, i, ale_entry);
2324 if ((ALE_TYPE(ale_entry) == ALE_TYPE_ADDR ||
2325 ALE_TYPE(ale_entry) == ALE_TYPE_VLAN_ADDR) &&
2326 ALE_MCAST(ale_entry) == 1) { /* MCast link addr */
2327 ale_entry[0] = ale_entry[1] = ale_entry[2] = 0;
2328 cpsw_ale_write_entry(sc, i, ale_entry);
2334 cpsw_ale_mc_entry_set(struct cpsw_softc *sc, uint8_t portmap, int vlan,
2337 int free_index = -1, matching_index = -1, i;
2338 uint32_t ale_entry[3], ale_type;
2340 /* Find a matching entry or a free entry. */
2341 for (i = 10; i < CPSW_MAX_ALE_ENTRIES; i++) {
2342 cpsw_ale_read_entry(sc, i, ale_entry);
2344 /* Entry Type[61:60] is 0 for free entry */
2345 if (free_index < 0 && ALE_TYPE(ale_entry) == 0)
2348 if ((((ale_entry[1] >> 8) & 0xFF) == mac[0]) &&
2349 (((ale_entry[1] >> 0) & 0xFF) == mac[1]) &&
2350 (((ale_entry[0] >>24) & 0xFF) == mac[2]) &&
2351 (((ale_entry[0] >>16) & 0xFF) == mac[3]) &&
2352 (((ale_entry[0] >> 8) & 0xFF) == mac[4]) &&
2353 (((ale_entry[0] >> 0) & 0xFF) == mac[5])) {
2359 if (matching_index < 0) {
2366 ale_type = ALE_TYPE_VLAN_ADDR << 28 | vlan << 16;
2368 ale_type = ALE_TYPE_ADDR << 28;
2370 /* Set MAC address */
2371 ale_entry[0] = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
2372 ale_entry[1] = mac[0] << 8 | mac[1];
2374 /* Entry type[61:60] and Mcast fwd state[63:62] is fw(3). */
2375 ale_entry[1] |= ALE_MCAST_FWD | ale_type;
2377 /* Set portmask [68:66] */
2378 ale_entry[2] = (portmap & 7) << 2;
2380 cpsw_ale_write_entry(sc, i, ale_entry);
2386 cpsw_ale_dump_table(struct cpsw_softc *sc) {
2388 uint32_t ale_entry[3];
2389 for (i = 0; i < CPSW_MAX_ALE_ENTRIES; i++) {
2390 cpsw_ale_read_entry(sc, i, ale_entry);
2391 switch (ALE_TYPE(ale_entry)) {
2393 printf("ALE[%4u] %08x %08x %08x ", i, ale_entry[2],
2394 ale_entry[1], ale_entry[0]);
2395 printf("type: %u ", ALE_TYPE(ale_entry));
2396 printf("vlan: %u ", ALE_VLAN(ale_entry));
2397 printf("untag: %u ", ALE_VLAN_UNTAG(ale_entry));
2398 printf("reg flood: %u ", ALE_VLAN_REGFLOOD(ale_entry));
2399 printf("unreg flood: %u ", ALE_VLAN_UNREGFLOOD(ale_entry));
2400 printf("members: %u ", ALE_VLAN_MEMBERS(ale_entry));
2404 case ALE_TYPE_VLAN_ADDR:
2405 printf("ALE[%4u] %08x %08x %08x ", i, ale_entry[2],
2406 ale_entry[1], ale_entry[0]);
2407 printf("type: %u ", ALE_TYPE(ale_entry));
2408 printf("mac: %02x:%02x:%02x:%02x:%02x:%02x ",
2409 (ale_entry[1] >> 8) & 0xFF,
2410 (ale_entry[1] >> 0) & 0xFF,
2411 (ale_entry[0] >>24) & 0xFF,
2412 (ale_entry[0] >>16) & 0xFF,
2413 (ale_entry[0] >> 8) & 0xFF,
2414 (ale_entry[0] >> 0) & 0xFF);
2415 printf(ALE_MCAST(ale_entry) ? "mcast " : "ucast ");
2416 if (ALE_TYPE(ale_entry) == ALE_TYPE_VLAN_ADDR)
2417 printf("vlan: %u ", ALE_VLAN(ale_entry));
2418 printf("port: %u ", ALE_PORTS(ale_entry));
2427 cpswp_ale_update_addresses(struct cpswp_softc *sc, int purge)
2430 uint32_t ale_entry[3], ale_type, portmask;
2431 struct ifmultiaddr *ifma;
2433 if (sc->swsc->dualemac) {
2434 ale_type = ALE_TYPE_VLAN_ADDR << 28 | sc->vlan << 16;
2435 portmask = 1 << (sc->unit + 1) | 1 << 0;
2437 ale_type = ALE_TYPE_ADDR << 28;
2442 * Route incoming packets for our MAC address to Port 0 (host).
2443 * For simplicity, keep this entry at table index 0 for port 1 and
2444 * at index 2 for port 2 in the ALE.
2446 if_addr_rlock(sc->ifp);
2447 mac = LLADDR((struct sockaddr_dl *)sc->ifp->if_addr->ifa_addr);
2448 ale_entry[0] = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
2449 ale_entry[1] = ale_type | mac[0] << 8 | mac[1]; /* addr entry + mac */
2450 ale_entry[2] = 0; /* port = 0 */
2451 cpsw_ale_write_entry(sc->swsc, 0 + 2 * sc->unit, ale_entry);
2453 /* Set outgoing MAC Address for slave port. */
2454 cpsw_write_4(sc->swsc, CPSW_PORT_P_SA_HI(sc->unit + 1),
2455 mac[3] << 24 | mac[2] << 16 | mac[1] << 8 | mac[0]);
2456 cpsw_write_4(sc->swsc, CPSW_PORT_P_SA_LO(sc->unit + 1),
2457 mac[5] << 8 | mac[4]);
2458 if_addr_runlock(sc->ifp);
2460 /* Keep the broadcast address at table entry 1 (or 3). */
2461 ale_entry[0] = 0xffffffff; /* Lower 32 bits of MAC */
2462 /* ALE_MCAST_FWD, Addr type, upper 16 bits of Mac */
2463 ale_entry[1] = ALE_MCAST_FWD | ale_type | 0xffff;
2464 ale_entry[2] = portmask << 2;
2465 cpsw_ale_write_entry(sc->swsc, 1 + 2 * sc->unit, ale_entry);
2467 /* SIOCDELMULTI doesn't specify the particular address
2468 being removed, so we have to remove all and rebuild. */
2470 cpsw_ale_remove_all_mc_entries(sc->swsc);
2472 /* Set other multicast addrs desired. */
2473 if_maddr_rlock(sc->ifp);
2474 CK_STAILQ_FOREACH(ifma, &sc->ifp->if_multiaddrs, ifma_link) {
2475 if (ifma->ifma_addr->sa_family != AF_LINK)
2477 cpsw_ale_mc_entry_set(sc->swsc, portmask, sc->vlan,
2478 LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
2480 if_maddr_runlock(sc->ifp);
2486 cpsw_ale_update_vlan_table(struct cpsw_softc *sc, int vlan, int ports,
2487 int untag, int mcregflood, int mcunregflood)
2489 int free_index, i, matching_index;
2490 uint32_t ale_entry[3];
2492 free_index = matching_index = -1;
2493 /* Find a matching entry or a free entry. */
2494 for (i = 5; i < CPSW_MAX_ALE_ENTRIES; i++) {
2495 cpsw_ale_read_entry(sc, i, ale_entry);
2497 /* Entry Type[61:60] is 0 for free entry */
2498 if (free_index < 0 && ALE_TYPE(ale_entry) == 0)
2501 if (ALE_VLAN(ale_entry) == vlan) {
2507 if (matching_index < 0) {
2513 ale_entry[0] = (untag & 7) << 24 | (mcregflood & 7) << 16 |
2514 (mcunregflood & 7) << 8 | (ports & 7);
2515 ale_entry[1] = ALE_TYPE_VLAN << 28 | vlan << 16;
2517 cpsw_ale_write_entry(sc, i, ale_entry);
2524 * Statistics and Sysctls.
2530 cpsw_stats_dump(struct cpsw_softc *sc)
2535 for (i = 0; i < CPSW_SYSCTL_COUNT; ++i) {
2536 r = cpsw_read_4(sc, CPSW_STATS_OFFSET +
2537 cpsw_stat_sysctls[i].reg);
2538 CPSW_DEBUGF(sc, ("%s: %ju + %u = %ju", cpsw_stat_sysctls[i].oid,
2539 (intmax_t)sc->shadow_stats[i], r,
2540 (intmax_t)sc->shadow_stats[i] + r));
2546 cpsw_stats_collect(struct cpsw_softc *sc)
2551 CPSW_DEBUGF(sc, ("Controller shadow statistics updated."));
2553 for (i = 0; i < CPSW_SYSCTL_COUNT; ++i) {
2554 r = cpsw_read_4(sc, CPSW_STATS_OFFSET +
2555 cpsw_stat_sysctls[i].reg);
2556 sc->shadow_stats[i] += r;
2557 cpsw_write_4(sc, CPSW_STATS_OFFSET + cpsw_stat_sysctls[i].reg,
2563 cpsw_stats_sysctl(SYSCTL_HANDLER_ARGS)
2565 struct cpsw_softc *sc;
2566 struct cpsw_stat *stat;
2569 sc = (struct cpsw_softc *)arg1;
2570 stat = &cpsw_stat_sysctls[oidp->oid_number];
2571 result = sc->shadow_stats[oidp->oid_number];
2572 result += cpsw_read_4(sc, CPSW_STATS_OFFSET + stat->reg);
2573 return (sysctl_handle_64(oidp, &result, 0, req));
2577 cpsw_stat_attached(SYSCTL_HANDLER_ARGS)
2579 struct cpsw_softc *sc;
2583 sc = (struct cpsw_softc *)arg1;
2585 bintime_sub(&t, &sc->attach_uptime);
2587 return (sysctl_handle_int(oidp, &result, 0, req));
2591 cpsw_intr_coalesce(SYSCTL_HANDLER_ARGS)
2594 struct cpsw_softc *sc;
2595 uint32_t ctrl, intr_per_ms;
2597 sc = (struct cpsw_softc *)arg1;
2598 error = sysctl_handle_int(oidp, &sc->coal_us, 0, req);
2599 if (error != 0 || req->newptr == NULL)
2602 ctrl = cpsw_read_4(sc, CPSW_WR_INT_CONTROL);
2603 ctrl &= ~(CPSW_WR_INT_PACE_EN | CPSW_WR_INT_PRESCALE_MASK);
2604 if (sc->coal_us == 0) {
2605 /* Disable the interrupt pace hardware. */
2606 cpsw_write_4(sc, CPSW_WR_INT_CONTROL, ctrl);
2607 cpsw_write_4(sc, CPSW_WR_C_RX_IMAX(0), 0);
2608 cpsw_write_4(sc, CPSW_WR_C_TX_IMAX(0), 0);
2612 if (sc->coal_us > CPSW_WR_C_IMAX_US_MAX)
2613 sc->coal_us = CPSW_WR_C_IMAX_US_MAX;
2614 if (sc->coal_us < CPSW_WR_C_IMAX_US_MIN)
2615 sc->coal_us = CPSW_WR_C_IMAX_US_MIN;
2616 intr_per_ms = 1000 / sc->coal_us;
2617 /* Just to make sure... */
2618 if (intr_per_ms > CPSW_WR_C_IMAX_MAX)
2619 intr_per_ms = CPSW_WR_C_IMAX_MAX;
2620 if (intr_per_ms < CPSW_WR_C_IMAX_MIN)
2621 intr_per_ms = CPSW_WR_C_IMAX_MIN;
2623 /* Set the prescale to produce 4us pulses from the 125 Mhz clock. */
2624 ctrl |= (125 * 4) & CPSW_WR_INT_PRESCALE_MASK;
2626 /* Enable the interrupt pace hardware. */
2627 cpsw_write_4(sc, CPSW_WR_C_RX_IMAX(0), intr_per_ms);
2628 cpsw_write_4(sc, CPSW_WR_C_TX_IMAX(0), intr_per_ms);
2629 ctrl |= CPSW_WR_INT_C0_RX_PULSE | CPSW_WR_INT_C0_TX_PULSE;
2630 cpsw_write_4(sc, CPSW_WR_INT_CONTROL, ctrl);
2636 cpsw_stat_uptime(SYSCTL_HANDLER_ARGS)
2638 struct cpsw_softc *swsc;
2639 struct cpswp_softc *sc;
2644 sc = device_get_softc(swsc->port[arg2].dev);
2645 if (sc->ifp->if_drv_flags & IFF_DRV_RUNNING) {
2647 bintime_sub(&t, &sc->init_uptime);
2651 return (sysctl_handle_int(oidp, &result, 0, req));
2655 cpsw_add_queue_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *node,
2656 struct cpsw_queue *queue)
2658 struct sysctl_oid_list *parent;
2660 parent = SYSCTL_CHILDREN(node);
2661 SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "totalBuffers",
2662 CTLFLAG_RD, &queue->queue_slots, 0,
2663 "Total buffers currently assigned to this queue");
2664 SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "activeBuffers",
2665 CTLFLAG_RD, &queue->active_queue_len, 0,
2666 "Buffers currently registered with hardware controller");
2667 SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "maxActiveBuffers",
2668 CTLFLAG_RD, &queue->max_active_queue_len, 0,
2669 "Max value of activeBuffers since last driver reset");
2670 SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "availBuffers",
2671 CTLFLAG_RD, &queue->avail_queue_len, 0,
2672 "Buffers allocated to this queue but not currently "
2673 "registered with hardware controller");
2674 SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "maxAvailBuffers",
2675 CTLFLAG_RD, &queue->max_avail_queue_len, 0,
2676 "Max value of availBuffers since last driver reset");
2677 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "totalEnqueued",
2678 CTLFLAG_RD, &queue->queue_adds, 0,
2679 "Total buffers added to queue");
2680 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "totalDequeued",
2681 CTLFLAG_RD, &queue->queue_removes, 0,
2682 "Total buffers removed from queue");
2683 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "queueRestart",
2684 CTLFLAG_RD, &queue->queue_restart, 0,
2685 "Total times the queue has been restarted");
2686 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "longestChain",
2687 CTLFLAG_RD, &queue->longest_chain, 0,
2688 "Max buffers used for a single packet");
2692 cpsw_add_watchdog_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *node,
2693 struct cpsw_softc *sc)
2695 struct sysctl_oid_list *parent;
2697 parent = SYSCTL_CHILDREN(node);
2698 SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "resets",
2699 CTLFLAG_RD, &sc->watchdog.resets, 0,
2700 "Total number of watchdog resets");
2704 cpsw_add_sysctls(struct cpsw_softc *sc)
2706 struct sysctl_ctx_list *ctx;
2707 struct sysctl_oid *stats_node, *queue_node, *node;
2708 struct sysctl_oid_list *parent, *stats_parent, *queue_parent;
2709 struct sysctl_oid_list *ports_parent, *port_parent;
2713 ctx = device_get_sysctl_ctx(sc->dev);
2714 parent = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
2716 SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "debug",
2717 CTLFLAG_RW, &sc->debug, 0, "Enable switch debug messages");
2719 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "attachedSecs",
2720 CTLTYPE_UINT | CTLFLAG_RD, sc, 0, cpsw_stat_attached, "IU",
2721 "Time since driver attach");
2723 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "intr_coalesce_us",
2724 CTLTYPE_UINT | CTLFLAG_RW, sc, 0, cpsw_intr_coalesce, "IU",
2725 "minimum time between interrupts");
2727 node = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "ports",
2728 CTLFLAG_RD, NULL, "CPSW Ports Statistics");
2729 ports_parent = SYSCTL_CHILDREN(node);
2730 for (i = 0; i < CPSW_PORTS; i++) {
2731 if (!sc->dualemac && i != sc->active_slave)
2735 node = SYSCTL_ADD_NODE(ctx, ports_parent, OID_AUTO,
2736 port, CTLFLAG_RD, NULL, "CPSW Port Statistics");
2737 port_parent = SYSCTL_CHILDREN(node);
2738 SYSCTL_ADD_PROC(ctx, port_parent, OID_AUTO, "uptime",
2739 CTLTYPE_UINT | CTLFLAG_RD, sc, i,
2740 cpsw_stat_uptime, "IU", "Seconds since driver init");
2743 stats_node = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats",
2744 CTLFLAG_RD, NULL, "CPSW Statistics");
2745 stats_parent = SYSCTL_CHILDREN(stats_node);
2746 for (i = 0; i < CPSW_SYSCTL_COUNT; ++i) {
2747 SYSCTL_ADD_PROC(ctx, stats_parent, i,
2748 cpsw_stat_sysctls[i].oid,
2749 CTLTYPE_U64 | CTLFLAG_RD, sc, 0,
2750 cpsw_stats_sysctl, "IU",
2751 cpsw_stat_sysctls[i].oid);
2754 queue_node = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "queue",
2755 CTLFLAG_RD, NULL, "CPSW Queue Statistics");
2756 queue_parent = SYSCTL_CHILDREN(queue_node);
2758 node = SYSCTL_ADD_NODE(ctx, queue_parent, OID_AUTO, "tx",
2759 CTLFLAG_RD, NULL, "TX Queue Statistics");
2760 cpsw_add_queue_sysctls(ctx, node, &sc->tx);
2762 node = SYSCTL_ADD_NODE(ctx, queue_parent, OID_AUTO, "rx",
2763 CTLFLAG_RD, NULL, "RX Queue Statistics");
2764 cpsw_add_queue_sysctls(ctx, node, &sc->rx);
2766 node = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "watchdog",
2767 CTLFLAG_RD, NULL, "Watchdog Statistics");
2768 cpsw_add_watchdog_sysctls(ctx, node, sc);
2771 #ifdef CPSW_ETHERSWITCH
2772 static etherswitch_info_t etherswitch_info = {
2773 .es_nports = CPSW_PORTS + 1,
2774 .es_nvlangroups = CPSW_VLANS,
2775 .es_name = "TI Common Platform Ethernet Switch (CPSW)",
2776 .es_vlan_caps = ETHERSWITCH_VLAN_DOT1Q,
2779 static etherswitch_info_t *
2780 cpsw_getinfo(device_t dev)
2782 return (ðerswitch_info);
2786 cpsw_getport(device_t dev, etherswitch_port_t *p)
2789 struct cpsw_softc *sc;
2790 struct cpswp_softc *psc;
2791 struct ifmediareq *ifmr;
2794 if (p->es_port < 0 || p->es_port > CPSW_PORTS)
2798 sc = device_get_softc(dev);
2799 if (p->es_port == CPSW_CPU_PORT) {
2800 p->es_flags |= ETHERSWITCH_PORT_CPU;
2802 ifmr->ifm_current = ifmr->ifm_active =
2803 IFM_ETHER | IFM_1000_T | IFM_FDX;
2805 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
2806 ifmr->ifm_count = 0;
2808 psc = device_get_softc(sc->port[p->es_port - 1].dev);
2809 err = ifmedia_ioctl(psc->ifp, &p->es_ifr,
2810 &psc->mii->mii_media, SIOCGIFMEDIA);
2812 reg = cpsw_read_4(sc, CPSW_PORT_P_VLAN(p->es_port));
2813 p->es_pvid = reg & ETHERSWITCH_VID_MASK;
2815 reg = cpsw_read_4(sc, CPSW_ALE_PORTCTL(p->es_port));
2816 if (reg & ALE_PORTCTL_DROP_UNTAGGED)
2817 p->es_flags |= ETHERSWITCH_PORT_DROPUNTAGGED;
2818 if (reg & ALE_PORTCTL_INGRESS)
2819 p->es_flags |= ETHERSWITCH_PORT_INGRESS;
2825 cpsw_setport(device_t dev, etherswitch_port_t *p)
2827 struct cpsw_softc *sc;
2828 struct cpswp_softc *psc;
2829 struct ifmedia *ifm;
2832 if (p->es_port < 0 || p->es_port > CPSW_PORTS)
2835 sc = device_get_softc(dev);
2836 if (p->es_pvid != 0) {
2837 cpsw_write_4(sc, CPSW_PORT_P_VLAN(p->es_port),
2838 p->es_pvid & ETHERSWITCH_VID_MASK);
2841 reg = cpsw_read_4(sc, CPSW_ALE_PORTCTL(p->es_port));
2842 if (p->es_flags & ETHERSWITCH_PORT_DROPUNTAGGED)
2843 reg |= ALE_PORTCTL_DROP_UNTAGGED;
2845 reg &= ~ALE_PORTCTL_DROP_UNTAGGED;
2846 if (p->es_flags & ETHERSWITCH_PORT_INGRESS)
2847 reg |= ALE_PORTCTL_INGRESS;
2849 reg &= ~ALE_PORTCTL_INGRESS;
2850 cpsw_write_4(sc, CPSW_ALE_PORTCTL(p->es_port), reg);
2852 /* CPU port does not allow media settings. */
2853 if (p->es_port == CPSW_CPU_PORT)
2856 psc = device_get_softc(sc->port[p->es_port - 1].dev);
2857 ifm = &psc->mii->mii_media;
2859 return (ifmedia_ioctl(psc->ifp, &p->es_ifr, ifm, SIOCSIFMEDIA));
2863 cpsw_getconf(device_t dev, etherswitch_conf_t *conf)
2866 /* Return the VLAN mode. */
2867 conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
2868 conf->vlan_mode = ETHERSWITCH_VLAN_DOT1Q;
2874 cpsw_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
2877 uint32_t ale_entry[3];
2878 struct cpsw_softc *sc;
2880 sc = device_get_softc(dev);
2882 if (vg->es_vlangroup >= CPSW_VLANS)
2886 vid = cpsw_vgroups[vg->es_vlangroup].vid;
2890 for (i = 0; i < CPSW_MAX_ALE_ENTRIES; i++) {
2891 cpsw_ale_read_entry(sc, i, ale_entry);
2892 if (ALE_TYPE(ale_entry) != ALE_TYPE_VLAN)
2894 if (vid != ALE_VLAN(ale_entry))
2898 vg->es_vid = ALE_VLAN(ale_entry) | ETHERSWITCH_VID_VALID;
2899 vg->es_member_ports = ALE_VLAN_MEMBERS(ale_entry);
2900 vg->es_untagged_ports = ALE_VLAN_UNTAG(ale_entry);
2907 cpsw_remove_vlan(struct cpsw_softc *sc, int vlan)
2910 uint32_t ale_entry[3];
2912 for (i = 0; i < CPSW_MAX_ALE_ENTRIES; i++) {
2913 cpsw_ale_read_entry(sc, i, ale_entry);
2914 if (ALE_TYPE(ale_entry) != ALE_TYPE_VLAN)
2916 if (vlan != ALE_VLAN(ale_entry))
2918 ale_entry[0] = ale_entry[1] = ale_entry[2] = 0;
2919 cpsw_ale_write_entry(sc, i, ale_entry);
2925 cpsw_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
2928 struct cpsw_softc *sc;
2930 sc = device_get_softc(dev);
2932 for (i = 0; i < CPSW_VLANS; i++) {
2933 /* Is this Vlan ID in use by another vlangroup ? */
2934 if (vg->es_vlangroup != i && cpsw_vgroups[i].vid == vg->es_vid)
2938 if (vg->es_vid == 0) {
2939 if (cpsw_vgroups[vg->es_vlangroup].vid == -1)
2941 cpsw_remove_vlan(sc, cpsw_vgroups[vg->es_vlangroup].vid);
2942 cpsw_vgroups[vg->es_vlangroup].vid = -1;
2943 vg->es_untagged_ports = 0;
2944 vg->es_member_ports = 0;
2949 vg->es_vid &= ETHERSWITCH_VID_MASK;
2950 vg->es_member_ports &= CPSW_PORTS_MASK;
2951 vg->es_untagged_ports &= CPSW_PORTS_MASK;
2953 if (cpsw_vgroups[vg->es_vlangroup].vid != -1 &&
2954 cpsw_vgroups[vg->es_vlangroup].vid != vg->es_vid)
2957 cpsw_vgroups[vg->es_vlangroup].vid = vg->es_vid;
2958 cpsw_ale_update_vlan_table(sc, vg->es_vid, vg->es_member_ports,
2959 vg->es_untagged_ports, vg->es_member_ports, 0);
2965 cpsw_readreg(device_t dev, int addr)
2968 /* Not supported. */
2973 cpsw_writereg(device_t dev, int addr, int value)
2976 /* Not supported. */
2981 cpsw_readphy(device_t dev, int phy, int reg)
2984 /* Not supported. */
2989 cpsw_writephy(device_t dev, int phy, int reg, int data)
2992 /* Not supported. */