if_le.c revision 1.5 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1982, 1990 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd *
33 1.2 cgd * from: @(#)if_le.c 7.6 (Berkeley) 5/8/91
34 1.5 mycroft * $Id: if_le.c,v 1.5 1994/02/05 05:06:54 mycroft Exp $
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #include "le.h"
38 1.1 cgd #if NLE > 0
39 1.1 cgd
40 1.1 cgd #include "bpfilter.h"
41 1.1 cgd
42 1.1 cgd /*
43 1.1 cgd * AMD 7990 LANCE
44 1.5 mycroft */
45 1.5 mycroft #include <sys/param.h>
46 1.5 mycroft #include <sys/systm.h>
47 1.5 mycroft #include <sys/kernel.h>
48 1.5 mycroft #include <sys/mbuf.h>
49 1.5 mycroft #include <sys/buf.h>
50 1.5 mycroft #include <sys/socket.h>
51 1.5 mycroft #include <sys/syslog.h>
52 1.5 mycroft #include <sys/ioctl.h>
53 1.5 mycroft #include <sys/malloc.h>
54 1.5 mycroft #include <sys/errno.h>
55 1.5 mycroft
56 1.5 mycroft #include <net/if.h>
57 1.5 mycroft #include <net/netisr.h>
58 1.5 mycroft #include <net/route.h>
59 1.5 mycroft #if NBPFILTER > 0
60 1.5 mycroft #include <net/bpf.h>
61 1.5 mycroft #include <net/bpfdesc.h>
62 1.5 mycroft #endif
63 1.1 cgd
64 1.1 cgd #ifdef INET
65 1.5 mycroft #include <netinet/in.h>
66 1.5 mycroft #include <netinet/in_systm.h>
67 1.5 mycroft #include <netinet/in_var.h>
68 1.5 mycroft #include <netinet/ip.h>
69 1.5 mycroft #include <netinet/if_ether.h>
70 1.1 cgd #endif
71 1.1 cgd
72 1.1 cgd #ifdef NS
73 1.5 mycroft #include <netns/ns.h>
74 1.5 mycroft #include <netns/ns_if.h>
75 1.1 cgd #endif
76 1.1 cgd
77 1.5 mycroft #include <machine/cpu.h>
78 1.5 mycroft #include <hp300/hp300/isr.h>
79 1.5 mycroft #include <machine/mtpr.h>
80 1.1 cgd
81 1.5 mycroft #include <hp300/dev/device.h>
82 1.5 mycroft #include <hp300/dev/if_lereg.h>
83 1.1 cgd
84 1.1 cgd /* offsets for: ID, REGS, MEM, NVRAM */
85 1.1 cgd int lestd[] = { 0, 0x4000, 0x8000, 0xC008 };
86 1.1 cgd
87 1.1 cgd int leattach();
88 1.1 cgd struct driver ledriver = {
89 1.1 cgd leattach, "le",
90 1.1 cgd };
91 1.1 cgd
92 1.1 cgd struct isr le_isr[NLE];
93 1.1 cgd int ledebug = 0; /* console error messages */
94 1.1 cgd
95 1.1 cgd int leintr(), leinit(), leioctl(), lestart(), ether_output();
96 1.1 cgd struct mbuf *leget();
97 1.1 cgd extern struct ifnet loif;
98 1.1 cgd
99 1.1 cgd /*
100 1.1 cgd * Ethernet software status per interface.
101 1.1 cgd *
102 1.1 cgd * Each interface is referenced by a network interface structure,
103 1.1 cgd * le_if, which the routing code uses to locate the interface.
104 1.1 cgd * This structure contains the output queue for the interface, its address, ...
105 1.1 cgd */
106 1.1 cgd struct le_softc {
107 1.1 cgd struct arpcom sc_ac; /* common Ethernet structures */
108 1.1 cgd #define sc_if sc_ac.ac_if /* network-visible interface */
109 1.1 cgd #define sc_addr sc_ac.ac_enaddr /* hardware Ethernet address */
110 1.1 cgd struct lereg0 *sc_r0; /* DIO registers */
111 1.1 cgd struct lereg1 *sc_r1; /* LANCE registers */
112 1.1 cgd struct lereg2 *sc_r2; /* dual-port RAM */
113 1.1 cgd int sc_rmd; /* predicted next rmd to process */
114 1.1 cgd int sc_runt;
115 1.1 cgd int sc_jab;
116 1.1 cgd int sc_merr;
117 1.1 cgd int sc_babl;
118 1.1 cgd int sc_cerr;
119 1.1 cgd int sc_miss;
120 1.1 cgd int sc_xint;
121 1.1 cgd int sc_xown;
122 1.1 cgd int sc_uflo;
123 1.1 cgd int sc_rxlen;
124 1.1 cgd int sc_rxoff;
125 1.1 cgd int sc_txoff;
126 1.1 cgd int sc_busy;
127 1.1 cgd short sc_iflags;
128 1.1 cgd #if NBPFILTER > 0
129 1.1 cgd caddr_t sc_bpf;
130 1.1 cgd #endif
131 1.1 cgd } le_softc[NLE];
132 1.1 cgd
133 1.1 cgd /* access LANCE registers */
134 1.1 cgd #define LERDWR(cntl, src, dst) \
135 1.1 cgd do { \
136 1.1 cgd (dst) = (src); \
137 1.1 cgd } while (((cntl)->ler0_status & LE_ACK) == 0);
138 1.1 cgd
139 1.1 cgd /*
140 1.1 cgd * Interface exists: make available by filling in network interface
141 1.1 cgd * record. System will initialize the interface when it is ready
142 1.1 cgd * to accept packets.
143 1.1 cgd */
144 1.1 cgd leattach(hd)
145 1.1 cgd struct hp_device *hd;
146 1.1 cgd {
147 1.1 cgd register struct lereg0 *ler0;
148 1.1 cgd register struct lereg2 *ler2;
149 1.1 cgd struct lereg2 *lemem = 0;
150 1.5 mycroft struct le_softc *sc = &le_softc[hd->hp_unit];
151 1.5 mycroft struct ifnet *ifp = &sc->sc_if;
152 1.1 cgd char *cp;
153 1.1 cgd int i;
154 1.1 cgd
155 1.5 mycroft ler0 = sc->sc_r0 = (struct lereg0 *)(lestd[0] + (int)hd->hp_addr);
156 1.5 mycroft sc->sc_r1 = (struct lereg1 *)(lestd[1] + (int)hd->hp_addr);
157 1.5 mycroft ler2 = sc->sc_r2 = (struct lereg2 *)(lestd[2] + (int)hd->hp_addr);
158 1.1 cgd if (ler0->ler0_id != LEID)
159 1.1 cgd return(0);
160 1.1 cgd le_isr[hd->hp_unit].isr_intr = leintr;
161 1.1 cgd hd->hp_ipl = le_isr[hd->hp_unit].isr_ipl = LE_IPL(ler0->ler0_status);
162 1.1 cgd le_isr[hd->hp_unit].isr_arg = hd->hp_unit;
163 1.1 cgd ler0->ler0_id = 0xFF;
164 1.1 cgd DELAY(100);
165 1.1 cgd
166 1.1 cgd /*
167 1.1 cgd * Read the ethernet address off the board, one nibble at a time.
168 1.1 cgd */
169 1.1 cgd cp = (char *)(lestd[3] + (int)hd->hp_addr);
170 1.5 mycroft for (i = 0; i < sizeof(sc->sc_addr); i++) {
171 1.5 mycroft sc->sc_addr[i] = (*++cp & 0xF) << 4;
172 1.1 cgd cp++;
173 1.5 mycroft sc->sc_addr[i] |= *++cp & 0xF;
174 1.1 cgd cp++;
175 1.1 cgd }
176 1.1 cgd printf("le%d: hardware address %s\n", hd->hp_unit,
177 1.5 mycroft ether_sprintf(sc->sc_addr));
178 1.1 cgd
179 1.1 cgd /*
180 1.1 cgd * Setup for transmit/receive
181 1.1 cgd */
182 1.1 cgd ler2->ler2_mode = LE_MODE;
183 1.5 mycroft ler2->ler2_padr[0] = sc->sc_addr[1];
184 1.5 mycroft ler2->ler2_padr[1] = sc->sc_addr[0];
185 1.5 mycroft ler2->ler2_padr[2] = sc->sc_addr[3];
186 1.5 mycroft ler2->ler2_padr[3] = sc->sc_addr[2];
187 1.5 mycroft ler2->ler2_padr[4] = sc->sc_addr[5];
188 1.5 mycroft ler2->ler2_padr[5] = sc->sc_addr[4];
189 1.1 cgd ler2->ler2_rlen = LE_RLEN;
190 1.1 cgd ler2->ler2_rdra = (int)lemem->ler2_rmd;
191 1.1 cgd ler2->ler2_tlen = LE_TLEN;
192 1.1 cgd ler2->ler2_tdra = (int)lemem->ler2_tmd;
193 1.1 cgd isrlink(&le_isr[hd->hp_unit]);
194 1.1 cgd ler0->ler0_status = LE_IE;
195 1.1 cgd
196 1.1 cgd ifp->if_unit = hd->hp_unit;
197 1.1 cgd ifp->if_name = "le";
198 1.1 cgd ifp->if_mtu = ETHERMTU;
199 1.1 cgd ifp->if_ioctl = leioctl;
200 1.1 cgd ifp->if_output = ether_output;
201 1.1 cgd ifp->if_start = lestart;
202 1.5 mycroft ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
203 1.5 mycroft IFF_NOTRAILERS;
204 1.1 cgd #if NBPFILTER > 0
205 1.5 mycroft bpfattach(&sc->sc_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
206 1.1 cgd #endif
207 1.1 cgd if_attach(ifp);
208 1.1 cgd return (1);
209 1.1 cgd }
210 1.1 cgd
211 1.1 cgd ledrinit(ler2)
212 1.1 cgd register struct lereg2 *ler2;
213 1.1 cgd {
214 1.1 cgd register struct lereg2 *lemem = 0;
215 1.1 cgd register int i;
216 1.1 cgd
217 1.1 cgd for (i = 0; i < LERBUF; i++) {
218 1.1 cgd ler2->ler2_rmd[i].rmd0 = (int)lemem->ler2_rbuf[i];
219 1.1 cgd ler2->ler2_rmd[i].rmd1 = LE_OWN;
220 1.1 cgd ler2->ler2_rmd[i].rmd2 = -LEMTU;
221 1.1 cgd ler2->ler2_rmd[i].rmd3 = 0;
222 1.1 cgd }
223 1.1 cgd for (i = 0; i < LETBUF; i++) {
224 1.1 cgd ler2->ler2_tmd[i].tmd0 = (int)lemem->ler2_tbuf[i];
225 1.1 cgd ler2->ler2_tmd[i].tmd1 = 0;
226 1.1 cgd ler2->ler2_tmd[i].tmd2 = 0;
227 1.1 cgd ler2->ler2_tmd[i].tmd3 = 0;
228 1.1 cgd }
229 1.1 cgd }
230 1.1 cgd
231 1.5 mycroft /*
232 1.5 mycroft * Set up the logical address filter
233 1.5 mycroft */
234 1.5 mycroft void
235 1.5 mycroft lesetladrf(sc)
236 1.5 mycroft struct le_softc *sc;
237 1.5 mycroft {
238 1.5 mycroft struct lereg2 *ler2 = sc->sc_r2;
239 1.5 mycroft struct ifnet *ifp = &sc->sc_if;
240 1.5 mycroft struct ether_multi *enm;
241 1.5 mycroft register u_char *cp, c;
242 1.5 mycroft register u_long crc;
243 1.5 mycroft register int i, len;
244 1.5 mycroft struct ether_multistep step;
245 1.5 mycroft
246 1.5 mycroft /*
247 1.5 mycroft * Set up multicast address filter by passing all multicast
248 1.5 mycroft * addresses through a crc generator, and then using the high
249 1.5 mycroft * order 6 bits as a index into the 64 bit logical address
250 1.5 mycroft * filter. The high order two bits select the word, while the
251 1.5 mycroft * rest of the bits select the bit within the word.
252 1.5 mycroft */
253 1.5 mycroft
254 1.5 mycroft ler2->ler2_ladrf[0] = 0;
255 1.5 mycroft ler2->ler2_ladrf[1] = 0;
256 1.5 mycroft ifp->if_flags &= ~IFF_ALLMULTI;
257 1.5 mycroft ETHER_FIRST_MULTI(step, &sc->sc_ac, enm);
258 1.5 mycroft while (enm != NULL) {
259 1.5 mycroft if (bcmp((caddr_t)&enm->enm_addrlo,
260 1.5 mycroft (caddr_t)&enm->enm_addrhi, sizeof(enm->enm_addrlo)) != 0) {
261 1.5 mycroft /*
262 1.5 mycroft * We must listen to a range of multicast
263 1.5 mycroft * addresses. For now, just accept all
264 1.5 mycroft * multicasts, rather than trying to set only
265 1.5 mycroft * those filter bits needed to match the range.
266 1.5 mycroft * (At this time, the only use of address
267 1.5 mycroft * ranges is for IP multicast routing, for
268 1.5 mycroft * which the range is big enough to require all
269 1.5 mycroft * bits set.)
270 1.5 mycroft */
271 1.5 mycroft ler2->ler2_ladrf[0] = 0xffffffff;
272 1.5 mycroft ler2->ler2_ladrf[1] = 0xffffffff;
273 1.5 mycroft ifp->if_flags |= IFF_ALLMULTI;
274 1.5 mycroft return;
275 1.5 mycroft }
276 1.5 mycroft
277 1.5 mycroft /*
278 1.5 mycroft * One would think, given the AM7990 document's polynomial
279 1.5 mycroft * of 0x04c11db6, that this should be 0x6db88320 (the bit
280 1.5 mycroft * reversal of the AMD value), but that is not right. See
281 1.5 mycroft * the BASIC listing: bit 0 (our bit 31) must then be set.
282 1.5 mycroft */
283 1.5 mycroft cp = (unsigned char *)&enm->enm_addrlo;
284 1.5 mycroft crc = 0xffffffff;
285 1.5 mycroft for (len = 6; --len >= 0;) {
286 1.5 mycroft c = *cp++;
287 1.5 mycroft for (i = 8; --i >= 0;) {
288 1.5 mycroft if ((c & 0x01) ^ (crc & 0x01)) {
289 1.5 mycroft crc >>= 1;
290 1.5 mycroft crc = crc ^ 0xedb88320;
291 1.5 mycroft } else
292 1.5 mycroft crc >>= 1;
293 1.5 mycroft c >>= 1;
294 1.5 mycroft }
295 1.5 mycroft }
296 1.5 mycroft /* Just want the 6 most significant bits. */
297 1.5 mycroft crc = crc >> 26;
298 1.5 mycroft
299 1.5 mycroft /* Turn on the corresponding bit in the filter. */
300 1.5 mycroft ler2->ler2_ladrf[crc >> 5] |= 1 << (crc & 0x1f);
301 1.5 mycroft
302 1.5 mycroft ETHER_NEXT_MULTI(step, enm);
303 1.5 mycroft }
304 1.5 mycroft }
305 1.5 mycroft
306 1.5 mycroft lereset(sc)
307 1.5 mycroft register struct le_softc *sc;
308 1.1 cgd {
309 1.5 mycroft register struct lereg0 *ler0 = sc->sc_r0;
310 1.5 mycroft register struct lereg1 *ler1 = sc->sc_r1;
311 1.5 mycroft register struct lereg2 *ler2 = sc->sc_r2;
312 1.5 mycroft struct lereg2 *lemem = 0;
313 1.5 mycroft register int timo, stat;
314 1.1 cgd
315 1.1 cgd #if NBPFILTER > 0
316 1.5 mycroft if (sc->sc_if.if_flags & IFF_PROMISC)
317 1.1 cgd /* set the promiscuous bit */
318 1.5 mycroft ler2->ler2_mode = LE_MODE|0x8000;
319 1.1 cgd else
320 1.1 cgd #endif
321 1.5 mycroft ler2->ler2_mode = LE_MODE;
322 1.1 cgd LERDWR(ler0, LE_CSR0, ler1->ler1_rap);
323 1.1 cgd LERDWR(ler0, LE_STOP, ler1->ler1_rdp);
324 1.5 mycroft
325 1.5 mycroft lesetladrf(sc);
326 1.5 mycroft ledrinit(ler2);
327 1.5 mycroft sc->sc_rmd = 0;
328 1.5 mycroft
329 1.1 cgd LERDWR(ler0, LE_CSR1, ler1->ler1_rap);
330 1.1 cgd LERDWR(ler0, (int)&lemem->ler2_mode, ler1->ler1_rdp);
331 1.1 cgd LERDWR(ler0, LE_CSR2, ler1->ler1_rap);
332 1.1 cgd LERDWR(ler0, 0, ler1->ler1_rdp);
333 1.5 mycroft LERDWR(ler0, LE_CSR3, ler1->ler1_rap);
334 1.5 mycroft LERDWR(ler0, LE_BSWP, ler1->ler1_rdp);
335 1.1 cgd LERDWR(ler0, LE_CSR0, ler1->ler1_rap);
336 1.1 cgd LERDWR(ler0, LE_INIT, ler1->ler1_rdp);
337 1.5 mycroft timo = 100000;
338 1.1 cgd do {
339 1.1 cgd if (--timo == 0) {
340 1.5 mycroft printf("le%d: init timeout, stat=0x%x\n",
341 1.5 mycroft sc->sc_if.if_unit, stat);
342 1.1 cgd break;
343 1.1 cgd }
344 1.1 cgd LERDWR(ler0, ler1->ler1_rdp, stat);
345 1.5 mycroft } while ((stat & (LE_IDON | LE_ERR)) == 0);
346 1.5 mycroft if (stat & LE_ERR)
347 1.5 mycroft printf("le%d: init failed, stat=0x%x\n",
348 1.5 mycroft sc->sc_if.if_unit, stat);
349 1.5 mycroft else
350 1.5 mycroft LERDWR(ler0, LE_IDON, ler1->ler1_rdp);
351 1.1 cgd LERDWR(ler0, LE_STRT | LE_INEA, ler1->ler1_rdp);
352 1.5 mycroft sc->sc_if.if_flags &= ~IFF_OACTIVE;
353 1.1 cgd }
354 1.1 cgd
355 1.1 cgd /*
356 1.1 cgd * Initialization of interface
357 1.1 cgd */
358 1.1 cgd leinit(unit)
359 1.1 cgd int unit;
360 1.1 cgd {
361 1.5 mycroft struct le_softc *sc = &le_softc[unit];
362 1.5 mycroft register struct ifnet *ifp = &sc->sc_if;
363 1.1 cgd int s;
364 1.1 cgd
365 1.1 cgd /* not yet, if address still unknown */
366 1.1 cgd if (ifp->if_addrlist == (struct ifaddr *)0)
367 1.1 cgd return;
368 1.1 cgd if ((ifp->if_flags & IFF_RUNNING) == 0) {
369 1.1 cgd s = splimp();
370 1.1 cgd ifp->if_flags |= IFF_RUNNING;
371 1.5 mycroft lereset(sc);
372 1.5 mycroft (void) lestart(ifp);
373 1.1 cgd splx(s);
374 1.1 cgd }
375 1.1 cgd }
376 1.1 cgd
377 1.1 cgd /*
378 1.1 cgd * Start output on interface. Get another datagram to send
379 1.1 cgd * off of the interface queue, and copy it to the interface
380 1.1 cgd * before starting the output.
381 1.1 cgd */
382 1.1 cgd lestart(ifp)
383 1.1 cgd struct ifnet *ifp;
384 1.1 cgd {
385 1.5 mycroft register struct le_softc *sc = &le_softc[ifp->if_unit];
386 1.1 cgd register struct letmd *tmd;
387 1.1 cgd register struct mbuf *m;
388 1.1 cgd int len;
389 1.1 cgd
390 1.5 mycroft if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
391 1.1 cgd return (0);
392 1.5 mycroft IF_DEQUEUE(&sc->sc_if.if_snd, m);
393 1.1 cgd if (m == 0)
394 1.1 cgd return (0);
395 1.5 mycroft len = leput(sc->sc_r2->ler2_tbuf[0], m);
396 1.1 cgd #if NBPFILTER > 0
397 1.1 cgd /*
398 1.1 cgd * If bpf is listening on this interface, let it
399 1.1 cgd * see the packet before we commit it to the wire.
400 1.1 cgd */
401 1.5 mycroft if (sc->sc_bpf)
402 1.5 mycroft bpf_tap(sc->sc_bpf, sc->sc_r2->ler2_tbuf[0], len);
403 1.1 cgd #endif
404 1.5 mycroft tmd = sc->sc_r2->ler2_tmd;
405 1.1 cgd tmd->tmd3 = 0;
406 1.1 cgd tmd->tmd2 = -len;
407 1.1 cgd tmd->tmd1 = LE_OWN | LE_STP | LE_ENP;
408 1.5 mycroft sc->sc_if.if_flags |= IFF_OACTIVE;
409 1.1 cgd return (0);
410 1.1 cgd }
411 1.1 cgd
412 1.1 cgd leintr(unit)
413 1.1 cgd register int unit;
414 1.1 cgd {
415 1.5 mycroft register struct le_softc *sc = &le_softc[unit];
416 1.5 mycroft register struct lereg0 *ler0 = sc->sc_r0;
417 1.1 cgd register struct lereg1 *ler1;
418 1.1 cgd register int stat;
419 1.1 cgd
420 1.1 cgd if ((ler0->ler0_status & LE_IR) == 0)
421 1.1 cgd return(0);
422 1.1 cgd if (ler0->ler0_status & LE_JAB) {
423 1.5 mycroft sc->sc_jab++;
424 1.5 mycroft lereset(sc);
425 1.1 cgd return(1);
426 1.1 cgd }
427 1.5 mycroft
428 1.5 mycroft ler1 = sc->sc_r1;
429 1.1 cgd LERDWR(ler0, ler1->ler1_rdp, stat);
430 1.1 cgd if (stat & LE_SERR) {
431 1.5 mycroft leerror(sc, stat);
432 1.1 cgd if (stat & LE_MERR) {
433 1.5 mycroft sc->sc_merr++;
434 1.5 mycroft lereset(sc);
435 1.1 cgd return(1);
436 1.1 cgd }
437 1.1 cgd if (stat & LE_BABL)
438 1.5 mycroft sc->sc_babl++;
439 1.1 cgd if (stat & LE_CERR)
440 1.5 mycroft sc->sc_cerr++;
441 1.1 cgd if (stat & LE_MISS)
442 1.5 mycroft sc->sc_miss++;
443 1.1 cgd LERDWR(ler0, LE_BABL|LE_CERR|LE_MISS|LE_INEA, ler1->ler1_rdp);
444 1.1 cgd }
445 1.1 cgd if ((stat & LE_RXON) == 0) {
446 1.5 mycroft sc->sc_rxoff++;
447 1.5 mycroft lereset(sc);
448 1.1 cgd return(1);
449 1.1 cgd }
450 1.1 cgd if ((stat & LE_TXON) == 0) {
451 1.5 mycroft sc->sc_txoff++;
452 1.5 mycroft lereset(sc);
453 1.1 cgd return(1);
454 1.1 cgd }
455 1.1 cgd if (stat & LE_RINT) {
456 1.1 cgd /* interrupt is cleared in lerint */
457 1.5 mycroft lerint(sc);
458 1.1 cgd }
459 1.1 cgd if (stat & LE_TINT) {
460 1.1 cgd LERDWR(ler0, LE_TINT|LE_INEA, ler1->ler1_rdp);
461 1.5 mycroft lexint(sc);
462 1.1 cgd }
463 1.1 cgd return(1);
464 1.1 cgd }
465 1.1 cgd
466 1.1 cgd /*
467 1.1 cgd * Ethernet interface transmitter interrupt.
468 1.1 cgd * Start another output if more data to send.
469 1.1 cgd */
470 1.5 mycroft lexint(sc)
471 1.5 mycroft register struct le_softc *sc;
472 1.1 cgd {
473 1.5 mycroft register struct letmd *tmd = sc->sc_r2->ler2_tmd;
474 1.1 cgd
475 1.5 mycroft if ((sc->sc_if.if_flags & IFF_OACTIVE) == 0) {
476 1.5 mycroft sc->sc_xint++;
477 1.1 cgd return;
478 1.1 cgd }
479 1.1 cgd if (tmd->tmd1 & LE_OWN) {
480 1.5 mycroft sc->sc_xown++;
481 1.1 cgd return;
482 1.1 cgd }
483 1.1 cgd if (tmd->tmd1 & LE_ERR) {
484 1.1 cgd err:
485 1.5 mycroft lexerror(sc);
486 1.5 mycroft sc->sc_if.if_oerrors++;
487 1.1 cgd if (tmd->tmd3 & (LE_TBUFF|LE_UFLO)) {
488 1.5 mycroft sc->sc_uflo++;
489 1.5 mycroft lereset(sc);
490 1.5 mycroft } else if (tmd->tmd3 & LE_LCOL)
491 1.5 mycroft sc->sc_if.if_collisions++;
492 1.1 cgd else if (tmd->tmd3 & LE_RTRY)
493 1.5 mycroft sc->sc_if.if_collisions += 16;
494 1.1 cgd }
495 1.1 cgd else if (tmd->tmd3 & LE_TBUFF)
496 1.1 cgd /* XXX documentation says BUFF not included in ERR */
497 1.1 cgd goto err;
498 1.1 cgd else if (tmd->tmd1 & LE_ONE)
499 1.5 mycroft sc->sc_if.if_collisions++;
500 1.1 cgd else if (tmd->tmd1 & LE_MORE)
501 1.1 cgd /* what is the real number? */
502 1.5 mycroft sc->sc_if.if_collisions += 2;
503 1.1 cgd else
504 1.5 mycroft sc->sc_if.if_opackets++;
505 1.5 mycroft sc->sc_if.if_flags &= ~IFF_OACTIVE;
506 1.5 mycroft (void) lestart(&sc->sc_if);
507 1.1 cgd }
508 1.1 cgd
509 1.1 cgd #define LENEXTRMP \
510 1.5 mycroft if (++bix == LERBUF) bix = 0, rmd = sc->sc_r2->ler2_rmd; else ++rmd
511 1.1 cgd
512 1.1 cgd /*
513 1.1 cgd * Ethernet interface receiver interrupt.
514 1.1 cgd * If input error just drop packet.
515 1.1 cgd * Decapsulate packet based on type and pass to type specific
516 1.1 cgd * higher-level input routine.
517 1.1 cgd */
518 1.5 mycroft lerint(sc)
519 1.5 mycroft register struct le_softc *sc;
520 1.1 cgd {
521 1.5 mycroft register int bix = sc->sc_rmd;
522 1.5 mycroft register struct lermd *rmd = &sc->sc_r2->ler2_rmd[bix];
523 1.1 cgd
524 1.1 cgd /*
525 1.1 cgd * Out of sync with hardware, should never happen?
526 1.1 cgd */
527 1.1 cgd if (rmd->rmd1 & LE_OWN) {
528 1.5 mycroft do {
529 1.5 mycroft LENEXTRMP;
530 1.5 mycroft } while ((rmd->rmd1 & LE_OWN) && bix != sc->sc_rmd);
531 1.5 mycroft if (bix == sc->sc_rmd) {
532 1.5 mycroft printf("le%d: rint with no buffer\n",
533 1.5 mycroft sc->sc_if.if_unit);
534 1.5 mycroft LERDWR(sc->sc_r0, LE_RINT|LE_INEA, sc->sc_r1->ler1_rdp);
535 1.5 mycroft return;
536 1.5 mycroft }
537 1.1 cgd }
538 1.1 cgd
539 1.1 cgd /*
540 1.1 cgd * Process all buffers with valid data
541 1.1 cgd */
542 1.1 cgd while ((rmd->rmd1 & LE_OWN) == 0) {
543 1.1 cgd int len = rmd->rmd3;
544 1.1 cgd
545 1.1 cgd /* Clear interrupt to avoid race condition */
546 1.5 mycroft LERDWR(sc->sc_r0, LE_RINT|LE_INEA, sc->sc_r1->ler1_rdp);
547 1.1 cgd
548 1.1 cgd if (rmd->rmd1 & LE_ERR) {
549 1.5 mycroft sc->sc_rmd = bix;
550 1.5 mycroft lererror(sc, "bad packet");
551 1.5 mycroft sc->sc_if.if_ierrors++;
552 1.1 cgd } else if ((rmd->rmd1 & (LE_STP|LE_ENP)) != (LE_STP|LE_ENP)) {
553 1.1 cgd /*
554 1.1 cgd * Find the end of the packet so we can see how long
555 1.1 cgd * it was. We still throw it away.
556 1.1 cgd */
557 1.1 cgd do {
558 1.5 mycroft LERDWR(sc->sc_r0, LE_RINT|LE_INEA,
559 1.5 mycroft sc->sc_r1->ler1_rdp);
560 1.1 cgd rmd->rmd3 = 0;
561 1.1 cgd rmd->rmd1 = LE_OWN;
562 1.1 cgd LENEXTRMP;
563 1.1 cgd } while (!(rmd->rmd1 & (LE_OWN|LE_ERR|LE_STP|LE_ENP)));
564 1.5 mycroft sc->sc_rmd = bix;
565 1.5 mycroft lererror(sc, "chained buffer");
566 1.5 mycroft sc->sc_rxlen++;
567 1.1 cgd /*
568 1.1 cgd * If search terminated without successful completion
569 1.1 cgd * we reset the hardware (conservative).
570 1.1 cgd */
571 1.1 cgd if ((rmd->rmd1 & (LE_OWN|LE_ERR|LE_STP|LE_ENP)) !=
572 1.1 cgd LE_ENP) {
573 1.5 mycroft lereset(sc);
574 1.1 cgd return;
575 1.1 cgd }
576 1.1 cgd } else
577 1.5 mycroft leread(sc, sc->sc_r2->ler2_rbuf[bix], len);
578 1.1 cgd rmd->rmd3 = 0;
579 1.1 cgd rmd->rmd1 = LE_OWN;
580 1.1 cgd LENEXTRMP;
581 1.1 cgd }
582 1.5 mycroft sc->sc_rmd = bix;
583 1.1 cgd }
584 1.1 cgd
585 1.5 mycroft leread(sc, buf, len)
586 1.5 mycroft register struct le_softc *sc;
587 1.1 cgd char *buf;
588 1.1 cgd int len;
589 1.1 cgd {
590 1.1 cgd register struct ether_header *et;
591 1.5 mycroft register struct ifnet *ifp = &sc->sc_if;
592 1.1 cgd struct mbuf *m;
593 1.1 cgd
594 1.5 mycroft ifp->if_ipackets++;
595 1.1 cgd et = (struct ether_header *)buf;
596 1.1 cgd /* adjust input length to account for header and CRC */
597 1.5 mycroft len -= sizeof(struct ether_header) + 4;
598 1.1 cgd
599 1.1 cgd if (len <= 0) {
600 1.1 cgd if (ledebug)
601 1.1 cgd log(LOG_WARNING,
602 1.1 cgd "le%d: ierror(runt packet): from %s: len=%d\n",
603 1.5 mycroft sc->sc_if.if_unit, ether_sprintf(et->ether_shost),
604 1.5 mycroft len);
605 1.5 mycroft sc->sc_runt++;
606 1.5 mycroft ifp->if_ierrors++;
607 1.1 cgd return;
608 1.1 cgd }
609 1.5 mycroft
610 1.1 cgd #if NBPFILTER > 0
611 1.1 cgd /*
612 1.1 cgd * Check if there's a bpf filter listening on this interface.
613 1.5 mycroft * If so, hand off the raw packet to bpf, then discard things
614 1.5 mycroft * not destined for us (but be sure to keep broadcast/multicast).
615 1.1 cgd */
616 1.5 mycroft if (sc->sc_bpf) {
617 1.5 mycroft bpf_tap(sc->sc_bpf, buf, len + sizeof(struct ether_header));
618 1.5 mycroft if ((ifp->if_flags & IFF_PROMISC) &&
619 1.5 mycroft (et->ether_dhost[0] & 1) == 0 &&
620 1.5 mycroft bcmp(et->ether_dhost, sc->sc_addr,
621 1.5 mycroft sizeof(et->ether_dhost)) != 0 &&
622 1.5 mycroft bcmp(et->ether_dhost, etherbroadcastaddr,
623 1.1 cgd sizeof(et->ether_dhost)) != 0)
624 1.1 cgd return;
625 1.1 cgd }
626 1.1 cgd #endif
627 1.5 mycroft
628 1.5 mycroft m = leget(buf, len, 0, ifp);
629 1.1 cgd if (m == 0)
630 1.1 cgd return;
631 1.5 mycroft
632 1.5 mycroft ether_input(ifp, et, m);
633 1.1 cgd }
634 1.1 cgd
635 1.1 cgd /*
636 1.1 cgd * Routine to copy from mbuf chain to transmit
637 1.1 cgd * buffer in board local memory.
638 1.1 cgd */
639 1.1 cgd leput(lebuf, m)
640 1.1 cgd register char *lebuf;
641 1.1 cgd register struct mbuf *m;
642 1.1 cgd {
643 1.1 cgd register struct mbuf *mp;
644 1.1 cgd register int len, tlen = 0;
645 1.1 cgd
646 1.1 cgd for (mp = m; mp; mp = mp->m_next) {
647 1.1 cgd len = mp->m_len;
648 1.1 cgd if (len == 0)
649 1.1 cgd continue;
650 1.1 cgd tlen += len;
651 1.1 cgd bcopy(mtod(mp, char *), lebuf, len);
652 1.1 cgd lebuf += len;
653 1.1 cgd }
654 1.1 cgd m_freem(m);
655 1.1 cgd if (tlen < LEMINSIZE) {
656 1.1 cgd bzero(lebuf, LEMINSIZE - tlen);
657 1.1 cgd tlen = LEMINSIZE;
658 1.1 cgd }
659 1.1 cgd return(tlen);
660 1.1 cgd }
661 1.1 cgd
662 1.1 cgd /*
663 1.1 cgd * Routine to copy from board local memory into mbufs.
664 1.1 cgd */
665 1.1 cgd struct mbuf *
666 1.1 cgd leget(lebuf, totlen, off0, ifp)
667 1.1 cgd char *lebuf;
668 1.1 cgd int totlen, off0;
669 1.1 cgd struct ifnet *ifp;
670 1.1 cgd {
671 1.1 cgd register struct mbuf *m;
672 1.1 cgd struct mbuf *top = 0, **mp = ⊤
673 1.1 cgd register int off = off0, len;
674 1.1 cgd register char *cp;
675 1.1 cgd char *epkt;
676 1.1 cgd
677 1.1 cgd lebuf += sizeof (struct ether_header);
678 1.1 cgd cp = lebuf;
679 1.1 cgd epkt = cp + totlen;
680 1.1 cgd if (off) {
681 1.1 cgd cp += off + 2 * sizeof(u_short);
682 1.1 cgd totlen -= 2 * sizeof(u_short);
683 1.1 cgd }
684 1.1 cgd
685 1.1 cgd MGETHDR(m, M_DONTWAIT, MT_DATA);
686 1.1 cgd if (m == 0)
687 1.1 cgd return (0);
688 1.1 cgd m->m_pkthdr.rcvif = ifp;
689 1.1 cgd m->m_pkthdr.len = totlen;
690 1.1 cgd m->m_len = MHLEN;
691 1.1 cgd
692 1.1 cgd while (totlen > 0) {
693 1.1 cgd if (top) {
694 1.1 cgd MGET(m, M_DONTWAIT, MT_DATA);
695 1.1 cgd if (m == 0) {
696 1.1 cgd m_freem(top);
697 1.1 cgd return (0);
698 1.1 cgd }
699 1.1 cgd m->m_len = MLEN;
700 1.1 cgd }
701 1.1 cgd len = min(totlen, epkt - cp);
702 1.1 cgd if (len >= MINCLSIZE) {
703 1.1 cgd MCLGET(m, M_DONTWAIT);
704 1.1 cgd if (m->m_flags & M_EXT)
705 1.1 cgd m->m_len = len = min(len, MCLBYTES);
706 1.1 cgd else
707 1.1 cgd len = m->m_len;
708 1.1 cgd } else {
709 1.1 cgd /*
710 1.1 cgd * Place initial small packet/header at end of mbuf.
711 1.1 cgd */
712 1.1 cgd if (len < m->m_len) {
713 1.1 cgd if (top == 0 && len + max_linkhdr <= m->m_len)
714 1.1 cgd m->m_data += max_linkhdr;
715 1.1 cgd m->m_len = len;
716 1.1 cgd } else
717 1.1 cgd len = m->m_len;
718 1.1 cgd }
719 1.1 cgd bcopy(cp, mtod(m, caddr_t), (unsigned)len);
720 1.1 cgd cp += len;
721 1.1 cgd *mp = m;
722 1.1 cgd mp = &m->m_next;
723 1.1 cgd totlen -= len;
724 1.1 cgd if (cp == epkt)
725 1.1 cgd cp = lebuf;
726 1.1 cgd }
727 1.1 cgd return (top);
728 1.1 cgd }
729 1.1 cgd
730 1.1 cgd /*
731 1.1 cgd * Process an ioctl request.
732 1.1 cgd */
733 1.1 cgd leioctl(ifp, cmd, data)
734 1.1 cgd register struct ifnet *ifp;
735 1.1 cgd int cmd;
736 1.1 cgd caddr_t data;
737 1.1 cgd {
738 1.5 mycroft register struct ifaddr *ifa;
739 1.5 mycroft struct le_softc *sc = &le_softc[ifp->if_unit];
740 1.5 mycroft struct lereg1 *ler1;
741 1.1 cgd int s = splimp(), error = 0;
742 1.1 cgd
743 1.1 cgd switch (cmd) {
744 1.1 cgd
745 1.1 cgd case SIOCSIFADDR:
746 1.5 mycroft ifa = (struct ifaddr *)data;
747 1.1 cgd ifp->if_flags |= IFF_UP;
748 1.1 cgd switch (ifa->ifa_addr->sa_family) {
749 1.1 cgd #ifdef INET
750 1.1 cgd case AF_INET:
751 1.1 cgd leinit(ifp->if_unit); /* before arpwhohas */
752 1.1 cgd ((struct arpcom *)ifp)->ac_ipaddr =
753 1.1 cgd IA_SIN(ifa)->sin_addr;
754 1.1 cgd arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
755 1.1 cgd break;
756 1.1 cgd #endif
757 1.1 cgd #ifdef NS
758 1.1 cgd case AF_NS:
759 1.1 cgd {
760 1.1 cgd register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
761 1.1 cgd
762 1.1 cgd if (ns_nullhost(*ina))
763 1.5 mycroft ina->x_host = *(union ns_host *)(sc->sc_addr);
764 1.1 cgd else {
765 1.1 cgd /*
766 1.1 cgd * The manual says we can't change the address
767 1.1 cgd * while the receiver is armed,
768 1.1 cgd * so reset everything
769 1.1 cgd */
770 1.1 cgd ifp->if_flags &= ~IFF_RUNNING;
771 1.1 cgd bcopy((caddr_t)ina->x_host.c_host,
772 1.5 mycroft (caddr_t)sc->sc_addr, sizeof(sc->sc_addr));
773 1.1 cgd }
774 1.1 cgd leinit(ifp->if_unit); /* does le_setaddr() */
775 1.1 cgd break;
776 1.1 cgd }
777 1.1 cgd #endif
778 1.1 cgd default:
779 1.1 cgd leinit(ifp->if_unit);
780 1.1 cgd break;
781 1.1 cgd }
782 1.1 cgd break;
783 1.1 cgd
784 1.1 cgd case SIOCSIFFLAGS:
785 1.5 mycroft ler1 = sc->sc_r1;
786 1.1 cgd if ((ifp->if_flags & IFF_UP) == 0 &&
787 1.1 cgd ifp->if_flags & IFF_RUNNING) {
788 1.5 mycroft LERDWR(sc->sc_r0, LE_STOP, ler1->ler1_rdp);
789 1.1 cgd ifp->if_flags &= ~IFF_RUNNING;
790 1.1 cgd } else if (ifp->if_flags & IFF_UP &&
791 1.1 cgd (ifp->if_flags & IFF_RUNNING) == 0)
792 1.1 cgd leinit(ifp->if_unit);
793 1.1 cgd /*
794 1.1 cgd * If the state of the promiscuous bit changes, the interface
795 1.1 cgd * must be reset to effect the change.
796 1.1 cgd */
797 1.5 mycroft if (((ifp->if_flags ^ sc->sc_iflags) & IFF_PROMISC) &&
798 1.1 cgd (ifp->if_flags & IFF_RUNNING)) {
799 1.5 mycroft sc->sc_iflags = ifp->if_flags;
800 1.5 mycroft lereset(sc);
801 1.1 cgd lestart(ifp);
802 1.1 cgd }
803 1.1 cgd break;
804 1.1 cgd
805 1.5 mycroft case SIOCADDMULTI:
806 1.5 mycroft error = ether_addmulti((struct ifreq *)data, &sc->sc_ac);
807 1.5 mycroft goto update_multicast;
808 1.5 mycroft
809 1.5 mycroft case SIOCDELMULTI:
810 1.5 mycroft error = ether_delmulti((struct ifreq *)data, &sc->sc_ac);
811 1.5 mycroft update_multicast:
812 1.5 mycroft if (error == ENETRESET) {
813 1.5 mycroft /*
814 1.5 mycroft * Multicast list has changed; set the hardware
815 1.5 mycroft * filter accordingly.
816 1.5 mycroft */
817 1.5 mycroft lereset(sc);
818 1.5 mycroft error = 0;
819 1.5 mycroft }
820 1.5 mycroft break;
821 1.5 mycroft
822 1.1 cgd default:
823 1.1 cgd error = EINVAL;
824 1.1 cgd }
825 1.1 cgd splx(s);
826 1.1 cgd return (error);
827 1.1 cgd }
828 1.1 cgd
829 1.5 mycroft leerror(sc, stat)
830 1.5 mycroft register struct le_softc *sc;
831 1.1 cgd int stat;
832 1.1 cgd {
833 1.1 cgd if (!ledebug)
834 1.1 cgd return;
835 1.1 cgd
836 1.1 cgd /*
837 1.1 cgd * Not all transceivers implement heartbeat
838 1.1 cgd * so we only log CERR once.
839 1.1 cgd */
840 1.5 mycroft if ((stat & LE_CERR) && sc->sc_cerr)
841 1.1 cgd return;
842 1.1 cgd log(LOG_WARNING,
843 1.5 mycroft "le%d: error: stat=%b\n", sc->sc_if.if_unit, stat,
844 1.1 cgd "\20\20ERR\17BABL\16CERR\15MISS\14MERR\13RINT\12TINT\11IDON\10INTR\07INEA\06RXON\05TXON\04TDMD\03STOP\02STRT\01INIT");
845 1.1 cgd }
846 1.1 cgd
847 1.5 mycroft lererror(sc, msg)
848 1.5 mycroft register struct le_softc *sc;
849 1.1 cgd char *msg;
850 1.1 cgd {
851 1.1 cgd register struct lermd *rmd;
852 1.1 cgd int len;
853 1.1 cgd
854 1.1 cgd if (!ledebug)
855 1.1 cgd return;
856 1.1 cgd
857 1.5 mycroft rmd = &sc->sc_r2->ler2_rmd[sc->sc_rmd];
858 1.1 cgd len = rmd->rmd3;
859 1.1 cgd log(LOG_WARNING,
860 1.1 cgd "le%d: ierror(%s): from %s: buf=%d, len=%d, rmd1=%b\n",
861 1.5 mycroft sc->sc_if.if_unit, msg,
862 1.5 mycroft len > 11 ? ether_sprintf(&sc->sc_r2->ler2_rbuf[sc->sc_rmd][6]) : "unknown",
863 1.5 mycroft sc->sc_rmd, len, rmd->rmd1,
864 1.1 cgd "\20\20OWN\17ERR\16FRAM\15OFLO\14CRC\13RBUF\12STP\11ENP");
865 1.1 cgd }
866 1.1 cgd
867 1.5 mycroft lexerror(sc)
868 1.5 mycroft register struct le_softc *sc;
869 1.1 cgd {
870 1.1 cgd register struct letmd *tmd;
871 1.5 mycroft register int len;
872 1.1 cgd
873 1.1 cgd if (!ledebug)
874 1.1 cgd return;
875 1.1 cgd
876 1.5 mycroft tmd = sc->sc_r2->ler2_tmd;
877 1.1 cgd len = -tmd->tmd2;
878 1.1 cgd log(LOG_WARNING,
879 1.1 cgd "le%d: oerror: to %s: buf=%d, len=%d, tmd1=%b, tmd3=%b\n",
880 1.5 mycroft sc->sc_if.if_unit,
881 1.5 mycroft len > 5 ? ether_sprintf(&sc->sc_r2->ler2_tbuf[0][0]) : "unknown",
882 1.5 mycroft 0, len, tmd->tmd1,
883 1.1 cgd "\20\20OWN\17ERR\16RES\15MORE\14ONE\13DEF\12STP\11ENP",
884 1.1 cgd tmd->tmd3,
885 1.1 cgd "\20\20BUFF\17UFLO\16RES\15LCOL\14LCAR\13RTRY");
886 1.1 cgd }
887 1.1 cgd #endif
888