Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*	$NetBSD: if_xe.c,v 1.25 2019/04/25 08:31:33 msaitoh Exp $	*/
/*
 * Copyright (c) 1998 Darrin B. Jewell
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_xe.c,v 1.25 2019/04/25 08:31:33 msaitoh Exp $");

#include "opt_inet.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/syslog.h>
#include <sys/socket.h>
#include <sys/device.h>

#include <net/if.h>
#include <net/if_ether.h>
#include <net/if_media.h>

#ifdef INET
#include <netinet/in.h>
#include <netinet/if_inarp.h>
#endif

#include <machine/autoconf.h>
#include <machine/cpu.h>
#include <machine/intr.h>
#include <machine/bus.h>

#include <next68k/next68k/isr.h>

#include <next68k/dev/mb8795reg.h>
#include <next68k/dev/mb8795var.h>

#include <next68k/dev/bmapreg.h>
#include <next68k/dev/intiovar.h>
#include <next68k/dev/nextdmareg.h>
#include <next68k/dev/nextdmavar.h>

#include <next68k/dev/if_xevar.h>
#include <next68k/dev/if_xereg.h>

#ifdef DEBUG
#define XE_DEBUG
#endif

#ifdef XE_DEBUG
int xe_debug = 0;
#define DPRINTF(x) if (xe_debug) printf x;
#else
#define DPRINTF(x)
#endif
#define PRINTF(x) printf x;

extern int turbo;

int	xe_match(device_t, cfdata_t, void *);
void	xe_attach(device_t, device_t, void *);
int	xe_tint(void *);
int	xe_rint(void *);

struct mbuf *xe_dma_rxmap_load(struct mb8795_softc *, bus_dmamap_t);

bus_dmamap_t xe_dma_rx_continue(void *);
void	xe_dma_rx_completed(bus_dmamap_t, void *);
bus_dmamap_t xe_dma_tx_continue(void *);
void	xe_dma_tx_completed(bus_dmamap_t, void *);
void	xe_dma_rx_shutdown(void *);
void	xe_dma_tx_shutdown(void *);

static void findchannel_defer(device_t);

CFATTACH_DECL_NEW(xe, sizeof(struct xe_softc),
    xe_match, xe_attach, NULL, NULL);

static int xe_dma_medias[] = {
	IFM_ETHER | IFM_AUTO,
	IFM_ETHER | IFM_10_T,
	IFM_ETHER | IFM_10_2,
};
static int nxe_dma_medias = __arraycount(xe_dma_medias);

static int attached = 0;

/*
 * Functions and the switch for the MI code.
 */
u_char		xe_read_reg(struct mb8795_softc *, int);
void		xe_write_reg(struct mb8795_softc *, int, u_char);
void		xe_dma_reset(struct mb8795_softc *);
void		xe_dma_rx_setup(struct mb8795_softc *);
void		xe_dma_rx_go(struct mb8795_softc *);
struct mbuf *	xe_dma_rx_mbuf(struct mb8795_softc *);
void		xe_dma_tx_setup(struct mb8795_softc *);
void		xe_dma_tx_go(struct mb8795_softc *);
int		xe_dma_tx_mbuf(struct mb8795_softc *, struct mbuf *);
int		xe_dma_tx_isactive(struct mb8795_softc *);

struct mb8795_glue xe_glue = {
	xe_read_reg,
	xe_write_reg,
	xe_dma_reset,
	xe_dma_rx_setup,
	xe_dma_rx_go,
	xe_dma_rx_mbuf,
	xe_dma_tx_setup,
	xe_dma_tx_go,
	xe_dma_tx_mbuf,
	xe_dma_tx_isactive,
};

int
xe_match(device_t parent, cfdata_t match, void *aux)
{
	struct intio_attach_args *ia = (struct intio_attach_args *)aux;

	if (attached)
		return 0;

	ia->ia_addr = (void *)NEXT_P_ENET;

	return 1;
}

