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