if_ec.c revision 1.34.4.1 1 /* $NetBSD: if_ec.c,v 1.34.4.1 2020/02/29 20:18:31 ad Exp $ */
2
3 /*
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthew Fredette.
9 *
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.
18 *
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.
30 */
31
32 /*
33 * 3Com 3C400 device driver
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: if_ec.c,v 1.34.4.1 2020/02/29 20:18:31 ad Exp $");
38
39 #include "opt_inet.h"
40 #include "opt_ns.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/errno.h>
45 #include <sys/ioctl.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/syslog.h>
49 #include <sys/device.h>
50 #include <sys/endian.h>
51 #include <sys/rndsource.h>
52
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/if_ether.h>
57 #include <net/if_media.h>
58 #include <net/bpf.h>
59
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip.h>
65 #include <netinet/if_inarp.h>
66 #endif
67
68 #include <machine/cpu.h>
69 #include <machine/autoconf.h>
70 #include <machine/idprom.h>
71 #include <machine/bus.h>
72 #include <machine/intr.h>
73
74 #include <sun2/dev/if_ecreg.h>
75
76 /*
77 * Interface softc.
78 */
79 struct ec_softc {
80 device_t sc_dev;
81 void *sc_ih;
82
83 struct ethercom sc_ethercom; /* ethernet common */
84 struct ifmedia sc_media; /* our supported media */
85
86 bus_space_tag_t sc_iot; /* bus space tag */
87 bus_space_handle_t sc_ioh; /* bus space handle */
88
89 u_char sc_jammed; /* nonzero if the net is jammed */
90 u_char sc_colliding; /* nonzero if the net is colliding */
91 uint32_t sc_backoff_seed; /* seed for the backoff PRNG */
92
93 krndsource_t rnd_source;
94 };
95
96 /* Macros to read and write the CSR. */
97 #define ECREG_CSR_RD bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECREG_CSR)
98 #define ECREG_CSR_WR(val) bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_CSR, val)
99
100 /* After this many collisions, the packet is dropped. */
101 #define EC_COLLISIONS_JAMMED 16
102
103 /*
104 * Various constants used in the backoff pseudorandom
105 * number generator.
106 */
107 #define EC_BACKOFF_PRNG_COLL_MAX 10
108 #define EC_BACKOFF_PRNG_MUL 1103515245
109 #define EC_BACKOFF_PRNG_ADD 12345
110 #define EC_BACKOFF_PRNG_MASK 0x7fffffff
111
112 /*
113 * Prototypes
114 */
115 int ec_intr(void *);
116 void ec_reset(struct ifnet *);
117 int ec_init(struct ifnet *);
118 int ec_ioctl(struct ifnet *, u_long, void *);
119 void ec_watchdog(struct ifnet *);
120 void ec_start(struct ifnet *);
121
122 void ec_recv(struct ec_softc *, int);
123 void ec_coll(struct ec_softc *);
124 void ec_copyin(struct ec_softc *, void *, int, size_t);
125 void ec_copyout(struct ec_softc *, const void *, int, size_t);
126
127 int ec_mediachange(struct ifnet *);
128 void ec_mediastatus(struct ifnet *, struct ifmediareq *);
129
130 int ec_match(device_t, cfdata_t, void *);
131 void ec_attach(device_t, device_t, void *);
132
133 CFATTACH_DECL_NEW(ec, sizeof(struct ec_softc),
134 ec_match, ec_attach, NULL, NULL);
135
136 /*
137 * Copy board memory to kernel.
138 */
139 void
140 ec_copyin(struct ec_softc *sc, void *p, int offset, size_t size)
141 {
142
143 bus_space_copyin(sc->sc_iot, sc->sc_ioh, offset, p, size);
144 }
145
146 /*
147 * Copy from kernel space to board memory.
148 */
149 void
150 ec_copyout(struct ec_softc *sc, const void *p, int offset, size_t size)
151 {
152
153 bus_space_copyout(sc->sc_iot, sc->sc_ioh, offset, p, size);
154 }
155
156 int
157 ec_match(device_t parent, cfdata_t cf, void *aux)
158 {
159 struct mbmem_attach_args *mbma = aux;
160 bus_space_handle_t bh;
161 bool matched;
162
163 /* No default Multibus address. */
164 if (mbma->mbma_paddr == -1)
165 return 0;
166
167 /* Make sure there is something there... */
168 if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, ECREG_BANK_SZ,
169 0, &bh))
170 return 0;
171 matched = (bus_space_peek_2(mbma->mbma_bustag, bh, 0, NULL) == 0);
172 bus_space_unmap(mbma->mbma_bustag, bh, ECREG_BANK_SZ);
173 if (!matched)
174 return 0;
175
176 /* Default interrupt priority. */
177 if (mbma->mbma_pri == -1)
178 mbma->mbma_pri = 3;
179
180 return 1;
181 }
182
183 void
184 ec_attach(device_t parent, device_t self, void *aux)
185 {
186 struct ec_softc *sc = device_private(self);
187 struct mbmem_attach_args *mbma = aux;
188 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
189 uint8_t myaddr[ETHER_ADDR_LEN];
190
191 sc->sc_dev = self;
192
193 aprint_normal("\n");
194
195 /* Map in the board control regs. */
196 sc->sc_iot = mbma->mbma_bustag;
197 if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, ECREG_BANK_SZ,
198 0, &sc->sc_ioh))
199 panic("%s: can't map regs", __func__);
200
201 /* Reset the board. */
202 ECREG_CSR_WR(EC_CSR_RESET);
203 delay(160);
204
205 /*
206 * Copy out the board ROM Ethernet address,
207 * and use the non-vendor-ID part to seed
208 * our backoff pseudorandom number generator.
209 */
210 bus_space_read_region_1(sc->sc_iot, sc->sc_ioh,
211 ECREG_AROM, myaddr, ETHER_ADDR_LEN);
212 sc->sc_backoff_seed =
213 (myaddr[3] << 16) | (myaddr[4] << 8) | (myaddr[5]) | 1;
214
215 /* Initialize ifnet structure. */
216 strcpy(ifp->if_xname, device_xname(self));
217 ifp->if_softc = sc;
218 ifp->if_start = ec_start;
219 ifp->if_ioctl = ec_ioctl;
220 ifp->if_init = ec_init;
221 ifp->if_watchdog = ec_watchdog;
222 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
223 IFQ_SET_READY(&ifp->if_snd);
224
225 /* Initialize ifmedia structures. */
226 sc->sc_ethercom.ec_ifmedia = &sc->sc_media;
227 ifmedia_init(&sc->sc_media, 0, ec_mediachange, ec_mediastatus);
228 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
229 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_MANUAL);
230
231 /* Now we can attach the interface. */
232 if_attach(ifp);
233 if_deferred_start_init(ifp, NULL);
234 idprom_etheraddr(myaddr);
235 ether_ifattach(ifp, myaddr);
236 aprint_normal_dev(self, "address %s\n", ether_sprintf(myaddr));
237
238 bus_intr_establish(mbma->mbma_bustag, mbma->mbma_pri, IPL_NET, 0,
239 ec_intr, sc);
240
241 rnd_attach_source(&sc->rnd_source, device_xname(self),
242 RND_TYPE_NET, RND_FLAG_DEFAULT);
243 }
244
245 /*
246 * Reset interface.
247 */
248 void
249 ec_reset(struct ifnet *ifp)
250 {
251 int s;
252
253 s = splnet();
254 ec_init(ifp);
255 splx(s);
256 }
257
258
259 /*
260 * Initialize interface.
261 */
262 int
263 ec_init(struct ifnet *ifp)
264 {
265 struct ec_softc *sc = ifp->if_softc;
266
267 /* Reset the board. */
268 ECREG_CSR_WR(EC_CSR_RESET);
269 delay(160);
270
271 /* Set the Ethernet address. */
272 bus_space_write_region_1(sc->sc_iot, sc->sc_ioh,
273 ECREG_ARAM, CLLADDR(sc->sc_ethercom.ec_if.if_sadl), ETHER_ADDR_LEN);
274 ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_AMSW);
275 ECREG_CSR_WR(ECREG_CSR_RD & 0);
276
277 /* Enable interrupts. */
278 ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) |
279 EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_BINT | EC_CSR_AINT |
280 (ifp->if_flags & IFF_PROMISC ? EC_CSR_PROMISC : EC_CSR_PA));
281
282 /* Set flags appropriately. */
283 ifp->if_flags |= IFF_RUNNING;
284 ifp->if_flags &= ~IFF_OACTIVE;
285
286 /* Start output. */
287 ec_start(ifp);
288
289 return 0;
290 }
291
292 /*
293 * Start output on interface.
294 */
295 void
296 ec_start(struct ifnet *ifp)
297 {
298 struct ec_softc *sc = ifp->if_softc;
299 struct mbuf *m, *m0;
300 int s;
301 u_int count, realcount;
302 bus_size_t off;
303 static uint8_t padding[ETHER_MIN_LEN - ETHER_CRC_LEN] = {0};
304
305 s = splnet();
306
307 /* Don't do anything if output is active. */
308 if ((ifp->if_flags & IFF_OACTIVE) != 0) {
309 splx(s);
310 return;
311 }
312 /* Don't do anything if the output queue is empty. */
313 IFQ_DEQUEUE(&ifp->if_snd, m0);
314 if (m0 == NULL) {
315 splx(s);
316 return;
317 }
318
319 /* The BPF tap. */
320 bpf_mtap(ifp, m0, BPF_D_OUT);
321
322 /* Size the packet. */
323 count = EC_BUF_SZ - m0->m_pkthdr.len;
324
325 /* Copy the packet into the xmit buffer. */
326 realcount = MIN(count, EC_PKT_MAXTDOFF);
327 bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_TBUF, realcount);
328 for (off = realcount, m = m0; m != 0; off += m->m_len, m = m->m_next)
329 ec_copyout(sc, mtod(m, uint8_t *), ECREG_TBUF + off, m->m_len);
330 m_freem(m0);
331 if (count - realcount)
332 ec_copyout(sc, padding, ECREG_TBUF + off, count - realcount);
333
334 /* Enable the transmitter. */
335 ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_PA) |
336 EC_CSR_TBSW | EC_CSR_TINT | EC_CSR_JINT);
337 ifp->if_flags |= IFF_OACTIVE;
338
339 /* Done. */
340 splx(s);
341 }
342
343 /*
344 * Controller interrupt.
345 */
346 int
347 ec_intr(void *arg)
348 {
349 struct ec_softc *sc = arg;
350 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
351 int recv_first;
352 int recv_second;
353 int retval;
354
355 retval = 0;
356
357 /* Check for received packet(s). */
358 recv_first = recv_second = 0;
359 switch (ECREG_CSR_RD & (EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_RBBA)) {
360
361 case (EC_CSR_BBSW | EC_CSR_ABSW):
362 case (EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_RBBA):
363 /* Neither buffer is full. Is this a transmit interrupt?
364 * Acknowledge the interrupt ourselves. */
365 ECREG_CSR_WR(ECREG_CSR_RD &
366 (EC_CSR_TINT | EC_CSR_JINT | EC_CSR_PAMASK));
367 ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) |
368 EC_CSR_BINT | EC_CSR_AINT);
369 break;
370
371 case EC_CSR_BBSW:
372 case (EC_CSR_BBSW | EC_CSR_RBBA):
373 /* Only the A buffer is full. */
374 recv_first = EC_CSR_AINT;
375 break;
376
377 case EC_CSR_ABSW:
378 case (EC_CSR_ABSW | EC_CSR_RBBA):
379 /* Only the B buffer is full. */
380 recv_first = EC_CSR_BINT;
381 break;
382
383 case 0:
384 /* Both the A buffer and the B buffer are full, and the A
385 * buffer is older than the B buffer. */
386 recv_first = EC_CSR_AINT;
387 recv_second = EC_CSR_BINT;
388 break;
389
390 case EC_CSR_RBBA:
391 /* Both the A buffer and the B buffer are full, and the B
392 * buffer is older than the A buffer. */
393 recv_first = EC_CSR_BINT;
394 recv_second = EC_CSR_AINT;
395 break;
396 }
397
398 /* Receive packets. */
399 if (recv_first) {
400
401 /* Acknowledge the interrupt. */
402 ECREG_CSR_WR(ECREG_CSR_RD &
403 ((EC_CSR_BINT | EC_CSR_AINT | EC_CSR_TINT | EC_CSR_JINT |
404 EC_CSR_PAMASK) ^ (recv_first | recv_second)));
405
406 /* Receive a packet. */
407 ec_recv(sc, recv_first);
408
409 /* Receive a packet. */
410 if (recv_second)
411 ec_recv(sc, recv_second);
412
413 retval++;
414 }
415 /* Check for a transmitted packet. */
416 if (ifp->if_flags & IFF_OACTIVE) {
417
418 /* If we got a collision. */
419 if (ECREG_CSR_RD & EC_CSR_JAM) {
420 ECREG_CSR_WR(ECREG_CSR_RD &
421 (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
422 if_statinc(ifp, if_collisions);
423 retval++;
424 ec_coll(sc);
425
426 }
427 /* If we transmitted a packet. */
428 else if ((ECREG_CSR_RD & EC_CSR_TBSW) == 0) {
429 ECREG_CSR_WR(ECREG_CSR_RD &
430 (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
431 retval++;
432 if_statinc(ifp, if_opackets);
433 sc->sc_jammed = 0;
434 ifp->if_flags &= ~IFF_OACTIVE;
435 if_schedule_deferred_start(ifp);
436 }
437 } else {
438
439 /* Make sure we disable transmitter interrupts. */
440 ECREG_CSR_WR(ECREG_CSR_RD &
441 (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
442 }
443
444 return retval;
445 }
446
447 /*
448 * Read in a packet from the board.
449 */
450 void
451 ec_recv(struct ec_softc *sc, int intbit)
452 {
453 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
454 struct mbuf *m0, *m, *newm;
455 bus_size_t buf;
456 uint16_t status;
457 uint16_t doff;
458 int length, total_length;
459
460 buf = EC_CSR_INT_BUF(intbit);
461
462 /* Read in the packet status. */
463 status = bus_space_read_2(sc->sc_iot, sc->sc_ioh, buf);
464 doff = status & EC_PKT_DOFF;
465
466 for (total_length = -1, m0 = NULL;;) {
467
468 /* Check for an error. */
469 if (status & (EC_PKT_FCSERR | EC_PKT_RGERR | EC_PKT_FRERR) ||
470 doff < EC_PKT_MINRDOFF ||
471 doff > EC_PKT_MAXRDOFF) {
472 printf("%s: garbled packet, status 0x%04x; dropping\n",
473 device_xname(sc->sc_dev), (unsigned int)status);
474 break;
475 }
476
477 /* Adjust for the header. */
478 total_length = doff - EC_PKT_RDOFF;
479 buf += EC_PKT_RDOFF;
480
481 /* XXX - sometimes the card reports a large data offset. */
482 if (total_length > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
483 #ifdef DEBUG
484 printf("%s: fixing too-large length of %d\n",
485 device_xname(sc->sc_dev), total_length);
486 #endif
487 total_length = (ETHER_MAX_LEN - ETHER_CRC_LEN);
488 }
489
490 MGETHDR(m0, M_DONTWAIT, MT_DATA);
491 if (m0 == NULL)
492 break;
493 m_set_rcvif(m0, ifp);
494 m0->m_pkthdr.len = total_length;
495 length = MHLEN;
496 m = m0;
497
498 while (total_length > 0) {
499 if (total_length >= MINCLSIZE) {
500 MCLGET(m, M_DONTWAIT);
501 if ((m->m_flags & M_EXT) == 0)
502 break;
503 length = MCLBYTES;
504 }
505 m->m_len = length = uimin(total_length, length);
506 ec_copyin(sc, mtod(m, uint8_t *), buf, length);
507 total_length -= length;
508 buf += length;
509
510 if (total_length > 0) {
511 MGET(newm, M_DONTWAIT, MT_DATA);
512 if (newm == NULL)
513 break;
514 length = MLEN;
515 m = m->m_next = newm;
516 }
517 }
518 break;
519 }
520
521 if (total_length == 0) {
522 /* Pass the packet up. */
523 if_percpuq_enqueue(ifp->if_percpuq, m0);
524
525 } else {
526 /* Something went wrong. */
527 if (m0 != NULL)
528 m_freem(m0);
529 if_statinc(ifp, if_ierrors);
530 }
531
532 /* Give the receive buffer back to the card. */
533 buf = EC_CSR_INT_BUF(intbit);
534 bus_space_write_2(sc->sc_iot, sc->sc_ioh, buf, 0);
535 ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) |
536 EC_CSR_INT_BSW(intbit) | intbit);
537 }
538
539 int
540 ec_mediachange(struct ifnet *ifp)
541 {
542
543 return 0;
544 }
545
546 void
547 ec_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
548 {
549
550 if ((ifp->if_flags & IFF_UP) == 0)
551 return;
552
553 ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
554 }
555
556 /*
557 * Process an ioctl request. This code needs some work - it looks pretty ugly.
558 */
559 int
560 ec_ioctl(struct ifnet *ifp, u_long cmd, void *data)
561 {
562 struct ifaddr *ifa = (struct ifaddr *)data;
563 int s, error = 0;
564
565 s = splnet();
566
567 switch (cmd) {
568
569 case SIOCINITIFADDR:
570 ifp->if_flags |= IFF_UP;
571
572 switch (ifa->ifa_addr->sa_family) {
573 #ifdef INET
574 case AF_INET:
575 ec_init(ifp);
576 arp_ifinit(ifp, ifa);
577 break;
578 #endif
579 default:
580 ec_init(ifp);
581 break;
582 }
583 break;
584
585 case SIOCSIFFLAGS:
586 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
587 break;
588 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
589 case IFF_RUNNING:
590 /*
591 * If interface is marked down and it is running, then
592 * stop it.
593 */
594 ifp->if_flags &= ~IFF_RUNNING;
595 break;
596 case IFF_UP:
597 /*
598 * If interface is marked up and it is stopped, then
599 * start it.
600 */
601 ec_init(ifp);
602 break;
603 default:
604 /*
605 * Some other important flag might have changed, so
606 * reset.
607 */
608 ec_reset(ifp);
609 break;
610 }
611 break;
612
613 default:
614 error = ether_ioctl(ifp, cmd, data);
615 break;
616 }
617
618 splx(s);
619 return error;
620 }
621
622 /*
623 * Collision routine.
624 */
625 void
626 ec_coll(struct ec_softc *sc)
627 {
628 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
629 u_short jams;
630
631 if ((++sc->sc_colliding) >= EC_COLLISIONS_JAMMED) {
632 if_statinc(ifp, if_oerrors);
633 if (!sc->sc_jammed)
634 printf("%s: ethernet jammed\n",
635 device_xname(sc->sc_dev));
636 sc->sc_jammed = 1;
637 sc->sc_colliding = 0;
638 ifp->if_flags &= ~IFF_OACTIVE;
639 if_schedule_deferred_start(ifp);
640 } else {
641 jams = MAX(sc->sc_colliding, EC_BACKOFF_PRNG_COLL_MAX);
642 sc->sc_backoff_seed =
643 ((sc->sc_backoff_seed * EC_BACKOFF_PRNG_MUL) +
644 EC_BACKOFF_PRNG_ADD) & EC_BACKOFF_PRNG_MASK;
645 bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_BACKOFF,
646 -(((sc->sc_backoff_seed >> 8) & ~(-1 << jams)) + 1));
647 ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) |
648 EC_CSR_JAM | EC_CSR_TINT | EC_CSR_JINT);
649 }
650 }
651
652 /*
653 * Device timeout routine.
654 */
655 void
656 ec_watchdog(struct ifnet *ifp)
657 {
658 struct ec_softc *sc = ifp->if_softc;
659
660 log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
661 if_statinc(ifp, if_oerrors);
662
663 ec_reset(ifp);
664 }
665