2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (C) 2001 Eduardo Horvath.
5 * Copyright (c) 2001-2003 Thomas Moestl
6 * Copyright (c) 2007-2009 Marius Strobl <marius@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * from: NetBSD: gem.c,v 1.21 2002/06/01 23:50:58 lukem Exp
31 * from: FreeBSD: if_gem.c 182060 2008-08-23 15:03:26Z marius
34 #include <sys/cdefs.h>
36 * driver for Sun Cassini/Cassini+ and National Semiconductor DP83065
37 * Saturn Gigabit Ethernet controllers
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/callout.h>
48 #include <sys/endian.h>
50 #include <sys/malloc.h>
51 #include <sys/kernel.h>
53 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/refcount.h>
56 #include <sys/resource.h>
58 #include <sys/socket.h>
59 #include <sys/sockio.h>
60 #include <sys/taskqueue.h>
63 #include <net/ethernet.h>
65 #include <net/if_var.h>
66 #include <net/if_arp.h>
67 #include <net/if_dl.h>
68 #include <net/if_media.h>
69 #include <net/if_types.h>
70 #include <net/if_vlan_var.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/tcp.h>
76 #include <netinet/udp.h>
78 #include <machine/bus.h>
79 #if defined(__powerpc__)
80 #include <dev/ofw/ofw_bus.h>
81 #include <dev/ofw/openfirm.h>
82 #include <machine/ofw_machdep.h>
84 #include <machine/resource.h>
86 #include <dev/mii/mii.h>
87 #include <dev/mii/miivar.h>
89 #include <dev/cas/if_casreg.h>
90 #include <dev/cas/if_casvar.h>
92 #include <dev/pci/pcireg.h>
93 #include <dev/pci/pcivar.h>
95 #include "miibus_if.h"
97 #define RINGASSERT(n , min, max) \
98 CTASSERT(powerof2(n) && (n) >= (min) && (n) <= (max))
100 RINGASSERT(CAS_NRXCOMP, 128, 32768);
101 RINGASSERT(CAS_NRXDESC, 32, 8192);
102 RINGASSERT(CAS_NRXDESC2, 32, 8192);
103 RINGASSERT(CAS_NTXDESC, 32, 8192);
107 #define CCDASSERT(m, a) \
108 CTASSERT((offsetof(struct cas_control_data, m) & ((a) - 1)) == 0)
110 CCDASSERT(ccd_rxcomps, CAS_RX_COMP_ALIGN);
111 CCDASSERT(ccd_rxdescs, CAS_RX_DESC_ALIGN);
112 CCDASSERT(ccd_rxdescs2, CAS_RX_DESC_ALIGN);
116 #define CAS_TRIES 10000
119 * According to documentation, the hardware has support for basic TCP
120 * checksum offloading only, in practice this can be also used for UDP
121 * however (i.e. the problem of previous Sun NICs that a checksum of 0x0
122 * is not converted to 0xffff no longer exists).
124 #define CAS_CSUM_FEATURES (CSUM_TCP | CSUM_UDP)
126 static inline void cas_add_rxdesc(struct cas_softc *sc, u_int idx);
127 static int cas_attach(struct cas_softc *sc);
128 static int cas_bitwait(struct cas_softc *sc, bus_addr_t r, uint32_t clr,
130 static void cas_cddma_callback(void *xsc, bus_dma_segment_t *segs,
131 int nsegs, int error);
132 static void cas_detach(struct cas_softc *sc);
133 static int cas_disable_rx(struct cas_softc *sc);
134 static int cas_disable_tx(struct cas_softc *sc);
135 static void cas_eint(struct cas_softc *sc, u_int status);
136 static void cas_free(struct mbuf *m);
137 static void cas_init(void *xsc);
138 static void cas_init_locked(struct cas_softc *sc);
139 static void cas_init_regs(struct cas_softc *sc);
140 static int cas_intr(void *v);
141 static void cas_intr_task(void *arg, int pending __unused);
142 static int cas_ioctl(if_t ifp, u_long cmd, caddr_t data);
143 static int cas_load_txmbuf(struct cas_softc *sc, struct mbuf **m_head);
144 static int cas_mediachange(if_t ifp);
145 static void cas_mediastatus(if_t ifp, struct ifmediareq *ifmr);
146 static void cas_meminit(struct cas_softc *sc);
147 static void cas_mifinit(struct cas_softc *sc);
148 static int cas_mii_readreg(device_t dev, int phy, int reg);
149 static void cas_mii_statchg(device_t dev);
150 static int cas_mii_writereg(device_t dev, int phy, int reg, int val);
151 static void cas_reset(struct cas_softc *sc);
152 static int cas_reset_rx(struct cas_softc *sc);
153 static int cas_reset_tx(struct cas_softc *sc);
154 static void cas_resume(struct cas_softc *sc);
155 static u_int cas_descsize(u_int sz);
156 static void cas_rint(struct cas_softc *sc);
157 static void cas_rint_timeout(void *arg);
158 static inline void cas_rxcksum(struct mbuf *m, uint16_t cksum);
159 static inline void cas_rxcompinit(struct cas_rx_comp *rxcomp);
160 static u_int cas_rxcompsize(u_int sz);
161 static void cas_rxdma_callback(void *xsc, bus_dma_segment_t *segs,
162 int nsegs, int error);
163 static void cas_setladrf(struct cas_softc *sc);
164 static void cas_start(if_t ifp);
165 static void cas_stop(if_t ifp);
166 static void cas_suspend(struct cas_softc *sc);
167 static void cas_tick(void *arg);
168 static void cas_tint(struct cas_softc *sc);
169 static void cas_tx_task(void *arg, int pending __unused);
170 static inline void cas_txkick(struct cas_softc *sc);
171 static void cas_watchdog(struct cas_softc *sc);
173 MODULE_DEPEND(cas, ether, 1, 1, 1);
174 MODULE_DEPEND(cas, miibus, 1, 1, 1);
178 #define KTR_CAS KTR_SPARE2
182 cas_attach(struct cas_softc *sc)
184 struct cas_txsoft *txs;
189 /* Set up ifnet structure. */
190 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
193 if_setsoftc(ifp, sc);
194 if_initname(ifp, device_get_name(sc->sc_dev),
195 device_get_unit(sc->sc_dev));
196 if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
197 if_setstartfn(ifp, cas_start);
198 if_setioctlfn(ifp, cas_ioctl);
199 if_setinitfn(ifp, cas_init);
200 if_setsendqlen(ifp, CAS_TXQUEUELEN);
201 if_setsendqready(ifp);
203 callout_init_mtx(&sc->sc_tick_ch, &sc->sc_mtx, 0);
204 callout_init_mtx(&sc->sc_rx_ch, &sc->sc_mtx, 0);
205 /* Create local taskq. */
206 NET_TASK_INIT(&sc->sc_intr_task, 0, cas_intr_task, sc);
207 TASK_INIT(&sc->sc_tx_task, 1, cas_tx_task, ifp);
208 sc->sc_tq = taskqueue_create_fast("cas_taskq", M_WAITOK,
209 taskqueue_thread_enqueue, &sc->sc_tq);
210 if (sc->sc_tq == NULL) {
211 device_printf(sc->sc_dev, "could not create taskqueue\n");
215 error = taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq",
216 device_get_nameunit(sc->sc_dev));
218 device_printf(sc->sc_dev, "could not start threads\n");
222 /* Make sure the chip is stopped. */
225 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
226 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
227 BUS_SPACE_MAXSIZE, 0, BUS_SPACE_MAXSIZE, 0, NULL, NULL,
232 error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0,
233 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
234 CAS_PAGE_SIZE, 1, CAS_PAGE_SIZE, 0, NULL, NULL, &sc->sc_rdmatag);
238 error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0,
239 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
240 MCLBYTES * CAS_NTXSEGS, CAS_NTXSEGS, MCLBYTES,
241 BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_tdmatag);
245 error = bus_dma_tag_create(sc->sc_pdmatag, CAS_TX_DESC_ALIGN, 0,
246 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
247 sizeof(struct cas_control_data), 1,
248 sizeof(struct cas_control_data), 0,
249 NULL, NULL, &sc->sc_cdmatag);
254 * Allocate the control data structures, create and load the
257 if ((error = bus_dmamem_alloc(sc->sc_cdmatag,
258 (void **)&sc->sc_control_data,
259 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
260 &sc->sc_cddmamap)) != 0) {
261 device_printf(sc->sc_dev,
262 "unable to allocate control data, error = %d\n", error);
267 if ((error = bus_dmamap_load(sc->sc_cdmatag, sc->sc_cddmamap,
268 sc->sc_control_data, sizeof(struct cas_control_data),
269 cas_cddma_callback, sc, 0)) != 0 || sc->sc_cddma == 0) {
270 device_printf(sc->sc_dev,
271 "unable to load control data DMA map, error = %d\n",
277 * Initialize the transmit job descriptors.
279 STAILQ_INIT(&sc->sc_txfreeq);
280 STAILQ_INIT(&sc->sc_txdirtyq);
283 * Create the transmit buffer DMA maps.
286 for (i = 0; i < CAS_TXQUEUELEN; i++) {
287 txs = &sc->sc_txsoft[i];
288 txs->txs_mbuf = NULL;
290 if ((error = bus_dmamap_create(sc->sc_tdmatag, 0,
291 &txs->txs_dmamap)) != 0) {
292 device_printf(sc->sc_dev,
293 "unable to create TX DMA map %d, error = %d\n",
297 STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
301 * Allocate the receive buffers, create and load the DMA maps
304 for (i = 0; i < CAS_NRXDESC; i++) {
305 if ((error = bus_dmamem_alloc(sc->sc_rdmatag,
306 &sc->sc_rxdsoft[i].rxds_buf, BUS_DMA_WAITOK,
307 &sc->sc_rxdsoft[i].rxds_dmamap)) != 0) {
308 device_printf(sc->sc_dev,
309 "unable to allocate RX buffer %d, error = %d\n",
315 sc->sc_rxdsoft[i].rxds_paddr = 0;
316 if ((error = bus_dmamap_load(sc->sc_rdmatag,
317 sc->sc_rxdsoft[i].rxds_dmamap, sc->sc_rxdsoft[i].rxds_buf,
318 CAS_PAGE_SIZE, cas_rxdma_callback, sc, 0)) != 0 ||
319 sc->sc_rxdsoft[i].rxds_paddr == 0) {
320 device_printf(sc->sc_dev,
321 "unable to load RX DMA map %d, error = %d\n",
327 if ((sc->sc_flags & CAS_SERDES) == 0) {
328 CAS_WRITE_4(sc, CAS_PCS_DATAPATH, CAS_PCS_DATAPATH_MII);
329 CAS_BARRIER(sc, CAS_PCS_DATAPATH, 4,
330 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
333 * Look for an external PHY.
336 v = CAS_READ_4(sc, CAS_MIF_CONF);
337 if ((v & CAS_MIF_CONF_MDI1) != 0) {
338 v |= CAS_MIF_CONF_PHY_SELECT;
339 CAS_WRITE_4(sc, CAS_MIF_CONF, v);
340 CAS_BARRIER(sc, CAS_MIF_CONF, 4,
341 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
342 /* Enable/unfreeze the GMII pins of Saturn. */
343 if (sc->sc_variant == CAS_SATURN) {
344 CAS_WRITE_4(sc, CAS_SATURN_PCFG,
345 CAS_READ_4(sc, CAS_SATURN_PCFG) &
346 ~CAS_SATURN_PCFG_FSI);
347 CAS_BARRIER(sc, CAS_SATURN_PCFG, 4,
348 BUS_SPACE_BARRIER_READ |
349 BUS_SPACE_BARRIER_WRITE);
352 error = mii_attach(sc->sc_dev, &sc->sc_miibus, ifp,
353 cas_mediachange, cas_mediastatus, BMSR_DEFCAPMASK,
354 MII_PHY_ANY, MII_OFFSET_ANY, MIIF_DOPAUSE);
357 * Fall back on an internal PHY if no external PHY was found.
359 if (error != 0 && (v & CAS_MIF_CONF_MDI0) != 0) {
360 v &= ~CAS_MIF_CONF_PHY_SELECT;
361 CAS_WRITE_4(sc, CAS_MIF_CONF, v);
362 CAS_BARRIER(sc, CAS_MIF_CONF, 4,
363 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
364 /* Freeze the GMII pins of Saturn for saving power. */
365 if (sc->sc_variant == CAS_SATURN) {
366 CAS_WRITE_4(sc, CAS_SATURN_PCFG,
367 CAS_READ_4(sc, CAS_SATURN_PCFG) |
368 CAS_SATURN_PCFG_FSI);
369 CAS_BARRIER(sc, CAS_SATURN_PCFG, 4,
370 BUS_SPACE_BARRIER_READ |
371 BUS_SPACE_BARRIER_WRITE);
374 error = mii_attach(sc->sc_dev, &sc->sc_miibus, ifp,
375 cas_mediachange, cas_mediastatus, BMSR_DEFCAPMASK,
376 MII_PHY_ANY, MII_OFFSET_ANY, MIIF_DOPAUSE);
380 * Use the external PCS SERDES.
382 CAS_WRITE_4(sc, CAS_PCS_DATAPATH, CAS_PCS_DATAPATH_SERDES);
383 CAS_BARRIER(sc, CAS_PCS_DATAPATH, 4, BUS_SPACE_BARRIER_WRITE);
384 /* Enable/unfreeze the SERDES pins of Saturn. */
385 if (sc->sc_variant == CAS_SATURN) {
386 CAS_WRITE_4(sc, CAS_SATURN_PCFG, 0);
387 CAS_BARRIER(sc, CAS_SATURN_PCFG, 4,
388 BUS_SPACE_BARRIER_WRITE);
390 CAS_WRITE_4(sc, CAS_PCS_SERDES_CTRL, CAS_PCS_SERDES_CTRL_ESD);
391 CAS_BARRIER(sc, CAS_PCS_SERDES_CTRL, 4,
392 BUS_SPACE_BARRIER_WRITE);
393 CAS_WRITE_4(sc, CAS_PCS_CONF, CAS_PCS_CONF_EN);
394 CAS_BARRIER(sc, CAS_PCS_CONF, 4,
395 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
396 error = mii_attach(sc->sc_dev, &sc->sc_miibus, ifp,
397 cas_mediachange, cas_mediastatus, BMSR_DEFCAPMASK,
398 CAS_PHYAD_EXTERNAL, MII_OFFSET_ANY, MIIF_DOPAUSE);
401 device_printf(sc->sc_dev, "attaching PHYs failed\n");
404 sc->sc_mii = device_get_softc(sc->sc_miibus);
407 * From this point forward, the attachment cannot fail. A failure
408 * before this point releases all resources that may have been
412 /* Announce FIFO sizes. */
413 v = CAS_READ_4(sc, CAS_TX_FIFO_SIZE);
414 device_printf(sc->sc_dev, "%ukB RX FIFO, %ukB TX FIFO\n",
415 CAS_RX_FIFO_SIZE / 1024, v / 16);
417 /* Attach the interface. */
418 ether_ifattach(ifp, sc->sc_enaddr);
421 * Tell the upper layer(s) we support long frames/checksum offloads.
423 if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
424 if_setcapabilities(ifp, IFCAP_VLAN_MTU);
425 if ((sc->sc_flags & CAS_NO_CSUM) == 0) {
426 if_setcapabilitiesbit(ifp, IFCAP_HWCSUM, 0);
427 if_sethwassist(ifp, CAS_CSUM_FEATURES);
429 if_setcapenable(ifp, if_getcapabilities(ifp));
434 * Free any resources we've allocated during the failed attach
435 * attempt. Do this in reverse order and fall through.
438 for (i = 0; i < CAS_NRXDESC; i++)
439 if (sc->sc_rxdsoft[i].rxds_paddr != 0)
440 bus_dmamap_unload(sc->sc_rdmatag,
441 sc->sc_rxdsoft[i].rxds_dmamap);
443 for (i = 0; i < CAS_NRXDESC; i++)
444 if (sc->sc_rxdsoft[i].rxds_buf != NULL)
445 bus_dmamem_free(sc->sc_rdmatag,
446 sc->sc_rxdsoft[i].rxds_buf,
447 sc->sc_rxdsoft[i].rxds_dmamap);
449 for (i = 0; i < CAS_TXQUEUELEN; i++)
450 if (sc->sc_txsoft[i].txs_dmamap != NULL)
451 bus_dmamap_destroy(sc->sc_tdmatag,
452 sc->sc_txsoft[i].txs_dmamap);
453 bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap);
455 bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data,
458 bus_dma_tag_destroy(sc->sc_cdmatag);
460 bus_dma_tag_destroy(sc->sc_tdmatag);
462 bus_dma_tag_destroy(sc->sc_rdmatag);
464 bus_dma_tag_destroy(sc->sc_pdmatag);
466 taskqueue_free(sc->sc_tq);
473 cas_detach(struct cas_softc *sc)
475 if_t ifp = sc->sc_ifp;
482 callout_drain(&sc->sc_tick_ch);
483 callout_drain(&sc->sc_rx_ch);
484 taskqueue_drain(sc->sc_tq, &sc->sc_intr_task);
485 taskqueue_drain(sc->sc_tq, &sc->sc_tx_task);
487 taskqueue_free(sc->sc_tq);
488 device_delete_child(sc->sc_dev, sc->sc_miibus);
490 for (i = 0; i < CAS_NRXDESC; i++)
491 if (sc->sc_rxdsoft[i].rxds_dmamap != NULL)
492 bus_dmamap_sync(sc->sc_rdmatag,
493 sc->sc_rxdsoft[i].rxds_dmamap,
494 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
495 for (i = 0; i < CAS_NRXDESC; i++)
496 if (sc->sc_rxdsoft[i].rxds_paddr != 0)
497 bus_dmamap_unload(sc->sc_rdmatag,
498 sc->sc_rxdsoft[i].rxds_dmamap);
499 for (i = 0; i < CAS_NRXDESC; i++)
500 if (sc->sc_rxdsoft[i].rxds_buf != NULL)
501 bus_dmamem_free(sc->sc_rdmatag,
502 sc->sc_rxdsoft[i].rxds_buf,
503 sc->sc_rxdsoft[i].rxds_dmamap);
504 for (i = 0; i < CAS_TXQUEUELEN; i++)
505 if (sc->sc_txsoft[i].txs_dmamap != NULL)
506 bus_dmamap_destroy(sc->sc_tdmatag,
507 sc->sc_txsoft[i].txs_dmamap);
508 CAS_CDSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
509 bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap);
510 bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data,
512 bus_dma_tag_destroy(sc->sc_cdmatag);
513 bus_dma_tag_destroy(sc->sc_tdmatag);
514 bus_dma_tag_destroy(sc->sc_rdmatag);
515 bus_dma_tag_destroy(sc->sc_pdmatag);
519 cas_suspend(struct cas_softc *sc)
521 if_t ifp = sc->sc_ifp;
529 cas_resume(struct cas_softc *sc)
531 if_t ifp = sc->sc_ifp;
535 * On resume all registers have to be initialized again like
538 sc->sc_flags &= ~CAS_INITED;
539 if (if_getflags(ifp) & IFF_UP)
545 cas_rxcksum(struct mbuf *m, uint16_t cksum)
547 struct ether_header *eh;
551 int32_t hlen, len, pktlen;
554 pktlen = m->m_pkthdr.len;
555 if (pktlen < sizeof(struct ether_header) + sizeof(struct ip))
557 eh = mtod(m, struct ether_header *);
558 if (eh->ether_type != htons(ETHERTYPE_IP))
560 ip = (struct ip *)(eh + 1);
561 if (ip->ip_v != IPVERSION)
564 hlen = ip->ip_hl << 2;
565 pktlen -= sizeof(struct ether_header);
566 if (hlen < sizeof(struct ip))
568 if (ntohs(ip->ip_len) < hlen)
570 if (ntohs(ip->ip_len) != pktlen)
572 if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
573 return; /* Cannot handle fragmented packet. */
577 if (pktlen < (hlen + sizeof(struct tcphdr)))
581 if (pktlen < (hlen + sizeof(struct udphdr)))
583 uh = (struct udphdr *)((uint8_t *)ip + hlen);
585 return; /* no checksum */
592 /* checksum fixup for IP options */
593 len = hlen - sizeof(struct ip);
595 opts = (uint16_t *)(ip + 1);
596 for (; len > 0; len -= sizeof(uint16_t), opts++) {
597 temp32 = cksum - *opts;
598 temp32 = (temp32 >> 16) + (temp32 & 65535);
599 cksum = temp32 & 65535;
602 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
603 m->m_pkthdr.csum_data = cksum;
607 cas_cddma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
609 struct cas_softc *sc = xsc;
614 panic("%s: bad control buffer segment count", __func__);
615 sc->sc_cddma = segs[0].ds_addr;
619 cas_rxdma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
621 struct cas_softc *sc = xsc;
626 panic("%s: bad RX buffer segment count", __func__);
627 sc->sc_rxdsoft[sc->sc_rxdptr].rxds_paddr = segs[0].ds_addr;
633 struct cas_softc *sc = arg;
634 if_t ifp = sc->sc_ifp;
637 CAS_LOCK_ASSERT(sc, MA_OWNED);
640 * Unload collision and error counters.
642 if_inc_counter(ifp, IFCOUNTER_COLLISIONS,
643 CAS_READ_4(sc, CAS_MAC_NORM_COLL_CNT) +
644 CAS_READ_4(sc, CAS_MAC_FIRST_COLL_CNT));
645 v = CAS_READ_4(sc, CAS_MAC_EXCESS_COLL_CNT) +
646 CAS_READ_4(sc, CAS_MAC_LATE_COLL_CNT);
647 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, v);
648 if_inc_counter(ifp, IFCOUNTER_OERRORS, v);
649 if_inc_counter(ifp, IFCOUNTER_IERRORS,
650 CAS_READ_4(sc, CAS_MAC_RX_LEN_ERR_CNT) +
651 CAS_READ_4(sc, CAS_MAC_RX_ALIGN_ERR) +
652 CAS_READ_4(sc, CAS_MAC_RX_CRC_ERR_CNT) +
653 CAS_READ_4(sc, CAS_MAC_RX_CODE_VIOL));
656 * Then clear the hardware counters.
658 CAS_WRITE_4(sc, CAS_MAC_NORM_COLL_CNT, 0);
659 CAS_WRITE_4(sc, CAS_MAC_FIRST_COLL_CNT, 0);
660 CAS_WRITE_4(sc, CAS_MAC_EXCESS_COLL_CNT, 0);
661 CAS_WRITE_4(sc, CAS_MAC_LATE_COLL_CNT, 0);
662 CAS_WRITE_4(sc, CAS_MAC_RX_LEN_ERR_CNT, 0);
663 CAS_WRITE_4(sc, CAS_MAC_RX_ALIGN_ERR, 0);
664 CAS_WRITE_4(sc, CAS_MAC_RX_CRC_ERR_CNT, 0);
665 CAS_WRITE_4(sc, CAS_MAC_RX_CODE_VIOL, 0);
667 mii_tick(sc->sc_mii);
669 if (sc->sc_txfree != CAS_MAXTXFREE)
674 callout_reset(&sc->sc_tick_ch, hz, cas_tick, sc);
678 cas_bitwait(struct cas_softc *sc, bus_addr_t r, uint32_t clr, uint32_t set)
683 for (i = CAS_TRIES; i--; DELAY(100)) {
684 reg = CAS_READ_4(sc, r);
685 if ((reg & clr) == 0 && (reg & set) == set)
692 cas_reset(struct cas_softc *sc)
696 CTR2(KTR_CAS, "%s: %s", device_get_name(sc->sc_dev), __func__);
698 /* Disable all interrupts in order to avoid spurious ones. */
699 CAS_WRITE_4(sc, CAS_INTMASK, 0xffffffff);
705 * Do a full reset modulo the result of the last auto-negotiation
706 * when using the SERDES.
708 CAS_WRITE_4(sc, CAS_RESET, CAS_RESET_RX | CAS_RESET_TX |
709 ((sc->sc_flags & CAS_SERDES) != 0 ? CAS_RESET_PCS_DIS : 0));
710 CAS_BARRIER(sc, CAS_RESET, 4,
711 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
713 if (!cas_bitwait(sc, CAS_RESET, CAS_RESET_RX | CAS_RESET_TX, 0))
714 device_printf(sc->sc_dev, "cannot reset device\n");
720 struct cas_softc *sc = if_getsoftc(ifp);
721 struct cas_txsoft *txs;
724 CTR2(KTR_CAS, "%s: %s", device_get_name(sc->sc_dev), __func__);
727 callout_stop(&sc->sc_tick_ch);
728 callout_stop(&sc->sc_rx_ch);
730 /* Disable all interrupts in order to avoid spurious ones. */
731 CAS_WRITE_4(sc, CAS_INTMASK, 0xffffffff);
737 * Release any queued transmit buffers.
739 while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
740 STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
741 if (txs->txs_ndescs != 0) {
742 bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap,
743 BUS_DMASYNC_POSTWRITE);
744 bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap);
745 if (txs->txs_mbuf != NULL) {
746 m_freem(txs->txs_mbuf);
747 txs->txs_mbuf = NULL;
750 STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
754 * Mark the interface down and cancel the watchdog timer.
756 if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
757 sc->sc_flags &= ~CAS_LINK;
758 sc->sc_wdog_timer = 0;
762 cas_reset_rx(struct cas_softc *sc)
766 * Resetting while DMA is in progress can cause a bus hang, so we
769 (void)cas_disable_rx(sc);
770 CAS_WRITE_4(sc, CAS_RX_CONF, 0);
771 CAS_BARRIER(sc, CAS_RX_CONF, 4,
772 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
773 if (!cas_bitwait(sc, CAS_RX_CONF, CAS_RX_CONF_RXDMA_EN, 0))
774 device_printf(sc->sc_dev, "cannot disable RX DMA\n");
776 /* Finally, reset the ERX. */
777 CAS_WRITE_4(sc, CAS_RESET, CAS_RESET_RX |
778 ((sc->sc_flags & CAS_SERDES) != 0 ? CAS_RESET_PCS_DIS : 0));
779 CAS_BARRIER(sc, CAS_RESET, 4,
780 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
781 if (!cas_bitwait(sc, CAS_RESET, CAS_RESET_RX, 0)) {
782 device_printf(sc->sc_dev, "cannot reset receiver\n");
789 cas_reset_tx(struct cas_softc *sc)
793 * Resetting while DMA is in progress can cause a bus hang, so we
796 (void)cas_disable_tx(sc);
797 CAS_WRITE_4(sc, CAS_TX_CONF, 0);
798 CAS_BARRIER(sc, CAS_TX_CONF, 4,
799 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
800 if (!cas_bitwait(sc, CAS_TX_CONF, CAS_TX_CONF_TXDMA_EN, 0))
801 device_printf(sc->sc_dev, "cannot disable TX DMA\n");
803 /* Finally, reset the ETX. */
804 CAS_WRITE_4(sc, CAS_RESET, CAS_RESET_TX |
805 ((sc->sc_flags & CAS_SERDES) != 0 ? CAS_RESET_PCS_DIS : 0));
806 CAS_BARRIER(sc, CAS_RESET, 4,
807 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
808 if (!cas_bitwait(sc, CAS_RESET, CAS_RESET_TX, 0)) {
809 device_printf(sc->sc_dev, "cannot reset transmitter\n");
816 cas_disable_rx(struct cas_softc *sc)
819 CAS_WRITE_4(sc, CAS_MAC_RX_CONF,
820 CAS_READ_4(sc, CAS_MAC_RX_CONF) & ~CAS_MAC_RX_CONF_EN);
821 CAS_BARRIER(sc, CAS_MAC_RX_CONF, 4,
822 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
823 if (cas_bitwait(sc, CAS_MAC_RX_CONF, CAS_MAC_RX_CONF_EN, 0))
826 device_printf(sc->sc_dev, "cannot disable RX MAC\n");
831 cas_disable_tx(struct cas_softc *sc)
834 CAS_WRITE_4(sc, CAS_MAC_TX_CONF,
835 CAS_READ_4(sc, CAS_MAC_TX_CONF) & ~CAS_MAC_TX_CONF_EN);
836 CAS_BARRIER(sc, CAS_MAC_TX_CONF, 4,
837 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
838 if (cas_bitwait(sc, CAS_MAC_TX_CONF, CAS_MAC_TX_CONF_EN, 0))
841 device_printf(sc->sc_dev, "cannot disable TX MAC\n");
846 cas_rxcompinit(struct cas_rx_comp *rxcomp)
849 rxcomp->crc_word1 = 0;
850 rxcomp->crc_word2 = 0;
852 htole64(CAS_SET(ETHER_HDR_LEN + sizeof(struct ip), CAS_RC3_CSO));
853 rxcomp->crc_word4 = htole64(CAS_RC4_ZERO);
857 cas_meminit(struct cas_softc *sc)
861 CAS_LOCK_ASSERT(sc, MA_OWNED);
864 * Initialize the transmit descriptor ring.
866 for (i = 0; i < CAS_NTXDESC; i++) {
867 sc->sc_txdescs[i].cd_flags = 0;
868 sc->sc_txdescs[i].cd_buf_ptr = 0;
870 sc->sc_txfree = CAS_MAXTXFREE;
875 * Initialize the receive completion ring.
877 for (i = 0; i < CAS_NRXCOMP; i++)
878 cas_rxcompinit(&sc->sc_rxcomps[i]);
882 * Initialize the first receive descriptor ring. We leave
883 * the second one zeroed as we don't actually use it.
885 for (i = 0; i < CAS_NRXDESC; i++)
886 CAS_INIT_RXDESC(sc, i, i);
889 CAS_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
893 cas_descsize(u_int sz)
898 return (CAS_DESC_32);
900 return (CAS_DESC_64);
902 return (CAS_DESC_128);
904 return (CAS_DESC_256);
906 return (CAS_DESC_512);
908 return (CAS_DESC_1K);
910 return (CAS_DESC_2K);
912 return (CAS_DESC_4K);
914 return (CAS_DESC_8K);
916 printf("%s: invalid descriptor ring size %d\n", __func__, sz);
917 return (CAS_DESC_32);
922 cas_rxcompsize(u_int sz)
927 return (CAS_RX_CONF_COMP_128);
929 return (CAS_RX_CONF_COMP_256);
931 return (CAS_RX_CONF_COMP_512);
933 return (CAS_RX_CONF_COMP_1K);
935 return (CAS_RX_CONF_COMP_2K);
937 return (CAS_RX_CONF_COMP_4K);
939 return (CAS_RX_CONF_COMP_8K);
941 return (CAS_RX_CONF_COMP_16K);
943 return (CAS_RX_CONF_COMP_32K);
945 printf("%s: invalid dcompletion ring size %d\n", __func__, sz);
946 return (CAS_RX_CONF_COMP_128);
953 struct cas_softc *sc = xsc;
961 * Initialization of interface; set up initialization block
962 * and transmit/receive descriptor rings.
965 cas_init_locked(struct cas_softc *sc)
967 if_t ifp = sc->sc_ifp;
970 CAS_LOCK_ASSERT(sc, MA_OWNED);
972 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
976 CTR2(KTR_CAS, "%s: %s: calling stop", device_get_name(sc->sc_dev),
980 * Initialization sequence. The numbered steps below correspond
981 * to the sequence outlined in section 6.3.5.1 in the Ethernet
982 * Channel Engine manual (part of the PCIO manual).
983 * See also the STP2002-STQ document from Sun Microsystems.
986 /* step 1 & 2. Reset the Ethernet Channel. */
990 CTR2(KTR_CAS, "%s: %s: restarting", device_get_name(sc->sc_dev),
994 if ((sc->sc_flags & CAS_SERDES) == 0)
995 /* Re-initialize the MIF. */
998 /* step 3. Setup data structures in host memory. */
1001 /* step 4. TX MAC registers & counters */
1004 /* step 5. RX MAC registers & counters */
1006 /* step 6 & 7. Program Ring Base Addresses. */
1007 CAS_WRITE_4(sc, CAS_TX_DESC3_BASE_HI,
1008 (((uint64_t)CAS_CDTXDADDR(sc, 0)) >> 32));
1009 CAS_WRITE_4(sc, CAS_TX_DESC3_BASE_LO,
1010 CAS_CDTXDADDR(sc, 0) & 0xffffffff);
1012 CAS_WRITE_4(sc, CAS_RX_COMP_BASE_HI,
1013 (((uint64_t)CAS_CDRXCADDR(sc, 0)) >> 32));
1014 CAS_WRITE_4(sc, CAS_RX_COMP_BASE_LO,
1015 CAS_CDRXCADDR(sc, 0) & 0xffffffff);
1017 CAS_WRITE_4(sc, CAS_RX_DESC_BASE_HI,
1018 (((uint64_t)CAS_CDRXDADDR(sc, 0)) >> 32));
1019 CAS_WRITE_4(sc, CAS_RX_DESC_BASE_LO,
1020 CAS_CDRXDADDR(sc, 0) & 0xffffffff);
1022 if ((sc->sc_flags & CAS_REG_PLUS) != 0) {
1023 CAS_WRITE_4(sc, CAS_RX_DESC2_BASE_HI,
1024 (((uint64_t)CAS_CDRXD2ADDR(sc, 0)) >> 32));
1025 CAS_WRITE_4(sc, CAS_RX_DESC2_BASE_LO,
1026 CAS_CDRXD2ADDR(sc, 0) & 0xffffffff);
1031 "loading TXDR %lx, RXCR %lx, RXDR %lx, RXD2R %lx, cddma %lx",
1032 CAS_CDTXDADDR(sc, 0), CAS_CDRXCADDR(sc, 0), CAS_CDRXDADDR(sc, 0),
1033 CAS_CDRXD2ADDR(sc, 0), sc->sc_cddma);
1036 /* step 8. Global Configuration & Interrupt Masks */
1038 /* Disable weighted round robin. */
1039 CAS_WRITE_4(sc, CAS_CAW, CAS_CAW_RR_DIS);
1042 * Enable infinite bursts for revisions without PCI issues if
1043 * applicable. Doing so greatly improves the TX performance.
1045 CAS_WRITE_4(sc, CAS_INF_BURST,
1046 (sc->sc_flags & CAS_TABORT) == 0 ? CAS_INF_BURST_EN :
1049 /* Set up interrupts. */
1050 CAS_WRITE_4(sc, CAS_INTMASK,
1051 ~(CAS_INTR_TX_INT_ME | CAS_INTR_TX_TAG_ERR |
1052 CAS_INTR_RX_DONE | CAS_INTR_RX_BUF_NA | CAS_INTR_RX_TAG_ERR |
1053 CAS_INTR_RX_COMP_FULL | CAS_INTR_RX_BUF_AEMPTY |
1054 CAS_INTR_RX_COMP_AFULL | CAS_INTR_RX_LEN_MMATCH |
1055 CAS_INTR_PCI_ERROR_INT
1057 | CAS_INTR_PCS_INT | CAS_INTR_MIF
1060 /* Don't clear top level interrupts when CAS_STATUS_ALIAS is read. */
1061 CAS_WRITE_4(sc, CAS_CLEAR_ALIAS, 0);
1062 CAS_WRITE_4(sc, CAS_MAC_RX_MASK, ~CAS_MAC_RX_OVERFLOW);
1063 CAS_WRITE_4(sc, CAS_MAC_TX_MASK,
1064 ~(CAS_MAC_TX_UNDERRUN | CAS_MAC_TX_MAX_PKT_ERR));
1066 CAS_WRITE_4(sc, CAS_MAC_CTRL_MASK,
1067 ~(CAS_MAC_CTRL_PAUSE_RCVD | CAS_MAC_CTRL_PAUSE |
1068 CAS_MAC_CTRL_NON_PAUSE));
1070 CAS_WRITE_4(sc, CAS_MAC_CTRL_MASK,
1071 CAS_MAC_CTRL_PAUSE_RCVD | CAS_MAC_CTRL_PAUSE |
1072 CAS_MAC_CTRL_NON_PAUSE);
1075 /* Enable PCI error interrupts. */
1076 CAS_WRITE_4(sc, CAS_ERROR_MASK,
1077 ~(CAS_ERROR_DTRTO | CAS_ERROR_OTHER | CAS_ERROR_DMAW_ZERO |
1078 CAS_ERROR_DMAR_ZERO | CAS_ERROR_RTRTO));
1080 /* Enable PCI error interrupts in BIM configuration. */
1081 CAS_WRITE_4(sc, CAS_BIM_CONF,
1082 CAS_BIM_CONF_DPAR_EN | CAS_BIM_CONF_RMA_EN | CAS_BIM_CONF_RTA_EN);
1085 * step 9. ETX Configuration: encode receive descriptor ring size,
1086 * enable DMA and disable pre-interrupt writeback completion.
1088 v = cas_descsize(CAS_NTXDESC) << CAS_TX_CONF_DESC3_SHFT;
1089 CAS_WRITE_4(sc, CAS_TX_CONF, v | CAS_TX_CONF_TXDMA_EN |
1090 CAS_TX_CONF_RDPP_DIS | CAS_TX_CONF_PICWB_DIS);
1092 /* step 10. ERX Configuration */
1095 * Encode receive completion and descriptor ring sizes, set the
1098 v = cas_rxcompsize(CAS_NRXCOMP) << CAS_RX_CONF_COMP_SHFT;
1099 v |= cas_descsize(CAS_NRXDESC) << CAS_RX_CONF_DESC_SHFT;
1100 if ((sc->sc_flags & CAS_REG_PLUS) != 0)
1101 v |= cas_descsize(CAS_NRXDESC2) << CAS_RX_CONF_DESC2_SHFT;
1102 CAS_WRITE_4(sc, CAS_RX_CONF,
1103 v | (ETHER_ALIGN << CAS_RX_CONF_SOFF_SHFT));
1105 /* Set the PAUSE thresholds. We use the maximum OFF threshold. */
1106 CAS_WRITE_4(sc, CAS_RX_PTHRS,
1107 (111 << CAS_RX_PTHRS_XOFF_SHFT) | (15 << CAS_RX_PTHRS_XON_SHFT));
1110 CAS_WRITE_4(sc, CAS_RX_BLANK,
1111 (15 << CAS_RX_BLANK_TIME_SHFT) | (5 << CAS_RX_BLANK_PKTS_SHFT));
1113 /* Set RX_COMP_AFULL threshold to half of the RX completions. */
1114 CAS_WRITE_4(sc, CAS_RX_AEMPTY_THRS,
1115 (CAS_NRXCOMP / 2) << CAS_RX_AEMPTY_COMP_SHFT);
1117 /* Initialize the RX page size register as appropriate for 8k. */
1118 CAS_WRITE_4(sc, CAS_RX_PSZ,
1119 (CAS_RX_PSZ_8K << CAS_RX_PSZ_SHFT) |
1120 (4 << CAS_RX_PSZ_MB_CNT_SHFT) |
1121 (CAS_RX_PSZ_MB_STRD_2K << CAS_RX_PSZ_MB_STRD_SHFT) |
1122 (CAS_RX_PSZ_MB_OFF_64 << CAS_RX_PSZ_MB_OFF_SHFT));
1124 /* Disable RX random early detection. */
1125 CAS_WRITE_4(sc, CAS_RX_RED, 0);
1127 /* Zero the RX reassembly DMA table. */
1128 for (v = 0; v <= CAS_RX_REAS_DMA_ADDR_LC; v++) {
1129 CAS_WRITE_4(sc, CAS_RX_REAS_DMA_ADDR, v);
1130 CAS_WRITE_4(sc, CAS_RX_REAS_DMA_DATA_LO, 0);
1131 CAS_WRITE_4(sc, CAS_RX_REAS_DMA_DATA_MD, 0);
1132 CAS_WRITE_4(sc, CAS_RX_REAS_DMA_DATA_HI, 0);
1135 /* Ensure the RX control FIFO and RX IPP FIFO addresses are zero. */
1136 CAS_WRITE_4(sc, CAS_RX_CTRL_FIFO, 0);
1137 CAS_WRITE_4(sc, CAS_RX_IPP_ADDR, 0);
1139 /* Finally, enable RX DMA. */
1140 CAS_WRITE_4(sc, CAS_RX_CONF,
1141 CAS_READ_4(sc, CAS_RX_CONF) | CAS_RX_CONF_RXDMA_EN);
1143 /* step 11. Configure Media. */
1145 /* step 12. RX_MAC Configuration Register */
1146 v = CAS_READ_4(sc, CAS_MAC_RX_CONF);
1147 v &= ~(CAS_MAC_RX_CONF_STRPPAD | CAS_MAC_RX_CONF_EN);
1148 v |= CAS_MAC_RX_CONF_STRPFCS;
1149 sc->sc_mac_rxcfg = v;
1151 * Clear the RX filter and reprogram it. This will also set the
1152 * current RX MAC configuration and enable it.
1156 /* step 13. TX_MAC Configuration Register */
1157 v = CAS_READ_4(sc, CAS_MAC_TX_CONF);
1158 v |= CAS_MAC_TX_CONF_EN;
1159 (void)cas_disable_tx(sc);
1160 CAS_WRITE_4(sc, CAS_MAC_TX_CONF, v);
1162 /* step 14. Issue Transmit Pending command. */
1164 /* step 15. Give the receiver a swift kick. */
1165 CAS_WRITE_4(sc, CAS_RX_KICK, CAS_NRXDESC - 4);
1166 CAS_WRITE_4(sc, CAS_RX_COMP_TAIL, 0);
1167 if ((sc->sc_flags & CAS_REG_PLUS) != 0)
1168 CAS_WRITE_4(sc, CAS_RX_KICK2, CAS_NRXDESC2 - 4);
1170 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
1171 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1173 mii_mediachg(sc->sc_mii);
1175 /* Start the one second timer. */
1176 sc->sc_wdog_timer = 0;
1177 callout_reset(&sc->sc_tick_ch, hz, cas_tick, sc);
1181 cas_load_txmbuf(struct cas_softc *sc, struct mbuf **m_head)
1183 bus_dma_segment_t txsegs[CAS_NTXSEGS];
1184 struct cas_txsoft *txs;
1188 int error, nexttx, nsegs, offset, seg;
1190 CAS_LOCK_ASSERT(sc, MA_OWNED);
1192 /* Get a work queue entry. */
1193 if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) {
1194 /* Ran out of descriptors. */
1199 if (((*m_head)->m_pkthdr.csum_flags & CAS_CSUM_FEATURES) != 0) {
1200 if (M_WRITABLE(*m_head) == 0) {
1201 m = m_dup(*m_head, M_NOWAIT);
1207 offset = sizeof(struct ether_header);
1208 m = m_pullup(*m_head, offset + sizeof(struct ip));
1213 ip = (struct ip *)(mtod(m, caddr_t) + offset);
1214 offset += (ip->ip_hl << 2);
1215 cflags = (offset << CAS_TD_CKSUM_START_SHFT) |
1216 ((offset + m->m_pkthdr.csum_data) <<
1217 CAS_TD_CKSUM_STUFF_SHFT) | CAS_TD_CKSUM_EN;
1221 error = bus_dmamap_load_mbuf_sg(sc->sc_tdmatag, txs->txs_dmamap,
1222 *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
1223 if (error == EFBIG) {
1224 m = m_collapse(*m_head, M_NOWAIT, CAS_NTXSEGS);
1231 error = bus_dmamap_load_mbuf_sg(sc->sc_tdmatag,
1232 txs->txs_dmamap, *m_head, txsegs, &nsegs,
1239 } else if (error != 0)
1241 /* If nsegs is wrong then the stack is corrupt. */
1242 KASSERT(nsegs <= CAS_NTXSEGS,
1243 ("%s: too many DMA segments (%d)", __func__, nsegs));
1251 * Ensure we have enough descriptors free to describe
1252 * the packet. Note, we always reserve one descriptor
1253 * at the end of the ring as a termination point, in
1254 * order to prevent wrap-around.
1256 if (nsegs > sc->sc_txfree - 1) {
1257 txs->txs_ndescs = 0;
1258 bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap);
1262 txs->txs_ndescs = nsegs;
1263 txs->txs_firstdesc = sc->sc_txnext;
1264 nexttx = txs->txs_firstdesc;
1265 for (seg = 0; seg < nsegs; seg++, nexttx = CAS_NEXTTX(nexttx)) {
1268 "%s: mapping seg %d (txd %d), len %lx, addr %#lx (%#lx)",
1269 __func__, seg, nexttx, txsegs[seg].ds_len,
1270 txsegs[seg].ds_addr, htole64(txsegs[seg].ds_addr));
1272 sc->sc_txdescs[nexttx].cd_buf_ptr =
1273 htole64(txsegs[seg].ds_addr);
1274 KASSERT(txsegs[seg].ds_len <
1275 CAS_TD_BUF_LEN_MASK >> CAS_TD_BUF_LEN_SHFT,
1276 ("%s: segment size too large!", __func__));
1277 sc->sc_txdescs[nexttx].cd_flags =
1278 htole64(txsegs[seg].ds_len << CAS_TD_BUF_LEN_SHFT);
1279 txs->txs_lastdesc = nexttx;
1282 /* Set EOF on the last descriptor. */
1284 CTR3(KTR_CAS, "%s: end of frame at segment %d, TX %d",
1285 __func__, seg, nexttx);
1287 sc->sc_txdescs[txs->txs_lastdesc].cd_flags |=
1288 htole64(CAS_TD_END_OF_FRAME);
1290 /* Lastly set SOF on the first descriptor. */
1292 CTR3(KTR_CAS, "%s: start of frame at segment %d, TX %d",
1293 __func__, seg, nexttx);
1295 if (sc->sc_txwin += nsegs > CAS_MAXTXFREE * 2 / 3) {
1297 sc->sc_txdescs[txs->txs_firstdesc].cd_flags |=
1298 htole64(cflags | CAS_TD_START_OF_FRAME | CAS_TD_INT_ME);
1300 sc->sc_txdescs[txs->txs_firstdesc].cd_flags |=
1301 htole64(cflags | CAS_TD_START_OF_FRAME);
1303 /* Sync the DMA map. */
1304 bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap,
1305 BUS_DMASYNC_PREWRITE);
1308 CTR4(KTR_CAS, "%s: setting firstdesc=%d, lastdesc=%d, ndescs=%d",
1309 __func__, txs->txs_firstdesc, txs->txs_lastdesc,
1312 STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
1313 STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
1314 txs->txs_mbuf = *m_head;
1316 sc->sc_txnext = CAS_NEXTTX(txs->txs_lastdesc);
1317 sc->sc_txfree -= txs->txs_ndescs;
1323 cas_init_regs(struct cas_softc *sc)
1326 const u_char *laddr = if_getlladdr(sc->sc_ifp);
1328 CAS_LOCK_ASSERT(sc, MA_OWNED);
1330 /* These registers are not cleared on reset. */
1331 if ((sc->sc_flags & CAS_INITED) == 0) {
1333 CAS_WRITE_4(sc, CAS_MAC_IPG0, 0);
1334 CAS_WRITE_4(sc, CAS_MAC_IPG1, 8);
1335 CAS_WRITE_4(sc, CAS_MAC_IPG2, 4);
1337 /* min frame length */
1338 CAS_WRITE_4(sc, CAS_MAC_MIN_FRAME, ETHER_MIN_LEN);
1339 /* max frame length and max burst size */
1340 CAS_WRITE_4(sc, CAS_MAC_MAX_BF,
1341 ((ETHER_MAX_LEN_JUMBO + ETHER_VLAN_ENCAP_LEN) <<
1342 CAS_MAC_MAX_BF_FRM_SHFT) |
1343 (0x2000 << CAS_MAC_MAX_BF_BST_SHFT));
1345 /* more magic values */
1346 CAS_WRITE_4(sc, CAS_MAC_PREAMBLE_LEN, 0x7);
1347 CAS_WRITE_4(sc, CAS_MAC_JAM_SIZE, 0x4);
1348 CAS_WRITE_4(sc, CAS_MAC_ATTEMPT_LIMIT, 0x10);
1349 CAS_WRITE_4(sc, CAS_MAC_CTRL_TYPE, 0x8808);
1351 /* random number seed */
1352 CAS_WRITE_4(sc, CAS_MAC_RANDOM_SEED,
1353 ((laddr[5] << 8) | laddr[4]) & 0x3ff);
1355 /* secondary MAC addresses: 0:0:0:0:0:0 */
1356 for (i = CAS_MAC_ADDR3; i <= CAS_MAC_ADDR41;
1357 i += CAS_MAC_ADDR4 - CAS_MAC_ADDR3)
1358 CAS_WRITE_4(sc, i, 0);
1360 /* MAC control address: 01:80:c2:00:00:01 */
1361 CAS_WRITE_4(sc, CAS_MAC_ADDR42, 0x0001);
1362 CAS_WRITE_4(sc, CAS_MAC_ADDR43, 0xc200);
1363 CAS_WRITE_4(sc, CAS_MAC_ADDR44, 0x0180);
1365 /* MAC filter address: 0:0:0:0:0:0 */
1366 CAS_WRITE_4(sc, CAS_MAC_AFILTER0, 0);
1367 CAS_WRITE_4(sc, CAS_MAC_AFILTER1, 0);
1368 CAS_WRITE_4(sc, CAS_MAC_AFILTER2, 0);
1369 CAS_WRITE_4(sc, CAS_MAC_AFILTER_MASK1_2, 0);
1370 CAS_WRITE_4(sc, CAS_MAC_AFILTER_MASK0, 0);
1372 /* Zero the hash table. */
1373 for (i = CAS_MAC_HASH0; i <= CAS_MAC_HASH15;
1374 i += CAS_MAC_HASH1 - CAS_MAC_HASH0)
1375 CAS_WRITE_4(sc, i, 0);
1377 sc->sc_flags |= CAS_INITED;
1380 /* Counters need to be zeroed. */
1381 CAS_WRITE_4(sc, CAS_MAC_NORM_COLL_CNT, 0);
1382 CAS_WRITE_4(sc, CAS_MAC_FIRST_COLL_CNT, 0);
1383 CAS_WRITE_4(sc, CAS_MAC_EXCESS_COLL_CNT, 0);
1384 CAS_WRITE_4(sc, CAS_MAC_LATE_COLL_CNT, 0);
1385 CAS_WRITE_4(sc, CAS_MAC_DEFER_TMR_CNT, 0);
1386 CAS_WRITE_4(sc, CAS_MAC_PEAK_ATTEMPTS, 0);
1387 CAS_WRITE_4(sc, CAS_MAC_RX_FRAME_COUNT, 0);
1388 CAS_WRITE_4(sc, CAS_MAC_RX_LEN_ERR_CNT, 0);
1389 CAS_WRITE_4(sc, CAS_MAC_RX_ALIGN_ERR, 0);
1390 CAS_WRITE_4(sc, CAS_MAC_RX_CRC_ERR_CNT, 0);
1391 CAS_WRITE_4(sc, CAS_MAC_RX_CODE_VIOL, 0);
1393 /* Set XOFF PAUSE time. */
1394 CAS_WRITE_4(sc, CAS_MAC_SPC, 0x1BF0 << CAS_MAC_SPC_TIME_SHFT);
1396 /* Set the station address. */
1397 CAS_WRITE_4(sc, CAS_MAC_ADDR0, (laddr[4] << 8) | laddr[5]);
1398 CAS_WRITE_4(sc, CAS_MAC_ADDR1, (laddr[2] << 8) | laddr[3]);
1399 CAS_WRITE_4(sc, CAS_MAC_ADDR2, (laddr[0] << 8) | laddr[1]);
1401 /* Enable MII outputs. */
1402 CAS_WRITE_4(sc, CAS_MAC_XIF_CONF, CAS_MAC_XIF_CONF_TX_OE);
1406 cas_tx_task(void *arg, int pending __unused)
1415 cas_txkick(struct cas_softc *sc)
1419 * Update the TX kick register. This register has to point to the
1420 * descriptor after the last valid one and for optimum performance
1421 * should be incremented in multiples of 4 (the DMA engine fetches/
1422 * updates descriptors in batches of 4).
1425 CTR3(KTR_CAS, "%s: %s: kicking TX %d",
1426 device_get_name(sc->sc_dev), __func__, sc->sc_txnext);
1428 CAS_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1429 CAS_WRITE_4(sc, CAS_TX_KICK3, sc->sc_txnext);
1435 struct cas_softc *sc = if_getsoftc(ifp);
1441 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1442 IFF_DRV_RUNNING || (sc->sc_flags & CAS_LINK) == 0) {
1447 if (sc->sc_txfree < CAS_MAXTXFREE / 4)
1451 CTR4(KTR_CAS, "%s: %s: txfree %d, txnext %d",
1452 device_get_name(sc->sc_dev), __func__, sc->sc_txfree,
1457 for (; !if_sendq_empty(ifp) && sc->sc_txfree > 1;) {
1458 m = if_dequeue(ifp);
1461 if (cas_load_txmbuf(sc, &m) != 0) {
1464 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
1465 if_sendq_prepend(ifp, m);
1468 if ((sc->sc_txnext % 4) == 0) {
1481 CTR2(KTR_CAS, "%s: packets enqueued, OWN on %d",
1482 device_get_name(sc->sc_dev), sc->sc_txnext);
1485 /* Set a watchdog timer in case the chip flakes out. */
1486 sc->sc_wdog_timer = 5;
1488 CTR3(KTR_CAS, "%s: %s: watchdog %d",
1489 device_get_name(sc->sc_dev), __func__,
1498 cas_tint(struct cas_softc *sc)
1500 if_t ifp = sc->sc_ifp;
1501 struct cas_txsoft *txs;
1507 CAS_LOCK_ASSERT(sc, MA_OWNED);
1509 CTR2(KTR_CAS, "%s: %s", device_get_name(sc->sc_dev), __func__);
1513 * Go through our TX list and free mbufs for those
1514 * frames that have been transmitted.
1517 CAS_CDSYNC(sc, BUS_DMASYNC_POSTREAD);
1518 while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1520 if ((if_getflags(ifp) & IFF_DEBUG) != 0) {
1521 printf(" txsoft %p transmit chain:\n", txs);
1522 for (i = txs->txs_firstdesc;; i = CAS_NEXTTX(i)) {
1523 printf("descriptor %d: ", i);
1524 printf("cd_flags: 0x%016llx\t",
1526 sc->sc_txdescs[i].cd_flags));
1527 printf("cd_buf_ptr: 0x%016llx\n",
1529 sc->sc_txdescs[i].cd_buf_ptr));
1530 if (i == txs->txs_lastdesc)
1537 * In theory, we could harvest some descriptors before
1538 * the ring is empty, but that's a bit complicated.
1540 * CAS_TX_COMPn points to the last descriptor
1543 txlast = CAS_READ_4(sc, CAS_TX_COMP3);
1545 CTR4(KTR_CAS, "%s: txs->txs_firstdesc = %d, "
1546 "txs->txs_lastdesc = %d, txlast = %d",
1547 __func__, txs->txs_firstdesc, txs->txs_lastdesc, txlast);
1549 if (txs->txs_firstdesc <= txs->txs_lastdesc) {
1550 if ((txlast >= txs->txs_firstdesc) &&
1551 (txlast <= txs->txs_lastdesc))
1554 /* Ick -- this command wraps. */
1555 if ((txlast >= txs->txs_firstdesc) ||
1556 (txlast <= txs->txs_lastdesc))
1561 CTR1(KTR_CAS, "%s: releasing a descriptor", __func__);
1563 STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
1565 sc->sc_txfree += txs->txs_ndescs;
1567 bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap,
1568 BUS_DMASYNC_POSTWRITE);
1569 bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap);
1570 if (txs->txs_mbuf != NULL) {
1571 m_freem(txs->txs_mbuf);
1572 txs->txs_mbuf = NULL;
1575 STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1577 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1582 CTR5(KTR_CAS, "%s: CAS_TX_SM1 %x CAS_TX_SM2 %x CAS_TX_DESC_BASE %llx "
1584 __func__, CAS_READ_4(sc, CAS_TX_SM1), CAS_READ_4(sc, CAS_TX_SM2),
1585 ((long long)CAS_READ_4(sc, CAS_TX_DESC3_BASE_HI) << 32) |
1586 CAS_READ_4(sc, CAS_TX_DESC3_BASE_LO),
1587 CAS_READ_4(sc, CAS_TX_COMP3));
1591 /* We freed some descriptors, so reset IFF_DRV_OACTIVE. */
1592 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1593 if (STAILQ_EMPTY(&sc->sc_txdirtyq))
1594 sc->sc_wdog_timer = 0;
1598 CTR3(KTR_CAS, "%s: %s: watchdog %d",
1599 device_get_name(sc->sc_dev), __func__, sc->sc_wdog_timer);
1604 cas_rint_timeout(void *arg)
1606 struct epoch_tracker et;
1607 struct cas_softc *sc = arg;
1609 CAS_LOCK_ASSERT(sc, MA_OWNED);
1611 NET_EPOCH_ENTER(et);
1617 cas_rint(struct cas_softc *sc)
1619 struct cas_rxdsoft *rxds, *rxds2;
1620 if_t ifp = sc->sc_ifp;
1621 struct mbuf *m, *m2;
1622 uint64_t word1, word2, word3 __unused, word4;
1624 u_int idx, idx2, len, off, skip;
1626 CAS_LOCK_ASSERT(sc, MA_OWNED);
1628 callout_stop(&sc->sc_rx_ch);
1631 CTR2(KTR_CAS, "%s: %s", device_get_name(sc->sc_dev), __func__);
1634 #define PRINTWORD(n, delimiter) \
1635 printf("word ## n: 0x%016llx%c", (long long)word ## n, delimiter)
1637 #define SKIPASSERT(n) \
1638 KASSERT(sc->sc_rxcomps[sc->sc_rxcptr].crc_word ## n == 0, \
1639 ("%s: word ## n not 0", __func__))
1641 #define WORDTOH(n) \
1642 word ## n = le64toh(sc->sc_rxcomps[sc->sc_rxcptr].crc_word ## n)
1645 * Read the completion head register once. This limits
1646 * how long the following loop can execute.
1648 rxhead = CAS_READ_4(sc, CAS_RX_COMP_HEAD);
1650 CTR4(KTR_CAS, "%s: sc->sc_rxcptr %d, sc->sc_rxdptr %d, head %d",
1651 __func__, sc->sc_rxcptr, sc->sc_rxdptr, rxhead);
1654 CAS_CDSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1655 for (; sc->sc_rxcptr != rxhead;
1656 sc->sc_rxcptr = CAS_NEXTRXCOMP(sc->sc_rxcptr)) {
1672 if ((if_getflags(ifp) & IFF_DEBUG) != 0) {
1673 printf(" completion %d: ", sc->sc_rxcptr);
1681 if (__predict_false(
1682 (word1 & CAS_RC1_TYPE_MASK) == CAS_RC1_TYPE_HW ||
1683 (word4 & CAS_RC4_ZERO) != 0)) {
1685 * The descriptor is still marked as owned, although
1686 * it is supposed to have completed. This has been
1687 * observed on some machines. Just exiting here
1688 * might leave the packet sitting around until another
1689 * one arrives to trigger a new interrupt, which is
1690 * generally undesirable, so set up a timeout.
1692 callout_reset(&sc->sc_rx_ch, CAS_RXOWN_TICKS,
1693 cas_rint_timeout, sc);
1697 if (__predict_false(
1698 (word4 & (CAS_RC4_BAD | CAS_RC4_LEN_MMATCH)) != 0)) {
1699 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1700 device_printf(sc->sc_dev,
1701 "receive error: CRC error\n");
1705 KASSERT(CAS_GET(word1, CAS_RC1_DATA_SIZE) == 0 ||
1706 CAS_GET(word2, CAS_RC2_HDR_SIZE) == 0,
1707 ("%s: data and header present", __func__));
1708 KASSERT((word1 & CAS_RC1_SPLIT_PKT) == 0 ||
1709 CAS_GET(word2, CAS_RC2_HDR_SIZE) == 0,
1710 ("%s: split and header present", __func__));
1711 KASSERT(CAS_GET(word1, CAS_RC1_DATA_SIZE) == 0 ||
1712 (word1 & CAS_RC1_RELEASE_HDR) == 0,
1713 ("%s: data present but header release", __func__));
1714 KASSERT(CAS_GET(word2, CAS_RC2_HDR_SIZE) == 0 ||
1715 (word1 & CAS_RC1_RELEASE_DATA) == 0,
1716 ("%s: header present but data release", __func__));
1718 if ((len = CAS_GET(word2, CAS_RC2_HDR_SIZE)) != 0) {
1719 idx = CAS_GET(word2, CAS_RC2_HDR_INDEX);
1720 off = CAS_GET(word2, CAS_RC2_HDR_OFF);
1722 CTR4(KTR_CAS, "%s: hdr at idx %d, off %d, len %d",
1723 __func__, idx, off, len);
1725 rxds = &sc->sc_rxdsoft[idx];
1726 MGETHDR(m, M_NOWAIT, MT_DATA);
1728 refcount_acquire(&rxds->rxds_refcount);
1729 bus_dmamap_sync(sc->sc_rdmatag,
1730 rxds->rxds_dmamap, BUS_DMASYNC_POSTREAD);
1731 m_extadd(m, (char *)rxds->rxds_buf +
1732 off * 256 + ETHER_ALIGN, len, cas_free,
1733 sc, (void *)(uintptr_t)idx,
1734 M_RDONLY, EXT_NET_DRV);
1735 if ((m->m_flags & M_EXT) == 0) {
1741 m->m_pkthdr.rcvif = ifp;
1742 m->m_pkthdr.len = m->m_len = len;
1743 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1744 if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0)
1745 cas_rxcksum(m, CAS_GET(word4,
1752 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1754 if ((word1 & CAS_RC1_RELEASE_HDR) != 0 &&
1755 refcount_release(&rxds->rxds_refcount) != 0)
1756 cas_add_rxdesc(sc, idx);
1757 } else if ((len = CAS_GET(word1, CAS_RC1_DATA_SIZE)) != 0) {
1758 idx = CAS_GET(word1, CAS_RC1_DATA_INDEX);
1759 off = CAS_GET(word1, CAS_RC1_DATA_OFF);
1761 CTR4(KTR_CAS, "%s: data at idx %d, off %d, len %d",
1762 __func__, idx, off, len);
1764 rxds = &sc->sc_rxdsoft[idx];
1765 MGETHDR(m, M_NOWAIT, MT_DATA);
1767 refcount_acquire(&rxds->rxds_refcount);
1769 m->m_len = min(CAS_PAGE_SIZE - off, len);
1770 bus_dmamap_sync(sc->sc_rdmatag,
1771 rxds->rxds_dmamap, BUS_DMASYNC_POSTREAD);
1772 m_extadd(m, (char *)rxds->rxds_buf + off,
1773 m->m_len, cas_free, sc,
1774 (void *)(uintptr_t)idx, M_RDONLY,
1776 if ((m->m_flags & M_EXT) == 0) {
1784 if ((word1 & CAS_RC1_SPLIT_PKT) != 0) {
1785 KASSERT((word1 & CAS_RC1_RELEASE_NEXT) != 0,
1786 ("%s: split but no release next",
1789 idx2 = CAS_GET(word2, CAS_RC2_NEXT_INDEX);
1791 CTR2(KTR_CAS, "%s: split at idx %d",
1794 rxds2 = &sc->sc_rxdsoft[idx2];
1796 MGET(m2, M_NOWAIT, MT_DATA);
1799 &rxds2->rxds_refcount);
1800 m2->m_len = len - m->m_len;
1804 BUS_DMASYNC_POSTREAD);
1806 (char *)rxds2->rxds_buf,
1807 m2->m_len, cas_free, sc,
1808 (void *)(uintptr_t)idx2,
1809 M_RDONLY, EXT_NET_DRV);
1810 if ((m2->m_flags & M_EXT) ==
1819 else if (m != NULL) {
1825 m->m_pkthdr.rcvif = ifp;
1826 m->m_pkthdr.len = len;
1827 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1828 if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0)
1829 cas_rxcksum(m, CAS_GET(word4,
1836 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1838 if ((word1 & CAS_RC1_RELEASE_DATA) != 0 &&
1839 refcount_release(&rxds->rxds_refcount) != 0)
1840 cas_add_rxdesc(sc, idx);
1841 if ((word1 & CAS_RC1_SPLIT_PKT) != 0 &&
1842 refcount_release(&rxds2->rxds_refcount) != 0)
1843 cas_add_rxdesc(sc, idx2);
1846 skip = CAS_GET(word1, CAS_RC1_SKIP);
1849 cas_rxcompinit(&sc->sc_rxcomps[sc->sc_rxcptr]);
1850 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
1853 CAS_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1854 CAS_WRITE_4(sc, CAS_RX_COMP_TAIL, sc->sc_rxcptr);
1861 CTR4(KTR_CAS, "%s: done sc->sc_rxcptr %d, sc->sc_rxdptr %d, head %d",
1862 __func__, sc->sc_rxcptr, sc->sc_rxdptr,
1863 CAS_READ_4(sc, CAS_RX_COMP_HEAD));
1868 cas_free(struct mbuf *m)
1870 struct cas_rxdsoft *rxds;
1871 struct cas_softc *sc;
1874 sc = m->m_ext.ext_arg1;
1875 idx = (uintptr_t)m->m_ext.ext_arg2;
1876 rxds = &sc->sc_rxdsoft[idx];
1877 if (refcount_release(&rxds->rxds_refcount) == 0)
1881 * NB: this function can be called via m_freem(9) within
1884 if ((locked = CAS_LOCK_OWNED(sc)) == 0)
1886 cas_add_rxdesc(sc, idx);
1892 cas_add_rxdesc(struct cas_softc *sc, u_int idx)
1895 CAS_LOCK_ASSERT(sc, MA_OWNED);
1897 bus_dmamap_sync(sc->sc_rdmatag, sc->sc_rxdsoft[idx].rxds_dmamap,
1898 BUS_DMASYNC_PREREAD);
1899 CAS_UPDATE_RXDESC(sc, sc->sc_rxdptr, idx);
1900 sc->sc_rxdptr = CAS_NEXTRXDESC(sc->sc_rxdptr);
1903 * Update the RX kick register. This register has to point to the
1904 * descriptor after the last valid one (before the current batch)
1905 * and for optimum performance should be incremented in multiples
1906 * of 4 (the DMA engine fetches/updates descriptors in batches of 4).
1908 if ((sc->sc_rxdptr % 4) == 0) {
1909 CAS_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1910 CAS_WRITE_4(sc, CAS_RX_KICK,
1911 (sc->sc_rxdptr + CAS_NRXDESC - 4) & CAS_NRXDESC_MASK);
1916 cas_eint(struct cas_softc *sc, u_int status)
1918 if_t ifp = sc->sc_ifp;
1920 CAS_LOCK_ASSERT(sc, MA_OWNED);
1922 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1924 device_printf(sc->sc_dev, "%s: status 0x%x", __func__, status);
1925 if ((status & CAS_INTR_PCI_ERROR_INT) != 0) {
1926 status = CAS_READ_4(sc, CAS_ERROR_STATUS);
1927 printf(", PCI bus error 0x%x", status);
1928 if ((status & CAS_ERROR_OTHER) != 0) {
1929 status = pci_read_config(sc->sc_dev, PCIR_STATUS, 2);
1930 printf(", PCI status 0x%x", status);
1931 pci_write_config(sc->sc_dev, PCIR_STATUS, status, 2);
1936 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
1937 cas_init_locked(sc);
1938 if (!if_sendq_empty(ifp))
1939 taskqueue_enqueue(sc->sc_tq, &sc->sc_tx_task);
1945 struct cas_softc *sc = v;
1947 if (__predict_false((CAS_READ_4(sc, CAS_STATUS_ALIAS) &
1948 CAS_INTR_SUMMARY) == 0))
1949 return (FILTER_STRAY);
1951 /* Disable interrupts. */
1952 CAS_WRITE_4(sc, CAS_INTMASK, 0xffffffff);
1953 taskqueue_enqueue(sc->sc_tq, &sc->sc_intr_task);
1955 return (FILTER_HANDLED);
1959 cas_intr_task(void *arg, int pending __unused)
1961 struct cas_softc *sc = arg;
1962 if_t ifp = sc->sc_ifp;
1963 uint32_t status, status2;
1965 CAS_LOCK_ASSERT(sc, MA_NOTOWNED);
1967 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
1970 status = CAS_READ_4(sc, CAS_STATUS);
1971 if (__predict_false((status & CAS_INTR_SUMMARY) == 0))
1976 CTR4(KTR_CAS, "%s: %s: cplt %x, status %x",
1977 device_get_name(sc->sc_dev), __func__,
1978 (status >> CAS_STATUS_TX_COMP3_SHFT), (u_int)status);
1981 * PCS interrupts must be cleared, otherwise no traffic is passed!
1983 if ((status & CAS_INTR_PCS_INT) != 0) {
1985 CAS_READ_4(sc, CAS_PCS_INTR_STATUS) |
1986 CAS_READ_4(sc, CAS_PCS_INTR_STATUS);
1987 if ((status2 & CAS_PCS_INTR_LINK) != 0)
1988 device_printf(sc->sc_dev,
1989 "%s: PCS link status changed\n", __func__);
1991 if ((status & CAS_MAC_CTRL_STATUS) != 0) {
1992 status2 = CAS_READ_4(sc, CAS_MAC_CTRL_STATUS);
1993 if ((status2 & CAS_MAC_CTRL_PAUSE) != 0)
1994 device_printf(sc->sc_dev,
1995 "%s: PAUSE received (PAUSE time %d slots)\n",
1997 (status2 & CAS_MAC_CTRL_STATUS_PT_MASK) >>
1998 CAS_MAC_CTRL_STATUS_PT_SHFT);
1999 if ((status2 & CAS_MAC_CTRL_PAUSE) != 0)
2000 device_printf(sc->sc_dev,
2001 "%s: transited to PAUSE state\n", __func__);
2002 if ((status2 & CAS_MAC_CTRL_NON_PAUSE) != 0)
2003 device_printf(sc->sc_dev,
2004 "%s: transited to non-PAUSE state\n", __func__);
2006 if ((status & CAS_INTR_MIF) != 0)
2007 device_printf(sc->sc_dev, "%s: MIF interrupt\n", __func__);
2010 if (__predict_false((status &
2011 (CAS_INTR_TX_TAG_ERR | CAS_INTR_RX_TAG_ERR |
2012 CAS_INTR_RX_LEN_MMATCH | CAS_INTR_PCI_ERROR_INT)) != 0)) {
2013 cas_eint(sc, status);
2018 if (__predict_false(status & CAS_INTR_TX_MAC_INT)) {
2019 status2 = CAS_READ_4(sc, CAS_MAC_TX_STATUS);
2021 (CAS_MAC_TX_UNDERRUN | CAS_MAC_TX_MAX_PKT_ERR)) != 0)
2022 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2023 else if ((status2 & ~CAS_MAC_TX_FRAME_XMTD) != 0)
2024 device_printf(sc->sc_dev,
2025 "MAC TX fault, status %x\n", status2);
2028 if (__predict_false(status & CAS_INTR_RX_MAC_INT)) {
2029 status2 = CAS_READ_4(sc, CAS_MAC_RX_STATUS);
2030 if ((status2 & CAS_MAC_RX_OVERFLOW) != 0)
2031 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
2032 else if ((status2 & ~CAS_MAC_RX_FRAME_RCVD) != 0)
2033 device_printf(sc->sc_dev,
2034 "MAC RX fault, status %x\n", status2);
2038 (CAS_INTR_RX_DONE | CAS_INTR_RX_BUF_NA | CAS_INTR_RX_COMP_FULL |
2039 CAS_INTR_RX_BUF_AEMPTY | CAS_INTR_RX_COMP_AFULL)) != 0) {
2042 if (__predict_false((status &
2043 (CAS_INTR_RX_BUF_NA | CAS_INTR_RX_COMP_FULL |
2044 CAS_INTR_RX_BUF_AEMPTY | CAS_INTR_RX_COMP_AFULL)) != 0))
2045 device_printf(sc->sc_dev,
2046 "RX fault, status %x\n", status);
2051 (CAS_INTR_TX_INT_ME | CAS_INTR_TX_ALL | CAS_INTR_TX_DONE)) != 0)
2054 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
2057 } else if (!if_sendq_empty(ifp))
2058 taskqueue_enqueue(sc->sc_tq, &sc->sc_tx_task);
2061 status = CAS_READ_4(sc, CAS_STATUS_ALIAS);
2062 if (__predict_false((status & CAS_INTR_SUMMARY) != 0)) {
2063 taskqueue_enqueue(sc->sc_tq, &sc->sc_intr_task);
2068 /* Re-enable interrupts. */
2069 CAS_WRITE_4(sc, CAS_INTMASK,
2070 ~(CAS_INTR_TX_INT_ME | CAS_INTR_TX_TAG_ERR |
2071 CAS_INTR_RX_DONE | CAS_INTR_RX_BUF_NA | CAS_INTR_RX_TAG_ERR |
2072 CAS_INTR_RX_COMP_FULL | CAS_INTR_RX_BUF_AEMPTY |
2073 CAS_INTR_RX_COMP_AFULL | CAS_INTR_RX_LEN_MMATCH |
2074 CAS_INTR_PCI_ERROR_INT
2076 | CAS_INTR_PCS_INT | CAS_INTR_MIF
2082 cas_watchdog(struct cas_softc *sc)
2084 if_t ifp = sc->sc_ifp;
2086 CAS_LOCK_ASSERT(sc, MA_OWNED);
2090 "%s: CAS_RX_CONF %x CAS_MAC_RX_STATUS %x CAS_MAC_RX_CONF %x",
2091 __func__, CAS_READ_4(sc, CAS_RX_CONF),
2092 CAS_READ_4(sc, CAS_MAC_RX_STATUS),
2093 CAS_READ_4(sc, CAS_MAC_RX_CONF));
2095 "%s: CAS_TX_CONF %x CAS_MAC_TX_STATUS %x CAS_MAC_TX_CONF %x",
2096 __func__, CAS_READ_4(sc, CAS_TX_CONF),
2097 CAS_READ_4(sc, CAS_MAC_TX_STATUS),
2098 CAS_READ_4(sc, CAS_MAC_TX_CONF));
2101 if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0)
2104 if ((sc->sc_flags & CAS_LINK) != 0)
2105 device_printf(sc->sc_dev, "device timeout\n");
2106 else if (bootverbose)
2107 device_printf(sc->sc_dev, "device timeout (no link)\n");
2108 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2110 /* Try to get more packets going. */
2111 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2112 cas_init_locked(sc);
2113 if (!if_sendq_empty(ifp))
2114 taskqueue_enqueue(sc->sc_tq, &sc->sc_tx_task);
2118 cas_mifinit(struct cas_softc *sc)
2121 /* Configure the MIF in frame mode. */
2122 CAS_WRITE_4(sc, CAS_MIF_CONF,
2123 CAS_READ_4(sc, CAS_MIF_CONF) & ~CAS_MIF_CONF_BB_MODE);
2124 CAS_BARRIER(sc, CAS_MIF_CONF, 4,
2125 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
2131 * The MII interface supports at least three different operating modes:
2133 * Bitbang mode is implemented using data, clock and output enable registers.
2135 * Frame mode is implemented by loading a complete frame into the frame
2136 * register and polling the valid bit for completion.
2138 * Polling mode uses the frame register but completion is indicated by
2143 cas_mii_readreg(device_t dev, int phy, int reg)
2145 struct cas_softc *sc;
2149 #ifdef CAS_DEBUG_PHY
2150 printf("%s: phy %d reg %d\n", __func__, phy, reg);
2153 sc = device_get_softc(dev);
2154 if ((sc->sc_flags & CAS_SERDES) != 0) {
2160 reg = CAS_PCS_STATUS;
2169 reg = CAS_PCS_ANLPAR;
2172 return (EXTSR_1000XFDX | EXTSR_1000XHDX);
2174 device_printf(sc->sc_dev,
2175 "%s: unhandled register %d\n", __func__, reg);
2178 return (CAS_READ_4(sc, reg));
2181 /* Construct the frame command. */
2182 v = CAS_MIF_FRAME_READ |
2183 (phy << CAS_MIF_FRAME_PHY_SHFT) |
2184 (reg << CAS_MIF_FRAME_REG_SHFT);
2186 CAS_WRITE_4(sc, CAS_MIF_FRAME, v);
2187 CAS_BARRIER(sc, CAS_MIF_FRAME, 4,
2188 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
2189 for (n = 0; n < 100; n++) {
2191 v = CAS_READ_4(sc, CAS_MIF_FRAME);
2192 if (v & CAS_MIF_FRAME_TA_LSB)
2193 return (v & CAS_MIF_FRAME_DATA);
2196 device_printf(sc->sc_dev, "%s: timed out\n", __func__);
2201 cas_mii_writereg(device_t dev, int phy, int reg, int val)
2203 struct cas_softc *sc;
2207 #ifdef CAS_DEBUG_PHY
2208 printf("%s: phy %d reg %d val %x\n", phy, reg, val, __func__);
2211 sc = device_get_softc(dev);
2212 if ((sc->sc_flags & CAS_SERDES) != 0) {
2215 reg = CAS_PCS_STATUS;
2219 if ((val & CAS_PCS_CTRL_RESET) == 0)
2221 CAS_WRITE_4(sc, CAS_PCS_CTRL, val);
2222 CAS_BARRIER(sc, CAS_PCS_CTRL, 4,
2223 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
2224 if (!cas_bitwait(sc, CAS_PCS_CTRL,
2225 CAS_PCS_CTRL_RESET, 0))
2226 device_printf(sc->sc_dev,
2227 "cannot reset PCS\n");
2230 CAS_WRITE_4(sc, CAS_PCS_CONF, 0);
2231 CAS_BARRIER(sc, CAS_PCS_CONF, 4,
2232 BUS_SPACE_BARRIER_WRITE);
2233 CAS_WRITE_4(sc, CAS_PCS_ANAR, val);
2234 CAS_BARRIER(sc, CAS_PCS_ANAR, 4,
2235 BUS_SPACE_BARRIER_WRITE);
2236 CAS_WRITE_4(sc, CAS_PCS_SERDES_CTRL,
2237 CAS_PCS_SERDES_CTRL_ESD);
2238 CAS_BARRIER(sc, CAS_PCS_CONF, 4,
2239 BUS_SPACE_BARRIER_WRITE);
2240 CAS_WRITE_4(sc, CAS_PCS_CONF,
2242 CAS_BARRIER(sc, CAS_PCS_CONF, 4,
2243 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
2246 reg = CAS_PCS_ANLPAR;
2249 device_printf(sc->sc_dev,
2250 "%s: unhandled register %d\n", __func__, reg);
2253 CAS_WRITE_4(sc, reg, val);
2254 CAS_BARRIER(sc, reg, 4,
2255 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
2259 /* Construct the frame command. */
2260 v = CAS_MIF_FRAME_WRITE |
2261 (phy << CAS_MIF_FRAME_PHY_SHFT) |
2262 (reg << CAS_MIF_FRAME_REG_SHFT) |
2263 (val & CAS_MIF_FRAME_DATA);
2265 CAS_WRITE_4(sc, CAS_MIF_FRAME, v);
2266 CAS_BARRIER(sc, CAS_MIF_FRAME, 4,
2267 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
2268 for (n = 0; n < 100; n++) {
2270 v = CAS_READ_4(sc, CAS_MIF_FRAME);
2271 if (v & CAS_MIF_FRAME_TA_LSB)
2275 device_printf(sc->sc_dev, "%s: timed out\n", __func__);
2280 cas_mii_statchg(device_t dev)
2282 struct cas_softc *sc;
2285 uint32_t rxcfg, txcfg, v;
2287 sc = device_get_softc(dev);
2290 CAS_LOCK_ASSERT(sc, MA_OWNED);
2293 if ((if_getflags(ifp) & IFF_DEBUG) != 0)
2294 device_printf(sc->sc_dev, "%s: status changen", __func__);
2297 if ((sc->sc_mii->mii_media_status & IFM_ACTIVE) != 0 &&
2298 IFM_SUBTYPE(sc->sc_mii->mii_media_active) != IFM_NONE)
2299 sc->sc_flags |= CAS_LINK;
2301 sc->sc_flags &= ~CAS_LINK;
2303 switch (IFM_SUBTYPE(sc->sc_mii->mii_media_active)) {
2315 * The configuration done here corresponds to the steps F) and
2316 * G) and as far as enabling of RX and TX MAC goes also step H)
2317 * of the initialization sequence outlined in section 11.2.1 of
2318 * the Cassini+ ASIC Specification.
2321 rxcfg = sc->sc_mac_rxcfg;
2322 rxcfg &= ~CAS_MAC_RX_CONF_CARR;
2323 txcfg = CAS_MAC_TX_CONF_EN_IPG0 | CAS_MAC_TX_CONF_NGU |
2324 CAS_MAC_TX_CONF_NGUL;
2325 if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0)
2326 txcfg |= CAS_MAC_TX_CONF_ICARR | CAS_MAC_TX_CONF_ICOLLIS;
2327 else if (gigabit != 0) {
2328 rxcfg |= CAS_MAC_RX_CONF_CARR;
2329 txcfg |= CAS_MAC_TX_CONF_CARR;
2331 (void)cas_disable_tx(sc);
2332 CAS_WRITE_4(sc, CAS_MAC_TX_CONF, txcfg);
2333 (void)cas_disable_rx(sc);
2334 CAS_WRITE_4(sc, CAS_MAC_RX_CONF, rxcfg);
2336 v = CAS_READ_4(sc, CAS_MAC_CTRL_CONF) &
2337 ~(CAS_MAC_CTRL_CONF_TXP | CAS_MAC_CTRL_CONF_RXP);
2338 if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) &
2339 IFM_ETH_RXPAUSE) != 0)
2340 v |= CAS_MAC_CTRL_CONF_RXP;
2341 if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) &
2342 IFM_ETH_TXPAUSE) != 0)
2343 v |= CAS_MAC_CTRL_CONF_TXP;
2344 CAS_WRITE_4(sc, CAS_MAC_CTRL_CONF, v);
2347 * All supported chips have a bug causing incorrect checksum
2348 * to be calculated when letting them strip the FCS in half-
2349 * duplex mode. In theory we could disable FCS stripping and
2350 * manually adjust the checksum accordingly. It seems to make
2351 * more sense to optimze for the common case and just disable
2352 * hardware checksumming in half-duplex mode though.
2354 if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) == 0) {
2355 if_setcapenablebit(ifp, 0, IFCAP_HWCSUM);
2356 if_sethwassist(ifp, 0);
2357 } else if ((sc->sc_flags & CAS_NO_CSUM) == 0) {
2358 if_setcapenable(ifp, if_getcapabilities(ifp));
2359 if_sethwassist(ifp, CAS_CSUM_FEATURES);
2362 if (sc->sc_variant == CAS_SATURN) {
2363 if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) == 0)
2364 /* silicon bug workaround */
2365 CAS_WRITE_4(sc, CAS_MAC_PREAMBLE_LEN, 0x41);
2367 CAS_WRITE_4(sc, CAS_MAC_PREAMBLE_LEN, 0x7);
2370 if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) == 0 &&
2372 CAS_WRITE_4(sc, CAS_MAC_SLOT_TIME,
2373 CAS_MAC_SLOT_TIME_CARR);
2375 CAS_WRITE_4(sc, CAS_MAC_SLOT_TIME,
2376 CAS_MAC_SLOT_TIME_NORM);
2378 /* XIF Configuration */
2379 v = CAS_MAC_XIF_CONF_TX_OE | CAS_MAC_XIF_CONF_LNKLED;
2380 if ((sc->sc_flags & CAS_SERDES) == 0) {
2381 if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) == 0)
2382 v |= CAS_MAC_XIF_CONF_NOECHO;
2383 v |= CAS_MAC_XIF_CONF_BUF_OE;
2386 v |= CAS_MAC_XIF_CONF_GMII;
2387 if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0)
2388 v |= CAS_MAC_XIF_CONF_FDXLED;
2389 CAS_WRITE_4(sc, CAS_MAC_XIF_CONF, v);
2391 sc->sc_mac_rxcfg = rxcfg;
2392 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0 &&
2393 (sc->sc_flags & CAS_LINK) != 0) {
2394 CAS_WRITE_4(sc, CAS_MAC_TX_CONF,
2395 txcfg | CAS_MAC_TX_CONF_EN);
2396 CAS_WRITE_4(sc, CAS_MAC_RX_CONF,
2397 rxcfg | CAS_MAC_RX_CONF_EN);
2402 cas_mediachange(if_t ifp)
2404 struct cas_softc *sc = if_getsoftc(ifp);
2407 /* XXX add support for serial media. */
2410 error = mii_mediachg(sc->sc_mii);
2416 cas_mediastatus(if_t ifp, struct ifmediareq *ifmr)
2418 struct cas_softc *sc = if_getsoftc(ifp);
2421 if ((if_getflags(ifp) & IFF_UP) == 0) {
2426 mii_pollstat(sc->sc_mii);
2427 ifmr->ifm_active = sc->sc_mii->mii_media_active;
2428 ifmr->ifm_status = sc->sc_mii->mii_media_status;
2433 cas_ioctl(if_t ifp, u_long cmd, caddr_t data)
2435 struct cas_softc *sc = if_getsoftc(ifp);
2436 struct ifreq *ifr = (struct ifreq *)data;
2443 if ((if_getflags(ifp) & IFF_UP) != 0) {
2444 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0 &&
2445 ((if_getflags(ifp) ^ sc->sc_ifflags) &
2446 (IFF_ALLMULTI | IFF_PROMISC)) != 0)
2449 cas_init_locked(sc);
2450 } else if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
2452 sc->sc_ifflags = if_getflags(ifp);
2457 if ((sc->sc_flags & CAS_NO_CSUM) != 0) {
2462 if_setcapenable(ifp, ifr->ifr_reqcap);
2463 if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
2464 if_sethwassist(ifp, CAS_CSUM_FEATURES);
2466 if_sethwassist(ifp, 0);
2472 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
2477 if ((ifr->ifr_mtu < ETHERMIN) ||
2478 (ifr->ifr_mtu > ETHERMTU_JUMBO))
2481 if_setmtu(ifp, ifr->ifr_mtu);
2485 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd);
2488 error = ether_ioctl(ifp, cmd, data);
2496 cas_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
2498 uint32_t crc, *hash = arg;
2500 crc = ether_crc32_le(LLADDR(sdl), ETHER_ADDR_LEN);
2501 /* We just want the 8 most significant bits. */
2503 /* Set the corresponding bit in the filter. */
2504 hash[crc >> 4] |= 1 << (15 - (crc & 15));
2510 cas_setladrf(struct cas_softc *sc)
2512 if_t ifp = sc->sc_ifp;
2517 CAS_LOCK_ASSERT(sc, MA_OWNED);
2520 * Turn off the RX MAC and the hash filter as required by the Sun
2521 * Cassini programming restrictions.
2523 v = sc->sc_mac_rxcfg & ~(CAS_MAC_RX_CONF_HFILTER |
2524 CAS_MAC_RX_CONF_EN);
2525 CAS_WRITE_4(sc, CAS_MAC_RX_CONF, v);
2526 CAS_BARRIER(sc, CAS_MAC_RX_CONF, 4,
2527 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
2528 if (!cas_bitwait(sc, CAS_MAC_RX_CONF, CAS_MAC_RX_CONF_HFILTER |
2529 CAS_MAC_RX_CONF_EN, 0))
2530 device_printf(sc->sc_dev,
2531 "cannot disable RX MAC or hash filter\n");
2533 v &= ~(CAS_MAC_RX_CONF_PROMISC | CAS_MAC_RX_CONF_PGRP);
2534 if ((if_getflags(ifp) & IFF_PROMISC) != 0) {
2535 v |= CAS_MAC_RX_CONF_PROMISC;
2538 if ((if_getflags(ifp) & IFF_ALLMULTI) != 0) {
2539 v |= CAS_MAC_RX_CONF_PGRP;
2544 * Set up multicast address filter by passing all multicast
2545 * addresses through a crc generator, and then using the high
2546 * order 8 bits as an index into the 256 bit logical address
2547 * filter. The high order 4 bits selects the word, while the
2548 * other 4 bits select the bit within the word (where bit 0
2552 memset(hash, 0, sizeof(hash));
2553 if_foreach_llmaddr(ifp, cas_hash_maddr, &hash);
2555 v |= CAS_MAC_RX_CONF_HFILTER;
2557 /* Now load the hash table into the chip (if we are using it). */
2558 for (i = 0; i < 16; i++)
2560 CAS_MAC_HASH0 + i * (CAS_MAC_HASH1 - CAS_MAC_HASH0),
2564 sc->sc_mac_rxcfg = v;
2565 CAS_WRITE_4(sc, CAS_MAC_RX_CONF, v | CAS_MAC_RX_CONF_EN);
2568 static int cas_pci_attach(device_t dev);
2569 static int cas_pci_detach(device_t dev);
2570 static int cas_pci_probe(device_t dev);
2571 static int cas_pci_resume(device_t dev);
2572 static int cas_pci_suspend(device_t dev);
2574 static device_method_t cas_pci_methods[] = {
2575 /* Device interface */
2576 DEVMETHOD(device_probe, cas_pci_probe),
2577 DEVMETHOD(device_attach, cas_pci_attach),
2578 DEVMETHOD(device_detach, cas_pci_detach),
2579 DEVMETHOD(device_suspend, cas_pci_suspend),
2580 DEVMETHOD(device_resume, cas_pci_resume),
2581 /* Use the suspend handler here, it is all that is required. */
2582 DEVMETHOD(device_shutdown, cas_pci_suspend),
2585 DEVMETHOD(miibus_readreg, cas_mii_readreg),
2586 DEVMETHOD(miibus_writereg, cas_mii_writereg),
2587 DEVMETHOD(miibus_statchg, cas_mii_statchg),
2592 static driver_t cas_pci_driver = {
2595 sizeof(struct cas_softc)
2598 static const struct cas_pci_dev {
2602 const char *cpd_desc;
2603 } cas_pci_devlist[] = {
2604 { 0x0035100b, 0x0, CAS_SATURN, "NS DP83065 Saturn Gigabit Ethernet" },
2605 { 0xabba108e, 0x10, CAS_CASPLUS, "Sun Cassini+ Gigabit Ethernet" },
2606 { 0xabba108e, 0x0, CAS_CAS, "Sun Cassini Gigabit Ethernet" },
2610 DRIVER_MODULE(cas, pci, cas_pci_driver, 0, 0);
2611 MODULE_PNP_INFO("W32:vendor/device", pci, cas, cas_pci_devlist,
2612 nitems(cas_pci_devlist) - 1);
2613 DRIVER_MODULE(miibus, cas, miibus_driver, 0, 0);
2614 MODULE_DEPEND(cas, pci, 1, 1, 1);
2617 cas_pci_probe(device_t dev)
2621 for (i = 0; cas_pci_devlist[i].cpd_desc != NULL; i++) {
2622 if (pci_get_devid(dev) == cas_pci_devlist[i].cpd_devid &&
2623 pci_get_revid(dev) >= cas_pci_devlist[i].cpd_revid) {
2624 device_set_desc(dev, cas_pci_devlist[i].cpd_desc);
2625 return (BUS_PROBE_DEFAULT);
2632 static struct resource_spec cas_pci_res_spec[] = {
2633 { SYS_RES_IRQ, 0, RF_SHAREABLE | RF_ACTIVE }, /* CAS_RES_INTR */
2634 { SYS_RES_MEMORY, PCIR_BAR(0), RF_ACTIVE }, /* CAS_RES_MEM */
2638 #define CAS_LOCAL_MAC_ADDRESS "local-mac-address"
2639 #define CAS_PHY_INTERFACE "phy-interface"
2640 #define CAS_PHY_TYPE "phy-type"
2641 #define CAS_PHY_TYPE_PCS "pcs"
2644 cas_pci_attach(device_t dev)
2646 char buf[sizeof(CAS_LOCAL_MAC_ADDRESS)];
2647 struct cas_softc *sc;
2649 #if !defined(__powerpc__)
2650 u_char enaddr[4][ETHER_ADDR_LEN];
2651 u_int j, k, lma, pcs[4], phy;
2654 sc = device_get_softc(dev);
2655 sc->sc_variant = CAS_UNKNOWN;
2656 for (i = 0; cas_pci_devlist[i].cpd_desc != NULL; i++) {
2657 if (pci_get_devid(dev) == cas_pci_devlist[i].cpd_devid &&
2658 pci_get_revid(dev) >= cas_pci_devlist[i].cpd_revid) {
2659 sc->sc_variant = cas_pci_devlist[i].cpd_variant;
2663 if (sc->sc_variant == CAS_UNKNOWN) {
2664 device_printf(dev, "unknown adaptor\n");
2668 /* PCI configuration */
2669 pci_write_config(dev, PCIR_COMMAND,
2670 pci_read_config(dev, PCIR_COMMAND, 2) | PCIM_CMD_BUSMASTEREN |
2671 PCIM_CMD_MWRICEN | PCIM_CMD_PERRESPEN | PCIM_CMD_SERRESPEN, 2);
2674 if (sc->sc_variant == CAS_CAS && pci_get_devid(dev) < 0x02)
2675 /* Hardware checksumming may hang TX. */
2676 sc->sc_flags |= CAS_NO_CSUM;
2677 if (sc->sc_variant == CAS_CASPLUS || sc->sc_variant == CAS_SATURN)
2678 sc->sc_flags |= CAS_REG_PLUS;
2679 if (sc->sc_variant == CAS_CAS ||
2680 (sc->sc_variant == CAS_CASPLUS && pci_get_revid(dev) < 0x11))
2681 sc->sc_flags |= CAS_TABORT;
2683 device_printf(dev, "flags=0x%x\n", sc->sc_flags);
2685 if (bus_alloc_resources(dev, cas_pci_res_spec, sc->sc_res)) {
2686 device_printf(dev, "failed to allocate resources\n");
2687 bus_release_resources(dev, cas_pci_res_spec, sc->sc_res);
2691 CAS_LOCK_INIT(sc, device_get_nameunit(dev));
2693 #if defined(__powerpc__)
2694 OF_getetheraddr(dev, sc->sc_enaddr);
2695 if (OF_getprop(ofw_bus_get_node(dev), CAS_PHY_INTERFACE, buf,
2696 sizeof(buf)) > 0 || OF_getprop(ofw_bus_get_node(dev),
2697 CAS_PHY_TYPE, buf, sizeof(buf)) > 0) {
2698 buf[sizeof(buf) - 1] = '\0';
2699 if (strcmp(buf, CAS_PHY_TYPE_PCS) == 0)
2700 sc->sc_flags |= CAS_SERDES;
2704 * Dig out VPD (vital product data) and read the MAC address as well
2705 * as the PHY type. The VPD resides in the PCI Expansion ROM (PCI
2706 * FCode) and can't be accessed via the PCI capability pointer.
2707 * SUNW,pci-ce and SUNW,pci-qge use the Enhanced VPD format described
2708 * in the free US Patent 7149820.
2711 #define PCI_ROMHDR_SIZE 0x1c
2712 #define PCI_ROMHDR_SIG 0x00
2713 #define PCI_ROMHDR_SIG_MAGIC 0xaa55 /* little endian */
2714 #define PCI_ROMHDR_PTR_DATA 0x18
2715 #define PCI_ROM_SIZE 0x18
2716 #define PCI_ROM_SIG 0x00
2717 #define PCI_ROM_SIG_MAGIC 0x52494350 /* "PCIR", endian */
2719 #define PCI_ROM_VENDOR 0x04
2720 #define PCI_ROM_DEVICE 0x06
2721 #define PCI_ROM_PTR_VPD 0x08
2722 #define PCI_VPDRES_BYTE0 0x00
2723 #define PCI_VPDRES_ISLARGE(x) ((x) & 0x80)
2724 #define PCI_VPDRES_LARGE_NAME(x) ((x) & 0x7f)
2725 #define PCI_VPDRES_LARGE_LEN_LSB 0x01
2726 #define PCI_VPDRES_LARGE_LEN_MSB 0x02
2727 #define PCI_VPDRES_LARGE_SIZE 0x03
2728 #define PCI_VPDRES_TYPE_ID_STRING 0x02 /* large */
2729 #define PCI_VPDRES_TYPE_VPD 0x10 /* large */
2730 #define PCI_VPD_KEY0 0x00
2731 #define PCI_VPD_KEY1 0x01
2732 #define PCI_VPD_LEN 0x02
2733 #define PCI_VPD_SIZE 0x03
2735 #define CAS_ROM_READ_1(sc, offs) \
2736 CAS_READ_1((sc), CAS_PCI_ROM_OFFSET + (offs))
2737 #define CAS_ROM_READ_2(sc, offs) \
2738 CAS_READ_2((sc), CAS_PCI_ROM_OFFSET + (offs))
2739 #define CAS_ROM_READ_4(sc, offs) \
2740 CAS_READ_4((sc), CAS_PCI_ROM_OFFSET + (offs))
2743 memset(enaddr, 0, sizeof(enaddr));
2744 memset(pcs, 0, sizeof(pcs));
2746 /* Enable PCI Expansion ROM access. */
2747 CAS_WRITE_4(sc, CAS_BIM_LDEV_OEN,
2748 CAS_BIM_LDEV_OEN_PAD | CAS_BIM_LDEV_OEN_PROM);
2750 /* Read PCI Expansion ROM header. */
2751 if (CAS_ROM_READ_2(sc, PCI_ROMHDR_SIG) != PCI_ROMHDR_SIG_MAGIC ||
2752 (i = CAS_ROM_READ_2(sc, PCI_ROMHDR_PTR_DATA)) <
2754 device_printf(dev, "unexpected PCI Expansion ROM header\n");
2758 /* Read PCI Expansion ROM data. */
2759 if (CAS_ROM_READ_4(sc, i + PCI_ROM_SIG) != PCI_ROM_SIG_MAGIC ||
2760 CAS_ROM_READ_2(sc, i + PCI_ROM_VENDOR) != pci_get_vendor(dev) ||
2761 CAS_ROM_READ_2(sc, i + PCI_ROM_DEVICE) != pci_get_device(dev) ||
2762 (j = CAS_ROM_READ_2(sc, i + PCI_ROM_PTR_VPD)) <
2764 device_printf(dev, "unexpected PCI Expansion ROM data\n");
2770 if (PCI_VPDRES_ISLARGE(CAS_ROM_READ_1(sc,
2771 j + PCI_VPDRES_BYTE0)) == 0) {
2772 device_printf(dev, "no large PCI VPD\n");
2776 i = (CAS_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_LEN_MSB) << 8) |
2777 CAS_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_LEN_LSB);
2778 switch (PCI_VPDRES_LARGE_NAME(CAS_ROM_READ_1(sc,
2779 j + PCI_VPDRES_BYTE0))) {
2780 case PCI_VPDRES_TYPE_ID_STRING:
2781 /* Skip identifier string. */
2782 j += PCI_VPDRES_LARGE_SIZE + i;
2784 case PCI_VPDRES_TYPE_VPD:
2785 for (j += PCI_VPDRES_LARGE_SIZE; i > 0;
2786 i -= PCI_VPD_SIZE + CAS_ROM_READ_1(sc, j + PCI_VPD_LEN),
2787 j += PCI_VPD_SIZE + CAS_ROM_READ_1(sc, j + PCI_VPD_LEN)) {
2788 if (CAS_ROM_READ_1(sc, j + PCI_VPD_KEY0) != 'Z')
2789 /* no Enhanced VPD */
2791 if (CAS_ROM_READ_1(sc, j + PCI_VPD_SIZE) != 'I')
2792 /* no instance property */
2794 if (CAS_ROM_READ_1(sc, j + PCI_VPD_SIZE + 3) == 'B') {
2796 if (CAS_ROM_READ_1(sc,
2797 j + PCI_VPD_SIZE + 4) != ETHER_ADDR_LEN)
2799 bus_read_region_1(sc->sc_res[CAS_RES_MEM],
2800 CAS_PCI_ROM_OFFSET + j + PCI_VPD_SIZE + 5,
2802 buf[sizeof(buf) - 1] = '\0';
2803 if (strcmp(buf, CAS_LOCAL_MAC_ADDRESS) != 0)
2805 bus_read_region_1(sc->sc_res[CAS_RES_MEM],
2806 CAS_PCI_ROM_OFFSET + j + PCI_VPD_SIZE +
2807 5 + sizeof(CAS_LOCAL_MAC_ADDRESS),
2808 enaddr[lma], sizeof(enaddr[lma]));
2810 if (lma == 4 && phy == 4)
2812 } else if (CAS_ROM_READ_1(sc, j + PCI_VPD_SIZE + 3) ==
2815 if (CAS_ROM_READ_1(sc,
2816 j + PCI_VPD_SIZE + 4) !=
2817 sizeof(CAS_PHY_TYPE_PCS))
2819 bus_read_region_1(sc->sc_res[CAS_RES_MEM],
2820 CAS_PCI_ROM_OFFSET + j + PCI_VPD_SIZE + 5,
2822 buf[sizeof(buf) - 1] = '\0';
2823 if (strcmp(buf, CAS_PHY_INTERFACE) == 0)
2824 k = sizeof(CAS_PHY_INTERFACE);
2825 else if (strcmp(buf, CAS_PHY_TYPE) == 0)
2826 k = sizeof(CAS_PHY_TYPE);
2829 bus_read_region_1(sc->sc_res[CAS_RES_MEM],
2830 CAS_PCI_ROM_OFFSET + j + PCI_VPD_SIZE +
2831 5 + k, buf, sizeof(buf));
2832 buf[sizeof(buf) - 1] = '\0';
2833 if (strcmp(buf, CAS_PHY_TYPE_PCS) == 0)
2836 if (lma == 4 && phy == 4)
2842 device_printf(dev, "unexpected PCI VPD\n");
2847 CAS_WRITE_4(sc, CAS_BIM_LDEV_OEN, 0);
2850 device_printf(dev, "could not determine Ethernet address\n");
2854 if (lma > 1 && pci_get_slot(dev) < nitems(enaddr))
2855 i = pci_get_slot(dev);
2856 memcpy(sc->sc_enaddr, enaddr[i], ETHER_ADDR_LEN);
2859 device_printf(dev, "could not determine PHY type\n");
2863 if (phy > 1 && pci_get_slot(dev) < nitems(pcs))
2864 i = pci_get_slot(dev);
2866 sc->sc_flags |= CAS_SERDES;
2869 if (cas_attach(sc) != 0) {
2870 device_printf(dev, "could not be attached\n");
2874 if (bus_setup_intr(dev, sc->sc_res[CAS_RES_INTR], INTR_TYPE_NET |
2875 INTR_MPSAFE, cas_intr, NULL, sc, &sc->sc_ih) != 0) {
2876 device_printf(dev, "failed to set up interrupt\n");
2883 CAS_LOCK_DESTROY(sc);
2884 bus_release_resources(dev, cas_pci_res_spec, sc->sc_res);
2889 cas_pci_detach(device_t dev)
2891 struct cas_softc *sc;
2893 sc = device_get_softc(dev);
2894 bus_teardown_intr(dev, sc->sc_res[CAS_RES_INTR], sc->sc_ih);
2896 CAS_LOCK_DESTROY(sc);
2897 bus_release_resources(dev, cas_pci_res_spec, sc->sc_res);
2902 cas_pci_suspend(device_t dev)
2905 cas_suspend(device_get_softc(dev));
2910 cas_pci_resume(device_t dev)
2913 cas_resume(device_get_softc(dev));