if_de.c revision 1.1 1 1.1 ragge /* $NetBSD: if_de.c,v 1.1 2000/04/30 11:43:26 ragge Exp $ */
2 1.1 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.1 ragge * Rewritten by Ragge 000430 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.1 ragge #include "opt_iso.h"
54 1.1 ragge #include "opt_ns.h"
55 1.1 ragge
56 1.1 ragge #include <sys/param.h>
57 1.1 ragge #include <sys/systm.h>
58 1.1 ragge #include <sys/mbuf.h>
59 1.1 ragge #include <sys/buf.h>
60 1.1 ragge #include <sys/protosw.h>
61 1.1 ragge #include <sys/socket.h>
62 1.1 ragge #include <sys/ioctl.h>
63 1.1 ragge #include <sys/errno.h>
64 1.1 ragge #include <sys/syslog.h>
65 1.1 ragge #include <sys/device.h>
66 1.1 ragge
67 1.1 ragge #include <net/if.h>
68 1.1 ragge #include <net/if_ether.h>
69 1.1 ragge #include <net/if_dl.h>
70 1.1 ragge
71 1.1 ragge #ifdef INET
72 1.1 ragge #include <netinet/in.h>
73 1.1 ragge #include <netinet/if_inarp.h>
74 1.1 ragge #endif
75 1.1 ragge
76 1.1 ragge #ifdef NS
77 1.1 ragge #include <netns/ns.h>
78 1.1 ragge #include <netns/ns_if.h>
79 1.1 ragge #endif
80 1.1 ragge
81 1.1 ragge #ifdef ISO
82 1.1 ragge #include <netiso/iso.h>
83 1.1 ragge #include <netiso/iso_var.h>
84 1.1 ragge extern char all_es_snpa[], all_is_snpa[];
85 1.1 ragge #endif
86 1.1 ragge
87 1.1 ragge #include <machine/bus.h>
88 1.1 ragge
89 1.1 ragge #include <dev/qbus/ubavar.h>
90 1.1 ragge #include <dev/qbus/if_dereg.h>
91 1.1 ragge
92 1.1 ragge #include "ioconf.h"
93 1.1 ragge
94 1.1 ragge /*
95 1.1 ragge * Be careful with transmit/receive buffers, each entry steals 4 map
96 1.1 ragge * registers, and there is only 496 on one unibus...
97 1.1 ragge */
98 1.1 ragge #define NRCV 10 /* number of receive buffers (must be > 1) */
99 1.1 ragge #define NXMT 20 /* number of transmit buffers */
100 1.1 ragge #define NFRAGS 8 /* Number of frags per transmit buffer */
101 1.1 ragge
102 1.1 ragge /*
103 1.1 ragge * Structure containing the elements that must be in DMA-safe memory.
104 1.1 ragge */
105 1.1 ragge struct de_cdata {
106 1.1 ragge /* the following structures are always mapped in */
107 1.1 ragge struct de_pcbb dc_pcbb; /* port control block */
108 1.1 ragge struct de_ring dc_xrent[NXMT]; /* transmit ring entrys */
109 1.1 ragge struct de_ring dc_rrent[NRCV]; /* receive ring entrys */
110 1.1 ragge struct de_udbbuf dc_udbbuf; /* UNIBUS data buffer */
111 1.1 ragge /* end mapped area */
112 1.1 ragge };
113 1.1 ragge
114 1.1 ragge /*
115 1.1 ragge * Ethernet software status per interface.
116 1.1 ragge *
117 1.1 ragge * Each interface is referenced by a network interface structure,
118 1.1 ragge * ds_if, which the routing code uses to locate the interface.
119 1.1 ragge * This structure contains the output queue for the interface, its address, ...
120 1.1 ragge * We also have, for each interface, a UBA interface structure, which
121 1.1 ragge * contains information about the UNIBUS resources held by the interface:
122 1.1 ragge * map registers, buffered data paths, etc. Information is cached in this
123 1.1 ragge * structure for use by the if_uba.c routines in running the interface
124 1.1 ragge * efficiently.
125 1.1 ragge */
126 1.1 ragge struct de_softc {
127 1.1 ragge struct device sc_dev; /* Configuration common part */
128 1.1 ragge struct ethercom sc_ec; /* Ethernet common part */
129 1.1 ragge #define sc_if sc_ec.ec_if /* network-visible interface */
130 1.1 ragge int sc_flags;
131 1.1 ragge #define DSF_RUNNING 2 /* board is enabled */
132 1.1 ragge #define DSF_SETADDR 4 /* physical address is changed */
133 1.1 ragge bus_space_tag_t sc_iot;
134 1.1 ragge bus_addr_t sc_ioh;
135 1.1 ragge bus_dma_tag_t sc_dmat;
136 1.1 ragge struct de_cdata *sc_dedata; /* Control structure */
137 1.1 ragge struct de_cdata *sc_pdedata; /* Bus-mapped control structure */
138 1.1 ragge bus_dmamap_t sc_xmtmap[NXMT]; /* unibus receive maps */
139 1.1 ragge bus_dmamap_t sc_rcvmap[NRCV]; /* unibus xmt maps */
140 1.1 ragge struct mbuf *sc_txmbuf[NXMT];
141 1.1 ragge struct mbuf *sc_rxmbuf[NRCV];
142 1.1 ragge int sc_nexttx;
143 1.1 ragge int sc_nextrx;
144 1.1 ragge int sc_inq;
145 1.1 ragge int sc_lastack;
146 1.1 ragge };
147 1.1 ragge
148 1.1 ragge static int dematch(struct device *, struct cfdata *, void *);
149 1.1 ragge static void deattach(struct device *, struct device *, void *);
150 1.1 ragge static int dewait(struct de_softc *, char *);
151 1.1 ragge static void deinit(struct de_softc *);
152 1.1 ragge static int deioctl(struct ifnet *, u_long, caddr_t);
153 1.1 ragge static void dereset(struct device *);
154 1.1 ragge static void destart(struct ifnet *);
155 1.1 ragge static void derecv(struct de_softc *);
156 1.1 ragge static void dexmit(struct de_softc *);
157 1.1 ragge static void de_setaddr(u_char *, struct de_softc *);
158 1.1 ragge static void deintr(void *);
159 1.1 ragge static int de_add_rxbuf(struct de_softc *, int);
160 1.1 ragge
161 1.1 ragge struct cfattach de_ca = {
162 1.1 ragge sizeof(struct de_softc), dematch, deattach
163 1.1 ragge };
164 1.1 ragge
165 1.1 ragge #define DE_WCSR(csr, val) \
166 1.1 ragge bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
167 1.1 ragge #define DE_WLOW(val) \
168 1.1 ragge bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0, val)
169 1.1 ragge #define DE_WHIGH(val) \
170 1.1 ragge bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0 + 1, val)
171 1.1 ragge #define DE_RCSR(csr) \
172 1.1 ragge bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
173 1.1 ragge
174 1.1 ragge #define LOWORD(x) ((int)(x) & 0xffff)
175 1.1 ragge #define HIWORD(x) (((int)(x) >> 16) & 0x3)
176 1.1 ragge /*
177 1.1 ragge * Interface exists: make available by filling in network interface
178 1.1 ragge * record. System will initialize the interface when it is ready
179 1.1 ragge * to accept packets. We get the ethernet address here.
180 1.1 ragge */
181 1.1 ragge void
182 1.1 ragge deattach(struct device *parent, struct device *self, void *aux)
183 1.1 ragge {
184 1.1 ragge struct uba_attach_args *ua = aux;
185 1.1 ragge struct de_softc *sc = (struct de_softc *)self;
186 1.1 ragge struct ifnet *ifp = &sc->sc_if;
187 1.1 ragge u_int8_t myaddr[ETHER_ADDR_LEN];
188 1.1 ragge bus_dma_segment_t seg;
189 1.1 ragge int csr1, rseg, error, i;
190 1.1 ragge char *c;
191 1.1 ragge
192 1.1 ragge sc->sc_iot = ua->ua_iot;
193 1.1 ragge sc->sc_ioh = ua->ua_ioh;
194 1.1 ragge sc->sc_dmat = ua->ua_dmat;
195 1.1 ragge
196 1.1 ragge /*
197 1.1 ragge * What kind of a board is this?
198 1.1 ragge * The error bits 4-6 in pcsr1 are a device id as long as
199 1.1 ragge * the high byte is zero.
200 1.1 ragge */
201 1.1 ragge csr1 = DE_RCSR(DE_PCSR1);
202 1.1 ragge if (csr1 & 0xff60)
203 1.1 ragge c = "broken";
204 1.1 ragge else if (csr1 & 0x10)
205 1.1 ragge c = "delua";
206 1.1 ragge else
207 1.1 ragge c = "deuna";
208 1.1 ragge
209 1.1 ragge /*
210 1.1 ragge * Reset the board and temporarily map
211 1.1 ragge * the pcbb buffer onto the Unibus.
212 1.1 ragge */
213 1.1 ragge DE_WCSR(DE_PCSR0, 0); /* reset INTE */
214 1.1 ragge DELAY(100);
215 1.1 ragge DE_WCSR(DE_PCSR0, PCSR0_RSET);
216 1.1 ragge (void)dewait(sc, "reset");
217 1.1 ragge
218 1.1 ragge if ((error = bus_dmamem_alloc(sc->sc_dmat,
219 1.1 ragge sizeof(struct de_cdata), NBPG, 0, &seg, 1, &rseg,
220 1.1 ragge BUS_DMA_NOWAIT)) != 0) {
221 1.1 ragge printf(": unable to allocate control data, error = %d\n",
222 1.1 ragge error);
223 1.1 ragge goto fail_0;
224 1.1 ragge }
225 1.1 ragge if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
226 1.1 ragge sizeof(struct de_cdata), (caddr_t *)&sc->sc_dedata,
227 1.1 ragge BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
228 1.1 ragge printf(": unable to map control data, error = %d\n", error);
229 1.1 ragge goto fail_1;
230 1.1 ragge }
231 1.1 ragge
232 1.1 ragge /*
233 1.1 ragge * Create the transmit descriptor DMA maps.
234 1.1 ragge *
235 1.1 ragge * XXX - should allocate transmit map pages when needed, not here.
236 1.1 ragge */
237 1.1 ragge for (i = 0; i < NXMT; i++) {
238 1.1 ragge if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, NFRAGS,
239 1.1 ragge MCLBYTES, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
240 1.1 ragge &sc->sc_xmtmap[i]))) {
241 1.1 ragge printf(": unable to create tx DMA map %d, error = %d\n",
242 1.1 ragge i, error);
243 1.1 ragge goto fail_4;
244 1.1 ragge }
245 1.1 ragge }
246 1.1 ragge
247 1.1 ragge /*
248 1.1 ragge * Create receive buffer DMA maps.
249 1.1 ragge */
250 1.1 ragge for (i = 0; i < NRCV; i++) {
251 1.1 ragge if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
252 1.1 ragge MCLBYTES, 0, BUS_DMA_NOWAIT,
253 1.1 ragge &sc->sc_rcvmap[i]))) {
254 1.1 ragge printf(": unable to create rx DMA map %d, error = %d\n",
255 1.1 ragge i, error);
256 1.1 ragge goto fail_5;
257 1.1 ragge }
258 1.1 ragge }
259 1.1 ragge
260 1.1 ragge /*
261 1.1 ragge * Pre-allocate the receive buffers.
262 1.1 ragge */
263 1.1 ragge for (i = 0; i < NRCV; i++) {
264 1.1 ragge if ((error = de_add_rxbuf(sc, i)) != 0) {
265 1.1 ragge printf(": unable to allocate or map rx buffer %d\n,"
266 1.1 ragge " error = %d\n", i, error);
267 1.1 ragge goto fail_6;
268 1.1 ragge }
269 1.1 ragge }
270 1.1 ragge
271 1.1 ragge bzero(sc->sc_dedata, sizeof(struct de_cdata));
272 1.1 ragge sc->sc_pdedata = (struct de_cdata *)seg.ds_addr;
273 1.1 ragge
274 1.1 ragge /*
275 1.1 ragge * Tell the DEUNA about our PCB
276 1.1 ragge */
277 1.1 ragge DE_WCSR(DE_PCSR2, LOWORD(sc->sc_pdedata));
278 1.1 ragge DE_WCSR(DE_PCSR3, HIWORD(sc->sc_pdedata));
279 1.1 ragge DE_WLOW(CMD_GETPCBB);
280 1.1 ragge (void)dewait(sc, "pcbb");
281 1.1 ragge
282 1.1 ragge sc->sc_dedata->dc_pcbb.pcbb0 = FC_RDPHYAD;
283 1.1 ragge DE_WLOW(CMD_GETCMD);
284 1.1 ragge (void)dewait(sc, "read addr ");
285 1.1 ragge
286 1.1 ragge bcopy((caddr_t)&sc->sc_dedata->dc_pcbb.pcbb2, myaddr, sizeof (myaddr));
287 1.1 ragge printf("%s: %s, hardware address %s\n", c, sc->sc_dev.dv_xname,
288 1.1 ragge ether_sprintf(myaddr));
289 1.1 ragge
290 1.1 ragge uba_intr_establish(ua->ua_icookie, ua->ua_cvec, deintr, sc);
291 1.1 ragge uba_reset_establish(dereset, &sc->sc_dev);
292 1.1 ragge
293 1.1 ragge strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
294 1.1 ragge ifp->if_softc = sc;
295 1.1 ragge ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX;
296 1.1 ragge ifp->if_ioctl = deioctl;
297 1.1 ragge ifp->if_start = destart;
298 1.1 ragge if_attach(ifp);
299 1.1 ragge ether_ifattach(ifp, myaddr);
300 1.1 ragge #if NBPFILTER > 0
301 1.1 ragge bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
302 1.1 ragge #endif
303 1.1 ragge return;
304 1.1 ragge
305 1.1 ragge /*
306 1.1 ragge * Free any resources we've allocated during the failed attach
307 1.1 ragge * attempt. Do this in reverse order and fall through.
308 1.1 ragge */
309 1.1 ragge fail_6:
310 1.1 ragge for (i = 0; i < NRCV; i++) {
311 1.1 ragge if (sc->sc_rxmbuf[i] != NULL) {
312 1.1 ragge bus_dmamap_unload(sc->sc_dmat, sc->sc_xmtmap[i]);
313 1.1 ragge m_freem(sc->sc_rxmbuf[i]);
314 1.1 ragge }
315 1.1 ragge }
316 1.1 ragge fail_5:
317 1.1 ragge for (i = 0; i < NRCV; i++) {
318 1.1 ragge if (sc->sc_xmtmap[i] != NULL)
319 1.1 ragge bus_dmamap_destroy(sc->sc_dmat, sc->sc_xmtmap[i]);
320 1.1 ragge }
321 1.1 ragge fail_4:
322 1.1 ragge for (i = 0; i < NXMT; i++) {
323 1.1 ragge if (sc->sc_rcvmap[i] != NULL)
324 1.1 ragge bus_dmamap_destroy(sc->sc_dmat, sc->sc_rcvmap[i]);
325 1.1 ragge }
326 1.1 ragge bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_dedata,
327 1.1 ragge sizeof(struct de_cdata));
328 1.1 ragge fail_1:
329 1.1 ragge bus_dmamem_free(sc->sc_dmat, &seg, rseg);
330 1.1 ragge fail_0:
331 1.1 ragge return;
332 1.1 ragge }
333 1.1 ragge
334 1.1 ragge /*
335 1.1 ragge * Reset of interface after UNIBUS reset.
336 1.1 ragge */
337 1.1 ragge void
338 1.1 ragge dereset(struct device *dev)
339 1.1 ragge {
340 1.1 ragge struct de_softc *sc = (void *)dev;
341 1.1 ragge
342 1.1 ragge sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
343 1.1 ragge sc->sc_flags &= ~DSF_RUNNING;
344 1.1 ragge DE_WCSR(DE_PCSR0, PCSR0_RSET);
345 1.1 ragge (void)dewait(sc, "reset");
346 1.1 ragge deinit(sc);
347 1.1 ragge }
348 1.1 ragge
349 1.1 ragge /*
350 1.1 ragge * Initialization of interface; clear recorded pending
351 1.1 ragge * operations, and reinitialize UNIBUS usage.
352 1.1 ragge */
353 1.1 ragge void
354 1.1 ragge deinit(struct de_softc *sc)
355 1.1 ragge {
356 1.1 ragge struct de_cdata *dc;
357 1.1 ragge int s, i;
358 1.1 ragge
359 1.1 ragge if (sc->sc_flags & DSF_RUNNING)
360 1.1 ragge return;
361 1.1 ragge /*
362 1.1 ragge * Tell the DEUNA about our PCB
363 1.1 ragge */
364 1.1 ragge DE_WCSR(DE_PCSR2, LOWORD(sc->sc_pdedata));
365 1.1 ragge DE_WCSR(DE_PCSR3, HIWORD(sc->sc_pdedata));
366 1.1 ragge DE_WLOW(0); /* reset INTE */
367 1.1 ragge DELAY(500);
368 1.1 ragge DE_WLOW(CMD_GETPCBB);
369 1.1 ragge (void)dewait(sc, "pcbb");
370 1.1 ragge
371 1.1 ragge dc = sc->sc_dedata;
372 1.1 ragge /* set the transmit and receive ring header addresses */
373 1.1 ragge dc->dc_pcbb.pcbb0 = FC_WTRING;
374 1.1 ragge dc->dc_pcbb.pcbb2 = LOWORD(&sc->sc_pdedata->dc_udbbuf);
375 1.1 ragge dc->dc_pcbb.pcbb2 = HIWORD(&sc->sc_pdedata->dc_udbbuf);
376 1.1 ragge
377 1.1 ragge dc->dc_udbbuf.b_tdrbl = LOWORD(&sc->sc_pdedata->dc_xrent[0]);
378 1.1 ragge dc->dc_udbbuf.b_tdrbh = HIWORD(&sc->sc_pdedata->dc_xrent[0]);
379 1.1 ragge dc->dc_udbbuf.b_telen = sizeof (struct de_ring) / sizeof(u_int16_t);
380 1.1 ragge dc->dc_udbbuf.b_trlen = NXMT;
381 1.1 ragge dc->dc_udbbuf.b_rdrbl = LOWORD(&sc->sc_pdedata->dc_rrent[0]);
382 1.1 ragge dc->dc_udbbuf.b_rdrbh = HIWORD(&sc->sc_pdedata->dc_rrent[0]);
383 1.1 ragge dc->dc_udbbuf.b_relen = sizeof (struct de_ring) / sizeof(u_int16_t);
384 1.1 ragge dc->dc_udbbuf.b_rrlen = NRCV;
385 1.1 ragge
386 1.1 ragge DE_WLOW(CMD_GETCMD);
387 1.1 ragge (void)dewait(sc, "wtring");
388 1.1 ragge
389 1.1 ragge /* initialize the mode - enable hardware padding */
390 1.1 ragge dc->dc_pcbb.pcbb0 = FC_WTMODE;
391 1.1 ragge /* let hardware do padding - set MTCH bit on broadcast */
392 1.1 ragge dc->dc_pcbb.pcbb2 = MOD_TPAD|MOD_HDX;
393 1.1 ragge DE_WLOW(CMD_GETCMD);
394 1.1 ragge (void)dewait(sc, "wtmode");
395 1.1 ragge
396 1.1 ragge /* set up the receive and transmit ring entries */
397 1.1 ragge for (i = 0; i < NXMT; i++) {
398 1.1 ragge if (sc->sc_txmbuf[i]) {
399 1.1 ragge bus_dmamap_unload(sc->sc_dmat, sc->sc_xmtmap[i]);
400 1.1 ragge m_freem(sc->sc_txmbuf[i]);
401 1.1 ragge sc->sc_txmbuf[i] = 0;
402 1.1 ragge }
403 1.1 ragge dc->dc_xrent[i].r_flags = 0;
404 1.1 ragge }
405 1.1 ragge for (i = 0; i < NRCV; i++)
406 1.1 ragge dc->dc_rrent[i].r_flags = RFLG_OWN;
407 1.1 ragge sc->sc_nexttx = sc->sc_inq = sc->sc_lastack = sc->sc_nextrx = 0;
408 1.1 ragge
409 1.1 ragge /* start up the board (rah rah) */
410 1.1 ragge s = splnet();
411 1.1 ragge sc->sc_if.if_flags |= IFF_RUNNING;
412 1.1 ragge DE_WLOW(PCSR0_INTE);
413 1.1 ragge destart(&sc->sc_if); /* queue output packets */
414 1.1 ragge sc->sc_flags |= DSF_RUNNING; /* need before de_setaddr */
415 1.1 ragge if (sc->sc_flags & DSF_SETADDR)
416 1.1 ragge de_setaddr(LLADDR(sc->sc_if.if_sadl), sc);
417 1.1 ragge DE_WLOW(CMD_START|PCSR0_INTE);
418 1.1 ragge splx(s);
419 1.1 ragge }
420 1.1 ragge
421 1.1 ragge /*
422 1.1 ragge * Setup output on interface.
423 1.1 ragge * Get another datagram to send off of the interface queue,
424 1.1 ragge * and map it to the interface before starting the output.
425 1.1 ragge * Must be called from ipl >= our interrupt level.
426 1.1 ragge */
427 1.1 ragge void
428 1.1 ragge destart(struct ifnet *ifp)
429 1.1 ragge {
430 1.1 ragge struct de_softc *sc = ifp->if_softc;
431 1.1 ragge bus_dmamap_t map;
432 1.1 ragge struct de_ring *rp = 0;
433 1.1 ragge struct mbuf *m, *m0;
434 1.1 ragge int idx, i, err, seg, s;
435 1.1 ragge
436 1.1 ragge /*
437 1.1 ragge * the following test is necessary, since
438 1.1 ragge * the code is not reentrant and we have
439 1.1 ragge * multiple transmission buffers.
440 1.1 ragge */
441 1.1 ragge if (ifp->if_flags & IFF_OACTIVE)
442 1.1 ragge return;
443 1.1 ragge s = splimp();
444 1.1 ragge while (sc->sc_inq < (NXMT - 1)) {
445 1.1 ragge idx = sc->sc_nexttx;
446 1.1 ragge IF_DEQUEUE(&ifp->if_snd, m);
447 1.1 ragge if (m == 0)
448 1.1 ragge goto out;
449 1.1 ragge /*
450 1.1 ragge * Count number of mbufs in chain.
451 1.1 ragge * Always do DMA directly from mbufs.
452 1.1 ragge */
453 1.1 ragge for (m0 = m, i = 0; m0; m0 = m0->m_next)
454 1.1 ragge if (m0->m_len)
455 1.1 ragge i++;
456 1.1 ragge if (i >= NFRAGS) { /* XXX pullup */
457 1.1 ragge panic("destart");
458 1.1 ragge }
459 1.1 ragge
460 1.1 ragge if ((i + sc->sc_inq) >= (NXMT - 1)) {
461 1.1 ragge IF_PREPEND(&sc->sc_if.if_snd, m);
462 1.1 ragge ifp->if_flags |= IFF_OACTIVE;
463 1.1 ragge goto out;
464 1.1 ragge }
465 1.1 ragge #if NBPFILTER > 0
466 1.1 ragge if (ifp->if_bpf)
467 1.1 ragge bpf_mtap(ifp->if_bpf, m);
468 1.1 ragge #endif
469 1.1 ragge /*
470 1.1 ragge * m now points to a mbuf chain that can be loaded.
471 1.1 ragge * Loop around and set it.
472 1.1 ragge */
473 1.1 ragge err = bus_dmamap_load_mbuf(sc->sc_dmat, sc->sc_xmtmap[idx],
474 1.1 ragge m, BUS_DMA_NOWAIT);
475 1.1 ragge if (err) /* Can't fail here */
476 1.1 ragge panic("destart: load_mbuf failed, err %d", err);
477 1.1 ragge
478 1.1 ragge sc->sc_txmbuf[idx] = m;
479 1.1 ragge map = sc->sc_xmtmap[idx];
480 1.1 ragge for (seg = 0; seg < map->dm_nsegs; seg++) {
481 1.1 ragge rp = &sc->sc_dedata->dc_xrent[idx];
482 1.1 ragge
483 1.1 ragge rp->r_flags = seg == 0 ? XFLG_STP : 0;
484 1.1 ragge rp->r_slen = map->dm_segs[seg].ds_len;
485 1.1 ragge rp->r_segbl = LOWORD(map->dm_segs[seg].ds_addr);
486 1.1 ragge rp->r_segbh = HIWORD(map->dm_segs[seg].ds_addr);
487 1.1 ragge
488 1.1 ragge if (++idx == NXMT)
489 1.1 ragge idx = 0;
490 1.1 ragge sc->sc_inq++;
491 1.1 ragge }
492 1.1 ragge rp->r_flags |= XFLG_ENP|XFLG_OWN;
493 1.1 ragge sc->sc_nexttx = idx;
494 1.1 ragge }
495 1.1 ragge if (sc->sc_inq == (NXMT - 1))
496 1.1 ragge ifp->if_flags |= IFF_OACTIVE;
497 1.1 ragge
498 1.1 ragge out: if (sc->sc_inq)
499 1.1 ragge ifp->if_timer = 5; /* If transmit logic dies */
500 1.1 ragge
501 1.1 ragge if (sc->sc_flags & DSF_RUNNING)
502 1.1 ragge DE_WLOW(PCSR0_INTE|CMD_PDMD);
503 1.1 ragge splx(s);
504 1.1 ragge }
505 1.1 ragge
506 1.1 ragge /*
507 1.1 ragge * Command done interrupt.
508 1.1 ragge */
509 1.1 ragge void
510 1.1 ragge deintr(void *arg)
511 1.1 ragge {
512 1.1 ragge struct de_softc *sc = arg;
513 1.1 ragge short csr0;
514 1.1 ragge
515 1.1 ragge /* save flags right away - clear out interrupt bits */
516 1.1 ragge csr0 = DE_RCSR(DE_PCSR0);
517 1.1 ragge DE_WHIGH(csr0 >> 8);
518 1.1 ragge
519 1.1 ragge if (csr0 & PCSR0_RXI)
520 1.1 ragge derecv(sc);
521 1.1 ragge
522 1.1 ragge if (csr0 & PCSR0_TXI)
523 1.1 ragge dexmit(sc);
524 1.1 ragge
525 1.1 ragge #ifdef notyet
526 1.1 ragge Handle error interrupts
527 1.1 ragge #endif
528 1.1 ragge destart(&sc->sc_if);
529 1.1 ragge }
530 1.1 ragge
531 1.1 ragge void
532 1.1 ragge dexmit(struct de_softc *sc)
533 1.1 ragge {
534 1.1 ragge struct ifnet *ifp = &sc->sc_if;
535 1.1 ragge struct de_ring *rp;
536 1.1 ragge int midx;
537 1.1 ragge
538 1.1 ragge /*
539 1.1 ragge * Poll transmit ring and check status.
540 1.1 ragge * Then free buffer space and check for
541 1.1 ragge * more transmit requests.
542 1.1 ragge */
543 1.1 ragge midx = -1;
544 1.1 ragge rp = &sc->sc_dedata->dc_xrent[sc->sc_lastack];
545 1.1 ragge while ((rp->r_flags & XFLG_OWN) == 0) {
546 1.1 ragge if (sc->sc_txmbuf[sc->sc_lastack])
547 1.1 ragge midx = sc->sc_lastack;
548 1.1 ragge if (rp->r_flags & XFLG_ENP) {
549 1.1 ragge if (midx >= 0)
550 1.1 ragge m_freem(sc->sc_txmbuf[midx]);
551 1.1 ragge ifp->if_opackets++;
552 1.1 ragge }
553 1.1 ragge if (rp->r_flags & (XFLG_ERRS|XFLG_MTCH|XFLG_ONE|XFLG_MORE)) {
554 1.1 ragge if (rp->r_flags & XFLG_ERRS) {
555 1.1 ragge ifp->if_oerrors++;
556 1.1 ragge } else if (rp->r_flags & XFLG_ONE) {
557 1.1 ragge ifp->if_collisions++;
558 1.1 ragge } else if (rp->r_flags & XFLG_MORE) {
559 1.1 ragge ifp->if_collisions += 3;
560 1.1 ragge }
561 1.1 ragge /* else if (rp->r_flags & XFLG_MTCH)
562 1.1 ragge * Matches ourself, but why care?
563 1.1 ragge * Let upper layer deal with this.
564 1.1 ragge */
565 1.1 ragge }
566 1.1 ragge if (++sc->sc_lastack == NXMT)
567 1.1 ragge sc->sc_lastack = 0;
568 1.1 ragge sc->sc_inq--;
569 1.1 ragge rp = &sc->sc_dedata->dc_xrent[sc->sc_lastack];
570 1.1 ragge }
571 1.1 ragge ifp->if_flags &= ~IFF_OACTIVE;
572 1.1 ragge if (sc->sc_inq == 0)
573 1.1 ragge ifp->if_timer = 0;
574 1.1 ragge }
575 1.1 ragge
576 1.1 ragge /*
577 1.1 ragge * Ethernet interface receiver interface.
578 1.1 ragge * If input error just drop packet.
579 1.1 ragge * Otherwise purge input buffered data path and examine
580 1.1 ragge * packet to determine type. If can't determine length
581 1.1 ragge * from type, then have to drop packet. Othewise decapsulate
582 1.1 ragge * packet based on type and pass to type specific higher-level
583 1.1 ragge * input routine.
584 1.1 ragge */
585 1.1 ragge void
586 1.1 ragge derecv(struct de_softc *sc)
587 1.1 ragge {
588 1.1 ragge struct ifnet *ifp = &sc->sc_if;
589 1.1 ragge struct ether_header *eh;
590 1.1 ragge struct de_ring *rp;
591 1.1 ragge struct mbuf *m;
592 1.1 ragge int len;
593 1.1 ragge
594 1.1 ragge rp = &sc->sc_dedata->dc_rrent[sc->sc_nextrx];
595 1.1 ragge while ((rp->r_flags & RFLG_OWN) == 0) {
596 1.1 ragge ifp->if_ipackets++;
597 1.1 ragge len = (rp->r_lenerr&RERR_MLEN) - 4; /* don't forget checksum! */
598 1.1 ragge /* check for errors */
599 1.1 ragge if ((rp->r_flags & (RFLG_ERRS|RFLG_FRAM|RFLG_OFLO|RFLG_CRC)) ||
600 1.1 ragge (rp->r_flags&(RFLG_STP|RFLG_ENP)) != (RFLG_STP|RFLG_ENP) ||
601 1.1 ragge (rp->r_lenerr & (RERR_BUFL|RERR_UBTO|RERR_NCHN)) ||
602 1.1 ragge len < ETHERMIN || len > ETHERMTU) {
603 1.1 ragge ifp->if_ierrors++;
604 1.1 ragge goto next;
605 1.1 ragge }
606 1.1 ragge m = sc->sc_rxmbuf[sc->sc_nextrx];
607 1.1 ragge de_add_rxbuf(sc, sc->sc_nextrx);
608 1.1 ragge m->m_pkthdr.rcvif = ifp;
609 1.1 ragge m->m_pkthdr.len = m->m_len = len;
610 1.1 ragge eh = mtod(m, struct ether_header *);
611 1.1 ragge #if NBPFILTER > 0
612 1.1 ragge if (ifp->if_bpf) {
613 1.1 ragge bpf_mtap(ifp->if_bpf, m);
614 1.1 ragge if ((ifp->if_flags & IFF_PROMISC) != 0 &&
615 1.1 ragge bcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
616 1.1 ragge ETHER_ADDR_LEN) != 0 &&
617 1.1 ragge ((eh->ether_dhost[0] & 1) == 0)) {
618 1.1 ragge m_freem(m);
619 1.1 ragge goto next;
620 1.1 ragge }
621 1.1 ragge }
622 1.1 ragge #endif
623 1.1 ragge (*ifp->if_input)(ifp, m);
624 1.1 ragge
625 1.1 ragge /* hang the receive buffer again */
626 1.1 ragge next: rp->r_lenerr = 0;
627 1.1 ragge rp->r_flags = RFLG_OWN;
628 1.1 ragge
629 1.1 ragge /* check next receive buffer */
630 1.1 ragge if (++sc->sc_nextrx == NRCV)
631 1.1 ragge sc->sc_nextrx = 0;
632 1.1 ragge rp = &sc->sc_dedata->dc_rrent[sc->sc_nextrx];
633 1.1 ragge }
634 1.1 ragge }
635 1.1 ragge
636 1.1 ragge /*
637 1.1 ragge * Add a receive buffer to the indicated descriptor.
638 1.1 ragge */
639 1.1 ragge int
640 1.1 ragge de_add_rxbuf(sc, i)
641 1.1 ragge struct de_softc *sc;
642 1.1 ragge int i;
643 1.1 ragge {
644 1.1 ragge struct mbuf *m;
645 1.1 ragge struct de_ring *rp;
646 1.1 ragge vaddr_t addr;
647 1.1 ragge int error;
648 1.1 ragge
649 1.1 ragge MGETHDR(m, M_DONTWAIT, MT_DATA);
650 1.1 ragge if (m == NULL)
651 1.1 ragge return (ENOBUFS);
652 1.1 ragge
653 1.1 ragge MCLGET(m, M_DONTWAIT);
654 1.1 ragge if ((m->m_flags & M_EXT) == 0) {
655 1.1 ragge m_freem(m);
656 1.1 ragge return (ENOBUFS);
657 1.1 ragge }
658 1.1 ragge
659 1.1 ragge if (sc->sc_rxmbuf[i] != NULL)
660 1.1 ragge bus_dmamap_unload(sc->sc_dmat, sc->sc_rcvmap[i]);
661 1.1 ragge
662 1.1 ragge error = bus_dmamap_load(sc->sc_dmat, sc->sc_rcvmap[i],
663 1.1 ragge m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
664 1.1 ragge if (error)
665 1.1 ragge panic("%s: can't load rx DMA map %d, error = %d\n",
666 1.1 ragge sc->sc_dev.dv_xname, i, error);
667 1.1 ragge sc->sc_rxmbuf[i] = m;
668 1.1 ragge
669 1.1 ragge bus_dmamap_sync(sc->sc_dmat, sc->sc_rcvmap[i], 0,
670 1.1 ragge sc->sc_rcvmap[i]->dm_mapsize, BUS_DMASYNC_PREREAD);
671 1.1 ragge
672 1.1 ragge /*
673 1.1 ragge * We know that the mbuf cluster is page aligned. Also, be sure
674 1.1 ragge * that the IP header will be longword aligned.
675 1.1 ragge */
676 1.1 ragge m->m_data += 2;
677 1.1 ragge addr = sc->sc_rcvmap[i]->dm_segs[0].ds_addr + 2;
678 1.1 ragge rp = &sc->sc_dedata->dc_rrent[i];
679 1.1 ragge rp->r_lenerr = 0;
680 1.1 ragge rp->r_segbl = LOWORD(addr);
681 1.1 ragge rp->r_segbh = HIWORD(addr);
682 1.1 ragge rp->r_slen = m->m_ext.ext_size - 2;
683 1.1 ragge rp->r_flags = RFLG_OWN;
684 1.1 ragge
685 1.1 ragge return (0);
686 1.1 ragge }
687 1.1 ragge
688 1.1 ragge
689 1.1 ragge /*
690 1.1 ragge * Process an ioctl request.
691 1.1 ragge */
692 1.1 ragge int
693 1.1 ragge deioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
694 1.1 ragge {
695 1.1 ragge struct ifaddr *ifa = (struct ifaddr *)data;
696 1.1 ragge struct de_softc *sc = ifp->if_softc;
697 1.1 ragge int s = splnet(), error = 0;
698 1.1 ragge
699 1.1 ragge switch (cmd) {
700 1.1 ragge
701 1.1 ragge case SIOCSIFADDR:
702 1.1 ragge ifp->if_flags |= IFF_UP;
703 1.1 ragge deinit(sc);
704 1.1 ragge
705 1.1 ragge switch (ifa->ifa_addr->sa_family) {
706 1.1 ragge #ifdef INET
707 1.1 ragge case AF_INET:
708 1.1 ragge arp_ifinit(ifp, ifa);
709 1.1 ragge break;
710 1.1 ragge #endif
711 1.1 ragge #ifdef NS
712 1.1 ragge case AF_NS:
713 1.1 ragge {
714 1.1 ragge register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
715 1.1 ragge
716 1.1 ragge if (ns_nullhost(*ina))
717 1.1 ragge ina->x_host =
718 1.1 ragge *(union ns_host *)LLADDR(ifp->if_sadl);
719 1.1 ragge else
720 1.1 ragge de_setaddr(ina->x_host.c_host, ds);
721 1.1 ragge break;
722 1.1 ragge }
723 1.1 ragge #endif
724 1.1 ragge }
725 1.1 ragge break;
726 1.1 ragge
727 1.1 ragge case SIOCSIFFLAGS:
728 1.1 ragge if ((ifp->if_flags & IFF_UP) == 0 &&
729 1.1 ragge sc->sc_flags & DSF_RUNNING) {
730 1.1 ragge DE_WLOW(0);
731 1.1 ragge DELAY(5000);
732 1.1 ragge DE_WLOW(PCSR0_RSET);
733 1.1 ragge sc->sc_flags &= ~DSF_RUNNING;
734 1.1 ragge ifp->if_flags &= ~IFF_OACTIVE;
735 1.1 ragge } else if (ifp->if_flags & IFF_UP &&
736 1.1 ragge (sc->sc_flags & DSF_RUNNING) == 0)
737 1.1 ragge deinit(sc);
738 1.1 ragge break;
739 1.1 ragge
740 1.1 ragge default:
741 1.1 ragge error = EINVAL;
742 1.1 ragge }
743 1.1 ragge splx(s);
744 1.1 ragge return (error);
745 1.1 ragge }
746 1.1 ragge
747 1.1 ragge /*
748 1.1 ragge * set ethernet address for unit
749 1.1 ragge */
750 1.1 ragge void
751 1.1 ragge de_setaddr(u_char *physaddr, struct de_softc *sc)
752 1.1 ragge {
753 1.1 ragge
754 1.1 ragge if (! (sc->sc_flags & DSF_RUNNING))
755 1.1 ragge return;
756 1.1 ragge
757 1.1 ragge bcopy((caddr_t) physaddr, (caddr_t) &sc->sc_dedata->dc_pcbb.pcbb2, 6);
758 1.1 ragge sc->sc_dedata->dc_pcbb.pcbb0 = FC_WTPHYAD;
759 1.1 ragge DE_WLOW(PCSR0_INTE|CMD_GETCMD);
760 1.1 ragge if (dewait(sc, "address change") == 0) {
761 1.1 ragge sc->sc_flags |= DSF_SETADDR;
762 1.1 ragge bcopy((caddr_t) physaddr, LLADDR(sc->sc_if.if_sadl), 6);
763 1.1 ragge }
764 1.1 ragge }
765 1.1 ragge
766 1.1 ragge /*
767 1.1 ragge * Await completion of the named function
768 1.1 ragge * and check for errors.
769 1.1 ragge */
770 1.1 ragge int
771 1.1 ragge dewait(struct de_softc *sc, char *fn)
772 1.1 ragge {
773 1.1 ragge int csr0;
774 1.1 ragge
775 1.1 ragge while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
776 1.1 ragge ;
777 1.1 ragge csr0 = DE_RCSR(DE_PCSR0);
778 1.1 ragge DE_WHIGH(csr0 >> 8);
779 1.1 ragge if (csr0 & PCSR0_PCEI) {
780 1.1 ragge char bits[64];
781 1.1 ragge
782 1.1 ragge printf("%s: %s failed, csr0=%s ", sc->sc_dev.dv_xname, fn,
783 1.1 ragge bitmask_snprintf(csr0, PCSR0_BITS, bits, sizeof(bits)));
784 1.1 ragge printf("csr1=%s\n", bitmask_snprintf(DE_RCSR(DE_PCSR1),
785 1.1 ragge PCSR1_BITS, bits, sizeof(bits)));
786 1.1 ragge }
787 1.1 ragge return (csr0 & PCSR0_PCEI);
788 1.1 ragge }
789 1.1 ragge
790 1.1 ragge int
791 1.1 ragge dematch(struct device *parent, struct cfdata *cf, void *aux)
792 1.1 ragge {
793 1.1 ragge struct uba_attach_args *ua = aux;
794 1.1 ragge struct de_softc ssc;
795 1.1 ragge struct de_softc *sc = &ssc;
796 1.1 ragge int i;
797 1.1 ragge
798 1.1 ragge sc->sc_iot = ua->ua_iot;
799 1.1 ragge sc->sc_ioh = ua->ua_ioh;
800 1.1 ragge /*
801 1.1 ragge * Make sure self-test is finished before we screw with the board.
802 1.1 ragge * Self-test on a DELUA can take 15 seconds (argh).
803 1.1 ragge */
804 1.1 ragge for (i = 0;
805 1.1 ragge (i < 160) &&
806 1.1 ragge (DE_RCSR(DE_PCSR0) & PCSR0_FATI) == 0 &&
807 1.1 ragge (DE_RCSR(DE_PCSR1) & PCSR1_STMASK) == STAT_RESET;
808 1.1 ragge ++i)
809 1.1 ragge DELAY(50000);
810 1.1 ragge if (((DE_RCSR(DE_PCSR0) & PCSR0_FATI) != 0) ||
811 1.1 ragge (((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_READY) &&
812 1.1 ragge ((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_RUN)))
813 1.1 ragge return(0);
814 1.1 ragge
815 1.1 ragge DE_WCSR(DE_PCSR0, 0);
816 1.1 ragge DELAY(5000);
817 1.1 ragge DE_WCSR(DE_PCSR0, PCSR0_RSET);
818 1.1 ragge while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
819 1.1 ragge ;
820 1.1 ragge /* make board interrupt by executing a GETPCBB command */
821 1.1 ragge DE_WCSR(DE_PCSR0, PCSR0_INTE);
822 1.1 ragge DE_WCSR(DE_PCSR2, 0);
823 1.1 ragge DE_WCSR(DE_PCSR3, 0);
824 1.1 ragge DE_WCSR(DE_PCSR0, PCSR0_INTE|CMD_GETPCBB);
825 1.1 ragge DELAY(50000);
826 1.1 ragge
827 1.1 ragge return 1;
828 1.1 ragge }
829