if_de.c revision 1.8 1 /* $NetBSD: if_de.c,v 1.8 2000/12/14 07:15:45 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
5 * Copyright (c) 2000 Ludd, University of Lule}, Sweden.
6 * All rights reserved.
7 *
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)if_de.c 7.12 (Berkeley) 12/16/90
38 */
39
40 /*
41 * DEC DEUNA interface
42 *
43 * Lou Salkind
44 * New York University
45 *
46 * Rewritten by Ragge 30 April 2000 to match new world.
47 *
48 * TODO:
49 * timeout routine (get statistics)
50 */
51
52 #include "opt_inet.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 bus_dmamap_t sc_rcvmap[NRCV]; /* unibus receive maps */
131 struct mbuf *sc_rxmbuf[NRCV];
132 int sc_xindex; /* UNA index into transmit chain */
133 int sc_rindex; /* UNA index into receive chain */
134 int sc_xfree; /* index for next transmit buffer */
135 int sc_nxmit; /* # of transmits in progress */
136 void *sc_sh; /* shutdownhook cookie */
137 };
138
139 static int dematch(struct device *, struct cfdata *, void *);
140 static void deattach(struct device *, struct device *, void *);
141 static void dewait(struct de_softc *, char *);
142 static void deinit(struct de_softc *);
143 static int deioctl(struct ifnet *, u_long, caddr_t);
144 static void dereset(struct device *);
145 static void destart(struct ifnet *);
146 static void derecv(struct de_softc *);
147 static void deintr(void *);
148 static int de_add_rxbuf(struct de_softc *, int);
149 static void deshutdown(void *);
150
151 struct cfattach de_ca = {
152 sizeof(struct de_softc), dematch, deattach
153 };
154
155 #define DE_WCSR(csr, val) \
156 bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
157 #define DE_WLOW(val) \
158 bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0, val)
159 #define DE_WHIGH(val) \
160 bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0 + 1, val)
161 #define DE_RCSR(csr) \
162 bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
163
164 #define LOWORD(x) ((int)(x) & 0xffff)
165 #define HIWORD(x) (((int)(x) >> 16) & 0x3)
166 /*
167 * Interface exists: make available by filling in network interface
168 * record. System will initialize the interface when it is ready
169 * to accept packets. We get the ethernet address here.
170 */
171 void
172 deattach(struct device *parent, struct device *self, void *aux)
173 {
174 struct uba_attach_args *ua = aux;
175 struct de_softc *sc = (struct de_softc *)self;
176 struct ifnet *ifp = &sc->sc_if;
177 u_int8_t myaddr[ETHER_ADDR_LEN];
178 int csr1, rseg, error, i;
179 bus_dma_segment_t seg;
180 char *c;
181
182 sc->sc_iot = ua->ua_iot;
183 sc->sc_ioh = ua->ua_ioh;
184 sc->sc_dmat = ua->ua_dmat;
185
186 /*
187 * What kind of a board is this?
188 * The error bits 4-6 in pcsr1 are a device id as long as
189 * the high byte is zero.
190 */
191 csr1 = DE_RCSR(DE_PCSR1);
192 if (csr1 & 0xff60)
193 c = "broken";
194 else if (csr1 & 0x10)
195 c = "delua";
196 else
197 c = "deuna";
198
199 /*
200 * Reset the board and temporarily map
201 * the pcbb buffer onto the Unibus.
202 */
203 DE_WCSR(DE_PCSR0, 0); /* reset INTE */
204 DELAY(100);
205 DE_WCSR(DE_PCSR0, PCSR0_RSET);
206 dewait(sc, "reset");
207
208 if ((error = bus_dmamem_alloc(sc->sc_dmat,
209 sizeof(struct de_cdata), NBPG, 0, &seg, 1, &rseg,
210 BUS_DMA_NOWAIT)) != 0) {
211 printf(": unable to allocate control data, error = %d\n",
212 error);
213 goto fail_0;
214 }
215 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
216 sizeof(struct de_cdata), (caddr_t *)&sc->sc_dedata,
217 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
218 printf(": unable to map control data, error = %d\n", error);
219 goto fail_1;
220 }
221
222 if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct de_cdata),
223 1, sizeof(struct de_cdata), 0, BUS_DMA_NOWAIT,
224 &sc->sc_cmap)) != 0) {
225 printf(": unable to create control data DMA map, error = %d\n",
226 error);
227 goto fail_2;
228 }
229
230 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cmap,
231 sc->sc_dedata, sizeof(struct de_cdata), NULL,
232 BUS_DMA_NOWAIT)) != 0) {
233 printf(": unable to load control data DMA map, error = %d\n",
234 error);
235 goto fail_3;
236 }
237
238 bzero(sc->sc_dedata, sizeof(struct de_cdata));
239 sc->sc_pdedata = (struct de_cdata *)sc->sc_cmap->dm_segs[0].ds_addr;
240
241 /*
242 * Create receive buffer DMA maps.
243 */
244 for (i = 0; i < NRCV; i++) {
245 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
246 MCLBYTES, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
247 &sc->sc_rcvmap[i]))) {
248 printf(": unable to create rx DMA map %d, error = %d\n",
249 i, error);
250 goto fail_5;
251 }
252 }
253
254 /*
255 * Pre-allocate the receive buffers.
256 */
257 for (i = 0; i < NRCV; i++) {
258 if ((error = de_add_rxbuf(sc, i)) != 0) {
259 printf(": unable to allocate or map rx buffer %d\n,"
260 " error = %d\n", i, error);
261 goto fail_6;
262 }
263 }
264
265 /*
266 * Tell the DEUNA about our PCB
267 */
268 DE_WCSR(DE_PCSR2, LOWORD(sc->sc_pdedata));
269 DE_WCSR(DE_PCSR3, HIWORD(sc->sc_pdedata));
270 DE_WLOW(CMD_GETPCBB);
271 dewait(sc, "pcbb");
272
273 sc->sc_dedata->dc_pcbb.pcbb0 = FC_RDPHYAD;
274 DE_WLOW(CMD_GETCMD);
275 dewait(sc, "read addr ");
276
277 bcopy((caddr_t)&sc->sc_dedata->dc_pcbb.pcbb2, myaddr, sizeof (myaddr));
278 printf("\n%s: %s, hardware address %s\n", sc->sc_dev.dv_xname, c,
279 ether_sprintf(myaddr));
280
281 uba_intr_establish(ua->ua_icookie, ua->ua_cvec, deintr, sc,
282 &sc->sc_intrcnt);
283 uba_reset_establish(dereset, &sc->sc_dev);
284 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
285 sc->sc_dev.dv_xname, "intr");
286
287 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
288 ifp->if_softc = sc;
289 ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI;
290 ifp->if_ioctl = deioctl;
291 ifp->if_start = destart;
292 IFQ_SET_READY(&ifp->if_snd);
293
294 if_attach(ifp);
295 ether_ifattach(ifp, myaddr);
296
297 sc->sc_sh = shutdownhook_establish(deshutdown, sc);
298 return;
299
300 /*
301 * Free any resources we've allocated during the failed attach
302 * attempt. Do this in reverse order and fall through.
303 */
304 fail_6:
305 for (i = 0; i < NRCV; i++) {
306 if (sc->sc_rxmbuf[i] != NULL) {
307 bus_dmamap_unload(sc->sc_dmat, sc->sc_rcvmap[i]);
308 m_freem(sc->sc_rxmbuf[i]);
309 }
310 }
311 fail_5:
312 for (i = 0; i < NRCV; i++) {
313 if (sc->sc_rcvmap[i] != NULL)
314 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rcvmap[i]);
315 }
316
317 fail_3:
318 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cmap);
319 fail_2:
320 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_dedata,
321 sizeof(struct de_cdata));
322 fail_1:
323 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
324 fail_0:
325 return;
326 }
327
328 /*
329 * Reset of interface after UNIBUS reset.
330 */
331 void
332 dereset(struct device *dev)
333 {
334 struct de_softc *sc = (void *)dev;
335
336 sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
337 sc->sc_pdedata = NULL; /* All mappings lost */
338 DE_WCSR(DE_PCSR0, PCSR0_RSET);
339 dewait(sc, "reset");
340 deinit(sc);
341 }
342
343 /*
344 * Initialization of interface; clear recorded pending
345 * operations, and reinitialize UNIBUS usage.
346 */
347 void
348 deinit(struct de_softc *sc)
349 {
350 struct de_cdata *dc, *pdc;
351 int s, i;
352
353 if (sc->sc_if.if_flags & IFF_RUNNING)
354 return;
355 /*
356 * Tell the DEUNA about our PCB
357 */
358 DE_WCSR(DE_PCSR2, LOWORD(sc->sc_pdedata));
359 DE_WCSR(DE_PCSR3, HIWORD(sc->sc_pdedata));
360 DE_WLOW(0); /* reset INTE */
361 DELAY(500);
362 DE_WLOW(CMD_GETPCBB);
363 dewait(sc, "pcbb");
364
365 dc = sc->sc_dedata;
366 pdc = sc->sc_pdedata;
367 /* set the transmit and receive ring header addresses */
368 dc->dc_pcbb.pcbb0 = FC_WTRING;
369 dc->dc_pcbb.pcbb2 = LOWORD(&pdc->dc_udbbuf);
370 dc->dc_pcbb.pcbb4 = HIWORD(&pdc->dc_udbbuf);
371
372 dc->dc_udbbuf.b_tdrbl = LOWORD(&pdc->dc_xrent[0]);
373 dc->dc_udbbuf.b_tdrbh = HIWORD(&pdc->dc_xrent[0]);
374 dc->dc_udbbuf.b_telen = sizeof (struct de_ring) / sizeof(u_int16_t);
375 dc->dc_udbbuf.b_trlen = NXMT;
376 dc->dc_udbbuf.b_rdrbl = LOWORD(&pdc->dc_rrent[0]);
377 dc->dc_udbbuf.b_rdrbh = HIWORD(&pdc->dc_rrent[0]);
378 dc->dc_udbbuf.b_relen = sizeof (struct de_ring) / sizeof(u_int16_t);
379 dc->dc_udbbuf.b_rrlen = NRCV;
380
381 DE_WLOW(CMD_GETCMD);
382 dewait(sc, "wtring");
383
384 sc->sc_dedata->dc_pcbb.pcbb0 = FC_WTMODE;
385 sc->sc_dedata->dc_pcbb.pcbb2 = MOD_TPAD|MOD_HDX|MOD_DRDC|MOD_ENAL;
386 DE_WLOW(CMD_GETCMD);
387 dewait(sc, "wtmode");
388
389 /* set up the receive and transmit ring entries */
390 for (i = 0; i < NXMT; i++) {
391 dc->dc_xrent[i].r_flags = 0;
392 dc->dc_xrent[i].r_segbl = LOWORD(&pdc->dc_xbuf[i][0]);
393 dc->dc_xrent[i].r_segbh = HIWORD(&pdc->dc_xbuf[i][0]);
394 }
395
396 for (i = 0; i < NRCV; i++)
397 dc->dc_rrent[i].r_flags = RFLG_OWN;
398
399 /* start up the board (rah rah) */
400 s = splnet();
401 sc->sc_rindex = sc->sc_xindex = sc->sc_xfree = sc->sc_nxmit = 0;
402 sc->sc_if.if_flags |= IFF_RUNNING;
403 DE_WLOW(PCSR0_INTE); /* avoid interlock */
404 destart(&sc->sc_if); /* queue output packets */
405 DE_WLOW(CMD_START|PCSR0_INTE);
406 splx(s);
407 }
408
409 /*
410 * Setup output on interface.
411 * Get another datagram to send off of the interface queue,
412 * and map it to the interface before starting the output.
413 * Must be called from ipl >= our interrupt level.
414 */
415 void
416 destart(struct ifnet *ifp)
417 {
418 struct de_softc *sc = ifp->if_softc;
419 struct de_cdata *dc;
420 struct de_ring *rp;
421 struct mbuf *m;
422 int nxmit;
423
424 /*
425 * the following test is necessary, since
426 * the code is not reentrant and we have
427 * multiple transmission buffers.
428 */
429 if (sc->sc_if.if_flags & IFF_OACTIVE)
430 return;
431 dc = sc->sc_dedata;
432 for (nxmit = sc->sc_nxmit; nxmit < NXMT; nxmit++) {
433 IFQ_DEQUEUE(&ifp->if_snd, m);
434 if (m == 0)
435 break;
436 rp = &dc->dc_xrent[sc->sc_xfree];
437 if (rp->r_flags & XFLG_OWN)
438 panic("deuna xmit in progress");
439 m_copydata(m, 0, m->m_pkthdr.len, &dc->dc_xbuf[sc->sc_xfree][0]);
440 rp->r_slen = m->m_pkthdr.len;
441 rp->r_tdrerr = 0;
442 rp->r_flags = XFLG_STP|XFLG_ENP|XFLG_OWN;
443
444 #if NBPFILTER > 0
445 if (ifp->if_bpf)
446 bpf_mtap(ifp->if_bpf, m);
447 #endif
448
449 m_freem(m);
450 sc->sc_xfree++;
451 if (sc->sc_xfree == NXMT)
452 sc->sc_xfree = 0;
453 }
454 if (sc->sc_nxmit != nxmit) {
455 sc->sc_nxmit = nxmit;
456 if (ifp->if_flags & IFF_RUNNING)
457 DE_WLOW(PCSR0_INTE|CMD_PDMD);
458 }
459 }
460
461 /*
462 * Command done interrupt.
463 */
464 void
465 deintr(void *arg)
466 {
467 struct de_cdata *dc;
468 struct de_softc *sc = arg;
469 struct de_ring *rp;
470 short csr0;
471
472 /* save flags right away - clear out interrupt bits */
473 csr0 = DE_RCSR(DE_PCSR0);
474 DE_WHIGH(csr0 >> 8);
475
476
477 sc->sc_if.if_flags |= IFF_OACTIVE; /* prevent entering destart */
478 /*
479 * if receive, put receive buffer on mbuf
480 * and hang the request again
481 */
482 derecv(sc);
483
484 /*
485 * Poll transmit ring and check status.
486 * Be careful about loopback requests.
487 * Then free buffer space and check for
488 * more transmit requests.
489 */
490 dc = sc->sc_dedata;
491 for ( ; sc->sc_nxmit > 0; sc->sc_nxmit--) {
492 rp = &dc->dc_xrent[sc->sc_xindex];
493 if (rp->r_flags & XFLG_OWN)
494 break;
495 sc->sc_if.if_opackets++;
496 /* check for unusual conditions */
497 if (rp->r_flags & (XFLG_ERRS|XFLG_MTCH|XFLG_ONE|XFLG_MORE)) {
498 if (rp->r_flags & XFLG_ERRS) {
499 /* output error */
500 sc->sc_if.if_oerrors++;
501 } else if (rp->r_flags & XFLG_ONE) {
502 /* one collision */
503 sc->sc_if.if_collisions++;
504 } else if (rp->r_flags & XFLG_MORE) {
505 /* more than one collision */
506 sc->sc_if.if_collisions += 2; /* guess */
507 }
508 }
509 /* check if next transmit buffer also finished */
510 sc->sc_xindex++;
511 if (sc->sc_xindex == NXMT)
512 sc->sc_xindex = 0;
513 }
514 sc->sc_if.if_flags &= ~IFF_OACTIVE;
515 destart(&sc->sc_if);
516
517 if (csr0 & PCSR0_RCBI) {
518 DE_WLOW(PCSR0_INTE|CMD_PDMD);
519 }
520 }
521
522 /*
523 * Ethernet interface receiver interface.
524 * If input error just drop packet.
525 * Otherwise purge input buffered data path and examine
526 * packet to determine type. If can't determine length
527 * from type, then have to drop packet. Othewise decapsulate
528 * packet based on type and pass to type specific higher-level
529 * input routine.
530 */
531 void
532 derecv(struct de_softc *sc)
533 {
534 struct ifnet *ifp = &sc->sc_if;
535 struct de_ring *rp;
536 struct de_cdata *dc;
537 struct mbuf *m;
538 int len;
539
540 dc = sc->sc_dedata;
541 rp = &dc->dc_rrent[sc->sc_rindex];
542 while ((rp->r_flags & RFLG_OWN) == 0) {
543 sc->sc_if.if_ipackets++;
544 len = (rp->r_lenerr&RERR_MLEN) - ETHER_CRC_LEN;
545 /* check for errors */
546 if ((rp->r_flags & (RFLG_ERRS|RFLG_FRAM|RFLG_OFLO|RFLG_CRC)) ||
547 (rp->r_lenerr & (RERR_BUFL|RERR_UBTO))) {
548 sc->sc_if.if_ierrors++;
549 goto next;
550 }
551 m = sc->sc_rxmbuf[sc->sc_rindex];
552 #if NBPFILTER > 0
553 if (ifp->if_bpf)
554 bpf_mtap(ifp->if_bpf, m);
555 #endif
556
557 if (de_add_rxbuf(sc, sc->sc_rindex) == 0) {
558 m->m_pkthdr.rcvif = ifp;
559 m->m_pkthdr.len = m->m_len = len;
560 (*ifp->if_input)(ifp, m);
561 } else
562 sc->sc_if.if_ierrors++;
563
564 /* hang the receive buffer again */
565 next: rp->r_lenerr = 0;
566 rp->r_flags = RFLG_OWN;
567
568 /* check next receive buffer */
569 sc->sc_rindex++;
570 if (sc->sc_rindex == NRCV)
571 sc->sc_rindex = 0;
572 rp = &dc->dc_rrent[sc->sc_rindex];
573 }
574 }
575
576 /*
577 * Add a receive buffer to the indicated descriptor.
578 */
579 int
580 de_add_rxbuf(sc, i)
581 struct de_softc *sc;
582 int i;
583 {
584 struct mbuf *m;
585 struct de_ring *rp;
586 vaddr_t addr;
587 int error;
588
589 MGETHDR(m, M_DONTWAIT, MT_DATA);
590 if (m == NULL)
591 return (ENOBUFS);
592
593 MCLGET(m, M_DONTWAIT);
594 if ((m->m_flags & M_EXT) == 0) {
595 m_freem(m);
596 return (ENOBUFS);
597 }
598
599 if (sc->sc_rxmbuf[i] != NULL)
600 bus_dmamap_unload(sc->sc_dmat, sc->sc_rcvmap[i]);
601
602 error = bus_dmamap_load(sc->sc_dmat, sc->sc_rcvmap[i],
603 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
604 if (error)
605 panic("%s: can't load rx DMA map %d, error = %d\n",
606 sc->sc_dev.dv_xname, i, error);
607 sc->sc_rxmbuf[i] = m;
608
609 bus_dmamap_sync(sc->sc_dmat, sc->sc_rcvmap[i], 0,
610 sc->sc_rcvmap[i]->dm_mapsize, BUS_DMASYNC_PREREAD);
611
612 /*
613 * We know that the mbuf cluster is page aligned. Also, be sure
614 * that the IP header will be longword aligned.
615 */
616 m->m_data += 2;
617 addr = sc->sc_rcvmap[i]->dm_segs[0].ds_addr + 2;
618 rp = &sc->sc_dedata->dc_rrent[i];
619 rp->r_lenerr = 0;
620 rp->r_segbl = LOWORD(addr);
621 rp->r_segbh = HIWORD(addr);
622 rp->r_slen = m->m_ext.ext_size - 2;
623 rp->r_flags = RFLG_OWN;
624
625 return (0);
626 }
627
628
629 /*
630 * Process an ioctl request.
631 */
632 int
633 deioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
634 {
635 struct ifaddr *ifa = (struct ifaddr *)data;
636 struct ifreq *ifr = (struct ifreq *)data;
637 struct de_softc *sc = ifp->if_softc;
638 int s = splnet(), error = 0;
639
640 switch (cmd) {
641
642 case SIOCSIFADDR:
643 ifp->if_flags |= IFF_UP;
644 switch (ifa->ifa_addr->sa_family) {
645 #ifdef INET
646 case AF_INET:
647 deinit(sc);
648 arp_ifinit(ifp, ifa);
649 break;
650 #endif
651 }
652 break;
653
654 case SIOCSIFFLAGS:
655 if ((ifp->if_flags & IFF_UP) == 0 &&
656 (ifp->if_flags & IFF_RUNNING) != 0) {
657 /*
658 * If interface is marked down and it is running,
659 * stop it.
660 */
661 DE_WLOW(0);
662 DELAY(5000);
663 DE_WLOW(PCSR0_RSET);
664 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
665 } else if ((ifp->if_flags & IFF_UP) != 0 &&
666 (ifp->if_flags & IFF_RUNNING) == 0) {
667 /*
668 * If interface it marked up and it is stopped, then
669 * start it.
670 */
671 deinit(sc);
672 } else if ((ifp->if_flags & IFF_UP) != 0) {
673 /*
674 * Send a new setup packet to match any new changes.
675 * (Like IFF_PROMISC etc)
676 */
677 sc->sc_dedata->dc_pcbb.pcbb0 = FC_WTMODE;
678 sc->sc_dedata->dc_pcbb.pcbb2 =
679 MOD_TPAD|MOD_HDX|MOD_DRDC|MOD_ENAL;
680 if (ifp->if_flags & IFF_PROMISC)
681 sc->sc_dedata->dc_pcbb.pcbb2 |= MOD_PROM;
682 DE_WLOW(CMD_GETCMD|PCSR0_INTE);
683 dewait(sc, "chgmode");
684 }
685 break;
686
687 case SIOCADDMULTI:
688 case SIOCDELMULTI:
689 /*
690 * Update our multicast list.
691 */
692 error = (cmd == SIOCADDMULTI) ?
693 ether_addmulti(ifr, &sc->sc_ec):
694 ether_delmulti(ifr, &sc->sc_ec);
695
696 if (error == ENETRESET) {
697 /*
698 * Multicast list has changed; set the hardware filter
699 * accordingly.
700 */
701 error = 0;
702 }
703 break;
704
705 default:
706 error = EINVAL;
707 }
708 splx(s);
709 return (error);
710 }
711
712 /*
713 * Await completion of the named function
714 * and check for errors.
715 */
716 void
717 dewait(struct de_softc *sc, char *fn)
718 {
719 int csr0;
720
721 while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
722 ;
723 csr0 = DE_RCSR(DE_PCSR0);
724 DE_WHIGH(csr0 >> 8);
725 if (csr0 & PCSR0_PCEI) {
726 char bits[64];
727 printf("%s: %s failed, csr0=%s ", sc->sc_dev.dv_xname, fn,
728 bitmask_snprintf(csr0, PCSR0_BITS, bits, sizeof(bits)));
729 printf("csr1=%s\n", bitmask_snprintf(DE_RCSR(DE_PCSR1),
730 PCSR1_BITS, bits, sizeof(bits)));
731 }
732 }
733
734 int
735 dematch(struct device *parent, struct cfdata *cf, void *aux)
736 {
737 struct uba_attach_args *ua = aux;
738 struct de_softc ssc;
739 struct de_softc *sc = &ssc;
740 int i;
741
742 sc->sc_iot = ua->ua_iot;
743 sc->sc_ioh = ua->ua_ioh;
744 /*
745 * Make sure self-test is finished before we screw with the board.
746 * Self-test on a DELUA can take 15 seconds (argh).
747 */
748 for (i = 0;
749 (i < 160) &&
750 (DE_RCSR(DE_PCSR0) & PCSR0_FATI) == 0 &&
751 (DE_RCSR(DE_PCSR1) & PCSR1_STMASK) == STAT_RESET;
752 ++i)
753 DELAY(50000);
754 if (((DE_RCSR(DE_PCSR0) & PCSR0_FATI) != 0) ||
755 (((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_READY) &&
756 ((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_RUN)))
757 return(0);
758
759 DE_WCSR(DE_PCSR0, 0);
760 DELAY(5000);
761 DE_WCSR(DE_PCSR0, PCSR0_RSET);
762 while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
763 ;
764 /* make board interrupt by executing a GETPCBB command */
765 DE_WCSR(DE_PCSR0, PCSR0_INTE);
766 DE_WCSR(DE_PCSR2, 0);
767 DE_WCSR(DE_PCSR3, 0);
768 DE_WCSR(DE_PCSR0, PCSR0_INTE|CMD_GETPCBB);
769 DELAY(50000);
770
771 return 1;
772 }
773
774 void
775 deshutdown(void *arg)
776 {
777 struct de_softc *sc = arg;
778
779 DE_WCSR(DE_PCSR0, 0);
780 DELAY(1000);
781 DE_WCSR(DE_PCSR0, PCSR0_RSET);
782 dewait(sc, "shutdown");
783 }
784