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