gold: Fix hardcoded library search path
[dragonfly.git] / sys / dev / netif / el / if_el.c
... / ...
CommitLineData
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.24 2008/09/07 07:48:29 swildner 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
52DECLARE_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 */
62static 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 */
69static int el_attach(struct isa_device *);
70static void el_init(void *);
71static int el_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
72static int el_probe(struct isa_device *);
73static void el_start(struct ifnet *);
74static void el_reset(void *);
75static void el_watchdog(struct ifnet *);
76
77static void el_stop(void *);
78static int el_xmit(struct el_softc *,int);
79static void elintr(void *);
80static __inline void elread(struct el_softc *,caddr_t,int);
81static struct mbuf *elget(caddr_t,int,struct ifnet *);
82static __inline void el_hardreset(void *);
83
84/* isa_driver structure for autoconf */
85struct isa_driver eldriver = {
86 el_probe, el_attach, "el"
87};
88
89static struct lwkt_serialize el_serializer;
90
91/* Probe routine. See if the card is there and at the right place. */
92static int
93el_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()! */
148static __inline void
149el_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 */
173static int
174el_attach(struct isa_device *idev)
175{
176 struct el_softc *sc;
177 struct ifnet *ifp;
178
179 dprintf(("Attaching el%d...\n",idev->id_unit));
180
181 /* Get things pointing to the right places. */
182 idev->id_intr = (inthand2_t *)elintr;
183 sc = &el_softc[idev->id_unit];
184 ifp = &sc->arpcom.ac_if;
185
186 /* Now reset the board */
187 dprintf(("Resetting board...\n"));
188 el_hardreset(sc);
189
190 /* Initialize ifnet structure */
191 ifp->if_softc = sc;
192 if_initname(ifp, "el", idev->id_unit);
193 ifp->if_mtu = ETHERMTU;
194 ifp->if_start = el_start;
195 ifp->if_ioctl = el_ioctl;
196 ifp->if_watchdog = el_watchdog;
197 ifp->if_init = el_init;
198 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX);
199 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
200 ifq_set_ready(&ifp->if_snd);
201
202 /* Now we can attach the interface */
203 dprintf(("Attaching interface...\n"));
204 ether_ifattach(ifp, sc->arpcom.ac_enaddr, &el_serializer);
205
206 dprintf(("el_attach() finished.\n"));
207 return(1);
208}
209
210/* This routine resets the interface. */
211static void
212el_reset(void *xsc)
213{
214 struct el_softc *sc = xsc;
215
216 dprintf(("elreset()\n"));
217 el_stop(sc);
218 el_init(sc);
219}
220
221static void
222el_stop(void *xsc)
223{
224 struct el_softc *sc = xsc;
225
226 outb(sc->el_base+EL_AC,0);
227}
228
229/* Initialize interface. */
230static void
231el_init(void *xsc)
232{
233 struct el_softc *sc = xsc;
234 struct ifnet *ifp = &sc->arpcom.ac_if;
235 u_short base = sc->el_base;
236
237
238 /* First, reset the board. */
239 dprintf(("Resetting board...\n"));
240 el_hardreset(sc);
241
242 /* Configure rx */
243 dprintf(("Configuring rx...\n"));
244 if(ifp->if_flags & IFF_PROMISC)
245 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
246 else
247 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
248 outb(base+EL_RBC,0);
249
250 /* Configure TX */
251 dprintf(("Configuring tx...\n"));
252 outb(base+EL_TXC,0);
253
254 /* Start reception */
255 dprintf(("Starting reception...\n"));
256 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
257
258 /* Set flags appropriately */
259 ifp->if_flags |= IFF_RUNNING;
260 ifp->if_flags &= ~IFF_OACTIVE;
261
262 /* And start output. */
263 el_start(ifp);
264}
265
266/* Start output on interface. Get datagrams from the queue and output
267 * them, giving the receiver a chance between datagrams. Call only
268 * from splimp or interrupt level!
269 */
270static void
271el_start(struct ifnet *ifp)
272{
273 struct el_softc *sc;
274 u_short base;
275 struct mbuf *m, *m0;
276 int i, len, retries, done;
277
278 /* Get things pointing in the right directions */
279 sc = ifp->if_softc;
280 base = sc->el_base;
281
282 dprintf(("el_start()...\n"));
283
284 /* Don't do anything if output is active */
285 if (ifp->if_flags & IFF_OACTIVE)
286 return;
287 ifp->if_flags |= IFF_OACTIVE;
288
289 /* The main loop. They warned me against endless loops, but
290 * would I listen? NOOO....
291 */
292 while(1) {
293 /* Dequeue the next datagram */
294 m0 = ifq_dequeue(&ifp->if_snd, NULL);
295
296 /* If there's nothing to send, return. */
297 if(m0 == NULL) {
298 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
299 return;
300 }
301
302 /* Disable the receiver */
303 outb(base+EL_AC,EL_AC_HOST);
304 outb(base+EL_RBC,0);
305
306 /* Copy the datagram to the buffer. */
307 len = 0;
308 for(m = m0; m != NULL; m = m->m_next) {
309 if(m->m_len == 0)
310 continue;
311 bcopy(mtod(m,caddr_t),sc->el_pktbuf+len,m->m_len);
312 len += m->m_len;
313 }
314 m_freem(m0);
315
316 len = max(len,ETHER_MIN_LEN);
317
318 BPF_TAP(&sc->arpcom.ac_if, sc->el_pktbuf, len);
319
320 /* Transfer datagram to board */
321 dprintf(("el: xfr pkt length=%d...\n",len));
322 i = EL_BUFSIZ - len;
323 outb(base+EL_GPBL,(i & 0xff));
324 outb(base+EL_GPBH,((i>>8)&0xff));
325 outsb(base+EL_BUF,sc->el_pktbuf,len);
326
327 /* Now transmit the datagram */
328 retries=0;
329 done=0;
330 while(!done) {
331 if(el_xmit(sc,len)) { /* Something went wrong */
332 done = -1;
333 break;
334 }
335 /* Check out status */
336 i = inb(base+EL_TXS);
337 dprintf(("tx status=0x%x\n",i));
338 if(!(i & EL_TXS_READY)) {
339 dprintf(("el: err txs=%x\n",i));
340 sc->arpcom.ac_if.if_oerrors++;
341 if(i & (EL_TXS_COLL|EL_TXS_COLL16)) {
342 if((!(i & EL_TXC_DCOLL16)) && retries < 15) {
343 retries++;
344 outb(base+EL_AC,EL_AC_HOST);
345 }
346 }
347 else
348 done = 1;
349 }
350 else {
351 sc->arpcom.ac_if.if_opackets++;
352 done = 1;
353 }
354 }
355 if(done == -1) /* Packet not transmitted */
356 continue;
357
358 /* Now give the card a chance to receive.
359 * Gotta love 3c501s...
360 */
361 inb(base+EL_AS);
362 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
363 }
364}
365
366/* This function actually attempts to transmit a datagram downloaded
367 * to the board. Call at splimp or interrupt, after downloading data!
368 * Returns 0 on success, non-0 on failure
369 */
370static int
371el_xmit(struct el_softc *sc,int len)
372{
373 int gpl;
374 int i;
375
376 gpl = EL_BUFSIZ - len;
377 dprintf(("el: xmit..."));
378 outb((sc->el_base)+EL_GPBL,(gpl & 0xff));
379 outb((sc->el_base)+EL_GPBH,((gpl>>8)&0xff));
380 outb((sc->el_base)+EL_AC,EL_AC_TXFRX);
381 i = 20000;
382 while((inb((sc->el_base)+EL_AS) & EL_AS_TXBUSY) && (i>0))
383 i--;
384 if(i == 0) {
385 dprintf(("tx not ready\n"));
386 sc->arpcom.ac_if.if_oerrors++;
387 return(-1);
388 }
389 dprintf(("%d cycles.\n",(20000-i)));
390 return(0);
391}
392
393/* Pass a packet up to the higher levels. */
394static __inline void
395elread(struct el_softc *sc,caddr_t buf,int len)
396{
397 struct mbuf *m;
398
399 /*
400 * Put packet into an mbuf chain
401 */
402 m = elget(buf,len,&sc->arpcom.ac_if);
403 if(m)
404 sc->arpcom.ac_if.if_input(&sc->arpcom.ac_if, m);
405}
406
407/* controller interrupt */
408static void
409elintr(void *arg)
410{
411 int unit = (int)arg;
412 struct el_softc *sc;
413 int base;
414 int stat, rxstat, len, done;
415
416 lwkt_serialize_enter(&el_serializer);
417
418 /* Get things pointing properly */
419 sc = &el_softc[unit];
420 base = sc->el_base;
421
422 dprintf(("elintr: "));
423
424 /* Check board status */
425 stat = inb(base+EL_AS);
426 if(stat & EL_AS_RXBUSY) {
427 inb(base+EL_RXC);
428 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
429 lwkt_serialize_exit(&el_serializer);
430 return;
431 }
432
433 done = 0;
434 while(!done) {
435 rxstat = inb(base+EL_RXS);
436 if(rxstat & EL_RXS_STALE) {
437 inb(base+EL_RXC);
438 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
439 lwkt_serialize_exit(&el_serializer);
440 return;
441 }
442
443 /* If there's an overflow, reinit the board. */
444 if(!(rxstat & EL_RXS_NOFLOW)) {
445 dprintf(("overflow.\n"));
446 el_hardreset(sc);
447 /* Put board back into receive mode */
448 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
449 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
450 else
451 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
452 inb(base+EL_AS);
453 outb(base+EL_RBC,0);
454 inb(base+EL_RXC);
455 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
456 lwkt_serialize_exit(&el_serializer);
457 return;
458 }
459
460 /* Incoming packet */
461 len = inb(base+EL_RBL);
462 len |= inb(base+EL_RBH) << 8;
463 dprintf(("receive len=%d rxstat=%x ",len,rxstat));
464 outb(base+EL_AC,EL_AC_HOST);
465
466 /* If packet too short or too long, restore rx mode and return
467 */
468 if((len <= sizeof(struct ether_header)) || (len > ETHER_MAX_LEN)) {
469 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
470 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
471 else
472 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
473 inb(base+EL_AS);
474 outb(base+EL_RBC,0);
475 inb(base+EL_RXC);
476 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
477 lwkt_serialize_exit(&el_serializer);
478 return;
479 }
480
481 sc->arpcom.ac_if.if_ipackets++;
482
483 /* Copy the data into our buffer */
484 outb(base+EL_GPBL,0);
485 outb(base+EL_GPBH,0);
486 insb(base+EL_BUF,sc->el_pktbuf,len);
487 outb(base+EL_RBC,0);
488 outb(base+EL_AC,EL_AC_RX);
489 dprintf(("%6D-->",sc->el_pktbuf+6,":"));
490 dprintf(("%6D\n",sc->el_pktbuf,":"));
491
492 /* Pass data up to upper levels */
493 elread(sc,(caddr_t)(sc->el_pktbuf),len);
494
495 /* Is there another packet? */
496 stat = inb(base+EL_AS);
497
498 /* If so, do it all again (i.e. don't set done to 1) */
499 if(!(stat & EL_AS_RXBUSY))
500 dprintf(("<rescan> "));
501 else
502 done = 1;
503 }
504
505 inb(base+EL_RXC);
506 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
507 lwkt_serialize_exit(&el_serializer);
508}
509
510/*
511 * Pull read data off a interface.
512 * Len is length of data, with local net header stripped.
513 */
514static struct mbuf *
515elget(caddr_t buf, int totlen, struct ifnet *ifp)
516{
517 struct mbuf *top, **mp, *m;
518 int len;
519 caddr_t cp;
520 char *epkt;
521
522 cp = buf;
523 epkt = cp + totlen;
524
525 MGETHDR(m, MB_DONTWAIT, MT_DATA);
526 if (m == 0)
527 return (0);
528 m->m_pkthdr.len = totlen;
529 m->m_len = MHLEN;
530 top = 0;
531 mp = &top;
532 while (totlen > 0) {
533 if (top) {
534 MGET(m, MB_DONTWAIT, MT_DATA);
535 if (m == 0) {
536 m_freem(top);
537 return (0);
538 }
539 m->m_len = MLEN;
540 }
541 len = min(totlen, epkt - cp);
542 if (len >= MINCLSIZE) {
543 MCLGET(m, MB_DONTWAIT);
544 if (m->m_flags & M_EXT)
545 m->m_len = len = min(len, MCLBYTES);
546 else
547 len = m->m_len;
548 } else {
549 /*
550 * Place initial small packet/header at end of mbuf.
551 */
552 if (len < m->m_len) {
553 if (top == 0 && len + max_linkhdr <= m->m_len)
554 m->m_data += max_linkhdr;
555 m->m_len = len;
556 } else
557 len = m->m_len;
558 }
559 bcopy(cp, mtod(m, caddr_t), (unsigned)len);
560 cp += len;
561 *mp = m;
562 mp = &m->m_next;
563 totlen -= len;
564 if (cp == epkt)
565 cp = buf;
566 }
567 return (top);
568}
569
570/*
571 * Process an ioctl request. This code needs some work - it looks
572 * pretty ugly.
573 */
574static int
575el_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
576{
577 int error = 0;
578
579 switch (command) {
580 case SIOCSIFFLAGS:
581 /*
582 * If interface is marked down and it is running, then stop it
583 */
584 if (((ifp->if_flags & IFF_UP) == 0) &&
585 (ifp->if_flags & IFF_RUNNING)) {
586 el_stop(ifp->if_softc);
587 ifp->if_flags &= ~IFF_RUNNING;
588 } else {
589 /*
590 * If interface is marked up and it is stopped, then start it
591 */
592 if ((ifp->if_flags & IFF_UP) &&
593 ((ifp->if_flags & IFF_RUNNING) == 0))
594 el_init(ifp->if_softc);
595 }
596 break;
597 default:
598 error = ether_ioctl(ifp, command, data);
599 break;
600 }
601 return (error);
602}
603
604/* Device timeout routine */
605static void
606el_watchdog(struct ifnet *ifp)
607{
608 log(LOG_ERR,"%s: device timeout\n", ifp->if_xname);
609 ifp->if_oerrors++;
610 el_reset(ifp->if_softc);
611}