Major cleanup of the interrupt registration subsystem.
[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.17 2005/10/13 00:02:37 dillon 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 inthand2_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_intr = 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(void *arg)
424 {
425         int unit = (int)arg;
426         struct el_softc *sc;
427         int base;
428         int stat, rxstat, len, done;
429
430         /* Get things pointing properly */
431         sc = &el_softc[unit];
432         base = sc->el_base;
433
434         dprintf(("elintr: "));
435
436         /* Check board status */
437         stat = inb(base+EL_AS);
438         if(stat & EL_AS_RXBUSY) {
439                 (void)inb(base+EL_RXC);
440                 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
441                 return;
442         }
443
444         done = 0;
445         while(!done) {
446                 rxstat = inb(base+EL_RXS);
447                 if(rxstat & EL_RXS_STALE) {
448                         (void)inb(base+EL_RXC);
449                         outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
450                         return;
451                 }
452
453                 /* If there's an overflow, reinit the board. */
454                 if(!(rxstat & EL_RXS_NOFLOW)) {
455                         dprintf(("overflow.\n"));
456                         el_hardreset(sc);
457                         /* Put board back into receive mode */
458                         if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
459                                 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
460                         else
461                                 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
462                         (void)inb(base+EL_AS);
463                         outb(base+EL_RBC,0);
464                         (void)inb(base+EL_RXC);
465                         outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
466                         return;
467                 }
468
469                 /* Incoming packet */
470                 len = inb(base+EL_RBL);
471                 len |= inb(base+EL_RBH) << 8;
472                 dprintf(("receive len=%d rxstat=%x ",len,rxstat));
473                 outb(base+EL_AC,EL_AC_HOST);
474
475                 /* If packet too short or too long, restore rx mode and return
476                  */
477                 if((len <= sizeof(struct ether_header)) || (len > ETHER_MAX_LEN)) {
478                         if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
479                                 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
480                         else
481                                 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
482                         (void)inb(base+EL_AS);
483                         outb(base+EL_RBC,0);
484                         (void)inb(base+EL_RXC);
485                         outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
486                         return;
487                 }
488
489                 sc->arpcom.ac_if.if_ipackets++;
490
491                 /* Copy the data into our buffer */
492                 outb(base+EL_GPBL,0);
493                 outb(base+EL_GPBH,0);
494                 insb(base+EL_BUF,sc->el_pktbuf,len);
495                 outb(base+EL_RBC,0);
496                 outb(base+EL_AC,EL_AC_RX);
497                 dprintf(("%6D-->",sc->el_pktbuf+6,":"));
498                 dprintf(("%6D\n",sc->el_pktbuf,":"));
499
500                 /* Pass data up to upper levels */
501                 len -= sizeof(struct ether_header);
502                 elread(sc,(caddr_t)(sc->el_pktbuf),len);
503
504                 /* Is there another packet? */
505                 stat = inb(base+EL_AS);
506
507                 /* If so, do it all again (i.e. don't set done to 1) */
508                 if(!(stat & EL_AS_RXBUSY))
509                         dprintf(("<rescan> "));
510                 else
511                         done = 1;
512         }
513
514         (void)inb(base+EL_RXC);
515         outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
516         return;
517 }
518
519 /*
520  * Pull read data off a interface.
521  * Len is length of data, with local net header stripped.
522  */
523 static struct mbuf *
524 elget(buf, totlen, ifp)
525         caddr_t buf;
526         int totlen;
527         struct ifnet *ifp;
528 {
529         struct mbuf *top, **mp, *m;
530         int len;
531         caddr_t cp;
532         char *epkt;
533
534         buf += sizeof(struct ether_header);
535         cp = buf;
536         epkt = cp + totlen;
537
538         MGETHDR(m, MB_DONTWAIT, MT_DATA);
539         if (m == 0)
540                 return (0);
541         m->m_pkthdr.rcvif = ifp;
542         m->m_pkthdr.len = totlen;
543         m->m_len = MHLEN;
544         top = 0;
545         mp = &top;
546         while (totlen > 0) {
547                 if (top) {
548                         MGET(m, MB_DONTWAIT, MT_DATA);
549                         if (m == 0) {
550                                 m_freem(top);
551                                 return (0);
552                         }
553                         m->m_len = MLEN;
554                 }
555                 len = min(totlen, epkt - cp);
556                 if (len >= MINCLSIZE) {
557                         MCLGET(m, MB_DONTWAIT);
558                         if (m->m_flags & M_EXT)
559                                 m->m_len = len = min(len, MCLBYTES);
560                         else
561                                 len = m->m_len;
562                 } else {
563                         /*
564                          * Place initial small packet/header at end of mbuf.
565                          */
566                         if (len < m->m_len) {
567                                 if (top == 0 && len + max_linkhdr <= m->m_len)
568                                         m->m_data += max_linkhdr;
569                                 m->m_len = len;
570                         } else
571                                 len = m->m_len;
572                 }
573                 bcopy(cp, mtod(m, caddr_t), (unsigned)len);
574                 cp += len;
575                 *mp = m;
576                 mp = &m->m_next;
577                 totlen -= len;
578                 if (cp == epkt)
579                         cp = buf;
580         }
581         return (top);
582 }
583
584 /*
585  * Process an ioctl request. This code needs some work - it looks
586  *      pretty ugly.
587  */
588 static int
589 el_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
590 {
591         int error = 0;
592
593         crit_enter();
594
595         switch (command) {
596         case SIOCSIFFLAGS:
597                 /*
598                  * If interface is marked down and it is running, then stop it
599                  */
600                 if (((ifp->if_flags & IFF_UP) == 0) &&
601                     (ifp->if_flags & IFF_RUNNING)) {
602                         el_stop(ifp->if_softc);
603                         ifp->if_flags &= ~IFF_RUNNING;
604                 } else {
605                 /*
606                  * If interface is marked up and it is stopped, then start it
607                  */
608                         if ((ifp->if_flags & IFF_UP) &&
609                             ((ifp->if_flags & IFF_RUNNING) == 0))
610                                 el_init(ifp->if_softc);
611                 }
612                 break;
613         default:
614                 error = ether_ioctl(ifp, command, data);
615                 break;
616         }
617
618         crit_exit();
619
620         return (error);
621 }
622
623 /* Device timeout routine */
624 static void
625 el_watchdog(struct ifnet *ifp)
626 {
627         log(LOG_ERR,"%s: device timeout\n", ifp->if_xname);
628         ifp->if_oerrors++;
629         el_reset(ifp->if_softc);
630 }