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