if_de.c revision 1.3 1 /* $NetBSD: if_de.c,v 1.3 2000/06/04 06:17:03 matt Exp $ */
2 /*
3 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
4 * Copyright (c) 2000 Ludd, University of Lule}, Sweden.
5 * All rights reserved.
6 *
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)if_de.c 7.12 (Berkeley) 12/16/90
37 */
38
39 /*
40 * DEC DEUNA interface
41 *
42 * Lou Salkind
43 * New York University
44 *
45 * Rewritten by Ragge 30 April 2000 to match new world.
46 *
47 * TODO:
48 * timeout routine (get statistics)
49 */
50
51 #include "opt_inet.h"
52 #include "opt_iso.h"
53 #include "bpfilter.h"
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/mbuf.h>
58 #include <sys/buf.h>
59 #include <sys/protosw.h>
60 #include <sys/socket.h>
61 #include <sys/ioctl.h>
62 #include <sys/errno.h>
63 #include <sys/syslog.h>
64 #include <sys/device.h>
65
66 #include <net/if.h>
67 #include <net/if_ether.h>
68 #include <net/if_dl.h>
69
70 #ifdef INET
71 #include <netinet/in.h>
72 #include <netinet/if_inarp.h>
73 #endif
74
75 #if NBPFILTER > 0
76 #include <net/bpf.h>
77 #include <net/bpfdesc.h>
78 #endif
79
80 #include <machine/bus.h>
81
82 #include <dev/qbus/ubavar.h>
83 #include <dev/qbus/if_dereg.h>
84
85 #include "ioconf.h"
86
87 /*
88 * Be careful with transmit/receive buffers, each entry steals 4 map
89 * registers, and there is only 496 on one unibus...
90 */
91 #define NRCV 10 /* number of receive buffers (must be > 1) */
92 #define NXMT 10 /* number of transmit buffers */
93
94 /*
95 * Structure containing the elements that must be in DMA-safe memory.
96 */
97 struct de_cdata {
98 /* the following structures are always mapped in */
99 struct de_pcbb dc_pcbb; /* port control block */
100 struct de_ring dc_xrent[NXMT]; /* transmit ring entrys */
101 struct de_ring dc_rrent[NRCV]; /* receive ring entrys */
102 struct de_udbbuf dc_udbbuf; /* UNIBUS data buffer */
103 char dc_xbuf[NXMT][ETHER_MAX_LEN];
104 /* end mapped area */
105 };
106
107 /*
108 * Ethernet software status per interface.
109 *
110 * Each interface is referenced by a network interface structure,
111 * ds_if, which the routing code uses to locate the interface.
112 * This structure contains the output queue for the interface, its address, ...
113 * We also have, for each interface, a UBA interface structure, which
114 * contains information about the UNIBUS resources held by the interface:
115 * map registers, buffered data paths, etc. Information is cached in this
116 * structure for use by the if_uba.c routines in running the interface
117 * efficiently.
118 */
119 struct de_softc {
120 struct device sc_dev; /* Configuration common part */
121 struct evcnt sc_intrcnt; /* Interrupt counting */
122 struct ethercom sc_ec; /* Ethernet common part */
123 #define sc_if sc_ec.ec_if /* network-visible interface */
124 bus_space_tag_t sc_iot;
125 bus_addr_t sc_ioh;
126 bus_dma_tag_t sc_dmat;
127 bus_dmamap_t sc_cmap;
128 struct de_cdata *sc_dedata; /* Control structure */
129 struct de_cdata *sc_pdedata; /* Bus-mapped control structure */
130 #ifdef notdef
131 bus_dmamap_t sc_xmtmap[NXMT]; /* unibus xmit maps */
132 struct mbuf *sc_txmbuf[NXMT];
133 #endif
134 bus_dmamap_t sc_rcvmap[NRCV]; /* unibus receive maps */
135 struct mbuf *sc_rxmbuf[NRCV];
136 int sc_nexttx; /* next tx descriptor to put data on */
137 int sc_nextrx; /* next rx descriptor for recv */
138 int sc_inq; /* # if xmit packets in queue */
139 int sc_lastack; /* Last handled rx descriptor */
140 void *sc_sh; /* shutdownhook cookie */
141 };
142
143 static int dematch(struct device *, struct cfdata *, void *);
144 static void deattach(struct device *, struct device *, void *);
145 static void dewait(struct de_softc *, char *);
146 static void deinit(struct de_softc *);
147 static int deioctl(struct ifnet *, u_long, caddr_t);
148 static void dereset(struct device *);
149 static void destart(struct ifnet *);
150 static void derecv(struct de_softc *);
151 static void dexmit(struct de_softc *);
152 static void deintr(void *);
153 static int de_add_rxbuf(struct de_softc *, int);
154 static void desetup(struct de_softc *sc);
155 static void deshutdown(void *);
156
157 struct cfattach de_ca = {
158 sizeof(struct de_softc), dematch, deattach
159 };
160
161 #define DE_WCSR(csr, val) \
162 bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
163 #define DE_WLOW(val) \
164 bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0, val)
165 #define DE_WHIGH(val) \
166 bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0 + 1, val)
167 #define DE_RCSR(csr) \
168 bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
169
170 #define LOWORD(x) ((int)(x) & 0xffff)
171 #define HIWORD(x) (((int)(x) >> 16) & 0x3)
172 /*
173 * Interface exists: make available by filling in network interface
174 * record. System will initialize the interface when it is ready
175 * to accept packets. We get the ethernet address here.
176 */
177 void
178 deattach(struct device *parent, struct device *self, void *aux)
179 {
180 struct uba_attach_args *ua = aux;
181 struct de_softc *sc = (struct de_softc *)self;
182 struct ifnet *ifp = &sc->sc_if;
183 u_int8_t myaddr[ETHER_ADDR_LEN];
184 int csr1, rseg, error, i;
185 bus_dma_segment_t seg;
186 char *c;
187
188 sc->sc_iot = ua->ua_iot;
189 sc->sc_ioh = ua->ua_ioh;
190 sc->sc_dmat = ua->ua_dmat;
191
192 /*
193 * What kind of a board is this?
194 * The error bits 4-6 in pcsr1 are a device id as long as
195 * the high byte is zero.
196 */
197 csr1 = DE_RCSR(DE_PCSR1);
198 if (csr1 & 0xff60)
199 c = "broken";
200 else if (csr1 & 0x10)
201 c = "delua";
202 else
203 c = "deuna";
204
205 /*
206 * Reset the board and temporarily map
207 * the pcbb buffer onto the Unibus.
208 */
209 DE_WCSR(DE_PCSR0, 0); /* reset INTE */
210 DELAY(100);
211 DE_WCSR(DE_PCSR0, PCSR0_RSET);
212 dewait(sc, "reset");
213
214 if ((error = bus_dmamem_alloc(sc->sc_dmat,
215 sizeof(struct de_cdata), NBPG, 0, &seg, 1, &rseg,
216 BUS_DMA_NOWAIT)) != 0) {
217 printf(": unable to allocate control data, error = %d\n",
218 error);
219 goto fail_0;
220 }
221 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
222 sizeof(struct de_cdata), (caddr_t *)&sc->sc_dedata,
223 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
224 printf(": unable to map control data, error = %d\n", error);
225 goto fail_1;
226 }
227
228 if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct de_cdata),
229 1, sizeof(struct de_cdata), 0, BUS_DMA_NOWAIT,
230 &sc->sc_cmap)) != 0) {
231 printf(": unable to create control data DMA map, error = %d\n",
232 error);
233 goto fail_2;
234 }
235
236 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cmap,
237 sc->sc_dedata, sizeof(struct de_cdata), NULL,
238 BUS_DMA_NOWAIT)) != 0) {
239 printf(": unable to load control data DMA map, error = %d\n",
240 error);
241 goto fail_3;
242 }
243
244 bzero(sc->sc_dedata, sizeof(struct de_cdata));
245 sc->sc_pdedata = (struct de_cdata *)sc->sc_cmap->dm_segs[0].ds_addr;
246
247 #ifdef notdef
248 /*
249 * Create the transmit descriptor DMA maps.
250 *
251 * XXX - should allocate transmit map pages when needed, not here.
252 */
253 for (i = 0; i < NXMT; i++) {
254 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
255 MCLBYTES, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
256 &sc->sc_xmtmap[i]))) {
257 printf(": unable to create tx DMA map %d, error = %d\n",
258 i, error);
259 goto fail_4;
260 }
261 }
262 #endif
263
264 /*
265 * Create receive buffer DMA maps.
266 */
267 for (i = 0; i < NRCV; i++) {
268 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
269 MCLBYTES, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
270 &sc->sc_rcvmap[i]))) {
271 printf(": unable to create rx DMA map %d, error = %d\n",
272 i, error);
273 goto fail_5;
274 }
275 }
276
277 /*
278 * Pre-allocate the receive buffers.
279 */
280 for (i = 0; i < NRCV; i++) {
281 if ((error = de_add_rxbuf(sc, i)) != 0) {
282 printf(": unable to allocate or map rx buffer %d\n,"
283 " error = %d\n", i, error);
284 goto fail_6;
285 }
286 }
287
288 /*
289 * Tell the DEUNA about our PCB
290 */
291 DE_WCSR(DE_PCSR2, LOWORD(sc->sc_pdedata));
292 DE_WCSR(DE_PCSR3, HIWORD(sc->sc_pdedata));
293 DE_WLOW(CMD_GETPCBB);
294 dewait(sc, "pcbb");
295
296 sc->sc_dedata->dc_pcbb.pcbb0 = FC_RDPHYAD;
297 DE_WLOW(CMD_GETCMD);
298 dewait(sc, "read addr ");
299
300 bcopy((caddr_t)&sc->sc_dedata->dc_pcbb.pcbb2, myaddr, sizeof (myaddr));
301 printf("\n%s: %s, hardware address %s\n", sc->sc_dev.dv_xname, c,
302 ether_sprintf(myaddr));
303
304 uba_intr_establish(ua->ua_icookie, ua->ua_cvec, deintr,
305 sc, &sc->sc_intrcnt);
306 uba_reset_establish(dereset, &sc->sc_dev);
307 evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
308
309 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
310 ifp->if_softc = sc;
311 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX;
312 ifp->if_ioctl = deioctl;
313 ifp->if_start = destart;
314 if_attach(ifp);
315 ether_ifattach(ifp, myaddr);
316 #if NBPFILTER > 0
317 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
318 #endif
319 sc->sc_sh = shutdownhook_establish(deshutdown, sc);
320 return;
321
322 /*
323 * Free any resources we've allocated during the failed attach
324 * attempt. Do this in reverse order and fall through.
325 */
326 fail_6:
327 for (i = 0; i < NRCV; i++) {
328 if (sc->sc_rxmbuf[i] != NULL) {
329 bus_dmamap_unload(sc->sc_dmat, sc->sc_rcvmap[i]);
330 m_freem(sc->sc_rxmbuf[i]);
331 }
332 }
333 fail_5:
334 for (i = 0; i < NRCV; i++) {
335 if (sc->sc_rcvmap[i] != NULL)
336 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rcvmap[i]);
337 }
338 #ifdef notdef
339 fail_4:
340 for (i = 0; i < NXMT; i++) {
341 if (sc->sc_xmtmap[i] != NULL)
342 bus_dmamap_destroy(sc->sc_dmat, sc->sc_xmtmap[i]);
343 }
344 bus_dmamap_unload(sc->sc_dmat, sc->sc_cmap);
345 #endif
346 fail_3:
347 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cmap);
348 fail_2:
349 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_dedata,
350 sizeof(struct de_cdata));
351 fail_1:
352 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
353 fail_0:
354 return;
355 }
356
357 /*
358 * Reset of interface after UNIBUS reset.
359 */
360 void
361 dereset(struct device *dev)
362 {
363 struct de_softc *sc = (void *)dev;
364
365 sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
366 DE_WCSR(DE_PCSR0, PCSR0_RSET);
367 dewait(sc, "reset");
368 deinit(sc);
369 }
370
371 /*
372 * Initialization of interface; clear recorded pending
373 * operations, and reinitialize UNIBUS usage.
374 */
375 void
376 deinit(struct de_softc *sc)
377 {
378 struct de_cdata *dc, *pdc;
379 int s, i;
380
381 if (sc->sc_if.if_flags & IFF_RUNNING)
382 return;
383 /*
384 * Tell the DEUNA about our PCB
385 */
386 DE_WCSR(DE_PCSR2, LOWORD(sc->sc_pdedata));
387 DE_WCSR(DE_PCSR3, HIWORD(sc->sc_pdedata));
388 DE_WLOW(0); /* reset INTE */
389 DELAY(500);
390 DE_WLOW(CMD_GETPCBB);
391 dewait(sc, "pcbb");
392
393 dc = sc->sc_dedata;
394 pdc = sc->sc_pdedata;
395 /* set the transmit and receive ring header addresses */
396 dc->dc_pcbb.pcbb0 = FC_WTRING;
397 dc->dc_pcbb.pcbb2 = LOWORD(&pdc->dc_udbbuf);
398 dc->dc_pcbb.pcbb4 = HIWORD(&pdc->dc_udbbuf);
399
400 dc->dc_udbbuf.b_tdrbl = LOWORD(&pdc->dc_xrent[0]);
401 dc->dc_udbbuf.b_tdrbh = HIWORD(&pdc->dc_xrent[0]);
402 dc->dc_udbbuf.b_telen = sizeof (struct de_ring) / sizeof(u_int16_t);
403 dc->dc_udbbuf.b_trlen = NXMT;
404 dc->dc_udbbuf.b_rdrbl = LOWORD(&pdc->dc_rrent[0]);
405 dc->dc_udbbuf.b_rdrbh = HIWORD(&pdc->dc_rrent[0]);
406 dc->dc_udbbuf.b_relen = sizeof (struct de_ring) / sizeof(u_int16_t);
407 dc->dc_udbbuf.b_rrlen = NRCV;
408
409 DE_WLOW(CMD_GETCMD);
410 dewait(sc, "wtring");
411
412 desetup(sc);
413
414 /* Link the transmit buffers to the descriptors */
415 for (i = 0; i < NXMT; i++) {
416 dc->dc_xrent[i].r_flags = 0;
417 dc->dc_xrent[i].r_segbl = LOWORD(&pdc->dc_xbuf[i][0]);
418 dc->dc_xrent[i].r_segbh = HIWORD(&pdc->dc_xbuf[i][0]);
419 }
420
421 for (i = 0; i < NRCV; i++)
422 dc->dc_rrent[i].r_flags = RFLG_OWN;
423 sc->sc_nexttx = sc->sc_inq = sc->sc_lastack = sc->sc_nextrx = 0;
424
425 /* start up the board (rah rah) */
426 s = splnet();
427 sc->sc_if.if_flags |= IFF_RUNNING;
428 DE_WLOW(PCSR0_INTE); /* Change to interrupts */
429 DELAY(500);
430 DE_WLOW(CMD_START|PCSR0_INTE);
431 dewait(sc, "start");
432 DE_WLOW(CMD_PDMD|PCSR0_INTE);
433 dewait(sc, "initpoll");
434 splx(s);
435 }
436
437 /*
438 * Setup output on interface.
439 * Get another datagram to send off of the interface queue,
440 * and map it to the interface before starting the output.
441 * Must be called from ipl >= our interrupt level.
442 */
443 void
444 destart(struct ifnet *ifp)
445 {
446 struct de_softc *sc = ifp->if_softc;
447 struct de_cdata *dc;
448 struct mbuf *m;
449 int idx, s, running;
450
451 /*
452 * the following test is necessary, since
453 * the code is not reentrant and we have
454 * multiple transmission buffers.
455 */
456 if (ifp->if_flags & IFF_OACTIVE) /* Too much to do already */
457 return;
458
459 if (ifp->if_snd.ifq_head == 0) /* Nothing to do at all */
460 return;
461
462 s = splimp();
463 dc = sc->sc_dedata;
464 running = (sc->sc_inq != 0);
465 while (sc->sc_inq < (NXMT - 1)) {
466
467 idx = sc->sc_nexttx;
468 IF_DEQUEUE(&ifp->if_snd, m);
469 if (m == 0)
470 goto out;
471
472 #if NBPFILTER > 0
473 if (ifp->if_bpf)
474 bpf_mtap(ifp->if_bpf, m);
475 #endif
476 m_copydata(m, 0, m->m_pkthdr.len, &dc->dc_xbuf[idx][0]);
477 dc->dc_xrent[idx].r_slen = m->m_pkthdr.len;
478 dc->dc_xrent[idx].r_tdrerr = 0;
479 dc->dc_xrent[idx].r_flags = XFLG_STP|XFLG_ENP|XFLG_OWN;
480 m_freem(m);
481
482 sc->sc_inq++;
483 if (++sc->sc_nexttx == NXMT)
484 sc->sc_nexttx = 0;
485 ifp->if_timer = 5; /* If transmit logic dies */
486 }
487 if (sc->sc_inq == (NXMT - 1))
488 ifp->if_flags |= IFF_OACTIVE;
489
490 out: if (running == 0) {
491 DE_WLOW(PCSR0_INTE|CMD_PDMD);
492 dewait(sc, "poll");
493 }
494
495 splx(s);
496 }
497
498 /*
499 * Command done interrupt.
500 */
501 void
502 deintr(void *arg)
503 {
504 struct de_softc *sc = arg;
505 short csr0, csr1;
506
507 /* save flags right away - clear out interrupt bits */
508 csr0 = DE_RCSR(DE_PCSR0);
509 csr1 = DE_RCSR(DE_PCSR1);
510 DE_WHIGH(csr0 >> 8);
511
512 if (csr0 & PCSR0_RXI)
513 derecv(sc);
514
515 if (csr0 & PCSR0_TXI)
516 dexmit(sc);
517
518 /* Should never end up here */
519 if (csr0 & PCSR0_PCEI) {
520 printf("%s: Port command error interrupt\n",
521 sc->sc_dev.dv_xname);
522 }
523
524 if (csr0 & PCSR0_SERI) {
525 printf("%s: Status error interrupt\n", sc->sc_dev.dv_xname);
526 }
527
528 if (csr0 & PCSR0_RCBI) {
529 printf("%s: Receive buffer unavail interrupt\n",
530 sc->sc_dev.dv_xname);
531 DE_WLOW(PCSR0_INTE|CMD_PDMD);
532 dewait(sc, "repoll");
533 }
534 destart(&sc->sc_if);
535 }
536
537 void
538 dexmit(struct de_softc *sc)
539 {
540 struct ifnet *ifp = &sc->sc_if;
541 struct de_ring *rp;
542
543 /*
544 * Poll transmit ring and check status.
545 * Then free buffer space and check for
546 * more transmit requests.
547 */
548 rp = &sc->sc_dedata->dc_xrent[sc->sc_lastack];
549 while ((rp->r_flags & XFLG_OWN) == 0) {
550 int idx = sc->sc_lastack;
551
552 if (idx == sc->sc_nexttx)
553 break;
554 if (rp->r_flags & XFLG_ENP)
555 ifp->if_opackets++;
556 if (rp->r_flags & (XFLG_ERRS|XFLG_MTCH|XFLG_ONE|XFLG_MORE)) {
557 if (rp->r_flags & XFLG_ERRS) {
558 ifp->if_oerrors++;
559 } else if (rp->r_flags & XFLG_ONE) {
560 ifp->if_collisions++;
561 } else if (rp->r_flags & XFLG_MORE) {
562 ifp->if_collisions += 3;
563 }
564 /* else if (rp->r_flags & XFLG_MTCH)
565 * Matches ourself, but why care?
566 * Let upper layer deal with this.
567 */
568 }
569 if (++sc->sc_lastack == NXMT)
570 sc->sc_lastack = 0;
571 sc->sc_inq--;
572 rp = &sc->sc_dedata->dc_xrent[sc->sc_lastack];
573 }
574 ifp->if_flags &= ~IFF_OACTIVE;
575 if (sc->sc_inq == 0)
576 ifp->if_timer = 0;
577 }
578
579 /*
580 * Ethernet interface receiver interface.
581 * If input error just drop packet.
582 * Otherwise purge input buffered data path and examine
583 * packet to determine type. If can't determine length
584 * from type, then have to drop packet. Othewise decapsulate
585 * packet based on type and pass to type specific higher-level
586 * input routine.
587 */
588 void
589 derecv(struct de_softc *sc)
590 {
591 struct ifnet *ifp = &sc->sc_if;
592 struct de_ring *rp;
593 struct mbuf *m;
594 int len;
595
596 rp = &sc->sc_dedata->dc_rrent[sc->sc_nextrx];
597 while ((rp->r_flags & RFLG_OWN) == 0) {
598 ifp->if_ipackets++;
599 /* check for errors */
600 if ((rp->r_flags & (RFLG_ERRS|RFLG_FRAM|RFLG_OFLO|RFLG_CRC)) ||
601 (rp->r_flags&(RFLG_STP|RFLG_ENP)) != (RFLG_STP|RFLG_ENP) ||
602 (rp->r_lenerr & (RERR_BUFL|RERR_UBTO))) {
603 ifp->if_ierrors++;
604 goto next;
605 }
606 m = sc->sc_rxmbuf[sc->sc_nextrx];
607 len = (rp->r_lenerr&RERR_MLEN) - ETHER_CRC_LEN;
608 de_add_rxbuf(sc, sc->sc_nextrx);
609 m->m_pkthdr.rcvif = ifp;
610 m->m_pkthdr.len = m->m_len = len;
611
612 #if NBPFILTER > 0
613 if (ifp->if_bpf) {
614 struct ether_header *eh;
615
616 eh = mtod(m, struct ether_header *);
617 bpf_mtap(ifp->if_bpf, m);
618 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
619 bcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
620 ETHER_ADDR_LEN) != 0 &&
621 (ETHER_IS_MULTICAST(eh->ether_dhost) == 0)) {
622 m_freem(m);
623 goto next;
624 }
625 }
626 #endif
627 (*ifp->if_input)(ifp, m);
628
629 /* hang the receive buffer again */
630 next: rp->r_lenerr = 0;
631 rp->r_flags = RFLG_OWN;
632
633 /* check next receive buffer */
634 if (++sc->sc_nextrx == NRCV)
635 sc->sc_nextrx = 0;
636 rp = &sc->sc_dedata->dc_rrent[sc->sc_nextrx];
637 }
638 }
639
640 /*
641 * Add a receive buffer to the indicated descriptor.
642 */
643 int
644 de_add_rxbuf(sc, i)
645 struct de_softc *sc;
646 int i;
647 {
648 struct mbuf *m;
649 struct de_ring *rp;
650 vaddr_t addr;
651 int error;
652
653 MGETHDR(m, M_DONTWAIT, MT_DATA);
654 if (m == NULL)
655 return (ENOBUFS);
656
657 MCLGET(m, M_DONTWAIT);
658 if ((m->m_flags & M_EXT) == 0) {
659 m_freem(m);
660 return (ENOBUFS);
661 }
662
663 if (sc->sc_rxmbuf[i] != NULL)
664 bus_dmamap_unload(sc->sc_dmat, sc->sc_rcvmap[i]);
665
666 error = bus_dmamap_load(sc->sc_dmat, sc->sc_rcvmap[i],
667 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
668 if (error)
669 panic("%s: can't load rx DMA map %d, error = %d\n",
670 sc->sc_dev.dv_xname, i, error);
671 sc->sc_rxmbuf[i] = m;
672
673 bus_dmamap_sync(sc->sc_dmat, sc->sc_rcvmap[i], 0,
674 sc->sc_rcvmap[i]->dm_mapsize, BUS_DMASYNC_PREREAD);
675
676 /*
677 * We know that the mbuf cluster is page aligned. Also, be sure
678 * that the IP header will be longword aligned.
679 */
680 m->m_data += 2;
681 addr = sc->sc_rcvmap[i]->dm_segs[0].ds_addr + 2;
682 rp = &sc->sc_dedata->dc_rrent[i];
683 rp->r_lenerr = 0;
684 rp->r_segbl = LOWORD(addr);
685 rp->r_segbh = HIWORD(addr);
686 rp->r_slen = m->m_ext.ext_size - 2;
687 rp->r_flags = RFLG_OWN;
688
689 return (0);
690 }
691
692
693 /*
694 * Process an ioctl request.
695 */
696 int
697 deioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
698 {
699 struct ifaddr *ifa = (struct ifaddr *)data;
700 struct ifreq *ifr = (struct ifreq *)data;
701 struct de_softc *sc = ifp->if_softc;
702 int s = splnet(), error = 0;
703
704 switch (cmd) {
705
706 case SIOCSIFADDR:
707 ifp->if_flags |= IFF_UP;
708 switch (ifa->ifa_addr->sa_family) {
709 #ifdef INET
710 case AF_INET:
711 deinit(sc);
712 arp_ifinit(ifp, ifa);
713 break;
714 #endif
715 }
716 break;
717
718 case SIOCSIFFLAGS:
719 if ((ifp->if_flags & IFF_UP) == 0 &&
720 (ifp->if_flags & IFF_RUNNING) != 0) {
721 /*
722 * If interface is marked down and it is running,
723 * stop it.
724 */
725 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
726 DE_WCSR(DE_PCSR0, PCSR0_RSET);
727 dewait(sc, "down");
728 } else if ((ifp->if_flags & IFF_UP) != 0 &&
729 (ifp->if_flags & IFF_RUNNING) == 0) {
730 /*
731 * If interface it marked up and it is stopped, then
732 * start it.
733 */
734 deinit(sc);
735 } else if ((ifp->if_flags & IFF_UP) != 0) {
736 /*
737 * Send a new setup packet to match any new changes.
738 * (Like IFF_PROMISC etc)
739 */
740 desetup(sc);
741 }
742 break;
743
744 case SIOCADDMULTI:
745 case SIOCDELMULTI:
746 /*
747 * Update our multicast list.
748 */
749 error = (cmd == SIOCADDMULTI) ?
750 ether_addmulti(ifr, &sc->sc_ec):
751 ether_delmulti(ifr, &sc->sc_ec);
752
753 if (error == ENETRESET) {
754 /*
755 * Multicast list has changed; set the hardware filter
756 * accordingly.
757 */
758 desetup(sc);
759 error = 0;
760 }
761 break;
762
763 default:
764 error = EINVAL;
765 }
766 splx(s);
767 return (error);
768 }
769
770 /*
771 * Await completion of the named function
772 * and check for errors.
773 */
774 void
775 dewait(struct de_softc *sc, char *fn)
776 {
777 int csr0, s;
778
779 s = splimp();
780 while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
781 ;
782 csr0 = DE_RCSR(DE_PCSR0);
783 DE_WHIGH(csr0 >> 8);
784 if (csr0 & PCSR0_PCEI) {
785 char bits[64];
786
787 printf("%s: %s failed, csr0=%s ", sc->sc_dev.dv_xname, fn,
788 bitmask_snprintf(csr0, PCSR0_BITS, bits, sizeof(bits)));
789 printf("csr1=%s\n", bitmask_snprintf(DE_RCSR(DE_PCSR1),
790 PCSR1_BITS, bits, sizeof(bits)));
791 }
792 splx(s);
793 }
794
795 /*
796 * Changes multicast filter list/promiscous modes etc...
797 */
798 void
799 desetup(struct de_softc *sc)
800 {
801 short mode, intr;
802
803 /*
804 * XXX - so far use ALLMULTI to receive multicast packets.
805 */
806 sc->sc_if.if_flags &= ~IFF_ALLMULTI;
807 if (sc->sc_ec.ec_multiaddrs.lh_first)
808 sc->sc_if.if_flags |= IFF_ALLMULTI;
809
810 mode = MOD_TPAD|MOD_HDX|MOD_DRDC;
811 if (sc->sc_if.if_flags & IFF_PROMISC)
812 mode |= MOD_PROM;
813 else if (sc->sc_if.if_flags & IFF_ALLMULTI)
814 mode |= MOD_ENAL;
815
816 sc->sc_dedata->dc_pcbb.pcbb0 = FC_WTMODE;
817 sc->sc_dedata->dc_pcbb.pcbb2 = mode;
818 intr = DE_RCSR(DE_PCSR0) & PCSR0_INTE;
819 DE_WLOW(CMD_GETCMD | intr);
820 dewait(sc, "wtmode");
821 }
822
823 int
824 dematch(struct device *parent, struct cfdata *cf, void *aux)
825 {
826 struct uba_attach_args *ua = aux;
827 struct de_softc ssc;
828 struct de_softc *sc = &ssc;
829 int i;
830
831 sc->sc_iot = ua->ua_iot;
832 sc->sc_ioh = ua->ua_ioh;
833 /*
834 * Make sure self-test is finished before we screw with the board.
835 * Self-test on a DELUA can take 15 seconds (argh).
836 */
837 for (i = 0;
838 (i < 160) &&
839 (DE_RCSR(DE_PCSR0) & PCSR0_FATI) == 0 &&
840 (DE_RCSR(DE_PCSR1) & PCSR1_STMASK) == STAT_RESET;
841 ++i)
842 DELAY(50000);
843 if (((DE_RCSR(DE_PCSR0) & PCSR0_FATI) != 0) ||
844 (((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_READY) &&
845 ((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_RUN)))
846 return(0);
847
848 DE_WCSR(DE_PCSR0, 0);
849 DELAY(5000);
850 DE_WCSR(DE_PCSR0, PCSR0_RSET);
851 while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
852 ;
853 /* make board interrupt by executing a GETPCBB command */
854 DE_WCSR(DE_PCSR0, PCSR0_INTE);
855 DE_WCSR(DE_PCSR2, 0);
856 DE_WCSR(DE_PCSR3, 0);
857 DE_WCSR(DE_PCSR0, PCSR0_INTE|CMD_GETPCBB);
858 DELAY(50000);
859
860 return 1;
861 }
862
863 void
864 deshutdown(void *arg)
865 {
866 struct de_softc *sc = arg;
867
868 DE_WCSR(DE_PCSR0, PCSR0_RSET);
869 dewait(sc, "shutdown");
870 }
871
872