Annotation of sys/arch/mvme68k/stand/netboot/if_le.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: if_le.c,v 1.10 2003/08/20 00:26:00 deraadt 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 "stand.h"
66: #include "libsa.h"
67: #include "netif.h"
68: #include "config.h"
69:
70: #include "if_lereg.h"
71:
72: int le_debug = 0;
73:
74: void le_end(struct netif *);
75: void le_error(struct netif *, char *, volatile struct lereg1 *);
76: int le_get(struct iodesc *, void *, size_t, time_t);
77: void le_init(struct iodesc *, void *);
78: int le_match(struct netif *, void *);
79: int le_poll(struct iodesc *, void *, int);
80: int le_probe(struct netif *, void *);
81: int le_put(struct iodesc *, void *, size_t);
82: void le_reset(struct netif *, u_char *);
83:
84: struct netif_stats le_stats;
85:
86: struct netif_dif le0_dif = {
87: 0, /* unit */
88: 1, /* nsel */
89: &le_stats,
90: 0,
91: 0,
92: };
93:
94: struct netif_driver le_driver = {
95: "le", /* netif_bname */
96: le_match, /* match */
97: le_probe, /* probe */
98: le_init, /* init */
99: le_get, /* get */
100: le_put, /* put */
101: le_end, /* end */
102: &le0_dif, /* netif_ifs */
103: 1, /* netif_nifs */
104: };
105:
106: struct le_configuration {
107: unsigned int phys_addr;
108: int used;
109: } le_config[] = {
110: { LANCE_REG_ADDR, 0 }
111: };
112:
113: int nle_config = sizeof(le_config) / (sizeof(le_config[0]));
114:
115: struct {
116: struct lereg1 *sc_r1; /* LANCE registers */
117: struct lereg2 *sc_r2; /* RAM */
118: int next_rmd;
119: int next_tmd;
120: } le_softc;
121:
122: int
123: le_match(nif, machdep_hint)
124: struct netif *nif;
125: void *machdep_hint;
126: {
127: char *name;
128: int i, val = 0;
129:
130: if (bugargs.cputyp != CPU_147)
131: return (0);
132: name = machdep_hint;
133: if (name && !bcmp(le_driver.netif_bname, name, 2))
134: val += 10;
135: for (i = 0; i < nle_config; i++) {
136: if (le_config[i].used)
137: continue;
138: if (le_debug)
139: printf("le%d: le_match --> %d\n", i, val + 1);
140: le_config[i].used++;
141: return val + 1;
142: }
143: if (le_debug)
144: printf("le%d: le_match --> 0\n", i);
145: return 0;
146: }
147:
148: int
149: le_probe(nif, machdep_hint)
150: struct netif *nif;
151: void *machdep_hint;
152: {
153:
154: /* the set unit is the current unit */
155: if (le_debug)
156: printf("le%d: le_probe called\n", nif->nif_unit);
157:
158: if (bugargs.cputyp == CPU_147)
159: return 0;
160: return 1;
161: }
162:
163: void
164: le_error(nif, str, ler1)
165: struct netif *nif;
166: char *str;
167: volatile struct lereg1 *ler1;
168: {
169: /* ler1->ler1_rap = LE_CSRO done in caller */
170: if (ler1->ler1_rdp & LE_C0_BABL)
171: panic("le%d: been babbling, found by '%s'", nif->nif_unit, str);
172: if (ler1->ler1_rdp & LE_C0_CERR) {
173: le_stats.collision_error++;
174: ler1->ler1_rdp = LE_C0_CERR;
175: }
176: if (ler1->ler1_rdp & LE_C0_MISS) {
177: le_stats.missed++;
178: ler1->ler1_rdp = LE_C0_MISS;
179: }
180: if (ler1->ler1_rdp & LE_C0_MERR) {
181: printf("le%d: memory error in '%s'\n", nif->nif_unit, str);
182: panic("memory error");
183: }
184: }
185:
186: void
187: le_reset(nif, myea)
188: struct netif *nif;
189: u_char *myea;
190: {
191: struct lereg1 *ler1 = le_softc.sc_r1;
192: struct lereg2 *ler2 = le_softc.sc_r2;
193: unsigned int a;
194: int timo = 100000, stat, i;
195:
196: if (le_debug)
197: printf("le%d: le_reset called\n", nif->nif_unit);
198: ler1->ler1_rap = LE_CSR0;
199: ler1->ler1_rdp = LE_C0_STOP; /* do nothing until we are finished */
200:
201: bzero(ler2, sizeof(*ler2));
202:
203: ler2->ler2_mode = LE_MODE_NORMAL;
204: ler2->ler2_padr[0] = myea[1];
205: ler2->ler2_padr[1] = myea[0];
206: ler2->ler2_padr[2] = myea[3];
207: ler2->ler2_padr[3] = myea[2];
208: ler2->ler2_padr[4] = myea[5];
209: ler2->ler2_padr[5] = myea[4];
210:
211:
212: ler2->ler2_ladrf0 = 0;
213: ler2->ler2_ladrf1 = 0;
214:
215: a = (u_int) ler2->ler2_rmd;
216: ler2->ler2_rlen = LE_RLEN | (a >> 16);
217: ler2->ler2_rdra = a & LE_ADDR_LOW_MASK;
218:
219: a = (u_int) ler2->ler2_tmd;
220: ler2->ler2_tlen = LE_TLEN | (a >> 16);
221: ler2->ler2_tdra = a & LE_ADDR_LOW_MASK;
222:
223: ler1->ler1_rap = LE_CSR1;
224: a = (u_int) ler2;
225: ler1->ler1_rdp = a & LE_ADDR_LOW_MASK;
226: ler1->ler1_rap = LE_CSR2;
227: ler1->ler1_rdp = a >> 16;
228:
229: for (i = 0; i < LERBUF; i++) {
230: a = (u_int) & ler2->ler2_rbuf[i];
231: ler2->ler2_rmd[i].rmd0 = a & LE_ADDR_LOW_MASK;
232: ler2->ler2_rmd[i].rmd1_bits = LE_R1_OWN;
233: ler2->ler2_rmd[i].rmd1_hadr = a >> 16;
234: ler2->ler2_rmd[i].rmd2 = -LEMTU;
235: ler2->ler2_rmd[i].rmd3 = 0;
236: }
237: for (i = 0; i < LETBUF; i++) {
238: a = (u_int) & ler2->ler2_tbuf[i];
239: ler2->ler2_tmd[i].tmd0 = a & LE_ADDR_LOW_MASK;
240: ler2->ler2_tmd[i].tmd1_bits = 0;
241: ler2->ler2_tmd[i].tmd1_hadr = a >> 16;
242: ler2->ler2_tmd[i].tmd2 = 0;
243: ler2->ler2_tmd[i].tmd3 = 0;
244: }
245:
246: ler1->ler1_rap = LE_CSR3;
247: ler1->ler1_rdp = LE_C3_BSWP;
248:
249: ler1->ler1_rap = LE_CSR0;
250: ler1->ler1_rdp = LE_C0_INIT;
251: do {
252: if (--timo == 0) {
253: printf("le%d: init timeout, stat = 0x%x\n",
254: nif->nif_unit, stat);
255: break;
256: }
257: stat = ler1->ler1_rdp;
258: } while ((stat & LE_C0_IDON) == 0);
259:
260: ler1->ler1_rdp = LE_C0_IDON;
261: le_softc.next_rmd = 0;
262: le_softc.next_tmd = 0;
263: ler1->ler1_rap = LE_CSR0;
264: ler1->ler1_rdp = LE_C0_STRT;
265: }
266:
267: int
268: le_poll(desc, pkt, len)
269: struct iodesc *desc;
270: void *pkt;
271: int len;
272: {
273: struct lereg1 *ler1 = le_softc.sc_r1;
274: struct lereg2 *ler2 = le_softc.sc_r2;
275: unsigned int a;
276: int length;
277: struct lermd *rmd;
278:
279:
280: ler1->ler1_rap = LE_CSR0;
281: if ((ler1->ler1_rdp & LE_C0_RINT) != 0)
282: ler1->ler1_rdp = LE_C0_RINT;
283: rmd = &ler2->ler2_rmd[le_softc.next_rmd];
284: if (rmd->rmd1_bits & LE_R1_OWN) {
285: return (0);
286: }
287: if (ler1->ler1_rdp & LE_C0_ERR)
288: le_error(desc->io_netif, "le_poll", ler1);
289: if (rmd->rmd1_bits & LE_R1_ERR) {
290: printf("le%d_poll: rmd status 0x%x\n", desc->io_netif->nif_unit,
291: rmd->rmd1_bits);
292: length = 0;
293: goto cleanup;
294: }
295: if ((rmd->rmd1_bits & (LE_R1_STP | LE_R1_ENP)) != (LE_R1_STP | LE_R1_ENP))
296: panic("le_poll: chained packet");
297:
298: length = rmd->rmd3;
299: if (length >= LEMTU) {
300: length = 0;
301: panic("csr0 when bad things happen: %x", ler1->ler1_rdp);
302: goto cleanup;
303: }
304: if (!length)
305: goto cleanup;
306: length -= 4;
307: if (length > 0) {
308:
309: /*
310: * if buffer is smaller than the packet truncate it.
311: * (is this wise?)
312: */
313: if (length > len)
314: length = len;
315:
316: bcopy((void *)&ler2->ler2_rbuf[le_softc.next_rmd], pkt, length);
317: }
318: cleanup:
319: a = (u_int) & ler2->ler2_rbuf[le_softc.next_rmd];
320: rmd->rmd0 = a & LE_ADDR_LOW_MASK;
321: rmd->rmd1_hadr = a >> 16;
322: rmd->rmd2 = -LEMTU;
323: le_softc.next_rmd =
324: (le_softc.next_rmd == (LERBUF - 1)) ? 0 : (le_softc.next_rmd + 1);
325: rmd->rmd1_bits = LE_R1_OWN;
326: return length;
327: }
328:
329: int
330: le_put(desc, pkt, len)
331: struct iodesc *desc;
332: void *pkt;
333: size_t len;
334: {
335: volatile struct lereg1 *ler1 = le_softc.sc_r1;
336: volatile struct lereg2 *ler2 = le_softc.sc_r2;
337: volatile struct letmd *tmd;
338: int timo = 100000, stat, i;
339: unsigned int a;
340:
341: ler1->ler1_rap = LE_CSR0;
342: if (ler1->ler1_rdp & LE_C0_ERR)
343: le_error(desc->io_netif, "le_put(way before xmit)", ler1);
344: tmd = &ler2->ler2_tmd[le_softc.next_tmd];
345: while (tmd->tmd1_bits & LE_T1_OWN) {
346: printf("le%d: output buffer busy\n", desc->io_netif->nif_unit);
347: }
348: bcopy(pkt, (void *)ler2->ler2_tbuf[le_softc.next_tmd], len);
349: if (len < 64)
350: tmd->tmd2 = -64;
351: else
352: tmd->tmd2 = -len;
353: tmd->tmd3 = 0;
354: if (ler1->ler1_rdp & LE_C0_ERR)
355: le_error(desc->io_netif, "le_put(before xmit)", ler1);
356: tmd->tmd1_bits = LE_T1_STP | LE_T1_ENP | LE_T1_OWN;
357: a = (u_int) & ler2->ler2_tbuf[le_softc.next_tmd];
358: tmd->tmd0 = a & LE_ADDR_LOW_MASK;
359: tmd->tmd1_hadr = a >> 16;
360: ler1->ler1_rdp = LE_C0_TDMD;
361: if (ler1->ler1_rdp & LE_C0_ERR)
362: le_error(desc->io_netif, "le_put(after xmit)", ler1);
363: do {
364: if (--timo == 0) {
365: printf("le%d: transmit timeout, stat = 0x%x\n",
366: desc->io_netif->nif_unit, stat);
367: if (ler1->ler1_rdp & LE_C0_ERR)
368: le_error(desc->io_netif, "le_put(timeout)", ler1);
369: break;
370: }
371: stat = ler1->ler1_rdp;
372: } while ((stat & LE_C0_TINT) == 0);
373: ler1->ler1_rdp = LE_C0_TINT;
374: if (ler1->ler1_rdp & LE_C0_ERR) {
375: if ((ler1->ler1_rdp & (LE_C0_BABL | LE_C0_CERR | LE_C0_MISS |
376: LE_C0_MERR)) !=
377: LE_C0_CERR)
378: printf("le_put: xmit error, buf %d\n", le_softc.next_tmd);
379: le_error(desc->io_netif, "le_put(xmit error)", ler1);
380: }
381: le_softc.next_tmd = 0;
382: /* (le_softc.next_tmd == (LETBUF - 1)) ? 0 : le_softc.next_tmd + 1;*/
383: if (tmd->tmd1_bits & LE_T1_DEF)
384: le_stats.deferred++;
385: if (tmd->tmd1_bits & LE_T1_ONE)
386: le_stats.collisions++;
387: if (tmd->tmd1_bits & LE_T1_MORE)
388: le_stats.collisions += 2;
389: if (tmd->tmd1_bits & LE_T1_ERR) {
390: printf("le%d: transmit error, error = 0x%x\n", desc->io_netif->nif_unit,
391: tmd->tmd3);
392: return -1;
393: }
394: if (le_debug) {
395: printf("le%d: le_put() successful: sent %d\n",
396: desc->io_netif->nif_unit, len);
397: printf("le%d: le_put(): tmd1_bits: %x tmd3: %x\n",
398: desc->io_netif->nif_unit,
399: (unsigned int) tmd->tmd1_bits,
400: (unsigned int) tmd->tmd3);
401: }
402: return len;
403: }
404:
405: int
406: le_get(desc, pkt, len, timeout)
407: struct iodesc *desc;
408: void *pkt;
409: size_t len;
410: time_t timeout;
411: {
412: time_t t;
413: int cc;
414:
415: t = getsecs();
416: cc = 0;
417: while (((getsecs() - t) < timeout) && !cc) {
418: cc = le_poll(desc, pkt, len);
419: }
420: return cc;
421: }
422: /*
423: * init le device. return 0 on failure, 1 if ok.
424: */
425: void
426: le_init(desc, machdep_hint)
427: struct iodesc *desc;
428: void *machdep_hint;
429: {
430: u_long eram = 4*1024*1024;
431: struct netif *nif = desc->io_netif;
432:
433: if (le_debug)
434: printf("le%d: le_init called\n", desc->io_netif->nif_unit);
435: machdep_common_ether(desc->myea);
436: bzero(&le_softc, sizeof(le_softc));
437: le_softc.sc_r1 =
438: (struct lereg1 *) le_config[desc->io_netif->nif_unit].phys_addr;
439: le_softc.sc_r2 = (struct lereg2 *) (eram - (1024 * 1024));
440: le_reset(desc->io_netif, desc->myea);
441: printf("device: %s%d attached to %s\n", nif->nif_driver->netif_bname,
442: nif->nif_unit, ether_sprintf(desc->myea));
443: }
444:
445: void
446: le_end(nif)
447: struct netif *nif;
448: {
449: struct lereg1 *ler1 = le_softc.sc_r1;
450:
451: if (le_debug)
452: printf("le%d: le_end called\n", nif->nif_unit);
453: ler1->ler1_rap = LE_CSR0;
454: ler1->ler1_rdp = LE_C0_STOP;
455: }
CVSweb