Rename printf -> kprintf in sys/ and add some defines where necessary
[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 $
e3869ec7 10 * $DragonFly: src/sys/dev/netif/el/if_el.c,v 1.22 2006/12/22 23:26:19 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
1f2de5d4
MD
49#include <bus/isa/i386/isa_device.h>
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;
178 u_short base;
179
180 dprintf(("Attaching el%d...\n",idev->id_unit));
181
182 /* Get things pointing to the right places. */
1b51b0fa 183 idev->id_intr = (inthand2_t *)elintr;
984263bc
MD
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;
cdb7d804 194 if_initname(ifp, "el", idev->id_unit);
984263bc 195 ifp->if_mtu = ETHERMTU;
984263bc
MD
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);
68adc7bf
JS
201 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
202 ifq_set_ready(&ifp->if_snd);
984263bc
MD
203
204 /* Now we can attach the interface */
205 dprintf(("Attaching interface...\n"));
78195a76 206 ether_ifattach(ifp, sc->arpcom.ac_enaddr, &el_serializer);
984263bc 207
984263bc
MD
208 dprintf(("el_attach() finished.\n"));
209 return(1);
210}
211
212/* This routine resets the interface. */
213static void
7b9f668c 214el_reset(void *xsc)
984263bc
MD
215{
216 struct el_softc *sc = xsc;
984263bc
MD
217
218 dprintf(("elreset()\n"));
984263bc
MD
219 el_stop(sc);
220 el_init(sc);
984263bc
MD
221}
222
7b9f668c
SW
223static void
224el_stop(void *xsc)
984263bc
MD
225{
226 struct el_softc *sc = xsc;
227
228 outb(sc->el_base+EL_AC,0);
229}
230
231/* Initialize interface. */
232static void
7b9f668c 233el_init(void *xsc)
984263bc
MD
234{
235 struct el_softc *sc = xsc;
4440f38a
JS
236 struct ifnet *ifp = &sc->arpcom.ac_if;
237 u_short base = sc->el_base;
984263bc 238
984263bc
MD
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);
984263bc
MD
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 */
272static void
273el_start(struct ifnet *ifp)
274{
275 struct el_softc *sc;
276 u_short base;
277 struct mbuf *m, *m0;
4440f38a 278 int i, len, retries, done;
984263bc
MD
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"));
984263bc
MD
285
286 /* Don't do anything if output is active */
4440f38a 287 if (ifp->if_flags & IFF_OACTIVE)
984263bc 288 return;
4440f38a 289 ifp->if_flags |= IFF_OACTIVE;
984263bc
MD
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 */
d2c71fa0 296 m0 = ifq_dequeue(&ifp->if_snd, NULL);
984263bc
MD
297
298 /* If there's nothing to send, return. */
299 if(m0 == NULL) {
300 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
984263bc
MD
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
7600679e 320 BPF_TAP(&sc->arpcom.ac_if, sc->el_pktbuf, len);
984263bc
MD
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 */
7b9f668c 363 inb(base+EL_AS);
984263bc 364 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
984263bc
MD
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 */
372static int
373el_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. */
396static __inline void
397elread(struct el_softc *sc,caddr_t buf,int len)
398{
984263bc
MD
399 struct mbuf *m;
400
984263bc
MD
401 /*
402 * Put packet into an mbuf chain
403 */
404 m = elget(buf,len,&sc->arpcom.ac_if);
78195a76
MD
405 if(m)
406 sc->arpcom.ac_if.if_input(&sc->arpcom.ac_if, m);
984263bc
MD
407}
408
409/* controller interrupt */
410static void
477d3c1c 411elintr(void *arg)
984263bc 412{
477d3c1c 413 int unit = (int)arg;
c9faf524
RG
414 struct el_softc *sc;
415 int base;
984263bc
MD
416 int stat, rxstat, len, done;
417
78195a76
MD
418 lwkt_serialize_enter(&el_serializer);
419
984263bc
MD
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) {
7b9f668c 429 inb(base+EL_RXC);
984263bc 430 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
78195a76 431 lwkt_serialize_exit(&el_serializer);
984263bc
MD
432 return;
433 }
434
435 done = 0;
436 while(!done) {
437 rxstat = inb(base+EL_RXS);
438 if(rxstat & EL_RXS_STALE) {
7b9f668c 439 inb(base+EL_RXC);
984263bc 440 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
78195a76 441 lwkt_serialize_exit(&el_serializer);
984263bc
MD
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));
7b9f668c 454 inb(base+EL_AS);
984263bc 455 outb(base+EL_RBC,0);
7b9f668c 456 inb(base+EL_RXC);
984263bc 457 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
78195a76 458 lwkt_serialize_exit(&el_serializer);
984263bc
MD
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));
7b9f668c 475 inb(base+EL_AS);
984263bc 476 outb(base+EL_RBC,0);
7b9f668c 477 inb(base+EL_RXC);
984263bc 478 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
78195a76 479 lwkt_serialize_exit(&el_serializer);
984263bc
MD
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 */
984263bc
MD
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
7b9f668c 507 inb(base+EL_RXC);
984263bc 508 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
78195a76 509 lwkt_serialize_exit(&el_serializer);
984263bc
MD
510}
511
512/*
513 * Pull read data off a interface.
514 * Len is length of data, with local net header stripped.
515 */
516static struct mbuf *
7b9f668c 517elget(caddr_t buf, int totlen, struct ifnet *ifp)
984263bc
MD
518{
519 struct mbuf *top, **mp, *m;
520 int len;
c9faf524 521 caddr_t cp;
984263bc
MD
522 char *epkt;
523
984263bc
MD
524 cp = buf;
525 epkt = cp + totlen;
526
74f1caca 527 MGETHDR(m, MB_DONTWAIT, MT_DATA);
984263bc
MD
528 if (m == 0)
529 return (0);
984263bc
MD
530 m->m_pkthdr.len = totlen;
531 m->m_len = MHLEN;
532 top = 0;
533 mp = &top;
534 while (totlen > 0) {
535 if (top) {
74f1caca 536 MGET(m, MB_DONTWAIT, MT_DATA);
984263bc
MD
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) {
74f1caca 545 MCLGET(m, MB_DONTWAIT);
984263bc
MD
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 */
576static int
9974b71d 577el_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
984263bc 578{
4440f38a 579 int error = 0;
984263bc 580
984263bc 581 switch (command) {
984263bc
MD
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:
4cde4dd5
JS
600 error = ether_ioctl(ifp, command, data);
601 break;
984263bc 602 }
984263bc
MD
603 return (error);
604}
605
606/* Device timeout routine */
607static void
608el_watchdog(struct ifnet *ifp)
609{
cdb7d804 610 log(LOG_ERR,"%s: device timeout\n", ifp->if_xname);
984263bc
MD
611 ifp->if_oerrors++;
612 el_reset(ifp->if_softc);
613}