if_bm.c revision 179645
1179645Smarcel/*-
2179645Smarcel * Copyright 2008 Nathan Whitehorn. All rights reserved.
3179645Smarcel * Copyright 2003 by Peter Grehan. All rights reserved.
4179645Smarcel * Copyright (C) 1998, 1999, 2000 Tsubai Masanari.  All rights reserved.
5179645Smarcel *
6179645Smarcel * Redistribution and use in source and binary forms, with or without
7179645Smarcel * modification, are permitted provided that the following conditions
8179645Smarcel * are met:
9179645Smarcel * 1. Redistributions of source code must retain the above copyright
10179645Smarcel *    notice, this list of conditions and the following disclaimer.
11179645Smarcel * 2. Redistributions in binary form must reproduce the above copyright
12179645Smarcel *    notice, this list of conditions and the following disclaimer in the
13179645Smarcel *    documentation and/or other materials provided with the distribution.
14179645Smarcel * 3. The name of the author may not be used to endorse or promote products
15179645Smarcel *    derived from this software without specific prior written permission.
16179645Smarcel *
17179645Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18179645Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19179645Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20179645Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21179645Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22179645Smarcel * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23179645Smarcel * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24179645Smarcel * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25179645Smarcel * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26179645Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27179645Smarcel * SUCH DAMAGE.
28179645Smarcel *
29179645Smarcel * From:
30179645Smarcel *   NetBSD: if_bm.c,v 1.9.2.1 2000/11/01 15:02:49 tv Exp
31179645Smarcel */
32179645Smarcel
33179645Smarcel/*
34179645Smarcel * BMAC/BMAC+ Macio cell 10/100 ethernet driver
35179645Smarcel * 	The low-cost, low-feature Apple variant of the Sun HME
36179645Smarcel */
37179645Smarcel
38179645Smarcel#include <sys/cdefs.h>
39179645Smarcel__FBSDID("$FreeBSD: head/sys/dev/bm/if_bm.c 179645 2008-06-07 22:58:32Z marcel $");
40179645Smarcel
41179645Smarcel#include <sys/param.h>
42179645Smarcel#include <sys/systm.h>
43179645Smarcel#include <sys/sockio.h>
44179645Smarcel#include <sys/endian.h>
45179645Smarcel#include <sys/mbuf.h>
46179645Smarcel#include <sys/module.h>
47179645Smarcel#include <sys/malloc.h>
48179645Smarcel#include <sys/kernel.h>
49179645Smarcel#include <sys/socket.h>
50179645Smarcel
51179645Smarcel#include <net/bpf.h>
52179645Smarcel#include <net/if.h>
53179645Smarcel#include <net/if_arp.h>
54179645Smarcel#include <net/ethernet.h>
55179645Smarcel#include <net/if_dl.h>
56179645Smarcel#include <net/if_media.h>
57179645Smarcel#include <net/if_types.h>
58179645Smarcel
59179645Smarcel#include <machine/pio.h>
60179645Smarcel#include <machine/bus.h>
61179645Smarcel#include <machine/resource.h>
62179645Smarcel#include <sys/bus.h>
63179645Smarcel#include <sys/rman.h>
64179645Smarcel
65179645Smarcel#include <dev/mii/mii.h>
66179645Smarcel#include <dev/mii/miivar.h>
67179645Smarcel
68179645Smarcel#include <dev/ofw/ofw_bus.h>
69179645Smarcel#include <dev/ofw/openfirm.h>
70179645Smarcel#include <machine/dbdma.h>
71179645Smarcel
72179645SmarcelMODULE_DEPEND(bm, ether, 1, 1, 1);
73179645SmarcelMODULE_DEPEND(bm, miibus, 1, 1, 1);
74179645Smarcel
75179645Smarcel/* "controller miibus0" required.  See GENERIC if you get errors here. */
76179645Smarcel#include "miibus_if.h"
77179645Smarcel
78179645Smarcel#include "if_bmreg.h"
79179645Smarcel#include "if_bmvar.h"
80179645Smarcel
81179645Smarcelstatic int bm_probe		(device_t);
82179645Smarcelstatic int bm_attach		(device_t);
83179645Smarcelstatic int bm_detach		(device_t);
84179645Smarcelstatic void bm_shutdown		(device_t);
85179645Smarcel
86179645Smarcelstatic void bm_start		(struct ifnet *);
87179645Smarcelstatic void bm_start_locked	(struct ifnet *);
88179645Smarcelstatic int bm_encap 		(struct bm_softc *sc, struct mbuf **m_head);
89179645Smarcelstatic int bm_ioctl		(struct ifnet *, u_long, caddr_t);
90179645Smarcelstatic void bm_init		(void *);
91179645Smarcelstatic void bm_init_locked	(struct bm_softc *sc);
92179645Smarcelstatic void bm_chip_setup	(struct bm_softc *sc);
93179645Smarcelstatic void bm_stop		(struct bm_softc *sc);
94179645Smarcelstatic void bm_setladrf		(struct bm_softc *sc);
95179645Smarcelstatic void bm_dummypacket	(struct bm_softc *sc);
96179645Smarcelstatic void bm_txintr		(void *xsc);
97179645Smarcelstatic void bm_rxintr		(void *xsc);
98179645Smarcel
99179645Smarcelstatic int bm_add_rxbuf		(struct bm_softc *sc, int i);
100179645Smarcelstatic int bm_add_rxbuf_dma	(struct bm_softc *sc, int i);
101179645Smarcelstatic void bm_enable_interrupts (struct bm_softc *sc);
102179645Smarcelstatic void bm_disable_interrupts (struct bm_softc *sc);
103179645Smarcelstatic void bm_tick		(void *xsc);
104179645Smarcelstatic int  bm_watchdog		(struct bm_softc *sc);
105179645Smarcel
106179645Smarcelstatic int bm_ifmedia_upd	(struct ifnet *);
107179645Smarcelstatic void bm_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
108179645Smarcel
109179645Smarcelstatic void bm_miicsr_dwrite	(struct bm_softc *, u_int16_t);
110179645Smarcelstatic void bm_mii_writebit	(struct bm_softc *, int);
111179645Smarcelstatic int bm_mii_readbit	(struct bm_softc *);
112179645Smarcelstatic void bm_mii_sync		(struct bm_softc *);
113179645Smarcelstatic void bm_mii_send		(struct bm_softc *, u_int32_t, int);
114179645Smarcelstatic int bm_mii_readreg	(struct bm_softc *, struct bm_mii_frame *);
115179645Smarcelstatic int bm_mii_writereg	(struct bm_softc *, struct bm_mii_frame *);
116179645Smarcelstatic int bm_miibus_readreg	(device_t, int, int);
117179645Smarcelstatic int bm_miibus_writereg	(device_t, int, int, int);
118179645Smarcelstatic void bm_miibus_statchg	(device_t);
119179645Smarcel
120179645Smarcelstatic device_method_t bm_methods[] = {
121179645Smarcel	/* Device interface */
122179645Smarcel	DEVMETHOD(device_probe,		bm_probe),
123179645Smarcel	DEVMETHOD(device_attach,	bm_attach),
124179645Smarcel	DEVMETHOD(device_detach,	bm_detach),
125179645Smarcel	DEVMETHOD(device_shutdown,	bm_shutdown),
126179645Smarcel
127179645Smarcel	/* bus interface, for miibus */
128179645Smarcel	DEVMETHOD(bus_print_child,	bus_generic_print_child),
129179645Smarcel	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
130179645Smarcel
131179645Smarcel	/* MII interface */
132179645Smarcel	DEVMETHOD(miibus_readreg,	bm_miibus_readreg),
133179645Smarcel	DEVMETHOD(miibus_writereg,	bm_miibus_writereg),
134179645Smarcel	DEVMETHOD(miibus_statchg,	bm_miibus_statchg),
135179645Smarcel	{ 0, 0 }
136179645Smarcel};
137179645Smarcel
138179645Smarcelstatic driver_t bm_macio_driver = {
139179645Smarcel	"bm",
140179645Smarcel	bm_methods,
141179645Smarcel	sizeof(struct bm_softc)
142179645Smarcel};
143179645Smarcel
144179645Smarcelstatic devclass_t bm_devclass;
145179645Smarcel
146179645SmarcelDRIVER_MODULE(bm, macio, bm_macio_driver, bm_devclass, 0, 0);
147179645SmarcelDRIVER_MODULE(miibus, bm, miibus_driver, miibus_devclass, 0, 0);
148179645Smarcel
149179645Smarcel/*
150179645Smarcel * MII internal routines
151179645Smarcel */
152179645Smarcel
153179645Smarcel/*
154179645Smarcel * Write to the MII csr, introducing a delay to allow valid
155179645Smarcel * MII clock pulses to be formed
156179645Smarcel */
157179645Smarcelstatic void
158179645Smarcelbm_miicsr_dwrite(struct bm_softc *sc, u_int16_t val)
159179645Smarcel{
160179645Smarcel	CSR_WRITE_2(sc, BM_MII_CSR, val);
161179645Smarcel	/*
162179645Smarcel	 * Assume this is a clock toggle and generate a 1us delay
163179645Smarcel	 * to cover both MII's 160ns high/low minimum and 400ns
164179645Smarcel	 * cycle miniumum
165179645Smarcel	 */
166179645Smarcel	DELAY(1);
167179645Smarcel}
168179645Smarcel
169179645Smarcel/*
170179645Smarcel * Write a bit to the MII bus.
171179645Smarcel */
172179645Smarcelstatic void
173179645Smarcelbm_mii_writebit(struct bm_softc *sc, int bit)
174179645Smarcel{
175179645Smarcel	u_int16_t regval;
176179645Smarcel
177179645Smarcel	regval = BM_MII_OENABLE;
178179645Smarcel	if (bit)
179179645Smarcel		regval |= BM_MII_DATAOUT;
180179645Smarcel
181179645Smarcel	bm_miicsr_dwrite(sc, regval);
182179645Smarcel	bm_miicsr_dwrite(sc, regval | BM_MII_CLK);
183179645Smarcel	bm_miicsr_dwrite(sc, regval);
184179645Smarcel}
185179645Smarcel
186179645Smarcel/*
187179645Smarcel * Read a bit from the MII bus.
188179645Smarcel */
189179645Smarcelstatic int
190179645Smarcelbm_mii_readbit(struct bm_softc *sc)
191179645Smarcel{
192179645Smarcel	u_int16_t regval, bitin;
193179645Smarcel
194179645Smarcel	/* ~BM_MII_OENABLE */
195179645Smarcel	regval = 0;
196179645Smarcel
197179645Smarcel	bm_miicsr_dwrite(sc, regval);
198179645Smarcel	bm_miicsr_dwrite(sc, regval | BM_MII_CLK);
199179645Smarcel	bm_miicsr_dwrite(sc, regval);
200179645Smarcel	bitin = CSR_READ_2(sc, BM_MII_CSR) & BM_MII_DATAIN;
201179645Smarcel
202179645Smarcel	return (bitin == BM_MII_DATAIN);
203179645Smarcel}
204179645Smarcel
205179645Smarcel/*
206179645Smarcel * Sync the PHYs by setting data bit and strobing the clock 32 times.
207179645Smarcel */
208179645Smarcelstatic void
209179645Smarcelbm_mii_sync(struct bm_softc *sc)
210179645Smarcel{
211179645Smarcel	int i;
212179645Smarcel	u_int16_t regval;
213179645Smarcel
214179645Smarcel	regval = BM_MII_OENABLE | BM_MII_DATAOUT;
215179645Smarcel
216179645Smarcel	bm_miicsr_dwrite(sc, regval);
217179645Smarcel	for (i = 0; i < 32; i++) {
218179645Smarcel		bm_miicsr_dwrite(sc, regval | BM_MII_CLK);
219179645Smarcel		bm_miicsr_dwrite(sc, regval);
220179645Smarcel	}
221179645Smarcel}
222179645Smarcel
223179645Smarcel/*
224179645Smarcel * Clock a series of bits through the MII.
225179645Smarcel */
226179645Smarcelstatic void
227179645Smarcelbm_mii_send(struct bm_softc *sc, u_int32_t bits, int cnt)
228179645Smarcel{
229179645Smarcel	int i;
230179645Smarcel
231179645Smarcel	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
232179645Smarcel		bm_mii_writebit(sc, bits & i);
233179645Smarcel}
234179645Smarcel
235179645Smarcel/*
236179645Smarcel * Read a PHY register through the MII.
237179645Smarcel */
238179645Smarcelstatic int
239179645Smarcelbm_mii_readreg(struct bm_softc *sc, struct bm_mii_frame *frame)
240179645Smarcel{
241179645Smarcel	int i, ack, bit;
242179645Smarcel
243179645Smarcel	BM_LOCK(sc);
244179645Smarcel
245179645Smarcel	/*
246179645Smarcel	 * Set up frame for RX.
247179645Smarcel	 */
248179645Smarcel	frame->mii_stdelim = BM_MII_STARTDELIM;
249179645Smarcel	frame->mii_opcode = BM_MII_READOP;
250179645Smarcel	frame->mii_turnaround = 0;
251179645Smarcel	frame->mii_data = 0;
252179645Smarcel
253179645Smarcel	/*
254179645Smarcel	 * Sync the PHYs
255179645Smarcel	 */
256179645Smarcel	bm_mii_sync(sc);
257179645Smarcel
258179645Smarcel	/*
259179645Smarcel	 * Send command/address info
260179645Smarcel	 */
261179645Smarcel	bm_mii_send(sc, frame->mii_stdelim, 2);
262179645Smarcel	bm_mii_send(sc, frame->mii_opcode, 2);
263179645Smarcel	bm_mii_send(sc, frame->mii_phyaddr, 5);
264179645Smarcel	bm_mii_send(sc, frame->mii_regaddr, 5);
265179645Smarcel
266179645Smarcel	/*
267179645Smarcel	 * Check for ack.
268179645Smarcel	 */
269179645Smarcel	ack = bm_mii_readbit(sc);
270179645Smarcel
271179645Smarcel	/*
272179645Smarcel	 * Now try reading data bits. If the ack failed, we still
273179645Smarcel	 * need to clock through 16 cycles to keep the PHY(s) in sync.
274179645Smarcel	 */
275179645Smarcel	for (i = 0x8000; i; i >>= 1) {
276179645Smarcel		bit = bm_mii_readbit(sc);
277179645Smarcel		if (!ack && bit)
278179645Smarcel			frame->mii_data |= i;
279179645Smarcel	}
280179645Smarcel
281179645Smarcel	/*
282179645Smarcel	 * Skip through idle bit-times
283179645Smarcel	 */
284179645Smarcel	bm_mii_writebit(sc, 0);
285179645Smarcel	bm_mii_writebit(sc, 0);
286179645Smarcel
287179645Smarcel	BM_UNLOCK(sc);
288179645Smarcel
289179645Smarcel	return ((ack) ? 1 : 0);
290179645Smarcel}
291179645Smarcel
292179645Smarcel/*
293179645Smarcel * Write to a PHY register through the MII.
294179645Smarcel */
295179645Smarcelstatic int
296179645Smarcelbm_mii_writereg(struct bm_softc *sc, struct bm_mii_frame *frame)
297179645Smarcel{
298179645Smarcel	BM_LOCK(sc);
299179645Smarcel
300179645Smarcel	/*
301179645Smarcel	 * Set up frame for tx
302179645Smarcel	 */
303179645Smarcel	frame->mii_stdelim = BM_MII_STARTDELIM;
304179645Smarcel	frame->mii_opcode = BM_MII_WRITEOP;
305179645Smarcel	frame->mii_turnaround = BM_MII_TURNAROUND;
306179645Smarcel
307179645Smarcel	/*
308179645Smarcel	 * Sync the phy and start the bitbang write sequence
309179645Smarcel	 */
310179645Smarcel	bm_mii_sync(sc);
311179645Smarcel
312179645Smarcel	bm_mii_send(sc, frame->mii_stdelim, 2);
313179645Smarcel	bm_mii_send(sc, frame->mii_opcode, 2);
314179645Smarcel	bm_mii_send(sc, frame->mii_phyaddr, 5);
315179645Smarcel	bm_mii_send(sc, frame->mii_regaddr, 5);
316179645Smarcel	bm_mii_send(sc, frame->mii_turnaround, 2);
317179645Smarcel	bm_mii_send(sc, frame->mii_data, 16);
318179645Smarcel
319179645Smarcel	/*
320179645Smarcel	 * Idle bit.
321179645Smarcel	 */
322179645Smarcel	bm_mii_writebit(sc, 0);
323179645Smarcel
324179645Smarcel	BM_UNLOCK(sc);
325179645Smarcel
326179645Smarcel	return (0);
327179645Smarcel}
328179645Smarcel
329179645Smarcel/*
330179645Smarcel * MII bus i/f
331179645Smarcel */
332179645Smarcelstatic int
333179645Smarcelbm_miibus_readreg(device_t dev, int phy, int reg)
334179645Smarcel{
335179645Smarcel	struct bm_softc *sc;
336179645Smarcel	struct bm_mii_frame frame;
337179645Smarcel
338179645Smarcel	sc = device_get_softc(dev);
339179645Smarcel	bzero(&frame, sizeof(frame));
340179645Smarcel
341179645Smarcel	frame.mii_phyaddr = phy;
342179645Smarcel	frame.mii_regaddr = reg;
343179645Smarcel
344179645Smarcel	bm_mii_readreg(sc, &frame);
345179645Smarcel
346179645Smarcel	return (frame.mii_data);
347179645Smarcel}
348179645Smarcel
349179645Smarcelstatic int
350179645Smarcelbm_miibus_writereg(device_t dev, int phy, int reg, int data)
351179645Smarcel{
352179645Smarcel	struct bm_softc *sc;
353179645Smarcel	struct bm_mii_frame frame;
354179645Smarcel
355179645Smarcel	sc = device_get_softc(dev);
356179645Smarcel	bzero(&frame, sizeof(frame));
357179645Smarcel
358179645Smarcel	frame.mii_phyaddr = phy;
359179645Smarcel	frame.mii_regaddr = reg;
360179645Smarcel	frame.mii_data = data;
361179645Smarcel
362179645Smarcel	bm_mii_writereg(sc, &frame);
363179645Smarcel
364179645Smarcel	return (0);
365179645Smarcel}
366179645Smarcel
367179645Smarcelstatic void
368179645Smarcelbm_miibus_statchg(device_t dev)
369179645Smarcel{
370179645Smarcel	struct bm_softc *sc = device_get_softc(dev);
371179645Smarcel	uint16_t reg;
372179645Smarcel	int new_duplex;
373179645Smarcel
374179645Smarcel	reg = CSR_READ_2(sc, BM_TX_CONFIG);
375179645Smarcel	new_duplex = IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX;
376179645Smarcel
377179645Smarcel	if (new_duplex != sc->sc_duplex) {
378179645Smarcel		/* Turn off TX MAC while we fiddle its settings */
379179645Smarcel		reg &= ~BM_ENABLE;
380179645Smarcel
381179645Smarcel		CSR_WRITE_2(sc, BM_TX_CONFIG, reg);
382179645Smarcel		while (CSR_READ_2(sc, BM_TX_CONFIG) & BM_ENABLE)
383179645Smarcel			DELAY(10);
384179645Smarcel	}
385179645Smarcel
386179645Smarcel	if (new_duplex && !sc->sc_duplex)
387179645Smarcel		reg |= BM_TX_IGNORECOLL | BM_TX_FULLDPX;
388179645Smarcel	else if (!new_duplex && sc->sc_duplex)
389179645Smarcel		reg &= ~(BM_TX_IGNORECOLL | BM_TX_FULLDPX);
390179645Smarcel
391179645Smarcel	if (new_duplex != sc->sc_duplex) {
392179645Smarcel		/* Turn TX MAC back on */
393179645Smarcel		reg |= BM_ENABLE;
394179645Smarcel
395179645Smarcel		CSR_WRITE_2(sc, BM_TX_CONFIG, reg);
396179645Smarcel		sc->sc_duplex = new_duplex;
397179645Smarcel	}
398179645Smarcel}
399179645Smarcel
400179645Smarcel/*
401179645Smarcel * ifmedia/mii callbacks
402179645Smarcel */
403179645Smarcelstatic int
404179645Smarcelbm_ifmedia_upd(struct ifnet *ifp)
405179645Smarcel{
406179645Smarcel	struct bm_softc *sc = ifp->if_softc;
407179645Smarcel	int error;
408179645Smarcel
409179645Smarcel	BM_LOCK(sc);
410179645Smarcel	error = mii_mediachg(sc->sc_mii);
411179645Smarcel	BM_UNLOCK(sc);
412179645Smarcel	return (error);
413179645Smarcel}
414179645Smarcel
415179645Smarcelstatic void
416179645Smarcelbm_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifm)
417179645Smarcel{
418179645Smarcel	struct bm_softc *sc = ifp->if_softc;
419179645Smarcel
420179645Smarcel	BM_LOCK(sc);
421179645Smarcel	mii_pollstat(sc->sc_mii);
422179645Smarcel	ifm->ifm_active = sc->sc_mii->mii_media_active;
423179645Smarcel	ifm->ifm_status = sc->sc_mii->mii_media_status;
424179645Smarcel	BM_UNLOCK(sc);
425179645Smarcel}
426179645Smarcel
427179645Smarcel/*
428179645Smarcel * Macio probe/attach
429179645Smarcel */
430179645Smarcelstatic int
431179645Smarcelbm_probe(device_t dev)
432179645Smarcel{
433179645Smarcel	const char *dname = ofw_bus_get_name(dev);
434179645Smarcel	const char *dcompat = ofw_bus_get_compat(dev);
435179645Smarcel
436179645Smarcel	/*
437179645Smarcel	 * BMAC+ cells have a name of "ethernet" and
438179645Smarcel	 * a compatible property of "bmac+"
439179645Smarcel	 */
440179645Smarcel	if (strcmp(dname, "bmac") == 0) {
441179645Smarcel		device_set_desc(dev, "Apple BMAC Ethernet Adaptor");
442179645Smarcel	} else if (strcmp(dcompat, "bmac+") == 0) {
443179645Smarcel		device_set_desc(dev, "Apple BMAC+ Ethernet Adaptor");
444179645Smarcel	} else
445179645Smarcel		return (ENXIO);
446179645Smarcel
447179645Smarcel	return (0);
448179645Smarcel}
449179645Smarcel
450179645Smarcelstatic int
451179645Smarcelbm_attach(device_t dev)
452179645Smarcel{
453179645Smarcel	phandle_t node;
454179645Smarcel	u_char *eaddr;
455179645Smarcel	struct ifnet *ifp;
456179645Smarcel	int error, cellid, i;
457179645Smarcel	struct bm_txsoft *txs;
458179645Smarcel	struct bm_softc *sc = device_get_softc(dev);
459179645Smarcel
460179645Smarcel	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
461179645Smarcel	ifp->if_softc = sc;
462179645Smarcel	sc->sc_dev = dev;
463179645Smarcel	sc->sc_duplex = ~IFM_FDX;
464179645Smarcel
465179645Smarcel	error = 0;
466179645Smarcel	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
467179645Smarcel	    MTX_DEF | MTX_RECURSE);
468179645Smarcel	callout_init_mtx(&sc->sc_tick_ch, &sc->sc_mtx, 0);
469179645Smarcel
470179645Smarcel	/* Check for an improved version of Paddington */
471179645Smarcel	sc->sc_streaming = 0;
472179645Smarcel	cellid = -1;
473179645Smarcel	node = ofw_bus_get_node(dev);
474179645Smarcel
475179645Smarcel	OF_getprop(node, "cell-id", &cellid, sizeof(cellid));
476179645Smarcel	if (cellid >= 0xc4)
477179645Smarcel		sc->sc_streaming = 1;
478179645Smarcel
479179645Smarcel	sc->sc_memrid = 0;
480179645Smarcel	sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
481179645Smarcel	    &sc->sc_memrid, RF_ACTIVE);
482179645Smarcel	if (sc->sc_memr == NULL) {
483179645Smarcel		device_printf(dev, "Could not alloc chip registers!\n");
484179645Smarcel		return (ENXIO);
485179645Smarcel	}
486179645Smarcel
487179645Smarcel	sc->sc_btag = rman_get_bustag(sc->sc_memr);
488179645Smarcel	sc->sc_bhandle = rman_get_bushandle(sc->sc_memr);
489179645Smarcel
490179645Smarcel	sc->sc_txdmarid = BM_TXDMA_REGISTERS;
491179645Smarcel	sc->sc_rxdmarid = BM_RXDMA_REGISTERS;
492179645Smarcel
493179645Smarcel	sc->sc_txdmar = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
494179645Smarcel	    &sc->sc_txdmarid, RF_ACTIVE);
495179645Smarcel	sc->sc_rxdmar = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
496179645Smarcel	    &sc->sc_rxdmarid, RF_ACTIVE);
497179645Smarcel
498179645Smarcel	if (sc->sc_txdmar == NULL || sc->sc_rxdmar == NULL) {
499179645Smarcel		device_printf(dev, "Could not map DBDMA registers!\n");
500179645Smarcel		return (ENXIO);
501179645Smarcel	}
502179645Smarcel
503179645Smarcel	error = dbdma_allocate_channel(sc->sc_txdmar, bus_get_dma_tag(dev),
504179645Smarcel	    BM_MAX_DMA_COMMANDS, &sc->sc_txdma);
505179645Smarcel	error += dbdma_allocate_channel(sc->sc_rxdmar, bus_get_dma_tag(dev),
506179645Smarcel	    BM_MAX_DMA_COMMANDS, &sc->sc_rxdma);
507179645Smarcel
508179645Smarcel	if (error) {
509179645Smarcel		device_printf(dev,"Could not allocate DBDMA channel!\n");
510179645Smarcel		return (ENXIO);
511179645Smarcel	}
512179645Smarcel
513179645Smarcel	/* alloc DMA tags and buffers */
514179645Smarcel	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
515179645Smarcel	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
516179645Smarcel	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, NULL,
517179645Smarcel	    NULL, &sc->sc_pdma_tag);
518179645Smarcel
519179645Smarcel	if (error) {
520179645Smarcel		device_printf(dev,"Could not allocate DMA tag!\n");
521179645Smarcel		return (ENXIO);
522179645Smarcel	}
523179645Smarcel
524179645Smarcel	error = bus_dma_tag_create(sc->sc_pdma_tag, 1, 0, BUS_SPACE_MAXADDR,
525179645Smarcel	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES,
526179645Smarcel	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_rdma_tag);
527179645Smarcel
528179645Smarcel	if (error) {
529179645Smarcel		device_printf(dev,"Could not allocate RX DMA channel!\n");
530179645Smarcel		return (ENXIO);
531179645Smarcel	}
532179645Smarcel
533179645Smarcel	error = bus_dma_tag_create(sc->sc_pdma_tag, 1, 0, BUS_SPACE_MAXADDR,
534179645Smarcel	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * BM_NTXSEGS, BM_NTXSEGS,
535179645Smarcel	    MCLBYTES, BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_tdma_tag);
536179645Smarcel
537179645Smarcel	if (error) {
538179645Smarcel		device_printf(dev,"Could not allocate TX DMA tag!\n");
539179645Smarcel		return (ENXIO);
540179645Smarcel	}
541179645Smarcel
542179645Smarcel	/* init transmit descriptors */
543179645Smarcel	STAILQ_INIT(&sc->sc_txfreeq);
544179645Smarcel	STAILQ_INIT(&sc->sc_txdirtyq);
545179645Smarcel
546179645Smarcel	/* create TX DMA maps */
547179645Smarcel	error = ENOMEM;
548179645Smarcel	for (i = 0; i < BM_MAX_TX_PACKETS; i++) {
549179645Smarcel		txs = &sc->sc_txsoft[i];
550179645Smarcel		txs->txs_mbuf = NULL;
551179645Smarcel		error = bus_dmamap_create(sc->sc_tdma_tag, 0, &txs->txs_dmamap);
552179645Smarcel		if (error) {
553179645Smarcel			device_printf(sc->sc_dev,
554179645Smarcel			    "unable to create TX DMA map %d, error = %d\n",
555179645Smarcel			    i, error);
556179645Smarcel		}
557179645Smarcel		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
558179645Smarcel	}
559179645Smarcel
560179645Smarcel	/* Create the receive buffer DMA maps. */
561179645Smarcel	for (i = 0; i < BM_MAX_RX_PACKETS; i++) {
562179645Smarcel		error = bus_dmamap_create(sc->sc_rdma_tag, 0,
563179645Smarcel		    &sc->sc_rxsoft[i].rxs_dmamap);
564179645Smarcel		if (error) {
565179645Smarcel			device_printf(sc->sc_dev,
566179645Smarcel			    "unable to create RX DMA map %d, error = %d\n",
567179645Smarcel			    i, error);
568179645Smarcel		}
569179645Smarcel		sc->sc_rxsoft[i].rxs_mbuf = NULL;
570179645Smarcel	}
571179645Smarcel
572179645Smarcel	/* alloc interrupt */
573179645Smarcel
574179645Smarcel	sc->sc_txdmairqid = BM_TXDMA_INTERRUPT;
575179645Smarcel	sc->sc_txdmairq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
576179645Smarcel	    &sc->sc_txdmairqid, RF_ACTIVE);
577179645Smarcel
578179645Smarcel	if (error) {
579179645Smarcel		device_printf(dev,"Could not allocate TX interrupt!\n");
580179645Smarcel		return (ENXIO);
581179645Smarcel	}
582179645Smarcel
583179645Smarcel	bus_setup_intr(dev,sc->sc_txdmairq,
584179645Smarcel	    INTR_TYPE_MISC | INTR_MPSAFE | INTR_ENTROPY, NULL, bm_txintr, sc,
585179645Smarcel	    &sc->sc_txihtx);
586179645Smarcel
587179645Smarcel	sc->sc_rxdmairqid = BM_RXDMA_INTERRUPT;
588179645Smarcel	sc->sc_rxdmairq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
589179645Smarcel	    &sc->sc_rxdmairqid, RF_ACTIVE);
590179645Smarcel
591179645Smarcel	if (error) {
592179645Smarcel		device_printf(dev,"Could not allocate RX interrupt!\n");
593179645Smarcel		return (ENXIO);
594179645Smarcel	}
595179645Smarcel
596179645Smarcel	bus_setup_intr(dev,sc->sc_rxdmairq,
597179645Smarcel	    INTR_TYPE_MISC | INTR_MPSAFE | INTR_ENTROPY, NULL, bm_rxintr, sc,
598179645Smarcel	    &sc->sc_rxih);
599179645Smarcel
600179645Smarcel	/*
601179645Smarcel	 * Get the ethernet address from OpenFirmware
602179645Smarcel	 */
603179645Smarcel	eaddr = sc->sc_enaddr;
604179645Smarcel	OF_getprop(node, "local-mac-address", eaddr, ETHER_ADDR_LEN);
605179645Smarcel
606179645Smarcel	/* reset the adapter  */
607179645Smarcel	bm_chip_setup(sc);
608179645Smarcel
609179645Smarcel	/* setup MII */
610179645Smarcel	error = mii_phy_probe(dev, &sc->sc_miibus, bm_ifmedia_upd,
611179645Smarcel	    bm_ifmedia_sts);
612179645Smarcel	if (error != 0)
613179645Smarcel		device_printf(dev,"PHY probe failed: %d\n", error);
614179645Smarcel
615179645Smarcel	sc->sc_mii = device_get_softc(sc->sc_miibus);
616179645Smarcel
617179645Smarcel	if_initname(ifp, device_get_name(sc->sc_dev),
618179645Smarcel	    device_get_unit(sc->sc_dev));
619179645Smarcel	ifp->if_mtu = ETHERMTU;
620179645Smarcel	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
621179645Smarcel	ifp->if_start = bm_start;
622179645Smarcel	ifp->if_ioctl = bm_ioctl;
623179645Smarcel	ifp->if_init = bm_init;
624179645Smarcel	IFQ_SET_MAXLEN(&ifp->if_snd, BM_MAX_TX_PACKETS);
625179645Smarcel	ifp->if_snd.ifq_drv_maxlen = BM_MAX_TX_PACKETS;
626179645Smarcel	IFQ_SET_READY(&ifp->if_snd);
627179645Smarcel
628179645Smarcel	/* Attach the interface. */
629179645Smarcel	ether_ifattach(ifp, sc->sc_enaddr);
630179645Smarcel
631179645Smarcel	ifp->if_data.ifi_hdrlen = sizeof(struct ether_header);
632179645Smarcel	ifp->if_hwassist = 0;
633179645Smarcel
634179645Smarcel	return (0);
635179645Smarcel}
636179645Smarcel
637179645Smarcelstatic int
638179645Smarcelbm_detach(device_t dev)
639179645Smarcel{
640179645Smarcel	struct bm_softc *sc = device_get_softc(dev);
641179645Smarcel
642179645Smarcel	callout_drain(&sc->sc_tick_ch);
643179645Smarcel
644179645Smarcel	BM_LOCK(sc);
645179645Smarcel	bm_stop(sc);
646179645Smarcel
647179645Smarcel	dbdma_free_channel(sc->sc_txdma);
648179645Smarcel	dbdma_free_channel(sc->sc_rxdma);
649179645Smarcel
650179645Smarcel	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memr);
651179645Smarcel	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_txdmarid,
652179645Smarcel	    sc->sc_txdmar);
653179645Smarcel	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rxdmarid,
654179645Smarcel	    sc->sc_rxdmar);
655179645Smarcel
656179645Smarcel	bus_teardown_intr(dev, sc->sc_txdmairq, sc->sc_txihtx);
657179645Smarcel	bus_teardown_intr(dev, sc->sc_rxdmairq, sc->sc_rxih);
658179645Smarcel	bus_release_resource(dev, SYS_RES_IRQ, sc->sc_txdmairqid,
659179645Smarcel	    sc->sc_txdmairq);
660179645Smarcel	bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rxdmairqid,
661179645Smarcel	    sc->sc_rxdmairq);
662179645Smarcel	BM_UNLOCK(sc);
663179645Smarcel
664179645Smarcel	mtx_destroy(&sc->sc_mtx);
665179645Smarcel
666179645Smarcel	return (0);
667179645Smarcel}
668179645Smarcel
669179645Smarcelstatic void
670179645Smarcelbm_shutdown(device_t dev)
671179645Smarcel{
672179645Smarcel	bm_stop(device_get_softc(dev));
673179645Smarcel}
674179645Smarcel
675179645Smarcelstatic void
676179645Smarcelbm_dummypacket(struct bm_softc *sc)
677179645Smarcel{
678179645Smarcel	struct mbuf *m;
679179645Smarcel	struct ifnet *ifp;
680179645Smarcel
681179645Smarcel	ifp = sc->sc_ifp;
682179645Smarcel
683179645Smarcel	MGETHDR(m, M_DONTWAIT, MT_DATA);
684179645Smarcel
685179645Smarcel	if (m == NULL)
686179645Smarcel		return;
687179645Smarcel
688179645Smarcel	bcopy(sc->sc_enaddr,
689179645Smarcel	    mtod(m, struct ether_header *)->ether_dhost, ETHER_ADDR_LEN);
690179645Smarcel	bcopy(sc->sc_enaddr,
691179645Smarcel	    mtod(m, struct ether_header *)->ether_shost, ETHER_ADDR_LEN);
692179645Smarcel	mtod(m, struct ether_header *)->ether_type = htons(3);
693179645Smarcel	mtod(m, unsigned char *)[14] = 0;
694179645Smarcel	mtod(m, unsigned char *)[15] = 0;
695179645Smarcel	mtod(m, unsigned char *)[16] = 0xE3;
696179645Smarcel	m->m_len = m->m_pkthdr.len = sizeof(struct ether_header) + 3;
697179645Smarcel	IF_ENQUEUE(&ifp->if_snd, m);
698179645Smarcel	bm_start(ifp);
699179645Smarcel}
700179645Smarcel
701179645Smarcelstatic void
702179645Smarcelbm_rxintr(void *xsc)
703179645Smarcel{
704179645Smarcel	struct bm_softc *sc = xsc;
705179645Smarcel	struct ifnet *ifp = sc->sc_ifp;
706179645Smarcel	struct mbuf *m;
707179645Smarcel	int i, prev_stop, new_stop;
708179645Smarcel	uint16_t status;
709179645Smarcel
710179645Smarcel	BM_LOCK(sc);
711179645Smarcel
712179645Smarcel	status = dbdma_get_chan_status(sc->sc_rxdma);
713179645Smarcel	if (status & DBDMA_STATUS_DEAD) {
714179645Smarcel		dbdma_reset(sc->sc_rxdma);
715179645Smarcel		BM_UNLOCK(sc);
716179645Smarcel		return;
717179645Smarcel	}
718179645Smarcel	if (!(status & DBDMA_STATUS_RUN)) {
719179645Smarcel		device_printf(sc->sc_dev,"Bad RX Interrupt!\n");
720179645Smarcel		BM_UNLOCK(sc);
721179645Smarcel		return;
722179645Smarcel	}
723179645Smarcel
724179645Smarcel	prev_stop = sc->next_rxdma_slot - 1;
725179645Smarcel	if (prev_stop < 0)
726179645Smarcel		prev_stop = sc->rxdma_loop_slot - 1;
727179645Smarcel
728179645Smarcel	if (prev_stop < 0) {
729179645Smarcel		BM_UNLOCK(sc);
730179645Smarcel		return;
731179645Smarcel	}
732179645Smarcel
733179645Smarcel	new_stop = -1;
734179645Smarcel	dbdma_sync_commands(sc->sc_rxdma, BUS_DMASYNC_POSTREAD);
735179645Smarcel
736179645Smarcel	for (i = sc->next_rxdma_slot; i < BM_MAX_RX_PACKETS; i++) {
737179645Smarcel		if (i == sc->rxdma_loop_slot)
738179645Smarcel			i = 0;
739179645Smarcel
740179645Smarcel		if (i == prev_stop)
741179645Smarcel			break;
742179645Smarcel
743179645Smarcel		status = dbdma_get_cmd_status(sc->sc_rxdma, i);
744179645Smarcel
745179645Smarcel		if (status == 0)
746179645Smarcel			break;
747179645Smarcel
748179645Smarcel		m = sc->sc_rxsoft[i].rxs_mbuf;
749179645Smarcel
750179645Smarcel		if (bm_add_rxbuf(sc, i)) {
751179645Smarcel			ifp->if_ierrors++;
752179645Smarcel			m = NULL;
753179645Smarcel			continue;
754179645Smarcel		}
755179645Smarcel
756179645Smarcel		if (m == NULL)
757179645Smarcel			continue;
758179645Smarcel
759179645Smarcel		ifp->if_ipackets++;
760179645Smarcel		m->m_pkthdr.rcvif = ifp;
761179645Smarcel		m->m_len -= (dbdma_get_residuals(sc->sc_rxdma, i) + 2);
762179645Smarcel		m->m_pkthdr.len = m->m_len;
763179645Smarcel
764179645Smarcel		/* Send up the stack */
765179645Smarcel		BM_UNLOCK(sc);
766179645Smarcel		(*ifp->if_input)(ifp, m);
767179645Smarcel		BM_LOCK(sc);
768179645Smarcel
769179645Smarcel		/* Clear all fields on this command */
770179645Smarcel		bm_add_rxbuf_dma(sc, i);
771179645Smarcel
772179645Smarcel		new_stop = i;
773179645Smarcel	}
774179645Smarcel
775179645Smarcel	/* Change the last packet we processed to the ring buffer terminator,
776179645Smarcel	 * and restore a receive buffer to the old terminator */
777179645Smarcel	if (new_stop >= 0) {
778179645Smarcel		dbdma_insert_stop(sc->sc_rxdma, new_stop);
779179645Smarcel		bm_add_rxbuf_dma(sc, prev_stop);
780179645Smarcel		if (i < sc->rxdma_loop_slot)
781179645Smarcel			sc->next_rxdma_slot = i;
782179645Smarcel		else
783179645Smarcel			sc->next_rxdma_slot = 0;
784179645Smarcel	}
785179645Smarcel	dbdma_sync_commands(sc->sc_rxdma, BUS_DMASYNC_PREWRITE);
786179645Smarcel
787179645Smarcel	dbdma_wake(sc->sc_rxdma);
788179645Smarcel
789179645Smarcel	BM_UNLOCK(sc);
790179645Smarcel}
791179645Smarcel
792179645Smarcelstatic void
793179645Smarcelbm_txintr(void *xsc)
794179645Smarcel{
795179645Smarcel	struct bm_softc *sc = xsc;
796179645Smarcel	struct ifnet *ifp = sc->sc_ifp;
797179645Smarcel	struct bm_txsoft *txs;
798179645Smarcel	int progress = 0;
799179645Smarcel
800179645Smarcel	BM_LOCK(sc);
801179645Smarcel
802179645Smarcel	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
803179645Smarcel		if (!dbdma_get_cmd_status(sc->sc_txdma, txs->txs_lastdesc))
804179645Smarcel			break;
805179645Smarcel
806179645Smarcel		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
807179645Smarcel		bus_dmamap_unload(sc->sc_tdma_tag, txs->txs_dmamap);
808179645Smarcel
809179645Smarcel		if (txs->txs_mbuf != NULL) {
810179645Smarcel			m_freem(txs->txs_mbuf);
811179645Smarcel			txs->txs_mbuf = NULL;
812179645Smarcel		}
813179645Smarcel
814179645Smarcel		/* Set the first used TXDMA slot to the location of the
815179645Smarcel		 * STOP/NOP command associated with this packet. */
816179645Smarcel
817179645Smarcel		sc->first_used_txdma_slot = txs->txs_stopdesc;
818179645Smarcel
819179645Smarcel		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
820179645Smarcel
821179645Smarcel		ifp->if_opackets++;
822179645Smarcel		progress = 1;
823179645Smarcel	}
824179645Smarcel
825179645Smarcel	if (progress) {
826179645Smarcel		/*
827179645Smarcel		 * We freed some descriptors, so reset IFF_DRV_OACTIVE
828179645Smarcel		 * and restart.
829179645Smarcel		 */
830179645Smarcel		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
831179645Smarcel		sc->sc_wdog_timer = STAILQ_EMPTY(&sc->sc_txdirtyq) ? 0 : 5;
832179645Smarcel
833179645Smarcel		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) &&
834179645Smarcel		    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
835179645Smarcel			bm_start_locked(ifp);
836179645Smarcel	}
837179645Smarcel
838179645Smarcel	BM_UNLOCK(sc);
839179645Smarcel}
840179645Smarcel
841179645Smarcelstatic void
842179645Smarcelbm_start(struct ifnet *ifp)
843179645Smarcel{
844179645Smarcel	struct bm_softc *sc = ifp->if_softc;
845179645Smarcel
846179645Smarcel	BM_LOCK(sc);
847179645Smarcel	bm_start_locked(ifp);
848179645Smarcel	BM_UNLOCK(sc);
849179645Smarcel}
850179645Smarcel
851179645Smarcelstatic void
852179645Smarcelbm_start_locked(struct ifnet *ifp)
853179645Smarcel{
854179645Smarcel	struct bm_softc *sc = ifp->if_softc;
855179645Smarcel	struct mbuf *mb_head;
856179645Smarcel	int prev_stop;
857179645Smarcel	int txqueued = 0;
858179645Smarcel
859179645Smarcel	/*
860179645Smarcel	 * We lay out our DBDMA program in the following manner:
861179645Smarcel	 *	OUTPUT_MORE
862179645Smarcel	 *	...
863179645Smarcel	 *	OUTPUT_LAST (+ Interrupt)
864179645Smarcel	 *	STOP
865179645Smarcel	 *
866179645Smarcel	 * To extend the channel, we append a new program,
867179645Smarcel	 * then replace STOP with NOP and wake the channel.
868179645Smarcel	 * If we stalled on the STOP already, the program proceeds,
869179645Smarcel	 * if not it will sail through the NOP.
870179645Smarcel	 */
871179645Smarcel
872179645Smarcel	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
873179645Smarcel		IFQ_DRV_DEQUEUE(&ifp->if_snd, mb_head);
874179645Smarcel
875179645Smarcel		if (mb_head == NULL)
876179645Smarcel			break;
877179645Smarcel
878179645Smarcel		prev_stop = sc->next_txdma_slot - 1;
879179645Smarcel
880179645Smarcel		if (bm_encap(sc, &mb_head)) {
881179645Smarcel			/* Put the packet back and stop */
882179645Smarcel			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
883179645Smarcel			IFQ_DRV_PREPEND(&ifp->if_snd, mb_head);
884179645Smarcel			break;
885179645Smarcel		}
886179645Smarcel
887179645Smarcel		dbdma_insert_nop(sc->sc_txdma, prev_stop);
888179645Smarcel
889179645Smarcel		txqueued = 1;
890179645Smarcel
891179645Smarcel		BPF_MTAP(ifp, mb_head);
892179645Smarcel	}
893179645Smarcel
894179645Smarcel	dbdma_sync_commands(sc->sc_txdma, BUS_DMASYNC_PREWRITE);
895179645Smarcel
896179645Smarcel	if (txqueued) {
897179645Smarcel		dbdma_wake(sc->sc_txdma);
898179645Smarcel		sc->sc_wdog_timer = 5;
899179645Smarcel	}
900179645Smarcel}
901179645Smarcel
902179645Smarcelstatic int
903179645Smarcelbm_encap(struct bm_softc *sc, struct mbuf **m_head)
904179645Smarcel{
905179645Smarcel	bus_dma_segment_t segs[BM_NTXSEGS];
906179645Smarcel	struct bm_txsoft *txs;
907179645Smarcel	struct mbuf *m;
908179645Smarcel	int nsegs = BM_NTXSEGS;
909179645Smarcel	int error = 0;
910179645Smarcel	uint8_t branch_type;
911179645Smarcel	int i;
912179645Smarcel
913179645Smarcel	/* Limit the command size to the number of free DBDMA slots */
914179645Smarcel
915179645Smarcel	if (sc->next_txdma_slot >= sc->first_used_txdma_slot)
916179645Smarcel		nsegs = BM_MAX_DMA_COMMANDS - 2 - sc->next_txdma_slot +
917179645Smarcel		    sc->first_used_txdma_slot;  /* -2 for branch and indexing */
918179645Smarcel	else
919179645Smarcel		nsegs = sc->first_used_txdma_slot - sc->next_txdma_slot;
920179645Smarcel
921179645Smarcel	/* Remove one slot for the STOP/NOP terminator */
922179645Smarcel	nsegs--;
923179645Smarcel
924179645Smarcel	if (nsegs > BM_NTXSEGS)
925179645Smarcel		nsegs = BM_NTXSEGS;
926179645Smarcel
927179645Smarcel	/* Get a work queue entry. */
928179645Smarcel	if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) {
929179645Smarcel		/* Ran out of descriptors. */
930179645Smarcel		return (ENOBUFS);
931179645Smarcel	}
932179645Smarcel
933179645Smarcel	error = bus_dmamap_load_mbuf_sg(sc->sc_tdma_tag, txs->txs_dmamap,
934179645Smarcel	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
935179645Smarcel
936179645Smarcel	if (error == EFBIG) {
937179645Smarcel		m = m_collapse(*m_head, M_DONTWAIT, nsegs);
938179645Smarcel		if (m == NULL) {
939179645Smarcel			m_freem(*m_head);
940179645Smarcel			*m_head = NULL;
941179645Smarcel			return (ENOBUFS);
942179645Smarcel		}
943179645Smarcel		*m_head = m;
944179645Smarcel
945179645Smarcel		error = bus_dmamap_load_mbuf_sg(sc->sc_tdma_tag,
946179645Smarcel		    txs->txs_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
947179645Smarcel		if (error != 0) {
948179645Smarcel			m_freem(*m_head);
949179645Smarcel			*m_head = NULL;
950179645Smarcel			return (error);
951179645Smarcel		}
952179645Smarcel	} else if (error != 0)
953179645Smarcel		return (error);
954179645Smarcel
955179645Smarcel	if (nsegs == 0) {
956179645Smarcel		m_freem(*m_head);
957179645Smarcel		*m_head = NULL;
958179645Smarcel		return (EIO);
959179645Smarcel	}
960179645Smarcel
961179645Smarcel	txs->txs_ndescs = nsegs;
962179645Smarcel	txs->txs_firstdesc = sc->next_txdma_slot;
963179645Smarcel
964179645Smarcel	for (i = 0; i < nsegs; i++) {
965179645Smarcel		/* Loop back to the beginning if this is our last slot */
966179645Smarcel		if (sc->next_txdma_slot == (BM_MAX_DMA_COMMANDS - 1))
967179645Smarcel			branch_type = DBDMA_ALWAYS;
968179645Smarcel		else
969179645Smarcel			branch_type = DBDMA_NEVER;
970179645Smarcel
971179645Smarcel		if (i+1 == nsegs)
972179645Smarcel			txs->txs_lastdesc = sc->next_txdma_slot;
973179645Smarcel
974179645Smarcel		dbdma_insert_command(sc->sc_txdma, sc->next_txdma_slot++,
975179645Smarcel		    (i + 1 < nsegs) ? DBDMA_OUTPUT_MORE : DBDMA_OUTPUT_LAST,
976179645Smarcel		    0, segs[i].ds_addr, segs[i].ds_len,
977179645Smarcel		    (i + 1 < nsegs) ? DBDMA_NEVER : DBDMA_ALWAYS,
978179645Smarcel		    branch_type, DBDMA_NEVER, 0);
979179645Smarcel
980179645Smarcel		if (branch_type == DBDMA_ALWAYS)
981179645Smarcel			sc->next_txdma_slot = 0;
982179645Smarcel	}
983179645Smarcel
984179645Smarcel	/* We have a corner case where the STOP command is the last slot,
985179645Smarcel	 * but you can't branch in STOP commands. So add a NOP branch here
986179645Smarcel	 * and the STOP in slot 0. */
987179645Smarcel
988179645Smarcel	if (sc->next_txdma_slot == (BM_MAX_DMA_COMMANDS - 1)) {
989179645Smarcel		dbdma_insert_branch(sc->sc_txdma, sc->next_txdma_slot, 0);
990179645Smarcel		sc->next_txdma_slot = 0;
991179645Smarcel	}
992179645Smarcel
993179645Smarcel	txs->txs_stopdesc = sc->next_txdma_slot;
994179645Smarcel	dbdma_insert_stop(sc->sc_txdma, sc->next_txdma_slot++);
995179645Smarcel
996179645Smarcel	STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
997179645Smarcel	STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
998179645Smarcel	txs->txs_mbuf = *m_head;
999179645Smarcel
1000179645Smarcel	return (0);
1001179645Smarcel}
1002179645Smarcel
1003179645Smarcelstatic int
1004179645Smarcelbm_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1005179645Smarcel{
1006179645Smarcel	struct bm_softc *sc = ifp->if_softc;
1007179645Smarcel	struct ifreq *ifr = (struct ifreq *)data;
1008179645Smarcel	int error;
1009179645Smarcel
1010179645Smarcel	error = 0;
1011179645Smarcel
1012179645Smarcel	switch(cmd) {
1013179645Smarcel	case SIOCSIFFLAGS:
1014179645Smarcel		BM_LOCK(sc);
1015179645Smarcel		if ((ifp->if_flags & IFF_UP) != 0) {
1016179645Smarcel			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
1017179645Smarcel			   ((ifp->if_flags ^ sc->sc_ifpflags) &
1018179645Smarcel			    (IFF_ALLMULTI | IFF_PROMISC)) != 0)
1019179645Smarcel				bm_setladrf(sc);
1020179645Smarcel			else
1021179645Smarcel				bm_init_locked(sc);
1022179645Smarcel		} else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1023179645Smarcel			bm_stop(sc);
1024179645Smarcel		sc->sc_ifpflags = ifp->if_flags;
1025179645Smarcel		BM_UNLOCK(sc);
1026179645Smarcel		break;
1027179645Smarcel	case SIOCADDMULTI:
1028179645Smarcel	case SIOCDELMULTI:
1029179645Smarcel		BM_LOCK(sc);
1030179645Smarcel		bm_setladrf(sc);
1031179645Smarcel		BM_UNLOCK(sc);
1032179645Smarcel	case SIOCGIFMEDIA:
1033179645Smarcel	case SIOCSIFMEDIA:
1034179645Smarcel		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd);
1035179645Smarcel		break;
1036179645Smarcel	default:
1037179645Smarcel		error = ether_ioctl(ifp, cmd, data);
1038179645Smarcel		break;
1039179645Smarcel	}
1040179645Smarcel
1041179645Smarcel	return (error);
1042179645Smarcel}
1043179645Smarcel
1044179645Smarcelstatic void
1045179645Smarcelbm_setladrf(struct bm_softc *sc)
1046179645Smarcel{
1047179645Smarcel	struct ifnet *ifp = sc->sc_ifp;
1048179645Smarcel	struct ifmultiaddr *inm;
1049179645Smarcel	uint16_t hash[4];
1050179645Smarcel	uint16_t reg;
1051179645Smarcel	uint32_t crc;
1052179645Smarcel
1053179645Smarcel	reg = BM_CRC_ENABLE | BM_REJECT_OWN_PKTS;
1054179645Smarcel
1055179645Smarcel	/* Turn off RX MAC while we fiddle its settings */
1056179645Smarcel	CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
1057179645Smarcel	while (CSR_READ_2(sc, BM_RX_CONFIG) & BM_ENABLE)
1058179645Smarcel		DELAY(10);
1059179645Smarcel
1060179645Smarcel	if ((ifp->if_flags & IFF_PROMISC) != 0) {
1061179645Smarcel		reg |= BM_PROMISC;
1062179645Smarcel
1063179645Smarcel		CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
1064179645Smarcel
1065179645Smarcel		DELAY(15);
1066179645Smarcel
1067179645Smarcel		reg = CSR_READ_2(sc, BM_RX_CONFIG);
1068179645Smarcel		reg |= BM_ENABLE;
1069179645Smarcel		CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
1070179645Smarcel		return;
1071179645Smarcel	}
1072179645Smarcel
1073179645Smarcel	if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
1074179645Smarcel		hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
1075179645Smarcel	} else {
1076179645Smarcel		/* Clear the hash table. */
1077179645Smarcel		memset(hash, 0, sizeof(hash));
1078179645Smarcel
1079179645Smarcel		IF_ADDR_LOCK(ifp);
1080179645Smarcel		TAILQ_FOREACH(inm, &ifp->if_multiaddrs, ifma_link) {
1081179645Smarcel			if (inm->ifma_addr->sa_family != AF_LINK)
1082179645Smarcel				continue;
1083179645Smarcel			crc = ether_crc32_le(LLADDR((struct sockaddr_dl *)
1084179645Smarcel			    inm->ifma_addr), ETHER_ADDR_LEN);
1085179645Smarcel
1086179645Smarcel			/* We just want the 6 most significant bits */
1087179645Smarcel			crc >>= 26;
1088179645Smarcel
1089179645Smarcel			/* Set the corresponding bit in the filter. */
1090179645Smarcel			hash[crc >> 4] |= 1 << (crc & 0xf);
1091179645Smarcel		}
1092179645Smarcel		IF_ADDR_UNLOCK(ifp);
1093179645Smarcel	}
1094179645Smarcel
1095179645Smarcel	/* Write out new hash table */
1096179645Smarcel	CSR_WRITE_2(sc, BM_HASHTAB0, hash[0]);
1097179645Smarcel	CSR_WRITE_2(sc, BM_HASHTAB1, hash[1]);
1098179645Smarcel	CSR_WRITE_2(sc, BM_HASHTAB2, hash[2]);
1099179645Smarcel	CSR_WRITE_2(sc, BM_HASHTAB3, hash[3]);
1100179645Smarcel
1101179645Smarcel	/* And turn the RX MAC back on, this time with the hash bit set */
1102179645Smarcel	reg |= BM_HASH_FILTER_ENABLE;
1103179645Smarcel	CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
1104179645Smarcel
1105179645Smarcel	while (!(CSR_READ_2(sc, BM_RX_CONFIG) & BM_HASH_FILTER_ENABLE))
1106179645Smarcel		DELAY(10);
1107179645Smarcel
1108179645Smarcel	reg = CSR_READ_2(sc, BM_RX_CONFIG);
1109179645Smarcel	reg |= BM_ENABLE;
1110179645Smarcel	CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
1111179645Smarcel}
1112179645Smarcel
1113179645Smarcelstatic void
1114179645Smarcelbm_init(void *xsc)
1115179645Smarcel{
1116179645Smarcel	struct bm_softc *sc = xsc;
1117179645Smarcel
1118179645Smarcel	BM_LOCK(sc);
1119179645Smarcel	bm_init_locked(sc);
1120179645Smarcel	BM_UNLOCK(sc);
1121179645Smarcel}
1122179645Smarcel
1123179645Smarcelstatic void
1124179645Smarcelbm_chip_setup(struct bm_softc *sc)
1125179645Smarcel{
1126179645Smarcel	uint16_t reg;
1127179645Smarcel	uint16_t *eaddr_sect;
1128179645Smarcel	char hrow_path[128];
1129179645Smarcel	ihandle_t hrow_ih;
1130179645Smarcel
1131179645Smarcel	eaddr_sect = (uint16_t *)(sc->sc_enaddr);
1132179645Smarcel
1133179645Smarcel	/* Enable BMAC cell */
1134179645Smarcel	OF_package_to_path(OF_parent(ofw_bus_get_node(sc->sc_dev)),
1135179645Smarcel	    hrow_path, sizeof(hrow_path));
1136179645Smarcel	hrow_ih = OF_open(hrow_path);
1137179645Smarcel	if (hrow_ih == -1) {
1138179645Smarcel		device_printf(sc->sc_dev,
1139179645Smarcel		    "Enabling BMAC cell failed! Hoping it's already active.\n");
1140179645Smarcel	} else {
1141179645Smarcel		OF_call_method("enable-enet", hrow_ih, 0, 0);
1142179645Smarcel		OF_close(hrow_ih);
1143179645Smarcel	}
1144179645Smarcel
1145179645Smarcel	/* Reset chip */
1146179645Smarcel	CSR_WRITE_2(sc, BM_RX_RESET, 0x0000);
1147179645Smarcel	CSR_WRITE_2(sc, BM_TX_RESET, 0x0001);
1148179645Smarcel	do {
1149179645Smarcel		reg = CSR_READ_2(sc, BM_TX_RESET);
1150179645Smarcel	} while (reg & 0x0001);
1151179645Smarcel
1152179645Smarcel	/* Some random junk. OS X uses the system time. We use
1153179645Smarcel	 * the low 16 bits of the MAC address. */
1154179645Smarcel	CSR_WRITE_2(sc,	BM_TX_RANDSEED, eaddr_sect[2]);
1155179645Smarcel
1156179645Smarcel	/* Enable transmit */
1157179645Smarcel	reg = CSR_READ_2(sc, BM_TX_IFC);
1158179645Smarcel	reg |= BM_ENABLE;
1159179645Smarcel	CSR_WRITE_2(sc, BM_TX_IFC, reg);
1160179645Smarcel
1161179645Smarcel	CSR_READ_2(sc, BM_TX_PEAKCNT);
1162179645Smarcel}
1163179645Smarcel
1164179645Smarcelstatic void
1165179645Smarcelbm_stop(struct bm_softc *sc)
1166179645Smarcel{
1167179645Smarcel	struct bm_txsoft *txs;
1168179645Smarcel	uint16_t reg;
1169179645Smarcel
1170179645Smarcel	/* Disable TX and RX MACs */
1171179645Smarcel	reg = CSR_READ_2(sc, BM_TX_CONFIG);
1172179645Smarcel	reg &= ~BM_ENABLE;
1173179645Smarcel	CSR_WRITE_2(sc, BM_TX_CONFIG, reg);
1174179645Smarcel
1175179645Smarcel	reg = CSR_READ_2(sc, BM_RX_CONFIG);
1176179645Smarcel	reg &= ~BM_ENABLE;
1177179645Smarcel	CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
1178179645Smarcel
1179179645Smarcel	DELAY(100);
1180179645Smarcel
1181179645Smarcel	/* Stop DMA engine */
1182179645Smarcel	dbdma_stop(sc->sc_rxdma);
1183179645Smarcel	dbdma_stop(sc->sc_txdma);
1184179645Smarcel	sc->next_rxdma_slot = 0;
1185179645Smarcel	sc->rxdma_loop_slot = 0;
1186179645Smarcel
1187179645Smarcel	/* Disable interrupts */
1188179645Smarcel	bm_disable_interrupts(sc);
1189179645Smarcel
1190179645Smarcel	/* Don't worry about pending transmits anymore */
1191179645Smarcel	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1192179645Smarcel		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
1193179645Smarcel		if (txs->txs_ndescs != 0) {
1194179645Smarcel			bus_dmamap_sync(sc->sc_tdma_tag, txs->txs_dmamap,
1195179645Smarcel			    BUS_DMASYNC_POSTWRITE);
1196179645Smarcel			bus_dmamap_unload(sc->sc_tdma_tag, txs->txs_dmamap);
1197179645Smarcel			if (txs->txs_mbuf != NULL) {
1198179645Smarcel				m_freem(txs->txs_mbuf);
1199179645Smarcel				txs->txs_mbuf = NULL;
1200179645Smarcel			}
1201179645Smarcel		}
1202179645Smarcel		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1203179645Smarcel	}
1204179645Smarcel
1205179645Smarcel	/* And we're down */
1206179645Smarcel	sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1207179645Smarcel	sc->sc_wdog_timer = 0;
1208179645Smarcel}
1209179645Smarcel
1210179645Smarcelstatic void
1211179645Smarcelbm_init_locked(struct bm_softc *sc)
1212179645Smarcel{
1213179645Smarcel	uint16_t reg;
1214179645Smarcel	uint16_t *eaddr_sect;
1215179645Smarcel	struct bm_rxsoft *rxs;
1216179645Smarcel	int i;
1217179645Smarcel
1218179645Smarcel	eaddr_sect = (uint16_t *)(sc->sc_enaddr);
1219179645Smarcel
1220179645Smarcel	/* Zero RX slot info and stop DMA */
1221179645Smarcel	dbdma_stop(sc->sc_rxdma);
1222179645Smarcel	dbdma_stop(sc->sc_txdma);
1223179645Smarcel	sc->next_rxdma_slot = 0;
1224179645Smarcel	sc->rxdma_loop_slot = 0;
1225179645Smarcel
1226179645Smarcel	/* Initialize TX/RX DBDMA programs */
1227179645Smarcel	dbdma_insert_stop(sc->sc_rxdma, 0);
1228179645Smarcel	dbdma_insert_stop(sc->sc_txdma, 0);
1229179645Smarcel	dbdma_set_current_cmd(sc->sc_rxdma, 0);
1230179645Smarcel	dbdma_set_current_cmd(sc->sc_txdma, 0);
1231179645Smarcel
1232179645Smarcel	sc->next_rxdma_slot = 0;
1233179645Smarcel	sc->next_txdma_slot = 1;
1234179645Smarcel	sc->first_used_txdma_slot = 0;
1235179645Smarcel
1236179645Smarcel	for (i = 0; i < BM_MAX_RX_PACKETS; i++) {
1237179645Smarcel		rxs = &sc->sc_rxsoft[i];
1238179645Smarcel		rxs->dbdma_slot = i;
1239179645Smarcel
1240179645Smarcel		if (rxs->rxs_mbuf == NULL) {
1241179645Smarcel			bm_add_rxbuf(sc, i);
1242179645Smarcel
1243179645Smarcel			if (rxs->rxs_mbuf == NULL) {
1244179645Smarcel				/* If we can't add anymore, mark the problem */
1245179645Smarcel				rxs->dbdma_slot = -1;
1246179645Smarcel				break;
1247179645Smarcel			}
1248179645Smarcel		}
1249179645Smarcel
1250179645Smarcel		if (i > 0)
1251179645Smarcel			bm_add_rxbuf_dma(sc, i);
1252179645Smarcel	}
1253179645Smarcel
1254179645Smarcel	/*
1255179645Smarcel	 * Now terminate the RX ring buffer, and follow with the loop to
1256179645Smarcel	 * the beginning.
1257179645Smarcel	 */
1258179645Smarcel	dbdma_insert_stop(sc->sc_rxdma, i - 1);
1259179645Smarcel	dbdma_insert_branch(sc->sc_rxdma, i, 0);
1260179645Smarcel	sc->rxdma_loop_slot = i;
1261179645Smarcel
1262179645Smarcel	/* Now add in the first element of the RX DMA chain */
1263179645Smarcel	bm_add_rxbuf_dma(sc, 0);
1264179645Smarcel
1265179645Smarcel	dbdma_sync_commands(sc->sc_rxdma, BUS_DMASYNC_PREWRITE);
1266179645Smarcel	dbdma_sync_commands(sc->sc_txdma, BUS_DMASYNC_PREWRITE);
1267179645Smarcel
1268179645Smarcel	/* Zero collision counters */
1269179645Smarcel	CSR_WRITE_2(sc, BM_TX_NCCNT, 0);
1270179645Smarcel	CSR_WRITE_2(sc, BM_TX_FCCNT, 0);
1271179645Smarcel	CSR_WRITE_2(sc, BM_TX_EXCNT, 0);
1272179645Smarcel	CSR_WRITE_2(sc, BM_TX_LTCNT, 0);
1273179645Smarcel
1274179645Smarcel	/* Zero receive counters */
1275179645Smarcel	CSR_WRITE_2(sc, BM_RX_FRCNT, 0);
1276179645Smarcel	CSR_WRITE_2(sc, BM_RX_LECNT, 0);
1277179645Smarcel	CSR_WRITE_2(sc, BM_RX_AECNT, 0);
1278179645Smarcel	CSR_WRITE_2(sc, BM_RX_FECNT, 0);
1279179645Smarcel	CSR_WRITE_2(sc, BM_RXCV, 0);
1280179645Smarcel
1281179645Smarcel	/* Prime transmit */
1282179645Smarcel	CSR_WRITE_2(sc, BM_TX_THRESH, 0xff);
1283179645Smarcel
1284179645Smarcel	CSR_WRITE_2(sc, BM_TXFIFO_CSR, 0);
1285179645Smarcel	CSR_WRITE_2(sc, BM_TXFIFO_CSR, 0x0001);
1286179645Smarcel
1287179645Smarcel	/* Prime receive */
1288179645Smarcel	CSR_WRITE_2(sc, BM_RXFIFO_CSR, 0);
1289179645Smarcel	CSR_WRITE_2(sc, BM_RXFIFO_CSR, 0x0001);
1290179645Smarcel
1291179645Smarcel	/* Clear status reg */
1292179645Smarcel	CSR_READ_2(sc, BM_STATUS);
1293179645Smarcel
1294179645Smarcel	/* Zero hash filters */
1295179645Smarcel	CSR_WRITE_2(sc, BM_HASHTAB0, 0);
1296179645Smarcel	CSR_WRITE_2(sc, BM_HASHTAB1, 0);
1297179645Smarcel	CSR_WRITE_2(sc, BM_HASHTAB2, 0);
1298179645Smarcel	CSR_WRITE_2(sc, BM_HASHTAB3, 0);
1299179645Smarcel
1300179645Smarcel	/* Write MAC address to chip */
1301179645Smarcel	CSR_WRITE_2(sc, BM_MACADDR0, eaddr_sect[0]);
1302179645Smarcel	CSR_WRITE_2(sc, BM_MACADDR1, eaddr_sect[1]);
1303179645Smarcel	CSR_WRITE_2(sc, BM_MACADDR2, eaddr_sect[2]);
1304179645Smarcel
1305179645Smarcel	/* Final receive engine setup */
1306179645Smarcel	reg = BM_CRC_ENABLE | BM_REJECT_OWN_PKTS | BM_HASH_FILTER_ENABLE;
1307179645Smarcel	CSR_WRITE_2(sc, BM_RX_CONFIG, reg);
1308179645Smarcel
1309179645Smarcel	/* Now turn it all on! */
1310179645Smarcel	dbdma_reset(sc->sc_rxdma);
1311179645Smarcel	dbdma_reset(sc->sc_txdma);
1312179645Smarcel
1313179645Smarcel	/* Enable RX and TX MACs. Setting the address filter has
1314179645Smarcel	 * the side effect of enabling the RX MAC. */
1315179645Smarcel	bm_setladrf(sc);
1316179645Smarcel
1317179645Smarcel	reg = CSR_READ_2(sc, BM_TX_CONFIG);
1318179645Smarcel	reg |= BM_ENABLE;
1319179645Smarcel	CSR_WRITE_2(sc, BM_TX_CONFIG, reg);
1320179645Smarcel
1321179645Smarcel	/*
1322179645Smarcel	 * Enable interrupts, unwedge the controller with a dummy packet,
1323179645Smarcel	 * and nudge the DMA queue.
1324179645Smarcel	 */
1325179645Smarcel	bm_enable_interrupts(sc);
1326179645Smarcel	bm_dummypacket(sc);
1327179645Smarcel	dbdma_wake(sc->sc_rxdma); /* Nudge RXDMA */
1328179645Smarcel
1329179645Smarcel	sc->sc_ifp->if_drv_flags |= IFF_DRV_RUNNING;
1330179645Smarcel	sc->sc_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1331179645Smarcel	sc->sc_ifpflags = sc->sc_ifp->if_flags;
1332179645Smarcel
1333179645Smarcel	/* Resync PHY and MAC states */
1334179645Smarcel	sc->sc_mii = device_get_softc(sc->sc_miibus);
1335179645Smarcel	sc->sc_duplex = ~IFM_FDX;
1336179645Smarcel	mii_mediachg(sc->sc_mii);
1337179645Smarcel
1338179645Smarcel	/* Start the one second timer. */
1339179645Smarcel	sc->sc_wdog_timer = 0;
1340179645Smarcel	callout_reset(&sc->sc_tick_ch, hz, bm_tick, sc);
1341179645Smarcel}
1342179645Smarcel
1343179645Smarcelstatic void
1344179645Smarcelbm_tick(void *arg)
1345179645Smarcel{
1346179645Smarcel	struct bm_softc *sc = arg;
1347179645Smarcel
1348179645Smarcel	/* Read error counters */
1349179645Smarcel	sc->sc_ifp->if_collisions += CSR_READ_2(sc, BM_TX_NCCNT) +
1350179645Smarcel	    CSR_READ_2(sc, BM_TX_FCCNT) + CSR_READ_2(sc, BM_TX_EXCNT) +
1351179645Smarcel	    CSR_READ_2(sc, BM_TX_LTCNT);
1352179645Smarcel
1353179645Smarcel	sc->sc_ifp->if_ierrors += CSR_READ_2(sc, BM_RX_LECNT) +
1354179645Smarcel	    CSR_READ_2(sc, BM_RX_AECNT) + CSR_READ_2(sc, BM_RX_FECNT);
1355179645Smarcel
1356179645Smarcel	/* Zero collision counters */
1357179645Smarcel	CSR_WRITE_2(sc, BM_TX_NCCNT, 0);
1358179645Smarcel	CSR_WRITE_2(sc, BM_TX_FCCNT, 0);
1359179645Smarcel	CSR_WRITE_2(sc, BM_TX_EXCNT, 0);
1360179645Smarcel	CSR_WRITE_2(sc, BM_TX_LTCNT, 0);
1361179645Smarcel
1362179645Smarcel	/* Zero receive counters */
1363179645Smarcel	CSR_WRITE_2(sc, BM_RX_FRCNT, 0);
1364179645Smarcel	CSR_WRITE_2(sc, BM_RX_LECNT, 0);
1365179645Smarcel	CSR_WRITE_2(sc, BM_RX_AECNT, 0);
1366179645Smarcel	CSR_WRITE_2(sc, BM_RX_FECNT, 0);
1367179645Smarcel	CSR_WRITE_2(sc, BM_RXCV, 0);
1368179645Smarcel
1369179645Smarcel	/* Check for link changes and run watchdog */
1370179645Smarcel	mii_tick(sc->sc_mii);
1371179645Smarcel	bm_miibus_statchg(sc->sc_dev);
1372179645Smarcel
1373179645Smarcel	if (bm_watchdog(sc) == EJUSTRETURN)
1374179645Smarcel		return;
1375179645Smarcel
1376179645Smarcel	callout_reset(&sc->sc_tick_ch, hz, bm_tick, sc);
1377179645Smarcel}
1378179645Smarcel
1379179645Smarcelstatic int
1380179645Smarcelbm_watchdog(struct bm_softc *sc)
1381179645Smarcel{
1382179645Smarcel	if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0)
1383179645Smarcel		return (0);
1384179645Smarcel
1385179645Smarcel	device_printf(sc->sc_dev, "device timeout\n");
1386179645Smarcel
1387179645Smarcel	bm_init_locked(sc);
1388179645Smarcel	return (EJUSTRETURN);
1389179645Smarcel}
1390179645Smarcel
1391179645Smarcelstatic int
1392179645Smarcelbm_add_rxbuf(struct bm_softc *sc, int idx)
1393179645Smarcel{
1394179645Smarcel	struct bm_rxsoft *rxs = &sc->sc_rxsoft[idx];
1395179645Smarcel	struct mbuf *m;
1396179645Smarcel	bus_dma_segment_t segs[1];
1397179645Smarcel	int error, nsegs;
1398179645Smarcel
1399179645Smarcel	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1400179645Smarcel	if (m == NULL)
1401179645Smarcel		return (ENOBUFS);
1402179645Smarcel	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
1403179645Smarcel
1404179645Smarcel	if (rxs->rxs_mbuf != NULL) {
1405179645Smarcel		bus_dmamap_sync(sc->sc_rdma_tag, rxs->rxs_dmamap,
1406179645Smarcel		    BUS_DMASYNC_POSTREAD);
1407179645Smarcel		bus_dmamap_unload(sc->sc_rdma_tag, rxs->rxs_dmamap);
1408179645Smarcel	}
1409179645Smarcel
1410179645Smarcel	error = bus_dmamap_load_mbuf_sg(sc->sc_rdma_tag, rxs->rxs_dmamap, m,
1411179645Smarcel	    segs, &nsegs, BUS_DMA_NOWAIT);
1412179645Smarcel	if (error != 0) {
1413179645Smarcel		device_printf(sc->sc_dev,
1414179645Smarcel		    "cannot load RS DMA map %d, error = %d\n", idx, error);
1415179645Smarcel		m_freem(m);
1416179645Smarcel		return (error);
1417179645Smarcel	}
1418179645Smarcel	/* If nsegs is wrong then the stack is corrupt. */
1419179645Smarcel	KASSERT(nsegs == 1,
1420179645Smarcel	    ("%s: too many DMA segments (%d)", __func__, nsegs));
1421179645Smarcel	rxs->rxs_mbuf = m;
1422179645Smarcel	rxs->segment = segs[0];
1423179645Smarcel
1424179645Smarcel	bus_dmamap_sync(sc->sc_rdma_tag, rxs->rxs_dmamap, BUS_DMASYNC_PREREAD);
1425179645Smarcel
1426179645Smarcel	return (0);
1427179645Smarcel}
1428179645Smarcel
1429179645Smarcelstatic int
1430179645Smarcelbm_add_rxbuf_dma(struct bm_softc *sc, int idx)
1431179645Smarcel{
1432179645Smarcel	struct bm_rxsoft *rxs = &sc->sc_rxsoft[idx];
1433179645Smarcel
1434179645Smarcel	dbdma_insert_command(sc->sc_rxdma, idx, DBDMA_INPUT_LAST, 0,
1435179645Smarcel	    rxs->segment.ds_addr, rxs->segment.ds_len, DBDMA_ALWAYS,
1436179645Smarcel	    DBDMA_NEVER, DBDMA_NEVER, 0);
1437179645Smarcel
1438179645Smarcel	return (0);
1439179645Smarcel}
1440179645Smarcel
1441179645Smarcelstatic void
1442179645Smarcelbm_enable_interrupts(struct bm_softc *sc)
1443179645Smarcel{
1444179645Smarcel	CSR_WRITE_2(sc, BM_INTR_DISABLE,
1445179645Smarcel	    (sc->sc_streaming) ? BM_INTR_NONE : BM_INTR_NORMAL);
1446179645Smarcel}
1447179645Smarcel
1448179645Smarcelstatic void
1449179645Smarcelbm_disable_interrupts(struct bm_softc *sc)
1450179645Smarcel{
1451179645Smarcel	CSR_WRITE_2(sc, BM_INTR_DISABLE, BM_INTR_NONE);
1452179645Smarcel}
1453