am7990.c revision 1.29 1 1.29 veego /* $NetBSD: am7990.c,v 1.29 1997/03/27 21:01:47 veego Exp $ */
2 1.1 cgd
3 1.1 cgd /*-
4 1.27 thorpej * Copyright (c) 1997 Jason R. Thorpe. All rights reserved.
5 1.1 cgd * Copyright (c) 1995 Charles M. Hannum. All rights reserved.
6 1.1 cgd * Copyright (c) 1992, 1993
7 1.1 cgd * The Regents of the University of California. All rights reserved.
8 1.1 cgd *
9 1.1 cgd * This code is derived from software contributed to Berkeley by
10 1.1 cgd * Ralph Campbell and Rick Macklem.
11 1.1 cgd *
12 1.1 cgd * Redistribution and use in source and binary forms, with or without
13 1.1 cgd * modification, are permitted provided that the following conditions
14 1.1 cgd * are met:
15 1.1 cgd * 1. Redistributions of source code must retain the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer.
17 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 cgd * notice, this list of conditions and the following disclaimer in the
19 1.1 cgd * documentation and/or other materials provided with the distribution.
20 1.1 cgd * 3. All advertising materials mentioning features or use of this software
21 1.1 cgd * must display the following acknowledgement:
22 1.1 cgd * This product includes software developed by the University of
23 1.1 cgd * California, Berkeley and its contributors.
24 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.1 cgd * may be used to endorse or promote products derived from this software
26 1.1 cgd * without specific prior written permission.
27 1.1 cgd *
28 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 cgd * SUCH DAMAGE.
39 1.1 cgd *
40 1.1 cgd * @(#)if_le.c 8.2 (Berkeley) 11/16/93
41 1.1 cgd */
42 1.1 cgd
43 1.19 thorpej #include "bpfilter.h"
44 1.19 thorpej
45 1.19 thorpej #include <sys/param.h>
46 1.19 thorpej #include <sys/systm.h>
47 1.19 thorpej #include <sys/mbuf.h>
48 1.19 thorpej #include <sys/syslog.h>
49 1.19 thorpej #include <sys/socket.h>
50 1.19 thorpej #include <sys/device.h>
51 1.19 thorpej #include <sys/malloc.h>
52 1.1 cgd #include <sys/ioctl.h>
53 1.1 cgd #include <sys/errno.h>
54 1.1 cgd
55 1.19 thorpej #include <net/if.h>
56 1.26 is #include <net/if_dl.h>
57 1.26 is #include <net/if_ether.h>
58 1.27 thorpej #include <net/if_media.h>
59 1.19 thorpej
60 1.1 cgd #ifdef INET
61 1.19 thorpej #include <netinet/in.h>
62 1.26 is #include <netinet/if_inarp.h>
63 1.1 cgd #include <netinet/in_systm.h>
64 1.1 cgd #include <netinet/in_var.h>
65 1.1 cgd #include <netinet/ip.h>
66 1.1 cgd #endif
67 1.1 cgd
68 1.1 cgd #ifdef NS
69 1.1 cgd #include <netns/ns.h>
70 1.1 cgd #include <netns/ns_if.h>
71 1.1 cgd #endif
72 1.1 cgd
73 1.1 cgd #if defined(CCITT) && defined(LLC)
74 1.1 cgd #include <sys/socketvar.h>
75 1.1 cgd #include <netccitt/x25.h>
76 1.11 christos #include <netccitt/pk.h>
77 1.11 christos #include <netccitt/pk_var.h>
78 1.11 christos #include <netccitt/pk_extern.h>
79 1.1 cgd #endif
80 1.1 cgd
81 1.1 cgd #if NBPFILTER > 0
82 1.1 cgd #include <net/bpf.h>
83 1.1 cgd #include <net/bpfdesc.h>
84 1.1 cgd #endif
85 1.1 cgd
86 1.19 thorpej #include <dev/ic/am7990reg.h>
87 1.19 thorpej #include <dev/ic/am7990var.h>
88 1.19 thorpej
89 1.1 cgd #ifdef LEDEBUG
90 1.19 thorpej void am7990_recv_print __P((struct am7990_softc *, int));
91 1.19 thorpej void am7990_xmit_print __P((struct am7990_softc *, int));
92 1.1 cgd #endif
93 1.1 cgd
94 1.19 thorpej integrate void am7990_rint __P((struct am7990_softc *));
95 1.19 thorpej integrate void am7990_tint __P((struct am7990_softc *));
96 1.19 thorpej
97 1.19 thorpej integrate int am7990_put __P((struct am7990_softc *, int, struct mbuf *));
98 1.19 thorpej integrate struct mbuf *am7990_get __P((struct am7990_softc *, int, int));
99 1.19 thorpej integrate void am7990_read __P((struct am7990_softc *, int, int));
100 1.19 thorpej
101 1.19 thorpej hide void am7990_shutdown __P((void *));
102 1.19 thorpej
103 1.27 thorpej int am7990_mediachange __P((struct ifnet *));
104 1.27 thorpej void am7990_mediastatus __P((struct ifnet *, struct ifmediareq *));
105 1.27 thorpej
106 1.26 is #define ifp (&sc->sc_ethercom.ec_if)
107 1.7 mycroft
108 1.19 thorpej static inline u_int16_t ether_cmp __P((void *, void *));
109 1.19 thorpej
110 1.19 thorpej /*
111 1.19 thorpej * Compare two Ether/802 addresses for equality, inlined and
112 1.19 thorpej * unrolled for speed. I'd love to have an inline assembler
113 1.19 thorpej * version of this... XXX: Who wanted that? mycroft?
114 1.19 thorpej * I wrote one, but the following is just as efficient.
115 1.19 thorpej * This expands to 10 short m68k instructions! -gwr
116 1.19 thorpej * Note: use this like bcmp()
117 1.19 thorpej */
118 1.19 thorpej static inline u_short
119 1.19 thorpej ether_cmp(one, two)
120 1.19 thorpej void *one, *two;
121 1.19 thorpej {
122 1.19 thorpej register u_int16_t *a = (u_short *) one;
123 1.19 thorpej register u_int16_t *b = (u_short *) two;
124 1.19 thorpej register u_int16_t diff;
125 1.19 thorpej
126 1.27 thorpej diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
127 1.19 thorpej
128 1.19 thorpej return (diff);
129 1.19 thorpej }
130 1.19 thorpej
131 1.19 thorpej #define ETHER_CMP ether_cmp
132 1.13 gwr
133 1.19 thorpej /*
134 1.19 thorpej * am7990 configuration driver. Attachments are provided by
135 1.19 thorpej * machine-dependent driver front-ends.
136 1.19 thorpej */
137 1.19 thorpej struct cfdriver le_cd = {
138 1.19 thorpej NULL, "le", DV_IFNET
139 1.19 thorpej };
140 1.19 thorpej
141 1.1 cgd void
142 1.19 thorpej am7990_config(sc)
143 1.19 thorpej struct am7990_softc *sc;
144 1.1 cgd {
145 1.25 leo int mem, i;
146 1.1 cgd
147 1.1 cgd /* Make sure the chip is stopped. */
148 1.19 thorpej am7990_stop(sc);
149 1.1 cgd
150 1.1 cgd /* Initialize ifnet structure. */
151 1.19 thorpej bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
152 1.19 thorpej ifp->if_softc = sc;
153 1.19 thorpej ifp->if_start = am7990_start;
154 1.19 thorpej ifp->if_ioctl = am7990_ioctl;
155 1.19 thorpej ifp->if_watchdog = am7990_watchdog;
156 1.1 cgd ifp->if_flags =
157 1.1 cgd IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
158 1.5 mycroft #ifdef LANCE_REVC_BUG
159 1.5 mycroft ifp->if_flags &= ~IFF_MULTICAST;
160 1.5 mycroft #endif
161 1.1 cgd
162 1.27 thorpej /* Initialize ifmedia structures. */
163 1.27 thorpej ifmedia_init(&sc->sc_media, 0, am7990_mediachange, am7990_mediastatus);
164 1.27 thorpej if (sc->sc_supmedia != NULL) {
165 1.27 thorpej for (i = 0; i < sc->sc_nsupmedia; i++)
166 1.27 thorpej ifmedia_add(&sc->sc_media, sc->sc_supmedia[i],
167 1.27 thorpej 0, NULL);
168 1.27 thorpej ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
169 1.27 thorpej } else {
170 1.27 thorpej ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
171 1.27 thorpej ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
172 1.27 thorpej }
173 1.27 thorpej
174 1.1 cgd /* Attach the interface. */
175 1.1 cgd if_attach(ifp);
176 1.26 is ether_ifattach(ifp, sc->sc_enaddr);
177 1.1 cgd
178 1.1 cgd #if NBPFILTER > 0
179 1.1 cgd bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
180 1.1 cgd #endif
181 1.1 cgd
182 1.1 cgd switch (sc->sc_memsize) {
183 1.1 cgd case 8192:
184 1.1 cgd sc->sc_nrbuf = 4;
185 1.1 cgd sc->sc_ntbuf = 1;
186 1.1 cgd break;
187 1.1 cgd case 16384:
188 1.1 cgd sc->sc_nrbuf = 8;
189 1.1 cgd sc->sc_ntbuf = 2;
190 1.1 cgd break;
191 1.1 cgd case 32768:
192 1.1 cgd sc->sc_nrbuf = 16;
193 1.1 cgd sc->sc_ntbuf = 4;
194 1.1 cgd break;
195 1.1 cgd case 65536:
196 1.1 cgd sc->sc_nrbuf = 32;
197 1.1 cgd sc->sc_ntbuf = 8;
198 1.24 pk break;
199 1.24 pk case 131072:
200 1.24 pk sc->sc_nrbuf = 64;
201 1.24 pk sc->sc_ntbuf = 16;
202 1.1 cgd break;
203 1.1 cgd default:
204 1.19 thorpej panic("am7990_config: weird memory size");
205 1.1 cgd }
206 1.1 cgd
207 1.26 is printf(": address %s\n", ether_sprintf(sc->sc_enaddr));
208 1.22 christos printf("%s: %d receive buffers, %d transmit buffers\n",
209 1.5 mycroft sc->sc_dev.dv_xname, sc->sc_nrbuf, sc->sc_ntbuf);
210 1.1 cgd
211 1.19 thorpej sc->sc_sh = shutdownhook_establish(am7990_shutdown, sc);
212 1.19 thorpej if (sc->sc_sh == NULL)
213 1.19 thorpej panic("am7990_config: can't establish shutdownhook");
214 1.25 leo sc->sc_rbufaddr = malloc(sc->sc_nrbuf * sizeof(int), M_DEVBUF,
215 1.25 leo M_WAITOK);
216 1.25 leo sc->sc_tbufaddr = malloc(sc->sc_ntbuf * sizeof(int), M_DEVBUF,
217 1.25 leo M_WAITOK);
218 1.19 thorpej
219 1.1 cgd mem = 0;
220 1.1 cgd sc->sc_initaddr = mem;
221 1.1 cgd mem += sizeof(struct leinit);
222 1.1 cgd sc->sc_rmdaddr = mem;
223 1.1 cgd mem += sizeof(struct lermd) * sc->sc_nrbuf;
224 1.1 cgd sc->sc_tmdaddr = mem;
225 1.1 cgd mem += sizeof(struct letmd) * sc->sc_ntbuf;
226 1.25 leo for (i = 0; i < sc->sc_nrbuf; i++, mem += LEBLEN)
227 1.25 leo sc->sc_rbufaddr[i] = mem;
228 1.25 leo for (i = 0; i < sc->sc_ntbuf; i++, mem += LEBLEN)
229 1.25 leo sc->sc_tbufaddr[i] = mem;
230 1.1 cgd #ifdef notyet
231 1.1 cgd if (mem > ...)
232 1.1 cgd panic(...);
233 1.1 cgd #endif
234 1.1 cgd }
235 1.1 cgd
236 1.1 cgd void
237 1.19 thorpej am7990_reset(sc)
238 1.19 thorpej struct am7990_softc *sc;
239 1.1 cgd {
240 1.2 mycroft int s;
241 1.1 cgd
242 1.2 mycroft s = splimp();
243 1.19 thorpej am7990_init(sc);
244 1.2 mycroft splx(s);
245 1.1 cgd }
246 1.1 cgd
247 1.1 cgd /*
248 1.1 cgd * Set up the initialization block and the descriptor rings.
249 1.1 cgd */
250 1.1 cgd void
251 1.19 thorpej am7990_meminit(sc)
252 1.19 thorpej register struct am7990_softc *sc;
253 1.1 cgd {
254 1.1 cgd u_long a;
255 1.1 cgd int bix;
256 1.1 cgd struct leinit init;
257 1.1 cgd struct lermd rmd;
258 1.1 cgd struct letmd tmd;
259 1.28 is u_int8_t *myaddr;
260 1.1 cgd
261 1.1 cgd #if NBPFILTER > 0
262 1.1 cgd if (ifp->if_flags & IFF_PROMISC)
263 1.1 cgd init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM;
264 1.1 cgd else
265 1.1 cgd #endif
266 1.1 cgd init.init_mode = LE_MODE_NORMAL;
267 1.29 veego if (sc->sc_initmodemedia == 1)
268 1.29 veego init.init_mode |= LE_MODE_PSEL0;
269 1.28 is
270 1.28 is myaddr = LLADDR(ifp->if_sadl);
271 1.28 is init.init_padr[0] = (myaddr[1] << 8) | myaddr[0];
272 1.28 is init.init_padr[1] = (myaddr[3] << 8) | myaddr[2];
273 1.28 is init.init_padr[2] = (myaddr[5] << 8) | myaddr[4];
274 1.26 is am7990_setladrf(&sc->sc_ethercom, init.init_ladrf);
275 1.1 cgd
276 1.1 cgd sc->sc_last_rd = 0;
277 1.1 cgd sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
278 1.1 cgd
279 1.1 cgd a = sc->sc_addr + LE_RMDADDR(sc, 0);
280 1.1 cgd init.init_rdra = a;
281 1.1 cgd init.init_rlen = (a >> 16) | ((ffs(sc->sc_nrbuf) - 1) << 13);
282 1.1 cgd
283 1.1 cgd a = sc->sc_addr + LE_TMDADDR(sc, 0);
284 1.1 cgd init.init_tdra = a;
285 1.1 cgd init.init_tlen = (a >> 16) | ((ffs(sc->sc_ntbuf) - 1) << 13);
286 1.1 cgd
287 1.1 cgd (*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
288 1.1 cgd
289 1.1 cgd /*
290 1.1 cgd * Set up receive ring descriptors.
291 1.1 cgd */
292 1.1 cgd for (bix = 0; bix < sc->sc_nrbuf; bix++) {
293 1.1 cgd a = sc->sc_addr + LE_RBUFADDR(sc, bix);
294 1.1 cgd rmd.rmd0 = a;
295 1.1 cgd rmd.rmd1_hadr = a >> 16;
296 1.1 cgd rmd.rmd1_bits = LE_R1_OWN;
297 1.1 cgd rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
298 1.1 cgd rmd.rmd3 = 0;
299 1.1 cgd (*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
300 1.1 cgd sizeof(rmd));
301 1.1 cgd }
302 1.1 cgd
303 1.1 cgd /*
304 1.1 cgd * Set up transmit ring descriptors.
305 1.1 cgd */
306 1.1 cgd for (bix = 0; bix < sc->sc_ntbuf; bix++) {
307 1.1 cgd a = sc->sc_addr + LE_TBUFADDR(sc, bix);
308 1.1 cgd tmd.tmd0 = a;
309 1.1 cgd tmd.tmd1_hadr = a >> 16;
310 1.1 cgd tmd.tmd1_bits = 0;
311 1.1 cgd tmd.tmd2 = 0 | LE_XMD2_ONES;
312 1.1 cgd tmd.tmd3 = 0;
313 1.1 cgd (*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
314 1.1 cgd sizeof(tmd));
315 1.1 cgd }
316 1.1 cgd }
317 1.1 cgd
318 1.1 cgd void
319 1.19 thorpej am7990_stop(sc)
320 1.19 thorpej struct am7990_softc *sc;
321 1.1 cgd {
322 1.1 cgd
323 1.19 thorpej (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
324 1.1 cgd }
325 1.1 cgd
326 1.1 cgd /*
327 1.1 cgd * Initialization of interface; set up initialization block
328 1.1 cgd * and transmit/receive descriptor rings.
329 1.1 cgd */
330 1.1 cgd void
331 1.19 thorpej am7990_init(sc)
332 1.19 thorpej register struct am7990_softc *sc;
333 1.1 cgd {
334 1.1 cgd register int timo;
335 1.1 cgd u_long a;
336 1.1 cgd
337 1.19 thorpej (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
338 1.19 thorpej DELAY(100);
339 1.29 veego
340 1.29 veego /* Newer LANCE chips have a reset register */
341 1.29 veego if (sc->sc_hwreset)
342 1.29 veego (*sc->sc_hwreset)(sc);
343 1.1 cgd
344 1.1 cgd /* Set the correct byte swapping mode, etc. */
345 1.19 thorpej (*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
346 1.1 cgd
347 1.1 cgd /* Set up LANCE init block. */
348 1.19 thorpej am7990_meminit(sc);
349 1.1 cgd
350 1.1 cgd /* Give LANCE the physical address of its init block. */
351 1.1 cgd a = sc->sc_addr + LE_INITADDR(sc);
352 1.19 thorpej (*sc->sc_wrcsr)(sc, LE_CSR1, a);
353 1.19 thorpej (*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
354 1.1 cgd
355 1.1 cgd /* Try to initialize the LANCE. */
356 1.19 thorpej DELAY(100);
357 1.19 thorpej (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
358 1.1 cgd
359 1.1 cgd /* Wait for initialization to finish. */
360 1.1 cgd for (timo = 100000; timo; timo--)
361 1.19 thorpej if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
362 1.1 cgd break;
363 1.1 cgd
364 1.19 thorpej if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
365 1.1 cgd /* Start the LANCE. */
366 1.19 thorpej (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT |
367 1.19 thorpej LE_C0_IDON);
368 1.1 cgd ifp->if_flags |= IFF_RUNNING;
369 1.1 cgd ifp->if_flags &= ~IFF_OACTIVE;
370 1.1 cgd ifp->if_timer = 0;
371 1.19 thorpej am7990_start(ifp);
372 1.1 cgd } else
373 1.22 christos printf("%s: card failed to initialize\n", sc->sc_dev.dv_xname);
374 1.19 thorpej if (sc->sc_hwinit)
375 1.19 thorpej (*sc->sc_hwinit)(sc);
376 1.1 cgd }
377 1.1 cgd
378 1.1 cgd /*
379 1.1 cgd * Routine to copy from mbuf chain to transmit buffer in
380 1.1 cgd * network buffer memory.
381 1.1 cgd */
382 1.1 cgd integrate int
383 1.19 thorpej am7990_put(sc, boff, m)
384 1.19 thorpej struct am7990_softc *sc;
385 1.1 cgd int boff;
386 1.1 cgd register struct mbuf *m;
387 1.1 cgd {
388 1.1 cgd register struct mbuf *n;
389 1.1 cgd register int len, tlen = 0;
390 1.1 cgd
391 1.1 cgd for (; m; m = n) {
392 1.1 cgd len = m->m_len;
393 1.1 cgd if (len == 0) {
394 1.1 cgd MFREE(m, n);
395 1.1 cgd continue;
396 1.1 cgd }
397 1.1 cgd (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
398 1.1 cgd boff += len;
399 1.1 cgd tlen += len;
400 1.1 cgd MFREE(m, n);
401 1.1 cgd }
402 1.1 cgd if (tlen < LEMINSIZE) {
403 1.1 cgd (*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
404 1.1 cgd tlen = LEMINSIZE;
405 1.1 cgd }
406 1.1 cgd return (tlen);
407 1.1 cgd }
408 1.1 cgd
409 1.1 cgd /*
410 1.1 cgd * Pull data off an interface.
411 1.1 cgd * Len is length of data, with local net header stripped.
412 1.1 cgd * We copy the data into mbufs. When full cluster sized units are present
413 1.1 cgd * we copy into clusters.
414 1.1 cgd */
415 1.1 cgd integrate struct mbuf *
416 1.19 thorpej am7990_get(sc, boff, totlen)
417 1.19 thorpej struct am7990_softc *sc;
418 1.1 cgd int boff, totlen;
419 1.1 cgd {
420 1.1 cgd register struct mbuf *m;
421 1.1 cgd struct mbuf *top, **mp;
422 1.1 cgd int len, pad;
423 1.1 cgd
424 1.1 cgd MGETHDR(m, M_DONTWAIT, MT_DATA);
425 1.1 cgd if (m == 0)
426 1.1 cgd return (0);
427 1.7 mycroft m->m_pkthdr.rcvif = ifp;
428 1.1 cgd m->m_pkthdr.len = totlen;
429 1.1 cgd pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
430 1.1 cgd m->m_data += pad;
431 1.1 cgd len = MHLEN - pad;
432 1.1 cgd top = 0;
433 1.1 cgd mp = ⊤
434 1.1 cgd
435 1.1 cgd while (totlen > 0) {
436 1.1 cgd if (top) {
437 1.1 cgd MGET(m, M_DONTWAIT, MT_DATA);
438 1.1 cgd if (m == 0) {
439 1.1 cgd m_freem(top);
440 1.1 cgd return 0;
441 1.1 cgd }
442 1.1 cgd len = MLEN;
443 1.1 cgd }
444 1.1 cgd if (top && totlen >= MINCLSIZE) {
445 1.1 cgd MCLGET(m, M_DONTWAIT);
446 1.1 cgd if (m->m_flags & M_EXT)
447 1.1 cgd len = MCLBYTES;
448 1.1 cgd }
449 1.1 cgd m->m_len = len = min(totlen, len);
450 1.1 cgd (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
451 1.1 cgd boff += len;
452 1.1 cgd totlen -= len;
453 1.1 cgd *mp = m;
454 1.1 cgd mp = &m->m_next;
455 1.1 cgd }
456 1.1 cgd
457 1.1 cgd return (top);
458 1.1 cgd }
459 1.1 cgd
460 1.1 cgd /*
461 1.1 cgd * Pass a packet to the higher levels.
462 1.1 cgd */
463 1.1 cgd integrate void
464 1.19 thorpej am7990_read(sc, boff, len)
465 1.19 thorpej register struct am7990_softc *sc;
466 1.1 cgd int boff, len;
467 1.1 cgd {
468 1.1 cgd struct mbuf *m;
469 1.1 cgd struct ether_header *eh;
470 1.1 cgd
471 1.2 mycroft if (len <= sizeof(struct ether_header) ||
472 1.3 mycroft len > ETHERMTU + sizeof(struct ether_header)) {
473 1.6 mycroft #ifdef LEDEBUG
474 1.22 christos printf("%s: invalid packet size %d; dropping\n",
475 1.2 mycroft sc->sc_dev.dv_xname, len);
476 1.6 mycroft #endif
477 1.2 mycroft ifp->if_ierrors++;
478 1.1 cgd return;
479 1.2 mycroft }
480 1.1 cgd
481 1.1 cgd /* Pull packet off interface. */
482 1.19 thorpej m = am7990_get(sc, boff, len);
483 1.2 mycroft if (m == 0) {
484 1.2 mycroft ifp->if_ierrors++;
485 1.1 cgd return;
486 1.2 mycroft }
487 1.2 mycroft
488 1.2 mycroft ifp->if_ipackets++;
489 1.1 cgd
490 1.1 cgd /* We assume that the header fit entirely in one mbuf. */
491 1.1 cgd eh = mtod(m, struct ether_header *);
492 1.1 cgd
493 1.1 cgd #if NBPFILTER > 0
494 1.1 cgd /*
495 1.1 cgd * Check if there's a BPF listener on this interface.
496 1.1 cgd * If so, hand off the raw packet to BPF.
497 1.1 cgd */
498 1.1 cgd if (ifp->if_bpf) {
499 1.1 cgd bpf_mtap(ifp->if_bpf, m);
500 1.1 cgd
501 1.5 mycroft #ifndef LANCE_REVC_BUG
502 1.1 cgd /*
503 1.1 cgd * Note that the interface cannot be in promiscuous mode if
504 1.1 cgd * there are no BPF listeners. And if we are in promiscuous
505 1.1 cgd * mode, we have to check if this packet is really ours.
506 1.1 cgd */
507 1.1 cgd if ((ifp->if_flags & IFF_PROMISC) != 0 &&
508 1.1 cgd (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
509 1.26 is ETHER_CMP(eh->ether_dhost, LLADDR(ifp->if_sadl))) {
510 1.1 cgd m_freem(m);
511 1.1 cgd return;
512 1.1 cgd }
513 1.5 mycroft #endif
514 1.5 mycroft }
515 1.5 mycroft #endif
516 1.5 mycroft
517 1.5 mycroft #ifdef LANCE_REVC_BUG
518 1.13 gwr /*
519 1.13 gwr * The old LANCE (Rev. C) chips have a bug which causes
520 1.13 gwr * garbage to be inserted in front of the received packet.
521 1.13 gwr * The work-around is to ignore packets with an invalid
522 1.13 gwr * destination address (garbage will usually not match).
523 1.13 gwr * Of course, this precludes multicast support...
524 1.13 gwr */
525 1.26 is if (ETHER_CMP(eh->ether_dhost, LLADDR(ifp->if_sadl)) &&
526 1.14 mycroft ETHER_CMP(eh->ether_dhost, etherbroadcastaddr)) {
527 1.5 mycroft m_freem(m);
528 1.5 mycroft return;
529 1.1 cgd }
530 1.1 cgd #endif
531 1.1 cgd
532 1.1 cgd /* Pass the packet up, with the ether header sort-of removed. */
533 1.1 cgd m_adj(m, sizeof(struct ether_header));
534 1.1 cgd ether_input(ifp, eh, m);
535 1.1 cgd }
536 1.1 cgd
537 1.1 cgd integrate void
538 1.19 thorpej am7990_rint(sc)
539 1.19 thorpej struct am7990_softc *sc;
540 1.1 cgd {
541 1.1 cgd register int bix;
542 1.1 cgd int rp;
543 1.1 cgd struct lermd rmd;
544 1.1 cgd
545 1.1 cgd bix = sc->sc_last_rd;
546 1.1 cgd
547 1.1 cgd /* Process all buffers with valid data. */
548 1.1 cgd for (;;) {
549 1.1 cgd rp = LE_RMDADDR(sc, bix);
550 1.1 cgd (*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
551 1.1 cgd
552 1.1 cgd if (rmd.rmd1_bits & LE_R1_OWN)
553 1.1 cgd break;
554 1.1 cgd
555 1.1 cgd if (rmd.rmd1_bits & LE_R1_ERR) {
556 1.1 cgd if (rmd.rmd1_bits & LE_R1_ENP) {
557 1.6 mycroft #ifdef LEDEBUG
558 1.1 cgd if ((rmd.rmd1_bits & LE_R1_OFLO) == 0) {
559 1.1 cgd if (rmd.rmd1_bits & LE_R1_FRAM)
560 1.22 christos printf("%s: framing error\n",
561 1.1 cgd sc->sc_dev.dv_xname);
562 1.1 cgd if (rmd.rmd1_bits & LE_R1_CRC)
563 1.22 christos printf("%s: crc mismatch\n",
564 1.1 cgd sc->sc_dev.dv_xname);
565 1.1 cgd }
566 1.6 mycroft #endif
567 1.1 cgd } else {
568 1.1 cgd if (rmd.rmd1_bits & LE_R1_OFLO)
569 1.22 christos printf("%s: overflow\n",
570 1.1 cgd sc->sc_dev.dv_xname);
571 1.1 cgd }
572 1.1 cgd if (rmd.rmd1_bits & LE_R1_BUFF)
573 1.22 christos printf("%s: receive buffer error\n",
574 1.1 cgd sc->sc_dev.dv_xname);
575 1.7 mycroft ifp->if_ierrors++;
576 1.12 christos } else if ((rmd.rmd1_bits & (LE_R1_STP | LE_R1_ENP)) !=
577 1.1 cgd (LE_R1_STP | LE_R1_ENP)) {
578 1.22 christos printf("%s: dropping chained buffer\n",
579 1.1 cgd sc->sc_dev.dv_xname);
580 1.7 mycroft ifp->if_ierrors++;
581 1.1 cgd } else {
582 1.1 cgd #ifdef LEDEBUG
583 1.1 cgd if (sc->sc_debug)
584 1.19 thorpej am7990_recv_print(sc, sc->sc_last_rd);
585 1.1 cgd #endif
586 1.19 thorpej am7990_read(sc, LE_RBUFADDR(sc, bix),
587 1.19 thorpej (int)rmd.rmd3 - 4);
588 1.1 cgd }
589 1.1 cgd
590 1.1 cgd rmd.rmd1_bits = LE_R1_OWN;
591 1.1 cgd rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
592 1.1 cgd rmd.rmd3 = 0;
593 1.1 cgd (*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
594 1.1 cgd
595 1.1 cgd #ifdef LEDEBUG
596 1.1 cgd if (sc->sc_debug)
597 1.22 christos printf("sc->sc_last_rd = %x, rmd: "
598 1.16 pk "ladr %04x, hadr %02x, flags %02x, "
599 1.16 pk "bcnt %04x, mcnt %04x\n",
600 1.16 pk sc->sc_last_rd,
601 1.16 pk rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits,
602 1.16 pk rmd.rmd2, rmd.rmd3);
603 1.1 cgd #endif
604 1.1 cgd
605 1.1 cgd if (++bix == sc->sc_nrbuf)
606 1.1 cgd bix = 0;
607 1.1 cgd }
608 1.1 cgd
609 1.1 cgd sc->sc_last_rd = bix;
610 1.1 cgd }
611 1.1 cgd
612 1.1 cgd integrate void
613 1.19 thorpej am7990_tint(sc)
614 1.19 thorpej register struct am7990_softc *sc;
615 1.1 cgd {
616 1.1 cgd register int bix;
617 1.1 cgd struct letmd tmd;
618 1.9 thorpej
619 1.1 cgd bix = sc->sc_first_td;
620 1.1 cgd
621 1.1 cgd for (;;) {
622 1.1 cgd if (sc->sc_no_td <= 0)
623 1.1 cgd break;
624 1.1 cgd
625 1.1 cgd #ifdef LEDEBUG
626 1.1 cgd if (sc->sc_debug)
627 1.22 christos printf("trans tmd: "
628 1.21 christos "ladr %04x, hadr %02x, flags %02x, "
629 1.21 christos "bcnt %04x, mcnt %04x\n",
630 1.21 christos tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits,
631 1.21 christos tmd.tmd2, tmd.tmd3);
632 1.1 cgd #endif
633 1.1 cgd
634 1.1 cgd (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
635 1.1 cgd sizeof(tmd));
636 1.1 cgd
637 1.1 cgd if (tmd.tmd1_bits & LE_T1_OWN)
638 1.1 cgd break;
639 1.1 cgd
640 1.1 cgd ifp->if_flags &= ~IFF_OACTIVE;
641 1.1 cgd
642 1.1 cgd if (tmd.tmd1_bits & LE_T1_ERR) {
643 1.1 cgd if (tmd.tmd3 & LE_T3_BUFF)
644 1.22 christos printf("%s: transmit buffer error\n",
645 1.19 thorpej sc->sc_dev.dv_xname);
646 1.1 cgd else if (tmd.tmd3 & LE_T3_UFLO)
647 1.22 christos printf("%s: underflow\n", sc->sc_dev.dv_xname);
648 1.1 cgd if (tmd.tmd3 & (LE_T3_BUFF | LE_T3_UFLO)) {
649 1.19 thorpej am7990_reset(sc);
650 1.1 cgd return;
651 1.1 cgd }
652 1.20 abrown if (tmd.tmd3 & LE_T3_LCAR) {
653 1.27 thorpej sc->sc_havecarrier = 0;
654 1.20 abrown if (sc->sc_nocarrier)
655 1.20 abrown (*sc->sc_nocarrier)(sc);
656 1.20 abrown else
657 1.22 christos printf("%s: lost carrier\n",
658 1.20 abrown sc->sc_dev.dv_xname);
659 1.20 abrown }
660 1.1 cgd if (tmd.tmd3 & LE_T3_LCOL)
661 1.1 cgd ifp->if_collisions++;
662 1.1 cgd if (tmd.tmd3 & LE_T3_RTRY) {
663 1.22 christos printf("%s: excessive collisions, tdr %d\n",
664 1.19 thorpej sc->sc_dev.dv_xname,
665 1.19 thorpej tmd.tmd3 & LE_T3_TDR_MASK);
666 1.1 cgd ifp->if_collisions += 16;
667 1.1 cgd }
668 1.1 cgd ifp->if_oerrors++;
669 1.1 cgd } else {
670 1.1 cgd if (tmd.tmd1_bits & LE_T1_ONE)
671 1.1 cgd ifp->if_collisions++;
672 1.1 cgd else if (tmd.tmd1_bits & LE_T1_MORE)
673 1.1 cgd /* Real number is unknown. */
674 1.1 cgd ifp->if_collisions += 2;
675 1.1 cgd ifp->if_opackets++;
676 1.1 cgd }
677 1.1 cgd
678 1.1 cgd if (++bix == sc->sc_ntbuf)
679 1.1 cgd bix = 0;
680 1.1 cgd
681 1.1 cgd --sc->sc_no_td;
682 1.1 cgd }
683 1.1 cgd
684 1.1 cgd sc->sc_first_td = bix;
685 1.1 cgd
686 1.19 thorpej am7990_start(ifp);
687 1.1 cgd
688 1.1 cgd if (sc->sc_no_td == 0)
689 1.1 cgd ifp->if_timer = 0;
690 1.1 cgd }
691 1.1 cgd
692 1.1 cgd /*
693 1.1 cgd * Controller interrupt.
694 1.1 cgd */
695 1.1 cgd int
696 1.19 thorpej am7990_intr(arg)
697 1.1 cgd register void *arg;
698 1.1 cgd {
699 1.19 thorpej register struct am7990_softc *sc = arg;
700 1.1 cgd register u_int16_t isr;
701 1.1 cgd
702 1.25 leo isr = (*sc->sc_rdcsr)(sc, LE_CSR0) | sc->sc_saved_csr0;
703 1.25 leo sc->sc_saved_csr0 = 0;
704 1.1 cgd #ifdef LEDEBUG
705 1.1 cgd if (sc->sc_debug)
706 1.22 christos printf("%s: am7990_intr entering with isr=%04x\n",
707 1.1 cgd sc->sc_dev.dv_xname, isr);
708 1.1 cgd #endif
709 1.1 cgd if ((isr & LE_C0_INTR) == 0)
710 1.1 cgd return (0);
711 1.1 cgd
712 1.19 thorpej (*sc->sc_wrcsr)(sc, LE_CSR0,
713 1.1 cgd isr & (LE_C0_INEA | LE_C0_BABL | LE_C0_MISS | LE_C0_MERR |
714 1.1 cgd LE_C0_RINT | LE_C0_TINT | LE_C0_IDON));
715 1.1 cgd if (isr & LE_C0_ERR) {
716 1.1 cgd if (isr & LE_C0_BABL) {
717 1.6 mycroft #ifdef LEDEBUG
718 1.22 christos printf("%s: babble\n", sc->sc_dev.dv_xname);
719 1.6 mycroft #endif
720 1.7 mycroft ifp->if_oerrors++;
721 1.1 cgd }
722 1.1 cgd #if 0
723 1.1 cgd if (isr & LE_C0_CERR) {
724 1.22 christos printf("%s: collision error\n", sc->sc_dev.dv_xname);
725 1.7 mycroft ifp->if_collisions++;
726 1.1 cgd }
727 1.1 cgd #endif
728 1.6 mycroft if (isr & LE_C0_MISS) {
729 1.6 mycroft #ifdef LEDEBUG
730 1.22 christos printf("%s: missed packet\n", sc->sc_dev.dv_xname);
731 1.6 mycroft #endif
732 1.7 mycroft ifp->if_ierrors++;
733 1.6 mycroft }
734 1.1 cgd if (isr & LE_C0_MERR) {
735 1.22 christos printf("%s: memory error\n", sc->sc_dev.dv_xname);
736 1.19 thorpej am7990_reset(sc);
737 1.1 cgd return (1);
738 1.1 cgd }
739 1.1 cgd }
740 1.1 cgd
741 1.1 cgd if ((isr & LE_C0_RXON) == 0) {
742 1.22 christos printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
743 1.7 mycroft ifp->if_ierrors++;
744 1.19 thorpej am7990_reset(sc);
745 1.1 cgd return (1);
746 1.1 cgd }
747 1.1 cgd if ((isr & LE_C0_TXON) == 0) {
748 1.22 christos printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
749 1.7 mycroft ifp->if_oerrors++;
750 1.19 thorpej am7990_reset(sc);
751 1.1 cgd return (1);
752 1.1 cgd }
753 1.1 cgd
754 1.27 thorpej /*
755 1.27 thorpej * Pretend we have carrier; if we don't this will be cleared
756 1.27 thorpej * shortly.
757 1.27 thorpej */
758 1.27 thorpej sc->sc_havecarrier = 1;
759 1.27 thorpej
760 1.1 cgd if (isr & LE_C0_RINT)
761 1.19 thorpej am7990_rint(sc);
762 1.1 cgd if (isr & LE_C0_TINT)
763 1.19 thorpej am7990_tint(sc);
764 1.1 cgd
765 1.1 cgd return (1);
766 1.1 cgd }
767 1.7 mycroft
768 1.7 mycroft #undef ifp
769 1.1 cgd
770 1.19 thorpej void
771 1.19 thorpej am7990_watchdog(ifp)
772 1.19 thorpej struct ifnet *ifp;
773 1.19 thorpej {
774 1.19 thorpej struct am7990_softc *sc = ifp->if_softc;
775 1.19 thorpej
776 1.19 thorpej log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
777 1.19 thorpej ++ifp->if_oerrors;
778 1.19 thorpej
779 1.19 thorpej am7990_reset(sc);
780 1.19 thorpej }
781 1.19 thorpej
782 1.27 thorpej int
783 1.27 thorpej am7990_mediachange(ifp)
784 1.27 thorpej struct ifnet *ifp;
785 1.27 thorpej {
786 1.27 thorpej struct am7990_softc *sc = ifp->if_softc;
787 1.27 thorpej
788 1.27 thorpej if (sc->sc_mediachange)
789 1.27 thorpej return ((*sc->sc_mediachange)(sc));
790 1.27 thorpej return (EINVAL);
791 1.27 thorpej }
792 1.27 thorpej
793 1.27 thorpej void
794 1.27 thorpej am7990_mediastatus(ifp, ifmr)
795 1.27 thorpej struct ifnet *ifp;
796 1.27 thorpej struct ifmediareq *ifmr;
797 1.27 thorpej {
798 1.27 thorpej struct am7990_softc *sc = ifp->if_softc;
799 1.27 thorpej
800 1.27 thorpej if ((ifp->if_flags & IFF_UP) == 0)
801 1.27 thorpej return;
802 1.27 thorpej
803 1.27 thorpej ifmr->ifm_status = IFM_AVALID;
804 1.27 thorpej if (sc->sc_havecarrier)
805 1.27 thorpej ifmr->ifm_status |= IFM_ACTIVE;
806 1.27 thorpej
807 1.27 thorpej if (sc->sc_mediastatus)
808 1.27 thorpej (*sc->sc_mediastatus)(sc, ifmr);
809 1.27 thorpej }
810 1.27 thorpej
811 1.1 cgd /*
812 1.1 cgd * Setup output on interface.
813 1.1 cgd * Get another datagram to send off of the interface queue, and map it to the
814 1.1 cgd * interface before starting the output.
815 1.1 cgd * Called only at splimp or interrupt level.
816 1.1 cgd */
817 1.1 cgd void
818 1.19 thorpej am7990_start(ifp)
819 1.1 cgd register struct ifnet *ifp;
820 1.1 cgd {
821 1.19 thorpej register struct am7990_softc *sc = ifp->if_softc;
822 1.1 cgd register int bix;
823 1.1 cgd register struct mbuf *m;
824 1.1 cgd struct letmd tmd;
825 1.1 cgd int rp;
826 1.1 cgd int len;
827 1.1 cgd
828 1.1 cgd if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
829 1.1 cgd return;
830 1.1 cgd
831 1.1 cgd bix = sc->sc_last_td;
832 1.1 cgd
833 1.1 cgd for (;;) {
834 1.1 cgd rp = LE_TMDADDR(sc, bix);
835 1.1 cgd (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
836 1.1 cgd
837 1.1 cgd if (tmd.tmd1_bits & LE_T1_OWN) {
838 1.1 cgd ifp->if_flags |= IFF_OACTIVE;
839 1.22 christos printf("missing buffer, no_td = %d, last_td = %d\n",
840 1.1 cgd sc->sc_no_td, sc->sc_last_td);
841 1.1 cgd }
842 1.1 cgd
843 1.1 cgd IF_DEQUEUE(&ifp->if_snd, m);
844 1.1 cgd if (m == 0)
845 1.1 cgd break;
846 1.1 cgd
847 1.1 cgd #if NBPFILTER > 0
848 1.1 cgd /*
849 1.1 cgd * If BPF is listening on this interface, let it see the packet
850 1.1 cgd * before we commit it to the wire.
851 1.1 cgd */
852 1.1 cgd if (ifp->if_bpf)
853 1.1 cgd bpf_mtap(ifp->if_bpf, m);
854 1.1 cgd #endif
855 1.1 cgd
856 1.1 cgd /*
857 1.1 cgd * Copy the mbuf chain into the transmit buffer.
858 1.1 cgd */
859 1.19 thorpej len = am7990_put(sc, LE_TBUFADDR(sc, bix), m);
860 1.1 cgd
861 1.1 cgd #ifdef LEDEBUG
862 1.3 mycroft if (len > ETHERMTU + sizeof(struct ether_header))
863 1.22 christos printf("packet length %d\n", len);
864 1.1 cgd #endif
865 1.1 cgd
866 1.1 cgd ifp->if_timer = 5;
867 1.1 cgd
868 1.1 cgd /*
869 1.1 cgd * Init transmit registers, and set transmit start flag.
870 1.1 cgd */
871 1.1 cgd tmd.tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
872 1.1 cgd tmd.tmd2 = -len | LE_XMD2_ONES;
873 1.1 cgd tmd.tmd3 = 0;
874 1.1 cgd
875 1.1 cgd (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
876 1.1 cgd
877 1.1 cgd #ifdef LEDEBUG
878 1.1 cgd if (sc->sc_debug)
879 1.19 thorpej am7990_xmit_print(sc, sc->sc_last_td);
880 1.1 cgd #endif
881 1.1 cgd
882 1.19 thorpej (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
883 1.1 cgd
884 1.1 cgd if (++bix == sc->sc_ntbuf)
885 1.1 cgd bix = 0;
886 1.1 cgd
887 1.1 cgd if (++sc->sc_no_td == sc->sc_ntbuf) {
888 1.1 cgd ifp->if_flags |= IFF_OACTIVE;
889 1.1 cgd break;
890 1.1 cgd }
891 1.1 cgd
892 1.1 cgd }
893 1.1 cgd
894 1.1 cgd sc->sc_last_td = bix;
895 1.1 cgd }
896 1.1 cgd
897 1.1 cgd /*
898 1.1 cgd * Process an ioctl request.
899 1.1 cgd */
900 1.1 cgd int
901 1.19 thorpej am7990_ioctl(ifp, cmd, data)
902 1.1 cgd register struct ifnet *ifp;
903 1.1 cgd u_long cmd;
904 1.1 cgd caddr_t data;
905 1.1 cgd {
906 1.19 thorpej register struct am7990_softc *sc = ifp->if_softc;
907 1.1 cgd struct ifaddr *ifa = (struct ifaddr *)data;
908 1.1 cgd struct ifreq *ifr = (struct ifreq *)data;
909 1.1 cgd int s, error = 0;
910 1.1 cgd
911 1.1 cgd s = splimp();
912 1.1 cgd
913 1.1 cgd switch (cmd) {
914 1.1 cgd
915 1.1 cgd case SIOCSIFADDR:
916 1.1 cgd ifp->if_flags |= IFF_UP;
917 1.1 cgd
918 1.1 cgd switch (ifa->ifa_addr->sa_family) {
919 1.1 cgd #ifdef INET
920 1.1 cgd case AF_INET:
921 1.19 thorpej am7990_init(sc);
922 1.26 is arp_ifinit(ifp, ifa);
923 1.1 cgd break;
924 1.1 cgd #endif
925 1.1 cgd #ifdef NS
926 1.1 cgd case AF_NS:
927 1.1 cgd {
928 1.1 cgd register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
929 1.1 cgd
930 1.1 cgd if (ns_nullhost(*ina))
931 1.1 cgd ina->x_host =
932 1.26 is *(union ns_host *)LLADDR(ifp->if_sadl);
933 1.26 is else {
934 1.1 cgd bcopy(ina->x_host.c_host,
935 1.26 is LLADDR(ifp->if_sadl),
936 1.26 is sizeof(sc->sc_enaddr));
937 1.26 is }
938 1.1 cgd /* Set new address. */
939 1.19 thorpej am7990_init(sc);
940 1.1 cgd break;
941 1.1 cgd }
942 1.1 cgd #endif
943 1.1 cgd default:
944 1.19 thorpej am7990_init(sc);
945 1.1 cgd break;
946 1.1 cgd }
947 1.1 cgd break;
948 1.1 cgd
949 1.1 cgd #if defined(CCITT) && defined(LLC)
950 1.1 cgd case SIOCSIFCONF_X25:
951 1.1 cgd ifp->if_flags |= IFF_UP;
952 1.11 christos ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
953 1.1 cgd error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
954 1.1 cgd if (error == 0)
955 1.19 thorpej am7990_init(sc);
956 1.1 cgd break;
957 1.1 cgd #endif /* CCITT && LLC */
958 1.1 cgd
959 1.1 cgd case SIOCSIFFLAGS:
960 1.1 cgd if ((ifp->if_flags & IFF_UP) == 0 &&
961 1.1 cgd (ifp->if_flags & IFF_RUNNING) != 0) {
962 1.1 cgd /*
963 1.1 cgd * If interface is marked down and it is running, then
964 1.1 cgd * stop it.
965 1.1 cgd */
966 1.19 thorpej am7990_stop(sc);
967 1.1 cgd ifp->if_flags &= ~IFF_RUNNING;
968 1.1 cgd } else if ((ifp->if_flags & IFF_UP) != 0 &&
969 1.1 cgd (ifp->if_flags & IFF_RUNNING) == 0) {
970 1.1 cgd /*
971 1.1 cgd * If interface is marked up and it is stopped, then
972 1.1 cgd * start it.
973 1.1 cgd */
974 1.19 thorpej am7990_init(sc);
975 1.1 cgd } else {
976 1.1 cgd /*
977 1.1 cgd * Reset the interface to pick up changes in any other
978 1.1 cgd * flags that affect hardware registers.
979 1.1 cgd */
980 1.19 thorpej /*am7990_stop(sc);*/
981 1.19 thorpej am7990_init(sc);
982 1.1 cgd }
983 1.1 cgd #ifdef LEDEBUG
984 1.1 cgd if (ifp->if_flags & IFF_DEBUG)
985 1.1 cgd sc->sc_debug = 1;
986 1.1 cgd else
987 1.1 cgd sc->sc_debug = 0;
988 1.1 cgd #endif
989 1.1 cgd break;
990 1.1 cgd
991 1.1 cgd case SIOCADDMULTI:
992 1.1 cgd case SIOCDELMULTI:
993 1.1 cgd error = (cmd == SIOCADDMULTI) ?
994 1.26 is ether_addmulti(ifr, &sc->sc_ethercom) :
995 1.26 is ether_delmulti(ifr, &sc->sc_ethercom);
996 1.1 cgd
997 1.1 cgd if (error == ENETRESET) {
998 1.1 cgd /*
999 1.1 cgd * Multicast list has changed; set the hardware filter
1000 1.1 cgd * accordingly.
1001 1.1 cgd */
1002 1.19 thorpej am7990_reset(sc);
1003 1.1 cgd error = 0;
1004 1.1 cgd }
1005 1.27 thorpej break;
1006 1.27 thorpej
1007 1.27 thorpej case SIOCGIFMEDIA:
1008 1.27 thorpej case SIOCSIFMEDIA:
1009 1.27 thorpej error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1010 1.1 cgd break;
1011 1.1 cgd
1012 1.1 cgd default:
1013 1.1 cgd error = EINVAL;
1014 1.2 mycroft break;
1015 1.1 cgd }
1016 1.1 cgd
1017 1.1 cgd splx(s);
1018 1.1 cgd return (error);
1019 1.1 cgd }
1020 1.1 cgd
1021 1.19 thorpej hide void
1022 1.19 thorpej am7990_shutdown(arg)
1023 1.19 thorpej void *arg;
1024 1.19 thorpej {
1025 1.19 thorpej
1026 1.19 thorpej am7990_stop((struct am7990_softc *)arg);
1027 1.19 thorpej }
1028 1.19 thorpej
1029 1.1 cgd #ifdef LEDEBUG
1030 1.1 cgd void
1031 1.19 thorpej am7990_recv_print(sc, no)
1032 1.19 thorpej struct am7990_softc *sc;
1033 1.1 cgd int no;
1034 1.1 cgd {
1035 1.1 cgd struct lermd rmd;
1036 1.1 cgd u_int16_t len;
1037 1.1 cgd struct ether_header eh;
1038 1.1 cgd
1039 1.1 cgd (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
1040 1.1 cgd len = rmd.rmd3;
1041 1.22 christos printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
1042 1.1 cgd len);
1043 1.22 christos printf("%s: status %04x\n", sc->sc_dev.dv_xname,
1044 1.19 thorpej (*sc->sc_rdcsr)(sc, LE_CSR0));
1045 1.22 christos printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
1046 1.1 cgd sc->sc_dev.dv_xname,
1047 1.1 cgd rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, rmd.rmd2, rmd.rmd3);
1048 1.1 cgd if (len >= sizeof(eh)) {
1049 1.1 cgd (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
1050 1.22 christos printf("%s: dst %s", sc->sc_dev.dv_xname,
1051 1.16 pk ether_sprintf(eh.ether_dhost));
1052 1.22 christos printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
1053 1.16 pk ntohs(eh.ether_type));
1054 1.1 cgd }
1055 1.1 cgd }
1056 1.1 cgd
1057 1.1 cgd void
1058 1.19 thorpej am7990_xmit_print(sc, no)
1059 1.19 thorpej struct am7990_softc *sc;
1060 1.1 cgd int no;
1061 1.1 cgd {
1062 1.1 cgd struct letmd tmd;
1063 1.1 cgd u_int16_t len;
1064 1.1 cgd struct ether_header eh;
1065 1.1 cgd
1066 1.1 cgd (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
1067 1.1 cgd len = -tmd.tmd2;
1068 1.22 christos printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
1069 1.1 cgd len);
1070 1.22 christos printf("%s: status %04x\n", sc->sc_dev.dv_xname,
1071 1.19 thorpej (*sc->sc_rdcsr)(sc, LE_CSR0));
1072 1.22 christos printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
1073 1.1 cgd sc->sc_dev.dv_xname,
1074 1.1 cgd tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, tmd.tmd2, tmd.tmd3);
1075 1.1 cgd if (len >= sizeof(eh)) {
1076 1.1 cgd (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
1077 1.22 christos printf("%s: dst %s", sc->sc_dev.dv_xname,
1078 1.16 pk ether_sprintf(eh.ether_dhost));
1079 1.22 christos printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
1080 1.1 cgd ntohs(eh.ether_type));
1081 1.1 cgd }
1082 1.1 cgd }
1083 1.1 cgd #endif /* LEDEBUG */
1084 1.1 cgd
1085 1.1 cgd /*
1086 1.1 cgd * Set up the logical address filter.
1087 1.1 cgd */
1088 1.1 cgd void
1089 1.19 thorpej am7990_setladrf(ac, af)
1090 1.26 is struct ethercom *ac;
1091 1.1 cgd u_int16_t *af;
1092 1.1 cgd {
1093 1.26 is struct ifnet *ifp = &ac->ec_if;
1094 1.1 cgd struct ether_multi *enm;
1095 1.1 cgd register u_char *cp, c;
1096 1.1 cgd register u_int32_t crc;
1097 1.1 cgd register int i, len;
1098 1.1 cgd struct ether_multistep step;
1099 1.1 cgd
1100 1.1 cgd /*
1101 1.1 cgd * Set up multicast address filter by passing all multicast addresses
1102 1.1 cgd * through a crc generator, and then using the high order 6 bits as an
1103 1.1 cgd * index into the 64 bit logical address filter. The high order bit
1104 1.1 cgd * selects the word, while the rest of the bits select the bit within
1105 1.1 cgd * the word.
1106 1.1 cgd */
1107 1.1 cgd
1108 1.1 cgd if (ifp->if_flags & IFF_PROMISC)
1109 1.1 cgd goto allmulti;
1110 1.1 cgd
1111 1.1 cgd af[0] = af[1] = af[2] = af[3] = 0x0000;
1112 1.1 cgd ETHER_FIRST_MULTI(step, ac, enm);
1113 1.1 cgd while (enm != NULL) {
1114 1.13 gwr if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
1115 1.1 cgd /*
1116 1.1 cgd * We must listen to a range of multicast addresses.
1117 1.1 cgd * For now, just accept all multicasts, rather than
1118 1.1 cgd * trying to set only those filter bits needed to match
1119 1.1 cgd * the range. (At this time, the only use of address
1120 1.1 cgd * ranges is for IP multicast routing, for which the
1121 1.1 cgd * range is big enough to require all bits set.)
1122 1.1 cgd */
1123 1.1 cgd goto allmulti;
1124 1.1 cgd }
1125 1.1 cgd
1126 1.1 cgd cp = enm->enm_addrlo;
1127 1.1 cgd crc = 0xffffffff;
1128 1.1 cgd for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1129 1.1 cgd c = *cp++;
1130 1.1 cgd for (i = 8; --i >= 0;) {
1131 1.1 cgd if ((crc & 0x01) ^ (c & 0x01)) {
1132 1.1 cgd crc >>= 1;
1133 1.1 cgd crc ^= 0xedb88320;
1134 1.1 cgd } else
1135 1.1 cgd crc >>= 1;
1136 1.1 cgd c >>= 1;
1137 1.1 cgd }
1138 1.1 cgd }
1139 1.1 cgd /* Just want the 6 most significant bits. */
1140 1.1 cgd crc >>= 26;
1141 1.1 cgd
1142 1.1 cgd /* Set the corresponding bit in the filter. */
1143 1.1 cgd af[crc >> 4] |= 1 << (crc & 0xf);
1144 1.1 cgd
1145 1.1 cgd ETHER_NEXT_MULTI(step, enm);
1146 1.1 cgd }
1147 1.1 cgd ifp->if_flags &= ~IFF_ALLMULTI;
1148 1.1 cgd return;
1149 1.1 cgd
1150 1.1 cgd allmulti:
1151 1.1 cgd ifp->if_flags |= IFF_ALLMULTI;
1152 1.1 cgd af[0] = af[1] = af[2] = af[3] = 0xffff;
1153 1.1 cgd }
1154 1.1 cgd
1155 1.1 cgd
1156 1.1 cgd /*
1157 1.4 cgd * Routines for accessing the transmit and receive buffers.
1158 1.4 cgd * The various CPU and adapter configurations supported by this
1159 1.4 cgd * driver require three different access methods for buffers
1160 1.4 cgd * and descriptors:
1161 1.4 cgd * (1) contig (contiguous data; no padding),
1162 1.4 cgd * (2) gap2 (two bytes of data followed by two bytes of padding),
1163 1.4 cgd * (3) gap16 (16 bytes of data followed by 16 bytes of padding).
1164 1.1 cgd */
1165 1.1 cgd
1166 1.4 cgd /*
1167 1.4 cgd * contig: contiguous data with no padding.
1168 1.4 cgd *
1169 1.4 cgd * Buffers may have any alignment.
1170 1.4 cgd */
1171 1.1 cgd
1172 1.17 cgd void
1173 1.17 cgd am7990_copytobuf_contig(sc, from, boff, len)
1174 1.19 thorpej struct am7990_softc *sc;
1175 1.4 cgd void *from;
1176 1.1 cgd int boff, len;
1177 1.1 cgd {
1178 1.1 cgd volatile caddr_t buf = sc->sc_mem;
1179 1.1 cgd
1180 1.1 cgd /*
1181 1.1 cgd * Just call bcopy() to do the work.
1182 1.1 cgd */
1183 1.1 cgd bcopy(from, buf + boff, len);
1184 1.1 cgd }
1185 1.1 cgd
1186 1.17 cgd void
1187 1.17 cgd am7990_copyfrombuf_contig(sc, to, boff, len)
1188 1.19 thorpej struct am7990_softc *sc;
1189 1.4 cgd void *to;
1190 1.1 cgd int boff, len;
1191 1.1 cgd {
1192 1.1 cgd volatile caddr_t buf = sc->sc_mem;
1193 1.1 cgd
1194 1.1 cgd /*
1195 1.1 cgd * Just call bcopy() to do the work.
1196 1.1 cgd */
1197 1.1 cgd bcopy(buf + boff, to, len);
1198 1.1 cgd }
1199 1.1 cgd
1200 1.17 cgd void
1201 1.17 cgd am7990_zerobuf_contig(sc, boff, len)
1202 1.19 thorpej struct am7990_softc *sc;
1203 1.1 cgd int boff, len;
1204 1.1 cgd {
1205 1.1 cgd volatile caddr_t buf = sc->sc_mem;
1206 1.1 cgd
1207 1.1 cgd /*
1208 1.1 cgd * Just let bzero() do the work
1209 1.1 cgd */
1210 1.1 cgd bzero(buf + boff, len);
1211 1.1 cgd }
1212 1.1 cgd
1213 1.19 thorpej #if 0
1214 1.19 thorpej /*
1215 1.19 thorpej * Examples only; duplicate these and tweak (if necessary) in
1216 1.19 thorpej * machine-specific front-ends.
1217 1.19 thorpej */
1218 1.19 thorpej
1219 1.1 cgd /*
1220 1.4 cgd * gap2: two bytes of data followed by two bytes of pad.
1221 1.4 cgd *
1222 1.4 cgd * Buffers must be 4-byte aligned. The code doesn't worry about
1223 1.4 cgd * doing an extra byte.
1224 1.1 cgd */
1225 1.4 cgd
1226 1.17 cgd void
1227 1.17 cgd am7990_copytobuf_gap2(sc, fromv, boff, len)
1228 1.19 thorpej struct am7990_softc *sc;
1229 1.4 cgd void *fromv;
1230 1.1 cgd int boff;
1231 1.1 cgd register int len;
1232 1.1 cgd {
1233 1.1 cgd volatile caddr_t buf = sc->sc_mem;
1234 1.4 cgd register caddr_t from = fromv;
1235 1.4 cgd register volatile u_int16_t *bptr;
1236 1.1 cgd
1237 1.1 cgd if (boff & 0x1) {
1238 1.1 cgd /* handle unaligned first byte */
1239 1.4 cgd bptr = ((volatile u_int16_t *)buf) + (boff - 1);
1240 1.1 cgd *bptr = (*from++ << 8) | (*bptr & 0xff);
1241 1.1 cgd bptr += 2;
1242 1.1 cgd len--;
1243 1.1 cgd } else
1244 1.4 cgd bptr = ((volatile u_int16_t *)buf) + boff;
1245 1.4 cgd while (len > 1) {
1246 1.4 cgd *bptr = (from[1] << 8) | (from[0] & 0xff);
1247 1.4 cgd bptr += 2;
1248 1.4 cgd from += 2;
1249 1.4 cgd len -= 2;
1250 1.1 cgd }
1251 1.1 cgd if (len == 1)
1252 1.4 cgd *bptr = (u_int16_t)*from;
1253 1.1 cgd }
1254 1.1 cgd
1255 1.17 cgd void
1256 1.17 cgd am7990_copyfrombuf_gap2(sc, tov, boff, len)
1257 1.19 thorpej struct am7990_softc *sc;
1258 1.4 cgd void *tov;
1259 1.1 cgd int boff, len;
1260 1.1 cgd {
1261 1.1 cgd volatile caddr_t buf = sc->sc_mem;
1262 1.4 cgd register caddr_t to = tov;
1263 1.4 cgd register volatile u_int16_t *bptr;
1264 1.4 cgd register u_int16_t tmp;
1265 1.1 cgd
1266 1.1 cgd if (boff & 0x1) {
1267 1.1 cgd /* handle unaligned first byte */
1268 1.4 cgd bptr = ((volatile u_int16_t *)buf) + (boff - 1);
1269 1.1 cgd *to++ = (*bptr >> 8) & 0xff;
1270 1.1 cgd bptr += 2;
1271 1.1 cgd len--;
1272 1.1 cgd } else
1273 1.4 cgd bptr = ((volatile u_int16_t *)buf) + boff;
1274 1.4 cgd while (len > 1) {
1275 1.4 cgd tmp = *bptr;
1276 1.4 cgd *to++ = tmp & 0xff;
1277 1.4 cgd *to++ = (tmp >> 8) & 0xff;
1278 1.4 cgd bptr += 2;
1279 1.4 cgd len -= 2;
1280 1.1 cgd }
1281 1.1 cgd if (len == 1)
1282 1.1 cgd *to = *bptr & 0xff;
1283 1.1 cgd }
1284 1.1 cgd
1285 1.17 cgd void
1286 1.17 cgd am7990_zerobuf_gap2(sc, boff, len)
1287 1.19 thorpej struct am7990_softc *sc;
1288 1.1 cgd int boff, len;
1289 1.1 cgd {
1290 1.1 cgd volatile caddr_t buf = sc->sc_mem;
1291 1.4 cgd register volatile u_int16_t *bptr;
1292 1.1 cgd
1293 1.1 cgd if ((unsigned)boff & 0x1) {
1294 1.4 cgd bptr = ((volatile u_int16_t *)buf) + (boff - 1);
1295 1.1 cgd *bptr &= 0xff;
1296 1.1 cgd bptr += 2;
1297 1.1 cgd len--;
1298 1.1 cgd } else
1299 1.4 cgd bptr = ((volatile u_int16_t *)buf) + boff;
1300 1.1 cgd while (len > 0) {
1301 1.1 cgd *bptr = 0;
1302 1.1 cgd bptr += 2;
1303 1.1 cgd len -= 2;
1304 1.1 cgd }
1305 1.1 cgd }
1306 1.1 cgd
1307 1.1 cgd /*
1308 1.4 cgd * gap16: 16 bytes of data followed by 16 bytes of pad.
1309 1.4 cgd *
1310 1.4 cgd * Buffers must be 32-byte aligned.
1311 1.1 cgd */
1312 1.4 cgd
1313 1.17 cgd void
1314 1.17 cgd am7990_copytobuf_gap16(sc, fromv, boff, len)
1315 1.19 thorpej struct am7990_softc *sc;
1316 1.4 cgd void *fromv;
1317 1.1 cgd int boff;
1318 1.1 cgd register int len;
1319 1.1 cgd {
1320 1.1 cgd volatile caddr_t buf = sc->sc_mem;
1321 1.4 cgd register caddr_t from = fromv;
1322 1.1 cgd register caddr_t bptr;
1323 1.1 cgd register int xfer;
1324 1.1 cgd
1325 1.1 cgd bptr = buf + ((boff << 1) & ~0x1f);
1326 1.1 cgd boff &= 0xf;
1327 1.1 cgd xfer = min(len, 16 - boff);
1328 1.1 cgd while (len > 0) {
1329 1.1 cgd bcopy(from, bptr + boff, xfer);
1330 1.1 cgd from += xfer;
1331 1.1 cgd bptr += 32;
1332 1.1 cgd boff = 0;
1333 1.1 cgd len -= xfer;
1334 1.1 cgd xfer = min(len, 16);
1335 1.1 cgd }
1336 1.1 cgd }
1337 1.1 cgd
1338 1.17 cgd void
1339 1.17 cgd am7990_copyfrombuf_gap16(sc, tov, boff, len)
1340 1.19 thorpej struct am7990_softc *sc;
1341 1.4 cgd void *tov;
1342 1.1 cgd int boff, len;
1343 1.1 cgd {
1344 1.1 cgd volatile caddr_t buf = sc->sc_mem;
1345 1.4 cgd register caddr_t to = tov;
1346 1.1 cgd register caddr_t bptr;
1347 1.1 cgd register int xfer;
1348 1.1 cgd
1349 1.1 cgd bptr = buf + ((boff << 1) & ~0x1f);
1350 1.1 cgd boff &= 0xf;
1351 1.1 cgd xfer = min(len, 16 - boff);
1352 1.1 cgd while (len > 0) {
1353 1.1 cgd bcopy(bptr + boff, to, xfer);
1354 1.1 cgd to += xfer;
1355 1.1 cgd bptr += 32;
1356 1.1 cgd boff = 0;
1357 1.1 cgd len -= xfer;
1358 1.1 cgd xfer = min(len, 16);
1359 1.1 cgd }
1360 1.1 cgd }
1361 1.1 cgd
1362 1.17 cgd void
1363 1.17 cgd am7990_zerobuf_gap16(sc, boff, len)
1364 1.19 thorpej struct am7990_softc *sc;
1365 1.1 cgd int boff, len;
1366 1.1 cgd {
1367 1.1 cgd volatile caddr_t buf = sc->sc_mem;
1368 1.1 cgd register caddr_t bptr;
1369 1.1 cgd register int xfer;
1370 1.1 cgd
1371 1.1 cgd bptr = buf + ((boff << 1) & ~0x1f);
1372 1.1 cgd boff &= 0xf;
1373 1.1 cgd xfer = min(len, 16 - boff);
1374 1.1 cgd while (len > 0) {
1375 1.1 cgd bzero(bptr + boff, xfer);
1376 1.1 cgd bptr += 32;
1377 1.1 cgd boff = 0;
1378 1.1 cgd len -= xfer;
1379 1.1 cgd xfer = min(len, 16);
1380 1.1 cgd }
1381 1.1 cgd }
1382 1.19 thorpej #endif /* Example only */
1383