xref: /netbsd-src/sys/arch/macppc/dev/if_bm.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: if_bm.c,v 1.54 2018/06/26 06:47:58 msaitoh Exp $	*/
2 
3 /*-
4  * Copyright (C) 1998, 1999, 2000 Tsubai Masanari.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: if_bm.c,v 1.54 2018/06/26 06:47:58 msaitoh Exp $");
31 
32 #include "opt_inet.h"
33 
34 #include <sys/param.h>
35 #include <sys/device.h>
36 #include <sys/ioctl.h>
37 #include <sys/kernel.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/systm.h>
41 #include <sys/callout.h>
42 
43 #include <uvm/uvm_extern.h>
44 
45 #include <net/if.h>
46 #include <net/if_dl.h>
47 #include <net/if_ether.h>
48 #include <net/if_media.h>
49 
50 #include <net/bpf.h>
51 
52 #ifdef INET
53 #include <netinet/in.h>
54 #include <netinet/if_inarp.h>
55 #endif
56 
57 
58 #include <dev/ofw/openfirm.h>
59 
60 #include <dev/mii/mii.h>
61 #include <dev/mii/miivar.h>
62 #include <dev/mii/mii_bitbang.h>
63 
64 #include <powerpc/spr.h>
65 #include <powerpc/oea/spr.h>
66 
67 #include <machine/autoconf.h>
68 #include <machine/pio.h>
69 
70 #include <macppc/dev/dbdma.h>
71 #include <macppc/dev/if_bmreg.h>
72 #include <macppc/dev/obiovar.h>
73 
74 #define BMAC_TXBUFS 2
75 #define BMAC_RXBUFS 16
76 #define BMAC_BUFLEN 2048
77 
78 struct bmac_softc {
79 	device_t sc_dev;
80 	struct ethercom sc_ethercom;
81 #define sc_if sc_ethercom.ec_if
82 	struct callout sc_tick_ch;
83 	bus_space_tag_t sc_iot;
84 	bus_space_handle_t sc_ioh;
85 	dbdma_regmap_t *sc_txdma;
86 	dbdma_regmap_t *sc_rxdma;
87 	dbdma_command_t *sc_txcmd;
88 	dbdma_command_t *sc_rxcmd;
89 	void *sc_txbuf;
90 	void *sc_rxbuf;
91 	int sc_rxlast;
92 	int sc_flags;
93 	struct mii_data sc_mii;
94 	u_char sc_enaddr[6];
95 };
96 
97 #define BMAC_BMACPLUS	0x01
98 #define BMAC_DEBUGFLAG	0x02
99 
100 int bmac_match(device_t, cfdata_t, void *);
101 void bmac_attach(device_t, device_t, void *);
102 void bmac_reset_chip(struct bmac_softc *);
103 void bmac_init(struct bmac_softc *);
104 void bmac_init_dma(struct bmac_softc *);
105 int bmac_intr(void *);
106 int bmac_rint(void *);
107 void bmac_reset(struct bmac_softc *);
108 void bmac_stop(struct bmac_softc *);
109 void bmac_start(struct ifnet *);
110 void bmac_transmit_packet(struct bmac_softc *, void *, int);
111 int bmac_put(struct bmac_softc *, void *, struct mbuf *);
112 struct mbuf *bmac_get(struct bmac_softc *, void *, int);
113 void bmac_watchdog(struct ifnet *);
114 int bmac_ioctl(struct ifnet *, u_long, void *);
115 void bmac_setladrf(struct bmac_softc *);
116 
117 int bmac_mii_readreg(device_t, int, int);
118 void bmac_mii_writereg(device_t, int, int, int);
119 void bmac_mii_statchg(struct ifnet *);
120 void bmac_mii_tick(void *);
121 u_int32_t bmac_mbo_read(device_t);
122 void bmac_mbo_write(device_t, u_int32_t);
123 
124 CFATTACH_DECL_NEW(bm, sizeof(struct bmac_softc),
125     bmac_match, bmac_attach, NULL, NULL);
126 
127 const struct mii_bitbang_ops bmac_mbo = {
128 	bmac_mbo_read, bmac_mbo_write,
129 	{ MIFDO, MIFDI, MIFDC, MIFDIR, 0 }
130 };
131 
132 static inline uint16_t
133 bmac_read_reg(struct bmac_softc *sc, bus_size_t off)
134 {
135 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, off);
136 }
137 
138 static inline void
139 bmac_write_reg(struct bmac_softc *sc, bus_size_t off, uint16_t val)
140 {
141 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, off, val);
142 }
143 
144 static inline void
145 bmac_set_bits(struct bmac_softc *sc, bus_size_t off, uint16_t val)
146 {
147 	val |= bmac_read_reg(sc, off);
148 	bmac_write_reg(sc, off, val);
149 }
150 
151 static inline void
152 bmac_reset_bits(struct bmac_softc *sc, bus_size_t off, uint16_t val)
153 {
154 	bmac_write_reg(sc, off, bmac_read_reg(sc, off) & ~val);
155 }
156 
157 int
158 bmac_match(device_t parent, cfdata_t cf, void *aux)
159 {
160 	struct confargs *ca = aux;
161 
162 	if (ca->ca_nreg < 24 || ca->ca_nintr < 12)
163 		return 0;
164 
165 	if (strcmp(ca->ca_name, "bmac") == 0)		/* bmac */
166 		return 1;
167 	if (strcmp(ca->ca_name, "ethernet") == 0)	/* bmac+ */
168 		return 1;
169 
170 	return 0;
171 }
172 
173 void
174 bmac_attach(device_t parent, device_t self, void *aux)
175 {
176 	struct confargs *ca = aux;
177 	struct bmac_softc *sc = device_private(self);
178 	struct ifnet *ifp = &sc->sc_if;
179 	struct mii_data *mii = &sc->sc_mii;
180 	u_char laddr[6];
181 
182 	callout_init(&sc->sc_tick_ch, 0);
183 
184 	sc->sc_dev = self;
185 	sc->sc_flags = 0;
186 	if (strcmp(ca->ca_name, "ethernet") == 0) {
187 		char name[64];
188 
189 		memset(name, 0, 64);
190 		OF_package_to_path(ca->ca_node, name, sizeof(name));
191 		OF_open(name);
192 		sc->sc_flags |= BMAC_BMACPLUS;
193 	}
194 
195 	ca->ca_reg[0] += ca->ca_baseaddr;
196 	ca->ca_reg[2] += ca->ca_baseaddr;
197 	ca->ca_reg[4] += ca->ca_baseaddr;
198 
199 	sc->sc_iot = ca->ca_tag;
200 	if (bus_space_map(sc->sc_iot, ca->ca_reg[0], ca->ca_reg[1], 0,
201 	    &sc->sc_ioh) != 0) {
202 		aprint_error(": couldn't map %#x", ca->ca_reg[0]);
203 		return;
204 	}
205 
206 	bmac_write_reg(sc, INTDISABLE, NoEventsMask);
207 
208 	if (OF_getprop(ca->ca_node, "local-mac-address", laddr, 6) == -1 &&
209 	    OF_getprop(ca->ca_node, "mac-address", laddr, 6) == -1) {
210 		aprint_error(": cannot get mac-address\n");
211 		return;
212 	}
213 	memcpy(sc->sc_enaddr, laddr, 6);
214 
215 	sc->sc_txdma = mapiodev(ca->ca_reg[2], PAGE_SIZE, false);
216 	sc->sc_rxdma = mapiodev(ca->ca_reg[4], PAGE_SIZE, false);
217 	sc->sc_txcmd = dbdma_alloc(BMAC_TXBUFS * sizeof(dbdma_command_t), NULL);
218 	sc->sc_rxcmd = dbdma_alloc((BMAC_RXBUFS + 1) * sizeof(dbdma_command_t),
219 	    NULL);
220 	sc->sc_txbuf = malloc(BMAC_BUFLEN * BMAC_TXBUFS, M_DEVBUF, M_NOWAIT);
221 	sc->sc_rxbuf = malloc(BMAC_BUFLEN * BMAC_RXBUFS, M_DEVBUF, M_NOWAIT);
222 	if (sc->sc_txbuf == NULL || sc->sc_rxbuf == NULL ||
223 	    sc->sc_txcmd == NULL || sc->sc_rxcmd == NULL) {
224 		aprint_error("cannot allocate memory\n");
225 		return;
226 	}
227 
228 	aprint_normal(" irq %d,%d: address %s\n",
229 	    ca->ca_intr[0], ca->ca_intr[2],
230 	    ether_sprintf(laddr));
231 
232 	intr_establish(ca->ca_intr[0], IST_EDGE, IPL_NET, bmac_intr, sc);
233 	intr_establish(ca->ca_intr[2], IST_EDGE, IPL_NET, bmac_rint, sc);
234 
235 	memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
236 	ifp->if_softc = sc;
237 	ifp->if_ioctl = bmac_ioctl;
238 	ifp->if_start = bmac_start;
239 	ifp->if_flags =
240 		IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
241 	ifp->if_watchdog = bmac_watchdog;
242 	IFQ_SET_READY(&ifp->if_snd);
243 
244 	mii->mii_ifp = ifp;
245 	mii->mii_readreg = bmac_mii_readreg;
246 	mii->mii_writereg = bmac_mii_writereg;
247 	mii->mii_statchg = bmac_mii_statchg;
248 
249 	sc->sc_ethercom.ec_mii = mii;
250 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
251 	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
252 
253 	/* Choose a default media. */
254 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
255 		ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_10_T, 0, NULL);
256 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_10_T);
257 	} else
258 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
259 
260 	bmac_reset_chip(sc);
261 
262 	if_attach(ifp);
263 	if_deferred_start_init(ifp, NULL);
264 	ether_ifattach(ifp, sc->sc_enaddr);
265 }
266 
267 /*
268  * Reset and enable bmac by heathrow FCR.
269  */
270 void
271 bmac_reset_chip(struct bmac_softc *sc)
272 {
273 	u_int v;
274 
275 	dbdma_reset(sc->sc_txdma);
276 	dbdma_reset(sc->sc_rxdma);
277 
278 	v = obio_read_4(HEATHROW_FCR);
279 
280 	v |= EnetEnable;
281 	obio_write_4(HEATHROW_FCR, v);
282 	delay(50000);
283 
284 	v |= ResetEnetCell;
285 	obio_write_4(HEATHROW_FCR, v);
286 	delay(50000);
287 
288 	v &= ~ResetEnetCell;
289 	obio_write_4(HEATHROW_FCR, v);
290 	delay(50000);
291 
292 	obio_write_4(HEATHROW_FCR, v);
293 }
294 
295 void
296 bmac_init(struct bmac_softc *sc)
297 {
298 	struct ifnet *ifp = &sc->sc_if;
299 	struct ether_header *eh;
300 	void *data;
301 	int i, tb, bmcr;
302 	u_short *p;
303 
304 	bmac_reset_chip(sc);
305 
306 	/* XXX */
307 	bmcr = bmac_mii_readreg(sc->sc_dev, 0, MII_BMCR);
308 	bmcr &= ~BMCR_ISO;
309 	bmac_mii_writereg(sc->sc_dev, 0, MII_BMCR, bmcr);
310 
311 	bmac_write_reg(sc, RXRST, RxResetValue);
312 	bmac_write_reg(sc, TXRST, TxResetBit);
313 
314 	/* Wait for reset completion. */
315 	for (i = 1000; i > 0; i -= 10) {
316 		if ((bmac_read_reg(sc, TXRST) & TxResetBit) == 0)
317 			break;
318 		delay(10);
319 	}
320 	if (i <= 0)
321 		printf("%s: reset timeout\n", ifp->if_xname);
322 
323 	if (! (sc->sc_flags & BMAC_BMACPLUS))
324 		bmac_set_bits(sc, XCVRIF, ClkBit|SerialMode|COLActiveLow);
325 
326 	if ((mfpvr() >> 16) == MPC601)
327 		tb = mfrtcl();
328 	else
329 		tb = mftbl();
330 	bmac_write_reg(sc, RSEED, tb);
331 	bmac_set_bits(sc, XIFC, TxOutputEnable);
332 	bmac_read_reg(sc, PAREG);
333 
334 	/* Reset various counters. */
335 	bmac_write_reg(sc, NCCNT, 0);
336 	bmac_write_reg(sc, NTCNT, 0);
337 	bmac_write_reg(sc, EXCNT, 0);
338 	bmac_write_reg(sc, LTCNT, 0);
339 	bmac_write_reg(sc, FRCNT, 0);
340 	bmac_write_reg(sc, LECNT, 0);
341 	bmac_write_reg(sc, AECNT, 0);
342 	bmac_write_reg(sc, FECNT, 0);
343 	bmac_write_reg(sc, RXCV, 0);
344 
345 	/* Set tx fifo information. */
346 	bmac_write_reg(sc, TXTH, 4);	/* 4 octets before tx starts */
347 
348 	bmac_write_reg(sc, TXFIFOCSR, 0);
349 	bmac_write_reg(sc, TXFIFOCSR, TxFIFOEnable);
350 
351 	/* Set rx fifo information. */
352 	bmac_write_reg(sc, RXFIFOCSR, 0);
353 	bmac_write_reg(sc, RXFIFOCSR, RxFIFOEnable);
354 
355 	/* Clear status register. */
356 	bmac_read_reg(sc, STATUS);
357 
358 	bmac_write_reg(sc, HASH3, 0);
359 	bmac_write_reg(sc, HASH2, 0);
360 	bmac_write_reg(sc, HASH1, 0);
361 	bmac_write_reg(sc, HASH0, 0);
362 
363 	/* Set MAC address. */
364 	p = (u_short *)sc->sc_enaddr;
365 	bmac_write_reg(sc, MADD0, *p++);
366 	bmac_write_reg(sc, MADD1, *p++);
367 	bmac_write_reg(sc, MADD2, *p);
368 
369 	bmac_write_reg(sc, RXCFG,
370 		RxCRCEnable | RxHashFilterEnable | RxRejectOwnPackets);
371 
372 	if (ifp->if_flags & IFF_PROMISC)
373 		bmac_set_bits(sc, RXCFG, RxPromiscEnable);
374 
375 	bmac_init_dma(sc);
376 
377 	/* Enable TX/RX */
378 	bmac_set_bits(sc, RXCFG, RxMACEnable);
379 	bmac_set_bits(sc, TXCFG, TxMACEnable);
380 
381 	bmac_write_reg(sc, INTDISABLE, NormalIntEvents);
382 
383 	ifp->if_flags |= IFF_RUNNING;
384 	ifp->if_flags &= ~IFF_OACTIVE;
385 	ifp->if_timer = 0;
386 
387 	data = sc->sc_txbuf;
388 	eh = (struct ether_header *)data;
389 
390 	memset(data, 0, sizeof(eh) + ETHERMIN);
391 	memcpy(eh->ether_dhost, sc->sc_enaddr, ETHER_ADDR_LEN);
392 	memcpy(eh->ether_shost, sc->sc_enaddr, ETHER_ADDR_LEN);
393 	bmac_transmit_packet(sc, data, sizeof(eh) + ETHERMIN);
394 
395 	bmac_start(ifp);
396 
397 	callout_reset(&sc->sc_tick_ch, hz, bmac_mii_tick, sc);
398 }
399 
400 void
401 bmac_init_dma(struct bmac_softc *sc)
402 {
403 	dbdma_command_t *cmd = sc->sc_rxcmd;
404 	int i;
405 
406 	dbdma_reset(sc->sc_txdma);
407 	dbdma_reset(sc->sc_rxdma);
408 
409 	memset(sc->sc_txcmd, 0, BMAC_TXBUFS * sizeof(dbdma_command_t));
410 	memset(sc->sc_rxcmd, 0, (BMAC_RXBUFS + 1) * sizeof(dbdma_command_t));
411 
412 	for (i = 0; i < BMAC_RXBUFS; i++) {
413 		DBDMA_BUILD(cmd, DBDMA_CMD_IN_LAST, 0, BMAC_BUFLEN,
414 			vtophys((vaddr_t)sc->sc_rxbuf + BMAC_BUFLEN * i),
415 			DBDMA_INT_ALWAYS, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
416 		cmd++;
417 	}
418 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
419 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
420 	out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_rxcmd));
421 
422 	sc->sc_rxlast = 0;
423 
424 	dbdma_start(sc->sc_rxdma, sc->sc_rxcmd);
425 }
426 
427 int
428 bmac_intr(void *v)
429 {
430 	struct bmac_softc *sc = v;
431 	int stat;
432 
433 	stat = bmac_read_reg(sc, STATUS);
434 	if (stat == 0)
435 		return 0;
436 
437 #ifdef BMAC_DEBUG
438 	printf("bmac_intr status = 0x%x\n", stat);
439 #endif
440 
441 	if (stat & IntFrameSent) {
442 		sc->sc_if.if_flags &= ~IFF_OACTIVE;
443 		sc->sc_if.if_timer = 0;
444 		sc->sc_if.if_opackets++;
445 		if_schedule_deferred_start(&sc->sc_if);
446 	}
447 
448 	/* XXX should do more! */
449 
450 	return 1;
451 }
452 
453 int
454 bmac_rint(void *v)
455 {
456 	struct bmac_softc *sc = v;
457 	struct ifnet *ifp = &sc->sc_if;
458 	struct mbuf *m;
459 	dbdma_command_t *cmd;
460 	int status, resid, count, datalen;
461 	int i, n;
462 	void *data;
463 
464 	i = sc->sc_rxlast;
465 	for (n = 0; n < BMAC_RXBUFS; n++, i++) {
466 		if (i == BMAC_RXBUFS)
467 			i = 0;
468 		cmd = &sc->sc_rxcmd[i];
469 		status = in16rb(&cmd->d_status);
470 		resid = in16rb(&cmd->d_resid);
471 
472 #ifdef BMAC_DEBUG
473 		if (status != 0 && status != 0x8440 && status != 0x9440)
474 			printf("bmac_rint status = 0x%x\n", status);
475 #endif
476 
477 		if ((status & DBDMA_CNTRL_ACTIVE) == 0)	/* 0x9440 | 0x8440 */
478 			continue;
479 		count = in16rb(&cmd->d_count);
480 		datalen = count - resid - 2;		/* 2 == framelen */
481 		if (datalen < sizeof(struct ether_header)) {
482 			printf("%s: short packet len = %d\n",
483 				ifp->if_xname, datalen);
484 			goto next;
485 		}
486 		DBDMA_BUILD_CMD(cmd, DBDMA_CMD_STOP, 0, 0, 0, 0);
487 		data = (char *)sc->sc_rxbuf + BMAC_BUFLEN * i;
488 
489 		/* XXX Sometimes bmac reads one extra byte. */
490 		if (datalen == ETHER_MAX_LEN + 1)
491 			datalen--;
492 
493 		/* Trim the CRC. */
494 		datalen -= ETHER_CRC_LEN;
495 
496 		m = bmac_get(sc, data, datalen);
497 		if (m == NULL) {
498 			ifp->if_ierrors++;
499 			goto next;
500 		}
501 
502 		if_percpuq_enqueue(ifp->if_percpuq, m);
503 
504 next:
505 		DBDMA_BUILD_CMD(cmd, DBDMA_CMD_IN_LAST, 0, DBDMA_INT_ALWAYS,
506 			DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
507 
508 		cmd->d_status = 0;
509 		cmd->d_resid = 0;
510 		sc->sc_rxlast = i + 1;
511 	}
512 	ether_mediachange(ifp);
513 
514 	dbdma_continue(sc->sc_rxdma);
515 
516 	return 1;
517 }
518 
519 void
520 bmac_reset(struct bmac_softc *sc)
521 {
522 	int s;
523 
524 	s = splnet();
525 	bmac_init(sc);
526 	splx(s);
527 }
528 
529 void
530 bmac_stop(struct bmac_softc *sc)
531 {
532 	struct ifnet *ifp = &sc->sc_if;
533 	int s;
534 
535 	s = splnet();
536 
537 	callout_stop(&sc->sc_tick_ch);
538 	mii_down(&sc->sc_mii);
539 
540 	/* Disable TX/RX. */
541 	bmac_reset_bits(sc, TXCFG, TxMACEnable);
542 	bmac_reset_bits(sc, RXCFG, RxMACEnable);
543 
544 	/* Disable all interrupts. */
545 	bmac_write_reg(sc, INTDISABLE, NoEventsMask);
546 
547 	dbdma_stop(sc->sc_txdma);
548 	dbdma_stop(sc->sc_rxdma);
549 
550 	ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
551 	ifp->if_timer = 0;
552 
553 	splx(s);
554 }
555 
556 void
557 bmac_start(struct ifnet *ifp)
558 {
559 	struct bmac_softc *sc = ifp->if_softc;
560 	struct mbuf *m;
561 	int tlen;
562 
563 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
564 		return;
565 
566 	while (1) {
567 		if (ifp->if_flags & IFF_OACTIVE)
568 			return;
569 
570 		IFQ_DEQUEUE(&ifp->if_snd, m);
571 		if (m == 0)
572 			break;
573 		/*
574 		 * If BPF is listening on this interface, let it see the
575 		 * packet before we commit it to the wire.
576 		 */
577 		bpf_mtap(ifp, m, BPF_D_OUT);
578 
579 		ifp->if_flags |= IFF_OACTIVE;
580 		tlen = bmac_put(sc, sc->sc_txbuf, m);
581 
582 		/* 5 seconds to watch for failing to transmit */
583 		ifp->if_timer = 5;
584 		ifp->if_opackets++;		/* # of pkts */
585 
586 		bmac_transmit_packet(sc, sc->sc_txbuf, tlen);
587 	}
588 }
589 
590 void
591 bmac_transmit_packet(struct bmac_softc *sc, void *buff, int len)
592 {
593 	dbdma_command_t *cmd = sc->sc_txcmd;
594 	vaddr_t va = (vaddr_t)buff;
595 
596 #ifdef BMAC_DEBUG
597 	if (vtophys(va) + len - 1 != vtophys(va + len - 1))
598 		panic("bmac_transmit_packet");
599 #endif
600 
601 	DBDMA_BUILD(cmd, DBDMA_CMD_OUT_LAST, 0, len, vtophys(va),
602 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
603 	cmd++;
604 	DBDMA_BUILD(cmd, DBDMA_CMD_STOP, 0, 0, 0,
605 		DBDMA_INT_ALWAYS, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
606 
607 	dbdma_start(sc->sc_txdma, sc->sc_txcmd);
608 }
609 
610 int
611 bmac_put(struct bmac_softc *sc, void *buff, struct mbuf *m)
612 {
613 	struct mbuf *n;
614 	int len, tlen = 0;
615 
616 	for (; m; m = n) {
617 		len = m->m_len;
618 		if (len == 0) {
619 			n = m_free(m);
620 			continue;
621 		}
622 		memcpy(buff, mtod(m, void *), len);
623 		buff = (char *)buff + len;
624 		tlen += len;
625 		n = m_free(m);
626 	}
627 	if (tlen > PAGE_SIZE)
628 		panic("%s: putpacket packet overflow",
629 		    device_xname(sc->sc_dev));
630 
631 	return tlen;
632 }
633 
634 struct mbuf *
635 bmac_get(struct bmac_softc *sc, void *pkt, int totlen)
636 {
637 	struct mbuf *m;
638 	struct mbuf *top, **mp;
639 	int len;
640 
641 	MGETHDR(m, M_DONTWAIT, MT_DATA);
642 	if (m == 0)
643 		return 0;
644 	m_set_rcvif(m, &sc->sc_if);
645 	m->m_pkthdr.len = totlen;
646 	len = MHLEN;
647 	top = 0;
648 	mp = &top;
649 
650 	while (totlen > 0) {
651 		if (top) {
652 			MGET(m, M_DONTWAIT, MT_DATA);
653 			if (m == 0) {
654 				m_freem(top);
655 				return 0;
656 			}
657 			len = MLEN;
658 		}
659 		if (totlen >= MINCLSIZE) {
660 			MCLGET(m, M_DONTWAIT);
661 			if ((m->m_flags & M_EXT) == 0) {
662 				m_free(m);
663 				m_freem(top);
664 				return 0;
665 			}
666 			len = MCLBYTES;
667 		}
668 		m->m_len = len = min(totlen, len);
669 		memcpy(mtod(m, void *), pkt, len);
670 		pkt = (char *)pkt + len;
671 		totlen -= len;
672 		*mp = m;
673 		mp = &m->m_next;
674 	}
675 
676 	return top;
677 }
678 
679 void
680 bmac_watchdog(struct ifnet *ifp)
681 {
682 	struct bmac_softc *sc = ifp->if_softc;
683 
684 	bmac_reset_bits(sc, RXCFG, RxMACEnable);
685 	bmac_reset_bits(sc, TXCFG, TxMACEnable);
686 
687 	printf("%s: device timeout\n", ifp->if_xname);
688 	ifp->if_oerrors++;
689 
690 	bmac_reset(sc);
691 }
692 
693 int
694 bmac_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
695 {
696 	struct bmac_softc *sc = ifp->if_softc;
697 	struct ifaddr *ifa = (struct ifaddr *)data;
698 	int s, error = 0;
699 
700 	s = splnet();
701 
702 	switch (cmd) {
703 
704 	case SIOCINITIFADDR:
705 		ifp->if_flags |= IFF_UP;
706 
707 		bmac_init(sc);
708 		switch (ifa->ifa_addr->sa_family) {
709 #ifdef INET
710 		case AF_INET:
711 			arp_ifinit(ifp, ifa);
712 			break;
713 #endif
714 		default:
715 			break;
716 		}
717 		break;
718 
719 	case SIOCSIFFLAGS:
720 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
721 			break;
722 		/* XXX see the comment in ed_ioctl() about code re-use */
723 		if ((ifp->if_flags & IFF_UP) == 0 &&
724 		    (ifp->if_flags & IFF_RUNNING) != 0) {
725 			/*
726 			 * If interface is marked down and it is running, then
727 			 * stop it.
728 			 */
729 			bmac_stop(sc);
730 			ifp->if_flags &= ~IFF_RUNNING;
731 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
732 		    (ifp->if_flags & IFF_RUNNING) == 0) {
733 			/*
734 			 * If interface is marked up and it is stopped, then
735 			 * start it.
736 			 */
737 			bmac_init(sc);
738 		} else {
739 			/*
740 			 * Reset the interface to pick up changes in any other
741 			 * flags that affect hardware registers.
742 			 */
743 			/*bmac_stop(sc);*/
744 			bmac_init(sc);
745 		}
746 #ifdef BMAC_DEBUG
747 		if (ifp->if_flags & IFF_DEBUG)
748 			sc->sc_flags |= BMAC_DEBUGFLAG;
749 #endif
750 		break;
751 
752 	case SIOCADDMULTI:
753 	case SIOCDELMULTI:
754 	case SIOCGIFMEDIA:
755 	case SIOCSIFMEDIA:
756 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
757 			/*
758 			 * Multicast list has changed; set the hardware filter
759 			 * accordingly.
760 			 */
761 			if (ifp->if_flags & IFF_RUNNING) {
762 				bmac_init(sc);
763 				bmac_setladrf(sc);
764 			}
765 			error = 0;
766 		}
767 		break;
768 	default:
769 		error = ether_ioctl(ifp, cmd, data);
770 		break;
771 	}
772 
773 	splx(s);
774 	return error;
775 }
776 
777 /*
778  * Set up the logical address filter.
779  */
780 void
781 bmac_setladrf(struct bmac_softc *sc)
782 {
783 	struct ifnet *ifp = &sc->sc_if;
784 	struct ether_multi *enm;
785 	struct ether_multistep step;
786 	u_int32_t crc;
787 	u_int16_t hash[4];
788 	int x;
789 
790 	/*
791 	 * Set up multicast address filter by passing all multicast addresses
792 	 * through a crc generator, and then using the high order 6 bits as an
793 	 * index into the 64 bit logical address filter.  The high order bit
794 	 * selects the word, while the rest of the bits select the bit within
795 	 * the word.
796 	 */
797 
798 	if (ifp->if_flags & IFF_PROMISC) {
799 		bmac_set_bits(sc, RXCFG, RxPromiscEnable);
800 		return;
801 	}
802 
803 	if (ifp->if_flags & IFF_ALLMULTI) {
804 		hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
805 		goto chipit;
806 	}
807 
808 	hash[3] = hash[2] = hash[1] = hash[0] = 0;
809 
810 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
811 	while (enm != NULL) {
812 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
813 			/*
814 			 * We must listen to a range of multicast addresses.
815 			 * For now, just accept all multicasts, rather than
816 			 * trying to set only those filter bits needed to match
817 			 * the range.  (At this time, the only use of address
818 			 * ranges is for IP multicast routing, for which the
819 			 * range is big enough to require all bits set.)
820 			 */
821 			hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
822 			ifp->if_flags |= IFF_ALLMULTI;
823 			goto chipit;
824 		}
825 
826 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
827 
828 		/* Just want the 6 most significant bits. */
829 		crc >>= 26;
830 
831 		/* Set the corresponding bit in the filter. */
832 		hash[crc >> 4] |= 1 << (crc & 0xf);
833 
834 		ETHER_NEXT_MULTI(step, enm);
835 	}
836 
837 	ifp->if_flags &= ~IFF_ALLMULTI;
838 
839 chipit:
840 	bmac_write_reg(sc, HASH0, hash[0]);
841 	bmac_write_reg(sc, HASH1, hash[1]);
842 	bmac_write_reg(sc, HASH2, hash[2]);
843 	bmac_write_reg(sc, HASH3, hash[3]);
844 	x = bmac_read_reg(sc, RXCFG);
845 	x &= ~RxPromiscEnable;
846 	x |= RxHashFilterEnable;
847 	bmac_write_reg(sc, RXCFG, x);
848 }
849 
850 int
851 bmac_mii_readreg(device_t self, int phy, int reg)
852 {
853 	return mii_bitbang_readreg(self, &bmac_mbo, phy, reg);
854 }
855 
856 void
857 bmac_mii_writereg(device_t self, int phy, int reg, int val)
858 {
859 	mii_bitbang_writereg(self, &bmac_mbo, phy, reg, val);
860 }
861 
862 u_int32_t
863 bmac_mbo_read(device_t self)
864 {
865 	struct bmac_softc *sc = device_private(self);
866 
867 	return bmac_read_reg(sc, MIFCSR);
868 }
869 
870 void
871 bmac_mbo_write(device_t self, u_int32_t val)
872 {
873 	struct bmac_softc *sc = device_private(self);
874 
875 	bmac_write_reg(sc, MIFCSR, val);
876 }
877 
878 void
879 bmac_mii_statchg(struct ifnet *ifp)
880 {
881 	struct bmac_softc *sc = ifp->if_softc;
882 	int x;
883 
884 	/* Update duplex mode in TX configuration */
885 	x = bmac_read_reg(sc, TXCFG);
886 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
887 		x |= TxFullDuplex;
888 	else
889 		x &= ~TxFullDuplex;
890 	bmac_write_reg(sc, TXCFG, x);
891 
892 #ifdef BMAC_DEBUG
893 	printf("bmac_mii_statchg 0x%x\n",
894 		IFM_OPTIONS(sc->sc_mii.mii_media_active));
895 #endif
896 }
897 
898 void
899 bmac_mii_tick(void *v)
900 {
901 	struct bmac_softc *sc = v;
902 	int s;
903 
904 	s = splnet();
905 	mii_tick(&sc->sc_mii);
906 	splx(s);
907 
908 	callout_reset(&sc->sc_tick_ch, hz, bmac_mii_tick, sc);
909 }
910