en.c revision 1.6 1 /* $NetBSD: en.c,v 1.6 2002/09/11 02:17:15 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 extern int turbo;
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 DPRINTF (("en_media: %s\n",
142 (bmap_chip[13] & 0x20000000) ? "BNC" : "TP"));
143 if (!(bmap_chip[13] & 0x20000000)) {
144 bmap_chip[12] |= 0x90000000;
145 bmap_chip[13] |= (0x80000000|0x10000000); /* TP */
146 }
147 }
148
149 /* if (turbo) { */
150 /* er->txmode |= EN_TMD_COLLSHIFT; */
151 /* } else { */
152 /* er->txmode &= ~EN_TMD_LB_DISABLE; /\* ZZZ *\/ */
153 /* } */
154
155 er->rxmask = 0;
156 er->rxstat = 0xff;
157 if (turbo) {
158 er->rxmode = EN_RMD_TEST;
159 } else {
160 er->rxmode = EN_RMD_RECV_NORMAL;
161 }
162 for (i=0; i<6; i++)
163 er->addr[i] = desc->myea[i] = MON(char *,MG_clientetheraddr)[i];
164
165 DPRINTF(("ethernet addr (%x:%x:%x:%x:%x:%x)\n",
166 desc->myea[0],desc->myea[1],desc->myea[2],
167 desc->myea[3],desc->myea[4],desc->myea[5]));
168
169 /* if (!turbo) */
170 er->reset = 0;
171 }
172
173 #if 0
174 /* ### remove this when things work! */
175 #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
176 void
177 dump_pkt(unsigned char *pkt, size_t len)
178 {
179 size_t i, j;
180
181 printf("0000: ");
182 for(i=0; i<len; i++) {
183 printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
184 if ((i+1) % 16 == 0) {
185 printf(" %c", '"');
186 for(j=0; j<16; j++)
187 printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
188 printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
189 XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
190 }
191 }
192 printf("\n");
193 }
194 #endif
195
196 int
197 en_put(struct iodesc *desc, void *pkt, size_t len)
198 {
199 volatile struct en_regs *er;
200 volatile struct dma_dev *txdma;
201 int state, txs;
202 int retries;
203
204 DPRINTF(("en_put: %d bytes at 0x%lx\n", len, (unsigned long)pkt));
205 #if 0
206 dump_pkt(pkt,len);
207 #endif
208
209 er = (struct en_regs *)P_ENET;
210 txdma = (struct dma_dev *)P_ENETX_CSR;
211
212 DPRINTF(("en_put: txdma->dd_csr = %x\n",txdma->dd_csr));
213
214 if (len > 1600) {
215 errno = EINVAL;
216 return -1;
217 }
218
219 if (!turbo) {
220 while ((er->txstat & EN_TXS_READY) == 0)
221 printf("en: tx not ready\n");
222 }
223
224 for (retries = 0; retries < EN_RETRIES; retries++) {
225 er->txstat = 0xff;
226 bcopy(pkt, dma_buffers[0], len);
227 txdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
228 DMACSR_RESET | DMACSR_WRITE;
229 txdma->dd_csr = 0;
230 txdma->dd_next/* _initbuf */ = dma_buffers[0];
231 txdma->dd_start = (turbo ? dma_buffers[0] : 0);
232 txdma->dd_limit = ENDMA_ENDALIGN(char *, dma_buffers[0]+len);
233 txdma->dd_stop = 0;
234 txdma->dd_csr = DMACSR_SETENABLE;
235 if (turbo)
236 er->txmode |= 0x80;
237
238 while(1) {
239 if (en_wait_for_intr(ENETX_DMA_INTR)) {
240 errno = EIO;
241 return -1;
242 }
243
244 state = txdma->dd_csr &
245 (DMACSR_BUSEXC | DMACSR_COMPLETE
246 | DMACSR_SUPDATE | DMACSR_ENABLE);
247
248 #if 01
249 DPRINTF(("en_put: dma state = 0x%x.\n", state));
250 #endif
251 if (state & (DMACSR_COMPLETE|DMACSR_BUSEXC))
252 txdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
253 break;
254 }
255
256 txs = er->txstat;
257
258 #if 01
259 DPRINTF(("en_put: done txstat=%x.\n", txs));
260 #endif
261
262 #define EN_TXS_ERROR (EN_TXS_SHORTED | EN_TXS_UNDERFLOW | EN_TXS_PARERR)
263 if ((state & DMACSR_COMPLETE) == 0 ||
264 (txs & EN_TXS_ERROR) != 0) {
265 errno = EIO;
266 return -1;
267 }
268 if ((txs & EN_TXS_COLLERR) == 0)
269 return len; /* success */
270 }
271
272 errno = EIO; /* too many retries */
273 return -1;
274 }
275
276 int
277 en_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
278 {
279 volatile struct en_regs *er;
280 volatile struct dma_dev *rxdma;
281 volatile struct dma_dev *txdma;
282 int state, rxs;
283 size_t rlen;
284 char *gotpkt;
285
286 rxdma = (struct dma_dev *)P_ENETR_CSR;
287 txdma = (struct dma_dev *)P_ENETX_CSR;
288 er = (struct en_regs *)P_ENET;
289
290 DPRINTF(("en_get: rxdma->dd_csr = %x\n",rxdma->dd_csr));
291
292 er->rxstat = 0xff;
293
294 /* this is mouse's code now ... still doesn't work :( */
295 /* The previous comment is now a lie, this does work
296 * Darrin B Jewell <jewell (at) mit.edu> Sat Jan 24 21:44:56 1998
297 */
298
299 rxdma->dd_csr = 0;
300 rxdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
301 DMACSR_READ | DMACSR_RESET;
302
303 if (!turbo) {
304 rxdma->dd_saved_next = 0;
305 rxdma->dd_saved_limit = 0;
306 rxdma->dd_saved_start = 0;
307 rxdma->dd_saved_stop = 0;
308 } else {
309 rxdma->dd_saved_next = dma_buffers[0];
310 }
311
312 rxdma->dd_next = dma_buffers[0];
313 rxdma->dd_limit = DMA_ENDALIGN(char *, dma_buffers[0]+MAX_DMASIZE);
314 #if 0
315 if (turbo) {
316 /* !!! not a typo: txdma */
317 txdma->dd_stop = dma_buffers[0];
318 }
319 #endif
320 rxdma->dd_start = 0;
321 rxdma->dd_stop = 0;
322 rxdma->dd_csr = DMACSR_SETENABLE | DMACSR_READ;
323 if (turbo) {
324 er->rxmode = EN_RMD_TEST | EN_RMD_RECV_MULTI;
325 } else {
326 er->rxmode = EN_RMD_RECV_NORMAL;
327 }
328
329 #if 01
330 DPRINTF(("en_get: blocking on rcv dma\n"));
331 #endif
332
333 while(1) {
334 if (en_wait_for_intr(ENETR_DMA_INTR)) { /* ### use timeout? */
335 errno = EIO;
336 return -1;
337 }
338
339 state = rxdma->dd_csr &
340 (DMACSR_BUSEXC | DMACSR_COMPLETE
341 | DMACSR_SUPDATE | DMACSR_ENABLE);
342 DPRINTF(("en_get: dma state = 0x%x.\n", state));
343 if ((state & DMACSR_ENABLE) == 0) {
344 rxdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
345 break;
346 }
347
348 if (state & DMACSR_COMPLETE) {
349 PRINTF(("en_get: ending dma sequence\n"));
350 rxdma->dd_csr = DMACSR_CLRCOMPLETE;
351 }
352
353 }
354
355 rxs = er->rxstat;
356
357 if ((state & DMACSR_COMPLETE) == 0 ||
358 (rxs & EN_RX_OK) == 0) {
359 errno = EIO;
360 return -1; /* receive failed */
361 }
362
363 if (turbo) {
364 gotpkt = rxdma->dd_saved_next;
365 rlen = rxdma->dd_next - rxdma->dd_saved_next;
366 } else {
367 gotpkt = rxdma->dd_saved_next;
368 rlen = rxdma->dd_next - rxdma->dd_saved_next;
369 }
370
371 if (gotpkt != dma_buffers[0]) {
372 printf("Unexpected received packet location\n");
373 printf("DEBUG: rxstat=%x.\n", rxs);
374 printf("DEBUG: gotpkt = 0x%lx, rlen = %d, len = %d\n",(u_long)gotpkt,rlen,len);
375 printf("DEBUG: rxdma->dd_csr = 0x%lx\n",(u_long)rxdma->dd_csr);
376 printf("DEBUG: rxdma->dd_saved_next = 0x%lx\n",(u_long)rxdma->dd_saved_next);
377 printf("DEBUG: rxdma->dd_saved_limit = 0x%lx\n",(u_long)rxdma->dd_saved_limit);
378 printf("DEBUG: rxdma->dd_saved_start = 0x%lx\n",(u_long)rxdma->dd_saved_start);
379 printf("DEBUG: rxdma->dd_saved_stop = 0x%lx\n",(u_long)rxdma->dd_saved_stop);
380 printf("DEBUG: rxdma->dd_next = 0x%lx\n",(u_long)rxdma->dd_next);
381 printf("DEBUG: rxdma->dd_limit = 0x%lx\n",(u_long)rxdma->dd_limit);
382 printf("DEBUG: rxdma->dd_start = 0x%lx\n",(u_long)rxdma->dd_start);
383 printf("DEBUG: rxdma->dd_stop = 0x%lx\n",(u_long)rxdma->dd_stop);
384 printf("DEBUG: rxdma->dd_next_initbuf = 0x%lx\n",(u_long)rxdma->dd_next_initbuf);
385 }
386
387 #if 0
388 dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
389 #endif
390
391 DPRINTF(("en_get: done rxstat=%x.\n", rxs));
392
393 if (rlen > len) {
394 DPRINTF(("en_get: buffer too small. want %d, got %d\n",
395 len, rlen));
396 rlen = len;
397 }
398
399
400 bcopy(gotpkt, pkt, rlen);
401
402 #if 0
403 printf("DEBUG: gotpkt = 0x%lx, pkt = 0x%lx, rlen = %d\n",
404 (u_long)gotpkt,(u_long)pkt,rlen);
405 dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
406 dump_pkt(pkt, rlen < 255 ? rlen : 128);
407 #endif
408
409 return rlen;
410 }
411
412
413 void
414 en_end(struct netif *a)
415 {
416 DPRINTF(("en_end: WARNING not doing anything\n"));
417 }
418
419 #if 0
420
421 #define MKPANIC(ret,name,args) \
422 ret name args { panic(#name ## ": not implemented.\n"); }
423
424 MKPANIC(void, en_end, (struct netif *a));
425
426 /* device functions */
427
428 int
429 enopen(struct open_file *f, char count, char lun, char part)
430 {
431 int error;
432
433 DPRINTF(("open: en(%d,%d,%d)\n", count, lun, part));
434
435 if (count != 0 || lun != 0 || part != 0)
436 return EUNIT; /* there can be exactly one ethernet */
437
438 if (open_count == 0) {
439 /* Find network interface. */
440 if ((netdev_sock = netif_open(NULL)) < 0)
441 return errno;
442 if ((error = mountroot(netdev_sock)) != 0) {
443 if (open_count == 0)
444 netif_close(netdev_sock);
445 return error;
446 }
447 }
448 open_count++;
449 f->f_devdata = NULL; /* ### nfs_root_node ?! */
450 return 0;
451 }
452
453 int
454 enclose(struct open_file *f)
455 {
456 if (open_count > 0)
457 if (--open_count == 0)
458 netif_close(netdev_sock);
459 f->f_devdata = NULL;
460 return 0;
461 }
462
463 int
464 enstrategy(void *devdata, int rw, daddr_t dblk,
465 size_t size, void *buf, size_t *rsize)
466 {
467 return ENXIO; /* wrong access method */
468 }
469
470 /* private function */
471
472 static int
473 mountroot(int sock)
474 {
475 /* Mount the root directory from a boot server */
476 #if 0
477 struct in_addr in = {
478 0xc2793418
479 };
480 u_char *res;
481
482 res = arpwhohas(socktodesc(sock), in);
483 panic("arpwhohas returned %s", res);
484 #endif
485 /* 1. use bootp. This does most of the work for us. */
486 bootp(sock);
487
488 if (myip.s_addr == 0 || rootip.s_addr == 0 || rootpath[0] == '\0')
489 return ETIMEDOUT;
490
491 printf("Using IP address: %s\n", inet_ntoa(myip));
492 printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
493
494 /* 2. mount. */
495 if (nfs_mount(sock, rootip, rootpath) < 0)
496 return errno;
497
498 return 0;
499 }
500 #endif
501
502 static int
503 en_wait_for_intr(int flag)
504 {
505 volatile int *intrstat = MON(volatile int *,MG_intrstat);
506
507 int count;
508
509 for(count = 0; count < EN_TIMEOUT; count++)
510 if (*intrstat & flag)
511 return 0;
512
513 printf("enintr: timed out.\n");
514 return -1;
515 }
516