No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / sun2 / dev / if_ec.c
blobdff67818836d50b69c9f2b580202c16aa5a9be08
1 /* $NetBSD: if_ec.c,v 1.15 2008/07/06 13:29:50 tsutsui Exp $ */
3 /*
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthew Fredette.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * 3Com 3C400 device driver
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: if_ec.c,v 1.15 2008/07/06 13:29:50 tsutsui Exp $");
39 #include "opt_inet.h"
40 #include "opt_ns.h"
41 #include "bpfilter.h"
42 #include "rnd.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/ioctl.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/syslog.h>
51 #include <sys/device.h>
52 #include <sys/endian.h>
53 #if NRND > 0
54 #include <sys/rnd.h>
55 #endif
57 #include <net/if.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
61 #include <net/if_ether.h>
62 #include <net/if_media.h>
64 #ifdef INET
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip.h>
69 #include <netinet/if_inarp.h>
70 #endif
72 #ifdef NS
73 #include <netns/ns.h>
74 #include <netns/ns_if.h>
75 #endif
77 #if NBPFILTER > 0
78 #include <net/bpf.h>
79 #include <net/bpfdesc.h>
80 #endif
82 #include <machine/cpu.h>
83 #include <machine/autoconf.h>
84 #include <machine/idprom.h>
85 #include <machine/bus.h>
86 #include <machine/intr.h>
88 #include <sun2/dev/if_ecreg.h>
91 * Interface softc.
93 struct ec_softc {
94 device_t sc_dev;
95 void *sc_ih;
97 struct ethercom sc_ethercom; /* ethernet common */
98 struct ifmedia sc_media; /* our supported media */
100 bus_space_tag_t sc_iot; /* bus space tag */
101 bus_space_handle_t sc_ioh; /* bus space handle */
103 u_char sc_jammed; /* nonzero if the net is jammed */
104 u_char sc_colliding; /* nonzero if the net is colliding */
105 uint32_t sc_backoff_seed; /* seed for the backoff PRNG */
107 #if NRND > 0
108 rndsource_element_t rnd_source;
109 #endif
112 /* Macros to read and write the CSR. */
113 #define ECREG_CSR_RD bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECREG_CSR)
114 #define ECREG_CSR_WR(val) bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_CSR, val)
116 /* After this many collisions, the packet is dropped. */
117 #define EC_COLLISIONS_JAMMED 16
120 * Various constants used in the backoff pseudorandom
121 * number generator.
123 #define EC_BACKOFF_PRNG_COLL_MAX 10
124 #define EC_BACKOFF_PRNG_MUL 1103515245
125 #define EC_BACKOFF_PRNG_ADD 12345
126 #define EC_BACKOFF_PRNG_MASK 0x7fffffff
129 * Prototypes
131 int ec_intr(void *);
132 void ec_reset(struct ifnet *);
133 int ec_init(struct ifnet *);
134 int ec_ioctl(struct ifnet *, u_long, void *);
135 void ec_watchdog(struct ifnet *);
136 void ec_start(struct ifnet *);
138 void ec_recv(struct ec_softc *, int);
139 void ec_coll(struct ec_softc *);
140 void ec_copyin(struct ec_softc *, void *, int, size_t);
141 void ec_copyout(struct ec_softc *, const void *, int, size_t);
143 int ec_mediachange(struct ifnet *);
144 void ec_mediastatus(struct ifnet *, struct ifmediareq *);
146 int ec_match(device_t, cfdata_t, void *);
147 void ec_attach(device_t, device_t, void *);
149 CFATTACH_DECL_NEW(ec, sizeof(struct ec_softc),
150 ec_match, ec_attach, NULL, NULL);
153 * Copy board memory to kernel.
155 void
156 ec_copyin(struct ec_softc *sc, void *p, int offset, size_t size)
159 bus_space_copyin(sc->sc_iot, sc->sc_ioh, offset, p, size);
163 * Copy from kernel space to board memory.
165 void
166 ec_copyout(struct ec_softc *sc, const void *p, int offset, size_t size)
169 bus_space_copyout(sc->sc_iot, sc->sc_ioh, offset, p, size);
172 int
173 ec_match(device_t parent, cfdata_t cf, void *aux)
175 struct mbmem_attach_args *mbma = aux;
176 bus_space_handle_t bh;
177 bool matched;
179 /* No default Multibus address. */
180 if (mbma->mbma_paddr == -1)
181 return 0;
183 /* Make sure there is something there... */
184 if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, ECREG_BANK_SZ,
185 0, &bh))
186 return 0;
187 matched = (bus_space_peek_2(mbma->mbma_bustag, bh, 0, NULL) == 0);
188 bus_space_unmap(mbma->mbma_bustag, bh, ECREG_BANK_SZ);
189 if (!matched)
190 return 0;
192 /* Default interrupt priority. */
193 if (mbma->mbma_pri == -1)
194 mbma->mbma_pri = 3;
196 return 1;
199 void
200 ec_attach(device_t parent, device_t self, void *aux)
202 struct ec_softc *sc = device_private(self);
203 struct mbmem_attach_args *mbma = aux;
204 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
205 uint8_t myaddr[ETHER_ADDR_LEN];
207 sc->sc_dev = self;
209 aprint_normal("\n");
211 /* Map in the board control regs. */
212 sc->sc_iot = mbma->mbma_bustag;
213 if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, ECREG_BANK_SZ,
214 0, &sc->sc_ioh))
215 panic("%s: can't map regs", __func__);
217 /* Reset the board. */
218 ECREG_CSR_WR(EC_CSR_RESET);
219 delay(160);
222 * Copy out the board ROM Ethernet address,
223 * and use the non-vendor-ID part to seed
224 * our backoff pseudorandom number generator.
226 bus_space_read_region_1(sc->sc_iot, sc->sc_ioh,
227 ECREG_AROM, myaddr, ETHER_ADDR_LEN);
228 sc->sc_backoff_seed =
229 (myaddr[3] << 16) | (myaddr[4] << 8) | (myaddr[5]) | 1;
231 /* Initialize ifnet structure. */
232 strcpy(ifp->if_xname, device_xname(self));
233 ifp->if_softc = sc;
234 ifp->if_start = ec_start;
235 ifp->if_ioctl = ec_ioctl;
236 ifp->if_init = ec_init;
237 ifp->if_watchdog = ec_watchdog;
238 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
239 IFQ_SET_READY(&ifp->if_snd);
241 /* Initialize ifmedia structures. */
242 ifmedia_init(&sc->sc_media, 0, ec_mediachange, ec_mediastatus);
243 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
244 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
246 /* Now we can attach the interface. */
247 if_attach(ifp);
248 idprom_etheraddr(myaddr);
249 ether_ifattach(ifp, myaddr);
250 aprint_normal_dev(self, "address %s\n", ether_sprintf(myaddr));
252 bus_intr_establish(mbma->mbma_bustag, mbma->mbma_pri, IPL_NET, 0,
253 ec_intr, sc);
255 #if NRND > 0
256 rnd_attach_source(&sc->rnd_source, device_xname(self),
257 RND_TYPE_NET, 0);
258 #endif
262 * Reset interface.
264 void
265 ec_reset(struct ifnet *ifp)
267 int s;
269 s = splnet();
270 ec_init(ifp);
271 splx(s);
276 * Initialize interface.
278 int
279 ec_init(struct ifnet *ifp)
281 struct ec_softc *sc = ifp->if_softc;
283 /* Reset the board. */
284 ECREG_CSR_WR(EC_CSR_RESET);
285 delay(160);
287 /* Set the Ethernet address. */
288 bus_space_write_region_1(sc->sc_iot, sc->sc_ioh,
289 ECREG_ARAM, CLLADDR(sc->sc_ethercom.ec_if.if_sadl), ETHER_ADDR_LEN);
290 ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_AMSW);
291 ECREG_CSR_WR(ECREG_CSR_RD & 0);
293 /* Enable interrupts. */
294 ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) |
295 EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_BINT | EC_CSR_AINT |
296 (ifp->if_flags & IFF_PROMISC ? EC_CSR_PROMISC : EC_CSR_PA));
298 /* Set flags appropriately. */
299 ifp->if_flags |= IFF_RUNNING;
300 ifp->if_flags &= ~IFF_OACTIVE;
302 /* Start output. */
303 ec_start(ifp);
305 return 0;
309 * Start output on interface.
311 void
312 ec_start(struct ifnet *ifp)
314 struct ec_softc *sc = ifp->if_softc;
315 struct mbuf *m, *m0;
316 int s;
317 u_int count, realcount;
318 bus_size_t off;
319 static uint8_t padding[ETHER_MIN_LEN - ETHER_CRC_LEN] = {0};
321 s = splnet();
323 /* Don't do anything if output is active. */
324 if ((ifp->if_flags & IFF_OACTIVE) != 0) {
325 splx(s);
326 return;
328 /* Don't do anything if the output queue is empty. */
329 IFQ_DEQUEUE(&ifp->if_snd, m0);
330 if (m0 == NULL) {
331 splx(s);
332 return;
335 #if NBPFILTER > 0
336 /* The BPF tap. */
337 if (ifp->if_bpf)
338 bpf_mtap(ifp->if_bpf, m0);
339 #endif
341 /* Size the packet. */
342 count = EC_BUF_SZ - m0->m_pkthdr.len;
344 /* Copy the packet into the xmit buffer. */
345 realcount = MIN(count, EC_PKT_MAXTDOFF);
346 bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_TBUF, realcount);
347 for (off = realcount, m = m0; m != 0; off += m->m_len, m = m->m_next)
348 ec_copyout(sc, mtod(m, uint8_t *), ECREG_TBUF + off, m->m_len);
349 m_freem(m0);
350 if (count - realcount)
351 ec_copyout(sc, padding, ECREG_TBUF + off, count - realcount);
353 /* Enable the transmitter. */
354 ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_PA) |
355 EC_CSR_TBSW | EC_CSR_TINT | EC_CSR_JINT);
356 ifp->if_flags |= IFF_OACTIVE;
358 /* Done. */
359 splx(s);
363 * Controller interrupt.
365 int
366 ec_intr(void *arg)
368 struct ec_softc *sc = arg;
369 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
370 int recv_first;
371 int recv_second;
372 int retval;
373 struct mbuf *m0;
375 retval = 0;
377 /* Check for received packet(s). */
378 recv_first = recv_second = 0;
379 switch (ECREG_CSR_RD & (EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_RBBA)) {
381 case (EC_CSR_BBSW | EC_CSR_ABSW):
382 case (EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_RBBA):
383 /* Neither buffer is full. Is this a transmit interrupt?
384 * Acknowledge the interrupt ourselves. */
385 ECREG_CSR_WR(ECREG_CSR_RD &
386 (EC_CSR_TINT | EC_CSR_JINT | EC_CSR_PAMASK));
387 ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) |
388 EC_CSR_BINT | EC_CSR_AINT);
389 break;
391 case EC_CSR_BBSW:
392 case (EC_CSR_BBSW | EC_CSR_RBBA):
393 /* Only the A buffer is full. */
394 recv_first = EC_CSR_AINT;
395 break;
397 case EC_CSR_ABSW:
398 case (EC_CSR_ABSW | EC_CSR_RBBA):
399 /* Only the B buffer is full. */
400 recv_first = EC_CSR_BINT;
401 break;
403 case 0:
404 /* Both the A buffer and the B buffer are full, and the A
405 * buffer is older than the B buffer. */
406 recv_first = EC_CSR_AINT;
407 recv_second = EC_CSR_BINT;
408 break;
410 case EC_CSR_RBBA:
411 /* Both the A buffer and the B buffer are full, and the B
412 * buffer is older than the A buffer. */
413 recv_first = EC_CSR_BINT;
414 recv_second = EC_CSR_AINT;
415 break;
418 /* Receive packets. */
419 if (recv_first) {
421 /* Acknowledge the interrupt. */
422 ECREG_CSR_WR(ECREG_CSR_RD &
423 ((EC_CSR_BINT | EC_CSR_AINT | EC_CSR_TINT | EC_CSR_JINT |
424 EC_CSR_PAMASK) ^ (recv_first | recv_second)));
426 /* Receive a packet. */
427 ec_recv(sc, recv_first);
429 /* Receive a packet. */
430 if (recv_second)
431 ec_recv(sc, recv_second);
433 retval++;
435 /* Check for a transmitted packet. */
436 if (ifp->if_flags & IFF_OACTIVE) {
438 /* If we got a collision. */
439 if (ECREG_CSR_RD & EC_CSR_JAM) {
440 ECREG_CSR_WR(ECREG_CSR_RD &
441 (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
442 sc->sc_ethercom.ec_if.if_collisions++;
443 retval++;
444 ec_coll(sc);
447 /* If we transmitted a packet. */
448 else if ((ECREG_CSR_RD & EC_CSR_TBSW) == 0) {
449 ECREG_CSR_WR(ECREG_CSR_RD &
450 (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
451 retval++;
452 sc->sc_ethercom.ec_if.if_opackets++;
453 sc->sc_jammed = 0;
454 ifp->if_flags &= ~IFF_OACTIVE;
455 IFQ_POLL(&ifp->if_snd, m0);
456 if (m0 != NULL)
457 ec_start(ifp);
459 } else {
461 /* Make sure we disable transmitter interrupts. */
462 ECREG_CSR_WR(ECREG_CSR_RD &
463 (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
466 return retval;
470 * Read in a packet from the board.
472 void
473 ec_recv(struct ec_softc *sc, int intbit)
475 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
476 struct mbuf *m0, *m, *newm;
477 bus_size_t buf;
478 uint16_t status;
479 uint16_t doff;
480 int length, total_length;
482 buf = EC_CSR_INT_BUF(intbit);
484 /* Read in the packet status. */
485 status = bus_space_read_2(sc->sc_iot, sc->sc_ioh, buf);
486 doff = status & EC_PKT_DOFF;
488 for (total_length = -1, m0 = NULL;;) {
490 /* Check for an error. */
491 if (status & (EC_PKT_FCSERR | EC_PKT_RGERR | EC_PKT_FRERR) ||
492 doff < EC_PKT_MINRDOFF ||
493 doff > EC_PKT_MAXRDOFF) {
494 printf("%s: garbled packet, status 0x%04x; dropping\n",
495 device_xname(sc->sc_dev), (unsigned int)status);
496 break;
499 /* Adjust for the header. */
500 total_length = doff - EC_PKT_RDOFF;
501 buf += EC_PKT_RDOFF;
503 /* XXX - sometimes the card reports a large data offset. */
504 if (total_length > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
505 #ifdef DEBUG
506 printf("%s: fixing too-large length of %d\n",
507 device_xname(sc->sc_dev), total_length);
508 #endif
509 total_length = (ETHER_MAX_LEN - ETHER_CRC_LEN);
512 MGETHDR(m0, M_DONTWAIT, MT_DATA);
513 if (m0 == NULL)
514 break;
515 m0->m_pkthdr.rcvif = ifp;
516 m0->m_pkthdr.len = total_length;
517 length = MHLEN;
518 m = m0;
520 while (total_length > 0) {
521 if (total_length >= MINCLSIZE) {
522 MCLGET(m, M_DONTWAIT);
523 if ((m->m_flags & M_EXT) == 0)
524 break;
525 length = MCLBYTES;
527 m->m_len = length = min(total_length, length);
528 ec_copyin(sc, mtod(m, uint8_t *), buf, length);
529 total_length -= length;
530 buf += length;
532 if (total_length > 0) {
533 MGET(newm, M_DONTWAIT, MT_DATA);
534 if (newm == NULL)
535 break;
536 length = MLEN;
537 m = m->m_next = newm;
540 break;
543 if (total_length == 0) {
544 ifp->if_ipackets++;
546 #if NBPFILTER > 0
548 * Check if there's a BPF listener on this interface.
549 * If so, hand off the raw packet to BPF.
551 if (ifp->if_bpf)
552 bpf_mtap(ifp->if_bpf, m0);
553 #endif
555 /* Pass the packet up. */
556 (*ifp->if_input)(ifp, m0);
558 } else {
559 /* Something went wrong. */
560 if (m0 != NULL)
561 m_freem(m0);
562 ifp->if_ierrors++;
565 /* Give the receive buffer back to the card. */
566 buf = EC_CSR_INT_BUF(intbit);
567 bus_space_write_2(sc->sc_iot, sc->sc_ioh, buf, 0);
568 ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) |
569 EC_CSR_INT_BSW(intbit) | intbit);
572 int
573 ec_mediachange(struct ifnet *ifp)
576 return 0;
579 void
580 ec_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
583 if ((ifp->if_flags & IFF_UP) == 0)
584 return;
586 ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
590 * Process an ioctl request. This code needs some work - it looks pretty ugly.
592 int
593 ec_ioctl(struct ifnet *ifp, u_long cmd, void *data)
595 struct ifaddr *ifa = (struct ifaddr *)data;
596 struct ifreq *ifr = (struct ifreq *)data;
597 struct ec_softc *sc = ifp->if_softc;
598 int s, error = 0;
600 s = splnet();
602 switch (cmd) {
604 case SIOCINITIFADDR:
605 ifp->if_flags |= IFF_UP;
607 switch (ifa->ifa_addr->sa_family) {
608 #ifdef INET
609 case AF_INET:
610 ec_init(ifp);
611 arp_ifinit(ifp, ifa);
612 break;
613 #endif
614 default:
615 ec_init(ifp);
616 break;
618 break;
620 case SIOCSIFFLAGS:
621 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
622 break;
623 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
624 case IFF_RUNNING:
626 * If interface is marked down and it is running, then
627 * stop it.
629 ifp->if_flags &= ~IFF_RUNNING;
630 break;
631 case IFF_UP:
633 * If interface is marked up and it is stopped, then
634 * start it.
636 ec_init(ifp);
637 break;
638 default:
640 * Some other important flag might have changed, so
641 * reset.
643 ec_reset(ifp);
644 break;
646 break;
648 case SIOCGIFMEDIA:
649 case SIOCSIFMEDIA:
650 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
651 break;
653 default:
654 error = ether_ioctl(ifp, cmd, data);
655 break;
658 splx(s);
659 return error;
663 * Collision routine.
665 void
666 ec_coll(struct ec_softc *sc)
668 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
669 u_short jams;
670 struct mbuf *m0;
672 if ((++sc->sc_colliding) >= EC_COLLISIONS_JAMMED) {
673 sc->sc_ethercom.ec_if.if_oerrors++;
674 if (!sc->sc_jammed)
675 printf("%s: ethernet jammed\n",
676 device_xname(sc->sc_dev));
677 sc->sc_jammed = 1;
678 sc->sc_colliding = 0;
679 ifp->if_flags &= ~IFF_OACTIVE;
680 IFQ_POLL(&ifp->if_snd, m0);
681 if (m0 != NULL)
682 ec_start(ifp);
683 } else {
684 jams = MAX(sc->sc_colliding, EC_BACKOFF_PRNG_COLL_MAX);
685 sc->sc_backoff_seed =
686 ((sc->sc_backoff_seed * EC_BACKOFF_PRNG_MUL) +
687 EC_BACKOFF_PRNG_ADD) & EC_BACKOFF_PRNG_MASK;
688 bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_BACKOFF,
689 -(((sc->sc_backoff_seed >> 8) & ~(-1 << jams)) + 1));
690 ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) |
691 EC_CSR_JAM | EC_CSR_TINT | EC_CSR_JINT);
696 * Device timeout routine.
698 void
699 ec_watchdog(struct ifnet *ifp)
701 struct ec_softc *sc = ifp->if_softc;
703 log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
704 sc->sc_ethercom.ec_if.if_oerrors++;
706 ec_reset(ifp);