hme.c revision 1.3 1 /* $NetBSD: hme.c,v 1.3 1999/12/15 10:33:31 pk Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * HME Ethernet module driver.
41 */
42
43 #define HMEDEBUG
44
45 #include "opt_inet.h"
46 #include "opt_ccitt.h"
47 #include "opt_llc.h"
48 #include "opt_ns.h"
49 #include "bpfilter.h"
50 #include "rnd.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/mbuf.h>
55 #include <sys/syslog.h>
56 #include <sys/socket.h>
57 #include <sys/device.h>
58 #include <sys/malloc.h>
59 #include <sys/ioctl.h>
60 #include <sys/errno.h>
61 #if NRND > 0
62 #include <sys/rnd.h>
63 #endif
64
65 #include <net/if.h>
66 #include <net/if_dl.h>
67 #include <net/if_ether.h>
68 #include <net/if_media.h>
69
70 #ifdef INET
71 #include <netinet/in.h>
72 #include <netinet/if_inarp.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip.h>
76 #endif
77
78 #ifdef NS
79 #include <netns/ns.h>
80 #include <netns/ns_if.h>
81 #endif
82
83 #if NBPFILTER > 0
84 #include <net/bpf.h>
85 #include <net/bpfdesc.h>
86 #endif
87
88 #include <dev/mii/mii.h>
89 #include <dev/mii/miivar.h>
90
91 #include <machine/bus.h>
92
93 #include <dev/ic/hmereg.h>
94 #include <dev/ic/hmevar.h>
95
96 void hme_start __P((struct ifnet *));
97 void hme_stop __P((struct hme_softc *));
98 int hme_ioctl __P((struct ifnet *, u_long, caddr_t));
99 void hme_watchdog __P((struct ifnet *));
100 void hme_shutdown __P((void *));
101 void hme_init __P((struct hme_softc *));
102 void hme_meminit __P((struct hme_softc *));
103 void hme_reset __P((struct hme_softc *));
104 void hme_setladrf __P((struct hme_softc *));
105
106 /* MII methods & callbacks */
107 static int hme_mii_readreg __P((struct device *, int, int));
108 static void hme_mii_writereg __P((struct device *, int, int, int));
109 static void hme_mii_statchg __P((struct device *));
110
111 int hme_mediachange __P((struct ifnet *));
112 void hme_mediastatus __P((struct ifnet *, struct ifmediareq *));
113
114 struct mbuf *hme_get __P((struct hme_softc *, int, int));
115 int hme_put __P((struct hme_softc *, int, struct mbuf *));
116 void hme_read __P((struct hme_softc *, int, int));
117 int hme_eint __P((struct hme_softc *, u_int));
118 int hme_rint __P((struct hme_softc *));
119 int hme_tint __P((struct hme_softc *));
120
121 static int ether_cmp __P((u_char *, u_char *));
122
123 /* Default buffer copy routines */
124 void hme_copytobuf_contig __P((struct hme_softc *, void *, int, int));
125 void hme_copyfrombuf_contig __P((struct hme_softc *, void *, int, int));
126 void hme_zerobuf_contig __P((struct hme_softc *, int, int));
127
128
129 void
130 hme_config(sc)
131 struct hme_softc *sc;
132 {
133 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
134 struct mii_data *mii = &sc->sc_mii;
135 bus_dma_segment_t seg;
136 bus_size_t size;
137 int rseg, error;
138
139 /*
140 * HME common initialization.
141 *
142 * hme_softc fields that must be initialized by the front-end:
143 *
144 * the bus tag:
145 * sc_bustag
146 *
147 * the dma bus tag:
148 * sc_dmatag
149 *
150 * the bus handles:
151 * sc_seb (Shared Ethernet Block registers)
152 * sc_erx (Receiver Unit registers)
153 * sc_etx (Transmitter Unit registers)
154 * sc_mac (MAC registers)
155 * sc_mif (Managment Interface registers)
156 *
157 * the maximum bus burst size:
158 * sc_burst
159 *
160 * (notyet:DMA capable memory for the ring descriptors & packet buffers:
161 * rb_membase, rb_dmabase)
162 *
163 * the local Ethernet address:
164 * sc_enaddr
165 *
166 */
167
168 /* Make sure the chip is stopped. */
169 hme_stop(sc);
170
171
172 /*
173 * Allocate descriptors and buffers
174 * XXX - do all this differently.. and more configurably,
175 * eg. use things as `dma_load_mbuf()' on transmit,
176 * and a pool of `EXTMEM' mbufs (with buffers DMA-mapped
177 * all the time) on the reveiver side.
178 */
179 #define _HME_NDESC 32
180 #define _HME_BUFSZ 1536
181
182 /* Note: the # of descriptors must be a multiple of 16 */
183 sc->sc_rb.rb_ntbuf = _HME_NDESC;
184 sc->sc_rb.rb_nrbuf = _HME_NDESC;
185
186 /*
187 * Allocate DMA capable memory
188 * Buffer descriptors must be aligned on a 2048 byte boundary;
189 * take this into account when calculating the size. Note that
190 * the maximum number of descriptors (256) occupies 2048 bytes,
191 * so we allocate that much regardless of _HME_NDESC.
192 */
193 size = 2048 + /* TX descriptors */
194 2048 + /* RX descriptors */
195 sc->sc_rb.rb_ntbuf * _HME_BUFSZ + /* TX buffers */
196 sc->sc_rb.rb_nrbuf * _HME_BUFSZ; /* TX buffers */
197 if ((error = bus_dmamem_alloc(sc->sc_dmatag, size,
198 2048, 0,
199 &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
200 printf("%s: DMA buffer alloc error %d\n",
201 sc->sc_dev.dv_xname, error);
202 }
203 sc->sc_rb.rb_dmabase = seg.ds_addr;
204
205 /* Map DMA memory in CPU adressable space */
206 if ((error = bus_dmamem_map(sc->sc_dmatag, &seg, rseg, size,
207 &sc->sc_rb.rb_membase,
208 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
209 printf("%s: DMA buffer map error %d\n",
210 sc->sc_dev.dv_xname, error);
211 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
212 return;
213 }
214
215 #if 0
216 /*
217 * Install default copy routines if not supplied.
218 */
219 if (sc->sc_copytobuf == NULL)
220 sc->sc_copytobuf = hme_copytobuf_contig;
221
222 if (sc->sc_copyfrombuf == NULL)
223 sc->sc_copyfrombuf = hme_copyfrombuf_contig;
224 #endif
225
226 printf(": address %s\n", ether_sprintf(sc->sc_enaddr));
227
228 /* Initialize ifnet structure. */
229 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
230 ifp->if_softc = sc;
231 ifp->if_start = hme_start;
232 ifp->if_ioctl = hme_ioctl;
233 ifp->if_watchdog = hme_watchdog;
234 ifp->if_flags =
235 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
236
237 /* Initialize ifmedia structures and MII info */
238 mii->mii_ifp = ifp;
239 mii->mii_readreg = hme_mii_readreg;
240 mii->mii_writereg = hme_mii_writereg;
241 mii->mii_statchg = hme_mii_statchg;
242
243 ifmedia_init(&mii->mii_media, 0, hme_mediachange, hme_mediastatus);
244
245 mii_phy_probe(&sc->sc_dev, mii, 0xffffffff,
246 MII_PHY_ANY, MII_OFFSET_ANY);
247
248 if (LIST_FIRST(&mii->mii_phys) == NULL) {
249 /* No PHY attached */
250 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
251 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
252 } else {
253 /*
254 * XXX - we can really do the following ONLY if the
255 * phy indeed has the auto negotiation capability!!
256 */
257 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
258 }
259
260 /* Attach the interface. */
261 if_attach(ifp);
262 ether_ifattach(ifp, sc->sc_enaddr);
263
264 #if NBPFILTER > 0
265 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
266 #endif
267
268 sc->sc_sh = shutdownhook_establish(hme_shutdown, sc);
269 if (sc->sc_sh == NULL)
270 panic("hme_config: can't establish shutdownhook");
271
272 #if 0
273 printf("%s: %d receive buffers, %d transmit buffers\n",
274 sc->sc_dev.dv_xname, sc->sc_nrbuf, sc->sc_ntbuf);
275 sc->sc_rbufaddr = malloc(sc->sc_nrbuf * sizeof(int), M_DEVBUF,
276 M_WAITOK);
277 sc->sc_tbufaddr = malloc(sc->sc_ntbuf * sizeof(int), M_DEVBUF,
278 M_WAITOK);
279 #endif
280
281 #if NRND > 0
282 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
283 RND_TYPE_NET, 0);
284 #endif
285 }
286
287 void
288 hme_reset(sc)
289 struct hme_softc *sc;
290 {
291 int s;
292
293 s = splnet();
294 hme_init(sc);
295 splx(s);
296 }
297
298 void
299 hme_stop(sc)
300 struct hme_softc *sc;
301 {
302 bus_space_tag_t t = sc->sc_bustag;
303 bus_space_handle_t seb = sc->sc_seb;
304 int n;
305
306 /* Reset transmitter and receiver */
307 bus_space_write_4(t, seb, HME_SEBI_RESET,
308 (HME_SEB_RESET_ETX | HME_SEB_RESET_ERX));
309
310 for (n = 0; n < 20; n++) {
311 u_int32_t v = bus_space_read_4(t, seb, HME_SEBI_RESET);
312 if ((v & (HME_SEB_RESET_ETX | HME_SEB_RESET_ERX)) == 0)
313 return;
314 DELAY(20);
315 }
316
317 printf("%s: hme_stop: reset failed\n", sc->sc_dev.dv_xname);
318 }
319
320 void
321 hme_meminit(sc)
322 struct hme_softc *sc;
323 {
324 bus_addr_t txbufdma, rxbufdma;
325 bus_addr_t dma;
326 caddr_t p;
327 unsigned int ntbuf, nrbuf, i;
328 struct hme_ring *hr = &sc->sc_rb;
329
330 p = hr->rb_membase;
331 dma = hr->rb_dmabase;
332
333 ntbuf = hr->rb_ntbuf;
334 nrbuf = hr->rb_nrbuf;
335
336 /*
337 * Allocate transmit descriptors
338 */
339 hr->rb_txd = p;
340 hr->rb_txddma = dma;
341 p += ntbuf * HME_XD_SIZE;
342 dma += ntbuf * HME_XD_SIZE;
343
344 /*
345 * Allocate receive descriptors
346 * Buffer descriptors must be aligned on a 2048 byte boundary.
347 */
348 dma = (bus_addr_t)roundup((long)dma, 2048);
349 p = (caddr_t)roundup((long)p, 2048);
350 hr->rb_rxd = p;
351 hr->rb_rxddma = dma;
352 p += nrbuf * HME_XD_SIZE;
353 dma += nrbuf * HME_XD_SIZE;
354
355
356 /*
357 * Allocate transmit buffers
358 */
359 hr->rb_txbuf = p;
360 txbufdma = dma;
361 p += ntbuf * _HME_BUFSZ;
362 dma += ntbuf * _HME_BUFSZ;
363
364 /*
365 * Allocate receive buffers
366 */
367 hr->rb_rxbuf = p;
368 rxbufdma = dma;
369 p += nrbuf * _HME_BUFSZ;
370 dma += nrbuf * _HME_BUFSZ;
371
372 /*
373 * Initialize transmit buffer descriptors
374 */
375 for (i = 0; i < ntbuf; i++) {
376 HME_XD_SETADDR(hr->rb_txd, i, txbufdma + i * _HME_BUFSZ);
377 HME_XD_SETFLAGS(hr->rb_txd, i, 0);
378 }
379
380 /*
381 * Initialize receive buffer descriptors
382 */
383 for (i = 0; i < nrbuf; i++) {
384 HME_XD_SETADDR(hr->rb_rxd, i, rxbufdma + i * _HME_BUFSZ);
385 HME_XD_SETFLAGS(hr->rb_rxd, i,
386 HME_XD_OWN | HME_XD_ENCODE_RSIZE(_HME_BUFSZ));
387 }
388
389 hr->rb_tdhead = hr->rb_tdtail = 0;
390 hr->rb_td_nbusy = 0;
391 hr->rb_rdtail = 0;
392 }
393
394 /*
395 * Initialization of interface; set up initialization block
396 * and transmit/receive descriptor rings.
397 */
398 void
399 hme_init(sc)
400 struct hme_softc *sc;
401 {
402 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
403 bus_space_tag_t t = sc->sc_bustag;
404 bus_space_handle_t seb = sc->sc_seb;
405 bus_space_handle_t etx = sc->sc_etx;
406 bus_space_handle_t erx = sc->sc_erx;
407 bus_space_handle_t mac = sc->sc_mac;
408 bus_space_handle_t mif = sc->sc_mif;
409 u_int8_t *ea;
410 u_int32_t v;
411
412 /*
413 * Initialization sequence. The numbered steps below correspond
414 * to the sequence outlined in section 6.3.5.1 in the Ethernet
415 * Channel Engine manual (part of the PCIO manual).
416 * See also the STP2002-STQ document from Sun Microsystems.
417 */
418
419 /* step 1 & 2. Reset the Ethernet Channel */
420 hme_stop(sc);
421
422 /* Call MI reset function if any */
423 if (sc->sc_hwreset)
424 (*sc->sc_hwreset)(sc);
425
426 #if 0
427 /* Mask all MIF interrupts, just in case */
428 bus_space_write_4(t, mif, HME_MIFI_IMASK, 0xffff);
429 #endif
430
431 /* step 3. Setup data structures in host memory */
432 hme_meminit(sc);
433
434 /* step 4. TX MAC registers & counters */
435 bus_space_write_4(t, mac, HME_MACI_NCCNT, 0);
436 bus_space_write_4(t, mac, HME_MACI_FCCNT, 0);
437 bus_space_write_4(t, mac, HME_MACI_EXCNT, 0);
438 bus_space_write_4(t, mac, HME_MACI_LTCNT, 0);
439
440 /* Load station MAC address */
441 ea = sc->sc_enaddr;
442 bus_space_write_4(t, mac, HME_MACI_MACADDR0, (ea[0] << 8) | ea[1]);
443 bus_space_write_4(t, mac, HME_MACI_MACADDR1, (ea[2] << 8) | ea[3]);
444 bus_space_write_4(t, mac, HME_MACI_MACADDR2, (ea[4] << 8) | ea[5]);
445
446 /*
447 * Init seed for backoff
448 * (source suggested by manual: low 10 bits of MAC address)
449 */
450 v = ((ea[4] << 8) | ea[5]) & 0x3fff;
451 bus_space_write_4(t, mac, HME_MACI_RANDSEED, v);
452
453
454 /* Note: Accepting power-on default for other MAC registers here.. */
455
456
457 /* step 5. RX MAC registers & counters */
458 hme_setladrf(sc);
459
460 /* step 6 & 7. Program Descriptor Ring Base Addresses */
461 bus_space_write_4(t, etx, HME_ETXI_RING, sc->sc_rb.rb_txddma);
462 bus_space_write_4(t, etx, HME_ETXI_RSIZE, sc->sc_rb.rb_ntbuf);
463
464 bus_space_write_4(t, erx, HME_ERXI_RING, sc->sc_rb.rb_rxddma);
465
466
467 /* step 8. Global Configuration & Interrupt Mask */
468 bus_space_write_4(t, seb, HME_SEBI_IMASK,
469 ~(
470 /*HME_SEB_STAT_GOTFRAME | HME_SEB_STAT_SENTFRAME |*/
471 HME_SEB_STAT_HOSTTOTX |
472 HME_SEB_STAT_RXTOHOST |
473 HME_SEB_STAT_TXALL |
474 HME_SEB_STAT_TXPERR |
475 HME_SEB_STAT_RCNTEXP |
476 HME_SEB_STAT_ALL_ERRORS ));
477
478 switch (sc->sc_burst) {
479 default:
480 v = 0;
481 break;
482 case 16:
483 v = HME_SEB_CFG_BURST16;
484 break;
485 case 32:
486 v = HME_SEB_CFG_BURST32;
487 break;
488 case 64:
489 v = HME_SEB_CFG_BURST64;
490 break;
491 }
492 bus_space_write_4(t, seb, HME_SEBI_CFG, v);
493
494 /* step 9. ETX Configuration: use mostly default values */
495
496 /* Enable DMA */
497 v = bus_space_read_4(t, etx, HME_ETXI_CFG);
498 v |= HME_ETX_CFG_DMAENABLE;
499 bus_space_write_4(t, etx, HME_ETXI_CFG, v);
500
501 /* Transmit Descriptor ring size: in increments of 16 */
502 bus_space_write_4(t, etx, HME_ETXI_RSIZE, _HME_NDESC / 16 - 1);
503
504
505 /* step 10. ERX Configuration */
506 v = bus_space_read_4(t, erx, HME_ERXI_CFG);
507
508 /* Encode Receive Descriptor ring size: four possible values */
509 switch (_HME_NDESC /*XXX*/) {
510 case 32:
511 v |= HME_ERX_CFG_RINGSIZE32;
512 break;
513 case 64:
514 v |= HME_ERX_CFG_RINGSIZE64;
515 break;
516 case 128:
517 v |= HME_ERX_CFG_RINGSIZE128;
518 break;
519 case 256:
520 v |= HME_ERX_CFG_RINGSIZE256;
521 break;
522 default:
523 printf("hme: invalid Receive Descriptor ring size\n");
524 break;
525 }
526
527 /* Enable DMA */
528 v |= HME_ERX_CFG_DMAENABLE;
529 bus_space_write_4(t, erx, HME_ERXI_CFG, v);
530
531 /* step 11. XIF Configuration */
532 v = bus_space_read_4(t, mac, HME_MACI_XIF);
533 v |= HME_MAC_XIF_OE;
534 /* If an external transceiver is connected, disable MII drivers */
535 if ((bus_space_read_4(t, mif, HME_MIFI_CFG) & HME_MIF_CFG_MDI1) != 0)
536 v |= HME_MAC_XIF_MIIDISAB;
537 bus_space_write_4(t, mac, HME_MACI_XIF, v);
538
539
540 /* step 12. RX_MAC Configuration Register */
541 v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
542 v |= HME_MAC_RXCFG_ENABLE;
543 bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
544
545 /* step 13. TX_MAC Configuration Register */
546 v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
547 v |= (HME_MAC_TXCFG_ENABLE | HME_MAC_TXCFG_DGIVEUP);
548 bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
549
550 /* step 14. Issue Transmit Pending command */
551
552 /*
553 * Put MIF in frame mode
554 * XXX - do bit-bang mode later
555 */
556 v = bus_space_read_4(t, mif, HME_MIFI_CFG);
557 v &= ~HME_MIF_CFG_BBMODE;
558 bus_space_write_4(t, mif, HME_MIFI_CFG, v);
559
560 /* Call MI initialization function if any */
561 if (sc->sc_hwinit)
562 (*sc->sc_hwinit)(sc);
563
564 ifp->if_flags |= IFF_RUNNING;
565 ifp->if_flags &= ~IFF_OACTIVE;
566 ifp->if_timer = 0;
567 hme_start(ifp);
568 }
569
570 /*
571 * Compare two Ether/802 addresses for equality, inlined and unrolled for
572 * speed.
573 */
574 static __inline__ int
575 ether_cmp(a, b)
576 u_char *a, *b;
577 {
578
579 if (a[5] != b[5] || a[4] != b[4] || a[3] != b[3] ||
580 a[2] != b[2] || a[1] != b[1] || a[0] != b[0])
581 return (0);
582 return (1);
583 }
584
585
586 /*
587 * Routine to copy from mbuf chain to transmit buffer in
588 * network buffer memory.
589 * Returns the amount of data copied.
590 */
591 int
592 hme_put(sc, ri, m)
593 struct hme_softc *sc;
594 int ri; /* Ring index */
595 struct mbuf *m;
596 {
597 struct mbuf *n;
598 int len, tlen = 0;
599 caddr_t bp;
600
601 bp = sc->sc_rb.rb_txbuf + (ri % sc->sc_rb.rb_ntbuf) * _HME_BUFSZ;
602 for (; m; m = n) {
603 len = m->m_len;
604 if (len == 0) {
605 MFREE(m, n);
606 continue;
607 }
608 bcopy(mtod(m, caddr_t), bp, len);
609 bp += len;
610 tlen += len;
611 MFREE(m, n);
612 }
613 return (tlen);
614 }
615
616 /*
617 * Pull data off an interface.
618 * Len is length of data, with local net header stripped.
619 * We copy the data into mbufs. When full cluster sized units are present
620 * we copy into clusters.
621 */
622 struct mbuf *
623 hme_get(sc, ri, totlen)
624 struct hme_softc *sc;
625 int ri, totlen;
626 {
627 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
628 struct mbuf *m, *m0, *newm;
629 caddr_t bp;
630 int len;
631
632 MGETHDR(m0, M_DONTWAIT, MT_DATA);
633 if (m0 == 0)
634 return (0);
635 m0->m_pkthdr.rcvif = ifp;
636 m0->m_pkthdr.len = totlen;
637 len = MHLEN;
638 m = m0;
639
640 bp = sc->sc_rb.rb_rxbuf + (ri % sc->sc_rb.rb_nrbuf) * _HME_BUFSZ;
641
642 while (totlen > 0) {
643 if (totlen >= MINCLSIZE) {
644 MCLGET(m, M_DONTWAIT);
645 if ((m->m_flags & M_EXT) == 0)
646 goto bad;
647 len = MCLBYTES;
648 }
649
650 if (m == m0) {
651 caddr_t newdata = (caddr_t)
652 ALIGN(m->m_data + sizeof(struct ether_header)) -
653 sizeof(struct ether_header);
654 len -= newdata - m->m_data;
655 m->m_data = newdata;
656 }
657
658 m->m_len = len = min(totlen, len);
659 bcopy(bp, mtod(m, caddr_t), len);
660 bp += len;
661
662 totlen -= len;
663 if (totlen > 0) {
664 MGET(newm, M_DONTWAIT, MT_DATA);
665 if (newm == 0)
666 goto bad;
667 len = MLEN;
668 m = m->m_next = newm;
669 }
670 }
671
672 return (m0);
673
674 bad:
675 m_freem(m0);
676 return (0);
677 }
678
679 /*
680 * Pass a packet to the higher levels.
681 */
682 void
683 hme_read(sc, ix, len)
684 struct hme_softc *sc;
685 int ix, len;
686 {
687 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
688 struct mbuf *m;
689
690 if (len <= sizeof(struct ether_header) ||
691 len > ETHERMTU + sizeof(struct ether_header)) {
692 #ifdef HMEDEBUG
693 printf("%s: invalid packet size %d; dropping\n",
694 sc->sc_dev.dv_xname, len);
695 #endif
696 ifp->if_ierrors++;
697 return;
698 }
699
700 /* Pull packet off interface. */
701 m = hme_get(sc, ix, len);
702 if (m == 0) {
703 ifp->if_ierrors++;
704 return;
705 }
706
707 ifp->if_ipackets++;
708
709 #if NBPFILTER > 0
710 /*
711 * Check if there's a BPF listener on this interface.
712 * If so, hand off the raw packet to BPF.
713 */
714 if (ifp->if_bpf) {
715 struct ether_header *eh;
716
717 bpf_mtap(ifp->if_bpf, m);
718
719 /*
720 * Note that the interface cannot be in promiscuous mode if
721 * there are no BPF listeners. And if we are in promiscuous
722 * mode, we have to check if this packet is really ours.
723 */
724
725 /* We assume that the header fit entirely in one mbuf. */
726 eh = mtod(m, struct ether_header *);
727
728 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
729 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
730 ether_cmp(eh->ether_dhost, sc->sc_enaddr)) {
731 m_freem(m);
732 return;
733 }
734 }
735 #endif
736
737 /* Pass the packet up. */
738 (*ifp->if_input)(ifp, m);
739 }
740
741 void
742 hme_start(ifp)
743 struct ifnet *ifp;
744 {
745 struct hme_softc *sc = (struct hme_softc *)ifp->if_softc;
746 caddr_t txd = sc->sc_rb.rb_txd;
747 struct mbuf *m;
748 unsigned int ri, len;
749 unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
750
751 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
752 return;
753
754 ri = sc->sc_rb.rb_tdhead;
755
756 for (;;) {
757 IF_DEQUEUE(&ifp->if_snd, m);
758 if (m == 0)
759 break;
760
761 #if NBPFILTER > 0
762 /*
763 * If BPF is listening on this interface, let it see the
764 * packet before we commit it to the wire.
765 */
766 if (ifp->if_bpf)
767 bpf_mtap(ifp->if_bpf, m);
768 #endif
769
770 /*
771 * Copy the mbuf chain into the transmit buffer.
772 */
773 len = hme_put(sc, ri, m);
774
775 /*
776 * Initialize transmit registers and start transmission
777 */
778 HME_XD_SETFLAGS(txd, ri,
779 HME_XD_OWN | HME_XD_SOP | HME_XD_EOP |
780 HME_XD_ENCODE_TSIZE(len));
781
782 /*if (sc->sc_rb.rb_td_nbusy <= 0)*/
783 bus_space_write_4(sc->sc_bustag, sc->sc_etx, HME_ETXI_PENDING,
784 HME_ETX_TP_DMAWAKEUP);
785
786 if (++ri == ntbuf)
787 ri = 0;
788
789 if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
790 ifp->if_flags |= IFF_OACTIVE;
791 break;
792 }
793 }
794
795 sc->sc_rb.rb_tdhead = ri;
796 }
797
798 /*
799 * Transmit interrupt.
800 */
801 int
802 hme_tint(sc)
803 struct hme_softc *sc;
804 {
805 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
806 bus_space_tag_t t = sc->sc_bustag;
807 bus_space_handle_t mac = sc->sc_mac;
808 unsigned int ri, txflags;
809
810 /*
811 * Unload collision counters
812 */
813 ifp->if_collisions +=
814 bus_space_read_4(t, mac, HME_MACI_NCCNT) +
815 bus_space_read_4(t, mac, HME_MACI_FCCNT) +
816 bus_space_read_4(t, mac, HME_MACI_EXCNT) +
817 bus_space_read_4(t, mac, HME_MACI_LTCNT);
818
819 /*
820 * then clear the hardware counters.
821 */
822 bus_space_write_4(t, mac, HME_MACI_NCCNT, 0);
823 bus_space_write_4(t, mac, HME_MACI_FCCNT, 0);
824 bus_space_write_4(t, mac, HME_MACI_EXCNT, 0);
825 bus_space_write_4(t, mac, HME_MACI_LTCNT, 0);
826
827 /* Fetch current position in the transmit ring */
828 ri = sc->sc_rb.rb_tdtail;
829
830 for (;;) {
831 if (sc->sc_rb.rb_td_nbusy <= 0)
832 break;
833
834 txflags = HME_XD_GETFLAGS(sc->sc_rb.rb_txd, ri);
835
836 if (txflags & HME_XD_OWN)
837 break;
838
839 ifp->if_flags &= ~IFF_OACTIVE;
840 ifp->if_opackets++;
841
842 if (++ri == sc->sc_rb.rb_ntbuf)
843 ri = 0;
844
845 --sc->sc_rb.rb_td_nbusy;
846 }
847
848 /* Update ring */
849 sc->sc_rb.rb_tdtail = ri;
850
851 hme_start(ifp);
852
853 if (sc->sc_rb.rb_td_nbusy == 0)
854 ifp->if_timer = 0;
855
856 return (1);
857 }
858
859 /*
860 * Receive interrupt.
861 */
862 int
863 hme_rint(sc)
864 struct hme_softc *sc;
865 {
866 caddr_t xdr = sc->sc_rb.rb_rxd;
867 unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
868 unsigned int ri, len;
869 u_int32_t flags;
870
871 ri = sc->sc_rb.rb_rdtail;
872
873 /*
874 * Process all buffers with valid data.
875 */
876 for (;;) {
877 flags = HME_XD_GETFLAGS(xdr, ri);
878 if (flags & HME_XD_OWN)
879 break;
880
881 len = HME_XD_DECODE_RSIZE(flags);
882 hme_read(sc, ri, len);
883
884 /* This buffer can be used by the hardware again */
885 HME_XD_SETFLAGS(xdr, ri,
886 HME_XD_OWN | HME_XD_ENCODE_RSIZE(_HME_BUFSZ));
887
888 if (++ri == nrbuf)
889 ri = 0;
890 }
891
892 sc->sc_rb.rb_rdtail = ri;
893
894 return (1);
895 }
896
897 int
898 hme_eint(sc, status)
899 struct hme_softc *sc;
900 u_int status;
901 {
902 char bits[128];
903
904 if ((status & HME_SEB_STAT_MIFIRQ) != 0) {
905 printf("%s: XXXlink status changed\n", sc->sc_dev.dv_xname);
906 return (1);
907 }
908
909 printf("%s: status=%s\n", sc->sc_dev.dv_xname,
910 bitmask_snprintf(status, HME_SEB_STAT_BITS, bits,sizeof(bits)));
911 return (1);
912 }
913
914 int
915 hme_intr(v)
916 void *v;
917 {
918 struct hme_softc *sc = (struct hme_softc *)v;
919 bus_space_tag_t t = sc->sc_bustag;
920 bus_space_handle_t seb = sc->sc_seb;
921 u_int32_t status;
922 int r = 0;
923
924 status = bus_space_read_4(t, seb, HME_SEBI_STAT);
925
926 if ((status & HME_SEB_STAT_ALL_ERRORS) != 0)
927 r |= hme_eint(sc, status);
928
929 if ((status & (HME_SEB_STAT_TXALL | HME_SEB_STAT_HOSTTOTX)) != 0)
930 r |= hme_tint(sc);
931
932 if ((status & HME_SEB_STAT_RXTOHOST) != 0)
933 r |= hme_rint(sc);
934
935 return (r);
936 }
937
938
939 void
940 hme_watchdog(ifp)
941 struct ifnet *ifp;
942 {
943 struct hme_softc *sc = ifp->if_softc;
944
945 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
946 ++ifp->if_oerrors;
947
948 hme_reset(sc);
949 }
950
951 /*
952 * MII interface
953 */
954 static int
955 hme_mii_readreg(self, phy, reg)
956 struct device *self;
957 int phy, reg;
958 {
959 struct hme_softc *sc = (void *)self;
960 bus_space_tag_t t = sc->sc_bustag;
961 bus_space_handle_t mif = sc->sc_mif;
962 int n;
963 u_int32_t v;
964
965 /* Construct the frame command */
966 v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT) |
967 HME_MIF_FO_TAMSB |
968 (MII_COMMAND_READ << HME_MIF_FO_OPC_SHIFT) |
969 (phy << HME_MIF_FO_PHYAD_SHIFT) |
970 (reg << HME_MIF_FO_REGAD_SHIFT);
971
972 bus_space_write_4(t, mif, HME_MIFI_FO, v);
973 for (n = 0; n < 100; n++) {
974 DELAY(1);
975 v = bus_space_read_4(t, mif, HME_MIFI_FO);
976 if (v & HME_MIF_FO_TALSB)
977 return (v & HME_MIF_FO_DATA);
978 }
979
980 printf("%s: mii_read timeout\n", sc->sc_dev.dv_xname);
981 return (0);
982 }
983
984 static void
985 hme_mii_writereg(self, phy, reg, val)
986 struct device *self;
987 int phy, reg, val;
988 {
989 struct hme_softc *sc = (void *)self;
990 bus_space_tag_t t = sc->sc_bustag;
991 bus_space_handle_t mif = sc->sc_mif;
992 int n;
993 u_int32_t v;
994
995 /* Construct the frame command */
996 v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT) |
997 HME_MIF_FO_TAMSB |
998 (MII_COMMAND_WRITE << HME_MIF_FO_OPC_SHIFT) |
999 (phy << HME_MIF_FO_PHYAD_SHIFT) |
1000 (reg << HME_MIF_FO_REGAD_SHIFT) |
1001 (val & HME_MIF_FO_DATA);
1002
1003 bus_space_write_4(t, mif, HME_MIFI_FO, v);
1004 for (n = 0; n < 100; n++) {
1005 DELAY(1);
1006 v = bus_space_read_4(t, mif, HME_MIFI_FO);
1007 if (v & HME_MIF_FO_TALSB)
1008 return;
1009 }
1010
1011 printf("%s: mii_write timeout\n", sc->sc_dev.dv_xname);
1012 }
1013
1014 static void
1015 hme_mii_statchg(dev)
1016 struct device *dev;
1017 {
1018 #ifdef HMEDEBUG
1019 struct hme_softc *sc = (void *)dev;
1020 if (sc->sc_debug)
1021 printf("hme_mii_statchg: status change\n");
1022 #endif
1023 }
1024
1025 int
1026 hme_mediachange(ifp)
1027 struct ifnet *ifp;
1028 {
1029 struct hme_softc *sc = ifp->if_softc;
1030 struct ifmedia *ifm = &sc->sc_media;
1031 int newmedia = ifm->ifm_media;
1032 bus_space_tag_t t = sc->sc_bustag;
1033 bus_space_handle_t mac = sc->sc_mac;
1034 u_int32_t v;
1035 int error;
1036
1037 if (IFM_TYPE(newmedia) != IFM_ETHER)
1038 return (EINVAL);
1039
1040 if ((ifp->if_flags & IFF_UP) == 0)
1041 return (0);
1042
1043 if ((error = mii_mediachg(&sc->sc_mii)) != 0)
1044 return (error);
1045
1046 v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
1047 if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
1048 v |= HME_MAC_TXCFG_FULLDPLX;
1049 else
1050 v &= ~HME_MAC_TXCFG_FULLDPLX;
1051 bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
1052
1053 return (0);
1054 }
1055
1056 void
1057 hme_mediastatus(ifp, ifmr)
1058 struct ifnet *ifp;
1059 struct ifmediareq *ifmr;
1060 {
1061 struct hme_softc *sc = ifp->if_softc;
1062
1063 if ((ifp->if_flags & IFF_UP) == 0)
1064 return;
1065
1066 mii_pollstat(&sc->sc_mii);
1067 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1068 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1069 }
1070
1071 /*
1072 * Process an ioctl request.
1073 */
1074 int
1075 hme_ioctl(ifp, cmd, data)
1076 struct ifnet *ifp;
1077 u_long cmd;
1078 caddr_t data;
1079 {
1080 struct hme_softc *sc = ifp->if_softc;
1081 struct ifaddr *ifa = (struct ifaddr *)data;
1082 struct ifreq *ifr = (struct ifreq *)data;
1083 int s, error = 0;
1084
1085 s = splnet();
1086
1087 switch (cmd) {
1088
1089 case SIOCSIFADDR:
1090 ifp->if_flags |= IFF_UP;
1091
1092 switch (ifa->ifa_addr->sa_family) {
1093 #ifdef INET
1094 case AF_INET:
1095 hme_init(sc);
1096 arp_ifinit(ifp, ifa);
1097 break;
1098 #endif
1099 #ifdef NS
1100 case AF_NS:
1101 {
1102 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1103
1104 if (ns_nullhost(*ina))
1105 ina->x_host =
1106 *(union ns_host *)LLADDR(ifp->if_sadl);
1107 else {
1108 bcopy(ina->x_host.c_host,
1109 LLADDR(ifp->if_sadl),
1110 sizeof(sc->sc_enaddr));
1111 }
1112 /* Set new address. */
1113 hme_init(sc);
1114 break;
1115 }
1116 #endif
1117 default:
1118 hme_init(sc);
1119 break;
1120 }
1121 break;
1122
1123 case SIOCSIFFLAGS:
1124 if ((ifp->if_flags & IFF_UP) == 0 &&
1125 (ifp->if_flags & IFF_RUNNING) != 0) {
1126 /*
1127 * If interface is marked down and it is running, then
1128 * stop it.
1129 */
1130 hme_stop(sc);
1131 ifp->if_flags &= ~IFF_RUNNING;
1132 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1133 (ifp->if_flags & IFF_RUNNING) == 0) {
1134 /*
1135 * If interface is marked up and it is stopped, then
1136 * start it.
1137 */
1138 hme_init(sc);
1139 } else if ((ifp->if_flags & IFF_UP) != 0) {
1140 /*
1141 * Reset the interface to pick up changes in any other
1142 * flags that affect hardware registers.
1143 */
1144 /*hme_stop(sc);*/
1145 hme_init(sc);
1146 }
1147 #ifdef HMEDEBUG
1148 sc->sc_debug = (ifp->if_flags & IFF_DEBUG) != 0 ? 1 : 0;
1149 #endif
1150 break;
1151
1152 case SIOCADDMULTI:
1153 case SIOCDELMULTI:
1154 error = (cmd == SIOCADDMULTI) ?
1155 ether_addmulti(ifr, &sc->sc_ethercom) :
1156 ether_delmulti(ifr, &sc->sc_ethercom);
1157
1158 if (error == ENETRESET) {
1159 /*
1160 * Multicast list has changed; set the hardware filter
1161 * accordingly.
1162 */
1163 hme_setladrf(sc);
1164 error = 0;
1165 }
1166 break;
1167
1168 case SIOCGIFMEDIA:
1169 case SIOCSIFMEDIA:
1170 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1171 break;
1172
1173 default:
1174 error = EINVAL;
1175 break;
1176 }
1177
1178 splx(s);
1179 return (error);
1180 }
1181
1182 void
1183 hme_shutdown(arg)
1184 void *arg;
1185 {
1186
1187 hme_stop((struct hme_softc *)arg);
1188 }
1189
1190 /*
1191 * Set up the logical address filter.
1192 */
1193 void
1194 hme_setladrf(sc)
1195 struct hme_softc *sc;
1196 {
1197 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1198 struct ether_multi *enm;
1199 struct ether_multistep step;
1200 struct ethercom *ec = &sc->sc_ethercom;
1201 bus_space_tag_t t = sc->sc_bustag;
1202 bus_space_handle_t mac = sc->sc_mac;
1203 u_char *cp;
1204 u_int32_t crc;
1205 u_int32_t hash[4];
1206 int len;
1207
1208 /*
1209 * Set up multicast address filter by passing all multicast addresses
1210 * through a crc generator, and then using the high order 6 bits as an
1211 * index into the 64 bit logical address filter. The high order bit
1212 * selects the word, while the rest of the bits select the bit within
1213 * the word.
1214 */
1215
1216 if ((ifp->if_flags & IFF_PROMISC) != 0) {
1217 u_int32_t v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
1218 v |= HME_MAC_RXCFG_PMISC;
1219 bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
1220 goto allmulti;
1221 }
1222
1223 /* Clear hash table */
1224 hash[3] = hash[2] = hash[1] = hash[0] = 0;
1225 ETHER_FIRST_MULTI(step, ec, enm);
1226 while (enm != NULL) {
1227 if (ether_cmp(enm->enm_addrlo, enm->enm_addrhi)) {
1228 /*
1229 * We must listen to a range of multicast addresses.
1230 * For now, just accept all multicasts, rather than
1231 * trying to set only those filter bits needed to match
1232 * the range. (At this time, the only use of address
1233 * ranges is for IP multicast routing, for which the
1234 * range is big enough to require all bits set.)
1235 */
1236 goto allmulti;
1237 }
1238
1239 cp = enm->enm_addrlo;
1240 crc = 0xffffffff;
1241 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1242 int octet = *cp++;
1243 int i;
1244
1245 #define MC_POLY_LE 0xedb88320UL /* mcast crc, little endian */
1246 for (i = 0; i < 8; i++) {
1247 if ((crc & 1) ^ (octet & 1)) {
1248 crc >>= 1;
1249 crc ^= MC_POLY_LE;
1250 } else {
1251 crc >>= 1;
1252 }
1253 octet >>= 1;
1254 }
1255 }
1256 /* Just want the 6 most significant bits. */
1257 crc >>= 26;
1258
1259 /* Set the corresponding bit in the filter. */
1260 hash[crc >> 4] |= 1 << (crc & 0xf);
1261
1262 ETHER_NEXT_MULTI(step, enm);
1263 }
1264
1265 /* Now load the hash table onto the chip */
1266 bus_space_write_4(t, mac, HME_MACI_HASHTAB0, hash[0]);
1267 bus_space_write_4(t, mac, HME_MACI_HASHTAB1, hash[1]);
1268 bus_space_write_4(t, mac, HME_MACI_HASHTAB2, hash[2]);
1269 bus_space_write_4(t, mac, HME_MACI_HASHTAB3, hash[3]);
1270
1271 ifp->if_flags &= ~IFF_ALLMULTI;
1272 return;
1273
1274 allmulti:
1275 ifp->if_flags |= IFF_ALLMULTI;
1276 bus_space_write_4(t, mac, HME_MACI_HASHTAB0, 0xffff);
1277 bus_space_write_4(t, mac, HME_MACI_HASHTAB1, 0xffff);
1278 bus_space_write_4(t, mac, HME_MACI_HASHTAB2, 0xffff);
1279 bus_space_write_4(t, mac, HME_MACI_HASHTAB3, 0xffff);
1280 }
1281
1282 /*
1283 * Routines for accessing the transmit and receive buffers.
1284 * The various CPU and adapter configurations supported by this
1285 * driver require three different access methods for buffers
1286 * and descriptors:
1287 * (1) contig (contiguous data; no padding),
1288 * (2) gap2 (two bytes of data followed by two bytes of padding),
1289 * (3) gap16 (16 bytes of data followed by 16 bytes of padding).
1290 */
1291
1292 #if 0
1293 /*
1294 * contig: contiguous data with no padding.
1295 *
1296 * Buffers may have any alignment.
1297 */
1298
1299 void
1300 hme_copytobuf_contig(sc, from, ri, len)
1301 struct hme_softc *sc;
1302 void *from;
1303 int ri, len;
1304 {
1305 volatile caddr_t buf = sc->sc_rb.rb_txbuf + (ri * _HME_BUFSZ);
1306
1307 /*
1308 * Just call bcopy() to do the work.
1309 */
1310 bcopy(from, buf, len);
1311 }
1312
1313 void
1314 hme_copyfrombuf_contig(sc, to, boff, len)
1315 struct hme_softc *sc;
1316 void *to;
1317 int boff, len;
1318 {
1319 volatile caddr_t buf = sc->sc_rb.rb_rxbuf + (ri * _HME_BUFSZ);
1320
1321 /*
1322 * Just call bcopy() to do the work.
1323 */
1324 bcopy(buf, to, len);
1325 }
1326 #endif
1327