1 /* $OpenBSD: if_txp.c,v 1.48 2001/06/27 06:34:50 kjc Exp $ */
2 /* $FreeBSD: src/sys/dev/txp/if_txp.c,v 1.4.2.4 2001/12/14 19:50:43 jlemon Exp $ */
6 * Jason L. Wright <jason@thought.net>, Theo de Raadt, and
7 * Aaron Campbell <aaron@monkey.org>. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Jason L. Wright,
20 * Theo de Raadt and Aaron Campbell.
21 * 4. Neither the name of the author nor the names of any co-contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35 * THE POSSIBILITY OF SUCH DAMAGE.
39 * Driver for 3c990 (Typhoon) Ethernet ASIC
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/sockio.h>
46 #include <sys/malloc.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/serialize.h>
52 #include <sys/thread2.h>
53 #include <sys/interrupt.h>
56 #include <net/ifq_var.h>
57 #include <net/if_arp.h>
58 #include <net/ethernet.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/vlan/if_vlan_var.h>
62 #include <net/vlan/if_vlan_ether.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip.h>
68 #include <netinet/if_ether.h>
69 #include <sys/in_cksum.h>
71 #include <net/if_media.h>
75 #include <vm/vm.h> /* for vtophys */
76 #include <vm/pmap.h> /* for vtophys */
78 #include "../mii_layer/mii.h"
79 #include "../mii_layer/miivar.h"
81 #include <bus/pci/pcidevs.h>
82 #include <bus/pci/pcireg.h>
83 #include <bus/pci/pcivar.h>
85 #define TXP_USEIOSPACE
86 #define __STRICT_ALIGNMENT
88 #include "if_txpreg.h"
92 * Various supported device vendors/types and their names.
94 static struct txp_type txp_devs[] = {
95 { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CR990TX95,
96 "3Com 3cR990-TX-95 Etherlink with 3XP Processor" },
97 { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CR990TX97,
98 "3Com 3cR990-TX-97 Etherlink with 3XP Processor" },
99 { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C990B,
100 "3Com 3cR990B-TXM Etherlink with 3XP Processor" },
101 { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CR990SVR95,
102 "3Com 3cR990-SRV-95 Etherlink Server with 3XP Processor" },
103 { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CR990SVR97,
104 "3Com 3cR990-SRV-97 Etherlink Server with 3XP Processor" },
105 { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C990BSVR,
106 "3Com 3cR990B-SRV Etherlink Server with 3XP Processor" },
110 static int txp_probe (device_t);
111 static int txp_attach (device_t);
112 static int txp_detach (device_t);
113 static void txp_intr (void *);
114 static void txp_tick (void *);
115 static int txp_shutdown (device_t);
116 static int txp_ioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
117 static void txp_start (struct ifnet *);
118 static void txp_stop (struct txp_softc *);
119 static void txp_init (void *);
120 static void txp_watchdog (struct ifnet *);
122 static void txp_release_resources (device_t);
123 static int txp_chip_init (struct txp_softc *);
124 static int txp_reset_adapter (struct txp_softc *);
125 static int txp_download_fw (struct txp_softc *);
126 static int txp_download_fw_wait (struct txp_softc *);
127 static int txp_download_fw_section (struct txp_softc *,
128 struct txp_fw_section_header *, int);
129 static int txp_alloc_rings (struct txp_softc *);
130 static int txp_rxring_fill (struct txp_softc *);
131 static void txp_rxring_empty (struct txp_softc *);
132 static void txp_set_filter (struct txp_softc *);
134 static int txp_cmd_desc_numfree (struct txp_softc *);
135 static int txp_command (struct txp_softc *, u_int16_t, u_int16_t, u_int32_t,
136 u_int32_t, u_int16_t *, u_int32_t *, u_int32_t *, int);
137 static int txp_command2 (struct txp_softc *, u_int16_t, u_int16_t,
138 u_int32_t, u_int32_t, struct txp_ext_desc *, u_int8_t,
139 struct txp_rsp_desc **, int);
140 static int txp_response (struct txp_softc *, u_int32_t, u_int16_t, u_int16_t,
141 struct txp_rsp_desc **);
142 static void txp_rsp_fixup (struct txp_softc *, struct txp_rsp_desc *,
143 struct txp_rsp_desc *);
144 static void txp_capabilities (struct txp_softc *);
146 static void txp_ifmedia_sts (struct ifnet *, struct ifmediareq *);
147 static int txp_ifmedia_upd (struct ifnet *);
149 static void txp_show_descriptor (void *);
151 static void txp_tx_reclaim (struct txp_softc *, struct txp_tx_ring *);
152 static void txp_rxbuf_reclaim (struct txp_softc *);
153 static void txp_rx_reclaim (struct txp_softc *, struct txp_rx_ring *);
155 #ifdef TXP_USEIOSPACE
156 #define TXP_RES SYS_RES_IOPORT
157 #define TXP_RID TXP_PCI_LOIO
159 #define TXP_RES SYS_RES_MEMORY
160 #define TXP_RID TXP_PCI_LOMEM
163 static device_method_t txp_methods[] = {
164 /* Device interface */
165 DEVMETHOD(device_probe, txp_probe),
166 DEVMETHOD(device_attach, txp_attach),
167 DEVMETHOD(device_detach, txp_detach),
168 DEVMETHOD(device_shutdown, txp_shutdown),
172 static driver_t txp_driver = {
175 sizeof(struct txp_softc)
178 static devclass_t txp_devclass;
180 DECLARE_DUMMY_MODULE(if_txp);
181 DRIVER_MODULE(if_txp, pci, txp_driver, txp_devclass, NULL, NULL);
184 txp_probe(device_t dev)
189 vid = pci_get_vendor(dev);
190 did = pci_get_device(dev);
192 for (t = txp_devs; t->txp_name != NULL; ++t) {
193 if ((vid == t->txp_vid) && (did == t->txp_did)) {
194 device_set_desc(dev, t->txp_name);
203 txp_attach(device_t dev)
205 struct txp_softc *sc;
209 uint8_t enaddr[ETHER_ADDR_LEN];
212 sc = device_get_softc(dev);
213 callout_init(&sc->txp_stat_timer);
215 ifp = &sc->sc_arpcom.ac_if;
216 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
218 pci_enable_busmaster(dev);
221 sc->sc_res = bus_alloc_resource_any(dev, TXP_RES, &rid, RF_ACTIVE);
223 if (sc->sc_res == NULL) {
224 device_printf(dev, "couldn't map ports/memory\n");
228 sc->sc_bt = rman_get_bustag(sc->sc_res);
229 sc->sc_bh = rman_get_bushandle(sc->sc_res);
231 /* Allocate interrupt */
233 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
234 RF_SHAREABLE | RF_ACTIVE);
236 if (sc->sc_irq == NULL) {
237 device_printf(dev, "couldn't map interrupt\n");
242 if (txp_chip_init(sc)) {
247 sc->sc_fwbuf = contigmalloc(32768, M_DEVBUF,
248 M_WAITOK, 0, 0xffffffff, PAGE_SIZE, 0);
249 error = txp_download_fw(sc);
250 contigfree(sc->sc_fwbuf, 32768, M_DEVBUF);
256 sc->sc_ldata = contigmalloc(sizeof(struct txp_ldata), M_DEVBUF,
257 M_WAITOK | M_ZERO, 0, 0xffffffff, PAGE_SIZE, 0);
259 if (txp_alloc_rings(sc)) {
264 if (txp_command(sc, TXP_CMD_MAX_PKT_SIZE_WRITE, TXP_MAX_PKTLEN, 0, 0,
265 NULL, NULL, NULL, 1)) {
270 if (txp_command(sc, TXP_CMD_STATION_ADDRESS_READ, 0, 0, 0,
271 &p1, &p2, NULL, 1)) {
278 enaddr[0] = ((uint8_t *)&p1)[1];
279 enaddr[1] = ((uint8_t *)&p1)[0];
280 enaddr[2] = ((uint8_t *)&p2)[3];
281 enaddr[3] = ((uint8_t *)&p2)[2];
282 enaddr[4] = ((uint8_t *)&p2)[1];
283 enaddr[5] = ((uint8_t *)&p2)[0];
285 ifmedia_init(&sc->sc_ifmedia, 0, txp_ifmedia_upd, txp_ifmedia_sts);
286 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
287 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
288 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
289 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
290 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_100_TX|IFM_HDX, 0, NULL);
291 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
292 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
294 sc->sc_xcvr = TXP_XCVR_AUTO;
295 txp_command(sc, TXP_CMD_XCVR_SELECT, TXP_XCVR_AUTO, 0, 0,
296 NULL, NULL, NULL, 0);
297 ifmedia_set(&sc->sc_ifmedia, IFM_ETHER|IFM_AUTO);
300 ifp->if_mtu = ETHERMTU;
301 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
302 ifp->if_ioctl = txp_ioctl;
303 ifp->if_start = txp_start;
304 ifp->if_watchdog = txp_watchdog;
305 ifp->if_init = txp_init;
306 ifp->if_baudrate = 100000000;
307 ifq_set_maxlen(&ifp->if_snd, TX_ENTRIES);
308 ifq_set_ready(&ifp->if_snd);
309 ifp->if_hwassist = 0;
310 txp_capabilities(sc);
312 ether_ifattach(ifp, enaddr, NULL);
314 error = bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE,
315 txp_intr, sc, &sc->sc_intrhand,
318 device_printf(dev, "couldn't set up irq\n");
323 ifp->if_cpuid = rman_get_cpuid(sc->sc_irq);
324 KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
329 txp_release_resources(dev);
334 txp_detach(device_t dev)
336 struct txp_softc *sc = device_get_softc(dev);
337 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
340 lwkt_serialize_enter(ifp->if_serializer);
344 bus_teardown_intr(dev, sc->sc_irq, sc->sc_intrhand);
346 lwkt_serialize_exit(ifp->if_serializer);
348 ifmedia_removeall(&sc->sc_ifmedia);
351 for (i = 0; i < RXBUF_ENTRIES; i++)
352 kfree(sc->sc_rxbufs[i].rb_sd, M_DEVBUF);
354 txp_release_resources(dev);
360 txp_release_resources(device_t dev)
362 struct txp_softc *sc;
364 sc = device_get_softc(dev);
366 if (sc->sc_irq != NULL)
367 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
369 if (sc->sc_res != NULL)
370 bus_release_resource(dev, TXP_RES, TXP_RID, sc->sc_res);
372 if (sc->sc_ldata != NULL)
373 contigfree(sc->sc_ldata, sizeof(struct txp_ldata), M_DEVBUF);
379 txp_chip_init(struct txp_softc *sc)
381 /* disable interrupts */
382 WRITE_REG(sc, TXP_IER, 0);
383 WRITE_REG(sc, TXP_IMR,
384 TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
385 TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
388 /* ack all interrupts */
389 WRITE_REG(sc, TXP_ISR, TXP_INT_RESERVED | TXP_INT_LATCH |
390 TXP_INT_A2H_7 | TXP_INT_A2H_6 | TXP_INT_A2H_5 | TXP_INT_A2H_4 |
391 TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
392 TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
393 TXP_INT_A2H_3 | TXP_INT_A2H_2 | TXP_INT_A2H_1 | TXP_INT_A2H_0);
395 if (txp_reset_adapter(sc))
398 /* disable interrupts */
399 WRITE_REG(sc, TXP_IER, 0);
400 WRITE_REG(sc, TXP_IMR,
401 TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
402 TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
405 /* ack all interrupts */
406 WRITE_REG(sc, TXP_ISR, TXP_INT_RESERVED | TXP_INT_LATCH |
407 TXP_INT_A2H_7 | TXP_INT_A2H_6 | TXP_INT_A2H_5 | TXP_INT_A2H_4 |
408 TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
409 TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
410 TXP_INT_A2H_3 | TXP_INT_A2H_2 | TXP_INT_A2H_1 | TXP_INT_A2H_0);
416 txp_reset_adapter(struct txp_softc *sc)
421 WRITE_REG(sc, TXP_SRR, TXP_SRR_ALL);
423 WRITE_REG(sc, TXP_SRR, 0);
425 /* Should wait max 6 seconds */
426 for (i = 0; i < 6000; i++) {
427 r = READ_REG(sc, TXP_A2H_0);
428 if (r == STAT_WAITING_FOR_HOST_REQUEST)
433 if (r != STAT_WAITING_FOR_HOST_REQUEST) {
434 if_printf(&sc->sc_arpcom.ac_if, "reset hung\n");
442 txp_download_fw(struct txp_softc *sc)
444 struct txp_fw_file_header *fileheader;
445 struct txp_fw_section_header *secthead;
447 u_int32_t r, i, ier, imr;
449 ier = READ_REG(sc, TXP_IER);
450 WRITE_REG(sc, TXP_IER, ier | TXP_INT_A2H_0);
452 imr = READ_REG(sc, TXP_IMR);
453 WRITE_REG(sc, TXP_IMR, imr | TXP_INT_A2H_0);
455 for (i = 0; i < 10000; i++) {
456 r = READ_REG(sc, TXP_A2H_0);
457 if (r == STAT_WAITING_FOR_HOST_REQUEST)
461 if (r != STAT_WAITING_FOR_HOST_REQUEST) {
462 if_printf(&sc->sc_arpcom.ac_if,
463 "not waiting for host request\n");
468 WRITE_REG(sc, TXP_ISR, TXP_INT_A2H_0);
470 fileheader = (struct txp_fw_file_header *)tc990image;
471 if (bcmp("TYPHOON", fileheader->magicid, sizeof(fileheader->magicid))) {
472 if_printf(&sc->sc_arpcom.ac_if, "fw invalid magic\n");
476 /* Tell boot firmware to get ready for image */
477 WRITE_REG(sc, TXP_H2A_1, fileheader->addr);
478 WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_RUNTIME_IMAGE);
480 if (txp_download_fw_wait(sc)) {
481 if_printf(&sc->sc_arpcom.ac_if, "fw wait failed, initial\n");
485 secthead = (struct txp_fw_section_header *)(((u_int8_t *)tc990image) +
486 sizeof(struct txp_fw_file_header));
488 for (sect = 0; sect < fileheader->nsections; sect++) {
489 if (txp_download_fw_section(sc, secthead, sect))
491 secthead = (struct txp_fw_section_header *)
492 (((u_int8_t *)secthead) + secthead->nbytes +
496 WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_DOWNLOAD_COMPLETE);
498 for (i = 0; i < 10000; i++) {
499 r = READ_REG(sc, TXP_A2H_0);
500 if (r == STAT_WAITING_FOR_BOOT)
504 if (r != STAT_WAITING_FOR_BOOT) {
505 if_printf(&sc->sc_arpcom.ac_if, "not waiting for boot\n");
509 WRITE_REG(sc, TXP_IER, ier);
510 WRITE_REG(sc, TXP_IMR, imr);
516 txp_download_fw_wait(struct txp_softc *sc)
520 for (i = 0; i < 10000; i++) {
521 r = READ_REG(sc, TXP_ISR);
522 if (r & TXP_INT_A2H_0)
527 if (!(r & TXP_INT_A2H_0)) {
528 if_printf(&sc->sc_arpcom.ac_if, "fw wait failed comm0\n");
532 WRITE_REG(sc, TXP_ISR, TXP_INT_A2H_0);
534 r = READ_REG(sc, TXP_A2H_0);
535 if (r != STAT_WAITING_FOR_SEGMENT) {
536 if_printf(&sc->sc_arpcom.ac_if, "fw not waiting for segment\n");
543 txp_download_fw_section(struct txp_softc *sc,
544 struct txp_fw_section_header *sect, int sectnum)
551 /* Skip zero length sections */
552 if (sect->nbytes == 0)
555 /* Make sure we aren't past the end of the image */
556 rseg = ((u_int8_t *)sect) - ((u_int8_t *)tc990image);
557 if (rseg >= sizeof(tc990image)) {
558 if_printf(&sc->sc_arpcom.ac_if, "fw invalid section address, "
559 "section %d\n", sectnum);
563 /* Make sure this section doesn't go past the end */
564 rseg += sect->nbytes;
565 if (rseg >= sizeof(tc990image)) {
566 if_printf(&sc->sc_arpcom.ac_if, "fw truncated section %d\n",
571 bcopy(((u_int8_t *)sect) + sizeof(*sect), sc->sc_fwbuf, sect->nbytes);
572 dma = vtophys(sc->sc_fwbuf);
575 * dummy up mbuf and verify section checksum
578 m.m_next = m.m_nextpkt = NULL;
579 m.m_len = sect->nbytes;
580 m.m_data = sc->sc_fwbuf;
582 csum = in_cksum(&m, sect->nbytes);
583 if (csum != sect->cksum) {
584 if_printf(&sc->sc_arpcom.ac_if, "fw section %d, bad "
585 "cksum (expected 0x%x got 0x%x)\n",
586 sectnum, sect->cksum, csum);
591 WRITE_REG(sc, TXP_H2A_1, sect->nbytes);
592 WRITE_REG(sc, TXP_H2A_2, sect->cksum);
593 WRITE_REG(sc, TXP_H2A_3, sect->addr);
594 WRITE_REG(sc, TXP_H2A_4, 0);
595 WRITE_REG(sc, TXP_H2A_5, dma & 0xffffffff);
596 WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_SEGMENT_AVAILABLE);
598 if (txp_download_fw_wait(sc)) {
599 if_printf(&sc->sc_arpcom.ac_if, "fw wait failed, "
600 "section %d\n", sectnum);
611 struct txp_softc *sc = vsc;
612 struct txp_hostvar *hv = sc->sc_hostvar;
615 /* mask all interrupts */
616 WRITE_REG(sc, TXP_IMR, TXP_INT_RESERVED | TXP_INT_SELF |
617 TXP_INT_A2H_7 | TXP_INT_A2H_6 | TXP_INT_A2H_5 | TXP_INT_A2H_4 |
618 TXP_INT_A2H_2 | TXP_INT_A2H_1 | TXP_INT_A2H_0 |
619 TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
620 TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT | TXP_INT_LATCH);
622 isr = READ_REG(sc, TXP_ISR);
624 WRITE_REG(sc, TXP_ISR, isr);
626 if ((*sc->sc_rxhir.r_roff) != (*sc->sc_rxhir.r_woff))
627 txp_rx_reclaim(sc, &sc->sc_rxhir);
628 if ((*sc->sc_rxlor.r_roff) != (*sc->sc_rxlor.r_woff))
629 txp_rx_reclaim(sc, &sc->sc_rxlor);
631 if (hv->hv_rx_buf_write_idx == hv->hv_rx_buf_read_idx)
632 txp_rxbuf_reclaim(sc);
634 if (sc->sc_txhir.r_cnt && (sc->sc_txhir.r_cons !=
635 TXP_OFFSET2IDX(*(sc->sc_txhir.r_off))))
636 txp_tx_reclaim(sc, &sc->sc_txhir);
638 if (sc->sc_txlor.r_cnt && (sc->sc_txlor.r_cons !=
639 TXP_OFFSET2IDX(*(sc->sc_txlor.r_off))))
640 txp_tx_reclaim(sc, &sc->sc_txlor);
642 isr = READ_REG(sc, TXP_ISR);
645 /* unmask all interrupts */
646 WRITE_REG(sc, TXP_IMR, TXP_INT_A2H_3);
648 if_devstart(&sc->sc_arpcom.ac_if);
652 txp_rx_reclaim(struct txp_softc *sc, struct txp_rx_ring *r)
654 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
655 struct txp_rx_desc *rxd;
657 struct txp_swdesc *sd = NULL;
658 u_int32_t roff, woff;
662 rxd = r->r_desc + (roff / sizeof(struct txp_rx_desc));
664 while (roff != woff) {
666 if (rxd->rx_flags & RX_FLAGS_ERROR) {
667 if_printf(ifp, "error 0x%x\n", rxd->rx_stat);
672 /* retrieve stashed pointer */
678 m->m_pkthdr.len = m->m_len = rxd->rx_len;
680 #ifdef __STRICT_ALIGNMENT
683 * XXX Nice chip, except it won't accept "off by 2"
684 * buffers, so we're force to copy. Supposedly
685 * this will be fixed in a newer firmware rev
686 * and this will be temporary.
690 MGETHDR(mnew, MB_DONTWAIT, MT_DATA);
695 if (m->m_len > (MHLEN - 2)) {
696 MCLGET(mnew, MB_DONTWAIT);
697 if (!(mnew->m_flags & M_EXT)) {
703 mnew->m_pkthdr.rcvif = ifp;
705 mnew->m_pkthdr.len = mnew->m_len = m->m_len;
706 m_copydata(m, 0, m->m_pkthdr.len, mtod(mnew, caddr_t));
712 if (rxd->rx_stat & RX_STAT_IPCKSUMBAD)
713 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
714 else if (rxd->rx_stat & RX_STAT_IPCKSUMGOOD)
715 m->m_pkthdr.csum_flags |=
716 CSUM_IP_CHECKED|CSUM_IP_VALID;
718 if ((rxd->rx_stat & RX_STAT_TCPCKSUMGOOD) ||
719 (rxd->rx_stat & RX_STAT_UDPCKSUMGOOD)) {
720 m->m_pkthdr.csum_flags |=
721 CSUM_DATA_VALID|CSUM_PSEUDO_HDR|
722 CSUM_FRAG_NOT_CHECKED;
723 m->m_pkthdr.csum_data = 0xffff;
726 if (rxd->rx_stat & RX_STAT_VLAN) {
727 m->m_flags |= M_VLANTAG;
728 m->m_pkthdr.ether_vlantag = htons(rxd->rx_vlan >> 16);
730 ifp->if_input(ifp, m);
734 roff += sizeof(struct txp_rx_desc);
735 if (roff == (RX_ENTRIES * sizeof(struct txp_rx_desc))) {
749 txp_rxbuf_reclaim(struct txp_softc *sc)
751 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
752 struct txp_hostvar *hv = sc->sc_hostvar;
753 struct txp_rxbuf_desc *rbd;
754 struct txp_swdesc *sd;
757 if (!(ifp->if_flags & IFF_RUNNING))
760 i = sc->sc_rxbufprod;
761 rbd = sc->sc_rxbufs + i;
765 if (sd->sd_mbuf != NULL)
768 MGETHDR(sd->sd_mbuf, MB_DONTWAIT, MT_DATA);
769 if (sd->sd_mbuf == NULL)
772 MCLGET(sd->sd_mbuf, MB_DONTWAIT);
773 if ((sd->sd_mbuf->m_flags & M_EXT) == 0)
775 sd->sd_mbuf->m_pkthdr.rcvif = ifp;
776 sd->sd_mbuf->m_pkthdr.len = sd->sd_mbuf->m_len = MCLBYTES;
778 rbd->rb_paddrlo = vtophys(mtod(sd->sd_mbuf, vm_offset_t))
782 hv->hv_rx_buf_write_idx = TXP_IDX2OFFSET(i);
784 if (++i == RXBUF_ENTRIES) {
791 sc->sc_rxbufprod = i;
796 m_freem(sd->sd_mbuf);
802 * Reclaim mbufs and entries from a transmit ring.
805 txp_tx_reclaim(struct txp_softc *sc, struct txp_tx_ring *r)
807 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
808 u_int32_t idx = TXP_OFFSET2IDX(*(r->r_off));
809 u_int32_t cons = r->r_cons, cnt = r->r_cnt;
810 struct txp_tx_desc *txd = r->r_desc + cons;
811 struct txp_swdesc *sd = sc->sc_txd + cons;
814 while (cons != idx) {
818 if ((txd->tx_flags & TX_FLAGS_TYPE_M) ==
819 TX_FLAGS_TYPE_DATA) {
828 ifq_clr_oactive(&ifp->if_snd);
830 if (++cons == TX_ENTRIES) {
849 txp_shutdown(device_t dev)
851 struct txp_softc *sc;
854 sc = device_get_softc(dev);
855 ifp = &sc->sc_arpcom.ac_if;
856 lwkt_serialize_enter(ifp->if_serializer);
858 /* mask all interrupts */
859 WRITE_REG(sc, TXP_IMR,
860 TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
861 TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
864 txp_command(sc, TXP_CMD_TX_DISABLE, 0, 0, 0, NULL, NULL, NULL, 0);
865 txp_command(sc, TXP_CMD_RX_DISABLE, 0, 0, 0, NULL, NULL, NULL, 0);
866 txp_command(sc, TXP_CMD_HALT, 0, 0, 0, NULL, NULL, NULL, 0);
868 lwkt_serialize_exit(ifp->if_serializer);
873 txp_alloc_rings(struct txp_softc *sc)
875 struct txp_boot_record *boot;
876 struct txp_ldata *ld;
881 boot = &ld->txp_boot;
887 bzero(&ld->txp_hostvar, sizeof(struct txp_hostvar));
888 boot->br_hostvar_lo = vtophys(&ld->txp_hostvar);
889 boot->br_hostvar_hi = 0;
890 sc->sc_hostvar = (struct txp_hostvar *)&ld->txp_hostvar;
892 /* hi priority tx ring */
893 boot->br_txhipri_lo = vtophys(&ld->txp_txhiring);
894 boot->br_txhipri_hi = 0;
895 boot->br_txhipri_siz = TX_ENTRIES * sizeof(struct txp_tx_desc);
896 sc->sc_txhir.r_reg = TXP_H2A_1;
897 sc->sc_txhir.r_desc = (struct txp_tx_desc *)&ld->txp_txhiring;
898 sc->sc_txhir.r_cons = sc->sc_txhir.r_prod = sc->sc_txhir.r_cnt = 0;
899 sc->sc_txhir.r_off = &sc->sc_hostvar->hv_tx_hi_desc_read_idx;
901 /* lo priority tx ring */
902 boot->br_txlopri_lo = vtophys(&ld->txp_txloring);
903 boot->br_txlopri_hi = 0;
904 boot->br_txlopri_siz = TX_ENTRIES * sizeof(struct txp_tx_desc);
905 sc->sc_txlor.r_reg = TXP_H2A_3;
906 sc->sc_txlor.r_desc = (struct txp_tx_desc *)&ld->txp_txloring;
907 sc->sc_txlor.r_cons = sc->sc_txlor.r_prod = sc->sc_txlor.r_cnt = 0;
908 sc->sc_txlor.r_off = &sc->sc_hostvar->hv_tx_lo_desc_read_idx;
910 /* high priority rx ring */
911 boot->br_rxhipri_lo = vtophys(&ld->txp_rxhiring);
912 boot->br_rxhipri_hi = 0;
913 boot->br_rxhipri_siz = RX_ENTRIES * sizeof(struct txp_rx_desc);
914 sc->sc_rxhir.r_desc = (struct txp_rx_desc *)&ld->txp_rxhiring;
915 sc->sc_rxhir.r_roff = &sc->sc_hostvar->hv_rx_hi_read_idx;
916 sc->sc_rxhir.r_woff = &sc->sc_hostvar->hv_rx_hi_write_idx;
918 /* low priority rx ring */
919 boot->br_rxlopri_lo = vtophys(&ld->txp_rxloring);
920 boot->br_rxlopri_hi = 0;
921 boot->br_rxlopri_siz = RX_ENTRIES * sizeof(struct txp_rx_desc);
922 sc->sc_rxlor.r_desc = (struct txp_rx_desc *)&ld->txp_rxloring;
923 sc->sc_rxlor.r_roff = &sc->sc_hostvar->hv_rx_lo_read_idx;
924 sc->sc_rxlor.r_woff = &sc->sc_hostvar->hv_rx_lo_write_idx;
927 bzero(&ld->txp_cmdring, sizeof(struct txp_cmd_desc) * CMD_ENTRIES);
928 boot->br_cmd_lo = vtophys(&ld->txp_cmdring);
930 boot->br_cmd_siz = CMD_ENTRIES * sizeof(struct txp_cmd_desc);
931 sc->sc_cmdring.base = (struct txp_cmd_desc *)&ld->txp_cmdring;
932 sc->sc_cmdring.size = CMD_ENTRIES * sizeof(struct txp_cmd_desc);
933 sc->sc_cmdring.lastwrite = 0;
936 bzero(&ld->txp_rspring, sizeof(struct txp_rsp_desc) * RSP_ENTRIES);
937 boot->br_resp_lo = vtophys(&ld->txp_rspring);
938 boot->br_resp_hi = 0;
939 boot->br_resp_siz = CMD_ENTRIES * sizeof(struct txp_rsp_desc);
940 sc->sc_rspring.base = (struct txp_rsp_desc *)&ld->txp_rspring;
941 sc->sc_rspring.size = RSP_ENTRIES * sizeof(struct txp_rsp_desc);
942 sc->sc_rspring.lastwrite = 0;
944 /* receive buffer ring */
945 boot->br_rxbuf_lo = vtophys(&ld->txp_rxbufs);
946 boot->br_rxbuf_hi = 0;
947 boot->br_rxbuf_siz = RXBUF_ENTRIES * sizeof(struct txp_rxbuf_desc);
948 sc->sc_rxbufs = (struct txp_rxbuf_desc *)&ld->txp_rxbufs;
950 for (i = 0; i < RXBUF_ENTRIES; i++) {
951 struct txp_swdesc *sd;
952 if (sc->sc_rxbufs[i].rb_sd != NULL)
954 sc->sc_rxbufs[i].rb_sd = kmalloc(sizeof(struct txp_swdesc),
956 sd = sc->sc_rxbufs[i].rb_sd;
959 sc->sc_rxbufprod = 0;
962 bzero(&ld->txp_zero, sizeof(u_int32_t));
963 boot->br_zero_lo = vtophys(&ld->txp_zero);
964 boot->br_zero_hi = 0;
966 /* See if it's waiting for boot, and try to boot it */
967 for (i = 0; i < 10000; i++) {
968 r = READ_REG(sc, TXP_A2H_0);
969 if (r == STAT_WAITING_FOR_BOOT)
974 if (r != STAT_WAITING_FOR_BOOT) {
975 if_printf(&sc->sc_arpcom.ac_if, "not waiting for boot\n");
979 WRITE_REG(sc, TXP_H2A_2, 0);
980 WRITE_REG(sc, TXP_H2A_1, vtophys(sc->sc_boot));
981 WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_REGISTER_BOOT_RECORD);
983 /* See if it booted */
984 for (i = 0; i < 10000; i++) {
985 r = READ_REG(sc, TXP_A2H_0);
986 if (r == STAT_RUNNING)
990 if (r != STAT_RUNNING) {
991 if_printf(&sc->sc_arpcom.ac_if, "fw not running\n");
995 /* Clear TX and CMD ring write registers */
996 WRITE_REG(sc, TXP_H2A_1, TXP_BOOTCMD_NULL);
997 WRITE_REG(sc, TXP_H2A_2, TXP_BOOTCMD_NULL);
998 WRITE_REG(sc, TXP_H2A_3, TXP_BOOTCMD_NULL);
999 WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_NULL);
1005 txp_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1007 struct txp_softc *sc = ifp->if_softc;
1008 struct ifreq *ifr = (struct ifreq *)data;
1013 if (ifp->if_flags & IFF_UP) {
1016 if (ifp->if_flags & IFF_RUNNING)
1023 * Multicast list has changed; set the hardware
1024 * filter accordingly.
1031 error = ifmedia_ioctl(ifp, ifr, &sc->sc_ifmedia, command);
1034 error = ether_ioctl(ifp, command, data);
1041 txp_rxring_fill(struct txp_softc *sc)
1045 struct txp_swdesc *sd;
1047 ifp = &sc->sc_arpcom.ac_if;
1049 for (i = 0; i < RXBUF_ENTRIES; i++) {
1050 sd = sc->sc_rxbufs[i].rb_sd;
1051 MGETHDR(sd->sd_mbuf, MB_DONTWAIT, MT_DATA);
1052 if (sd->sd_mbuf == NULL)
1055 MCLGET(sd->sd_mbuf, MB_DONTWAIT);
1056 if ((sd->sd_mbuf->m_flags & M_EXT) == 0) {
1057 m_freem(sd->sd_mbuf);
1060 sd->sd_mbuf->m_pkthdr.len = sd->sd_mbuf->m_len = MCLBYTES;
1061 sd->sd_mbuf->m_pkthdr.rcvif = ifp;
1063 sc->sc_rxbufs[i].rb_paddrlo =
1064 vtophys(mtod(sd->sd_mbuf, vm_offset_t));
1065 sc->sc_rxbufs[i].rb_paddrhi = 0;
1068 sc->sc_hostvar->hv_rx_buf_write_idx = (RXBUF_ENTRIES - 1) *
1069 sizeof(struct txp_rxbuf_desc);
1075 txp_rxring_empty(struct txp_softc *sc)
1078 struct txp_swdesc *sd;
1080 if (sc->sc_rxbufs == NULL)
1083 for (i = 0; i < RXBUF_ENTRIES; i++) {
1084 if (&sc->sc_rxbufs[i] == NULL)
1086 sd = sc->sc_rxbufs[i].rb_sd;
1089 if (sd->sd_mbuf != NULL) {
1090 m_freem(sd->sd_mbuf);
1101 struct txp_softc *sc;
1107 ifp = &sc->sc_arpcom.ac_if;
1109 if (ifp->if_flags & IFF_RUNNING)
1114 txp_command(sc, TXP_CMD_MAX_PKT_SIZE_WRITE, TXP_MAX_PKTLEN, 0, 0,
1115 NULL, NULL, NULL, 1);
1117 /* Set station address. */
1118 ((u_int8_t *)&p1)[1] = sc->sc_arpcom.ac_enaddr[0];
1119 ((u_int8_t *)&p1)[0] = sc->sc_arpcom.ac_enaddr[1];
1120 ((u_int8_t *)&p2)[3] = sc->sc_arpcom.ac_enaddr[2];
1121 ((u_int8_t *)&p2)[2] = sc->sc_arpcom.ac_enaddr[3];
1122 ((u_int8_t *)&p2)[1] = sc->sc_arpcom.ac_enaddr[4];
1123 ((u_int8_t *)&p2)[0] = sc->sc_arpcom.ac_enaddr[5];
1124 txp_command(sc, TXP_CMD_STATION_ADDRESS_WRITE, p1, p2, 0,
1125 NULL, NULL, NULL, 1);
1129 txp_rxring_fill(sc);
1131 txp_command(sc, TXP_CMD_TX_ENABLE, 0, 0, 0, NULL, NULL, NULL, 1);
1132 txp_command(sc, TXP_CMD_RX_ENABLE, 0, 0, 0, NULL, NULL, NULL, 1);
1134 WRITE_REG(sc, TXP_IER, TXP_INT_RESERVED | TXP_INT_SELF |
1135 TXP_INT_A2H_7 | TXP_INT_A2H_6 | TXP_INT_A2H_5 | TXP_INT_A2H_4 |
1136 TXP_INT_A2H_2 | TXP_INT_A2H_1 | TXP_INT_A2H_0 |
1137 TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
1138 TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT | TXP_INT_LATCH);
1139 WRITE_REG(sc, TXP_IMR, TXP_INT_A2H_3);
1141 ifp->if_flags |= IFF_RUNNING;
1142 ifq_clr_oactive(&ifp->if_snd);
1145 callout_reset(&sc->txp_stat_timer, hz, txp_tick, sc);
1151 struct txp_softc *sc = vsc;
1152 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1153 struct txp_rsp_desc *rsp = NULL;
1154 struct txp_ext_desc *ext;
1156 lwkt_serialize_enter(ifp->if_serializer);
1157 txp_rxbuf_reclaim(sc);
1159 if (txp_command2(sc, TXP_CMD_READ_STATISTICS, 0, 0, 0, NULL, 0,
1162 if (rsp->rsp_numdesc != 6)
1164 if (txp_command(sc, TXP_CMD_CLEAR_STATISTICS, 0, 0, 0,
1165 NULL, NULL, NULL, 1))
1167 ext = (struct txp_ext_desc *)(rsp + 1);
1169 ifp->if_ierrors += ext[3].ext_2 + ext[3].ext_3 + ext[3].ext_4 +
1170 ext[4].ext_1 + ext[4].ext_4;
1171 ifp->if_oerrors += ext[0].ext_1 + ext[1].ext_1 + ext[1].ext_4 +
1173 ifp->if_collisions += ext[0].ext_2 + ext[0].ext_3 + ext[1].ext_2 +
1175 ifp->if_opackets += rsp->rsp_par2;
1176 ifp->if_ipackets += ext[2].ext_3;
1180 kfree(rsp, M_DEVBUF);
1182 callout_reset(&sc->txp_stat_timer, hz, txp_tick, sc);
1183 lwkt_serialize_exit(ifp->if_serializer);
1187 txp_start(struct ifnet *ifp)
1189 struct txp_softc *sc = ifp->if_softc;
1190 struct txp_tx_ring *r = &sc->sc_txhir;
1191 struct txp_tx_desc *txd;
1192 struct txp_frag_desc *fxd;
1193 struct mbuf *m, *m0, *m_defragged;
1194 struct txp_swdesc *sd;
1195 u_int32_t firstprod, firstcnt, prod, cnt;
1197 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
1209 if ((TX_ENTRIES - cnt) < 4)
1213 m = ifq_dequeue(&ifp->if_snd, NULL);
1217 frag = 1; /* Extra desc */
1218 for (m0 = m; m0 != NULL; m0 = m0->m_next)
1220 if ((cnt + frag) >= (TX_ENTRIES - 4)) {
1221 if (m_defragged != NULL) {
1223 * Even after defragmentation, there
1224 * are still too many fragments, so
1231 m_defragged = m_defrag(m, MB_DONTWAIT);
1232 if (m_defragged == NULL) {
1238 /* Recount # of fragments */
1242 sd = sc->sc_txd + prod;
1245 txd = r->r_desc + prod;
1246 txd->tx_flags = TX_FLAGS_TYPE_DATA;
1247 txd->tx_numdesc = 0;
1253 if (++prod == TX_ENTRIES)
1257 KASSERT(cnt < (TX_ENTRIES - 4), ("too many frag"));
1259 if (m->m_flags & M_VLANTAG) {
1260 txd->tx_pflags = TX_PFLAGS_VLAN |
1261 (htons(m->m_pkthdr.ether_vlantag) <<
1262 TX_PFLAGS_VLANTAG_S);
1265 if (m->m_pkthdr.csum_flags & CSUM_IP)
1266 txd->tx_pflags |= TX_PFLAGS_IPCKSUM;
1269 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1270 txd->tx_pflags |= TX_PFLAGS_TCPCKSUM;
1271 if (m->m_pkthdr.csum_flags & CSUM_UDP)
1272 txd->tx_pflags |= TX_PFLAGS_UDPCKSUM;
1275 fxd = (struct txp_frag_desc *)(r->r_desc + prod);
1276 for (m0 = m; m0 != NULL; m0 = m0->m_next) {
1281 KASSERT(cnt < (TX_ENTRIES - 4), ("too many frag"));
1285 fxd->frag_flags = FRAG_FLAGS_TYPE_FRAG;
1286 fxd->frag_rsvd1 = 0;
1287 fxd->frag_len = m0->m_len;
1288 fxd->frag_addrlo = vtophys(mtod(m0, vm_offset_t));
1289 fxd->frag_addrhi = 0;
1290 fxd->frag_rsvd2 = 0;
1292 if (++prod == TX_ENTRIES) {
1293 fxd = (struct txp_frag_desc *)r->r_desc;
1301 ETHER_BPF_MTAP(ifp, m);
1302 WRITE_REG(sc, r->r_reg, TXP_IDX2OFFSET(prod));
1310 ifq_set_oactive(&ifp->if_snd);
1311 r->r_prod = firstprod;
1312 r->r_cnt = firstcnt;
1317 * Handle simple commands sent to the typhoon
1320 txp_command(struct txp_softc *sc, u_int16_t id, u_int16_t in1, u_int32_t in2,
1321 u_int32_t in3, u_int16_t *out1, u_int32_t *out2, u_int32_t *out3,
1324 struct txp_rsp_desc *rsp = NULL;
1326 if (txp_command2(sc, id, in1, in2, in3, NULL, 0, &rsp, wait))
1333 *out1 = rsp->rsp_par1;
1335 *out2 = rsp->rsp_par2;
1337 *out3 = rsp->rsp_par3;
1338 kfree(rsp, M_DEVBUF);
1343 txp_command2(struct txp_softc *sc, u_int16_t id, u_int16_t in1, u_int32_t in2,
1344 u_int32_t in3, struct txp_ext_desc *in_extp, u_int8_t in_extn,
1345 struct txp_rsp_desc **rspp, int wait)
1347 struct txp_hostvar *hv = sc->sc_hostvar;
1348 struct txp_cmd_desc *cmd;
1349 struct txp_ext_desc *ext;
1353 if (txp_cmd_desc_numfree(sc) < (in_extn + 1)) {
1354 if_printf(&sc->sc_arpcom.ac_if, "no free cmd descriptors\n");
1358 idx = sc->sc_cmdring.lastwrite;
1359 cmd = (struct txp_cmd_desc *)(((u_int8_t *)sc->sc_cmdring.base) + idx);
1360 bzero(cmd, sizeof(*cmd));
1362 cmd->cmd_numdesc = in_extn;
1363 cmd->cmd_seq = seq = sc->sc_seq++;
1365 cmd->cmd_par1 = in1;
1366 cmd->cmd_par2 = in2;
1367 cmd->cmd_par3 = in3;
1368 cmd->cmd_flags = CMD_FLAGS_TYPE_CMD |
1369 (wait ? CMD_FLAGS_RESP : 0) | CMD_FLAGS_VALID;
1371 idx += sizeof(struct txp_cmd_desc);
1372 if (idx == sc->sc_cmdring.size)
1375 for (i = 0; i < in_extn; i++) {
1376 ext = (struct txp_ext_desc *)(((u_int8_t *)sc->sc_cmdring.base) + idx);
1377 bcopy(in_extp, ext, sizeof(struct txp_ext_desc));
1379 idx += sizeof(struct txp_cmd_desc);
1380 if (idx == sc->sc_cmdring.size)
1384 sc->sc_cmdring.lastwrite = idx;
1386 WRITE_REG(sc, TXP_H2A_2, sc->sc_cmdring.lastwrite);
1391 for (i = 0; i < 10000; i++) {
1392 idx = hv->hv_resp_read_idx;
1393 if (idx != hv->hv_resp_write_idx) {
1395 if (txp_response(sc, idx, id, seq, rspp))
1402 if (i == 1000 || (*rspp) == NULL) {
1403 if_printf(&sc->sc_arpcom.ac_if, "0x%x command failed\n", id);
1411 txp_response(struct txp_softc *sc, u_int32_t ridx, u_int16_t id, u_int16_t seq,
1412 struct txp_rsp_desc **rspp)
1414 struct txp_hostvar *hv = sc->sc_hostvar;
1415 struct txp_rsp_desc *rsp;
1417 while (ridx != hv->hv_resp_write_idx) {
1418 rsp = (struct txp_rsp_desc *)(((u_int8_t *)sc->sc_rspring.base) + ridx);
1420 if (id == rsp->rsp_id && rsp->rsp_seq == seq) {
1421 *rspp = (struct txp_rsp_desc *)kmalloc(
1422 sizeof(struct txp_rsp_desc) * (rsp->rsp_numdesc + 1),
1423 M_DEVBUF, M_INTWAIT);
1424 if ((*rspp) == NULL)
1426 txp_rsp_fixup(sc, rsp, *rspp);
1430 if (rsp->rsp_flags & RSP_FLAGS_ERROR) {
1431 if_printf(&sc->sc_arpcom.ac_if, "response error!\n");
1432 txp_rsp_fixup(sc, rsp, NULL);
1433 ridx = hv->hv_resp_read_idx;
1437 switch (rsp->rsp_id) {
1438 case TXP_CMD_CYCLE_STATISTICS:
1439 case TXP_CMD_MEDIA_STATUS_READ:
1441 case TXP_CMD_HELLO_RESPONSE:
1442 if_printf(&sc->sc_arpcom.ac_if, "hello\n");
1445 if_printf(&sc->sc_arpcom.ac_if, "unknown id(0x%x)\n",
1449 txp_rsp_fixup(sc, rsp, NULL);
1450 ridx = hv->hv_resp_read_idx;
1451 hv->hv_resp_read_idx = ridx;
1458 txp_rsp_fixup(struct txp_softc *sc, struct txp_rsp_desc *rsp,
1459 struct txp_rsp_desc *dst)
1461 struct txp_rsp_desc *src = rsp;
1462 struct txp_hostvar *hv = sc->sc_hostvar;
1465 ridx = hv->hv_resp_read_idx;
1467 for (i = 0; i < rsp->rsp_numdesc + 1; i++) {
1469 bcopy(src, dst++, sizeof(struct txp_rsp_desc));
1470 ridx += sizeof(struct txp_rsp_desc);
1471 if (ridx == sc->sc_rspring.size) {
1472 src = sc->sc_rspring.base;
1476 sc->sc_rspring.lastwrite = hv->hv_resp_read_idx = ridx;
1479 hv->hv_resp_read_idx = ridx;
1483 txp_cmd_desc_numfree(struct txp_softc *sc)
1485 struct txp_hostvar *hv = sc->sc_hostvar;
1486 struct txp_boot_record *br = sc->sc_boot;
1487 u_int32_t widx, ridx, nfree;
1489 widx = sc->sc_cmdring.lastwrite;
1490 ridx = hv->hv_cmd_read_idx;
1493 /* Ring is completely free */
1494 nfree = br->br_cmd_siz - sizeof(struct txp_cmd_desc);
1497 nfree = br->br_cmd_siz -
1498 (widx - ridx + sizeof(struct txp_cmd_desc));
1500 nfree = ridx - widx - sizeof(struct txp_cmd_desc);
1503 return (nfree / sizeof(struct txp_cmd_desc));
1507 txp_stop(struct txp_softc *sc)
1511 ifp = &sc->sc_arpcom.ac_if;
1513 ifp->if_flags &= ~IFF_RUNNING;
1514 ifq_clr_oactive(&ifp->if_snd);
1516 callout_stop(&sc->txp_stat_timer);
1518 txp_command(sc, TXP_CMD_TX_DISABLE, 0, 0, 0, NULL, NULL, NULL, 1);
1519 txp_command(sc, TXP_CMD_RX_DISABLE, 0, 0, 0, NULL, NULL, NULL, 1);
1521 txp_rxring_empty(sc);
1527 txp_watchdog(struct ifnet *ifp)
1533 txp_ifmedia_upd(struct ifnet *ifp)
1535 struct txp_softc *sc = ifp->if_softc;
1536 struct ifmedia *ifm = &sc->sc_ifmedia;
1539 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1542 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_T) {
1543 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1544 new_xcvr = TXP_XCVR_10_FDX;
1546 new_xcvr = TXP_XCVR_10_HDX;
1547 } else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) {
1548 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1549 new_xcvr = TXP_XCVR_100_FDX;
1551 new_xcvr = TXP_XCVR_100_HDX;
1552 } else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
1553 new_xcvr = TXP_XCVR_AUTO;
1558 if (sc->sc_xcvr == new_xcvr)
1561 txp_command(sc, TXP_CMD_XCVR_SELECT, new_xcvr, 0, 0,
1562 NULL, NULL, NULL, 0);
1563 sc->sc_xcvr = new_xcvr;
1569 txp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1571 struct txp_softc *sc = ifp->if_softc;
1572 struct ifmedia *ifm = &sc->sc_ifmedia;
1573 u_int16_t bmsr, bmcr, anlpar;
1575 ifmr->ifm_status = IFM_AVALID;
1576 ifmr->ifm_active = IFM_ETHER;
1578 if (txp_command(sc, TXP_CMD_PHY_MGMT_READ, 0, MII_BMSR, 0,
1579 &bmsr, NULL, NULL, 1))
1581 if (txp_command(sc, TXP_CMD_PHY_MGMT_READ, 0, MII_BMSR, 0,
1582 &bmsr, NULL, NULL, 1))
1585 if (txp_command(sc, TXP_CMD_PHY_MGMT_READ, 0, MII_BMCR, 0,
1586 &bmcr, NULL, NULL, 1))
1589 if (txp_command(sc, TXP_CMD_PHY_MGMT_READ, 0, MII_ANLPAR, 0,
1590 &anlpar, NULL, NULL, 1))
1593 if (bmsr & BMSR_LINK)
1594 ifmr->ifm_status |= IFM_ACTIVE;
1596 if (bmcr & BMCR_ISO) {
1597 ifmr->ifm_active |= IFM_NONE;
1598 ifmr->ifm_status = 0;
1602 if (bmcr & BMCR_LOOP)
1603 ifmr->ifm_active |= IFM_LOOP;
1605 if (bmcr & BMCR_AUTOEN) {
1606 if ((bmsr & BMSR_ACOMP) == 0) {
1607 ifmr->ifm_active |= IFM_NONE;
1611 if (anlpar & ANLPAR_T4)
1612 ifmr->ifm_active |= IFM_100_T4;
1613 else if (anlpar & ANLPAR_TX_FD)
1614 ifmr->ifm_active |= IFM_100_TX|IFM_FDX;
1615 else if (anlpar & ANLPAR_TX)
1616 ifmr->ifm_active |= IFM_100_TX;
1617 else if (anlpar & ANLPAR_10_FD)
1618 ifmr->ifm_active |= IFM_10_T|IFM_FDX;
1619 else if (anlpar & ANLPAR_10)
1620 ifmr->ifm_active |= IFM_10_T;
1622 ifmr->ifm_active |= IFM_NONE;
1624 ifmr->ifm_active = ifm->ifm_cur->ifm_media;
1628 ifmr->ifm_active |= IFM_NONE;
1629 ifmr->ifm_status &= ~IFM_AVALID;
1634 txp_show_descriptor(void *d)
1636 struct txp_cmd_desc *cmd = d;
1637 struct txp_rsp_desc *rsp = d;
1638 struct txp_tx_desc *txd = d;
1639 struct txp_frag_desc *frgd = d;
1641 switch (cmd->cmd_flags & CMD_FLAGS_TYPE_M) {
1642 case CMD_FLAGS_TYPE_CMD:
1643 /* command descriptor */
1644 kprintf("[cmd flags 0x%x num %d id %d seq %d par1 0x%x par2 0x%x par3 0x%x]\n",
1645 cmd->cmd_flags, cmd->cmd_numdesc, cmd->cmd_id, cmd->cmd_seq,
1646 cmd->cmd_par1, cmd->cmd_par2, cmd->cmd_par3);
1648 case CMD_FLAGS_TYPE_RESP:
1649 /* response descriptor */
1650 kprintf("[rsp flags 0x%x num %d id %d seq %d par1 0x%x par2 0x%x par3 0x%x]\n",
1651 rsp->rsp_flags, rsp->rsp_numdesc, rsp->rsp_id, rsp->rsp_seq,
1652 rsp->rsp_par1, rsp->rsp_par2, rsp->rsp_par3);
1654 case CMD_FLAGS_TYPE_DATA:
1655 /* data header (assuming tx for now) */
1656 kprintf("[data flags 0x%x num %d totlen %d addr 0x%x/0x%x pflags 0x%x]",
1657 txd->tx_flags, txd->tx_numdesc, txd->tx_totlen,
1658 txd->tx_addrlo, txd->tx_addrhi, txd->tx_pflags);
1660 case CMD_FLAGS_TYPE_FRAG:
1661 /* fragment descriptor */
1662 kprintf("[frag flags 0x%x rsvd1 0x%x len %d addr 0x%x/0x%x rsvd2 0x%x]",
1663 frgd->frag_flags, frgd->frag_rsvd1, frgd->frag_len,
1664 frgd->frag_addrlo, frgd->frag_addrhi, frgd->frag_rsvd2);
1667 kprintf("[unknown(%x) flags 0x%x num %d id %d seq %d par1 0x%x par2 0x%x par3 0x%x]\n",
1668 cmd->cmd_flags & CMD_FLAGS_TYPE_M,
1669 cmd->cmd_flags, cmd->cmd_numdesc, cmd->cmd_id, cmd->cmd_seq,
1670 cmd->cmd_par1, cmd->cmd_par2, cmd->cmd_par3);
1677 txp_set_filter(struct txp_softc *sc)
1679 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1681 struct ifmultiaddr *ifma;
1683 if (ifp->if_flags & IFF_PROMISC) {
1684 filter = TXP_RXFILT_PROMISC;
1688 filter = TXP_RXFILT_DIRECT;
1690 if (ifp->if_flags & IFF_BROADCAST)
1691 filter |= TXP_RXFILT_BROADCAST;
1693 if (ifp->if_flags & IFF_ALLMULTI) {
1694 filter |= TXP_RXFILT_ALLMULTI;
1696 uint32_t hashbit, hash[2];
1699 hash[0] = hash[1] = 0;
1701 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1702 if (ifma->ifma_addr->sa_family != AF_LINK)
1706 hashbit = (uint16_t)(ether_crc32_be(
1707 LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1708 ETHER_ADDR_LEN) & (64 - 1));
1709 hash[hashbit / 32] |= (1 << hashbit % 32);
1713 filter |= TXP_RXFILT_HASHMULTI;
1714 txp_command(sc, TXP_CMD_MCAST_HASH_MASK_WRITE,
1715 2, hash[0], hash[1], NULL, NULL, NULL, 0);
1720 txp_command(sc, TXP_CMD_RX_FILTER_WRITE, filter, 0, 0,
1721 NULL, NULL, NULL, 1);
1725 txp_capabilities(struct txp_softc *sc)
1727 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1728 struct txp_rsp_desc *rsp = NULL;
1729 struct txp_ext_desc *ext;
1731 if (txp_command2(sc, TXP_CMD_OFFLOAD_READ, 0, 0, 0, NULL, 0, &rsp, 1))
1734 if (rsp->rsp_numdesc != 1)
1736 ext = (struct txp_ext_desc *)(rsp + 1);
1738 sc->sc_tx_capability = ext->ext_1 & OFFLOAD_MASK;
1739 sc->sc_rx_capability = ext->ext_2 & OFFLOAD_MASK;
1740 ifp->if_capabilities = 0;
1742 if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_VLAN) {
1743 sc->sc_tx_capability |= OFFLOAD_VLAN;
1744 sc->sc_rx_capability |= OFFLOAD_VLAN;
1745 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
1750 if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_IPSEC) {
1751 sc->sc_tx_capability |= OFFLOAD_IPSEC;
1752 sc->sc_rx_capability |= OFFLOAD_IPSEC;
1753 ifp->if_capabilities |= IFCAP_IPSEC;
1757 if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_IPCKSUM) {
1758 sc->sc_tx_capability |= OFFLOAD_IPCKSUM;
1759 sc->sc_rx_capability |= OFFLOAD_IPCKSUM;
1760 ifp->if_capabilities |= IFCAP_HWCSUM;
1761 ifp->if_hwassist |= CSUM_IP;
1764 if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_TCPCKSUM) {
1766 sc->sc_tx_capability |= OFFLOAD_TCPCKSUM;
1768 sc->sc_rx_capability |= OFFLOAD_TCPCKSUM;
1769 ifp->if_capabilities |= IFCAP_HWCSUM;
1772 if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_UDPCKSUM) {
1774 sc->sc_tx_capability |= OFFLOAD_UDPCKSUM;
1776 sc->sc_rx_capability |= OFFLOAD_UDPCKSUM;
1777 ifp->if_capabilities |= IFCAP_HWCSUM;
1779 ifp->if_capenable = ifp->if_capabilities;
1781 if (txp_command(sc, TXP_CMD_OFFLOAD_WRITE, 0,
1782 sc->sc_tx_capability, sc->sc_rx_capability, NULL, NULL, NULL, 1))
1787 kfree(rsp, M_DEVBUF);