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