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