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