Announce MAC address in ether_ifattach, not in each NIC indepently.
[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.10 2004/07/02 17:42:17 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
39 #include <netinet/in.h>
40 #include <netinet/if_ether.h>
41
42 #include <net/bpf.h>
43
44 #include <machine/clock.h>
45
46 #include <bus/isa/i386/isa_device.h>
47 #include "if_elreg.h"
48
49 DECLARE_DUMMY_MODULE(if_el);
50
51 /* For debugging convenience */
52 #ifdef EL_DEBUG
53 #define dprintf(x) printf x
54 #else
55 #define dprintf(x)
56 #endif
57
58 /* el_softc: per line info and status */
59 static struct el_softc {
60         struct arpcom arpcom;   /* Ethernet common */
61         u_short el_base;        /* Base I/O addr */
62         char el_pktbuf[EL_BUFSIZ];      /* Frame buffer */
63 } el_softc[NEL];
64
65 /* Prototypes */
66 static int el_attach(struct isa_device *);
67 static void el_init(void *);
68 static int el_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
69 static int el_probe(struct isa_device *);
70 static void el_start(struct ifnet *);
71 static void el_reset(void *);
72 static void el_watchdog(struct ifnet *);
73
74 static void el_stop(void *);
75 static int el_xmit(struct el_softc *,int);
76 static ointhand2_t elintr;
77 static __inline void elread(struct el_softc *,caddr_t,int);
78 static struct mbuf *elget(caddr_t,int,struct ifnet *);
79 static __inline void el_hardreset(void *);
80
81 /* isa_driver structure for autoconf */
82 struct isa_driver eldriver = {
83         el_probe, el_attach, "el"
84 };
85
86 /* Probe routine.  See if the card is there and at the right place. */
87 static int
88 el_probe(struct isa_device *idev)
89 {
90         struct el_softc *sc;
91         u_short base; /* Just for convenience */
92         u_char station_addr[ETHER_ADDR_LEN];
93         int i;
94
95         /* Grab some info for our structure */
96         sc = &el_softc[idev->id_unit];
97         sc->el_base = idev->id_iobase;
98         base = sc->el_base;
99
100         /* First check the base */
101         if((base < 0x280) || (base > 0x3f0)) {
102                 printf("el%d: ioaddr must be between 0x280 and 0x3f0\n",
103                         idev->id_unit);
104                 return(0);
105         }
106
107         /* Now attempt to grab the station address from the PROM
108          * and see if it contains the 3com vendor code.
109          */
110         dprintf(("Probing 3c501 at 0x%x...\n",base));
111
112         /* Reset the board */
113         dprintf(("Resetting board...\n"));
114         outb(base+EL_AC,EL_AC_RESET);
115         DELAY(5);
116         outb(base+EL_AC,0);
117         dprintf(("Reading station address...\n"));
118         /* Now read the address */
119         for(i=0;i<ETHER_ADDR_LEN;i++) {
120                 outb(base+EL_GPBL,i);
121                 station_addr[i] = inb(base+EL_EAW);
122         }
123         dprintf(("Address is %6D\n",station_addr, ":"));
124
125         /* If the vendor code is ok, return a 1.  We'll assume that
126          * whoever configured this system is right about the IRQ.
127          */
128         if((station_addr[0] != 0x02) || (station_addr[1] != 0x60)
129            || (station_addr[2] != 0x8c)) {
130                 dprintf(("Bad vendor code.\n"));
131                 return(0);
132         } else {
133                 dprintf(("Vendor code ok.\n"));
134                 /* Copy the station address into the arpcom structure */
135                 bcopy(station_addr,sc->arpcom.ac_enaddr,ETHER_ADDR_LEN);
136                 return(1);
137         }
138 }
139
140 /* Do a hardware reset of the 3c501.  Do not call until after el_probe()! */
141 static __inline void
142 el_hardreset(xsc)
143         void *xsc;
144 {
145         struct el_softc *sc = xsc;
146         int base;
147         int j;
148
149         base = sc->el_base;
150
151         /* First reset the board */
152         outb(base+EL_AC,EL_AC_RESET);
153         DELAY(5);
154         outb(base+EL_AC,0);
155
156         /* Then give it back its ethernet address.  Thanks to the mach
157          * source code for this undocumented goodie...
158          */
159         for(j=0;j<ETHER_ADDR_LEN;j++)
160                 outb(base+j,sc->arpcom.ac_enaddr[j]);
161 }
162
163 /* Attach the interface to the kernel data structures.  By the time
164  * this is called, we know that the card exists at the given I/O address.
165  * We still assume that the IRQ given is correct.
166  */
167 static int
168 el_attach(struct isa_device *idev)
169 {
170         struct el_softc *sc;
171         struct ifnet *ifp;
172         u_short base;
173
174         dprintf(("Attaching el%d...\n",idev->id_unit));
175
176         /* Get things pointing to the right places. */
177         idev->id_ointr = elintr;
178         sc = &el_softc[idev->id_unit];
179         ifp = &sc->arpcom.ac_if;
180         base = sc->el_base;
181
182         /* Now reset the board */
183         dprintf(("Resetting board...\n"));
184         el_hardreset(sc);
185
186         /* Initialize ifnet structure */
187         ifp->if_softc = sc;
188         if_initname(ifp, "el", idev->id_unit);
189         ifp->if_mtu = ETHERMTU;
190         ifp->if_output = ether_output;
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
197         /* Now we can attach the interface */
198         dprintf(("Attaching interface...\n"));
199         ether_ifattach(ifp, sc->arpcom.ac_enaddr);
200
201         dprintf(("el_attach() finished.\n"));
202         return(1);
203 }
204
205 /* This routine resets the interface. */
206 static void 
207 el_reset(xsc)
208         void *xsc;
209 {
210         struct el_softc *sc = xsc;
211         int s;
212
213         dprintf(("elreset()\n"));
214         s = splimp();
215         el_stop(sc);
216         el_init(sc);
217         splx(s);
218 }
219
220 static void el_stop(xsc)
221         void *xsc;
222 {
223         struct el_softc *sc = xsc;
224
225         outb(sc->el_base+EL_AC,0);
226 }
227
228 /* Initialize interface.  */
229 static void 
230 el_init(xsc)
231         void *xsc;
232 {
233         struct el_softc *sc = xsc;
234         struct ifnet *ifp;
235         int s;
236         u_short base;
237
238         /* Set up pointers */
239         ifp = &sc->arpcom.ac_if;
240         base = sc->el_base;
241
242         /* If address not known, do nothing. */
243         if(TAILQ_EMPTY(&ifp->if_addrhead)) /* XXX unlikely */
244                 return;
245
246         s = splimp();
247
248         /* First, reset the board. */
249         dprintf(("Resetting board...\n"));
250         el_hardreset(sc);
251
252         /* Configure rx */
253         dprintf(("Configuring rx...\n"));
254         if(ifp->if_flags & IFF_PROMISC)
255                 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
256         else
257                 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
258         outb(base+EL_RBC,0);
259
260         /* Configure TX */
261         dprintf(("Configuring tx...\n"));
262         outb(base+EL_TXC,0);
263
264         /* Start reception */
265         dprintf(("Starting reception...\n"));
266         outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
267
268         /* Set flags appropriately */
269         ifp->if_flags |= IFF_RUNNING;
270         ifp->if_flags &= ~IFF_OACTIVE;
271
272         /* And start output. */
273         el_start(ifp);
274
275         splx(s);
276 }
277
278 /* Start output on interface.  Get datagrams from the queue and output
279  * them, giving the receiver a chance between datagrams.  Call only
280  * from splimp or interrupt level!
281  */
282 static void
283 el_start(struct ifnet *ifp)
284 {
285         struct el_softc *sc;
286         u_short base;
287         struct mbuf *m, *m0;
288         int s, i, len, retries, done;
289
290         /* Get things pointing in the right directions */
291         sc = ifp->if_softc;
292         base = sc->el_base;
293
294         dprintf(("el_start()...\n"));
295         s = splimp();
296
297         /* Don't do anything if output is active */
298         if(sc->arpcom.ac_if.if_flags & IFF_OACTIVE)
299                 return;
300         sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
301
302         /* The main loop.  They warned me against endless loops, but
303          * would I listen?  NOOO....
304          */
305         while(1) {
306                 /* Dequeue the next datagram */
307                 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd,m0);
308
309                 /* If there's nothing to send, return. */
310                 if(m0 == NULL) {
311                         sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
312                         splx(s);
313                         return;
314                 }
315
316                 /* Disable the receiver */
317                 outb(base+EL_AC,EL_AC_HOST);
318                 outb(base+EL_RBC,0);
319
320                 /* Copy the datagram to the buffer. */
321                 len = 0;
322                 for(m = m0; m != NULL; m = m->m_next) {
323                         if(m->m_len == 0)
324                                 continue;
325                         bcopy(mtod(m,caddr_t),sc->el_pktbuf+len,m->m_len);
326                         len += m->m_len;
327                 }
328                 m_freem(m0);
329
330                 len = max(len,ETHER_MIN_LEN);
331
332                 /* Give the packet to the bpf, if any */
333                 if(sc->arpcom.ac_if.if_bpf)
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 SIOCSIFADDR:
606         case SIOCGIFADDR:
607         case SIOCSIFMTU:
608                 error = ether_ioctl(ifp, command, data);
609                 break;
610
611         case SIOCSIFFLAGS:
612                 /*
613                  * If interface is marked down and it is running, then stop it
614                  */
615                 if (((ifp->if_flags & IFF_UP) == 0) &&
616                     (ifp->if_flags & IFF_RUNNING)) {
617                         el_stop(ifp->if_softc);
618                         ifp->if_flags &= ~IFF_RUNNING;
619                 } else {
620                 /*
621                  * If interface is marked up and it is stopped, then start it
622                  */
623                         if ((ifp->if_flags & IFF_UP) &&
624                             ((ifp->if_flags & IFF_RUNNING) == 0))
625                                 el_init(ifp->if_softc);
626                 }
627                 break;
628         default:
629                 error = EINVAL;
630         }
631         (void) splx(s);
632         return (error);
633 }
634
635 /* Device timeout routine */
636 static void
637 el_watchdog(struct ifnet *ifp)
638 {
639         log(LOG_ERR,"%s: device timeout\n", ifp->if_xname);
640         ifp->if_oerrors++;
641         el_reset(ifp->if_softc);
642 }