static void
findchannel_defer(device_t self)
{
	struct xe_softc *xsc = device_private(self);
	struct mb8795_softc *sc = &xsc->sc_mb8795;
	int i, error;

	if (!xsc->sc_txdma) {
		xsc->sc_txdma = nextdma_findchannel ("enetx");
		if (xsc->sc_txdma == NULL)
			panic("%s: can't find enetx DMA channel",
			       device_xname(sc->sc_dev));
	}
	if (!xsc->sc_rxdma) {
		xsc->sc_rxdma = nextdma_findchannel ("enetr");
		if (xsc->sc_rxdma == NULL)
			panic ("%s: can't find enetr DMA channel",
			       device_xname(sc->sc_dev));
	}
	aprint_normal_dev(sc->sc_dev, "using DMA channels %s %s\n",
		device_xname(xsc->sc_txdma->sc_dev),
		device_xname(xsc->sc_rxdma->sc_dev));

	nextdma_setconf (xsc->sc_rxdma, continue_cb, xe_dma_rx_continue);
	nextdma_setconf (xsc->sc_rxdma, completed_cb, xe_dma_rx_completed);
	nextdma_setconf (xsc->sc_rxdma, shutdown_cb, xe_dma_rx_shutdown);
	nextdma_setconf (xsc->sc_rxdma, cb_arg, sc);

	nextdma_setconf (xsc->sc_txdma, continue_cb, xe_dma_tx_continue);
	nextdma_setconf (xsc->sc_txdma, completed_cb, xe_dma_tx_completed);
	nextdma_setconf (xsc->sc_txdma, shutdown_cb, xe_dma_tx_shutdown);
	nextdma_setconf (xsc->sc_txdma, cb_arg, sc);

	/* Initialize the DMA maps */
	error = bus_dmamap_create(xsc->sc_txdma->sc_dmat, MCLBYTES,
	    (MCLBYTES/MSIZE), MCLBYTES, 0, BUS_DMA_ALLOCNOW,
	    &xsc->sc_tx_dmamap);
	if (error) {
		aprint_error_dev(sc->sc_dev,
		    "can't create tx DMA map, error = %d", error);
	}

	for(i = 0; i < MB8795_NRXBUFS; i++) {
		error = bus_dmamap_create(xsc->sc_rxdma->sc_dmat, MCLBYTES,
		    (MCLBYTES/MSIZE), MCLBYTES, 0, BUS_DMA_ALLOCNOW,
		    &xsc->sc_rx_dmamap[i]);
		if (error) {
			panic("%s: can't create rx DMA map, error = %d",
			      device_xname(sc->sc_dev), error);
		}
		xsc->sc_rx_mb_head[i] = NULL;
	}
	xsc->sc_rx_loaded_idx = 0;
	xsc->sc_rx_completed_idx = 0;
	xsc->sc_rx_handled_idx = 0;

	/* @@@ more next hacks 
	 * the  2000 covers at least a 1500 mtu + headers
	 * + DMA_BEGINALIGNMENT+ DMA_ENDALIGNMENT
	 */
	xsc->sc_txbuf = malloc(2000, M_DEVBUF, M_NOWAIT);
	if (!xsc->sc_txbuf)
		panic("%s: can't malloc tx DMA buffer",
		    device_xname(sc->sc_dev));
	
	xsc->sc_tx_mb_head = NULL;
	xsc->sc_tx_loaded = 0;
	
	mb8795_config(sc, xe_dma_medias, nxe_dma_medias, xe_dma_medias[0]);
	
	isrlink_autovec(xe_tint, sc, NEXT_I_IPL(NEXT_I_ENETX), 1, NULL);
	INTR_ENABLE(NEXT_I_ENETX);
	isrlink_autovec(xe_rint, sc, NEXT_I_IPL(NEXT_I_ENETR), 1, NULL);
	INTR_ENABLE(NEXT_I_ENETR);
}

void
xe_attach(device_t parent, device_t self, void *aux)
{
	struct intio_attach_args *ia = (struct intio_attach_args *)aux;
	struct xe_softc *xsc = device_private(self);
	struct mb8795_softc *sc = &xsc->sc_mb8795;

	sc->sc_dev = self;
	DPRINTF(("%s: xe_attach()\n", device_xname(self)));

	{
		/* kludge from machdep.c:next68k_bootargs() */
		extern u_char rom_enetaddr[6];
		int i;

		for (i = 0; i < 6; i++)
			sc->sc_enaddr[i] = rom_enetaddr[i];
	}

	printf("\n%s: MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
	       device_xname(self),
	       sc->sc_enaddr[0],sc->sc_enaddr[1],sc->sc_enaddr[2],
	       sc->sc_enaddr[3],sc->sc_enaddr[4],sc->sc_enaddr[5]);

	xsc->sc_bst = ia->ia_bst;
	if (bus_space_map(xsc->sc_bst, NEXT_P_ENET,
			  XE_DEVICE_SIZE, 0, &xsc->sc_bsh)) {
		panic("\n%s: can't map mb8795 registers",
		      device_xname(self));
	}

	sc->sc_bmap_bst = ia->ia_bst;
	if (bus_space_map(sc->sc_bmap_bst, NEXT_P_BMAP,
	    BMAP_SIZE, 0, &sc->sc_bmap_bsh))
		panic("\n%s: can't map bmap registers", device_xname(self));

	/* Set up glue for MI code. */
	sc->sc_glue = &xe_glue;

	xsc->sc_txdma = nextdma_findchannel("enetx");
	xsc->sc_rxdma = nextdma_findchannel("enetr");
	if (xsc->sc_rxdma && xsc->sc_txdma)
		findchannel_defer(self);
	else
		config_defer(self, findchannel_defer);

	attached = 1;
}

