mb8795.c revision 1.27 1 /* $NetBSD: mb8795.c,v 1.27 2002/07/11 16:03:11 christos Exp $ */
2 /*
3 * Copyright (c) 1998 Darrin B. Jewell
4 * 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. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Darrin B. Jewell
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "opt_inet.h"
33 #include "opt_ccitt.h"
34 #include "opt_llc.h"
35 #include "opt_ns.h"
36 #include "bpfilter.h"
37 #include "rnd.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mbuf.h>
42 #include <sys/syslog.h>
43 #include <sys/socket.h>
44 #include <sys/device.h>
45 #include <sys/malloc.h>
46 #include <sys/ioctl.h>
47 #include <sys/errno.h>
48 #if NRND > 0
49 #include <sys/rnd.h>
50 #endif
51
52 #include <net/if.h>
53 #include <net/if_dl.h>
54 #include <net/if_ether.h>
55
56 #include <net/if_media.h>
57
58 #ifdef INET
59 #include <netinet/in.h>
60 #include <netinet/if_inarp.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip.h>
64 #endif
65
66 #ifdef NS
67 #include <netns/ns.h>
68 #include <netns/ns_if.h>
69 #endif
70
71 #if defined(CCITT) && defined(LLC)
72 #include <sys/socketvar.h>
73 #include <netccitt/x25.h>
74 #include <netccitt/pk.h>
75 #include <netccitt/pk_var.h>
76 #include <netccitt/pk_extern.h>
77 #endif
78
79 #if NBPFILTER > 0
80 #include <net/bpf.h>
81 #include <net/bpfdesc.h>
82 #endif
83
84 #include <machine/cpu.h>
85 #include <machine/bus.h>
86 #include <machine/intr.h>
87
88 /* @@@ this is here for the REALIGN_DMABUF hack below */
89 #include "nextdmareg.h"
90 #include "nextdmavar.h"
91
92 #include "mb8795reg.h"
93 #include "mb8795var.h"
94
95 #include "bmapreg.h"
96
97 #if 1
98 #define XE_DEBUG
99 #endif
100
101 #define PRINTF(x) printf x;
102 #ifdef XE_DEBUG
103 int xe_debug = 0;
104 #define DPRINTF(x) if (xe_debug) printf x;
105 #else
106 #define DPRINTF(x)
107 #endif
108
109
110 /*
111 * Support for
112 * Fujitsu Ethernet Data Link Controller (MB8795)
113 * and the Fujitsu Manchester Encoder/Decoder (MB502).
114 */
115
116 void mb8795_shutdown __P((void *));
117
118 struct mbuf * mb8795_rxdmamap_load __P((struct mb8795_softc *,
119 bus_dmamap_t map));
120
121 bus_dmamap_t mb8795_rxdma_continue __P((void *));
122 void mb8795_rxdma_completed __P((bus_dmamap_t,void *));
123 bus_dmamap_t mb8795_txdma_continue __P((void *));
124 void mb8795_txdma_completed __P((bus_dmamap_t,void *));
125 void mb8795_rxdma_shutdown __P((void *));
126 void mb8795_txdma_shutdown __P((void *));
127 bus_dmamap_t mb8795_txdma_restart __P((bus_dmamap_t,void *));
128 void mb8795_start_dma __P((struct ifnet *));
129
130 int mb8795_mediachange __P((struct ifnet *));
131 void mb8795_mediastatus __P((struct ifnet *, struct ifmediareq *));
132
133 void
134 mb8795_config(sc, media, nmedia, defmedia)
135 struct mb8795_softc *sc;
136 int *media, nmedia, defmedia;
137 {
138 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
139 int i;
140
141 DPRINTF(("%s: mb8795_config()\n",sc->sc_dev.dv_xname));
142
143 /* Initialize ifnet structure. */
144 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
145 ifp->if_softc = sc;
146 ifp->if_start = mb8795_start;
147 ifp->if_ioctl = mb8795_ioctl;
148 ifp->if_watchdog = mb8795_watchdog;
149 ifp->if_flags =
150 IFF_BROADCAST | IFF_NOTRAILERS;
151
152 /* Initialize media goo. */
153 ifmedia_init(&sc->sc_media, 0, mb8795_mediachange,
154 mb8795_mediastatus);
155 if (media != NULL) {
156 for (i = 0; i < nmedia; i++)
157 ifmedia_add(&sc->sc_media, media[i], 0, NULL);
158 ifmedia_set(&sc->sc_media, defmedia);
159 } else {
160 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
161 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
162 }
163
164 /* Attach the interface. */
165 if_attach(ifp);
166 ether_ifattach(ifp, sc->sc_enaddr);
167
168 sc->sc_sh = shutdownhook_establish(mb8795_shutdown, sc);
169 if (sc->sc_sh == NULL)
170 panic("mb8795_config: can't establish shutdownhook");
171
172 #if NRND > 0
173 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
174 RND_TYPE_NET, 0);
175 #endif
176
177 /* Initialize the dma maps */
178 {
179 int error;
180 if ((error = bus_dmamap_create(sc->sc_tx_dmat, MCLBYTES,
181 (MCLBYTES/MSIZE), MCLBYTES, 0, BUS_DMA_ALLOCNOW,
182 &sc->sc_tx_dmamap)) != 0) {
183 panic("%s: can't create tx DMA map, error = %d\n",
184 sc->sc_dev.dv_xname, error);
185 }
186 {
187 int i;
188 for(i=0;i<MB8795_NRXBUFS;i++) {
189 if ((error = bus_dmamap_create(sc->sc_rx_dmat, MCLBYTES,
190 (MCLBYTES/MSIZE), MCLBYTES, 0, BUS_DMA_ALLOCNOW,
191 &sc->sc_rx_dmamap[i])) != 0) {
192 panic("%s: can't create rx DMA map, error = %d\n",
193 sc->sc_dev.dv_xname, error);
194 }
195 sc->sc_rx_mb_head[i] = NULL;
196 }
197 sc->sc_rx_loaded_idx = 0;
198 sc->sc_rx_completed_idx = 0;
199 sc->sc_rx_handled_idx = 0;
200 }
201 }
202
203 /* @@@ more next hacks
204 * the 2000 covers at least a 1500 mtu + headers
205 * + DMA_BEGINALIGNMENT+ DMA_ENDALIGNMENT
206 */
207 sc->sc_txbuf = malloc(2000, M_DEVBUF, M_NOWAIT);
208 if (!sc->sc_txbuf) panic("%s: can't malloc tx DMA buffer",
209 sc->sc_dev.dv_xname);
210
211 sc->sc_tx_mb_head = NULL;
212 sc->sc_tx_loaded = 0;
213
214 sc->sc_tx_nd->nd_shutdown_cb = mb8795_txdma_shutdown;
215 sc->sc_tx_nd->nd_continue_cb = mb8795_txdma_continue;
216 sc->sc_tx_nd->nd_completed_cb = mb8795_txdma_completed;
217 sc->sc_tx_nd->nd_cb_arg = sc;
218
219 sc->sc_rx_nd->nd_shutdown_cb = mb8795_rxdma_shutdown;
220 sc->sc_rx_nd->nd_continue_cb = mb8795_rxdma_continue;
221 sc->sc_rx_nd->nd_completed_cb = mb8795_rxdma_completed;
222 sc->sc_rx_nd->nd_cb_arg = sc;
223
224 DPRINTF(("%s: leaving mb8795_config()\n",sc->sc_dev.dv_xname));
225 }
226
227 /*
228 * Media change callback.
229 */
230 int
231 mb8795_mediachange(ifp)
232 struct ifnet *ifp;
233 {
234 struct mb8795_softc *sc = ifp->if_softc;
235 int data;
236
237 switch IFM_SUBTYPE(sc->sc_media.ifm_media) {
238 case IFM_AUTO:
239 if (bus_space_read_1(sc->sc_bmap_bst, sc->sc_bmap_bsh, BMAP_DATA) &
240 BMAP_DATA_UTPCARRIER_MASK) {
241 data = 0;
242 sc->sc_media.ifm_cur->ifm_data = IFM_ETHER|IFM_10_2;
243 } else {
244 data = BMAP_DATA_UTPENABLE;
245 sc->sc_media.ifm_cur->ifm_data = IFM_ETHER|IFM_10_T;
246 }
247 break;
248 case IFM_10_T:
249 data = BMAP_DATA_UTPENABLE;
250 break;
251 case IFM_10_2:
252 data = 0;
253 break;
254 default:
255 return (1);
256 break;
257 }
258
259 bus_space_write_1(sc->sc_bmap_bst, sc->sc_bmap_bsh,
260 BMAP_DDIR, BMAP_DDIR_UTPENABLE_MASK);
261 bus_space_write_1(sc->sc_bmap_bst, sc->sc_bmap_bsh,
262 BMAP_DATA, data);
263
264 return (0);
265 }
266
267 /*
268 * Media status callback.
269 */
270 void
271 mb8795_mediastatus(ifp, ifmr)
272 struct ifnet *ifp;
273 struct ifmediareq *ifmr;
274 {
275 struct mb8795_softc *sc = ifp->if_softc;
276
277 if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_AUTO) {
278 ifmr->ifm_active = sc->sc_media.ifm_cur->ifm_data;
279 }
280 if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_10_T) {
281 ifmr->ifm_status = IFM_AVALID;
282 if (!(bus_space_read_1(sc->sc_bmap_bst, sc->sc_bmap_bsh, BMAP_DATA) &
283 BMAP_DATA_UTPCARRIER_MASK))
284 ifmr->ifm_status |= IFM_ACTIVE;
285 } else {
286 ifmr->ifm_status &= ~IFM_AVALID; /* don't know for 10_2 */
287 }
288 return;
289 }
290
291 /****************************************************************/
292 #ifdef XE_DEBUG
293 #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
294 static void
295 xe_hex_dump(unsigned char *pkt, size_t len)
296 {
297 size_t i, j;
298
299 printf("00000000 ");
300 for(i=0; i<len; i++) {
301 printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
302 if ((i+1) % 16 == 8) {
303 printf(" ");
304 }
305 if ((i+1) % 16 == 0) {
306 printf(" %c", '|');
307 for(j=0; j<16; j++) {
308 printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
309 }
310 printf("%c\n%c%c%c%c%c%c%c%c ", '|',
311 XCHR((i+1)>>28),XCHR((i+1)>>24),XCHR((i+1)>>20),XCHR((i+1)>>16),
312 XCHR((i+1)>>12), XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
313 }
314 }
315 printf("\n");
316 }
317 #undef XCHR
318 #endif
319
320 /*
321 * Controller receive interrupt.
322 */
323 void
324 mb8795_rint(sc)
325 struct mb8795_softc *sc;
326 {
327 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
328 int error = 0;
329 u_char rxstat;
330 u_char rxmask;
331
332 rxstat = bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_RXSTAT);
333 rxmask = bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_RXMASK);
334
335 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_RXSTAT, XE_RXSTAT_CLEAR);
336
337 if (rxstat & XE_RXSTAT_RESET) {
338 DPRINTF(("%s: rx reset packet\n",
339 sc->sc_dev.dv_xname));
340 error++;
341 }
342 if (rxstat & XE_RXSTAT_SHORT) {
343 DPRINTF(("%s: rx short packet\n",
344 sc->sc_dev.dv_xname));
345 error++;
346 }
347 if (rxstat & XE_RXSTAT_ALIGNERR) {
348 DPRINTF(("%s: rx alignment error\n",
349 sc->sc_dev.dv_xname));
350 #if 0
351 error++;
352 #endif
353 }
354 if (rxstat & XE_RXSTAT_CRCERR) {
355 DPRINTF(("%s: rx CRC error\n",
356 sc->sc_dev.dv_xname));
357 #if 0
358 error++;
359 #endif
360 }
361 if (rxstat & XE_RXSTAT_OVERFLOW) {
362 DPRINTF(("%s: rx overflow error\n",
363 sc->sc_dev.dv_xname));
364 #if 0
365 error++;
366 #endif
367 }
368
369 if (error) {
370 ifp->if_ierrors++;
371 /* @@@ handle more gracefully, free memory, etc. */
372 }
373
374 if (rxstat & XE_RXSTAT_OK) {
375 int s;
376 s = spldma();
377
378 while(sc->sc_rx_handled_idx != sc->sc_rx_completed_idx) {
379 struct mbuf *m;
380 bus_dmamap_t map;
381
382 sc->sc_rx_handled_idx++;
383 sc->sc_rx_handled_idx %= MB8795_NRXBUFS;
384
385 /* Should probably not do this much while interrupts
386 * are disabled, but for now we will.
387 */
388
389 map = sc->sc_rx_dmamap[sc->sc_rx_handled_idx];
390 m = sc->sc_rx_mb_head[sc->sc_rx_handled_idx];
391
392 m->m_pkthdr.len = m->m_len = map->dm_xfer_len;
393 m->m_flags |= M_HASFCS;
394 m->m_pkthdr.rcvif = ifp;
395
396 bus_dmamap_sync(sc->sc_rx_dmat, map,
397 0, map->dm_mapsize, BUS_DMASYNC_POSTREAD);
398
399 bus_dmamap_unload(sc->sc_rx_dmat, map);
400
401 /* Install a fresh mbuf for next packet */
402
403 sc->sc_rx_mb_head[sc->sc_rx_handled_idx] =
404 mb8795_rxdmamap_load(sc,map);
405
406 /* Punt runt packets
407 * dma restarts create 0 length packets for example
408 */
409 if (m->m_len < ETHER_MIN_LEN) {
410 m_freem(m);
411 continue;
412 }
413
414 /* Find receive length, keep crc */
415 /* enable dma interrupts while we process the packet */
416 splx(s);
417
418 #if defined(XE_DEBUG)
419 /* Peek at the packet */
420 DPRINTF(("%s: received packet, at VA %p-%p,len %d\n",
421 sc->sc_dev.dv_xname,mtod(m,u_char *),mtod(m,u_char *)+m->m_len,m->m_len));
422 if (xe_debug > 3) {
423 xe_hex_dump(mtod(m,u_char *), m->m_pkthdr.len);
424 } else if (xe_debug > 2) {
425 xe_hex_dump(mtod(m,u_char *), m->m_pkthdr.len < 255 ? m->m_pkthdr.len : 128 );
426 }
427 #endif
428
429 #if NBPFILTER > 0
430 /*
431 * Pass packet to bpf if there is a listener.
432 */
433 if (ifp->if_bpf)
434 bpf_mtap(ifp->if_bpf, m);
435 #endif
436
437 {
438 ifp->if_ipackets++;
439
440 /* Pass the packet up. */
441 (*ifp->if_input)(ifp, m);
442 }
443
444 s = spldma();
445
446 }
447
448 splx(s);
449
450 }
451
452 #ifdef XE_DEBUG
453 if (xe_debug) {
454 char sbuf[256];
455
456 bitmask_snprintf(rxstat, XE_RXSTAT_BITS, sbuf, sizeof(sbuf));
457 printf("%s: rx interrupt, rxstat = %s\n",
458 sc->sc_dev.dv_xname, sbuf);
459
460 bitmask_snprintf(bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_RXSTAT),
461 XE_RXSTAT_BITS, sbuf, sizeof(sbuf));
462 printf("rxstat = 0x%s\n", sbuf);
463
464 bitmask_snprintf(bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_RXMASK),
465 XE_RXMASK_BITS, sbuf, sizeof(sbuf));
466 printf("rxmask = 0x%s\n", sbuf);
467
468 bitmask_snprintf(bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_RXMODE),
469 XE_RXMODE_BITS, sbuf, sizeof(sbuf));
470 printf("rxmode = 0x%s\n", sbuf);
471 }
472 #endif
473
474 return;
475 }
476
477 /*
478 * Controller transmit interrupt.
479 */
480 void
481 mb8795_tint(sc)
482 struct mb8795_softc *sc;
483
484 {
485 u_char txstat;
486 u_char txmask;
487 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
488
489 txstat = bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_TXSTAT);
490 txmask = bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_TXMASK);
491
492 if (txstat & XE_TXSTAT_SHORTED) {
493 printf("%s: tx cable shorted\n", sc->sc_dev.dv_xname);
494 ifp->if_oerrors++;
495 }
496 if (txstat & XE_TXSTAT_UNDERFLOW) {
497 printf("%s: tx underflow\n", sc->sc_dev.dv_xname);
498 ifp->if_oerrors++;
499 }
500 if (txstat & XE_TXSTAT_COLLERR) {
501 DPRINTF(("%s: tx collision\n", sc->sc_dev.dv_xname));
502 ifp->if_collisions++;
503 }
504 if (txstat & XE_TXSTAT_COLLERR16) {
505 printf("%s: tx 16th collision\n", sc->sc_dev.dv_xname);
506 ifp->if_oerrors++;
507 ifp->if_collisions += 16;
508 }
509
510 #if 0
511 if (txstat & XE_TXSTAT_READY) {
512 char sbuf[256];
513
514 bitmask_snprintf(txstat, XE_TXSTAT_BITS, sbuf, sizeof(sbuf));
515 panic("%s: unexpected tx interrupt %s",
516 sc->sc_dev.dv_xname, sbuf);
517
518 /* turn interrupt off */
519 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXMASK,
520 txmask & ~XE_TXMASK_READYIE);
521 }
522 #endif
523
524 return;
525 }
526
527 /****************************************************************/
528
529 void
530 mb8795_reset(sc)
531 struct mb8795_softc *sc;
532 {
533 int s;
534 int i;
535
536 s = splnet();
537
538 DPRINTF (("%s: mb8795_reset()\n",sc->sc_dev.dv_xname));
539
540 sc->sc_ethercom.ec_if.if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
541 sc->sc_ethercom.ec_if.if_timer = 0;
542
543 nextdma_reset(sc->sc_tx_nd);
544 nextdma_reset(sc->sc_rx_nd);
545
546 if (sc->sc_tx_loaded) {
547 bus_dmamap_sync(sc->sc_tx_dmat, sc->sc_tx_dmamap,
548 0, sc->sc_tx_dmamap->dm_mapsize,
549 BUS_DMASYNC_POSTWRITE);
550 bus_dmamap_unload(sc->sc_tx_dmat, sc->sc_tx_dmamap);
551 }
552 sc->sc_tx_loaded = 0;
553 if (sc->sc_tx_mb_head) {
554 m_freem(sc->sc_tx_mb_head);
555 sc->sc_tx_mb_head = NULL;
556 }
557
558 for(i=0;i<MB8795_NRXBUFS;i++) {
559 if (sc->sc_rx_mb_head[i]) {
560 bus_dmamap_unload(sc->sc_rx_dmat, sc->sc_rx_dmamap[i]);
561 m_freem(sc->sc_rx_mb_head[i]);
562 sc->sc_rx_mb_head[i] = NULL;
563 }
564 }
565
566 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_RESET, XE_RESET_MODE);
567
568 mb8795_mediachange(&sc->sc_ethercom.ec_if);
569
570 #if 0 /* This interrupt was sometimes failing to ack correctly
571 * causing a loop @@@
572 */
573 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXMASK,
574 XE_TXMASK_UNDERFLOWIE | XE_TXMASK_COLLIE | XE_TXMASK_COLL16IE
575 | XE_TXMASK_PARERRIE);
576 #else
577 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXMASK, 0);
578 #endif
579 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXSTAT, XE_TXSTAT_CLEAR);
580
581 #if 0
582 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_RXMASK,
583 XE_RXMASK_OKIE | XE_RXMASK_RESETIE | XE_RXMASK_SHORTIE |
584 XE_RXMASK_ALIGNERRIE | XE_RXMASK_CRCERRIE | XE_RXMASK_OVERFLOWIE);
585 #else
586 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_RXMASK,
587 XE_RXMASK_OKIE | XE_RXMASK_RESETIE | XE_RXMASK_SHORTIE);
588 #endif
589
590 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_RXSTAT, XE_RXSTAT_CLEAR);
591
592 for(i=0;i<sizeof(sc->sc_enaddr);i++) {
593 bus_space_write_1(sc->sc_bst,sc->sc_bsh,XE_ENADDR+i,sc->sc_enaddr[i]);
594 }
595
596 DPRINTF(("%s: initializing ethernet %02x:%02x:%02x:%02x:%02x:%02x, size=%d\n",
597 sc->sc_dev.dv_xname,
598 sc->sc_enaddr[0],sc->sc_enaddr[1],sc->sc_enaddr[2],
599 sc->sc_enaddr[3],sc->sc_enaddr[4],sc->sc_enaddr[5],
600 sizeof(sc->sc_enaddr)));
601
602 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_RESET, 0);
603
604 splx(s);
605 }
606
607 void
608 mb8795_watchdog(ifp)
609 struct ifnet *ifp;
610 {
611 struct mb8795_softc *sc = ifp->if_softc;
612
613 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
614 ++ifp->if_oerrors;
615
616 DPRINTF(("%s: %lld input errors, %lld input packets\n",
617 sc->sc_dev.dv_xname, ifp->if_ierrors, ifp->if_ipackets));
618
619 ifp->if_flags &= ~IFF_RUNNING;
620 mb8795_init(sc);
621 }
622
623 /*
624 * Initialization of interface; set up initialization block
625 * and transmit/receive descriptor rings.
626 */
627 void
628 mb8795_init(sc)
629 struct mb8795_softc *sc;
630 {
631 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
632 int s;
633 int i;
634
635 DPRINTF (("%s: mb8795_init()\n",sc->sc_dev.dv_xname));
636 if (ifp->if_flags & IFF_UP) {
637 int rxmode;
638
639 s = splnet ();
640 if ((ifp->if_flags & IFF_RUNNING) == 0) {
641 mb8795_reset(sc);
642 }
643 if (ifp->if_flags & IFF_PROMISC) {
644 rxmode = XE_RXMODE_PROMISCUOUS;
645 } else {
646 /* XXX add support for multicast */
647 rxmode = XE_RXMODE_NORMAL;
648 }
649
650 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXMODE, XE_TXMODE_LB_DISABLE);
651 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_RXMODE, rxmode);
652
653 if ((ifp->if_flags & IFF_RUNNING) == 0) {
654 for(i=0;i<MB8795_NRXBUFS;i++) {
655 sc->sc_rx_mb_head[i] =
656 mb8795_rxdmamap_load(sc, sc->sc_rx_dmamap[i]);
657 }
658 sc->sc_rx_loaded_idx = 0;
659 sc->sc_rx_completed_idx = 0;
660 sc->sc_rx_handled_idx = 0;
661
662 ifp->if_flags |= IFF_RUNNING;
663 ifp->if_flags &= ~IFF_OACTIVE;
664 ifp->if_timer = 0;
665
666 nextdma_init(sc->sc_tx_nd);
667 nextdma_init(sc->sc_rx_nd);
668
669 nextdma_start(sc->sc_rx_nd, DMACSR_SETREAD);
670 }
671 splx(s);
672 #if 0
673 s = spldma();
674 if (! IF_IS_EMPTY(&sc->sc_tx_snd)) {
675 mb8795_start_dma(ifp);
676 }
677 splx(s);
678 #endif
679 } else {
680 /* ifp->if_flags &= ~IFF_RUNNING; */
681 /* ifp->if_flags &= ~IFF_OACTIVE; */
682 /* ifp->if_timer = 0; */
683 mb8795_reset(sc);
684 }
685 }
686
687 void
688 mb8795_shutdown(arg)
689 void *arg;
690 {
691 struct mb8795_softc *sc = (struct mb8795_softc *)arg;
692 /* struct ifnet *ifp = &sc->sc_ethercom.ec_if; */
693 /* ifp->if_flags &= ~IFF_RUNNING; */
694 mb8795_reset(sc);
695 }
696
697 /****************************************************************/
698 int
699 mb8795_ioctl(ifp, cmd, data)
700 register struct ifnet *ifp;
701 u_long cmd;
702 caddr_t data;
703 {
704 register struct mb8795_softc *sc = ifp->if_softc;
705 struct ifaddr *ifa = (struct ifaddr *)data;
706 struct ifreq *ifr = (struct ifreq *)data;
707 int s, error = 0;
708
709 s = splnet();
710
711 switch (cmd) {
712
713 case SIOCSIFADDR:
714 ifp->if_flags |= IFF_UP;
715
716 switch (ifa->ifa_addr->sa_family) {
717 #ifdef INET
718 case AF_INET:
719 mb8795_init(sc);
720 arp_ifinit(ifp, ifa);
721 break;
722 #endif
723 #ifdef NS
724 case AF_NS:
725 {
726 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
727
728 if (ns_nullhost(*ina))
729 ina->x_host =
730 *(union ns_host *)LLADDR(ifp->if_sadl);
731 else {
732 bcopy(ina->x_host.c_host,
733 LLADDR(ifp->if_sadl),
734 sizeof(sc->sc_enaddr));
735 }
736 /* Set new address. */
737 mb8795_init(sc);
738 break;
739 }
740 #endif
741 default:
742 mb8795_init(sc);
743 break;
744 }
745 break;
746
747 #if defined(CCITT) && defined(LLC)
748 case SIOCSIFCONF_X25:
749 ifp->if_flags |= IFF_UP;
750 ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
751 error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
752 if (error == 0)
753 mb8795_init(sc);
754 break;
755 #endif /* CCITT && LLC */
756
757 case SIOCSIFFLAGS:
758 if ((ifp->if_flags & IFF_UP) == 0 &&
759 (ifp->if_flags & IFF_RUNNING) != 0) {
760 /*
761 * If interface is marked down and it is running, then
762 * stop it.
763 */
764 /* ifp->if_flags &= ~IFF_RUNNING; */
765 mb8795_reset(sc);
766 } else if ((ifp->if_flags & IFF_UP) != 0 &&
767 (ifp->if_flags & IFF_RUNNING) == 0) {
768 /*
769 * If interface is marked up and it is stopped, then
770 * start it.
771 */
772 mb8795_init(sc);
773 } else {
774 /*
775 * Reset the interface to pick up changes in any other
776 * flags that affect hardware registers.
777 */
778 mb8795_init(sc);
779 }
780 #ifdef XE_DEBUG
781 if (ifp->if_flags & IFF_DEBUG)
782 sc->sc_debug = 1;
783 else
784 sc->sc_debug = 0;
785 #endif
786 break;
787
788 case SIOCADDMULTI:
789 case SIOCDELMULTI:
790 error = (cmd == SIOCADDMULTI) ?
791 ether_addmulti(ifr, &sc->sc_ethercom) :
792 ether_delmulti(ifr, &sc->sc_ethercom);
793
794 if (error == ENETRESET) {
795 /*
796 * Multicast list has changed; set the hardware filter
797 * accordingly.
798 */
799 mb8795_init(sc);
800 error = 0;
801 }
802 break;
803
804 case SIOCGIFMEDIA:
805 case SIOCSIFMEDIA:
806 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
807 break;
808
809 default:
810 error = EINVAL;
811 break;
812 }
813
814 splx(s);
815
816 #if 0
817 DPRINTF(("DEBUG: mb8795_ioctl(0x%lx) returning %d\n",
818 cmd,error));
819 #endif
820
821 return (error);
822 }
823
824 /*
825 * Setup output on interface.
826 * Get another datagram to send off of the interface queue, and map it to the
827 * interface before starting the output.
828 * Called only at splnet or interrupt level.
829 */
830 void
831 mb8795_start(ifp)
832 struct ifnet *ifp;
833 {
834 struct mb8795_softc *sc = ifp->if_softc;
835 struct mbuf *m;
836 int s;
837
838 DPRINTF(("%s: mb8795_start()\n",sc->sc_dev.dv_xname));
839
840 #ifdef DIAGNOSTIC
841 IFQ_POLL(&ifp->if_snd, m);
842 if (m == 0) {
843 panic("%s: No packet to start\n",
844 sc->sc_dev.dv_xname);
845 }
846 #endif
847
848 while (1) {
849 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
850 return;
851
852 #if 0
853 return; /* @@@ Turn off xmit for debugging */
854 #endif
855
856 ifp->if_flags |= IFF_OACTIVE;
857
858 IFQ_DEQUEUE(&ifp->if_snd, m);
859 if (m == 0) {
860 ifp->if_flags &= ~IFF_OACTIVE;
861 return;
862 }
863
864 #if NBPFILTER > 0
865 /*
866 * Pass packet to bpf if there is a listener.
867 */
868 if (ifp->if_bpf)
869 bpf_mtap(ifp->if_bpf, m);
870 #endif
871
872 s = spldma();
873 IF_ENQUEUE(&sc->sc_tx_snd, m);
874 if (sc->sc_tx_loaded == 0)
875 mb8795_start_dma(ifp);
876 splx(s);
877
878 ifp->if_flags &= ~IFF_OACTIVE;
879 }
880
881 }
882
883 void
884 mb8795_start_dma(ifp)
885 struct ifnet *ifp;
886 {
887 int error;
888 struct mb8795_softc *sc = ifp->if_softc;
889
890 DPRINTF(("%s: mb8795_start_dma()\n",sc->sc_dev.dv_xname));
891
892 #if (defined(DIAGNOSTIC))
893 {
894 u_char txstat;
895 txstat = bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_TXSTAT);
896 if (!(txstat & XE_TXSTAT_READY)) {
897 /* @@@ I used to panic here, but then it paniced once.
898 * Let's see if I can just reset instead. [ dbj 980706.1900 ]
899 */
900 printf("%s: transmitter not ready\n",
901 sc->sc_dev.dv_xname);
902 ifp->if_flags &= ~IFF_RUNNING;
903 mb8795_init(sc);
904 return;
905 }
906 }
907 #endif
908
909 #if 0
910 return; /* @@@ Turn off xmit for debugging */
911 #endif
912
913 IF_DEQUEUE(&sc->sc_tx_snd, sc->sc_tx_mb_head);
914 if (sc->sc_tx_mb_head == 0) {
915 #ifdef DIAGNOSTIC
916 panic("%s: No packet to start_dma\n",
917 sc->sc_dev.dv_xname);
918 #endif
919 return;
920 }
921
922 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXSTAT, XE_TXSTAT_CLEAR);
923
924 ifp->if_timer = 5;
925
926 /* The following is a next specific hack that should
927 * probably be moved out of MI code.
928 * This macro assumes it can move forward as needed
929 * in the buffer. Perhaps it should zero the extra buffer.
930 */
931 #define REALIGN_DMABUF(s,l) \
932 { (s) = ((u_char *)(((unsigned)(s)+DMA_BEGINALIGNMENT-1) \
933 &~(DMA_BEGINALIGNMENT-1))); \
934 (l) = ((u_char *)(((unsigned)((s)+(l))+DMA_ENDALIGNMENT-1) \
935 &~(DMA_ENDALIGNMENT-1)))-(s);}
936
937 #if 0
938 error = bus_dmamap_load_mbuf(sc->sc_tx_dmat,
939 sc->sc_tx_dmamap, sc->sc_tx_mb_head, BUS_DMA_NOWAIT);
940 #else
941 {
942 u_char *buf = sc->sc_txbuf;
943 int buflen = 0;
944 struct mbuf *m = sc->sc_tx_mb_head;
945 buflen = m->m_pkthdr.len;
946
947 /* Fix runt packets, @@@ memory overrun */
948 if (buflen < ETHERMIN+sizeof(struct ether_header)) {
949 buflen = ETHERMIN+sizeof(struct ether_header);
950 }
951
952 {
953 u_char *p = buf;
954 for (m=sc->sc_tx_mb_head; m; m = m->m_next) {
955 if (m->m_len == 0) continue;
956 bcopy(mtod(m, u_char *), p, m->m_len);
957 p += m->m_len;
958 }
959 }
960
961 error = bus_dmamap_load(sc->sc_tx_dmat, sc->sc_tx_dmamap,
962 buf,buflen,NULL,BUS_DMA_NOWAIT);
963 }
964 #endif
965 if (error) {
966 printf("%s: can't load mbuf chain, error = %d\n",
967 sc->sc_dev.dv_xname, error);
968 m_freem(sc->sc_tx_mb_head);
969 sc->sc_tx_mb_head = NULL;
970 return;
971 }
972
973 #ifdef DIAGNOSTIC
974 if (sc->sc_tx_loaded != 0) {
975 panic("%s: sc->sc_tx_loaded is %d",sc->sc_dev.dv_xname,
976 sc->sc_tx_loaded);
977 }
978 #endif
979
980 bus_dmamap_sync(sc->sc_tx_dmat, sc->sc_tx_dmamap, 0,
981 sc->sc_tx_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
982
983 nextdma_start(sc->sc_tx_nd, DMACSR_SETWRITE);
984
985 ifp->if_opackets++;
986 }
987
988 /****************************************************************/
989
990 void
991 mb8795_txdma_completed(map, arg)
992 bus_dmamap_t map;
993 void *arg;
994 {
995 struct mb8795_softc *sc = arg;
996
997 DPRINTF(("%s: mb8795_txdma_completed()\n",sc->sc_dev.dv_xname));
998
999 #ifdef DIAGNOSTIC
1000 if (!sc->sc_tx_loaded) {
1001 panic("%s: tx completed never loaded ",sc->sc_dev.dv_xname);
1002 }
1003 if (map != sc->sc_tx_dmamap) {
1004 panic("%s: unexpected tx completed map",sc->sc_dev.dv_xname);
1005 }
1006
1007 #endif
1008 }
1009
1010 void
1011 mb8795_txdma_shutdown(arg)
1012 void *arg;
1013 {
1014 struct mb8795_softc *sc = arg;
1015 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1016
1017 DPRINTF(("%s: mb8795_txdma_shutdown()\n",sc->sc_dev.dv_xname));
1018
1019 #ifdef DIAGNOSTIC
1020 if (!sc->sc_tx_loaded) {
1021 panic("%s: tx shutdown never loaded ",sc->sc_dev.dv_xname);
1022 }
1023 #endif
1024
1025 {
1026
1027 if (sc->sc_tx_loaded) {
1028 bus_dmamap_sync(sc->sc_tx_dmat, sc->sc_tx_dmamap,
1029 0, sc->sc_tx_dmamap->dm_mapsize,
1030 BUS_DMASYNC_POSTWRITE);
1031 bus_dmamap_unload(sc->sc_tx_dmat, sc->sc_tx_dmamap);
1032 m_freem(sc->sc_tx_mb_head);
1033 sc->sc_tx_mb_head = NULL;
1034
1035 sc->sc_tx_loaded--;
1036 }
1037
1038 #ifdef DIAGNOSTIC
1039 if (sc->sc_tx_loaded != 0) {
1040 panic("%s: sc->sc_tx_loaded is %d",sc->sc_dev.dv_xname,
1041 sc->sc_tx_loaded);
1042 }
1043 #endif
1044
1045 ifp->if_timer = 0;
1046
1047 if ((ifp->if_flags & IFF_RUNNING) && !IF_IS_EMPTY(&sc->sc_tx_snd)) {
1048 mb8795_start_dma(ifp);
1049 }
1050
1051 }
1052
1053 #if 0
1054 /* Enable ready interrupt */
1055 bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXMASK,
1056 bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_TXMASK)
1057 | XE_TXMASK_READYIE);
1058 #endif
1059 }
1060
1061
1062 void
1063 mb8795_rxdma_completed(map, arg)
1064 bus_dmamap_t map;
1065 void *arg;
1066 {
1067 struct mb8795_softc *sc = arg;
1068 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1069
1070 if (ifp->if_flags & IFF_RUNNING) {
1071 sc->sc_rx_completed_idx++;
1072 sc->sc_rx_completed_idx %= MB8795_NRXBUFS;
1073
1074 DPRINTF(("%s: mb8795_rxdma_completed(), sc->sc_rx_completed_idx = %d\n",
1075 sc->sc_dev.dv_xname, sc->sc_rx_completed_idx));
1076
1077 #if (defined(DIAGNOSTIC))
1078 if (map != sc->sc_rx_dmamap[sc->sc_rx_completed_idx]) {
1079 panic("%s: Unexpected rx dmamap completed\n",
1080 sc->sc_dev.dv_xname);
1081 }
1082 #endif
1083 }
1084 #ifdef DIAGNOSTIC
1085 else
1086 DPRINTF(("%s: Unexpected rx dmamap completed while if not running\n",
1087 sc->sc_dev.dv_xname));
1088 #endif
1089 }
1090
1091 void
1092 mb8795_rxdma_shutdown(arg)
1093 void *arg;
1094 {
1095 struct mb8795_softc *sc = arg;
1096 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1097
1098 if (ifp->if_flags & IFF_RUNNING) {
1099 DPRINTF(("%s: mb8795_rxdma_shutdown(), restarting.\n",
1100 sc->sc_dev.dv_xname));
1101
1102 nextdma_start(sc->sc_rx_nd, DMACSR_SETREAD);
1103 }
1104 #ifdef DIAGNOSTIC
1105 else
1106 DPRINTF(("%s: Unexpected rx dma shutdown while if not running\n",
1107 sc->sc_dev.dv_xname));
1108 #endif
1109 }
1110
1111 /*
1112 * load a dmamap with a freshly allocated mbuf
1113 */
1114 struct mbuf *
1115 mb8795_rxdmamap_load(sc,map)
1116 struct mb8795_softc *sc;
1117 bus_dmamap_t map;
1118 {
1119 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1120 struct mbuf *m;
1121 int error;
1122
1123 MGETHDR(m, M_DONTWAIT, MT_DATA);
1124 if (m) {
1125 MCLGET(m, M_DONTWAIT);
1126 if ((m->m_flags & M_EXT) == 0) {
1127 m_freem(m);
1128 m = NULL;
1129 } else {
1130 m->m_len = MCLBYTES;
1131 }
1132 }
1133 if (!m) {
1134 /* @@@ Handle this gracefully by reusing a scratch buffer
1135 * or something.
1136 */
1137 panic("Unable to get memory for incoming ethernet\n");
1138 }
1139
1140 /* Align buffer, @@@ next specific.
1141 * perhaps should be using M_ALIGN here instead?
1142 * First we give us a little room to align with.
1143 */
1144 {
1145 u_char *buf = m->m_data;
1146 int buflen = m->m_len;
1147 buflen -= DMA_ENDALIGNMENT+DMA_BEGINALIGNMENT;
1148 REALIGN_DMABUF(buf, buflen);
1149 m->m_data = buf;
1150 m->m_len = buflen;
1151 }
1152
1153 m->m_pkthdr.rcvif = ifp;
1154 m->m_pkthdr.len = m->m_len;
1155
1156 error = bus_dmamap_load_mbuf(sc->sc_rx_dmat,
1157 map, m, BUS_DMA_NOWAIT);
1158
1159 bus_dmamap_sync(sc->sc_rx_dmat, map, 0,
1160 map->dm_mapsize, BUS_DMASYNC_PREREAD);
1161
1162 if (error) {
1163 DPRINTF(("DEBUG: m->m_data = %p, m->m_len = %d\n",
1164 m->m_data, m->m_len));
1165 DPRINTF(("DEBUG: MCLBYTES = %d, map->_dm_size = %ld\n",
1166 MCLBYTES, map->_dm_size));
1167
1168 panic("%s: can't load rx mbuf chain, error = %d\n",
1169 sc->sc_dev.dv_xname, error);
1170 m_freem(m);
1171 m = NULL;
1172 }
1173
1174 return(m);
1175 }
1176
1177 bus_dmamap_t
1178 mb8795_rxdma_continue(arg)
1179 void *arg;
1180 {
1181 struct mb8795_softc *sc = arg;
1182 bus_dmamap_t map = NULL;
1183 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1184
1185 if (ifp->if_flags & IFF_RUNNING) {
1186 /*
1187 * Currently, starts dumping new packets if the buffers
1188 * fill up. This should probably reclaim unhandled
1189 * buffers instead so we drop older packets instead
1190 * of newer ones.
1191 */
1192 if (((sc->sc_rx_loaded_idx+1)%MB8795_NRXBUFS) != sc->sc_rx_handled_idx){
1193 sc->sc_rx_loaded_idx++;
1194 sc->sc_rx_loaded_idx %= MB8795_NRXBUFS;
1195 map = sc->sc_rx_dmamap[sc->sc_rx_loaded_idx];
1196
1197 DPRINTF(("%s: mb8795_rxdma_continue() sc->sc_rx_loaded_idx = %d\nn",
1198 sc->sc_dev.dv_xname,sc->sc_rx_loaded_idx));
1199 }
1200 #if (defined(DIAGNOSTIC))
1201 else {
1202 DPRINTF(("%s: out of receive DMA buffers\n",sc->sc_dev.dv_xname));
1203 }
1204 #endif
1205 }
1206 #ifdef DIAGNOSTIC
1207 else
1208 panic("%s: Unexpected rx dma continue while if not running\n",
1209 sc->sc_dev.dv_xname);
1210 #endif
1211
1212 return(map);
1213 }
1214
1215 bus_dmamap_t
1216 mb8795_txdma_continue(arg)
1217 void *arg;
1218 {
1219 struct mb8795_softc *sc = arg;
1220 bus_dmamap_t map;
1221
1222 DPRINTF(("%s: mb8795_txdma_continue()\n",sc->sc_dev.dv_xname));
1223
1224 if (sc->sc_tx_loaded) {
1225 map = NULL;
1226 } else {
1227 map = sc->sc_tx_dmamap;
1228 sc->sc_tx_loaded++;
1229 }
1230
1231 #ifdef DIAGNOSTIC
1232 if (sc->sc_tx_loaded != 1) {
1233 panic("%s: sc->sc_tx_loaded is %d",sc->sc_dev.dv_xname,
1234 sc->sc_tx_loaded);
1235 }
1236 #endif
1237
1238 return(map);
1239 }
1240
1241 /****************************************************************/
1242