en.c revision 1.5 1 /* $NetBSD: en.c,v 1.5 2002/09/11 01:46:36 mycroft Exp $ */
2 /*
3 * Copyright (c) 1996 Rolf Grossmann
4 * All rights reserved.
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 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Rolf Grossmann.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <next68k/dev/enreg.h>
37 #include <next68k/next68k/nextrom.h>
38 #include "enreg.h"
39 #include "dmareg.h"
40
41 #include <stand.h>
42 #include <netif.h>
43 #include <net.h>
44 #include <nfs.h>
45
46 #include <lib/libkern/libkern.h>
47
48 extern char *mg;
49 #define MON(type, off) (*(type *)((u_int) (mg) + off))
50
51 #define PRINTF(x) printf x;
52 #ifdef EN_DEBUG
53 #define DPRINTF(x) printf x;
54 #else
55 #define DPRINTF(x)
56 #endif
57
58 #define EN_TIMEOUT 2000000
59 #define EN_RETRIES 10
60
61 int en_match __P((struct netif *nif, void *machdep_hint));
62 int en_probe __P((struct netif *nif, void *machdep_hint));
63 void en_init __P((struct iodesc *desc, void *machdep_hint));
64 int en_get __P((struct iodesc *a, void *b, size_t c, time_t d));
65 int en_put __P((struct iodesc *a, void *b, size_t c));
66 void en_end __P((struct netif *a));
67
68 /* ### static int mountroot __P((int sock)); */
69 static int en_wait_for_intr __P((int flag));
70
71 struct netif_stats en_stats;
72
73 struct netif_dif en_ifs[] = {
74 /* dif_unit dif_nsel dif_stats dif_private */
75 { 0, 1, &en_stats, NULL, },
76 };
77
78 struct netif_driver en_driver = {
79 "en",
80 en_match, en_probe, en_init, en_get, en_put, en_end,
81 en_ifs, NENTS(en_ifs)
82 };
83
84 int turbo = 1;
85
86 /* ### int netdev_sock;
87 static int open_count; */
88
89 char dma_buffer1[MAX_DMASIZE+DMA_ENDALIGNMENT],
90 dma_buffer2[MAX_DMASIZE+DMA_ENDALIGNMENT],
91 *dma_buffers[2];
92 int next_dma_buffer;
93
94
95 int
96 en_match(struct netif *nif, void *machdep_hint)
97 {
98 /* we always match, because every NeXT has an ethernet interface
99 * and we've checked the unit numbers before we even started this
100 * search.
101 * ### now that open is generic, we should check the params!
102 */
103 return 1;
104 }
105
106 int
107 en_probe(struct netif *nif, void *machdep_hint)
108 {
109 /* we also always probe ok, see en_match. */
110 return 0;
111 }
112
113 void
114 en_init(struct iodesc *desc, void *machdep_hint)
115 {
116 volatile struct en_regs *er;
117 volatile u_int *bmap_chip;
118 int i;
119
120 DPRINTF(("en_init\n"));
121
122 er = (struct en_regs *)P_ENET;
123 bmap_chip = (u_int *)P_BMAP;
124
125 dma_buffers[0] = DMA_ALIGN(char *, dma_buffer1);
126 dma_buffers[1] = DMA_ALIGN(char *, dma_buffer2);
127
128 er->reset = EN_RST_RESET;
129 /* if (turbo) */
130 /* er->reset = 0; */
131
132 er->txmask = 0;
133 er->txstat = 0xff;
134 if (turbo)
135 er->txmode = 0 | EN_TMD_COLLSHIFT;
136 else
137 er->txmode = EN_TMD_LB_DISABLE;
138
139 /* setup for bnc/tp */
140 if (turbo) {
141
142 } else {
143 DPRINTF (("en_media: %s\n",
144 (bmap_chip[13] & 0x20000000) ? "BNC" : "TP"));
145 if (!(bmap_chip[13] & 0x20000000)) {
146 bmap_chip[12] |= 0x90000000;
147 bmap_chip[13] |= (0x80000000|0x10000000); /* TP */
148 }
149 }
150
151 /* if (turbo) { */
152 /* er->txmode |= EN_TMD_COLLSHIFT; */
153 /* } else { */
154 /* er->txmode &= ~EN_TMD_LB_DISABLE; /\* ZZZ *\/ */
155 /* } */
156
157 er->rxmask = 0;
158 er->rxstat = 0xff;
159 if (turbo) {
160 er->rxmode = EN_RMD_TEST;
161 } else {
162 er->rxmode = EN_RMD_RECV_NORMAL;
163 }
164 for (i=0; i<6; i++)
165 er->addr[i] = desc->myea[i] = MON(char *,MG_clientetheraddr)[i];
166
167 DPRINTF(("ethernet addr (%x:%x:%x:%x:%x:%x)\n",
168 desc->myea[0],desc->myea[1],desc->myea[2],
169 desc->myea[3],desc->myea[4],desc->myea[5]));
170
171 /* if (!turbo) */
172 er->reset = 0;
173 }
174
175 #if 0
176 /* ### remove this when things work! */
177 #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
178 void
179 dump_pkt(unsigned char *pkt, size_t len)
180 {
181 size_t i, j;
182
183 printf("0000: ");
184 for(i=0; i<len; i++) {
185 printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
186 if ((i+1) % 16 == 0) {
187 printf(" %c", '"');
188 for(j=0; j<16; j++)
189 printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
190 printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
191 XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
192 }
193 }
194 printf("\n");
195 }
196 #endif
197
198 int
199 en_put(struct iodesc *desc, void *pkt, size_t len)
200 {
201 volatile struct en_regs *er;
202 volatile struct dma_dev *txdma;
203 int state, txs;
204 int retries;
205
206 DPRINTF(("en_put: %d bytes at 0x%lx\n", len, (unsigned long)pkt));
207 #if 0
208 dump_pkt(pkt,len);
209 #endif
210
211 er = (struct en_regs *)P_ENET;
212 txdma = (struct dma_dev *)P_ENETX_CSR;
213
214 DPRINTF(("en_put: txdma->dd_csr = %x\n",txdma->dd_csr));
215
216 if (len > 1600) {
217 errno = EINVAL;
218 return -1;
219 }
220
221 if (!turbo) {
222 while ((er->txstat & EN_TXS_READY) == 0)
223 printf("en: tx not ready\n");
224 }
225
226 for (retries = 0; retries < EN_RETRIES; retries++) {
227 er->txstat = 0xff;
228 bcopy(pkt, dma_buffers[0], len);
229 txdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
230 DMACSR_RESET | DMACSR_WRITE;
231 txdma->dd_csr = 0;
232 txdma->dd_next/* _initbuf */ = dma_buffers[0];
233 txdma->dd_start = (turbo ? dma_buffers[0] : 0);
234 txdma->dd_limit = ENDMA_ENDALIGN(char *, dma_buffers[0]+len);
235 txdma->dd_stop = 0;
236 txdma->dd_csr = DMACSR_SETENABLE;
237 if (turbo)
238 er->txmode |= 0x80;
239
240 while(1) {
241 if (en_wait_for_intr(ENETX_DMA_INTR)) {
242 errno = EIO;
243 return -1;
244 }
245
246 state = txdma->dd_csr &
247 (DMACSR_BUSEXC | DMACSR_COMPLETE
248 | DMACSR_SUPDATE | DMACSR_ENABLE);
249
250 #if 01
251 DPRINTF(("en_put: dma state = 0x%x.\n", state));
252 #endif
253 if (state & (DMACSR_COMPLETE|DMACSR_BUSEXC))
254 txdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
255 break;
256 }
257
258 txs = er->txstat;
259
260 #if 01
261 DPRINTF(("en_put: done txstat=%x.\n", txs));
262 #endif
263
264 #define EN_TXS_ERROR (EN_TXS_SHORTED | EN_TXS_UNDERFLOW | EN_TXS_PARERR)
265 if ((state & DMACSR_COMPLETE) == 0 ||
266 (txs & EN_TXS_ERROR) != 0) {
267 errno = EIO;
268 return -1;
269 }
270 if ((txs & EN_TXS_COLLERR) == 0)
271 return len; /* success */
272 }
273
274 errno = EIO; /* too many retries */
275 return -1;
276 }
277
278 int
279 en_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
280 {
281 volatile struct en_regs *er;
282 volatile struct dma_dev *rxdma;
283 volatile struct dma_dev *txdma;
284 int state, rxs;
285 size_t rlen;
286 char *gotpkt;
287
288 rxdma = (struct dma_dev *)P_ENETR_CSR;
289 txdma = (struct dma_dev *)P_ENETX_CSR;
290 er = (struct en_regs *)P_ENET;
291
292 DPRINTF(("en_get: rxdma->dd_csr = %x\n",rxdma->dd_csr));
293
294 er->rxstat = 0xff;
295
296 /* this is mouse's code now ... still doesn't work :( */
297 /* The previous comment is now a lie, this does work
298 * Darrin B Jewell <jewell (at) mit.edu> Sat Jan 24 21:44:56 1998
299 */
300
301 rxdma->dd_csr = 0;
302 rxdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
303 DMACSR_READ | DMACSR_RESET;
304
305 if (!turbo) {
306 rxdma->dd_saved_next = 0;
307 rxdma->dd_saved_limit = 0;
308 rxdma->dd_saved_start = 0;
309 rxdma->dd_saved_stop = 0;
310 } else {
311 rxdma->dd_saved_next = dma_buffers[0];
312 }
313
314 rxdma->dd_next = dma_buffers[0];
315 rxdma->dd_limit = DMA_ENDALIGN(char *, dma_buffers[0]+MAX_DMASIZE);
316 #if 0
317 if (turbo) {
318 /* !!! not a typo: txdma */
319 txdma->dd_stop = dma_buffers[0];
320 }
321 #endif
322 rxdma->dd_start = 0;
323 rxdma->dd_stop = 0;
324 rxdma->dd_csr = DMACSR_SETENABLE | DMACSR_READ;
325 if (turbo) {
326 er->rxmode = EN_RMD_TEST | EN_RMD_RECV_MULTI;
327 } else {
328 er->rxmode = EN_RMD_RECV_NORMAL;
329 }
330
331 #if 01
332 DPRINTF(("en_get: blocking on rcv dma\n"));
333 #endif
334
335 while(1) {
336 if (en_wait_for_intr(ENETR_DMA_INTR)) { /* ### use timeout? */
337 errno = EIO;
338 return -1;
339 }
340
341 state = rxdma->dd_csr &
342 (DMACSR_BUSEXC | DMACSR_COMPLETE
343 | DMACSR_SUPDATE | DMACSR_ENABLE);
344 DPRINTF(("en_get: dma state = 0x%x.\n", state));
345 if ((state & DMACSR_ENABLE) == 0) {
346 rxdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
347 break;
348 }
349
350 if (state & DMACSR_COMPLETE) {
351 PRINTF(("en_get: ending dma sequence\n"));
352 rxdma->dd_csr = DMACSR_CLRCOMPLETE;
353 }
354
355 }
356
357 rxs = er->rxstat;
358
359 if ((state & DMACSR_COMPLETE) == 0 ||
360 (rxs & EN_RX_OK) == 0) {
361 errno = EIO;
362 return -1; /* receive failed */
363 }
364
365 if (turbo) {
366 gotpkt = rxdma->dd_saved_next;
367 rlen = rxdma->dd_next - rxdma->dd_saved_next;
368 } else {
369 gotpkt = rxdma->dd_saved_next;
370 rlen = rxdma->dd_next - rxdma->dd_saved_next;
371 }
372
373 if (gotpkt != dma_buffers[0]) {
374 printf("Unexpected received packet location\n");
375 printf("DEBUG: rxstat=%x.\n", rxs);
376 printf("DEBUG: gotpkt = 0x%lx, rlen = %d, len = %d\n",(u_long)gotpkt,rlen,len);
377 printf("DEBUG: rxdma->dd_csr = 0x%lx\n",(u_long)rxdma->dd_csr);
378 printf("DEBUG: rxdma->dd_saved_next = 0x%lx\n",(u_long)rxdma->dd_saved_next);
379 printf("DEBUG: rxdma->dd_saved_limit = 0x%lx\n",(u_long)rxdma->dd_saved_limit);
380 printf("DEBUG: rxdma->dd_saved_start = 0x%lx\n",(u_long)rxdma->dd_saved_start);
381 printf("DEBUG: rxdma->dd_saved_stop = 0x%lx\n",(u_long)rxdma->dd_saved_stop);
382 printf("DEBUG: rxdma->dd_next = 0x%lx\n",(u_long)rxdma->dd_next);
383 printf("DEBUG: rxdma->dd_limit = 0x%lx\n",(u_long)rxdma->dd_limit);
384 printf("DEBUG: rxdma->dd_start = 0x%lx\n",(u_long)rxdma->dd_start);
385 printf("DEBUG: rxdma->dd_stop = 0x%lx\n",(u_long)rxdma->dd_stop);
386 printf("DEBUG: rxdma->dd_next_initbuf = 0x%lx\n",(u_long)rxdma->dd_next_initbuf);
387 }
388
389 #if 0
390 dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
391 #endif
392
393 DPRINTF(("en_get: done rxstat=%x.\n", rxs));
394
395 if (rlen > len) {
396 DPRINTF(("en_get: buffer too small. want %d, got %d\n",
397 len, rlen));
398 rlen = len;
399 }
400
401
402 bcopy(gotpkt, pkt, rlen);
403
404 #if 0
405 printf("DEBUG: gotpkt = 0x%lx, pkt = 0x%lx, rlen = %d\n",
406 (u_long)gotpkt,(u_long)pkt,rlen);
407 dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
408 dump_pkt(pkt, rlen < 255 ? rlen : 128);
409 #endif
410
411 return rlen;
412 }
413
414
415 void
416 en_end(struct netif *a)
417 {
418 DPRINTF(("en_end: WARNING not doing anything\n"));
419 }
420
421 #if 0
422
423 #define MKPANIC(ret,name,args) \
424 ret name args { panic(#name ## ": not implemented.\n"); }
425
426 MKPANIC(void, en_end, (struct netif *a));
427
428 /* device functions */
429
430 int
431 enopen(struct open_file *f, char count, char lun, char part)
432 {
433 int error;
434
435 DPRINTF(("open: en(%d,%d,%d)\n", count, lun, part));
436
437 if (count != 0 || lun != 0 || part != 0)
438 return EUNIT; /* there can be exactly one ethernet */
439
440 if (open_count == 0) {
441 /* Find network interface. */
442 if ((netdev_sock = netif_open(NULL)) < 0)
443 return errno;
444 if ((error = mountroot(netdev_sock)) != 0) {
445 if (open_count == 0)
446 netif_close(netdev_sock);
447 return error;
448 }
449 }
450 open_count++;
451 f->f_devdata = NULL; /* ### nfs_root_node ?! */
452 return 0;
453 }
454
455 int
456 enclose(struct open_file *f)
457 {
458 if (open_count > 0)
459 if (--open_count == 0)
460 netif_close(netdev_sock);
461 f->f_devdata = NULL;
462 return 0;
463 }
464
465 int
466 enstrategy(void *devdata, int rw, daddr_t dblk,
467 size_t size, void *buf, size_t *rsize)
468 {
469 return ENXIO; /* wrong access method */
470 }
471
472 /* private function */
473
474 static int
475 mountroot(int sock)
476 {
477 /* Mount the root directory from a boot server */
478 #if 0
479 struct in_addr in = {
480 0xc2793418
481 };
482 u_char *res;
483
484 res = arpwhohas(socktodesc(sock), in);
485 panic("arpwhohas returned %s", res);
486 #endif
487 /* 1. use bootp. This does most of the work for us. */
488 bootp(sock);
489
490 if (myip.s_addr == 0 || rootip.s_addr == 0 || rootpath[0] == '\0')
491 return ETIMEDOUT;
492
493 printf("Using IP address: %s\n", inet_ntoa(myip));
494 printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
495
496 /* 2. mount. */
497 if (nfs_mount(sock, rootip, rootpath) < 0)
498 return errno;
499
500 return 0;
501 }
502 #endif
503
504 static int
505 en_wait_for_intr(int flag)
506 {
507 volatile int *intrstat = MON(volatile int *,MG_intrstat);
508
509 int count;
510
511 for(count = 0; count < EN_TIMEOUT; count++)
512 if (*intrstat & flag)
513 return 0;
514
515 printf("enintr: timed out.\n");
516 return -1;
517 }
518