]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/isa/if_el.c
This commit was generated by cvs2svn to compensate for changes in r53142,
[FreeBSD/FreeBSD.git] / sys / i386 / isa / if_el.c
1 /* Copyright (c) 1994, Matthew E. Kimmel.  Permission is hereby granted
2  * to use, copy, modify and distribute this software provided that both
3  * the copyright notice and this permission notice appear in all copies
4  * of the software, derivative works or modified versions, and any
5  * portions thereof.
6  *
7  * Questions, comments, bug reports and fixes to kimmel@cs.umass.edu.
8  *
9  * $FreeBSD$
10  */
11 /* Except of course for the portions of code lifted from other FreeBSD
12  * drivers (mainly elread, elget and el_ioctl)
13  */
14 /* 3COM Etherlink 3C501 device driver for FreeBSD */
15 /* Yeah, I know these cards suck, but you can also get them for free
16  * really easily...
17  */
18 /* Bugs/possible improvements:
19  *      - Does not currently support DMA
20  *      - Does not currently support multicasts
21  */
22 #include "el.h"
23 #include "opt_inet.h"
24 #include "opt_ipx.h"
25
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/sockio.h>
29 #include <sys/mbuf.h>
30 #include <sys/socket.h>
31 #include <sys/syslog.h>
32
33 #include <net/ethernet.h>
34 #include <net/if.h>
35
36 #include <netinet/in.h>
37 #include <netinet/if_ether.h>
38
39 #include <net/bpf.h>
40
41 #include <machine/clock.h>
42
43 #include <i386/isa/isa_device.h>
44 #include <i386/isa/if_elreg.h>
45
46 /* For debugging convenience */
47 #ifdef EL_DEBUG
48 #define dprintf(x) printf x
49 #else
50 #define dprintf(x)
51 #endif
52
53 /* el_softc: per line info and status */
54 static struct el_softc {
55         struct arpcom arpcom;   /* Ethernet common */
56         u_short el_base;        /* Base I/O addr */
57         char el_pktbuf[EL_BUFSIZ];      /* Frame buffer */
58 } el_softc[NEL];
59
60 /* Prototypes */
61 static int el_attach(struct isa_device *);
62 static void el_init(void *);
63 static int el_ioctl(struct ifnet *,u_long,caddr_t);
64 static int el_probe(struct isa_device *);
65 static void el_start(struct ifnet *);
66 static void el_reset(void *);
67 static void el_watchdog(struct ifnet *);
68
69 static void el_stop(void *);
70 static int el_xmit(struct el_softc *,int);
71 static ointhand2_t elintr;
72 static __inline void elread(struct el_softc *,caddr_t,int);
73 static struct mbuf *elget(caddr_t,int,int,struct ifnet *);
74 static __inline void el_hardreset(void *);
75
76 /* isa_driver structure for autoconf */
77 struct isa_driver eldriver = {
78         el_probe, el_attach, "el"
79 };
80
81 /* Probe routine.  See if the card is there and at the right place. */
82 static int
83 el_probe(struct isa_device *idev)
84 {
85         struct el_softc *sc;
86         u_short base; /* Just for convenience */
87         u_char station_addr[ETHER_ADDR_LEN];
88         int i;
89
90         /* Grab some info for our structure */
91         sc = &el_softc[idev->id_unit];
92         sc->el_base = idev->id_iobase;
93         base = sc->el_base;
94
95         /* First check the base */
96         if((base < 0x280) || (base > 0x3f0)) {
97                 printf("el%d: ioaddr must be between 0x280 and 0x3f0\n",
98                         idev->id_unit);
99                 return(0);
100         }
101
102         /* Now attempt to grab the station address from the PROM
103          * and see if it contains the 3com vendor code.
104          */
105         dprintf(("Probing 3c501 at 0x%x...\n",base));
106
107         /* Reset the board */
108         dprintf(("Resetting board...\n"));
109         outb(base+EL_AC,EL_AC_RESET);
110         DELAY(5);
111         outb(base+EL_AC,0);
112         dprintf(("Reading station address...\n"));
113         /* Now read the address */
114         for(i=0;i<ETHER_ADDR_LEN;i++) {
115                 outb(base+EL_GPBL,i);
116                 station_addr[i] = inb(base+EL_EAW);
117         }
118         dprintf(("Address is %6D\n",station_addr, ":"));
119
120         /* If the vendor code is ok, return a 1.  We'll assume that
121          * whoever configured this system is right about the IRQ.
122          */
123         if((station_addr[0] != 0x02) || (station_addr[1] != 0x60)
124            || (station_addr[2] != 0x8c)) {
125                 dprintf(("Bad vendor code.\n"));
126                 return(0);
127         } else {
128                 dprintf(("Vendor code ok.\n"));
129                 /* Copy the station address into the arpcom structure */
130                 bcopy(station_addr,sc->arpcom.ac_enaddr,ETHER_ADDR_LEN);
131                 return(1);
132         }
133 }
134
135 /* Do a hardware reset of the 3c501.  Do not call until after el_probe()! */
136 static __inline void
137 el_hardreset(xsc)
138         void *xsc;
139 {
140         register struct el_softc *sc = xsc;
141         register int base;
142         register int j;
143
144         base = sc->el_base;
145
146         /* First reset the board */
147         outb(base+EL_AC,EL_AC_RESET);
148         DELAY(5);
149         outb(base+EL_AC,0);
150
151         /* Then give it back its ethernet address.  Thanks to the mach
152          * source code for this undocumented goodie...
153          */
154         for(j=0;j<ETHER_ADDR_LEN;j++)
155                 outb(base+j,sc->arpcom.ac_enaddr[j]);
156 }
157
158 /* Attach the interface to the kernel data structures.  By the time
159  * this is called, we know that the card exists at the given I/O address.
160  * We still assume that the IRQ given is correct.
161  */
162 static int
163 el_attach(struct isa_device *idev)
164 {
165         struct el_softc *sc;
166         struct ifnet *ifp;
167         u_short base;
168
169         dprintf(("Attaching el%d...\n",idev->id_unit));
170
171         /* Get things pointing to the right places. */
172         idev->id_ointr = elintr;
173         sc = &el_softc[idev->id_unit];
174         ifp = &sc->arpcom.ac_if;
175         base = sc->el_base;
176
177         /* Now reset the board */
178         dprintf(("Resetting board...\n"));
179         el_hardreset(sc);
180
181         /* Initialize ifnet structure */
182         ifp->if_softc = sc;
183         ifp->if_unit = idev->id_unit;
184         ifp->if_name = "el";
185         ifp->if_mtu = ETHERMTU;
186         ifp->if_output = ether_output;
187         ifp->if_start = el_start;
188         ifp->if_ioctl = el_ioctl;
189         ifp->if_watchdog = el_watchdog;
190         ifp->if_init = el_init;
191         ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX);
192
193         /* Now we can attach the interface */
194         dprintf(("Attaching interface...\n"));
195         if_attach(ifp);
196         ether_ifattach(ifp);
197
198         /* Print out some information for the user */
199         printf("el%d: 3c501 address %6D\n",idev->id_unit,
200           sc->arpcom.ac_enaddr, ":");
201
202         /* Finally, attach to bpf filter if it is present. */
203         dprintf(("Attaching to BPF...\n"));
204         bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
205
206         dprintf(("el_attach() finished.\n"));
207         return(1);
208 }
209
210 /* This routine resets the interface. */
211 static void 
212 el_reset(xsc)
213         void *xsc;
214 {
215         struct el_softc *sc = xsc;
216         int s;
217
218         dprintf(("elreset()\n"));
219         s = splimp();
220         el_stop(sc);
221         el_init(sc);
222         splx(s);
223 }
224
225 static void el_stop(xsc)
226         void *xsc;
227 {
228         struct el_softc *sc = xsc;
229
230         outb(sc->el_base+EL_AC,0);
231 }
232
233 /* Initialize interface.  */
234 static void 
235 el_init(xsc)
236         void *xsc;
237 {
238         struct el_softc *sc = xsc;
239         struct ifnet *ifp;
240         int s;
241         u_short base;
242
243         /* Set up pointers */
244         ifp = &sc->arpcom.ac_if;
245         base = sc->el_base;
246
247         /* If address not known, do nothing. */
248         if(TAILQ_EMPTY(&ifp->if_addrhead)) /* XXX unlikely */
249                 return;
250
251         s = splimp();
252
253         /* First, reset the board. */
254         dprintf(("Resetting board...\n"));
255         el_hardreset(sc);
256
257         /* Configure rx */
258         dprintf(("Configuring rx...\n"));
259         if(ifp->if_flags & IFF_PROMISC)
260                 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
261         else
262                 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
263         outb(base+EL_RBC,0);
264
265         /* Configure TX */
266         dprintf(("Configuring tx...\n"));
267         outb(base+EL_TXC,0);
268
269         /* Start reception */
270         dprintf(("Starting reception...\n"));
271         outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
272
273         /* Set flags appropriately */
274         ifp->if_flags |= IFF_RUNNING;
275         ifp->if_flags &= ~IFF_OACTIVE;
276
277         /* And start output. */
278         el_start(ifp);
279
280         splx(s);
281 }
282
283 /* Start output on interface.  Get datagrams from the queue and output
284  * them, giving the receiver a chance between datagrams.  Call only
285  * from splimp or interrupt level!
286  */
287 static void
288 el_start(struct ifnet *ifp)
289 {
290         struct el_softc *sc;
291         u_short base;
292         struct mbuf *m, *m0;
293         int s, i, len, retries, done;
294
295         /* Get things pointing in the right directions */
296         sc = ifp->if_softc;
297         base = sc->el_base;
298
299         dprintf(("el_start()...\n"));
300         s = splimp();
301
302         /* Don't do anything if output is active */
303         if(sc->arpcom.ac_if.if_flags & IFF_OACTIVE)
304                 return;
305         sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
306
307         /* The main loop.  They warned me against endless loops, but
308          * would I listen?  NOOO....
309          */
310         while(1) {
311                 /* Dequeue the next datagram */
312                 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd,m0);
313
314                 /* If there's nothing to send, return. */
315                 if(m0 == NULL) {
316                         sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
317                         splx(s);
318                         return;
319                 }
320
321                 /* Disable the receiver */
322                 outb(base+EL_AC,EL_AC_HOST);
323                 outb(base+EL_RBC,0);
324
325                 /* Copy the datagram to the buffer. */
326                 len = 0;
327                 for(m = m0; m != NULL; m = m->m_next) {
328                         if(m->m_len == 0)
329                                 continue;
330                         bcopy(mtod(m,caddr_t),sc->el_pktbuf+len,m->m_len);
331                         len += m->m_len;
332                 }
333                 m_freem(m0);
334
335                 len = max(len,ETHER_MIN_LEN);
336
337                 /* Give the packet to the bpf, if any */
338                 if(sc->arpcom.ac_if.if_bpf)
339                         bpf_tap(&sc->arpcom.ac_if, sc->el_pktbuf, len);
340
341                 /* Transfer datagram to board */
342                 dprintf(("el: xfr pkt length=%d...\n",len));
343                 i = EL_BUFSIZ - len;
344                 outb(base+EL_GPBL,(i & 0xff));
345                 outb(base+EL_GPBH,((i>>8)&0xff));
346                 outsb(base+EL_BUF,sc->el_pktbuf,len);
347
348                 /* Now transmit the datagram */
349                 retries=0;
350                 done=0;
351                 while(!done) {
352                         if(el_xmit(sc,len)) { /* Something went wrong */
353                                 done = -1;
354                                 break;
355                         }
356                         /* Check out status */
357                         i = inb(base+EL_TXS);
358                         dprintf(("tx status=0x%x\n",i));
359                         if(!(i & EL_TXS_READY)) {
360                                 dprintf(("el: err txs=%x\n",i));
361                                 sc->arpcom.ac_if.if_oerrors++;
362                                 if(i & (EL_TXS_COLL|EL_TXS_COLL16)) {
363                                         if((!(i & EL_TXC_DCOLL16)) && retries < 15) {
364                                                 retries++;
365                                                 outb(base+EL_AC,EL_AC_HOST);
366                                         }
367                                 }
368                                 else
369                                         done = 1;
370                         }
371                         else {
372                                 sc->arpcom.ac_if.if_opackets++;
373                                 done = 1;
374                         }
375                 }
376                 if(done == -1)  /* Packet not transmitted */
377                         continue;
378
379                 /* Now give the card a chance to receive.
380                  * Gotta love 3c501s...
381                  */
382                 (void)inb(base+EL_AS);
383                 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
384                 splx(s);
385                 /* Interrupt here */
386                 s = splimp();
387         }
388 }
389
390 /* This function actually attempts to transmit a datagram downloaded
391  * to the board.  Call at splimp or interrupt, after downloading data!
392  * Returns 0 on success, non-0 on failure
393  */
394 static int
395 el_xmit(struct el_softc *sc,int len)
396 {
397         int gpl;
398         int i;
399
400         gpl = EL_BUFSIZ - len;
401         dprintf(("el: xmit..."));
402         outb((sc->el_base)+EL_GPBL,(gpl & 0xff));
403         outb((sc->el_base)+EL_GPBH,((gpl>>8)&0xff));
404         outb((sc->el_base)+EL_AC,EL_AC_TXFRX);
405         i = 20000;
406         while((inb((sc->el_base)+EL_AS) & EL_AS_TXBUSY) && (i>0))
407                 i--;
408         if(i == 0) {
409                 dprintf(("tx not ready\n"));
410                 sc->arpcom.ac_if.if_oerrors++;
411                 return(-1);
412         }
413         dprintf(("%d cycles.\n",(20000-i)));
414         return(0);
415 }
416
417 /* Pass a packet up to the higher levels. */
418 static __inline void
419 elread(struct el_softc *sc,caddr_t buf,int len)
420 {
421         register struct ether_header *eh;
422         struct mbuf *m;
423
424         eh = (struct ether_header *)buf;
425
426         /*
427          * Check if there's a bpf filter listening on this interface.
428          * If so, hand off the raw packet to bpf.
429          */
430         if(sc->arpcom.ac_if.if_bpf) {
431                 bpf_tap(&sc->arpcom.ac_if, buf, 
432                         len + sizeof(struct ether_header));
433
434                 /*
435                  * Note that the interface cannot be in promiscuous mode if
436                  * there are no bpf listeners.  And if el are in promiscuous
437                  * mode, el have to check if this packet is really ours.
438                  *
439                  * This test does not support multicasts.
440                  */
441                 if((sc->arpcom.ac_if.if_flags & IFF_PROMISC)
442                    && bcmp(eh->ether_dhost,sc->arpcom.ac_enaddr,
443                            sizeof(eh->ether_dhost)) != 0
444                    && bcmp(eh->ether_dhost,etherbroadcastaddr,
445                            sizeof(eh->ether_dhost)) != 0)
446                         return;
447         }
448
449         /*
450          * Pull packet off interface.
451          */
452         m = elget(buf,len,0,&sc->arpcom.ac_if);
453         if(m == 0)
454                 return;
455
456         ether_input(&sc->arpcom.ac_if,eh,m);
457 }
458
459 /* controller interrupt */
460 static void
461 elintr(int unit)
462 {
463         register struct el_softc *sc;
464         register int base;
465         int stat, rxstat, len, done;
466
467         /* Get things pointing properly */
468         sc = &el_softc[unit];
469         base = sc->el_base;
470
471         dprintf(("elintr: "));
472
473         /* Check board status */
474         stat = inb(base+EL_AS);
475         if(stat & EL_AS_RXBUSY) {
476                 (void)inb(base+EL_RXC);
477                 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
478                 return;
479         }
480
481         done = 0;
482         while(!done) {
483                 rxstat = inb(base+EL_RXS);
484                 if(rxstat & EL_RXS_STALE) {
485                         (void)inb(base+EL_RXC);
486                         outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
487                         return;
488                 }
489
490                 /* If there's an overflow, reinit the board. */
491                 if(!(rxstat & EL_RXS_NOFLOW)) {
492                         dprintf(("overflow.\n"));
493                         el_hardreset(sc);
494                         /* Put board back into receive mode */
495                         if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
496                                 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
497                         else
498                                 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
499                         (void)inb(base+EL_AS);
500                         outb(base+EL_RBC,0);
501                         (void)inb(base+EL_RXC);
502                         outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
503                         return;
504                 }
505
506                 /* Incoming packet */
507                 len = inb(base+EL_RBL);
508                 len |= inb(base+EL_RBH) << 8;
509                 dprintf(("receive len=%d rxstat=%x ",len,rxstat));
510                 outb(base+EL_AC,EL_AC_HOST);
511
512                 /* If packet too short or too long, restore rx mode and return
513                  */
514                 if((len <= sizeof(struct ether_header)) || (len > ETHER_MAX_LEN)) {
515                         if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
516                                 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
517                         else
518                                 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
519                         (void)inb(base+EL_AS);
520                         outb(base+EL_RBC,0);
521                         (void)inb(base+EL_RXC);
522                         outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
523                         return;
524                 }
525
526                 sc->arpcom.ac_if.if_ipackets++;
527
528                 /* Copy the data into our buffer */
529                 outb(base+EL_GPBL,0);
530                 outb(base+EL_GPBH,0);
531                 insb(base+EL_BUF,sc->el_pktbuf,len);
532                 outb(base+EL_RBC,0);
533                 outb(base+EL_AC,EL_AC_RX);
534                 dprintf(("%6D-->",sc->el_pktbuf+6,":"));
535                 dprintf(("%6D\n",sc->el_pktbuf,":"));
536
537                 /* Pass data up to upper levels */
538                 len -= sizeof(struct ether_header);
539                 elread(sc,(caddr_t)(sc->el_pktbuf),len);
540
541                 /* Is there another packet? */
542                 stat = inb(base+EL_AS);
543
544                 /* If so, do it all again (i.e. don't set done to 1) */
545                 if(!(stat & EL_AS_RXBUSY))
546                         dprintf(("<rescan> "));
547                 else
548                         done = 1;
549         }
550
551         (void)inb(base+EL_RXC);
552         outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
553         return;
554 }
555
556 /*
557  * Pull read data off a interface.
558  * Len is length of data, with local net header stripped.
559  */
560 struct mbuf *
561 elget(buf, totlen, off0, ifp)
562         caddr_t buf;
563         int totlen, off0;
564         struct ifnet *ifp;
565 {
566         struct mbuf *top, **mp, *m;
567         int off = off0, len;
568         register caddr_t cp = buf;
569         char *epkt;
570
571         buf += sizeof(struct ether_header);
572         cp = buf;
573         epkt = cp + totlen;
574
575
576         if (off) {
577                 cp += off + 2 * sizeof(u_short);
578                 totlen -= 2 * sizeof(u_short);
579         }
580
581         MGETHDR(m, M_DONTWAIT, MT_DATA);
582         if (m == 0)
583                 return (0);
584         m->m_pkthdr.rcvif = ifp;
585         m->m_pkthdr.len = totlen;
586         m->m_len = MHLEN;
587         top = 0;
588         mp = &top;
589         while (totlen > 0) {
590                 if (top) {
591                         MGET(m, M_DONTWAIT, MT_DATA);
592                         if (m == 0) {
593                                 m_freem(top);
594                                 return (0);
595                         }
596                         m->m_len = MLEN;
597                 }
598                 len = min(totlen, epkt - cp);
599                 if (len >= MINCLSIZE) {
600                         MCLGET(m, M_DONTWAIT);
601                         if (m->m_flags & M_EXT)
602                                 m->m_len = len = min(len, MCLBYTES);
603                         else
604                                 len = m->m_len;
605                 } else {
606                         /*
607                          * Place initial small packet/header at end of mbuf.
608                          */
609                         if (len < m->m_len) {
610                                 if (top == 0 && len + max_linkhdr <= m->m_len)
611                                         m->m_data += max_linkhdr;
612                                 m->m_len = len;
613                         } else
614                                 len = m->m_len;
615                 }
616                 bcopy(cp, mtod(m, caddr_t), (unsigned)len);
617                 cp += len;
618                 *mp = m;
619                 mp = &m->m_next;
620                 totlen -= len;
621                 if (cp == epkt)
622                         cp = buf;
623         }
624         return (top);
625 }
626
627 /*
628  * Process an ioctl request. This code needs some work - it looks
629  *      pretty ugly.
630  */
631 static int
632 el_ioctl(ifp, command, data)
633         register struct ifnet *ifp;
634         u_long command;
635         caddr_t data;
636 {
637         int s, error = 0;
638
639         s = splimp();
640
641         switch (command) {
642         case SIOCSIFADDR:
643         case SIOCGIFADDR:
644         case SIOCSIFMTU:
645                 error = ether_ioctl(ifp, command, data);
646                 break;
647
648         case SIOCSIFFLAGS:
649                 /*
650                  * If interface is marked down and it is running, then stop it
651                  */
652                 if (((ifp->if_flags & IFF_UP) == 0) &&
653                     (ifp->if_flags & IFF_RUNNING)) {
654                         el_stop(ifp->if_softc);
655                         ifp->if_flags &= ~IFF_RUNNING;
656                 } else {
657                 /*
658                  * If interface is marked up and it is stopped, then start it
659                  */
660                         if ((ifp->if_flags & IFF_UP) &&
661                             ((ifp->if_flags & IFF_RUNNING) == 0))
662                                 el_init(ifp->if_softc);
663                 }
664                 break;
665         default:
666                 error = EINVAL;
667         }
668         (void) splx(s);
669         return (error);
670 }
671
672 /* Device timeout routine */
673 static void
674 el_watchdog(struct ifnet *ifp)
675 {
676         log(LOG_ERR,"el%d: device timeout\n", ifp->if_unit);
677         ifp->if_oerrors++;
678         el_reset(ifp->if_softc);
679 }