en.c revision 1.19 1 /* $NetBSD: en.c,v 1.19 2018/03/08 03:12:02 mrg 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(struct netif *, void *);
62 int en_probe(struct netif *, void *);
63 void en_init(struct iodesc *, void *);
64 int en_get(struct iodesc *, void *, size_t, saseconds_t);
65 int en_put(struct iodesc *, void *, size_t);
66 void en_end(struct netif *);
67
68 static int en_wait_for_intr(int);
69
70 struct netif_stats en_stats;
71
72 struct netif_dif en_ifs[] = {
73 /* dif_unit dif_nsel dif_stats dif_private */
74 { 0, 1, &en_stats, NULL, },
75 };
76
77 struct netif_driver en_driver = {
78 "en",
79 en_match, en_probe, en_init, en_get, en_put, en_end,
80 en_ifs, sizeof(en_ifs) / sizeof(en_ifs[0])
81 };
82
83 extern int turbo;
84
85 /* ### int netdev_sock;
86 static int open_count; */
87
88 char dma_buffer1[MAX_DMASIZE+DMA_ENDALIGNMENT],
89 dma_buffer2[MAX_DMASIZE+DMA_ENDALIGNMENT],
90 *dma_buffers[2];
91 int next_dma_buffer;
92
93
94 int
95 en_match(struct netif *nif, void *machdep_hint)
96 {
97 /* we always match, because every NeXT has an ethernet interface
98 * and we've checked the unit numbers before we even started this
99 * search.
100 * ### now that open is generic, we should check the params!
101 */
102 return 1;
103 }
104
105 int
106 en_probe(struct netif *nif, void *machdep_hint)
107 {
108 /* we also always probe ok, see en_match. */
109 return 0;
110 }
111
112 void
113 en_init(struct iodesc *desc, void *machdep_hint)
114 {
115 volatile struct en_regs *er;
116 volatile u_int *bmap_chip;
117 int i;
118
119 DPRINTF(("en_init\n"));
120
121 er = (struct en_regs *)P_ENET;
122 bmap_chip = (u_int *)P_BMAP;
123
124 dma_buffers[0] = DMA_ALIGN(char *, dma_buffer1);
125 dma_buffers[1] = DMA_ALIGN(char *, dma_buffer2);
126
127 er->reset = EN_RST_RESET;
128 /* if (turbo) */
129 /* er->reset = 0; */
130
131 er->txmask = 0;
132 er->txstat = 0xff;
133 if (turbo)
134 er->txmode = 0 | EN_TMD_COLLSHIFT;
135 else
136 er->txmode = EN_TMD_LB_DISABLE;
137
138 /* setup for bnc/tp */
139 if (!turbo) {
140 DPRINTF (("en_media: %s\n",
141 (bmap_chip[13] & 0x20000000) ? "BNC" : "TP"));
142 if (!(bmap_chip[13] & 0x20000000)) {
143 bmap_chip[12] |= 0x90000000;
144 bmap_chip[13] |= (0x80000000|0x10000000); /* TP */
145 }
146 }
147
148 /* if (turbo) { */
149 /* er->txmode |= EN_TMD_COLLSHIFT; */
150 /* } else { */
151 /* er->txmode &= ~EN_TMD_LB_DISABLE; /\* ZZZ *\/ */
152 /* } */
153
154 er->rxmask = 0;
155 er->rxstat = 0xff;
156 if (turbo)
157 er->rxmode = EN_RMD_TEST | EN_RMD_RECV_NORMAL;
158 else
159 er->rxmode = EN_RMD_RECV_NORMAL;
160 for (i=0; i<6; i++)
161 er->addr[i] = desc->myea[i] = MON(char *,MG_clientetheraddr)[i];
162
163 DPRINTF(("ethernet addr (%x:%x:%x:%x:%x:%x)\n",
164 desc->myea[0],desc->myea[1],desc->myea[2],
165 desc->myea[3],desc->myea[4],desc->myea[5]));
166
167 /* if (!turbo) */
168 er->reset = 0;
169 }
170
171 #if 0
172 /* ### remove this when things work! */
173 #define XCHR(x) hexdigits[(x) & 0xf]
174 void
175 dump_pkt(unsigned char *pkt, size_t len)
176 {
177 size_t i, j;
178
179 printf("0000: ");
180 for(i=0; i<len; i++) {
181 printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
182 if ((i+1) % 16 == 0) {
183 printf(" %c", '"');
184 for(j=0; j<16; j++)
185 printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
186 printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
187 XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
188 }
189 }
190 printf("\n");
191 }
192 #endif
193
194 int
195 en_put(struct iodesc *desc, void *pkt, size_t len)
196 {
197 volatile struct en_regs *er;
198 volatile struct dma_dev *txdma;
199 int state, txs;
200 int retries;
201
202 DPRINTF(("en_put: %d bytes at 0x%lx\n", len, (unsigned long)pkt));
203 #if 0
204 dump_pkt(pkt,len);
205 #endif
206
207 er = (struct en_regs *)P_ENET;
208 txdma = (struct dma_dev *)P_ENETX_CSR;
209
210 DPRINTF(("en_put: txdma->dd_csr = %x\n",txdma->dd_csr));
211
212 if (len > 1600) {
213 errno = EINVAL;
214 return -1;
215 }
216
217 if (!turbo) {
218 while ((er->txstat & EN_TXS_READY) == 0)
219 printf("en: tx not ready\n");
220 }
221
222 for (retries = 0; retries < EN_RETRIES; retries++) {
223 er->txstat = 0xff;
224 memcpy(dma_buffers[0], pkt, len);
225 txdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
226 DMACSR_RESET | DMACSR_WRITE;
227 txdma->dd_csr = 0;
228 txdma->dd_next/* _initbuf */ = dma_buffers[0];
229 txdma->dd_start = (turbo ? dma_buffers[0] : 0);
230 txdma->dd_limit = ENDMA_ENDALIGN(char *, dma_buffers[0]+len);
231 txdma->dd_stop = 0;
232 txdma->dd_csr = DMACSR_SETENABLE;
233 if (turbo)
234 er->txmode |= 0x80;
235
236 while (1) {
237 if (en_wait_for_intr(ENETX_DMA_INTR)) {
238 printf("en_put: timed out\n");
239 errno = EIO;
240 return -1;
241 }
242
243 state = txdma->dd_csr &
244 (DMACSR_BUSEXC | DMACSR_COMPLETE
245 | DMACSR_SUPDATE | DMACSR_ENABLE);
246
247 #if 01
248 DPRINTF(("en_put: DMA state = 0x%x.\n", state));
249 #endif
250 if (state & (DMACSR_COMPLETE|DMACSR_BUSEXC)) {
251 txdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
252 break;
253 }
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, saseconds_t timeout)
278 {
279 volatile struct en_regs *er;
280 volatile struct dma_dev *rxdma;
281 #if 0
282 volatile struct dma_dev *txdma;
283 #endif
284 int state, rxs;
285 size_t rlen;
286 char *gotpkt;
287
288 rxdma = (struct dma_dev *)P_ENETR_CSR;
289 #if 0
290 txdma = (struct dma_dev *)P_ENETX_CSR;
291 #endif
292 er = (struct en_regs *)P_ENET;
293
294 DPRINTF(("en_get: rxdma->dd_csr = %x\n",rxdma->dd_csr));
295
296 er->rxstat = 0xff;
297
298 /* this is mouse's code now ... still doesn't work :( */
299 /* The previous comment is now a lie, this does work
300 * Darrin B Jewell <jewell (at) mit.edu> Sat Jan 24 21:44:56 1998
301 */
302
303 rxdma->dd_csr = 0;
304 rxdma->dd_csr = (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
305 DMACSR_READ | DMACSR_RESET;
306
307 if (!turbo) {
308 rxdma->dd_saved_next = 0;
309 rxdma->dd_saved_limit = 0;
310 rxdma->dd_saved_start = 0;
311 rxdma->dd_saved_stop = 0;
312 } else {
313 rxdma->dd_saved_next = dma_buffers[0];
314 }
315
316 rxdma->dd_next = dma_buffers[0];
317 rxdma->dd_limit = DMA_ENDALIGN(char *, dma_buffers[0]+MAX_DMASIZE);
318 #if 0
319 if (turbo) {
320 /* !!! not a typo: txdma */
321 txdma->dd_stop = dma_buffers[0];
322 }
323 #endif
324 rxdma->dd_start = 0;
325 rxdma->dd_stop = 0;
326 rxdma->dd_csr = DMACSR_SETENABLE | DMACSR_READ;
327 if (turbo)
328 er->rxmode = EN_RMD_TEST | EN_RMD_RECV_NORMAL;
329 else
330 er->rxmode = EN_RMD_RECV_NORMAL;
331
332 #if 01
333 DPRINTF(("en_get: blocking on rcv DMA\n"));
334 #endif
335
336 while (1) {
337 if (en_wait_for_intr(ENETR_DMA_INTR)) /* ### use timeout? */
338 return 0;
339
340 state = rxdma->dd_csr &
341 (DMACSR_BUSEXC | DMACSR_COMPLETE
342 | DMACSR_SUPDATE | DMACSR_ENABLE);
343 DPRINTF(("en_get: DMA state = 0x%x.\n", state));
344 if ((state & DMACSR_ENABLE) == 0) {
345 rxdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
346 break;
347 }
348
349 if (state & DMACSR_COMPLETE) {
350 PRINTF(("en_get: ending DMA sequence\n"));
351 rxdma->dd_csr = DMACSR_CLRCOMPLETE;
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 memcpy(pkt, gotpkt, rlen);
400
401 #if 0
402 printf("DEBUG: gotpkt = 0x%lx, pkt = 0x%lx, rlen = %d\n",
403 (u_long)gotpkt,(u_long)pkt,rlen);
404 dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
405 dump_pkt(pkt, rlen < 255 ? rlen : 128);
406 #endif
407
408 return rlen;
409 }
410
411
412 void
413 en_end(struct netif *a)
414 {
415 DPRINTF(("en_end: WARNING not doing anything\n"));
416 }
417
418 #if 0
419
420 #define MKPANIC(ret,name,args) \
421 ret name args { panic(#name ## ": not implemented.\n"); }
422
423 MKPANIC(void, en_end, (struct netif *a));
424
425 /* device functions */
426
427 int
428 enopen(struct open_file *f, char count, char lun, char part)
429 {
430 int error;
431
432 DPRINTF(("open: en(%d,%d,%d)\n", count, lun, part));
433
434 if (count != 0 || lun != 0 || part != 0)
435 return EUNIT; /* there can be exactly one ethernet */
436
437 if (open_count == 0) {
438 /* Find network interface. */
439 if ((netdev_sock = netif_open(NULL)) < 0)
440 return errno;
441 if ((error = mountroot(netdev_sock)) != 0) {
442 if (open_count == 0)
443 netif_close(netdev_sock);
444 return error;
445 }
446 }
447 open_count++;
448 f->f_devdata = NULL; /* ### nfs_root_node ?! */
449 return 0;
450 }
451
452 int
453 enclose(struct open_file *f)
454 {
455 if (open_count > 0)
456 if (--open_count == 0)
457 netif_close(netdev_sock);
458 f->f_devdata = NULL;
459 return 0;
460 }
461
462 int
463 enstrategy(void *devdata, int rw, daddr_t dblk,
464 size_t size, void *buf, size_t *rsize)
465 {
466 return ENXIO; /* wrong access method */
467 }
468
469 /* private function */
470
471 static int
472 mountroot(int sock)
473 {
474 /* Mount the root directory from a boot server */
475 #if 0
476 struct in_addr in = {
477 0xc2793418
478 };
479 u_char *res;
480
481 res = arpwhohas(socktodesc(sock), in);
482 panic("arpwhohas returned %s", res);
483 #endif
484 /* 1. use bootp. This does most of the work for us. */
485 bootp(sock);
486
487 if (myip.s_addr == 0 || rootip.s_addr == 0 || rootpath[0] == '\0')
488 return ETIMEDOUT;
489
490 printf("Using IP address: %s\n", inet_ntoa(myip));
491 printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
492
493 /* 2. mount. */
494 if (nfs_mount(sock, rootip, rootpath) < 0)
495 return errno;
496
497 return 0;
498 }
499 #endif
500
501 static int
502 en_wait_for_intr(int flag)
503 {
504 volatile int *intrstat = MON(volatile int *, MG_intrstat);
505
506 int count;
507
508 for (count = 0; count < EN_TIMEOUT; count++)
509 if (*intrstat & flag)
510 return 0;
511
512 return -1;
513 }
514