if_es.c revision 1.4 1 1.4 chopps /* $NetBSD: if_es.c,v 1.4 1995/04/14 17:29:50 chopps Exp $ */
2 1.1 chopps
3 1.1 chopps /*
4 1.1 chopps * Copyright (c) 1995 Michael L. Hitch
5 1.1 chopps * All rights reserved.
6 1.1 chopps *
7 1.1 chopps * Redistribution and use in source and binary forms, with or without
8 1.1 chopps * modification, are permitted provided that the following conditions
9 1.1 chopps * are met:
10 1.1 chopps * 1. Redistributions of source code must retain the above copyright
11 1.1 chopps * notice, this list of conditions and the following disclaimer.
12 1.1 chopps * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 chopps * notice, this list of conditions and the following disclaimer in the
14 1.1 chopps * documentation and/or other materials provided with the distribution.
15 1.1 chopps * 3. All advertising materials mentioning features or use of this software
16 1.1 chopps * must display the following acknowledgement:
17 1.1 chopps * This product includes software developed by Michael L. Hitch.
18 1.1 chopps * 4. The name of the author may not be used to endorse or promote products
19 1.1 chopps * derived from this software without specific prior written permission
20 1.1 chopps *
21 1.1 chopps * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 chopps * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 chopps * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 chopps * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 chopps * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 chopps * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 chopps * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 chopps * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 chopps * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 chopps * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 chopps */
32 1.1 chopps
33 1.1 chopps /*
34 1.1 chopps * SMC 91C90 Single-Chip Ethernet Controller
35 1.1 chopps */
36 1.1 chopps
37 1.1 chopps #include <sys/param.h>
38 1.1 chopps #include <sys/systm.h>
39 1.1 chopps #include <sys/mbuf.h>
40 1.1 chopps #include <sys/buf.h>
41 1.1 chopps #include <sys/protosw.h>
42 1.1 chopps #include <sys/socket.h>
43 1.1 chopps #include <sys/syslog.h>
44 1.1 chopps #include <sys/ioctl.h>
45 1.1 chopps #include <sys/errno.h>
46 1.1 chopps #include <sys/device.h>
47 1.1 chopps
48 1.1 chopps #include <net/if.h>
49 1.1 chopps #include <net/netisr.h>
50 1.1 chopps #include <net/route.h>
51 1.1 chopps
52 1.1 chopps #ifdef INET
53 1.1 chopps #include <netinet/in.h>
54 1.1 chopps #include <netinet/in_systm.h>
55 1.1 chopps #include <netinet/in_var.h>
56 1.1 chopps #include <netinet/ip.h>
57 1.1 chopps #include <netinet/if_ether.h>
58 1.1 chopps #endif
59 1.1 chopps
60 1.1 chopps #ifdef NS
61 1.1 chopps #include <netns/ns.h>
62 1.1 chopps #include <netns/ns_if.h>
63 1.1 chopps #endif
64 1.1 chopps
65 1.1 chopps #include <machine/cpu.h>
66 1.1 chopps #include <machine/mtpr.h>
67 1.1 chopps #include <amiga/amiga/device.h>
68 1.1 chopps #include <amiga/amiga/isr.h>
69 1.1 chopps #include <amiga/dev/zbusvar.h>
70 1.1 chopps #include <amiga/dev/if_esreg.h>
71 1.1 chopps
72 1.1 chopps #define SWAP(x) (((x & 0xff) << 8) | ((x >> 8) & 0xff))
73 1.1 chopps
74 1.1 chopps #define ESDEBUG
75 1.1 chopps #define USEPKTBUF
76 1.1 chopps
77 1.1 chopps /*
78 1.1 chopps * Ethernet software status per interface.
79 1.1 chopps *
80 1.1 chopps * Each interface is referenced by a network interface structure,
81 1.1 chopps * es_if, which the routing code uses to locate the interface.
82 1.1 chopps * This structure contains the output queue for the interface, its address, ...
83 1.1 chopps */
84 1.1 chopps struct es_softc {
85 1.1 chopps struct device sc_dev;
86 1.1 chopps struct isr sc_isr;
87 1.3 mycroft struct arpcom sc_arpcom; /* common Ethernet structures */
88 1.1 chopps void *sc_base; /* base address of board */
89 1.1 chopps short sc_iflags;
90 1.1 chopps unsigned short sc_intctl;
91 1.1 chopps #ifdef ESDEBUG
92 1.1 chopps int sc_debug;
93 1.1 chopps #endif
94 1.1 chopps };
95 1.1 chopps
96 1.1 chopps #if NBPFILTER > 0
97 1.1 chopps #include <net/bpf.h>
98 1.1 chopps #include <net/bpfdesc.h>
99 1.1 chopps #endif
100 1.1 chopps
101 1.1 chopps #ifdef ESDEBUG
102 1.1 chopps /* console error messages */
103 1.1 chopps int esdebug = 0;
104 1.1 chopps #endif
105 1.1 chopps
106 1.1 chopps int esintr __P((struct es_softc *));
107 1.3 mycroft void esstart __P((struct ifnet *));
108 1.3 mycroft void eswatchdog __P((int));
109 1.1 chopps int esioctl __P((struct ifnet *, u_long, caddr_t));
110 1.1 chopps void esrint __P((struct es_softc *));
111 1.1 chopps void estint __P((struct es_softc *));
112 1.1 chopps void esinit __P((struct es_softc *));
113 1.1 chopps void esreset __P((struct es_softc *));
114 1.1 chopps
115 1.3 mycroft int esmatch __P((struct device *, void *, void *));
116 1.1 chopps void esattach __P((struct device *, struct device *, void *));
117 1.1 chopps
118 1.1 chopps struct cfdriver escd = {
119 1.3 mycroft NULL, "es", esmatch, esattach, DV_IFNET, sizeof(struct es_softc)
120 1.3 mycroft };
121 1.1 chopps
122 1.1 chopps int
123 1.3 mycroft esmatch(parent, match, aux)
124 1.3 mycroft struct device *parent;
125 1.3 mycroft void *match, *aux;
126 1.1 chopps {
127 1.3 mycroft struct zbus_args *zap = aux;
128 1.1 chopps
129 1.1 chopps /* Ameristar A4066 ethernet card */
130 1.3 mycroft if (zap->manid == 1053 && zap->prodid == 10)
131 1.1 chopps return(1);
132 1.1 chopps
133 1.1 chopps return (0);
134 1.1 chopps }
135 1.1 chopps
136 1.1 chopps /*
137 1.1 chopps * Interface exists: make available by filling in network interface
138 1.1 chopps * record. System will initialize the interface when it is ready
139 1.1 chopps * to accept packets.
140 1.1 chopps */
141 1.1 chopps void
142 1.3 mycroft esattach(parent, self, aux)
143 1.3 mycroft struct device *parent, *self;
144 1.3 mycroft void *aux;
145 1.1 chopps {
146 1.3 mycroft struct es_softc *sc = (void *)self;
147 1.3 mycroft struct zbus_args *zap = aux;
148 1.3 mycroft struct ifnet *ifp = &sc->sc_arpcom.ac_if;
149 1.1 chopps unsigned long ser;
150 1.1 chopps
151 1.1 chopps sc->sc_base = zap->va;
152 1.1 chopps
153 1.1 chopps /*
154 1.1 chopps * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
155 1.1 chopps * (Currently only Ameristar.)
156 1.1 chopps */
157 1.3 mycroft sc->sc_arpcom.ac_enaddr[0] = 0x00;
158 1.3 mycroft sc->sc_arpcom.ac_enaddr[1] = 0x00;
159 1.3 mycroft sc->sc_arpcom.ac_enaddr[2] = 0x9f;
160 1.1 chopps
161 1.1 chopps /*
162 1.1 chopps * Serial number for board contains last 3 bytes.
163 1.1 chopps */
164 1.1 chopps ser = (unsigned long) zap->serno;
165 1.1 chopps
166 1.3 mycroft sc->sc_arpcom.ac_enaddr[3] = (ser >> 16) & 0xff;
167 1.3 mycroft sc->sc_arpcom.ac_enaddr[4] = (ser >> 8) & 0xff;
168 1.3 mycroft sc->sc_arpcom.ac_enaddr[5] = (ser ) & 0xff;
169 1.1 chopps
170 1.3 mycroft /* Initialize ifnet structure. */
171 1.3 mycroft ifp->if_unit = sc->sc_dev.dv_unit;
172 1.1 chopps ifp->if_name = escd.cd_name;
173 1.1 chopps ifp->if_ioctl = esioctl;
174 1.1 chopps ifp->if_start = esstart;
175 1.1 chopps ifp->if_watchdog = eswatchdog;
176 1.1 chopps /* XXX IFF_MULTICAST */
177 1.1 chopps ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
178 1.1 chopps
179 1.3 mycroft /* Attach the interface. */
180 1.1 chopps if_attach(ifp);
181 1.1 chopps ether_ifattach(ifp);
182 1.3 mycroft
183 1.3 mycroft /* Print additional info when attached. */
184 1.3 mycroft printf(": address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
185 1.3 mycroft
186 1.1 chopps #if NBPFILTER > 0
187 1.3 mycroft bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
188 1.1 chopps #endif
189 1.1 chopps
190 1.1 chopps sc->sc_isr.isr_intr = esintr;
191 1.1 chopps sc->sc_isr.isr_arg = sc;
192 1.1 chopps sc->sc_isr.isr_ipl = 2;
193 1.3 mycroft add_isr(&sc->sc_isr);
194 1.1 chopps }
195 1.1 chopps
196 1.1 chopps void
197 1.4 chopps esstop(sc)
198 1.4 chopps struct es_softc* sc;
199 1.4 chopps {
200 1.4 chopps }
201 1.4 chopps
202 1.4 chopps void
203 1.1 chopps esinit(sc)
204 1.1 chopps struct es_softc *sc;
205 1.1 chopps {
206 1.3 mycroft struct ifnet *ifp = &sc->sc_arpcom.ac_if;
207 1.1 chopps union smcregs *smc = sc->sc_base;
208 1.1 chopps int s;
209 1.1 chopps
210 1.3 mycroft if (ifp->if_addrlist == 0)
211 1.1 chopps return;
212 1.1 chopps
213 1.1 chopps s = splimp();
214 1.1 chopps
215 1.3 mycroft smc->b0.bsr = BSR_BANK0; /* Select bank 0 */
216 1.1 chopps smc->b0.rcr = RCR_EPH_RST;
217 1.1 chopps smc->b0.rcr = 0;
218 1.3 mycroft smc->b3.bsr = BSR_BANK3; /* Select bank 3 */
219 1.1 chopps smc->b3.mt[0] = 0; /* clear Multicast table */
220 1.1 chopps smc->b3.mt[1] = 0;
221 1.1 chopps smc->b3.mt[2] = 0;
222 1.1 chopps smc->b3.mt[3] = 0;
223 1.1 chopps /* XXX set Multicast table from Multicast list */
224 1.3 mycroft smc->b1.bsr = BSR_BANK1; /* Select bank 1 */
225 1.1 chopps smc->b1.cr = CR_RAM32K | CR_NO_WAIT_ST | CR_SET_SQLCH;
226 1.1 chopps smc->b1.ctr = CTR_AUTO_RLSE;
227 1.3 mycroft smc->b1.iar[0] = *((unsigned short *) &sc->sc_arpcom.ac_enaddr[0]);
228 1.3 mycroft smc->b1.iar[1] = *((unsigned short *) &sc->sc_arpcom.ac_enaddr[2]);
229 1.3 mycroft smc->b1.iar[2] = *((unsigned short *) &sc->sc_arpcom.ac_enaddr[4]);
230 1.3 mycroft smc->b2.bsr = BSR_BANK2; /* Select bank 2 */
231 1.1 chopps smc->b2.mmucr = MMUCR_RESET;
232 1.3 mycroft smc->b0.bsr = BSR_BANK0; /* Select bank 0 */
233 1.3 mycroft smc->b0.mcr = SWAP(0x0020); /* reserve 8K for transmit buffers */
234 1.1 chopps smc->b0.tcr = TCR_PAD_EN | TCR_TXENA + TCR_MON_CSN;
235 1.1 chopps smc->b0.rcr = RCR_FILT_CAR | RCR_STRIP_CRC | RCR_RXEN;
236 1.1 chopps /* XXX add multicast/promiscuous flags */
237 1.3 mycroft smc->b2.bsr = BSR_BANK2; /* Select bank 2 */
238 1.1 chopps smc->b2.msk = sc->sc_intctl = MSK_RX_OVRN | MSK_RX;
239 1.1 chopps
240 1.1 chopps /* Interface is now 'running', with no output active. */
241 1.1 chopps ifp->if_flags |= IFF_RUNNING;
242 1.1 chopps ifp->if_flags &= ~IFF_OACTIVE;
243 1.1 chopps
244 1.1 chopps /* Attempt to start output, if any. */
245 1.1 chopps esstart(ifp);
246 1.1 chopps
247 1.1 chopps splx(s);
248 1.1 chopps }
249 1.1 chopps
250 1.1 chopps int
251 1.1 chopps esintr(sc)
252 1.1 chopps struct es_softc *sc;
253 1.1 chopps {
254 1.1 chopps int i;
255 1.2 chopps u_short intsts, intact;
256 1.1 chopps int done = 0;
257 1.1 chopps union smcregs *smc;
258 1.1 chopps
259 1.1 chopps smc = sc->sc_base;
260 1.2 chopps intsts = smc->b2.ist;
261 1.2 chopps intact = smc->b2.msk & intsts;
262 1.2 chopps if ((intact) == 0)
263 1.1 chopps return (0);
264 1.1 chopps #ifdef ESDEBUG
265 1.1 chopps if (esdebug)
266 1.1 chopps printf ("%s: esintr ist %02x msk %02x",
267 1.2 chopps sc->sc_dev.dv_xname, intsts, smc->b2.msk);
268 1.1 chopps #endif
269 1.1 chopps smc->b2.msk = 0;
270 1.1 chopps #ifdef ESDEBUG
271 1.1 chopps if (esdebug)
272 1.1 chopps printf ("=>%02x%02x pnr %02x arr %02x fifo %04x\n",
273 1.1 chopps smc->b2.ist, smc->b2.ist, smc->b2.pnr, smc->b2.arr,
274 1.1 chopps smc->b2.fifo);
275 1.1 chopps #endif
276 1.2 chopps if (intact & IST_ALLOC) {
277 1.1 chopps sc->sc_intctl &= ~MSK_ALLOC;
278 1.1 chopps #ifdef ESDEBUG
279 1.1 chopps if (esdebug || 1)
280 1.1 chopps printf ("%s: ist %02x\n", sc->sc_dev.dv_xname,
281 1.2 chopps intsts);
282 1.1 chopps #endif
283 1.1 chopps if ((smc->b2.arr & ARR_FAILED) == 0) {
284 1.1 chopps #ifdef ESDEBUG
285 1.1 chopps if (esdebug || 1)
286 1.1 chopps printf ("%s: arr %02x\n", sc->sc_dev.dv_xname,
287 1.1 chopps smc->b2.arr);
288 1.1 chopps #endif
289 1.1 chopps smc->b2.pnr = smc->b2.arr;
290 1.1 chopps smc->b2.mmucr = MMUCR_RLSPKT;
291 1.1 chopps while (smc->b2.mmucr & MMUCR_BUSY)
292 1.1 chopps ;
293 1.3 mycroft sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
294 1.1 chopps }
295 1.1 chopps }
296 1.1 chopps while ((smc->b2.fifo & FIFO_REMPTY) == 0) {
297 1.1 chopps esrint(sc);
298 1.1 chopps }
299 1.2 chopps if (intact & IST_RX_OVRN) {
300 1.1 chopps printf ("%s: Overrun ist %02x", sc->sc_dev.dv_xname,
301 1.2 chopps intsts);
302 1.1 chopps smc->b2.ist = ACK_RX_OVRN;
303 1.1 chopps printf ("->%02x\n", smc->b2.ist);
304 1.3 mycroft sc->sc_arpcom.ac_if.if_ierrors++;
305 1.1 chopps }
306 1.2 chopps if (intact & IST_TX) {
307 1.1 chopps #ifdef ESDEBUG
308 1.1 chopps if (esdebug)
309 1.1 chopps printf ("%s: TX INT ist %02x",
310 1.2 chopps sc->sc_dev.dv_xname, intsts);
311 1.1 chopps #endif
312 1.1 chopps smc->b2.ist = ACK_TX;
313 1.1 chopps #ifdef ESDEBUG
314 1.1 chopps if (esdebug)
315 1.1 chopps printf ("->%02x\n", smc->b2.ist);
316 1.1 chopps #endif
317 1.3 mycroft smc->b0.bsr = BSR_BANK0;
318 1.2 chopps if ((smc->b0.tcr & TCR_TXENA) == 0) {
319 1.3 mycroft printf("%s: IST %02x masked %02x EPH status %04x tcr %04x\n",
320 1.2 chopps sc->sc_dev.dv_xname, intsts, intact, smc->b0.ephsr,
321 1.2 chopps smc->b0.tcr);
322 1.2 chopps if ((smc->b0.ephsr & EPHSR_TX_SUC) == 0) {
323 1.2 chopps smc->b0.tcr |= TCR_TXENA;
324 1.3 mycroft sc->sc_arpcom.ac_if.if_oerrors++;
325 1.2 chopps }
326 1.2 chopps }
327 1.2 chopps /*
328 1.2 chopps * else - got a TX interrupt, but TCR_TXENA is still set??
329 1.2 chopps */
330 1.2 chopps #if 0
331 1.2 chopps else if ((intact & IST_TX_EMPTY) == 0) {
332 1.3 mycroft printf("%s: unexpected TX interrupt %02x %02x\n",
333 1.2 chopps sc->sc_dev.dv_xname, intsts, intact);
334 1.2 chopps smc->b2.bsr = 0x0200;
335 1.2 chopps printf (" %02x\n", smc->b2.ist);
336 1.1 chopps }
337 1.2 chopps #endif
338 1.3 mycroft smc->b2.bsr = BSR_BANK2;
339 1.1 chopps }
340 1.2 chopps if (intact & IST_TX_EMPTY) {
341 1.1 chopps u_short ecr;
342 1.1 chopps #ifdef ESDEBUG
343 1.1 chopps if (esdebug)
344 1.1 chopps printf ("%s: TX EMPTY %02x",
345 1.2 chopps sc->sc_dev.dv_xname, intsts);
346 1.1 chopps #endif
347 1.1 chopps smc->b2.ist = ACK_TX_EMPTY;
348 1.1 chopps #ifdef ESDEBUG
349 1.1 chopps if (esdebug)
350 1.1 chopps printf ("->%02x intcl %x pnr %02x arr %02x\n",
351 1.1 chopps smc->b2.ist, sc->sc_intctl, smc->b2.pnr,
352 1.1 chopps smc->b2.arr);
353 1.1 chopps #endif
354 1.1 chopps sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
355 1.3 mycroft smc->b0.bsr = BSR_BANK0;
356 1.1 chopps ecr = smc->b0.ecr; /* Get error counters */
357 1.1 chopps if (ecr & 0xff00)
358 1.3 mycroft sc->sc_arpcom.ac_if.if_collisions += ((ecr >> 8) & 15) +
359 1.1 chopps ((ecr >> 11) & 0x1e);
360 1.3 mycroft smc->b2.bsr = BSR_BANK2;
361 1.1 chopps }
362 1.1 chopps /* output packets */
363 1.1 chopps estint(sc);
364 1.1 chopps smc->b2.msk = sc->sc_intctl;
365 1.1 chopps return (1);
366 1.1 chopps }
367 1.1 chopps
368 1.1 chopps void
369 1.3 mycroft esrint(sc)
370 1.1 chopps struct es_softc *sc;
371 1.1 chopps {
372 1.1 chopps union smcregs *smc = sc->sc_base;
373 1.1 chopps int i;
374 1.1 chopps u_short len;
375 1.1 chopps short cnt;
376 1.1 chopps u_short pktctlw, pktlen, *buf;
377 1.1 chopps u_long *lbuf;
378 1.1 chopps volatile u_short *data;
379 1.1 chopps volatile u_long *ldata;
380 1.1 chopps struct ifnet *ifp;
381 1.1 chopps struct mbuf *top, **mp, *m;
382 1.1 chopps struct ether_header *eh;
383 1.1 chopps #ifdef USEPKTBUF
384 1.1 chopps u_char *b, pktbuf[1530];
385 1.1 chopps #endif
386 1.1 chopps
387 1.1 chopps #ifdef ESDEBUG
388 1.1 chopps if (esdebug)
389 1.1 chopps printf ("%s: esrint fifo %04x", sc->sc_dev.dv_xname,
390 1.1 chopps smc->b2.fifo);
391 1.1 chopps #endif
392 1.1 chopps data = (u_short *)&smc->b2.data;
393 1.3 mycroft smc->b2.ptr = PTR_RCV | PTR_AUTOINCR | PTR_READ | SWAP(0x0002);
394 1.1 chopps (void) smc->b2.mmucr;
395 1.1 chopps #ifdef ESDEBUG
396 1.1 chopps if (esdebug)
397 1.1 chopps printf ("->%04x", smc->b2.fifo);
398 1.1 chopps #endif
399 1.1 chopps len = *data;
400 1.3 mycroft len = SWAP(len); /* Careful of macro side-effects */
401 1.1 chopps #ifdef ESDEBUG
402 1.1 chopps if (esdebug)
403 1.1 chopps printf (" length %d", len);
404 1.1 chopps #endif
405 1.3 mycroft smc->b2.ptr = PTR_RCV | PTR_AUTOINCR + PTR_READ | SWAP(0x0000);
406 1.1 chopps (void) smc->b2.mmucr;
407 1.1 chopps pktctlw = *data;
408 1.1 chopps pktlen = *data;
409 1.3 mycroft pktctlw = SWAP(pktctlw);
410 1.3 mycroft pktlen = SWAP(pktlen) - 6;
411 1.1 chopps if (pktctlw & RFSW_ODDFRM)
412 1.1 chopps pktlen++;
413 1.1 chopps /* XXX copy directly from controller to mbuf */
414 1.1 chopps #ifdef USEPKTBUF
415 1.1 chopps #if 1
416 1.1 chopps lbuf = (u_long *) pktbuf;
417 1.1 chopps ldata = (u_long *)data;
418 1.1 chopps cnt = (len - 4) / 4;
419 1.1 chopps while (cnt--)
420 1.1 chopps *lbuf++ = *ldata;
421 1.1 chopps if ((len - 4) & 2) {
422 1.1 chopps buf = (u_short *) lbuf;
423 1.1 chopps *buf = *data;
424 1.1 chopps }
425 1.1 chopps #else
426 1.1 chopps buf = (u_short *)pktbuf;
427 1.1 chopps cnt = (len - 4) / 2;
428 1.1 chopps while (cnt--)
429 1.1 chopps *buf++ = *data;
430 1.1 chopps #endif
431 1.1 chopps smc->b2.mmucr = MMUCR_REMRLS_RX;
432 1.1 chopps while (smc->b2.mmucr & MMUCR_BUSY)
433 1.1 chopps ;
434 1.1 chopps #ifdef ESDEBUG
435 1.1 chopps if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
436 1.1 chopps printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
437 1.1 chopps /* count input error? */
438 1.1 chopps }
439 1.1 chopps if (esdebug) {
440 1.1 chopps printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
441 1.1 chopps smc->b2.fifo);
442 1.1 chopps for (i = 0; i < pktlen; ++i)
443 1.1 chopps printf ("%02x%s", pktbuf[i], ((i & 31) == 31) ? "\n" :
444 1.1 chopps "");
445 1.1 chopps if (i & 31)
446 1.1 chopps printf ("\n");
447 1.1 chopps }
448 1.1 chopps #endif
449 1.1 chopps #else /* USEPKTBUF */
450 1.1 chopps #ifdef ESDEBUG
451 1.1 chopps if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
452 1.1 chopps printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
453 1.1 chopps /* count input error? */
454 1.1 chopps }
455 1.1 chopps if (esdebug) {
456 1.1 chopps printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
457 1.1 chopps smc->b2.fifo);
458 1.1 chopps }
459 1.1 chopps #endif
460 1.1 chopps #endif /* USEPKTBUF */
461 1.3 mycroft ifp = &sc->sc_arpcom.ac_if;
462 1.1 chopps ifp->if_ipackets++;
463 1.1 chopps MGETHDR(m, M_DONTWAIT, MT_DATA);
464 1.1 chopps if (m == NULL)
465 1.1 chopps return;
466 1.1 chopps m->m_pkthdr.rcvif = ifp;
467 1.1 chopps m->m_pkthdr.len = pktlen;
468 1.1 chopps len = MHLEN;
469 1.1 chopps top = NULL;
470 1.1 chopps mp = ⊤
471 1.1 chopps #ifdef USEPKTBUF
472 1.1 chopps b = pktbuf;
473 1.1 chopps #endif
474 1.1 chopps while (pktlen > 0) {
475 1.1 chopps if (top) {
476 1.1 chopps MGET(m, M_DONTWAIT, MT_DATA);
477 1.1 chopps if (m == 0) {
478 1.1 chopps m_freem(top);
479 1.1 chopps return;
480 1.1 chopps }
481 1.1 chopps len = MLEN;
482 1.1 chopps }
483 1.1 chopps if (pktlen >= MINCLSIZE) {
484 1.1 chopps MCLGET(m, M_DONTWAIT);
485 1.1 chopps if (m->m_flags & M_EXT)
486 1.1 chopps len = MCLBYTES;
487 1.1 chopps }
488 1.1 chopps m->m_len = len = min(pktlen, len);
489 1.1 chopps #ifdef USEPKTBUF
490 1.1 chopps bcopy((caddr_t)b, mtod(m, caddr_t), len);
491 1.1 chopps b += len;
492 1.1 chopps #else /* USEPKTBUF */
493 1.1 chopps buf = mtod(m, u_short *);
494 1.1 chopps cnt = len / 2;
495 1.1 chopps while (cnt--)
496 1.1 chopps *buf++ = *data;
497 1.1 chopps if (len & 1)
498 1.1 chopps *buf = *data; /* XXX should be byte store */
499 1.1 chopps #ifdef ESDEBUG
500 1.1 chopps if (esdebug) {
501 1.1 chopps buf = mtod(m, u_short *);
502 1.1 chopps for (i = 0; i < len; ++i)
503 1.1 chopps printf ("%02x%s", ((u_char *)buf)[i],
504 1.1 chopps ((i & 31) == 31) ? "\n" : "");
505 1.1 chopps if (i & 31)
506 1.1 chopps printf ("\n");
507 1.1 chopps }
508 1.1 chopps #endif
509 1.1 chopps #endif /* USEPKTBUF */
510 1.1 chopps pktlen -= len;
511 1.1 chopps *mp = m;
512 1.1 chopps mp = &m->m_next;
513 1.1 chopps }
514 1.1 chopps #ifndef USEPKTBUF
515 1.1 chopps smc->b2.mmucr = MMUCR_REMRLS_RX;
516 1.1 chopps while (smc->b2.mmucr & MMUCR_BUSY)
517 1.1 chopps ;
518 1.1 chopps #endif
519 1.1 chopps eh = mtod(top, struct ether_header *);
520 1.1 chopps top->m_pkthdr.len -= sizeof (*eh);
521 1.1 chopps top->m_len -= sizeof (*eh);
522 1.1 chopps top->m_data += sizeof (*eh);
523 1.1 chopps
524 1.1 chopps ether_input(ifp, eh, top);
525 1.1 chopps }
526 1.1 chopps
527 1.1 chopps void
528 1.3 mycroft estint(sc)
529 1.1 chopps struct es_softc *sc;
530 1.1 chopps {
531 1.3 mycroft
532 1.3 mycroft esstart(&sc->sc_arpcom.ac_if);
533 1.1 chopps }
534 1.1 chopps
535 1.3 mycroft void
536 1.1 chopps esstart(ifp)
537 1.1 chopps struct ifnet *ifp;
538 1.1 chopps {
539 1.1 chopps struct es_softc *sc = escd.cd_devs[ifp->if_unit];
540 1.1 chopps union smcregs *smc = sc->sc_base;
541 1.1 chopps struct mbuf *m0, *m;
542 1.1 chopps #ifdef USEPKTBUF
543 1.3 mycroft u_short pktbuf[ETHERMTU + 2];
544 1.1 chopps #endif
545 1.1 chopps u_short pktctlw, pktlen;
546 1.1 chopps u_short *buf;
547 1.1 chopps volatile u_short *data;
548 1.1 chopps u_long *lbuf;
549 1.1 chopps volatile u_long *ldata;
550 1.1 chopps short cnt;
551 1.1 chopps int i;
552 1.1 chopps
553 1.3 mycroft if ((sc->sc_arpcom.ac_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
554 1.1 chopps IFF_RUNNING)
555 1.1 chopps return;
556 1.1 chopps
557 1.1 chopps for (;;) {
558 1.1 chopps /*
559 1.1 chopps * Sneak a peek at the next packet to get the length
560 1.1 chopps * and see if the SMC 91C90 can accept it.
561 1.1 chopps */
562 1.3 mycroft m = sc->sc_arpcom.ac_if.if_snd.ifq_head;
563 1.1 chopps if (!m)
564 1.1 chopps break;
565 1.1 chopps #ifdef ESDEBUG
566 1.1 chopps if (esdebug && (m->m_next || m->m_len & 1))
567 1.1 chopps printf("%s: esstart m_next %x m_len %d\n", sc->sc_dev.dv_xname,
568 1.1 chopps m->m_next, m->m_len);
569 1.1 chopps #endif
570 1.1 chopps for (m0 = m, pktlen = 0; m0; m0 = m0->m_next)
571 1.1 chopps pktlen += m0->m_len;
572 1.1 chopps #ifdef ESDEBUG
573 1.1 chopps if (esdebug)
574 1.1 chopps printf("%s: esstart r1 %x len %d", sc->sc_dev.dv_xname, smc->b2.pnr, pktlen);
575 1.1 chopps #endif
576 1.1 chopps pktctlw = 0;
577 1.1 chopps pktlen += 4;
578 1.1 chopps if (pktlen & 1)
579 1.1 chopps ++pktlen; /* control byte after last byte */
580 1.1 chopps else
581 1.1 chopps pktlen += 2; /* control byte after pad byte */
582 1.1 chopps smc->b2.mmucr = MMUCR_ALLOC | (pktlen & 0x0700);
583 1.1 chopps for (i = 0; i <= 5; ++i)
584 1.1 chopps if ((smc->b2.arr & ARR_FAILED) == 0)
585 1.1 chopps break;
586 1.1 chopps if (smc->b2.arr & ARR_FAILED) {
587 1.3 mycroft sc->sc_arpcom.ac_if.if_flags |= IFF_OACTIVE;
588 1.1 chopps #ifdef ESDEBUG
589 1.1 chopps if (esdebug)
590 1.1 chopps printf("-no xmit bufs r1 %x\n", smc->b2.pnr);
591 1.1 chopps #endif
592 1.2 chopps sc->sc_intctl |= MSK_ALLOC;
593 1.1 chopps break;
594 1.1 chopps }
595 1.1 chopps smc->b2.pnr = smc->b2.arr;
596 1.1 chopps
597 1.3 mycroft IF_DEQUEUE(&sc->sc_arpcom.ac_if.if_snd, m);
598 1.1 chopps smc->b2.ptr = PTR_AUTOINCR;
599 1.1 chopps (void) smc->b2.mmucr;
600 1.1 chopps data = (u_short *)&smc->b2.data;
601 1.3 mycroft *data = SWAP(pktctlw);
602 1.3 mycroft *data = SWAP(pktlen);
603 1.1 chopps #ifdef USEPKTBUF
604 1.1 chopps i = 0;
605 1.1 chopps for (m0 = m; m; m = m->m_next) {
606 1.1 chopps bcopy(mtod(m, caddr_t), (char *)pktbuf + i, m->m_len);
607 1.1 chopps i += m->m_len;
608 1.1 chopps }
609 1.1 chopps
610 1.1 chopps if (i & 1) /* Figure out where to put control byte */
611 1.1 chopps pktbuf[i/2] = (pktbuf[i/2] & 0xff00) | CTLB_ODD;
612 1.1 chopps else
613 1.1 chopps pktbuf[i/2] = 0;
614 1.1 chopps #ifdef ESDEBUG
615 1.1 chopps if (esdebug) {
616 1.1 chopps int j;
617 1.1 chopps printf("-sending len %d pktlen %d r1 %04x\n", i, pktlen, smc->b2.pnr);
618 1.1 chopps for (j = 0; j < pktlen - 4; ++j)
619 1.1 chopps printf("%02x%s", ((u_char *)pktbuf)[j], ((j & 31) == 31) ? "\n" : "");
620 1.1 chopps if (j & 31)
621 1.1 chopps printf("\n");
622 1.1 chopps }
623 1.1 chopps #endif
624 1.1 chopps #if 0 /* doesn't quite work? */
625 1.1 chopps lbuf = (u_long *)(pktbuf);
626 1.1 chopps ldata = (u_long *)data;
627 1.1 chopps cnt = pktlen / 4;
628 1.1 chopps while(cnt--)
629 1.1 chopps *ldata = *lbuf++;
630 1.1 chopps if (pktlen & 2) {
631 1.1 chopps buf = (u_short *)lbuf;
632 1.1 chopps *data = *buf;
633 1.1 chopps }
634 1.1 chopps #else
635 1.1 chopps buf = pktbuf;
636 1.1 chopps cnt = pktlen / 2;
637 1.1 chopps while (cnt--)
638 1.1 chopps *data = *buf++;
639 1.1 chopps #endif
640 1.1 chopps #else /* USEPKTBUF */
641 1.1 chopps #ifdef ESDEBUG
642 1.1 chopps if (esdebug)
643 1.1 chopps printf("-sending pktlen %d r1 %04x\n", pktlen, smc->b2.pnr);
644 1.1 chopps #endif
645 1.1 chopps pktctlw = 0;
646 1.1 chopps for (m0 = m; m; m = m->m_next) {
647 1.1 chopps buf = mtod(m, u_short *);
648 1.1 chopps #ifdef ESDEBUG
649 1.1 chopps if (esdebug) {
650 1.1 chopps printf(" m->m_len %d\n", m->m_len);
651 1.1 chopps for (i = 0; i < m->m_len; ++i)
652 1.1 chopps printf("%02x%s", ((u_char *)buf)[i], ((i & 31) == 31) ? "\n" : "");
653 1.1 chopps if (i & 31)
654 1.1 chopps printf("\n");
655 1.1 chopps }
656 1.1 chopps #endif
657 1.1 chopps cnt = m->m_len / 2;
658 1.1 chopps while (cnt--)
659 1.1 chopps *data = *buf++;
660 1.1 chopps if (m->m_len & 1)
661 1.1 chopps pktctlw = (*buf & 0xff00) | CTLB_ODD;
662 1.1 chopps }
663 1.1 chopps *data = pktctlw;
664 1.1 chopps #endif /* USEPKTBUF */
665 1.1 chopps smc->b2.mmucr = MMUCR_ENQ_TX;
666 1.1 chopps #ifdef ESDEBUG
667 1.1 chopps if (esdebug)
668 1.1 chopps printf("%s: esstart packet sent r1 %x\n", sc->sc_dev.dv_xname, smc->b2.pnr);
669 1.1 chopps #endif
670 1.1 chopps #if NBPFILTER > 0
671 1.3 mycroft if (sc->sc_arpcom.ac_if.if_bpf)
672 1.3 mycroft bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m0);
673 1.1 chopps #endif
674 1.1 chopps m_freem(m0);
675 1.3 mycroft sc->sc_arpcom.ac_if.if_opackets++; /* move to interrupt? */
676 1.2 chopps sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
677 1.1 chopps }
678 1.1 chopps smc->b2.msk = sc->sc_intctl;
679 1.1 chopps }
680 1.1 chopps
681 1.1 chopps int
682 1.3 mycroft esioctl(ifp, command, data)
683 1.3 mycroft register struct ifnet *ifp;
684 1.3 mycroft u_long command;
685 1.1 chopps caddr_t data;
686 1.1 chopps {
687 1.1 chopps struct es_softc *sc = escd.cd_devs[ifp->if_unit];
688 1.3 mycroft register struct ifaddr *ifa = (struct ifaddr *)data;
689 1.1 chopps struct ifreq *ifr = (struct ifreq *)data;
690 1.1 chopps int s, error = 0;
691 1.1 chopps
692 1.3 mycroft s = splimp();
693 1.1 chopps
694 1.3 mycroft switch (command) {
695 1.1 chopps
696 1.1 chopps case SIOCSIFADDR:
697 1.1 chopps ifp->if_flags |= IFF_UP;
698 1.1 chopps
699 1.1 chopps switch (ifa->ifa_addr->sa_family) {
700 1.1 chopps #ifdef INET
701 1.1 chopps case AF_INET:
702 1.1 chopps esinit(sc);
703 1.3 mycroft arp_ifinit(&sc->sc_arpcom, ifa);
704 1.1 chopps break;
705 1.1 chopps #endif
706 1.1 chopps #ifdef NS
707 1.1 chopps case AF_NS:
708 1.1 chopps {
709 1.1 chopps register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
710 1.1 chopps
711 1.1 chopps if (ns_nullhost(*ina))
712 1.1 chopps ina->x_host =
713 1.3 mycroft *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
714 1.1 chopps else
715 1.1 chopps bcopy(ina->x_host.c_host,
716 1.3 mycroft sc->sc_arpcom.ac_enaddr,
717 1.3 mycroft sizeof(sc->sc_arpcom.ac_enaddr));
718 1.3 mycroft /* Set new address. */
719 1.1 chopps esinit(sc);
720 1.1 chopps break;
721 1.1 chopps #endif
722 1.1 chopps default:
723 1.1 chopps esinit(sc);
724 1.1 chopps break;
725 1.1 chopps }
726 1.1 chopps break;
727 1.3 mycroft
728 1.1 chopps case SIOCSIFFLAGS:
729 1.1 chopps /*
730 1.1 chopps * If interface is marked down and it is running, then stop it
731 1.1 chopps */
732 1.1 chopps if ((ifp->if_flags & IFF_UP) == 0 &&
733 1.1 chopps (ifp->if_flags & IFF_RUNNING) != 0) {
734 1.1 chopps /*
735 1.1 chopps * If interface is marked down and it is running, then
736 1.1 chopps * stop it.
737 1.1 chopps */
738 1.3 mycroft esstop(sc);
739 1.1 chopps ifp->if_flags &= ~IFF_RUNNING;
740 1.1 chopps } else if ((ifp->if_flags & IFF_UP) != 0 &&
741 1.1 chopps (ifp->if_flags & IFF_RUNNING) == 0) {
742 1.1 chopps /*
743 1.1 chopps * If interface is marked up and it is stopped, then
744 1.1 chopps * start it.
745 1.1 chopps */
746 1.1 chopps esinit(sc);
747 1.1 chopps } else {
748 1.1 chopps /*
749 1.1 chopps * Reset the interface to pick up changes in any other
750 1.1 chopps * flags that affect hardware registers.
751 1.1 chopps */
752 1.3 mycroft esstop(sc);
753 1.1 chopps esinit(sc);
754 1.1 chopps }
755 1.1 chopps #ifdef ESDEBUG
756 1.1 chopps if (ifp->if_flags & IFF_DEBUG)
757 1.1 chopps esdebug = sc->sc_debug = 1;
758 1.1 chopps else
759 1.1 chopps esdebug = sc->sc_debug = 0;
760 1.1 chopps #endif
761 1.1 chopps break;
762 1.1 chopps
763 1.1 chopps default:
764 1.1 chopps error = EINVAL;
765 1.1 chopps }
766 1.1 chopps
767 1.1 chopps splx(s);
768 1.1 chopps return (error);
769 1.1 chopps }
770 1.1 chopps
771 1.1 chopps void
772 1.1 chopps esreset(sc)
773 1.1 chopps struct es_softc *sc;
774 1.1 chopps {
775 1.1 chopps int s;
776 1.1 chopps
777 1.1 chopps s = splimp();
778 1.3 mycroft esstop(sc);
779 1.1 chopps esinit(sc);
780 1.1 chopps splx(s);
781 1.1 chopps }
782 1.1 chopps
783 1.3 mycroft void
784 1.1 chopps eswatchdog(unit)
785 1.1 chopps int unit;
786 1.1 chopps {
787 1.3 mycroft struct es_softc *sc = escd.cd_devs[unit];
788 1.1 chopps
789 1.1 chopps log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
790 1.3 mycroft ++sc->sc_arpcom.ac_if.if_oerrors;
791 1.3 mycroft
792 1.1 chopps esreset(sc);
793 1.1 chopps }
794