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