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