if_ae.c revision 1.21 1 1.21 briggs /* $NetBSD: if_ae.c,v 1.21 1995/04/12 15:01:14 briggs Exp $ */
2 1.14 cgd
3 1.1 briggs /*
4 1.21 briggs * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
5 1.21 briggs * adapters.
6 1.1 briggs *
7 1.21 briggs * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
8 1.1 briggs *
9 1.21 briggs * Copyright (C) 1993, David Greenman. This software may be used, modified,
10 1.21 briggs * copied, distributed, and sold, in both source and binary form provided that
11 1.21 briggs * the above copyright and these terms are retained. Under no circumstances is
12 1.21 briggs * the author responsible for the proper functioning of this software, nor does
13 1.21 briggs * the author assume any responsibility for damages incurred with its use.
14 1.1 briggs *
15 1.21 briggs * Adapted for MacBSD by Brad Parker <brad (at) fcr.com>.
16 1.1 briggs *
17 1.1 briggs * Currently supports:
18 1.1 briggs * Apples NB Ethernet card
19 1.1 briggs * Interlan A310 Nubus Ethernet card
20 1.1 briggs * Cayman Systems GatorCard
21 1.15 briggs * Asante MacCon II/E
22 1.1 briggs */
23 1.1 briggs
24 1.1 briggs #include "bpfilter.h"
25 1.1 briggs
26 1.9 briggs #include <sys/param.h>
27 1.9 briggs #include <sys/systm.h>
28 1.9 briggs #include <sys/errno.h>
29 1.9 briggs #include <sys/ioctl.h>
30 1.9 briggs #include <sys/mbuf.h>
31 1.9 briggs #include <sys/socket.h>
32 1.9 briggs #include <sys/syslog.h>
33 1.21 briggs #include <sys/device.h>
34 1.1 briggs
35 1.5 briggs #include <net/if.h>
36 1.5 briggs #include <net/if_dl.h>
37 1.5 briggs #include <net/if_types.h>
38 1.5 briggs #include <net/netisr.h>
39 1.1 briggs
40 1.1 briggs #ifdef INET
41 1.5 briggs #include <netinet/in.h>
42 1.5 briggs #include <netinet/in_systm.h>
43 1.5 briggs #include <netinet/in_var.h>
44 1.5 briggs #include <netinet/ip.h>
45 1.5 briggs #include <netinet/if_ether.h>
46 1.1 briggs #endif
47 1.1 briggs
48 1.1 briggs #ifdef NS
49 1.5 briggs #include <netns/ns.h>
50 1.5 briggs #include <netns/ns_if.h>
51 1.1 briggs #endif
52 1.1 briggs
53 1.1 briggs #if NBPFILTER > 0
54 1.5 briggs #include <net/bpf.h>
55 1.5 briggs #include <net/bpfdesc.h>
56 1.1 briggs #endif
57 1.1 briggs
58 1.18 briggs #include "../mac68k/via.h"
59 1.3 briggs #include "nubus.h"
60 1.1 briggs #include "if_aereg.h"
61 1.1 briggs
62 1.1 briggs /*
63 1.1 briggs * ae_softc: per line info and status
64 1.1 briggs */
65 1.1 briggs struct ae_softc {
66 1.21 briggs struct device sc_dev;
67 1.21 briggs /* struct nubusdev sc_nu;
68 1.21 briggs struct intrhand sc_ih; */
69 1.3 briggs
70 1.21 briggs struct arpcom sc_arpcom; /* ethernet common */
71 1.1 briggs
72 1.1 briggs char *type_str; /* pointer to type string */
73 1.1 briggs u_char vendor; /* interface vendor */
74 1.1 briggs u_char type; /* interface type code */
75 1.15 briggs u_char regs_rev; /* registers are reversed */
76 1.15 briggs
77 1.15 briggs #define REG_MAP(sc, reg) ((sc)->regs_rev ? (0x0f-(reg))<<2 : (reg)<<2)
78 1.1 briggs #define NIC_GET(sc, reg) ((sc)->nic_addr[REG_MAP(sc, reg)])
79 1.1 briggs #define NIC_PUT(sc, reg, val) ((sc)->nic_addr[REG_MAP(sc, reg)] = (val))
80 1.1 briggs volatile caddr_t nic_addr; /* NIC (DS8390) I/O bus address */
81 1.1 briggs caddr_t rom_addr; /* on board prom address */
82 1.1 briggs
83 1.21 briggs u_char cr_proto; /* values always set in CR */
84 1.21 briggs
85 1.21 briggs caddr_t mem_start; /* shared memory start address */
86 1.21 briggs caddr_t mem_end; /* shared memory end address */
87 1.21 briggs u_long mem_size; /* total shared memory size */
88 1.21 briggs caddr_t mem_ring; /* start of RX ring-buffer (in smem) */
89 1.21 briggs
90 1.21 briggs u_char mem_wr_short; /* card memory requires int16 writes */
91 1.1 briggs
92 1.1 briggs u_char xmit_busy; /* transmitter is busy */
93 1.1 briggs u_char txb_cnt; /* Number of transmit buffers */
94 1.21 briggs u_char txb_inuse; /* number of TX buffers currently in-use*/
95 1.21 briggs
96 1.21 briggs u_char txb_new; /* pointer to where new buffer will be added */
97 1.21 briggs u_char txb_next_tx; /* pointer to next buffer ready to xmit */
98 1.21 briggs u_short txb_len[8]; /* buffered xmit buffer lengths */
99 1.1 briggs u_char tx_page_start; /* first page of TX buffer area */
100 1.1 briggs u_char rec_page_start; /* first page of RX ring-buffer */
101 1.1 briggs u_char rec_page_stop; /* last page of RX ring-buffer */
102 1.1 briggs u_char next_packet; /* pointer to next unread RX packet */
103 1.21 briggs };
104 1.1 briggs
105 1.21 briggs int ae_probe __P((struct device *, void *, void *));
106 1.21 briggs void ae_attach __P((struct device *, struct device *, void *));
107 1.21 briggs int ae_intr __P((int));
108 1.21 briggs int ae_ioctl __P((struct ifnet *, u_long, caddr_t));
109 1.21 briggs void ae_start __P((struct ifnet *));
110 1.21 briggs void ae_watchdog __P((/* short */));
111 1.21 briggs void ae_reset __P((struct ae_softc *));
112 1.21 briggs void ae_init __P((struct ae_softc *));
113 1.21 briggs void ae_stop __P((struct ae_softc *));
114 1.21 briggs void ae_getmcaf __P((struct arpcom *, u_long *));
115 1.21 briggs u_short ae_put __P((struct ae_softc *, struct mbuf *, caddr_t));
116 1.21 briggs
117 1.21 briggs #define inline /* XXX for debugging porpoises */
118 1.21 briggs
119 1.21 briggs void ae_get_packet __P((struct ae_softc *, caddr_t, u_short));
120 1.21 briggs static inline void ae_rint __P((struct ae_softc *));
121 1.21 briggs static inline void ae_xmit __P((struct ae_softc *));
122 1.21 briggs static inline caddr_t ae_ring_copy __P((/* struct ae_softc *, caddr_t, caddr_t,
123 1.21 briggs u_short */));
124 1.1 briggs
125 1.21 briggs struct cfdriver aecd = {
126 1.21 briggs NULL, "ae", ae_probe, ae_attach, DV_IFNET, sizeof(struct ae_softc)
127 1.21 briggs };
128 1.1 briggs
129 1.1 briggs #define ETHER_MIN_LEN 64
130 1.1 briggs #define ETHER_MAX_LEN 1518
131 1.1 briggs #define ETHER_ADDR_LEN 6
132 1.1 briggs
133 1.1 briggs char ae_name[] = "8390 Nubus Ethernet card";
134 1.1 briggs static char zero = 0;
135 1.1 briggs static u_char ones = 0xff;
136 1.1 briggs
137 1.8 briggs struct vendor_S {
138 1.8 briggs char *manu;
139 1.8 briggs int len;
140 1.8 briggs int vendor;
141 1.8 briggs } vend[] = {
142 1.8 briggs { "Apple", 5, AE_VENDOR_APPLE },
143 1.8 briggs { "3Com", 4, AE_VENDOR_APPLE },
144 1.8 briggs { "Dayna", 5, AE_VENDOR_DAYNA },
145 1.8 briggs { "Inter", 5, AE_VENDOR_INTERLAN },
146 1.15 briggs { "Asant", 5, AE_VENDOR_ASANTE },
147 1.8 briggs };
148 1.8 briggs
149 1.8 briggs static int numvend = sizeof(vend)/sizeof(vend[0]);
150 1.8 briggs
151 1.12 lkestel /*
152 1.12 lkestel * XXX These two should be moved to locore, and maybe changed to use shorts
153 1.12 lkestel * instead of bytes. The reason for these is that bcopy and bzero use longs,
154 1.12 lkestel * which the ethernet cards can't handle.
155 1.12 lkestel */
156 1.12 lkestel
157 1.12 lkestel void
158 1.21 briggs bszero(u_short *addr, int len)
159 1.12 lkestel {
160 1.21 briggs
161 1.21 briggs while (len--)
162 1.12 lkestel *addr++ = 0;
163 1.12 lkestel }
164 1.12 lkestel
165 1.21 briggs /*
166 1.21 briggs * Memory copy, copies word at time.
167 1.21 briggs */
168 1.21 briggs #if 1
169 1.21 briggs static inline void
170 1.21 briggs word_copy(a, b, len)
171 1.21 briggs caddr_t a, b;
172 1.21 briggs int len;
173 1.12 lkestel {
174 1.21 briggs u_short *x = (u_short *)a,
175 1.21 briggs *y = (u_short *)b;
176 1.12 lkestel
177 1.21 briggs len >>= 1;
178 1.21 briggs while (len--)
179 1.21 briggs *y++ = *x++;
180 1.21 briggs }
181 1.21 briggs #else
182 1.21 briggs static inline void
183 1.21 briggs word_copy(src, dest, len)
184 1.21 briggs caddr_t src, dest;
185 1.21 briggs int len;
186 1.15 briggs {
187 1.15 briggs u_short *d = (u_short *)dest;
188 1.15 briggs u_short *s = (u_short *)src;
189 1.15 briggs char b1, b2;
190 1.15 briggs
191 1.15 briggs /* odd case, src addr is unaligned */
192 1.15 briggs if ( ((u_long)src) & 1 ) {
193 1.15 briggs while (len > 0) {
194 1.15 briggs b1 = *src++;
195 1.15 briggs b2 = len > 1 ? *src++ : (*d & 0xff);
196 1.15 briggs *d++ = (b1 << 8) | b2;
197 1.15 briggs len -= 2;
198 1.15 briggs }
199 1.15 briggs return;
200 1.15 briggs }
201 1.15 briggs
202 1.15 briggs /* normal case, aligned src & dst */
203 1.15 briggs while (len > 0) {
204 1.15 briggs *d++ = *s++;
205 1.15 briggs len -= 2;
206 1.15 briggs }
207 1.15 briggs }
208 1.21 briggs #endif
209 1.15 briggs
210 1.8 briggs void
211 1.8 briggs ae_id_card(nu, sc)
212 1.8 briggs struct nubus_hw *nu;
213 1.8 briggs struct ae_softc *sc;
214 1.8 briggs {
215 1.21 briggs int i;
216 1.8 briggs
217 1.8 briggs /*
218 1.8 briggs * Try to determine what type of card this is...
219 1.8 briggs */
220 1.8 briggs sc->vendor = AE_VENDOR_UNKNOWN;
221 1.21 briggs for (i = 0; i < numvend; i++) {
222 1.21 briggs if (!strncmp(nu->Slot.manufacturer, vend[i].manu, vend[i].len)) {
223 1.8 briggs sc->vendor = vend[i].vendor;
224 1.8 briggs break;
225 1.8 briggs }
226 1.8 briggs }
227 1.21 briggs sc->type_str = (char *)(nu->Slot.manufacturer);
228 1.8 briggs
229 1.15 briggs }
230 1.15 briggs
231 1.15 briggs int
232 1.15 briggs ae_size_card_memory(sc)
233 1.15 briggs struct ae_softc *sc;
234 1.15 briggs {
235 1.15 briggs u_short *p;
236 1.15 briggs u_short i1, i2, i3, i4;
237 1.15 briggs int size;
238 1.21 briggs
239 1.21 briggs p = (u_short *)sc->mem_start;
240 1.15 briggs
241 1.15 briggs /*
242 1.15 briggs * very simple size memory, assuming it's installed in 8k
243 1.15 briggs * banks; also assume it will generally mirror in upper banks
244 1.15 briggs * if not installed.
245 1.15 briggs */
246 1.15 briggs i1 = (8192*0)/2;
247 1.15 briggs i2 = (8192*1)/2;
248 1.15 briggs i3 = (8192*2)/2;
249 1.15 briggs i4 = (8192*3)/2;
250 1.21 briggs
251 1.15 briggs p[i1] = 0x1111;
252 1.15 briggs p[i2] = 0x2222;
253 1.15 briggs p[i3] = 0x3333;
254 1.15 briggs p[i4] = 0x4444;
255 1.21 briggs
256 1.15 briggs if (p[i1] == 0x1111 && p[i2] == 0x2222 &&
257 1.15 briggs p[i3] == 0x3333 && p[i4] == 0x4444)
258 1.21 briggs return 8192*4;
259 1.21 briggs
260 1.21 briggs if ((p[i1] == 0x1111 && p[i2] == 0x2222) ||
261 1.21 briggs (p[i1] == 0x3333 && p[i2] == 0x4444))
262 1.21 briggs return 8192*2;
263 1.15 briggs
264 1.21 briggs if (p[i1] == 0x1111 || p[i1] == 0x4444)
265 1.21 briggs return 8192;
266 1.15 briggs
267 1.21 briggs return 0;
268 1.8 briggs }
269 1.8 briggs
270 1.1 briggs int
271 1.21 briggs ae_probe(parent, match, aux)
272 1.21 briggs struct device *parent;
273 1.21 briggs void *match, *aux;
274 1.1 briggs {
275 1.21 briggs struct ae_softc *sc = match;
276 1.21 briggs register struct nubus_hw *nu = aux;
277 1.1 briggs int i, memsize;
278 1.1 briggs int flags = 0;
279 1.1 briggs
280 1.3 briggs if (nu->Slot.type != NUBUS_NETWORK)
281 1.3 briggs return 0;
282 1.3 briggs
283 1.8 briggs ae_id_card(nu, sc);
284 1.1 briggs
285 1.15 briggs sc->regs_rev = 0;
286 1.21 briggs sc->mem_wr_short = 0;
287 1.15 briggs
288 1.1 briggs switch (sc->vendor) {
289 1.1 briggs case AE_VENDOR_INTERLAN:
290 1.1 briggs sc->nic_addr = nu->addr + GC_NIC_OFFSET;
291 1.1 briggs sc->rom_addr = nu->addr + GC_ROM_OFFSET;
292 1.21 briggs sc->mem_start = nu->addr + GC_DATA_OFFSET;
293 1.15 briggs if ((memsize = ae_size_card_memory(sc)) == 0)
294 1.15 briggs return 0;
295 1.1 briggs
296 1.1 briggs /* reset the NIC chip */
297 1.1 briggs *((caddr_t)nu->addr + GC_RESET_OFFSET) = (char)zero;
298 1.21 briggs
299 1.1 briggs /* Get station address from on-board ROM */
300 1.1 briggs for (i = 0; i < ETHER_ADDR_LEN; ++i)
301 1.21 briggs sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i*4);
302 1.1 briggs break;
303 1.1 briggs
304 1.15 briggs case AE_VENDOR_ASANTE:
305 1.15 briggs /* memory writes require *(u_short *) */
306 1.21 briggs sc->mem_wr_short = 1;
307 1.15 briggs /* otherwise, pretend to be an apple card (fall through) */
308 1.15 briggs
309 1.1 briggs case AE_VENDOR_APPLE:
310 1.15 briggs sc->regs_rev = 1;
311 1.1 briggs sc->nic_addr = nu->addr + AE_NIC_OFFSET;
312 1.1 briggs sc->rom_addr = nu->addr + AE_ROM_OFFSET;
313 1.21 briggs sc->mem_start = nu->addr + AE_DATA_OFFSET;
314 1.15 briggs if ((memsize = ae_size_card_memory(sc)) == 0)
315 1.21 briggs return (0);
316 1.1 briggs
317 1.1 briggs /* Get station address from on-board ROM */
318 1.1 briggs for (i = 0; i < ETHER_ADDR_LEN; ++i)
319 1.21 briggs sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i*2);
320 1.1 briggs break;
321 1.8 briggs
322 1.8 briggs case AE_VENDOR_DAYNA:
323 1.9 briggs printf("We think we are a Dayna card, but ");
324 1.10 briggs sc->nic_addr = nu->addr + DP_NIC_OFFSET;
325 1.10 briggs sc->rom_addr = nu->addr + DP_ROM_OFFSET;
326 1.21 briggs sc->mem_start = nu->addr + DP_DATA_OFFSET;
327 1.8 briggs memsize = 8192;
328 1.8 briggs
329 1.8 briggs /* Get station address from on-board ROM */
330 1.8 briggs for (i = 0; i < ETHER_ADDR_LEN; ++i)
331 1.21 briggs sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i*2);
332 1.9 briggs printf("it is dangerous to continue.\n");
333 1.21 briggs return (0); /* Since we don't work yet... */
334 1.8 briggs break;
335 1.8 briggs
336 1.8 briggs default:
337 1.21 briggs return (0);
338 1.8 briggs break;
339 1.1 briggs }
340 1.7 briggs
341 1.21 briggs sc->cr_proto = AE_CR_RD2;
342 1.21 briggs
343 1.21 briggs /* Allocate one xmit buffer if < 16k, two buffers otherwise. */
344 1.21 briggs if ((memsize < 16384) || (flags & AE_FLAGS_NO_DOUBLE_BUFFERING))
345 1.1 briggs sc->txb_cnt = 1;
346 1.21 briggs else
347 1.1 briggs sc->txb_cnt = 2;
348 1.1 briggs
349 1.1 briggs sc->tx_page_start = 0;
350 1.21 briggs sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * AE_TXBUF_SIZE;
351 1.21 briggs sc->rec_page_stop = sc->tx_page_start + (memsize >> AE_PAGE_SHIFT);
352 1.21 briggs sc->mem_ring = sc->mem_start + (sc->rec_page_start << AE_PAGE_SHIFT);
353 1.21 briggs sc->mem_size = memsize;
354 1.21 briggs sc->mem_end = sc->mem_start + memsize;
355 1.1 briggs
356 1.21 briggs /* Now zero memory and verify that it is clear. */
357 1.21 briggs bszero((u_short *)sc->mem_start, memsize / 2);
358 1.1 briggs
359 1.1 briggs for (i = 0; i < memsize; ++i)
360 1.21 briggs if (sc->mem_start[i]) {
361 1.21 briggs printf("%s: failed to clear shared memory at %x - check configuration\n",
362 1.21 briggs sc->sc_dev.dv_xname,
363 1.21 briggs sc->mem_start + i);
364 1.21 briggs return (0);
365 1.1 briggs }
366 1.1 briggs
367 1.21 briggs return (1);
368 1.21 briggs }
369 1.1 briggs
370 1.1 briggs /*
371 1.1 briggs * Install interface into kernel networking data structures
372 1.1 briggs */
373 1.9 briggs void
374 1.3 briggs ae_attach(parent, self, aux)
375 1.21 briggs struct device *parent, *self;
376 1.21 briggs void *aux;
377 1.1 briggs {
378 1.21 briggs struct ae_softc *sc = (void *)self;
379 1.3 briggs struct nubus_hw *nu = aux;
380 1.21 briggs struct cfdata *cf = sc->sc_dev.dv_cfdata;
381 1.21 briggs struct ifnet *ifp = &sc->sc_arpcom.ac_if;
382 1.3 briggs
383 1.21 briggs /* Set interface to stopped condition (reset). */
384 1.3 briggs ae_stop(sc);
385 1.1 briggs
386 1.21 briggs /* Initialize ifnet structure. */
387 1.21 briggs ifp->if_unit = sc->sc_dev.dv_unit;
388 1.3 briggs ifp->if_name = aecd.cd_name;
389 1.1 briggs ifp->if_start = ae_start;
390 1.1 briggs ifp->if_ioctl = ae_ioctl;
391 1.1 briggs ifp->if_watchdog = ae_watchdog;
392 1.21 briggs ifp->if_flags =
393 1.21 briggs IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
394 1.3 briggs
395 1.21 briggs /* Attach the interface. */
396 1.1 briggs if_attach(ifp);
397 1.21 briggs ether_ifattach(ifp);
398 1.1 briggs
399 1.21 briggs /* Print additional info when attached. */
400 1.21 briggs printf(": address %s, ", ether_sprintf(sc->sc_arpcom.ac_enaddr));
401 1.1 briggs
402 1.1 briggs if (sc->type_str && (*sc->type_str != 0))
403 1.15 briggs printf("type %s", sc->type_str);
404 1.1 briggs else
405 1.15 briggs printf("type unknown (0x%x)", sc->type);
406 1.15 briggs
407 1.21 briggs printf(", %dk mem.\n", sc->mem_size / 1024);
408 1.1 briggs
409 1.1 briggs #if NBPFILTER > 0
410 1.21 briggs bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
411 1.1 briggs #endif
412 1.21 briggs
413 1.21 briggs /* make sure interrupts are vectored to us */
414 1.21 briggs add_nubus_intr((int)sc->rom_addr & 0xFF000000, ae_intr, ifp->if_unit);
415 1.3 briggs }
416 1.1 briggs
417 1.1 briggs /*
418 1.1 briggs * Reset interface.
419 1.1 briggs */
420 1.21 briggs void
421 1.3 briggs ae_reset(sc)
422 1.3 briggs struct ae_softc *sc;
423 1.1 briggs {
424 1.1 briggs int s;
425 1.1 briggs
426 1.21 briggs s = splimp();
427 1.3 briggs ae_stop(sc);
428 1.3 briggs ae_init(sc);
429 1.21 briggs splx(s);
430 1.21 briggs }
431 1.1 briggs
432 1.1 briggs /*
433 1.1 briggs * Take interface offline.
434 1.1 briggs */
435 1.1 briggs void
436 1.3 briggs ae_stop(sc)
437 1.3 briggs struct ae_softc *sc;
438 1.1 briggs {
439 1.1 briggs int n = 5000;
440 1.1 briggs
441 1.21 briggs /* Stop everything on the interface, and select page 0 registers. */
442 1.21 briggs NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STP);
443 1.1 briggs
444 1.1 briggs /*
445 1.21 briggs * Wait for interface to enter stopped state, but limit # of checks to
446 1.21 briggs * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but
447 1.21 briggs * just in case it's an old one.
448 1.1 briggs */
449 1.1 briggs while (((NIC_GET(sc, AE_P0_ISR) & AE_ISR_RST) == 0) && --n);
450 1.1 briggs }
451 1.1 briggs
452 1.1 briggs /*
453 1.21 briggs * Device timeout/watchdog routine. Entered if the device neglects to generate
454 1.21 briggs * an interrupt after a transmit has been started on it.
455 1.1 briggs */
456 1.21 briggs static int aeintr_ctr = 0;
457 1.20 briggs void
458 1.1 briggs ae_watchdog(unit)
459 1.20 briggs int unit;
460 1.1 briggs {
461 1.21 briggs struct ae_softc *sc = aecd.cd_devs[unit];
462 1.15 briggs
463 1.18 briggs #if 1
464 1.18 briggs /*
465 1.18 briggs * This is a kludge! The via code seems to miss slot interrupts
466 1.18 briggs * sometimes. This kludges around that by calling the handler
467 1.18 briggs * by hand if the watchdog is activated. -- XXX (akb)
468 1.18 briggs */
469 1.18 briggs int i;
470 1.18 briggs
471 1.18 briggs i = aeintr_ctr;
472 1.18 briggs
473 1.18 briggs (*via2itab[1])(1);
474 1.18 briggs
475 1.19 briggs if (i != aeintr_ctr) {
476 1.19 briggs log(LOG_ERR, "ae%d: device timeout, recovered\n", unit);
477 1.18 briggs return;
478 1.19 briggs }
479 1.18 briggs #endif
480 1.18 briggs
481 1.21 briggs log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
482 1.21 briggs ++sc->sc_arpcom.ac_if.if_oerrors;
483 1.21 briggs
484 1.15 briggs ae_reset(sc);
485 1.1 briggs }
486 1.1 briggs
487 1.1 briggs /*
488 1.21 briggs * Initialize device.
489 1.1 briggs */
490 1.21 briggs void
491 1.3 briggs ae_init(sc)
492 1.3 briggs struct ae_softc *sc;
493 1.1 briggs {
494 1.21 briggs struct ifnet *ifp = &sc->sc_arpcom.ac_if;
495 1.1 briggs int i, s;
496 1.21 briggs u_char command;
497 1.21 briggs u_long mcaf[2];
498 1.1 briggs
499 1.21 briggs /* Address not known. */
500 1.21 briggs if (ifp->if_addrlist == 0)
501 1.21 briggs return;
502 1.1 briggs
503 1.1 briggs /*
504 1.1 briggs * Initialize the NIC in the exact order outlined in the NS manual.
505 1.21 briggs * This init procedure is "mandatory"...don't change what or when
506 1.21 briggs * things happen.
507 1.1 briggs */
508 1.21 briggs s = splimp();
509 1.1 briggs
510 1.21 briggs /* Reset transmitter flags. */
511 1.1 briggs sc->xmit_busy = 0;
512 1.21 briggs sc->sc_arpcom.ac_if.if_timer = 0;
513 1.1 briggs
514 1.21 briggs sc->txb_inuse = 0;
515 1.21 briggs sc->txb_new = 0;
516 1.21 briggs sc->txb_next_tx = 0;
517 1.1 briggs
518 1.21 briggs /* Set interface for page 0, remote DMA complete, stopped. */
519 1.21 briggs NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STP);
520 1.1 briggs
521 1.1 briggs /*
522 1.21 briggs * Set FIFO threshold to 8, No auto-init Remote DMA, byte
523 1.21 briggs * order=80x86, word-wide DMA xfers,
524 1.1 briggs */
525 1.21 briggs NIC_PUT(sc, AE_P0_DCR,
526 1.21 briggs AE_DCR_FT1 | AE_DCR_WTS | AE_DCR_LS);
527 1.1 briggs
528 1.21 briggs /* Clear remote byte count registers. */
529 1.21 briggs NIC_PUT(sc, AE_P0_RBCR0, 0);
530 1.21 briggs NIC_PUT(sc, AE_P0_RBCR1, 0);
531 1.1 briggs
532 1.21 briggs /* Tell RCR to do nothing for now. */
533 1.21 briggs NIC_PUT(sc, AE_P0_RCR, AE_RCR_MON);
534 1.1 briggs
535 1.21 briggs /* Place NIC in internal loopback mode. */
536 1.1 briggs NIC_PUT(sc, AE_P0_TCR, AE_TCR_LB0);
537 1.1 briggs
538 1.21 briggs /* Initialize receive buffer ring. */
539 1.21 briggs NIC_PUT(sc, AE_P0_BNRY, sc->rec_page_start);
540 1.1 briggs NIC_PUT(sc, AE_P0_PSTART, sc->rec_page_start);
541 1.1 briggs NIC_PUT(sc, AE_P0_PSTOP, sc->rec_page_stop);
542 1.1 briggs
543 1.1 briggs /*
544 1.21 briggs * Clear all interrupts. A '1' in each bit position clears the
545 1.21 briggs * corresponding flag.
546 1.1 briggs */
547 1.21 briggs NIC_PUT(sc, AE_P0_ISR, 0xff);
548 1.15 briggs
549 1.1 briggs /*
550 1.1 briggs * Enable the following interrupts: receive/transmit complete,
551 1.21 briggs * receive/transmit error, and Receiver OverWrite.
552 1.1 briggs *
553 1.1 briggs * Counter overflow and Remote DMA complete are *not* enabled.
554 1.1 briggs */
555 1.1 briggs NIC_PUT(sc, AE_P0_IMR,
556 1.21 briggs AE_IMR_PRXE | AE_IMR_PTXE | AE_IMR_RXEE | AE_IMR_TXEE |
557 1.21 briggs AE_IMR_OVWE);
558 1.1 briggs
559 1.21 briggs /* Program command register for page 1. */
560 1.21 briggs NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_1 | AE_CR_STP);
561 1.1 briggs
562 1.21 briggs /* Copy out our station address. */
563 1.1 briggs for (i = 0; i < ETHER_ADDR_LEN; ++i)
564 1.21 briggs NIC_PUT(sc, AE_P1_PAR0 + i, sc->sc_arpcom.ac_enaddr[i]);
565 1.1 briggs
566 1.21 briggs /* Set multicast filter on chip. */
567 1.21 briggs ae_getmcaf(&sc->sc_arpcom, mcaf);
568 1.21 briggs for (i = 0; i < 8; i++)
569 1.21 briggs NIC_PUT(sc, AE_P1_MAR0 + i, ((u_char *)mcaf)[i]);
570 1.1 briggs
571 1.1 briggs /*
572 1.21 briggs * Set current page pointer to one page after the boundary pointer, as
573 1.21 briggs * recommended in the National manual.
574 1.1 briggs */
575 1.21 briggs sc->next_packet = sc->rec_page_start + 1;
576 1.1 briggs NIC_PUT(sc, AE_P1_CURR, sc->next_packet);
577 1.1 briggs
578 1.21 briggs /* Program command register for page 0. */
579 1.21 briggs NIC_PUT(sc, AE_P1_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STP);
580 1.21 briggs
581 1.21 briggs i = AE_RCR_AB | AE_RCR_AM;
582 1.21 briggs if (ifp->if_flags & IFF_PROMISC) {
583 1.21 briggs /*
584 1.21 briggs * Set promiscuous mode. Multicast filter was set earlier so
585 1.21 briggs * that we should receive all multicast packets.
586 1.21 briggs */
587 1.21 briggs i |= AE_RCR_PRO | AE_RCR_AR | AE_RCR_SEP;
588 1.21 briggs }
589 1.21 briggs NIC_PUT(sc, AE_P0_RCR, i);
590 1.21 briggs
591 1.21 briggs /* Take interface out of loopback. */
592 1.21 briggs NIC_PUT(sc, AE_P0_TCR, 0);
593 1.1 briggs
594 1.21 briggs /* Fire up the interface. */
595 1.21 briggs NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STA);
596 1.1 briggs
597 1.21 briggs /* Set 'running' flag, and clear output active flag. */
598 1.1 briggs ifp->if_flags |= IFF_RUNNING;
599 1.1 briggs ifp->if_flags &= ~IFF_OACTIVE;
600 1.1 briggs
601 1.21 briggs /* ...and attempt to start output. */
602 1.1 briggs ae_start(ifp);
603 1.1 briggs
604 1.21 briggs splx(s);
605 1.1 briggs }
606 1.21 briggs
607 1.1 briggs /*
608 1.21 briggs * This routine actually starts the transmission on the interface.
609 1.1 briggs */
610 1.21 briggs static inline void
611 1.21 briggs ae_xmit(sc)
612 1.21 briggs struct ae_softc *sc;
613 1.1 briggs {
614 1.21 briggs struct ifnet *ifp = &sc->sc_arpcom.ac_if;
615 1.21 briggs u_short len;
616 1.21 briggs
617 1.21 briggs len = sc->txb_len[sc->txb_next_tx];
618 1.1 briggs
619 1.21 briggs /* Set NIC for page 0 register access. */
620 1.21 briggs NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STA);
621 1.1 briggs
622 1.21 briggs /* Set TX buffer start page. */
623 1.1 briggs NIC_PUT(sc, AE_P0_TPSR, sc->tx_page_start +
624 1.21 briggs sc->txb_next_tx * AE_TXBUF_SIZE);
625 1.1 briggs
626 1.21 briggs /* Set TX length. */
627 1.21 briggs NIC_PUT(sc, AE_P0_TBCR0, len);
628 1.1 briggs NIC_PUT(sc, AE_P0_TBCR1, len >> 8);
629 1.1 briggs
630 1.21 briggs /* Set page 0, remote DMA complete, transmit packet, and *start*. */
631 1.21 briggs NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_TXP | AE_CR_STA);
632 1.21 briggs sc->xmit_busy = 1;
633 1.1 briggs
634 1.21 briggs /* Point to next transmit buffer slot and wrap if necessary. */
635 1.21 briggs sc->txb_next_tx++;
636 1.21 briggs if (sc->txb_next_tx == sc->txb_cnt)
637 1.21 briggs sc->txb_next_tx = 0;
638 1.1 briggs
639 1.21 briggs /* Set a timer just in case we never hear from the board again. */
640 1.18 briggs ifp->if_timer = 2;
641 1.1 briggs }
642 1.1 briggs
643 1.1 briggs /*
644 1.1 briggs * Start output on interface.
645 1.1 briggs * We make two assumptions here:
646 1.21 briggs * 1) that the current priority is set to splimp _before_ this code
647 1.1 briggs * is called *and* is returned to the appropriate priority after
648 1.1 briggs * return
649 1.1 briggs * 2) that the IFF_OACTIVE flag is checked before this code is called
650 1.1 briggs * (i.e. that the output part of the interface is idle)
651 1.1 briggs */
652 1.20 briggs void
653 1.1 briggs ae_start(ifp)
654 1.1 briggs struct ifnet *ifp;
655 1.1 briggs {
656 1.21 briggs struct ae_softc *sc = aecd.cd_devs[ifp->if_unit];
657 1.1 briggs struct mbuf *m0, *m;
658 1.1 briggs caddr_t buffer;
659 1.1 briggs int len;
660 1.1 briggs
661 1.1 briggs outloop:
662 1.1 briggs /*
663 1.21 briggs * First, see if there are buffered packets and an idle transmitter -
664 1.21 briggs * should never happen at this point.
665 1.1 briggs */
666 1.21 briggs if (sc->txb_inuse && (sc->xmit_busy == 0)) {
667 1.21 briggs printf("%s: packets buffered, but transmitter idle\n",
668 1.21 briggs sc->sc_dev.dv_xname);
669 1.21 briggs ae_xmit(sc);
670 1.21 briggs }
671 1.21 briggs
672 1.21 briggs /* See if there is room to put another packet in the buffer. */
673 1.21 briggs if (sc->txb_inuse == sc->txb_cnt) {
674 1.21 briggs /* No room. Indicate this to the outside world and exit. */
675 1.21 briggs ifp->if_flags |= IFF_OACTIVE;
676 1.21 briggs return;
677 1.21 briggs }
678 1.1 briggs
679 1.21 briggs IF_DEQUEUE(&sc->sc_arpcom.ac_if.if_snd, m);
680 1.1 briggs if (m == 0) {
681 1.21 briggs /*
682 1.21 briggs * We are using the !OACTIVE flag to indicate to the outside
683 1.21 briggs * world that we can accept an additional packet rather than
684 1.21 briggs * that the transmitter is _actually_ active. Indeed, the
685 1.21 briggs * transmitter may be active, but if we haven't filled all the
686 1.21 briggs * buffers with data then we still want to accept more.
687 1.21 briggs */
688 1.1 briggs ifp->if_flags &= ~IFF_OACTIVE;
689 1.1 briggs return;
690 1.1 briggs }
691 1.1 briggs
692 1.21 briggs /* Copy the mbuf chain into the transmit buffer. */
693 1.21 briggs m0 = m;
694 1.21 briggs
695 1.21 briggs /* txb_new points to next open buffer slot. */
696 1.21 briggs buffer = sc->mem_start + ((sc->txb_new * AE_TXBUF_SIZE) << AE_PAGE_SHIFT);
697 1.21 briggs
698 1.21 briggs len = ae_put(sc, m, buffer);
699 1.1 briggs
700 1.21 briggs sc->txb_len[sc->txb_new] = max(len, ETHER_MIN_LEN);
701 1.21 briggs sc->txb_inuse++;
702 1.1 briggs
703 1.21 briggs /* Point to next buffer slot and wrap if necessary. */
704 1.21 briggs if (++sc->txb_new == sc->txb_cnt)
705 1.21 briggs sc->txb_new = 0;
706 1.1 briggs
707 1.1 briggs if (sc->xmit_busy == 0)
708 1.21 briggs ae_xmit(sc);
709 1.21 briggs
710 1.1 briggs #if NBPFILTER > 0
711 1.21 briggs /* Tap off here if there is a BPF listener. */
712 1.21 briggs if (sc->sc_arpcom.ac_if.if_bpf)
713 1.21 briggs bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m0);
714 1.1 briggs #endif
715 1.1 briggs
716 1.1 briggs m_freem(m0);
717 1.1 briggs
718 1.21 briggs /* Loop back to the top to possibly buffer more packets. */
719 1.21 briggs goto outloop;
720 1.1 briggs }
721 1.21 briggs
722 1.1 briggs /*
723 1.1 briggs * Ethernet interface receiver interrupt.
724 1.1 briggs */
725 1.1 briggs static inline void
726 1.21 briggs ae_rint(sc)
727 1.21 briggs struct ae_softc *sc;
728 1.1 briggs {
729 1.21 briggs u_char boundary, current;
730 1.21 briggs u_short len, len_save;
731 1.21 briggs u_char nlen;
732 1.21 briggs struct ae_ring packet_hdr;
733 1.21 briggs caddr_t packet_ptr;
734 1.21 briggs
735 1.21 briggs loop:
736 1.21 briggs /* Set NIC to page 1 registers to get 'current' pointer. */
737 1.21 briggs NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_1 | AE_CR_STA);
738 1.1 briggs
739 1.1 briggs /*
740 1.1 briggs * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
741 1.21 briggs * it points to where new data has been buffered. The 'CURR' (current)
742 1.21 briggs * register points to the logical end of the ring-buffer - i.e. it
743 1.21 briggs * points to where additional new data will be added. We loop here
744 1.21 briggs * until the logical beginning equals the logical end (or in other
745 1.21 briggs * words, until the ring-buffer is empty).
746 1.1 briggs */
747 1.21 briggs current = NIC_GET(sc, AE_P1_CURR);
748 1.21 briggs if (sc->next_packet == current)
749 1.21 briggs return;
750 1.1 briggs
751 1.21 briggs /* Set NIC to page 0 registers to update boundary register. */
752 1.21 briggs NIC_PUT(sc, AE_P1_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STA);
753 1.1 briggs
754 1.21 briggs do {
755 1.21 briggs /* Get pointer to this buffer's header structure. */
756 1.21 briggs packet_ptr = sc->mem_ring +
757 1.21 briggs ((sc->next_packet - sc->rec_page_start) << AE_PAGE_SHIFT);
758 1.1 briggs
759 1.1 briggs /*
760 1.21 briggs * The byte count includes a 4 byte header that was added by
761 1.21 briggs * the NIC.
762 1.1 briggs */
763 1.21 briggs packet_hdr = *(struct ae_ring *)packet_ptr;
764 1.21 briggs len = packet_hdr.count;
765 1.21 briggs len_save = len = ((len >> 8) & 0xff) | ((len & 0xff) << 8);
766 1.1 briggs
767 1.1 briggs /*
768 1.21 briggs * Try do deal with old, buggy chips that sometimes duplicate
769 1.21 briggs * the low byte of the length into the high byte. We do this
770 1.21 briggs * by simply ignoring the high byte of the length and always
771 1.21 briggs * recalculating it.
772 1.21 briggs *
773 1.21 briggs * NOTE: sc->next_packet is pointing at the current packet.
774 1.1 briggs */
775 1.21 briggs if (packet_hdr.next_packet >= sc->next_packet)
776 1.21 briggs nlen = (packet_hdr.next_packet - sc->next_packet);
777 1.21 briggs else
778 1.21 briggs nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
779 1.21 briggs (sc->rec_page_stop - sc->next_packet));
780 1.21 briggs --nlen;
781 1.21 briggs if ((len & AE_PAGE_MASK) + sizeof(packet_hdr) > AE_PAGE_SIZE)
782 1.21 briggs --nlen;
783 1.21 briggs len = (len & AE_PAGE_MASK) | (nlen << AE_PAGE_SHIFT);
784 1.21 briggs #ifdef DIAGNOSTIC
785 1.21 briggs if (len != len_save) {
786 1.21 briggs printf("%s: length does not match next packet pointer\n",
787 1.21 briggs sc->sc_dev.dv_xname);
788 1.21 briggs printf("%s: len %04x nlen %04x start %02x first %02x curr %02x next %02x stop %02x\n",
789 1.21 briggs sc->sc_dev.dv_xname, packet_hdr.count, len,
790 1.21 briggs sc->rec_page_start, sc->next_packet, current,
791 1.21 briggs packet_hdr.next_packet, sc->rec_page_stop);
792 1.21 briggs }
793 1.21 briggs #endif
794 1.1 briggs
795 1.1 briggs /*
796 1.21 briggs * Be fairly liberal about what we allow as a "reasonable"
797 1.21 briggs * length so that a [crufty] packet will make it to BPF (and
798 1.21 briggs * can thus be analyzed). Note that all that is really
799 1.21 briggs * important is that we have a length that will fit into one
800 1.21 briggs * mbuf cluster or less; the upper layer protocols can then
801 1.21 briggs * figure out the length from their own length field(s).
802 1.1 briggs */
803 1.21 briggs if (len <= MCLBYTES &&
804 1.21 briggs packet_hdr.next_packet >= sc->rec_page_start &&
805 1.21 briggs packet_hdr.next_packet < sc->rec_page_stop) {
806 1.21 briggs /* Go get packet. */
807 1.21 briggs ae_get_packet(sc, packet_ptr + sizeof(struct ae_ring),
808 1.21 briggs len - sizeof(struct ae_ring));
809 1.21 briggs ++sc->sc_arpcom.ac_if.if_ipackets;
810 1.21 briggs } else {
811 1.21 briggs /* Really BAD. The ring pointers are corrupted. */
812 1.21 briggs log(LOG_ERR,
813 1.21 briggs "%s: NIC memory corrupt - invalid packet length %d\n",
814 1.21 briggs sc->sc_dev.dv_xname, len);
815 1.21 briggs ++sc->sc_arpcom.ac_if.if_ierrors;
816 1.21 briggs ae_reset(sc);
817 1.21 briggs return;
818 1.21 briggs }
819 1.1 briggs
820 1.21 briggs /* Update next packet pointer. */
821 1.21 briggs sc->next_packet = packet_hdr.next_packet;
822 1.1 briggs
823 1.1 briggs /*
824 1.21 briggs * Update NIC boundary pointer - being careful to keep it one
825 1.21 briggs * buffer behind (as recommended by NS databook).
826 1.1 briggs */
827 1.21 briggs boundary = sc->next_packet - 1;
828 1.21 briggs if (boundary < sc->rec_page_start)
829 1.21 briggs boundary = sc->rec_page_stop - 1;
830 1.21 briggs NIC_PUT(sc, AE_P0_BNRY, boundary);
831 1.21 briggs } while (sc->next_packet != current);
832 1.21 briggs
833 1.21 briggs goto loop;
834 1.1 briggs }
835 1.1 briggs
836 1.21 briggs /* Ethernet interface interrupt processor. */
837 1.18 briggs int
838 1.21 briggs ae_intr(unit)
839 1.1 briggs int unit;
840 1.1 briggs {
841 1.21 briggs struct ae_softc *sc = aecd.cd_devs[unit];
842 1.1 briggs u_char isr;
843 1.1 briggs
844 1.18 briggs aeintr_ctr++;
845 1.1 briggs
846 1.21 briggs /* Set NIC to page 0 registers. */
847 1.21 briggs NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STA);
848 1.21 briggs
849 1.21 briggs isr = NIC_GET(sc, AE_P0_ISR);
850 1.21 briggs if (!isr)
851 1.21 briggs return (0);
852 1.1 briggs
853 1.21 briggs /* Loop until there are no more new interrupts. */
854 1.21 briggs for (;;) {
855 1.1 briggs /*
856 1.21 briggs * Reset all the bits that we are 'acknowledging' by writing a
857 1.21 briggs * '1' to each bit position that was set.
858 1.21 briggs * (Writing a '1' *clears* the bit.)
859 1.1 briggs */
860 1.1 briggs NIC_PUT(sc, AE_P0_ISR, isr);
861 1.1 briggs
862 1.1 briggs /*
863 1.21 briggs * Handle transmitter interrupts. Handle these first because
864 1.21 briggs * the receiver will reset the board under some conditions.
865 1.1 briggs */
866 1.21 briggs if (isr & (AE_ISR_PTX | AE_ISR_TXE)) {
867 1.21 briggs u_char collisions = NIC_GET(sc, AE_P0_NCR) & 0x0f;
868 1.1 briggs
869 1.1 briggs /*
870 1.21 briggs * Check for transmit error. If a TX completed with an
871 1.21 briggs * error, we end up throwing the packet away. Really
872 1.1 briggs * the only error that is possible is excessive
873 1.1 briggs * collisions, and in this case it is best to allow the
874 1.21 briggs * automatic mechanisms of TCP to backoff the flow. Of
875 1.1 briggs * course, with UDP we're screwed, but this is expected
876 1.1 briggs * when a network is heavily loaded.
877 1.1 briggs */
878 1.21 briggs (void) NIC_GET(sc, AE_P0_TSR);
879 1.1 briggs if (isr & AE_ISR_TXE) {
880 1.1 briggs /*
881 1.21 briggs * Excessive collisions (16).
882 1.1 briggs */
883 1.1 briggs if ((NIC_GET(sc, AE_P0_TSR) & AE_TSR_ABT)
884 1.21 briggs && (collisions == 0)) {
885 1.1 briggs /*
886 1.21 briggs * When collisions total 16, the P0_NCR
887 1.21 briggs * will indicate 0, and the TSR_ABT is
888 1.21 briggs * set.
889 1.1 briggs */
890 1.1 briggs collisions = 16;
891 1.1 briggs }
892 1.1 briggs
893 1.21 briggs /* Update output errors counter. */
894 1.21 briggs ++sc->sc_arpcom.ac_if.if_oerrors;
895 1.1 briggs } else {
896 1.1 briggs /*
897 1.1 briggs * Update total number of successfully
898 1.21 briggs * transmitted packets.
899 1.1 briggs */
900 1.21 briggs ++sc->sc_arpcom.ac_if.if_opackets;
901 1.1 briggs }
902 1.1 briggs
903 1.21 briggs /* Reset TX busy and output active flags. */
904 1.1 briggs sc->xmit_busy = 0;
905 1.21 briggs sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
906 1.1 briggs
907 1.21 briggs /* Clear watchdog timer. */
908 1.21 briggs sc->sc_arpcom.ac_if.if_timer = 0;
909 1.1 briggs
910 1.1 briggs /*
911 1.1 briggs * Add in total number of collisions on last
912 1.21 briggs * transmission.
913 1.1 briggs */
914 1.21 briggs sc->sc_arpcom.ac_if.if_collisions += collisions;
915 1.1 briggs
916 1.1 briggs /*
917 1.21 briggs * Decrement buffer in-use count if not zero (can only
918 1.21 briggs * be zero if a transmitter interrupt occured while not
919 1.21 briggs * actually transmitting).
920 1.1 briggs * If data is ready to transmit, start it transmitting,
921 1.21 briggs * otherwise defer until after handling receiver.
922 1.1 briggs */
923 1.21 briggs if (sc->txb_inuse && --sc->txb_inuse)
924 1.21 briggs ae_xmit(sc);
925 1.1 briggs }
926 1.1 briggs
927 1.21 briggs /* Handle receiver interrupts. */
928 1.21 briggs if (isr & (AE_ISR_PRX | AE_ISR_RXE | AE_ISR_OVW)) {
929 1.21 briggs /*
930 1.21 briggs * Overwrite warning. In order to make sure that a
931 1.21 briggs * lockup of the local DMA hasn't occurred, we reset
932 1.21 briggs * and re-init the NIC. The NSC manual suggests only a
933 1.21 briggs * partial reset/re-init is necessary - but some chips
934 1.21 briggs * seem to want more. The DMA lockup has been seen
935 1.21 briggs * only with early rev chips - Methinks this bug was
936 1.21 briggs * fixed in later revs. -DG
937 1.21 briggs */
938 1.1 briggs if (isr & AE_ISR_OVW) {
939 1.21 briggs ++sc->sc_arpcom.ac_if.if_ierrors;
940 1.21 briggs #ifdef DIAGNOSTIC
941 1.1 briggs log(LOG_WARNING,
942 1.21 briggs "%s: warning - receiver ring buffer overrun\n",
943 1.21 briggs sc->sc_dev.dv_xname);
944 1.21 briggs #endif
945 1.21 briggs /* Stop/reset/re-init NIC. */
946 1.21 briggs ae_reset(sc);
947 1.21 briggs } else {
948 1.1 briggs /*
949 1.21 briggs * Receiver Error. One or more of: CRC error,
950 1.21 briggs * frame alignment error FIFO overrun, or
951 1.21 briggs * missed packet.
952 1.1 briggs */
953 1.1 briggs if (isr & AE_ISR_RXE) {
954 1.21 briggs ++sc->sc_arpcom.ac_if.if_ierrors;
955 1.1 briggs #ifdef AE_DEBUG
956 1.21 briggs printf("%s: receive error %x\n",
957 1.21 briggs sc->sc_dev.dv_xname,
958 1.21 briggs NIC_GET(sc, AE_P0_RSR));
959 1.1 briggs #endif
960 1.1 briggs }
961 1.1 briggs
962 1.1 briggs /*
963 1.1 briggs * Go get the packet(s)
964 1.1 briggs * XXX - Doing this on an error is dubious
965 1.21 briggs * because there shouldn't be any data to get
966 1.21 briggs * (we've configured the interface to not
967 1.21 briggs * accept packets with errors).
968 1.1 briggs */
969 1.21 briggs ae_rint(sc);
970 1.1 briggs }
971 1.1 briggs }
972 1.1 briggs
973 1.1 briggs /*
974 1.21 briggs * If it looks like the transmitter can take more data, attempt
975 1.21 briggs * to start output on the interface. This is done after
976 1.21 briggs * handling the receiver to give the receiver priority.
977 1.1 briggs */
978 1.21 briggs if ((sc->sc_arpcom.ac_if.if_flags & IFF_OACTIVE) == 0)
979 1.21 briggs ae_start(&sc->sc_arpcom.ac_if);
980 1.1 briggs
981 1.1 briggs /*
982 1.21 briggs * Return NIC CR to standard state: page 0, remote DMA
983 1.21 briggs * complete, start (toggling the TXP bit off, even if was just
984 1.21 briggs * set in the transmit routine, is *okay* - it is 'edge'
985 1.21 briggs * triggered from low to high).
986 1.1 briggs */
987 1.21 briggs NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STA);
988 1.1 briggs
989 1.1 briggs /*
990 1.21 briggs * If the Network Talley Counters overflow, read them to reset
991 1.21 briggs * them. It appears that old 8390's won't clear the ISR flag
992 1.21 briggs * otherwise - resulting in an infinite loop.
993 1.1 briggs */
994 1.1 briggs if (isr & AE_ISR_CNT) {
995 1.1 briggs (void) NIC_GET(sc, AE_P0_CNTR0);
996 1.1 briggs (void) NIC_GET(sc, AE_P0_CNTR1);
997 1.1 briggs (void) NIC_GET(sc, AE_P0_CNTR2);
998 1.1 briggs }
999 1.21 briggs
1000 1.21 briggs isr = NIC_GET(sc, AE_P0_ISR);
1001 1.21 briggs if (!isr)
1002 1.21 briggs return (1);
1003 1.1 briggs }
1004 1.1 briggs }
1005 1.21 briggs
1006 1.1 briggs /*
1007 1.21 briggs * Process an ioctl request. This code needs some work - it looks pretty ugly.
1008 1.1 briggs */
1009 1.1 briggs int
1010 1.1 briggs ae_ioctl(ifp, command, data)
1011 1.1 briggs register struct ifnet *ifp;
1012 1.21 briggs u_long command;
1013 1.1 briggs caddr_t data;
1014 1.1 briggs {
1015 1.21 briggs struct ae_softc *sc = aecd.cd_devs[ifp->if_unit];
1016 1.1 briggs register struct ifaddr *ifa = (struct ifaddr *)data;
1017 1.1 briggs struct ifreq *ifr = (struct ifreq *)data;
1018 1.1 briggs int s, error = 0;
1019 1.1 briggs
1020 1.21 briggs s = splimp();
1021 1.1 briggs
1022 1.1 briggs switch (command) {
1023 1.1 briggs
1024 1.1 briggs case SIOCSIFADDR:
1025 1.1 briggs ifp->if_flags |= IFF_UP;
1026 1.1 briggs
1027 1.1 briggs switch (ifa->ifa_addr->sa_family) {
1028 1.1 briggs #ifdef INET
1029 1.1 briggs case AF_INET:
1030 1.21 briggs ae_init(sc);
1031 1.21 briggs arp_ifinit(&sc->sc_arpcom, ifa);
1032 1.1 briggs break;
1033 1.1 briggs #endif
1034 1.1 briggs #ifdef NS
1035 1.21 briggs /* XXX - This code is probably wrong. */
1036 1.1 briggs case AF_NS:
1037 1.1 briggs {
1038 1.21 briggs register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1039 1.1 briggs
1040 1.1 briggs if (ns_nullhost(*ina))
1041 1.1 briggs ina->x_host =
1042 1.21 briggs *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
1043 1.21 briggs else
1044 1.21 briggs bcopy(ina->x_host.c_host,
1045 1.21 briggs sc->sc_arpcom.ac_enaddr,
1046 1.21 briggs sizeof(sc->sc_arpcom.ac_enaddr));
1047 1.21 briggs /* Set new address. */
1048 1.11 briggs ae_init(sc);
1049 1.1 briggs break;
1050 1.1 briggs }
1051 1.1 briggs #endif
1052 1.1 briggs default:
1053 1.11 briggs ae_init(sc);
1054 1.1 briggs break;
1055 1.1 briggs }
1056 1.1 briggs break;
1057 1.1 briggs
1058 1.1 briggs case SIOCSIFFLAGS:
1059 1.21 briggs if ((ifp->if_flags & IFF_UP) == 0 &&
1060 1.21 briggs (ifp->if_flags & IFF_RUNNING) != 0) {
1061 1.21 briggs /*
1062 1.21 briggs * If interface is marked down and it is running, then
1063 1.21 briggs * stop it.
1064 1.21 briggs */
1065 1.15 briggs ae_stop(sc);
1066 1.1 briggs ifp->if_flags &= ~IFF_RUNNING;
1067 1.21 briggs } else if ((ifp->if_flags & IFF_UP) != 0 &&
1068 1.21 briggs (ifp->if_flags & IFF_RUNNING) == 0) {
1069 1.21 briggs /*
1070 1.21 briggs * If interface is marked up and it is stopped, then
1071 1.21 briggs * start it.
1072 1.21 briggs */
1073 1.21 briggs ae_init(sc);
1074 1.1 briggs } else {
1075 1.1 briggs /*
1076 1.21 briggs * Reset the interface to pick up changes in any other
1077 1.21 briggs * flags that affect hardware registers.
1078 1.1 briggs */
1079 1.21 briggs ae_stop(sc);
1080 1.21 briggs ae_init(sc);
1081 1.21 briggs }
1082 1.21 briggs break;
1083 1.21 briggs
1084 1.21 briggs case SIOCADDMULTI:
1085 1.21 briggs case SIOCDELMULTI:
1086 1.21 briggs /* Update our multicast list. */
1087 1.21 briggs error = (command == SIOCADDMULTI) ?
1088 1.21 briggs ether_addmulti(ifr, &sc->sc_arpcom) :
1089 1.21 briggs ether_delmulti(ifr, &sc->sc_arpcom);
1090 1.21 briggs
1091 1.21 briggs if (error == ENETRESET) {
1092 1.1 briggs /*
1093 1.21 briggs * Multicast list has changed; set the hardware filter
1094 1.21 briggs * accordingly.
1095 1.1 briggs */
1096 1.21 briggs ae_stop(sc); /* XXX for ds_setmcaf? */
1097 1.21 briggs ae_init(sc);
1098 1.21 briggs error = 0;
1099 1.1 briggs }
1100 1.1 briggs break;
1101 1.1 briggs
1102 1.1 briggs default:
1103 1.1 briggs error = EINVAL;
1104 1.1 briggs }
1105 1.21 briggs
1106 1.21 briggs splx(s);
1107 1.1 briggs return (error);
1108 1.1 briggs }
1109 1.1 briggs
1110 1.1 briggs /*
1111 1.1 briggs * Retreive packet from shared memory and send to the next level up via
1112 1.21 briggs * ether_input(). If there is a BPF listener, give a copy to BPF, too.
1113 1.1 briggs */
1114 1.21 briggs void
1115 1.1 briggs ae_get_packet(sc, buf, len)
1116 1.1 briggs struct ae_softc *sc;
1117 1.21 briggs caddr_t buf;
1118 1.1 briggs u_short len;
1119 1.1 briggs {
1120 1.1 briggs struct ether_header *eh;
1121 1.21 briggs struct mbuf *m, *ae_ring_to_mbuf();
1122 1.1 briggs
1123 1.21 briggs /* Allocate a header mbuf. */
1124 1.1 briggs MGETHDR(m, M_DONTWAIT, MT_DATA);
1125 1.1 briggs if (m == 0)
1126 1.21 briggs return;
1127 1.21 briggs m->m_pkthdr.rcvif = &sc->sc_arpcom.ac_if;
1128 1.1 briggs m->m_pkthdr.len = len;
1129 1.1 briggs m->m_len = 0;
1130 1.1 briggs
1131 1.21 briggs /* The following silliness is to make NFS happy. */
1132 1.1 briggs #define EROUND ((sizeof(struct ether_header) + 3) & ~3)
1133 1.1 briggs #define EOFF (EROUND - sizeof(struct ether_header))
1134 1.1 briggs
1135 1.1 briggs /*
1136 1.21 briggs * The following assumes there is room for the ether header in the
1137 1.21 briggs * header mbuf.
1138 1.1 briggs */
1139 1.21 briggs m->m_data += EOFF;
1140 1.21 briggs eh = mtod(m, struct ether_header *);
1141 1.21 briggs
1142 1.21 briggs word_copy(buf, mtod(m, caddr_t), sizeof(struct ether_header));
1143 1.1 briggs buf += sizeof(struct ether_header);
1144 1.21 briggs m->m_len += sizeof(struct ether_header);
1145 1.1 briggs len -= sizeof(struct ether_header);
1146 1.1 briggs
1147 1.21 briggs /* Pull packet off interface. */
1148 1.21 briggs if (ae_ring_to_mbuf(sc, buf, m, len) == 0) {
1149 1.21 briggs m_freem(m);
1150 1.21 briggs return;
1151 1.1 briggs }
1152 1.1 briggs
1153 1.1 briggs #if NBPFILTER > 0
1154 1.1 briggs /*
1155 1.21 briggs * Check if there's a BPF listener on this interface. If so, hand off
1156 1.21 briggs * the raw packet to bpf.
1157 1.1 briggs */
1158 1.21 briggs if (sc->sc_arpcom.ac_if.if_bpf) {
1159 1.21 briggs bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m);
1160 1.1 briggs
1161 1.1 briggs /*
1162 1.1 briggs * Note that the interface cannot be in promiscuous mode if
1163 1.1 briggs * there are no BPF listeners. And if we are in promiscuous
1164 1.1 briggs * mode, we have to check if this packet is really ours.
1165 1.1 briggs */
1166 1.21 briggs if ((sc->sc_arpcom.ac_if.if_flags & IFF_PROMISC) &&
1167 1.21 briggs (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
1168 1.21 briggs bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
1169 1.21 briggs sizeof(eh->ether_dhost)) != 0) {
1170 1.21 briggs m_freem(m);
1171 1.1 briggs return;
1172 1.1 briggs }
1173 1.1 briggs }
1174 1.1 briggs #endif
1175 1.1 briggs
1176 1.21 briggs /* Fix up data start offset in mbuf to point past ether header. */
1177 1.21 briggs m_adj(m, sizeof(struct ether_header));
1178 1.21 briggs ether_input(&sc->sc_arpcom.ac_if, eh, m);
1179 1.1 briggs }
1180 1.1 briggs
1181 1.1 briggs /*
1182 1.21 briggs * Supporting routines.
1183 1.1 briggs */
1184 1.1 briggs
1185 1.1 briggs /*
1186 1.21 briggs * Given a source and destination address, copy 'amount' of a packet from the
1187 1.21 briggs * ring buffer into a linear destination buffer. Takes into account ring-wrap.
1188 1.1 briggs */
1189 1.21 briggs static inline caddr_t
1190 1.21 briggs ae_ring_copy(sc, src, dst, amount)
1191 1.1 briggs struct ae_softc *sc;
1192 1.21 briggs caddr_t src, dst;
1193 1.1 briggs u_short amount;
1194 1.1 briggs {
1195 1.1 briggs u_short tmp_amount;
1196 1.1 briggs
1197 1.21 briggs /* Does copy wrap to lower addr in ring buffer? */
1198 1.21 briggs if (src + amount > sc->mem_end) {
1199 1.21 briggs tmp_amount = sc->mem_end - src;
1200 1.21 briggs
1201 1.21 briggs /* Copy amount up to end of NIC memory. */
1202 1.21 briggs word_copy(src, dst, tmp_amount);
1203 1.21 briggs
1204 1.1 briggs amount -= tmp_amount;
1205 1.21 briggs src = sc->mem_ring;
1206 1.1 briggs dst += tmp_amount;
1207 1.1 briggs }
1208 1.1 briggs
1209 1.21 briggs word_copy(src, dst, amount);
1210 1.1 briggs
1211 1.21 briggs return (src + amount);
1212 1.1 briggs }
1213 1.1 briggs
1214 1.1 briggs /*
1215 1.21 briggs * Copy data from receive buffer to end of mbuf chain allocate additional mbufs
1216 1.21 briggs * as needed. Return pointer to last mbuf in chain.
1217 1.21 briggs * sc = ae info (softc)
1218 1.21 briggs * src = pointer in ae ring buffer
1219 1.1 briggs * dst = pointer to last mbuf in mbuf chain to copy to
1220 1.1 briggs * amount = amount of data to copy
1221 1.1 briggs */
1222 1.1 briggs struct mbuf *
1223 1.21 briggs ae_ring_to_mbuf(sc, src, dst, total_len)
1224 1.1 briggs struct ae_softc *sc;
1225 1.21 briggs caddr_t src;
1226 1.1 briggs struct mbuf *dst;
1227 1.1 briggs u_short total_len;
1228 1.1 briggs {
1229 1.1 briggs register struct mbuf *m = dst;
1230 1.1 briggs
1231 1.21 briggs /* Round the length to a word boundary. */
1232 1.21 briggs total_len = (total_len + 1) & ~1;
1233 1.21 briggs
1234 1.1 briggs while (total_len) {
1235 1.1 briggs register u_short amount = min(total_len, M_TRAILINGSPACE(m));
1236 1.1 briggs
1237 1.15 briggs if (amount == 0) {
1238 1.1 briggs /*
1239 1.21 briggs * No more data in this mbuf; alloc another.
1240 1.21 briggs *
1241 1.1 briggs * If there is enough data for an mbuf cluster, attempt
1242 1.21 briggs * to allocate one of those, otherwise, a regular mbuf
1243 1.21 briggs * will do.
1244 1.1 briggs * Note that a regular mbuf is always required, even if
1245 1.21 briggs * we get a cluster - getting a cluster does not
1246 1.21 briggs * allocate any mbufs, and one is needed to assign the
1247 1.21 briggs * cluster to. The mbuf that has a cluster extension
1248 1.21 briggs * can not be used to contain data - only the cluster
1249 1.21 briggs * can contain data.
1250 1.21 briggs */
1251 1.1 briggs dst = m;
1252 1.1 briggs MGET(m, M_DONTWAIT, MT_DATA);
1253 1.1 briggs if (m == 0)
1254 1.1 briggs return (0);
1255 1.1 briggs
1256 1.1 briggs if (total_len >= MINCLSIZE)
1257 1.1 briggs MCLGET(m, M_DONTWAIT);
1258 1.1 briggs
1259 1.1 briggs m->m_len = 0;
1260 1.1 briggs dst->m_next = m;
1261 1.1 briggs amount = min(total_len, M_TRAILINGSPACE(m));
1262 1.1 briggs }
1263 1.1 briggs
1264 1.15 briggs src = ae_ring_copy(sc, src, mtod(m, caddr_t) + m->m_len,
1265 1.21 briggs amount);
1266 1.1 briggs
1267 1.1 briggs m->m_len += amount;
1268 1.1 briggs total_len -= amount;
1269 1.21 briggs }
1270 1.21 briggs return (m);
1271 1.21 briggs }
1272 1.21 briggs
1273 1.21 briggs /*
1274 1.21 briggs * Compute the multicast address filter from the list of multicast addresses we
1275 1.21 briggs * need to listen to.
1276 1.21 briggs */
1277 1.21 briggs void
1278 1.21 briggs ae_getmcaf(ac, af)
1279 1.21 briggs struct arpcom *ac;
1280 1.21 briggs u_long *af;
1281 1.21 briggs {
1282 1.21 briggs struct ifnet *ifp = &ac->ac_if;
1283 1.21 briggs struct ether_multi *enm;
1284 1.21 briggs register u_char *cp, c;
1285 1.21 briggs register u_long crc;
1286 1.21 briggs register int i, len;
1287 1.21 briggs struct ether_multistep step;
1288 1.21 briggs
1289 1.21 briggs /*
1290 1.21 briggs * Set up multicast address filter by passing all multicast addresses
1291 1.21 briggs * through a crc generator, and then using the high order 6 bits as an
1292 1.21 briggs * index into the 64 bit logical address filter. The high order bit
1293 1.21 briggs * selects the word, while the rest of the bits select the bit within
1294 1.21 briggs * the word.
1295 1.21 briggs */
1296 1.21 briggs
1297 1.21 briggs if (ifp->if_flags & IFF_PROMISC) {
1298 1.21 briggs ifp->if_flags |= IFF_ALLMULTI;
1299 1.21 briggs af[0] = af[1] = 0xffffffff;
1300 1.21 briggs return;
1301 1.21 briggs }
1302 1.21 briggs
1303 1.21 briggs af[0] = af[1] = 0;
1304 1.21 briggs ETHER_FIRST_MULTI(step, ac, enm);
1305 1.21 briggs while (enm != NULL) {
1306 1.21 briggs if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
1307 1.21 briggs sizeof(enm->enm_addrlo)) != 0) {
1308 1.21 briggs /*
1309 1.21 briggs * We must listen to a range of multicast addresses.
1310 1.21 briggs * For now, just accept all multicasts, rather than
1311 1.21 briggs * trying to set only those filter bits needed to match
1312 1.21 briggs * the range. (At this time, the only use of address
1313 1.21 briggs * ranges is for IP multicast routing, for which the
1314 1.21 briggs * range is big enough to require all bits set.)
1315 1.21 briggs */
1316 1.21 briggs ifp->if_flags |= IFF_ALLMULTI;
1317 1.21 briggs af[0] = af[1] = 0xffffffff;
1318 1.21 briggs return;
1319 1.21 briggs }
1320 1.1 briggs
1321 1.21 briggs cp = enm->enm_addrlo;
1322 1.21 briggs crc = 0xffffffff;
1323 1.21 briggs for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1324 1.21 briggs c = *cp++;
1325 1.21 briggs for (i = 8; --i >= 0;) {
1326 1.21 briggs if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
1327 1.21 briggs crc <<= 1;
1328 1.21 briggs crc ^= 0x04c11db6 | 1;
1329 1.21 briggs } else
1330 1.21 briggs crc <<= 1;
1331 1.21 briggs c >>= 1;
1332 1.21 briggs }
1333 1.21 briggs }
1334 1.21 briggs /* Just want the 6 most significant bits. */
1335 1.21 briggs crc >>= 26;
1336 1.21 briggs
1337 1.21 briggs /* Turn on the corresponding bit in the filter. */
1338 1.21 briggs af[crc >> 5] |= 1 << ((crc & 0x1f) ^ 0);
1339 1.21 briggs
1340 1.21 briggs ETHER_NEXT_MULTI(step, enm);
1341 1.1 briggs }
1342 1.21 briggs ifp->if_flags &= ~IFF_ALLMULTI;
1343 1.21 briggs }
1344 1.21 briggs
1345 1.21 briggs /*
1346 1.21 briggs * Copy packet from mbuf to the board memory
1347 1.21 briggs *
1348 1.21 briggs * Currently uses an extra buffer/extra memory copy,
1349 1.21 briggs * unless the whole packet fits in one mbuf.
1350 1.21 briggs *
1351 1.21 briggs */
1352 1.21 briggs u_short
1353 1.21 briggs ae_put(sc, m, buf)
1354 1.21 briggs struct ae_softc *sc;
1355 1.21 briggs struct mbuf *m;
1356 1.21 briggs caddr_t buf;
1357 1.21 briggs {
1358 1.21 briggs u_char *data, savebyte[2];
1359 1.21 briggs int len, wantbyte;
1360 1.21 briggs u_short totlen;
1361 1.21 briggs
1362 1.21 briggs wantbyte = 0;
1363 1.21 briggs
1364 1.21 briggs for (; m != 0; m = m->m_next) {
1365 1.21 briggs data = mtod(m, u_char *);
1366 1.21 briggs len = m->m_len;
1367 1.21 briggs totlen += len;
1368 1.21 briggs if (len > 0) {
1369 1.21 briggs /* Finish the last word. */
1370 1.21 briggs if (wantbyte) {
1371 1.21 briggs savebyte[1] = *data;
1372 1.21 briggs word_copy(savebyte, buf, 2);
1373 1.21 briggs buf += 2;
1374 1.21 briggs data++;
1375 1.21 briggs len--;
1376 1.21 briggs wantbyte = 0;
1377 1.21 briggs }
1378 1.21 briggs /* Output contiguous words. */
1379 1.21 briggs if (len > 1) {
1380 1.21 briggs word_copy(data, buf, len);
1381 1.21 briggs buf += len & ~1;
1382 1.21 briggs data += len & ~1;
1383 1.21 briggs len &= 1;
1384 1.21 briggs }
1385 1.21 briggs /* Save last byte, if necessary. */
1386 1.21 briggs if (len == 1) {
1387 1.21 briggs savebyte[0] = *data;
1388 1.21 briggs wantbyte = 1;
1389 1.21 briggs }
1390 1.21 briggs }
1391 1.21 briggs }
1392 1.21 briggs
1393 1.21 briggs if (wantbyte) {
1394 1.21 briggs savebyte[1] = 0;
1395 1.21 briggs word_copy(savebyte, buf, 2);
1396 1.21 briggs buf += 2;
1397 1.21 briggs }
1398 1.21 briggs
1399 1.21 briggs return (totlen);
1400 1.1 briggs }
1401