if_sn.c revision 1.16 1 1.16 briggs /* $NetBSD: if_sn.c,v 1.16 1997/11/04 13:02:45 briggs Exp $ */
2 1.1 briggs
3 1.1 briggs /*
4 1.15 scottr * National Semiconductor DP8393X SONIC Driver
5 1.1 briggs * Copyright (c) 1991 Algorithmics Ltd (http://www.algor.co.uk)
6 1.1 briggs * You may use, copy, and modify this program so long as you retain the
7 1.1 briggs * copyright line.
8 1.1 briggs *
9 1.1 briggs * This driver has been substantially modified since Algorithmics donated
10 1.1 briggs * it.
11 1.3 briggs *
12 1.5 briggs * Denton Gentry <denny1 (at) home.com>
13 1.3 briggs * and also
14 1.6 briggs * Yanagisawa Takeshi <yanagisw (at) aa.ap.titech.ac.jp>
15 1.3 briggs * did the work to get this running on the Macintosh.
16 1.1 briggs */
17 1.1 briggs
18 1.1 briggs #include <sys/param.h>
19 1.1 briggs #include <sys/systm.h>
20 1.1 briggs #include <sys/mbuf.h>
21 1.1 briggs #include <sys/buf.h>
22 1.1 briggs #include <sys/protosw.h>
23 1.1 briggs #include <sys/socket.h>
24 1.1 briggs #include <sys/syslog.h>
25 1.1 briggs #include <sys/ioctl.h>
26 1.1 briggs #include <sys/errno.h>
27 1.1 briggs #include <sys/device.h>
28 1.1 briggs
29 1.1 briggs #include <net/if.h>
30 1.8 briggs #include <net/if_dl.h>
31 1.2 is #include <net/if_ether.h>
32 1.1 briggs
33 1.1 briggs #ifdef INET
34 1.1 briggs #include <netinet/in.h>
35 1.1 briggs #include <netinet/in_systm.h>
36 1.1 briggs #include <netinet/in_var.h>
37 1.1 briggs #include <netinet/ip.h>
38 1.2 is #include <netinet/if_inarp.h>
39 1.1 briggs #endif
40 1.1 briggs
41 1.1 briggs #include <vm/vm.h>
42 1.1 briggs
43 1.1 briggs #include "bpfilter.h"
44 1.1 briggs #if NBPFILTER > 0
45 1.1 briggs #include <net/bpf.h>
46 1.1 briggs #include <net/bpfdesc.h>
47 1.1 briggs #endif
48 1.1 briggs
49 1.1 briggs #include <machine/bus.h>
50 1.1 briggs #include <machine/cpu.h>
51 1.1 briggs #include <machine/viareg.h>
52 1.1 briggs #include <mac68k/dev/if_snreg.h>
53 1.1 briggs #include <mac68k/dev/if_snvar.h>
54 1.1 briggs
55 1.12 scottr static void snwatchdog __P((struct ifnet *));
56 1.12 scottr static int sninit __P((struct sn_softc *sc));
57 1.12 scottr static int snstop __P((struct sn_softc *sc));
58 1.12 scottr static int snioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
59 1.12 scottr static void snstart __P((struct ifnet *ifp));
60 1.12 scottr static void snreset __P((struct sn_softc *sc));
61 1.12 scottr
62 1.12 scottr static void caminitialise __P((struct sn_softc *));
63 1.12 scottr static void camentry __P((struct sn_softc *, int, u_char *ea));
64 1.12 scottr static void camprogram __P((struct sn_softc *));
65 1.12 scottr static void initialise_tda __P((struct sn_softc *));
66 1.12 scottr static void initialise_rda __P((struct sn_softc *));
67 1.12 scottr static void initialise_rra __P((struct sn_softc *));
68 1.15 scottr #ifdef SNDEBUG
69 1.12 scottr static void camdump __P((struct sn_softc *sc));
70 1.12 scottr #endif
71 1.12 scottr
72 1.12 scottr static void sonictxint __P((struct sn_softc *));
73 1.12 scottr static void sonicrxint __P((struct sn_softc *));
74 1.1 briggs
75 1.15 scottr static __inline__ u_int sonicput __P((struct sn_softc *sc, struct mbuf *m0,
76 1.15 scottr int mtd_next));
77 1.12 scottr static __inline__ int sonic_read __P((struct sn_softc *, caddr_t, int));
78 1.12 scottr static __inline__ struct mbuf *sonic_get __P((struct sn_softc *,
79 1.12 scottr struct ether_header *, int));
80 1.1 briggs
81 1.1 briggs struct cfdriver sn_cd = {
82 1.1 briggs NULL, "sn", DV_IFNET
83 1.1 briggs };
84 1.1 briggs
85 1.1 briggs #undef assert
86 1.1 briggs #undef _assert
87 1.1 briggs
88 1.1 briggs #ifdef NDEBUG
89 1.1 briggs #define assert(e) ((void)0)
90 1.1 briggs #define _assert(e) ((void)0)
91 1.1 briggs #else
92 1.1 briggs #define _assert(e) assert(e)
93 1.1 briggs #ifdef __STDC__
94 1.1 briggs #define assert(e) ((e) ? (void)0 : __assert("sn ", __FILE__, __LINE__, #e))
95 1.1 briggs #else /* PCC */
96 1.1 briggs #define assert(e) ((e) ? (void)0 : __assert("sn "__FILE__, __LINE__, "e"))
97 1.1 briggs #endif
98 1.1 briggs #endif
99 1.1 briggs
100 1.12 scottr int sndebug = 0;
101 1.1 briggs
102 1.1 briggs /*
103 1.1 briggs * SONIC buffers need to be aligned 16 or 32 bit aligned.
104 1.1 briggs * These macros calculate and verify alignment.
105 1.1 briggs */
106 1.1 briggs #define ROUNDUP(p, N) (((int) p + N - 1) & ~(N - 1))
107 1.1 briggs
108 1.1 briggs #define SOALIGN(m, array) (m ? (ROUNDUP(array, 4)) : (ROUNDUP(array, 2)))
109 1.1 briggs
110 1.1 briggs #define LOWER(x) ((unsigned)(x) & 0xffff)
111 1.1 briggs #define UPPER(x) ((unsigned)(x) >> 16)
112 1.1 briggs
113 1.1 briggs /*
114 1.1 briggs * Interface exists: make available by filling in network interface
115 1.1 briggs * record. System will initialize the interface when it is ready
116 1.1 briggs * to accept packets.
117 1.1 briggs */
118 1.8 briggs int
119 1.2 is snsetup(sc, lladdr)
120 1.1 briggs struct sn_softc *sc;
121 1.2 is u_int8_t *lladdr;
122 1.1 briggs {
123 1.12 scottr struct ifnet *ifp = &sc->sc_if;
124 1.12 scottr u_char *p;
125 1.12 scottr u_char *pp;
126 1.12 scottr int i;
127 1.15 scottr int offset;
128 1.6 briggs
129 1.7 scottr /*
130 1.8 briggs * XXX if_sn.c is intended to be MI. Should it allocate memory
131 1.8 briggs * for its descriptor areas, or expect the MD attach code
132 1.8 briggs * to do that?
133 1.7 scottr */
134 1.8 briggs sc->space = malloc((SN_NPAGES + 1) * NBPG, M_DEVBUF, M_WAITOK);
135 1.8 briggs if (sc->space == NULL) {
136 1.8 briggs printf ("%s: memory allocation for descriptors failed\n",
137 1.15 scottr sc->sc_dev.dv_xname);
138 1.8 briggs return (1);
139 1.8 briggs }
140 1.1 briggs
141 1.7 scottr /*
142 1.8 briggs * Put the pup in reset mode (sninit() will fix it later),
143 1.8 briggs * stop the timer, disable all interrupts and clear any interrupts.
144 1.7 scottr */
145 1.9 briggs NIC_PUT(sc, SNR_CR, CR_STP);
146 1.9 briggs wbflush();
147 1.8 briggs NIC_PUT(sc, SNR_CR, CR_RST);
148 1.8 briggs wbflush();
149 1.8 briggs NIC_PUT(sc, SNR_IMR, 0);
150 1.1 briggs wbflush();
151 1.8 briggs NIC_PUT(sc, SNR_ISR, ISR_ALL);
152 1.1 briggs wbflush();
153 1.1 briggs
154 1.7 scottr /*
155 1.7 scottr * because the SONIC is basically 16bit device it 'concatenates'
156 1.7 scottr * a higher buffer address to a 16 bit offset--this will cause wrap
157 1.7 scottr * around problems near the end of 64k !!
158 1.7 scottr */
159 1.8 briggs p = sc->space;
160 1.12 scottr pp = (u_char *)ROUNDUP ((int)p, NBPG);
161 1.1 briggs p = pp;
162 1.1 briggs
163 1.8 briggs /*
164 1.8 briggs * Disable caching on the SONIC's data space.
165 1.8 briggs * The pages might not be physically contiguous, so set
166 1.8 briggs * each page individually.
167 1.8 briggs */
168 1.8 briggs for (i = 0; i < SN_NPAGES; i++) {
169 1.15 scottr physaccess (p, (caddr_t)SONIC_GETDMA(p), NBPG,
170 1.15 scottr PG_V | PG_RW | PG_CI);
171 1.8 briggs p += NBPG;
172 1.8 briggs }
173 1.8 briggs p = pp;
174 1.8 briggs
175 1.1 briggs for (i = 0; i < NRRA; i++) {
176 1.1 briggs sc->p_rra[i] = (void *)p;
177 1.15 scottr sc->v_rra[i] = SONIC_GETDMA(p);
178 1.1 briggs p += RXRSRC_SIZE(sc);
179 1.1 briggs }
180 1.15 scottr sc->v_rea = SONIC_GETDMA(p);
181 1.1 briggs
182 1.12 scottr p = (u_char *)SOALIGN(sc, p);
183 1.1 briggs
184 1.12 scottr sc->p_cda = (void *)(p);
185 1.15 scottr sc->v_cda = SONIC_GETDMA(p);
186 1.1 briggs p += CDA_SIZE(sc);
187 1.1 briggs
188 1.12 scottr p = (u_char *)SOALIGN(sc, p);
189 1.1 briggs
190 1.1 briggs for (i = 0; i < NTDA; i++) {
191 1.1 briggs struct mtd *mtdp = &sc->mtda[i];
192 1.1 briggs mtdp->mtd_txp = (void *)p;
193 1.15 scottr mtdp->mtd_vtxp = SONIC_GETDMA(p);
194 1.1 briggs p += TXP_SIZE(sc);
195 1.1 briggs }
196 1.1 briggs
197 1.12 scottr p = (u_char *)SOALIGN(sc, p);
198 1.1 briggs
199 1.1 briggs if ((p - pp) > NBPG) {
200 1.8 briggs printf ("%s: sizeof RRA (%ld) + CDA (%ld) +"
201 1.13 briggs "TDA (%ld) > NBPG (%d). Punt!\n",
202 1.12 scottr sc->sc_dev.dv_xname,
203 1.12 scottr (ulong)sc->p_cda - (ulong)sc->p_rra[0],
204 1.13 briggs (ulong)sc->mtda[0].mtd_txp - (ulong)sc->p_cda,
205 1.12 scottr (ulong)p - (ulong)sc->mtda[0].mtd_txp,
206 1.12 scottr NBPG);
207 1.8 briggs return(1);
208 1.1 briggs }
209 1.13 briggs
210 1.13 briggs p = pp + NBPG;
211 1.13 briggs pp = p;
212 1.13 briggs
213 1.15 scottr sc->sc_nrda = NBPG / RXPKT_SIZE(sc);
214 1.15 scottr sc->p_rda = (caddr_t) p;
215 1.15 scottr sc->v_rda = SONIC_GETDMA(p);
216 1.1 briggs
217 1.1 briggs p = pp + NBPG;
218 1.1 briggs
219 1.9 briggs for (i = 0; i < NRBA; i++) {
220 1.12 scottr sc->rbuf[i] = (caddr_t)p;
221 1.1 briggs p += NBPG;
222 1.1 briggs }
223 1.1 briggs
224 1.15 scottr pp = p;
225 1.15 scottr offset = TXBSIZE;
226 1.15 scottr for (i = 0; i < NTDA; i++) {
227 1.15 scottr struct mtd *mtdp = &sc->mtda[i];
228 1.15 scottr
229 1.15 scottr mtdp->mtd_buf = p;
230 1.15 scottr mtdp->mtd_vbuf = SONIC_GETDMA(p);
231 1.15 scottr offset += TXBSIZE;
232 1.15 scottr if (offset < NBPG) {
233 1.15 scottr p += TXBSIZE;
234 1.15 scottr } else {
235 1.15 scottr p = pp + NBPG;
236 1.15 scottr pp = p;
237 1.15 scottr offset = TXBSIZE;
238 1.15 scottr }
239 1.1 briggs }
240 1.1 briggs
241 1.15 scottr #ifdef SNDEBUG
242 1.1 briggs camdump(sc);
243 1.1 briggs #endif
244 1.2 is printf(" address %s\n", ether_sprintf(lladdr));
245 1.1 briggs
246 1.15 scottr #ifdef SNDEBUG
247 1.14 scottr printf("%s: buffers: rra=%p cda=%p rda=%p tda=%p\n",
248 1.12 scottr sc->sc_dev.dv_xname, sc->p_rra[0], sc->p_cda,
249 1.15 scottr sc->p_rda, sc->mtda[0].mtd_txp);
250 1.1 briggs #endif
251 1.4 briggs
252 1.1 briggs bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
253 1.1 briggs ifp->if_softc = sc;
254 1.1 briggs ifp->if_ioctl = snioctl;
255 1.1 briggs ifp->if_start = snstart;
256 1.8 briggs ifp->if_flags =
257 1.12 scottr IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
258 1.1 briggs ifp->if_watchdog = snwatchdog;
259 1.1 briggs #if NBPFILTER > 0
260 1.1 briggs bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
261 1.1 briggs #endif
262 1.1 briggs if_attach(ifp);
263 1.2 is ether_ifattach(ifp, lladdr);
264 1.1 briggs
265 1.8 briggs return (0);
266 1.1 briggs }
267 1.1 briggs
268 1.1 briggs static int
269 1.1 briggs snioctl(ifp, cmd, data)
270 1.1 briggs struct ifnet *ifp;
271 1.1 briggs u_long cmd;
272 1.1 briggs caddr_t data;
273 1.1 briggs {
274 1.12 scottr struct ifaddr *ifa;
275 1.12 scottr struct ifreq *ifr;
276 1.12 scottr struct sn_softc *sc = ifp->if_softc;
277 1.12 scottr int s = splnet(), err = 0;
278 1.12 scottr int temp;
279 1.1 briggs
280 1.1 briggs switch (cmd) {
281 1.1 briggs
282 1.1 briggs case SIOCSIFADDR:
283 1.1 briggs ifa = (struct ifaddr *)data;
284 1.1 briggs ifp->if_flags |= IFF_UP;
285 1.1 briggs switch (ifa->ifa_addr->sa_family) {
286 1.1 briggs #ifdef INET
287 1.1 briggs case AF_INET:
288 1.15 scottr (void)sninit(sc);
289 1.2 is arp_ifinit(ifp, ifa);
290 1.1 briggs break;
291 1.1 briggs #endif
292 1.1 briggs default:
293 1.15 scottr (void)sninit(sc);
294 1.1 briggs break;
295 1.1 briggs }
296 1.1 briggs break;
297 1.1 briggs
298 1.1 briggs case SIOCSIFFLAGS:
299 1.1 briggs if ((ifp->if_flags & IFF_UP) == 0 &&
300 1.15 scottr (ifp->if_flags & IFF_RUNNING) != 0) {
301 1.15 scottr /*
302 1.15 scottr * If interface is marked down and it is running,
303 1.15 scottr * then stop it.
304 1.15 scottr */
305 1.15 scottr snstop(sc);
306 1.1 briggs ifp->if_flags &= ~IFF_RUNNING;
307 1.15 scottr } else if ((ifp->if_flags & IFF_UP) != 0 &&
308 1.15 scottr (ifp->if_flags & IFF_RUNNING) == 0) {
309 1.15 scottr /*
310 1.15 scottr * If interface is marked up and it is stopped,
311 1.15 scottr * then start it.
312 1.15 scottr */
313 1.15 scottr (void)sninit(sc);
314 1.15 scottr } else {
315 1.15 scottr /*
316 1.15 scottr * reset the interface to pick up any other changes
317 1.15 scottr * in flags
318 1.15 scottr */
319 1.15 scottr temp = ifp->if_flags & IFF_UP;
320 1.1 briggs snreset(sc);
321 1.15 scottr ifp->if_flags |= temp;
322 1.1 briggs snstart(ifp);
323 1.1 briggs }
324 1.1 briggs break;
325 1.1 briggs
326 1.1 briggs case SIOCADDMULTI:
327 1.1 briggs case SIOCDELMULTI:
328 1.8 briggs ifr = (struct ifreq *) data;
329 1.8 briggs if (cmd == SIOCADDMULTI)
330 1.8 briggs err = ether_addmulti(ifr, &sc->sc_ethercom);
331 1.1 briggs else
332 1.8 briggs err = ether_delmulti(ifr, &sc->sc_ethercom);
333 1.1 briggs
334 1.1 briggs if (err == ENETRESET) {
335 1.1 briggs /*
336 1.1 briggs * Multicast list has changed; set the hardware
337 1.1 briggs * filter accordingly. But remember UP flag!
338 1.1 briggs */
339 1.15 scottr temp = ifp->if_flags & IFF_UP;
340 1.1 briggs snreset(sc);
341 1.15 scottr ifp->if_flags |= temp;
342 1.1 briggs err = 0;
343 1.1 briggs }
344 1.1 briggs break;
345 1.1 briggs default:
346 1.1 briggs err = EINVAL;
347 1.1 briggs }
348 1.1 briggs splx(s);
349 1.1 briggs return (err);
350 1.1 briggs }
351 1.1 briggs
352 1.1 briggs /*
353 1.1 briggs * Encapsulate a packet of type family for the local net.
354 1.1 briggs */
355 1.1 briggs static void
356 1.1 briggs snstart(ifp)
357 1.1 briggs struct ifnet *ifp;
358 1.1 briggs {
359 1.15 scottr struct sn_softc *sc = ifp->if_softc;
360 1.15 scottr struct mbuf *m;
361 1.15 scottr int mtd_next;
362 1.1 briggs
363 1.2 is if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
364 1.1 briggs return;
365 1.1 briggs
366 1.1 briggs outloop:
367 1.1 briggs /* Check for room in the xmit buffer. */
368 1.15 scottr if ((mtd_next = (sc->mtd_free + 1)) == NTDA)
369 1.15 scottr mtd_next = 0;
370 1.15 scottr
371 1.15 scottr if (mtd_next == sc->mtd_hw) {
372 1.1 briggs ifp->if_flags |= IFF_OACTIVE;
373 1.1 briggs return;
374 1.1 briggs }
375 1.1 briggs
376 1.2 is IF_DEQUEUE(&ifp->if_snd, m);
377 1.1 briggs if (m == 0)
378 1.1 briggs return;
379 1.1 briggs
380 1.1 briggs /* We need the header for m_pkthdr.len. */
381 1.1 briggs if ((m->m_flags & M_PKTHDR) == 0)
382 1.12 scottr panic("%s: snstart: no header mbuf", sc->sc_dev.dv_xname);
383 1.1 briggs
384 1.1 briggs #if NBPFILTER > 0
385 1.1 briggs /*
386 1.1 briggs * If bpf is listening on this interface, let it
387 1.1 briggs * see the packet before we commit it to the wire.
388 1.1 briggs */
389 1.2 is if (ifp->if_bpf)
390 1.2 is bpf_mtap(ifp->if_bpf, m);
391 1.1 briggs #endif
392 1.1 briggs
393 1.1 briggs /*
394 1.1 briggs * If there is nothing in the o/p queue, and there is room in
395 1.1 briggs * the Tx ring, then send the packet directly. Otherwise append
396 1.1 briggs * it to the o/p queue.
397 1.1 briggs */
398 1.15 scottr if ((sonicput(sc, m, mtd_next)) == 0) {
399 1.3 briggs IF_PREPEND(&ifp->if_snd, m);
400 1.3 briggs return;
401 1.1 briggs }
402 1.1 briggs
403 1.15 scottr sc->mtd_prev = sc->mtd_free;
404 1.15 scottr sc->mtd_free = mtd_next;
405 1.1 briggs
406 1.2 is ifp->if_opackets++; /* # of pkts */
407 1.1 briggs
408 1.1 briggs /* Jump back for possibly more punishment. */
409 1.1 briggs goto outloop;
410 1.1 briggs }
411 1.1 briggs
412 1.1 briggs /*
413 1.1 briggs * reset and restart the SONIC. Called in case of fatal
414 1.1 briggs * hardware/software errors.
415 1.1 briggs */
416 1.1 briggs static void
417 1.1 briggs snreset(sc)
418 1.1 briggs struct sn_softc *sc;
419 1.1 briggs {
420 1.1 briggs snstop(sc);
421 1.1 briggs sninit(sc);
422 1.1 briggs }
423 1.1 briggs
424 1.1 briggs static int
425 1.1 briggs sninit(sc)
426 1.1 briggs struct sn_softc *sc;
427 1.1 briggs {
428 1.12 scottr u_long s_rcr;
429 1.12 scottr int s;
430 1.1 briggs
431 1.1 briggs if (sc->sc_if.if_flags & IFF_RUNNING)
432 1.1 briggs /* already running */
433 1.1 briggs return (0);
434 1.1 briggs
435 1.1 briggs s = splnet();
436 1.1 briggs
437 1.8 briggs NIC_PUT(sc, SNR_CR, CR_RST); /* DCR only accessable in reset mode! */
438 1.1 briggs
439 1.1 briggs /* config it */
440 1.15 scottr NIC_PUT(sc, SNR_DCR, (sc->snr_dcr |
441 1.15 scottr (sc->bitmode ? DCR_DW32 : DCR_DW16)));
442 1.8 briggs NIC_PUT(sc, SNR_DCR2, sc->snr_dcr2);
443 1.8 briggs
444 1.8 briggs s_rcr = RCR_BRD | RCR_LBNONE;
445 1.8 briggs if (sc->sc_if.if_flags & IFF_PROMISC)
446 1.8 briggs s_rcr |= RCR_PRO;
447 1.8 briggs if (sc->sc_if.if_flags & IFF_ALLMULTI)
448 1.8 briggs s_rcr |= RCR_AMC;
449 1.8 briggs NIC_PUT(sc, SNR_RCR, s_rcr);
450 1.8 briggs
451 1.8 briggs NIC_PUT(sc, SNR_IMR, (IMR_PRXEN | IMR_PTXEN | IMR_TXEREN | IMR_LCDEN));
452 1.1 briggs
453 1.1 briggs /* clear pending interrupts */
454 1.8 briggs NIC_PUT(sc, SNR_ISR, ISR_ALL);
455 1.1 briggs
456 1.1 briggs /* clear tally counters */
457 1.8 briggs NIC_PUT(sc, SNR_CRCT, -1);
458 1.8 briggs NIC_PUT(sc, SNR_FAET, -1);
459 1.8 briggs NIC_PUT(sc, SNR_MPT, -1);
460 1.1 briggs
461 1.1 briggs initialise_tda(sc);
462 1.1 briggs initialise_rda(sc);
463 1.1 briggs initialise_rra(sc);
464 1.1 briggs
465 1.1 briggs /* enable the chip */
466 1.8 briggs NIC_PUT(sc, SNR_CR, 0);
467 1.1 briggs wbflush();
468 1.1 briggs
469 1.8 briggs /* program the CAM */
470 1.1 briggs camprogram(sc);
471 1.1 briggs
472 1.1 briggs /* get it to read resource descriptors */
473 1.8 briggs NIC_PUT(sc, SNR_CR, CR_RRRA);
474 1.1 briggs wbflush();
475 1.8 briggs while ((NIC_GET(sc, SNR_CR)) & CR_RRRA)
476 1.1 briggs continue;
477 1.1 briggs
478 1.1 briggs /* enable rx */
479 1.8 briggs NIC_PUT(sc, SNR_CR, CR_RXEN);
480 1.1 briggs wbflush();
481 1.1 briggs
482 1.1 briggs /* flag interface as "running" */
483 1.1 briggs sc->sc_if.if_flags |= IFF_RUNNING;
484 1.15 scottr sc->sc_if.if_flags &= ~IFF_OACTIVE;
485 1.1 briggs
486 1.1 briggs splx(s);
487 1.1 briggs return (0);
488 1.1 briggs }
489 1.1 briggs
490 1.1 briggs /*
491 1.1 briggs * close down an interface and free its buffers
492 1.1 briggs * Called on final close of device, or if sninit() fails
493 1.1 briggs * part way through.
494 1.1 briggs */
495 1.1 briggs static int
496 1.1 briggs snstop(sc)
497 1.1 briggs struct sn_softc *sc;
498 1.1 briggs {
499 1.1 briggs struct mtd *mtd;
500 1.12 scottr int s = splnet();
501 1.1 briggs
502 1.1 briggs /* stick chip in reset */
503 1.8 briggs NIC_PUT(sc, SNR_CR, CR_RST);
504 1.1 briggs wbflush();
505 1.1 briggs
506 1.1 briggs /* free all receive buffers (currently static so nothing to do) */
507 1.1 briggs
508 1.1 briggs /* free all pending transmit mbufs */
509 1.1 briggs while (sc->mtd_hw != sc->mtd_free) {
510 1.1 briggs mtd = &sc->mtda[sc->mtd_hw];
511 1.15 scottr if (mtd->mtd_mbuf)
512 1.15 scottr m_freem(mtd->mtd_mbuf);
513 1.1 briggs if (++sc->mtd_hw == NTDA) sc->mtd_hw = 0;
514 1.1 briggs }
515 1.1 briggs
516 1.1 briggs sc->sc_if.if_timer = 0;
517 1.1 briggs sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
518 1.1 briggs
519 1.1 briggs splx(s);
520 1.1 briggs return (0);
521 1.1 briggs }
522 1.1 briggs
523 1.1 briggs /*
524 1.1 briggs * Called if any Tx packets remain unsent after 5 seconds,
525 1.1 briggs * In all cases we just reset the chip, and any retransmission
526 1.1 briggs * will be handled by higher level protocol timeouts.
527 1.1 briggs */
528 1.1 briggs static void
529 1.1 briggs snwatchdog(ifp)
530 1.1 briggs struct ifnet *ifp;
531 1.1 briggs {
532 1.1 briggs struct sn_softc *sc = ifp->if_softc;
533 1.12 scottr struct mtd *mtd;
534 1.12 scottr int temp;
535 1.1 briggs
536 1.1 briggs if (sc->mtd_hw != sc->mtd_free) {
537 1.1 briggs /* something still pending for transmit */
538 1.1 briggs mtd = &sc->mtda[sc->mtd_hw];
539 1.1 briggs if (SRO(sc->bitmode, mtd->mtd_txp, TXP_STATUS) == 0)
540 1.1 briggs log(LOG_ERR, "%s: Tx - timeout\n",
541 1.12 scottr sc->sc_dev.dv_xname);
542 1.1 briggs else
543 1.1 briggs log(LOG_ERR, "%s: Tx - lost interrupt\n",
544 1.12 scottr sc->sc_dev.dv_xname);
545 1.2 is temp = ifp->if_flags & IFF_UP;
546 1.1 briggs snreset(sc);
547 1.2 is ifp->if_flags |= temp;
548 1.1 briggs }
549 1.1 briggs }
550 1.1 briggs
551 1.1 briggs /*
552 1.1 briggs * stuff packet into sonic (at splnet)
553 1.1 briggs */
554 1.15 scottr static __inline__ u_int
555 1.15 scottr sonicput(sc, m0, mtd_next)
556 1.1 briggs struct sn_softc *sc;
557 1.1 briggs struct mbuf *m0;
558 1.15 scottr int mtd_next;
559 1.1 briggs {
560 1.12 scottr struct mtd *mtdp;
561 1.12 scottr struct mbuf *m;
562 1.15 scottr u_char *buff;
563 1.12 scottr void *txp;
564 1.12 scottr u_int len = 0;
565 1.12 scottr u_int totlen = 0;
566 1.3 briggs
567 1.15 scottr #ifdef whyonearthwouldyoudothis
568 1.12 scottr if (NIC_GET(sc, SNR_CR) & CR_TXP)
569 1.3 briggs return (0);
570 1.15 scottr #endif
571 1.1 briggs
572 1.1 briggs /* grab the replacement mtd */
573 1.15 scottr mtdp = &sc->mtda[sc->mtd_free];
574 1.1 briggs
575 1.15 scottr buff = mtdp->mtd_buf;
576 1.1 briggs
577 1.1 briggs /* this packet goes to mtdnext fill in the TDA */
578 1.15 scottr mtdp->mtd_mbuf = m0;
579 1.1 briggs txp = mtdp->mtd_txp;
580 1.15 scottr
581 1.15 scottr /* Write to the config word. Every (NTDA/2)+1 packets we set an intr */
582 1.15 scottr if (sc->mtd_pint == 0) {
583 1.15 scottr sc->mtd_pint = NTDA/2;
584 1.15 scottr SWO(sc->bitmode, txp, TXP_CONFIG, TCR_PINT);
585 1.15 scottr } else {
586 1.15 scottr sc->mtd_pint--;
587 1.15 scottr SWO(sc->bitmode, txp, TXP_CONFIG, 0);
588 1.15 scottr }
589 1.1 briggs
590 1.1 briggs for (m = m0; m; m = m->m_next) {
591 1.12 scottr u_char *data = mtod(m, u_char *);
592 1.1 briggs len = m->m_len;
593 1.1 briggs totlen += len;
594 1.1 briggs bcopy(data, buff, len);
595 1.1 briggs buff += len;
596 1.1 briggs }
597 1.1 briggs if (totlen >= TXBSIZE) {
598 1.12 scottr panic("%s: sonicput: packet overflow", sc->sc_dev.dv_xname);
599 1.1 briggs }
600 1.15 scottr
601 1.12 scottr SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRLO,
602 1.15 scottr LOWER(mtdp->mtd_vbuf));
603 1.12 scottr SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FPTRHI,
604 1.15 scottr UPPER(mtdp->mtd_vbuf));
605 1.1 briggs
606 1.1 briggs if (totlen < ETHERMIN + sizeof(struct ether_header)) {
607 1.1 briggs int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
608 1.15 scottr bzero(mtdp->mtd_buf + totlen, pad);
609 1.1 briggs totlen = ETHERMIN + sizeof(struct ether_header);
610 1.1 briggs }
611 1.1 briggs
612 1.12 scottr SWO(sc->bitmode, txp, TXP_FRAGOFF + (0 * TXP_FRAGSIZE) + TXP_FSIZE,
613 1.12 scottr totlen);
614 1.1 briggs SWO(sc->bitmode, txp, TXP_FRAGCNT, 1);
615 1.1 briggs SWO(sc->bitmode, txp, TXP_PKTSIZE, totlen);
616 1.1 briggs
617 1.1 briggs /* link onto the next mtd that will be used */
618 1.12 scottr SWO(sc->bitmode, txp, TXP_FRAGOFF + (1 * TXP_FRAGSIZE) + TXP_FPTRLO,
619 1.12 scottr LOWER(sc->mtda[mtd_next].mtd_vtxp) | EOL);
620 1.1 briggs
621 1.1 briggs /*
622 1.1 briggs * The previous txp.tlink currently contains a pointer to
623 1.1 briggs * our txp | EOL. Want to clear the EOL, so write our
624 1.1 briggs * pointer to the previous txp.
625 1.1 briggs */
626 1.1 briggs SWO(sc->bitmode, sc->mtda[sc->mtd_prev].mtd_txp, sc->mtd_tlinko,
627 1.12 scottr LOWER(mtdp->mtd_vtxp));
628 1.1 briggs
629 1.1 briggs /* make sure chip is running */
630 1.1 briggs wbflush();
631 1.8 briggs NIC_PUT(sc, SNR_CR, CR_TXP);
632 1.1 briggs wbflush();
633 1.1 briggs sc->sc_if.if_timer = 5; /* 5 seconds to watch for failing to transmit */
634 1.1 briggs
635 1.1 briggs return (totlen);
636 1.1 briggs }
637 1.1 briggs
638 1.12 scottr /*
639 1.12 scottr * These are called from sonicioctl() when /etc/ifconfig is run to set
640 1.12 scottr * the address or switch the i/f on.
641 1.12 scottr */
642 1.1 briggs /*
643 1.1 briggs * CAM support
644 1.1 briggs */
645 1.8 briggs static void
646 1.1 briggs caminitialise(sc)
647 1.1 briggs struct sn_softc *sc;
648 1.1 briggs {
649 1.1 briggs void *p_cda = sc->p_cda;
650 1.12 scottr int i;
651 1.1 briggs int bitmode = sc->bitmode;
652 1.8 briggs int camoffset;
653 1.1 briggs
654 1.8 briggs for (i = 0; i < MAXCAM; i++) {
655 1.8 briggs camoffset = i * CDA_CAMDESC;
656 1.8 briggs SWO(bitmode, p_cda, (camoffset + CDA_CAMEP), i);
657 1.8 briggs SWO(bitmode, p_cda, (camoffset + CDA_CAMAP2), 0);
658 1.8 briggs SWO(bitmode, p_cda, (camoffset + CDA_CAMAP1), 0);
659 1.8 briggs SWO(bitmode, p_cda, (camoffset + CDA_CAMAP0), 0);
660 1.8 briggs }
661 1.1 briggs SWO(bitmode, p_cda, CDA_ENABLE, 0);
662 1.1 briggs }
663 1.1 briggs
664 1.8 briggs static void
665 1.1 briggs camentry(sc, entry, ea)
666 1.1 briggs int entry;
667 1.12 scottr u_char *ea;
668 1.1 briggs struct sn_softc *sc;
669 1.1 briggs {
670 1.12 scottr void *p_cda = sc->p_cda;
671 1.1 briggs int bitmode = sc->bitmode;
672 1.8 briggs int camoffset = entry * CDA_CAMDESC;
673 1.1 briggs
674 1.1 briggs SWO(bitmode, p_cda, camoffset + CDA_CAMEP, entry);
675 1.1 briggs SWO(bitmode, p_cda, camoffset + CDA_CAMAP2, (ea[5] << 8) | ea[4]);
676 1.1 briggs SWO(bitmode, p_cda, camoffset + CDA_CAMAP1, (ea[3] << 8) | ea[2]);
677 1.1 briggs SWO(bitmode, p_cda, camoffset + CDA_CAMAP0, (ea[1] << 8) | ea[0]);
678 1.8 briggs SWO(bitmode, p_cda, CDA_ENABLE,
679 1.12 scottr (SRO(bitmode, p_cda, CDA_ENABLE) | (1 << entry)));
680 1.1 briggs }
681 1.1 briggs
682 1.8 briggs static void
683 1.1 briggs camprogram(sc)
684 1.1 briggs struct sn_softc *sc;
685 1.1 briggs {
686 1.12 scottr struct ether_multistep step;
687 1.12 scottr struct ether_multi *enm;
688 1.12 scottr struct ifnet *ifp;
689 1.12 scottr int timeout;
690 1.12 scottr int mcount = 0;
691 1.8 briggs
692 1.8 briggs caminitialise(sc);
693 1.8 briggs
694 1.8 briggs ifp = &sc->sc_if;
695 1.8 briggs
696 1.8 briggs /* Always load our own address first. */
697 1.8 briggs camentry (sc, mcount, LLADDR(ifp->if_sadl));
698 1.8 briggs mcount++;
699 1.8 briggs
700 1.8 briggs /* Assume we won't need allmulti bit. */
701 1.8 briggs ifp->if_flags &= ~IFF_ALLMULTI;
702 1.8 briggs
703 1.8 briggs /* Loop through multicast addresses */
704 1.8 briggs ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
705 1.8 briggs while (enm != NULL) {
706 1.8 briggs if (mcount == MAXCAM) {
707 1.8 briggs ifp->if_flags |= IFF_ALLMULTI;
708 1.8 briggs break;
709 1.8 briggs }
710 1.8 briggs
711 1.8 briggs if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
712 1.12 scottr sizeof(enm->enm_addrlo)) != 0) {
713 1.8 briggs /*
714 1.8 briggs * SONIC's CAM is programmed with specific
715 1.8 briggs * addresses. It has no way to specify a range.
716 1.8 briggs * (Well, thats not exactly true. If the
717 1.8 briggs * range is small one could program each addr
718 1.8 briggs * within the range as a seperate CAM entry)
719 1.8 briggs */
720 1.8 briggs ifp->if_flags |= IFF_ALLMULTI;
721 1.8 briggs break;
722 1.8 briggs }
723 1.8 briggs
724 1.8 briggs /* program the CAM with the specified entry */
725 1.8 briggs camentry(sc, mcount, enm->enm_addrlo);
726 1.8 briggs mcount++;
727 1.1 briggs
728 1.8 briggs ETHER_NEXT_MULTI(step, enm);
729 1.8 briggs }
730 1.8 briggs
731 1.8 briggs NIC_PUT(sc, SNR_CDP, LOWER(sc->v_cda));
732 1.8 briggs NIC_PUT(sc, SNR_CDC, MAXCAM);
733 1.8 briggs NIC_PUT(sc, SNR_CR, CR_LCAM);
734 1.1 briggs wbflush();
735 1.1 briggs
736 1.1 briggs timeout = 10000;
737 1.8 briggs while ((NIC_GET(sc, SNR_CR) & CR_LCAM) && timeout--)
738 1.1 briggs continue;
739 1.1 briggs if (timeout == 0) {
740 1.1 briggs /* XXX */
741 1.12 scottr panic("%s: CAM initialisation failed\n", sc->sc_dev.dv_xname);
742 1.1 briggs }
743 1.1 briggs timeout = 10000;
744 1.8 briggs while (((NIC_GET(sc, SNR_ISR) & ISR_LCD) == 0) && timeout--)
745 1.1 briggs continue;
746 1.1 briggs
747 1.8 briggs if (NIC_GET(sc, SNR_ISR) & ISR_LCD)
748 1.8 briggs NIC_PUT(sc, SNR_ISR, ISR_LCD);
749 1.1 briggs else
750 1.7 scottr printf("%s: CAM initialisation without interrupt\n",
751 1.7 scottr sc->sc_dev.dv_xname);
752 1.1 briggs }
753 1.1 briggs
754 1.15 scottr #ifdef SNDEBUG
755 1.8 briggs static void
756 1.1 briggs camdump(sc)
757 1.1 briggs struct sn_softc *sc;
758 1.1 briggs {
759 1.12 scottr int i;
760 1.1 briggs
761 1.1 briggs printf("CAM entries:\n");
762 1.8 briggs NIC_PUT(sc, SNR_CR, CR_RST);
763 1.1 briggs wbflush();
764 1.1 briggs
765 1.1 briggs for (i = 0; i < 16; i++) {
766 1.1 briggs ushort ap2, ap1, ap0;
767 1.8 briggs NIC_PUT(sc, SNR_CEP, i);
768 1.1 briggs wbflush();
769 1.8 briggs ap2 = NIC_GET(sc, SNR_CAP2);
770 1.8 briggs ap1 = NIC_GET(sc, SNR_CAP1);
771 1.8 briggs ap0 = NIC_GET(sc, SNR_CAP0);
772 1.1 briggs printf("%d: ap2=0x%x ap1=0x%x ap0=0x%x\n", i, ap2, ap1, ap0);
773 1.1 briggs }
774 1.14 scottr printf("CAM enable 0x%x\n", NIC_GET(sc, SNR_CEP));
775 1.1 briggs
776 1.8 briggs NIC_PUT(sc, SNR_CR, 0);
777 1.1 briggs wbflush();
778 1.1 briggs }
779 1.1 briggs #endif
780 1.1 briggs
781 1.8 briggs static void
782 1.1 briggs initialise_tda(sc)
783 1.1 briggs struct sn_softc *sc;
784 1.1 briggs {
785 1.1 briggs struct mtd *mtd;
786 1.12 scottr int i;
787 1.1 briggs
788 1.1 briggs for (i = 0; i < NTDA; i++) {
789 1.1 briggs mtd = &sc->mtda[i];
790 1.15 scottr mtd->mtd_mbuf = 0;
791 1.1 briggs }
792 1.1 briggs
793 1.1 briggs sc->mtd_hw = 0;
794 1.12 scottr sc->mtd_prev = NTDA - 1;
795 1.1 briggs sc->mtd_free = 0;
796 1.1 briggs sc->mtd_tlinko = TXP_FRAGOFF + 1*TXP_FRAGSIZE + TXP_FPTRLO;
797 1.15 scottr sc->mtd_pint = NTDA/2;
798 1.1 briggs
799 1.8 briggs NIC_PUT(sc, SNR_UTDA, UPPER(sc->mtda[0].mtd_vtxp));
800 1.8 briggs NIC_PUT(sc, SNR_CTDA, LOWER(sc->mtda[0].mtd_vtxp));
801 1.1 briggs }
802 1.1 briggs
803 1.8 briggs static void
804 1.1 briggs initialise_rda(sc)
805 1.1 briggs struct sn_softc *sc;
806 1.1 briggs {
807 1.15 scottr int bitmode = sc->bitmode;
808 1.15 scottr int i;
809 1.15 scottr caddr_t p_rda = 0;
810 1.15 scottr u_int32_t v_rda = 0;
811 1.1 briggs
812 1.1 briggs /* link the RDA's together into a circular list */
813 1.15 scottr for (i = 0; i < (sc->sc_nrda - 1); i++) {
814 1.15 scottr p_rda = sc->p_rda + (i * RXPKT_SIZE(sc));
815 1.15 scottr v_rda = sc->v_rda + ((i+1) * RXPKT_SIZE(sc));
816 1.15 scottr SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(v_rda));
817 1.15 scottr SWO(bitmode, p_rda, RXPKT_INUSE, 1);
818 1.15 scottr }
819 1.15 scottr p_rda = sc->p_rda + ((sc->sc_nrda - 1) * RXPKT_SIZE(sc));
820 1.15 scottr SWO(bitmode, p_rda, RXPKT_RLINK, LOWER(sc->v_rda) | EOL);
821 1.15 scottr SWO(bitmode, p_rda, RXPKT_INUSE, 1);
822 1.1 briggs
823 1.1 briggs /* mark end of receive descriptor list */
824 1.15 scottr sc->sc_rdamark = sc->sc_nrda - 1;
825 1.1 briggs
826 1.1 briggs sc->sc_rxmark = 0;
827 1.1 briggs
828 1.15 scottr NIC_PUT(sc, SNR_URDA, UPPER(sc->v_rda));
829 1.15 scottr NIC_PUT(sc, SNR_CRDA, LOWER(sc->v_rda));
830 1.1 briggs wbflush();
831 1.1 briggs }
832 1.1 briggs
833 1.8 briggs static void
834 1.1 briggs initialise_rra(sc)
835 1.1 briggs struct sn_softc *sc;
836 1.1 briggs {
837 1.12 scottr int i;
838 1.12 scottr u_int v;
839 1.12 scottr int bitmode = sc->bitmode;
840 1.1 briggs
841 1.9 briggs if (bitmode)
842 1.8 briggs NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 2);
843 1.1 briggs else
844 1.8 briggs NIC_PUT(sc, SNR_EOBC, RBASIZE(sc) / 2 - 1);
845 1.9 briggs
846 1.8 briggs NIC_PUT(sc, SNR_URRA, UPPER(sc->v_rra[0]));
847 1.8 briggs NIC_PUT(sc, SNR_RSA, LOWER(sc->v_rra[0]));
848 1.1 briggs /* rea must point just past the end of the rra space */
849 1.8 briggs NIC_PUT(sc, SNR_REA, LOWER(sc->v_rea));
850 1.8 briggs NIC_PUT(sc, SNR_RRP, LOWER(sc->v_rra[0]));
851 1.11 briggs NIC_PUT(sc, SNR_RSC, 0);
852 1.1 briggs
853 1.1 briggs /* fill up SOME of the rra with buffers */
854 1.1 briggs for (i = 0; i < NRBA; i++) {
855 1.15 scottr v = SONIC_GETDMA(sc->rbuf[i]);
856 1.1 briggs SWO(bitmode, sc->p_rra[i], RXRSRC_PTRHI, UPPER(v));
857 1.1 briggs SWO(bitmode, sc->p_rra[i], RXRSRC_PTRLO, LOWER(v));
858 1.9 briggs SWO(bitmode, sc->p_rra[i], RXRSRC_WCHI, UPPER(NBPG/2));
859 1.9 briggs SWO(bitmode, sc->p_rra[i], RXRSRC_WCLO, LOWER(NBPG/2));
860 1.1 briggs }
861 1.1 briggs sc->sc_rramark = NRBA;
862 1.8 briggs NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[sc->sc_rramark]));
863 1.1 briggs wbflush();
864 1.1 briggs }
865 1.1 briggs
866 1.8 briggs void
867 1.1 briggs snintr(arg, slot)
868 1.1 briggs void *arg;
869 1.1 briggs int slot;
870 1.1 briggs {
871 1.12 scottr struct sn_softc *sc = (struct sn_softc *)arg;
872 1.1 briggs int isr;
873 1.1 briggs
874 1.8 briggs while ((isr = (NIC_GET(sc, SNR_ISR) & ISR_ALL)) != 0) {
875 1.1 briggs /* scrub the interrupts that we are going to service */
876 1.8 briggs NIC_PUT(sc, SNR_ISR, isr);
877 1.1 briggs wbflush();
878 1.1 briggs
879 1.9 briggs if (isr & (ISR_BR | ISR_LCD | ISR_TC))
880 1.7 scottr printf("%s: unexpected interrupt status 0x%x\n",
881 1.7 scottr sc->sc_dev.dv_xname, isr);
882 1.1 briggs
883 1.9 briggs if (isr & (ISR_TXDN | ISR_TXER | ISR_PINT))
884 1.1 briggs sonictxint(sc);
885 1.1 briggs
886 1.1 briggs if (isr & ISR_PKTRX)
887 1.1 briggs sonicrxint(sc);
888 1.1 briggs
889 1.1 briggs if (isr & (ISR_HBL | ISR_RDE | ISR_RBE | ISR_RBAE | ISR_RFO)) {
890 1.1 briggs if (isr & ISR_HBL)
891 1.1 briggs /*
892 1.1 briggs * The repeater is not providing a heartbeat.
893 1.1 briggs * In itself this isn't harmful, lots of the
894 1.1 briggs * cheap repeater hubs don't supply a heartbeat.
895 1.1 briggs * So ignore the lack of heartbeat. Its only
896 1.1 briggs * if we can't detect a carrier that we have a
897 1.1 briggs * problem.
898 1.1 briggs */
899 1.7 scottr ;
900 1.1 briggs if (isr & ISR_RDE)
901 1.7 scottr printf("%s: receive descriptors exhausted\n",
902 1.7 scottr sc->sc_dev.dv_xname);
903 1.1 briggs if (isr & ISR_RBE)
904 1.7 scottr printf("%s: receive buffers exhausted\n",
905 1.7 scottr sc->sc_dev.dv_xname);
906 1.1 briggs if (isr & ISR_RBAE)
907 1.7 scottr printf("%s: receive buffer area exhausted\n",
908 1.7 scottr sc->sc_dev.dv_xname);
909 1.1 briggs if (isr & ISR_RFO)
910 1.7 scottr printf("%s: receive FIFO overrun\n",
911 1.7 scottr sc->sc_dev.dv_xname);
912 1.1 briggs }
913 1.1 briggs if (isr & (ISR_CRC | ISR_FAE | ISR_MP)) {
914 1.1 briggs #ifdef notdef
915 1.1 briggs if (isr & ISR_CRC)
916 1.1 briggs sc->sc_crctally++;
917 1.1 briggs if (isr & ISR_FAE)
918 1.1 briggs sc->sc_faetally++;
919 1.1 briggs if (isr & ISR_MP)
920 1.1 briggs sc->sc_mptally++;
921 1.1 briggs #endif
922 1.1 briggs }
923 1.1 briggs snstart(&sc->sc_if);
924 1.1 briggs }
925 1.1 briggs return;
926 1.1 briggs }
927 1.1 briggs
928 1.1 briggs /*
929 1.1 briggs * Transmit interrupt routine
930 1.1 briggs */
931 1.8 briggs static void
932 1.1 briggs sonictxint(sc)
933 1.1 briggs struct sn_softc *sc;
934 1.1 briggs {
935 1.15 scottr struct mtd *mtd;
936 1.15 scottr void *txp;
937 1.15 scottr unsigned short txp_status;
938 1.15 scottr int mtd_hw;
939 1.15 scottr struct ifnet *ifp = &sc->sc_if;
940 1.15 scottr
941 1.15 scottr mtd_hw = sc->mtd_hw;
942 1.1 briggs
943 1.15 scottr if (mtd_hw == sc->mtd_free)
944 1.1 briggs return;
945 1.1 briggs
946 1.15 scottr while (mtd_hw != sc->mtd_free) {
947 1.15 scottr mtd = &sc->mtda[mtd_hw];
948 1.1 briggs
949 1.1 briggs txp = mtd->mtd_txp;
950 1.1 briggs
951 1.15 scottr if (SRO(sc->bitmode, txp, TXP_STATUS) == 0) {
952 1.15 scottr break; /* it hasn't really gone yet */
953 1.15 scottr }
954 1.1 briggs
955 1.15 scottr #ifdef SNDEBUG
956 1.15 scottr {
957 1.1 briggs struct ether_header *eh;
958 1.1 briggs
959 1.1 briggs eh = (struct ether_header *) mtd->mtd_buf;
960 1.12 scottr printf("%s: xmit status=0x%x len=%d type=0x%x from %s",
961 1.12 scottr sc->sc_dev.dv_xname,
962 1.1 briggs SRO(sc->bitmode, txp, TXP_STATUS),
963 1.1 briggs SRO(sc->bitmode, txp, TXP_PKTSIZE),
964 1.1 briggs htons(eh->ether_type),
965 1.1 briggs ether_sprintf(eh->ether_shost));
966 1.1 briggs printf(" (to %s)\n", ether_sprintf(eh->ether_dhost));
967 1.1 briggs }
968 1.15 scottr #endif /* SNDEBUG */
969 1.15 scottr
970 1.15 scottr ifp->if_flags &= ~IFF_OACTIVE;
971 1.15 scottr
972 1.15 scottr if (mtd->mtd_mbuf != 0) {
973 1.15 scottr m_freem(mtd->mtd_mbuf);
974 1.15 scottr mtd->mtd_mbuf = 0;
975 1.15 scottr }
976 1.15 scottr if (++mtd_hw == NTDA) mtd_hw = 0;
977 1.15 scottr
978 1.15 scottr txp_status = SRO(sc->bitmode, txp, TXP_STATUS);
979 1.1 briggs
980 1.15 scottr ifp->if_collisions += (txp_status & TCR_EXC) ? 16 :
981 1.15 scottr ((txp_status & TCR_NC) >> 12);
982 1.1 briggs
983 1.15 scottr if ((txp_status & TCR_PTX) == 0) {
984 1.15 scottr ifp->if_oerrors++;
985 1.7 scottr printf("%s: Tx packet status=0x%x\n",
986 1.15 scottr sc->sc_dev.dv_xname, txp_status);
987 1.15 scottr
988 1.1 briggs /* XXX - DG This looks bogus */
989 1.15 scottr if (mtd_hw != sc->mtd_free) {
990 1.1 briggs printf("resubmitting remaining packets\n");
991 1.15 scottr mtd = &sc->mtda[mtd_hw];
992 1.8 briggs NIC_PUT(sc, SNR_CTDA, LOWER(mtd->mtd_vtxp));
993 1.8 briggs NIC_PUT(sc, SNR_CR, CR_TXP);
994 1.1 briggs wbflush();
995 1.15 scottr break;
996 1.1 briggs }
997 1.1 briggs }
998 1.1 briggs }
999 1.15 scottr
1000 1.15 scottr sc->mtd_hw = mtd_hw;
1001 1.15 scottr return;
1002 1.1 briggs }
1003 1.1 briggs
1004 1.1 briggs /*
1005 1.1 briggs * Receive interrupt routine
1006 1.1 briggs */
1007 1.8 briggs static void
1008 1.1 briggs sonicrxint(sc)
1009 1.1 briggs struct sn_softc *sc;
1010 1.1 briggs {
1011 1.15 scottr caddr_t rda;
1012 1.12 scottr int orra;
1013 1.12 scottr int len;
1014 1.12 scottr int rramark;
1015 1.12 scottr int rdamark;
1016 1.12 scottr int bitmode = sc->bitmode;
1017 1.12 scottr u_int16_t rxpkt_ptr;
1018 1.1 briggs
1019 1.15 scottr rda = sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
1020 1.1 briggs
1021 1.1 briggs while (SRO(bitmode, rda, RXPKT_INUSE) == 0) {
1022 1.12 scottr u_int status = SRO(bitmode, rda, RXPKT_STATUS);
1023 1.1 briggs
1024 1.1 briggs orra = RBASEQ(SRO(bitmode, rda, RXPKT_SEQNO)) & RRAMASK;
1025 1.9 briggs rxpkt_ptr = SRO(bitmode, rda, RXPKT_PTRLO);
1026 1.1 briggs len = SRO(bitmode, rda, RXPKT_BYTEC) -
1027 1.1 briggs sizeof(struct ether_header) - FCSSIZE;
1028 1.1 briggs if (status & RCR_PRX) {
1029 1.12 scottr caddr_t pkt =
1030 1.12 scottr sc->rbuf[orra & RBAMASK] + (rxpkt_ptr & PGOFSET);
1031 1.15 scottr if (sonic_read(sc, pkt, len))
1032 1.1 briggs sc->sc_if.if_ipackets++;
1033 1.15 scottr else
1034 1.15 scottr sc->sc_if.if_ierrors++;
1035 1.1 briggs } else
1036 1.1 briggs sc->sc_if.if_ierrors++;
1037 1.1 briggs
1038 1.1 briggs /*
1039 1.1 briggs * give receive buffer area back to chip.
1040 1.1 briggs *
1041 1.9 briggs * If this was the last packet in the RRA, give the RRA to
1042 1.9 briggs * the chip again.
1043 1.9 briggs * If sonic read didnt copy it out then we would have to
1044 1.1 briggs * wait !!
1045 1.1 briggs * (dont bother add it back in again straight away)
1046 1.1 briggs *
1047 1.1 briggs * Really, we're doing p_rra[rramark] = p_rra[orra] but
1048 1.1 briggs * we have to use the macros because SONIC might be in
1049 1.1 briggs * 16 or 32 bit mode.
1050 1.1 briggs */
1051 1.9 briggs if (status & RCR_LPKT) {
1052 1.9 briggs void *tmp1, *tmp2;
1053 1.1 briggs
1054 1.9 briggs rramark = sc->sc_rramark;
1055 1.9 briggs tmp1 = sc->p_rra[rramark];
1056 1.9 briggs tmp2 = sc->p_rra[orra];
1057 1.9 briggs SWO(bitmode, tmp1, RXRSRC_PTRLO,
1058 1.9 briggs SRO(bitmode, tmp2, RXRSRC_PTRLO));
1059 1.9 briggs SWO(bitmode, tmp1, RXRSRC_PTRHI,
1060 1.9 briggs SRO(bitmode, tmp2, RXRSRC_PTRHI));
1061 1.9 briggs SWO(bitmode, tmp1, RXRSRC_WCLO,
1062 1.9 briggs SRO(bitmode, tmp2, RXRSRC_WCLO));
1063 1.9 briggs SWO(bitmode, tmp1, RXRSRC_WCHI,
1064 1.9 briggs SRO(bitmode, tmp2, RXRSRC_WCHI));
1065 1.9 briggs
1066 1.9 briggs /* zap old rra for fun */
1067 1.9 briggs SWO(bitmode, tmp2, RXRSRC_WCHI, 0);
1068 1.9 briggs SWO(bitmode, tmp2, RXRSRC_WCLO, 0);
1069 1.9 briggs
1070 1.9 briggs sc->sc_rramark = (++rramark) & RRAMASK;
1071 1.9 briggs NIC_PUT(sc, SNR_RWP, LOWER(sc->v_rra[rramark]));
1072 1.9 briggs wbflush();
1073 1.9 briggs }
1074 1.1 briggs
1075 1.1 briggs /*
1076 1.1 briggs * give receive descriptor back to chip simple
1077 1.1 briggs * list is circular
1078 1.1 briggs */
1079 1.1 briggs rdamark = sc->sc_rdamark;
1080 1.1 briggs SWO(bitmode, rda, RXPKT_INUSE, 1);
1081 1.1 briggs SWO(bitmode, rda, RXPKT_RLINK,
1082 1.1 briggs SRO(bitmode, rda, RXPKT_RLINK) | EOL);
1083 1.15 scottr SWO(bitmode, (sc->p_rda + (rdamark * RXPKT_SIZE(sc))), RXPKT_RLINK,
1084 1.15 scottr SRO(bitmode, (sc->p_rda + (rdamark * RXPKT_SIZE(sc))),
1085 1.15 scottr RXPKT_RLINK) & ~EOL);
1086 1.1 briggs sc->sc_rdamark = sc->sc_rxmark;
1087 1.1 briggs
1088 1.15 scottr if (++sc->sc_rxmark >= sc->sc_nrda)
1089 1.1 briggs sc->sc_rxmark = 0;
1090 1.15 scottr rda = sc->p_rda + (sc->sc_rxmark * RXPKT_SIZE(sc));
1091 1.1 briggs }
1092 1.1 briggs }
1093 1.1 briggs
1094 1.1 briggs /*
1095 1.1 briggs * sonic_read -- pull packet off interface and forward to
1096 1.1 briggs * appropriate protocol handler
1097 1.1 briggs */
1098 1.12 scottr static __inline__ int
1099 1.1 briggs sonic_read(sc, pkt, len)
1100 1.1 briggs struct sn_softc *sc;
1101 1.1 briggs caddr_t pkt;
1102 1.1 briggs int len;
1103 1.1 briggs {
1104 1.1 briggs struct ifnet *ifp = &sc->sc_if;
1105 1.1 briggs struct ether_header *et;
1106 1.1 briggs struct mbuf *m;
1107 1.1 briggs
1108 1.1 briggs /*
1109 1.12 scottr * Get pointer to ethernet header (in input buffer).
1110 1.12 scottr */
1111 1.1 briggs et = (struct ether_header *)pkt;
1112 1.1 briggs
1113 1.15 scottr #ifdef SNDEBUG
1114 1.15 scottr {
1115 1.12 scottr printf("%s: rcvd 0x%p len=%d type=0x%x from %s",
1116 1.12 scottr sc->sc_dev.dv_xname, et, len, htons(et->ether_type),
1117 1.1 briggs ether_sprintf(et->ether_shost));
1118 1.1 briggs printf(" (to %s)\n", ether_sprintf(et->ether_dhost));
1119 1.1 briggs }
1120 1.15 scottr #endif /* SNDEBUG */
1121 1.15 scottr
1122 1.1 briggs if (len < ETHERMIN || len > ETHERMTU) {
1123 1.7 scottr printf("%s: invalid packet length %d bytes\n",
1124 1.7 scottr sc->sc_dev.dv_xname, len);
1125 1.1 briggs return (0);
1126 1.1 briggs }
1127 1.1 briggs
1128 1.1 briggs #if NBPFILTER > 0
1129 1.1 briggs /*
1130 1.1 briggs * Check if there's a bpf filter listening on this interface.
1131 1.1 briggs * If so, hand off the raw packet to enet, then discard things
1132 1.1 briggs * not destined for us (but be sure to keep broadcast/multicast).
1133 1.1 briggs */
1134 1.8 briggs if (ifp->if_bpf) {
1135 1.8 briggs bpf_tap(ifp->if_bpf, pkt,
1136 1.1 briggs len + sizeof(struct ether_header));
1137 1.1 briggs if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1138 1.1 briggs (et->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
1139 1.8 briggs bcmp(et->ether_dhost, LLADDR(ifp->if_sadl),
1140 1.12 scottr sizeof(et->ether_dhost)) != 0)
1141 1.1 briggs return (0);
1142 1.1 briggs }
1143 1.1 briggs #endif
1144 1.1 briggs m = sonic_get(sc, et, len);
1145 1.1 briggs if (m == NULL)
1146 1.1 briggs return (0);
1147 1.1 briggs ether_input(ifp, et, m);
1148 1.12 scottr return (1);
1149 1.1 briggs }
1150 1.1 briggs
1151 1.12 scottr #define sonicdataaddr(eh, off, type) ((type)(((caddr_t)((eh) + 1) + (off))))
1152 1.1 briggs
1153 1.1 briggs /*
1154 1.1 briggs * munge the received packet into an mbuf chain
1155 1.1 briggs */
1156 1.12 scottr static __inline__ struct mbuf *
1157 1.1 briggs sonic_get(sc, eh, datalen)
1158 1.1 briggs struct sn_softc *sc;
1159 1.1 briggs struct ether_header *eh;
1160 1.1 briggs int datalen;
1161 1.1 briggs {
1162 1.15 scottr struct mbuf *m, *top, **mp;
1163 1.1 briggs int len;
1164 1.15 scottr caddr_t pkt = sonicdataaddr(eh, 0, caddr_t);
1165 1.1 briggs
1166 1.1 briggs MGETHDR(m, M_DONTWAIT, MT_DATA);
1167 1.1 briggs if (m == 0)
1168 1.1 briggs return (0);
1169 1.1 briggs m->m_pkthdr.rcvif = &sc->sc_if;
1170 1.1 briggs m->m_pkthdr.len = datalen;
1171 1.15 scottr len = MHLEN;
1172 1.15 scottr top = 0;
1173 1.15 scottr mp = ⊤
1174 1.1 briggs
1175 1.1 briggs while (datalen > 0) {
1176 1.1 briggs if (top) {
1177 1.1 briggs MGET(m, M_DONTWAIT, MT_DATA);
1178 1.1 briggs if (m == 0) {
1179 1.1 briggs m_freem(top);
1180 1.1 briggs return (0);
1181 1.1 briggs }
1182 1.15 scottr len = MLEN;
1183 1.1 briggs }
1184 1.15 scottr if (datalen >= MINCLSIZE) {
1185 1.1 briggs MCLGET(m, M_DONTWAIT);
1186 1.15 scottr if ((m->m_flags & M_EXT) == 0) {
1187 1.15 scottr if (top) m_freem(top);
1188 1.15 scottr return (0);
1189 1.15 scottr }
1190 1.16 briggs len = MCLBYTES;
1191 1.1 briggs }
1192 1.15 scottr m->m_len = len = min(datalen, len);
1193 1.15 scottr
1194 1.15 scottr bcopy(pkt, mtod(m, caddr_t), (unsigned) len);
1195 1.15 scottr pkt += len;
1196 1.15 scottr datalen -= len;
1197 1.1 briggs *mp = m;
1198 1.1 briggs mp = &m->m_next;
1199 1.1 briggs }
1200 1.15 scottr
1201 1.1 briggs return (top);
1202 1.10 briggs }
1203 1.10 briggs
1204 1.10 briggs static u_char bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
1205 1.10 briggs #define bbr(v) ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
1206 1.10 briggs
1207 1.10 briggs void
1208 1.10 briggs sn_get_enaddr(t, h, o, dst)
1209 1.12 scottr bus_space_tag_t t;
1210 1.10 briggs bus_space_handle_t h;
1211 1.12 scottr vm_offset_t o;
1212 1.12 scottr u_char *dst;
1213 1.10 briggs {
1214 1.12 scottr int i, do_bbr;
1215 1.12 scottr u_char b;
1216 1.10 briggs
1217 1.10 briggs /*
1218 1.10 briggs * For reasons known only to Apple, MAC addresses in the ethernet
1219 1.10 briggs * PROM are stored in Token Ring (IEEE 802.5) format, that is
1220 1.10 briggs * with all of the bits in each byte reversed (canonical bit format).
1221 1.10 briggs * When the address is read out it must be reversed to ethernet format
1222 1.10 briggs * before use.
1223 1.10 briggs *
1224 1.10 briggs * Apple has been assigned OUI's 08:00:07 and 00:a0:40. All onboard
1225 1.10 briggs * ethernet addresses on 68K machines should be in one of these
1226 1.10 briggs * two ranges.
1227 1.10 briggs *
1228 1.10 briggs * Here is where it gets complicated.
1229 1.10 briggs *
1230 1.10 briggs * The PMac 7200, 7500, 8500, and 9500 accidentally had the PROM
1231 1.10 briggs * written in standard ethernet format. The MacOS accounted for this
1232 1.10 briggs * in these systems, and did not reverse the bytes. Some other
1233 1.10 briggs * networking utilities were not so forgiving, and got confused.
1234 1.10 briggs * "Some" of Apple's Nubus ethernet cards also had their bits
1235 1.10 briggs * burned in ethernet format.
1236 1.10 briggs *
1237 1.10 briggs * Apple petitioned the IEEE and was granted the 00:05:02 (bit reversal
1238 1.10 briggs * of 00:a0:40) as well. As of OpenTransport 1.1.1, Apple removed
1239 1.10 briggs * their workaround and now reverses the bits regardless of
1240 1.10 briggs * what kind of machine it is. So PMac systems and the affected
1241 1.10 briggs * Nubus cards now use 00:05:02, instead of the 00:a0:40 for which they
1242 1.10 briggs * were intended.
1243 1.10 briggs *
1244 1.10 briggs * See Apple Techinfo article TECHINFO-0020552, "OpenTransport 1.1.1
1245 1.10 briggs * and MacOS System 7.5.3 FAQ (10/96)" for more details.
1246 1.10 briggs */
1247 1.10 briggs do_bbr = 0;
1248 1.10 briggs b = bus_space_read_1(t, h, o);
1249 1.10 briggs if (b == 0x10)
1250 1.10 briggs do_bbr = 1;
1251 1.10 briggs dst[0] = (do_bbr) ? bbr(b) : b;
1252 1.10 briggs
1253 1.10 briggs for (i = 1 ; i < ETHER_ADDR_LEN ; i++) {
1254 1.10 briggs b = bus_space_read_1(t, h, o+i);
1255 1.10 briggs dst[i] = (do_bbr) ? bbr(b) : b;
1256 1.10 briggs }
1257 1.1 briggs }
1258