if_ste.c revision 1.61 1 /* $NetBSD: if_ste.c,v 1.61 2020/03/13 00:41:24 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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 * Device driver for the Sundance Tech. ST-201 10/100
34 * Ethernet controller.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: if_ste.c,v 1.61 2020/03/13 00:41:24 thorpej Exp $");
39
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/callout.h>
44 #include <sys/mbuf.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
47 #include <sys/socket.h>
48 #include <sys/ioctl.h>
49 #include <sys/errno.h>
50 #include <sys/device.h>
51 #include <sys/queue.h>
52
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #include <net/if_media.h>
56 #include <net/if_ether.h>
57
58 #include <net/bpf.h>
59
60 #include <sys/bus.h>
61 #include <sys/intr.h>
62
63 #include <dev/mii/mii.h>
64 #include <dev/mii/miivar.h>
65 #include <dev/mii/mii_bitbang.h>
66
67 #include <dev/pci/pcireg.h>
68 #include <dev/pci/pcivar.h>
69 #include <dev/pci/pcidevs.h>
70
71 #include <dev/pci/if_stereg.h>
72
73 /*
74 * Transmit descriptor list size.
75 */
76 #define STE_NTXDESC 256
77 #define STE_NTXDESC_MASK (STE_NTXDESC - 1)
78 #define STE_NEXTTX(x) (((x) + 1) & STE_NTXDESC_MASK)
79
80 /*
81 * Receive descriptor list size.
82 */
83 #define STE_NRXDESC 128
84 #define STE_NRXDESC_MASK (STE_NRXDESC - 1)
85 #define STE_NEXTRX(x) (((x) + 1) & STE_NRXDESC_MASK)
86
87 /*
88 * Control structures are DMA'd to the ST-201 chip. We allocate them in
89 * a single clump that maps to a single DMA segment to make several things
90 * easier.
91 */
92 struct ste_control_data {
93 /*
94 * The transmit descriptors.
95 */
96 struct ste_tfd scd_txdescs[STE_NTXDESC];
97
98 /*
99 * The receive descriptors.
100 */
101 struct ste_rfd scd_rxdescs[STE_NRXDESC];
102 };
103
104 #define STE_CDOFF(x) offsetof(struct ste_control_data, x)
105 #define STE_CDTXOFF(x) STE_CDOFF(scd_txdescs[(x)])
106 #define STE_CDRXOFF(x) STE_CDOFF(scd_rxdescs[(x)])
107
108 /*
109 * Software state for transmit and receive jobs.
110 */
111 struct ste_descsoft {
112 struct mbuf *ds_mbuf; /* head of our mbuf chain */
113 bus_dmamap_t ds_dmamap; /* our DMA map */
114 };
115
116 /*
117 * Software state per device.
118 */
119 struct ste_softc {
120 device_t sc_dev; /* generic device information */
121 bus_space_tag_t sc_st; /* bus space tag */
122 bus_space_handle_t sc_sh; /* bus space handle */
123 bus_dma_tag_t sc_dmat; /* bus DMA tag */
124 struct ethercom sc_ethercom; /* ethernet common data */
125
126 void *sc_ih; /* interrupt cookie */
127
128 struct mii_data sc_mii; /* MII/media information */
129
130 callout_t sc_tick_ch; /* tick callout */
131
132 bus_dmamap_t sc_cddmamap; /* control data DMA map */
133 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
134
135 /*
136 * Software state for transmit and receive descriptors.
137 */
138 struct ste_descsoft sc_txsoft[STE_NTXDESC];
139 struct ste_descsoft sc_rxsoft[STE_NRXDESC];
140
141 /*
142 * Control data structures.
143 */
144 struct ste_control_data *sc_control_data;
145 #define sc_txdescs sc_control_data->scd_txdescs
146 #define sc_rxdescs sc_control_data->scd_rxdescs
147
148 int sc_txpending; /* number of Tx requests pending */
149 int sc_txdirty; /* first dirty Tx descriptor */
150 int sc_txlast; /* last used Tx descriptor */
151
152 int sc_rxptr; /* next ready Rx descriptor/descsoft */
153
154 int sc_txthresh; /* Tx threshold */
155 uint32_t sc_DMACtrl; /* prototype DMACtrl register */
156 uint16_t sc_IntEnable; /* prototype IntEnable register */
157 uint16_t sc_MacCtrl0; /* prototype MacCtrl0 register */
158 uint8_t sc_ReceiveMode; /* prototype ReceiveMode register */
159
160 bool sc_enable_phy0; /* access to phy #0 allowed */
161 };
162
163 #define STE_CDTXADDR(sc, x) ((sc)->sc_cddma + STE_CDTXOFF((x)))
164 #define STE_CDRXADDR(sc, x) ((sc)->sc_cddma + STE_CDRXOFF((x)))
165
166 #define STE_CDTXSYNC(sc, x, ops) \
167 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
168 STE_CDTXOFF((x)), sizeof(struct ste_tfd), (ops))
169
170 #define STE_CDRXSYNC(sc, x, ops) \
171 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
172 STE_CDRXOFF((x)), sizeof(struct ste_rfd), (ops))
173
174 #define STE_INIT_RXDESC(sc, x) \
175 do { \
176 struct ste_descsoft *__ds = &(sc)->sc_rxsoft[(x)]; \
177 struct ste_rfd *__rfd = &(sc)->sc_rxdescs[(x)]; \
178 struct mbuf *__m = __ds->ds_mbuf; \
179 \
180 /* \
181 * Note: We scoot the packet forward 2 bytes in the buffer \
182 * so that the payload after the Ethernet header is aligned \
183 * to a 4-byte boundary. \
184 */ \
185 __m->m_data = __m->m_ext.ext_buf + 2; \
186 __rfd->rfd_frag.frag_addr = \
187 htole32(__ds->ds_dmamap->dm_segs[0].ds_addr + 2); \
188 __rfd->rfd_frag.frag_len = htole32((MCLBYTES - 2) | FRAG_LAST); \
189 __rfd->rfd_next = htole32(STE_CDRXADDR((sc), STE_NEXTRX((x)))); \
190 __rfd->rfd_status = 0; \
191 STE_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); \
192 } while (/*CONSTCOND*/0)
193
194 #define STE_TIMEOUT 1000
195
196 static void ste_start(struct ifnet *);
197 static void ste_watchdog(struct ifnet *);
198 static int ste_ioctl(struct ifnet *, u_long, void *);
199 static int ste_init(struct ifnet *);
200 static void ste_stop(struct ifnet *, int);
201
202 static bool ste_shutdown(device_t, int);
203
204 static void ste_reset(struct ste_softc *, uint32_t);
205 static void ste_setthresh(struct ste_softc *);
206 static void ste_txrestart(struct ste_softc *, uint8_t);
207 static void ste_rxdrain(struct ste_softc *);
208 static int ste_add_rxbuf(struct ste_softc *, int);
209 static void ste_read_eeprom(struct ste_softc *, int, uint16_t *);
210 static void ste_tick(void *);
211
212 static void ste_stats_update(struct ste_softc *);
213
214 static void ste_set_filter(struct ste_softc *);
215
216 static int ste_intr(void *);
217 static void ste_txintr(struct ste_softc *);
218 static void ste_rxintr(struct ste_softc *);
219
220 static int ste_mii_readreg(device_t, int, int, uint16_t *);
221 static int ste_mii_writereg(device_t, int, int, uint16_t);
222 static void ste_mii_statchg(struct ifnet *);
223
224 static int ste_match(device_t, cfdata_t, void *);
225 static void ste_attach(device_t, device_t, void *);
226
227 int ste_copy_small = 0;
228
229 CFATTACH_DECL_NEW(ste, sizeof(struct ste_softc),
230 ste_match, ste_attach, NULL, NULL);
231
232 static uint32_t ste_mii_bitbang_read(device_t);
233 static void ste_mii_bitbang_write(device_t, uint32_t);
234
235 static const struct mii_bitbang_ops ste_mii_bitbang_ops = {
236 ste_mii_bitbang_read,
237 ste_mii_bitbang_write,
238 {
239 PC_MgmtData, /* MII_BIT_MDO */
240 PC_MgmtData, /* MII_BIT_MDI */
241 PC_MgmtClk, /* MII_BIT_MDC */
242 PC_MgmtDir, /* MII_BIT_DIR_HOST_PHY */
243 0, /* MII_BIT_DIR_PHY_HOST */
244 }
245 };
246
247 /*
248 * Devices supported by this driver.
249 */
250 struct ste_product {
251 pci_vendor_id_t ste_vendor;
252 pci_product_id_t ste_product;
253 const char *ste_name;
254 const struct ste_product *ste_subs;
255 };
256
257 static const struct ste_product ste_dlink_products[] = {
258 { PCI_VENDOR_DLINK, 0x1002,
259 "D-Link DFE-550TX 10/100 Ethernet",
260 NULL },
261
262 { PCI_VENDOR_DLINK, 0x1003,
263 "D-Link DFE-550FX Ethernet",
264 NULL },
265
266 { PCI_VENDOR_DLINK, 0x1012,
267 "D-Link DFE-580TX 4-port 10/100 Ethernet",
268 NULL },
269
270 { PCI_VENDOR_DLINK, 0x1040,
271 "D-Link DFE-530TXS 10/100 Ethernet",
272 NULL },
273
274 { 0, 0,
275 NULL,
276 NULL },
277 };
278
279 static const struct ste_product ste_products[] = {
280 { PCI_VENDOR_SUNDANCETI, PCI_PRODUCT_SUNDANCETI_IP100A,
281 "IC Plus Corp. IP00A 10/100 Fast Ethernet Adapter",
282 NULL },
283
284 { PCI_VENDOR_SUNDANCETI, PCI_PRODUCT_SUNDANCETI_ST201,
285 "Sundance ST-201 10/100 Ethernet",
286 NULL },
287
288 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DL1002,
289 "D-Link DL-1002 10/100 Ethernet",
290 ste_dlink_products },
291
292 { 0, 0,
293 NULL,
294 NULL },
295 };
296
297 static const struct ste_product *
298 ste_lookup_table(pcireg_t pci_id, const struct ste_product * const products)
299 {
300 const struct ste_product *sp;
301
302 for (sp = products; sp->ste_name != NULL; sp++) {
303 if (PCI_VENDOR(pci_id) == sp->ste_vendor &&
304 PCI_PRODUCT(pci_id) == sp->ste_product)
305 return (sp);
306 }
307 return (NULL);
308 }
309
310 static const struct ste_product *
311 ste_lookup(const struct pci_attach_args *pa)
312 {
313 const struct ste_product *sp;
314
315 sp = ste_lookup_table(pa->pa_id, ste_products);
316 if (sp && sp->ste_subs) {
317 const pcireg_t subsys =
318 pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
319 const struct ste_product *ssp =
320 ste_lookup_table(subsys, sp->ste_subs);
321 if (ssp)
322 sp = ssp;
323 }
324 return (sp);
325 }
326
327 static int
328 ste_match(device_t parent, cfdata_t cf, void *aux)
329 {
330 struct pci_attach_args *pa = aux;
331
332 if (ste_lookup(pa) != NULL)
333 return (1);
334
335 return (0);
336 }
337
338 static void
339 ste_attach(device_t parent, device_t self, void *aux)
340 {
341 struct ste_softc *sc = device_private(self);
342 struct pci_attach_args *pa = aux;
343 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
344 struct mii_data * const mii = &sc->sc_mii;
345 pci_chipset_tag_t pc = pa->pa_pc;
346 pci_intr_handle_t ih;
347 const char *intrstr = NULL;
348 bus_space_tag_t iot, memt;
349 bus_space_handle_t ioh, memh;
350 bus_dma_segment_t seg;
351 int ioh_valid, memh_valid;
352 int i, rseg, error;
353 const struct ste_product *sp;
354 uint8_t enaddr[ETHER_ADDR_LEN];
355 uint16_t myea[ETHER_ADDR_LEN / 2];
356 char intrbuf[PCI_INTRSTR_LEN];
357
358 sc->sc_dev = self;
359
360 callout_init(&sc->sc_tick_ch, 0);
361 callout_setfunc(&sc->sc_tick_ch, ste_tick, sc);
362
363 sp = ste_lookup(pa);
364 if (sp == NULL) {
365 printf("\n");
366 panic("ste_attach: impossible");
367 }
368
369 printf(": %s\n", sp->ste_name);
370
371 /*
372 * Map the device.
373 */
374 ioh_valid = (pci_mapreg_map(pa, STE_PCI_IOBA,
375 PCI_MAPREG_TYPE_IO, 0,
376 &iot, &ioh, NULL, NULL) == 0);
377 memh_valid = (pci_mapreg_map(pa, STE_PCI_MMBA,
378 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
379 &memt, &memh, NULL, NULL) == 0);
380
381 if (memh_valid) {
382 sc->sc_st = memt;
383 sc->sc_sh = memh;
384 } else if (ioh_valid) {
385 sc->sc_st = iot;
386 sc->sc_sh = ioh;
387 } else {
388 aprint_error_dev(self, "unable to map device registers\n");
389 return;
390 }
391
392 sc->sc_dmat = pa->pa_dmat;
393
394 /* Enable bus mastering. */
395 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
396 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
397 PCI_COMMAND_MASTER_ENABLE);
398
399 /* power up chip */
400 if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self,
401 NULL)) && error != EOPNOTSUPP) {
402 aprint_error_dev(sc->sc_dev, "cannot activate %d\n", error);
403 return;
404 }
405
406 /*
407 * Map and establish our interrupt.
408 */
409 if (pci_intr_map(pa, &ih)) {
410 aprint_error_dev(sc->sc_dev, "unable to map interrupt\n");
411 return;
412 }
413 intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
414 sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_NET, ste_intr, sc,
415 device_xname(self));
416 if (sc->sc_ih == NULL) {
417 aprint_error_dev(sc->sc_dev, "unable to establish interrupt");
418 if (intrstr != NULL)
419 aprint_error(" at %s", intrstr);
420 aprint_error("\n");
421 return;
422 }
423 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
424
425 /*
426 * Allocate the control data structures, and create and load the
427 * DMA map for it.
428 */
429 if ((error = bus_dmamem_alloc(sc->sc_dmat,
430 sizeof(struct ste_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
431 0)) != 0) {
432 aprint_error_dev(sc->sc_dev,
433 "unable to allocate control data, error = %d\n", error);
434 goto fail_0;
435 }
436
437 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
438 sizeof(struct ste_control_data), (void **)&sc->sc_control_data,
439 BUS_DMA_COHERENT)) != 0) {
440 aprint_error_dev(sc->sc_dev,
441 "unable to map control data, error = %d\n", error);
442 goto fail_1;
443 }
444
445 if ((error = bus_dmamap_create(sc->sc_dmat,
446 sizeof(struct ste_control_data), 1,
447 sizeof(struct ste_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
448 aprint_error_dev(sc->sc_dev,
449 "unable to create control data DMA map, error = %d\n",
450 error);
451 goto fail_2;
452 }
453
454 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
455 sc->sc_control_data, sizeof(struct ste_control_data), NULL,
456 0)) != 0) {
457 aprint_error_dev(sc->sc_dev,
458 "unable to load control data DMA map, error = %d\n",
459 error);
460 goto fail_3;
461 }
462
463 /*
464 * Create the transmit buffer DMA maps.
465 */
466 for (i = 0; i < STE_NTXDESC; i++) {
467 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
468 STE_NTXFRAGS, MCLBYTES, 0, 0,
469 &sc->sc_txsoft[i].ds_dmamap)) != 0) {
470 aprint_error_dev(sc->sc_dev,
471 "unable to create tx DMA map %d, error = %d\n", i,
472 error);
473 goto fail_4;
474 }
475 }
476
477 /*
478 * Create the receive buffer DMA maps.
479 */
480 for (i = 0; i < STE_NRXDESC; i++) {
481 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
482 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].ds_dmamap)) != 0) {
483 aprint_error_dev(sc->sc_dev,
484 "unable to create rx DMA map %d, error = %d\n", i,
485 error);
486 goto fail_5;
487 }
488 sc->sc_rxsoft[i].ds_mbuf = NULL;
489 }
490
491 /*
492 * Reset the chip to a known state.
493 */
494 ste_reset(sc, AC_GlobalReset | AC_RxReset | AC_TxReset | AC_DMA |
495 AC_FIFO | AC_Network | AC_Host | AC_AutoInit | AC_RstOut);
496
497 /*
498 * Read the Ethernet address from the EEPROM.
499 */
500 for (i = 0; i < 3; i++) {
501 ste_read_eeprom(sc, STE_EEPROM_StationAddress0 + i, &myea[i]);
502 myea[i] = le16toh(myea[i]);
503 }
504 memcpy(enaddr, myea, sizeof(enaddr));
505
506 printf("%s: Ethernet address %s\n", device_xname(sc->sc_dev),
507 ether_sprintf(enaddr));
508
509 /*
510 * Initialize our media structures and probe the MII.
511 */
512 mii->mii_ifp = ifp;
513 mii->mii_readreg = ste_mii_readreg;
514 mii->mii_writereg = ste_mii_writereg;
515 mii->mii_statchg = ste_mii_statchg;
516 sc->sc_ethercom.ec_mii = mii;
517 ifmedia_init(&mii->mii_media, IFM_IMASK, ether_mediachange,
518 ether_mediastatus);
519 mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
520 MII_OFFSET_ANY, 0);
521 if (LIST_FIRST(&mii->mii_phys) == NULL) {
522 /*
523 * It seems that some variants of this chip "ghost" the
524 * single PHY at #0 and #1. We will try probing the MII
525 * first while ignoring #0 access. If we find the PHY,
526 * great! If not, un-ignore #0 and try probing *just*
527 * #0 to see if we can find it.
528 */
529 sc->sc_enable_phy0 = true;
530 mii_attach(sc->sc_dev, mii, 0xffffffff, 0,
531 MII_OFFSET_ANY, 0);
532 }
533 if (LIST_FIRST(&mii->mii_phys) == NULL) {
534 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
535 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
536 } else
537 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
538
539 ifp = &sc->sc_ethercom.ec_if;
540 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
541 ifp->if_softc = sc;
542 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
543 ifp->if_ioctl = ste_ioctl;
544 ifp->if_start = ste_start;
545 ifp->if_watchdog = ste_watchdog;
546 ifp->if_init = ste_init;
547 ifp->if_stop = ste_stop;
548 IFQ_SET_READY(&ifp->if_snd);
549
550 /*
551 * Default the transmit threshold to 128 bytes.
552 */
553 sc->sc_txthresh = 128;
554
555 /*
556 * Disable MWI if the PCI layer tells us to.
557 */
558 sc->sc_DMACtrl = 0;
559 if ((pa->pa_flags & PCI_FLAGS_MWI_OKAY) == 0)
560 sc->sc_DMACtrl |= DC_MWIDisable;
561
562 /*
563 * We can support 802.1Q VLAN-sized frames.
564 */
565 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
566
567 /*
568 * Attach the interface.
569 */
570 if_attach(ifp);
571 if_deferred_start_init(ifp, NULL);
572 ether_ifattach(ifp, enaddr);
573
574 /*
575 * Make sure the interface is shutdown during reboot.
576 */
577 if (pmf_device_register1(self, NULL, NULL, ste_shutdown))
578 pmf_class_network_register(self, ifp);
579 else
580 aprint_error_dev(self, "couldn't establish power handler\n");
581
582 return;
583
584 /*
585 * Free any resources we've allocated during the failed attach
586 * attempt. Do this in reverse order and fall through.
587 */
588 fail_5:
589 for (i = 0; i < STE_NRXDESC; i++) {
590 if (sc->sc_rxsoft[i].ds_dmamap != NULL)
591 bus_dmamap_destroy(sc->sc_dmat,
592 sc->sc_rxsoft[i].ds_dmamap);
593 }
594 fail_4:
595 for (i = 0; i < STE_NTXDESC; i++) {
596 if (sc->sc_txsoft[i].ds_dmamap != NULL)
597 bus_dmamap_destroy(sc->sc_dmat,
598 sc->sc_txsoft[i].ds_dmamap);
599 }
600 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
601 fail_3:
602 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
603 fail_2:
604 bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
605 sizeof(struct ste_control_data));
606 fail_1:
607 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
608 fail_0:
609 return;
610 }
611
612 /*
613 * ste_shutdown:
614 *
615 * Make sure the interface is stopped at reboot time.
616 */
617 static bool
618 ste_shutdown(device_t self, int howto)
619 {
620 struct ste_softc *sc;
621
622 sc = device_private(self);
623 ste_stop(&sc->sc_ethercom.ec_if, 1);
624
625 return true;
626 }
627
628 static void
629 ste_dmahalt_wait(struct ste_softc *sc)
630 {
631 int i;
632
633 for (i = 0; i < STE_TIMEOUT; i++) {
634 delay(2);
635 if ((bus_space_read_4(sc->sc_st, sc->sc_sh, STE_DMACtrl) &
636 DC_DMAHaltBusy) == 0)
637 break;
638 }
639
640 if (i == STE_TIMEOUT)
641 printf("%s: DMA halt timed out\n", device_xname(sc->sc_dev));
642 }
643
644 /*
645 * ste_start: [ifnet interface function]
646 *
647 * Start packet transmission on the interface.
648 */
649 static void
650 ste_start(struct ifnet *ifp)
651 {
652 struct ste_softc *sc = ifp->if_softc;
653 struct mbuf *m0, *m;
654 struct ste_descsoft *ds;
655 struct ste_tfd *tfd;
656 bus_dmamap_t dmamap;
657 int error, olasttx, nexttx, opending, seg, totlen;
658
659 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
660 return;
661
662 /*
663 * Remember the previous number of pending transmissions
664 * and the current last descriptor in the list.
665 */
666 opending = sc->sc_txpending;
667 olasttx = sc->sc_txlast;
668
669 /*
670 * Loop through the send queue, setting up transmit descriptors
671 * until we drain the queue, or use up all available transmit
672 * descriptors.
673 */
674 while (sc->sc_txpending < STE_NTXDESC) {
675 /*
676 * Grab a packet off the queue.
677 */
678 IFQ_POLL(&ifp->if_snd, m0);
679 if (m0 == NULL)
680 break;
681 m = NULL;
682
683 /*
684 * Get the last and next available transmit descriptor.
685 */
686 nexttx = STE_NEXTTX(sc->sc_txlast);
687 tfd = &sc->sc_txdescs[nexttx];
688 ds = &sc->sc_txsoft[nexttx];
689
690 dmamap = ds->ds_dmamap;
691
692 /*
693 * Load the DMA map. If this fails, the packet either
694 * didn't fit in the alloted number of segments, or we
695 * were short on resources. In this case, we'll copy
696 * and try again.
697 */
698 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
699 BUS_DMA_WRITE | BUS_DMA_NOWAIT) != 0) {
700 MGETHDR(m, M_DONTWAIT, MT_DATA);
701 if (m == NULL) {
702 printf("%s: unable to allocate Tx mbuf\n",
703 device_xname(sc->sc_dev));
704 break;
705 }
706 MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
707 if (m0->m_pkthdr.len > MHLEN) {
708 MCLGET(m, M_DONTWAIT);
709 if ((m->m_flags & M_EXT) == 0) {
710 printf("%s: unable to allocate Tx "
711 "cluster\n",
712 device_xname(sc->sc_dev));
713 m_freem(m);
714 break;
715 }
716 }
717 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
718 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
719 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
720 m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
721 if (error) {
722 printf("%s: unable to load Tx buffer, "
723 "error = %d\n", device_xname(sc->sc_dev),
724 error);
725 break;
726 }
727 }
728
729 IFQ_DEQUEUE(&ifp->if_snd, m0);
730 if (m != NULL) {
731 m_freem(m0);
732 m0 = m;
733 }
734
735 /*
736 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
737 */
738
739 /* Sync the DMA map. */
740 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
741 BUS_DMASYNC_PREWRITE);
742
743 /* Initialize the fragment list. */
744 for (totlen = 0, seg = 0; seg < dmamap->dm_nsegs; seg++) {
745 tfd->tfd_frags[seg].frag_addr =
746 htole32(dmamap->dm_segs[seg].ds_addr);
747 tfd->tfd_frags[seg].frag_len =
748 htole32(dmamap->dm_segs[seg].ds_len);
749 totlen += dmamap->dm_segs[seg].ds_len;
750 }
751 tfd->tfd_frags[seg - 1].frag_len |= htole32(FRAG_LAST);
752
753 /* Initialize the descriptor. */
754 tfd->tfd_next = htole32(STE_CDTXADDR(sc, nexttx));
755 tfd->tfd_control = htole32(TFD_FrameId(nexttx) | (totlen & 3));
756
757 /* Sync the descriptor. */
758 STE_CDTXSYNC(sc, nexttx,
759 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
760
761 /*
762 * Store a pointer to the packet so we can free it later,
763 * and remember what txdirty will be once the packet is
764 * done.
765 */
766 ds->ds_mbuf = m0;
767
768 /* Advance the tx pointer. */
769 sc->sc_txpending++;
770 sc->sc_txlast = nexttx;
771
772 /*
773 * Pass the packet to any BPF listeners.
774 */
775 bpf_mtap(ifp, m0, BPF_D_OUT);
776 }
777
778 if (sc->sc_txpending == STE_NTXDESC) {
779 /* No more slots left; notify upper layer. */
780 ifp->if_flags |= IFF_OACTIVE;
781 }
782
783 if (sc->sc_txpending != opending) {
784 /*
785 * We enqueued packets. If the transmitter was idle,
786 * reset the txdirty pointer.
787 */
788 if (opending == 0)
789 sc->sc_txdirty = STE_NEXTTX(olasttx);
790
791 /*
792 * Cause a descriptor interrupt to happen on the
793 * last packet we enqueued, and also cause the
794 * DMA engine to wait after is has finished processing
795 * it.
796 */
797 sc->sc_txdescs[sc->sc_txlast].tfd_next = 0;
798 sc->sc_txdescs[sc->sc_txlast].tfd_control |=
799 htole32(TFD_TxDMAIndicate);
800 STE_CDTXSYNC(sc, sc->sc_txlast,
801 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
802
803 /*
804 * Link up the new chain of descriptors to the
805 * last.
806 */
807 sc->sc_txdescs[olasttx].tfd_next =
808 htole32(STE_CDTXADDR(sc, STE_NEXTTX(olasttx)));
809 STE_CDTXSYNC(sc, olasttx,
810 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
811
812 /*
813 * Kick the transmit DMA logic. Note that since we're
814 * using auto-polling, reading the Tx desc pointer will
815 * give it the nudge it needs to get going.
816 */
817 if (bus_space_read_4(sc->sc_st, sc->sc_sh,
818 STE_TxDMAListPtr) == 0) {
819 bus_space_write_4(sc->sc_st, sc->sc_sh,
820 STE_DMACtrl, DC_TxDMAHalt);
821 ste_dmahalt_wait(sc);
822 bus_space_write_4(sc->sc_st, sc->sc_sh,
823 STE_TxDMAListPtr,
824 STE_CDTXADDR(sc, STE_NEXTTX(olasttx)));
825 bus_space_write_4(sc->sc_st, sc->sc_sh,
826 STE_DMACtrl, DC_TxDMAResume);
827 }
828
829 /* Set a watchdog timer in case the chip flakes out. */
830 ifp->if_timer = 5;
831 }
832 }
833
834 /*
835 * ste_watchdog: [ifnet interface function]
836 *
837 * Watchdog timer handler.
838 */
839 static void
840 ste_watchdog(struct ifnet *ifp)
841 {
842 struct ste_softc *sc = ifp->if_softc;
843
844 printf("%s: device timeout\n", device_xname(sc->sc_dev));
845 if_statinc(ifp, if_oerrors);
846
847 ste_txintr(sc);
848 ste_rxintr(sc);
849 (void) ste_init(ifp);
850
851 /* Try to get more packets going. */
852 ste_start(ifp);
853 }
854
855 /*
856 * ste_ioctl: [ifnet interface function]
857 *
858 * Handle control requests from the operator.
859 */
860 static int
861 ste_ioctl(struct ifnet *ifp, u_long cmd, void *data)
862 {
863 struct ste_softc *sc = ifp->if_softc;
864 int s, error;
865
866 s = splnet();
867
868 error = ether_ioctl(ifp, cmd, data);
869 if (error == ENETRESET) {
870 /*
871 * Multicast list has changed; set the hardware filter
872 * accordingly.
873 */
874 if (ifp->if_flags & IFF_RUNNING)
875 ste_set_filter(sc);
876 error = 0;
877 }
878
879 /* Try to get more packets going. */
880 ste_start(ifp);
881
882 splx(s);
883 return (error);
884 }
885
886 /*
887 * ste_intr:
888 *
889 * Interrupt service routine.
890 */
891 static int
892 ste_intr(void *arg)
893 {
894 struct ste_softc *sc = arg;
895 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
896 uint16_t isr;
897 uint8_t txstat;
898 int wantinit;
899
900 if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STE_IntStatus) &
901 IS_InterruptStatus) == 0)
902 return (0);
903
904 for (wantinit = 0; wantinit == 0;) {
905 isr = bus_space_read_2(sc->sc_st, sc->sc_sh, STE_IntStatusAck);
906 if ((isr & sc->sc_IntEnable) == 0)
907 break;
908
909 /* Receive interrupts. */
910 if (isr & IE_RxDMAComplete)
911 ste_rxintr(sc);
912
913 /* Transmit interrupts. */
914 if (isr & (IE_TxDMAComplete | IE_TxComplete))
915 ste_txintr(sc);
916
917 /* Statistics overflow. */
918 if (isr & IE_UpdateStats)
919 ste_stats_update(sc);
920
921 /* Transmission errors. */
922 if (isr & IE_TxComplete) {
923 for (;;) {
924 txstat = bus_space_read_1(sc->sc_st, sc->sc_sh,
925 STE_TxStatus);
926 if ((txstat & TS_TxComplete) == 0)
927 break;
928 if (txstat & TS_TxUnderrun) {
929 sc->sc_txthresh += 32;
930 if (sc->sc_txthresh > 0x1ffc)
931 sc->sc_txthresh = 0x1ffc;
932 printf("%s: transmit underrun, new "
933 "threshold: %d bytes\n",
934 device_xname(sc->sc_dev),
935 sc->sc_txthresh);
936 ste_reset(sc, AC_TxReset | AC_DMA |
937 AC_FIFO | AC_Network);
938 ste_setthresh(sc);
939 bus_space_write_1(sc->sc_st, sc->sc_sh,
940 STE_TxDMAPollPeriod, 127);
941 ste_txrestart(sc,
942 bus_space_read_1(sc->sc_st,
943 sc->sc_sh, STE_TxFrameId));
944 }
945 if (txstat & TS_TxReleaseError) {
946 printf("%s: Tx FIFO release error\n",
947 device_xname(sc->sc_dev));
948 wantinit = 1;
949 }
950 if (txstat & TS_MaxCollisions) {
951 printf("%s: excessive collisions\n",
952 device_xname(sc->sc_dev));
953 wantinit = 1;
954 }
955 if (txstat & TS_TxStatusOverflow) {
956 printf("%s: status overflow\n",
957 device_xname(sc->sc_dev));
958 wantinit = 1;
959 }
960 bus_space_write_2(sc->sc_st, sc->sc_sh,
961 STE_TxStatus, 0);
962 }
963 }
964
965 /* Host interface errors. */
966 if (isr & IE_HostError) {
967 printf("%s: Host interface error\n",
968 device_xname(sc->sc_dev));
969 wantinit = 1;
970 }
971 }
972
973 if (wantinit)
974 ste_init(ifp);
975
976 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_IntEnable,
977 sc->sc_IntEnable);
978
979 /* Try to get more packets going. */
980 if_schedule_deferred_start(ifp);
981
982 return (1);
983 }
984
985 /*
986 * ste_txintr:
987 *
988 * Helper; handle transmit interrupts.
989 */
990 static void
991 ste_txintr(struct ste_softc *sc)
992 {
993 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
994 struct ste_descsoft *ds;
995 uint32_t control;
996 int i;
997
998 ifp->if_flags &= ~IFF_OACTIVE;
999
1000 /*
1001 * Go through our Tx list and free mbufs for those
1002 * frames which have been transmitted.
1003 */
1004 for (i = sc->sc_txdirty; sc->sc_txpending != 0;
1005 i = STE_NEXTTX(i), sc->sc_txpending--) {
1006 ds = &sc->sc_txsoft[i];
1007
1008 STE_CDTXSYNC(sc, i,
1009 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1010
1011 control = le32toh(sc->sc_txdescs[i].tfd_control);
1012 if ((control & TFD_TxDMAComplete) == 0)
1013 break;
1014
1015 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap,
1016 0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1017 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1018 m_freem(ds->ds_mbuf);
1019 ds->ds_mbuf = NULL;
1020 }
1021
1022 /* Update the dirty transmit buffer pointer. */
1023 sc->sc_txdirty = i;
1024
1025 /*
1026 * If there are no more pending transmissions, cancel the watchdog
1027 * timer.
1028 */
1029 if (sc->sc_txpending == 0)
1030 ifp->if_timer = 0;
1031 }
1032
1033 /*
1034 * ste_rxintr:
1035 *
1036 * Helper; handle receive interrupts.
1037 */
1038 static void
1039 ste_rxintr(struct ste_softc *sc)
1040 {
1041 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1042 struct ste_descsoft *ds;
1043 struct mbuf *m;
1044 uint32_t status;
1045 int i, len;
1046
1047 for (i = sc->sc_rxptr;; i = STE_NEXTRX(i)) {
1048 ds = &sc->sc_rxsoft[i];
1049
1050 STE_CDRXSYNC(sc, i,
1051 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1052
1053 status = le32toh(sc->sc_rxdescs[i].rfd_status);
1054
1055 if ((status & RFD_RxDMAComplete) == 0)
1056 break;
1057
1058 /*
1059 * If the packet had an error, simply recycle the
1060 * buffer. Note, we count the error later in the
1061 * periodic stats update.
1062 */
1063 if (status & RFD_RxFrameError) {
1064 STE_INIT_RXDESC(sc, i);
1065 continue;
1066 }
1067
1068 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1069 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1070
1071 /*
1072 * No errors; receive the packet. Note, we have
1073 * configured the chip to not include the CRC at
1074 * the end of the packet.
1075 */
1076 len = RFD_RxDMAFrameLen(status);
1077
1078 /*
1079 * If the packet is small enough to fit in a
1080 * single header mbuf, allocate one and copy
1081 * the data into it. This greatly reduces
1082 * memory consumption when we receive lots
1083 * of small packets.
1084 *
1085 * Otherwise, we add a new buffer to the receive
1086 * chain. If this fails, we drop the packet and
1087 * recycle the old buffer.
1088 */
1089 if (ste_copy_small != 0 && len <= (MHLEN - 2)) {
1090 MGETHDR(m, M_DONTWAIT, MT_DATA);
1091 if (m == NULL)
1092 goto dropit;
1093 MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
1094 m->m_data += 2;
1095 memcpy(mtod(m, void *),
1096 mtod(ds->ds_mbuf, void *), len);
1097 STE_INIT_RXDESC(sc, i);
1098 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1099 ds->ds_dmamap->dm_mapsize,
1100 BUS_DMASYNC_PREREAD);
1101 } else {
1102 m = ds->ds_mbuf;
1103 if (ste_add_rxbuf(sc, i) != 0) {
1104 dropit:
1105 if_statinc(ifp, if_ierrors);
1106 STE_INIT_RXDESC(sc, i);
1107 bus_dmamap_sync(sc->sc_dmat,
1108 ds->ds_dmamap, 0,
1109 ds->ds_dmamap->dm_mapsize,
1110 BUS_DMASYNC_PREREAD);
1111 continue;
1112 }
1113 }
1114
1115 m_set_rcvif(m, ifp);
1116 m->m_pkthdr.len = m->m_len = len;
1117
1118 /* Pass it on. */
1119 if_percpuq_enqueue(ifp->if_percpuq, m);
1120 }
1121
1122 /* Update the receive pointer. */
1123 sc->sc_rxptr = i;
1124 }
1125
1126 /*
1127 * ste_tick:
1128 *
1129 * One second timer, used to tick the MII.
1130 */
1131 static void
1132 ste_tick(void *arg)
1133 {
1134 struct ste_softc *sc = arg;
1135 int s;
1136
1137 s = splnet();
1138 mii_tick(&sc->sc_mii);
1139 ste_stats_update(sc);
1140 splx(s);
1141
1142 callout_schedule(&sc->sc_tick_ch, hz);
1143 }
1144
1145 /*
1146 * ste_stats_update:
1147 *
1148 * Read the ST-201 statistics counters.
1149 */
1150 static void
1151 ste_stats_update(struct ste_softc *sc)
1152 {
1153 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1154 bus_space_tag_t st = sc->sc_st;
1155 bus_space_handle_t sh = sc->sc_sh;
1156
1157 (void) bus_space_read_2(st, sh, STE_OctetsReceivedOk0);
1158 (void) bus_space_read_2(st, sh, STE_OctetsReceivedOk1);
1159
1160 (void) bus_space_read_2(st, sh, STE_OctetsTransmittedOk0);
1161 (void) bus_space_read_2(st, sh, STE_OctetsTransmittedOk1);
1162
1163 net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
1164
1165 if_statadd_ref(nsr, if_opackets,
1166 (u_int) bus_space_read_2(st, sh, STE_FramesTransmittedOK));
1167
1168 (void) bus_space_read_2(st, sh, STE_FramesReceivedOK);
1169
1170 if_statadd_ref(nsr, if_collisions,
1171 (u_int) bus_space_read_1(st, sh, STE_LateCollisions) +
1172 (u_int) bus_space_read_1(st, sh, STE_MultipleColFrames) +
1173 (u_int) bus_space_read_1(st, sh, STE_SingleColFrames));
1174
1175 (void) bus_space_read_1(st, sh, STE_FramesWDeferredXmt);
1176
1177 if_statadd_ref(nsr, if_ierrors,
1178 (u_int) bus_space_read_1(st, sh, STE_FramesLostRxErrors));
1179
1180 if_statadd_ref(nsr, if_oerrors,
1181 (u_int) bus_space_read_1(st, sh, STE_FramesWExDeferral) +
1182 (u_int) bus_space_read_1(st, sh, STE_FramesXbortXSColls) +
1183 bus_space_read_1(st, sh, STE_CarrierSenseErrors));
1184
1185 IF_STAT_PUTREF(ifp);
1186
1187 (void) bus_space_read_1(st, sh, STE_BcstFramesXmtdOk);
1188 (void) bus_space_read_1(st, sh, STE_BcstFramesRcvdOk);
1189 (void) bus_space_read_1(st, sh, STE_McstFramesXmtdOk);
1190 (void) bus_space_read_1(st, sh, STE_McstFramesRcvdOk);
1191 }
1192
1193 /*
1194 * ste_reset:
1195 *
1196 * Perform a soft reset on the ST-201.
1197 */
1198 static void
1199 ste_reset(struct ste_softc *sc, uint32_t rstbits)
1200 {
1201 uint32_t ac;
1202 int i;
1203
1204 ac = bus_space_read_4(sc->sc_st, sc->sc_sh, STE_AsicCtrl);
1205
1206 bus_space_write_4(sc->sc_st, sc->sc_sh, STE_AsicCtrl, ac | rstbits);
1207
1208 delay(50000);
1209
1210 for (i = 0; i < STE_TIMEOUT; i++) {
1211 delay(1000);
1212 if ((bus_space_read_4(sc->sc_st, sc->sc_sh, STE_AsicCtrl) &
1213 AC_ResetBusy) == 0)
1214 break;
1215 }
1216
1217 if (i == STE_TIMEOUT)
1218 printf("%s: reset failed to complete\n",
1219 device_xname(sc->sc_dev));
1220
1221 delay(1000);
1222 }
1223
1224 /*
1225 * ste_setthresh:
1226 *
1227 * set the various transmit threshold registers
1228 */
1229 static void
1230 ste_setthresh(struct ste_softc *sc)
1231 {
1232 /* set the TX threhold */
1233 bus_space_write_2(sc->sc_st, sc->sc_sh,
1234 STE_TxStartThresh, sc->sc_txthresh);
1235 /* Urgent threshold: set to sc_txthresh / 2 */
1236 bus_space_write_1(sc->sc_st, sc->sc_sh, STE_TxDMAUrgentThresh,
1237 sc->sc_txthresh >> 6);
1238 /* Burst threshold: use default value (256 bytes) */
1239 }
1240
1241 /*
1242 * restart TX at the given frame ID in the transmitter ring
1243 */
1244 static void
1245 ste_txrestart(struct ste_softc *sc, uint8_t id)
1246 {
1247 uint32_t control;
1248
1249 STE_CDTXSYNC(sc, id, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1250 control = le32toh(sc->sc_txdescs[id].tfd_control);
1251 control &= ~TFD_TxDMAComplete;
1252 sc->sc_txdescs[id].tfd_control = htole32(control);
1253 STE_CDTXSYNC(sc, id, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1254
1255 bus_space_write_4(sc->sc_st, sc->sc_sh, STE_TxDMAListPtr, 0);
1256 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_MacCtrl1, MC1_TxEnable);
1257 bus_space_write_4(sc->sc_st, sc->sc_sh, STE_DMACtrl, DC_TxDMAHalt);
1258 ste_dmahalt_wait(sc);
1259 bus_space_write_4(sc->sc_st, sc->sc_sh, STE_TxDMAListPtr,
1260 STE_CDTXADDR(sc, id));
1261 bus_space_write_4(sc->sc_st, sc->sc_sh, STE_DMACtrl, DC_TxDMAResume);
1262 }
1263
1264 /*
1265 * ste_init: [ ifnet interface function ]
1266 *
1267 * Initialize the interface. Must be called at splnet().
1268 */
1269 static int
1270 ste_init(struct ifnet *ifp)
1271 {
1272 struct ste_softc *sc = ifp->if_softc;
1273 bus_space_tag_t st = sc->sc_st;
1274 bus_space_handle_t sh = sc->sc_sh;
1275 struct ste_descsoft *ds;
1276 int i, error = 0;
1277
1278 /*
1279 * Cancel any pending I/O.
1280 */
1281 ste_stop(ifp, 0);
1282
1283 /*
1284 * Reset the chip to a known state.
1285 */
1286 ste_reset(sc, AC_GlobalReset | AC_RxReset | AC_TxReset | AC_DMA |
1287 AC_FIFO | AC_Network | AC_Host | AC_AutoInit | AC_RstOut);
1288
1289 /*
1290 * Initialize the transmit descriptor ring.
1291 */
1292 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1293 sc->sc_txpending = 0;
1294 sc->sc_txdirty = 0;
1295 sc->sc_txlast = STE_NTXDESC - 1;
1296
1297 /*
1298 * Initialize the receive descriptor and receive job
1299 * descriptor rings.
1300 */
1301 for (i = 0; i < STE_NRXDESC; i++) {
1302 ds = &sc->sc_rxsoft[i];
1303 if (ds->ds_mbuf == NULL) {
1304 if ((error = ste_add_rxbuf(sc, i)) != 0) {
1305 printf("%s: unable to allocate or map rx "
1306 "buffer %d, error = %d\n",
1307 device_xname(sc->sc_dev), i, error);
1308 /*
1309 * XXX Should attempt to run with fewer receive
1310 * XXX buffers instead of just failing.
1311 */
1312 ste_rxdrain(sc);
1313 goto out;
1314 }
1315 } else
1316 STE_INIT_RXDESC(sc, i);
1317 }
1318 sc->sc_rxptr = 0;
1319
1320 /* Set the station address. */
1321 for (i = 0; i < ETHER_ADDR_LEN; i++)
1322 bus_space_write_1(st, sh, STE_StationAddress0 + 1,
1323 CLLADDR(ifp->if_sadl)[i]);
1324
1325 /* Set up the receive filter. */
1326 ste_set_filter(sc);
1327
1328 /*
1329 * Give the receive ring to the chip.
1330 */
1331 bus_space_write_4(st, sh, STE_RxDMAListPtr,
1332 STE_CDRXADDR(sc, sc->sc_rxptr));
1333
1334 /*
1335 * We defer giving the transmit ring to the chip until we
1336 * transmit the first packet.
1337 */
1338
1339 /*
1340 * Initialize the Tx auto-poll period. It's OK to make this number
1341 * large (127 is the max) -- we explicitly kick the transmit engine
1342 * when there's actually a packet. We are using auto-polling only
1343 * to make the interface to the transmit engine not suck.
1344 */
1345 bus_space_write_1(sc->sc_st, sc->sc_sh, STE_TxDMAPollPeriod, 127);
1346
1347 /* ..and the Rx auto-poll period. */
1348 bus_space_write_1(st, sh, STE_RxDMAPollPeriod, 64);
1349
1350 /* Initialize the Tx start threshold. */
1351 ste_setthresh(sc);
1352
1353 /* Set the FIFO release threshold to 512 bytes. */
1354 bus_space_write_1(st, sh, STE_TxReleaseThresh, 512 >> 4);
1355
1356 /* Set maximum packet size for VLAN. */
1357 if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
1358 bus_space_write_2(st, sh, STE_MaxFrameSize, ETHER_MAX_LEN + 4);
1359 else
1360 bus_space_write_2(st, sh, STE_MaxFrameSize, ETHER_MAX_LEN);
1361
1362 /*
1363 * Initialize the interrupt mask.
1364 */
1365 sc->sc_IntEnable = IE_HostError | IE_TxComplete | IE_UpdateStats |
1366 IE_TxDMAComplete | IE_RxDMAComplete;
1367
1368 bus_space_write_2(st, sh, STE_IntStatus, 0xffff);
1369 bus_space_write_2(st, sh, STE_IntEnable, sc->sc_IntEnable);
1370
1371 /*
1372 * Start the receive DMA engine.
1373 */
1374 bus_space_write_4(st, sh, STE_DMACtrl, sc->sc_DMACtrl | DC_RxDMAResume);
1375
1376 /*
1377 * Initialize MacCtrl0 -- do it before setting the media,
1378 * as setting the media will actually program the register.
1379 */
1380 sc->sc_MacCtrl0 = MC0_IFSSelect(0);
1381 if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
1382 sc->sc_MacCtrl0 |= MC0_RcvLargeFrames;
1383
1384 /*
1385 * Set the current media.
1386 */
1387 if ((error = ether_mediachange(ifp)) != 0)
1388 goto out;
1389
1390 /*
1391 * Start the MAC.
1392 */
1393 bus_space_write_2(st, sh, STE_MacCtrl1,
1394 MC1_StatisticsEnable | MC1_TxEnable | MC1_RxEnable);
1395
1396 /*
1397 * Start the one second MII clock.
1398 */
1399 callout_schedule(&sc->sc_tick_ch, hz);
1400
1401 /*
1402 * ...all done!
1403 */
1404 ifp->if_flags |= IFF_RUNNING;
1405 ifp->if_flags &= ~IFF_OACTIVE;
1406
1407 out:
1408 if (error)
1409 printf("%s: interface not running\n", device_xname(sc->sc_dev));
1410 return (error);
1411 }
1412
1413 /*
1414 * ste_drain:
1415 *
1416 * Drain the receive queue.
1417 */
1418 static void
1419 ste_rxdrain(struct ste_softc *sc)
1420 {
1421 struct ste_descsoft *ds;
1422 int i;
1423
1424 for (i = 0; i < STE_NRXDESC; i++) {
1425 ds = &sc->sc_rxsoft[i];
1426 if (ds->ds_mbuf != NULL) {
1427 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1428 m_freem(ds->ds_mbuf);
1429 ds->ds_mbuf = NULL;
1430 }
1431 }
1432 }
1433
1434 /*
1435 * ste_stop: [ ifnet interface function ]
1436 *
1437 * Stop transmission on the interface.
1438 */
1439 static void
1440 ste_stop(struct ifnet *ifp, int disable)
1441 {
1442 struct ste_softc *sc = ifp->if_softc;
1443 struct ste_descsoft *ds;
1444 int i;
1445
1446 /*
1447 * Stop the one second clock.
1448 */
1449 callout_stop(&sc->sc_tick_ch);
1450
1451 /* Down the MII. */
1452 mii_down(&sc->sc_mii);
1453
1454 /*
1455 * Disable interrupts.
1456 */
1457 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_IntEnable, 0);
1458
1459 /*
1460 * Stop receiver, transmitter, and stats update.
1461 */
1462 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_MacCtrl1,
1463 MC1_StatisticsDisable | MC1_TxDisable | MC1_RxDisable);
1464
1465 /*
1466 * Stop the transmit and receive DMA.
1467 */
1468 bus_space_write_4(sc->sc_st, sc->sc_sh, STE_DMACtrl,
1469 DC_RxDMAHalt | DC_TxDMAHalt);
1470 ste_dmahalt_wait(sc);
1471
1472 /*
1473 * Release any queued transmit buffers.
1474 */
1475 for (i = 0; i < STE_NTXDESC; i++) {
1476 ds = &sc->sc_txsoft[i];
1477 if (ds->ds_mbuf != NULL) {
1478 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1479 m_freem(ds->ds_mbuf);
1480 ds->ds_mbuf = NULL;
1481 }
1482 }
1483
1484 /*
1485 * Mark the interface down and cancel the watchdog timer.
1486 */
1487 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1488 ifp->if_timer = 0;
1489
1490 if (disable)
1491 ste_rxdrain(sc);
1492 }
1493
1494 static int
1495 ste_eeprom_wait(struct ste_softc *sc)
1496 {
1497 int i;
1498
1499 for (i = 0; i < STE_TIMEOUT; i++) {
1500 delay(1000);
1501 if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STE_EepromCtrl) &
1502 EC_EepromBusy) == 0)
1503 return (0);
1504 }
1505 return (1);
1506 }
1507
1508 /*
1509 * ste_read_eeprom:
1510 *
1511 * Read data from the serial EEPROM.
1512 */
1513 static void
1514 ste_read_eeprom(struct ste_softc *sc, int offset, uint16_t *data)
1515 {
1516
1517 if (ste_eeprom_wait(sc))
1518 printf("%s: EEPROM failed to come ready\n",
1519 device_xname(sc->sc_dev));
1520
1521 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_EepromCtrl,
1522 EC_EepromAddress(offset) | EC_EepromOpcode(EC_OP_R));
1523 if (ste_eeprom_wait(sc))
1524 printf("%s: EEPROM read timed out\n",
1525 device_xname(sc->sc_dev));
1526 *data = bus_space_read_2(sc->sc_st, sc->sc_sh, STE_EepromData);
1527 }
1528
1529 /*
1530 * ste_add_rxbuf:
1531 *
1532 * Add a receive buffer to the indicated descriptor.
1533 */
1534 static int
1535 ste_add_rxbuf(struct ste_softc *sc, int idx)
1536 {
1537 struct ste_descsoft *ds = &sc->sc_rxsoft[idx];
1538 struct mbuf *m;
1539 int error;
1540
1541 MGETHDR(m, M_DONTWAIT, MT_DATA);
1542 if (m == NULL)
1543 return (ENOBUFS);
1544
1545 MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
1546 MCLGET(m, M_DONTWAIT);
1547 if ((m->m_flags & M_EXT) == 0) {
1548 m_freem(m);
1549 return (ENOBUFS);
1550 }
1551
1552 if (ds->ds_mbuf != NULL)
1553 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1554
1555 ds->ds_mbuf = m;
1556
1557 error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
1558 m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
1559 BUS_DMA_READ | BUS_DMA_NOWAIT);
1560 if (error) {
1561 printf("%s: can't load rx DMA map %d, error = %d\n",
1562 device_xname(sc->sc_dev), idx, error);
1563 panic("ste_add_rxbuf"); /* XXX */
1564 }
1565
1566 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1567 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1568
1569 STE_INIT_RXDESC(sc, idx);
1570
1571 return (0);
1572 }
1573
1574 /*
1575 * ste_set_filter:
1576 *
1577 * Set up the receive filter.
1578 */
1579 static void
1580 ste_set_filter(struct ste_softc *sc)
1581 {
1582 struct ethercom *ec = &sc->sc_ethercom;
1583 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1584 struct ether_multi *enm;
1585 struct ether_multistep step;
1586 uint32_t crc;
1587 uint16_t mchash[4];
1588
1589 sc->sc_ReceiveMode = RM_ReceiveUnicast;
1590 if (ifp->if_flags & IFF_BROADCAST)
1591 sc->sc_ReceiveMode |= RM_ReceiveBroadcast;
1592
1593 if (ifp->if_flags & IFF_PROMISC) {
1594 sc->sc_ReceiveMode |= RM_ReceiveAllFrames;
1595 goto allmulti;
1596 }
1597
1598 /*
1599 * Set up the multicast address filter by passing all multicast
1600 * addresses through a CRC generator, and then using the low-order
1601 * 6 bits as an index into the 64 bit multicast hash table. The
1602 * high order bits select the register, while the rest of the bits
1603 * select the bit within the register.
1604 */
1605
1606 memset(mchash, 0, sizeof(mchash));
1607
1608 ETHER_LOCK(ec);
1609 ETHER_FIRST_MULTI(step, ec, enm);
1610 if (enm == NULL) {
1611 ETHER_UNLOCK(ec);
1612 goto done;
1613 }
1614
1615 while (enm != NULL) {
1616 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1617 /*
1618 * We must listen to a range of multicast addresses.
1619 * For now, just accept all multicasts, rather than
1620 * trying to set only those filter bits needed to match
1621 * the range. (At this time, the only use of address
1622 * ranges is for IP multicast routing, for which the
1623 * range is big enough to require all bits set.)
1624 */
1625 ETHER_UNLOCK(ec);
1626 goto allmulti;
1627 }
1628
1629 crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
1630
1631 /* Just want the 6 least significant bits. */
1632 crc &= 0x3f;
1633
1634 /* Set the corresponding bit in the hash table. */
1635 mchash[crc >> 4] |= 1 << (crc & 0xf);
1636
1637 ETHER_NEXT_MULTI(step, enm);
1638 }
1639 ETHER_UNLOCK(ec);
1640
1641 sc->sc_ReceiveMode |= RM_ReceiveMulticastHash;
1642
1643 ifp->if_flags &= ~IFF_ALLMULTI;
1644 goto done;
1645
1646 allmulti:
1647 ifp->if_flags |= IFF_ALLMULTI;
1648 sc->sc_ReceiveMode |= RM_ReceiveMulticast;
1649
1650 done:
1651 if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
1652 /*
1653 * Program the multicast hash table.
1654 */
1655 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_HashTable0,
1656 mchash[0]);
1657 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_HashTable1,
1658 mchash[1]);
1659 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_HashTable2,
1660 mchash[2]);
1661 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_HashTable3,
1662 mchash[3]);
1663 }
1664
1665 bus_space_write_1(sc->sc_st, sc->sc_sh, STE_ReceiveMode,
1666 sc->sc_ReceiveMode);
1667 }
1668
1669 /*
1670 * ste_mii_readreg: [mii interface function]
1671 *
1672 * Read a PHY register on the MII of the ST-201.
1673 */
1674 static int
1675 ste_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
1676 {
1677 struct ste_softc *sc = device_private(self);
1678
1679 if (phy == 0 && !sc->sc_enable_phy0)
1680 return EIO;
1681
1682 return mii_bitbang_readreg(self, &ste_mii_bitbang_ops, phy, reg, val);
1683 }
1684
1685 /*
1686 * ste_mii_writereg: [mii interface function]
1687 *
1688 * Write a PHY register on the MII of the ST-201.
1689 */
1690 static int
1691 ste_mii_writereg(device_t self, int phy, int reg, uint16_t val)
1692 {
1693 struct ste_softc *sc = device_private(self);
1694
1695 if (phy == 0 && !sc->sc_enable_phy0)
1696 return EIO;
1697
1698 return mii_bitbang_writereg(self, &ste_mii_bitbang_ops, phy, reg, val);
1699 }
1700
1701 /*
1702 * ste_mii_statchg: [mii interface function]
1703 *
1704 * Callback from MII layer when media changes.
1705 */
1706 static void
1707 ste_mii_statchg(struct ifnet *ifp)
1708 {
1709 struct ste_softc *sc = ifp->if_softc;
1710
1711 if (sc->sc_mii.mii_media_active & IFM_FDX)
1712 sc->sc_MacCtrl0 |= MC0_FullDuplexEnable;
1713 else
1714 sc->sc_MacCtrl0 &= ~MC0_FullDuplexEnable;
1715
1716 /* XXX 802.1x flow-control? */
1717
1718 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_MacCtrl0, sc->sc_MacCtrl0);
1719 }
1720
1721 /*
1722 * ste_mii_bitbang_read: [mii bit-bang interface function]
1723 *
1724 * Read the MII serial port for the MII bit-bang module.
1725 */
1726 static uint32_t
1727 ste_mii_bitbang_read(device_t self)
1728 {
1729 struct ste_softc *sc = device_private(self);
1730
1731 return (bus_space_read_1(sc->sc_st, sc->sc_sh, STE_PhyCtrl));
1732 }
1733
1734 /*
1735 * ste_mii_bitbang_write: [mii big-bang interface function]
1736 *
1737 * Write the MII serial port for the MII bit-bang module.
1738 */
1739 static void
1740 ste_mii_bitbang_write(device_t self, uint32_t val)
1741 {
1742 struct ste_softc *sc = device_private(self);
1743
1744 bus_space_write_1(sc->sc_st, sc->sc_sh, STE_PhyCtrl, val);
1745 }
1746