if_le.c revision 1.6 1 1.6 drochner /* $NetBSD: if_le.c,v 1.6 2003/03/13 13:37:15 drochner Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1993 Adam Glass
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
16 1.1 thorpej * must display the following acknowledgement:
17 1.1 thorpej * This product includes software developed by Adam Glass.
18 1.1 thorpej * 4. The name of the Author may not be used to endorse or promote products
19 1.1 thorpej * derived from this software without specific prior written permission.
20 1.1 thorpej *
21 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
22 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 thorpej * SUCH DAMAGE.
32 1.1 thorpej */
33 1.1 thorpej
34 1.1 thorpej #include <sys/param.h>
35 1.1 thorpej #include <sys/types.h>
36 1.1 thorpej
37 1.4 drochner #include <net/if_ether.h>
38 1.1 thorpej #include <netinet/in.h>
39 1.1 thorpej #include <netinet/in_systm.h>
40 1.1 thorpej
41 1.1 thorpej #include <lib/libsa/netif.h>
42 1.1 thorpej
43 1.1 thorpej #include <hp300/stand/common/device.h>
44 1.1 thorpej #include <hp300/stand/common/if_lereg.h>
45 1.1 thorpej #include <hp300/stand/common/samachdep.h>
46 1.1 thorpej
47 1.1 thorpej #ifndef NLE
48 1.1 thorpej #define NLE 1
49 1.1 thorpej #endif
50 1.1 thorpej
51 1.1 thorpej #ifdef LE_DEBUG
52 1.1 thorpej int le_debug = 0;
53 1.1 thorpej #endif
54 1.1 thorpej
55 1.1 thorpej int le_probe();
56 1.1 thorpej int le_match();
57 1.1 thorpej void le_init();
58 1.1 thorpej int le_get();
59 1.1 thorpej int le_put();
60 1.1 thorpej void le_end();
61 1.1 thorpej
62 1.1 thorpej struct le_sel {
63 1.1 thorpej int le_id;
64 1.1 thorpej int le_regs;
65 1.1 thorpej int le_mem;
66 1.1 thorpej int le_nvram;
67 1.1 thorpej int le_heat;
68 1.1 thorpej int le_bonus;
69 1.1 thorpej } le0conf[] = {
70 1.1 thorpej /* offsets for: ID REGS MEM NVRAM le_heat le_bonus*/
71 1.1 thorpej { 0, 0x4000, 0x8000, 0xC008, 1, 10 }
72 1.1 thorpej };
73 1.6 drochner #define NLE0CONF (sizeof(le0conf) / sizeof(le0conf[0]))
74 1.1 thorpej
75 1.1 thorpej extern struct netif_stats le_stats[];
76 1.1 thorpej
77 1.1 thorpej struct netif_dif le_ifs[] = {
78 1.1 thorpej /* dif_unit dif_nsel dif_stats dif_private */
79 1.6 drochner { 0, NLE0CONF, &le_stats[0], le0conf, },
80 1.1 thorpej };
81 1.6 drochner #define NLE_IFS (sizeof(le_ifs) / sizeof(le_ifs[0]))
82 1.1 thorpej
83 1.6 drochner struct netif_stats le_stats[NLE_IFS];
84 1.1 thorpej
85 1.1 thorpej struct netif_driver le_driver = {
86 1.1 thorpej "le", /* netif_bname */
87 1.1 thorpej le_match, /* netif_match */
88 1.1 thorpej le_probe, /* netif_probe */
89 1.1 thorpej le_init, /* netif_init */
90 1.1 thorpej le_get, /* netif_get */
91 1.1 thorpej le_put, /* netif_put */
92 1.1 thorpej le_end, /* netif_end */
93 1.1 thorpej le_ifs, /* netif_ifs */
94 1.6 drochner NLE_IFS /* netif_nifs */
95 1.1 thorpej };
96 1.1 thorpej
97 1.1 thorpej struct le_softc {
98 1.1 thorpej struct lereg0 *sc_r0; /* DIO registers */
99 1.1 thorpej struct lereg1 *sc_r1; /* LANCE registers */
100 1.1 thorpej void *sc_mem;
101 1.1 thorpej struct init_block *sc_init;
102 1.1 thorpej struct mds *sc_rd, *sc_td;
103 1.1 thorpej u_char *sc_rbuf, *sc_tbuf;
104 1.1 thorpej int sc_next_rd, sc_next_td;
105 1.1 thorpej u_char sc_addr[ETHER_ADDR_LEN];
106 1.1 thorpej } le_softc[NLE];
107 1.1 thorpej
108 1.1 thorpej static inline void
109 1.1 thorpej lewrcsr(sc, port, val)
110 1.1 thorpej struct le_softc *sc;
111 1.1 thorpej register u_short port;
112 1.1 thorpej register u_short val;
113 1.1 thorpej {
114 1.1 thorpej register struct lereg0 *ler0 = sc->sc_r0;
115 1.1 thorpej register struct lereg1 *ler1 = sc->sc_r1;
116 1.1 thorpej
117 1.1 thorpej do {
118 1.1 thorpej ler1->ler1_rap = port;
119 1.1 thorpej } while ((ler0->ler0_status & LE_ACK) == 0);
120 1.1 thorpej do {
121 1.1 thorpej ler1->ler1_rdp = val;
122 1.1 thorpej } while ((ler0->ler0_status & LE_ACK) == 0);
123 1.1 thorpej }
124 1.1 thorpej
125 1.1 thorpej static inline u_short
126 1.1 thorpej lerdcsr(sc, port)
127 1.1 thorpej struct le_softc *sc;
128 1.1 thorpej register u_short port;
129 1.1 thorpej {
130 1.1 thorpej register struct lereg0 *ler0 = sc->sc_r0;
131 1.1 thorpej register struct lereg1 *ler1 = sc->sc_r1;
132 1.1 thorpej register u_short val;
133 1.1 thorpej
134 1.1 thorpej do {
135 1.1 thorpej ler1->ler1_rap = port;
136 1.1 thorpej } while ((ler0->ler0_status & LE_ACK) == 0);
137 1.1 thorpej do {
138 1.1 thorpej val = ler1->ler1_rdp;
139 1.1 thorpej } while ((ler0->ler0_status & LE_ACK) == 0);
140 1.1 thorpej return (val);
141 1.1 thorpej }
142 1.1 thorpej
143 1.1 thorpej leinit()
144 1.1 thorpej {
145 1.1 thorpej extern struct hp_hw sc_table[];
146 1.1 thorpej register struct hp_hw *hw;
147 1.1 thorpej struct le_softc *sc;
148 1.1 thorpej struct le_sel *sels;
149 1.1 thorpej register int i, n;
150 1.1 thorpej char *cp;
151 1.1 thorpej
152 1.1 thorpej i = 0;
153 1.1 thorpej
154 1.1 thorpej for (hw = sc_table; i < NLE && hw < &sc_table[MAXCTLRS]; hw++) {
155 1.1 thorpej #ifdef LE_DEBUG
156 1.1 thorpej if (le_debug)
157 1.1 thorpej printf("found type %x\n", hw->hw_type);
158 1.1 thorpej #endif
159 1.1 thorpej
160 1.1 thorpej #if 0
161 1.1 thorpej if (!HW_ISDEV(hw, D_LAN))
162 1.1 thorpej continue;
163 1.1 thorpej #endif
164 1.1 thorpej
165 1.1 thorpej sels = (struct le_sel *)le_ifs[i].dif_private;
166 1.1 thorpej
167 1.1 thorpej sc = &le_softc[i];
168 1.1 thorpej sc->sc_r0 = (struct lereg0 *)(sels->le_id + (int)hw->hw_kva);
169 1.1 thorpej
170 1.1 thorpej if (sc->sc_r0->ler0_id != LEID)
171 1.1 thorpej continue;
172 1.1 thorpej
173 1.1 thorpej sc->sc_r1 = (struct lereg1 *)(sels->le_regs + (int)hw->hw_kva);
174 1.1 thorpej sc->sc_mem = (struct lereg2 *)(sels->le_mem + (int)hw->hw_kva);
175 1.1 thorpej
176 1.1 thorpej #ifdef LE_DEBUG
177 1.1 thorpej if (le_debug)
178 1.1 thorpej printf("le%d: DIO=%x regs=%x mem=%x\n",
179 1.1 thorpej i, sc->sc_r0, sc->sc_r1, sc->sc_mem);
180 1.1 thorpej #endif
181 1.1 thorpej
182 1.1 thorpej /*
183 1.1 thorpej * Read the ethernet address off the board, one nibble at a time.
184 1.1 thorpej */
185 1.1 thorpej cp = (char *)(sels->le_nvram + (int)hw->hw_kva);
186 1.1 thorpej for (n = 0; n < sizeof(sc->sc_addr); n++) {
187 1.1 thorpej sc->sc_addr[n] = (*++cp & 0xF) << 4;
188 1.1 thorpej cp++;
189 1.1 thorpej sc->sc_addr[n] |= *++cp & 0xF;
190 1.1 thorpej cp++;
191 1.1 thorpej }
192 1.1 thorpej #ifdef LE_DEBUG
193 1.1 thorpej if (le_debug)
194 1.1 thorpej printf("le%d at sc%d physical address %s\n",
195 1.1 thorpej i, hw->hw_sc, ether_sprintf(sc->sc_addr));
196 1.1 thorpej #endif
197 1.1 thorpej hw->hw_pa = (caddr_t) i; /* XXX for autoconfig */
198 1.1 thorpej i++;
199 1.1 thorpej }
200 1.1 thorpej }
201 1.1 thorpej
202 1.1 thorpej int
203 1.1 thorpej le_match(nif, machdep_hint)
204 1.1 thorpej struct netif *nif;
205 1.1 thorpej void *machdep_hint;
206 1.1 thorpej {
207 1.1 thorpej struct le_sel *sels;
208 1.1 thorpej char *name = machdep_hint;
209 1.1 thorpej int rv = 0;
210 1.1 thorpej
211 1.1 thorpej if (nif->nif_sel < le_ifs[nif->nif_unit].dif_nsel) {
212 1.1 thorpej sels = (struct le_sel *)le_ifs[nif->nif_unit].dif_private;
213 1.1 thorpej rv = sels[nif->nif_sel].le_heat;
214 1.1 thorpej if (name && !strncmp(le_driver.netif_bname, name, 2))
215 1.1 thorpej rv += sels[nif->nif_sel].le_bonus;
216 1.1 thorpej }
217 1.1 thorpej #ifdef LE_DEBUG
218 1.1 thorpej if (le_debug)
219 1.1 thorpej printf("le%d: sel %d --> %d\n", nif->nif_unit, nif->nif_sel,
220 1.1 thorpej rv);
221 1.1 thorpej #endif
222 1.1 thorpej return rv;
223 1.1 thorpej }
224 1.1 thorpej
225 1.1 thorpej le_probe(nif, machdep_hint)
226 1.1 thorpej struct netif *nif;
227 1.1 thorpej void *machdep_hint;
228 1.1 thorpej {
229 1.1 thorpej char *cp;
230 1.1 thorpej int i;
231 1.1 thorpej
232 1.1 thorpej /* the set unit is the current unit */
233 1.1 thorpej #ifdef LE_DEBUG
234 1.1 thorpej if (le_debug)
235 1.1 thorpej printf("le%d.%d: le_probe called\n", nif->nif_unit, nif->nif_sel);
236 1.1 thorpej #endif
237 1.1 thorpej /* XXX reset controller */
238 1.1 thorpej return 0;
239 1.1 thorpej }
240 1.1 thorpej
241 1.1 thorpej #ifdef MEM_SUMMARY
242 1.1 thorpej void le_mem_summary(unit)
243 1.1 thorpej {
244 1.1 thorpej struct lereg1 *ler1 = le_softc.sc_r1;
245 1.1 thorpej struct lereg2 *ler2 = le_softc.sc_r2;
246 1.1 thorpej register int i;
247 1.1 thorpej
248 1.1 thorpej printf("le%d: ler1 = %x\n", unit, ler1);
249 1.1 thorpej printf("le%d: ler2 = %x\n", unit, ler2);
250 1.1 thorpej
251 1.1 thorpej #if 0
252 1.1 thorpej ler1->ler1_rap = LE_CSR0;
253 1.1 thorpej ler1->ler1_rdp = LE_STOP;
254 1.1 thorpej printf("le%d: csr0 = %x\n", unit, ler1->ler1_rdp);
255 1.1 thorpej ler1->ler1_rap = LE_CSR1;
256 1.1 thorpej printf("le%d: csr1 = %x\n", unit, ler1->ler1_rdp);
257 1.1 thorpej ler1->ler1_rap = LE_CSR2;
258 1.1 thorpej printf("le%d: csr2 = %x\n", unit, ler1->ler1_rdp);
259 1.1 thorpej ler1->ler1_rap = LE_CSR3;
260 1.1 thorpej printf("le%d: csr3 = %x\n", unit, ler1->ler1_rdp);
261 1.1 thorpej #endif
262 1.1 thorpej printf("le%d: ladrf[0] = %x\n", unit, ler2->ler2_ladrf[0]);
263 1.1 thorpej printf("le%d: ladrf[1] = %x\n", unit, ler2->ler2_ladrf[1]);
264 1.1 thorpej printf("le%d: ler2_rdra = %x\n", unit, ler2->ler2_rdra);
265 1.1 thorpej printf("le%d: ler2_rlen = %x\n", unit, ler2->ler2_rlen);
266 1.1 thorpej printf("le%d: ler2_tdra = %x\n", unit, ler2->ler2_tdra);
267 1.1 thorpej printf("le%d: ler2_tlen = %x\n", unit, ler2->ler2_tlen);
268 1.1 thorpej
269 1.1 thorpej for (i = 0; i < LERBUF; i++) {
270 1.1 thorpej printf("le%d: ler2_rmd[%d].rmd0 (ladr) = %x\n", unit, i,
271 1.1 thorpej ler2->ler2_rmd[i].rmd0);
272 1.1 thorpej printf("le%d: ler2_rmd[%d].rmd1 = %x\n", unit, i,
273 1.1 thorpej ler2->ler2_rmd[i].rmd1);
274 1.1 thorpej printf("le%d: ler2_rmd[%d].rmd2 (-bcnt) = %x\n", unit, i,
275 1.1 thorpej ler2->ler2_rmd[i].rmd2);
276 1.1 thorpej printf("le%d: ler2_rmd[%d].rmd3 (mcnt) = %x\n", unit, i,
277 1.1 thorpej ler2->ler2_rmd[i].rmd3);
278 1.1 thorpej printf("le%d: ler2_rbuf[%d] addr = %x\n", unit, i,
279 1.1 thorpej &ler2->ler2_rbuf[i]);
280 1.1 thorpej }
281 1.1 thorpej for (i = 0; i < LETBUF; i++) {
282 1.1 thorpej printf("le%d: ler2_tmd[%d].tmd0 = %x\n", unit, i,
283 1.1 thorpej ler2->ler2_tmd[i].tmd0);
284 1.1 thorpej printf("le%d: ler2_tmd[%d].tmd1 = %x\n", unit, i,
285 1.1 thorpej ler2->ler2_tmd[i].tmd1);
286 1.1 thorpej printf("le%d: ler2_tmd[%d].tmd2 (bcnt) = %x\n", unit, i,
287 1.1 thorpej ler2->ler2_tmd[i].tmd2);
288 1.1 thorpej printf("le%d: ler2_tmd[%d].tmd3 = %x\n", unit, i,
289 1.1 thorpej ler2->ler2_tmd[i].tmd3);
290 1.1 thorpej printf("le%d: ler2_tbuf[%d] addr = %x\n", unit, i,
291 1.1 thorpej &ler2->ler2_tbuf[i]);
292 1.1 thorpej }
293 1.1 thorpej }
294 1.1 thorpej #else
295 1.1 thorpej #define le_mem_summary(u)
296 1.1 thorpej #endif
297 1.1 thorpej
298 1.1 thorpej void
299 1.1 thorpej le_error(unit, str, stat)
300 1.1 thorpej int unit;
301 1.1 thorpej char *str;
302 1.1 thorpej u_short stat;
303 1.1 thorpej {
304 1.1 thorpej
305 1.1 thorpej if (stat & LE_BABL)
306 1.5 provos panic("le%d: been babbling, found by '%s'", unit, str);
307 1.1 thorpej if (stat & LE_CERR)
308 1.1 thorpej le_stats[unit].collision_error++;
309 1.1 thorpej if (stat & LE_MISS)
310 1.1 thorpej le_stats[unit].missed++;
311 1.1 thorpej if (stat & LE_MERR) {
312 1.1 thorpej printf("le%d: memory error in '%s'\n", unit, str);
313 1.1 thorpej le_mem_summary(unit);
314 1.1 thorpej panic("bye");
315 1.1 thorpej }
316 1.1 thorpej }
317 1.1 thorpej
318 1.1 thorpej #define LANCE_ADDR(sc, a) \
319 1.1 thorpej ((u_long)(a) - (u_long)sc->sc_mem)
320 1.1 thorpej
321 1.1 thorpej /* LANCE initialization block set up. */
322 1.1 thorpej void
323 1.1 thorpej lememinit(sc)
324 1.1 thorpej register struct le_softc *sc;
325 1.1 thorpej {
326 1.1 thorpej int i;
327 1.1 thorpej void *mem;
328 1.1 thorpej u_long a;
329 1.1 thorpej
330 1.1 thorpej /*
331 1.1 thorpej * At this point we assume that the memory allocated to the Lance is
332 1.1 thorpej * quadword aligned. If it isn't then the initialisation is going
333 1.1 thorpej * fail later on.
334 1.1 thorpej */
335 1.1 thorpej mem = sc->sc_mem;
336 1.1 thorpej
337 1.1 thorpej sc->sc_init = mem;
338 1.1 thorpej sc->sc_init->mode = LE_NORMAL;
339 1.1 thorpej for (i = 0; i < ETHER_ADDR_LEN; i++)
340 1.1 thorpej sc->sc_init->padr[i] = sc->sc_addr[i^1];
341 1.1 thorpej sc->sc_init->ladrf[0] = sc->sc_init->ladrf[1] = 0;
342 1.1 thorpej mem += sizeof(struct init_block);
343 1.1 thorpej
344 1.1 thorpej sc->sc_rd = mem;
345 1.1 thorpej a = LANCE_ADDR(sc, mem);
346 1.1 thorpej sc->sc_init->rdra = a;
347 1.1 thorpej sc->sc_init->rlen = ((a >> 16) & 0xff) | (RLEN << 13);
348 1.1 thorpej mem += NRBUF * sizeof(struct mds);
349 1.1 thorpej
350 1.1 thorpej sc->sc_td = mem;
351 1.1 thorpej a = LANCE_ADDR(sc, mem);
352 1.1 thorpej sc->sc_init->tdra = a;
353 1.1 thorpej sc->sc_init->tlen = ((a >> 16) & 0xff) | (TLEN << 13);
354 1.1 thorpej mem += NTBUF * sizeof(struct mds);
355 1.1 thorpej
356 1.1 thorpej /*
357 1.1 thorpej * Set up receive ring descriptors.
358 1.1 thorpej */
359 1.1 thorpej sc->sc_rbuf = mem;
360 1.1 thorpej for (i = 0; i < NRBUF; i++) {
361 1.1 thorpej a = LANCE_ADDR(sc, mem);
362 1.1 thorpej sc->sc_rd[i].addr = a;
363 1.1 thorpej sc->sc_rd[i].flags = ((a >> 16) & 0xff) | LE_OWN;
364 1.1 thorpej sc->sc_rd[i].bcnt = -BUFSIZE;
365 1.1 thorpej sc->sc_rd[i].mcnt = 0;
366 1.1 thorpej mem += BUFSIZE;
367 1.1 thorpej }
368 1.1 thorpej
369 1.1 thorpej /*
370 1.1 thorpej * Set up transmit ring descriptors.
371 1.1 thorpej */
372 1.1 thorpej sc->sc_tbuf = mem;
373 1.1 thorpej for (i = 0; i < NTBUF; i++) {
374 1.1 thorpej a = LANCE_ADDR(sc, mem);
375 1.1 thorpej sc->sc_td[i].addr = a;
376 1.1 thorpej sc->sc_td[i].flags = ((a >> 16) & 0xff);
377 1.1 thorpej sc->sc_td[i].bcnt = 0xf000;
378 1.1 thorpej sc->sc_td[i].mcnt = 0;
379 1.1 thorpej mem += BUFSIZE;
380 1.1 thorpej }
381 1.1 thorpej }
382 1.1 thorpej
383 1.1 thorpej void
384 1.1 thorpej le_reset(unit, myea)
385 1.1 thorpej int unit;
386 1.1 thorpej u_char *myea;
387 1.1 thorpej {
388 1.1 thorpej struct le_softc *sc = &le_softc[unit];
389 1.1 thorpej u_long a;
390 1.1 thorpej int timo = 100000, stat, i;
391 1.1 thorpej
392 1.1 thorpej #ifdef LE_DEBUG
393 1.1 thorpej if (le_debug) {
394 1.1 thorpej printf("le%d: le_reset called\n", unit);
395 1.1 thorpej printf(" r0=%x, r1=%x, mem=%x, addr=%x:%x:%x:%x:%x:%x\n",
396 1.1 thorpej sc->sc_r0, sc->sc_r1, sc->sc_mem,
397 1.1 thorpej sc->sc_addr[0], sc->sc_addr[1], sc->sc_addr[2],
398 1.1 thorpej sc->sc_addr[3], sc->sc_addr[4], sc->sc_addr[5]);
399 1.1 thorpej }
400 1.1 thorpej #endif
401 1.1 thorpej lewrcsr(sc, 0, LE_STOP);
402 1.1 thorpej for (timo = 1000; timo; timo--);
403 1.1 thorpej
404 1.1 thorpej sc->sc_next_rd = sc->sc_next_td = 0;
405 1.1 thorpej
406 1.1 thorpej /* Set up LANCE init block. */
407 1.1 thorpej lememinit(sc);
408 1.1 thorpej
409 1.1 thorpej if (myea)
410 1.1 thorpej bcopy(sc->sc_addr, myea, ETHER_ADDR_LEN);
411 1.1 thorpej
412 1.1 thorpej /* Turn on byte swapping. */
413 1.1 thorpej lewrcsr(sc, 3, LE_BSWP);
414 1.1 thorpej
415 1.1 thorpej /* Give LANCE the physical address of its init block. */
416 1.1 thorpej a = LANCE_ADDR(sc, sc->sc_init);
417 1.1 thorpej lewrcsr(sc, 1, a);
418 1.1 thorpej lewrcsr(sc, 2, (a >> 16) & 0xff);
419 1.1 thorpej
420 1.1 thorpej #ifdef LE_DEBUG
421 1.1 thorpej if (le_debug)
422 1.1 thorpej printf("le%d: before init\n", unit);
423 1.1 thorpej #endif
424 1.1 thorpej
425 1.1 thorpej /* Try to initialize the LANCE. */
426 1.1 thorpej lewrcsr(sc, 0, LE_INIT);
427 1.1 thorpej
428 1.1 thorpej /* Wait for initialization to finish. */
429 1.1 thorpej for (timo = 100000; timo; timo--)
430 1.1 thorpej if (lerdcsr(sc, 0) & LE_IDON)
431 1.1 thorpej break;
432 1.1 thorpej
433 1.1 thorpej if (lerdcsr(sc, 0) & LE_IDON) {
434 1.1 thorpej /* Start the LANCE. */
435 1.1 thorpej lewrcsr(sc, 0, LE_INEA | LE_STRT | LE_IDON);
436 1.1 thorpej } else
437 1.1 thorpej printf("le%d: card failed to initialize\n", unit);
438 1.1 thorpej
439 1.1 thorpej #ifdef LE_DEBUG
440 1.1 thorpej if (le_debug)
441 1.1 thorpej printf("le%d: after init\n", unit);
442 1.1 thorpej #endif
443 1.1 thorpej
444 1.1 thorpej le_mem_summary(unit);
445 1.1 thorpej }
446 1.1 thorpej
447 1.1 thorpej int
448 1.1 thorpej le_poll(desc, pkt, len)
449 1.1 thorpej struct iodesc *desc;
450 1.1 thorpej void *pkt;
451 1.1 thorpej int len;
452 1.1 thorpej {
453 1.1 thorpej struct netif *nif = desc->io_netif;
454 1.1 thorpej int unit = /*nif->nif_unit*/0;
455 1.1 thorpej struct le_softc *sc = &le_softc[unit];
456 1.1 thorpej volatile struct lereg0 *ler0 = sc->sc_r0;
457 1.1 thorpej volatile struct lereg1 *ler1 = sc->sc_r1;
458 1.1 thorpej int length;
459 1.1 thorpej volatile struct mds *cdm;
460 1.1 thorpej register int stat;
461 1.1 thorpej
462 1.1 thorpej #ifdef LE_DEBUG
463 1.1 thorpej if (/*le_debug*/0)
464 1.1 thorpej printf("le%d: le_poll called. next_rd=%d\n", unit, sc->sc_next_rd);
465 1.1 thorpej #endif
466 1.1 thorpej stat = lerdcsr(sc, 0);
467 1.1 thorpej lewrcsr(sc, 0, stat & (LE_BABL | LE_MISS | LE_MERR | LE_RINT));
468 1.1 thorpej cdm = &sc->sc_rd[sc->sc_next_rd];
469 1.1 thorpej if (cdm->flags & LE_OWN)
470 1.1 thorpej return 0;
471 1.1 thorpej #ifdef LE_DEBUG
472 1.1 thorpej if (le_debug) {
473 1.1 thorpej printf("next_rd %d\n", sc->sc_next_rd);
474 1.1 thorpej printf("cdm->flags %x\n", cdm->flags);
475 1.1 thorpej printf("cdm->bcnt %x, cdm->mcnt %x\n", cdm->bcnt, cdm->mcnt);
476 1.1 thorpej printf("cdm->rbuf msg %d buf %d\n", cdm->mcnt, -cdm->bcnt );
477 1.1 thorpej }
478 1.1 thorpej #endif
479 1.1 thorpej if (stat & (LE_BABL | LE_CERR | LE_MISS | LE_MERR))
480 1.1 thorpej le_error(unit, "le_poll", stat);
481 1.1 thorpej if (cdm->flags & (LE_FRAM | LE_OFLO | LE_CRC | LE_RBUFF)) {
482 1.1 thorpej printf("le%d_poll: rmd status 0x%x\n", unit, cdm->flags);
483 1.1 thorpej length = 0;
484 1.1 thorpej goto cleanup;
485 1.1 thorpej }
486 1.1 thorpej if ((cdm->flags & (LE_STP|LE_ENP)) != (LE_STP|LE_ENP))
487 1.5 provos panic("le_poll: chained packet");
488 1.1 thorpej
489 1.1 thorpej length = cdm->mcnt;
490 1.1 thorpej #ifdef LE_DEBUG
491 1.1 thorpej if (le_debug)
492 1.1 thorpej printf("le_poll: length %d\n", length);
493 1.1 thorpej #endif
494 1.1 thorpej if (length >= BUFSIZE) {
495 1.1 thorpej length = 0;
496 1.5 provos panic("csr0 when bad things happen: %x", stat);
497 1.1 thorpej goto cleanup;
498 1.1 thorpej }
499 1.1 thorpej if (!length)
500 1.1 thorpej goto cleanup;
501 1.1 thorpej length -= 4;
502 1.1 thorpej
503 1.1 thorpej if (length > 0) {
504 1.1 thorpej /*
505 1.1 thorpej * If the length of the packet is greater than the size of the
506 1.1 thorpej * buffer, we have to truncate it, to avoid Bad Things.
507 1.1 thorpej * XXX Is this the right thing to do?
508 1.1 thorpej */
509 1.1 thorpej if (length > len)
510 1.1 thorpej length = len;
511 1.1 thorpej
512 1.1 thorpej bcopy(sc->sc_rbuf + (BUFSIZE * sc->sc_next_rd), pkt, length);
513 1.1 thorpej }
514 1.1 thorpej
515 1.1 thorpej cleanup:
516 1.1 thorpej cdm->mcnt = 0;
517 1.1 thorpej cdm->flags |= LE_OWN;
518 1.1 thorpej if (++sc->sc_next_rd >= NRBUF)
519 1.1 thorpej sc->sc_next_rd = 0;
520 1.1 thorpej #ifdef LE_DEBUG
521 1.1 thorpej if (le_debug)
522 1.1 thorpej printf("new next_rd %d\n", sc->sc_next_rd);
523 1.1 thorpej #endif
524 1.1 thorpej
525 1.1 thorpej return length;
526 1.1 thorpej }
527 1.1 thorpej
528 1.1 thorpej int
529 1.1 thorpej le_put(desc, pkt, len)
530 1.1 thorpej struct iodesc *desc;
531 1.1 thorpej void *pkt;
532 1.1 thorpej int len;
533 1.1 thorpej {
534 1.1 thorpej struct netif *nif = desc->io_netif;
535 1.1 thorpej int unit = /*nif->nif_unit*/0;
536 1.1 thorpej struct le_softc *sc = &le_softc[unit];
537 1.1 thorpej volatile struct lereg0 *ler0 = sc->sc_r0;
538 1.1 thorpej volatile struct lereg1 *ler1 = sc->sc_r1;
539 1.1 thorpej volatile struct mds *cdm;
540 1.1 thorpej int timo, i, stat;
541 1.1 thorpej
542 1.1 thorpej le_put_loop:
543 1.1 thorpej timo = 100000;
544 1.1 thorpej
545 1.1 thorpej #ifdef LE_DEBUG
546 1.1 thorpej if (le_debug)
547 1.1 thorpej printf("le%d: le_put called. next_td=%d\n", unit, sc->sc_next_td);
548 1.1 thorpej #endif
549 1.1 thorpej stat = lerdcsr(sc, 0);
550 1.1 thorpej lewrcsr(sc, 0, stat & (LE_BABL | LE_MISS | LE_MERR | LE_TINT));
551 1.1 thorpej if (stat & (LE_BABL | LE_CERR | LE_MISS | LE_MERR))
552 1.1 thorpej le_error(unit, "le_put(way before xmit)", stat);
553 1.1 thorpej cdm = &sc->sc_td[sc->sc_next_td];
554 1.1 thorpej i = 0;
555 1.1 thorpej #if 0
556 1.1 thorpej while (cdm->flags & LE_OWN) {
557 1.1 thorpej if ((i % 100) == 0)
558 1.1 thorpej printf("le%d: output buffer busy - flags=%x\n",
559 1.1 thorpej unit, cdm->flags);
560 1.1 thorpej if (i++ > 500) break;
561 1.1 thorpej }
562 1.1 thorpej if (cdm->flags & LE_OWN)
563 1.1 thorpej getchar();
564 1.1 thorpej #else
565 1.1 thorpej while (cdm->flags & LE_OWN);
566 1.1 thorpej #endif
567 1.1 thorpej bcopy(pkt, sc->sc_tbuf + (BUFSIZE * sc->sc_next_td), len);
568 1.1 thorpej if (len < ETHER_MIN_LEN)
569 1.1 thorpej cdm->bcnt = -ETHER_MIN_LEN;
570 1.1 thorpej else
571 1.1 thorpej cdm->bcnt = -len;
572 1.1 thorpej cdm->mcnt = 0;
573 1.1 thorpej cdm->flags |= LE_OWN | LE_STP | LE_ENP;
574 1.1 thorpej stat = lerdcsr(sc, 0);
575 1.1 thorpej if (stat & (LE_BABL | LE_CERR | LE_MISS | LE_MERR))
576 1.1 thorpej le_error(unit, "le_put(before xmit)", stat);
577 1.1 thorpej lewrcsr(sc, 0, LE_TDMD);
578 1.1 thorpej stat = lerdcsr(sc, 0);
579 1.1 thorpej if (stat & (LE_BABL | LE_CERR | LE_MISS | LE_MERR))
580 1.1 thorpej le_error(unit, "le_put(after xmit)", stat);
581 1.1 thorpej do {
582 1.1 thorpej if (--timo == 0) {
583 1.1 thorpej printf("le%d: transmit timeout, stat = 0x%x\n",
584 1.1 thorpej unit, stat);
585 1.1 thorpej if (stat & LE_SERR)
586 1.1 thorpej le_error(unit, "le_put(timeout)", stat);
587 1.1 thorpej if (stat & LE_INIT) {
588 1.1 thorpej printf("le%d: reset and retry packet\n");
589 1.1 thorpej lewrcsr(sc, 0, LE_TINT); /* sanity */
590 1.1 thorpej le_init();
591 1.1 thorpej goto le_put_loop;
592 1.1 thorpej }
593 1.1 thorpej break;
594 1.1 thorpej }
595 1.1 thorpej stat = lerdcsr(sc, 0);
596 1.1 thorpej } while ((stat & LE_TINT) == 0);
597 1.1 thorpej lewrcsr(sc, 0, LE_TINT);
598 1.1 thorpej if (stat & (LE_BABL |/* LE_CERR |*/ LE_MISS | LE_MERR)) {
599 1.1 thorpej printf("le_put: xmit error, buf %d\n", sc->sc_next_td);
600 1.1 thorpej le_error(unit, "le_put(xmit error)", stat);
601 1.1 thorpej }
602 1.1 thorpej if (++sc->sc_next_td >= NTBUF)
603 1.1 thorpej sc->sc_next_td = 0;
604 1.1 thorpej if (cdm->flags & LE_DEF)
605 1.1 thorpej le_stats[unit].deferred++;
606 1.1 thorpej if (cdm->flags & LE_ONE)
607 1.1 thorpej le_stats[unit].collisions++;
608 1.1 thorpej if (cdm->flags & LE_MORE)
609 1.2 scottr le_stats[unit].collisions += 2;
610 1.1 thorpej if (cdm->flags & LE_ERR) {
611 1.2 scottr if (cdm->mcnt & LE_UFLO)
612 1.2 scottr printf("le%d: transmit underflow\n", unit);
613 1.2 scottr if (cdm->mcnt & LE_LCOL)
614 1.2 scottr le_stats[unit].collisions++;
615 1.2 scottr if (cdm->mcnt & LE_LCAR)
616 1.2 scottr printf("le%d: lost carrier\n", unit);
617 1.2 scottr if (cdm->mcnt & LE_RTRY)
618 1.2 scottr le_stats[unit].collisions += 16;
619 1.1 thorpej return -1;
620 1.1 thorpej }
621 1.1 thorpej #ifdef LE_DEBUG
622 1.1 thorpej if (le_debug) {
623 1.1 thorpej printf("le%d: le_put() successful: sent %d\n", unit, len);
624 1.1 thorpej printf("le%d: le_put(): flags: %x mcnt: %x\n", unit,
625 1.1 thorpej (unsigned int) cdm->flags,
626 1.1 thorpej (unsigned int) cdm->mcnt);
627 1.1 thorpej }
628 1.1 thorpej #endif
629 1.1 thorpej return len;
630 1.1 thorpej }
631 1.1 thorpej
632 1.1 thorpej
633 1.1 thorpej int
634 1.1 thorpej le_get(desc, pkt, len, timeout)
635 1.1 thorpej struct iodesc *desc;
636 1.1 thorpej void *pkt;
637 1.1 thorpej int len;
638 1.1 thorpej time_t timeout;
639 1.1 thorpej {
640 1.1 thorpej time_t t;
641 1.1 thorpej int cc;
642 1.1 thorpej
643 1.1 thorpej t = getsecs();
644 1.1 thorpej cc = 0;
645 1.1 thorpej while (((getsecs() - t) < timeout) && !cc) {
646 1.1 thorpej cc = le_poll(desc, pkt, len);
647 1.1 thorpej }
648 1.1 thorpej return cc;
649 1.1 thorpej }
650 1.1 thorpej
651 1.1 thorpej void
652 1.1 thorpej le_init(desc, machdep_hint)
653 1.1 thorpej struct iodesc *desc;
654 1.1 thorpej void *machdep_hint;
655 1.1 thorpej {
656 1.1 thorpej struct netif *nif = desc->io_netif;
657 1.1 thorpej int unit = nif->nif_unit;
658 1.1 thorpej
659 1.1 thorpej /* Get machine's common ethernet interface. This is done in leinit() */
660 1.1 thorpej /* machdep_common_ether(myea); */
661 1.1 thorpej leinit();
662 1.1 thorpej
663 1.1 thorpej #ifdef LE_DEBUG
664 1.1 thorpej if (le_debug)
665 1.1 thorpej printf("le%d: le_init called\n", unit);
666 1.1 thorpej #endif
667 1.1 thorpej unit = 0;
668 1.1 thorpej le_reset(unit, desc->myea);
669 1.1 thorpej }
670 1.1 thorpej
671 1.1 thorpej void
672 1.1 thorpej le_end(nif)
673 1.1 thorpej struct netif *nif;
674 1.1 thorpej {
675 1.1 thorpej int unit = nif->nif_unit;
676 1.1 thorpej
677 1.1 thorpej #ifdef LE_DEBUG
678 1.1 thorpej if (le_debug)
679 1.1 thorpej printf("le%d: le_end called\n", unit);
680 1.1 thorpej #endif
681 1.1 thorpej
682 1.1 thorpej lewrcsr(&le_softc[unit], 0, LE_STOP);
683 1.1 thorpej }
684