if_es.c revision 1.2 1 1.2 chopps /* $NetBSD: if_es.c,v 1.2 1995/04/02 20:38:45 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 #define ETHER_MIN_LEN 64
78 1.1 chopps #define ETHER_MAX_LEN 1518
79 1.1 chopps #define ETHER_ADDR_LEN 6
80 1.1 chopps
81 1.1 chopps /*
82 1.1 chopps * Ethernet software status per interface.
83 1.1 chopps *
84 1.1 chopps * Each interface is referenced by a network interface structure,
85 1.1 chopps * es_if, which the routing code uses to locate the interface.
86 1.1 chopps * This structure contains the output queue for the interface, its address, ...
87 1.1 chopps */
88 1.1 chopps struct es_softc {
89 1.1 chopps struct device sc_dev;
90 1.1 chopps struct isr sc_isr;
91 1.1 chopps struct arpcom sc_ac; /* common Ethernet structures */
92 1.1 chopps #define sc_if sc_ac.ac_if /* network-visible interface */
93 1.1 chopps #define sc_addr sc_ac.ac_enaddr /* hardware Ethernet address */
94 1.1 chopps void *sc_base; /* base address of board */
95 1.1 chopps short sc_iflags;
96 1.1 chopps #if NBPFILTER > 0
97 1.1 chopps caddr_t sc_bpf;
98 1.1 chopps #endif
99 1.1 chopps unsigned short sc_intctl;
100 1.1 chopps #ifdef ESDEBUG
101 1.1 chopps int sc_debug;
102 1.1 chopps #endif
103 1.1 chopps };
104 1.1 chopps
105 1.1 chopps #if NBPFILTER > 0
106 1.1 chopps #include <net/bpf.h>
107 1.1 chopps #include <net/bpfdesc.h>
108 1.1 chopps #endif
109 1.1 chopps
110 1.1 chopps #ifdef ESDEBUG
111 1.1 chopps /* console error messages */
112 1.1 chopps int esdebug = 0;
113 1.1 chopps #endif
114 1.1 chopps
115 1.1 chopps int esintr __P((struct es_softc *));
116 1.1 chopps int esstart __P((struct ifnet *));
117 1.1 chopps int eswatchdog __P((int));
118 1.1 chopps int esioctl __P((struct ifnet *, u_long, caddr_t));
119 1.1 chopps void esrint __P((struct es_softc *));
120 1.1 chopps void estint __P((struct es_softc *));
121 1.1 chopps void esinit __P((struct es_softc *));
122 1.1 chopps void esreset __P((struct es_softc *));
123 1.1 chopps
124 1.1 chopps extern struct ifnet loif;
125 1.1 chopps
126 1.1 chopps void esattach __P((struct device *, struct device *, void *));
127 1.1 chopps int esmatch __P((struct device *, struct cfdata *, void *args));
128 1.1 chopps
129 1.1 chopps struct cfdriver escd = {
130 1.1 chopps NULL, "es", (cfmatch_t)esmatch, esattach, DV_IFNET,
131 1.1 chopps sizeof(struct es_softc), NULL, 0};
132 1.1 chopps
133 1.1 chopps int
134 1.1 chopps esmatch(pdp, cfp, auxp)
135 1.1 chopps struct device *pdp;
136 1.1 chopps struct cfdata *cfp;
137 1.1 chopps void *auxp;
138 1.1 chopps {
139 1.1 chopps
140 1.1 chopps struct zbus_args *zap;
141 1.1 chopps
142 1.1 chopps zap = (struct zbus_args *)auxp;
143 1.1 chopps
144 1.1 chopps /* Ameristar A4066 ethernet card */
145 1.1 chopps if ( zap->manid == 1053 && zap->prodid == 10)
146 1.1 chopps return(1);
147 1.1 chopps
148 1.1 chopps return (0);
149 1.1 chopps }
150 1.1 chopps
151 1.1 chopps /*
152 1.1 chopps * Interface exists: make available by filling in network interface
153 1.1 chopps * record. System will initialize the interface when it is ready
154 1.1 chopps * to accept packets.
155 1.1 chopps */
156 1.1 chopps void
157 1.1 chopps esattach(pdp, dp, auxp)
158 1.1 chopps struct device *pdp, *dp;
159 1.1 chopps void *auxp;
160 1.1 chopps {
161 1.1 chopps struct zbus_args *zap;
162 1.1 chopps struct es_softc *sc = (struct es_softc *) dp;
163 1.1 chopps struct ifnet *ifp = &sc->sc_if;
164 1.1 chopps char *cp;
165 1.1 chopps int i;
166 1.1 chopps unsigned long ser;
167 1.1 chopps
168 1.1 chopps zap =(struct zbus_args *)auxp;
169 1.1 chopps
170 1.1 chopps /*
171 1.1 chopps * Make config msgs look nicer.
172 1.1 chopps */
173 1.1 chopps /* printf("\n");*/
174 1.1 chopps
175 1.1 chopps sc->sc_base = zap->va;
176 1.1 chopps
177 1.1 chopps /*
178 1.1 chopps * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
179 1.1 chopps * (Currently only Ameristar.)
180 1.1 chopps */
181 1.1 chopps sc->sc_addr[0] = 0x00;
182 1.1 chopps sc->sc_addr[1] = 0x00;
183 1.1 chopps sc->sc_addr[2] = 0x9f;
184 1.1 chopps
185 1.1 chopps /*
186 1.1 chopps * Serial number for board contains last 3 bytes.
187 1.1 chopps */
188 1.1 chopps ser = (unsigned long) zap->serno;
189 1.1 chopps
190 1.1 chopps sc->sc_addr[3] = (ser >> 16) & 0xff;
191 1.1 chopps sc->sc_addr[4] = (ser >> 8) & 0xff;
192 1.1 chopps sc->sc_addr[5] = (ser ) & 0xff;
193 1.1 chopps
194 1.1 chopps #if 0
195 1.1 chopps printf("es%d: hardware address %s\n",
196 1.1 chopps dp->dv_unit, ether_sprintf(sc->sc_addr));
197 1.1 chopps #else
198 1.1 chopps printf(": address %s\n", ether_sprintf(sc->sc_addr));
199 1.1 chopps #endif
200 1.1 chopps
201 1.1 chopps ifp->if_unit = dp->dv_unit;
202 1.1 chopps ifp->if_name = escd.cd_name;
203 1.1 chopps ifp->if_mtu = ETHERMTU;
204 1.1 chopps ifp->if_output = ether_output;
205 1.1 chopps ifp->if_ioctl = esioctl;
206 1.1 chopps ifp->if_start = esstart;
207 1.1 chopps ifp->if_watchdog = eswatchdog;
208 1.1 chopps /* XXX IFF_MULTICAST */
209 1.1 chopps ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
210 1.1 chopps
211 1.1 chopps if_attach(ifp);
212 1.1 chopps ether_ifattach(ifp);
213 1.1 chopps #if NBPFILTER > 0
214 1.1 chopps bpfattach(&sc->sc_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
215 1.1 chopps #endif
216 1.1 chopps
217 1.1 chopps sc->sc_isr.isr_intr = esintr;
218 1.1 chopps sc->sc_isr.isr_arg = sc;
219 1.1 chopps sc->sc_isr.isr_ipl = 2;
220 1.1 chopps add_isr (&sc->sc_isr);
221 1.1 chopps return;
222 1.1 chopps }
223 1.1 chopps
224 1.1 chopps void
225 1.1 chopps esinit(sc)
226 1.1 chopps struct es_softc *sc;
227 1.1 chopps {
228 1.1 chopps struct ifnet *ifp = &sc->sc_ac.ac_if;
229 1.1 chopps union smcregs *smc = sc->sc_base;
230 1.1 chopps int s;
231 1.1 chopps
232 1.1 chopps if (!ifp->if_addrlist)
233 1.1 chopps return;
234 1.1 chopps
235 1.1 chopps s = splimp();
236 1.1 chopps
237 1.1 chopps smc->b0.bsr = 0x0000; /* Select bank 0 */
238 1.1 chopps smc->b0.rcr = RCR_EPH_RST;
239 1.1 chopps smc->b0.rcr = 0;
240 1.1 chopps smc->b3.bsr = 0x0300; /* Select bank 3 */
241 1.1 chopps smc->b3.mt[0] = 0; /* clear Multicast table */
242 1.1 chopps smc->b3.mt[1] = 0;
243 1.1 chopps smc->b3.mt[2] = 0;
244 1.1 chopps smc->b3.mt[3] = 0;
245 1.1 chopps /* XXX set Multicast table from Multicast list */
246 1.1 chopps smc->b1.bsr = 0x0100; /* Select bank 1 */
247 1.1 chopps smc->b1.cr = CR_RAM32K | CR_NO_WAIT_ST | CR_SET_SQLCH;
248 1.1 chopps smc->b1.ctr = CTR_AUTO_RLSE;
249 1.1 chopps smc->b1.iar[0] = *((unsigned short *) &sc->sc_addr[0]);
250 1.1 chopps smc->b1.iar[1] = *((unsigned short *) &sc->sc_addr[2]);
251 1.1 chopps smc->b1.iar[2] = *((unsigned short *) &sc->sc_addr[4]);
252 1.1 chopps smc->b2.bsr = 0x0200; /* Select bank 2 */
253 1.1 chopps smc->b2.mmucr = MMUCR_RESET;
254 1.1 chopps smc->b0.bsr = 0x0000; /* Select bank 0 */
255 1.1 chopps smc->b0.mcr = SWAP (0x0020); /* reserve 8K for transmit buffers */
256 1.1 chopps smc->b0.tcr = TCR_PAD_EN | TCR_TXENA + TCR_MON_CSN;
257 1.1 chopps smc->b0.rcr = RCR_FILT_CAR | RCR_STRIP_CRC | RCR_RXEN;
258 1.1 chopps /* XXX add multicast/promiscuous flags */
259 1.1 chopps smc->b2.bsr = 0x0200; /* Select bank 2 */
260 1.1 chopps smc->b2.msk = sc->sc_intctl = MSK_RX_OVRN | MSK_RX;
261 1.1 chopps
262 1.1 chopps /* Interface is now 'running', with no output active. */
263 1.1 chopps ifp->if_flags |= IFF_RUNNING;
264 1.1 chopps ifp->if_flags &= ~IFF_OACTIVE;
265 1.1 chopps
266 1.1 chopps /* Attempt to start output, if any. */
267 1.1 chopps esstart(ifp);
268 1.1 chopps
269 1.1 chopps splx(s);
270 1.1 chopps }
271 1.1 chopps
272 1.1 chopps int
273 1.1 chopps esintr(sc)
274 1.1 chopps struct es_softc *sc;
275 1.1 chopps {
276 1.1 chopps int i;
277 1.2 chopps u_short intsts, intact;
278 1.1 chopps int done = 0;
279 1.1 chopps union smcregs *smc;
280 1.1 chopps
281 1.1 chopps smc = sc->sc_base;
282 1.2 chopps intsts = smc->b2.ist;
283 1.2 chopps intact = smc->b2.msk & intsts;
284 1.2 chopps if ((intact) == 0)
285 1.1 chopps return (0);
286 1.1 chopps #ifdef ESDEBUG
287 1.1 chopps if (esdebug)
288 1.1 chopps printf ("%s: esintr ist %02x msk %02x",
289 1.2 chopps sc->sc_dev.dv_xname, intsts, smc->b2.msk);
290 1.1 chopps #endif
291 1.1 chopps smc->b2.msk = 0;
292 1.1 chopps #ifdef ESDEBUG
293 1.1 chopps if (esdebug)
294 1.1 chopps printf ("=>%02x%02x pnr %02x arr %02x fifo %04x\n",
295 1.1 chopps smc->b2.ist, smc->b2.ist, smc->b2.pnr, smc->b2.arr,
296 1.1 chopps smc->b2.fifo);
297 1.1 chopps #endif
298 1.2 chopps if (intact & IST_ALLOC) {
299 1.1 chopps sc->sc_intctl &= ~MSK_ALLOC;
300 1.1 chopps #ifdef ESDEBUG
301 1.1 chopps if (esdebug || 1)
302 1.1 chopps printf ("%s: ist %02x\n", sc->sc_dev.dv_xname,
303 1.2 chopps intsts);
304 1.1 chopps #endif
305 1.1 chopps if ((smc->b2.arr & ARR_FAILED) == 0) {
306 1.1 chopps #ifdef ESDEBUG
307 1.1 chopps if (esdebug || 1)
308 1.1 chopps printf ("%s: arr %02x\n", sc->sc_dev.dv_xname,
309 1.1 chopps smc->b2.arr);
310 1.1 chopps #endif
311 1.1 chopps smc->b2.pnr = smc->b2.arr;
312 1.1 chopps smc->b2.mmucr = MMUCR_RLSPKT;
313 1.1 chopps while (smc->b2.mmucr & MMUCR_BUSY)
314 1.1 chopps ;
315 1.1 chopps sc->sc_ac.ac_if.if_flags &= ~IFF_OACTIVE;
316 1.1 chopps }
317 1.1 chopps }
318 1.1 chopps while ((smc->b2.fifo & FIFO_REMPTY) == 0) {
319 1.1 chopps esrint(sc);
320 1.1 chopps }
321 1.2 chopps if (intact & IST_RX_OVRN) {
322 1.1 chopps printf ("%s: Overrun ist %02x", sc->sc_dev.dv_xname,
323 1.2 chopps intsts);
324 1.1 chopps smc->b2.ist = ACK_RX_OVRN;
325 1.1 chopps printf ("->%02x\n", smc->b2.ist);
326 1.1 chopps sc->sc_if.if_ierrors++;
327 1.1 chopps }
328 1.2 chopps if (intact & IST_TX) {
329 1.1 chopps #ifdef ESDEBUG
330 1.1 chopps if (esdebug)
331 1.1 chopps printf ("%s: TX INT ist %02x",
332 1.2 chopps sc->sc_dev.dv_xname, intsts);
333 1.1 chopps #endif
334 1.1 chopps smc->b2.ist = ACK_TX;
335 1.1 chopps #ifdef ESDEBUG
336 1.1 chopps if (esdebug)
337 1.1 chopps printf ("->%02x\n", smc->b2.ist);
338 1.1 chopps #endif
339 1.1 chopps smc->b0.bsr = 0x0000;
340 1.2 chopps if ((smc->b0.tcr & TCR_TXENA) == 0) {
341 1.2 chopps printf ("%s: IST %02x masked %02x EPH status %04x tcr %04x\n",
342 1.2 chopps sc->sc_dev.dv_xname, intsts, intact, smc->b0.ephsr,
343 1.2 chopps smc->b0.tcr);
344 1.2 chopps if ((smc->b0.ephsr & EPHSR_TX_SUC) == 0) {
345 1.2 chopps smc->b0.tcr |= TCR_TXENA;
346 1.2 chopps sc->sc_if.if_oerrors++;
347 1.2 chopps }
348 1.2 chopps }
349 1.2 chopps /*
350 1.2 chopps * else - got a TX interrupt, but TCR_TXENA is still set??
351 1.2 chopps */
352 1.2 chopps #if 0
353 1.2 chopps else if ((intact & IST_TX_EMPTY) == 0) {
354 1.2 chopps printf("%s: unexpected TX interrupt %02x %02x",
355 1.2 chopps sc->sc_dev.dv_xname, intsts, intact);
356 1.2 chopps smc->b2.bsr = 0x0200;
357 1.2 chopps printf (" %02x\n", smc->b2.ist);
358 1.1 chopps }
359 1.2 chopps #endif
360 1.1 chopps smc->b2.bsr = 0x0200;
361 1.1 chopps }
362 1.2 chopps if (intact & IST_TX_EMPTY) {
363 1.1 chopps u_short ecr;
364 1.1 chopps #ifdef ESDEBUG
365 1.1 chopps if (esdebug)
366 1.1 chopps printf ("%s: TX EMPTY %02x",
367 1.2 chopps sc->sc_dev.dv_xname, intsts);
368 1.1 chopps #endif
369 1.1 chopps smc->b2.ist = ACK_TX_EMPTY;
370 1.1 chopps #ifdef ESDEBUG
371 1.1 chopps if (esdebug)
372 1.1 chopps printf ("->%02x intcl %x pnr %02x arr %02x\n",
373 1.1 chopps smc->b2.ist, sc->sc_intctl, smc->b2.pnr,
374 1.1 chopps smc->b2.arr);
375 1.1 chopps #endif
376 1.1 chopps sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
377 1.1 chopps smc->b0.bsr = 0x0000;
378 1.1 chopps ecr = smc->b0.ecr; /* Get error counters */
379 1.1 chopps if (ecr & 0xff00)
380 1.1 chopps sc->sc_if.if_collisions += ((ecr >> 8) & 15) +
381 1.1 chopps ((ecr >> 11) & 0x1e);
382 1.1 chopps smc->b2.bsr = 0x0200;
383 1.1 chopps }
384 1.1 chopps /* output packets */
385 1.1 chopps estint(sc);
386 1.1 chopps smc->b2.msk = sc->sc_intctl;
387 1.1 chopps return (1);
388 1.1 chopps }
389 1.1 chopps
390 1.1 chopps void
391 1.1 chopps esrint (sc)
392 1.1 chopps struct es_softc *sc;
393 1.1 chopps {
394 1.1 chopps union smcregs *smc = sc->sc_base;
395 1.1 chopps int i;
396 1.1 chopps u_short len;
397 1.1 chopps short cnt;
398 1.1 chopps u_short pktctlw, pktlen, *buf;
399 1.1 chopps u_long *lbuf;
400 1.1 chopps volatile u_short *data;
401 1.1 chopps volatile u_long *ldata;
402 1.1 chopps struct ifnet *ifp;
403 1.1 chopps struct mbuf *top, **mp, *m;
404 1.1 chopps struct ether_header *eh;
405 1.1 chopps #ifdef USEPKTBUF
406 1.1 chopps u_char *b, pktbuf[1530];
407 1.1 chopps #endif
408 1.1 chopps
409 1.1 chopps #ifdef ESDEBUG
410 1.1 chopps if (esdebug)
411 1.1 chopps printf ("%s: esrint fifo %04x", sc->sc_dev.dv_xname,
412 1.1 chopps smc->b2.fifo);
413 1.1 chopps #endif
414 1.1 chopps data = (u_short *)&smc->b2.data;
415 1.1 chopps smc->b2.ptr = PTR_RCV | PTR_AUTOINCR | PTR_READ | SWAP (0x0002);
416 1.1 chopps (void) smc->b2.mmucr;
417 1.1 chopps #ifdef ESDEBUG
418 1.1 chopps if (esdebug)
419 1.1 chopps printf ("->%04x", smc->b2.fifo);
420 1.1 chopps #endif
421 1.1 chopps len = *data;
422 1.1 chopps len = SWAP (len); /* Careful of macro side-effects */
423 1.1 chopps #ifdef ESDEBUG
424 1.1 chopps if (esdebug)
425 1.1 chopps printf (" length %d", len);
426 1.1 chopps #endif
427 1.1 chopps smc->b2.ptr = PTR_RCV | PTR_AUTOINCR + PTR_READ | SWAP (0x0000);
428 1.1 chopps (void) smc->b2.mmucr;
429 1.1 chopps pktctlw = *data;
430 1.1 chopps pktlen = *data;
431 1.1 chopps pktctlw = SWAP (pktctlw);
432 1.1 chopps pktlen = SWAP (pktlen) - 6;
433 1.1 chopps if (pktctlw & RFSW_ODDFRM)
434 1.1 chopps pktlen++;
435 1.1 chopps /* XXX copy directly from controller to mbuf */
436 1.1 chopps #ifdef USEPKTBUF
437 1.1 chopps #if 1
438 1.1 chopps lbuf = (u_long *) pktbuf;
439 1.1 chopps ldata = (u_long *)data;
440 1.1 chopps cnt = (len - 4) / 4;
441 1.1 chopps while (cnt--)
442 1.1 chopps *lbuf++ = *ldata;
443 1.1 chopps if ((len - 4) & 2) {
444 1.1 chopps buf = (u_short *) lbuf;
445 1.1 chopps *buf = *data;
446 1.1 chopps }
447 1.1 chopps #else
448 1.1 chopps buf = (u_short *)pktbuf;
449 1.1 chopps cnt = (len - 4) / 2;
450 1.1 chopps while (cnt--)
451 1.1 chopps *buf++ = *data;
452 1.1 chopps #endif
453 1.1 chopps smc->b2.mmucr = MMUCR_REMRLS_RX;
454 1.1 chopps while (smc->b2.mmucr & MMUCR_BUSY)
455 1.1 chopps ;
456 1.1 chopps #ifdef ESDEBUG
457 1.1 chopps if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
458 1.1 chopps printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
459 1.1 chopps /* count input error? */
460 1.1 chopps }
461 1.1 chopps if (esdebug) {
462 1.1 chopps printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
463 1.1 chopps smc->b2.fifo);
464 1.1 chopps for (i = 0; i < pktlen; ++i)
465 1.1 chopps printf ("%02x%s", pktbuf[i], ((i & 31) == 31) ? "\n" :
466 1.1 chopps "");
467 1.1 chopps if (i & 31)
468 1.1 chopps printf ("\n");
469 1.1 chopps }
470 1.1 chopps #endif
471 1.1 chopps #else /* USEPKTBUF */
472 1.1 chopps #ifdef ESDEBUG
473 1.1 chopps if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
474 1.1 chopps printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
475 1.1 chopps /* count input error? */
476 1.1 chopps }
477 1.1 chopps if (esdebug) {
478 1.1 chopps printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
479 1.1 chopps smc->b2.fifo);
480 1.1 chopps }
481 1.1 chopps #endif
482 1.1 chopps #endif /* USEPKTBUF */
483 1.1 chopps ifp = &sc->sc_ac.ac_if;
484 1.1 chopps ifp->if_ipackets++;
485 1.1 chopps MGETHDR(m, M_DONTWAIT, MT_DATA);
486 1.1 chopps if (m == NULL)
487 1.1 chopps return;
488 1.1 chopps m->m_pkthdr.rcvif = ifp;
489 1.1 chopps m->m_pkthdr.len = pktlen;
490 1.1 chopps len = MHLEN;
491 1.1 chopps top = NULL;
492 1.1 chopps mp = ⊤
493 1.1 chopps #ifdef USEPKTBUF
494 1.1 chopps b = pktbuf;
495 1.1 chopps #endif
496 1.1 chopps while (pktlen > 0) {
497 1.1 chopps if (top) {
498 1.1 chopps MGET(m, M_DONTWAIT, MT_DATA);
499 1.1 chopps if (m == 0) {
500 1.1 chopps m_freem(top);
501 1.1 chopps return;
502 1.1 chopps }
503 1.1 chopps len = MLEN;
504 1.1 chopps }
505 1.1 chopps if (pktlen >= MINCLSIZE) {
506 1.1 chopps MCLGET(m, M_DONTWAIT);
507 1.1 chopps if (m->m_flags & M_EXT)
508 1.1 chopps len = MCLBYTES;
509 1.1 chopps }
510 1.1 chopps m->m_len = len = min(pktlen, len);
511 1.1 chopps #ifdef USEPKTBUF
512 1.1 chopps bcopy((caddr_t)b, mtod(m, caddr_t), len);
513 1.1 chopps b += len;
514 1.1 chopps #else /* USEPKTBUF */
515 1.1 chopps buf = mtod(m, u_short *);
516 1.1 chopps cnt = len / 2;
517 1.1 chopps while (cnt--)
518 1.1 chopps *buf++ = *data;
519 1.1 chopps if (len & 1)
520 1.1 chopps *buf = *data; /* XXX should be byte store */
521 1.1 chopps #ifdef ESDEBUG
522 1.1 chopps if (esdebug) {
523 1.1 chopps buf = mtod(m, u_short *);
524 1.1 chopps for (i = 0; i < len; ++i)
525 1.1 chopps printf ("%02x%s", ((u_char *)buf)[i],
526 1.1 chopps ((i & 31) == 31) ? "\n" : "");
527 1.1 chopps if (i & 31)
528 1.1 chopps printf ("\n");
529 1.1 chopps }
530 1.1 chopps #endif
531 1.1 chopps #endif /* USEPKTBUF */
532 1.1 chopps pktlen -= len;
533 1.1 chopps *mp = m;
534 1.1 chopps mp = &m->m_next;
535 1.1 chopps }
536 1.1 chopps #ifndef USEPKTBUF
537 1.1 chopps smc->b2.mmucr = MMUCR_REMRLS_RX;
538 1.1 chopps while (smc->b2.mmucr & MMUCR_BUSY)
539 1.1 chopps ;
540 1.1 chopps #endif
541 1.1 chopps eh = mtod(top, struct ether_header *);
542 1.1 chopps top->m_pkthdr.len -= sizeof (*eh);
543 1.1 chopps top->m_len -= sizeof (*eh);
544 1.1 chopps top->m_data += sizeof (*eh);
545 1.1 chopps
546 1.1 chopps ether_input(ifp, eh, top);
547 1.1 chopps }
548 1.1 chopps
549 1.1 chopps void
550 1.1 chopps estint (sc)
551 1.1 chopps struct es_softc *sc;
552 1.1 chopps {
553 1.1 chopps esstart (&sc->sc_ac.ac_if);
554 1.1 chopps }
555 1.1 chopps
556 1.1 chopps int
557 1.1 chopps esstart(ifp)
558 1.1 chopps struct ifnet *ifp;
559 1.1 chopps {
560 1.1 chopps struct es_softc *sc = escd.cd_devs[ifp->if_unit];
561 1.1 chopps union smcregs *smc = sc->sc_base;
562 1.1 chopps struct mbuf *m0, *m;
563 1.1 chopps #ifdef USEPKTBUF
564 1.1 chopps u_short pktbuf[ETHER_MAX_LEN + 2];
565 1.1 chopps #endif
566 1.1 chopps u_short pktctlw, pktlen;
567 1.1 chopps u_short *buf;
568 1.1 chopps volatile u_short *data;
569 1.1 chopps u_long *lbuf;
570 1.1 chopps volatile u_long *ldata;
571 1.1 chopps short cnt;
572 1.1 chopps int i;
573 1.1 chopps
574 1.1 chopps if ((sc->sc_ac.ac_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
575 1.1 chopps IFF_RUNNING)
576 1.1 chopps return;
577 1.1 chopps
578 1.1 chopps for (;;) {
579 1.1 chopps /*
580 1.1 chopps * Sneak a peek at the next packet to get the length
581 1.1 chopps * and see if the SMC 91C90 can accept it.
582 1.1 chopps */
583 1.1 chopps m = sc->sc_ac.ac_if.if_snd.ifq_head;
584 1.1 chopps if (!m)
585 1.1 chopps break;
586 1.1 chopps #ifdef ESDEBUG
587 1.1 chopps if (esdebug && (m->m_next || m->m_len & 1))
588 1.1 chopps printf("%s: esstart m_next %x m_len %d\n", sc->sc_dev.dv_xname,
589 1.1 chopps m->m_next, m->m_len);
590 1.1 chopps #endif
591 1.1 chopps for (m0 = m, pktlen = 0; m0; m0 = m0->m_next)
592 1.1 chopps pktlen += m0->m_len;
593 1.1 chopps #ifdef ESDEBUG
594 1.1 chopps if (esdebug)
595 1.1 chopps printf("%s: esstart r1 %x len %d", sc->sc_dev.dv_xname, smc->b2.pnr, pktlen);
596 1.1 chopps #endif
597 1.1 chopps pktctlw = 0;
598 1.1 chopps pktlen += 4;
599 1.1 chopps if (pktlen & 1)
600 1.1 chopps ++pktlen; /* control byte after last byte */
601 1.1 chopps else
602 1.1 chopps pktlen += 2; /* control byte after pad byte */
603 1.1 chopps smc->b2.mmucr = MMUCR_ALLOC | (pktlen & 0x0700);
604 1.1 chopps for (i = 0; i <= 5; ++i)
605 1.1 chopps if ((smc->b2.arr & ARR_FAILED) == 0)
606 1.1 chopps break;
607 1.1 chopps if (smc->b2.arr & ARR_FAILED) {
608 1.1 chopps sc->sc_ac.ac_if.if_flags |= IFF_OACTIVE;
609 1.1 chopps #ifdef ESDEBUG
610 1.1 chopps if (esdebug)
611 1.1 chopps printf("-no xmit bufs r1 %x\n", smc->b2.pnr);
612 1.1 chopps #endif
613 1.2 chopps sc->sc_intctl |= MSK_ALLOC;
614 1.1 chopps break;
615 1.1 chopps }
616 1.1 chopps smc->b2.pnr = smc->b2.arr;
617 1.1 chopps
618 1.1 chopps IF_DEQUEUE(&sc->sc_ac.ac_if.if_snd, m);
619 1.1 chopps smc->b2.ptr = PTR_AUTOINCR;
620 1.1 chopps (void) smc->b2.mmucr;
621 1.1 chopps data = (u_short *)&smc->b2.data;
622 1.1 chopps *data = SWAP (pktctlw);
623 1.1 chopps *data = SWAP (pktlen);
624 1.1 chopps #ifdef USEPKTBUF
625 1.1 chopps i = 0;
626 1.1 chopps for (m0 = m; m; m = m->m_next) {
627 1.1 chopps bcopy(mtod(m, caddr_t), (char *)pktbuf + i, m->m_len);
628 1.1 chopps i += m->m_len;
629 1.1 chopps }
630 1.1 chopps
631 1.1 chopps if (i & 1) /* Figure out where to put control byte */
632 1.1 chopps pktbuf[i/2] = (pktbuf[i/2] & 0xff00) | CTLB_ODD;
633 1.1 chopps else
634 1.1 chopps pktbuf[i/2] = 0;
635 1.1 chopps #ifdef ESDEBUG
636 1.1 chopps if (esdebug) {
637 1.1 chopps int j;
638 1.1 chopps printf("-sending len %d pktlen %d r1 %04x\n", i, pktlen, smc->b2.pnr);
639 1.1 chopps for (j = 0; j < pktlen - 4; ++j)
640 1.1 chopps printf("%02x%s", ((u_char *)pktbuf)[j], ((j & 31) == 31) ? "\n" : "");
641 1.1 chopps if (j & 31)
642 1.1 chopps printf("\n");
643 1.1 chopps }
644 1.1 chopps #endif
645 1.1 chopps #if 0 /* doesn't quite work? */
646 1.1 chopps lbuf = (u_long *)(pktbuf);
647 1.1 chopps ldata = (u_long *)data;
648 1.1 chopps cnt = pktlen / 4;
649 1.1 chopps while(cnt--)
650 1.1 chopps *ldata = *lbuf++;
651 1.1 chopps if (pktlen & 2) {
652 1.1 chopps buf = (u_short *)lbuf;
653 1.1 chopps *data = *buf;
654 1.1 chopps }
655 1.1 chopps #else
656 1.1 chopps buf = pktbuf;
657 1.1 chopps cnt = pktlen / 2;
658 1.1 chopps while (cnt--)
659 1.1 chopps *data = *buf++;
660 1.1 chopps #endif
661 1.1 chopps #else /* USEPKTBUF */
662 1.1 chopps #ifdef ESDEBUG
663 1.1 chopps if (esdebug)
664 1.1 chopps printf("-sending pktlen %d r1 %04x\n", pktlen, smc->b2.pnr);
665 1.1 chopps #endif
666 1.1 chopps pktctlw = 0;
667 1.1 chopps for (m0 = m; m; m = m->m_next) {
668 1.1 chopps buf = mtod(m, u_short *);
669 1.1 chopps #ifdef ESDEBUG
670 1.1 chopps if (esdebug) {
671 1.1 chopps printf(" m->m_len %d\n", m->m_len);
672 1.1 chopps for (i = 0; i < m->m_len; ++i)
673 1.1 chopps printf("%02x%s", ((u_char *)buf)[i], ((i & 31) == 31) ? "\n" : "");
674 1.1 chopps if (i & 31)
675 1.1 chopps printf("\n");
676 1.1 chopps }
677 1.1 chopps #endif
678 1.1 chopps cnt = m->m_len / 2;
679 1.1 chopps while (cnt--)
680 1.1 chopps *data = *buf++;
681 1.1 chopps if (m->m_len & 1)
682 1.1 chopps pktctlw = (*buf & 0xff00) | CTLB_ODD;
683 1.1 chopps }
684 1.1 chopps *data = pktctlw;
685 1.1 chopps #endif /* USEPKTBUF */
686 1.1 chopps smc->b2.mmucr = MMUCR_ENQ_TX;
687 1.1 chopps #ifdef ESDEBUG
688 1.1 chopps if (esdebug)
689 1.1 chopps printf("%s: esstart packet sent r1 %x\n", sc->sc_dev.dv_xname, smc->b2.pnr);
690 1.1 chopps #endif
691 1.1 chopps #if NBPFILTER > 0
692 1.1 chopps if (sc->sc_ac.ac_if.if_bpf)
693 1.1 chopps bpf_mtap(sc->sc_ac.ac_if.if_bpf, m0);
694 1.1 chopps #endif
695 1.1 chopps m_freem(m0);
696 1.1 chopps sc->sc_ac.ac_if.if_opackets++; /* move to interrupt? */
697 1.2 chopps sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
698 1.1 chopps }
699 1.1 chopps smc->b2.msk = sc->sc_intctl;
700 1.1 chopps return 0;
701 1.1 chopps }
702 1.1 chopps
703 1.1 chopps int
704 1.1 chopps esioctl(ifp, cmd, data)
705 1.1 chopps struct ifnet *ifp;
706 1.1 chopps u_long cmd;
707 1.1 chopps caddr_t data;
708 1.1 chopps {
709 1.1 chopps struct es_softc *sc = escd.cd_devs[ifp->if_unit];
710 1.1 chopps struct ifaddr *ifa = (struct ifaddr *)data;
711 1.1 chopps struct ifreq *ifr = (struct ifreq *)data;
712 1.1 chopps int s, error = 0;
713 1.1 chopps
714 1.1 chopps #ifdef ESDEBUG
715 1.1 chopps if (esdebug)
716 1.1 chopps printf ("esioctl called: cmd %x\n", cmd);
717 1.1 chopps #endif
718 1.1 chopps
719 1.1 chopps s = splimp();
720 1.1 chopps
721 1.1 chopps switch (cmd) {
722 1.1 chopps case SIOCSIFADDR:
723 1.1 chopps ifp->if_flags |= IFF_UP;
724 1.1 chopps
725 1.1 chopps switch (ifa->ifa_addr->sa_family) {
726 1.1 chopps #ifdef INET
727 1.1 chopps case AF_INET:
728 1.1 chopps esinit(sc);
729 1.1 chopps sc->sc_ac.ac_ipaddr = IA_SIN(ifa)->sin_addr;
730 1.1 chopps arpwhohas(&sc->sc_ac, &IA_SIN(ifa)->sin_addr);
731 1.1 chopps break;
732 1.1 chopps #endif
733 1.1 chopps #ifdef NS
734 1.1 chopps case AF_NS:
735 1.1 chopps {
736 1.1 chopps register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
737 1.1 chopps
738 1.1 chopps if (ns_nullhost(*ina))
739 1.1 chopps ina->x_host =
740 1.1 chopps *(uniion ns_host *)(sc->sc_addr);
741 1.1 chopps else
742 1.1 chopps bcopy(ina->x_host.c_host,
743 1.1 chopps sc->sc_addr, sizeof(sc->sc_addr));
744 1.1 chopps /* Set new address */
745 1.1 chopps esinit(sc);
746 1.1 chopps break;
747 1.1 chopps #endif
748 1.1 chopps default:
749 1.1 chopps esinit(sc);
750 1.1 chopps break;
751 1.1 chopps }
752 1.1 chopps break;
753 1.1 chopps case SIOCSIFFLAGS:
754 1.1 chopps /*
755 1.1 chopps * If interface is marked down and it is running, then stop it
756 1.1 chopps */
757 1.1 chopps if ((ifp->if_flags & IFF_UP) == 0 &&
758 1.1 chopps (ifp->if_flags & IFF_RUNNING) != 0) {
759 1.1 chopps /*
760 1.1 chopps * If interface is marked down and it is running, then
761 1.1 chopps * stop it.
762 1.1 chopps */
763 1.1 chopps /* esstop(sc); */
764 1.1 chopps ifp->if_flags &= ~IFF_RUNNING;
765 1.1 chopps } else if ((ifp->if_flags & IFF_UP) != 0 &&
766 1.1 chopps (ifp->if_flags & IFF_RUNNING) == 0) {
767 1.1 chopps /*
768 1.1 chopps * If interface is marked up and it is stopped, then
769 1.1 chopps * start it.
770 1.1 chopps */
771 1.1 chopps esinit(sc);
772 1.1 chopps } else {
773 1.1 chopps /*
774 1.1 chopps * Reset the interface to pick up changes in any other
775 1.1 chopps * flags that affect hardware registers.
776 1.1 chopps */
777 1.1 chopps /* esstop(sc); */
778 1.1 chopps esinit(sc);
779 1.1 chopps }
780 1.1 chopps #ifdef ESDEBUG
781 1.1 chopps if (ifp->if_flags & IFF_DEBUG)
782 1.1 chopps esdebug = sc->sc_debug = 1;
783 1.1 chopps else
784 1.1 chopps esdebug = sc->sc_debug = 0;
785 1.1 chopps #endif
786 1.1 chopps break;
787 1.1 chopps case SIOCADDMULTI:
788 1.1 chopps case SIOCDELMULTI:
789 1.1 chopps
790 1.1 chopps default:
791 1.1 chopps error = EINVAL;
792 1.1 chopps }
793 1.1 chopps
794 1.1 chopps splx(s);
795 1.1 chopps return (error);
796 1.1 chopps }
797 1.1 chopps
798 1.1 chopps void
799 1.1 chopps esreset(sc)
800 1.1 chopps struct es_softc *sc;
801 1.1 chopps {
802 1.1 chopps int s;
803 1.1 chopps
804 1.1 chopps s = splimp();
805 1.1 chopps /* esstop(sc); */
806 1.1 chopps esinit(sc);
807 1.1 chopps splx(s);
808 1.1 chopps }
809 1.1 chopps
810 1.1 chopps int
811 1.1 chopps eswatchdog(unit)
812 1.1 chopps int unit;
813 1.1 chopps {
814 1.1 chopps register struct es_softc *sc = escd.cd_devs[unit];
815 1.1 chopps
816 1.1 chopps log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
817 1.1 chopps esreset(sc);
818 1.1 chopps return (0);
819 1.1 chopps }
820