if_de.c revision 1.4 1 /* $NetBSD: if_de.c,v 1.4 2000/06/05 00:09:18 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_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
308 sc->sc_dev.dv_xname, "intr");
309
310 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
311 ifp->if_softc = sc;
312 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX;
313 ifp->if_ioctl = deioctl;
314 ifp->if_start = destart;
315 if_attach(ifp);
316 ether_ifattach(ifp, myaddr);
317 #if NBPFILTER > 0
318 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
319 #endif
320 sc->sc_sh = shutdownhook_establish(deshutdown, sc);
321 return;
322
323 /*
324 * Free any resources we've allocated during the failed attach
325 * attempt. Do this in reverse order and fall through.
326 */
327 fail_6:
328 for (i = 0; i < NRCV; i++) {
329 if (sc->sc_rxmbuf[i] != NULL) {
330 bus_dmamap_unload(sc->sc_dmat, sc->sc_rcvmap[i]);
331 m_freem(sc->sc_rxmbuf[i]);
332 }
333 }
334 fail_5:
335 for (i = 0; i < NRCV; i++) {
336 if (sc->sc_rcvmap[i] != NULL)
337 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rcvmap[i]);
338 }
339 #ifdef notdef
340 fail_4:
341 for (i = 0; i < NXMT; i++) {
342 if (sc->sc_xmtmap[i] != NULL)
343 bus_dmamap_destroy(sc->sc_dmat, sc->sc_xmtmap[i]);
344 }
345 bus_dmamap_unload(sc->sc_dmat, sc->sc_cmap);
346 #endif
347 fail_3:
348 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cmap);
349 fail_2:
350 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_dedata,
351 sizeof(struct de_cdata));
352 fail_1:
353 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
354 fail_0:
355 return;
356 }
357
358 /*
359 * Reset of interface after UNIBUS reset.
360 */
361 void
362 dereset(struct device *dev)
363 {
364 struct de_softc *sc = (void *)dev;
365
366 sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
367 DE_WCSR(DE_PCSR0, PCSR0_RSET);
368 dewait(sc, "reset");
369 deinit(sc);
370 }
371
372 /*
373 * Initialization of interface; clear recorded pending
374 * operations, and reinitialize UNIBUS usage.
375 */
376 void
377 deinit(struct de_softc *sc)
378 {
379 struct de_cdata *dc, *pdc;
380 int s, i;
381
382 if (sc->sc_if.if_flags & IFF_RUNNING)
383 return;
384 /*
385 * Tell the DEUNA about our PCB
386 */
387 DE_WCSR(DE_PCSR2, LOWORD(sc->sc_pdedata));
388 DE_WCSR(DE_PCSR3, HIWORD(sc->sc_pdedata));
389 DE_WLOW(0); /* reset INTE */
390 DELAY(500);
391 DE_WLOW(CMD_GETPCBB);
392 dewait(sc, "pcbb");
393
394 dc = sc->sc_dedata;
395 pdc = sc->sc_pdedata;
396 /* set the transmit and receive ring header addresses */
397 dc->dc_pcbb.pcbb0 = FC_WTRING;
398 dc->dc_pcbb.pcbb2 = LOWORD(&pdc->dc_udbbuf);
399 dc->dc_pcbb.pcbb4 = HIWORD(&pdc->dc_udbbuf);
400
401 dc->dc_udbbuf.b_tdrbl = LOWORD(&pdc->dc_xrent[0]);
402 dc->dc_udbbuf.b_tdrbh = HIWORD(&pdc->dc_xrent[0]);
403 dc->dc_udbbuf.b_telen = sizeof (struct de_ring) / sizeof(u_int16_t);
404 dc->dc_udbbuf.b_trlen = NXMT;
405 dc->dc_udbbuf.b_rdrbl = LOWORD(&pdc->dc_rrent[0]);
406 dc->dc_udbbuf.b_rdrbh = HIWORD(&pdc->dc_rrent[0]);
407 dc->dc_udbbuf.b_relen = sizeof (struct de_ring) / sizeof(u_int16_t);
408 dc->dc_udbbuf.b_rrlen = NRCV;
409
410 DE_WLOW(CMD_GETCMD);
411 dewait(sc, "wtring");
412
413 desetup(sc);
414
415 /* Link the transmit buffers to the descriptors */
416 for (i = 0; i < NXMT; i++) {
417 dc->dc_xrent[i].r_flags = 0;
418 dc->dc_xrent[i].r_segbl = LOWORD(&pdc->dc_xbuf[i][0]);
419 dc->dc_xrent[i].r_segbh = HIWORD(&pdc->dc_xbuf[i][0]);
420 }
421
422 for (i = 0; i < NRCV; i++)
423 dc->dc_rrent[i].r_flags = RFLG_OWN;
424 sc->sc_nexttx = sc->sc_inq = sc->sc_lastack = sc->sc_nextrx = 0;
425
426 /* start up the board (rah rah) */
427 s = splnet();
428 sc->sc_if.if_flags |= IFF_RUNNING;
429 DE_WLOW(PCSR0_INTE); /* Change to interrupts */
430 DELAY(500);
431 DE_WLOW(CMD_START|PCSR0_INTE);
432 dewait(sc, "start");
433 DE_WLOW(CMD_PDMD|PCSR0_INTE);
434 dewait(sc, "initpoll");
435 splx(s);
436 }
437
438 /*
439 * Setup output on interface.
440 * Get another datagram to send off of the interface queue,
441 * and map it to the interface before starting the output.
442 * Must be called from ipl >= our interrupt level.
443 */
444 void
445 destart(struct ifnet *ifp)
446 {
447 struct de_softc *sc = ifp->if_softc;
448 struct de_cdata *dc;
449 struct mbuf *m;
450 int idx, s, running;
451
452 /*
453 * the following test is necessary, since
454 * the code is not reentrant and we have
455 * multiple transmission buffers.
456 */
457 if (ifp->if_flags & IFF_OACTIVE) /* Too much to do already */
458 return;
459
460 if (ifp->if_snd.ifq_head == 0) /* Nothing to do at all */
461 return;
462
463 s = splimp();
464 dc = sc->sc_dedata;
465 running = (sc->sc_inq != 0);
466 while (sc->sc_inq < (NXMT - 1)) {
467
468 idx = sc->sc_nexttx;
469 IF_DEQUEUE(&ifp->if_snd, m);
470 if (m == 0)
471 goto out;
472
473 #if NBPFILTER > 0
474 if (ifp->if_bpf)
475 bpf_mtap(ifp->if_bpf, m);
476 #endif
477 m_copydata(m, 0, m->m_pkthdr.len, &dc->dc_xbuf[idx][0]);
478 dc->dc_xrent[idx].r_slen = m->m_pkthdr.len;
479 dc->dc_xrent[idx].r_tdrerr = 0;
480 dc->dc_xrent[idx].r_flags = XFLG_STP|XFLG_ENP|XFLG_OWN;
481 m_freem(m);
482
483 sc->sc_inq++;
484 if (++sc->sc_nexttx == NXMT)
485 sc->sc_nexttx = 0;
486 ifp->if_timer = 5; /* If transmit logic dies */
487 }
488 if (sc->sc_inq == (NXMT - 1))
489 ifp->if_flags |= IFF_OACTIVE;
490
491 out: if (running == 0) {
492 DE_WLOW(PCSR0_INTE|CMD_PDMD);
493 dewait(sc, "poll");
494 }
495
496 splx(s);
497 }
498
499 /*
500 * Command done interrupt.
501 */
502 void
503 deintr(void *arg)
504 {
505 struct de_softc *sc = arg;
506 short csr0, csr1;
507
508 /* save flags right away - clear out interrupt bits */
509 csr0 = DE_RCSR(DE_PCSR0);
510 csr1 = DE_RCSR(DE_PCSR1);
511 DE_WHIGH(csr0 >> 8);
512
513 if (csr0 & PCSR0_RXI)
514 derecv(sc);
515
516 if (csr0 & PCSR0_TXI)
517 dexmit(sc);
518
519 /* Should never end up here */
520 if (csr0 & PCSR0_PCEI) {
521 printf("%s: Port command error interrupt\n",
522 sc->sc_dev.dv_xname);
523 }
524
525 if (csr0 & PCSR0_SERI) {
526 printf("%s: Status error interrupt\n", sc->sc_dev.dv_xname);
527 }
528
529 if (csr0 & PCSR0_RCBI) {
530 printf("%s: Receive buffer unavail interrupt\n",
531 sc->sc_dev.dv_xname);
532 DE_WLOW(PCSR0_INTE|CMD_PDMD);
533 dewait(sc, "repoll");
534 }
535 destart(&sc->sc_if);
536 }
537
538 void
539 dexmit(struct de_softc *sc)
540 {
541 struct ifnet *ifp = &sc->sc_if;
542 struct de_ring *rp;
543
544 /*
545 * Poll transmit ring and check status.
546 * Then free buffer space and check for
547 * more transmit requests.
548 */
549 rp = &sc->sc_dedata->dc_xrent[sc->sc_lastack];
550 while ((rp->r_flags & XFLG_OWN) == 0) {
551 int idx = sc->sc_lastack;
552
553 if (idx == sc->sc_nexttx)
554 break;
555 if (rp->r_flags & XFLG_ENP)
556 ifp->if_opackets++;
557 if (rp->r_flags & (XFLG_ERRS|XFLG_MTCH|XFLG_ONE|XFLG_MORE)) {
558 if (rp->r_flags & XFLG_ERRS) {
559 ifp->if_oerrors++;
560 } else if (rp->r_flags & XFLG_ONE) {
561 ifp->if_collisions++;
562 } else if (rp->r_flags & XFLG_MORE) {
563 ifp->if_collisions += 3;
564 }
565 /* else if (rp->r_flags & XFLG_MTCH)
566 * Matches ourself, but why care?
567 * Let upper layer deal with this.
568 */
569 }
570 if (++sc->sc_lastack == NXMT)
571 sc->sc_lastack = 0;
572 sc->sc_inq--;
573 rp = &sc->sc_dedata->dc_xrent[sc->sc_lastack];
574 }
575 ifp->if_flags &= ~IFF_OACTIVE;
576 if (sc->sc_inq == 0)
577 ifp->if_timer = 0;
578 }
579
580 /*
581 * Ethernet interface receiver interface.
582 * If input error just drop packet.
583 * Otherwise purge input buffered data path and examine
584 * packet to determine type. If can't determine length
585 * from type, then have to drop packet. Othewise decapsulate
586 * packet based on type and pass to type specific higher-level
587 * input routine.
588 */
589 void
590 derecv(struct de_softc *sc)
591 {
592 struct ifnet *ifp = &sc->sc_if;
593 struct de_ring *rp;
594 struct mbuf *m;
595 int len;
596
597 rp = &sc->sc_dedata->dc_rrent[sc->sc_nextrx];
598 while ((rp->r_flags & RFLG_OWN) == 0) {
599 ifp->if_ipackets++;
600 /* check for errors */
601 if ((rp->r_flags & (RFLG_ERRS|RFLG_FRAM|RFLG_OFLO|RFLG_CRC)) ||
602 (rp->r_flags&(RFLG_STP|RFLG_ENP)) != (RFLG_STP|RFLG_ENP) ||
603 (rp->r_lenerr & (RERR_BUFL|RERR_UBTO))) {
604 ifp->if_ierrors++;
605 goto next;
606 }
607 m = sc->sc_rxmbuf[sc->sc_nextrx];
608 len = (rp->r_lenerr&RERR_MLEN) - ETHER_CRC_LEN;
609 de_add_rxbuf(sc, sc->sc_nextrx);
610 m->m_pkthdr.rcvif = ifp;
611 m->m_pkthdr.len = m->m_len = len;
612
613 #if NBPFILTER > 0
614 if (ifp->if_bpf) {
615 struct ether_header *eh;
616
617 eh = mtod(m, struct ether_header *);
618 bpf_mtap(ifp->if_bpf, m);
619 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
620 bcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
621 ETHER_ADDR_LEN) != 0 &&
622 (ETHER_IS_MULTICAST(eh->ether_dhost) == 0)) {
623 m_freem(m);
624 goto next;
625 }
626 }
627 #endif
628 (*ifp->if_input)(ifp, m);
629
630 /* hang the receive buffer again */
631 next: rp->r_lenerr = 0;
632 rp->r_flags = RFLG_OWN;
633
634 /* check next receive buffer */
635 if (++sc->sc_nextrx == NRCV)
636 sc->sc_nextrx = 0;
637 rp = &sc->sc_dedata->dc_rrent[sc->sc_nextrx];
638 }
639 }
640
641 /*
642 * Add a receive buffer to the indicated descriptor.
643 */
644 int
645 de_add_rxbuf(sc, i)
646 struct de_softc *sc;
647 int i;
648 {
649 struct mbuf *m;
650 struct de_ring *rp;
651 vaddr_t addr;
652 int error;
653
654 MGETHDR(m, M_DONTWAIT, MT_DATA);
655 if (m == NULL)
656 return (ENOBUFS);
657
658 MCLGET(m, M_DONTWAIT);
659 if ((m->m_flags & M_EXT) == 0) {
660 m_freem(m);
661 return (ENOBUFS);
662 }
663
664 if (sc->sc_rxmbuf[i] != NULL)
665 bus_dmamap_unload(sc->sc_dmat, sc->sc_rcvmap[i]);
666
667 error = bus_dmamap_load(sc->sc_dmat, sc->sc_rcvmap[i],
668 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
669 if (error)
670 panic("%s: can't load rx DMA map %d, error = %d\n",
671 sc->sc_dev.dv_xname, i, error);
672 sc->sc_rxmbuf[i] = m;
673
674 bus_dmamap_sync(sc->sc_dmat, sc->sc_rcvmap[i], 0,
675 sc->sc_rcvmap[i]->dm_mapsize, BUS_DMASYNC_PREREAD);
676
677 /*
678 * We know that the mbuf cluster is page aligned. Also, be sure
679 * that the IP header will be longword aligned.
680 */
681 m->m_data += 2;
682 addr = sc->sc_rcvmap[i]->dm_segs[0].ds_addr + 2;
683 rp = &sc->sc_dedata->dc_rrent[i];
684 rp->r_lenerr = 0;
685 rp->r_segbl = LOWORD(addr);
686 rp->r_segbh = HIWORD(addr);
687 rp->r_slen = m->m_ext.ext_size - 2;
688 rp->r_flags = RFLG_OWN;
689
690 return (0);
691 }
692
693
694 /*
695 * Process an ioctl request.
696 */
697 int
698 deioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
699 {
700 struct ifaddr *ifa = (struct ifaddr *)data;
701 struct ifreq *ifr = (struct ifreq *)data;
702 struct de_softc *sc = ifp->if_softc;
703 int s = splnet(), error = 0;
704
705 switch (cmd) {
706
707 case SIOCSIFADDR:
708 ifp->if_flags |= IFF_UP;
709 switch (ifa->ifa_addr->sa_family) {
710 #ifdef INET
711 case AF_INET:
712 deinit(sc);
713 arp_ifinit(ifp, ifa);
714 break;
715 #endif
716 }
717 break;
718
719 case SIOCSIFFLAGS:
720 if ((ifp->if_flags & IFF_UP) == 0 &&
721 (ifp->if_flags & IFF_RUNNING) != 0) {
722 /*
723 * If interface is marked down and it is running,
724 * stop it.
725 */
726 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
727 DE_WCSR(DE_PCSR0, PCSR0_RSET);
728 dewait(sc, "down");
729 } else if ((ifp->if_flags & IFF_UP) != 0 &&
730 (ifp->if_flags & IFF_RUNNING) == 0) {
731 /*
732 * If interface it marked up and it is stopped, then
733 * start it.
734 */
735 deinit(sc);
736 } else if ((ifp->if_flags & IFF_UP) != 0) {
737 /*
738 * Send a new setup packet to match any new changes.
739 * (Like IFF_PROMISC etc)
740 */
741 desetup(sc);
742 }
743 break;
744
745 case SIOCADDMULTI:
746 case SIOCDELMULTI:
747 /*
748 * Update our multicast list.
749 */
750 error = (cmd == SIOCADDMULTI) ?
751 ether_addmulti(ifr, &sc->sc_ec):
752 ether_delmulti(ifr, &sc->sc_ec);
753
754 if (error == ENETRESET) {
755 /*
756 * Multicast list has changed; set the hardware filter
757 * accordingly.
758 */
759 desetup(sc);
760 error = 0;
761 }
762 break;
763
764 default:
765 error = EINVAL;
766 }
767 splx(s);
768 return (error);
769 }
770
771 /*
772 * Await completion of the named function
773 * and check for errors.
774 */
775 void
776 dewait(struct de_softc *sc, char *fn)
777 {
778 int csr0, s;
779
780 s = splimp();
781 while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
782 ;
783 csr0 = DE_RCSR(DE_PCSR0);
784 DE_WHIGH(csr0 >> 8);
785 if (csr0 & PCSR0_PCEI) {
786 char bits[64];
787
788 printf("%s: %s failed, csr0=%s ", sc->sc_dev.dv_xname, fn,
789 bitmask_snprintf(csr0, PCSR0_BITS, bits, sizeof(bits)));
790 printf("csr1=%s\n", bitmask_snprintf(DE_RCSR(DE_PCSR1),
791 PCSR1_BITS, bits, sizeof(bits)));
792 }
793 splx(s);
794 }
795
796 /*
797 * Changes multicast filter list/promiscous modes etc...
798 */
799 void
800 desetup(struct de_softc *sc)
801 {
802 short mode, intr;
803
804 /*
805 * XXX - so far use ALLMULTI to receive multicast packets.
806 */
807 sc->sc_if.if_flags &= ~IFF_ALLMULTI;
808 if (sc->sc_ec.ec_multiaddrs.lh_first)
809 sc->sc_if.if_flags |= IFF_ALLMULTI;
810
811 mode = MOD_TPAD|MOD_HDX|MOD_DRDC;
812 if (sc->sc_if.if_flags & IFF_PROMISC)
813 mode |= MOD_PROM;
814 else if (sc->sc_if.if_flags & IFF_ALLMULTI)
815 mode |= MOD_ENAL;
816
817 sc->sc_dedata->dc_pcbb.pcbb0 = FC_WTMODE;
818 sc->sc_dedata->dc_pcbb.pcbb2 = mode;
819 intr = DE_RCSR(DE_PCSR0) & PCSR0_INTE;
820 DE_WLOW(CMD_GETCMD | intr);
821 dewait(sc, "wtmode");
822 }
823
824 int
825 dematch(struct device *parent, struct cfdata *cf, void *aux)
826 {
827 struct uba_attach_args *ua = aux;
828 struct de_softc ssc;
829 struct de_softc *sc = &ssc;
830 int i;
831
832 sc->sc_iot = ua->ua_iot;
833 sc->sc_ioh = ua->ua_ioh;
834 /*
835 * Make sure self-test is finished before we screw with the board.
836 * Self-test on a DELUA can take 15 seconds (argh).
837 */
838 for (i = 0;
839 (i < 160) &&
840 (DE_RCSR(DE_PCSR0) & PCSR0_FATI) == 0 &&
841 (DE_RCSR(DE_PCSR1) & PCSR1_STMASK) == STAT_RESET;
842 ++i)
843 DELAY(50000);
844 if (((DE_RCSR(DE_PCSR0) & PCSR0_FATI) != 0) ||
845 (((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_READY) &&
846 ((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_RUN)))
847 return(0);
848
849 DE_WCSR(DE_PCSR0, 0);
850 DELAY(5000);
851 DE_WCSR(DE_PCSR0, PCSR0_RSET);
852 while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
853 ;
854 /* make board interrupt by executing a GETPCBB command */
855 DE_WCSR(DE_PCSR0, PCSR0_INTE);
856 DE_WCSR(DE_PCSR2, 0);
857 DE_WCSR(DE_PCSR3, 0);
858 DE_WCSR(DE_PCSR0, PCSR0_INTE|CMD_GETPCBB);
859 DELAY(50000);
860
861 return 1;
862 }
863
864 void
865 deshutdown(void *arg)
866 {
867 struct de_softc *sc = arg;
868
869 DE_WCSR(DE_PCSR0, PCSR0_RSET);
870 dewait(sc, "shutdown");
871 }
872
873