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