1 /* $NetBSD: if_temac.c,v 1.4 2008/02/12 18:03:43 dyoung Exp $ */
4 * Copyright (c) 2006 Jachym Holecek
7 * Written for DFC Design, s.r.o.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Driver for Xilinx LocalLink TEMAC as wired on the GSRD platform.
39 * - Support jumbo frames
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: if_temac.c,v 1.4 2008/02/12 18:03:43 dyoung Exp $");
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/socket.h>
52 #include <sys/ioctl.h>
53 #include <sys/device.h>
55 #include <uvm/uvm_extern.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
60 #include <net/if_ether.h>
66 #include <machine/bus.h>
68 #include <evbppc/virtex/idcr.h>
69 #include <evbppc/virtex/dev/xcvbusvar.h>
70 #include <evbppc/virtex/dev/cdmacreg.h>
71 #include <evbppc/virtex/dev/temacreg.h>
72 #include <evbppc/virtex/dev/temacvar.h>
74 #include <dev/mii/miivar.h>
77 /* This is outside of TEMAC's DCR window, we have to hardcode it... */
78 #define DCR_ETH_BASE 0x0030
80 #define TEMAC_REGDEBUG 0
81 #define TEMAC_RXDEBUG 0
82 #define TEMAC_TXDEBUG 0
84 #if TEMAC_RXDEBUG > 0 || TEMAC_TXDEBUG > 0
90 #if TEMAC_REGDEBUG > 0
91 #define TRACEREG(arg) printf arg
93 #define TRACEREG(arg) /* nop */
96 /* DMA control chains take up one (16KB) page. */
97 #define TEMAC_NTXDESC 256
98 #define TEMAC_NRXDESC 256
100 #define TEMAC_TXQLEN 64 /* Software Tx queue length */
101 #define TEMAC_NTXSEG 16 /* Maximum Tx segments per packet */
103 #define TEMAC_NRXSEG 1 /* Maximum Rx segments per packet */
104 #define TEMAC_RXPERIOD 1 /* Interrupt every N descriptors. */
105 #define TEMAC_RXTIMO_HZ 100 /* Rx reaper frequency */
107 /* Next Tx descriptor and descriptor's offset WRT sc_cdaddr. */
108 #define TEMAC_TXSINC(n, i) (((n) + TEMAC_TXQLEN + (i)) % TEMAC_TXQLEN)
109 #define TEMAC_TXINC(n, i) (((n) + TEMAC_NTXDESC + (i)) % TEMAC_NTXDESC)
111 #define TEMAC_TXSNEXT(n) TEMAC_TXSINC((n), 1)
112 #define TEMAC_TXNEXT(n) TEMAC_TXINC((n), 1)
113 #define TEMAC_TXDOFF(n) (offsetof(struct temac_control, cd_txdesc) + \
114 (n) * sizeof(struct cdmac_descr))
116 /* Next Rx descriptor and descriptor's offset WRT sc_cdaddr. */
117 #define TEMAC_RXINC(n, i) (((n) + TEMAC_NRXDESC + (i)) % TEMAC_NRXDESC)
118 #define TEMAC_RXNEXT(n) TEMAC_RXINC((n), 1)
119 #define TEMAC_RXDOFF(n) (offsetof(struct temac_control, cd_rxdesc) + \
120 (n) * sizeof(struct cdmac_descr))
121 #define TEMAC_ISINTR(i) (((i) % TEMAC_RXPERIOD) == 0)
122 #define TEMAC_ISLAST(i) ((i) == (TEMAC_NRXDESC - 1))
125 struct temac_control
{
126 struct cdmac_descr cd_txdesc
[TEMAC_NTXDESC
];
127 struct cdmac_descr cd_rxdesc
[TEMAC_NRXDESC
];
130 struct temac_txsoft
{
131 bus_dmamap_t txs_dmap
;
132 struct mbuf
*txs_mbuf
;
136 struct temac_rxsoft
{
137 bus_dmamap_t rxs_dmap
;
138 struct mbuf
*rxs_mbuf
;
142 struct device sc_dev
;
143 struct ethercom sc_ec
;
144 #define sc_if sc_ec.ec_if
146 /* Peripheral registers */
147 bus_space_tag_t sc_iot
;
148 bus_space_handle_t sc_ioh
;
150 /* CDMAC channel registers */
151 bus_space_tag_t sc_dma_rxt
;
152 bus_space_handle_t sc_dma_rxh
; /* Rx channel */
153 bus_space_handle_t sc_dma_rsh
; /* Rx status */
155 bus_space_tag_t sc_dma_txt
;
156 bus_space_handle_t sc_dma_txh
; /* Tx channel */
157 bus_space_handle_t sc_dma_tsh
; /* Tx status */
159 struct temac_txsoft sc_txsoft
[TEMAC_TXQLEN
];
160 struct temac_rxsoft sc_rxsoft
[TEMAC_NRXDESC
];
162 struct callout sc_rx_timo
;
163 struct callout sc_mii_tick
;
164 struct mii_data sc_mii
;
166 bus_dmamap_t sc_control_dmap
;
167 #define sc_cdaddr sc_control_dmap->dm_segs[0].ds_addr
169 struct temac_control
*sc_control_data
;
170 #define sc_rxdescs sc_control_data->cd_rxdesc
171 #define sc_txdescs sc_control_data->cd_txdesc
185 int sc_dead
; /* Rx/Tx DMA error (fatal) */
195 bus_dma_tag_t sc_dmat
;
198 /* Device interface. */
199 static void temac_attach(struct device
*, struct device
*, void *);
201 /* Ifnet interface. */
202 static int temac_init(struct ifnet
*);
203 static int temac_ioctl(struct ifnet
*, u_long
, void *);
204 static void temac_start(struct ifnet
*);
205 static void temac_stop(struct ifnet
*, int);
207 /* Media management. */
208 static int temac_mii_readreg(struct device
*, int, int);
209 static void temac_mii_statchg(struct device
*);
210 static void temac_mii_tick(void *);
211 static void temac_mii_writereg(struct device
*, int, int, int);
213 /* Indirect hooks. */
214 static void temac_shutdown(void *);
215 static void temac_rx_intr(void *);
216 static void temac_tx_intr(void *);
219 static inline void temac_rxcdsync(struct temac_softc
*, int, int, int);
220 static inline void temac_txcdsync(struct temac_softc
*, int, int, int);
221 static void temac_txreap(struct temac_softc
*);
222 static void temac_rxreap(struct temac_softc
*);
223 static int temac_rxalloc(struct temac_softc
*, int, int);
224 static void temac_rxtimo(void *);
225 static void temac_rxdrain(struct temac_softc
*);
226 static void temac_reset(struct temac_softc
*);
227 static void temac_txkick(struct temac_softc
*);
229 /* Register access. */
230 static inline void gmi_write_8(uint32_t, uint32_t, uint32_t);
231 static inline void gmi_write_4(uint32_t, uint32_t);
232 static inline void gmi_read_8(uint32_t, uint32_t *, uint32_t *);
233 static inline uint32_t gmi_read_4(uint32_t);
234 static inline void hif_wait_stat(uint32_t);
236 #define cdmac_rx_stat(sc) \
237 bus_space_read_4((sc)->sc_dma_rxt, (sc)->sc_dma_rsh, 0 /* XXX hack */)
239 #define cdmac_rx_reset(sc) \
240 bus_space_write_4((sc)->sc_dma_rxt, (sc)->sc_dma_rsh, 0, CDMAC_STAT_RESET)
242 #define cdmac_rx_start(sc, val) \
243 bus_space_write_4((sc)->sc_dma_rxt, (sc)->sc_dma_rxh, CDMAC_CURDESC, (val))
245 #define cdmac_tx_stat(sc) \
246 bus_space_read_4((sc)->sc_dma_txt, (sc)->sc_dma_tsh, 0 /* XXX hack */)
248 #define cdmac_tx_reset(sc) \
249 bus_space_write_4((sc)->sc_dma_txt, (sc)->sc_dma_tsh, 0, CDMAC_STAT_RESET)
251 #define cdmac_tx_start(sc, val) \
252 bus_space_write_4((sc)->sc_dma_txt, (sc)->sc_dma_txh, CDMAC_CURDESC, (val))
255 CFATTACH_DECL(temac
, sizeof(struct temac_softc
),
256 xcvbus_child_match
, temac_attach
, NULL
, NULL
);
260 * Private bus utilities.
263 hif_wait_stat(uint32_t mask
)
267 while (mask
!= (mfidcr(IDCR_HIF_STAT
) & mask
)) {
269 printf("%s: timeout waiting for 0x%08x\n",
276 TRACEREG(("%s: stat %#08x loops %d\n", __func__
, mask
, i
));
280 gmi_write_4(uint32_t addr
, uint32_t lo
)
282 mtidcr(IDCR_HIF_ARG0
, lo
);
283 mtidcr(IDCR_HIF_CTRL
, (addr
& HIF_CTRL_GMIADDR
) | HIF_CTRL_WRITE
);
284 hif_wait_stat(HIF_STAT_GMIWR
);
286 TRACEREG(("%s: %#08x <- %#08x\n", __func__
, addr
, lo
));
290 gmi_write_8(uint32_t addr
, uint32_t lo
, uint32_t hi
)
292 mtidcr(IDCR_HIF_ARG1
, hi
);
293 gmi_write_4(addr
, lo
);
297 gmi_read_8(uint32_t addr
, uint32_t *lo
, uint32_t *hi
)
299 *lo
= gmi_read_4(addr
);
300 *hi
= mfidcr(IDCR_HIF_ARG1
);
303 static inline uint32_t
304 gmi_read_4(uint32_t addr
)
308 mtidcr(IDCR_HIF_CTRL
, addr
& HIF_CTRL_GMIADDR
);
309 hif_wait_stat(HIF_STAT_GMIRR
);
311 res
= mfidcr(IDCR_HIF_ARG0
);
312 TRACEREG(("%s: %#08x -> %#08x\n", __func__
, addr
, res
));
320 temac_attach(struct device
*parent
, struct device
*self
, void *aux
)
322 struct xcvbus_attach_args
*vaa
= aux
;
323 struct ll_dmac
*rx
= vaa
->vaa_rx_dmac
;
324 struct ll_dmac
*tx
= vaa
->vaa_tx_dmac
;
325 struct temac_softc
*sc
= (struct temac_softc
*)self
;
326 struct ifnet
*ifp
= &sc
->sc_if
;
327 struct mii_data
*mii
= &sc
->sc_mii
;
328 uint8_t enaddr
[ETHER_ADDR_LEN
];
329 bus_dma_segment_t seg
;
332 printf(": TEMAC\n"); /* XXX will be LL_TEMAC, PLB_TEMAC */
337 sc
->sc_dmat
= vaa
->vaa_dmat
;
339 sc
->sc_rx_drained
= 1;
341 sc
->sc_iot
= vaa
->vaa_iot
;
342 sc
->sc_dma_rxt
= rx
->dmac_iot
;
343 sc
->sc_dma_txt
= tx
->dmac_iot
;
346 * Map HIF and receive/transmit dmac registers.
348 if ((error
= bus_space_map(vaa
->vaa_iot
, vaa
->vaa_addr
, TEMAC_SIZE
, 0,
349 &sc
->sc_ioh
)) != 0) {
350 printf("%s: could not map registers\n", device_xname(self
));
354 if ((error
= bus_space_map(sc
->sc_dma_rxt
, rx
->dmac_ctrl_addr
,
355 CDMAC_CTRL_SIZE
, 0, &sc
->sc_dma_rxh
)) != 0) {
356 printf("%s: could not map Rx control registers\n",
360 if ((error
= bus_space_map(sc
->sc_dma_rxt
, rx
->dmac_stat_addr
,
361 CDMAC_STAT_SIZE
, 0, &sc
->sc_dma_rsh
)) != 0) {
362 printf("%s: could not map Rx status register\n",
367 if ((error
= bus_space_map(sc
->sc_dma_txt
, tx
->dmac_ctrl_addr
,
368 CDMAC_CTRL_SIZE
, 0, &sc
->sc_dma_txh
)) != 0) {
369 printf("%s: could not map Tx control registers\n",
373 if ((error
= bus_space_map(sc
->sc_dma_txt
, tx
->dmac_stat_addr
,
374 CDMAC_STAT_SIZE
, 0, &sc
->sc_dma_tsh
)) != 0) {
375 printf("%s: could not map Tx status register\n",
381 * Allocate and initialize DMA control chains.
383 if ((error
= bus_dmamem_alloc(sc
->sc_dmat
,
384 sizeof(struct temac_control
), 8, 0, &seg
, 1, &nseg
, 0)) != 0) {
385 printf("%s: could not allocate control data\n",
386 sc
->sc_dev
.dv_xname
);
390 if ((error
= bus_dmamem_map(sc
->sc_dmat
, &seg
, nseg
,
391 sizeof(struct temac_control
),
392 (void **)&sc
->sc_control_data
, BUS_DMA_COHERENT
)) != 0) {
393 printf("%s: could not map control data\n",
394 sc
->sc_dev
.dv_xname
);
398 if ((error
= bus_dmamap_create(sc
->sc_dmat
,
399 sizeof(struct temac_control
), 1,
400 sizeof(struct temac_control
), 0, 0, &sc
->sc_control_dmap
)) != 0) {
401 printf("%s: could not create control data DMA map\n",
402 sc
->sc_dev
.dv_xname
);
406 if ((error
= bus_dmamap_load(sc
->sc_dmat
, sc
->sc_control_dmap
,
407 sc
->sc_control_data
, sizeof(struct temac_control
), NULL
, 0)) != 0) {
408 printf("%s: could not load control data DMA map\n",
409 sc
->sc_dev
.dv_xname
);
414 * Link descriptor chains.
416 memset(sc
->sc_control_data
, 0, sizeof(struct temac_control
));
418 for (i
= 0; i
< TEMAC_NTXDESC
; i
++) {
419 sc
->sc_txdescs
[i
].desc_next
= sc
->sc_cdaddr
+
420 TEMAC_TXDOFF(TEMAC_TXNEXT(i
));
421 sc
->sc_txdescs
[i
].desc_stat
= CDMAC_STAT_DONE
;
423 for (i
= 0; i
< TEMAC_NRXDESC
; i
++) {
424 sc
->sc_rxdescs
[i
].desc_next
= sc
->sc_cdaddr
+
425 TEMAC_RXDOFF(TEMAC_RXNEXT(i
));
426 sc
->sc_txdescs
[i
].desc_stat
= CDMAC_STAT_DONE
;
429 bus_dmamap_sync(sc
->sc_dmat
, sc
->sc_control_dmap
, 0,
430 sizeof(struct temac_control
),
431 BUS_DMASYNC_PREREAD
| BUS_DMASYNC_PREWRITE
);
434 * Initialize software state for transmit/receive jobs.
436 for (i
= 0; i
< TEMAC_TXQLEN
; i
++) {
437 if ((error
= bus_dmamap_create(sc
->sc_dmat
,
438 ETHER_MAX_LEN_JUMBO
, TEMAC_NTXSEG
, ETHER_MAX_LEN_JUMBO
,
439 0, 0, &sc
->sc_txsoft
[i
].txs_dmap
)) != 0) {
440 printf("%s: could not create Tx DMA map %d\n",
441 sc
->sc_dev
.dv_xname
, i
);
444 sc
->sc_txsoft
[i
].txs_mbuf
= NULL
;
445 sc
->sc_txsoft
[i
].txs_last
= 0;
448 for (i
= 0; i
< TEMAC_NRXDESC
; i
++) {
449 if ((error
= bus_dmamap_create(sc
->sc_dmat
,
450 MCLBYTES
, TEMAC_NRXSEG
, MCLBYTES
, 0, 0,
451 &sc
->sc_rxsoft
[i
].rxs_dmap
)) != 0) {
452 printf("%s: could not create Rx DMA map %d\n",
453 sc
->sc_dev
.dv_xname
, i
);
456 sc
->sc_rxsoft
[i
].rxs_mbuf
= NULL
;
460 * Setup transfer interrupt handlers.
464 sc
->sc_rx_ih
= ll_dmac_intr_establish(rx
->dmac_chan
,
466 if (sc
->sc_rx_ih
== NULL
) {
467 printf("%s: could not establish Rx interrupt\n",
472 sc
->sc_tx_ih
= ll_dmac_intr_establish(tx
->dmac_chan
,
474 if (sc
->sc_tx_ih
== NULL
) {
475 printf("%s: could not establish Tx interrupt\n",
480 /* XXXFreza: faked, should read unicast address filter. */
489 * Initialize the TEMAC.
493 /* Configure MDIO link. */
494 gmi_write_4(TEMAC_GMI_MGMTCF
, GMI_MGMT_CLKDIV_100MHz
| GMI_MGMT_MDIO
);
496 /* Initialize PHY. */
498 mii
->mii_readreg
= temac_mii_readreg
;
499 mii
->mii_writereg
= temac_mii_writereg
;
500 mii
->mii_statchg
= temac_mii_statchg
;
501 sc
->sc_ec
.ec_mii
= mii
;
502 ifmedia_init(&mii
->mii_media
, 0, ether_mediachange
, ether_mediastatus
);
504 mii_attach(&sc
->sc_dev
, mii
, 0xffffffff, MII_PHY_ANY
,
506 if (LIST_FIRST(&mii
->mii_phys
) == NULL
) {
507 ifmedia_add(&mii
->mii_media
, IFM_ETHER
|IFM_NONE
, 0, NULL
);
508 ifmedia_set(&mii
->mii_media
, IFM_ETHER
|IFM_NONE
);
510 ifmedia_set(&mii
->mii_media
, IFM_ETHER
|IFM_AUTO
);
513 /* Hold PHY in reset. */
514 bus_space_write_4(sc
->sc_iot
, sc
->sc_ioh
, TEMAC_RESET
, TEMAC_RESET_PHY
);
517 bus_space_write_4(sc
->sc_iot
, sc
->sc_ioh
, TEMAC_RESET
,
521 /* Reset peripheral, awakes PHY and EMAC. */
522 bus_space_write_4(sc
->sc_iot
, sc
->sc_ioh
, TEMAC_RESET
,
526 /* (Re-)Configure MDIO link. */
527 gmi_write_4(TEMAC_GMI_MGMTCF
, GMI_MGMT_CLKDIV_100MHz
| GMI_MGMT_MDIO
);
530 * Hook up with network stack.
532 strcpy(ifp
->if_xname
, sc
->sc_dev
.dv_xname
);
534 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
535 ifp
->if_ioctl
= temac_ioctl
;
536 ifp
->if_start
= temac_start
;
537 ifp
->if_init
= temac_init
;
538 ifp
->if_stop
= temac_stop
;
539 ifp
->if_watchdog
= NULL
;
540 IFQ_SET_READY(&ifp
->if_snd
);
541 IFQ_SET_MAXLEN(&ifp
->if_snd
, TEMAC_TXQLEN
);
543 sc
->sc_ec
.ec_capabilities
|= ETHERCAP_VLAN_MTU
;
546 ether_ifattach(ifp
, enaddr
);
548 sc
->sc_sdhook
= shutdownhook_establish(temac_shutdown
, sc
);
549 if (sc
->sc_sdhook
== NULL
)
550 printf("%s: WARNING: unable to establish shutdown hook\n",
553 callout_setfunc(&sc
->sc_mii_tick
, temac_mii_tick
, sc
);
554 callout_setfunc(&sc
->sc_rx_timo
, temac_rxtimo
, sc
);
559 ll_dmac_intr_disestablish(rx
->dmac_chan
, sc
->sc_rx_ih
);
562 for (--i
; i
>= 0; i
--)
563 bus_dmamap_destroy(sc
->sc_dmat
, sc
->sc_rxsoft
[i
].rxs_dmap
);
566 for (--i
; i
>= 0; i
--)
567 bus_dmamap_destroy(sc
->sc_dmat
, sc
->sc_txsoft
[i
].txs_dmap
);
569 bus_dmamap_destroy(sc
->sc_dmat
, sc
->sc_control_dmap
);
571 bus_dmamem_unmap(sc
->sc_dmat
, (void *)sc
->sc_control_data
,
572 sizeof(struct temac_control
));
574 bus_dmamem_free(sc
->sc_dmat
, &seg
, nseg
);
576 printf("%s: error = %d\n", device_xname(self
), error
);
583 temac_init(struct ifnet
*ifp
)
585 struct temac_softc
*sc
= (struct temac_softc
*)ifp
->if_softc
;
589 /* Reset DMA channels. */
593 /* Set current media. */
594 if ((error
= ether_mediachange(ifp
)) != 0)
597 callout_schedule(&sc
->sc_mii_tick
, hz
);
599 /* Enable EMAC engine. */
600 rcr
= (gmi_read_4(TEMAC_GMI_RXCF1
) | GMI_RX_ENABLE
) &
601 ~(GMI_RX_JUMBO
| GMI_RX_FCS
);
602 gmi_write_4(TEMAC_GMI_RXCF1
, rcr
);
604 tcr
= (gmi_read_4(TEMAC_GMI_TXCF
) | GMI_TX_ENABLE
) &
605 ~(GMI_TX_JUMBO
| GMI_TX_FCS
);
606 gmi_write_4(TEMAC_GMI_TXCF
, tcr
);
608 /* XXXFreza: Force promiscuous mode, for now. */
609 gmi_write_4(TEMAC_GMI_AFM
, GMI_AFM_PROMISC
);
610 ifp
->if_flags
|= IFF_PROMISC
;
612 /* Rx/Tx queues are drained -- either from attach() or stop(). */
613 sc
->sc_txsfree
= TEMAC_TXQLEN
;
617 sc
->sc_txfree
= TEMAC_NTXDESC
;
623 /* Allocate and map receive buffers. */
624 if (sc
->sc_rx_drained
) {
625 for (i
= 0; i
< TEMAC_NRXDESC
; i
++) {
626 if ((error
= temac_rxalloc(sc
, i
, 1)) != 0) {
627 printf("%s: failed to allocate Rx "
629 sc
->sc_dev
.dv_xname
, i
);
635 sc
->sc_rx_drained
= 0;
637 temac_rxcdsync(sc
, 0, TEMAC_NRXDESC
,
638 BUS_DMASYNC_PREREAD
|BUS_DMASYNC_PREWRITE
);
639 cdmac_rx_start(sc
, sc
->sc_cdaddr
+ TEMAC_RXDOFF(0));
642 ifp
->if_flags
|= IFF_RUNNING
;
643 ifp
->if_flags
&= ~IFF_OACTIVE
;
649 temac_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
651 struct temac_softc
*sc
= (struct temac_softc
*)ifp
->if_softc
;
658 ret
= ether_ioctl(ifp
, cmd
, data
);
664 temac_start(struct ifnet
*ifp
)
666 struct temac_softc
*sc
= (struct temac_softc
*)ifp
->if_softc
;
667 struct temac_txsoft
*txs
;
670 int error
, head
, nsegs
, i
;
674 txs
= NULL
; /* gcc */
679 KASSERT(sc
->sc_txfree
>= 0);
680 KASSERT(sc
->sc_txsfree
>= 0);
683 * Push mbufs into descriptor chain until we drain the interface
684 * queue or run out of descriptors. We'll mark the first segment
685 * as "done" in hope that we might put CDMAC interrupt above IPL_NET
686 * and have it start jobs & mark packets for GC preemtively for
687 * us -- creativity due to limitations in CDMAC transfer engine
688 * (it really consumes lists, not circular queues, AFAICS).
690 * We schedule one interrupt per Tx batch.
693 IFQ_POLL(&ifp
->if_snd
, m
);
697 if (sc
->sc_txsfree
== 0) {
698 ifp
->if_flags
|= IFF_OACTIVE
;
702 txs
= &sc
->sc_txsoft
[sc
->sc_txscur
];
703 dmap
= txs
->txs_dmap
;
705 if (txs
->txs_mbuf
!= NULL
)
710 if ((error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, dmap
, m
,
711 BUS_DMA_WRITE
| BUS_DMA_NOWAIT
)) != 0) {
712 if (error
== EFBIG
) {
713 printf("%s: Tx consumes too many segments, "
714 "dropped\n", sc
->sc_dev
.dv_xname
);
715 IFQ_DEQUEUE(&ifp
->if_snd
, m
);
719 printf("%s: Tx stall due to resource "
720 "shortage\n", sc
->sc_dev
.dv_xname
);
726 * If we're short on DMA descriptors, notify upper layers
727 * and leave this packet for later.
729 if (dmap
->dm_nsegs
> sc
->sc_txfree
) {
730 bus_dmamap_unload(sc
->sc_dmat
, dmap
);
731 ifp
->if_flags
|= IFF_OACTIVE
;
735 IFQ_DEQUEUE(&ifp
->if_snd
, m
);
737 bus_dmamap_sync(sc
->sc_dmat
, dmap
, 0, dmap
->dm_mapsize
,
738 BUS_DMASYNC_PREWRITE
);
742 * Map the packet into descriptor chain. XXX We'll want
743 * to fill checksum offload commands here.
745 * We would be in a race if we weren't blocking CDMAC intr
746 * at this point -- we need to be locked against txreap()
747 * because of dmasync ops.
750 temac_txcdsync(sc
, sc
->sc_txcur
, dmap
->dm_nsegs
,
751 BUS_DMASYNC_POSTREAD
|BUS_DMASYNC_POSTWRITE
);
753 for (i
= 0; i
< dmap
->dm_nsegs
; i
++) {
754 sc
->sc_txdescs
[sc
->sc_txcur
].desc_addr
=
755 dmap
->dm_segs
[i
].ds_addr
;
756 sc
->sc_txdescs
[sc
->sc_txcur
].desc_size
=
757 dmap
->dm_segs
[i
].ds_len
;
758 sc
->sc_txdescs
[sc
->sc_txcur
].desc_stat
=
759 (i
== 0 ? CDMAC_STAT_SOP
: 0) |
760 (i
== (dmap
->dm_nsegs
- 1) ? CDMAC_STAT_EOP
: 0);
762 sc
->sc_txcur
= TEMAC_TXNEXT(sc
->sc_txcur
);
765 sc
->sc_txfree
-= dmap
->dm_nsegs
;
766 nsegs
+= dmap
->dm_nsegs
;
768 sc
->sc_txscur
= TEMAC_TXSNEXT(sc
->sc_txscur
);
772 /* Get data running if we queued any. */
774 int tail
= TEMAC_TXINC(sc
->sc_txcur
, -1);
776 /* Mark the last packet in this job. */
779 /* Mark the last descriptor in this job. */
780 sc
->sc_txdescs
[tail
].desc_stat
|= CDMAC_STAT_STOP
|
782 temac_txcdsync(sc
, head
, nsegs
,
783 BUS_DMASYNC_PREREAD
|BUS_DMASYNC_PREWRITE
);
786 #if TEMAC_TXDEBUG > 0
787 printf("%s: start: txcur %03d -> %03d, nseg %03d\n",
788 sc
->sc_dev
.dv_xname
, head
, sc
->sc_txcur
, nsegs
);
794 temac_stop(struct ifnet
*ifp
, int disable
)
796 struct temac_softc
*sc
= (struct temac_softc
*)ifp
->if_softc
;
797 struct temac_txsoft
*txs
;
801 printf("%s: stop\n", device_xname(&sc
->sc_dev
));
805 callout_stop(&sc
->sc_mii_tick
);
806 mii_down(&sc
->sc_mii
);
808 /* Stop the engine. */
811 /* Drain buffers queues (unconditionally). */
814 for (i
= 0; i
< TEMAC_TXQLEN
; i
++) {
815 txs
= &sc
->sc_txsoft
[i
];
817 if (txs
->txs_mbuf
!= NULL
) {
818 bus_dmamap_unload(sc
->sc_dmat
, txs
->txs_dmap
);
819 m_freem(txs
->txs_mbuf
);
820 txs
->txs_mbuf
= NULL
;
826 /* Acknowledge we're down. */
827 ifp
->if_flags
&= ~(IFF_RUNNING
|IFF_OACTIVE
);
831 temac_mii_readreg(struct device
*self
, int phy
, int reg
)
833 mtidcr(IDCR_HIF_ARG0
, (phy
<< 5) | reg
);
834 mtidcr(IDCR_HIF_CTRL
, TEMAC_GMI_MII_ADDR
);
835 hif_wait_stat(HIF_STAT_MIIRR
);
837 return (int)mfidcr(IDCR_HIF_ARG0
);
841 temac_mii_writereg(struct device
*self
, int phy
, int reg
, int val
)
843 mtidcr(IDCR_HIF_ARG0
, val
);
844 mtidcr(IDCR_HIF_CTRL
, TEMAC_GMI_MII_WRVAL
| HIF_CTRL_WRITE
);
845 mtidcr(IDCR_HIF_ARG0
, (phy
<< 5) | reg
);
846 mtidcr(IDCR_HIF_CTRL
, TEMAC_GMI_MII_ADDR
| HIF_CTRL_WRITE
);
847 hif_wait_stat(HIF_STAT_MIIWR
);
851 temac_mii_statchg(struct device
*self
)
853 struct temac_softc
*sc
= (struct temac_softc
*)self
;
854 uint32_t rcf
, tcf
, mmc
;
856 /* Full/half duplex link. */
857 rcf
= gmi_read_4(TEMAC_GMI_RXCF1
);
858 tcf
= gmi_read_4(TEMAC_GMI_TXCF
);
860 if (sc
->sc_mii
.mii_media_active
& IFM_FDX
) {
861 gmi_write_4(TEMAC_GMI_RXCF1
, rcf
& ~GMI_RX_HDX
);
862 gmi_write_4(TEMAC_GMI_TXCF
, tcf
& ~GMI_TX_HDX
);
864 gmi_write_4(TEMAC_GMI_RXCF1
, rcf
| GMI_RX_HDX
);
865 gmi_write_4(TEMAC_GMI_TXCF
, tcf
| GMI_TX_HDX
);
869 mmc
= gmi_read_4(TEMAC_GMI_MMC
) & ~GMI_MMC_SPEED_MASK
;
871 switch (IFM_SUBTYPE(sc
->sc_mii
.mii_media_active
)) {
874 * XXXFreza: the GMAC is not happy with 10Mbit ethernet,
875 * although the documentation claims it's supported. Maybe
876 * it's just my equipment...
878 mmc
|= GMI_MMC_SPEED_10
;
881 mmc
|= GMI_MMC_SPEED_100
;
884 mmc
|= GMI_MMC_SPEED_1000
;
888 gmi_write_4(TEMAC_GMI_MMC
, mmc
);
892 temac_mii_tick(void *arg
)
894 struct temac_softc
*sc
= (struct temac_softc
*)arg
;
897 if (!device_is_active(&sc
->sc_dev
))
901 mii_tick(&sc
->sc_mii
);
904 callout_schedule(&sc
->sc_mii_tick
, hz
);
911 temac_shutdown(void *arg
)
913 struct temac_softc
*sc
= (struct temac_softc
*)arg
;
919 temac_tx_intr(void *arg
)
921 struct temac_softc
*sc
= (struct temac_softc
*)arg
;
924 /* XXX: We may need to splnet() here if cdmac(4) changes. */
926 if ((stat
= cdmac_tx_stat(sc
)) & CDMAC_STAT_ERROR
) {
927 printf("%s: transmit DMA is toast (%#08x), halted!\n",
928 sc
->sc_dev
.dv_xname
, stat
);
930 /* XXXFreza: how to signal this upstream? */
931 temac_stop(&sc
->sc_if
, 1);
936 printf("%s: tx intr 0x%08x\n", device_xname(&sc
->sc_dev
), stat
);
942 temac_rx_intr(void *arg
)
944 struct temac_softc
*sc
= (struct temac_softc
*)arg
;
947 /* XXX: We may need to splnet() here if cdmac(4) changes. */
949 if ((stat
= cdmac_rx_stat(sc
)) & CDMAC_STAT_ERROR
) {
950 printf("%s: receive DMA is toast (%#08x), halted!\n",
951 sc
->sc_dev
.dv_xname
, stat
);
953 /* XXXFreza: how to signal this upstream? */
954 temac_stop(&sc
->sc_if
, 1);
959 printf("%s: rx intr 0x%08x\n", device_xname(&sc
->sc_dev
), stat
);
968 temac_txcdsync(struct temac_softc
*sc
, int first
, int cnt
, int flag
)
970 if ((first
+ cnt
) > TEMAC_NTXDESC
) {
971 bus_dmamap_sync(sc
->sc_dmat
, sc
->sc_control_dmap
,
973 sizeof(struct cdmac_descr
) * (TEMAC_NTXDESC
- first
),
975 cnt
= (first
+ cnt
) % TEMAC_NTXDESC
;
979 bus_dmamap_sync(sc
->sc_dmat
, sc
->sc_control_dmap
,
981 sizeof(struct cdmac_descr
) * cnt
,
986 temac_rxcdsync(struct temac_softc
*sc
, int first
, int cnt
, int flag
)
988 if ((first
+ cnt
) > TEMAC_NRXDESC
) {
989 bus_dmamap_sync(sc
->sc_dmat
, sc
->sc_control_dmap
,
991 sizeof(struct cdmac_descr
) * (TEMAC_NRXDESC
- first
),
993 cnt
= (first
+ cnt
) % TEMAC_NRXDESC
;
997 bus_dmamap_sync(sc
->sc_dmat
, sc
->sc_control_dmap
,
999 sizeof(struct cdmac_descr
) * cnt
,
1004 temac_txreap(struct temac_softc
*sc
)
1006 struct temac_txsoft
*txs
;
1011 * Transmit interrupts happen on the last descriptor of Tx jobs.
1012 * Hence, every time we're called (and we assume txintr is our
1013 * only caller!), we reap packets upto and including the one
1014 * marked as last-in-batch.
1016 * XXX we rely on that we make EXACTLY one batch per intr, no more
1018 while (sc
->sc_txsfree
!= TEMAC_TXQLEN
) {
1019 txs
= &sc
->sc_txsoft
[sc
->sc_txsreap
];
1020 dmap
= txs
->txs_dmap
;
1022 sc
->sc_txreap
= TEMAC_TXINC(sc
->sc_txreap
, dmap
->dm_nsegs
);
1023 sc
->sc_txfree
+= dmap
->dm_nsegs
;
1025 bus_dmamap_unload(sc
->sc_dmat
, txs
->txs_dmap
);
1026 m_freem(txs
->txs_mbuf
);
1027 txs
->txs_mbuf
= NULL
;
1029 sc
->sc_if
.if_opackets
++;
1032 sc
->sc_txsreap
= TEMAC_TXSNEXT(sc
->sc_txsreap
);
1035 if (txs
->txs_last
) {
1037 sc
->sc_txbusy
= 0; /* channel stopped now */
1044 if (sent
&& (sc
->sc_if
.if_flags
& IFF_OACTIVE
))
1045 sc
->sc_if
.if_flags
&= ~IFF_OACTIVE
;
1049 temac_rxalloc(struct temac_softc
*sc
, int which
, int verbose
)
1051 struct temac_rxsoft
*rxs
;
1056 rxs
= &sc
->sc_rxsoft
[which
];
1058 /* The mbuf itself is not our problem, just clear DMA related stuff. */
1059 if (rxs
->rxs_mbuf
!= NULL
) {
1060 bus_dmamap_unload(sc
->sc_dmat
, rxs
->rxs_dmap
);
1061 rxs
->rxs_mbuf
= NULL
;
1065 * We would like to store mbuf and dmap in application specific
1066 * fields of the descriptor, but that doesn't work for Rx. Shame
1067 * on Xilinx for this (and for the useless timer architecture).
1069 * Hence each descriptor needs its own soft state. We may want
1070 * to merge multiple rxs's into a monster mbuf when we support
1071 * jumbo frames though. Also, we use single set of indexing
1072 * variables for both sc_rxdescs[] and sc_rxsoft[].
1074 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1077 printf("%s: out of Rx header mbufs\n",
1078 sc
->sc_dev
.dv_xname
);
1081 MCLAIM(m
, &sc
->sc_ec
.ec_rx_mowner
);
1083 MCLGET(m
, M_DONTWAIT
);
1084 if ((m
->m_flags
& M_EXT
) == 0) {
1086 printf("%s: out of Rx cluster mbufs\n",
1087 sc
->sc_dev
.dv_xname
);
1093 m
->m_pkthdr
.len
= m
->m_len
= MCLBYTES
;
1095 /* Make sure the payload after ethernet header is 4-aligned. */
1098 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, rxs
->rxs_dmap
, m
,
1102 printf("%s: could not map Rx descriptor %d, "
1103 "error = %d\n", sc
->sc_dev
.dv_xname
, which
, error
);
1105 rxs
->rxs_mbuf
= NULL
;
1112 (TEMAC_ISINTR(which
) ? CDMAC_STAT_INTR
: 0) |
1113 (TEMAC_ISLAST(which
) ? CDMAC_STAT_STOP
: 0);
1115 bus_dmamap_sync(sc
->sc_dmat
, rxs
->rxs_dmap
, 0,
1116 rxs
->rxs_dmap
->dm_mapsize
, BUS_DMASYNC_PREREAD
);
1118 /* Descriptor post-sync, if needed, left to the caller. */
1120 sc
->sc_rxdescs
[which
].desc_addr
= rxs
->rxs_dmap
->dm_segs
[0].ds_addr
;
1121 sc
->sc_rxdescs
[which
].desc_size
= rxs
->rxs_dmap
->dm_segs
[0].ds_len
;
1122 sc
->sc_rxdescs
[which
].desc_stat
= stat
;
1124 /* Descriptor pre-sync, if needed, left to the caller. */
1130 temac_rxreap(struct temac_softc
*sc
)
1132 struct ifnet
*ifp
= &sc
->sc_if
;
1133 uint32_t stat
, rxstat
, rxsize
;
1135 int nseg
, head
, tail
;
1137 head
= sc
->sc_rxreap
;
1142 * Collect finished entries on the Rx list, kick DMA if we hit
1143 * the end. DMA will always stop on the last descriptor in chain,
1144 * so it will never hit a reap-in-progress descriptor.
1147 /* Maybe we previously failed to refresh this one? */
1148 if (sc
->sc_rxsoft
[sc
->sc_rxreap
].rxs_mbuf
== NULL
) {
1149 if (temac_rxalloc(sc
, sc
->sc_rxreap
, 0) != 0)
1152 sc
->sc_rxreap
= TEMAC_RXNEXT(sc
->sc_rxreap
);
1155 temac_rxcdsync(sc
, sc
->sc_rxreap
, 1,
1156 BUS_DMASYNC_POSTREAD
|BUS_DMASYNC_POSTWRITE
);
1158 stat
= sc
->sc_rxdescs
[sc
->sc_rxreap
].desc_stat
;
1161 if ((stat
& CDMAC_STAT_DONE
) == 0)
1164 /* Count any decriptor we've collected, regardless of status. */
1167 /* XXXFreza: This won't work for jumbo frames. */
1169 if ((stat
& (CDMAC_STAT_EOP
| CDMAC_STAT_SOP
)) !=
1170 (CDMAC_STAT_EOP
| CDMAC_STAT_SOP
)) {
1171 printf("%s: Rx packet doesn't fit in "
1172 "one descriptor, stat = %#08x\n",
1173 sc
->sc_dev
.dv_xname
, stat
);
1177 /* Dissect TEMAC footer if this is end of packet. */
1178 rxstat
= sc
->sc_rxdescs
[sc
->sc_rxreap
].desc_rxstat
;
1179 rxsize
= sc
->sc_rxdescs
[sc
->sc_rxreap
].desc_rxsize
&
1182 if ((rxstat
& RXSTAT_GOOD
) == 0 ||
1183 (rxstat
& RXSTAT_SICK
) != 0) {
1184 printf("%s: corrupt Rx packet, rxstat = %#08x\n",
1185 sc
->sc_dev
.dv_xname
, rxstat
);
1189 /* We are now bound to succeed. */
1190 bus_dmamap_sync(sc
->sc_dmat
,
1191 sc
->sc_rxsoft
[sc
->sc_rxreap
].rxs_dmap
, 0,
1192 sc
->sc_rxsoft
[sc
->sc_rxreap
].rxs_dmap
->dm_mapsize
,
1193 BUS_DMASYNC_POSTREAD
);
1195 m
= sc
->sc_rxsoft
[sc
->sc_rxreap
].rxs_mbuf
;
1196 m
->m_pkthdr
.rcvif
= ifp
;
1197 m
->m_pkthdr
.len
= m
->m_len
= rxsize
;
1200 /* Get ready for more work. */
1201 tail
= sc
->sc_rxreap
;
1202 sc
->sc_rxreap
= TEMAC_RXNEXT(sc
->sc_rxreap
);
1204 /* On failures we reuse the descriptor and go ahead. */
1206 sc
->sc_rxdescs
[tail
].desc_stat
=
1207 (TEMAC_ISINTR(tail
) ? CDMAC_STAT_INTR
: 0) |
1208 (TEMAC_ISLAST(tail
) ? CDMAC_STAT_STOP
: 0);
1215 if (ifp
->if_bpf
!= NULL
)
1216 bpf_mtap(ifp
->if_bpf
, m
);
1220 (ifp
->if_input
)(ifp
, m
);
1222 /* Refresh descriptor, bail out if we're out of buffers. */
1223 if (temac_rxalloc(sc
, tail
, 1) != 0) {
1224 sc
->sc_rxreap
= TEMAC_RXINC(sc
->sc_rxreap
, -1);
1225 printf("%s: Rx give up for now\n", sc
->sc_dev
.dv_xname
);
1230 /* We may now have a contiguous ready-to-go chunk of descriptors. */
1232 #if TEMAC_RXDEBUG > 0
1233 printf("%s: rxreap: rxreap %03d -> %03d, nseg %03d\n",
1234 sc
->sc_dev
.dv_xname
, head
, sc
->sc_rxreap
, nseg
);
1236 temac_rxcdsync(sc
, head
, nseg
,
1237 BUS_DMASYNC_PREREAD
|BUS_DMASYNC_PREWRITE
);
1239 if (TEMAC_ISLAST(tail
))
1240 cdmac_rx_start(sc
, sc
->sc_cdaddr
+ TEMAC_RXDOFF(0));
1243 /* Ensure maximum Rx latency is kept under control. */
1244 callout_schedule(&sc
->sc_rx_timo
, hz
/ TEMAC_RXTIMO_HZ
);
1248 temac_rxtimo(void *arg
)
1250 struct temac_softc
*sc
= (struct temac_softc
*)arg
;
1253 /* We run TEMAC_RXTIMO_HZ times/sec to ensure Rx doesn't stall. */
1260 temac_reset(struct temac_softc
*sc
)
1264 /* Kill CDMAC channels. */
1268 /* Disable receiver. */
1269 rcr
= gmi_read_4(TEMAC_GMI_RXCF1
) & ~GMI_RX_ENABLE
;
1270 gmi_write_4(TEMAC_GMI_RXCF1
, rcr
);
1272 /* Disable transmitter. */
1273 tcr
= gmi_read_4(TEMAC_GMI_TXCF
) & ~GMI_TX_ENABLE
;
1274 gmi_write_4(TEMAC_GMI_TXCF
, tcr
);
1278 temac_rxdrain(struct temac_softc
*sc
)
1280 struct temac_rxsoft
*rxs
;
1283 for (i
= 0; i
< TEMAC_NRXDESC
; i
++) {
1284 rxs
= &sc
->sc_rxsoft
[i
];
1286 if (rxs
->rxs_mbuf
!= NULL
) {
1287 bus_dmamap_unload(sc
->sc_dmat
, rxs
->rxs_dmap
);
1288 m_freem(rxs
->rxs_mbuf
);
1289 rxs
->rxs_mbuf
= NULL
;
1293 sc
->sc_rx_drained
= 1;
1297 temac_txkick(struct temac_softc
*sc
)
1299 if (sc
->sc_txsoft
[sc
->sc_txsreap
].txs_mbuf
!= NULL
&&
1300 sc
->sc_txbusy
== 0) {
1301 cdmac_tx_start(sc
, sc
->sc_cdaddr
+ TEMAC_TXDOFF(sc
->sc_txreap
));