am79900.c revision 1.14 1 1.14 thorpej /* $NetBSD: am79900.c,v 1.14 2004/08/24 00:53:29 thorpej Exp $ */
2 1.13 thorpej
3 1.13 thorpej /*-
4 1.13 thorpej * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 1.13 thorpej * All rights reserved.
6 1.13 thorpej *
7 1.13 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.13 thorpej * by Jason R. Thorpe.
9 1.13 thorpej *
10 1.13 thorpej * Redistribution and use in source and binary forms, with or without
11 1.13 thorpej * modification, are permitted provided that the following conditions
12 1.13 thorpej * are met:
13 1.13 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.13 thorpej * notice, this list of conditions and the following disclaimer.
15 1.13 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.13 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.13 thorpej * documentation and/or other materials provided with the distribution.
18 1.13 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.13 thorpej * must display the following acknowledgement:
20 1.13 thorpej * This product includes software developed by the NetBSD
21 1.13 thorpej * Foundation, Inc. and its contributors.
22 1.13 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.13 thorpej * contributors may be used to endorse or promote products derived
24 1.13 thorpej * from this software without specific prior written permission.
25 1.13 thorpej *
26 1.13 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.13 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.13 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.13 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.13 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.13 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.13 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.13 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.13 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.13 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.13 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.13 thorpej */
38 1.12 agc
39 1.12 agc /*-
40 1.12 agc * Copyright (c) 1992, 1993
41 1.12 agc * The Regents of the University of California. All rights reserved.
42 1.12 agc *
43 1.12 agc * This code is derived from software contributed to Berkeley by
44 1.12 agc * Ralph Campbell and Rick Macklem.
45 1.12 agc *
46 1.12 agc * Redistribution and use in source and binary forms, with or without
47 1.12 agc * modification, are permitted provided that the following conditions
48 1.12 agc * are met:
49 1.12 agc * 1. Redistributions of source code must retain the above copyright
50 1.12 agc * notice, this list of conditions and the following disclaimer.
51 1.12 agc * 2. Redistributions in binary form must reproduce the above copyright
52 1.12 agc * notice, this list of conditions and the following disclaimer in the
53 1.12 agc * documentation and/or other materials provided with the distribution.
54 1.12 agc * 3. Neither the name of the University nor the names of its contributors
55 1.12 agc * may be used to endorse or promote products derived from this software
56 1.12 agc * without specific prior written permission.
57 1.12 agc *
58 1.12 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 1.12 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 1.12 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 1.12 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 1.12 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 1.12 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 1.12 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 1.12 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 1.12 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 1.12 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 1.12 agc * SUCH DAMAGE.
69 1.12 agc *
70 1.12 agc * @(#)if_le.c 8.2 (Berkeley) 11/16/93
71 1.12 agc */
72 1.1 drochner
73 1.1 drochner /*-
74 1.1 drochner * Copyright (c) 1998
75 1.1 drochner * Matthias Drochner. All rights reserved.
76 1.1 drochner * Copyright (c) 1995 Charles M. Hannum. All rights reserved.
77 1.1 drochner *
78 1.1 drochner * This code is derived from software contributed to Berkeley by
79 1.1 drochner * Ralph Campbell and Rick Macklem.
80 1.1 drochner *
81 1.1 drochner * Redistribution and use in source and binary forms, with or without
82 1.1 drochner * modification, are permitted provided that the following conditions
83 1.1 drochner * are met:
84 1.1 drochner * 1. Redistributions of source code must retain the above copyright
85 1.1 drochner * notice, this list of conditions and the following disclaimer.
86 1.1 drochner * 2. Redistributions in binary form must reproduce the above copyright
87 1.1 drochner * notice, this list of conditions and the following disclaimer in the
88 1.1 drochner * documentation and/or other materials provided with the distribution.
89 1.1 drochner * 3. All advertising materials mentioning features or use of this software
90 1.1 drochner * must display the following acknowledgement:
91 1.1 drochner * This product includes software developed by the University of
92 1.1 drochner * California, Berkeley and its contributors.
93 1.1 drochner * 4. Neither the name of the University nor the names of its contributors
94 1.1 drochner * may be used to endorse or promote products derived from this software
95 1.1 drochner * without specific prior written permission.
96 1.1 drochner *
97 1.1 drochner * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
98 1.1 drochner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
99 1.1 drochner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
100 1.1 drochner * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
101 1.1 drochner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
102 1.1 drochner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
103 1.1 drochner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
104 1.1 drochner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
105 1.1 drochner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
106 1.1 drochner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
107 1.1 drochner * SUCH DAMAGE.
108 1.1 drochner *
109 1.1 drochner * @(#)if_le.c 8.2 (Berkeley) 11/16/93
110 1.1 drochner */
111 1.11 lukem
112 1.11 lukem #include <sys/cdefs.h>
113 1.14 thorpej __KERNEL_RCSID(0, "$NetBSD: am79900.c,v 1.14 2004/08/24 00:53:29 thorpej Exp $");
114 1.1 drochner
115 1.1 drochner #include "bpfilter.h"
116 1.1 drochner #include "rnd.h"
117 1.1 drochner
118 1.1 drochner #include <sys/param.h>
119 1.1 drochner #include <sys/systm.h>
120 1.1 drochner #include <sys/mbuf.h>
121 1.1 drochner #include <sys/syslog.h>
122 1.1 drochner #include <sys/socket.h>
123 1.1 drochner #include <sys/device.h>
124 1.1 drochner #include <sys/malloc.h>
125 1.1 drochner #include <sys/ioctl.h>
126 1.1 drochner #include <sys/errno.h>
127 1.1 drochner #if NRND > 0
128 1.1 drochner #include <sys/rnd.h>
129 1.1 drochner #endif
130 1.1 drochner
131 1.1 drochner #include <net/if.h>
132 1.1 drochner #include <net/if_dl.h>
133 1.1 drochner #include <net/if_ether.h>
134 1.1 drochner #include <net/if_media.h>
135 1.1 drochner
136 1.1 drochner #if NBPFILTER > 0
137 1.1 drochner #include <net/bpf.h>
138 1.1 drochner #include <net/bpfdesc.h>
139 1.1 drochner #endif
140 1.1 drochner
141 1.1 drochner #include <dev/ic/lancereg.h>
142 1.1 drochner #include <dev/ic/lancevar.h>
143 1.1 drochner #include <dev/ic/am79900reg.h>
144 1.1 drochner #include <dev/ic/am79900var.h>
145 1.1 drochner
146 1.14 thorpej static void am79900_meminit(struct lance_softc *);
147 1.14 thorpej static void am79900_start(struct ifnet *);
148 1.1 drochner
149 1.9 mrg #if defined(_KERNEL_OPT)
150 1.1 drochner #include "opt_ddb.h"
151 1.1 drochner #endif
152 1.1 drochner
153 1.1 drochner #ifdef LEDEBUG
154 1.14 thorpej static void am79900_recv_print(struct lance_softc *, int);
155 1.14 thorpej static void am79900_xmit_print(struct lance_softc *, int);
156 1.1 drochner #endif
157 1.1 drochner
158 1.1 drochner #define ifp (&sc->sc_ethercom.ec_if)
159 1.1 drochner
160 1.1 drochner void
161 1.14 thorpej am79900_config(struct am79900_softc *sc)
162 1.1 drochner {
163 1.1 drochner int mem, i;
164 1.1 drochner
165 1.1 drochner sc->lsc.sc_meminit = am79900_meminit;
166 1.1 drochner sc->lsc.sc_start = am79900_start;
167 1.1 drochner
168 1.1 drochner lance_config(&sc->lsc);
169 1.1 drochner
170 1.1 drochner mem = 0;
171 1.1 drochner sc->lsc.sc_initaddr = mem;
172 1.1 drochner mem += sizeof(struct leinit);
173 1.1 drochner sc->lsc.sc_rmdaddr = mem;
174 1.1 drochner mem += sizeof(struct lermd) * sc->lsc.sc_nrbuf;
175 1.1 drochner sc->lsc.sc_tmdaddr = mem;
176 1.1 drochner mem += sizeof(struct letmd) * sc->lsc.sc_ntbuf;
177 1.1 drochner for (i = 0; i < sc->lsc.sc_nrbuf; i++, mem += LEBLEN)
178 1.1 drochner sc->lsc.sc_rbufaddr[i] = mem;
179 1.1 drochner for (i = 0; i < sc->lsc.sc_ntbuf; i++, mem += LEBLEN)
180 1.1 drochner sc->lsc.sc_tbufaddr[i] = mem;
181 1.1 drochner
182 1.1 drochner if (mem > sc->lsc.sc_memsize)
183 1.1 drochner panic("%s: memsize", sc->lsc.sc_dev.dv_xname);
184 1.1 drochner }
185 1.1 drochner
186 1.1 drochner /*
187 1.1 drochner * Set up the initialization block and the descriptor rings.
188 1.1 drochner */
189 1.14 thorpej static void
190 1.14 thorpej am79900_meminit(struct lance_softc *sc)
191 1.1 drochner {
192 1.1 drochner u_long a;
193 1.1 drochner int bix;
194 1.1 drochner struct leinit init;
195 1.1 drochner struct lermd rmd;
196 1.1 drochner struct letmd tmd;
197 1.1 drochner u_int8_t *myaddr;
198 1.1 drochner
199 1.1 drochner #if NBPFILTER > 0
200 1.1 drochner if (ifp->if_flags & IFF_PROMISC)
201 1.1 drochner init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM;
202 1.1 drochner else
203 1.1 drochner #endif
204 1.1 drochner init.init_mode = LE_MODE_NORMAL;
205 1.1 drochner if (sc->sc_initmodemedia == 1)
206 1.1 drochner init.init_mode |= LE_MODE_PSEL0;
207 1.1 drochner
208 1.1 drochner init.init_mode |= ((ffs(sc->sc_ntbuf) - 1) << 28)
209 1.1 drochner | ((ffs(sc->sc_nrbuf) - 1) << 20);
210 1.1 drochner
211 1.1 drochner /*
212 1.1 drochner * Update our private copy of the Ethernet address.
213 1.1 drochner * We NEED the copy so we can ensure its alignment!
214 1.1 drochner */
215 1.10 thorpej memcpy(sc->sc_enaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
216 1.1 drochner myaddr = sc->sc_enaddr;
217 1.1 drochner
218 1.1 drochner init.init_padr[0] = myaddr[0] | (myaddr[1] << 8)
219 1.1 drochner | (myaddr[2] << 16) | (myaddr[3] << 24);
220 1.1 drochner init.init_padr[1] = myaddr[4] | (myaddr[5] << 8);
221 1.1 drochner lance_setladrf(&sc->sc_ethercom, init.init_ladrf);
222 1.1 drochner
223 1.1 drochner sc->sc_last_rd = 0;
224 1.1 drochner sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
225 1.1 drochner
226 1.1 drochner a = sc->sc_addr + LE_RMDADDR(sc, 0);
227 1.1 drochner init.init_rdra = a;
228 1.1 drochner
229 1.1 drochner a = sc->sc_addr + LE_TMDADDR(sc, 0);
230 1.1 drochner init.init_tdra = a;
231 1.1 drochner
232 1.1 drochner (*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
233 1.1 drochner
234 1.1 drochner /*
235 1.1 drochner * Set up receive ring descriptors.
236 1.1 drochner */
237 1.1 drochner for (bix = 0; bix < sc->sc_nrbuf; bix++) {
238 1.1 drochner a = sc->sc_addr + LE_RBUFADDR(sc, bix);
239 1.1 drochner rmd.rmd0 = a;
240 1.1 drochner rmd.rmd1 = LE_R1_OWN | LE_R1_ONES | (-LEBLEN & 0xfff);
241 1.1 drochner rmd.rmd2 = 0;
242 1.1 drochner rmd.rmd3 = 0;
243 1.1 drochner (*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
244 1.1 drochner sizeof(rmd));
245 1.1 drochner }
246 1.1 drochner
247 1.1 drochner /*
248 1.1 drochner * Set up transmit ring descriptors.
249 1.1 drochner */
250 1.1 drochner for (bix = 0; bix < sc->sc_ntbuf; bix++) {
251 1.1 drochner a = sc->sc_addr + LE_TBUFADDR(sc, bix);
252 1.1 drochner tmd.tmd0 = a;
253 1.1 drochner tmd.tmd1 = LE_T1_ONES;
254 1.1 drochner tmd.tmd2 = 0;
255 1.1 drochner tmd.tmd3 = 0;
256 1.1 drochner (*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
257 1.1 drochner sizeof(tmd));
258 1.1 drochner }
259 1.1 drochner }
260 1.1 drochner
261 1.14 thorpej static __inline void
262 1.14 thorpej am79900_rint(struct lance_softc *sc)
263 1.1 drochner {
264 1.5 augustss int bix;
265 1.1 drochner int rp;
266 1.1 drochner struct lermd rmd;
267 1.1 drochner
268 1.1 drochner bix = sc->sc_last_rd;
269 1.1 drochner
270 1.1 drochner /* Process all buffers with valid data. */
271 1.1 drochner for (;;) {
272 1.1 drochner rp = LE_RMDADDR(sc, bix);
273 1.1 drochner (*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
274 1.1 drochner
275 1.1 drochner if (rmd.rmd1 & LE_R1_OWN)
276 1.1 drochner break;
277 1.1 drochner
278 1.1 drochner if (rmd.rmd1 & LE_R1_ERR) {
279 1.1 drochner if (rmd.rmd1 & LE_R1_ENP) {
280 1.1 drochner #ifdef LEDEBUG
281 1.1 drochner if ((rmd.rmd1 & LE_R1_OFLO) == 0) {
282 1.1 drochner if (rmd.rmd1 & LE_R1_FRAM)
283 1.1 drochner printf("%s: framing error\n",
284 1.1 drochner sc->sc_dev.dv_xname);
285 1.1 drochner if (rmd.rmd1 & LE_R1_CRC)
286 1.1 drochner printf("%s: crc mismatch\n",
287 1.1 drochner sc->sc_dev.dv_xname);
288 1.1 drochner }
289 1.1 drochner #endif
290 1.1 drochner } else {
291 1.1 drochner if (rmd.rmd1 & LE_R1_OFLO)
292 1.1 drochner printf("%s: overflow\n",
293 1.1 drochner sc->sc_dev.dv_xname);
294 1.1 drochner }
295 1.1 drochner if (rmd.rmd1 & LE_R1_BUFF)
296 1.1 drochner printf("%s: receive buffer error\n",
297 1.1 drochner sc->sc_dev.dv_xname);
298 1.1 drochner ifp->if_ierrors++;
299 1.1 drochner } else if ((rmd.rmd1 & (LE_R1_STP | LE_R1_ENP)) !=
300 1.1 drochner (LE_R1_STP | LE_R1_ENP)) {
301 1.1 drochner printf("%s: dropping chained buffer\n",
302 1.1 drochner sc->sc_dev.dv_xname);
303 1.1 drochner ifp->if_ierrors++;
304 1.1 drochner } else {
305 1.1 drochner #ifdef LEDEBUG
306 1.1 drochner if (sc->sc_debug)
307 1.1 drochner am79900_recv_print(sc, sc->sc_last_rd);
308 1.1 drochner #endif
309 1.1 drochner lance_read(sc, LE_RBUFADDR(sc, bix),
310 1.1 drochner (rmd.rmd2 & 0xfff) - 4);
311 1.1 drochner }
312 1.1 drochner
313 1.1 drochner rmd.rmd1 = LE_R1_OWN | LE_R1_ONES | (-LEBLEN & 0xfff);
314 1.1 drochner rmd.rmd2 = 0;
315 1.1 drochner rmd.rmd3 = 0;
316 1.1 drochner (*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
317 1.1 drochner
318 1.1 drochner #ifdef LEDEBUG
319 1.1 drochner if (sc->sc_debug)
320 1.1 drochner printf("sc->sc_last_rd = %x, rmd: "
321 1.1 drochner "adr %08x, flags/blen %08x\n",
322 1.1 drochner sc->sc_last_rd,
323 1.1 drochner rmd.rmd0, rmd.rmd1);
324 1.1 drochner #endif
325 1.1 drochner
326 1.1 drochner if (++bix == sc->sc_nrbuf)
327 1.1 drochner bix = 0;
328 1.1 drochner }
329 1.1 drochner
330 1.1 drochner sc->sc_last_rd = bix;
331 1.1 drochner }
332 1.1 drochner
333 1.14 thorpej static __inline void
334 1.14 thorpej am79900_tint(struct lance_softc *sc)
335 1.1 drochner {
336 1.5 augustss int bix;
337 1.1 drochner struct letmd tmd;
338 1.1 drochner
339 1.1 drochner bix = sc->sc_first_td;
340 1.1 drochner
341 1.1 drochner for (;;) {
342 1.1 drochner if (sc->sc_no_td <= 0)
343 1.1 drochner break;
344 1.1 drochner
345 1.1 drochner (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
346 1.1 drochner sizeof(tmd));
347 1.1 drochner
348 1.1 drochner #ifdef LEDEBUG
349 1.1 drochner if (sc->sc_debug)
350 1.1 drochner printf("trans tmd: "
351 1.1 drochner "adr %08x, flags/blen %08x\n",
352 1.1 drochner tmd.tmd0, tmd.tmd1);
353 1.1 drochner #endif
354 1.1 drochner
355 1.1 drochner if (tmd.tmd1 & LE_T1_OWN)
356 1.1 drochner break;
357 1.1 drochner
358 1.1 drochner ifp->if_flags &= ~IFF_OACTIVE;
359 1.1 drochner
360 1.1 drochner if (tmd.tmd1 & LE_T1_ERR) {
361 1.1 drochner if (tmd.tmd2 & LE_T2_BUFF)
362 1.1 drochner printf("%s: transmit buffer error\n",
363 1.1 drochner sc->sc_dev.dv_xname);
364 1.1 drochner else if (tmd.tmd2 & LE_T2_UFLO)
365 1.1 drochner printf("%s: underflow\n", sc->sc_dev.dv_xname);
366 1.1 drochner if (tmd.tmd2 & (LE_T2_BUFF | LE_T2_UFLO)) {
367 1.1 drochner lance_reset(sc);
368 1.1 drochner return;
369 1.1 drochner }
370 1.1 drochner if (tmd.tmd2 & LE_T2_LCAR) {
371 1.1 drochner sc->sc_havecarrier = 0;
372 1.1 drochner if (sc->sc_nocarrier)
373 1.1 drochner (*sc->sc_nocarrier)(sc);
374 1.1 drochner else
375 1.1 drochner printf("%s: lost carrier\n",
376 1.1 drochner sc->sc_dev.dv_xname);
377 1.1 drochner }
378 1.1 drochner if (tmd.tmd2 & LE_T2_LCOL)
379 1.1 drochner ifp->if_collisions++;
380 1.1 drochner if (tmd.tmd2 & LE_T2_RTRY) {
381 1.4 thorpej #ifdef LEDEBUG
382 1.1 drochner printf("%s: excessive collisions\n",
383 1.1 drochner sc->sc_dev.dv_xname);
384 1.4 thorpej #endif
385 1.1 drochner ifp->if_collisions += 16;
386 1.1 drochner }
387 1.1 drochner ifp->if_oerrors++;
388 1.1 drochner } else {
389 1.1 drochner if (tmd.tmd1 & LE_T1_ONE)
390 1.1 drochner ifp->if_collisions++;
391 1.1 drochner else if (tmd.tmd1 & LE_T1_MORE)
392 1.1 drochner /* Real number is unknown. */
393 1.1 drochner ifp->if_collisions += 2;
394 1.1 drochner ifp->if_opackets++;
395 1.1 drochner }
396 1.1 drochner
397 1.1 drochner if (++bix == sc->sc_ntbuf)
398 1.1 drochner bix = 0;
399 1.1 drochner
400 1.1 drochner --sc->sc_no_td;
401 1.1 drochner }
402 1.1 drochner
403 1.1 drochner sc->sc_first_td = bix;
404 1.1 drochner
405 1.1 drochner am79900_start(ifp);
406 1.1 drochner
407 1.1 drochner if (sc->sc_no_td == 0)
408 1.1 drochner ifp->if_timer = 0;
409 1.1 drochner }
410 1.1 drochner
411 1.1 drochner /*
412 1.1 drochner * Controller interrupt.
413 1.1 drochner */
414 1.1 drochner int
415 1.14 thorpej am79900_intr(void *arg)
416 1.1 drochner {
417 1.5 augustss struct lance_softc *sc = arg;
418 1.5 augustss u_int16_t isr;
419 1.1 drochner
420 1.1 drochner isr = (*sc->sc_rdcsr)(sc, LE_CSR0) | sc->sc_saved_csr0;
421 1.1 drochner sc->sc_saved_csr0 = 0;
422 1.6 jdolecek #if defined(LEDEBUG) && LEDEBUG > 1
423 1.1 drochner if (sc->sc_debug)
424 1.1 drochner printf("%s: am79900_intr entering with isr=%04x\n",
425 1.1 drochner sc->sc_dev.dv_xname, isr);
426 1.1 drochner #endif
427 1.1 drochner if ((isr & LE_C0_INTR) == 0)
428 1.1 drochner return (0);
429 1.1 drochner
430 1.1 drochner (*sc->sc_wrcsr)(sc, LE_CSR0,
431 1.1 drochner isr & (LE_C0_INEA | LE_C0_BABL | LE_C0_MISS | LE_C0_MERR |
432 1.1 drochner LE_C0_RINT | LE_C0_TINT | LE_C0_IDON));
433 1.1 drochner if (isr & LE_C0_ERR) {
434 1.1 drochner if (isr & LE_C0_BABL) {
435 1.1 drochner #ifdef LEDEBUG
436 1.1 drochner printf("%s: babble\n", sc->sc_dev.dv_xname);
437 1.1 drochner #endif
438 1.1 drochner ifp->if_oerrors++;
439 1.1 drochner }
440 1.1 drochner #if 0
441 1.1 drochner if (isr & LE_C0_CERR) {
442 1.1 drochner printf("%s: collision error\n", sc->sc_dev.dv_xname);
443 1.1 drochner ifp->if_collisions++;
444 1.1 drochner }
445 1.1 drochner #endif
446 1.1 drochner if (isr & LE_C0_MISS) {
447 1.1 drochner #ifdef LEDEBUG
448 1.1 drochner printf("%s: missed packet\n", sc->sc_dev.dv_xname);
449 1.1 drochner #endif
450 1.1 drochner ifp->if_ierrors++;
451 1.1 drochner }
452 1.1 drochner if (isr & LE_C0_MERR) {
453 1.1 drochner printf("%s: memory error\n", sc->sc_dev.dv_xname);
454 1.1 drochner lance_reset(sc);
455 1.1 drochner return (1);
456 1.1 drochner }
457 1.1 drochner }
458 1.1 drochner
459 1.1 drochner if ((isr & LE_C0_RXON) == 0) {
460 1.1 drochner printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
461 1.1 drochner ifp->if_ierrors++;
462 1.1 drochner lance_reset(sc);
463 1.1 drochner return (1);
464 1.1 drochner }
465 1.1 drochner if ((isr & LE_C0_TXON) == 0) {
466 1.1 drochner printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
467 1.1 drochner ifp->if_oerrors++;
468 1.1 drochner lance_reset(sc);
469 1.1 drochner return (1);
470 1.1 drochner }
471 1.1 drochner
472 1.1 drochner /*
473 1.1 drochner * Pretend we have carrier; if we don't this will be cleared
474 1.1 drochner * shortly.
475 1.1 drochner */
476 1.1 drochner sc->sc_havecarrier = 1;
477 1.1 drochner
478 1.1 drochner if (isr & LE_C0_RINT)
479 1.1 drochner am79900_rint(sc);
480 1.1 drochner if (isr & LE_C0_TINT)
481 1.1 drochner am79900_tint(sc);
482 1.2 drochner
483 1.2 drochner #if NRND > 0
484 1.2 drochner rnd_add_uint32(&sc->rnd_source, isr);
485 1.2 drochner #endif
486 1.1 drochner
487 1.1 drochner return (1);
488 1.1 drochner }
489 1.1 drochner
490 1.1 drochner #undef ifp
491 1.1 drochner
492 1.1 drochner /*
493 1.1 drochner * Setup output on interface.
494 1.1 drochner * Get another datagram to send off of the interface queue, and map it to the
495 1.1 drochner * interface before starting the output.
496 1.3 mycroft * Called only at splnet or interrupt level.
497 1.1 drochner */
498 1.14 thorpej static void
499 1.14 thorpej am79900_start(struct ifnet *ifp)
500 1.1 drochner {
501 1.5 augustss struct lance_softc *sc = ifp->if_softc;
502 1.5 augustss int bix;
503 1.5 augustss struct mbuf *m;
504 1.1 drochner struct letmd tmd;
505 1.1 drochner int rp;
506 1.1 drochner int len;
507 1.1 drochner
508 1.1 drochner if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
509 1.1 drochner return;
510 1.1 drochner
511 1.1 drochner bix = sc->sc_last_td;
512 1.1 drochner
513 1.1 drochner for (;;) {
514 1.1 drochner rp = LE_TMDADDR(sc, bix);
515 1.1 drochner (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
516 1.1 drochner
517 1.1 drochner if (tmd.tmd1 & LE_T1_OWN) {
518 1.1 drochner ifp->if_flags |= IFF_OACTIVE;
519 1.1 drochner printf("missing buffer, no_td = %d, last_td = %d\n",
520 1.1 drochner sc->sc_no_td, sc->sc_last_td);
521 1.1 drochner }
522 1.1 drochner
523 1.8 thorpej IFQ_DEQUEUE(&ifp->if_snd, m);
524 1.1 drochner if (m == 0)
525 1.1 drochner break;
526 1.1 drochner
527 1.1 drochner #if NBPFILTER > 0
528 1.1 drochner /*
529 1.1 drochner * If BPF is listening on this interface, let it see the packet
530 1.1 drochner * before we commit it to the wire.
531 1.1 drochner */
532 1.1 drochner if (ifp->if_bpf)
533 1.1 drochner bpf_mtap(ifp->if_bpf, m);
534 1.1 drochner #endif
535 1.1 drochner
536 1.1 drochner /*
537 1.1 drochner * Copy the mbuf chain into the transmit buffer.
538 1.1 drochner */
539 1.1 drochner len = lance_put(sc, LE_TBUFADDR(sc, bix), m);
540 1.1 drochner
541 1.1 drochner #ifdef LEDEBUG
542 1.1 drochner if (len > ETHERMTU + sizeof(struct ether_header))
543 1.1 drochner printf("packet length %d\n", len);
544 1.1 drochner #endif
545 1.1 drochner
546 1.1 drochner ifp->if_timer = 5;
547 1.1 drochner
548 1.1 drochner /*
549 1.1 drochner * Init transmit registers, and set transmit start flag.
550 1.1 drochner */
551 1.1 drochner tmd.tmd1 = LE_T1_OWN | LE_T1_STP | LE_T1_ENP | LE_T1_ONES | (-len & 0xfff);
552 1.1 drochner tmd.tmd2 = 0;
553 1.1 drochner tmd.tmd3 = 0;
554 1.1 drochner
555 1.1 drochner (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
556 1.1 drochner
557 1.1 drochner #ifdef LEDEBUG
558 1.1 drochner if (sc->sc_debug)
559 1.1 drochner am79900_xmit_print(sc, sc->sc_last_td);
560 1.1 drochner #endif
561 1.1 drochner
562 1.1 drochner (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
563 1.1 drochner
564 1.1 drochner if (++bix == sc->sc_ntbuf)
565 1.1 drochner bix = 0;
566 1.1 drochner
567 1.1 drochner if (++sc->sc_no_td == sc->sc_ntbuf) {
568 1.1 drochner ifp->if_flags |= IFF_OACTIVE;
569 1.1 drochner break;
570 1.1 drochner }
571 1.1 drochner
572 1.1 drochner }
573 1.1 drochner
574 1.1 drochner sc->sc_last_td = bix;
575 1.1 drochner }
576 1.1 drochner
577 1.1 drochner #ifdef LEDEBUG
578 1.14 thorpej static void
579 1.14 thorpej am79900_recv_print(struct lance_softc *sc, int no)
580 1.1 drochner {
581 1.1 drochner struct lermd rmd;
582 1.1 drochner u_int16_t len;
583 1.1 drochner struct ether_header eh;
584 1.1 drochner
585 1.1 drochner (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
586 1.1 drochner len = (rmd.rmd2 & 0xfff) - 4;
587 1.1 drochner printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
588 1.1 drochner len);
589 1.1 drochner printf("%s: status %04x\n", sc->sc_dev.dv_xname,
590 1.1 drochner (*sc->sc_rdcsr)(sc, LE_CSR0));
591 1.1 drochner printf("%s: adr %08x, flags/blen %08x\n",
592 1.1 drochner sc->sc_dev.dv_xname, rmd.rmd0, rmd.rmd1);
593 1.1 drochner if (len >= sizeof(eh)) {
594 1.1 drochner (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
595 1.1 drochner printf("%s: dst %s", sc->sc_dev.dv_xname,
596 1.1 drochner ether_sprintf(eh.ether_dhost));
597 1.1 drochner printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
598 1.1 drochner ntohs(eh.ether_type));
599 1.1 drochner }
600 1.1 drochner }
601 1.1 drochner
602 1.14 thorpej static void
603 1.14 thorpej am79900_xmit_print(struct lance_softc *sc, int no)
604 1.1 drochner {
605 1.1 drochner struct letmd tmd;
606 1.1 drochner u_int16_t len;
607 1.1 drochner struct ether_header eh;
608 1.1 drochner
609 1.1 drochner (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
610 1.1 drochner len = -(tmd.tmd1 & 0xfff);
611 1.1 drochner printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
612 1.1 drochner len);
613 1.1 drochner printf("%s: status %04x\n", sc->sc_dev.dv_xname,
614 1.1 drochner (*sc->sc_rdcsr)(sc, LE_CSR0));
615 1.1 drochner printf("%s: adr %08x, flags/blen %08x\n",
616 1.1 drochner sc->sc_dev.dv_xname, tmd.tmd0, tmd.tmd1);
617 1.1 drochner if (len >= sizeof(eh)) {
618 1.1 drochner (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
619 1.1 drochner printf("%s: dst %s", sc->sc_dev.dv_xname,
620 1.1 drochner ether_sprintf(eh.ether_dhost));
621 1.1 drochner printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
622 1.1 drochner ntohs(eh.ether_type));
623 1.1 drochner }
624 1.1 drochner }
625 1.1 drochner #endif /* LEDEBUG */
626