if_le.c revision 1.6.2.3 1 /* $NetBSD: if_le.c,v 1.6.2.3 2004/09/21 13:19:17 skrll Exp $ */
2
3 /*
4 * Copyright (c) 1995 Theo de Raadt
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * Copyright (c) 1993 Adam Glass
28 * All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by Adam Glass.
41 * 4. The name of the Author may not be used to endorse or promote products
42 * derived from this software without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57 #include <sys/param.h>
58 #include <sys/types.h>
59
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62
63 #include <machine/prom.h>
64
65 #include <lib/libkern/libkern.h>
66 #include <lib/libsa/stand.h>
67 #include <lib/libsa/net.h>
68
69 #include "libsa.h"
70 #include "netif.h"
71 #include "config.h"
72 #include "dev_net.h"
73
74 #include "if_lereg.h"
75
76 int le_debug = 0;
77
78 void le_end __P((struct netif *));
79 void le_error __P((struct netif *, char *, volatile struct lereg1 *));
80 int le_get __P((struct iodesc *, void *, size_t, time_t));
81 void le_init __P((struct iodesc *, void *));
82 int le_match __P((struct netif *, void *));
83 int le_poll __P((struct iodesc *, void *, int));
84 int le_probe __P((struct netif *, void *));
85 int le_put __P((struct iodesc *, void *, size_t));
86 void le_reset __P((struct netif *, u_char *));
87
88 struct netif_stats le_stats;
89
90 struct netif_dif le0_dif = {
91 0, /* unit */
92 1, /* nsel */
93 &le_stats,
94 0,
95 0,
96 };
97
98 struct netif_driver le_driver = {
99 "le", /* netif_bname */
100 le_match, /* match */
101 le_probe, /* probe */
102 le_init, /* init */
103 le_get, /* get */
104 le_put, /* put */
105 le_end, /* end */
106 &le0_dif, /* netif_ifs */
107 1, /* netif_nifs */
108 };
109
110 struct le_configuration {
111 unsigned int phys_addr;
112 int used;
113 } le_config[] = {
114 { LANCE_REG_ADDR, 0 }
115 };
116
117 int nle_config = sizeof(le_config) / (sizeof(le_config[0]));
118
119 struct {
120 struct lereg1 *sc_r1; /* LANCE registers */
121 struct lereg2 *sc_r2; /* RAM */
122 int next_rmd;
123 int next_tmd;
124 } le_softc;
125
126 int
127 le_match(nif, machdep_hint)
128 struct netif *nif;
129 void *machdep_hint;
130 {
131 char *name;
132 int i, val = 0;
133
134 if (bugargs.cputyp != CPU_147)
135 return (0);
136 name = machdep_hint;
137 if (name && !memcmp(le_driver.netif_bname, name, 2))
138 val += 10;
139 for (i = 0; i < nle_config; i++) {
140 if (le_config[i].used)
141 continue;
142 if (le_debug)
143 printf("le%d: le_match --> %d\n", i, val + 1);
144 le_config[i].used++;
145 return val + 1;
146 }
147 if (le_debug)
148 printf("le%d: le_match --> 0\n", i);
149 return 0;
150 }
151
152 int
153 le_probe(nif, machdep_hint)
154 struct netif *nif;
155 void *machdep_hint;
156 {
157
158 /* the set unit is the current unit */
159 if (le_debug)
160 printf("le%d: le_probe called\n", nif->nif_unit);
161
162 if (bugargs.cputyp == CPU_147)
163 return 0;
164 return 1;
165 }
166
167 void
168 le_error(nif, str, ler1)
169 struct netif *nif;
170 char *str;
171 volatile struct lereg1 *ler1;
172 {
173 /* ler1->ler1_rap = LE_CSRO done in caller */
174 if (ler1->ler1_rdp & LE_C0_BABL)
175 panic("le%d: been babbling, found by '%s'", nif->nif_unit, str);
176 if (ler1->ler1_rdp & LE_C0_CERR) {
177 le_stats.collision_error++;
178 ler1->ler1_rdp = LE_C0_CERR;
179 }
180 if (ler1->ler1_rdp & LE_C0_MISS) {
181 le_stats.missed++;
182 ler1->ler1_rdp = LE_C0_MISS;
183 }
184 if (ler1->ler1_rdp & LE_C0_MERR) {
185 printf("le%d: memory error in '%s'\n", nif->nif_unit, str);
186 panic("memory error");
187 }
188 }
189
190 void
191 le_reset(nif, myea)
192 struct netif *nif;
193 u_char *myea;
194 {
195 struct lereg1 *ler1 = le_softc.sc_r1;
196 struct lereg2 *ler2 = le_softc.sc_r2;
197 unsigned int a;
198 int timo = 100000, stat = 0, i;
199
200 if (le_debug)
201 printf("le%d: le_reset called\n", nif->nif_unit);
202 ler1->ler1_rap = LE_CSR0;
203 ler1->ler1_rdp = LE_C0_STOP; /* do nothing until we are finished */
204
205 memset(ler2, 0, sizeof(*ler2));
206
207 ler2->ler2_mode = LE_MODE_NORMAL;
208 ler2->ler2_padr[0] = myea[1];
209 ler2->ler2_padr[1] = myea[0];
210 ler2->ler2_padr[2] = myea[3];
211 ler2->ler2_padr[3] = myea[2];
212 ler2->ler2_padr[4] = myea[5];
213 ler2->ler2_padr[5] = myea[4];
214
215
216 ler2->ler2_ladrf0 = 0;
217 ler2->ler2_ladrf1 = 0;
218
219 a = (u_int) ler2->ler2_rmd;
220 ler2->ler2_rlen = LE_RLEN | (a >> 16);
221 ler2->ler2_rdra = a & LE_ADDR_LOW_MASK;
222
223 a = (u_int) ler2->ler2_tmd;
224 ler2->ler2_tlen = LE_TLEN | (a >> 16);
225 ler2->ler2_tdra = a & LE_ADDR_LOW_MASK;
226
227 ler1->ler1_rap = LE_CSR1;
228 a = (u_int) ler2;
229 ler1->ler1_rdp = a & LE_ADDR_LOW_MASK;
230 ler1->ler1_rap = LE_CSR2;
231 ler1->ler1_rdp = a >> 16;
232
233 for (i = 0; i < LERBUF; i++) {
234 a = (u_int) & ler2->ler2_rbuf[i];
235 ler2->ler2_rmd[i].rmd0 = a & LE_ADDR_LOW_MASK;
236 ler2->ler2_rmd[i].rmd1_bits = LE_R1_OWN;
237 ler2->ler2_rmd[i].rmd1_hadr = a >> 16;
238 ler2->ler2_rmd[i].rmd2 = -LEMTU;
239 ler2->ler2_rmd[i].rmd3 = 0;
240 }
241 for (i = 0; i < LETBUF; i++) {
242 a = (u_int) & ler2->ler2_tbuf[i];
243 ler2->ler2_tmd[i].tmd0 = a & LE_ADDR_LOW_MASK;
244 ler2->ler2_tmd[i].tmd1_bits = 0;
245 ler2->ler2_tmd[i].tmd1_hadr = a >> 16;
246 ler2->ler2_tmd[i].tmd2 = 0;
247 ler2->ler2_tmd[i].tmd3 = 0;
248 }
249
250 ler1->ler1_rap = LE_CSR3;
251 ler1->ler1_rdp = LE_C3_BSWP;
252
253 ler1->ler1_rap = LE_CSR0;
254 ler1->ler1_rdp = LE_C0_INIT;
255 do {
256 if (--timo == 0) {
257 printf("le%d: init timeout, stat = 0x%x\n",
258 nif->nif_unit, stat);
259 break;
260 }
261 stat = ler1->ler1_rdp;
262 } while ((stat & LE_C0_IDON) == 0);
263
264 ler1->ler1_rdp = LE_C0_IDON;
265 le_softc.next_rmd = 0;
266 le_softc.next_tmd = 0;
267 ler1->ler1_rap = LE_CSR0;
268 ler1->ler1_rdp = LE_C0_STRT;
269 }
270
271 int
272 le_poll(desc, pkt, len)
273 struct iodesc *desc;
274 void *pkt;
275 int len;
276 {
277 struct lereg1 *ler1 = le_softc.sc_r1;
278 struct lereg2 *ler2 = le_softc.sc_r2;
279 unsigned int a;
280 int length;
281 struct lermd *rmd;
282
283
284 ler1->ler1_rap = LE_CSR0;
285 if ((ler1->ler1_rdp & LE_C0_RINT) != 0)
286 ler1->ler1_rdp = LE_C0_RINT;
287 rmd = &ler2->ler2_rmd[le_softc.next_rmd];
288 if (rmd->rmd1_bits & LE_R1_OWN) {
289 return (0);
290 }
291 if (ler1->ler1_rdp & LE_C0_ERR)
292 le_error(desc->io_netif, "le_poll", ler1);
293 if (rmd->rmd1_bits & LE_R1_ERR) {
294 printf("le%d_poll: rmd status 0x%x\n",
295 ((struct netif *)desc->io_netif)->nif_unit,
296 rmd->rmd1_bits);
297 length = 0;
298 goto cleanup;
299 }
300 if ((rmd->rmd1_bits & (LE_R1_STP | LE_R1_ENP)) != (LE_R1_STP | LE_R1_ENP))
301 panic("le_poll: chained packet");
302
303 length = rmd->rmd3;
304 if (length >= LEMTU) {
305 length = 0;
306 panic("csr0 when bad things happen: %x", ler1->ler1_rdp);
307 goto cleanup;
308 }
309 if (!length)
310 goto cleanup;
311 length -= 4;
312 if (length > 0) {
313
314 /*
315 * if buffer is smaller than the packet truncate it.
316 * (is this wise?)
317 */
318 if (length > len)
319 length = len;
320
321 memcpy(pkt, (void *)&ler2->ler2_rbuf[le_softc.next_rmd],
322 length);
323 }
324 cleanup:
325 a = (u_int) & ler2->ler2_rbuf[le_softc.next_rmd];
326 rmd->rmd0 = a & LE_ADDR_LOW_MASK;
327 rmd->rmd1_hadr = a >> 16;
328 rmd->rmd2 = -LEMTU;
329 le_softc.next_rmd =
330 (le_softc.next_rmd == (LERBUF - 1)) ? 0 : (le_softc.next_rmd + 1);
331 rmd->rmd1_bits = LE_R1_OWN;
332 return length;
333 }
334
335 int
336 le_put(desc, pkt, len)
337 struct iodesc *desc;
338 void *pkt;
339 size_t len;
340 {
341 volatile struct lereg1 *ler1 = le_softc.sc_r1;
342 volatile struct lereg2 *ler2 = le_softc.sc_r2;
343 volatile struct letmd *tmd;
344 int timo = 100000, stat = 0;
345 unsigned int a;
346 int nifunit = ((struct netif *)desc->io_netif)->nif_unit;
347
348 ler1->ler1_rap = LE_CSR0;
349 if (ler1->ler1_rdp & LE_C0_ERR)
350 le_error(desc->io_netif, "le_put(way before xmit)", ler1);
351 tmd = &ler2->ler2_tmd[le_softc.next_tmd];
352 while (tmd->tmd1_bits & LE_T1_OWN) {
353 printf("le%d: output buffer busy\n", nifunit);
354 }
355 memcpy((void *)ler2->ler2_tbuf[le_softc.next_tmd], pkt, len);
356 if (len < 64)
357 tmd->tmd2 = -64;
358 else
359 tmd->tmd2 = -len;
360 tmd->tmd3 = 0;
361 if (ler1->ler1_rdp & LE_C0_ERR)
362 le_error(desc->io_netif, "le_put(before xmit)", ler1);
363 tmd->tmd1_bits = LE_T1_STP | LE_T1_ENP | LE_T1_OWN;
364 a = (u_int) & ler2->ler2_tbuf[le_softc.next_tmd];
365 tmd->tmd0 = a & LE_ADDR_LOW_MASK;
366 tmd->tmd1_hadr = a >> 16;
367 ler1->ler1_rdp = LE_C0_TDMD;
368 if (ler1->ler1_rdp & LE_C0_ERR)
369 le_error(desc->io_netif, "le_put(after xmit)", ler1);
370 do {
371 if (--timo == 0) {
372 printf("le%d: transmit timeout, stat = 0x%x\n",
373 nifunit, stat);
374 if (ler1->ler1_rdp & LE_C0_ERR)
375 le_error(desc->io_netif, "le_put(timeout)", ler1);
376 break;
377 }
378 stat = ler1->ler1_rdp;
379 } while ((stat & LE_C0_TINT) == 0);
380 ler1->ler1_rdp = LE_C0_TINT;
381 if (ler1->ler1_rdp & LE_C0_ERR) {
382 if ((ler1->ler1_rdp & (LE_C0_BABL | LE_C0_CERR | LE_C0_MISS |
383 LE_C0_MERR)) !=
384 LE_C0_CERR)
385 printf("le_put: xmit error, buf %d\n", le_softc.next_tmd);
386 le_error(desc->io_netif, "le_put(xmit error)", ler1);
387 }
388 le_softc.next_tmd = 0;
389 /* (le_softc.next_tmd == (LETBUF - 1)) ? 0 : le_softc.next_tmd + 1;*/
390 if (tmd->tmd1_bits & LE_T1_DEF)
391 le_stats.deferred++;
392 if (tmd->tmd1_bits & LE_T1_ONE)
393 le_stats.collisions++;
394 if (tmd->tmd1_bits & LE_T1_MORE)
395 le_stats.collisions += 2;
396 if (tmd->tmd1_bits & LE_T1_ERR) {
397 printf("le%d: transmit error, error = 0x%x\n", nifunit,
398 tmd->tmd3);
399 return -1;
400 }
401 if (le_debug) {
402 printf("le%d: le_put() successful: sent %d\n",
403 nifunit, len);
404 printf("le%d: le_put(): tmd1_bits: %x tmd3: %x\n",
405 nifunit,
406 (unsigned int) tmd->tmd1_bits,
407 (unsigned int) tmd->tmd3);
408 }
409 return len;
410 }
411
412 int
413 le_get(desc, pkt, len, timeout)
414 struct iodesc *desc;
415 void *pkt;
416 size_t len;
417 time_t timeout;
418 {
419 time_t t;
420 int cc;
421
422 t = getsecs();
423 cc = 0;
424 while (((getsecs() - t) < timeout) && !cc) {
425 cc = le_poll(desc, pkt, len);
426 }
427 return cc;
428 }
429 /*
430 * init le device. return 0 on failure, 1 if ok.
431 */
432 void
433 le_init(desc, machdep_hint)
434 struct iodesc *desc;
435 void *machdep_hint;
436 {
437 u_long eram = 4*1024*1024;
438 struct netif *nif = desc->io_netif;
439
440 if (le_debug)
441 printf("le%d: le_init called\n", nif->nif_unit);
442 machdep_common_ether(desc->myea);
443 memset(&le_softc, 0, sizeof(le_softc));
444 le_softc.sc_r1 =
445 (struct lereg1 *) le_config[nif->nif_unit].phys_addr;
446 le_softc.sc_r2 = (struct lereg2 *) (eram - (1024 * 1024));
447 le_reset(desc->io_netif, desc->myea);
448 printf("device: %s%d attached to %s\n", nif->nif_driver->netif_bname,
449 nif->nif_unit, ether_sprintf(desc->myea));
450 }
451
452 void
453 le_end(nif)
454 struct netif *nif;
455 {
456 struct lereg1 *ler1 = le_softc.sc_r1;
457
458 if (le_debug)
459 printf("le%d: le_end called\n", nif->nif_unit);
460 ler1->ler1_rap = LE_CSR0;
461 ler1->ler1_rdp = LE_C0_STOP;
462 }
463