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