gold: Fix hardcoded library search path
[dragonfly.git] / sys / dev / netif / el / if_el.c
CommitLineData
984263bc
MD
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 $
1b0472e3 10 * $DragonFly: src/sys/dev/netif/el/if_el.c,v 1.24 2008/09/07 07:48:29 swildner Exp $
984263bc
MD
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 */
1f2de5d4 23#include "use_el.h"
984263bc
MD
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>
32832096
MD
33#include <sys/linker_set.h>
34#include <sys/module.h>
78195a76 35#include <sys/serialize.h>
4440f38a 36#include <sys/thread2.h>
984263bc
MD
37
38#include <net/ethernet.h>
39#include <net/if.h>
68adc7bf 40#include <net/ifq_var.h>
984263bc
MD
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
21ce0dfa 49#include <bus/isa/isa_device.h>
1f2de5d4 50#include "if_elreg.h"
984263bc 51
32832096
MD
52DECLARE_DUMMY_MODULE(if_el);
53
984263bc
MD
54/* For debugging convenience */
55#ifdef EL_DEBUG
e3869ec7 56#define dprintf(x) kprintf x
984263bc
MD
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 *);
9974b71d 71static int el_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
984263bc
MD
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);
1b51b0fa 79static void elintr(void *);
984263bc
MD
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
78195a76
MD
89static struct lwkt_serialize el_serializer;
90
984263bc
MD
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
78195a76
MD
100 lwkt_serialize_init(&el_serializer);
101
984263bc
MD
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)) {
e3869ec7 109 kprintf("el%d: ioaddr must be between 0x280 and 0x3f0\n",
984263bc
MD
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
7b9f668c 149el_hardreset(void *xsc)
984263bc 150{
c9faf524
RG
151 struct el_softc *sc = xsc;
152 int base;
153 int j;
984263bc
MD
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;
984263bc
MD
178
179 dprintf(("Attaching el%d...\n",idev->id_unit));
180
181 /* Get things pointing to the right places. */
1b51b0fa 182 idev->id_intr = (inthand2_t *)elintr;
984263bc
MD
183 sc = &el_softc[idev->id_unit];
184 ifp = &sc->arpcom.ac_if;
984263bc
MD
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;
cdb7d804 192 if_initname(ifp, "el", idev->id_unit);
984263bc 193 ifp->if_mtu = ETHERMTU;
984263bc
MD
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);
68adc7bf
JS
199 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
200 ifq_set_ready(&ifp->if_snd);
984263bc
MD
201
202 /* Now we can attach the interface */
203 dprintf(("Attaching interface...\n"));
78195a76 204 ether_ifattach(ifp, sc->arpcom.ac_enaddr, &el_serializer);
984263bc 205
984263bc
MD
206 dprintf(("el_attach() finished.\n"));
207 return(1);
208}
209
210/* This routine resets the interface. */
211static void
7b9f668c 212el_reset(void *xsc)
984263bc
MD
213{
214 struct el_softc *sc = xsc;
984263bc
MD
215
216 dprintf(("elreset()\n"));
984263bc
MD
217 el_stop(sc);
218 el_init(sc);
984263bc
MD
219}
220
7b9f668c
SW
221static void
222el_stop(void *xsc)
984263bc
MD
223{
224 struct el_softc *sc = xsc;
225
226 outb(sc->el_base+EL_AC,0);
227}
228
229/* Initialize interface. */
230static void
7b9f668c 231el_init(void *xsc)
984263bc
MD
232{
233 struct el_softc *sc = xsc;
4440f38a
JS
234 struct ifnet *ifp = &sc->arpcom.ac_if;
235 u_short base = sc->el_base;
984263bc 236
984263bc
MD
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);
984263bc
MD
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;
4440f38a 276 int i, len, retries, done;
984263bc
MD
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"));
984263bc
MD
283
284 /* Don't do anything if output is active */
4440f38a 285 if (ifp->if_flags & IFF_OACTIVE)
984263bc 286 return;
4440f38a 287 ifp->if_flags |= IFF_OACTIVE;
984263bc
MD
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 */
d2c71fa0 294 m0 = ifq_dequeue(&ifp->if_snd, NULL);
984263bc
MD
295
296 /* If there's nothing to send, return. */
297 if(m0 == NULL) {
298 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
984263bc
MD
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
7600679e 318 BPF_TAP(&sc->arpcom.ac_if, sc->el_pktbuf, len);
984263bc
MD
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 */
7b9f668c 361 inb(base+EL_AS);
984263bc 362 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
984263bc
MD
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{
984263bc
MD
397 struct mbuf *m;
398
984263bc
MD
399 /*
400 * Put packet into an mbuf chain
401 */
402 m = elget(buf,len,&sc->arpcom.ac_if);
78195a76
MD
403 if(m)
404 sc->arpcom.ac_if.if_input(&sc->arpcom.ac_if, m);
984263bc
MD
405}
406
407/* controller interrupt */
408static void
477d3c1c 409elintr(void *arg)
984263bc 410{
477d3c1c 411 int unit = (int)arg;
c9faf524
RG
412 struct el_softc *sc;
413 int base;
984263bc
MD
414 int stat, rxstat, len, done;
415
78195a76
MD
416 lwkt_serialize_enter(&el_serializer);
417
984263bc
MD
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) {
7b9f668c 427 inb(base+EL_RXC);
984263bc 428 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
78195a76 429 lwkt_serialize_exit(&el_serializer);
984263bc
MD
430 return;
431 }
432
433 done = 0;
434 while(!done) {
435 rxstat = inb(base+EL_RXS);
436 if(rxstat & EL_RXS_STALE) {
7b9f668c 437 inb(base+EL_RXC);
984263bc 438 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
78195a76 439 lwkt_serialize_exit(&el_serializer);
984263bc
MD
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));
7b9f668c 452 inb(base+EL_AS);
984263bc 453 outb(base+EL_RBC,0);
7b9f668c 454 inb(base+EL_RXC);
984263bc 455 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
78195a76 456 lwkt_serialize_exit(&el_serializer);
984263bc
MD
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));
7b9f668c 473 inb(base+EL_AS);
984263bc 474 outb(base+EL_RBC,0);
7b9f668c 475 inb(base+EL_RXC);
984263bc 476 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
78195a76 477 lwkt_serialize_exit(&el_serializer);
984263bc
MD
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 */
984263bc
MD
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
7b9f668c 505 inb(base+EL_RXC);
984263bc 506 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
78195a76 507 lwkt_serialize_exit(&el_serializer);
984263bc
MD
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 *
7b9f668c 515elget(caddr_t buf, int totlen, struct ifnet *ifp)
984263bc
MD
516{
517 struct mbuf *top, **mp, *m;
518 int len;
c9faf524 519 caddr_t cp;
984263bc
MD
520 char *epkt;
521
984263bc
MD
522 cp = buf;
523 epkt = cp + totlen;
524
74f1caca 525 MGETHDR(m, MB_DONTWAIT, MT_DATA);
984263bc
MD
526 if (m == 0)
527 return (0);
984263bc
MD
528 m->m_pkthdr.len = totlen;
529 m->m_len = MHLEN;
530 top = 0;
531 mp = &top;
532 while (totlen > 0) {
533 if (top) {
74f1caca 534 MGET(m, MB_DONTWAIT, MT_DATA);
984263bc
MD
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) {
74f1caca 543 MCLGET(m, MB_DONTWAIT);
984263bc
MD
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
9974b71d 575el_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
984263bc 576{
4440f38a 577 int error = 0;
984263bc 578
984263bc 579 switch (command) {
984263bc
MD
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:
4cde4dd5
JS
598 error = ether_ioctl(ifp, command, data);
599 break;
984263bc 600 }
984263bc
MD
601 return (error);
602}
603
604/* Device timeout routine */
605static void
606el_watchdog(struct ifnet *ifp)
607{
cdb7d804 608 log(LOG_ERR,"%s: device timeout\n", ifp->if_xname);
984263bc
MD
609 ifp->if_oerrors++;
610 el_reset(ifp->if_softc);
611}