int
xe_tint(void *arg)
{
	if (!INTR_OCCURRED(NEXT_I_ENETX))
		return 0;
	mb8795_tint((struct mb8795_softc *)arg);
	return 1;
}

int
xe_rint(void *arg)
{
	if (!INTR_OCCURRED(NEXT_I_ENETR))
		return 0;
	mb8795_rint((struct mb8795_softc *)arg);
	return 1;
}

/*
 * Glue functions.
 */

u_char
xe_read_reg(struct mb8795_softc *sc, int reg)
{
	struct xe_softc *xsc = (struct xe_softc *)sc;

	return bus_space_read_1(xsc->sc_bst, xsc->sc_bsh, reg);
}

void
xe_write_reg(struct mb8795_softc *sc, int reg, u_char val)
{
	struct xe_softc *xsc = (struct xe_softc *)sc;

	bus_space_write_1(xsc->sc_bst, xsc->sc_bsh, reg, val);
}

void
xe_dma_reset(struct mb8795_softc *sc)
{
	struct xe_softc *xsc = (struct xe_softc *)sc;
	int i;

	DPRINTF(("xe DMA reset\n"));
	
	nextdma_reset(xsc->sc_rxdma);
	nextdma_reset(xsc->sc_txdma);

	if (xsc->sc_tx_loaded) {
		bus_dmamap_sync(xsc->sc_txdma->sc_dmat, xsc->sc_tx_dmamap,
		    0, xsc->sc_tx_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
		bus_dmamap_unload(xsc->sc_txdma->sc_dmat, xsc->sc_tx_dmamap);
		xsc->sc_tx_loaded = 0;
	}
	if (xsc->sc_tx_mb_head) {
		m_freem(xsc->sc_tx_mb_head);
		xsc->sc_tx_mb_head = NULL;
	}

	for(i = 0; i < MB8795_NRXBUFS; i++) {
		if (xsc->sc_rx_mb_head[i]) {
			bus_dmamap_unload(xsc->sc_rxdma->sc_dmat,
			    xsc->sc_rx_dmamap[i]);
			m_freem(xsc->sc_rx_mb_head[i]);
			xsc->sc_rx_mb_head[i] = NULL;
		}
	}
}

void
xe_dma_rx_setup(struct mb8795_softc *sc)
{
	struct xe_softc *xsc = (struct xe_softc *)sc;
	int i;

	DPRINTF(("xe DMA rx setup\n"));

	for(i = 0; i < MB8795_NRXBUFS; i++)
		xsc->sc_rx_mb_head[i] = 
			xe_dma_rxmap_load(sc, xsc->sc_rx_dmamap[i]);

	xsc->sc_rx_loaded_idx = 0;
	xsc->sc_rx_completed_idx = 0;
	xsc->sc_rx_handled_idx = 0;

	nextdma_init(xsc->sc_rxdma);
}

void
xe_dma_rx_go(struct mb8795_softc *sc)
{
	struct xe_softc *xsc = (struct xe_softc *)sc;

	DPRINTF(("xe DMA rx go\n"));

	nextdma_start(xsc->sc_rxdma, DMACSR_SETREAD);
}

struct mbuf *
xe_dma_rx_mbuf(struct mb8795_softc *sc)
{
	struct xe_softc *xsc = (struct xe_softc *)sc;
	bus_dmamap_t map;
	struct mbuf *m;

	m = NULL;
	if (xsc->sc_rx_handled_idx != xsc->sc_rx_completed_idx) {
		xsc->sc_rx_handled_idx++;
		xsc->sc_rx_handled_idx %= MB8795_NRXBUFS;

		map = xsc->sc_rx_dmamap[xsc->sc_rx_handled_idx];
		m = xsc->sc_rx_mb_head[xsc->sc_rx_handled_idx];
		
		m->m_len = map->dm_xfer_len;

		bus_dmamap_sync(xsc->sc_rxdma->sc_dmat, map,
				0, map->dm_mapsize, BUS_DMASYNC_POSTREAD);
		
		bus_dmamap_unload(xsc->sc_rxdma->sc_dmat, map);
		
		/* Install a fresh mbuf for next packet */
		
		xsc->sc_rx_mb_head[xsc->sc_rx_handled_idx] = 
			xe_dma_rxmap_load(sc,map);

		/* Punt runt packets
		 * DMA restarts create 0 length packets for example
		 */
		if (m->m_len < ETHER_MIN_LEN) {
			m_freem(m);
			m = NULL;
		}
	}
	return m;
}

void
xe_dma_tx_setup(struct mb8795_softc *sc)
{
	struct xe_softc *xsc = (struct xe_softc *)sc;

	DPRINTF(("xe DMA tx setup\n"));

	nextdma_init(xsc->sc_txdma);
}

void
xe_dma_tx_go(struct mb8795_softc *sc)
{
	struct xe_softc *xsc = (struct xe_softc *)sc;

	DPRINTF(("xe DMA tx go\n"));

	nextdma_start(xsc->sc_txdma, DMACSR_SETWRITE);
}

int
xe_dma_tx_mbuf(struct mb8795_softc *sc, struct mbuf *m)
{
	struct xe_softc *xsc = (struct xe_softc *)sc;
	int error;

	xsc->sc_tx_mb_head = m;

/* The following is a next specific hack that should
 * probably be moved out of MI code.
 * This macro assumes it can move forward as needed
 * in the buffer.  Perhaps it should zero the extra buffer.
 */
#define REALIGN_DMABUF(s,l) \
	{ (s) = ((u_char *)(((unsigned)(s)+DMA_BEGINALIGNMENT-1) \
			&~(DMA_BEGINALIGNMENT-1))); \
    (l) = ((u_char *)(((unsigned)((s)+(l))+DMA_ENDALIGNMENT-1) \
				&~(DMA_ENDALIGNMENT-1)))-(s);}

#if 0
	error = bus_dmamap_load_mbuf(xsc->sc_txdma->sc_dmat,
	    xsc->sc_tx_dmamap, xsc->sc_tx_mb_head, BUS_DMA_NOWAIT);
#else
	{
		u_char *buf = xsc->sc_txbuf;
		int buflen = 0;

		buflen = m->m_pkthdr.len;

		{
			u_char *p = buf;
			for (m=xsc->sc_tx_mb_head; m; m = m->m_next) {
				if (m->m_len == 0) continue;
				memcpy(p, mtod(m, u_char *), m->m_len);
				p += m->m_len;
			}
			/* Fix runt packets */
			if (buflen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
				memset(p, 0,
				    ETHER_MIN_LEN - ETHER_CRC_LEN - buflen);
				buflen = ETHER_MIN_LEN - ETHER_CRC_LEN;
			}
		}
		
		error = bus_dmamap_load(xsc->sc_txdma->sc_dmat,
		    xsc->sc_tx_dmamap, buf, buflen, NULL, BUS_DMA_NOWAIT);
	}
#endif
	if (error) {
		aprint_error_dev(sc->sc_dev,
		    "can't load mbuf chain, error = %d\n", error);
		m_freem(xsc->sc_tx_mb_head);
		xsc->sc_tx_mb_head = NULL;
		return error;
	}

#ifdef DIAGNOSTIC
	if (xsc->sc_tx_loaded != 0) {
		panic("%s: xsc->sc_tx_loaded is %d", device_xname(sc->sc_dev),
		      xsc->sc_tx_loaded);
	}
#endif

	bus_dmamap_sync(xsc->sc_txdma->sc_dmat, xsc->sc_tx_dmamap, 0,
			xsc->sc_tx_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);

	return 0;
}

int
xe_dma_tx_isactive(struct mb8795_softc *sc)
{
	struct xe_softc *xsc = (struct xe_softc *)sc;

	return (xsc->sc_tx_loaded != 0);
}

/****************************************************************/

void 
xe_dma_tx_completed(bus_dmamap_t map, void *arg)
{
#if defined (XE_DEBUG) || defined (DIAGNOSTIC)
	struct mb8795_softc *sc = arg;
#endif
#ifdef DIAGNOSTIC
	struct xe_softc *xsc = (struct xe_softc *)sc;
#endif

	DPRINTF(("%s: xe_dma_tx_completed()\n", device_xname(sc->sc_dev)));

#ifdef DIAGNOSTIC
	if (!xsc->sc_tx_loaded)
		panic("%s: tx completed never loaded",
		    device_xname(sc->sc_dev));

	if (map != xsc->sc_tx_dmamap)
		panic("%s: unexpected tx completed map",
		    device_xname(sc->sc_dev));

#endif
}

void 
xe_dma_tx_shutdown(void *arg)
{
	struct mb8795_softc *sc = arg;
	struct xe_softc *xsc = (struct xe_softc *)sc;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;

	DPRINTF(("%s: xe_dma_tx_shutdown()\n", device_xname(sc->sc_dev)));

#ifdef DIAGNOSTIC
	if (!xsc->sc_tx_loaded)
		panic("%s: tx shutdown never loaded",
		    device_xname(sc->sc_dev));
#endif

	if (turbo)
		MB_WRITE_REG(sc, MB8795_TXMODE, MB8795_TXMODE_TURBO1);
	if (xsc->sc_tx_loaded) {
		bus_dmamap_sync(xsc->sc_txdma->sc_dmat, xsc->sc_tx_dmamap,
		    0, xsc->sc_tx_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
		bus_dmamap_unload(xsc->sc_txdma->sc_dmat, xsc->sc_tx_dmamap);
		m_freem(xsc->sc_tx_mb_head);
		xsc->sc_tx_mb_head = NULL;
		
		xsc->sc_tx_loaded--;
	}

#ifdef DIAGNOSTIC
	if (xsc->sc_tx_loaded != 0)
		panic("%s: sc->sc_tx_loaded is %d", device_xname(sc->sc_dev),
		      xsc->sc_tx_loaded);
#endif

	ifp->if_timer = 0;

#if 1
	if ((ifp->if_flags & IFF_RUNNING) && !IF_IS_EMPTY(&sc->sc_tx_snd)) {
		void mb8795_start_dma(struct mb8795_softc *); /* XXXX */
		mb8795_start_dma(sc);
	}
#endif

#if 0
	/* Enable ready interrupt */
	MB_WRITE_REG(sc, MB8795_TXMASK, 
		     MB_READ_REG(sc, MB8795_TXMASK)
		     | MB8795_TXMASK_TXRXIE/* READYIE */);
#endif
}


void 
xe_dma_rx_completed(bus_dmamap_t map, void *arg)
{
	struct mb8795_softc *sc = arg;
	struct xe_softc *xsc = (struct xe_softc *)sc;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;

	if (ifp->if_flags & IFF_RUNNING) {
		xsc->sc_rx_completed_idx++;
		xsc->sc_rx_completed_idx %= MB8795_NRXBUFS;
		
		DPRINTF(("%s: xe_dma_rx_completed(), "
			"sc->sc_rx_completed_idx = %d\n",
			 device_xname(sc->sc_dev), xsc->sc_rx_completed_idx));
		
#if (defined(DIAGNOSTIC))
		if (map != xsc->sc_rx_dmamap[xsc->sc_rx_completed_idx])
			panic("%s: Unexpected rx dmamap completed",
			      device_xname(sc->sc_dev));
#endif
	}
#ifdef DIAGNOSTIC
	else
		DPRINTF(("%s: Unexpected rx dmamap completed while if not "
			"running\n", device_xname(sc->sc_dev)));
#endif
}

void 
xe_dma_rx_shutdown(void *arg)
{
	struct mb8795_softc *sc = arg;
	struct xe_softc *xsc = (struct xe_softc *)sc;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;

	if (ifp->if_flags & IFF_RUNNING) {
		DPRINTF(("%s: xe_dma_rx_shutdown(), restarting.\n",
			 device_xname(sc->sc_dev)));
		
		nextdma_start(xsc->sc_rxdma, DMACSR_SETREAD);
		if (turbo)
			MB_WRITE_REG(sc, MB8795_RXMODE,
			    MB8795_RXMODE_TEST | MB8795_RXMODE_MULTICAST);
	}
#ifdef DIAGNOSTIC
	else
		DPRINTF(("%s: Unexpected rx DMA shutdown while if not "
			"running\n", device_xname(sc->sc_dev)));
#endif
}

/*
 * load a dmamap with a freshly allocated mbuf
 */
struct mbuf *
xe_dma_rxmap_load(struct mb8795_softc *sc, bus_dmamap_t map)
{
	struct xe_softc *xsc = (struct xe_softc *)sc;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct mbuf *m;
	int error;

	MGETHDR(m, M_DONTWAIT, MT_DATA);
	if (m) {
		MCLGET(m, M_DONTWAIT);
		if ((m->m_flags & M_EXT) == 0) {
			m_freem(m);
			m = NULL;
		} else
			m->m_len = MCLBYTES;
	}
	if (!m) {
		/*
		 * @@@ Handle this gracefully by reusing a scratch buffer
		 * or something.
		 */
		panic("Unable to get memory for incoming ethernet");
	}

	/*
	 * Align buffer, @@@ next specific.
	 * perhaps should be using M_ALIGN here instead? 
	 * First we give us a little room to align with.
	 */
	{
		u_char *buf = m->m_data;
		int buflen = m->m_len;
		buflen -= DMA_ENDALIGNMENT+DMA_BEGINALIGNMENT;
		REALIGN_DMABUF(buf, buflen);
		m->m_data = buf;
		m->m_len = buflen;
	}

	m_set_rcvif(m, ifp);
	m->m_pkthdr.len = m->m_len;

	error = bus_dmamap_load_mbuf(xsc->sc_rxdma->sc_dmat,
			map, m, BUS_DMA_NOWAIT);

	bus_dmamap_sync(xsc->sc_rxdma->sc_dmat, map, 0,
			map->dm_mapsize, BUS_DMASYNC_PREREAD);
	
	if (error) {
		DPRINTF(("DEBUG: m->m_data = %p, m->m_len = %d\n",
				m->m_data, m->m_len));
		DPRINTF(("DEBUG: MCLBYTES = %d, map->_dm_size = %ld\n",
				MCLBYTES, map->_dm_size));

		panic("%s: can't load rx mbuf chain, error = %d",
				device_xname(sc->sc_dev), error);
		m_freem(m);
		m = NULL;
	}

	return m;
}

bus_dmamap_t 
xe_dma_rx_continue(void *arg)
{
	struct mb8795_softc *sc = arg;
	struct xe_softc *xsc = (struct xe_softc *)sc;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	bus_dmamap_t map = NULL;

	if (ifp->if_flags & IFF_RUNNING) {
		if (((xsc->sc_rx_loaded_idx+1)%MB8795_NRXBUFS)
		    == xsc->sc_rx_handled_idx) {
			/* Make space for one packet by dropping one */
			struct mbuf *m;
			m = xe_dma_rx_mbuf (sc);
			if (m)
				m_freem(m);
#if (defined(DIAGNOSTIC))
			DPRINTF(("%s: out of receive DMA buffers\n",
				device_xname(sc->sc_dev)));
#endif
		}
		xsc->sc_rx_loaded_idx++;
		xsc->sc_rx_loaded_idx %= MB8795_NRXBUFS;
		map = xsc->sc_rx_dmamap[xsc->sc_rx_loaded_idx];
		
		DPRINTF(("%s: xe_dma_rx_continue() xsc->sc_rx_loaded_idx "
			"= %d\n", device_xname(sc->sc_dev),
			xsc->sc_rx_loaded_idx));
	}
#ifdef DIAGNOSTIC
	else
		panic("%s: Unexpected rx DMA continue while if not running",
		      device_xname(sc->sc_dev));
#endif
	
	return map;
}

bus_dmamap_t 
xe_dma_tx_continue(void *arg)
{
	struct mb8795_softc *sc = arg;
	struct xe_softc *xsc = (struct xe_softc *)sc;
	bus_dmamap_t map;

	DPRINTF(("%s: xe_dma_tx_continue()\n", device_xname(sc->sc_dev)));

	if (xsc->sc_tx_loaded)
		map = NULL;
	else {
		map = xsc->sc_tx_dmamap;
		xsc->sc_tx_loaded++;
	}

#ifdef DIAGNOSTIC
	if (xsc->sc_tx_loaded != 1)
		panic("%s: sc->sc_tx_loaded is %d", device_xname(sc->sc_dev),
				xsc->sc_tx_loaded);
#endif

	return map;
}