if_ste.c revision 1.63 1 /* $NetBSD: if_ste.c,v 1.63 2022/09/21 20:23:56 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.63 2022/09/21 20:23:56 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_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 m_freem(m);
726 break;
727 }
728 }
729
730 IFQ_DEQUEUE(&ifp->if_snd, m0);
731 if (m != NULL) {
732 m_freem(m0);
733 m0 = m;
734 }
735
736 /*
737 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
738 */
739
740 /* Sync the DMA map. */
741 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
742 BUS_DMASYNC_PREWRITE);
743
744 /* Initialize the fragment list. */
745 for (totlen = 0, seg = 0; seg < dmamap->dm_nsegs; seg++) {
746 tfd->tfd_frags[seg].frag_addr =
747 htole32(dmamap->dm_segs[seg].ds_addr);
748 tfd->tfd_frags[seg].frag_len =
749 htole32(dmamap->dm_segs[seg].ds_len);
750 totlen += dmamap->dm_segs[seg].ds_len;
751 }
752 tfd->tfd_frags[seg - 1].frag_len |= htole32(FRAG_LAST);
753
754 /* Initialize the descriptor. */
755 tfd->tfd_next = htole32(STE_CDTXADDR(sc, nexttx));
756 tfd->tfd_control = htole32(TFD_FrameId(nexttx) | (totlen & 3));
757
758 /* Sync the descriptor. */
759 STE_CDTXSYNC(sc, nexttx,
760 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
761
762 /*
763 * Store a pointer to the packet so we can free it later,
764 * and remember what txdirty will be once the packet is
765 * done.
766 */
767 ds->ds_mbuf = m0;
768
769 /* Advance the tx pointer. */
770 sc->sc_txpending++;
771 sc->sc_txlast = nexttx;
772
773 /*
774 * Pass the packet to any BPF listeners.
775 */
776 bpf_mtap(ifp, m0, BPF_D_OUT);
777 }
778
779 if (sc->sc_txpending != opending) {
780 /*
781 * We enqueued packets. If the transmitter was idle,
782 * reset the txdirty pointer.
783 */
784 if (opending == 0)
785 sc->sc_txdirty = STE_NEXTTX(olasttx);
786
787 /*
788 * Cause a descriptor interrupt to happen on the
789 * last packet we enqueued, and also cause the
790 * DMA engine to wait after is has finished processing
791 * it.
792 */
793 sc->sc_txdescs[sc->sc_txlast].tfd_next = 0;
794 sc->sc_txdescs[sc->sc_txlast].tfd_control |=
795 htole32(TFD_TxDMAIndicate);
796 STE_CDTXSYNC(sc, sc->sc_txlast,
797 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
798
799 /*
800 * Link up the new chain of descriptors to the
801 * last.
802 */
803 sc->sc_txdescs[olasttx].tfd_next =
804 htole32(STE_CDTXADDR(sc, STE_NEXTTX(olasttx)));
805 STE_CDTXSYNC(sc, olasttx,
806 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
807
808 /*
809 * Kick the transmit DMA logic. Note that since we're
810 * using auto-polling, reading the Tx desc pointer will
811 * give it the nudge it needs to get going.
812 */
813 if (bus_space_read_4(sc->sc_st, sc->sc_sh,
814 STE_TxDMAListPtr) == 0) {
815 bus_space_write_4(sc->sc_st, sc->sc_sh,
816 STE_DMACtrl, DC_TxDMAHalt);
817 ste_dmahalt_wait(sc);
818 bus_space_write_4(sc->sc_st, sc->sc_sh,
819 STE_TxDMAListPtr,
820 STE_CDTXADDR(sc, STE_NEXTTX(olasttx)));
821 bus_space_write_4(sc->sc_st, sc->sc_sh,
822 STE_DMACtrl, DC_TxDMAResume);
823 }
824
825 /* Set a watchdog timer in case the chip flakes out. */
826 ifp->if_timer = 5;
827 }
828 }
829
830 /*
831 * ste_watchdog: [ifnet interface function]
832 *
833 * Watchdog timer handler.
834 */
835 static void
836 ste_watchdog(struct ifnet *ifp)
837 {
838 struct ste_softc *sc = ifp->if_softc;
839
840 printf("%s: device timeout\n", device_xname(sc->sc_dev));
841 if_statinc(ifp, if_oerrors);
842
843 ste_txintr(sc);
844 ste_rxintr(sc);
845 (void) ste_init(ifp);
846
847 /* Try to get more packets going. */
848 ste_start(ifp);
849 }
850
851 /*
852 * ste_ioctl: [ifnet interface function]
853 *
854 * Handle control requests from the operator.
855 */
856 static int
857 ste_ioctl(struct ifnet *ifp, u_long cmd, void *data)
858 {
859 struct ste_softc *sc = ifp->if_softc;
860 int s, error;
861
862 s = splnet();
863
864 error = ether_ioctl(ifp, cmd, data);
865 if (error == ENETRESET) {
866 /*
867 * Multicast list has changed; set the hardware filter
868 * accordingly.
869 */
870 if (ifp->if_flags & IFF_RUNNING)
871 ste_set_filter(sc);
872 error = 0;
873 }
874
875 /* Try to get more packets going. */
876 ste_start(ifp);
877
878 splx(s);
879 return (error);
880 }
881
882 /*
883 * ste_intr:
884 *
885 * Interrupt service routine.
886 */
887 static int
888 ste_intr(void *arg)
889 {
890 struct ste_softc *sc = arg;
891 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
892 uint16_t isr;
893 uint8_t txstat;
894 int wantinit;
895
896 if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STE_IntStatus) &
897 IS_InterruptStatus) == 0)
898 return (0);
899
900 for (wantinit = 0; wantinit == 0;) {
901 isr = bus_space_read_2(sc->sc_st, sc->sc_sh, STE_IntStatusAck);
902 if ((isr & sc->sc_IntEnable) == 0)
903 break;
904
905 /* Receive interrupts. */
906 if (isr & IE_RxDMAComplete)
907 ste_rxintr(sc);
908
909 /* Transmit interrupts. */
910 if (isr & (IE_TxDMAComplete | IE_TxComplete))
911 ste_txintr(sc);
912
913 /* Statistics overflow. */
914 if (isr & IE_UpdateStats)
915 ste_stats_update(sc);
916
917 /* Transmission errors. */
918 if (isr & IE_TxComplete) {
919 for (;;) {
920 txstat = bus_space_read_1(sc->sc_st, sc->sc_sh,
921 STE_TxStatus);
922 if ((txstat & TS_TxComplete) == 0)
923 break;
924 if (txstat & TS_TxUnderrun) {
925 sc->sc_txthresh += 32;
926 if (sc->sc_txthresh > 0x1ffc)
927 sc->sc_txthresh = 0x1ffc;
928 printf("%s: transmit underrun, new "
929 "threshold: %d bytes\n",
930 device_xname(sc->sc_dev),
931 sc->sc_txthresh);
932 ste_reset(sc, AC_TxReset | AC_DMA |
933 AC_FIFO | AC_Network);
934 ste_setthresh(sc);
935 bus_space_write_1(sc->sc_st, sc->sc_sh,
936 STE_TxDMAPollPeriod, 127);
937 ste_txrestart(sc,
938 bus_space_read_1(sc->sc_st,
939 sc->sc_sh, STE_TxFrameId));
940 }
941 if (txstat & TS_TxReleaseError) {
942 printf("%s: Tx FIFO release error\n",
943 device_xname(sc->sc_dev));
944 wantinit = 1;
945 }
946 if (txstat & TS_MaxCollisions) {
947 printf("%s: excessive collisions\n",
948 device_xname(sc->sc_dev));
949 wantinit = 1;
950 }
951 if (txstat & TS_TxStatusOverflow) {
952 printf("%s: status overflow\n",
953 device_xname(sc->sc_dev));
954 wantinit = 1;
955 }
956 bus_space_write_2(sc->sc_st, sc->sc_sh,
957 STE_TxStatus, 0);
958 }
959 }
960
961 /* Host interface errors. */
962 if (isr & IE_HostError) {
963 printf("%s: Host interface error\n",
964 device_xname(sc->sc_dev));
965 wantinit = 1;
966 }
967 }
968
969 if (wantinit)
970 ste_init(ifp);
971
972 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_IntEnable,
973 sc->sc_IntEnable);
974
975 /* Try to get more packets going. */
976 if_schedule_deferred_start(ifp);
977
978 return (1);
979 }
980
981 /*
982 * ste_txintr:
983 *
984 * Helper; handle transmit interrupts.
985 */
986 static void
987 ste_txintr(struct ste_softc *sc)
988 {
989 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
990 struct ste_descsoft *ds;
991 uint32_t control;
992 int i;
993
994 /*
995 * Go through our Tx list and free mbufs for those
996 * frames which have been transmitted.
997 */
998 for (i = sc->sc_txdirty; sc->sc_txpending != 0;
999 i = STE_NEXTTX(i), sc->sc_txpending--) {
1000 ds = &sc->sc_txsoft[i];
1001
1002 STE_CDTXSYNC(sc, i,
1003 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1004
1005 control = le32toh(sc->sc_txdescs[i].tfd_control);
1006 if ((control & TFD_TxDMAComplete) == 0)
1007 break;
1008
1009 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap,
1010 0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1011 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1012 m_freem(ds->ds_mbuf);
1013 ds->ds_mbuf = NULL;
1014 }
1015
1016 /* Update the dirty transmit buffer pointer. */
1017 sc->sc_txdirty = i;
1018
1019 /*
1020 * If there are no more pending transmissions, cancel the watchdog
1021 * timer.
1022 */
1023 if (sc->sc_txpending == 0)
1024 ifp->if_timer = 0;
1025 }
1026
1027 /*
1028 * ste_rxintr:
1029 *
1030 * Helper; handle receive interrupts.
1031 */
1032 static void
1033 ste_rxintr(struct ste_softc *sc)
1034 {
1035 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1036 struct ste_descsoft *ds;
1037 struct mbuf *m;
1038 uint32_t status;
1039 int i, len;
1040
1041 for (i = sc->sc_rxptr;; i = STE_NEXTRX(i)) {
1042 ds = &sc->sc_rxsoft[i];
1043
1044 STE_CDRXSYNC(sc, i,
1045 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1046
1047 status = le32toh(sc->sc_rxdescs[i].rfd_status);
1048
1049 if ((status & RFD_RxDMAComplete) == 0)
1050 break;
1051
1052 /*
1053 * If the packet had an error, simply recycle the
1054 * buffer. Note, we count the error later in the
1055 * periodic stats update.
1056 */
1057 if (status & RFD_RxFrameError) {
1058 STE_INIT_RXDESC(sc, i);
1059 continue;
1060 }
1061
1062 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1063 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1064
1065 /*
1066 * No errors; receive the packet. Note, we have
1067 * configured the chip to not include the CRC at
1068 * the end of the packet.
1069 */
1070 len = RFD_RxDMAFrameLen(status);
1071
1072 /*
1073 * If the packet is small enough to fit in a
1074 * single header mbuf, allocate one and copy
1075 * the data into it. This greatly reduces
1076 * memory consumption when we receive lots
1077 * of small packets.
1078 *
1079 * Otherwise, we add a new buffer to the receive
1080 * chain. If this fails, we drop the packet and
1081 * recycle the old buffer.
1082 */
1083 if (ste_copy_small != 0 && len <= (MHLEN - 2)) {
1084 MGETHDR(m, M_DONTWAIT, MT_DATA);
1085 if (m == NULL)
1086 goto dropit;
1087 MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
1088 m->m_data += 2;
1089 memcpy(mtod(m, void *),
1090 mtod(ds->ds_mbuf, void *), len);
1091 STE_INIT_RXDESC(sc, i);
1092 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1093 ds->ds_dmamap->dm_mapsize,
1094 BUS_DMASYNC_PREREAD);
1095 } else {
1096 m = ds->ds_mbuf;
1097 if (ste_add_rxbuf(sc, i) != 0) {
1098 dropit:
1099 if_statinc(ifp, if_ierrors);
1100 STE_INIT_RXDESC(sc, i);
1101 bus_dmamap_sync(sc->sc_dmat,
1102 ds->ds_dmamap, 0,
1103 ds->ds_dmamap->dm_mapsize,
1104 BUS_DMASYNC_PREREAD);
1105 continue;
1106 }
1107 }
1108
1109 m_set_rcvif(m, ifp);
1110 m->m_pkthdr.len = m->m_len = len;
1111
1112 /* Pass it on. */
1113 if_percpuq_enqueue(ifp->if_percpuq, m);
1114 }
1115
1116 /* Update the receive pointer. */
1117 sc->sc_rxptr = i;
1118 }
1119
1120 /*
1121 * ste_tick:
1122 *
1123 * One second timer, used to tick the MII.
1124 */
1125 static void
1126 ste_tick(void *arg)
1127 {
1128 struct ste_softc *sc = arg;
1129 int s;
1130
1131 s = splnet();
1132 mii_tick(&sc->sc_mii);
1133 ste_stats_update(sc);
1134 splx(s);
1135
1136 callout_schedule(&sc->sc_tick_ch, hz);
1137 }
1138
1139 /*
1140 * ste_stats_update:
1141 *
1142 * Read the ST-201 statistics counters.
1143 */
1144 static void
1145 ste_stats_update(struct ste_softc *sc)
1146 {
1147 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1148 bus_space_tag_t st = sc->sc_st;
1149 bus_space_handle_t sh = sc->sc_sh;
1150
1151 (void) bus_space_read_2(st, sh, STE_OctetsReceivedOk0);
1152 (void) bus_space_read_2(st, sh, STE_OctetsReceivedOk1);
1153
1154 (void) bus_space_read_2(st, sh, STE_OctetsTransmittedOk0);
1155 (void) bus_space_read_2(st, sh, STE_OctetsTransmittedOk1);
1156
1157 net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
1158
1159 if_statadd_ref(nsr, if_opackets,
1160 (u_int) bus_space_read_2(st, sh, STE_FramesTransmittedOK));
1161
1162 (void) bus_space_read_2(st, sh, STE_FramesReceivedOK);
1163
1164 if_statadd_ref(nsr, if_collisions,
1165 (u_int) bus_space_read_1(st, sh, STE_LateCollisions) +
1166 (u_int) bus_space_read_1(st, sh, STE_MultipleColFrames) +
1167 (u_int) bus_space_read_1(st, sh, STE_SingleColFrames));
1168
1169 (void) bus_space_read_1(st, sh, STE_FramesWDeferredXmt);
1170
1171 if_statadd_ref(nsr, if_ierrors,
1172 (u_int) bus_space_read_1(st, sh, STE_FramesLostRxErrors));
1173
1174 if_statadd_ref(nsr, if_oerrors,
1175 (u_int) bus_space_read_1(st, sh, STE_FramesWExDeferral) +
1176 (u_int) bus_space_read_1(st, sh, STE_FramesXbortXSColls) +
1177 bus_space_read_1(st, sh, STE_CarrierSenseErrors));
1178
1179 IF_STAT_PUTREF(ifp);
1180
1181 (void) bus_space_read_1(st, sh, STE_BcstFramesXmtdOk);
1182 (void) bus_space_read_1(st, sh, STE_BcstFramesRcvdOk);
1183 (void) bus_space_read_1(st, sh, STE_McstFramesXmtdOk);
1184 (void) bus_space_read_1(st, sh, STE_McstFramesRcvdOk);
1185 }
1186
1187 /*
1188 * ste_reset:
1189 *
1190 * Perform a soft reset on the ST-201.
1191 */
1192 static void
1193 ste_reset(struct ste_softc *sc, uint32_t rstbits)
1194 {
1195 uint32_t ac;
1196 int i;
1197
1198 ac = bus_space_read_4(sc->sc_st, sc->sc_sh, STE_AsicCtrl);
1199
1200 bus_space_write_4(sc->sc_st, sc->sc_sh, STE_AsicCtrl, ac | rstbits);
1201
1202 delay(50000);
1203
1204 for (i = 0; i < STE_TIMEOUT; i++) {
1205 delay(1000);
1206 if ((bus_space_read_4(sc->sc_st, sc->sc_sh, STE_AsicCtrl) &
1207 AC_ResetBusy) == 0)
1208 break;
1209 }
1210
1211 if (i == STE_TIMEOUT)
1212 printf("%s: reset failed to complete\n",
1213 device_xname(sc->sc_dev));
1214
1215 delay(1000);
1216 }
1217
1218 /*
1219 * ste_setthresh:
1220 *
1221 * set the various transmit threshold registers
1222 */
1223 static void
1224 ste_setthresh(struct ste_softc *sc)
1225 {
1226 /* set the TX threhold */
1227 bus_space_write_2(sc->sc_st, sc->sc_sh,
1228 STE_TxStartThresh, sc->sc_txthresh);
1229 /* Urgent threshold: set to sc_txthresh / 2 */
1230 bus_space_write_1(sc->sc_st, sc->sc_sh, STE_TxDMAUrgentThresh,
1231 sc->sc_txthresh >> 6);
1232 /* Burst threshold: use default value (256 bytes) */
1233 }
1234
1235 /*
1236 * restart TX at the given frame ID in the transmitter ring
1237 */
1238 static void
1239 ste_txrestart(struct ste_softc *sc, uint8_t id)
1240 {
1241 uint32_t control;
1242
1243 STE_CDTXSYNC(sc, id, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1244 control = le32toh(sc->sc_txdescs[id].tfd_control);
1245 control &= ~TFD_TxDMAComplete;
1246 sc->sc_txdescs[id].tfd_control = htole32(control);
1247 STE_CDTXSYNC(sc, id, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1248
1249 bus_space_write_4(sc->sc_st, sc->sc_sh, STE_TxDMAListPtr, 0);
1250 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_MacCtrl1, MC1_TxEnable);
1251 bus_space_write_4(sc->sc_st, sc->sc_sh, STE_DMACtrl, DC_TxDMAHalt);
1252 ste_dmahalt_wait(sc);
1253 bus_space_write_4(sc->sc_st, sc->sc_sh, STE_TxDMAListPtr,
1254 STE_CDTXADDR(sc, id));
1255 bus_space_write_4(sc->sc_st, sc->sc_sh, STE_DMACtrl, DC_TxDMAResume);
1256 }
1257
1258 /*
1259 * ste_init: [ ifnet interface function ]
1260 *
1261 * Initialize the interface. Must be called at splnet().
1262 */
1263 static int
1264 ste_init(struct ifnet *ifp)
1265 {
1266 struct ste_softc *sc = ifp->if_softc;
1267 bus_space_tag_t st = sc->sc_st;
1268 bus_space_handle_t sh = sc->sc_sh;
1269 struct ste_descsoft *ds;
1270 int i, error = 0;
1271
1272 /*
1273 * Cancel any pending I/O.
1274 */
1275 ste_stop(ifp, 0);
1276
1277 /*
1278 * Reset the chip to a known state.
1279 */
1280 ste_reset(sc, AC_GlobalReset | AC_RxReset | AC_TxReset | AC_DMA |
1281 AC_FIFO | AC_Network | AC_Host | AC_AutoInit | AC_RstOut);
1282
1283 /*
1284 * Initialize the transmit descriptor ring.
1285 */
1286 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1287 sc->sc_txpending = 0;
1288 sc->sc_txdirty = 0;
1289 sc->sc_txlast = STE_NTXDESC - 1;
1290
1291 /*
1292 * Initialize the receive descriptor and receive job
1293 * descriptor rings.
1294 */
1295 for (i = 0; i < STE_NRXDESC; i++) {
1296 ds = &sc->sc_rxsoft[i];
1297 if (ds->ds_mbuf == NULL) {
1298 if ((error = ste_add_rxbuf(sc, i)) != 0) {
1299 printf("%s: unable to allocate or map rx "
1300 "buffer %d, error = %d\n",
1301 device_xname(sc->sc_dev), i, error);
1302 /*
1303 * XXX Should attempt to run with fewer receive
1304 * XXX buffers instead of just failing.
1305 */
1306 ste_rxdrain(sc);
1307 goto out;
1308 }
1309 } else
1310 STE_INIT_RXDESC(sc, i);
1311 }
1312 sc->sc_rxptr = 0;
1313
1314 /* Set the station address. */
1315 for (i = 0; i < ETHER_ADDR_LEN; i++)
1316 bus_space_write_1(st, sh, STE_StationAddress0 + 1,
1317 CLLADDR(ifp->if_sadl)[i]);
1318
1319 /* Set up the receive filter. */
1320 ste_set_filter(sc);
1321
1322 /*
1323 * Give the receive ring to the chip.
1324 */
1325 bus_space_write_4(st, sh, STE_RxDMAListPtr,
1326 STE_CDRXADDR(sc, sc->sc_rxptr));
1327
1328 /*
1329 * We defer giving the transmit ring to the chip until we
1330 * transmit the first packet.
1331 */
1332
1333 /*
1334 * Initialize the Tx auto-poll period. It's OK to make this number
1335 * large (127 is the max) -- we explicitly kick the transmit engine
1336 * when there's actually a packet. We are using auto-polling only
1337 * to make the interface to the transmit engine not suck.
1338 */
1339 bus_space_write_1(sc->sc_st, sc->sc_sh, STE_TxDMAPollPeriod, 127);
1340
1341 /* ..and the Rx auto-poll period. */
1342 bus_space_write_1(st, sh, STE_RxDMAPollPeriod, 64);
1343
1344 /* Initialize the Tx start threshold. */
1345 ste_setthresh(sc);
1346
1347 /* Set the FIFO release threshold to 512 bytes. */
1348 bus_space_write_1(st, sh, STE_TxReleaseThresh, 512 >> 4);
1349
1350 /* Set maximum packet size for VLAN. */
1351 if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
1352 bus_space_write_2(st, sh, STE_MaxFrameSize, ETHER_MAX_LEN + 4);
1353 else
1354 bus_space_write_2(st, sh, STE_MaxFrameSize, ETHER_MAX_LEN);
1355
1356 /*
1357 * Initialize the interrupt mask.
1358 */
1359 sc->sc_IntEnable = IE_HostError | IE_TxComplete | IE_UpdateStats |
1360 IE_TxDMAComplete | IE_RxDMAComplete;
1361
1362 bus_space_write_2(st, sh, STE_IntStatus, 0xffff);
1363 bus_space_write_2(st, sh, STE_IntEnable, sc->sc_IntEnable);
1364
1365 /*
1366 * Start the receive DMA engine.
1367 */
1368 bus_space_write_4(st, sh, STE_DMACtrl, sc->sc_DMACtrl | DC_RxDMAResume);
1369
1370 /*
1371 * Initialize MacCtrl0 -- do it before setting the media,
1372 * as setting the media will actually program the register.
1373 */
1374 sc->sc_MacCtrl0 = MC0_IFSSelect(0);
1375 if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
1376 sc->sc_MacCtrl0 |= MC0_RcvLargeFrames;
1377
1378 /*
1379 * Set the current media.
1380 */
1381 if ((error = ether_mediachange(ifp)) != 0)
1382 goto out;
1383
1384 /*
1385 * Start the MAC.
1386 */
1387 bus_space_write_2(st, sh, STE_MacCtrl1,
1388 MC1_StatisticsEnable | MC1_TxEnable | MC1_RxEnable);
1389
1390 /*
1391 * Start the one second MII clock.
1392 */
1393 callout_schedule(&sc->sc_tick_ch, hz);
1394
1395 /*
1396 * ...all done!
1397 */
1398 ifp->if_flags |= IFF_RUNNING;
1399
1400 out:
1401 if (error)
1402 printf("%s: interface not running\n", device_xname(sc->sc_dev));
1403 return (error);
1404 }
1405
1406 /*
1407 * ste_drain:
1408 *
1409 * Drain the receive queue.
1410 */
1411 static void
1412 ste_rxdrain(struct ste_softc *sc)
1413 {
1414 struct ste_descsoft *ds;
1415 int i;
1416
1417 for (i = 0; i < STE_NRXDESC; i++) {
1418 ds = &sc->sc_rxsoft[i];
1419 if (ds->ds_mbuf != NULL) {
1420 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1421 m_freem(ds->ds_mbuf);
1422 ds->ds_mbuf = NULL;
1423 }
1424 }
1425 }
1426
1427 /*
1428 * ste_stop: [ ifnet interface function ]
1429 *
1430 * Stop transmission on the interface.
1431 */
1432 static void
1433 ste_stop(struct ifnet *ifp, int disable)
1434 {
1435 struct ste_softc *sc = ifp->if_softc;
1436 struct ste_descsoft *ds;
1437 int i;
1438
1439 /*
1440 * Stop the one second clock.
1441 */
1442 callout_stop(&sc->sc_tick_ch);
1443
1444 /* Down the MII. */
1445 mii_down(&sc->sc_mii);
1446
1447 /*
1448 * Disable interrupts.
1449 */
1450 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_IntEnable, 0);
1451
1452 /*
1453 * Stop receiver, transmitter, and stats update.
1454 */
1455 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_MacCtrl1,
1456 MC1_StatisticsDisable | MC1_TxDisable | MC1_RxDisable);
1457
1458 /*
1459 * Stop the transmit and receive DMA.
1460 */
1461 bus_space_write_4(sc->sc_st, sc->sc_sh, STE_DMACtrl,
1462 DC_RxDMAHalt | DC_TxDMAHalt);
1463 ste_dmahalt_wait(sc);
1464
1465 /*
1466 * Release any queued transmit buffers.
1467 */
1468 for (i = 0; i < STE_NTXDESC; i++) {
1469 ds = &sc->sc_txsoft[i];
1470 if (ds->ds_mbuf != NULL) {
1471 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1472 m_freem(ds->ds_mbuf);
1473 ds->ds_mbuf = NULL;
1474 }
1475 }
1476
1477 /*
1478 * Mark the interface down and cancel the watchdog timer.
1479 */
1480 ifp->if_flags &= ~IFF_RUNNING;
1481 ifp->if_timer = 0;
1482
1483 if (disable)
1484 ste_rxdrain(sc);
1485 }
1486
1487 static int
1488 ste_eeprom_wait(struct ste_softc *sc)
1489 {
1490 int i;
1491
1492 for (i = 0; i < STE_TIMEOUT; i++) {
1493 delay(1000);
1494 if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STE_EepromCtrl) &
1495 EC_EepromBusy) == 0)
1496 return (0);
1497 }
1498 return (1);
1499 }
1500
1501 /*
1502 * ste_read_eeprom:
1503 *
1504 * Read data from the serial EEPROM.
1505 */
1506 static void
1507 ste_read_eeprom(struct ste_softc *sc, int offset, uint16_t *data)
1508 {
1509
1510 if (ste_eeprom_wait(sc))
1511 printf("%s: EEPROM failed to come ready\n",
1512 device_xname(sc->sc_dev));
1513
1514 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_EepromCtrl,
1515 EC_EepromAddress(offset) | EC_EepromOpcode(EC_OP_R));
1516 if (ste_eeprom_wait(sc))
1517 printf("%s: EEPROM read timed out\n",
1518 device_xname(sc->sc_dev));
1519 *data = bus_space_read_2(sc->sc_st, sc->sc_sh, STE_EepromData);
1520 }
1521
1522 /*
1523 * ste_add_rxbuf:
1524 *
1525 * Add a receive buffer to the indicated descriptor.
1526 */
1527 static int
1528 ste_add_rxbuf(struct ste_softc *sc, int idx)
1529 {
1530 struct ste_descsoft *ds = &sc->sc_rxsoft[idx];
1531 struct mbuf *m;
1532 int error;
1533
1534 MGETHDR(m, M_DONTWAIT, MT_DATA);
1535 if (m == NULL)
1536 return (ENOBUFS);
1537
1538 MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
1539 MCLGET(m, M_DONTWAIT);
1540 if ((m->m_flags & M_EXT) == 0) {
1541 m_freem(m);
1542 return (ENOBUFS);
1543 }
1544
1545 if (ds->ds_mbuf != NULL)
1546 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1547
1548 ds->ds_mbuf = m;
1549
1550 error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
1551 m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
1552 BUS_DMA_READ | BUS_DMA_NOWAIT);
1553 if (error) {
1554 printf("%s: can't load rx DMA map %d, error = %d\n",
1555 device_xname(sc->sc_dev), idx, error);
1556 panic("ste_add_rxbuf"); /* XXX */
1557 }
1558
1559 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1560 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1561
1562 STE_INIT_RXDESC(sc, idx);
1563
1564 return (0);
1565 }
1566
1567 /*
1568 * ste_set_filter:
1569 *
1570 * Set up the receive filter.
1571 */
1572 static void
1573 ste_set_filter(struct ste_softc *sc)
1574 {
1575 struct ethercom *ec = &sc->sc_ethercom;
1576 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1577 struct ether_multi *enm;
1578 struct ether_multistep step;
1579 uint32_t crc;
1580 uint16_t mchash[4];
1581
1582 sc->sc_ReceiveMode = RM_ReceiveUnicast;
1583 if (ifp->if_flags & IFF_BROADCAST)
1584 sc->sc_ReceiveMode |= RM_ReceiveBroadcast;
1585
1586 if (ifp->if_flags & IFF_PROMISC) {
1587 sc->sc_ReceiveMode |= RM_ReceiveAllFrames;
1588 goto allmulti;
1589 }
1590
1591 /*
1592 * Set up the multicast address filter by passing all multicast
1593 * addresses through a CRC generator, and then using the low-order
1594 * 6 bits as an index into the 64 bit multicast hash table. The
1595 * high order bits select the register, while the rest of the bits
1596 * select the bit within the register.
1597 */
1598
1599 memset(mchash, 0, sizeof(mchash));
1600
1601 ETHER_LOCK(ec);
1602 ETHER_FIRST_MULTI(step, ec, enm);
1603 if (enm == NULL) {
1604 ETHER_UNLOCK(ec);
1605 goto done;
1606 }
1607
1608 while (enm != NULL) {
1609 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1610 /*
1611 * We must listen to a range of multicast addresses.
1612 * For now, just accept all multicasts, rather than
1613 * trying to set only those filter bits needed to match
1614 * the range. (At this time, the only use of address
1615 * ranges is for IP multicast routing, for which the
1616 * range is big enough to require all bits set.)
1617 */
1618 ETHER_UNLOCK(ec);
1619 goto allmulti;
1620 }
1621
1622 crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
1623
1624 /* Just want the 6 least significant bits. */
1625 crc &= 0x3f;
1626
1627 /* Set the corresponding bit in the hash table. */
1628 mchash[crc >> 4] |= 1 << (crc & 0xf);
1629
1630 ETHER_NEXT_MULTI(step, enm);
1631 }
1632 ETHER_UNLOCK(ec);
1633
1634 sc->sc_ReceiveMode |= RM_ReceiveMulticastHash;
1635
1636 ifp->if_flags &= ~IFF_ALLMULTI;
1637 goto done;
1638
1639 allmulti:
1640 ifp->if_flags |= IFF_ALLMULTI;
1641 sc->sc_ReceiveMode |= RM_ReceiveMulticast;
1642
1643 done:
1644 if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
1645 /*
1646 * Program the multicast hash table.
1647 */
1648 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_HashTable0,
1649 mchash[0]);
1650 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_HashTable1,
1651 mchash[1]);
1652 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_HashTable2,
1653 mchash[2]);
1654 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_HashTable3,
1655 mchash[3]);
1656 }
1657
1658 bus_space_write_1(sc->sc_st, sc->sc_sh, STE_ReceiveMode,
1659 sc->sc_ReceiveMode);
1660 }
1661
1662 /*
1663 * ste_mii_readreg: [mii interface function]
1664 *
1665 * Read a PHY register on the MII of the ST-201.
1666 */
1667 static int
1668 ste_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
1669 {
1670 struct ste_softc *sc = device_private(self);
1671
1672 if (phy == 0 && !sc->sc_enable_phy0)
1673 return EIO;
1674
1675 return mii_bitbang_readreg(self, &ste_mii_bitbang_ops, phy, reg, val);
1676 }
1677
1678 /*
1679 * ste_mii_writereg: [mii interface function]
1680 *
1681 * Write a PHY register on the MII of the ST-201.
1682 */
1683 static int
1684 ste_mii_writereg(device_t self, int phy, int reg, uint16_t val)
1685 {
1686 struct ste_softc *sc = device_private(self);
1687
1688 if (phy == 0 && !sc->sc_enable_phy0)
1689 return EIO;
1690
1691 return mii_bitbang_writereg(self, &ste_mii_bitbang_ops, phy, reg, val);
1692 }
1693
1694 /*
1695 * ste_mii_statchg: [mii interface function]
1696 *
1697 * Callback from MII layer when media changes.
1698 */
1699 static void
1700 ste_mii_statchg(struct ifnet *ifp)
1701 {
1702 struct ste_softc *sc = ifp->if_softc;
1703
1704 if (sc->sc_mii.mii_media_active & IFM_FDX)
1705 sc->sc_MacCtrl0 |= MC0_FullDuplexEnable;
1706 else
1707 sc->sc_MacCtrl0 &= ~MC0_FullDuplexEnable;
1708
1709 /* XXX 802.1x flow-control? */
1710
1711 bus_space_write_2(sc->sc_st, sc->sc_sh, STE_MacCtrl0, sc->sc_MacCtrl0);
1712 }
1713
1714 /*
1715 * ste_mii_bitbang_read: [mii bit-bang interface function]
1716 *
1717 * Read the MII serial port for the MII bit-bang module.
1718 */
1719 static uint32_t
1720 ste_mii_bitbang_read(device_t self)
1721 {
1722 struct ste_softc *sc = device_private(self);
1723
1724 return (bus_space_read_1(sc->sc_st, sc->sc_sh, STE_PhyCtrl));
1725 }
1726
1727 /*
1728 * ste_mii_bitbang_write: [mii big-bang interface function]
1729 *
1730 * Write the MII serial port for the MII bit-bang module.
1731 */
1732 static void
1733 ste_mii_bitbang_write(device_t self, uint32_t val)
1734 {
1735 struct ste_softc *sc = device_private(self);
1736
1737 bus_space_write_1(sc->sc_st, sc->sc_sh, STE_PhyCtrl, val);
1738 }
1739