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