mtd8xx.c revision 1.6
1/*	$OpenBSD: mtd8xx.c,v 1.6 2004/09/23 17:45:16 brad Exp $	*/
2
3/*
4 * Copyright (c) 2003 Oleg Safiullin <form@pdp11.org.ru>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice unmodified, this list of conditions, and the following
12 *    disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31#include "bpfilter.h"
32
33#include <sys/param.h>
34#include <sys/mbuf.h>
35#include <sys/systm.h>
36#include <sys/device.h>
37#include <sys/socket.h>
38#include <sys/ioctl.h>
39
40#include <net/if.h>
41#include <net/if_media.h>
42
43#if NBPFILTER > 0
44#include <net/bpf.h>
45#endif
46
47#ifdef INET
48#include <netinet/in.h>
49#include <netinet/if_ether.h>
50#endif
51
52#include <machine/bus.h>
53
54#include <dev/mii/mii.h>
55#include <dev/mii/miivar.h>
56
57#include <dev/pci/pcidevs.h>
58#include <dev/pci/pcivar.h>
59
60#include <dev/ic/mtd8xxreg.h>
61#include <dev/ic/mtd8xxvar.h>
62
63
64static int mtd_ifmedia_upd(struct ifnet *);
65static void mtd_ifmedia_sts(struct ifnet *, struct ifmediareq *);
66
67static u_int32_t mtd_mii_command(struct mtd_softc *, int, int, int);
68static int mtd_miibus_readreg(struct device *, int, int);
69static void mtd_miibus_writereg(struct device *, int, int, int);
70static void mtd_miibus_statchg(struct device *);
71static void mtd_setmulti(struct mtd_softc *);
72
73static int mtd_encap(struct mtd_softc *, struct mbuf *, u_int32_t *);
74static int mtd_list_rx_init(struct mtd_softc *);
75static void mtd_list_tx_init(struct mtd_softc *);
76static int mtd_newbuf(struct mtd_softc *, int, struct mbuf *);
77
78static void mtd_reset(struct mtd_softc *sc);
79static int mtd_ioctl(struct ifnet *, u_long, caddr_t);
80static void mtd_init(struct ifnet *);
81static void mtd_start(struct ifnet *);
82static void mtd_stop(struct ifnet *);
83static void mtd_watchdog(struct ifnet *);
84
85static void mtd_rxeof(struct mtd_softc *);
86static int mtd_rx_resync(struct mtd_softc *);
87static void mtd_txeof(struct mtd_softc *);
88
89
90void
91mtd_attach(struct mtd_softc *sc)
92{
93	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
94	u_int32_t enaddr[2];
95	int i;
96
97	/* Reset the adapter. */
98	mtd_reset(sc);
99
100	if (bus_dmamem_alloc(sc->sc_dmat, sizeof(struct mtd_list_data),
101	    PAGE_SIZE, 0, sc->sc_listseg, 1, &sc->sc_listnseg,
102	    BUS_DMA_NOWAIT) != 0) {
103		printf(": can't alloc list mem\n");
104		return;
105	}
106	if (bus_dmamem_map(sc->sc_dmat, sc->sc_listseg, sc->sc_listnseg,
107	    sizeof(struct mtd_list_data), &sc->sc_listkva,
108	    BUS_DMA_NOWAIT) != 0) {
109		printf(": can't map list mem\n");
110		return;
111	}
112	if (bus_dmamap_create(sc->sc_dmat, sizeof(struct mtd_list_data), 1,
113	    sizeof(struct mtd_list_data), 0, BUS_DMA_NOWAIT,
114	    &sc->sc_listmap) != 0) {
115		printf(": can't alloc list map\n");
116		return;
117	}
118	if (bus_dmamap_load(sc->sc_dmat, sc->sc_listmap, sc->sc_listkva,
119	    sizeof(struct mtd_list_data), NULL, BUS_DMA_NOWAIT) != 0) {
120		printf(": can't load list map\n");
121		return;
122	}
123	sc->mtd_ldata = (struct mtd_list_data *)sc->sc_listkva;
124	bzero(sc->mtd_ldata, sizeof(struct mtd_list_data));
125
126	for (i = 0; i < MTD_RX_LIST_CNT; i++) {
127		if (bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
128		    0, BUS_DMA_NOWAIT,
129		    &sc->mtd_cdata.mtd_rx_chain[i].sd_map) != 0) {
130			printf(": can't create rx map\n");
131			return;
132		}
133	}
134	if (bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
135	    BUS_DMA_NOWAIT, &sc->sc_rx_sparemap) != 0) {
136		printf(": can't create rx spare map\n");
137		return;
138	}
139
140	for (i = 0; i < MTD_TX_LIST_CNT; i++) {
141		if (bus_dmamap_create(sc->sc_dmat, MCLBYTES,
142		    MTD_TX_LIST_CNT - 5, MCLBYTES, 0, BUS_DMA_NOWAIT,
143		    &sc->mtd_cdata.mtd_tx_chain[i].sd_map) != 0) {
144			printf(": can't create tx map\n");
145			return;
146		}
147	}
148	if (bus_dmamap_create(sc->sc_dmat, MCLBYTES, MTD_TX_LIST_CNT - 5,
149	    MCLBYTES, 0, BUS_DMA_NOWAIT, &sc->sc_tx_sparemap) != 0) {
150		printf(": can't create tx spare map\n");
151		return;
152	}
153
154
155	/* Get station address. */
156	enaddr[0] = letoh32(CSR_READ_4(MTD_PAR0));
157	enaddr[1] = letoh32(CSR_READ_4(MTD_PAR4));
158	bcopy(enaddr, sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN);
159	printf(" address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
160
161	/* Initialize interface */
162	ifp->if_softc = sc;
163	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
164	ifp->if_ioctl = mtd_ioctl;
165	ifp->if_start = mtd_start;
166	ifp->if_watchdog = mtd_watchdog;
167	ifp->if_baudrate = 10000000;
168	IFQ_SET_READY(&ifp->if_snd);
169	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
170
171	/*
172	 * Initialize our media structures and probe the MII.
173	 */
174	sc->sc_mii.mii_ifp = ifp;
175	sc->sc_mii.mii_readreg = mtd_miibus_readreg;
176	sc->sc_mii.mii_writereg = mtd_miibus_writereg;
177	sc->sc_mii.mii_statchg = mtd_miibus_statchg;
178	ifmedia_init(&sc->sc_mii.mii_media, 0, mtd_ifmedia_upd,
179	    mtd_ifmedia_sts);
180	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
181	    MII_OFFSET_ANY, 0);
182	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
183		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_NONE, 0,
184		    NULL);
185		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_NONE);
186	} else
187		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
188
189	/*
190	 * Attach us everywhere
191	 */
192	if_attach(ifp);
193	ether_ifattach(ifp);
194}
195
196
197static int
198mtd_ifmedia_upd(struct ifnet *ifp)
199{
200	struct mtd_softc *sc = ifp->if_softc;
201
202	return (mii_mediachg(&sc->sc_mii));
203}
204
205
206static void
207mtd_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
208{
209	struct mtd_softc *sc = ifp->if_softc;
210
211	mii_pollstat(&sc->sc_mii);
212	ifmr->ifm_active = sc->sc_mii.mii_media_active;
213	ifmr->ifm_status = sc->sc_mii.mii_media_status;
214}
215
216
217static u_int32_t
218mtd_mii_command(struct mtd_softc *sc, int opcode, int phy, int reg)
219{
220	u_int32_t miir, mask, data;
221	int i;
222
223	miir = (CSR_READ_4(MTD_MIIMGT) & ~MIIMGT_MASK) | MIIMGT_WRITE |
224	    MIIMGT_MDO;
225
226	for (i = 0; i < 32; i++) {
227		miir &= ~MIIMGT_MDC;
228		CSR_WRITE_4(MTD_MIIMGT, miir);
229		miir |= MIIMGT_MDC;
230		CSR_WRITE_4(MTD_MIIMGT, miir);
231	}
232
233	data = opcode | (phy << 7) | (reg << 2);
234
235	for (mask = 0; mask; mask >>= 1) {
236		miir &= ~(MIIMGT_MDC | MIIMGT_MDO);
237		if (mask & data)
238			miir |= MIIMGT_MDO;
239		CSR_WRITE_4(MTD_MIIMGT, miir);
240		miir |= MIIMGT_MDC;
241		CSR_WRITE_4(MTD_MIIMGT, miir);
242		DELAY(30);
243
244		if (mask == 0x4 && opcode == MII_OPCODE_RD)
245			miir &= ~MIIMGT_WRITE;
246	}
247	return (miir);
248}
249
250
251
252static int
253mtd_miibus_readreg(struct device *self, int phy, int reg)
254{
255	struct mtd_softc *sc = (void *)self;
256
257	if (sc->sc_devid == PCI_PRODUCT_MYSON_MTD803)
258		return (phy ? 0 : (int)CSR_READ_2(MTD_PHYCSR + (reg << 1)));
259	else {
260		u_int32_t miir, mask, data;
261
262		miir = mtd_mii_command(sc, MII_OPCODE_RD, phy, reg);
263		for (mask = 0x8000, data = 0; mask; mask >>= 1) {
264			miir &= ~MIIMGT_MDC;
265			CSR_WRITE_4(MTD_MIIMGT, miir);
266			miir = CSR_READ_4(MTD_MIIMGT);
267			if (miir & MIIMGT_MDI)
268				data |= mask;
269			miir |= MIIMGT_MDC;
270			CSR_WRITE_4(MTD_MIIMGT, miir);
271			DELAY(30);
272		}
273		miir &= ~MIIMGT_MDC;
274		CSR_WRITE_4(MTD_MIIMGT, miir);
275
276		return ((int)data);
277	}
278}
279
280
281static void
282mtd_miibus_writereg(struct device *self, int phy, int reg, int val)
283{
284	struct mtd_softc *sc = (void *)self;
285
286	if (sc->sc_devid == PCI_PRODUCT_MYSON_MTD803) {
287		if (!phy)
288			CSR_WRITE_2(MTD_PHYCSR + (reg << 1), val);
289	} else {
290		u_int32_t miir, mask;
291
292		miir = mtd_mii_command(sc, MII_OPCODE_WR, phy, reg);
293		for (mask = 0x8000; mask; mask >>= 1) {
294			miir &= ~(MIIMGT_MDC | MIIMGT_MDO);
295			if (mask & (u_int32_t)val)
296				miir |= MIIMGT_MDO;;
297			CSR_WRITE_4(MTD_MIIMGT, miir);
298			miir |= MIIMGT_MDC;
299			CSR_WRITE_4(MTD_MIIMGT, miir);
300			DELAY(1);
301		}
302		miir &= ~MIIMGT_MDC;
303		CSR_WRITE_4(MTD_MIIMGT, miir);
304	}
305}
306
307
308static void
309mtd_miibus_statchg(struct device *self)
310{
311	/* NOTHING */
312}
313
314
315void
316mtd_setmulti(struct mtd_softc *sc)
317{
318	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
319	u_int32_t rxfilt, crc, hash[2] = { 0, 0 };
320	struct ether_multistep step;
321	struct ether_multi *enm;
322	int mcnt = 0;
323
324allmulti:
325	rxfilt = CSR_READ_4(MTD_TCRRCR) & ~RCR_AM;
326	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
327		rxfilt |= RCR_AM;
328		CSR_WRITE_4(MTD_TCRRCR, rxfilt);
329		CSR_WRITE_4(MTD_MAR0, 0xffffffff);
330		CSR_WRITE_4(MTD_MAR4, 0xffffffff);
331		return;
332	}
333
334	/* First, zot all the existing hash bits. */
335	CSR_WRITE_4(MTD_MAR0, 0);
336	CSR_WRITE_4(MTD_MAR4, 0);
337
338	/* Now program new ones. */
339	ETHER_FIRST_MULTI(step, &sc->sc_arpcom, enm);
340	while (enm != NULL) {
341		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
342			ifp->if_flags |= IFF_ALLMULTI;
343			goto allmulti;
344		}
345		crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
346		hash[crc >> 5] |= 1 << (crc & 0xf);
347		++mcnt;
348		ETHER_NEXT_MULTI(step, enm);
349	}
350
351	if (mcnt)
352		rxfilt |= RCR_AM;
353	CSR_WRITE_4(MTD_MAR0, hash[0]);
354	CSR_WRITE_4(MTD_MAR4, hash[1]);
355	CSR_WRITE_4(MTD_TCRRCR, rxfilt);
356}
357
358
359/*
360 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
361 * pointers to the fragment pointers.
362 */
363int
364mtd_encap(struct mtd_softc *sc, struct mbuf *m_head, u_int32_t *txidx)
365{
366	struct mtd_tx_desc *f = NULL;
367	int frag, cur, cnt = 0, i, total_len = 0;
368	bus_dmamap_t map;
369
370	/*
371 	 * Start packing the mbufs in this chain into
372	 * the fragment pointers. Stop when we run out
373 	 * of fragments or hit the end of the mbuf chain.
374	 */
375	map = sc->sc_tx_sparemap;
376
377	if (bus_dmamap_load_mbuf(sc->sc_dmat, map,
378	    m_head, BUS_DMA_NOWAIT) != 0)
379		return (1);
380
381	cur = frag = *txidx;
382
383	for (i = 0; i < map->dm_nsegs; i++) {
384		if ((MTD_TX_LIST_CNT -
385		    (sc->mtd_cdata.mtd_tx_cnt + cnt)) < 5) {
386			bus_dmamap_unload(sc->sc_dmat, map);
387			return (1);
388		}
389
390		f = &sc->mtd_ldata->mtd_tx_list[frag];
391		f->td_tcw = htole32(map->dm_segs[i].ds_len);
392		total_len += map->dm_segs[i].ds_len;
393		if (cnt == 0) {
394			f->td_tsw = 0;
395			f->td_tcw |= htole32(TCW_FD | TCW_CRC | TCW_PAD);
396		} else
397			f->td_tsw = htole32(TSW_OWN);
398		f->td_buf = htole32(map->dm_segs[i].ds_addr);
399		cur = frag;
400		frag = (frag + 1) % MTD_TX_LIST_CNT;
401		cnt++;
402	}
403
404	sc->mtd_cdata.mtd_tx_cnt += cnt;
405	sc->mtd_cdata.mtd_tx_chain[cur].sd_mbuf = m_head;
406	sc->sc_tx_sparemap = sc->mtd_cdata.mtd_tx_chain[cur].sd_map;
407	sc->mtd_cdata.mtd_tx_chain[cur].sd_map = map;
408	sc->mtd_ldata->mtd_tx_list[cur].td_tcw |= htole32(TCW_LD | TCW_IC);
409	if (sc->sc_devid == PCI_PRODUCT_MYSON_MTD891)
410		sc->mtd_ldata->mtd_tx_list[cur].td_tcw |=
411		    htole32(TCW_EIC | TCW_RTLC);
412
413	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
414	    BUS_DMASYNC_PREWRITE);
415
416	sc->mtd_ldata->mtd_tx_list[*txidx].td_tsw = htole32(TSW_OWN);
417	sc->mtd_ldata->mtd_tx_list[*txidx].td_tcw |=
418	    htole32(total_len << TCW_PKTS_SHIFT);
419
420	bus_dmamap_sync(sc->sc_dmat, sc->sc_listmap,
421	    offsetof(struct mtd_list_data, mtd_tx_list[0]),
422	    sizeof(struct mtd_tx_desc) * MTD_TX_LIST_CNT,
423	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
424
425	*txidx = frag;
426
427	return (0);
428}
429
430
431/*
432 * Initialize the transmit descriptors.
433 */
434static void
435mtd_list_tx_init(struct mtd_softc *sc)
436{
437	struct mtd_chain_data *cd;
438	struct mtd_list_data *ld;
439	int i;
440
441	cd = &sc->mtd_cdata;
442	ld = sc->mtd_ldata;
443	for (i = 0; i < MTD_TX_LIST_CNT; i++) {
444		cd->mtd_tx_chain[i].sd_mbuf = NULL;
445		ld->mtd_tx_list[i].td_tsw = 0;
446		ld->mtd_tx_list[i].td_tcw = 0;
447		ld->mtd_tx_list[i].td_buf = 0;
448		ld->mtd_tx_list[i].td_next = htole32(
449		    sc->sc_listmap->dm_segs[0].ds_addr +
450		    offsetof(struct mtd_list_data,
451		    mtd_tx_list[(i + 1) % MTD_TX_LIST_CNT]));
452	}
453
454	cd->mtd_tx_prod = cd->mtd_tx_cons = cd->mtd_tx_cnt = 0;
455}
456
457
458/*
459 * Initialize the RX descriptors and allocate mbufs for them. Note that
460 * we arrange the descriptors in a closed ring, so that the last descriptor
461 * points back to the first.
462 */
463static int
464mtd_list_rx_init(struct mtd_softc *sc)
465{
466	struct mtd_list_data *ld;
467	int i;
468
469	ld = sc->mtd_ldata;
470
471	for (i = 0; i < MTD_RX_LIST_CNT; i++) {
472		if (mtd_newbuf(sc, i, NULL))
473			return (1);
474		ld->mtd_rx_list[i].rd_next = htole32(
475		    sc->sc_listmap->dm_segs[0].ds_addr +
476		    offsetof(struct mtd_list_data,
477		    mtd_rx_list[(i + 1) % MTD_RX_LIST_CNT])
478		);
479	}
480
481	sc->mtd_cdata.mtd_rx_prod = 0;
482
483	return (0);
484}
485
486
487/*
488 * Initialize an RX descriptor and attach an MBUF cluster.
489 */
490static int
491mtd_newbuf(struct mtd_softc *sc, int i, struct mbuf *m)
492{
493	struct mbuf *m_new = NULL;
494	struct mtd_rx_desc *c;
495	bus_dmamap_t map;
496
497	c = &sc->mtd_ldata->mtd_rx_list[i];
498
499	if (m == NULL) {
500		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
501		if (m_new == NULL) {
502			printf("%s: no memory for rx list "
503			    "-- packet dropped!\n", sc->sc_dev.dv_xname);
504			return (1);
505		}
506
507		MCLGET(m_new, M_DONTWAIT);
508		if (!(m_new->m_flags & M_EXT)) {
509			printf("%s: no memory for rx list "
510			    "-- packet dropped!\n", sc->sc_dev.dv_xname);
511			m_freem(m_new);
512			return (1);
513		}
514		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
515		if (bus_dmamap_load(sc->sc_dmat, sc->sc_rx_sparemap,
516		    mtod(m_new, caddr_t), MCLBYTES, NULL,
517		    BUS_DMA_NOWAIT) != 0) {
518			printf("%s: rx load failed\n", sc->sc_dev.dv_xname);
519			m_freem(m_new);
520			return (1);
521		}
522		map = sc->mtd_cdata.mtd_rx_chain[i].sd_map;
523		sc->mtd_cdata.mtd_rx_chain[i].sd_map = sc->sc_rx_sparemap;
524		sc->sc_rx_sparemap = map;
525	} else {
526		m_new = m;
527		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
528		m_new->m_data = m_new->m_ext.ext_buf;
529	}
530
531	m_adj(m_new, sizeof(u_int64_t));
532
533	bus_dmamap_sync(sc->sc_dmat, sc->mtd_cdata.mtd_rx_chain[i].sd_map, 0,
534	    sc->mtd_cdata.mtd_rx_chain[i].sd_map->dm_mapsize,
535	    BUS_DMASYNC_PREREAD);
536
537	sc->mtd_cdata.mtd_rx_chain[i].sd_mbuf = m_new;
538	c->rd_buf = htole32(
539	    sc->mtd_cdata.mtd_rx_chain[i].sd_map->dm_segs[0].ds_addr +
540	    sizeof(u_int64_t));
541	c->rd_rcw = htole32(MTD_RXLEN);
542	c->rd_rsr = htole32(RSR_OWN);
543
544	bus_dmamap_sync(sc->sc_dmat, sc->sc_listmap,
545	    offsetof(struct mtd_list_data, mtd_rx_list[i]),
546	    sizeof(struct mtd_rx_desc),
547	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
548
549	return (0);
550}
551
552
553static void
554mtd_reset(struct mtd_softc *sc)
555{
556	int i;
557
558	/* Set software reset bit */
559	CSR_WRITE_4(MTD_BCR, BCR_SWR);
560
561	/*
562	 * Wait until software reset completed.
563	 */
564	for (i = 0; i < MTD_TIMEOUT; ++i) {
565		DELAY(10);
566		if (!(CSR_READ_4(MTD_BCR) & BCR_SWR)) {
567			/*
568			 * Wait a little while for the chip to get
569			 * its brains in order.
570			 */
571			DELAY(1000);
572			return;
573		}
574	}
575
576	/* Reset timed out. */
577	printf("%s: reset never completed!\n", sc->sc_dev.dv_xname);
578}
579
580
581static int
582mtd_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
583{
584	struct mtd_softc *sc = ifp->if_softc;
585	struct ifreq *ifr = (struct ifreq *)data;
586	struct ifaddr *ifa = (struct ifaddr *)data;
587	int s, error;
588
589	s = splimp();
590	if ((error = ether_ioctl(ifp, &sc->sc_arpcom, command, data)) > 0) {
591		splx(s);
592		return (error);
593	}
594
595	switch (command) {
596	case SIOCSIFADDR:
597		ifp->if_flags |= IFF_UP;
598		mtd_init(ifp);
599		switch (ifa->ifa_addr->sa_family) {
600#ifdef INET
601		case AF_INET:
602			arp_ifinit(&sc->sc_arpcom, ifa);
603			break;
604#endif /* INET */
605		}
606		break;
607	case SIOCSIFMTU:
608		if (ifr->ifr_mtu >= ETHERMIN && ifr->ifr_mtu <= ETHERMTU)
609			ifp->if_mtu = ifr->ifr_mtu;
610		else
611			error = EINVAL;
612		break;
613
614	case SIOCSIFFLAGS:
615		if (ifp->if_flags & IFF_UP)
616			mtd_init(ifp);
617		else {
618			if (ifp->if_flags & IFF_RUNNING)
619				mtd_stop(ifp);
620		}
621		error = 0;
622		break;
623	case SIOCADDMULTI:
624	case SIOCDELMULTI:
625		error = (command == SIOCADDMULTI) ?
626		    ether_addmulti(ifr, &sc->sc_arpcom) :
627		    ether_delmulti(ifr, &sc->sc_arpcom);
628
629		if (error == ENETRESET) {
630			/*
631			 * Multicast list has changed; set the hardware
632			 * filter accordingly.
633			 */
634			mtd_setmulti(sc);
635			error = 0;
636		}
637		break;
638	case SIOCGIFMEDIA:
639	case SIOCSIFMEDIA:
640		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, command);
641		break;
642	default:
643		error = EINVAL;
644		break;
645	}
646
647	splx(s);
648	return (error);
649}
650
651
652static void
653mtd_init(struct ifnet *ifp)
654{
655	struct mtd_softc *sc = ifp->if_softc;
656	int s;
657
658	s = splimp();
659
660	/*
661	 * Cancel pending I/O and free all RX/TX buffers.
662	 */
663	mtd_stop(ifp);
664
665	/*
666	 * Set cache alignment and burst length.
667	 */
668	CSR_WRITE_4(MTD_BCR, BCR_PBL8);
669	CSR_WRITE_4(MTD_TCRRCR, TCR_TFTSF | RCR_RBLEN | RCR_RPBL512);
670	if (sc->sc_devid == PCI_PRODUCT_MYSON_MTD891) {
671		CSR_SETBIT(MTD_BCR, BCR_PROG);
672		CSR_SETBIT(MTD_TCRRCR, TCR_ENHANCED);
673	}
674
675	if (ifp->if_flags & IFF_PROMISC)
676		CSR_SETBIT(MTD_TCRRCR, RCR_PROM);
677	else
678		CSR_CLRBIT(MTD_TCRRCR, RCR_PROM);
679
680	if (ifp->if_flags & IFF_BROADCAST)
681		CSR_SETBIT(MTD_TCRRCR, RCR_AB);
682	else
683		CSR_CLRBIT(MTD_TCRRCR, RCR_AB);
684
685	mtd_setmulti(sc);
686
687	if (mtd_list_rx_init(sc)) {
688		printf("%s: can't allocate memeory for rx buffers\n",
689		    sc->sc_dev.dv_xname);
690		splx(s);
691		return;
692	}
693	mtd_list_tx_init(sc);
694
695	CSR_WRITE_4(MTD_RXLBA, sc->sc_listmap->dm_segs[0].ds_addr +
696	    offsetof(struct mtd_list_data, mtd_rx_list[0]));
697	CSR_WRITE_4(MTD_TXLBA, sc->sc_listmap->dm_segs[0].ds_addr +
698	    offsetof(struct mtd_list_data, mtd_tx_list[0]));
699
700	/*
701	 * Enable interrupts.
702	 */
703	CSR_WRITE_4(MTD_IMR, IMR_INTRS);
704	CSR_WRITE_4(MTD_ISR, 0xffffffff);
705
706	/* Enable receiver and transmitter */
707	CSR_SETBIT(MTD_TCRRCR, TCR_TE | RCR_RE);
708	CSR_WRITE_4(MTD_RXPDR, 0xffffffff);
709
710	ifp->if_flags |= IFF_RUNNING;
711	ifp->if_flags &= ~IFF_OACTIVE;
712	splx(s);
713}
714
715
716/*
717 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
718 * to the mbuf data regions directly in the transmit lists. We also save a
719 * copy of the pointers since the transmit list fragment pointers are
720 * physical addresses.
721 */
722static void
723mtd_start(struct ifnet *ifp)
724{
725	struct mtd_softc *sc = ifp->if_softc;
726	struct mbuf *m_head = NULL;
727	int idx;
728
729	if (sc->mtd_cdata.mtd_tx_cnt) {
730		ifp->if_flags |= IFF_OACTIVE;
731		return;
732	}
733
734	idx = sc->mtd_cdata.mtd_tx_prod;
735	while (sc->mtd_cdata.mtd_tx_chain[idx].sd_mbuf == NULL) {
736		IFQ_DEQUEUE(&ifp->if_snd, m_head);
737		if (m_head == NULL)
738			break;
739
740		if (mtd_encap(sc, m_head, &idx)) {
741			ifp->if_flags |= IFF_OACTIVE;
742			break;
743		}
744
745		/*
746		 * If there's a BPF listener, bounce a copy of this frame
747		 * to him.
748		 */
749#if NBPFILTER > 0
750		if (ifp->if_bpf != NULL)
751			bpf_mtap(ifp->if_bpf, m_head);
752#endif
753	}
754
755	if (idx == sc->mtd_cdata.mtd_tx_prod)
756		return;
757
758	/* Transmit */
759	sc->mtd_cdata.mtd_tx_prod = idx;
760	CSR_WRITE_4(MTD_TXPDR, 0xffffffff);
761
762	/*
763	 * Set a timeout in case the chip goes out to lunch.
764	 */
765	ifp->if_timer = 5;
766}
767
768
769static void
770mtd_stop(struct ifnet *ifp)
771{
772	struct mtd_softc *sc = ifp->if_softc;
773	int i;
774
775	ifp->if_timer = 0;
776	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
777
778	CSR_CLRBIT(MTD_TCRRCR, (RCR_RE | TCR_TE));
779	CSR_WRITE_4(MTD_IMR, 0);
780	CSR_WRITE_4(MTD_TXLBA, 0);
781	CSR_WRITE_4(MTD_RXLBA, 0);
782
783	/*
784	 * Free data in the RX lists.
785	 */
786	for (i = 0; i < MTD_RX_LIST_CNT; i++) {
787		if (sc->mtd_cdata.mtd_rx_chain[i].sd_map->dm_nsegs != 0) {
788			bus_dmamap_t map = sc->mtd_cdata.mtd_rx_chain[i].sd_map;
789
790			bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
791			    BUS_DMASYNC_POSTREAD);
792			bus_dmamap_unload(sc->sc_dmat, map);
793		}
794		if (sc->mtd_cdata.mtd_rx_chain[i].sd_mbuf != NULL) {
795			m_freem(sc->mtd_cdata.mtd_rx_chain[i].sd_mbuf);
796			sc->mtd_cdata.mtd_rx_chain[i].sd_mbuf = NULL;
797		}
798	}
799	bzero((char *)&sc->mtd_ldata->mtd_rx_list,
800		sizeof(sc->mtd_ldata->mtd_rx_list));
801
802	/*
803	 * Free the TX list buffers.
804	 */
805	for (i = 0; i < MTD_TX_LIST_CNT; i++) {
806		if (sc->mtd_cdata.mtd_tx_chain[i].sd_map->dm_nsegs != 0) {
807			bus_dmamap_t map = sc->mtd_cdata.mtd_tx_chain[i].sd_map;
808
809			bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
810			    BUS_DMASYNC_POSTWRITE);
811			bus_dmamap_unload(sc->sc_dmat, map);
812		}
813		if (sc->mtd_cdata.mtd_tx_chain[i].sd_mbuf != NULL) {
814			m_freem(sc->mtd_cdata.mtd_tx_chain[i].sd_mbuf);
815			sc->mtd_cdata.mtd_tx_chain[i].sd_mbuf = NULL;
816		}
817	}
818
819	bzero((char *)&sc->mtd_ldata->mtd_tx_list,
820		sizeof(sc->mtd_ldata->mtd_tx_list));
821
822}
823
824
825static void
826mtd_watchdog(struct ifnet *ifp)
827{
828	struct mtd_softc *sc = ifp->if_softc;
829
830	ifp->if_oerrors++;
831	printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
832
833	mtd_stop(ifp);
834	mtd_reset(sc);
835	mtd_init(ifp);
836
837	if (!IFQ_IS_EMPTY(&ifp->if_snd))
838		mtd_start(ifp);
839}
840
841
842int
843mtd_intr(void *xsc)
844{
845	struct mtd_softc *sc = xsc;
846	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
847	u_int32_t status;
848	int claimed = 0;
849
850	/* Supress unwanted interrupts */
851	if (!(ifp->if_flags & IFF_RUNNING)) {
852		if (CSR_READ_4(MTD_ISR) & ISR_INTRS)
853			mtd_stop(ifp);
854		return (claimed);
855	}
856
857	/* Disable interrupts. */
858	CSR_WRITE_4(MTD_IMR, 0);
859
860	while((status = CSR_READ_4(MTD_ISR)) & ISR_INTRS) {
861		claimed = 1;
862
863		CSR_WRITE_4(MTD_ISR, status);
864
865		/* RX interrupt. */
866		if (status & ISR_RI) {
867			int curpkts = ifp->if_ipackets;
868
869			mtd_rxeof(sc);
870			if (curpkts == ifp->if_ipackets)
871				while(mtd_rx_resync(sc))
872					mtd_rxeof(sc);
873		}
874
875		/* RX error interrupt. */
876		if (status & (ISR_RXERI | ISR_RBU))
877			ifp->if_ierrors++;
878
879		/* TX interrupt. */
880		if (status & (ISR_TI | ISR_ETI | ISR_TBU))
881			mtd_txeof(sc);
882
883		/* Fatal bus error interrupt. */
884		if (status & ISR_FBE) {
885			mtd_reset(sc);
886			mtd_start(ifp);
887		}
888	}
889
890	/* Re-enable interrupts. */
891	CSR_WRITE_4(MTD_IMR, IMR_INTRS);
892
893	if (!IFQ_IS_EMPTY(&ifp->if_snd))
894		mtd_start(ifp);
895
896	return (claimed);
897}
898
899
900/*
901 * A frame has been uploaded: pass the resulting mbuf chain up to
902 * the higher level protocols.
903 */
904static void
905mtd_rxeof(struct mtd_softc *sc)
906{
907	struct mbuf *m;
908	struct ifnet *ifp;
909	struct mtd_rx_desc *cur_rx;
910	int i, total_len = 0;
911	u_int32_t rxstat;
912
913	ifp = &sc->sc_arpcom.ac_if;
914	i = sc->mtd_cdata.mtd_rx_prod;
915
916	while(!(sc->mtd_ldata->mtd_rx_list[i].rd_rsr & htole32(RSR_OWN))) {
917		struct mbuf *m0 = NULL;
918
919		bus_dmamap_sync(sc->sc_dmat, sc->sc_listmap,
920		    offsetof(struct mtd_list_data, mtd_rx_list[i]),
921		    sizeof(struct mtd_rx_desc),
922		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
923
924		cur_rx = &sc->mtd_ldata->mtd_rx_list[i];
925		rxstat = letoh32(cur_rx->rd_rsr);
926		m = sc->mtd_cdata.mtd_rx_chain[i].sd_mbuf;
927		total_len = RSR_FLNG_GET(rxstat);
928
929		sc->mtd_cdata.mtd_rx_chain[i].sd_mbuf = NULL;
930
931		/*
932		 * If an error occurs, update stats, clear the
933		 * status word and leave the mbuf cluster in place:
934		 * it should simply get re-used next time this descriptor
935	 	 * comes up in the ring.
936		 */
937		if (rxstat & RSR_RXER) {
938			ifp->if_ierrors++;
939			mtd_newbuf(sc, i, m);
940			if (rxstat & RSR_CRC) {
941				i = (i + 1) % MTD_RX_LIST_CNT;
942				continue;
943			} else {
944				mtd_init(ifp);
945				return;
946			}
947		}
948
949		/* No errors; receive the packet. */
950		total_len -= ETHER_CRC_LEN;
951
952		bus_dmamap_sync(sc->sc_dmat, sc->mtd_cdata.mtd_rx_chain[i].sd_map,
953		    0, sc->mtd_cdata.mtd_rx_chain[i].sd_map->dm_mapsize,
954		    BUS_DMASYNC_POSTREAD);
955
956		m0 = m_devget(mtod(m, char *) - ETHER_ALIGN, total_len + ETHER_ALIGN,
957		    0, ifp, NULL);
958		mtd_newbuf(sc, i, m);
959		i = (i + 1) % MTD_RX_LIST_CNT;
960		if (m0 == NULL) {
961			ifp->if_ierrors++;
962			continue;
963		}
964		m_adj(m0, ETHER_ALIGN);
965		m = m0;
966
967		ifp->if_ipackets++;
968
969#if NBPFILTER > 0
970		if (ifp->if_bpf)
971			bpf_mtap(ifp->if_bpf, m);
972#endif
973		ether_input_mbuf(ifp, m);
974	}
975
976	sc->mtd_cdata.mtd_rx_prod = i;
977}
978
979
980/*
981 * This routine searches the RX ring for dirty descriptors in the
982 * event that the rxeof routine falls out of sync with the chip's
983 * current descriptor pointer. This may happen sometimes as a result
984 * of a "no RX buffer available" condition that happens when the chip
985 * consumes all of the RX buffers before the driver has a chance to
986 * process the RX ring. This routine may need to be called more than
987 * once to bring the driver back in sync with the chip, however we
988 * should still be getting RX DONE interrupts to drive the search
989 * for new packets in the RX ring, so we should catch up eventually.
990 */
991static int
992mtd_rx_resync(sc)
993	struct mtd_softc *sc;
994{
995	int i, pos;
996	struct mtd_rx_desc *cur_rx;
997
998	pos = sc->mtd_cdata.mtd_rx_prod;
999
1000	for (i = 0; i < MTD_RX_LIST_CNT; i++) {
1001		bus_dmamap_sync(sc->sc_dmat, sc->sc_listmap,
1002		    offsetof(struct mtd_list_data, mtd_rx_list[pos]),
1003		    sizeof(struct mtd_rx_desc),
1004		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1005
1006		cur_rx = &sc->mtd_ldata->mtd_rx_list[pos];
1007		if (!(cur_rx->rd_rsr & htole32(RSR_OWN)))
1008			break;
1009		pos = (pos + 1) % MTD_RX_LIST_CNT;
1010	}
1011
1012	/* If the ring really is empty, then just return. */
1013	if (i == MTD_RX_LIST_CNT)
1014		return (0);
1015
1016	/* We've fallen behind the chip: catch it. */
1017	sc->mtd_cdata.mtd_rx_prod = pos;
1018
1019	return (EAGAIN);
1020}
1021
1022
1023/*
1024 * A frame was downloaded to the chip. It's safe for us to clean up
1025 * the list buffers.
1026 */
1027static void
1028mtd_txeof(struct mtd_softc *sc)
1029{
1030	struct mtd_tx_desc *cur_tx = NULL;
1031	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1032	int idx;
1033
1034	/* Clear the timeout timer. */
1035	ifp->if_timer = 0;
1036
1037	/*
1038	 * Go through our tx list and free mbufs for those
1039	 * frames that have been transmitted.
1040	 */
1041	idx = sc->mtd_cdata.mtd_tx_cons;
1042	while(idx != sc->mtd_cdata.mtd_tx_prod) {
1043		u_int32_t txstat;
1044
1045		bus_dmamap_sync(sc->sc_dmat, sc->sc_listmap,
1046		    offsetof(struct mtd_list_data, mtd_tx_list[idx]),
1047		    sizeof(struct mtd_tx_desc),
1048		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1049
1050		cur_tx = &sc->mtd_ldata->mtd_tx_list[idx];
1051		txstat = letoh32(cur_tx->td_tsw);
1052
1053		if (txstat & TSW_OWN || txstat == TSW_UNSENT)
1054			break;
1055
1056		if (!(cur_tx->td_tcw & htole32(TCW_LD))) {
1057			sc->mtd_cdata.mtd_tx_cnt--;
1058			idx = (idx + 1) % MTD_TX_LIST_CNT;
1059			continue;
1060		}
1061
1062		if (CSR_READ_4(MTD_TCRRCR) & TCR_ENHANCED)
1063			ifp->if_collisions += TSR_NCR_GET(CSR_READ_4(MTD_TSR));
1064		else {
1065			if (txstat & TSW_TXERR) {
1066				ifp->if_oerrors++;
1067				if (txstat & TSW_EC)
1068					ifp->if_collisions++;
1069				if (txstat & TSW_LC)
1070					ifp->if_collisions++;
1071			}
1072			ifp->if_collisions += TSW_NCR_GET(txstat);
1073		}
1074
1075		ifp->if_opackets++;
1076		if (sc->mtd_cdata.mtd_tx_chain[idx].sd_map->dm_nsegs != 0) {
1077			bus_dmamap_t map =
1078			    sc->mtd_cdata.mtd_tx_chain[idx].sd_map;
1079			bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1080			    BUS_DMASYNC_POSTWRITE);
1081			bus_dmamap_unload(sc->sc_dmat, map);
1082		}
1083		if (sc->mtd_cdata.mtd_tx_chain[idx].sd_mbuf != NULL) {
1084			m_freem(sc->mtd_cdata.mtd_tx_chain[idx].sd_mbuf);
1085			sc->mtd_cdata.mtd_tx_chain[idx].sd_mbuf = NULL;
1086		}
1087		sc->mtd_cdata.mtd_tx_cnt--;
1088		idx = (idx + 1) % MTD_TX_LIST_CNT;
1089	}
1090
1091	if (cur_tx != NULL) {
1092		ifp->if_flags &= ~IFF_OACTIVE;
1093		sc->mtd_cdata.mtd_tx_cons = idx;
1094	} else
1095		if (sc->mtd_ldata->mtd_tx_list[idx].td_tsw ==
1096		    htole32(TSW_UNSENT)) {
1097			sc->mtd_ldata->mtd_tx_list[idx].td_tsw =
1098			    htole32(TSW_OWN);
1099			ifp->if_timer = 5;
1100			CSR_WRITE_4(MTD_TXPDR, 0xffffffff);
1101		}
1102}
1103
1104struct cfdriver mtd_cd = {
1105	0, "mtd", DV_IFNET
1106};
1107