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.12 2004/09/15 01:19:13 joerg 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, struct ucred *);
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_init(&sc->watchdog_timer);
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);
332 ifp = &sc->w.arpcom.ac_if;
333 if_initname(ifp, "wx", device_get_unit(dev));
334 ifp->if_mtu = ETHERMTU; /* we always start at ETHERMTU size */
335 ifp->if_baudrate = 1000000000;
336 ifp->if_init = (void (*)(void *))wx_init;
338 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
339 ifp->if_ioctl = wx_ioctl;
340 ifp->if_start = wx_start;
341 ifp->if_watchdog = wx_txwatchdog;
342 ifp->if_snd.ifq_maxlen = WX_MAX_TDESC - 1;
343 ether_ifattach(ifp, sc->w.arpcom.ac_enaddr);
350 wx_attach_phy(wx_softc_t *sc)
352 if (mii_phy_probe(sc->w.dev, &sc->w.miibus, wx_ifmedia_upd,
354 printf("%s: no PHY probed!\n", sc->wx_name);
362 wx_detach(device_t dev)
364 wx_softc_t *sc = device_get_softc(dev);
369 ether_ifdetach(&sc->w.arpcom.ac_if);
371 bus_generic_detach(dev);
372 device_delete_child(dev, sc->w.miibus);
374 ifmedia_removeall(&sc->wx_media);
376 bus_teardown_intr(dev, sc->w.irq, sc->w.ih);
377 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->w.irq);
378 bus_release_resource(dev, SYS_RES_MEMORY, WX_MMBA, sc->w.mem);
380 wx_dring_teardown(sc);
391 mtx_destroy(&sc->wx_mtx);
397 wx_shutdown(device_t dev)
399 wx_hw_stop((wx_softc_t *) device_get_softc(dev));
404 wx_mwi_whackon(wx_softc_t *sc)
406 sc->wx_cmdw = pci_read_config(sc->w.dev, PCIR_COMMAND, 2);
407 pci_write_config(sc->w.dev, PCIR_COMMAND, sc->wx_cmdw & ~MWI, 2);
411 wx_mwi_unwhack(wx_softc_t *sc)
413 if (sc->wx_cmdw & MWI) {
414 pci_write_config(sc->w.dev, PCIR_COMMAND, sc->wx_cmdw, 2);
419 wx_dring_setup(wx_softc_t *sc)
423 len = sizeof (wxrd_t) * WX_MAX_RDESC;
424 sc->rdescriptors = (wxrd_t *)
425 contigmalloc(len, M_DEVBUF, M_NOWAIT, 0, ~0, 4096, 0);
426 if (sc->rdescriptors == NULL) {
427 printf("%s: could not allocate rcv descriptors\n", sc->wx_name);
430 if (((intptr_t)sc->rdescriptors) & 0xfff) {
431 contigfree(sc->rdescriptors, len, M_DEVBUF);
432 sc->rdescriptors = NULL;
433 printf("%s: rcv descriptors not 4KB aligned\n", sc->wx_name);
436 bzero(sc->rdescriptors, len);
438 len = sizeof (wxtd_t) * WX_MAX_TDESC;
439 sc->tdescriptors = (wxtd_t *)
440 contigmalloc(len, M_DEVBUF, M_NOWAIT, 0, ~0, 4096, 0);
441 if (sc->tdescriptors == NULL) {
442 contigfree(sc->rdescriptors,
443 sizeof (wxrd_t) * WX_MAX_RDESC, M_DEVBUF);
444 sc->rdescriptors = NULL;
445 printf("%s: could not allocate xmt descriptors\n", sc->wx_name);
448 if (((intptr_t)sc->tdescriptors) & 0xfff) {
449 contigfree(sc->rdescriptors,
450 sizeof (wxrd_t) * WX_MAX_RDESC, M_DEVBUF);
451 contigfree(sc->tdescriptors, len, M_DEVBUF);
452 sc->rdescriptors = NULL;
453 sc->tdescriptors = NULL;
454 printf("%s: xmt descriptors not 4KB aligned\n", sc->wx_name);
457 bzero(sc->tdescriptors, len);
462 wx_dring_teardown(wx_softc_t *sc)
464 if (sc->rdescriptors) {
465 contigfree(sc->rdescriptors,
466 sizeof (wxrd_t) * WX_MAX_RDESC, M_DEVBUF);
467 sc->rdescriptors = NULL;
469 if (sc->tdescriptors) {
470 contigfree(sc->tdescriptors,
471 sizeof (wxtd_t) * WX_MAX_TDESC, M_DEVBUF);
472 sc->tdescriptors = NULL;
476 static device_method_t wx_methods[] = {
477 /* Device interface */
478 DEVMETHOD(device_probe, wx_probe),
479 DEVMETHOD(device_attach, wx_attach),
480 DEVMETHOD(device_detach, wx_detach),
481 DEVMETHOD(device_shutdown, wx_shutdown),
484 DEVMETHOD(bus_print_child, bus_generic_print_child),
485 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
488 DEVMETHOD(miibus_readreg, wx_miibus_readreg),
489 DEVMETHOD(miibus_writereg, wx_miibus_writereg),
490 DEVMETHOD(miibus_statchg, wx_miibus_statchg),
491 DEVMETHOD(miibus_mediainit, wx_miibus_mediainit),
496 static driver_t wx_driver = {
497 "wx", wx_methods, sizeof(wx_softc_t),
499 static devclass_t wx_devclass;
501 DECLARE_DUMMY_MODULE(if_wx);
502 MODULE_DEPEND(if_wx, miibus, 1, 1, 1);
503 DRIVER_MODULE(if_wx, pci, wx_driver, wx_devclass, 0, 0);
504 DRIVER_MODULE(miibus, wx, miibus_driver, miibus_devclass, 0, 0);
507 * Do generic parts of attach. Our registers have been mapped
508 * and our interrupt registered.
511 wx_attach_common(wx_softc_t *sc)
518 * First, check for revision support.
520 if (sc->wx_idnrev < WX_WISEMAN_2_0) {
521 printf("%s: cannot support ID 0x%x, revision %d chips\n",
522 sc->wx_name, sc->wx_idnrev >> 16, sc->wx_idnrev & 0xffff);
527 * Second, reset the chip.
532 * Third, validate our EEPROM.
538 * Fourth, read eeprom for our MAC address and other things.
540 wx_read_eeprom(sc, (u_int16_t *)sc->wx_enaddr, WX_EEPROM_MAC_OFF, 3);
543 * Fifth, establish some adapter parameters.
547 if (IS_LIVENGOOD_CU(sc)) {
549 /* settings to talk to PHY */
550 sc->wx_dcr |= WXDCR_FRCSPD | WXDCR_FRCDPX | WXDCR_SLU;
551 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
554 * Raise the PHY's reset line to make it operational.
556 tmp = READ_CSR(sc, WXREG_EXCT);
557 tmp |= WXPHY_RESET_DIR4;
558 WRITE_CSR(sc, WXREG_EXCT, tmp);
561 tmp = READ_CSR(sc, WXREG_EXCT);
562 tmp &= ~WXPHY_RESET4;
563 WRITE_CSR(sc, WXREG_EXCT, tmp);
566 tmp = READ_CSR(sc, WXREG_EXCT);
568 WRITE_CSR(sc, WXREG_EXCT, tmp);
571 if (wx_attach_phy(sc)) {
575 ifmedia_init(&sc->wx_media, IFM_IMASK,
576 wx_ifmedia_upd, wx_ifmedia_sts);
578 ifmedia_add(&sc->wx_media, IFM_ETHER|IFM_1000_SX, 0, NULL);
579 ifmedia_add(&sc->wx_media,
580 IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
581 ifmedia_set(&sc->wx_media, IFM_ETHER|IFM_1000_SX|IFM_FDX);
583 sc->wx_media.ifm_media = sc->wx_media.ifm_cur->ifm_media;
587 * Sixth, establish a default device control register word.
590 if (sc->wx_cfg1 & WX_EEPROM_CTLR1_FD)
591 sc->wx_dcr |= WXDCR_FD;
592 if (sc->wx_cfg1 & WX_EEPROM_CTLR1_ILOS)
593 sc->wx_dcr |= WXDCR_ILOS;
595 tmp = (sc->wx_cfg1 >> WX_EEPROM_CTLR1_SWDPIO_SHIFT) & WXDCR_SWDPIO_MASK;
596 sc->wx_dcr |= (tmp << WXDCR_SWDPIO_SHIFT);
599 sc->wx_dcr &= ~WXDCR_ILOS;
601 sc->wx_dcr |= WXDCR_ILOS;
602 if (sc->wx_no_flow == 0)
603 sc->wx_dcr |= WXDCR_RFCE | WXDCR_TFCE;
606 * Seventh, allocate various sw structures...
608 len = sizeof (rxpkt_t) * WX_MAX_RDESC;
609 sc->rbase = (rxpkt_t *) WXMALLOC(len);
610 if (sc->rbase == NULL) {
613 bzero(sc->rbase, len);
616 len = sizeof (txpkt_t) * WX_MAX_TDESC;
617 sc->tbase = (txpkt_t *) WXMALLOC(len);
618 if (sc->tbase == NULL) {
621 bzero(sc->tbase, len);
625 * Eighth, allocate and dma map (platform dependent) descriptor rings.
626 * They have to be aligned on a 4KB boundary.
628 if (wx_dring_setup(sc) == 0) {
633 printf("%s: failed to do common attach (%d)\n", sc->wx_name, ll);
634 wx_dring_teardown(sc);
651 wx_eeprom_raise_clk(wx_softc_t *sc, u_int32_t regval)
653 WRITE_CSR(sc, WXREG_EECDR, regval | WXEECD_SK);
658 wx_eeprom_lower_clk(wx_softc_t *sc, u_int32_t regval)
660 WRITE_CSR(sc, WXREG_EECDR, regval & ~WXEECD_SK);
665 wx_eeprom_sobits(wx_softc_t *sc, u_int16_t data, u_int16_t count)
667 u_int32_t regval, mask;
669 mask = 1 << (count - 1);
670 regval = READ_CSR(sc, WXREG_EECDR) & ~(WXEECD_DI|WXEECD_DO);
676 regval &= ~WXEECD_DI;
677 WRITE_CSR(sc, WXREG_EECDR, regval); DELAY(50);
678 wx_eeprom_raise_clk(sc, regval);
679 wx_eeprom_lower_clk(sc, regval);
682 WRITE_CSR(sc, WXREG_EECDR, regval & ~WXEECD_DI);
685 static INLINE u_int16_t
686 wx_eeprom_sibits(wx_softc_t *sc)
688 unsigned int regval, i;
692 regval = READ_CSR(sc, WXREG_EECDR) & ~(WXEECD_DI|WXEECD_DO);
693 for (i = 0; i != 16; i++) {
695 wx_eeprom_raise_clk(sc, regval);
696 regval = READ_CSR(sc, WXREG_EECDR) & ~WXEECD_DI;
697 if (regval & WXEECD_DO) {
700 wx_eeprom_lower_clk(sc, regval);
706 wx_eeprom_cleanup(wx_softc_t *sc)
709 regval = READ_CSR(sc, WXREG_EECDR) & ~(WXEECD_DI|WXEECD_CS);
710 WRITE_CSR(sc, WXREG_EECDR, regval); DELAY(50);
711 wx_eeprom_raise_clk(sc, regval);
712 wx_eeprom_lower_clk(sc, regval);
715 static u_int16_t INLINE
716 wx_read_eeprom_word(wx_softc_t *sc, int offset)
719 WRITE_CSR(sc, WXREG_EECDR, WXEECD_CS);
720 wx_eeprom_sobits(sc, EEPROM_READ_OPCODE, 3);
721 wx_eeprom_sobits(sc, offset, 6);
722 data = wx_eeprom_sibits(sc);
723 wx_eeprom_cleanup(sc);
728 wx_read_eeprom(wx_softc_t *sc, u_int16_t *data, int offset, int words)
731 for (i = 0; i < words; i++) {
732 *data++ = wx_read_eeprom_word(sc, offset++);
734 sc->wx_cfg1 = wx_read_eeprom_word(sc, WX_EEPROM_CTLR1_OFF);
738 * Start packet transmission on the interface.
742 wx_start(struct ifnet *ifp)
744 wx_softc_t *sc = SOFTC_IFP(ifp);
745 u_int16_t widx = WX_MAX_TDESC, cidx, nactv;
748 DPRINTF(sc, ("%s: wx_start\n", sc->wx_name));
750 while (nactv < WX_MAX_TDESC - 1) {
753 struct mbuf *m, *mb_head;
755 IF_DEQUEUE(&ifp->if_snd, mb_head);
756 if (mb_head == NULL) {
762 * If we have a packet less than ethermin, pad it out.
764 if (mb_head->m_pkthdr.len < WX_MIN_RPKT_SIZE) {
765 if (mb_head->m_next == NULL) {
766 mb_head->m_len = WX_MIN_RPKT_SIZE;
768 MGETHDR(m, MB_DONTWAIT, MT_DATA);
773 m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
775 m->m_pkthdr.len = m->m_len = WX_MIN_RPKT_SIZE;
776 bzero(mtod(m, char *) + mb_head->m_pkthdr.len,
777 WX_MIN_RPKT_SIZE - mb_head->m_pkthdr.len);
789 * Go through each of the mbufs in the chain and initialize
790 * the transmit buffer descriptors with the physical address
791 * and size of that mbuf. If we have a length less than our
792 * minimum transmit size, we bail (to do a pullup). If we run
793 * out of descriptors, we also bail and try and do a pullup.
795 for (plen = ndesc = 0, m = mb_head; m != NULL; m = m->m_next) {
800 * If this mbuf has no data, skip it.
807 * This appears to be a bogus check the PRO1000T.
808 * I think they meant that the minimum packet size
809 * is in fact WX_MIN_XPKT_SIZE (all data loaded)
813 * If this mbuf is too small for the chip's minimum,
814 * break out to cluster it.
816 if (m->m_len < WX_MIN_XPKT_SIZE) {
823 * Do we have a descriptor available for this mbuf?
825 if (++nactv == WX_MAX_TDESC) {
826 if (gctried++ == 0) {
833 sc->tbase[cidx].dptr = m;
834 td = &sc->tdescriptors[cidx];
835 td->length = m->m_len;
838 vptr = mtod(m, vm_offset_t);
839 td->address.highpart = 0;
840 td->address.lowpart = vtophys(vptr);
849 printf("%s: XMIT[%d] %p vptr %lx (length %d "
850 "DMA addr %x) idx %d\n", sc->wx_name,
851 ndesc, m, (long) vptr, td->length,
852 td->address.lowpart, cidx);
855 cidx = T_NXT_IDX(cidx);
859 * If we get here and m is NULL, we can send
860 * the the packet chain described by mb_head.
864 * Mark the last descriptor with EOP and tell the
865 * chip to insert a final checksum.
867 wxtd_t *td = &sc->tdescriptors[T_PREV_IDX(cidx)];
868 td->cmd = TXCMD_EOP|TXCMD_IFCS;
870 * Set up a delayed interrupt when this packet
871 * is sent and the descriptor written back.
872 * Additional packets completing will cause
873 * interrupt to be delayed further. Therefore,
874 * after the *last* packet is sent, after the delay
875 * period in TIDV, an interrupt will be generated
876 * which will cause us to garbage collect.
878 td->cmd |= TXCMD_IDE|TXCMD_RPS;
881 * Don't xmit odd length packets.
882 * We're okay with bumping things
883 * up as long as our mbuf allocation
884 * is always larger than our MTU
885 * by a comfortable amount.
887 * Yes, it's a hole to run past the end
895 sc->tbase[sc->tnxtfree].sidx = sc->tnxtfree;
896 sc->tbase[sc->tnxtfree].eidx = cidx;
897 sc->tbase[sc->tnxtfree].next = NULL;
899 sc->tbsyl->next = &sc->tbase[sc->tnxtfree];
901 sc->tbsyf = &sc->tbase[sc->tnxtfree];
903 sc->tbsyl = &sc->tbase[sc->tnxtfree];
908 bpf_mtap(WX_BPFTAP_ARG(ifp), mb_head);
909 /* defer xmit until we've got them all */
915 * Otherwise, we couldn't send this packet for some reason.
917 * If don't have a descriptor available, and this is a
918 * single mbuf packet, freeze output so that later we
919 * can restart when we have more room. Otherwise, we'll
920 * try and cluster the request. We've already tried to
921 * garbage collect completed descriptors.
923 if (nactv == WX_MAX_TDESC && mb_head->m_next == NULL) {
924 sc->wx_xmitputback++;
925 ifp->if_flags |= IFF_OACTIVE;
926 IF_PREPEND(&ifp->if_snd, mb_head);
931 * Otherwise, it's either a fragment length somewhere in the
932 * chain that isn't at least WX_MIN_XPKT_SIZE in length or
933 * the number of fragments exceeds the number of descriptors
936 * We could try a variety of strategies here- if this is
937 * a length problem for single mbuf packet or a length problem
938 * for the last mbuf in a chain (we could just try and adjust
939 * it), but it's just simpler to try and cluster it.
941 MGETHDR(m, MB_DONTWAIT, MT_DATA);
946 MCLGET(m, MB_DONTWAIT);
947 if ((m->m_flags & M_EXT) == 0) {
952 m_copydata(mb_head, 0, mb_head->m_pkthdr.len, mtod(m, caddr_t));
953 m->m_pkthdr.len = m->m_len = mb_head->m_pkthdr.len;
956 sc->wx_xmitcluster++;
960 if (widx < WX_MAX_TDESC) {
961 if (IS_WISEMAN(sc)) {
962 WRITE_CSR(sc, WXREG_TDT, widx);
964 WRITE_CSR(sc, WXREG_TDT_LIVENGOOD, widx);
968 if (sc->tactive == WX_MAX_TDESC - 1) {
971 if (sc->tactive >= WX_MAX_TDESC - 1) {
972 sc->wx_xmitblocked++;
973 ifp->if_flags |= IFF_OACTIVE;
977 /* used SW LED to indicate transmission active */
978 if (sc->tactive > 0 && sc->wx_mii) {
979 WRITE_CSR(sc, WXREG_DCR,
980 READ_CSR(sc, WXREG_DCR) | (WXDCR_SWDPIO0|WXDCR_SWDPIN0));
986 * Process interface interrupts.
991 wx_softc_t *sc = arg;
996 * Read interrupt cause register. Reading it clears bits.
998 sc->wx_icr = READ_CSR(sc, WXREG_ICR);
1003 if (sc->wx_icr & (WXISR_LSC|WXISR_RXSEQ|WXISR_GPI_EN1)) {
1005 wx_handle_link_intr(sc);
1007 wx_handle_rxint(sc);
1008 if (sc->wx_icr & WXISR_TXDW) {
1013 if (sc->wx_icr & WXISR_TXQE) {
1018 if (sc->wx_if.if_snd.ifq_head != NULL) {
1019 wx_start(&sc->wx_if);
1028 wx_handle_link_intr(wx_softc_t *sc)
1030 u_int32_t txcw, rxcw, dcr, dsr;
1033 dcr = READ_CSR(sc, WXREG_DCR);
1034 DPRINTF(sc, ("%s: handle_link_intr: icr=%#x dcr=%#x\n",
1035 sc->wx_name, sc->wx_icr, dcr));
1037 mii_data_t *mii = WX_MII_FROM_SOFTC(sc);
1039 if (mii->mii_media_status & IFM_ACTIVE) {
1040 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE) {
1041 IPRINTF(sc, (ldn, sc->wx_name));
1044 IPRINTF(sc, (lup, sc->wx_name));
1047 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
1048 } else if (sc->wx_icr & WXISR_RXSEQ) {
1049 DPRINTF(sc, (sqe, sc->wx_name));
1054 txcw = READ_CSR(sc, WXREG_XMIT_CFGW);
1055 rxcw = READ_CSR(sc, WXREG_RECV_CFGW);
1056 dsr = READ_CSR(sc, WXREG_DSR);
1059 * If we have LOS or are now receiving Ordered Sets and are not
1060 * doing auto-negotiation, restore autonegotiation.
1063 if (((dcr & WXDCR_SWDPIN1) || (rxcw & WXRXCW_C)) &&
1064 ((txcw & WXTXCW_ANE) == 0)) {
1065 DPRINTF(sc, (ane, sc->wx_name));
1066 WRITE_CSR(sc, WXREG_XMIT_CFGW, WXTXCW_DEFAULT);
1067 sc->wx_dcr &= ~WXDCR_SLU;
1068 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
1072 if (sc->wx_icr & WXISR_LSC) {
1073 if (READ_CSR(sc, WXREG_DSR) & WXDSR_LU) {
1074 IPRINTF(sc, (lup, sc->wx_name));
1076 sc->wx_dcr |= (WXDCR_SWDPIO0|WXDCR_SWDPIN0);
1078 IPRINTF(sc, (ldn, sc->wx_name));
1080 sc->wx_dcr &= ~(WXDCR_SWDPIO0|WXDCR_SWDPIN0);
1082 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
1084 DPRINTF(sc, (sqe, sc->wx_name));
1089 wx_check_link(wx_softc_t *sc)
1091 u_int32_t rxcw, dcr, dsr;
1094 mii_pollstat(WX_MII_FROM_SOFTC(sc));
1098 rxcw = READ_CSR(sc, WXREG_RECV_CFGW);
1099 dcr = READ_CSR(sc, WXREG_DCR);
1100 dsr = READ_CSR(sc, WXREG_DSR);
1102 if ((dsr & WXDSR_LU) == 0 && (dcr & WXDCR_SWDPIN1) == 0 &&
1103 (rxcw & WXRXCW_C) == 0) {
1104 if (sc->ane_failed == 0) {
1108 DPRINTF(sc, (inane, sc->wx_name));
1109 WRITE_CSR(sc, WXREG_XMIT_CFGW, WXTXCW_DEFAULT & ~WXTXCW_ANE);
1110 if (sc->wx_idnrev < WX_WISEMAN_2_1)
1111 sc->wx_dcr &= ~WXDCR_TFCE;
1112 sc->wx_dcr |= WXDCR_SLU;
1113 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
1114 } else if ((rxcw & WXRXCW_C) != 0 && (dcr & WXDCR_SLU) != 0) {
1115 DPRINTF(sc, (ane, sc->wx_name));
1116 WRITE_CSR(sc, WXREG_XMIT_CFGW, WXTXCW_DEFAULT);
1117 sc->wx_dcr &= ~WXDCR_SLU;
1118 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
1123 wx_handle_rxint(wx_softc_t *sc)
1125 struct mbuf *m0, *mb, *pending[WX_MAX_RDESC];
1126 struct ifnet *ifp = &sc->wx_if;
1127 int npkts, ndesc, lidx, idx, tlen;
1129 DPRINTF(sc, ("%s: wx_handle_rxint\n", sc->wx_name));
1131 for (m0 = sc->rpending, tlen = ndesc = npkts = 0, idx = sc->rnxt,
1132 lidx = R_PREV_IDX(idx); ndesc < WX_MAX_RDESC;
1133 ndesc++, lidx = idx, idx = R_NXT_IDX(idx)) {
1136 int length, offset, lastframe;
1138 rd = &sc->rdescriptors[idx];
1140 * XXX: DMA Flush descriptor
1142 if ((rd->status & RDSTAT_DD) == 0) {
1144 if (sc->rpending == NULL) {
1145 m0->m_pkthdr.len = tlen;
1152 DPRINTF(sc, ("%s: WXRX: ndesc %d idx %d lidx %d\n",
1153 sc->wx_name, ndesc, idx, lidx));
1157 if (rd->errors != 0) {
1158 printf("%s: packet with errors (%x)\n",
1159 sc->wx_name, rd->errors);
1166 m_freem(sc->rpending);
1167 sc->rpending = NULL;
1174 rxpkt = &sc->rbase[idx];
1177 printf("%s: receive descriptor with no mbuf\n",
1179 (void) wx_get_rbuf(sc, rxpkt);
1186 m_freem(sc->rpending);
1187 sc->rpending = NULL;
1193 /* XXX: Flush DMA for rxpkt */
1195 if (wx_get_rbuf(sc, rxpkt)) {
1197 wx_rxdma_map(sc, rxpkt, mb);
1204 m_freem(sc->rpending);
1205 sc->rpending = NULL;
1212 * Save the completing packet's offset value and length
1213 * and install the new one into the descriptor.
1215 lastframe = (rd->status & RDSTAT_EOP) != 0;
1216 length = rd->length;
1217 offset = rd->address.lowpart & 0xff;
1218 bzero (rd, sizeof (*rd));
1219 rd->address.lowpart = rxpkt->dma_addr + WX_RX_OFFSET_VALUE;
1222 mb->m_data += offset;
1227 } else if (m0 == sc->rpending) {
1229 * Pick up where we left off before. If
1230 * we have an offset (we're assuming the
1231 * first frame has an offset), then we've
1232 * lost sync somewhere along the line.
1235 printf("%s: lost sync with partial packet\n",
1237 m_freem(sc->rpending);
1238 sc->rpending = NULL;
1242 sc->rpending = NULL;
1243 tlen = m0->m_pkthdr.len;
1249 DPRINTF(sc, ("%s: RDESC[%d] len %d off %d lastframe %d\n",
1250 sc->wx_name, idx, mb->m_len, offset, lastframe));
1253 if (lastframe == 0) {
1256 m0->m_pkthdr.rcvif = ifp;
1257 m0->m_pkthdr.len = tlen - WX_CRC_LENGTH;
1258 mb->m_len -= WX_CRC_LENGTH;
1261 * No need to check for promiscous mode since
1262 * the decision to keep or drop the packet is
1263 * handled by ether_input()
1265 pending[npkts++] = m0;
1271 if (IS_WISEMAN(sc)) {
1272 WRITE_CSR(sc, WXREG_RDT0, lidx);
1274 WRITE_CSR(sc, WXREG_RDT0_LIVENGOOD, lidx);
1283 for (idx = 0; idx < npkts; idx++) {
1286 bpf_mtap(WX_BPFTAP_ARG(ifp), mb);
1289 DPRINTF(sc, ("%s: RECV packet length %d\n",
1290 sc->wx_name, mb->m_pkthdr.len));
1291 (*ifp->if_input)(ifp, mb);
1296 wx_gc(wx_softc_t *sc)
1298 struct ifnet *ifp = &sc->wx_if;
1304 if (IS_WISEMAN(sc)) {
1305 tdh = READ_CSR(sc, WXREG_TDH);
1307 tdh = READ_CSR(sc, WXREG_TDH_LIVENGOOD);
1309 while (txpkt != NULL) {
1310 u_int32_t end = txpkt->eidx, cidx = tdh;
1313 * Normalize start..end indices to 2 *
1314 * WX_MAX_TDESC range to eliminate wrap.
1316 if (txpkt->eidx < txpkt->sidx) {
1317 end += WX_MAX_TDESC;
1321 * Normalize current chip index to 2 *
1322 * WX_MAX_TDESC range to eliminate wrap.
1324 if (cidx < txpkt->sidx) {
1325 cidx += WX_MAX_TDESC;
1329 * If the current chip index is between low and
1330 * high indices for this packet, it's not finished
1331 * transmitting yet. Because transmits are done FIFO,
1332 * this means we're done garbage collecting too.
1335 if (txpkt->sidx <= cidx && cidx < txpkt->eidx) {
1336 DPRINTF(sc, ("%s: TXGC %d..%d TDH %d\n", sc->wx_name,
1337 txpkt->sidx, txpkt->eidx, tdh));
1343 (void) m_freem(txpkt->dptr);
1345 printf("%s: null mbuf in gc\n", sc->wx_name);
1348 for (cidx = txpkt->sidx; cidx != txpkt->eidx;
1349 cidx = T_NXT_IDX(cidx)) {
1353 td = &sc->tdescriptors[cidx];
1354 if (td->status & TXSTS_EC) {
1355 IPRINTF(sc, ("%s: excess collisions\n",
1357 ifp->if_collisions++;
1360 if (td->status & TXSTS_LC) {
1362 ("%s: lost carrier\n", sc->wx_name));
1365 tmp = &sc->tbase[cidx];
1366 DPRINTF(sc, ("%s: TXGC[%d] %p %d..%d done nact %d "
1367 "TDH %d\n", sc->wx_name, cidx, tmp->dptr,
1368 txpkt->sidx, txpkt->eidx, sc->tactive, tdh));
1370 if (sc->tactive == 0) {
1371 printf("%s: nactive < 0?\n", sc->wx_name);
1375 bzero(td, sizeof (*td));
1377 sc->tbsyf = txpkt->next;
1380 if (sc->tactive < WX_MAX_TDESC - 1) {
1382 ifp->if_flags &= ~IFF_OACTIVE;
1385 /* used SW LED to indicate transmission not active */
1386 if (sc->tactive == 0 && sc->wx_mii) {
1387 WRITE_CSR(sc, WXREG_DCR,
1388 READ_CSR(sc, WXREG_DCR) & ~(WXDCR_SWDPIO0|WXDCR_SWDPIN0));
1394 * Periodic timer to update packet in/out/collision statistics,
1395 * and, more importantly, garbage collect completed transmissions
1396 * and to handle link status changes.
1398 #define WX_PRT_STATS(sc, y) printf("\t" # y " = %u\n", ((sc)->y))
1399 #define WX_CLR_STATS(sc, y) ((sc)->y = 0)
1402 wx_watchdog(void *arg)
1404 wx_softc_t *sc = arg;
1407 if (sc->wx_needreinit) {
1409 if (wx_init(sc) == 0) {
1411 sc->wx_needreinit = 0;
1419 if (wx_dump_stats == device_get_unit(sc->w.dev)) {
1420 printf("%s: current statistics\n", sc->wx_name);
1421 WX_PRT_STATS(sc, wx_intr);
1422 WX_PRT_STATS(sc, wx_linkintr);
1423 WX_PRT_STATS(sc, wx_rxintr);
1424 WX_PRT_STATS(sc, wx_txqe);
1425 WX_PRT_STATS(sc, wx_xmitgc);
1426 WX_PRT_STATS(sc, wx_xmitpullup);
1427 WX_PRT_STATS(sc, wx_xmitcluster);
1428 WX_PRT_STATS(sc, wx_xmitputback);
1429 WX_PRT_STATS(sc, wx_xmitwanted);
1430 WX_PRT_STATS(sc, wx_xmitblocked);
1431 WX_PRT_STATS(sc, wx_xmitrunt);
1432 WX_PRT_STATS(sc, wx_rxnobuf);
1433 WX_PRT_STATS(sc, wx_oddpkt);
1436 if (wx_clr_stats == device_get_unit(sc->w.dev)) {
1437 printf("%s: statistics cleared\n", sc->wx_name);
1438 WX_CLR_STATS(sc, wx_intr);
1439 WX_CLR_STATS(sc, wx_linkintr);
1440 WX_CLR_STATS(sc, wx_rxintr);
1441 WX_CLR_STATS(sc, wx_txqe);
1442 WX_CLR_STATS(sc, wx_xmitgc);
1443 WX_CLR_STATS(sc, wx_xmitpullup);
1444 WX_CLR_STATS(sc, wx_xmitcluster);
1445 WX_CLR_STATS(sc, wx_xmitputback);
1446 WX_CLR_STATS(sc, wx_xmitwanted);
1447 WX_CLR_STATS(sc, wx_xmitblocked);
1448 WX_CLR_STATS(sc, wx_xmitrunt);
1449 WX_CLR_STATS(sc, wx_rxnobuf);
1450 WX_CLR_STATS(sc, wx_oddpkt);
1456 * Schedule another timeout one second from now.
1458 callout_reset(&sc->watchdog_timer, hz, wx_watchdog, sc);
1462 * Stop and reinitialize the hardware
1465 wx_hw_stop(wx_softc_t *sc)
1468 DPRINTF(sc, ("%s: wx_hw_stop\n", sc->wx_name));
1470 if (sc->wx_idnrev < WX_WISEMAN_2_1) {
1473 WRITE_CSR(sc, WXREG_DCR, WXDCR_RST);
1475 icr = READ_CSR(sc, WXREG_ICR);
1476 if (sc->wx_idnrev < WX_WISEMAN_2_1) {
1482 wx_set_addr(wx_softc_t *sc, int idx, u_int8_t *mac)
1485 DPRINTF(sc, ("%s: wx_set_addr\n", sc->wx_name));
1486 t0 = (mac[0]) | (mac[1] << 8) | (mac[2] << 16) | (mac[3] << 24);
1487 t1 = (mac[4] << 0) | (mac[5] << 8);
1489 WRITE_CSR(sc, WXREG_RAL_LO(idx), t0);
1490 WRITE_CSR(sc, WXREG_RAL_HI(idx), t1);
1494 wx_hw_initialize(wx_softc_t *sc)
1498 DPRINTF(sc, ("%s: wx_hw_initialize\n", sc->wx_name));
1500 WRITE_CSR(sc, WXREG_VET, 0);
1501 for (i = 0; i < (WX_VLAN_TAB_SIZE << 2); i += 4) {
1502 WRITE_CSR(sc, (WXREG_VFTA + i), 0);
1504 if (sc->wx_idnrev < WX_WISEMAN_2_1) {
1506 WRITE_CSR(sc, WXREG_RCTL, WXRCTL_RST);
1510 * Load the first receiver address with our MAC address,
1511 * and load as many multicast addresses as can fit into
1512 * the receive address array.
1514 wx_set_addr(sc, 0, sc->wx_enaddr);
1515 for (i = 1; i <= sc->wx_nmca; i++) {
1516 if (i >= WX_RAL_TAB_SIZE) {
1519 wx_set_addr(sc, i, sc->wx_mcaddr[i-1]);
1523 while (i < WX_RAL_TAB_SIZE) {
1524 WRITE_CSR(sc, WXREG_RAL_LO(i), 0);
1525 WRITE_CSR(sc, WXREG_RAL_HI(i), 0);
1529 if (sc->wx_idnrev < WX_WISEMAN_2_1) {
1530 WRITE_CSR(sc, WXREG_RCTL, 0);
1536 * Clear out the hashed multicast table array.
1538 for (i = 0; i < WX_MC_TAB_SIZE; i++) {
1539 WRITE_CSR(sc, WXREG_MTA + (sizeof (u_int32_t) * 4), 0);
1542 if (IS_LIVENGOOD_CU(sc)) {
1544 * has a PHY - raise its reset line to make it operational
1546 u_int32_t tmp = READ_CSR(sc, WXREG_EXCT);
1547 tmp |= WXPHY_RESET_DIR4;
1548 WRITE_CSR(sc, WXREG_EXCT, tmp);
1551 tmp = READ_CSR(sc, WXREG_EXCT);
1552 tmp &= ~WXPHY_RESET4;
1553 WRITE_CSR(sc, WXREG_EXCT, tmp);
1556 tmp = READ_CSR(sc, WXREG_EXCT);
1557 tmp |= WXPHY_RESET4;
1558 WRITE_CSR(sc, WXREG_EXCT, tmp);
1560 } else if (IS_LIVENGOOD(sc)) {
1564 * Handle link control
1566 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr | WXDCR_LRST);
1569 wx_read_eeprom(sc, &tew, WX_EEPROM_CTLR2_OFF, 1);
1570 tew = (tew & WX_EEPROM_CTLR2_SWDPIO) << WX_EEPROM_EXT_SHIFT;
1571 WRITE_CSR(sc, WXREG_EXCT, (u_int32_t)tew);
1574 if (sc->wx_dcr & (WXDCR_RFCE|WXDCR_TFCE)) {
1575 WRITE_CSR(sc, WXREG_FCAL, FC_FRM_CONST_LO);
1576 WRITE_CSR(sc, WXREG_FCAH, FC_FRM_CONST_HI);
1577 WRITE_CSR(sc, WXREG_FCT, FC_TYP_CONST);
1579 WRITE_CSR(sc, WXREG_FCAL, 0);
1580 WRITE_CSR(sc, WXREG_FCAH, 0);
1581 WRITE_CSR(sc, WXREG_FCT, 0);
1583 WRITE_CSR(sc, WXREG_FLOW_XTIMER, WX_XTIMER_DFLT);
1585 if (IS_WISEMAN(sc)) {
1586 if (sc->wx_idnrev < WX_WISEMAN_2_1) {
1587 WRITE_CSR(sc, WXREG_FLOW_RCV_HI, 0);
1588 WRITE_CSR(sc, WXREG_FLOW_RCV_LO, 0);
1589 sc->wx_dcr &= ~(WXDCR_RFCE|WXDCR_TFCE);
1591 WRITE_CSR(sc, WXREG_FLOW_RCV_HI, WX_RCV_FLOW_HI_DFLT);
1592 WRITE_CSR(sc, WXREG_FLOW_RCV_LO, WX_RCV_FLOW_LO_DFLT);
1595 WRITE_CSR(sc, WXREG_FLOW_RCV_HI_LIVENGOOD, WX_RCV_FLOW_HI_DFLT);
1596 WRITE_CSR(sc, WXREG_FLOW_RCV_LO_LIVENGOOD, WX_RCV_FLOW_LO_DFLT);
1599 if (!IS_LIVENGOOD_CU(sc))
1600 WRITE_CSR(sc, WXREG_XMIT_CFGW, WXTXCW_DEFAULT);
1602 WRITE_CSR(sc, WXREG_DCR, sc->wx_dcr);
1605 if (!IS_LIVENGOOD_CU(sc)) {
1607 * The pin stuff is all FM from the Linux driver.
1609 if ((READ_CSR(sc, WXREG_DCR) & WXDCR_SWDPIN1) == 0) {
1610 for (i = 0; i < (WX_LINK_UP_TIMEOUT/10); i++) {
1612 if (READ_CSR(sc, WXREG_DSR) & WXDSR_LU) {
1617 if (sc->linkup == 0) {
1623 printf("%s: SWDPIO1 did not clear- check for reversed "
1624 "or disconnected cable\n", sc->wx_name);
1625 /* but return okay anyway */
1629 sc->wx_ienable = WXIENABLE_DEFAULT;
1634 * Stop the interface. Cancels the statistics updater and resets the interface.
1637 wx_stop(wx_softc_t *sc)
1641 struct ifnet *ifp = &sc->wx_if;
1643 DPRINTF(sc, ("%s: wx_stop\n", sc->wx_name));
1645 * Cancel stats updater.
1647 callout_stop(&sc->watchdog_timer);
1655 * Release any xmit buffers.
1657 for (txp = sc->tbase; txp && txp < &sc->tbase[WX_MAX_TDESC]; txp++) {
1665 * Free all the receive buffers.
1667 for (rxp = sc->rbase; rxp && rxp < &sc->rbase[WX_MAX_RDESC]; rxp++) {
1675 m_freem(sc->rpending);
1676 sc->rpending = NULL;
1680 * And we're outta here...
1683 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1691 wx_txwatchdog(struct ifnet *ifp)
1693 wx_softc_t *sc = SOFTC_IFP(ifp);
1694 printf("%s: device timeout\n", sc->wx_name);
1697 printf("%s: could not re-init device\n", sc->wx_name);
1698 sc->wx_needreinit = 1;
1705 struct ifmedia *ifm;
1706 wx_softc_t *sc = xsc;
1707 struct ifnet *ifp = &sc->wx_if;
1713 DPRINTF(sc, ("%s: wx_init\n", sc->wx_name));
1717 * Cancel any pending I/O by resetting things.
1718 * wx_stop will free any allocated mbufs.
1723 * Reset the hardware. All network addresses loaded here, but
1724 * neither the receiver nor the transmitter are enabled.
1727 if (wx_hw_initialize(sc)) {
1728 DPRINTF(sc, ("%s: wx_hw_initialize failed\n", sc->wx_name));
1734 * Set up the receive ring stuff.
1736 len = sizeof (wxrd_t) * WX_MAX_RDESC;
1737 bzero(sc->rdescriptors, len);
1738 for (rxpkt = sc->rbase, i = 0; rxpkt != NULL && i < WX_MAX_RDESC;
1739 i += RXINCR, rxpkt++) {
1740 rd = &sc->rdescriptors[i];
1741 if (wx_get_rbuf(sc, rxpkt)) {
1744 rd->address.lowpart = rxpkt->dma_addr + WX_RX_OFFSET_VALUE;
1746 if (i != WX_MAX_RDESC) {
1747 printf("%s: could not set up rbufs\n", sc->wx_name);
1754 * Set up transmit parameters and enable the transmitter.
1756 sc->tnxtfree = sc->tactive = 0;
1757 sc->tbsyf = sc->tbsyl = NULL;
1758 WRITE_CSR(sc, WXREG_TCTL, 0);
1760 if (IS_WISEMAN(sc)) {
1761 WRITE_CSR(sc, WXREG_TDBA_LO,
1762 vtophys((vm_offset_t)&sc->tdescriptors[0]));
1763 WRITE_CSR(sc, WXREG_TDBA_HI, 0);
1764 WRITE_CSR(sc, WXREG_TDLEN, WX_MAX_TDESC * sizeof (wxtd_t));
1765 WRITE_CSR(sc, WXREG_TDH, 0);
1766 WRITE_CSR(sc, WXREG_TDT, 0);
1767 WRITE_CSR(sc, WXREG_TQSA_HI, 0);
1768 WRITE_CSR(sc, WXREG_TQSA_LO, 0);
1769 WRITE_CSR(sc, WXREG_TIPG, WX_WISEMAN_TIPG_DFLT);
1770 WRITE_CSR(sc, WXREG_TIDV, wx_txint_delay);
1772 WRITE_CSR(sc, WXREG_TDBA_LO_LIVENGOOD,
1773 vtophys((vm_offset_t)&sc->tdescriptors[0]));
1774 WRITE_CSR(sc, WXREG_TDBA_HI_LIVENGOOD, 0);
1775 WRITE_CSR(sc, WXREG_TDLEN_LIVENGOOD,
1776 WX_MAX_TDESC * sizeof (wxtd_t));
1777 WRITE_CSR(sc, WXREG_TDH_LIVENGOOD, 0);
1778 WRITE_CSR(sc, WXREG_TDT_LIVENGOOD, 0);
1779 WRITE_CSR(sc, WXREG_TQSA_HI, 0);
1780 WRITE_CSR(sc, WXREG_TQSA_LO, 0);
1781 WRITE_CSR(sc, WXREG_TIPG, WX_LIVENGOOD_TIPG_DFLT);
1782 WRITE_CSR(sc, WXREG_TIDV_LIVENGOOD, wx_txint_delay);
1784 WRITE_CSR(sc, WXREG_TCTL, (WXTCTL_CT(WX_COLLISION_THRESHOLD) |
1785 WXTCTL_COLD(WX_FDX_COLLISION_DX) | WXTCTL_EN));
1787 * Set up receive parameters and enable the receiver.
1791 WRITE_CSR(sc, WXREG_RCTL, 0);
1793 if (IS_WISEMAN(sc)) {
1794 WRITE_CSR(sc, WXREG_RDTR0, WXRDTR_FPD);
1795 WRITE_CSR(sc, WXREG_RDBA0_LO,
1796 vtophys((vm_offset_t)&sc->rdescriptors[0]));
1797 WRITE_CSR(sc, WXREG_RDBA0_HI, 0);
1798 WRITE_CSR(sc, WXREG_RDLEN0, WX_MAX_RDESC * sizeof (wxrd_t));
1799 WRITE_CSR(sc, WXREG_RDH0, 0);
1800 WRITE_CSR(sc, WXREG_RDT0, (WX_MAX_RDESC - RXINCR));
1803 * The delay should yield ~10us receive interrupt delay
1805 WRITE_CSR(sc, WXREG_RDTR0_LIVENGOOD, WXRDTR_FPD | 0x40);
1806 WRITE_CSR(sc, WXREG_RDBA0_LO_LIVENGOOD,
1807 vtophys((vm_offset_t)&sc->rdescriptors[0]));
1808 WRITE_CSR(sc, WXREG_RDBA0_HI_LIVENGOOD, 0);
1809 WRITE_CSR(sc, WXREG_RDLEN0_LIVENGOOD,
1810 WX_MAX_RDESC * sizeof (wxrd_t));
1811 WRITE_CSR(sc, WXREG_RDH0_LIVENGOOD, 0);
1812 WRITE_CSR(sc, WXREG_RDT0_LIVENGOOD, (WX_MAX_RDESC - RXINCR));
1814 WRITE_CSR(sc, WXREG_RDTR1, 0);
1815 WRITE_CSR(sc, WXREG_RDBA1_LO, 0);
1816 WRITE_CSR(sc, WXREG_RDBA1_HI, 0);
1817 WRITE_CSR(sc, WXREG_RDLEN1, 0);
1818 WRITE_CSR(sc, WXREG_RDH1, 0);
1819 WRITE_CSR(sc, WXREG_RDT1, 0);
1821 if (ifp->if_mtu > ETHERMTU) {
1822 bflags = WXRCTL_EN | WXRCTL_LPE | WXRCTL_2KRBUF;
1824 bflags = WXRCTL_EN | WXRCTL_2KRBUF;
1827 WRITE_CSR(sc, WXREG_RCTL, bflags |
1828 ((ifp->if_flags & IFF_BROADCAST) ? WXRCTL_BAM : 0) |
1829 ((ifp->if_flags & IFF_PROMISC) ? WXRCTL_UPE : 0) |
1830 ((sc->all_mcasts) ? WXRCTL_MPE : 0));
1838 mii_mediachg(WX_MII_FROM_SOFTC(sc));
1840 ifm = &sc->wx_media;
1842 ifm->ifm_media = ifm->ifm_cur->ifm_media;
1843 wx_ifmedia_upd(ifp);
1848 * Mark that we're up and running...
1850 ifp->if_flags |= IFF_RUNNING;
1851 ifp->if_flags &= ~IFF_OACTIVE;
1855 * Start stats updater.
1857 callout_reset(&sc->watchdog_timer, hz, wx_watchdog, sc);
1861 * And we're outta here...
1867 * Get a receive buffer for our use (and dma map the data area).
1869 * The Wiseman chip can have buffers be 256, 512, 1024 or 2048 bytes in size.
1870 * The LIVENGOOD chip can go higher (up to 16K), but what's the point as
1871 * we aren't doing non-MCLGET memory management.
1873 * It wants them aligned on 256 byte boundaries, but can actually cope
1874 * with an offset in the first 255 bytes of the head of a receive frame.
1876 * We'll allocate a MCLBYTE sized cluster but *not* adjust the data pointer
1877 * by any alignment value. Instead, we'll tell the chip to offset by any
1878 * alignment and we'll catch the alignment on the backend at interrupt time.
1881 wx_rxdma_map(wx_softc_t *sc, rxpkt_t *rxpkt, struct mbuf *mb)
1884 rxpkt->dma_addr = vtophys(mtod(mb, vm_offset_t));
1888 wx_get_rbuf(wx_softc_t *sc, rxpkt_t *rxpkt)
1891 MGETHDR(mb, MB_DONTWAIT, MT_DATA);
1896 MCLGET(mb, MB_DONTWAIT);
1897 if ((mb->m_flags & M_EXT) == 0) {
1902 wx_rxdma_map(sc, rxpkt, mb);
1907 wx_ioctl(struct ifnet *ifp, IOCTL_CMD_TYPE command, caddr_t data,
1910 wx_softc_t *sc = SOFTC_IFP(ifp);
1911 struct ifreq *ifr = (struct ifreq *) data;
1918 error = ether_ioctl(ifp, command, data);
1921 if (ifr->ifr_mtu > WX_MAXMTU || ifr->ifr_mtu < ETHERMIN) {
1923 } else if (ifp->if_mtu != ifr->ifr_mtu) {
1924 ifp->if_mtu = ifr->ifr_mtu;
1925 error = wx_init(sc);
1929 sc->all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1932 * If interface is marked up and not running, then start it.
1933 * If it is marked down and running, stop it.
1934 * If it's up then re-initialize it. This is so flags
1935 * such as IFF_PROMISC are handled.
1937 if (ifp->if_flags & IFF_UP) {
1938 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1939 error = wx_init(sc);
1942 if (ifp->if_flags & IFF_RUNNING) {
1950 sc->all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
1951 error = wx_mc_setup(sc);
1955 DPRINTF(sc, ("%s: ioctl SIOC[GS]IFMEDIA: command=%#lx\n",
1956 sc->wx_name, command));
1958 mii_data_t *mii = WX_MII_FROM_SOFTC(sc);
1959 error = ifmedia_ioctl(ifp, ifr,
1960 &mii->mii_media, command);
1962 error = ifmedia_ioctl(ifp, ifr, &sc->wx_media, command);
1975 wx_ifmedia_upd(struct ifnet *ifp)
1977 struct wx_softc *sc = SOFTC_IFP(ifp);
1978 struct ifmedia *ifm;
1980 DPRINTF(sc, ("%s: ifmedia_upd\n", sc->wx_name));
1983 mii_mediachg(WX_MII_FROM_SOFTC(sc));
1987 ifm = &sc->wx_media;
1989 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
1997 wx_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2000 struct wx_softc *sc = SOFTC_IFP(ifp);
2002 DPRINTF(sc, ("%s: ifmedia_sts: ", sc->wx_name));
2005 mii_data_t *mii = WX_MII_FROM_SOFTC(sc);
2007 ifmr->ifm_active = mii->mii_media_active;
2008 ifmr->ifm_status = mii->mii_media_status;
2009 DPRINTF(sc, ("active=%#x status=%#x\n",
2010 ifmr->ifm_active, ifmr->ifm_status));
2014 DPRINTF(sc, ("\n"));
2015 ifmr->ifm_status = IFM_AVALID;
2016 ifmr->ifm_active = IFM_ETHER;
2018 if (sc->linkup == 0)
2021 ifmr->ifm_status |= IFM_ACTIVE;
2022 dsr = READ_CSR(sc, WXREG_DSR);
2023 if (IS_LIVENGOOD(sc)) {
2024 if (dsr & WXDSR_1000BT) {
2025 if (IS_LIVENGOOD_CU(sc)) {
2026 ifmr->ifm_status |= IFM_1000_TX;
2029 ifmr->ifm_status |= IFM_1000_SX;
2031 } else if (dsr & WXDSR_100BT) {
2032 ifmr->ifm_status |= IFM_100_FX; /* ?? */
2034 ifmr->ifm_status |= IFM_10_T; /* ?? */
2037 ifmr->ifm_status |= IFM_1000_SX;
2039 if (dsr & WXDSR_FD) {
2040 ifmr->ifm_active |= IFM_FDX;
2045 #define RAISE_CLOCK(sc, dcr) \
2046 WRITE_CSR(sc, WXREG_DCR, (dcr) | WXPHY_MDC), DELAY(2)
2048 #define LOWER_CLOCK(sc, dcr) \
2049 WRITE_CSR(sc, WXREG_DCR, (dcr) & ~WXPHY_MDC), DELAY(2)
2052 wx_mii_shift_in(wx_softc_t *sc)
2057 dcr = READ_CSR(sc, WXREG_DCR);
2058 dcr &= ~(WXPHY_MDIO_DIR | WXPHY_MDIO);
2059 WRITE_CSR(sc, WXREG_DCR, dcr);
2060 RAISE_CLOCK(sc, dcr);
2061 LOWER_CLOCK(sc, dcr);
2063 for (i = 0; i < 16; i++) {
2065 RAISE_CLOCK(sc, dcr);
2066 dcr = READ_CSR(sc, WXREG_DCR);
2068 if (dcr & WXPHY_MDIO)
2071 LOWER_CLOCK(sc, dcr);
2074 RAISE_CLOCK(sc, dcr);
2075 LOWER_CLOCK(sc, dcr);
2080 wx_mii_shift_out(wx_softc_t *sc, u_int32_t data, u_int32_t count)
2082 u_int32_t dcr, mask;
2084 dcr = READ_CSR(sc, WXREG_DCR);
2085 dcr |= WXPHY_MDIO_DIR | WXPHY_MDC_DIR;
2087 for (mask = (1 << (count - 1)); mask; mask >>= 1) {
2093 WRITE_CSR(sc, WXREG_DCR, dcr);
2095 RAISE_CLOCK(sc, dcr);
2096 LOWER_CLOCK(sc, dcr);
2101 wx_miibus_readreg(void *arg, int phy, int reg)
2103 wx_softc_t *sc = WX_SOFTC_FROM_MII_ARG(arg);
2104 unsigned int data = 0;
2106 if (!IS_LIVENGOOD_CU(sc)) {
2109 wx_mii_shift_out(sc, WXPHYC_PREAMBLE, WXPHYC_PREAMBLE_LEN);
2110 wx_mii_shift_out(sc, reg | (phy << 5) | (WXPHYC_READ << 10) |
2111 (WXPHYC_SOF << 12), 14);
2112 data = wx_mii_shift_in(sc);
2113 return (data & WXMDIC_DATA_MASK);
2117 wx_miibus_writereg(void *arg, int phy, int reg, int data)
2119 wx_softc_t *sc = WX_SOFTC_FROM_MII_ARG(arg);
2120 if (!IS_LIVENGOOD_CU(sc)) {
2123 wx_mii_shift_out(sc, WXPHYC_PREAMBLE, WXPHYC_PREAMBLE_LEN);
2124 wx_mii_shift_out(sc, (u_int32_t)data | (WXPHYC_TURNAROUND << 16) |
2125 (reg << 18) | (phy << 23) | (WXPHYC_WRITE << 28) |
2126 (WXPHYC_SOF << 30), 32);
2131 wx_miibus_statchg(void *arg)
2133 wx_softc_t *sc = WX_SOFTC_FROM_MII_ARG(arg);
2134 mii_data_t *mii = WX_MII_FROM_SOFTC(sc);
2135 u_int32_t dcr, tctl;
2141 tctl = READ_CSR(sc, WXREG_TCTL);
2142 DPRINTF(sc, ("%s: statchg dcr=%#x tctl=%#x", sc->wx_name, dcr, tctl));
2144 dcr |= WXDCR_FRCSPD | WXDCR_FRCDPX | WXDCR_SLU;
2145 dcr &= ~(WXDCR_SPEED_MASK | WXDCR_ASDE /* | WXDCR_ILOS */);
2147 if (mii->mii_media_status & IFM_ACTIVE) {
2148 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE) {
2149 DPRINTF(sc, (" link-down\n"));
2157 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_TX) {
2158 DPRINTF(sc, (" 1000TX"));
2159 dcr |= WXDCR_1000BT;
2160 } else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
2161 DPRINTF(sc, (" 100TX"));
2163 } else /* assume IFM_10_TX */ {
2164 DPRINTF(sc, (" 10TX"));
2168 if (mii->mii_media_active & IFM_FDX) {
2169 DPRINTF(sc, ("-FD"));
2170 tctl = WXTCTL_CT(WX_COLLISION_THRESHOLD) |
2171 WXTCTL_COLD(WX_FDX_COLLISION_DX) | WXTCTL_EN;
2174 DPRINTF(sc, ("-HD"));
2175 tctl = WXTCTL_CT(WX_COLLISION_THRESHOLD) |
2176 WXTCTL_COLD(WX_HDX_COLLISION_DX) | WXTCTL_EN;
2180 /* FLAG0==rx-flow-control FLAG1==tx-flow-control */
2181 if (mii->mii_media_active & IFM_FLAG0) {
2187 if (mii->mii_media_active & IFM_FLAG1) {
2193 if (dcr & (WXDCR_RFCE|WXDCR_TFCE)) {
2194 WRITE_CSR(sc, WXREG_FCAL, FC_FRM_CONST_LO);
2195 WRITE_CSR(sc, WXREG_FCAH, FC_FRM_CONST_HI);
2196 WRITE_CSR(sc, WXREG_FCT, FC_TYP_CONST);
2198 WRITE_CSR(sc, WXREG_FCAL, 0);
2199 WRITE_CSR(sc, WXREG_FCAH, 0);
2200 WRITE_CSR(sc, WXREG_FCT, 0);
2203 DPRINTF(sc, (" dcr=%#x tctl=%#x\n", dcr, tctl));
2204 WRITE_CSR(sc, WXREG_TCTL, tctl);
2206 WRITE_CSR(sc, WXREG_DCR, dcr);
2210 wx_miibus_mediainit(void *arg)