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