hme.c revision 1.61 1 /* $NetBSD: hme.c,v 1.61 2008/01/19 22:10:16 dyoung 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 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: hme.c,v 1.61 2008/01/19 22:10:16 dyoung Exp $");
45
46 /* #define HMEDEBUG */
47
48 #include "opt_inet.h"
49 #include "bpfilter.h"
50 #include "rnd.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/mbuf.h>
56 #include <sys/syslog.h>
57 #include <sys/socket.h>
58 #include <sys/device.h>
59 #include <sys/malloc.h>
60 #include <sys/ioctl.h>
61 #include <sys/errno.h>
62 #if NRND > 0
63 #include <sys/rnd.h>
64 #endif
65
66 #include <net/if.h>
67 #include <net/if_dl.h>
68 #include <net/if_ether.h>
69 #include <net/if_media.h>
70
71 #ifdef INET
72 #include <netinet/in.h>
73 #include <netinet/if_inarp.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/in_var.h>
76 #include <netinet/ip.h>
77 #include <netinet/tcp.h>
78 #include <netinet/udp.h>
79 #endif
80
81
82 #if NBPFILTER > 0
83 #include <net/bpf.h>
84 #include <net/bpfdesc.h>
85 #endif
86
87 #include <dev/mii/mii.h>
88 #include <dev/mii/miivar.h>
89
90 #include <sys/bus.h>
91
92 #include <dev/ic/hmereg.h>
93 #include <dev/ic/hmevar.h>
94
95 void hme_start(struct ifnet *);
96 void hme_stop(struct hme_softc *,bool);
97 int hme_ioctl(struct ifnet *, u_long, void *);
98 void hme_tick(void *);
99 void hme_watchdog(struct ifnet *);
100 void hme_shutdown(void *);
101 int hme_init(struct hme_softc *);
102 void hme_meminit(struct hme_softc *);
103 void hme_mifinit(struct hme_softc *);
104 void hme_reset(struct hme_softc *);
105 void hme_setladrf(struct hme_softc *);
106
107 /* MII methods & callbacks */
108 static int hme_mii_readreg(struct device *, int, int);
109 static void hme_mii_writereg(struct device *, int, int, int);
110 static void hme_mii_statchg(struct device *);
111
112 int hme_mediachange(struct ifnet *);
113
114 struct mbuf *hme_get(struct hme_softc *, int, uint32_t);
115 int hme_put(struct hme_softc *, int, struct mbuf *);
116 void hme_read(struct hme_softc *, int, uint32_t);
117 int hme_eint(struct hme_softc *, u_int);
118 int hme_rint(struct hme_softc *);
119 int hme_tint(struct hme_softc *);
120
121 static int ether_cmp(u_char *, u_char *);
122
123 /* Default buffer copy routines */
124 void hme_copytobuf_contig(struct hme_softc *, void *, int, int);
125 void hme_copyfrombuf_contig(struct hme_softc *, void *, int, int);
126 void hme_zerobuf_contig(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 struct mii_softc *child;
136 bus_dma_tag_t dmatag = sc->sc_dmatag;
137 bus_dma_segment_t seg;
138 bus_size_t size;
139 int rseg, error;
140
141 /*
142 * HME common initialization.
143 *
144 * hme_softc fields that must be initialized by the front-end:
145 *
146 * the bus tag:
147 * sc_bustag
148 *
149 * the DMA bus tag:
150 * sc_dmatag
151 *
152 * the bus handles:
153 * sc_seb (Shared Ethernet Block registers)
154 * sc_erx (Receiver Unit registers)
155 * sc_etx (Transmitter Unit registers)
156 * sc_mac (MAC registers)
157 * sc_mif (Management Interface registers)
158 *
159 * the maximum bus burst size:
160 * sc_burst
161 *
162 * (notyet:DMA capable memory for the ring descriptors & packet buffers:
163 * rb_membase, rb_dmabase)
164 *
165 * the local Ethernet address:
166 * sc_enaddr
167 *
168 */
169
170 /* Make sure the chip is stopped. */
171 hme_stop(sc, true);
172
173
174 /*
175 * Allocate descriptors and buffers
176 * XXX - do all this differently.. and more configurably,
177 * eg. use things as `dma_load_mbuf()' on transmit,
178 * and a pool of `EXTMEM' mbufs (with buffers DMA-mapped
179 * all the time) on the receiver side.
180 *
181 * Note: receive buffers must be 64-byte aligned.
182 * Also, apparently, the buffers must extend to a DMA burst
183 * boundary beyond the maximum packet size.
184 */
185 #define _HME_NDESC 128
186 #define _HME_BUFSZ 1600
187
188 /* Note: the # of descriptors must be a multiple of 16 */
189 sc->sc_rb.rb_ntbuf = _HME_NDESC;
190 sc->sc_rb.rb_nrbuf = _HME_NDESC;
191
192 /*
193 * Allocate DMA capable memory
194 * Buffer descriptors must be aligned on a 2048 byte boundary;
195 * take this into account when calculating the size. Note that
196 * the maximum number of descriptors (256) occupies 2048 bytes,
197 * so we allocate that much regardless of _HME_NDESC.
198 */
199 size = 2048 + /* TX descriptors */
200 2048 + /* RX descriptors */
201 sc->sc_rb.rb_ntbuf * _HME_BUFSZ + /* TX buffers */
202 sc->sc_rb.rb_nrbuf * _HME_BUFSZ; /* RX buffers */
203
204 /* Allocate DMA buffer */
205 if ((error = bus_dmamem_alloc(dmatag, size,
206 2048, 0,
207 &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
208 printf("%s: DMA buffer alloc error %d\n",
209 sc->sc_dev.dv_xname, error);
210 return;
211 }
212
213 /* Map DMA memory in CPU addressable space */
214 if ((error = bus_dmamem_map(dmatag, &seg, rseg, size,
215 &sc->sc_rb.rb_membase,
216 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
217 printf("%s: DMA buffer map error %d\n",
218 sc->sc_dev.dv_xname, error);
219 bus_dmamap_unload(dmatag, sc->sc_dmamap);
220 bus_dmamem_free(dmatag, &seg, rseg);
221 return;
222 }
223
224 if ((error = bus_dmamap_create(dmatag, size, 1, size, 0,
225 BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
226 printf("%s: DMA map create error %d\n",
227 sc->sc_dev.dv_xname, error);
228 return;
229 }
230
231 /* Load the buffer */
232 if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
233 sc->sc_rb.rb_membase, size, NULL,
234 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
235 printf("%s: DMA buffer map load error %d\n",
236 sc->sc_dev.dv_xname, error);
237 bus_dmamem_free(dmatag, &seg, rseg);
238 return;
239 }
240 sc->sc_rb.rb_dmabase = sc->sc_dmamap->dm_segs[0].ds_addr;
241
242 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
243 ether_sprintf(sc->sc_enaddr));
244
245 /* Initialize ifnet structure. */
246 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
247 ifp->if_softc = sc;
248 ifp->if_start = hme_start;
249 ifp->if_ioctl = hme_ioctl;
250 ifp->if_watchdog = hme_watchdog;
251 ifp->if_flags =
252 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
253 sc->sc_if_flags = ifp->if_flags;
254 ifp->if_capabilities |=
255 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
256 IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
257 IFQ_SET_READY(&ifp->if_snd);
258
259 /* Initialize ifmedia structures and MII info */
260 mii->mii_ifp = ifp;
261 mii->mii_readreg = hme_mii_readreg;
262 mii->mii_writereg = hme_mii_writereg;
263 mii->mii_statchg = hme_mii_statchg;
264
265 sc->sc_ethercom.ec_mii = mii;
266 ifmedia_init(&mii->mii_media, 0, hme_mediachange, ether_mediastatus);
267
268 hme_mifinit(sc);
269
270 mii_attach(&sc->sc_dev, mii, 0xffffffff,
271 MII_PHY_ANY, MII_OFFSET_ANY, MIIF_FORCEANEG);
272
273 child = LIST_FIRST(&mii->mii_phys);
274 if (child == NULL) {
275 /* No PHY attached */
276 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
277 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
278 } else {
279 /*
280 * Walk along the list of attached MII devices and
281 * establish an `MII instance' to `phy number'
282 * mapping. We'll use this mapping in media change
283 * requests to determine which phy to use to program
284 * the MIF configuration register.
285 */
286 for (; child != NULL; child = LIST_NEXT(child, mii_list)) {
287 /*
288 * Note: we support just two PHYs: the built-in
289 * internal device and an external on the MII
290 * connector.
291 */
292 if (child->mii_phy > 1 || child->mii_inst > 1) {
293 printf("%s: cannot accommodate MII device %s"
294 " at phy %d, instance %d\n",
295 sc->sc_dev.dv_xname,
296 child->mii_dev.dv_xname,
297 child->mii_phy, child->mii_inst);
298 continue;
299 }
300
301 sc->sc_phys[child->mii_inst] = child->mii_phy;
302 }
303
304 /*
305 * XXX - we can really do the following ONLY if the
306 * phy indeed has the auto negotiation capability!!
307 */
308 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
309 }
310
311 /* claim 802.1q capability */
312 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
313
314 /* Attach the interface. */
315 if_attach(ifp);
316 ether_ifattach(ifp, sc->sc_enaddr);
317
318 sc->sc_sh = shutdownhook_establish(hme_shutdown, sc);
319 if (sc->sc_sh == NULL)
320 panic("hme_config: can't establish shutdownhook");
321
322 #if NRND > 0
323 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
324 RND_TYPE_NET, 0);
325 #endif
326
327 callout_init(&sc->sc_tick_ch, 0);
328 }
329
330 void
331 hme_tick(arg)
332 void *arg;
333 {
334 struct hme_softc *sc = arg;
335 int s;
336
337 s = splnet();
338 mii_tick(&sc->sc_mii);
339 splx(s);
340
341 callout_reset(&sc->sc_tick_ch, hz, hme_tick, sc);
342 }
343
344 void
345 hme_reset(sc)
346 struct hme_softc *sc;
347 {
348 int s;
349
350 s = splnet();
351 (void)hme_init(sc);
352 splx(s);
353 }
354
355 void
356 hme_stop(struct hme_softc *sc, bool chip_only)
357 {
358 bus_space_tag_t t = sc->sc_bustag;
359 bus_space_handle_t seb = sc->sc_seb;
360 int n;
361
362 if (!chip_only) {
363 callout_stop(&sc->sc_tick_ch);
364 mii_down(&sc->sc_mii);
365 }
366
367 /* Mask all interrupts */
368 bus_space_write_4(t, seb, HME_SEBI_IMASK, 0xffffffff);
369
370 /* Reset transmitter and receiver */
371 bus_space_write_4(t, seb, HME_SEBI_RESET,
372 (HME_SEB_RESET_ETX | HME_SEB_RESET_ERX));
373
374 for (n = 0; n < 20; n++) {
375 u_int32_t v = bus_space_read_4(t, seb, HME_SEBI_RESET);
376 if ((v & (HME_SEB_RESET_ETX | HME_SEB_RESET_ERX)) == 0)
377 return;
378 DELAY(20);
379 }
380
381 printf("%s: hme_stop: reset failed\n", sc->sc_dev.dv_xname);
382 }
383
384 void
385 hme_meminit(sc)
386 struct hme_softc *sc;
387 {
388 bus_addr_t txbufdma, rxbufdma;
389 bus_addr_t dma;
390 char *p;
391 unsigned int ntbuf, nrbuf, i;
392 struct hme_ring *hr = &sc->sc_rb;
393
394 p = hr->rb_membase;
395 dma = hr->rb_dmabase;
396
397 ntbuf = hr->rb_ntbuf;
398 nrbuf = hr->rb_nrbuf;
399
400 /*
401 * Allocate transmit descriptors
402 */
403 hr->rb_txd = p;
404 hr->rb_txddma = dma;
405 p += ntbuf * HME_XD_SIZE;
406 dma += ntbuf * HME_XD_SIZE;
407 /* We have reserved descriptor space until the next 2048 byte boundary.*/
408 dma = (bus_addr_t)roundup((u_long)dma, 2048);
409 p = (void *)roundup((u_long)p, 2048);
410
411 /*
412 * Allocate receive descriptors
413 */
414 hr->rb_rxd = p;
415 hr->rb_rxddma = dma;
416 p += nrbuf * HME_XD_SIZE;
417 dma += nrbuf * HME_XD_SIZE;
418 /* Again move forward to the next 2048 byte boundary.*/
419 dma = (bus_addr_t)roundup((u_long)dma, 2048);
420 p = (void *)roundup((u_long)p, 2048);
421
422
423 /*
424 * Allocate transmit buffers
425 */
426 hr->rb_txbuf = p;
427 txbufdma = dma;
428 p += ntbuf * _HME_BUFSZ;
429 dma += ntbuf * _HME_BUFSZ;
430
431 /*
432 * Allocate receive buffers
433 */
434 hr->rb_rxbuf = p;
435 rxbufdma = dma;
436 p += nrbuf * _HME_BUFSZ;
437 dma += nrbuf * _HME_BUFSZ;
438
439 /*
440 * Initialize transmit buffer descriptors
441 */
442 for (i = 0; i < ntbuf; i++) {
443 HME_XD_SETADDR(sc->sc_pci, hr->rb_txd, i, txbufdma + i * _HME_BUFSZ);
444 HME_XD_SETFLAGS(sc->sc_pci, hr->rb_txd, i, 0);
445 }
446
447 /*
448 * Initialize receive buffer descriptors
449 */
450 for (i = 0; i < nrbuf; i++) {
451 HME_XD_SETADDR(sc->sc_pci, hr->rb_rxd, i, rxbufdma + i * _HME_BUFSZ);
452 HME_XD_SETFLAGS(sc->sc_pci, hr->rb_rxd, i,
453 HME_XD_OWN | HME_XD_ENCODE_RSIZE(_HME_BUFSZ));
454 }
455
456 hr->rb_tdhead = hr->rb_tdtail = 0;
457 hr->rb_td_nbusy = 0;
458 hr->rb_rdtail = 0;
459 }
460
461 /*
462 * Initialization of interface; set up initialization block
463 * and transmit/receive descriptor rings.
464 */
465 int
466 hme_init(sc)
467 struct hme_softc *sc;
468 {
469 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
470 bus_space_tag_t t = sc->sc_bustag;
471 bus_space_handle_t seb = sc->sc_seb;
472 bus_space_handle_t etx = sc->sc_etx;
473 bus_space_handle_t erx = sc->sc_erx;
474 bus_space_handle_t mac = sc->sc_mac;
475 u_int8_t *ea;
476 u_int32_t v;
477 int rc;
478
479 /*
480 * Initialization sequence. The numbered steps below correspond
481 * to the sequence outlined in section 6.3.5.1 in the Ethernet
482 * Channel Engine manual (part of the PCIO manual).
483 * See also the STP2002-STQ document from Sun Microsystems.
484 */
485
486 /* step 1 & 2. Reset the Ethernet Channel */
487 hme_stop(sc, false);
488
489 /* Re-initialize the MIF */
490 hme_mifinit(sc);
491
492 /* Call MI reset function if any */
493 if (sc->sc_hwreset)
494 (*sc->sc_hwreset)(sc);
495
496 #if 0
497 /* Mask all MIF interrupts, just in case */
498 bus_space_write_4(t, mif, HME_MIFI_IMASK, 0xffff);
499 #endif
500
501 /* step 3. Setup data structures in host memory */
502 hme_meminit(sc);
503
504 /* step 4. TX MAC registers & counters */
505 bus_space_write_4(t, mac, HME_MACI_NCCNT, 0);
506 bus_space_write_4(t, mac, HME_MACI_FCCNT, 0);
507 bus_space_write_4(t, mac, HME_MACI_EXCNT, 0);
508 bus_space_write_4(t, mac, HME_MACI_LTCNT, 0);
509 bus_space_write_4(t, mac, HME_MACI_TXSIZE,
510 (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
511 ETHER_VLAN_ENCAP_LEN + ETHER_MAX_LEN : ETHER_MAX_LEN);
512 sc->sc_ec_capenable = sc->sc_ethercom.ec_capenable;
513
514 /* Load station MAC address */
515 ea = sc->sc_enaddr;
516 bus_space_write_4(t, mac, HME_MACI_MACADDR0, (ea[0] << 8) | ea[1]);
517 bus_space_write_4(t, mac, HME_MACI_MACADDR1, (ea[2] << 8) | ea[3]);
518 bus_space_write_4(t, mac, HME_MACI_MACADDR2, (ea[4] << 8) | ea[5]);
519
520 /*
521 * Init seed for backoff
522 * (source suggested by manual: low 10 bits of MAC address)
523 */
524 v = ((ea[4] << 8) | ea[5]) & 0x3fff;
525 bus_space_write_4(t, mac, HME_MACI_RANDSEED, v);
526
527
528 /* Note: Accepting power-on default for other MAC registers here.. */
529
530
531 /* step 5. RX MAC registers & counters */
532 hme_setladrf(sc);
533
534 /* step 6 & 7. Program Descriptor Ring Base Addresses */
535 bus_space_write_4(t, etx, HME_ETXI_RING, sc->sc_rb.rb_txddma);
536 bus_space_write_4(t, etx, HME_ETXI_RSIZE, sc->sc_rb.rb_ntbuf);
537
538 bus_space_write_4(t, erx, HME_ERXI_RING, sc->sc_rb.rb_rxddma);
539 bus_space_write_4(t, mac, HME_MACI_RXSIZE,
540 (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
541 ETHER_VLAN_ENCAP_LEN + ETHER_MAX_LEN : ETHER_MAX_LEN);
542
543 /* step 8. Global Configuration & Interrupt Mask */
544 bus_space_write_4(t, seb, HME_SEBI_IMASK,
545 ~(
546 /*HME_SEB_STAT_GOTFRAME | HME_SEB_STAT_SENTFRAME |*/
547 HME_SEB_STAT_HOSTTOTX |
548 HME_SEB_STAT_RXTOHOST |
549 HME_SEB_STAT_TXALL |
550 HME_SEB_STAT_TXPERR |
551 HME_SEB_STAT_RCNTEXP |
552 /*HME_SEB_STAT_MIFIRQ |*/
553 HME_SEB_STAT_ALL_ERRORS ));
554
555 switch (sc->sc_burst) {
556 default:
557 v = 0;
558 break;
559 case 16:
560 v = HME_SEB_CFG_BURST16;
561 break;
562 case 32:
563 v = HME_SEB_CFG_BURST32;
564 break;
565 case 64:
566 v = HME_SEB_CFG_BURST64;
567 break;
568 }
569 bus_space_write_4(t, seb, HME_SEBI_CFG, v);
570
571 /* step 9. ETX Configuration: use mostly default values */
572
573 /* Enable DMA */
574 v = bus_space_read_4(t, etx, HME_ETXI_CFG);
575 v |= HME_ETX_CFG_DMAENABLE;
576 bus_space_write_4(t, etx, HME_ETXI_CFG, v);
577
578 /* Transmit Descriptor ring size: in increments of 16 */
579 bus_space_write_4(t, etx, HME_ETXI_RSIZE, _HME_NDESC / 16 - 1);
580
581
582 /* step 10. ERX Configuration */
583 v = bus_space_read_4(t, erx, HME_ERXI_CFG);
584
585 /* Encode Receive Descriptor ring size: four possible values */
586 switch (_HME_NDESC /*XXX*/) {
587 case 32:
588 v |= HME_ERX_CFG_RINGSIZE32;
589 break;
590 case 64:
591 v |= HME_ERX_CFG_RINGSIZE64;
592 break;
593 case 128:
594 v |= HME_ERX_CFG_RINGSIZE128;
595 break;
596 case 256:
597 v |= HME_ERX_CFG_RINGSIZE256;
598 break;
599 default:
600 printf("hme: invalid Receive Descriptor ring size\n");
601 break;
602 }
603
604 /* Enable DMA */
605 v |= HME_ERX_CFG_DMAENABLE;
606
607 /* set h/w rx checksum start offset (# of half-words) */
608 #ifdef INET
609 v |= (((ETHER_HDR_LEN + sizeof(struct ip) +
610 ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
611 ETHER_VLAN_ENCAP_LEN : 0)) / 2) << HME_ERX_CFG_CSUMSHIFT) &
612 HME_ERX_CFG_CSUMSTART;
613 #endif
614 bus_space_write_4(t, erx, HME_ERXI_CFG, v);
615
616 /* step 11. XIF Configuration */
617 v = bus_space_read_4(t, mac, HME_MACI_XIF);
618 v |= HME_MAC_XIF_OE;
619 bus_space_write_4(t, mac, HME_MACI_XIF, v);
620
621 /* step 12. RX_MAC Configuration Register */
622 v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
623 v |= HME_MAC_RXCFG_ENABLE | HME_MAC_RXCFG_PSTRIP;
624 bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
625
626 /* step 13. TX_MAC Configuration Register */
627 v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
628 v |= (HME_MAC_TXCFG_ENABLE | HME_MAC_TXCFG_DGIVEUP);
629 bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
630
631 /* step 14. Issue Transmit Pending command */
632
633 /* Call MI initialization function if any */
634 if (sc->sc_hwinit)
635 (*sc->sc_hwinit)(sc);
636
637 /* Set the current media. */
638 if ((rc = hme_mediachange(ifp)) != 0)
639 return rc;
640
641 /* Start the one second timer. */
642 callout_reset(&sc->sc_tick_ch, hz, hme_tick, sc);
643
644 ifp->if_flags |= IFF_RUNNING;
645 ifp->if_flags &= ~IFF_OACTIVE;
646 sc->sc_if_flags = ifp->if_flags;
647 ifp->if_timer = 0;
648 hme_start(ifp);
649 return 0;
650 }
651
652 /*
653 * Compare two Ether/802 addresses for equality, inlined and unrolled for
654 * speed.
655 */
656 static inline int
657 ether_cmp(a, b)
658 u_char *a, *b;
659 {
660
661 if (a[5] != b[5] || a[4] != b[4] || a[3] != b[3] ||
662 a[2] != b[2] || a[1] != b[1] || a[0] != b[0])
663 return (0);
664 return (1);
665 }
666
667
668 /*
669 * Routine to copy from mbuf chain to transmit buffer in
670 * network buffer memory.
671 * Returns the amount of data copied.
672 */
673 int
674 hme_put(sc, ri, m)
675 struct hme_softc *sc;
676 int ri; /* Ring index */
677 struct mbuf *m;
678 {
679 struct mbuf *n;
680 int len, tlen = 0;
681 char *bp;
682
683 bp = (char *)sc->sc_rb.rb_txbuf + (ri % sc->sc_rb.rb_ntbuf) * _HME_BUFSZ;
684 for (; m; m = n) {
685 len = m->m_len;
686 if (len == 0) {
687 MFREE(m, n);
688 continue;
689 }
690 memcpy(bp, mtod(m, void *), len);
691 bp += len;
692 tlen += len;
693 MFREE(m, n);
694 }
695 return (tlen);
696 }
697
698 /*
699 * Pull data off an interface.
700 * Len is length of data, with local net header stripped.
701 * We copy the data into mbufs. When full cluster sized units are present
702 * we copy into clusters.
703 */
704 struct mbuf *
705 hme_get(sc, ri, flags)
706 struct hme_softc *sc;
707 int ri;
708 u_int32_t flags;
709 {
710 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
711 struct mbuf *m, *m0, *newm;
712 char *bp;
713 int len, totlen;
714
715 totlen = HME_XD_DECODE_RSIZE(flags);
716 MGETHDR(m0, M_DONTWAIT, MT_DATA);
717 if (m0 == 0)
718 return (0);
719 m0->m_pkthdr.rcvif = ifp;
720 m0->m_pkthdr.len = totlen;
721 len = MHLEN;
722 m = m0;
723
724 bp = (char *)sc->sc_rb.rb_rxbuf + (ri % sc->sc_rb.rb_nrbuf) * _HME_BUFSZ;
725
726 while (totlen > 0) {
727 if (totlen >= MINCLSIZE) {
728 MCLGET(m, M_DONTWAIT);
729 if ((m->m_flags & M_EXT) == 0)
730 goto bad;
731 len = MCLBYTES;
732 }
733
734 if (m == m0) {
735 char *newdata = (char *)
736 ALIGN(m->m_data + sizeof(struct ether_header)) -
737 sizeof(struct ether_header);
738 len -= newdata - m->m_data;
739 m->m_data = newdata;
740 }
741
742 m->m_len = len = min(totlen, len);
743 memcpy(mtod(m, void *), bp, len);
744 bp += len;
745
746 totlen -= len;
747 if (totlen > 0) {
748 MGET(newm, M_DONTWAIT, MT_DATA);
749 if (newm == 0)
750 goto bad;
751 len = MLEN;
752 m = m->m_next = newm;
753 }
754 }
755
756 #ifdef INET
757 /* hardware checksum */
758 if (ifp->if_csum_flags_rx & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
759 struct ether_header *eh;
760 struct ip *ip;
761 struct udphdr *uh;
762 uint16_t *opts;
763 int32_t hlen, pktlen;
764 uint32_t temp;
765
766 if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) {
767 pktlen = m0->m_pkthdr.len - ETHER_HDR_LEN -
768 ETHER_VLAN_ENCAP_LEN;
769 eh = (struct ether_header *) mtod(m0, void *) +
770 ETHER_VLAN_ENCAP_LEN;
771 } else {
772 pktlen = m0->m_pkthdr.len - ETHER_HDR_LEN;
773 eh = mtod(m0, struct ether_header *);
774 }
775 if (ntohs(eh->ether_type) != ETHERTYPE_IP)
776 goto swcsum;
777 ip = (struct ip *) ((char *)eh + ETHER_HDR_LEN);
778
779 /* IPv4 only */
780 if (ip->ip_v != IPVERSION)
781 goto swcsum;
782
783 hlen = ip->ip_hl << 2;
784 if (hlen < sizeof(struct ip))
785 goto swcsum;
786
787 /*
788 * bail if too short, has random trailing garbage, truncated,
789 * fragment, or has ethernet pad.
790 */
791 if ((ntohs(ip->ip_len) < hlen) || (ntohs(ip->ip_len) != pktlen)
792 || (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)))
793 goto swcsum;
794
795 switch (ip->ip_p) {
796 case IPPROTO_TCP:
797 if (! (ifp->if_csum_flags_rx & M_CSUM_TCPv4))
798 goto swcsum;
799 if (pktlen < (hlen + sizeof(struct tcphdr)))
800 goto swcsum;
801 m0->m_pkthdr.csum_flags = M_CSUM_TCPv4;
802 break;
803 case IPPROTO_UDP:
804 if (! (ifp->if_csum_flags_rx & M_CSUM_UDPv4))
805 goto swcsum;
806 if (pktlen < (hlen + sizeof(struct udphdr)))
807 goto swcsum;
808 uh = (struct udphdr *)((char *)ip + hlen);
809 /* no checksum */
810 if (uh->uh_sum == 0)
811 goto swcsum;
812 m0->m_pkthdr.csum_flags = M_CSUM_UDPv4;
813 break;
814 default:
815 goto swcsum;
816 }
817
818 /* w/ M_CSUM_NO_PSEUDOHDR, the uncomplemented sum is expected */
819 m0->m_pkthdr.csum_data = (~flags) & HME_XD_RXCKSUM;
820
821 /* if the pkt had ip options, we have to deduct them */
822 if (hlen > sizeof(struct ip)) {
823 uint32_t optsum;
824
825 optsum = 0;
826 temp = hlen - sizeof(struct ip);
827 opts = (uint16_t *)((char *)ip + sizeof(struct ip));
828
829 while (temp > 1) {
830 optsum += ntohs(*opts++);
831 temp -= 2;
832 }
833 while (optsum >> 16)
834 optsum = (optsum >> 16) + (optsum & 0xffff);
835
836 /* Deduct the ip opts sum from the hwsum (rfc 1624). */
837 m0->m_pkthdr.csum_data = ~((~m0->m_pkthdr.csum_data) -
838 ~optsum);
839
840 while (m0->m_pkthdr.csum_data >> 16)
841 m0->m_pkthdr.csum_data =
842 (m0->m_pkthdr.csum_data >> 16) +
843 (m0->m_pkthdr.csum_data & 0xffff);
844 }
845
846 m0->m_pkthdr.csum_flags |= M_CSUM_DATA | M_CSUM_NO_PSEUDOHDR;
847 }
848 swcsum:
849 m0->m_pkthdr.csum_flags = 0;
850 #endif
851
852 return (m0);
853
854 bad:
855 m_freem(m0);
856 return (0);
857 }
858
859 /*
860 * Pass a packet to the higher levels.
861 */
862 void
863 hme_read(sc, ix, flags)
864 struct hme_softc *sc;
865 int ix;
866 u_int32_t flags;
867 {
868 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
869 struct mbuf *m;
870 int len;
871
872 len = HME_XD_DECODE_RSIZE(flags);
873 if (len <= sizeof(struct ether_header) ||
874 len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
875 ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
876 ETHERMTU + sizeof(struct ether_header))) {
877 #ifdef HMEDEBUG
878 printf("%s: invalid packet size %d; dropping\n",
879 sc->sc_dev.dv_xname, len);
880 #endif
881 ifp->if_ierrors++;
882 return;
883 }
884
885 /* Pull packet off interface. */
886 m = hme_get(sc, ix, flags);
887 if (m == 0) {
888 ifp->if_ierrors++;
889 return;
890 }
891
892 ifp->if_ipackets++;
893
894 #if NBPFILTER > 0
895 /*
896 * Check if there's a BPF listener on this interface.
897 * If so, hand off the raw packet to BPF.
898 */
899 if (ifp->if_bpf)
900 bpf_mtap(ifp->if_bpf, m);
901 #endif
902
903 /* Pass the packet up. */
904 (*ifp->if_input)(ifp, m);
905 }
906
907 void
908 hme_start(ifp)
909 struct ifnet *ifp;
910 {
911 struct hme_softc *sc = (struct hme_softc *)ifp->if_softc;
912 void *txd = sc->sc_rb.rb_txd;
913 struct mbuf *m;
914 unsigned int txflags;
915 unsigned int ri, len;
916 unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
917
918 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
919 return;
920
921 ri = sc->sc_rb.rb_tdhead;
922
923 for (;;) {
924 IFQ_DEQUEUE(&ifp->if_snd, m);
925 if (m == 0)
926 break;
927
928 #if NBPFILTER > 0
929 /*
930 * If BPF is listening on this interface, let it see the
931 * packet before we commit it to the wire.
932 */
933 if (ifp->if_bpf)
934 bpf_mtap(ifp->if_bpf, m);
935 #endif
936
937 #ifdef INET
938 /* collect bits for h/w csum, before hme_put frees the mbuf */
939 if (ifp->if_csum_flags_tx & (M_CSUM_TCPv4 | M_CSUM_UDPv4) &&
940 m->m_pkthdr.csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
941 struct ether_header *eh;
942 uint16_t offset, start;
943
944 eh = mtod(m, struct ether_header *);
945 switch (ntohs(eh->ether_type)) {
946 case ETHERTYPE_IP:
947 start = ETHER_HDR_LEN;
948 break;
949 case ETHERTYPE_VLAN:
950 start = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
951 break;
952 default:
953 /* unsupported, drop it */
954 m_free(m);
955 continue;
956 }
957 start += M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data);
958 offset = M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data)
959 + start;
960 txflags = HME_XD_TXCKSUM |
961 (offset << HME_XD_TXCSSTUFFSHIFT) |
962 (start << HME_XD_TXCSSTARTSHIFT);
963 } else
964 #endif
965 txflags = 0;
966
967 /*
968 * Copy the mbuf chain into the transmit buffer.
969 */
970 len = hme_put(sc, ri, m);
971
972 /*
973 * Initialize transmit registers and start transmission
974 */
975 HME_XD_SETFLAGS(sc->sc_pci, txd, ri,
976 HME_XD_OWN | HME_XD_SOP | HME_XD_EOP |
977 HME_XD_ENCODE_TSIZE(len) | txflags);
978
979 /*if (sc->sc_rb.rb_td_nbusy <= 0)*/
980 bus_space_write_4(sc->sc_bustag, sc->sc_etx, HME_ETXI_PENDING,
981 HME_ETX_TP_DMAWAKEUP);
982
983 if (++ri == ntbuf)
984 ri = 0;
985
986 if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
987 ifp->if_flags |= IFF_OACTIVE;
988 break;
989 }
990 }
991
992 sc->sc_rb.rb_tdhead = ri;
993 }
994
995 /*
996 * Transmit interrupt.
997 */
998 int
999 hme_tint(sc)
1000 struct hme_softc *sc;
1001 {
1002 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1003 bus_space_tag_t t = sc->sc_bustag;
1004 bus_space_handle_t mac = sc->sc_mac;
1005 unsigned int ri, txflags;
1006
1007 /*
1008 * Unload collision counters
1009 */
1010 ifp->if_collisions +=
1011 bus_space_read_4(t, mac, HME_MACI_NCCNT) +
1012 bus_space_read_4(t, mac, HME_MACI_FCCNT) +
1013 bus_space_read_4(t, mac, HME_MACI_EXCNT) +
1014 bus_space_read_4(t, mac, HME_MACI_LTCNT);
1015
1016 /*
1017 * then clear the hardware counters.
1018 */
1019 bus_space_write_4(t, mac, HME_MACI_NCCNT, 0);
1020 bus_space_write_4(t, mac, HME_MACI_FCCNT, 0);
1021 bus_space_write_4(t, mac, HME_MACI_EXCNT, 0);
1022 bus_space_write_4(t, mac, HME_MACI_LTCNT, 0);
1023
1024 /* Fetch current position in the transmit ring */
1025 ri = sc->sc_rb.rb_tdtail;
1026
1027 for (;;) {
1028 if (sc->sc_rb.rb_td_nbusy <= 0)
1029 break;
1030
1031 txflags = HME_XD_GETFLAGS(sc->sc_pci, sc->sc_rb.rb_txd, ri);
1032
1033 if (txflags & HME_XD_OWN)
1034 break;
1035
1036 ifp->if_flags &= ~IFF_OACTIVE;
1037 ifp->if_opackets++;
1038
1039 if (++ri == sc->sc_rb.rb_ntbuf)
1040 ri = 0;
1041
1042 --sc->sc_rb.rb_td_nbusy;
1043 }
1044
1045 /* Update ring */
1046 sc->sc_rb.rb_tdtail = ri;
1047
1048 hme_start(ifp);
1049
1050 if (sc->sc_rb.rb_td_nbusy == 0)
1051 ifp->if_timer = 0;
1052
1053 return (1);
1054 }
1055
1056 /*
1057 * Receive interrupt.
1058 */
1059 int
1060 hme_rint(sc)
1061 struct hme_softc *sc;
1062 {
1063 void *xdr = sc->sc_rb.rb_rxd;
1064 unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
1065 unsigned int ri;
1066 u_int32_t flags;
1067
1068 ri = sc->sc_rb.rb_rdtail;
1069
1070 /*
1071 * Process all buffers with valid data.
1072 */
1073 for (;;) {
1074 flags = HME_XD_GETFLAGS(sc->sc_pci, xdr, ri);
1075 if (flags & HME_XD_OWN)
1076 break;
1077
1078 if (flags & HME_XD_OFL) {
1079 printf("%s: buffer overflow, ri=%d; flags=0x%x\n",
1080 sc->sc_dev.dv_xname, ri, flags);
1081 } else
1082 hme_read(sc, ri, flags);
1083
1084 /* This buffer can be used by the hardware again */
1085 HME_XD_SETFLAGS(sc->sc_pci, xdr, ri,
1086 HME_XD_OWN | HME_XD_ENCODE_RSIZE(_HME_BUFSZ));
1087
1088 if (++ri == nrbuf)
1089 ri = 0;
1090 }
1091
1092 sc->sc_rb.rb_rdtail = ri;
1093
1094 return (1);
1095 }
1096
1097 int
1098 hme_eint(sc, status)
1099 struct hme_softc *sc;
1100 u_int status;
1101 {
1102 char bits[128];
1103
1104 if ((status & HME_SEB_STAT_MIFIRQ) != 0) {
1105 bus_space_tag_t t = sc->sc_bustag;
1106 bus_space_handle_t mif = sc->sc_mif;
1107 u_int32_t cf, st, sm;
1108 cf = bus_space_read_4(t, mif, HME_MIFI_CFG);
1109 st = bus_space_read_4(t, mif, HME_MIFI_STAT);
1110 sm = bus_space_read_4(t, mif, HME_MIFI_SM);
1111 printf("%s: XXXlink status changed: cfg=%x, stat %x, sm %x\n",
1112 sc->sc_dev.dv_xname, cf, st, sm);
1113 return (1);
1114 }
1115
1116 printf("%s: status=%s\n", sc->sc_dev.dv_xname,
1117 bitmask_snprintf(status, HME_SEB_STAT_BITS, bits,sizeof(bits)));
1118 return (1);
1119 }
1120
1121 int
1122 hme_intr(v)
1123 void *v;
1124 {
1125 struct hme_softc *sc = (struct hme_softc *)v;
1126 bus_space_tag_t t = sc->sc_bustag;
1127 bus_space_handle_t seb = sc->sc_seb;
1128 u_int32_t status;
1129 int r = 0;
1130
1131 status = bus_space_read_4(t, seb, HME_SEBI_STAT);
1132
1133 if ((status & HME_SEB_STAT_ALL_ERRORS) != 0)
1134 r |= hme_eint(sc, status);
1135
1136 if ((status & (HME_SEB_STAT_TXALL | HME_SEB_STAT_HOSTTOTX)) != 0)
1137 r |= hme_tint(sc);
1138
1139 if ((status & HME_SEB_STAT_RXTOHOST) != 0)
1140 r |= hme_rint(sc);
1141
1142 #if NRND > 0
1143 rnd_add_uint32(&sc->rnd_source, status);
1144 #endif
1145
1146 return (r);
1147 }
1148
1149
1150 void
1151 hme_watchdog(ifp)
1152 struct ifnet *ifp;
1153 {
1154 struct hme_softc *sc = ifp->if_softc;
1155
1156 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
1157 ++ifp->if_oerrors;
1158
1159 hme_reset(sc);
1160 }
1161
1162 /*
1163 * Initialize the MII Management Interface
1164 */
1165 void
1166 hme_mifinit(sc)
1167 struct hme_softc *sc;
1168 {
1169 bus_space_tag_t t = sc->sc_bustag;
1170 bus_space_handle_t mif = sc->sc_mif;
1171 bus_space_handle_t mac = sc->sc_mac;
1172 int instance, phy;
1173 u_int32_t v;
1174
1175 if (sc->sc_mii.mii_media.ifm_cur != NULL) {
1176 instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
1177 phy = sc->sc_phys[instance];
1178 } else
1179 /* No media set yet, pick phy arbitrarily.. */
1180 phy = HME_PHYAD_EXTERNAL;
1181
1182 /* Configure the MIF in frame mode, no poll, current phy select */
1183 v = 0;
1184 if (phy == HME_PHYAD_EXTERNAL)
1185 v |= HME_MIF_CFG_PHY;
1186 bus_space_write_4(t, mif, HME_MIFI_CFG, v);
1187
1188 /* If an external transceiver is selected, enable its MII drivers */
1189 v = bus_space_read_4(t, mac, HME_MACI_XIF);
1190 v &= ~HME_MAC_XIF_MIIENABLE;
1191 if (phy == HME_PHYAD_EXTERNAL)
1192 v |= HME_MAC_XIF_MIIENABLE;
1193 bus_space_write_4(t, mac, HME_MACI_XIF, v);
1194 }
1195
1196 /*
1197 * MII interface
1198 */
1199 static int
1200 hme_mii_readreg(self, phy, reg)
1201 struct device *self;
1202 int phy, reg;
1203 {
1204 struct hme_softc *sc = (void *)self;
1205 bus_space_tag_t t = sc->sc_bustag;
1206 bus_space_handle_t mif = sc->sc_mif;
1207 bus_space_handle_t mac = sc->sc_mac;
1208 u_int32_t v, xif_cfg, mifi_cfg;
1209 int n;
1210
1211 /* We can at most have two PHYs */
1212 if (phy != HME_PHYAD_EXTERNAL && phy != HME_PHYAD_INTERNAL)
1213 return (0);
1214
1215 /* Select the desired PHY in the MIF configuration register */
1216 v = mifi_cfg = bus_space_read_4(t, mif, HME_MIFI_CFG);
1217 v &= ~HME_MIF_CFG_PHY;
1218 if (phy == HME_PHYAD_EXTERNAL)
1219 v |= HME_MIF_CFG_PHY;
1220 bus_space_write_4(t, mif, HME_MIFI_CFG, v);
1221
1222 /* Enable MII drivers on external transceiver */
1223 v = xif_cfg = bus_space_read_4(t, mac, HME_MACI_XIF);
1224 if (phy == HME_PHYAD_EXTERNAL)
1225 v |= HME_MAC_XIF_MIIENABLE;
1226 else
1227 v &= ~HME_MAC_XIF_MIIENABLE;
1228 bus_space_write_4(t, mac, HME_MACI_XIF, v);
1229
1230 #if 0
1231 /* This doesn't work reliably; the MDIO_1 bit is off most of the time */
1232 /*
1233 * Check whether a transceiver is connected by testing
1234 * the MIF configuration register's MDI_X bits. Note that
1235 * MDI_0 (int) == 0x100 and MDI_1 (ext) == 0x200; see hmereg.h
1236 */
1237 mif_mdi_bit = 1 << (8 + (1 - phy));
1238 delay(100);
1239 v = bus_space_read_4(t, mif, HME_MIFI_CFG);
1240 if ((v & mif_mdi_bit) == 0)
1241 return (0);
1242 #endif
1243
1244 /* Construct the frame command */
1245 v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT) |
1246 HME_MIF_FO_TAMSB |
1247 (MII_COMMAND_READ << HME_MIF_FO_OPC_SHIFT) |
1248 (phy << HME_MIF_FO_PHYAD_SHIFT) |
1249 (reg << HME_MIF_FO_REGAD_SHIFT);
1250
1251 bus_space_write_4(t, mif, HME_MIFI_FO, v);
1252 for (n = 0; n < 100; n++) {
1253 DELAY(1);
1254 v = bus_space_read_4(t, mif, HME_MIFI_FO);
1255 if (v & HME_MIF_FO_TALSB) {
1256 v &= HME_MIF_FO_DATA;
1257 goto out;
1258 }
1259 }
1260
1261 v = 0;
1262 printf("%s: mii_read timeout\n", sc->sc_dev.dv_xname);
1263
1264 out:
1265 /* Restore MIFI_CFG register */
1266 bus_space_write_4(t, mif, HME_MIFI_CFG, mifi_cfg);
1267 /* Restore XIF register */
1268 bus_space_write_4(t, mac, HME_MACI_XIF, xif_cfg);
1269 return (v);
1270 }
1271
1272 static void
1273 hme_mii_writereg(self, phy, reg, val)
1274 struct device *self;
1275 int phy, reg, val;
1276 {
1277 struct hme_softc *sc = (void *)self;
1278 bus_space_tag_t t = sc->sc_bustag;
1279 bus_space_handle_t mif = sc->sc_mif;
1280 bus_space_handle_t mac = sc->sc_mac;
1281 u_int32_t v, xif_cfg, mifi_cfg;
1282 int n;
1283
1284 /* We can at most have two PHYs */
1285 if (phy != HME_PHYAD_EXTERNAL && phy != HME_PHYAD_INTERNAL)
1286 return;
1287
1288 /* Select the desired PHY in the MIF configuration register */
1289 v = mifi_cfg = bus_space_read_4(t, mif, HME_MIFI_CFG);
1290 v &= ~HME_MIF_CFG_PHY;
1291 if (phy == HME_PHYAD_EXTERNAL)
1292 v |= HME_MIF_CFG_PHY;
1293 bus_space_write_4(t, mif, HME_MIFI_CFG, v);
1294
1295 /* Enable MII drivers on external transceiver */
1296 v = xif_cfg = bus_space_read_4(t, mac, HME_MACI_XIF);
1297 if (phy == HME_PHYAD_EXTERNAL)
1298 v |= HME_MAC_XIF_MIIENABLE;
1299 else
1300 v &= ~HME_MAC_XIF_MIIENABLE;
1301 bus_space_write_4(t, mac, HME_MACI_XIF, v);
1302
1303 #if 0
1304 /* This doesn't work reliably; the MDIO_1 bit is off most of the time */
1305 /*
1306 * Check whether a transceiver is connected by testing
1307 * the MIF configuration register's MDI_X bits. Note that
1308 * MDI_0 (int) == 0x100 and MDI_1 (ext) == 0x200; see hmereg.h
1309 */
1310 mif_mdi_bit = 1 << (8 + (1 - phy));
1311 delay(100);
1312 v = bus_space_read_4(t, mif, HME_MIFI_CFG);
1313 if ((v & mif_mdi_bit) == 0)
1314 return;
1315 #endif
1316
1317 /* Construct the frame command */
1318 v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT) |
1319 HME_MIF_FO_TAMSB |
1320 (MII_COMMAND_WRITE << HME_MIF_FO_OPC_SHIFT) |
1321 (phy << HME_MIF_FO_PHYAD_SHIFT) |
1322 (reg << HME_MIF_FO_REGAD_SHIFT) |
1323 (val & HME_MIF_FO_DATA);
1324
1325 bus_space_write_4(t, mif, HME_MIFI_FO, v);
1326 for (n = 0; n < 100; n++) {
1327 DELAY(1);
1328 v = bus_space_read_4(t, mif, HME_MIFI_FO);
1329 if (v & HME_MIF_FO_TALSB)
1330 goto out;
1331 }
1332
1333 printf("%s: mii_write timeout\n", sc->sc_dev.dv_xname);
1334 out:
1335 /* Restore MIFI_CFG register */
1336 bus_space_write_4(t, mif, HME_MIFI_CFG, mifi_cfg);
1337 /* Restore XIF register */
1338 bus_space_write_4(t, mac, HME_MACI_XIF, xif_cfg);
1339 }
1340
1341 static void
1342 hme_mii_statchg(dev)
1343 struct device *dev;
1344 {
1345 struct hme_softc *sc = (void *)dev;
1346 bus_space_tag_t t = sc->sc_bustag;
1347 bus_space_handle_t mac = sc->sc_mac;
1348 u_int32_t v;
1349
1350 #ifdef HMEDEBUG
1351 if (sc->sc_debug)
1352 printf("hme_mii_statchg: status change\n");
1353 #endif
1354
1355 /* Set the MAC Full Duplex bit appropriately */
1356 /* Apparently the hme chip is SIMPLEX if working in full duplex mode,
1357 but not otherwise. */
1358 v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
1359 if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0) {
1360 v |= HME_MAC_TXCFG_FULLDPLX;
1361 sc->sc_ethercom.ec_if.if_flags |= IFF_SIMPLEX;
1362 } else {
1363 v &= ~HME_MAC_TXCFG_FULLDPLX;
1364 sc->sc_ethercom.ec_if.if_flags &= ~IFF_SIMPLEX;
1365 }
1366 sc->sc_if_flags = sc->sc_ethercom.ec_if.if_flags;
1367 bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
1368 }
1369
1370 int
1371 hme_mediachange(ifp)
1372 struct ifnet *ifp;
1373 {
1374 struct hme_softc *sc = ifp->if_softc;
1375 bus_space_tag_t t = sc->sc_bustag;
1376 bus_space_handle_t mif = sc->sc_mif;
1377 bus_space_handle_t mac = sc->sc_mac;
1378 int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
1379 int phy = sc->sc_phys[instance];
1380 int rc;
1381 u_int32_t v;
1382
1383 #ifdef HMEDEBUG
1384 if (sc->sc_debug)
1385 printf("hme_mediachange: phy = %d\n", phy);
1386 #endif
1387 if (IFM_TYPE(sc->sc_mii.mii_media.ifm_media) != IFM_ETHER)
1388 return (EINVAL);
1389
1390 /* Select the current PHY in the MIF configuration register */
1391 v = bus_space_read_4(t, mif, HME_MIFI_CFG);
1392 v &= ~HME_MIF_CFG_PHY;
1393 if (phy == HME_PHYAD_EXTERNAL)
1394 v |= HME_MIF_CFG_PHY;
1395 bus_space_write_4(t, mif, HME_MIFI_CFG, v);
1396
1397 /* If an external transceiver is selected, enable its MII drivers */
1398 v = bus_space_read_4(t, mac, HME_MACI_XIF);
1399 v &= ~HME_MAC_XIF_MIIENABLE;
1400 if (phy == HME_PHYAD_EXTERNAL)
1401 v |= HME_MAC_XIF_MIIENABLE;
1402 bus_space_write_4(t, mac, HME_MACI_XIF, v);
1403
1404 if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
1405 return 0;
1406 return rc;
1407 }
1408
1409 /*
1410 * Process an ioctl request.
1411 */
1412 int
1413 hme_ioctl(ifp, cmd, data)
1414 struct ifnet *ifp;
1415 u_long cmd;
1416 void *data;
1417 {
1418 struct hme_softc *sc = ifp->if_softc;
1419 struct ifaddr *ifa = (struct ifaddr *)data;
1420 int s, error = 0;
1421
1422 s = splnet();
1423
1424 switch (cmd) {
1425
1426 case SIOCSIFADDR:
1427 switch (ifa->ifa_addr->sa_family) {
1428 #ifdef INET
1429 case AF_INET:
1430 if (ifp->if_flags & IFF_UP)
1431 hme_setladrf(sc);
1432 else {
1433 ifp->if_flags |= IFF_UP;
1434 error = hme_init(sc);
1435 }
1436 arp_ifinit(ifp, ifa);
1437 break;
1438 #endif
1439 default:
1440 ifp->if_flags |= IFF_UP;
1441 error = hme_init(sc);
1442 break;
1443 }
1444 break;
1445
1446 case SIOCSIFFLAGS:
1447 #ifdef HMEDEBUG
1448 sc->sc_debug = (ifp->if_flags & IFF_DEBUG) != 0 ? 1 : 0;
1449 #endif
1450
1451 if ((ifp->if_flags & IFF_UP) == 0 &&
1452 (ifp->if_flags & IFF_RUNNING) != 0) {
1453 /*
1454 * If interface is marked down and it is running, then
1455 * stop it.
1456 */
1457 hme_stop(sc, false);
1458 ifp->if_flags &= ~IFF_RUNNING;
1459 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1460 (ifp->if_flags & IFF_RUNNING) == 0) {
1461 /*
1462 * If interface is marked up and it is stopped, then
1463 * start it.
1464 */
1465 error = hme_init(sc);
1466 } else if ((ifp->if_flags & IFF_UP) != 0) {
1467 /*
1468 * If setting debug or promiscuous mode, do not reset
1469 * the chip; for everything else, call hme_init()
1470 * which will trigger a reset.
1471 */
1472 #define RESETIGN (IFF_CANTCHANGE | IFF_DEBUG)
1473 if (ifp->if_flags != sc->sc_if_flags) {
1474 if ((ifp->if_flags & (~RESETIGN))
1475 == (sc->sc_if_flags & (~RESETIGN)))
1476 hme_setladrf(sc);
1477 else
1478 error = hme_init(sc);
1479 }
1480 #undef RESETIGN
1481 }
1482
1483 if (sc->sc_ec_capenable != sc->sc_ethercom.ec_capenable)
1484 error = hme_init(sc);
1485
1486 break;
1487
1488 case SIOCADDMULTI:
1489 case SIOCDELMULTI:
1490 case SIOCGIFMEDIA:
1491 case SIOCSIFMEDIA:
1492 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1493 /*
1494 * Multicast list has changed; set the hardware filter
1495 * accordingly.
1496 */
1497 if (ifp->if_flags & IFF_RUNNING)
1498 hme_setladrf(sc);
1499 error = 0;
1500 }
1501 break;
1502 default:
1503 error = EINVAL;
1504 break;
1505 }
1506
1507 sc->sc_if_flags = ifp->if_flags;
1508 splx(s);
1509 return (error);
1510 }
1511
1512 void
1513 hme_shutdown(arg)
1514 void *arg;
1515 {
1516
1517 hme_stop((struct hme_softc *)arg, false);
1518 }
1519
1520 /*
1521 * Set up the logical address filter.
1522 */
1523 void
1524 hme_setladrf(sc)
1525 struct hme_softc *sc;
1526 {
1527 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1528 struct ether_multi *enm;
1529 struct ether_multistep step;
1530 struct ethercom *ec = &sc->sc_ethercom;
1531 bus_space_tag_t t = sc->sc_bustag;
1532 bus_space_handle_t mac = sc->sc_mac;
1533 u_char *cp;
1534 u_int32_t crc;
1535 u_int32_t hash[4];
1536 u_int32_t v;
1537 int len;
1538
1539 /* Clear hash table */
1540 hash[3] = hash[2] = hash[1] = hash[0] = 0;
1541
1542 /* Get current RX configuration */
1543 v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
1544
1545 if ((ifp->if_flags & IFF_PROMISC) != 0) {
1546 /* Turn on promiscuous mode; turn off the hash filter */
1547 v |= HME_MAC_RXCFG_PMISC;
1548 v &= ~HME_MAC_RXCFG_HENABLE;
1549 ifp->if_flags |= IFF_ALLMULTI;
1550 goto chipit;
1551 }
1552
1553 /* Turn off promiscuous mode; turn on the hash filter */
1554 v &= ~HME_MAC_RXCFG_PMISC;
1555 v |= HME_MAC_RXCFG_HENABLE;
1556
1557 /*
1558 * Set up multicast address filter by passing all multicast addresses
1559 * through a crc generator, and then using the high order 6 bits as an
1560 * index into the 64 bit logical address filter. The high order bit
1561 * selects the word, while the rest of the bits select the bit within
1562 * the word.
1563 */
1564
1565 ETHER_FIRST_MULTI(step, ec, enm);
1566 while (enm != NULL) {
1567 if (ether_cmp(enm->enm_addrlo, enm->enm_addrhi)) {
1568 /*
1569 * We must listen to a range of multicast addresses.
1570 * For now, just accept all multicasts, rather than
1571 * trying to set only those filter bits needed to match
1572 * the range. (At this time, the only use of address
1573 * ranges is for IP multicast routing, for which the
1574 * range is big enough to require all bits set.)
1575 */
1576 hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
1577 ifp->if_flags |= IFF_ALLMULTI;
1578 goto chipit;
1579 }
1580
1581 cp = enm->enm_addrlo;
1582 crc = 0xffffffff;
1583 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1584 int octet = *cp++;
1585 int i;
1586
1587 #define MC_POLY_LE 0xedb88320UL /* mcast crc, little endian */
1588 for (i = 0; i < 8; i++) {
1589 if ((crc & 1) ^ (octet & 1)) {
1590 crc >>= 1;
1591 crc ^= MC_POLY_LE;
1592 } else {
1593 crc >>= 1;
1594 }
1595 octet >>= 1;
1596 }
1597 }
1598 /* Just want the 6 most significant bits. */
1599 crc >>= 26;
1600
1601 /* Set the corresponding bit in the filter. */
1602 hash[crc >> 4] |= 1 << (crc & 0xf);
1603
1604 ETHER_NEXT_MULTI(step, enm);
1605 }
1606
1607 ifp->if_flags &= ~IFF_ALLMULTI;
1608
1609 chipit:
1610 /* Now load the hash table into the chip */
1611 bus_space_write_4(t, mac, HME_MACI_HASHTAB0, hash[0]);
1612 bus_space_write_4(t, mac, HME_MACI_HASHTAB1, hash[1]);
1613 bus_space_write_4(t, mac, HME_MACI_HASHTAB2, hash[2]);
1614 bus_space_write_4(t, mac, HME_MACI_HASHTAB3, hash[3]);
1615 bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
1616 }
1617
1618 /*
1619 * Routines for accessing the transmit and receive buffers.
1620 * The various CPU and adapter configurations supported by this
1621 * driver require three different access methods for buffers
1622 * and descriptors:
1623 * (1) contig (contiguous data; no padding),
1624 * (2) gap2 (two bytes of data followed by two bytes of padding),
1625 * (3) gap16 (16 bytes of data followed by 16 bytes of padding).
1626 */
1627
1628 #if 0
1629 /*
1630 * contig: contiguous data with no padding.
1631 *
1632 * Buffers may have any alignment.
1633 */
1634
1635 void
1636 hme_copytobuf_contig(sc, from, ri, len)
1637 struct hme_softc *sc;
1638 void *from;
1639 int ri, len;
1640 {
1641 volatile void *buf = sc->sc_rb.rb_txbuf + (ri * _HME_BUFSZ);
1642
1643 /*
1644 * Just call memcpy() to do the work.
1645 */
1646 memcpy(buf, from, len);
1647 }
1648
1649 void
1650 hme_copyfrombuf_contig(sc, to, boff, len)
1651 struct hme_softc *sc;
1652 void *to;
1653 int boff, len;
1654 {
1655 volatile void *buf = sc->sc_rb.rb_rxbuf + (ri * _HME_BUFSZ);
1656
1657 /*
1658 * Just call memcpy() to do the work.
1659 */
1660 memcpy(to, buf, len);
1661 }
1662 #endif
1663