1 /* $NetBSD: if_ec.c,v 1.15 2008/07/06 13:29:50 tsutsui Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 $");
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/ioctl.h>
49 #include <sys/socket.h>
50 #include <sys/syslog.h>
51 #include <sys/device.h>
52 #include <sys/endian.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>
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>
74 #include <netns/ns_if.h>
79 #include <net/bpfdesc.h>
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>
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 */
108 rndsource_element_t rnd_source
;
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
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
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.
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.
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
);
173 ec_match(device_t parent
, cfdata_t cf
, void *aux
)
175 struct mbmem_attach_args
*mbma
= aux
;
176 bus_space_handle_t bh
;
179 /* No default Multibus address. */
180 if (mbma
->mbma_paddr
== -1)
183 /* Make sure there is something there... */
184 if (bus_space_map(mbma
->mbma_bustag
, mbma
->mbma_paddr
, ECREG_BANK_SZ
,
187 matched
= (bus_space_peek_2(mbma
->mbma_bustag
, bh
, 0, NULL
) == 0);
188 bus_space_unmap(mbma
->mbma_bustag
, bh
, ECREG_BANK_SZ
);
192 /* Default interrupt priority. */
193 if (mbma
->mbma_pri
== -1)
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
];
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
,
215 panic("%s: can't map regs", __func__
);
217 /* Reset the board. */
218 ECREG_CSR_WR(EC_CSR_RESET
);
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
));
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. */
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,
256 rnd_attach_source(&sc
->rnd_source
, device_xname(self
),
265 ec_reset(struct ifnet
*ifp
)
276 * Initialize interface.
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
);
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
;
309 * Start output on interface.
312 ec_start(struct ifnet
*ifp
)
314 struct ec_softc
*sc
= ifp
->if_softc
;
317 u_int count
, realcount
;
319 static uint8_t padding
[ETHER_MIN_LEN
- ETHER_CRC_LEN
] = {0};
323 /* Don't do anything if output is active. */
324 if ((ifp
->if_flags
& IFF_OACTIVE
) != 0) {
328 /* Don't do anything if the output queue is empty. */
329 IFQ_DEQUEUE(&ifp
->if_snd
, m0
);
338 bpf_mtap(ifp
->if_bpf
, m0
);
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
);
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
;
363 * Controller interrupt.
368 struct ec_softc
*sc
= arg
;
369 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
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
);
392 case (EC_CSR_BBSW
| EC_CSR_RBBA
):
393 /* Only the A buffer is full. */
394 recv_first
= EC_CSR_AINT
;
398 case (EC_CSR_ABSW
| EC_CSR_RBBA
):
399 /* Only the B buffer is full. */
400 recv_first
= EC_CSR_BINT
;
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
;
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
;
418 /* Receive packets. */
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. */
431 ec_recv(sc
, recv_second
);
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
++;
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
));
452 sc
->sc_ethercom
.ec_if
.if_opackets
++;
454 ifp
->if_flags
&= ~IFF_OACTIVE
;
455 IFQ_POLL(&ifp
->if_snd
, m0
);
461 /* Make sure we disable transmitter interrupts. */
462 ECREG_CSR_WR(ECREG_CSR_RD
&
463 (EC_CSR_BINT
| EC_CSR_AINT
| EC_CSR_PAMASK
));
470 * Read in a packet from the board.
473 ec_recv(struct ec_softc
*sc
, int intbit
)
475 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
476 struct mbuf
*m0
, *m
, *newm
;
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
);
499 /* Adjust for the header. */
500 total_length
= doff
- EC_PKT_RDOFF
;
503 /* XXX - sometimes the card reports a large data offset. */
504 if (total_length
> (ETHER_MAX_LEN
- ETHER_CRC_LEN
)) {
506 printf("%s: fixing too-large length of %d\n",
507 device_xname(sc
->sc_dev
), total_length
);
509 total_length
= (ETHER_MAX_LEN
- ETHER_CRC_LEN
);
512 MGETHDR(m0
, M_DONTWAIT
, MT_DATA
);
515 m0
->m_pkthdr
.rcvif
= ifp
;
516 m0
->m_pkthdr
.len
= total_length
;
520 while (total_length
> 0) {
521 if (total_length
>= MINCLSIZE
) {
522 MCLGET(m
, M_DONTWAIT
);
523 if ((m
->m_flags
& M_EXT
) == 0)
527 m
->m_len
= length
= min(total_length
, length
);
528 ec_copyin(sc
, mtod(m
, uint8_t *), buf
, length
);
529 total_length
-= length
;
532 if (total_length
> 0) {
533 MGET(newm
, M_DONTWAIT
, MT_DATA
);
537 m
= m
->m_next
= newm
;
543 if (total_length
== 0) {
548 * Check if there's a BPF listener on this interface.
549 * If so, hand off the raw packet to BPF.
552 bpf_mtap(ifp
->if_bpf
, m0
);
555 /* Pass the packet up. */
556 (*ifp
->if_input
)(ifp
, m0
);
559 /* Something went wrong. */
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
);
573 ec_mediachange(struct ifnet
*ifp
)
580 ec_mediastatus(struct ifnet
*ifp
, struct ifmediareq
*ifmr
)
583 if ((ifp
->if_flags
& IFF_UP
) == 0)
586 ifmr
->ifm_status
= IFM_AVALID
| IFM_ACTIVE
;
590 * Process an ioctl request. This code needs some work - it looks pretty ugly.
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
;
605 ifp
->if_flags
|= IFF_UP
;
607 switch (ifa
->ifa_addr
->sa_family
) {
611 arp_ifinit(ifp
, ifa
);
621 if ((error
= ifioctl_common(ifp
, cmd
, data
)) != 0)
623 switch (ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) {
626 * If interface is marked down and it is running, then
629 ifp
->if_flags
&= ~IFF_RUNNING
;
633 * If interface is marked up and it is stopped, then
640 * Some other important flag might have changed, so
650 error
= ifmedia_ioctl(ifp
, ifr
, &sc
->sc_media
, cmd
);
654 error
= ether_ioctl(ifp
, cmd
, data
);
666 ec_coll(struct ec_softc
*sc
)
668 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
672 if ((++sc
->sc_colliding
) >= EC_COLLISIONS_JAMMED
) {
673 sc
->sc_ethercom
.ec_if
.if_oerrors
++;
675 printf("%s: ethernet jammed\n",
676 device_xname(sc
->sc_dev
));
678 sc
->sc_colliding
= 0;
679 ifp
->if_flags
&= ~IFF_OACTIVE
;
680 IFQ_POLL(&ifp
->if_snd
, m0
);
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.
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
++;