1 /* $FreeBSD: src/sys/pci/if_wx.c,v 1.5.2.12 2003/03/05 18:42:34 njl Exp $ */
2 /* $DragonFly: src/sys/dev/netif/wx/Attic/if_wx.c,v 1.6 2004/01/06 01:40:50 dillon Exp $ */
4 * Principal Author: Matthew Jacob <mjacob@feral.com>
5 * Copyright (c) 1999, 2001 by Traakan Software
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 unmodified, this list of conditions, and the following
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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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 * Additional Copyright (c) 2001 by Parag Patel
31 * under same licence for MII PHY code.
35 * Intel Gigabit Ethernet (82452/82453) Driver.
36 * Inspired by fxp driver by David Greenman for FreeBSD, and by
37 * Bill Paul's work in other FreeBSD network drivers.
41 * Many bug fixes gratefully acknowledged from:
43 * The folks at Sitara Networks
51 * Use only every other 16 byte receive descriptor, leaving the ones
52 * in between empty. This card is most efficient at reading/writing
53 * 32 byte cache lines, so avoid all the (not working for early rev
54 * cards) MWI and/or READ/MODIFY/WRITE cycles updating one descriptor
57 * This isn't debugged yet.
59 /* #define PADDED_CELL 1 */
62 * Since the includes are a mess, they'll all be in if_wxvar.h
69 #define vtophys(va) alpha_XXX_dmamap((vm_offset_t)(va))
70 #endif /* __alpha__ */
73 * Function Prototpes, yadda yadda...
76 static int wx_intr(void *);
77 static void wx_handle_link_intr(wx_softc_t *);
78 static void wx_check_link(wx_softc_t *);
79 static void wx_handle_rxint(wx_softc_t *);
80 static void wx_gc(wx_softc_t *);
81 static void wx_start(struct ifnet *);
82 static int wx_ioctl(struct ifnet *, IOCTL_CMD_TYPE, caddr_t);
83 static int wx_ifmedia_upd(struct ifnet *);
84 static void wx_ifmedia_sts(struct ifnet *, struct ifmediareq *);
85 static int wx_init(void *);
86 static void wx_hw_stop(wx_softc_t *);
87 static void wx_set_addr(wx_softc_t *, int, u_int8_t *);
88 static int wx_hw_initialize(wx_softc_t *);
89 static void wx_stop(wx_softc_t *);
90 static void wx_txwatchdog(struct ifnet *);
91 static int wx_get_rbuf(wx_softc_t *, rxpkt_t *);
92 static void wx_rxdma_map(wx_softc_t *, rxpkt_t *, struct mbuf *);
94 static INLINE void wx_eeprom_raise_clk(wx_softc_t *, u_int32_t);
95 static INLINE void wx_eeprom_lower_clk(wx_softc_t *, u_int32_t);
96 static INLINE void wx_eeprom_sobits(wx_softc_t *, u_int16_t, u_int16_t);
97 static INLINE u_int16_t wx_eeprom_sibits(wx_softc_t *);
98 static INLINE void wx_eeprom_cleanup(wx_softc_t *);
99 static INLINE u_int16_t wx_read_eeprom_word(wx_softc_t *, int);
100 static void wx_read_eeprom(wx_softc_t *, u_int16_t *, int, int);
102 static int wx_attach_common(wx_softc_t *);
103 static void wx_watchdog(void *);
105 static INLINE void wx_mwi_whackon(wx_softc_t *);
106 static INLINE void wx_mwi_unwhack(wx_softc_t *);
107 static int wx_dring_setup(wx_softc_t *);
108 static void wx_dring_teardown(wx_softc_t *);
110 static int wx_attach_phy(wx_softc_t *);
111 static int wx_miibus_readreg(void *, int, int);
112 static int wx_miibus_writereg(void *, int, int, int);
113 static void wx_miibus_statchg(void *);
114 static void wx_miibus_mediainit(void *);
116 static u_int32_t wx_mii_shift_in(wx_softc_t *);
117 static void wx_mii_shift_out(wx_softc_t *, u_int32_t, u_int32_t);
119 #define WX_DISABLE_INT(sc) WRITE_CSR(sc, WXREG_IMCLR, WXDISABLE)
120 #define WX_ENABLE_INT(sc) WRITE_CSR(sc, WXREG_IMASK, sc->wx_ienable)
123 * Until we do a bit more work, we can get no bigger than MCLBYTES
126 #define WX_MAXMTU (WX_MAX_PKT_SIZE_JUMBO - sizeof (struct ether_header))
128 #define WX_MAXMTU (MCLBYTES - sizeof (struct ether_header))
131 #define DPRINTF(sc, x) if (sc->wx_debug) printf x
132 #define IPRINTF(sc, x) if (sc->wx_verbose) printf x
134 static const char ldn[] = "%s: link down\n";
135 static const char lup[] = "%s: link up\n";
136 static const char sqe[] = "%s: receive sequence error\n";
137 static const char ane[] = "%s: /C/ ordered sets seen- enabling ANE\n";
138 static const char inane[] = "%s: no /C/ ordered sets seen- disabling ANE\n";
140 static int wx_txint_delay = 5000; /* ~5ms */
141 TUNABLE_INT("hw.wx.txint_delay", &wx_txint_delay);
143 SYSCTL_NODE(_hw, OID_AUTO, wx, CTLFLAG_RD, 0, "WX driver parameters");
144 SYSCTL_INT(_hw_wx, OID_AUTO, txint_delay, CTLFLAG_RW,
145 &wx_txint_delay, 0, "");
146 static int wx_dump_stats = -1;
147 SYSCTL_INT(_hw_wx, OID_AUTO, dump_stats, CTLFLAG_RW,
148 &wx_dump_stats, 0, "");
149 static int wx_clr_stats = -1;
150 SYSCTL_INT(_hw_wx, OID_AUTO, clear_stats, CTLFLAG_RW,
151 &wx_clr_stats, 0, "");
154 * Program multicast addresses.
156 * This function must be called at splimp, but it may sleep.
159 wx_mc_setup(wx_softc_t *sc)
161 struct ifnet *ifp = &sc->wx_if;
162 struct ifmultiaddr *ifma;
165 * XXX: drain TX queue
173 if ((ifp->if_flags & IFF_ALLMULTI) || (ifp->if_flags & IFF_PROMISC)) {
175 return (wx_init(sc));
179 for (ifma = ifp->if_multiaddrs.lh_first, sc->wx_nmca = 0;
180 ifma != NULL; ifma = ifma->ifma_link.le_next) {
182 if (ifma->ifma_addr->sa_family != AF_LINK) {
185 if (sc->wx_nmca >= WX_RAL_TAB_SIZE-1) {
190 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
191 (void *) &sc->wx_mcaddr[sc->wx_nmca++][0], 6);
193 return (wx_init(sc));
197 * Return identification string if this is device is ours.
200 wx_probe(device_t dev)
202 if (pci_get_vendor(dev) != WX_VENDOR_INTEL) {
205 switch (pci_get_device(dev)) {
206 case WX_PRODUCT_82452:
207 device_set_desc(dev, "Intel PRO/1000 Gigabit (WISEMAN)");
209 case WX_PRODUCT_LIVENGOOD:
210 device_set_desc(dev, "Intel PRO/1000 (LIVENGOOD)");
212 case WX_PRODUCT_82452_SC:
213 device_set_desc(dev, "Intel PRO/1000 F Gigabit Ethernet");
215 case WX_PRODUCT_82543:
216 device_set_desc(dev, "Intel PRO/1000 T Gigabit Ethernet");
225 wx_attach(device_t dev)
228 wx_softc_t *sc = device_get_softc(dev);
233 bzero(sc, sizeof (wx_softc_t));
235 callout_handle_init(&sc->w.sch);
241 if (getenv_int ("wx_debug", &rid)) {
242 if (rid & (1 << device_get_unit(dev))) {
247 if (getenv_int("wx_no_ilos", &rid)) {
248 if (rid & (1 << device_get_unit(dev))) {
253 if (getenv_int("wx_ilos", &rid)) {
254 if (rid & (1 << device_get_unit(dev))) {
259 if (getenv_int("wx_no_flow", &rid)) {
260 if (rid & (1 << device_get_unit(dev))) {
266 mtx_init(&sc->wx_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE);
270 * get revision && id...
272 sc->wx_idnrev = (pci_get_device(dev) << 16) | (pci_get_revid(dev));
275 * Enable bus mastering, make sure that the cache line size is right.
277 pci_enable_busmaster(dev);
278 pci_enable_io(dev, SYS_RES_MEMORY);
279 val = pci_read_config(dev, PCIR_COMMAND, 4);
280 if ((val & PCIM_CMD_MEMEN) == 0) {
281 device_printf(dev, "failed to enable memory mapping\n");
287 * Let the BIOS do it's job- but check for sanity.
289 val = pci_read_config(dev, PCIR_CACHELNSZ, 1);
290 if (val < 4 || val > 32) {
291 pci_write_config(dev, PCIR_CACHELNSZ, 8, 1);
295 * Map control/status registers.
298 sc->w.mem = bus_alloc_resource(dev, SYS_RES_MEMORY,
299 &rid, 0, ~0, 1, RF_ACTIVE);
301 device_printf(dev, "could not map memory\n");
305 sc->w.st = rman_get_bustag(sc->w.mem);
306 sc->w.sh = rman_get_bushandle(sc->w.mem);
309 sc->w.irq = bus_alloc_resource(dev, SYS_RES_IRQ,
310 &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
311 if (sc->w.irq == NULL) {
312 device_printf(dev, "could not map interrupt\n");
316 error = bus_setup_intr(dev, sc->w.irq, INTR_TYPE_NET,
317 (void (*)(void *))wx_intr, sc, &sc->w.ih);
319 device_printf(dev, "could not setup irq\n");
322 (void) snprintf(sc->wx_name, sizeof (sc->wx_name) - 1, "wx%d",
323 device_get_unit(dev));
324 if (wx_attach_common(sc)) {
325 bus_teardown_intr(dev, sc->w.irq, sc->w.ih);
326 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->w.irq);
327 bus_release_resource(dev, SYS_RES_MEMORY, WX_MMBA, sc->w.mem);
331 device_printf(dev, "Ethernet address %02x:%02x:%02x:%02x:%02x:%02x\n",
332 sc->w.arpcom.ac_enaddr[0], sc->w.arpcom.ac_enaddr[1],
333 sc->w.arpcom.ac_enaddr[2], sc->w.arpcom.ac_enaddr[3],
334 sc->w.arpcom.ac_enaddr[4], sc->w.arpcom.ac_enaddr[5]);
336 ifp = &sc->w.arpcom.ac_if;
337 if_initname(ifp, "wx", device_get_unit(dev));
338 ifp->if_mtu = ETHERMTU; /* we always start at ETHERMTU size */
339 ifp->if_output = ether_output;
340 ifp->if_baudrate = 1000000000;
341 ifp->if_init = (void (*)(void *))wx_init;
343 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
344 ifp->if_ioctl = wx_ioctl;
345 ifp->if_start = wx_start;
346 ifp->if_watchdog = wx_txwatchdog;
347 ifp->if_snd.ifq_maxlen = WX_MAX_TDESC - 1;
348 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
355 wx_attach_phy(wx_softc_t *sc)
357 if (mii_phy_probe(sc->w.dev, &sc->w.miibus, wx_ifmedia_upd,
359 printf("%s: no PHY probed!\n", sc->wx_name);
367 wx_detach(device_t dev)
369 wx_softc_t *sc = device_get_softc(dev);
374 ether_ifdetach(&sc->w.arpcom.ac_if, ETHER_BPF_SUPPORTED);
376 bus_generic_detach(dev);
377 device_delete_child(dev, sc->w.miibus);
379 ifmedia_removeall(&sc->wx_media);
381 bus_teardown_intr(dev, sc->w.irq, sc->w.ih);
382 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->w.irq);
383 bus_release_resource(dev, SYS_RES_MEMORY, WX_MMBA, sc->w.mem);
385 wx_dring_teardown(sc);
396 mtx_destroy(&sc->wx_mtx);
402 wx_shutdown(device_t dev)
404 wx_hw_stop((wx_softc_t *) device_get_softc(dev));
409 wx_mwi_whackon(wx_softc_t *sc)
411 sc->wx_cmdw = pci_read_config(sc->w.dev, PCIR_COMMAND, 2);
412 pci_write_config(sc->w.dev, PCIR_COMMAND, sc->wx_cmdw & ~MWI, 2);
416 wx_mwi_unwhack(wx_softc_t *sc)
418 if (sc->wx_cmdw & MWI) {
419 pci_write_config(sc->w.dev, PCIR_COMMAND, sc->wx_cmdw, 2);
424 wx_dring_setup(wx_softc_t *sc)
428 len = sizeof (wxrd_t) * WX_MAX_RDESC;
429 sc->rdescriptors = (wxrd_t *)
430 contigmalloc(len, M_DEVBUF, M_NOWAIT, 0, ~0, 4096, 0);
431 if (sc->rdescriptors == NULL) {
432 printf("%s: could not allocate rcv descriptors\n", sc->wx_name);
435 if (((intptr_t)sc->rdescriptors) & 0xfff) {
436 contigfree(sc->rdescriptors, len, M_DEVBUF);
437 sc->rdescriptors = NULL;
438 printf("%s: rcv descriptors not 4KB aligned\n", sc->wx_name);
441 bzero(sc->rdescriptors, len);
443 len = sizeof (wxtd_t) * WX_MAX_TDESC;
444 sc->tdescriptors = (wxtd_t *)
445 contigmalloc(len, M_DEVBUF, M_NOWAIT, 0, ~0, 4096, 0);
446 if (sc->tdescriptors == NULL) {
447 contigfree(sc->rdescriptors,
448 sizeof (wxrd_t) * WX_MAX_RDESC, M_DEVBUF);
449 sc->rdescriptors = NULL;
450 printf("%s: could not allocate xmt descriptors\n", sc->wx_name);
453 if (((intptr_t)sc->tdescriptors) & 0xfff) {
454 contigfree(sc->rdescriptors,
455 sizeof (wxrd_t) * WX_MAX_RDESC, M_DEVBUF);
456 contigfree(sc->tdescriptors, len, M_DEVBUF);
457 sc->rdescriptors = NULL;
458 sc->tdescriptors = NULL;
459 printf("%s: xmt descriptors not 4KB aligned\n", sc->wx_name);
462 bzero(sc->tdescriptors, len);
467 wx_dring_teardown(wx_softc_t *sc)
469 if (sc->rdescriptors) {
470 contigfree(sc->rdescriptors,
471 sizeof (wxrd_t) * WX_MAX_RDESC, M_DEVBUF);
472 sc->rdescriptors = NULL;
474 if (sc->tdescriptors) {
475 contigfree(sc->tdescriptors,
476 sizeof (wxtd_t) * WX_MAX_TDESC, M_DEVBUF);
477 sc->tdescriptors = NULL;
481 static device_method_t wx_methods[] = {
482 /* Device interface */
483 DEVMETHOD(device_probe, wx_probe),
484 DEVMETHOD(device_attach, wx_attach),
485 DEVMETHOD(device_detach, wx_detach),
486 DEVMETHOD(device_shutdown, wx_shutdown),
489 DEVMETHOD(bus_print_child, bus_generic_print_child),
490 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
493 DEVMETHOD(miibus_readreg, wx_miibus_readreg),
494 DEVMETHOD(miibus_writereg, wx_miibus_writereg),
495 DEVMETHOD(miibus_statchg, wx_miibus_statchg),
496 DEVMETHOD(miibus_mediainit, wx_miibus_mediainit),
501 static driver_t wx_driver = {
502 "wx", wx_methods, sizeof(wx_softc_t),
504 static devclass_t wx_devclass;
506 DECLARE_DUMMY_MODULE(if_wx);
507 MODULE_DEPEND(if_wx, miibus, 1, 1, 1);
508 DRIVER_MODULE(if_wx, pci, wx_driver, wx_devclass, 0, 0);
509 DRIVER_MODULE(miibus, wx, miibus_driver, miibus_devclass, 0, 0);
512 * Do generic parts of attach. Our registers have been mapped
513 * and our interrupt registered.
516 wx_attach_common(wx_softc_t *sc)
523 * First, check for revision support.
525 if (sc->wx_idnrev < WX_WISEMAN_2_0) {
526 printf("%s: cannot support ID 0x%x, revision %d chips\n",
527 sc->wx_name, sc->wx_idnrev >> 16, sc->wx_idnrev & 0xffff);
532 * Second, reset the chip.
537 * Third, validate our EEPROM.
543 * Fourth, read eeprom for our MAC address and other things.
545 wx_read_eeprom(sc, (u_int16_t *)sc->wx_enaddr, WX_EEPROM_MAC_OFF, 3);
548 * Fifth, establish some adapter parameters.
552 if (IS_LIVENGOOD_CU(sc)) {
554 /* settings to talk to PHY */
555 sc->wx_dcr |= WXDCR_FRCSPD | WXDCR_FRCDPX | WXDCR_SLU;
556 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
559 * Raise the PHY's reset line to make it operational.
561 tmp = READ_CSR(sc, WXREG_EXCT);
562 tmp |= WXPHY_RESET_DIR4;
563 WRITE_CSR(sc, WXREG_EXCT, tmp);
566 tmp = READ_CSR(sc, WXREG_EXCT);
567 tmp &= ~WXPHY_RESET4;
568 WRITE_CSR(sc, WXREG_EXCT, tmp);
571 tmp = READ_CSR(sc, WXREG_EXCT);
573 WRITE_CSR(sc, WXREG_EXCT, tmp);
576 if (wx_attach_phy(sc)) {
580 ifmedia_init(&sc->wx_media, IFM_IMASK,
581 wx_ifmedia_upd, wx_ifmedia_sts);
583 ifmedia_add(&sc->wx_media, IFM_ETHER|IFM_1000_SX, 0, NULL);
584 ifmedia_add(&sc->wx_media,
585 IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
586 ifmedia_set(&sc->wx_media, IFM_ETHER|IFM_1000_SX|IFM_FDX);
588 sc->wx_media.ifm_media = sc->wx_media.ifm_cur->ifm_media;
592 * Sixth, establish a default device control register word.
595 if (sc->wx_cfg1 & WX_EEPROM_CTLR1_FD)
596 sc->wx_dcr |= WXDCR_FD;
597 if (sc->wx_cfg1 & WX_EEPROM_CTLR1_ILOS)
598 sc->wx_dcr |= WXDCR_ILOS;
600 tmp = (sc->wx_cfg1 >> WX_EEPROM_CTLR1_SWDPIO_SHIFT) & WXDCR_SWDPIO_MASK;
601 sc->wx_dcr |= (tmp << WXDCR_SWDPIO_SHIFT);
604 sc->wx_dcr &= ~WXDCR_ILOS;
606 sc->wx_dcr |= WXDCR_ILOS;
607 if (sc->wx_no_flow == 0)
608 sc->wx_dcr |= WXDCR_RFCE | WXDCR_TFCE;
611 * Seventh, allocate various sw structures...
613 len = sizeof (rxpkt_t) * WX_MAX_RDESC;
614 sc->rbase = (rxpkt_t *) WXMALLOC(len);
615 if (sc->rbase == NULL) {
618 bzero(sc->rbase, len);
621 len = sizeof (txpkt_t) * WX_MAX_TDESC;
622 sc->tbase = (txpkt_t *) WXMALLOC(len);
623 if (sc->tbase == NULL) {
626 bzero(sc->tbase, len);
630 * Eighth, allocate and dma map (platform dependent) descriptor rings.
631 * They have to be aligned on a 4KB boundary.
633 if (wx_dring_setup(sc) == 0) {
638 printf("%s: failed to do common attach (%d)\n", sc->wx_name, ll);
639 wx_dring_teardown(sc);
656 wx_eeprom_raise_clk(wx_softc_t *sc, u_int32_t regval)
658 WRITE_CSR(sc, WXREG_EECDR, regval | WXEECD_SK);
663 wx_eeprom_lower_clk(wx_softc_t *sc, u_int32_t regval)
665 WRITE_CSR(sc, WXREG_EECDR, regval & ~WXEECD_SK);
670 wx_eeprom_sobits(wx_softc_t *sc, u_int16_t data, u_int16_t count)
672 u_int32_t regval, mask;
674 mask = 1 << (count - 1);
675 regval = READ_CSR(sc, WXREG_EECDR) & ~(WXEECD_DI|WXEECD_DO);
681 regval &= ~WXEECD_DI;
682 WRITE_CSR(sc, WXREG_EECDR, regval); DELAY(50);
683 wx_eeprom_raise_clk(sc, regval);
684 wx_eeprom_lower_clk(sc, regval);
687 WRITE_CSR(sc, WXREG_EECDR, regval & ~WXEECD_DI);
690 static INLINE u_int16_t
691 wx_eeprom_sibits(wx_softc_t *sc)
693 unsigned int regval, i;
697 regval = READ_CSR(sc, WXREG_EECDR) & ~(WXEECD_DI|WXEECD_DO);
698 for (i = 0; i != 16; i++) {
700 wx_eeprom_raise_clk(sc, regval);
701 regval = READ_CSR(sc, WXREG_EECDR) & ~WXEECD_DI;
702 if (regval & WXEECD_DO) {
705 wx_eeprom_lower_clk(sc, regval);
711 wx_eeprom_cleanup(wx_softc_t *sc)
714 regval = READ_CSR(sc, WXREG_EECDR) & ~(WXEECD_DI|WXEECD_CS);
715 WRITE_CSR(sc, WXREG_EECDR, regval); DELAY(50);
716 wx_eeprom_raise_clk(sc, regval);
717 wx_eeprom_lower_clk(sc, regval);
720 static u_int16_t INLINE
721 wx_read_eeprom_word(wx_softc_t *sc, int offset)
724 WRITE_CSR(sc, WXREG_EECDR, WXEECD_CS);
725 wx_eeprom_sobits(sc, EEPROM_READ_OPCODE, 3);
726 wx_eeprom_sobits(sc, offset, 6);
727 data = wx_eeprom_sibits(sc);
728 wx_eeprom_cleanup(sc);
733 wx_read_eeprom(wx_softc_t *sc, u_int16_t *data, int offset, int words)
736 for (i = 0; i < words; i++) {
737 *data++ = wx_read_eeprom_word(sc, offset++);
739 sc->wx_cfg1 = wx_read_eeprom_word(sc, WX_EEPROM_CTLR1_OFF);
743 * Start packet transmission on the interface.
747 wx_start(struct ifnet *ifp)
749 wx_softc_t *sc = SOFTC_IFP(ifp);
750 u_int16_t widx = WX_MAX_TDESC, cidx, nactv;
753 DPRINTF(sc, ("%s: wx_start\n", sc->wx_name));
755 while (nactv < WX_MAX_TDESC - 1) {
758 struct mbuf *m, *mb_head;
760 IF_DEQUEUE(&ifp->if_snd, mb_head);
761 if (mb_head == NULL) {
767 * If we have a packet less than ethermin, pad it out.
769 if (mb_head->m_pkthdr.len < WX_MIN_RPKT_SIZE) {
770 if (mb_head->m_next == NULL) {
771 mb_head->m_len = WX_MIN_RPKT_SIZE;
773 MGETHDR(m, M_DONTWAIT, MT_DATA);
778 m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
780 m->m_pkthdr.len = m->m_len = WX_MIN_RPKT_SIZE;
781 bzero(mtod(m, char *) + mb_head->m_pkthdr.len,
782 WX_MIN_RPKT_SIZE - mb_head->m_pkthdr.len);
794 * Go through each of the mbufs in the chain and initialize
795 * the transmit buffer descriptors with the physical address
796 * and size of that mbuf. If we have a length less than our
797 * minimum transmit size, we bail (to do a pullup). If we run
798 * out of descriptors, we also bail and try and do a pullup.
800 for (plen = ndesc = 0, m = mb_head; m != NULL; m = m->m_next) {
805 * If this mbuf has no data, skip it.
812 * This appears to be a bogus check the PRO1000T.
813 * I think they meant that the minimum packet size
814 * is in fact WX_MIN_XPKT_SIZE (all data loaded)
818 * If this mbuf is too small for the chip's minimum,
819 * break out to cluster it.
821 if (m->m_len < WX_MIN_XPKT_SIZE) {
828 * Do we have a descriptor available for this mbuf?
830 if (++nactv == WX_MAX_TDESC) {
831 if (gctried++ == 0) {
838 sc->tbase[cidx].dptr = m;
839 td = &sc->tdescriptors[cidx];
840 td->length = m->m_len;
843 vptr = mtod(m, vm_offset_t);
844 td->address.highpart = 0;
845 td->address.lowpart = vtophys(vptr);
854 printf("%s: XMIT[%d] %p vptr %lx (length %d "
855 "DMA addr %x) idx %d\n", sc->wx_name,
856 ndesc, m, (long) vptr, td->length,
857 td->address.lowpart, cidx);
860 cidx = T_NXT_IDX(cidx);
864 * If we get here and m is NULL, we can send
865 * the the packet chain described by mb_head.
869 * Mark the last descriptor with EOP and tell the
870 * chip to insert a final checksum.
872 wxtd_t *td = &sc->tdescriptors[T_PREV_IDX(cidx)];
873 td->cmd = TXCMD_EOP|TXCMD_IFCS;
875 * Set up a delayed interrupt when this packet
876 * is sent and the descriptor written back.
877 * Additional packets completing will cause
878 * interrupt to be delayed further. Therefore,
879 * after the *last* packet is sent, after the delay
880 * period in TIDV, an interrupt will be generated
881 * which will cause us to garbage collect.
883 td->cmd |= TXCMD_IDE|TXCMD_RPS;
886 * Don't xmit odd length packets.
887 * We're okay with bumping things
888 * up as long as our mbuf allocation
889 * is always larger than our MTU
890 * by a comfortable amount.
892 * Yes, it's a hole to run past the end
900 sc->tbase[sc->tnxtfree].sidx = sc->tnxtfree;
901 sc->tbase[sc->tnxtfree].eidx = cidx;
902 sc->tbase[sc->tnxtfree].next = NULL;
904 sc->tbsyl->next = &sc->tbase[sc->tnxtfree];
906 sc->tbsyf = &sc->tbase[sc->tnxtfree];
908 sc->tbsyl = &sc->tbase[sc->tnxtfree];
913 bpf_mtap(WX_BPFTAP_ARG(ifp), mb_head);
914 /* defer xmit until we've got them all */
920 * Otherwise, we couldn't send this packet for some reason.
922 * If don't have a descriptor available, and this is a
923 * single mbuf packet, freeze output so that later we
924 * can restart when we have more room. Otherwise, we'll
925 * try and cluster the request. We've already tried to
926 * garbage collect completed descriptors.
928 if (nactv == WX_MAX_TDESC && mb_head->m_next == NULL) {
929 sc->wx_xmitputback++;
930 ifp->if_flags |= IFF_OACTIVE;
931 IF_PREPEND(&ifp->if_snd, mb_head);
936 * Otherwise, it's either a fragment length somewhere in the
937 * chain that isn't at least WX_MIN_XPKT_SIZE in length or
938 * the number of fragments exceeds the number of descriptors
941 * We could try a variety of strategies here- if this is
942 * a length problem for single mbuf packet or a length problem
943 * for the last mbuf in a chain (we could just try and adjust
944 * it), but it's just simpler to try and cluster it.
946 MGETHDR(m, M_DONTWAIT, MT_DATA);
951 MCLGET(m, M_DONTWAIT);
952 if ((m->m_flags & M_EXT) == 0) {
957 m_copydata(mb_head, 0, mb_head->m_pkthdr.len, mtod(m, caddr_t));
958 m->m_pkthdr.len = m->m_len = mb_head->m_pkthdr.len;
961 sc->wx_xmitcluster++;
965 if (widx < WX_MAX_TDESC) {
966 if (IS_WISEMAN(sc)) {
967 WRITE_CSR(sc, WXREG_TDT, widx);
969 WRITE_CSR(sc, WXREG_TDT_LIVENGOOD, widx);
973 if (sc->tactive == WX_MAX_TDESC - 1) {
976 if (sc->tactive >= WX_MAX_TDESC - 1) {
977 sc->wx_xmitblocked++;
978 ifp->if_flags |= IFF_OACTIVE;
982 /* used SW LED to indicate transmission active */
983 if (sc->tactive > 0 && sc->wx_mii) {
984 WRITE_CSR(sc, WXREG_DCR,
985 READ_CSR(sc, WXREG_DCR) | (WXDCR_SWDPIO0|WXDCR_SWDPIN0));
991 * Process interface interrupts.
996 wx_softc_t *sc = arg;
1001 * Read interrupt cause register. Reading it clears bits.
1003 sc->wx_icr = READ_CSR(sc, WXREG_ICR);
1008 if (sc->wx_icr & (WXISR_LSC|WXISR_RXSEQ|WXISR_GPI_EN1)) {
1010 wx_handle_link_intr(sc);
1012 wx_handle_rxint(sc);
1013 if (sc->wx_icr & WXISR_TXDW) {
1018 if (sc->wx_icr & WXISR_TXQE) {
1023 if (sc->wx_if.if_snd.ifq_head != NULL) {
1024 wx_start(&sc->wx_if);
1033 wx_handle_link_intr(wx_softc_t *sc)
1035 u_int32_t txcw, rxcw, dcr, dsr;
1038 dcr = READ_CSR(sc, WXREG_DCR);
1039 DPRINTF(sc, ("%s: handle_link_intr: icr=%#x dcr=%#x\n",
1040 sc->wx_name, sc->wx_icr, dcr));
1042 mii_data_t *mii = WX_MII_FROM_SOFTC(sc);
1044 if (mii->mii_media_status & IFM_ACTIVE) {
1045 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE) {
1046 IPRINTF(sc, (ldn, sc->wx_name));
1049 IPRINTF(sc, (lup, sc->wx_name));
1052 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
1053 } else if (sc->wx_icr & WXISR_RXSEQ) {
1054 DPRINTF(sc, (sqe, sc->wx_name));
1059 txcw = READ_CSR(sc, WXREG_XMIT_CFGW);
1060 rxcw = READ_CSR(sc, WXREG_RECV_CFGW);
1061 dsr = READ_CSR(sc, WXREG_DSR);
1064 * If we have LOS or are now receiving Ordered Sets and are not
1065 * doing auto-negotiation, restore autonegotiation.
1068 if (((dcr & WXDCR_SWDPIN1) || (rxcw & WXRXCW_C)) &&
1069 ((txcw & WXTXCW_ANE) == 0)) {
1070 DPRINTF(sc, (ane, sc->wx_name));
1071 WRITE_CSR(sc, WXREG_XMIT_CFGW, WXTXCW_DEFAULT);
1072 sc->wx_dcr &= ~WXDCR_SLU;
1073 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
1077 if (sc->wx_icr & WXISR_LSC) {
1078 if (READ_CSR(sc, WXREG_DSR) & WXDSR_LU) {
1079 IPRINTF(sc, (lup, sc->wx_name));
1081 sc->wx_dcr |= (WXDCR_SWDPIO0|WXDCR_SWDPIN0);
1083 IPRINTF(sc, (ldn, sc->wx_name));
1085 sc->wx_dcr &= ~(WXDCR_SWDPIO0|WXDCR_SWDPIN0);
1087 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
1089 DPRINTF(sc, (sqe, sc->wx_name));
1094 wx_check_link(wx_softc_t *sc)
1096 u_int32_t rxcw, dcr, dsr;
1099 mii_pollstat(WX_MII_FROM_SOFTC(sc));
1103 rxcw = READ_CSR(sc, WXREG_RECV_CFGW);
1104 dcr = READ_CSR(sc, WXREG_DCR);
1105 dsr = READ_CSR(sc, WXREG_DSR);
1107 if ((dsr & WXDSR_LU) == 0 && (dcr & WXDCR_SWDPIN1) == 0 &&
1108 (rxcw & WXRXCW_C) == 0) {
1109 if (sc->ane_failed == 0) {
1113 DPRINTF(sc, (inane, sc->wx_name));
1114 WRITE_CSR(sc, WXREG_XMIT_CFGW, WXTXCW_DEFAULT & ~WXTXCW_ANE);
1115 if (sc->wx_idnrev < WX_WISEMAN_2_1)
1116 sc->wx_dcr &= ~WXDCR_TFCE;
1117 sc->wx_dcr |= WXDCR_SLU;
1118 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
1119 } else if ((rxcw & WXRXCW_C) != 0 && (dcr & WXDCR_SLU) != 0) {
1120 DPRINTF(sc, (ane, sc->wx_name));
1121 WRITE_CSR(sc, WXREG_XMIT_CFGW, WXTXCW_DEFAULT);
1122 sc->wx_dcr &= ~WXDCR_SLU;
1123 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
1128 wx_handle_rxint(wx_softc_t *sc)
1130 struct ether_header *eh;
1131 struct mbuf *m0, *mb, *pending[WX_MAX_RDESC];
1132 struct ifnet *ifp = &sc->wx_if;
1133 int npkts, ndesc, lidx, idx, tlen;
1135 DPRINTF(sc, ("%s: wx_handle_rxint\n", sc->wx_name));
1137 for (m0 = sc->rpending, tlen = ndesc = npkts = 0, idx = sc->rnxt,
1138 lidx = R_PREV_IDX(idx); ndesc < WX_MAX_RDESC;
1139 ndesc++, lidx = idx, idx = R_NXT_IDX(idx)) {
1142 int length, offset, lastframe;
1144 rd = &sc->rdescriptors[idx];
1146 * XXX: DMA Flush descriptor
1148 if ((rd->status & RDSTAT_DD) == 0) {
1150 if (sc->rpending == NULL) {
1151 m0->m_pkthdr.len = tlen;
1158 DPRINTF(sc, ("%s: WXRX: ndesc %d idx %d lidx %d\n",
1159 sc->wx_name, ndesc, idx, lidx));
1163 if (rd->errors != 0) {
1164 printf("%s: packet with errors (%x)\n",
1165 sc->wx_name, rd->errors);
1172 m_freem(sc->rpending);
1173 sc->rpending = NULL;
1180 rxpkt = &sc->rbase[idx];
1183 printf("%s: receive descriptor with no mbuf\n",
1185 (void) wx_get_rbuf(sc, rxpkt);
1192 m_freem(sc->rpending);
1193 sc->rpending = NULL;
1199 /* XXX: Flush DMA for rxpkt */
1201 if (wx_get_rbuf(sc, rxpkt)) {
1203 wx_rxdma_map(sc, rxpkt, mb);
1210 m_freem(sc->rpending);
1211 sc->rpending = NULL;
1218 * Save the completing packet's offset value and length
1219 * and install the new one into the descriptor.
1221 lastframe = (rd->status & RDSTAT_EOP) != 0;
1222 length = rd->length;
1223 offset = rd->address.lowpart & 0xff;
1224 bzero (rd, sizeof (*rd));
1225 rd->address.lowpart = rxpkt->dma_addr + WX_RX_OFFSET_VALUE;
1228 mb->m_data += offset;
1233 } else if (m0 == sc->rpending) {
1235 * Pick up where we left off before. If
1236 * we have an offset (we're assuming the
1237 * first frame has an offset), then we've
1238 * lost sync somewhere along the line.
1241 printf("%s: lost sync with partial packet\n",
1243 m_freem(sc->rpending);
1244 sc->rpending = NULL;
1248 sc->rpending = NULL;
1249 tlen = m0->m_pkthdr.len;
1255 DPRINTF(sc, ("%s: RDESC[%d] len %d off %d lastframe %d\n",
1256 sc->wx_name, idx, mb->m_len, offset, lastframe));
1259 if (lastframe == 0) {
1262 m0->m_pkthdr.rcvif = ifp;
1263 m0->m_pkthdr.len = tlen - WX_CRC_LENGTH;
1264 mb->m_len -= WX_CRC_LENGTH;
1266 eh = mtod(m0, struct ether_header *);
1268 * No need to check for promiscous mode since
1269 * the decision to keep or drop the packet is
1270 * handled by ether_input()
1272 pending[npkts++] = m0;
1278 if (IS_WISEMAN(sc)) {
1279 WRITE_CSR(sc, WXREG_RDT0, lidx);
1281 WRITE_CSR(sc, WXREG_RDT0_LIVENGOOD, lidx);
1290 for (idx = 0; idx < npkts; idx++) {
1293 bpf_mtap(WX_BPFTAP_ARG(ifp), mb);
1296 DPRINTF(sc, ("%s: RECV packet length %d\n",
1297 sc->wx_name, mb->m_pkthdr.len));
1298 eh = mtod(mb, struct ether_header *);
1299 m_adj(mb, sizeof (struct ether_header));
1300 ether_input(ifp, eh, mb);
1305 wx_gc(wx_softc_t *sc)
1307 struct ifnet *ifp = &sc->wx_if;
1313 if (IS_WISEMAN(sc)) {
1314 tdh = READ_CSR(sc, WXREG_TDH);
1316 tdh = READ_CSR(sc, WXREG_TDH_LIVENGOOD);
1318 while (txpkt != NULL) {
1319 u_int32_t end = txpkt->eidx, cidx = tdh;
1322 * Normalize start..end indices to 2 *
1323 * WX_MAX_TDESC range to eliminate wrap.
1325 if (txpkt->eidx < txpkt->sidx) {
1326 end += WX_MAX_TDESC;
1330 * Normalize current chip index to 2 *
1331 * WX_MAX_TDESC range to eliminate wrap.
1333 if (cidx < txpkt->sidx) {
1334 cidx += WX_MAX_TDESC;
1338 * If the current chip index is between low and
1339 * high indices for this packet, it's not finished
1340 * transmitting yet. Because transmits are done FIFO,
1341 * this means we're done garbage collecting too.
1344 if (txpkt->sidx <= cidx && cidx < txpkt->eidx) {
1345 DPRINTF(sc, ("%s: TXGC %d..%d TDH %d\n", sc->wx_name,
1346 txpkt->sidx, txpkt->eidx, tdh));
1352 (void) m_freem(txpkt->dptr);
1354 printf("%s: null mbuf in gc\n", sc->wx_name);
1357 for (cidx = txpkt->sidx; cidx != txpkt->eidx;
1358 cidx = T_NXT_IDX(cidx)) {
1362 td = &sc->tdescriptors[cidx];
1363 if (td->status & TXSTS_EC) {
1364 IPRINTF(sc, ("%s: excess collisions\n",
1366 ifp->if_collisions++;
1369 if (td->status & TXSTS_LC) {
1371 ("%s: lost carrier\n", sc->wx_name));
1374 tmp = &sc->tbase[cidx];
1375 DPRINTF(sc, ("%s: TXGC[%d] %p %d..%d done nact %d "
1376 "TDH %d\n", sc->wx_name, cidx, tmp->dptr,
1377 txpkt->sidx, txpkt->eidx, sc->tactive, tdh));
1379 if (sc->tactive == 0) {
1380 printf("%s: nactive < 0?\n", sc->wx_name);
1384 bzero(td, sizeof (*td));
1386 sc->tbsyf = txpkt->next;
1389 if (sc->tactive < WX_MAX_TDESC - 1) {
1391 ifp->if_flags &= ~IFF_OACTIVE;
1394 /* used SW LED to indicate transmission not active */
1395 if (sc->tactive == 0 && sc->wx_mii) {
1396 WRITE_CSR(sc, WXREG_DCR,
1397 READ_CSR(sc, WXREG_DCR) & ~(WXDCR_SWDPIO0|WXDCR_SWDPIN0));
1403 * Periodic timer to update packet in/out/collision statistics,
1404 * and, more importantly, garbage collect completed transmissions
1405 * and to handle link status changes.
1407 #define WX_PRT_STATS(sc, y) printf("\t" # y " = %u\n", ((sc)->y))
1408 #define WX_CLR_STATS(sc, y) ((sc)->y = 0)
1411 wx_watchdog(void *arg)
1413 wx_softc_t *sc = arg;
1416 if (sc->wx_needreinit) {
1418 if (wx_init(sc) == 0) {
1420 sc->wx_needreinit = 0;
1428 if (wx_dump_stats == device_get_unit(sc->w.dev)) {
1429 printf("%s: current statistics\n", sc->wx_name);
1430 WX_PRT_STATS(sc, wx_intr);
1431 WX_PRT_STATS(sc, wx_linkintr);
1432 WX_PRT_STATS(sc, wx_rxintr);
1433 WX_PRT_STATS(sc, wx_txqe);
1434 WX_PRT_STATS(sc, wx_xmitgc);
1435 WX_PRT_STATS(sc, wx_xmitpullup);
1436 WX_PRT_STATS(sc, wx_xmitcluster);
1437 WX_PRT_STATS(sc, wx_xmitputback);
1438 WX_PRT_STATS(sc, wx_xmitwanted);
1439 WX_PRT_STATS(sc, wx_xmitblocked);
1440 WX_PRT_STATS(sc, wx_xmitrunt);
1441 WX_PRT_STATS(sc, wx_rxnobuf);
1442 WX_PRT_STATS(sc, wx_oddpkt);
1445 if (wx_clr_stats == device_get_unit(sc->w.dev)) {
1446 printf("%s: statistics cleared\n", sc->wx_name);
1447 WX_CLR_STATS(sc, wx_intr);
1448 WX_CLR_STATS(sc, wx_linkintr);
1449 WX_CLR_STATS(sc, wx_rxintr);
1450 WX_CLR_STATS(sc, wx_txqe);
1451 WX_CLR_STATS(sc, wx_xmitgc);
1452 WX_CLR_STATS(sc, wx_xmitpullup);
1453 WX_CLR_STATS(sc, wx_xmitcluster);
1454 WX_CLR_STATS(sc, wx_xmitputback);
1455 WX_CLR_STATS(sc, wx_xmitwanted);
1456 WX_CLR_STATS(sc, wx_xmitblocked);
1457 WX_CLR_STATS(sc, wx_xmitrunt);
1458 WX_CLR_STATS(sc, wx_rxnobuf);
1459 WX_CLR_STATS(sc, wx_oddpkt);
1465 * Schedule another timeout one second from now.
1467 TIMEOUT(sc, wx_watchdog, sc, hz);
1471 * Stop and reinitialize the hardware
1474 wx_hw_stop(wx_softc_t *sc)
1477 DPRINTF(sc, ("%s: wx_hw_stop\n", sc->wx_name));
1479 if (sc->wx_idnrev < WX_WISEMAN_2_1) {
1482 WRITE_CSR(sc, WXREG_DCR, WXDCR_RST);
1484 icr = READ_CSR(sc, WXREG_ICR);
1485 if (sc->wx_idnrev < WX_WISEMAN_2_1) {
1491 wx_set_addr(wx_softc_t *sc, int idx, u_int8_t *mac)
1494 DPRINTF(sc, ("%s: wx_set_addr\n", sc->wx_name));
1495 t0 = (mac[0]) | (mac[1] << 8) | (mac[2] << 16) | (mac[3] << 24);
1496 t1 = (mac[4] << 0) | (mac[5] << 8);
1498 WRITE_CSR(sc, WXREG_RAL_LO(idx), t0);
1499 WRITE_CSR(sc, WXREG_RAL_HI(idx), t1);
1503 wx_hw_initialize(wx_softc_t *sc)
1507 DPRINTF(sc, ("%s: wx_hw_initialize\n", sc->wx_name));
1509 WRITE_CSR(sc, WXREG_VET, 0);
1510 for (i = 0; i < (WX_VLAN_TAB_SIZE << 2); i += 4) {
1511 WRITE_CSR(sc, (WXREG_VFTA + i), 0);
1513 if (sc->wx_idnrev < WX_WISEMAN_2_1) {
1515 WRITE_CSR(sc, WXREG_RCTL, WXRCTL_RST);
1519 * Load the first receiver address with our MAC address,
1520 * and load as many multicast addresses as can fit into
1521 * the receive address array.
1523 wx_set_addr(sc, 0, sc->wx_enaddr);
1524 for (i = 1; i <= sc->wx_nmca; i++) {
1525 if (i >= WX_RAL_TAB_SIZE) {
1528 wx_set_addr(sc, i, sc->wx_mcaddr[i-1]);
1532 while (i < WX_RAL_TAB_SIZE) {
1533 WRITE_CSR(sc, WXREG_RAL_LO(i), 0);
1534 WRITE_CSR(sc, WXREG_RAL_HI(i), 0);
1538 if (sc->wx_idnrev < WX_WISEMAN_2_1) {
1539 WRITE_CSR(sc, WXREG_RCTL, 0);
1545 * Clear out the hashed multicast table array.
1547 for (i = 0; i < WX_MC_TAB_SIZE; i++) {
1548 WRITE_CSR(sc, WXREG_MTA + (sizeof (u_int32_t) * 4), 0);
1551 if (IS_LIVENGOOD_CU(sc)) {
1553 * has a PHY - raise its reset line to make it operational
1555 u_int32_t tmp = READ_CSR(sc, WXREG_EXCT);
1556 tmp |= WXPHY_RESET_DIR4;
1557 WRITE_CSR(sc, WXREG_EXCT, tmp);
1560 tmp = READ_CSR(sc, WXREG_EXCT);
1561 tmp &= ~WXPHY_RESET4;
1562 WRITE_CSR(sc, WXREG_EXCT, tmp);
1565 tmp = READ_CSR(sc, WXREG_EXCT);
1566 tmp |= WXPHY_RESET4;
1567 WRITE_CSR(sc, WXREG_EXCT, tmp);
1569 } else if (IS_LIVENGOOD(sc)) {
1573 * Handle link control
1575 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr | WXDCR_LRST);
1578 wx_read_eeprom(sc, &tew, WX_EEPROM_CTLR2_OFF, 1);
1579 tew = (tew & WX_EEPROM_CTLR2_SWDPIO) << WX_EEPROM_EXT_SHIFT;
1580 WRITE_CSR(sc, WXREG_EXCT, (u_int32_t)tew);
1583 if (sc->wx_dcr & (WXDCR_RFCE|WXDCR_TFCE)) {
1584 WRITE_CSR(sc, WXREG_FCAL, FC_FRM_CONST_LO);
1585 WRITE_CSR(sc, WXREG_FCAH, FC_FRM_CONST_HI);
1586 WRITE_CSR(sc, WXREG_FCT, FC_TYP_CONST);
1588 WRITE_CSR(sc, WXREG_FCAL, 0);
1589 WRITE_CSR(sc, WXREG_FCAH, 0);
1590 WRITE_CSR(sc, WXREG_FCT, 0);
1592 WRITE_CSR(sc, WXREG_FLOW_XTIMER, WX_XTIMER_DFLT);
1594 if (IS_WISEMAN(sc)) {
1595 if (sc->wx_idnrev < WX_WISEMAN_2_1) {
1596 WRITE_CSR(sc, WXREG_FLOW_RCV_HI, 0);
1597 WRITE_CSR(sc, WXREG_FLOW_RCV_LO, 0);
1598 sc->wx_dcr &= ~(WXDCR_RFCE|WXDCR_TFCE);
1600 WRITE_CSR(sc, WXREG_FLOW_RCV_HI, WX_RCV_FLOW_HI_DFLT);
1601 WRITE_CSR(sc, WXREG_FLOW_RCV_LO, WX_RCV_FLOW_LO_DFLT);
1604 WRITE_CSR(sc, WXREG_FLOW_RCV_HI_LIVENGOOD, WX_RCV_FLOW_HI_DFLT);
1605 WRITE_CSR(sc, WXREG_FLOW_RCV_LO_LIVENGOOD, WX_RCV_FLOW_LO_DFLT);
1608 if (!IS_LIVENGOOD_CU(sc))
1609 WRITE_CSR(sc, WXREG_XMIT_CFGW, WXTXCW_DEFAULT);
1611 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
1614 if (!IS_LIVENGOOD_CU(sc)) {
1616 * The pin stuff is all FM from the Linux driver.
1618 if ((READ_CSR(sc, WXREG_DCR) & WXDCR_SWDPIN1) == 0) {
1619 for (i = 0; i < (WX_LINK_UP_TIMEOUT/10); i++) {
1621 if (READ_CSR(sc, WXREG_DSR) & WXDSR_LU) {
1626 if (sc->linkup == 0) {
1632 printf("%s: SWDPIO1 did not clear- check for reversed "
1633 "or disconnected cable\n", sc->wx_name);
1634 /* but return okay anyway */
1638 sc->wx_ienable = WXIENABLE_DEFAULT;
1643 * Stop the interface. Cancels the statistics updater and resets the interface.
1646 wx_stop(wx_softc_t *sc)
1650 struct ifnet *ifp = &sc->wx_if;
1652 DPRINTF(sc, ("%s: wx_stop\n", sc->wx_name));
1654 * Cancel stats updater.
1656 UNTIMEOUT(wx_watchdog, sc, sc);
1664 * Release any xmit buffers.
1666 for (txp = sc->tbase; txp && txp < &sc->tbase[WX_MAX_TDESC]; txp++) {
1674 * Free all the receive buffers.
1676 for (rxp = sc->rbase; rxp && rxp < &sc->rbase[WX_MAX_RDESC]; rxp++) {
1684 m_freem(sc->rpending);
1685 sc->rpending = NULL;
1689 * And we're outta here...
1692 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1700 wx_txwatchdog(struct ifnet *ifp)
1702 wx_softc_t *sc = SOFTC_IFP(ifp);
1703 printf("%s: device timeout\n", sc->wx_name);
1706 printf("%s: could not re-init device\n", sc->wx_name);
1707 sc->wx_needreinit = 1;
1714 struct ifmedia *ifm;
1715 wx_softc_t *sc = xsc;
1716 struct ifnet *ifp = &sc->wx_if;
1722 DPRINTF(sc, ("%s: wx_init\n", sc->wx_name));
1726 * Cancel any pending I/O by resetting things.
1727 * wx_stop will free any allocated mbufs.
1732 * Reset the hardware. All network addresses loaded here, but
1733 * neither the receiver nor the transmitter are enabled.
1736 if (wx_hw_initialize(sc)) {
1737 DPRINTF(sc, ("%s: wx_hw_initialize failed\n", sc->wx_name));
1743 * Set up the receive ring stuff.
1745 len = sizeof (wxrd_t) * WX_MAX_RDESC;
1746 bzero(sc->rdescriptors, len);
1747 for (rxpkt = sc->rbase, i = 0; rxpkt != NULL && i < WX_MAX_RDESC;
1748 i += RXINCR, rxpkt++) {
1749 rd = &sc->rdescriptors[i];
1750 if (wx_get_rbuf(sc, rxpkt)) {
1753 rd->address.lowpart = rxpkt->dma_addr + WX_RX_OFFSET_VALUE;
1755 if (i != WX_MAX_RDESC) {
1756 printf("%s: could not set up rbufs\n", sc->wx_name);
1763 * Set up transmit parameters and enable the transmitter.
1765 sc->tnxtfree = sc->tactive = 0;
1766 sc->tbsyf = sc->tbsyl = NULL;
1767 WRITE_CSR(sc, WXREG_TCTL, 0);
1769 if (IS_WISEMAN(sc)) {
1770 WRITE_CSR(sc, WXREG_TDBA_LO,
1771 vtophys((vm_offset_t)&sc->tdescriptors[0]));
1772 WRITE_CSR(sc, WXREG_TDBA_HI, 0);
1773 WRITE_CSR(sc, WXREG_TDLEN, WX_MAX_TDESC * sizeof (wxtd_t));
1774 WRITE_CSR(sc, WXREG_TDH, 0);
1775 WRITE_CSR(sc, WXREG_TDT, 0);
1776 WRITE_CSR(sc, WXREG_TQSA_HI, 0);
1777 WRITE_CSR(sc, WXREG_TQSA_LO, 0);
1778 WRITE_CSR(sc, WXREG_TIPG, WX_WISEMAN_TIPG_DFLT);
1779 WRITE_CSR(sc, WXREG_TIDV, wx_txint_delay);
1781 WRITE_CSR(sc, WXREG_TDBA_LO_LIVENGOOD,
1782 vtophys((vm_offset_t)&sc->tdescriptors[0]));
1783 WRITE_CSR(sc, WXREG_TDBA_HI_LIVENGOOD, 0);
1784 WRITE_CSR(sc, WXREG_TDLEN_LIVENGOOD,
1785 WX_MAX_TDESC * sizeof (wxtd_t));
1786 WRITE_CSR(sc, WXREG_TDH_LIVENGOOD, 0);
1787 WRITE_CSR(sc, WXREG_TDT_LIVENGOOD, 0);
1788 WRITE_CSR(sc, WXREG_TQSA_HI, 0);
1789 WRITE_CSR(sc, WXREG_TQSA_LO, 0);
1790 WRITE_CSR(sc, WXREG_TIPG, WX_LIVENGOOD_TIPG_DFLT);
1791 WRITE_CSR(sc, WXREG_TIDV_LIVENGOOD, wx_txint_delay);
1793 WRITE_CSR(sc, WXREG_TCTL, (WXTCTL_CT(WX_COLLISION_THRESHOLD) |
1794 WXTCTL_COLD(WX_FDX_COLLISION_DX) | WXTCTL_EN));
1796 * Set up receive parameters and enable the receiver.
1800 WRITE_CSR(sc, WXREG_RCTL, 0);
1802 if (IS_WISEMAN(sc)) {
1803 WRITE_CSR(sc, WXREG_RDTR0, WXRDTR_FPD);
1804 WRITE_CSR(sc, WXREG_RDBA0_LO,
1805 vtophys((vm_offset_t)&sc->rdescriptors[0]));
1806 WRITE_CSR(sc, WXREG_RDBA0_HI, 0);
1807 WRITE_CSR(sc, WXREG_RDLEN0, WX_MAX_RDESC * sizeof (wxrd_t));
1808 WRITE_CSR(sc, WXREG_RDH0, 0);
1809 WRITE_CSR(sc, WXREG_RDT0, (WX_MAX_RDESC - RXINCR));
1812 * The delay should yield ~10us receive interrupt delay
1814 WRITE_CSR(sc, WXREG_RDTR0_LIVENGOOD, WXRDTR_FPD | 0x40);
1815 WRITE_CSR(sc, WXREG_RDBA0_LO_LIVENGOOD,
1816 vtophys((vm_offset_t)&sc->rdescriptors[0]));
1817 WRITE_CSR(sc, WXREG_RDBA0_HI_LIVENGOOD, 0);
1818 WRITE_CSR(sc, WXREG_RDLEN0_LIVENGOOD,
1819 WX_MAX_RDESC * sizeof (wxrd_t));
1820 WRITE_CSR(sc, WXREG_RDH0_LIVENGOOD, 0);
1821 WRITE_CSR(sc, WXREG_RDT0_LIVENGOOD, (WX_MAX_RDESC - RXINCR));
1823 WRITE_CSR(sc, WXREG_RDTR1, 0);
1824 WRITE_CSR(sc, WXREG_RDBA1_LO, 0);
1825 WRITE_CSR(sc, WXREG_RDBA1_HI, 0);
1826 WRITE_CSR(sc, WXREG_RDLEN1, 0);
1827 WRITE_CSR(sc, WXREG_RDH1, 0);
1828 WRITE_CSR(sc, WXREG_RDT1, 0);
1830 if (ifp->if_mtu > ETHERMTU) {
1831 bflags = WXRCTL_EN | WXRCTL_LPE | WXRCTL_2KRBUF;
1833 bflags = WXRCTL_EN | WXRCTL_2KRBUF;
1836 WRITE_CSR(sc, WXREG_RCTL, bflags |
1837 ((ifp->if_flags & IFF_BROADCAST) ? WXRCTL_BAM : 0) |
1838 ((ifp->if_flags & IFF_PROMISC) ? WXRCTL_UPE : 0) |
1839 ((sc->all_mcasts) ? WXRCTL_MPE : 0));
1847 mii_mediachg(WX_MII_FROM_SOFTC(sc));
1849 ifm = &sc->wx_media;
1851 ifm->ifm_media = ifm->ifm_cur->ifm_media;
1852 wx_ifmedia_upd(ifp);
1857 * Mark that we're up and running...
1859 ifp->if_flags |= IFF_RUNNING;
1860 ifp->if_flags &= ~IFF_OACTIVE;
1864 * Start stats updater.
1866 TIMEOUT(sc, wx_watchdog, sc, hz);
1870 * And we're outta here...
1876 * Get a receive buffer for our use (and dma map the data area).
1878 * The Wiseman chip can have buffers be 256, 512, 1024 or 2048 bytes in size.
1879 * The LIVENGOOD chip can go higher (up to 16K), but what's the point as
1880 * we aren't doing non-MCLGET memory management.
1882 * It wants them aligned on 256 byte boundaries, but can actually cope
1883 * with an offset in the first 255 bytes of the head of a receive frame.
1885 * We'll allocate a MCLBYTE sized cluster but *not* adjust the data pointer
1886 * by any alignment value. Instead, we'll tell the chip to offset by any
1887 * alignment and we'll catch the alignment on the backend at interrupt time.
1890 wx_rxdma_map(wx_softc_t *sc, rxpkt_t *rxpkt, struct mbuf *mb)
1893 rxpkt->dma_addr = vtophys(mtod(mb, vm_offset_t));
1897 wx_get_rbuf(wx_softc_t *sc, rxpkt_t *rxpkt)
1900 MGETHDR(mb, M_DONTWAIT, MT_DATA);
1905 MCLGET(mb, M_DONTWAIT);
1906 if ((mb->m_flags & M_EXT) == 0) {
1911 wx_rxdma_map(sc, rxpkt, mb);
1916 wx_ioctl(struct ifnet *ifp, IOCTL_CMD_TYPE command, caddr_t data)
1918 wx_softc_t *sc = SOFTC_IFP(ifp);
1919 struct ifreq *ifr = (struct ifreq *) data;
1926 error = ether_ioctl(ifp, command, data);
1929 if (ifr->ifr_mtu > WX_MAXMTU || ifr->ifr_mtu < ETHERMIN) {
1931 } else if (ifp->if_mtu != ifr->ifr_mtu) {
1932 ifp->if_mtu = ifr->ifr_mtu;
1933 error = wx_init(sc);
1937 sc->all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1940 * If interface is marked up and not running, then start it.
1941 * If it is marked down and running, stop it.
1942 * If it's up then re-initialize it. This is so flags
1943 * such as IFF_PROMISC are handled.
1945 if (ifp->if_flags & IFF_UP) {
1946 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1947 error = wx_init(sc);
1950 if (ifp->if_flags & IFF_RUNNING) {
1958 sc->all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1959 error = wx_mc_setup(sc);
1963 DPRINTF(sc, ("%s: ioctl SIOC[GS]IFMEDIA: command=%#lx\n",
1964 sc->wx_name, command));
1966 mii_data_t *mii = WX_MII_FROM_SOFTC(sc);
1967 error = ifmedia_ioctl(ifp, ifr,
1968 &mii->mii_media, command);
1970 error = ifmedia_ioctl(ifp, ifr, &sc->wx_media, command);
1983 wx_ifmedia_upd(struct ifnet *ifp)
1985 struct wx_softc *sc = SOFTC_IFP(ifp);
1986 struct ifmedia *ifm;
1988 DPRINTF(sc, ("%s: ifmedia_upd\n", sc->wx_name));
1991 mii_mediachg(WX_MII_FROM_SOFTC(sc));
1995 ifm = &sc->wx_media;
1997 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
2005 wx_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2008 struct wx_softc *sc = SOFTC_IFP(ifp);
2010 DPRINTF(sc, ("%s: ifmedia_sts: ", sc->wx_name));
2013 mii_data_t *mii = WX_MII_FROM_SOFTC(sc);
2015 ifmr->ifm_active = mii->mii_media_active;
2016 ifmr->ifm_status = mii->mii_media_status;
2017 DPRINTF(sc, ("active=%#x status=%#x\n",
2018 ifmr->ifm_active, ifmr->ifm_status));
2022 DPRINTF(sc, ("\n"));
2023 ifmr->ifm_status = IFM_AVALID;
2024 ifmr->ifm_active = IFM_ETHER;
2026 if (sc->linkup == 0)
2029 ifmr->ifm_status |= IFM_ACTIVE;
2030 dsr = READ_CSR(sc, WXREG_DSR);
2031 if (IS_LIVENGOOD(sc)) {
2032 if (dsr & WXDSR_1000BT) {
2033 if (IS_LIVENGOOD_CU(sc)) {
2034 ifmr->ifm_status |= IFM_1000_TX;
2037 ifmr->ifm_status |= IFM_1000_SX;
2039 } else if (dsr & WXDSR_100BT) {
2040 ifmr->ifm_status |= IFM_100_FX; /* ?? */
2042 ifmr->ifm_status |= IFM_10_T; /* ?? */
2045 ifmr->ifm_status |= IFM_1000_SX;
2047 if (dsr & WXDSR_FD) {
2048 ifmr->ifm_active |= IFM_FDX;
2053 #define RAISE_CLOCK(sc, dcr) \
2054 WRITE_CSR(sc, WXREG_DCR, (dcr) | WXPHY_MDC), DELAY(2)
2056 #define LOWER_CLOCK(sc, dcr) \
2057 WRITE_CSR(sc, WXREG_DCR, (dcr) & ~WXPHY_MDC), DELAY(2)
2060 wx_mii_shift_in(wx_softc_t *sc)
2065 dcr = READ_CSR(sc, WXREG_DCR);
2066 dcr &= ~(WXPHY_MDIO_DIR | WXPHY_MDIO);
2067 WRITE_CSR(sc, WXREG_DCR, dcr);
2068 RAISE_CLOCK(sc, dcr);
2069 LOWER_CLOCK(sc, dcr);
2071 for (i = 0; i < 16; i++) {
2073 RAISE_CLOCK(sc, dcr);
2074 dcr = READ_CSR(sc, WXREG_DCR);
2076 if (dcr & WXPHY_MDIO)
2079 LOWER_CLOCK(sc, dcr);
2082 RAISE_CLOCK(sc, dcr);
2083 LOWER_CLOCK(sc, dcr);
2088 wx_mii_shift_out(wx_softc_t *sc, u_int32_t data, u_int32_t count)
2090 u_int32_t dcr, mask;
2092 dcr = READ_CSR(sc, WXREG_DCR);
2093 dcr |= WXPHY_MDIO_DIR | WXPHY_MDC_DIR;
2095 for (mask = (1 << (count - 1)); mask; mask >>= 1) {
2101 WRITE_CSR(sc, WXREG_DCR, dcr);
2103 RAISE_CLOCK(sc, dcr);
2104 LOWER_CLOCK(sc, dcr);
2109 wx_miibus_readreg(void *arg, int phy, int reg)
2111 wx_softc_t *sc = WX_SOFTC_FROM_MII_ARG(arg);
2112 unsigned int data = 0;
2114 if (!IS_LIVENGOOD_CU(sc)) {
2117 wx_mii_shift_out(sc, WXPHYC_PREAMBLE, WXPHYC_PREAMBLE_LEN);
2118 wx_mii_shift_out(sc, reg | (phy << 5) | (WXPHYC_READ << 10) |
2119 (WXPHYC_SOF << 12), 14);
2120 data = wx_mii_shift_in(sc);
2121 return (data & WXMDIC_DATA_MASK);
2125 wx_miibus_writereg(void *arg, int phy, int reg, int data)
2127 wx_softc_t *sc = WX_SOFTC_FROM_MII_ARG(arg);
2128 if (!IS_LIVENGOOD_CU(sc)) {
2131 wx_mii_shift_out(sc, WXPHYC_PREAMBLE, WXPHYC_PREAMBLE_LEN);
2132 wx_mii_shift_out(sc, (u_int32_t)data | (WXPHYC_TURNAROUND << 16) |
2133 (reg << 18) | (phy << 23) | (WXPHYC_WRITE << 28) |
2134 (WXPHYC_SOF << 30), 32);
2139 wx_miibus_statchg(void *arg)
2141 wx_softc_t *sc = WX_SOFTC_FROM_MII_ARG(arg);
2142 mii_data_t *mii = WX_MII_FROM_SOFTC(sc);
2143 u_int32_t dcr, tctl;
2149 tctl = READ_CSR(sc, WXREG_TCTL);
2150 DPRINTF(sc, ("%s: statchg dcr=%#x tctl=%#x", sc->wx_name, dcr, tctl));
2152 dcr |= WXDCR_FRCSPD | WXDCR_FRCDPX | WXDCR_SLU;
2153 dcr &= ~(WXDCR_SPEED_MASK | WXDCR_ASDE /* | WXDCR_ILOS */);
2155 if (mii->mii_media_status & IFM_ACTIVE) {
2156 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE) {
2157 DPRINTF(sc, (" link-down\n"));
2165 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_TX) {
2166 DPRINTF(sc, (" 1000TX"));
2167 dcr |= WXDCR_1000BT;
2168 } else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
2169 DPRINTF(sc, (" 100TX"));
2171 } else /* assume IFM_10_TX */ {
2172 DPRINTF(sc, (" 10TX"));
2176 if (mii->mii_media_active & IFM_FDX) {
2177 DPRINTF(sc, ("-FD"));
2178 tctl = WXTCTL_CT(WX_COLLISION_THRESHOLD) |
2179 WXTCTL_COLD(WX_FDX_COLLISION_DX) | WXTCTL_EN;
2182 DPRINTF(sc, ("-HD"));
2183 tctl = WXTCTL_CT(WX_COLLISION_THRESHOLD) |
2184 WXTCTL_COLD(WX_HDX_COLLISION_DX) | WXTCTL_EN;
2188 /* FLAG0==rx-flow-control FLAG1==tx-flow-control */
2189 if (mii->mii_media_active & IFM_FLAG0) {
2195 if (mii->mii_media_active & IFM_FLAG1) {
2201 if (dcr & (WXDCR_RFCE|WXDCR_TFCE)) {
2202 WRITE_CSR(sc, WXREG_FCAL, FC_FRM_CONST_LO);
2203 WRITE_CSR(sc, WXREG_FCAH, FC_FRM_CONST_HI);
2204 WRITE_CSR(sc, WXREG_FCT, FC_TYP_CONST);
2206 WRITE_CSR(sc, WXREG_FCAL, 0);
2207 WRITE_CSR(sc, WXREG_FCAH, 0);
2208 WRITE_CSR(sc, WXREG_FCT, 0);
2211 DPRINTF(sc, (" dcr=%#x tctl=%#x\n", dcr, tctl));
2212 WRITE_CSR(sc, WXREG_TCTL, tctl);
2214 WRITE_CSR(sc, WXREG_DCR, dcr);
2218 wx_miibus_mediainit(void *arg)