en.c revision 1.3 1 /* $NetBSD: en.c,v 1.3 1999/05/07 16:19:28 drochner 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 #ifdef EN_DEBUG
52 #define DPRINTF(x) printf x;
53 #else
54 #define DPRINTF(x)
55 #endif
56
57 #define EN_TIMEOUT 2000000
58 #define EN_RETRIES 10
59
60 int en_match __P((struct netif *nif, void *machdep_hint));
61 int en_probe __P((struct netif *nif, void *machdep_hint));
62 void en_init __P((struct iodesc *desc, void *machdep_hint));
63 int en_get __P((struct iodesc *a, void *b, size_t c, time_t d));
64 int en_put __P((struct iodesc *a, void *b, size_t c));
65 void en_end __P((struct netif *a));
66
67 /* ### static int mountroot __P((int sock)); */
68 static int en_wait_for_intr __P((int flag));
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, NENTS(en_ifs)
81 };
82
83 /* ### int netdev_sock;
84 static int open_count; */
85
86 char dma_buffer1[MAX_DMASIZE+DMA_ENDALIGNMENT],
87 dma_buffer2[MAX_DMASIZE+DMA_ENDALIGNMENT],
88 *dma_buffers[2];
89 int next_dma_buffer;
90
91
92 int
93 en_match(struct netif *nif, void *machdep_hint)
94 {
95 /* we always match, because every NeXT has an ethernet interface
96 * and we've checked the unit numbers before we even started this
97 * search.
98 * ### now that open is generic, we should check the params!
99 */
100 return 1;
101 }
102
103 int
104 en_probe(struct netif *nif, void *machdep_hint)
105 {
106 /* we also always probe ok, see en_match. */
107 return 0;
108 }
109
110 void
111 en_init(struct iodesc *desc, void *machdep_hint)
112 {
113 volatile struct en_regs *er;
114 volatile u_int *bmap_chip;
115 int i;
116
117 DPRINTF(("en_init\n"));
118
119 er = (struct en_regs *)P_ENET;
120 bmap_chip = (u_int *)P_BMAP;
121
122 dma_buffers[0] = DMA_ALIGN(char *, dma_buffer1);
123 dma_buffers[1] = DMA_ALIGN(char *, dma_buffer2);
124
125 er->reset = EN_RST_RESET;
126
127 /* ### we'll need this when we need to decide which interface to use
128 bmap_chip[12] = 0x90000000;
129 bmap_chip[13] = ~(0x80000000|0x10000000); * BMAP_TPE ???
130 */
131
132 er->txmode = EN_TMD_LB_DISABLE;
133 er->txmask = 0;
134 er->txstat = 0xff;
135 er->rxmode = EN_RMD_RECV_NORMAL;
136 er->rxmask = 0;
137 er->rxstat = 0xff;
138 for (i=0; i<6; i++)
139 er->addr[i] = desc->myea[i] = MON(char *,MG_clientetheraddr)[i];
140
141 DPRINTF(("ethernet addr (%x:%x:%x:%x:%x:%x)\n",
142 desc->myea[0],desc->myea[1],desc->myea[2],
143 desc->myea[3],desc->myea[4],desc->myea[5]));
144
145 er->reset = 0;
146 }
147
148 #if 0
149 /* ### remove this when things work! */
150 #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
151 void
152 dump_pkt(unsigned char *pkt, size_t len)
153 {
154 size_t i, j;
155
156 printf("0000: ");
157 for(i=0; i<len; i++) {
158 printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
159 if ((i+1) % 16 == 0) {
160 printf(" %c", '"');
161 for(j=0; j<16; j++)
162 printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
163 printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
164 XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
165 }
166 }
167 printf("\n");
168 }
169 #endif
170
171 int
172 en_put(struct iodesc *desc, void *pkt, size_t len)
173 {
174 volatile struct en_regs *er;
175 volatile struct dma_dev *txdma;
176 int state, txs;
177 int retries;
178
179 DPRINTF(("en_put: %d bytes at 0x%lx\n", len, (unsigned long)pkt));
180 #if 0
181 dump_pkt(pkt,len);
182 #endif
183
184 er = (struct en_regs *)P_ENET;
185 txdma = (struct dma_dev *)P_ENETX_CSR;
186
187 DPRINTF(("en_put: txdma->dd_csr = %x\n",txdma->dd_csr));
188
189 if (len > 1600) {
190 errno = EINVAL;
191 return -1;
192 }
193
194 while ((er->txstat & EN_TXS_READY) == 0)
195 printf("en: tx not ready\n");
196
197 for (retries = 0; retries < EN_RETRIES; retries++) {
198 er->txstat = 0xff;
199 bcopy(pkt, dma_buffers[0], len);
200 txdma->dd_csr = 0;
201 txdma->dd_csr = DMACSR_INITBUF | DMACSR_RESET | DMACSR_WRITE;
202 txdma->dd_next_initbuf = dma_buffers[0];
203 txdma->dd_limit = ENDMA_ENDALIGN(char *, dma_buffers[0]+len);
204 txdma->dd_start = 0;
205 txdma->dd_stop = 0;
206 txdma->dd_csr = DMACSR_SETENABLE;
207
208 while(1) {
209 if (en_wait_for_intr(ENETX_DMA_INTR)) {
210 errno = EIO;
211 return -1;
212 }
213
214 state = txdma->dd_csr &
215 (DMACSR_BUSEXC | DMACSR_COMPLETE
216 | DMACSR_SUPDATE | DMACSR_ENABLE);
217
218 #if 0
219 DPRINTF(("en_put: dma state = 0x%x.\n", state));
220 #endif
221 if (state & (DMACSR_COMPLETE|DMACSR_BUSEXC))
222 txdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
223 break;
224 }
225
226 txs = er->txstat;
227
228 #if 0
229 DPRINTF(("en_put: done txstat=%x.\n", txs));
230 #endif
231
232 #define EN_TXS_ERROR (EN_TXS_SHORTED | EN_TXS_UNDERFLOW | EN_TXS_PARERR)
233 if ((state & DMACSR_COMPLETE) == 0 ||
234 (txs & EN_TXS_ERROR) != 0) {
235 errno = EIO;
236 return -1;
237 }
238 if ((txs & EN_TXS_COLLERR) == 0)
239 return len; /* success */
240 }
241
242 errno = EIO; /* too many retries */
243 return -1;
244 }
245
246 int
247 en_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
248 {
249 volatile struct en_regs *er;
250 volatile struct dma_dev *rxdma;
251 int state, rxs;
252 size_t rlen;
253 char *gotpkt;
254
255 rxdma = (struct dma_dev *)P_ENETR_CSR;
256 er = (struct en_regs *)P_ENET;
257
258 er->rxstat = 0xff;
259
260 /* this is mouse's code now ... still doesn't work :( */
261 /* The previous comment is now a lie, this does work
262 * Darrin B Jewell <jewell (at) mit.edu> Sat Jan 24 21:44:56 1998
263 */
264
265 rxdma->dd_csr = 0;
266 rxdma->dd_csr = DMACSR_INITBUF | DMACSR_READ | DMACSR_RESET;
267
268 rxdma->dd_saved_next = 0;
269 rxdma->dd_saved_limit = 0;
270 rxdma->dd_saved_start = 0;
271 rxdma->dd_saved_stop = 0;
272
273 rxdma->dd_next = dma_buffers[0];
274 rxdma->dd_limit = DMA_ENDALIGN(char *, dma_buffers[0]+MAX_DMASIZE);
275 rxdma->dd_start = 0;
276 rxdma->dd_stop = 0;
277 rxdma->dd_csr = DMACSR_SETENABLE;
278
279 #if 0
280 DPRINTF(("en_get: blocking on rcv dma\n"));
281 #endif
282
283 while(1) {
284 if (en_wait_for_intr(ENETR_DMA_INTR)) { /* ### use timeout? */
285 errno = EIO;
286 return -1;
287 }
288
289 state = rxdma->dd_csr &
290 (DMACSR_BUSEXC | DMACSR_COMPLETE
291 | DMACSR_SUPDATE | DMACSR_ENABLE);
292 DPRINTF(("en_get: dma state = 0x%x.\n", state));
293 if ((state & DMACSR_ENABLE) == 0) {
294 rxdma->dd_csr = DMACSR_RESET | DMACSR_CLRCOMPLETE;
295 break;
296 }
297
298 if (state & DMACSR_COMPLETE) {
299 DPRINTF(("en_get: ending dma sequence\n"));
300 rxdma->dd_csr = DMACSR_CLRCOMPLETE;
301 }
302
303 }
304
305 rxs = er->rxstat;
306
307 if ((state & DMACSR_COMPLETE) == 0 ||
308 (rxs & EN_RX_OK) == 0) {
309 errno = EIO;
310 return -1; /* receive failed */
311 }
312
313 gotpkt = rxdma->dd_saved_next;
314 rlen = rxdma->dd_next - rxdma->dd_saved_next;
315
316 if (gotpkt != dma_buffers[0]) {
317 printf("Unexpected received packet location\n");
318 printf("DEBUG: rxstat=%x.\n", rxs);
319 printf("DEBUG: gotpkt = 0x%lx, rlen = %d, len = %d\n",(u_long)gotpkt,rlen,len);
320 printf("DEBUG: rxdma->dd_csr = 0x%lx\n",(u_long)rxdma->dd_csr);
321 printf("DEBUG: rxdma->dd_saved_next = 0x%lx\n",(u_long)rxdma->dd_saved_next);
322 printf("DEBUG: rxdma->dd_saved_limit = 0x%lx\n",(u_long)rxdma->dd_saved_limit);
323 printf("DEBUG: rxdma->dd_saved_start = 0x%lx\n",(u_long)rxdma->dd_saved_start);
324 printf("DEBUG: rxdma->dd_saved_stop = 0x%lx\n",(u_long)rxdma->dd_saved_stop);
325 printf("DEBUG: rxdma->dd_next = 0x%lx\n",(u_long)rxdma->dd_next);
326 printf("DEBUG: rxdma->dd_limit = 0x%lx\n",(u_long)rxdma->dd_limit);
327 printf("DEBUG: rxdma->dd_start = 0x%lx\n",(u_long)rxdma->dd_start);
328 printf("DEBUG: rxdma->dd_stop = 0x%lx\n",(u_long)rxdma->dd_stop);
329 printf("DEBUG: rxdma->dd_next_initbuf = 0x%lx\n",(u_long)rxdma->dd_next_initbuf);
330 }
331
332 #if 0
333 dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
334 #endif
335
336 DPRINTF(("en_get: done rxstat=%x.\n", rxs));
337
338 if (rlen > len) {
339 DPRINTF(("en_get: buffer too small. want %d, got %d\n",
340 len, rlen));
341 rlen = len;
342 }
343
344
345 bcopy(gotpkt, pkt, rlen);
346
347 #if 0
348 printf("DEBUG: gotpkt = 0x%lx, pkt = 0x%lx, rlen = %d\n",
349 (u_long)gotpkt,(u_long)pkt,rlen);
350 dump_pkt(gotpkt, rlen < 255 ? rlen : 128);
351 dump_pkt(pkt, rlen < 255 ? rlen : 128);
352 #endif
353
354 return rlen;
355 }
356
357
358 void
359 en_end(struct netif *a)
360 {
361 DPRINTF(("en_end: WARNING not doing anything\n"));
362 }
363
364 #if 0
365
366 #define MKPANIC(ret,name,args) \
367 ret name args { panic(#name ## ": not implemented.\n"); }
368
369 MKPANIC(void, en_end, (struct netif *a));
370
371 /* device functions */
372
373 int
374 enopen(struct open_file *f, char count, char lun, char part)
375 {
376 int error;
377
378 DPRINTF(("open: en(%d,%d,%d)\n", count, lun, part));
379
380 if (count != 0 || lun != 0 || part != 0)
381 return EUNIT; /* there can be exactly one ethernet */
382
383 if (open_count == 0) {
384 /* Find network interface. */
385 if ((netdev_sock = netif_open(NULL)) < 0)
386 return errno;
387 if ((error = mountroot(netdev_sock)) != 0) {
388 if (open_count == 0)
389 netif_close(netdev_sock);
390 return error;
391 }
392 }
393 open_count++;
394 f->f_devdata = NULL; /* ### nfs_root_node ?! */
395 return 0;
396 }
397
398 int
399 enclose(struct open_file *f)
400 {
401 if (open_count > 0)
402 if (--open_count == 0)
403 netif_close(netdev_sock);
404 f->f_devdata = NULL;
405 return 0;
406 }
407
408 int
409 enstrategy(void *devdata, int rw, daddr_t dblk,
410 size_t size, void *buf, size_t *rsize)
411 {
412 return ENXIO; /* wrong access method */
413 }
414
415 /* private function */
416
417 static int
418 mountroot(int sock)
419 {
420 /* Mount the root directory from a boot server */
421 #if 0
422 struct in_addr in = {
423 0xc2793418
424 };
425 u_char *res;
426
427 res = arpwhohas(socktodesc(sock), in);
428 panic("arpwhohas returned %s", res);
429 #endif
430 /* 1. use bootp. This does most of the work for us. */
431 bootp(sock);
432
433 if (myip.s_addr == 0 || rootip.s_addr == 0 || rootpath[0] == '\0')
434 return ETIMEDOUT;
435
436 printf("Using IP address: %s\n", inet_ntoa(myip));
437 printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
438
439 /* 2. mount. */
440 if (nfs_mount(sock, rootip, rootpath) < 0)
441 return errno;
442
443 return 0;
444 }
445 #endif
446
447 static int
448 en_wait_for_intr(int flag)
449 {
450 volatile int *intrstat = MON(volatile int *,MG_intrstat);
451
452 int count;
453
454 for(count = 0; count < EN_TIMEOUT; count++)
455 if (*intrstat & flag)
456 return 0;
457
458 printf("enintr: timed out.\n");
459 return -1;
460 }
461