if_de.c revision 1.10 1 1.10 ragge /* $NetBSD: if_de.c,v 1.10 2001/05/06 15:27:48 ragge Exp $ */
2 1.5 ragge
3 1.1 ragge /*
4 1.1 ragge * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
5 1.1 ragge * Copyright (c) 2000 Ludd, University of Lule}, Sweden.
6 1.1 ragge * All rights reserved.
7 1.1 ragge *
8 1.1 ragge *
9 1.1 ragge * Redistribution and use in source and binary forms, with or without
10 1.1 ragge * modification, are permitted provided that the following conditions
11 1.1 ragge * are met:
12 1.1 ragge * 1. Redistributions of source code must retain the above copyright
13 1.1 ragge * notice, this list of conditions and the following disclaimer.
14 1.1 ragge * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 ragge * notice, this list of conditions and the following disclaimer in the
16 1.1 ragge * documentation and/or other materials provided with the distribution.
17 1.1 ragge * 3. All advertising materials mentioning features or use of this software
18 1.1 ragge * must display the following acknowledgement:
19 1.1 ragge * This product includes software developed by the University of
20 1.1 ragge * California, Berkeley and its contributors.
21 1.1 ragge * 4. Neither the name of the University nor the names of its contributors
22 1.1 ragge * may be used to endorse or promote products derived from this software
23 1.1 ragge * without specific prior written permission.
24 1.1 ragge *
25 1.1 ragge * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 ragge * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 ragge * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 ragge * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 ragge * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 ragge * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 ragge * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 ragge * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 ragge * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 ragge * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 ragge * SUCH DAMAGE.
36 1.1 ragge *
37 1.1 ragge * @(#)if_de.c 7.12 (Berkeley) 12/16/90
38 1.1 ragge */
39 1.1 ragge
40 1.1 ragge /*
41 1.1 ragge * DEC DEUNA interface
42 1.1 ragge *
43 1.1 ragge * Lou Salkind
44 1.1 ragge * New York University
45 1.1 ragge *
46 1.2 ragge * Rewritten by Ragge 30 April 2000 to match new world.
47 1.1 ragge *
48 1.1 ragge * TODO:
49 1.1 ragge * timeout routine (get statistics)
50 1.1 ragge */
51 1.1 ragge
52 1.1 ragge #include "opt_inet.h"
53 1.2 ragge #include "bpfilter.h"
54 1.1 ragge
55 1.1 ragge #include <sys/param.h>
56 1.1 ragge #include <sys/systm.h>
57 1.1 ragge #include <sys/mbuf.h>
58 1.1 ragge #include <sys/buf.h>
59 1.1 ragge #include <sys/protosw.h>
60 1.1 ragge #include <sys/socket.h>
61 1.1 ragge #include <sys/ioctl.h>
62 1.1 ragge #include <sys/errno.h>
63 1.1 ragge #include <sys/syslog.h>
64 1.1 ragge #include <sys/device.h>
65 1.1 ragge
66 1.1 ragge #include <net/if.h>
67 1.1 ragge #include <net/if_ether.h>
68 1.1 ragge #include <net/if_dl.h>
69 1.1 ragge
70 1.1 ragge #ifdef INET
71 1.1 ragge #include <netinet/in.h>
72 1.1 ragge #include <netinet/if_inarp.h>
73 1.1 ragge #endif
74 1.1 ragge
75 1.2 ragge #if NBPFILTER > 0
76 1.2 ragge #include <net/bpf.h>
77 1.2 ragge #include <net/bpfdesc.h>
78 1.1 ragge #endif
79 1.1 ragge
80 1.1 ragge #include <machine/bus.h>
81 1.1 ragge
82 1.1 ragge #include <dev/qbus/ubavar.h>
83 1.1 ragge #include <dev/qbus/if_dereg.h>
84 1.10 ragge #include <dev/qbus/if_uba.h>
85 1.1 ragge
86 1.1 ragge #include "ioconf.h"
87 1.1 ragge
88 1.1 ragge /*
89 1.1 ragge * Be careful with transmit/receive buffers, each entry steals 4 map
90 1.1 ragge * registers, and there is only 496 on one unibus...
91 1.1 ragge */
92 1.10 ragge #define NRCV 7 /* number of receive buffers (must be > 1) */
93 1.10 ragge #define NXMT 3 /* number of transmit buffers */
94 1.1 ragge
95 1.1 ragge /*
96 1.1 ragge * Structure containing the elements that must be in DMA-safe memory.
97 1.1 ragge */
98 1.1 ragge struct de_cdata {
99 1.1 ragge /* the following structures are always mapped in */
100 1.1 ragge struct de_pcbb dc_pcbb; /* port control block */
101 1.1 ragge struct de_ring dc_xrent[NXMT]; /* transmit ring entrys */
102 1.1 ragge struct de_ring dc_rrent[NRCV]; /* receive ring entrys */
103 1.1 ragge struct de_udbbuf dc_udbbuf; /* UNIBUS data buffer */
104 1.1 ragge /* end mapped area */
105 1.1 ragge };
106 1.1 ragge
107 1.1 ragge /*
108 1.1 ragge * Ethernet software status per interface.
109 1.1 ragge *
110 1.1 ragge * Each interface is referenced by a network interface structure,
111 1.1 ragge * ds_if, which the routing code uses to locate the interface.
112 1.1 ragge * This structure contains the output queue for the interface, its address, ...
113 1.1 ragge * We also have, for each interface, a UBA interface structure, which
114 1.1 ragge * contains information about the UNIBUS resources held by the interface:
115 1.1 ragge * map registers, buffered data paths, etc. Information is cached in this
116 1.1 ragge * structure for use by the if_uba.c routines in running the interface
117 1.1 ragge * efficiently.
118 1.1 ragge */
119 1.1 ragge struct de_softc {
120 1.1 ragge struct device sc_dev; /* Configuration common part */
121 1.3 matt struct evcnt sc_intrcnt; /* Interrupt counting */
122 1.1 ragge struct ethercom sc_ec; /* Ethernet common part */
123 1.1 ragge #define sc_if sc_ec.ec_if /* network-visible interface */
124 1.1 ragge bus_space_tag_t sc_iot;
125 1.1 ragge bus_addr_t sc_ioh;
126 1.1 ragge bus_dma_tag_t sc_dmat;
127 1.10 ragge int sc_flags;
128 1.10 ragge #define DSF_MAPPED 1
129 1.9 ragge struct ubinfo sc_ui;
130 1.1 ragge struct de_cdata *sc_dedata; /* Control structure */
131 1.1 ragge struct de_cdata *sc_pdedata; /* Bus-mapped control structure */
132 1.10 ragge struct ifubinfo sc_ifuba; /* UNIBUS resources */
133 1.10 ragge struct ifrw sc_ifr[NRCV]; /* UNIBUS receive buffer maps */
134 1.10 ragge struct ifxmt sc_ifw[NXMT]; /* UNIBUS receive buffer maps */
135 1.10 ragge
136 1.5 ragge int sc_xindex; /* UNA index into transmit chain */
137 1.5 ragge int sc_rindex; /* UNA index into receive chain */
138 1.5 ragge int sc_xfree; /* index for next transmit buffer */
139 1.5 ragge int sc_nxmit; /* # of transmits in progress */
140 1.2 ragge void *sc_sh; /* shutdownhook cookie */
141 1.1 ragge };
142 1.1 ragge
143 1.1 ragge static int dematch(struct device *, struct cfdata *, void *);
144 1.1 ragge static void deattach(struct device *, struct device *, void *);
145 1.2 ragge static void dewait(struct de_softc *, char *);
146 1.10 ragge static int deinit(struct ifnet *);
147 1.1 ragge static int deioctl(struct ifnet *, u_long, caddr_t);
148 1.1 ragge static void dereset(struct device *);
149 1.10 ragge static void destop(struct ifnet *, int);
150 1.1 ragge static void destart(struct ifnet *);
151 1.1 ragge static void derecv(struct de_softc *);
152 1.1 ragge static void deintr(void *);
153 1.2 ragge static void deshutdown(void *);
154 1.1 ragge
155 1.1 ragge struct cfattach de_ca = {
156 1.1 ragge sizeof(struct de_softc), dematch, deattach
157 1.1 ragge };
158 1.1 ragge
159 1.1 ragge #define DE_WCSR(csr, val) \
160 1.1 ragge bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
161 1.1 ragge #define DE_WLOW(val) \
162 1.1 ragge bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0, val)
163 1.1 ragge #define DE_WHIGH(val) \
164 1.1 ragge bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0 + 1, val)
165 1.1 ragge #define DE_RCSR(csr) \
166 1.1 ragge bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
167 1.1 ragge
168 1.1 ragge #define LOWORD(x) ((int)(x) & 0xffff)
169 1.1 ragge #define HIWORD(x) (((int)(x) >> 16) & 0x3)
170 1.1 ragge /*
171 1.1 ragge * Interface exists: make available by filling in network interface
172 1.1 ragge * record. System will initialize the interface when it is ready
173 1.1 ragge * to accept packets. We get the ethernet address here.
174 1.1 ragge */
175 1.1 ragge void
176 1.1 ragge deattach(struct device *parent, struct device *self, void *aux)
177 1.1 ragge {
178 1.1 ragge struct uba_attach_args *ua = aux;
179 1.1 ragge struct de_softc *sc = (struct de_softc *)self;
180 1.1 ragge struct ifnet *ifp = &sc->sc_if;
181 1.1 ragge u_int8_t myaddr[ETHER_ADDR_LEN];
182 1.10 ragge int csr1, error;
183 1.1 ragge char *c;
184 1.1 ragge
185 1.1 ragge sc->sc_iot = ua->ua_iot;
186 1.1 ragge sc->sc_ioh = ua->ua_ioh;
187 1.1 ragge sc->sc_dmat = ua->ua_dmat;
188 1.1 ragge
189 1.1 ragge /*
190 1.1 ragge * What kind of a board is this?
191 1.1 ragge * The error bits 4-6 in pcsr1 are a device id as long as
192 1.1 ragge * the high byte is zero.
193 1.1 ragge */
194 1.1 ragge csr1 = DE_RCSR(DE_PCSR1);
195 1.1 ragge if (csr1 & 0xff60)
196 1.1 ragge c = "broken";
197 1.1 ragge else if (csr1 & 0x10)
198 1.1 ragge c = "delua";
199 1.1 ragge else
200 1.1 ragge c = "deuna";
201 1.1 ragge
202 1.1 ragge /*
203 1.1 ragge * Reset the board and temporarily map
204 1.1 ragge * the pcbb buffer onto the Unibus.
205 1.1 ragge */
206 1.1 ragge DE_WCSR(DE_PCSR0, 0); /* reset INTE */
207 1.1 ragge DELAY(100);
208 1.1 ragge DE_WCSR(DE_PCSR0, PCSR0_RSET);
209 1.2 ragge dewait(sc, "reset");
210 1.1 ragge
211 1.9 ragge sc->sc_ui.ui_size = sizeof(struct de_cdata);
212 1.10 ragge if ((error = ubmemalloc((struct uba_softc *)parent, &sc->sc_ui, 0)))
213 1.10 ragge return printf(": failed ubmemalloc(), error = %d\n", error);
214 1.9 ragge sc->sc_dedata = (struct de_cdata *)sc->sc_ui.ui_vaddr;
215 1.2 ragge
216 1.1 ragge /*
217 1.1 ragge * Tell the DEUNA about our PCB
218 1.1 ragge */
219 1.10 ragge DE_WCSR(DE_PCSR2, LOWORD(sc->sc_ui.ui_baddr));
220 1.10 ragge DE_WCSR(DE_PCSR3, HIWORD(sc->sc_ui.ui_baddr));
221 1.1 ragge DE_WLOW(CMD_GETPCBB);
222 1.2 ragge dewait(sc, "pcbb");
223 1.1 ragge
224 1.1 ragge sc->sc_dedata->dc_pcbb.pcbb0 = FC_RDPHYAD;
225 1.1 ragge DE_WLOW(CMD_GETCMD);
226 1.2 ragge dewait(sc, "read addr ");
227 1.1 ragge
228 1.1 ragge bcopy((caddr_t)&sc->sc_dedata->dc_pcbb.pcbb2, myaddr, sizeof (myaddr));
229 1.2 ragge printf("\n%s: %s, hardware address %s\n", sc->sc_dev.dv_xname, c,
230 1.1 ragge ether_sprintf(myaddr));
231 1.1 ragge
232 1.5 ragge uba_intr_establish(ua->ua_icookie, ua->ua_cvec, deintr, sc,
233 1.5 ragge &sc->sc_intrcnt);
234 1.1 ragge uba_reset_establish(dereset, &sc->sc_dev);
235 1.4 matt evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
236 1.5 ragge sc->sc_dev.dv_xname, "intr");
237 1.1 ragge
238 1.1 ragge strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
239 1.1 ragge ifp->if_softc = sc;
240 1.5 ragge ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI;
241 1.1 ragge ifp->if_ioctl = deioctl;
242 1.1 ragge ifp->if_start = destart;
243 1.10 ragge ifp->if_init = deinit;
244 1.10 ragge ifp->if_stop = destop;
245 1.8 thorpej IFQ_SET_READY(&ifp->if_snd);
246 1.8 thorpej
247 1.1 ragge if_attach(ifp);
248 1.1 ragge ether_ifattach(ifp, myaddr);
249 1.10 ragge ubmemfree((struct uba_softc *)parent, &sc->sc_ui);
250 1.7 thorpej
251 1.2 ragge sc->sc_sh = shutdownhook_establish(deshutdown, sc);
252 1.10 ragge }
253 1.1 ragge
254 1.10 ragge void
255 1.10 ragge destop(struct ifnet *ifp, int a)
256 1.10 ragge {
257 1.10 ragge struct de_softc *sc = ifp->if_softc;
258 1.10 ragge
259 1.10 ragge DE_WLOW(0);
260 1.10 ragge DELAY(5000);
261 1.10 ragge DE_WLOW(PCSR0_RSET);
262 1.1 ragge }
263 1.1 ragge
264 1.10 ragge
265 1.1 ragge /*
266 1.1 ragge * Reset of interface after UNIBUS reset.
267 1.1 ragge */
268 1.1 ragge void
269 1.1 ragge dereset(struct device *dev)
270 1.1 ragge {
271 1.1 ragge struct de_softc *sc = (void *)dev;
272 1.1 ragge
273 1.1 ragge sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
274 1.10 ragge sc->sc_flags &= ~DSF_MAPPED;
275 1.5 ragge sc->sc_pdedata = NULL; /* All mappings lost */
276 1.1 ragge DE_WCSR(DE_PCSR0, PCSR0_RSET);
277 1.2 ragge dewait(sc, "reset");
278 1.10 ragge deinit(&sc->sc_if);
279 1.1 ragge }
280 1.1 ragge
281 1.1 ragge /*
282 1.1 ragge * Initialization of interface; clear recorded pending
283 1.1 ragge * operations, and reinitialize UNIBUS usage.
284 1.1 ragge */
285 1.10 ragge int
286 1.10 ragge deinit(struct ifnet *ifp)
287 1.1 ragge {
288 1.10 ragge struct de_softc *sc = ifp->if_softc;
289 1.2 ragge struct de_cdata *dc, *pdc;
290 1.10 ragge struct ifrw *ifrw;
291 1.10 ragge struct ifxmt *ifxp;
292 1.10 ragge struct de_ring *rp;
293 1.10 ragge int s, error;
294 1.10 ragge
295 1.10 ragge if (ifp->if_flags & IFF_RUNNING)
296 1.10 ragge return 0;
297 1.10 ragge if ((sc->sc_flags & DSF_MAPPED) == 0) {
298 1.10 ragge if (if_ubaminit(&sc->sc_ifuba, (void *)sc->sc_dev.dv_parent,
299 1.10 ragge MCLBYTES, sc->sc_ifr, NRCV, sc->sc_ifw, NXMT)) {
300 1.10 ragge printf("%s: can't initialize\n", sc->sc_dev.dv_xname);
301 1.10 ragge ifp->if_flags &= ~IFF_UP;
302 1.10 ragge return 0;
303 1.10 ragge }
304 1.10 ragge sc->sc_ui.ui_size = sizeof(struct de_cdata);
305 1.10 ragge if ((error = ubmemalloc((void *)sc->sc_dev.dv_parent,
306 1.10 ragge &sc->sc_ui, 0))) {
307 1.10 ragge printf(": unable to ubmemalloc(), error = %d\n", error);
308 1.10 ragge return 0;
309 1.10 ragge }
310 1.10 ragge sc->sc_pdedata = (struct de_cdata *)sc->sc_ui.ui_baddr;
311 1.10 ragge sc->sc_dedata = (struct de_cdata *)sc->sc_ui.ui_vaddr;
312 1.10 ragge sc->sc_flags |= DSF_MAPPED;
313 1.10 ragge }
314 1.1 ragge
315 1.1 ragge /*
316 1.1 ragge * Tell the DEUNA about our PCB
317 1.1 ragge */
318 1.1 ragge DE_WCSR(DE_PCSR2, LOWORD(sc->sc_pdedata));
319 1.1 ragge DE_WCSR(DE_PCSR3, HIWORD(sc->sc_pdedata));
320 1.1 ragge DE_WLOW(0); /* reset INTE */
321 1.1 ragge DELAY(500);
322 1.1 ragge DE_WLOW(CMD_GETPCBB);
323 1.2 ragge dewait(sc, "pcbb");
324 1.1 ragge
325 1.1 ragge dc = sc->sc_dedata;
326 1.2 ragge pdc = sc->sc_pdedata;
327 1.1 ragge /* set the transmit and receive ring header addresses */
328 1.1 ragge dc->dc_pcbb.pcbb0 = FC_WTRING;
329 1.2 ragge dc->dc_pcbb.pcbb2 = LOWORD(&pdc->dc_udbbuf);
330 1.2 ragge dc->dc_pcbb.pcbb4 = HIWORD(&pdc->dc_udbbuf);
331 1.1 ragge
332 1.2 ragge dc->dc_udbbuf.b_tdrbl = LOWORD(&pdc->dc_xrent[0]);
333 1.2 ragge dc->dc_udbbuf.b_tdrbh = HIWORD(&pdc->dc_xrent[0]);
334 1.1 ragge dc->dc_udbbuf.b_telen = sizeof (struct de_ring) / sizeof(u_int16_t);
335 1.1 ragge dc->dc_udbbuf.b_trlen = NXMT;
336 1.2 ragge dc->dc_udbbuf.b_rdrbl = LOWORD(&pdc->dc_rrent[0]);
337 1.2 ragge dc->dc_udbbuf.b_rdrbh = HIWORD(&pdc->dc_rrent[0]);
338 1.1 ragge dc->dc_udbbuf.b_relen = sizeof (struct de_ring) / sizeof(u_int16_t);
339 1.1 ragge dc->dc_udbbuf.b_rrlen = NRCV;
340 1.1 ragge
341 1.1 ragge DE_WLOW(CMD_GETCMD);
342 1.2 ragge dewait(sc, "wtring");
343 1.1 ragge
344 1.5 ragge sc->sc_dedata->dc_pcbb.pcbb0 = FC_WTMODE;
345 1.5 ragge sc->sc_dedata->dc_pcbb.pcbb2 = MOD_TPAD|MOD_HDX|MOD_DRDC|MOD_ENAL;
346 1.5 ragge DE_WLOW(CMD_GETCMD);
347 1.5 ragge dewait(sc, "wtmode");
348 1.1 ragge
349 1.5 ragge /* set up the receive and transmit ring entries */
350 1.10 ragge ifxp = &sc->sc_ifw[0];
351 1.10 ragge for (rp = &dc->dc_xrent[0]; rp < &dc->dc_xrent[NXMT]; rp++) {
352 1.10 ragge rp->r_segbl = LOWORD(ifxp->ifw_info);
353 1.10 ragge rp->r_segbh = HIWORD(ifxp->ifw_info);
354 1.10 ragge rp->r_flags = 0;
355 1.10 ragge ifxp++;
356 1.10 ragge }
357 1.10 ragge ifrw = &sc->sc_ifr[0];
358 1.10 ragge for (rp = &dc->dc_rrent[0]; rp < &dc->dc_rrent[NRCV]; rp++) {
359 1.10 ragge rp->r_slen = MCLBYTES - 2;
360 1.10 ragge rp->r_segbl = LOWORD(ifrw->ifrw_info);
361 1.10 ragge rp->r_segbh = HIWORD(ifrw->ifrw_info);
362 1.10 ragge rp->r_flags = RFLG_OWN;
363 1.10 ragge ifrw++;
364 1.10 ragge }
365 1.1 ragge
366 1.1 ragge /* start up the board (rah rah) */
367 1.1 ragge s = splnet();
368 1.5 ragge sc->sc_rindex = sc->sc_xindex = sc->sc_xfree = sc->sc_nxmit = 0;
369 1.1 ragge sc->sc_if.if_flags |= IFF_RUNNING;
370 1.5 ragge DE_WLOW(PCSR0_INTE); /* avoid interlock */
371 1.5 ragge destart(&sc->sc_if); /* queue output packets */
372 1.1 ragge DE_WLOW(CMD_START|PCSR0_INTE);
373 1.1 ragge splx(s);
374 1.10 ragge return 0;
375 1.1 ragge }
376 1.1 ragge
377 1.1 ragge /*
378 1.1 ragge * Setup output on interface.
379 1.1 ragge * Get another datagram to send off of the interface queue,
380 1.1 ragge * and map it to the interface before starting the output.
381 1.1 ragge * Must be called from ipl >= our interrupt level.
382 1.1 ragge */
383 1.1 ragge void
384 1.1 ragge destart(struct ifnet *ifp)
385 1.1 ragge {
386 1.1 ragge struct de_softc *sc = ifp->if_softc;
387 1.2 ragge struct de_cdata *dc;
388 1.10 ragge struct de_ring *rp;
389 1.2 ragge struct mbuf *m;
390 1.10 ragge int nxmit, len;
391 1.1 ragge
392 1.1 ragge /*
393 1.1 ragge * the following test is necessary, since
394 1.1 ragge * the code is not reentrant and we have
395 1.1 ragge * multiple transmission buffers.
396 1.1 ragge */
397 1.5 ragge if (sc->sc_if.if_flags & IFF_OACTIVE)
398 1.1 ragge return;
399 1.2 ragge dc = sc->sc_dedata;
400 1.5 ragge for (nxmit = sc->sc_nxmit; nxmit < NXMT; nxmit++) {
401 1.8 thorpej IFQ_DEQUEUE(&ifp->if_snd, m);
402 1.1 ragge if (m == 0)
403 1.5 ragge break;
404 1.10 ragge
405 1.10 ragge rp = &dc->dc_xrent[sc->sc_xfree];
406 1.5 ragge if (rp->r_flags & XFLG_OWN)
407 1.5 ragge panic("deuna xmit in progress");
408 1.1 ragge #if NBPFILTER > 0
409 1.1 ragge if (ifp->if_bpf)
410 1.1 ragge bpf_mtap(ifp->if_bpf, m);
411 1.1 ragge #endif
412 1.5 ragge
413 1.10 ragge len = if_ubaput(&sc->sc_ifuba, &sc->sc_ifw[sc->sc_xfree], m);
414 1.10 ragge rp->r_slen = len;
415 1.10 ragge rp->r_tdrerr = 0;
416 1.10 ragge rp->r_flags = XFLG_STP|XFLG_ENP|XFLG_OWN;
417 1.10 ragge
418 1.5 ragge sc->sc_xfree++;
419 1.5 ragge if (sc->sc_xfree == NXMT)
420 1.5 ragge sc->sc_xfree = 0;
421 1.5 ragge }
422 1.10 ragge if (sc->sc_nxmit != nxmit) {
423 1.5 ragge sc->sc_nxmit = nxmit;
424 1.5 ragge if (ifp->if_flags & IFF_RUNNING)
425 1.5 ragge DE_WLOW(PCSR0_INTE|CMD_PDMD);
426 1.1 ragge }
427 1.1 ragge }
428 1.1 ragge
429 1.1 ragge /*
430 1.1 ragge * Command done interrupt.
431 1.1 ragge */
432 1.1 ragge void
433 1.1 ragge deintr(void *arg)
434 1.1 ragge {
435 1.10 ragge struct ifxmt *ifxp;
436 1.5 ragge struct de_cdata *dc;
437 1.1 ragge struct de_softc *sc = arg;
438 1.5 ragge struct de_ring *rp;
439 1.5 ragge short csr0;
440 1.1 ragge
441 1.1 ragge /* save flags right away - clear out interrupt bits */
442 1.1 ragge csr0 = DE_RCSR(DE_PCSR0);
443 1.1 ragge DE_WHIGH(csr0 >> 8);
444 1.1 ragge
445 1.1 ragge
446 1.5 ragge sc->sc_if.if_flags |= IFF_OACTIVE; /* prevent entering destart */
447 1.5 ragge /*
448 1.5 ragge * if receive, put receive buffer on mbuf
449 1.5 ragge * and hang the request again
450 1.5 ragge */
451 1.5 ragge derecv(sc);
452 1.1 ragge
453 1.1 ragge /*
454 1.1 ragge * Poll transmit ring and check status.
455 1.5 ragge * Be careful about loopback requests.
456 1.1 ragge * Then free buffer space and check for
457 1.1 ragge * more transmit requests.
458 1.1 ragge */
459 1.5 ragge dc = sc->sc_dedata;
460 1.5 ragge for ( ; sc->sc_nxmit > 0; sc->sc_nxmit--) {
461 1.5 ragge rp = &dc->dc_xrent[sc->sc_xindex];
462 1.5 ragge if (rp->r_flags & XFLG_OWN)
463 1.2 ragge break;
464 1.10 ragge
465 1.5 ragge sc->sc_if.if_opackets++;
466 1.10 ragge ifxp = &sc->sc_ifw[sc->sc_xindex];
467 1.5 ragge /* check for unusual conditions */
468 1.1 ragge if (rp->r_flags & (XFLG_ERRS|XFLG_MTCH|XFLG_ONE|XFLG_MORE)) {
469 1.1 ragge if (rp->r_flags & XFLG_ERRS) {
470 1.5 ragge /* output error */
471 1.5 ragge sc->sc_if.if_oerrors++;
472 1.1 ragge } else if (rp->r_flags & XFLG_ONE) {
473 1.5 ragge /* one collision */
474 1.5 ragge sc->sc_if.if_collisions++;
475 1.1 ragge } else if (rp->r_flags & XFLG_MORE) {
476 1.5 ragge /* more than one collision */
477 1.5 ragge sc->sc_if.if_collisions += 2; /* guess */
478 1.1 ragge }
479 1.1 ragge }
480 1.10 ragge if_ubaend(&sc->sc_ifuba, ifxp);
481 1.5 ragge /* check if next transmit buffer also finished */
482 1.5 ragge sc->sc_xindex++;
483 1.5 ragge if (sc->sc_xindex == NXMT)
484 1.5 ragge sc->sc_xindex = 0;
485 1.5 ragge }
486 1.5 ragge sc->sc_if.if_flags &= ~IFF_OACTIVE;
487 1.5 ragge destart(&sc->sc_if);
488 1.5 ragge
489 1.5 ragge if (csr0 & PCSR0_RCBI) {
490 1.5 ragge DE_WLOW(PCSR0_INTE|CMD_PDMD);
491 1.5 ragge }
492 1.1 ragge }
493 1.1 ragge
494 1.1 ragge /*
495 1.1 ragge * Ethernet interface receiver interface.
496 1.1 ragge * If input error just drop packet.
497 1.1 ragge * Otherwise purge input buffered data path and examine
498 1.1 ragge * packet to determine type. If can't determine length
499 1.1 ragge * from type, then have to drop packet. Othewise decapsulate
500 1.1 ragge * packet based on type and pass to type specific higher-level
501 1.1 ragge * input routine.
502 1.1 ragge */
503 1.1 ragge void
504 1.1 ragge derecv(struct de_softc *sc)
505 1.1 ragge {
506 1.1 ragge struct ifnet *ifp = &sc->sc_if;
507 1.1 ragge struct de_ring *rp;
508 1.5 ragge struct de_cdata *dc;
509 1.1 ragge struct mbuf *m;
510 1.1 ragge int len;
511 1.1 ragge
512 1.5 ragge dc = sc->sc_dedata;
513 1.5 ragge rp = &dc->dc_rrent[sc->sc_rindex];
514 1.1 ragge while ((rp->r_flags & RFLG_OWN) == 0) {
515 1.5 ragge sc->sc_if.if_ipackets++;
516 1.5 ragge len = (rp->r_lenerr&RERR_MLEN) - ETHER_CRC_LEN;
517 1.1 ragge /* check for errors */
518 1.1 ragge if ((rp->r_flags & (RFLG_ERRS|RFLG_FRAM|RFLG_OFLO|RFLG_CRC)) ||
519 1.2 ragge (rp->r_lenerr & (RERR_BUFL|RERR_UBTO))) {
520 1.5 ragge sc->sc_if.if_ierrors++;
521 1.1 ragge goto next;
522 1.1 ragge }
523 1.10 ragge m = if_ubaget(&sc->sc_ifuba, &sc->sc_ifr[sc->sc_rindex],
524 1.10 ragge ifp, len);
525 1.10 ragge if (m == 0) {
526 1.10 ragge sc->sc_if.if_ierrors++;
527 1.10 ragge goto next;
528 1.10 ragge }
529 1.1 ragge #if NBPFILTER > 0
530 1.6 thorpej if (ifp->if_bpf)
531 1.1 ragge bpf_mtap(ifp->if_bpf, m);
532 1.1 ragge #endif
533 1.5 ragge
534 1.10 ragge (*ifp->if_input)(ifp, m);
535 1.1 ragge
536 1.1 ragge /* hang the receive buffer again */
537 1.1 ragge next: rp->r_lenerr = 0;
538 1.1 ragge rp->r_flags = RFLG_OWN;
539 1.1 ragge
540 1.1 ragge /* check next receive buffer */
541 1.5 ragge sc->sc_rindex++;
542 1.5 ragge if (sc->sc_rindex == NRCV)
543 1.5 ragge sc->sc_rindex = 0;
544 1.5 ragge rp = &dc->dc_rrent[sc->sc_rindex];
545 1.1 ragge }
546 1.1 ragge }
547 1.1 ragge
548 1.1 ragge /*
549 1.1 ragge * Process an ioctl request.
550 1.1 ragge */
551 1.1 ragge int
552 1.1 ragge deioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
553 1.1 ragge {
554 1.10 ragge int s, error = 0;
555 1.1 ragge
556 1.10 ragge s = splnet();
557 1.1 ragge
558 1.10 ragge error = ether_ioctl(ifp, cmd, data);
559 1.10 ragge if (error == ENETRESET)
560 1.10 ragge error = 0;
561 1.2 ragge
562 1.1 ragge splx(s);
563 1.1 ragge return (error);
564 1.1 ragge }
565 1.1 ragge
566 1.1 ragge /*
567 1.1 ragge * Await completion of the named function
568 1.1 ragge * and check for errors.
569 1.1 ragge */
570 1.2 ragge void
571 1.1 ragge dewait(struct de_softc *sc, char *fn)
572 1.1 ragge {
573 1.5 ragge int csr0;
574 1.1 ragge
575 1.1 ragge while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
576 1.1 ragge ;
577 1.1 ragge csr0 = DE_RCSR(DE_PCSR0);
578 1.1 ragge DE_WHIGH(csr0 >> 8);
579 1.1 ragge if (csr0 & PCSR0_PCEI) {
580 1.1 ragge char bits[64];
581 1.1 ragge printf("%s: %s failed, csr0=%s ", sc->sc_dev.dv_xname, fn,
582 1.1 ragge bitmask_snprintf(csr0, PCSR0_BITS, bits, sizeof(bits)));
583 1.1 ragge printf("csr1=%s\n", bitmask_snprintf(DE_RCSR(DE_PCSR1),
584 1.1 ragge PCSR1_BITS, bits, sizeof(bits)));
585 1.1 ragge }
586 1.1 ragge }
587 1.1 ragge
588 1.1 ragge int
589 1.1 ragge dematch(struct device *parent, struct cfdata *cf, void *aux)
590 1.1 ragge {
591 1.1 ragge struct uba_attach_args *ua = aux;
592 1.1 ragge struct de_softc ssc;
593 1.1 ragge struct de_softc *sc = &ssc;
594 1.1 ragge int i;
595 1.1 ragge
596 1.1 ragge sc->sc_iot = ua->ua_iot;
597 1.1 ragge sc->sc_ioh = ua->ua_ioh;
598 1.1 ragge /*
599 1.1 ragge * Make sure self-test is finished before we screw with the board.
600 1.1 ragge * Self-test on a DELUA can take 15 seconds (argh).
601 1.1 ragge */
602 1.1 ragge for (i = 0;
603 1.1 ragge (i < 160) &&
604 1.1 ragge (DE_RCSR(DE_PCSR0) & PCSR0_FATI) == 0 &&
605 1.1 ragge (DE_RCSR(DE_PCSR1) & PCSR1_STMASK) == STAT_RESET;
606 1.1 ragge ++i)
607 1.1 ragge DELAY(50000);
608 1.1 ragge if (((DE_RCSR(DE_PCSR0) & PCSR0_FATI) != 0) ||
609 1.1 ragge (((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_READY) &&
610 1.1 ragge ((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_RUN)))
611 1.1 ragge return(0);
612 1.1 ragge
613 1.1 ragge DE_WCSR(DE_PCSR0, 0);
614 1.1 ragge DELAY(5000);
615 1.1 ragge DE_WCSR(DE_PCSR0, PCSR0_RSET);
616 1.1 ragge while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
617 1.1 ragge ;
618 1.1 ragge /* make board interrupt by executing a GETPCBB command */
619 1.1 ragge DE_WCSR(DE_PCSR0, PCSR0_INTE);
620 1.1 ragge DE_WCSR(DE_PCSR2, 0);
621 1.1 ragge DE_WCSR(DE_PCSR3, 0);
622 1.1 ragge DE_WCSR(DE_PCSR0, PCSR0_INTE|CMD_GETPCBB);
623 1.1 ragge DELAY(50000);
624 1.1 ragge
625 1.1 ragge return 1;
626 1.1 ragge }
627 1.2 ragge
628 1.2 ragge void
629 1.2 ragge deshutdown(void *arg)
630 1.2 ragge {
631 1.2 ragge struct de_softc *sc = arg;
632 1.2 ragge
633 1.5 ragge DE_WCSR(DE_PCSR0, 0);
634 1.5 ragge DELAY(1000);
635 1.2 ragge DE_WCSR(DE_PCSR0, PCSR0_RSET);
636 1.2 ragge dewait(sc, "shutdown");
637 1.2 ragge }
638