if_stge.c revision 1.2 1 1.2 thorpej /* $NetBSD: if_stge.c,v 1.2 2001/07/25 00:12:33 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1 thorpej * must display the following acknowledgement:
20 1.1 thorpej * This product includes software developed by the NetBSD
21 1.1 thorpej * Foundation, Inc. and its contributors.
22 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 thorpej * contributors may be used to endorse or promote products derived
24 1.1 thorpej * from this software without specific prior written permission.
25 1.1 thorpej *
26 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.1 thorpej /*
40 1.1 thorpej * Device driver for the Sundance Tech. TC9021 10/100/1000
41 1.1 thorpej * Ethernet controller.
42 1.1 thorpej */
43 1.1 thorpej
44 1.1 thorpej #include "bpfilter.h"
45 1.1 thorpej
46 1.1 thorpej #include <sys/param.h>
47 1.1 thorpej #include <sys/systm.h>
48 1.1 thorpej #include <sys/callout.h>
49 1.1 thorpej #include <sys/mbuf.h>
50 1.1 thorpej #include <sys/malloc.h>
51 1.1 thorpej #include <sys/kernel.h>
52 1.1 thorpej #include <sys/socket.h>
53 1.1 thorpej #include <sys/ioctl.h>
54 1.1 thorpej #include <sys/errno.h>
55 1.1 thorpej #include <sys/device.h>
56 1.1 thorpej #include <sys/queue.h>
57 1.1 thorpej
58 1.1 thorpej #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
59 1.1 thorpej
60 1.1 thorpej #include <net/if.h>
61 1.1 thorpej #include <net/if_dl.h>
62 1.1 thorpej #include <net/if_media.h>
63 1.1 thorpej #include <net/if_ether.h>
64 1.1 thorpej
65 1.1 thorpej #if NBPFILTER > 0
66 1.1 thorpej #include <net/bpf.h>
67 1.1 thorpej #endif
68 1.1 thorpej
69 1.1 thorpej #include <machine/bus.h>
70 1.1 thorpej #include <machine/intr.h>
71 1.1 thorpej
72 1.1 thorpej #include <dev/mii/mii.h>
73 1.1 thorpej #include <dev/mii/miivar.h>
74 1.1 thorpej #include <dev/mii/mii_bitbang.h>
75 1.1 thorpej
76 1.1 thorpej #include <dev/pci/pcireg.h>
77 1.1 thorpej #include <dev/pci/pcivar.h>
78 1.1 thorpej #include <dev/pci/pcidevs.h>
79 1.1 thorpej
80 1.1 thorpej #include <dev/pci/if_stgereg.h>
81 1.1 thorpej
82 1.1 thorpej /*
83 1.1 thorpej * Transmit descriptor list size.
84 1.1 thorpej */
85 1.1 thorpej #define STGE_NTXDESC 256
86 1.1 thorpej #define STGE_NTXDESC_MASK (STGE_NTXDESC - 1)
87 1.1 thorpej #define STGE_NEXTTX(x) (((x) + 1) & STGE_NTXDESC_MASK)
88 1.1 thorpej
89 1.1 thorpej /*
90 1.1 thorpej * Receive descriptor list size.
91 1.1 thorpej */
92 1.1 thorpej #define STGE_NRXDESC 256
93 1.1 thorpej #define STGE_NRXDESC_MASK (STGE_NRXDESC - 1)
94 1.1 thorpej #define STGE_NEXTRX(x) (((x) + 1) & STGE_NRXDESC_MASK)
95 1.1 thorpej
96 1.1 thorpej /*
97 1.1 thorpej * Only interrupt every N frames. Must be a power-of-two.
98 1.1 thorpej */
99 1.1 thorpej #define STGE_TXINTR_SPACING 16
100 1.1 thorpej #define STGE_TXINTR_SPACING_MASK (STGE_TXINTR_SPACING - 1)
101 1.1 thorpej
102 1.1 thorpej /*
103 1.1 thorpej * Control structures are DMA'd to the TC9021 chip. We allocate them in
104 1.1 thorpej * a single clump that maps to a single DMA segment to make several things
105 1.1 thorpej * easier.
106 1.1 thorpej */
107 1.1 thorpej struct stge_control_data {
108 1.1 thorpej /*
109 1.1 thorpej * The transmit descriptors.
110 1.1 thorpej */
111 1.1 thorpej struct stge_tfd scd_txdescs[STGE_NTXDESC];
112 1.1 thorpej
113 1.1 thorpej /*
114 1.1 thorpej * The receive descriptors.
115 1.1 thorpej */
116 1.1 thorpej struct stge_rfd scd_rxdescs[STGE_NRXDESC];
117 1.1 thorpej };
118 1.1 thorpej
119 1.1 thorpej #define STGE_CDOFF(x) offsetof(struct stge_control_data, x)
120 1.1 thorpej #define STGE_CDTXOFF(x) STGE_CDOFF(scd_txdescs[(x)])
121 1.1 thorpej #define STGE_CDRXOFF(x) STGE_CDOFF(scd_rxdescs[(x)])
122 1.1 thorpej
123 1.1 thorpej /*
124 1.1 thorpej * Software state for transmit and receive jobs.
125 1.1 thorpej */
126 1.1 thorpej struct stge_descsoft {
127 1.1 thorpej struct mbuf *ds_mbuf; /* head of our mbuf chain */
128 1.1 thorpej bus_dmamap_t ds_dmamap; /* our DMA map */
129 1.1 thorpej };
130 1.1 thorpej
131 1.1 thorpej /*
132 1.1 thorpej * Software state per device.
133 1.1 thorpej */
134 1.1 thorpej struct stge_softc {
135 1.1 thorpej struct device sc_dev; /* generic device information */
136 1.1 thorpej bus_space_tag_t sc_st; /* bus space tag */
137 1.1 thorpej bus_space_handle_t sc_sh; /* bus space handle */
138 1.1 thorpej bus_dma_tag_t sc_dmat; /* bus DMA tag */
139 1.1 thorpej struct ethercom sc_ethercom; /* ethernet common data */
140 1.1 thorpej void *sc_sdhook; /* shutdown hook */
141 1.1 thorpej int sc_rev; /* silicon revision */
142 1.1 thorpej
143 1.1 thorpej void *sc_ih; /* interrupt cookie */
144 1.1 thorpej
145 1.1 thorpej struct mii_data sc_mii; /* MII/media information */
146 1.1 thorpej
147 1.1 thorpej struct callout sc_tick_ch; /* tick callout */
148 1.1 thorpej
149 1.1 thorpej bus_dmamap_t sc_cddmamap; /* control data DMA map */
150 1.1 thorpej #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
151 1.1 thorpej
152 1.1 thorpej /*
153 1.1 thorpej * Software state for transmit and receive descriptors.
154 1.1 thorpej */
155 1.1 thorpej struct stge_descsoft sc_txsoft[STGE_NTXDESC];
156 1.1 thorpej struct stge_descsoft sc_rxsoft[STGE_NRXDESC];
157 1.1 thorpej
158 1.1 thorpej /*
159 1.1 thorpej * Control data structures.
160 1.1 thorpej */
161 1.1 thorpej struct stge_control_data *sc_control_data;
162 1.1 thorpej #define sc_txdescs sc_control_data->scd_txdescs
163 1.1 thorpej #define sc_rxdescs sc_control_data->scd_rxdescs
164 1.1 thorpej
165 1.1 thorpej #ifdef STGE_EVENT_COUNTERS
166 1.1 thorpej /*
167 1.1 thorpej * Event counters.
168 1.1 thorpej */
169 1.1 thorpej struct evcnt sc_ev_txstall; /* Tx stalled */
170 1.1 thorpej struct evcnt sc_ev_txdmaintr; /* Tx DMA interrupts */
171 1.1 thorpej struct evcnt sc_ev_txindintr; /* Tx Indicate interrupts */
172 1.1 thorpej struct evcnt sc_ev_rxintr; /* Rx interrupts */
173 1.1 thorpej
174 1.1 thorpej struct evcnt sc_ev_txseg1; /* Tx packets w/ 1 segment */
175 1.1 thorpej struct evcnt sc_ev_txseg2; /* Tx packets w/ 2 segments */
176 1.1 thorpej struct evcnt sc_ev_txseg3; /* Tx packets w/ 3 segments */
177 1.1 thorpej struct evcnt sc_ev_txseg4; /* Tx packets w/ 4 segments */
178 1.1 thorpej struct evcnt sc_ev_txseg5; /* Tx packets w/ 5 segments */
179 1.1 thorpej struct evcnt sc_ev_txsegmore; /* Tx packets w/ more than 5 segments */
180 1.1 thorpej struct evcnt sc_ev_txcopy; /* Tx packets that we had to copy */
181 1.1 thorpej
182 1.1 thorpej struct evcnt sc_ev_rxipsum; /* IP checksums checked in-bound */
183 1.1 thorpej struct evcnt sc_ev_rxtcpsum; /* TCP checksums checked in-bound */
184 1.1 thorpej struct evcnt sc_ev_rxudpsum; /* UDP checksums checked in-bound */
185 1.1 thorpej
186 1.1 thorpej struct evcnt sc_ev_txipsum; /* IP checksums comp. out-bound */
187 1.1 thorpej struct evcnt sc_ev_txtcpsum; /* TCP checksums comp. out-bound */
188 1.1 thorpej struct evcnt sc_ev_txudpsum; /* UDP checksums comp. out-bound */
189 1.1 thorpej #endif /* STGE_EVENT_COUNTERS */
190 1.1 thorpej
191 1.1 thorpej int sc_txpending; /* number of Tx requests pending */
192 1.1 thorpej int sc_txdirty; /* first dirty Tx descriptor */
193 1.1 thorpej int sc_txlast; /* last used Tx descriptor */
194 1.1 thorpej
195 1.1 thorpej int sc_rxptr; /* next ready Rx descriptor/descsoft */
196 1.1 thorpej int sc_rxdiscard;
197 1.1 thorpej int sc_rxlen;
198 1.1 thorpej struct mbuf *sc_rxhead;
199 1.1 thorpej struct mbuf *sc_rxtail;
200 1.1 thorpej struct mbuf **sc_rxtailp;
201 1.1 thorpej
202 1.1 thorpej int sc_txthresh; /* Tx threshold */
203 1.1 thorpej int sc_usefiber; /* if we're fiber */
204 1.1 thorpej uint32_t sc_DMACtrl; /* prototype DMACtrl register */
205 1.1 thorpej uint32_t sc_MACCtrl; /* prototype MacCtrl register */
206 1.1 thorpej uint16_t sc_IntEnable; /* prototype IntEnable register */
207 1.1 thorpej uint16_t sc_ReceiveMode; /* prototype ReceiveMode register */
208 1.1 thorpej uint8_t sc_PhyCtrl; /* prototype PhyCtrl register */
209 1.1 thorpej };
210 1.1 thorpej
211 1.1 thorpej #define STGE_RXCHAIN_RESET(sc) \
212 1.1 thorpej do { \
213 1.1 thorpej (sc)->sc_rxtailp = &(sc)->sc_rxhead; \
214 1.1 thorpej *(sc)->sc_rxtailp = NULL; \
215 1.1 thorpej (sc)->sc_rxlen = 0; \
216 1.1 thorpej } while (/*CONSTCOND*/0)
217 1.1 thorpej
218 1.1 thorpej #define STGE_RXCHAIN_LINK(sc, m) \
219 1.1 thorpej do { \
220 1.1 thorpej *(sc)->sc_rxtailp = (sc)->sc_rxtail = (m); \
221 1.1 thorpej (sc)->sc_rxtailp = &(m)->m_next; \
222 1.1 thorpej } while (/*CONSTCOND*/0)
223 1.1 thorpej
224 1.1 thorpej #ifdef STGE_EVENT_COUNTERS
225 1.1 thorpej #define STGE_EVCNT_INCR(ev) (ev)->ev_count++
226 1.1 thorpej #else
227 1.1 thorpej #define STGE_EVCNT_INCR(ev) /* nothing */
228 1.1 thorpej #endif
229 1.1 thorpej
230 1.1 thorpej #define STGE_CDTXADDR(sc, x) ((sc)->sc_cddma + STGE_CDTXOFF((x)))
231 1.1 thorpej #define STGE_CDRXADDR(sc, x) ((sc)->sc_cddma + STGE_CDRXOFF((x)))
232 1.1 thorpej
233 1.1 thorpej #define STGE_CDTXSYNC(sc, x, ops) \
234 1.1 thorpej bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
235 1.1 thorpej STGE_CDTXOFF((x)), sizeof(struct stge_tfd), (ops))
236 1.1 thorpej
237 1.1 thorpej #define STGE_CDRXSYNC(sc, x, ops) \
238 1.1 thorpej bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
239 1.1 thorpej STGE_CDRXOFF((x)), sizeof(struct stge_rfd), (ops))
240 1.1 thorpej
241 1.1 thorpej #define STGE_INIT_RXDESC(sc, x) \
242 1.1 thorpej do { \
243 1.1 thorpej struct stge_descsoft *__ds = &(sc)->sc_rxsoft[(x)]; \
244 1.1 thorpej struct stge_rfd *__rfd = &(sc)->sc_rxdescs[(x)]; \
245 1.1 thorpej \
246 1.1 thorpej /* \
247 1.1 thorpej * Note: We scoot the packet forward 2 bytes in the buffer \
248 1.1 thorpej * so that the payload after the Ethernet header is aligned \
249 1.1 thorpej * to a 4-byte boundary. \
250 1.1 thorpej */ \
251 1.1 thorpej __rfd->rfd_frag.frag_word0 = \
252 1.1 thorpej htole64(FRAG_ADDR(__ds->ds_dmamap->dm_segs[0].ds_addr + 2) |\
253 1.1 thorpej FRAG_LEN(MCLBYTES - 2)); \
254 1.1 thorpej __rfd->rfd_next = \
255 1.1 thorpej htole64((uint64_t)STGE_CDRXADDR((sc), STGE_NEXTRX((x)))); \
256 1.1 thorpej __rfd->rfd_status = 0; \
257 1.1 thorpej STGE_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
258 1.1 thorpej } while (/*CONSTCOND*/0)
259 1.1 thorpej
260 1.1 thorpej #define STGE_TIMEOUT 1000
261 1.1 thorpej
262 1.1 thorpej void stge_start(struct ifnet *);
263 1.1 thorpej void stge_watchdog(struct ifnet *);
264 1.1 thorpej int stge_ioctl(struct ifnet *, u_long, caddr_t);
265 1.1 thorpej int stge_init(struct ifnet *);
266 1.1 thorpej void stge_stop(struct ifnet *, int);
267 1.1 thorpej
268 1.1 thorpej void stge_shutdown(void *);
269 1.1 thorpej
270 1.1 thorpej void stge_reset(struct stge_softc *);
271 1.1 thorpej void stge_rxdrain(struct stge_softc *);
272 1.1 thorpej int stge_add_rxbuf(struct stge_softc *, int);
273 1.1 thorpej #if 0
274 1.1 thorpej void stge_read_eeprom(struct stge_softc *, int, uint16_t *);
275 1.1 thorpej #endif
276 1.1 thorpej void stge_tick(void *);
277 1.1 thorpej
278 1.1 thorpej void stge_stats_update(struct stge_softc *);
279 1.1 thorpej
280 1.1 thorpej void stge_set_filter(struct stge_softc *);
281 1.1 thorpej
282 1.1 thorpej int stge_intr(void *);
283 1.1 thorpej void stge_txintr(struct stge_softc *);
284 1.1 thorpej void stge_rxintr(struct stge_softc *);
285 1.1 thorpej
286 1.1 thorpej int stge_mii_readreg(struct device *, int, int);
287 1.1 thorpej void stge_mii_writereg(struct device *, int, int, int);
288 1.1 thorpej void stge_mii_statchg(struct device *);
289 1.1 thorpej
290 1.1 thorpej int stge_mediachange(struct ifnet *);
291 1.1 thorpej void stge_mediastatus(struct ifnet *, struct ifmediareq *);
292 1.1 thorpej
293 1.1 thorpej int stge_match(struct device *, struct cfdata *, void *);
294 1.1 thorpej void stge_attach(struct device *, struct device *, void *);
295 1.1 thorpej
296 1.1 thorpej int stge_copy_small = 0;
297 1.1 thorpej
298 1.1 thorpej struct cfattach stge_ca = {
299 1.1 thorpej sizeof(struct stge_softc), stge_match, stge_attach,
300 1.1 thorpej };
301 1.1 thorpej
302 1.1 thorpej uint32_t stge_mii_bitbang_read(struct device *);
303 1.1 thorpej void stge_mii_bitbang_write(struct device *, uint32_t);
304 1.1 thorpej
305 1.1 thorpej const struct mii_bitbang_ops stge_mii_bitbang_ops = {
306 1.1 thorpej stge_mii_bitbang_read,
307 1.1 thorpej stge_mii_bitbang_write,
308 1.1 thorpej {
309 1.1 thorpej PC_MgmtData, /* MII_BIT_MDO */
310 1.1 thorpej PC_MgmtData, /* MII_BIT_MDI */
311 1.1 thorpej PC_MgmtClk, /* MII_BIT_MDC */
312 1.1 thorpej PC_MgmtDir, /* MII_BIT_DIR_HOST_PHY */
313 1.1 thorpej 0, /* MII_BIT_DIR_PHY_HOST */
314 1.1 thorpej }
315 1.1 thorpej };
316 1.1 thorpej
317 1.1 thorpej /*
318 1.1 thorpej * Devices supported by this driver.
319 1.1 thorpej */
320 1.1 thorpej const struct stge_product {
321 1.1 thorpej pci_vendor_id_t stge_vendor;
322 1.1 thorpej pci_product_id_t stge_product;
323 1.1 thorpej const char *stge_name;
324 1.1 thorpej } stge_products[] = {
325 1.1 thorpej { PCI_VENDOR_SUNDANCETI, PCI_PRODUCT_SUNDANCETI_ST2021,
326 1.1 thorpej "Sundance ST-2021 Gigabit Ethernet" },
327 1.1 thorpej
328 1.1 thorpej { PCI_VENDOR_TAMARACK, PCI_PRODUCT_TAMARACK_TC9021,
329 1.1 thorpej "Tamarack TC9021 Gigabit Ethernet" },
330 1.1 thorpej
331 1.1 thorpej { PCI_VENDOR_TAMARACK, PCI_PRODUCT_TAMARACK_TC9021_ALT,
332 1.1 thorpej "Tamarack TC9021 Gigabit Ethernet" },
333 1.1 thorpej
334 1.1 thorpej /*
335 1.1 thorpej * The Sundance sample boards use the Sundance vendor ID,
336 1.1 thorpej * but the Tamarack product ID.
337 1.1 thorpej */
338 1.1 thorpej { PCI_VENDOR_SUNDANCETI, PCI_PRODUCT_TAMARACK_TC9021,
339 1.1 thorpej "Sundance TC9021 Gigabit Ethernet" },
340 1.1 thorpej
341 1.1 thorpej { PCI_VENDOR_SUNDANCETI, PCI_PRODUCT_TAMARACK_TC9021_ALT,
342 1.1 thorpej "Sundance TC9021 Gigabit Ethernet" },
343 1.1 thorpej
344 1.1 thorpej { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DL4000,
345 1.1 thorpej "D-Link DL-4000 Gigabit Ethernet" },
346 1.1 thorpej
347 1.1 thorpej { 0, 0,
348 1.1 thorpej NULL },
349 1.1 thorpej };
350 1.1 thorpej
351 1.1 thorpej static const struct stge_product *
352 1.1 thorpej stge_lookup(const struct pci_attach_args *pa)
353 1.1 thorpej {
354 1.1 thorpej const struct stge_product *sp;
355 1.1 thorpej
356 1.1 thorpej for (sp = stge_products; sp->stge_name != NULL; sp++) {
357 1.1 thorpej if (PCI_VENDOR(pa->pa_id) == sp->stge_vendor &&
358 1.1 thorpej PCI_PRODUCT(pa->pa_id) == sp->stge_product)
359 1.1 thorpej return (sp);
360 1.1 thorpej }
361 1.1 thorpej return (NULL);
362 1.1 thorpej }
363 1.1 thorpej
364 1.1 thorpej int
365 1.1 thorpej stge_match(struct device *parent, struct cfdata *cf, void *aux)
366 1.1 thorpej {
367 1.1 thorpej struct pci_attach_args *pa = aux;
368 1.1 thorpej
369 1.1 thorpej if (stge_lookup(pa) != NULL)
370 1.1 thorpej return (1);
371 1.1 thorpej
372 1.1 thorpej return (0);
373 1.1 thorpej }
374 1.1 thorpej
375 1.1 thorpej void
376 1.1 thorpej stge_attach(struct device *parent, struct device *self, void *aux)
377 1.1 thorpej {
378 1.1 thorpej struct stge_softc *sc = (struct stge_softc *) self;
379 1.1 thorpej struct pci_attach_args *pa = aux;
380 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
381 1.1 thorpej pci_chipset_tag_t pc = pa->pa_pc;
382 1.1 thorpej pci_intr_handle_t ih;
383 1.1 thorpej const char *intrstr = NULL;
384 1.1 thorpej bus_space_tag_t iot, memt;
385 1.1 thorpej bus_space_handle_t ioh, memh;
386 1.1 thorpej bus_dma_segment_t seg;
387 1.1 thorpej int ioh_valid, memh_valid;
388 1.1 thorpej int i, rseg, error;
389 1.1 thorpej const struct stge_product *sp;
390 1.1 thorpej pcireg_t pmode;
391 1.1 thorpej uint8_t enaddr[ETHER_ADDR_LEN];
392 1.1 thorpej int pmreg;
393 1.1 thorpej
394 1.1 thorpej callout_init(&sc->sc_tick_ch);
395 1.1 thorpej
396 1.1 thorpej sp = stge_lookup(pa);
397 1.1 thorpej if (sp == NULL) {
398 1.1 thorpej printf("\n");
399 1.1 thorpej panic("ste_attach: impossible");
400 1.1 thorpej }
401 1.1 thorpej
402 1.1 thorpej sc->sc_rev = PCI_REVISION(pa->pa_class);
403 1.1 thorpej
404 1.1 thorpej printf(": %s, rev. %d\n", sp->stge_name, sc->sc_rev);
405 1.1 thorpej
406 1.1 thorpej /*
407 1.1 thorpej * Map the device.
408 1.1 thorpej */
409 1.1 thorpej ioh_valid = (pci_mapreg_map(pa, STGE_PCI_IOBA,
410 1.1 thorpej PCI_MAPREG_TYPE_IO, 0,
411 1.1 thorpej &iot, &ioh, NULL, NULL) == 0);
412 1.1 thorpej memh_valid = (pci_mapreg_map(pa, STGE_PCI_MMBA,
413 1.1 thorpej PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
414 1.1 thorpej &memt, &memh, NULL, NULL) == 0);
415 1.1 thorpej
416 1.1 thorpej if (memh_valid) {
417 1.1 thorpej sc->sc_st = memt;
418 1.1 thorpej sc->sc_sh = memh;
419 1.1 thorpej } else if (ioh_valid) {
420 1.1 thorpej sc->sc_st = iot;
421 1.1 thorpej sc->sc_sh = ioh;
422 1.1 thorpej } else {
423 1.1 thorpej printf("%s: unable to map device registers\n",
424 1.1 thorpej sc->sc_dev.dv_xname);
425 1.1 thorpej return;
426 1.1 thorpej }
427 1.1 thorpej
428 1.1 thorpej sc->sc_dmat = pa->pa_dmat;
429 1.1 thorpej
430 1.1 thorpej /* Enable bus mastering. */
431 1.1 thorpej pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
432 1.1 thorpej pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
433 1.1 thorpej PCI_COMMAND_MASTER_ENABLE);
434 1.1 thorpej
435 1.1 thorpej /* Get it out of power save mode if needed. */
436 1.1 thorpej if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
437 1.1 thorpej pmode = pci_conf_read(pc, pa->pa_tag, pmreg + 4) & 0x3;
438 1.1 thorpej if (pmode == 3) {
439 1.1 thorpej /*
440 1.1 thorpej * The card has lost all configuration data in
441 1.1 thorpej * this state, so punt.
442 1.1 thorpej */
443 1.1 thorpej printf("%s: unable to wake up from power state D3\n",
444 1.1 thorpej sc->sc_dev.dv_xname);
445 1.1 thorpej return;
446 1.1 thorpej }
447 1.1 thorpej if (pmode != 0) {
448 1.1 thorpej printf("%s: waking up from power state D%d\n",
449 1.1 thorpej sc->sc_dev.dv_xname, pmode);
450 1.1 thorpej pci_conf_write(pc, pa->pa_tag, pmreg + 4, 0);
451 1.1 thorpej }
452 1.1 thorpej }
453 1.1 thorpej
454 1.1 thorpej /*
455 1.1 thorpej * Map and establish our interrupt.
456 1.1 thorpej */
457 1.1 thorpej if (pci_intr_map(pa, &ih)) {
458 1.1 thorpej printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
459 1.1 thorpej return;
460 1.1 thorpej }
461 1.1 thorpej intrstr = pci_intr_string(pc, ih);
462 1.1 thorpej sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, stge_intr, sc);
463 1.1 thorpej if (sc->sc_ih == NULL) {
464 1.1 thorpej printf("%s: unable to establish interrupt",
465 1.1 thorpej sc->sc_dev.dv_xname);
466 1.1 thorpej if (intrstr != NULL)
467 1.1 thorpej printf(" at %s", intrstr);
468 1.1 thorpej printf("\n");
469 1.1 thorpej return;
470 1.1 thorpej }
471 1.1 thorpej printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
472 1.1 thorpej
473 1.1 thorpej /*
474 1.1 thorpej * Allocate the control data structures, and create and load the
475 1.1 thorpej * DMA map for it.
476 1.1 thorpej */
477 1.1 thorpej if ((error = bus_dmamem_alloc(sc->sc_dmat,
478 1.1 thorpej sizeof(struct stge_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
479 1.1 thorpej 0)) != 0) {
480 1.1 thorpej printf("%s: unable to allocate control data, error = %d\n",
481 1.1 thorpej sc->sc_dev.dv_xname, error);
482 1.1 thorpej goto fail_0;
483 1.1 thorpej }
484 1.1 thorpej
485 1.1 thorpej if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
486 1.1 thorpej sizeof(struct stge_control_data), (caddr_t *)&sc->sc_control_data,
487 1.1 thorpej BUS_DMA_COHERENT)) != 0) {
488 1.1 thorpej printf("%s: unable to map control data, error = %d\n",
489 1.1 thorpej sc->sc_dev.dv_xname, error);
490 1.1 thorpej goto fail_1;
491 1.1 thorpej }
492 1.1 thorpej
493 1.1 thorpej if ((error = bus_dmamap_create(sc->sc_dmat,
494 1.1 thorpej sizeof(struct stge_control_data), 1,
495 1.1 thorpej sizeof(struct stge_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
496 1.1 thorpej printf("%s: unable to create control data DMA map, "
497 1.1 thorpej "error = %d\n", sc->sc_dev.dv_xname, error);
498 1.1 thorpej goto fail_2;
499 1.1 thorpej }
500 1.1 thorpej
501 1.1 thorpej if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
502 1.1 thorpej sc->sc_control_data, sizeof(struct stge_control_data), NULL,
503 1.1 thorpej 0)) != 0) {
504 1.1 thorpej printf("%s: unable to load control data DMA map, error = %d\n",
505 1.1 thorpej sc->sc_dev.dv_xname, error);
506 1.1 thorpej goto fail_3;
507 1.1 thorpej }
508 1.1 thorpej
509 1.1 thorpej /*
510 1.1 thorpej * Create the transmit buffer DMA maps. Note that rev B.3
511 1.1 thorpej * and earlier seem to have a bug regarding multi-fragment
512 1.1 thorpej * packets. We need to limit the number of Tx segments on
513 1.1 thorpej * such chips to 1.
514 1.1 thorpej */
515 1.1 thorpej for (i = 0; i < STGE_NTXDESC; i++) {
516 1.1 thorpej if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
517 1.1 thorpej STGE_NTXFRAGS, MCLBYTES, 0, 0,
518 1.1 thorpej &sc->sc_txsoft[i].ds_dmamap)) != 0) {
519 1.1 thorpej printf("%s: unable to create tx DMA map %d, "
520 1.1 thorpej "error = %d\n", sc->sc_dev.dv_xname, i, error);
521 1.1 thorpej goto fail_4;
522 1.1 thorpej }
523 1.1 thorpej }
524 1.1 thorpej
525 1.1 thorpej /*
526 1.1 thorpej * Create the receive buffer DMA maps.
527 1.1 thorpej */
528 1.1 thorpej for (i = 0; i < STGE_NRXDESC; i++) {
529 1.1 thorpej if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
530 1.1 thorpej MCLBYTES, 0, 0, &sc->sc_rxsoft[i].ds_dmamap)) != 0) {
531 1.1 thorpej printf("%s: unable to create rx DMA map %d, "
532 1.1 thorpej "error = %d\n", sc->sc_dev.dv_xname, i, error);
533 1.1 thorpej goto fail_5;
534 1.1 thorpej }
535 1.1 thorpej sc->sc_rxsoft[i].ds_mbuf = NULL;
536 1.1 thorpej }
537 1.1 thorpej
538 1.1 thorpej /*
539 1.1 thorpej * Determine if we're copper or fiber. It affects how we
540 1.1 thorpej * reset the card.
541 1.1 thorpej */
542 1.1 thorpej if (bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl) &
543 1.1 thorpej AC_PhyMedia)
544 1.1 thorpej sc->sc_usefiber = 1;
545 1.1 thorpej else
546 1.1 thorpej sc->sc_usefiber = 0;
547 1.1 thorpej
548 1.1 thorpej /*
549 1.1 thorpej * Reset the chip to a known state.
550 1.1 thorpej */
551 1.1 thorpej stge_reset(sc);
552 1.1 thorpej
553 1.1 thorpej /*
554 1.1 thorpej * Reading the station address from the EEPROM doesn't seem
555 1.1 thorpej * to work, at least on my sample boards. Instread, since
556 1.1 thorpej * the reset sequence does AutoInit, read it from the station
557 1.1 thorpej * address registers.
558 1.1 thorpej */
559 1.1 thorpej enaddr[0] = bus_space_read_2(sc->sc_st, sc->sc_sh,
560 1.1 thorpej STGE_StationAddress0) & 0xff;
561 1.1 thorpej enaddr[1] = bus_space_read_2(sc->sc_st, sc->sc_sh,
562 1.1 thorpej STGE_StationAddress0) >> 8;
563 1.1 thorpej enaddr[2] = bus_space_read_2(sc->sc_st, sc->sc_sh,
564 1.1 thorpej STGE_StationAddress1) & 0xff;
565 1.1 thorpej enaddr[3] = bus_space_read_2(sc->sc_st, sc->sc_sh,
566 1.1 thorpej STGE_StationAddress1) >> 8;
567 1.1 thorpej enaddr[4] = bus_space_read_2(sc->sc_st, sc->sc_sh,
568 1.1 thorpej STGE_StationAddress2) & 0xff;
569 1.1 thorpej enaddr[5] = bus_space_read_2(sc->sc_st, sc->sc_sh,
570 1.1 thorpej STGE_StationAddress2) >> 8;
571 1.1 thorpej
572 1.1 thorpej printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
573 1.1 thorpej ether_sprintf(enaddr));
574 1.1 thorpej
575 1.1 thorpej /*
576 1.1 thorpej * Read some important bits from the PhyCtrl register.
577 1.1 thorpej */
578 1.1 thorpej sc->sc_PhyCtrl = bus_space_read_1(sc->sc_st, sc->sc_sh,
579 1.1 thorpej STGE_PhyCtrl) & (PC_PhyDuplexPolarity | PC_PhyLnkPolarity);
580 1.1 thorpej
581 1.1 thorpej /*
582 1.1 thorpej * Initialize our media structures and probe the MII.
583 1.1 thorpej */
584 1.1 thorpej sc->sc_mii.mii_ifp = ifp;
585 1.1 thorpej sc->sc_mii.mii_readreg = stge_mii_readreg;
586 1.1 thorpej sc->sc_mii.mii_writereg = stge_mii_writereg;
587 1.1 thorpej sc->sc_mii.mii_statchg = stge_mii_statchg;
588 1.1 thorpej ifmedia_init(&sc->sc_mii.mii_media, 0, stge_mediachange,
589 1.1 thorpej stge_mediastatus);
590 1.1 thorpej mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
591 1.1 thorpej MII_OFFSET_ANY, 0);
592 1.1 thorpej if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
593 1.1 thorpej ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
594 1.1 thorpej ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
595 1.1 thorpej } else
596 1.1 thorpej ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
597 1.1 thorpej
598 1.1 thorpej ifp = &sc->sc_ethercom.ec_if;
599 1.1 thorpej strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
600 1.1 thorpej ifp->if_softc = sc;
601 1.1 thorpej ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
602 1.1 thorpej ifp->if_ioctl = stge_ioctl;
603 1.1 thorpej ifp->if_start = stge_start;
604 1.1 thorpej ifp->if_watchdog = stge_watchdog;
605 1.1 thorpej ifp->if_init = stge_init;
606 1.1 thorpej ifp->if_stop = stge_stop;
607 1.1 thorpej IFQ_SET_READY(&ifp->if_snd);
608 1.1 thorpej
609 1.1 thorpej /*
610 1.1 thorpej * The manual recommends disabling early transmit, so we
611 1.1 thorpej * do. It's disabled anyway, if using IP checksumming,
612 1.1 thorpej * since the entire packet must be in the FIFO in order
613 1.1 thorpej * for the chip to perform the checksum.
614 1.1 thorpej */
615 1.1 thorpej sc->sc_txthresh = 0x0fff;
616 1.1 thorpej
617 1.1 thorpej /*
618 1.1 thorpej * Disable MWI if the PCI layer tells us to.
619 1.1 thorpej */
620 1.1 thorpej sc->sc_DMACtrl = 0;
621 1.1 thorpej if ((pa->pa_flags & PCI_FLAGS_MWI_OKAY) == 0)
622 1.1 thorpej sc->sc_DMACtrl |= DMAC_MWIDisable;
623 1.1 thorpej
624 1.1 thorpej /*
625 1.1 thorpej * We can support 802.1Q VLAN-sized frames and jumbo
626 1.1 thorpej * Ethernet frames.
627 1.1 thorpej *
628 1.1 thorpej * XXX Figure out how to do hw-assisted VLAN tagging in
629 1.1 thorpej * XXX a reasonable way on this chip.
630 1.1 thorpej */
631 1.1 thorpej sc->sc_ethercom.ec_capabilities |=
632 1.1 thorpej ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
633 1.1 thorpej
634 1.1 thorpej /*
635 1.1 thorpej * We can do IPv4/TCPv4/UDPv4 checksums in hardware.
636 1.1 thorpej */
637 1.1 thorpej sc->sc_ethercom.ec_if.if_capabilities |= IFCAP_CSUM_IPv4 |
638 1.1 thorpej IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
639 1.1 thorpej
640 1.1 thorpej /*
641 1.1 thorpej * Attach the interface.
642 1.1 thorpej */
643 1.1 thorpej if_attach(ifp);
644 1.1 thorpej ether_ifattach(ifp, enaddr);
645 1.1 thorpej
646 1.1 thorpej #ifdef STGE_EVENT_COUNTERS
647 1.1 thorpej /*
648 1.1 thorpej * Attach event counters.
649 1.1 thorpej */
650 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
651 1.1 thorpej NULL, sc->sc_dev.dv_xname, "txstall");
652 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_txdmaintr, EVCNT_TYPE_INTR,
653 1.1 thorpej NULL, sc->sc_dev.dv_xname, "txdmaintr");
654 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_txindintr, EVCNT_TYPE_INTR,
655 1.1 thorpej NULL, sc->sc_dev.dv_xname, "txindintr");
656 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
657 1.1 thorpej NULL, sc->sc_dev.dv_xname, "rxintr");
658 1.1 thorpej
659 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_txseg1, EVCNT_TYPE_MISC,
660 1.1 thorpej NULL, sc->sc_dev.dv_xname, "txseg1");
661 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_txseg2, EVCNT_TYPE_MISC,
662 1.1 thorpej NULL, sc->sc_dev.dv_xname, "txseg2");
663 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_txseg3, EVCNT_TYPE_MISC,
664 1.1 thorpej NULL, sc->sc_dev.dv_xname, "txseg3");
665 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_txseg4, EVCNT_TYPE_MISC,
666 1.1 thorpej NULL, sc->sc_dev.dv_xname, "txseg4");
667 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_txseg5, EVCNT_TYPE_MISC,
668 1.1 thorpej NULL, sc->sc_dev.dv_xname, "txseg5");
669 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_txsegmore, EVCNT_TYPE_MISC,
670 1.1 thorpej NULL, sc->sc_dev.dv_xname, "txsegmore");
671 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_txcopy, EVCNT_TYPE_MISC,
672 1.1 thorpej NULL, sc->sc_dev.dv_xname, "txcopy");
673 1.1 thorpej
674 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_rxipsum, EVCNT_TYPE_MISC,
675 1.1 thorpej NULL, sc->sc_dev.dv_xname, "rxipsum");
676 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_rxtcpsum, EVCNT_TYPE_MISC,
677 1.1 thorpej NULL, sc->sc_dev.dv_xname, "rxtcpsum");
678 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_rxudpsum, EVCNT_TYPE_MISC,
679 1.1 thorpej NULL, sc->sc_dev.dv_xname, "rxudpsum");
680 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_txipsum, EVCNT_TYPE_MISC,
681 1.1 thorpej NULL, sc->sc_dev.dv_xname, "txipsum");
682 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_txtcpsum, EVCNT_TYPE_MISC,
683 1.1 thorpej NULL, sc->sc_dev.dv_xname, "txtcpsum");
684 1.1 thorpej evcnt_attach_dynamic(&sc->sc_ev_txudpsum, EVCNT_TYPE_MISC,
685 1.1 thorpej NULL, sc->sc_dev.dv_xname, "txudpsum");
686 1.1 thorpej #endif /* STGE_EVENT_COUNTERS */
687 1.1 thorpej
688 1.1 thorpej /*
689 1.1 thorpej * Make sure the interface is shutdown during reboot.
690 1.1 thorpej */
691 1.1 thorpej sc->sc_sdhook = shutdownhook_establish(stge_shutdown, sc);
692 1.1 thorpej if (sc->sc_sdhook == NULL)
693 1.1 thorpej printf("%s: WARNING: unable to establish shutdown hook\n",
694 1.1 thorpej sc->sc_dev.dv_xname);
695 1.1 thorpej return;
696 1.1 thorpej
697 1.1 thorpej /*
698 1.1 thorpej * Free any resources we've allocated during the failed attach
699 1.1 thorpej * attempt. Do this in reverse order and fall through.
700 1.1 thorpej */
701 1.1 thorpej fail_5:
702 1.1 thorpej for (i = 0; i < STGE_NRXDESC; i++) {
703 1.1 thorpej if (sc->sc_rxsoft[i].ds_dmamap != NULL)
704 1.1 thorpej bus_dmamap_destroy(sc->sc_dmat,
705 1.1 thorpej sc->sc_rxsoft[i].ds_dmamap);
706 1.1 thorpej }
707 1.1 thorpej fail_4:
708 1.1 thorpej for (i = 0; i < STGE_NTXDESC; i++) {
709 1.1 thorpej if (sc->sc_txsoft[i].ds_dmamap != NULL)
710 1.1 thorpej bus_dmamap_destroy(sc->sc_dmat,
711 1.1 thorpej sc->sc_txsoft[i].ds_dmamap);
712 1.1 thorpej }
713 1.1 thorpej bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
714 1.1 thorpej fail_3:
715 1.1 thorpej bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
716 1.1 thorpej fail_2:
717 1.1 thorpej bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
718 1.1 thorpej sizeof(struct stge_control_data));
719 1.1 thorpej fail_1:
720 1.1 thorpej bus_dmamem_free(sc->sc_dmat, &seg, rseg);
721 1.1 thorpej fail_0:
722 1.1 thorpej return;
723 1.1 thorpej }
724 1.1 thorpej
725 1.1 thorpej /*
726 1.1 thorpej * stge_shutdown:
727 1.1 thorpej *
728 1.1 thorpej * Make sure the interface is stopped at reboot time.
729 1.1 thorpej */
730 1.1 thorpej void
731 1.1 thorpej stge_shutdown(void *arg)
732 1.1 thorpej {
733 1.1 thorpej struct stge_softc *sc = arg;
734 1.1 thorpej
735 1.1 thorpej stge_stop(&sc->sc_ethercom.ec_if, 1);
736 1.1 thorpej }
737 1.1 thorpej
738 1.1 thorpej static void
739 1.1 thorpej stge_dma_wait(struct stge_softc *sc)
740 1.1 thorpej {
741 1.1 thorpej int i;
742 1.1 thorpej
743 1.1 thorpej for (i = 0; i < STGE_TIMEOUT; i++) {
744 1.1 thorpej delay(2);
745 1.1 thorpej if ((bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_DMACtrl) &
746 1.1 thorpej DMAC_TxDMAInProg) == 0)
747 1.1 thorpej break;
748 1.1 thorpej }
749 1.1 thorpej
750 1.1 thorpej if (i == STGE_TIMEOUT)
751 1.1 thorpej printf("%s: DMA wait timed out\n", sc->sc_dev.dv_xname);
752 1.1 thorpej }
753 1.1 thorpej
754 1.1 thorpej /*
755 1.1 thorpej * stge_start: [ifnet interface function]
756 1.1 thorpej *
757 1.1 thorpej * Start packet transmission on the interface.
758 1.1 thorpej */
759 1.1 thorpej void
760 1.1 thorpej stge_start(struct ifnet *ifp)
761 1.1 thorpej {
762 1.1 thorpej struct stge_softc *sc = ifp->if_softc;
763 1.1 thorpej struct mbuf *m0;
764 1.1 thorpej struct stge_descsoft *ds;
765 1.1 thorpej struct stge_tfd *tfd;
766 1.1 thorpej bus_dmamap_t dmamap;
767 1.1 thorpej int error, firsttx, nexttx, opending, seg, totlen;
768 1.1 thorpej uint64_t csum_flags;
769 1.1 thorpej
770 1.1 thorpej if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
771 1.1 thorpej return;
772 1.1 thorpej
773 1.1 thorpej /*
774 1.1 thorpej * Remember the previous number of pending transmissions
775 1.1 thorpej * and the first descriptor we will use.
776 1.1 thorpej */
777 1.1 thorpej opending = sc->sc_txpending;
778 1.1 thorpej firsttx = STGE_NEXTTX(sc->sc_txlast);
779 1.1 thorpej
780 1.1 thorpej /*
781 1.1 thorpej * Loop through the send queue, setting up transmit descriptors
782 1.1 thorpej * until we drain the queue, or use up all available transmit
783 1.1 thorpej * descriptors.
784 1.1 thorpej */
785 1.1 thorpej for (;;) {
786 1.1 thorpej /*
787 1.1 thorpej * Grab a packet off the queue.
788 1.1 thorpej */
789 1.1 thorpej IFQ_POLL(&ifp->if_snd, m0);
790 1.1 thorpej if (m0 == NULL)
791 1.1 thorpej break;
792 1.1 thorpej
793 1.1 thorpej /*
794 1.1 thorpej * Leave one unused descriptor at the end of the
795 1.1 thorpej * list to prevent wrapping completely around.
796 1.1 thorpej */
797 1.1 thorpej if (sc->sc_txpending == (STGE_NTXDESC - 1)) {
798 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_txstall);
799 1.1 thorpej break;
800 1.1 thorpej }
801 1.1 thorpej
802 1.1 thorpej /*
803 1.1 thorpej * Get the last and next available transmit descriptor.
804 1.1 thorpej */
805 1.1 thorpej nexttx = STGE_NEXTTX(sc->sc_txlast);
806 1.1 thorpej tfd = &sc->sc_txdescs[nexttx];
807 1.1 thorpej ds = &sc->sc_txsoft[nexttx];
808 1.1 thorpej
809 1.1 thorpej dmamap = ds->ds_dmamap;
810 1.1 thorpej
811 1.1 thorpej /*
812 1.1 thorpej * Load the DMA map. If this fails, the packet either
813 1.1 thorpej * didn't fit in the alloted number of segments, or we
814 1.1 thorpej * were short on resources. For the too-may-segments
815 1.1 thorpej * case, we simply report an error and drop the packet,
816 1.1 thorpej * since we can't sanely copy a jumbo packet to a single
817 1.1 thorpej * buffer.
818 1.1 thorpej */
819 1.1 thorpej error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
820 1.1 thorpej BUS_DMA_NOWAIT);
821 1.1 thorpej if (error) {
822 1.1 thorpej if (error == EFBIG) {
823 1.1 thorpej printf("%s: Tx packet consumes too many "
824 1.1 thorpej "DMA segments, dropping...\n",
825 1.1 thorpej sc->sc_dev.dv_xname);
826 1.1 thorpej IFQ_DEQUEUE(&ifp->if_snd, m0);
827 1.1 thorpej m_freem(m0);
828 1.1 thorpej continue;
829 1.1 thorpej }
830 1.1 thorpej /*
831 1.1 thorpej * Short on resources, just stop for now.
832 1.1 thorpej */
833 1.1 thorpej break;
834 1.1 thorpej }
835 1.1 thorpej
836 1.1 thorpej IFQ_DEQUEUE(&ifp->if_snd, m0);
837 1.1 thorpej
838 1.1 thorpej /*
839 1.1 thorpej * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
840 1.1 thorpej */
841 1.1 thorpej
842 1.1 thorpej /* Sync the DMA map. */
843 1.1 thorpej bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
844 1.1 thorpej BUS_DMASYNC_PREWRITE);
845 1.1 thorpej
846 1.1 thorpej /* Initialize the fragment list. */
847 1.1 thorpej for (totlen = 0, seg = 0; seg < dmamap->dm_nsegs; seg++) {
848 1.1 thorpej tfd->tfd_frags[seg].frag_word0 =
849 1.1 thorpej htole64(FRAG_ADDR(dmamap->dm_segs[seg].ds_addr) |
850 1.1 thorpej FRAG_LEN(dmamap->dm_segs[seg].ds_len));
851 1.1 thorpej totlen += dmamap->dm_segs[seg].ds_len;
852 1.1 thorpej }
853 1.1 thorpej
854 1.1 thorpej #ifdef STGE_EVENT_COUNTERS
855 1.1 thorpej switch (dmamap->dm_nsegs) {
856 1.1 thorpej case 1:
857 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_txseg1);
858 1.1 thorpej break;
859 1.1 thorpej case 2:
860 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_txseg2);
861 1.1 thorpej break;
862 1.1 thorpej case 3:
863 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_txseg3);
864 1.1 thorpej break;
865 1.1 thorpej case 4:
866 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_txseg4);
867 1.1 thorpej break;
868 1.1 thorpej case 5:
869 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_txseg5);
870 1.1 thorpej break;
871 1.1 thorpej default:
872 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_txsegmore);
873 1.1 thorpej break;
874 1.1 thorpej }
875 1.1 thorpej #endif /* STGE_EVENT_COUNTERS */
876 1.1 thorpej
877 1.1 thorpej /*
878 1.1 thorpej * Initialize checksumming flags in the descriptor.
879 1.1 thorpej * Byte-swap constants so the compiler can optimize.
880 1.1 thorpej */
881 1.1 thorpej csum_flags = 0;
882 1.1 thorpej if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) {
883 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_txipsum);
884 1.1 thorpej csum_flags |= htole64(TFD_IPChecksumEnable);
885 1.1 thorpej }
886 1.1 thorpej
887 1.1 thorpej if (m0->m_pkthdr.csum_flags & M_CSUM_TCPv4) {
888 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_txtcpsum);
889 1.1 thorpej csum_flags |= htole64(TFD_TCPChecksumEnable);
890 1.1 thorpej }
891 1.1 thorpej else if (m0->m_pkthdr.csum_flags & M_CSUM_UDPv4) {
892 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_txudpsum);
893 1.1 thorpej csum_flags |= htole64(TFD_UDPChecksumEnable);
894 1.1 thorpej }
895 1.1 thorpej
896 1.1 thorpej /*
897 1.1 thorpej * Initialize the descriptor and give it to the chip.
898 1.1 thorpej */
899 1.1 thorpej tfd->tfd_control = htole64(TFD_FrameId(nexttx) |
900 1.1 thorpej TFD_WordAlign(/*totlen & */3) |
901 1.1 thorpej TFD_FragCount(seg) | csum_flags |
902 1.1 thorpej (((nexttx & STGE_TXINTR_SPACING_MASK) == 0) ?
903 1.1 thorpej TFD_TxDMAIndicate : 0));
904 1.1 thorpej
905 1.1 thorpej /* Sync the descriptor. */
906 1.1 thorpej STGE_CDTXSYNC(sc, nexttx,
907 1.1 thorpej BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
908 1.1 thorpej
909 1.1 thorpej /*
910 1.1 thorpej * Kick the transmit DMA logic.
911 1.1 thorpej */
912 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_DMACtrl,
913 1.1 thorpej sc->sc_DMACtrl | DMAC_TxDMAPollNow);
914 1.1 thorpej
915 1.1 thorpej /*
916 1.1 thorpej * Store a pointer to the packet so we can free it later.
917 1.1 thorpej */
918 1.1 thorpej ds->ds_mbuf = m0;
919 1.1 thorpej
920 1.1 thorpej /* Advance the tx pointer. */
921 1.1 thorpej sc->sc_txpending++;
922 1.1 thorpej sc->sc_txlast = nexttx;
923 1.1 thorpej
924 1.1 thorpej #if NBPFILTER > 0
925 1.1 thorpej /*
926 1.1 thorpej * Pass the packet to any BPF listeners.
927 1.1 thorpej */
928 1.1 thorpej if (ifp->if_bpf)
929 1.1 thorpej bpf_mtap(ifp->if_bpf, m0);
930 1.1 thorpej #endif /* NBPFILTER > 0 */
931 1.1 thorpej }
932 1.1 thorpej
933 1.1 thorpej if (sc->sc_txpending == (STGE_NTXDESC - 1)) {
934 1.1 thorpej /* No more slots left; notify upper layer. */
935 1.1 thorpej ifp->if_flags |= IFF_OACTIVE;
936 1.1 thorpej }
937 1.1 thorpej
938 1.1 thorpej if (sc->sc_txpending != opending) {
939 1.1 thorpej /*
940 1.1 thorpej * We enqueued packets. If the transmitter was idle,
941 1.1 thorpej * reset the txdirty pointer.
942 1.1 thorpej */
943 1.1 thorpej if (opending == 0)
944 1.1 thorpej sc->sc_txdirty = firsttx;
945 1.1 thorpej
946 1.1 thorpej /* Set a watchdog timer in case the chip flakes out. */
947 1.1 thorpej ifp->if_timer = 5;
948 1.1 thorpej }
949 1.1 thorpej }
950 1.1 thorpej
951 1.1 thorpej /*
952 1.1 thorpej * stge_watchdog: [ifnet interface function]
953 1.1 thorpej *
954 1.1 thorpej * Watchdog timer handler.
955 1.1 thorpej */
956 1.1 thorpej void
957 1.1 thorpej stge_watchdog(struct ifnet *ifp)
958 1.1 thorpej {
959 1.1 thorpej struct stge_softc *sc = ifp->if_softc;
960 1.1 thorpej
961 1.1 thorpej /*
962 1.1 thorpej * Sweep up first, since we don't interrupt every frame.
963 1.1 thorpej */
964 1.1 thorpej stge_txintr(sc);
965 1.1 thorpej if (sc->sc_txpending != 0) {
966 1.1 thorpej printf("%s: device timeout\n", sc->sc_dev.dv_xname);
967 1.1 thorpej ifp->if_oerrors++;
968 1.1 thorpej
969 1.1 thorpej (void) stge_init(ifp);
970 1.1 thorpej
971 1.1 thorpej /* Try to get more packets going. */
972 1.1 thorpej stge_start(ifp);
973 1.1 thorpej }
974 1.1 thorpej }
975 1.1 thorpej
976 1.1 thorpej /*
977 1.1 thorpej * stge_ioctl: [ifnet interface function]
978 1.1 thorpej *
979 1.1 thorpej * Handle control requests from the operator.
980 1.1 thorpej */
981 1.1 thorpej int
982 1.1 thorpej stge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
983 1.1 thorpej {
984 1.1 thorpej struct stge_softc *sc = ifp->if_softc;
985 1.1 thorpej struct ifreq *ifr = (struct ifreq *)data;
986 1.1 thorpej int s, error;
987 1.1 thorpej
988 1.1 thorpej s = splnet();
989 1.1 thorpej
990 1.1 thorpej switch (cmd) {
991 1.1 thorpej case SIOCSIFMEDIA:
992 1.1 thorpej case SIOCGIFMEDIA:
993 1.1 thorpej error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
994 1.1 thorpej break;
995 1.1 thorpej
996 1.1 thorpej default:
997 1.1 thorpej error = ether_ioctl(ifp, cmd, data);
998 1.1 thorpej if (error == ENETRESET) {
999 1.1 thorpej /*
1000 1.1 thorpej * Multicast list has changed; set the hardware filter
1001 1.1 thorpej * accordingly.
1002 1.1 thorpej */
1003 1.1 thorpej stge_set_filter(sc);
1004 1.1 thorpej error = 0;
1005 1.1 thorpej }
1006 1.1 thorpej break;
1007 1.1 thorpej }
1008 1.1 thorpej
1009 1.1 thorpej /* Try to get more packets going. */
1010 1.1 thorpej stge_start(ifp);
1011 1.1 thorpej
1012 1.1 thorpej splx(s);
1013 1.1 thorpej return (error);
1014 1.1 thorpej }
1015 1.1 thorpej
1016 1.1 thorpej /*
1017 1.1 thorpej * stge_intr:
1018 1.1 thorpej *
1019 1.1 thorpej * Interrupt service routine.
1020 1.1 thorpej */
1021 1.1 thorpej int
1022 1.1 thorpej stge_intr(void *arg)
1023 1.1 thorpej {
1024 1.1 thorpej struct stge_softc *sc = arg;
1025 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1026 1.1 thorpej uint32_t txstat;
1027 1.1 thorpej int wantinit;
1028 1.1 thorpej uint16_t isr;
1029 1.1 thorpej
1030 1.1 thorpej if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_IntStatus) &
1031 1.1 thorpej IS_InterruptStatus) == 0)
1032 1.1 thorpej return (0);
1033 1.1 thorpej
1034 1.1 thorpej for (wantinit = 0; wantinit == 0;) {
1035 1.1 thorpej isr = bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_IntStatusAck);
1036 1.1 thorpej if ((isr & sc->sc_IntEnable) == 0)
1037 1.1 thorpej break;
1038 1.1 thorpej
1039 1.1 thorpej /* Receive interrupts. */
1040 1.1 thorpej if (isr & (IE_RxDMAComplete|IE_RFDListEnd)) {
1041 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_rxintr);
1042 1.1 thorpej stge_rxintr(sc);
1043 1.1 thorpej if (isr & IE_RFDListEnd) {
1044 1.1 thorpej printf("%s: receive ring overflow\n",
1045 1.1 thorpej sc->sc_dev.dv_xname);
1046 1.1 thorpej /*
1047 1.1 thorpej * XXX Should try to recover from this
1048 1.1 thorpej * XXX more gracefully.
1049 1.1 thorpej */
1050 1.1 thorpej wantinit = 1;
1051 1.1 thorpej }
1052 1.1 thorpej }
1053 1.1 thorpej
1054 1.1 thorpej /* Transmit interrupts. */
1055 1.1 thorpej if (isr & (IE_TxDMAComplete|IE_TxComplete)) {
1056 1.1 thorpej #ifdef STGE_EVENT_COUNTERS
1057 1.1 thorpej if (isr & IE_TxDMAComplete)
1058 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_txdmaintr);
1059 1.1 thorpej #endif
1060 1.1 thorpej stge_txintr(sc);
1061 1.1 thorpej }
1062 1.1 thorpej
1063 1.1 thorpej /* Statistics overflow. */
1064 1.1 thorpej if (isr & IE_UpdateStats)
1065 1.1 thorpej stge_stats_update(sc);
1066 1.1 thorpej
1067 1.1 thorpej /* Transmission errors. */
1068 1.1 thorpej if (isr & IE_TxComplete) {
1069 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_txindintr);
1070 1.1 thorpej for (;;) {
1071 1.1 thorpej txstat = bus_space_read_4(sc->sc_st, sc->sc_sh,
1072 1.1 thorpej STGE_TxStatus);
1073 1.1 thorpej if ((txstat & TS_TxComplete) == 0)
1074 1.1 thorpej break;
1075 1.1 thorpej if (txstat & TS_TxUnderrun) {
1076 1.1 thorpej sc->sc_txthresh++;
1077 1.1 thorpej if (sc->sc_txthresh > 0x0fff)
1078 1.1 thorpej sc->sc_txthresh = 0x0fff;
1079 1.1 thorpej printf("%s: transmit underrun, new "
1080 1.1 thorpej "threshold: %d bytes\n",
1081 1.1 thorpej sc->sc_dev.dv_xname,
1082 1.1 thorpej sc->sc_txthresh << 5);
1083 1.1 thorpej }
1084 1.1 thorpej if (txstat & TS_MaxCollisions)
1085 1.1 thorpej printf("%s: excessive collisions\n",
1086 1.1 thorpej sc->sc_dev.dv_xname);
1087 1.1 thorpej }
1088 1.1 thorpej wantinit = 1;
1089 1.1 thorpej }
1090 1.1 thorpej
1091 1.1 thorpej /* Host interface errors. */
1092 1.1 thorpej if (isr & IE_HostError) {
1093 1.1 thorpej printf("%s: Host interface error\n",
1094 1.1 thorpej sc->sc_dev.dv_xname);
1095 1.1 thorpej wantinit = 1;
1096 1.1 thorpej }
1097 1.1 thorpej }
1098 1.1 thorpej
1099 1.1 thorpej if (wantinit)
1100 1.1 thorpej stge_init(ifp);
1101 1.1 thorpej
1102 1.1 thorpej bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_IntEnable,
1103 1.1 thorpej sc->sc_IntEnable);
1104 1.1 thorpej
1105 1.1 thorpej /* Try to get more packets going. */
1106 1.1 thorpej stge_start(ifp);
1107 1.1 thorpej
1108 1.1 thorpej return (1);
1109 1.1 thorpej }
1110 1.1 thorpej
1111 1.1 thorpej /*
1112 1.1 thorpej * stge_txintr:
1113 1.1 thorpej *
1114 1.1 thorpej * Helper; handle transmit interrupts.
1115 1.1 thorpej */
1116 1.1 thorpej void
1117 1.1 thorpej stge_txintr(struct stge_softc *sc)
1118 1.1 thorpej {
1119 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1120 1.1 thorpej struct stge_descsoft *ds;
1121 1.1 thorpej uint64_t control;
1122 1.1 thorpej int i;
1123 1.1 thorpej
1124 1.1 thorpej ifp->if_flags &= ~IFF_OACTIVE;
1125 1.1 thorpej
1126 1.1 thorpej /*
1127 1.1 thorpej * Go through our Tx list and free mbufs for those
1128 1.1 thorpej * frames which have been transmitted.
1129 1.1 thorpej */
1130 1.1 thorpej for (i = sc->sc_txdirty; sc->sc_txpending != 0;
1131 1.1 thorpej i = STGE_NEXTTX(i), sc->sc_txpending--) {
1132 1.1 thorpej ds = &sc->sc_txsoft[i];
1133 1.1 thorpej
1134 1.1 thorpej STGE_CDTXSYNC(sc, i,
1135 1.1 thorpej BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1136 1.1 thorpej
1137 1.1 thorpej control = le64toh(sc->sc_txdescs[i].tfd_control);
1138 1.1 thorpej if ((control & TFD_TFDDone) == 0)
1139 1.1 thorpej break;
1140 1.1 thorpej
1141 1.1 thorpej bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap,
1142 1.1 thorpej 0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1143 1.1 thorpej bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1144 1.1 thorpej m_freem(ds->ds_mbuf);
1145 1.1 thorpej ds->ds_mbuf = NULL;
1146 1.1 thorpej }
1147 1.1 thorpej
1148 1.1 thorpej /* Update the dirty transmit buffer pointer. */
1149 1.1 thorpej sc->sc_txdirty = i;
1150 1.1 thorpej
1151 1.1 thorpej /*
1152 1.1 thorpej * If there are no more pending transmissions, cancel the watchdog
1153 1.1 thorpej * timer.
1154 1.1 thorpej */
1155 1.1 thorpej if (sc->sc_txpending == 0)
1156 1.1 thorpej ifp->if_timer = 0;
1157 1.1 thorpej }
1158 1.1 thorpej
1159 1.1 thorpej /*
1160 1.1 thorpej * stge_rxintr:
1161 1.1 thorpej *
1162 1.1 thorpej * Helper; handle receive interrupts.
1163 1.1 thorpej */
1164 1.1 thorpej void
1165 1.1 thorpej stge_rxintr(struct stge_softc *sc)
1166 1.1 thorpej {
1167 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1168 1.1 thorpej struct stge_descsoft *ds;
1169 1.1 thorpej struct mbuf *m, *tailm;
1170 1.1 thorpej uint64_t status;
1171 1.1 thorpej int i, len;
1172 1.1 thorpej
1173 1.1 thorpej for (i = sc->sc_rxptr;; i = STGE_NEXTRX(i)) {
1174 1.1 thorpej ds = &sc->sc_rxsoft[i];
1175 1.1 thorpej
1176 1.1 thorpej STGE_CDRXSYNC(sc, i,
1177 1.1 thorpej BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1178 1.1 thorpej
1179 1.1 thorpej status = le64toh(sc->sc_rxdescs[i].rfd_status);
1180 1.1 thorpej
1181 1.1 thorpej if ((status & RFD_RFDDone) == 0)
1182 1.1 thorpej break;
1183 1.1 thorpej
1184 1.1 thorpej if (__predict_false(sc->sc_rxdiscard)) {
1185 1.1 thorpej STGE_INIT_RXDESC(sc, i);
1186 1.1 thorpej if (status & RFD_FrameEnd) {
1187 1.1 thorpej /* Reset our state. */
1188 1.1 thorpej sc->sc_rxdiscard = 0;
1189 1.1 thorpej }
1190 1.1 thorpej continue;
1191 1.1 thorpej }
1192 1.1 thorpej
1193 1.1 thorpej bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1194 1.1 thorpej ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1195 1.1 thorpej
1196 1.1 thorpej m = ds->ds_mbuf;
1197 1.1 thorpej
1198 1.1 thorpej /*
1199 1.1 thorpej * Add a new receive buffer to the ring.
1200 1.1 thorpej */
1201 1.1 thorpej if (stge_add_rxbuf(sc, i) != 0) {
1202 1.1 thorpej /*
1203 1.1 thorpej * Failed, throw away what we've done so
1204 1.1 thorpej * far, and discard the rest of the packet.
1205 1.1 thorpej */
1206 1.1 thorpej ifp->if_ierrors++;
1207 1.1 thorpej bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1208 1.1 thorpej ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1209 1.1 thorpej STGE_INIT_RXDESC(sc, i);
1210 1.1 thorpej if ((status & RFD_FrameEnd) == 0)
1211 1.1 thorpej sc->sc_rxdiscard = 1;
1212 1.1 thorpej if (sc->sc_rxhead != NULL)
1213 1.1 thorpej m_freem(sc->sc_rxhead);
1214 1.1 thorpej STGE_RXCHAIN_RESET(sc);
1215 1.1 thorpej continue;
1216 1.1 thorpej }
1217 1.1 thorpej
1218 1.1 thorpej #ifdef DIAGNOSTIC
1219 1.1 thorpej if (status & RFD_FrameStart) {
1220 1.1 thorpej KASSERT(sc->sc_rxhead == NULL);
1221 1.1 thorpej KASSERT(sc->sc_rxtailp == &sc->sc_rxhead);
1222 1.1 thorpej }
1223 1.1 thorpej #endif
1224 1.1 thorpej
1225 1.1 thorpej STGE_RXCHAIN_LINK(sc, m);
1226 1.1 thorpej
1227 1.1 thorpej /*
1228 1.1 thorpej * If this is not the end of the packet, keep
1229 1.1 thorpej * looking.
1230 1.1 thorpej */
1231 1.1 thorpej if ((status & RFD_FrameEnd) == 0) {
1232 1.1 thorpej sc->sc_rxlen += m->m_len;
1233 1.1 thorpej continue;
1234 1.1 thorpej }
1235 1.1 thorpej
1236 1.1 thorpej /*
1237 1.1 thorpej * Okay, we have the entire packet now...
1238 1.1 thorpej */
1239 1.1 thorpej *sc->sc_rxtailp = NULL;
1240 1.1 thorpej m = sc->sc_rxhead;
1241 1.1 thorpej tailm = sc->sc_rxtail;
1242 1.1 thorpej
1243 1.1 thorpej STGE_RXCHAIN_RESET(sc);
1244 1.1 thorpej
1245 1.1 thorpej /*
1246 1.1 thorpej * If the packet had an error, drop it. Note we
1247 1.1 thorpej * count the error later in the periodic stats update.
1248 1.1 thorpej */
1249 1.1 thorpej if (status & (RFD_RxFIFOOverrun | RFD_RxRuntFrame |
1250 1.1 thorpej RFD_RxAlignmentError | RFD_RxFCSError |
1251 1.1 thorpej RFD_RxLengthError)) {
1252 1.1 thorpej m_freem(m);
1253 1.1 thorpej continue;
1254 1.1 thorpej }
1255 1.1 thorpej
1256 1.1 thorpej /*
1257 1.1 thorpej * No errors.
1258 1.1 thorpej *
1259 1.1 thorpej * Note we have configured the chip to not include
1260 1.1 thorpej * the CRC at the end of the packet.
1261 1.1 thorpej */
1262 1.1 thorpej len = RFD_RxDMAFrameLen(status);
1263 1.1 thorpej tailm->m_len = len - sc->sc_rxlen;
1264 1.1 thorpej
1265 1.1 thorpej /*
1266 1.1 thorpej * If the packet is small enough to fit in a
1267 1.1 thorpej * single header mbuf, allocate one and copy
1268 1.1 thorpej * the data into it. This greatly reduces
1269 1.1 thorpej * memory consumption when we receive lots
1270 1.1 thorpej * of small packets.
1271 1.1 thorpej */
1272 1.1 thorpej if (stge_copy_small != 0 && len <= (MHLEN - 2)) {
1273 1.1 thorpej struct mbuf *nm;
1274 1.1 thorpej MGETHDR(nm, M_DONTWAIT, MT_DATA);
1275 1.1 thorpej if (nm == NULL) {
1276 1.1 thorpej ifp->if_ierrors++;
1277 1.1 thorpej m_freem(m);
1278 1.1 thorpej continue;
1279 1.1 thorpej }
1280 1.1 thorpej nm->m_data += 2;
1281 1.1 thorpej nm->m_pkthdr.len = nm->m_len = len;
1282 1.1 thorpej m_copydata(m, 0, len, mtod(nm, caddr_t));
1283 1.1 thorpej m_freem(m);
1284 1.1 thorpej m = nm;
1285 1.1 thorpej }
1286 1.1 thorpej
1287 1.1 thorpej /*
1288 1.1 thorpej * Set the incoming checksum information for the packet.
1289 1.1 thorpej */
1290 1.1 thorpej if (status & RFD_IPDetected) {
1291 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_rxipsum);
1292 1.1 thorpej m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1293 1.1 thorpej if (status & RFD_IPError)
1294 1.1 thorpej m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1295 1.1 thorpej if (status & RFD_TCPDetected) {
1296 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_rxtcpsum);
1297 1.1 thorpej m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
1298 1.1 thorpej if (status & RFD_TCPError)
1299 1.1 thorpej m->m_pkthdr.csum_flags |=
1300 1.1 thorpej M_CSUM_TCP_UDP_BAD;
1301 1.1 thorpej } else if (status & RFD_UDPDetected) {
1302 1.1 thorpej STGE_EVCNT_INCR(&sc->sc_ev_rxudpsum);
1303 1.1 thorpej m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
1304 1.1 thorpej if (status & RFD_UDPError)
1305 1.1 thorpej m->m_pkthdr.csum_flags |=
1306 1.1 thorpej M_CSUM_TCP_UDP_BAD;
1307 1.1 thorpej }
1308 1.1 thorpej }
1309 1.1 thorpej
1310 1.1 thorpej m->m_pkthdr.rcvif = ifp;
1311 1.1 thorpej m->m_pkthdr.len = len;
1312 1.1 thorpej
1313 1.1 thorpej #if NBPFILTER > 0
1314 1.1 thorpej /*
1315 1.1 thorpej * Pass this up to any BPF listeners, but only
1316 1.1 thorpej * pass if up the stack if it's for us.
1317 1.1 thorpej */
1318 1.1 thorpej if (ifp->if_bpf)
1319 1.1 thorpej bpf_mtap(ifp->if_bpf, m);
1320 1.1 thorpej #endif /* NBPFILTER > 0 */
1321 1.1 thorpej
1322 1.1 thorpej /* Pass it on. */
1323 1.1 thorpej (*ifp->if_input)(ifp, m);
1324 1.1 thorpej }
1325 1.1 thorpej
1326 1.1 thorpej /* Update the receive pointer. */
1327 1.1 thorpej sc->sc_rxptr = i;
1328 1.1 thorpej }
1329 1.1 thorpej
1330 1.1 thorpej /*
1331 1.1 thorpej * stge_tick:
1332 1.1 thorpej *
1333 1.1 thorpej * One second timer, used to tick the MII.
1334 1.1 thorpej */
1335 1.1 thorpej void
1336 1.1 thorpej stge_tick(void *arg)
1337 1.1 thorpej {
1338 1.1 thorpej struct stge_softc *sc = arg;
1339 1.1 thorpej int s;
1340 1.1 thorpej
1341 1.1 thorpej s = splnet();
1342 1.1 thorpej mii_tick(&sc->sc_mii);
1343 1.1 thorpej stge_stats_update(sc);
1344 1.1 thorpej splx(s);
1345 1.1 thorpej
1346 1.1 thorpej callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
1347 1.1 thorpej }
1348 1.1 thorpej
1349 1.1 thorpej /*
1350 1.1 thorpej * stge_stats_update:
1351 1.1 thorpej *
1352 1.1 thorpej * Read the TC9021 statistics counters.
1353 1.1 thorpej */
1354 1.1 thorpej void
1355 1.1 thorpej stge_stats_update(struct stge_softc *sc)
1356 1.1 thorpej {
1357 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1358 1.1 thorpej bus_space_tag_t st = sc->sc_st;
1359 1.1 thorpej bus_space_handle_t sh = sc->sc_sh;
1360 1.1 thorpej
1361 1.1 thorpej (void) bus_space_read_4(st, sh, STGE_OctetRcvOk);
1362 1.1 thorpej
1363 1.1 thorpej ifp->if_ipackets +=
1364 1.1 thorpej bus_space_read_4(st, sh, STGE_FramesRcvdOk);
1365 1.1 thorpej
1366 1.1 thorpej ifp->if_ierrors +=
1367 1.1 thorpej (u_int) bus_space_read_2(st, sh, STGE_FramesLostRxErrors);
1368 1.1 thorpej
1369 1.1 thorpej (void) bus_space_read_4(st, sh, STGE_OctetXmtdOk);
1370 1.1 thorpej
1371 1.1 thorpej ifp->if_opackets +=
1372 1.1 thorpej bus_space_read_4(st, sh, STGE_FramesXmtdOk);
1373 1.1 thorpej
1374 1.1 thorpej ifp->if_collisions +=
1375 1.1 thorpej bus_space_read_4(st, sh, STGE_LateCollisions) +
1376 1.1 thorpej bus_space_read_4(st, sh, STGE_MultiColFrames) +
1377 1.1 thorpej bus_space_read_4(st, sh, STGE_SingleColFrames);
1378 1.1 thorpej
1379 1.1 thorpej ifp->if_oerrors +=
1380 1.1 thorpej (u_int) bus_space_read_2(st, sh, STGE_FramesAbortXSColls) +
1381 1.1 thorpej (u_int) bus_space_read_2(st, sh, STGE_FramesWEXDeferal);
1382 1.1 thorpej }
1383 1.1 thorpej
1384 1.1 thorpej /*
1385 1.1 thorpej * stge_reset:
1386 1.1 thorpej *
1387 1.1 thorpej * Perform a soft reset on the TC9021.
1388 1.1 thorpej */
1389 1.1 thorpej void
1390 1.1 thorpej stge_reset(struct stge_softc *sc)
1391 1.1 thorpej {
1392 1.1 thorpej uint32_t ac;
1393 1.1 thorpej int i;
1394 1.1 thorpej
1395 1.1 thorpej ac = bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl);
1396 1.1 thorpej
1397 1.1 thorpej /*
1398 1.1 thorpej * Only assert RstOut if we're fiber. We need GMII clocks
1399 1.1 thorpej * to be present in order for the reset to complete on fiber
1400 1.1 thorpej * cards.
1401 1.1 thorpej */
1402 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl,
1403 1.1 thorpej ac | AC_GlobalReset | AC_RxReset | AC_TxReset |
1404 1.1 thorpej AC_DMA | AC_FIFO | AC_Network | AC_Host | AC_AutoInit |
1405 1.1 thorpej (sc->sc_usefiber ? AC_RstOut : 0));
1406 1.1 thorpej
1407 1.1 thorpej delay(50000);
1408 1.1 thorpej
1409 1.1 thorpej for (i = 0; i < STGE_TIMEOUT; i++) {
1410 1.1 thorpej delay(5000);
1411 1.1 thorpej if ((bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl) &
1412 1.1 thorpej AC_ResetBusy) == 0)
1413 1.1 thorpej break;
1414 1.1 thorpej }
1415 1.1 thorpej
1416 1.1 thorpej if (i == STGE_TIMEOUT)
1417 1.1 thorpej printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
1418 1.1 thorpej
1419 1.1 thorpej delay(1000);
1420 1.1 thorpej }
1421 1.1 thorpej
1422 1.1 thorpej /*
1423 1.1 thorpej * stge_init: [ ifnet interface function ]
1424 1.1 thorpej *
1425 1.1 thorpej * Initialize the interface. Must be called at splnet().
1426 1.1 thorpej */
1427 1.1 thorpej int
1428 1.1 thorpej stge_init(struct ifnet *ifp)
1429 1.1 thorpej {
1430 1.1 thorpej struct stge_softc *sc = ifp->if_softc;
1431 1.1 thorpej bus_space_tag_t st = sc->sc_st;
1432 1.1 thorpej bus_space_handle_t sh = sc->sc_sh;
1433 1.1 thorpej struct stge_descsoft *ds;
1434 1.1 thorpej int i, error = 0;
1435 1.1 thorpej
1436 1.1 thorpej /*
1437 1.1 thorpej * Cancel any pending I/O.
1438 1.1 thorpej */
1439 1.1 thorpej stge_stop(ifp, 0);
1440 1.1 thorpej
1441 1.1 thorpej /*
1442 1.1 thorpej * Reset the chip to a known state.
1443 1.1 thorpej */
1444 1.1 thorpej stge_reset(sc);
1445 1.1 thorpej
1446 1.1 thorpej /*
1447 1.1 thorpej * Initialize the transmit descriptor ring.
1448 1.1 thorpej */
1449 1.1 thorpej memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1450 1.1 thorpej for (i = 0; i < STGE_NTXDESC; i++) {
1451 1.1 thorpej sc->sc_txdescs[i].tfd_next =
1452 1.1 thorpej (uint64_t) STGE_CDTXADDR(sc, STGE_NEXTTX(i));
1453 1.1 thorpej sc->sc_txdescs[i].tfd_control = htole64(TFD_TFDDone);
1454 1.1 thorpej }
1455 1.1 thorpej sc->sc_txpending = 0;
1456 1.1 thorpej sc->sc_txdirty = 0;
1457 1.1 thorpej sc->sc_txlast = STGE_NTXDESC - 1;
1458 1.1 thorpej
1459 1.1 thorpej /*
1460 1.1 thorpej * Initialize the receive descriptor and receive job
1461 1.1 thorpej * descriptor rings.
1462 1.1 thorpej */
1463 1.1 thorpej for (i = 0; i < STGE_NRXDESC; i++) {
1464 1.1 thorpej ds = &sc->sc_rxsoft[i];
1465 1.1 thorpej if (ds->ds_mbuf == NULL) {
1466 1.1 thorpej if ((error = stge_add_rxbuf(sc, i)) != 0) {
1467 1.1 thorpej printf("%s: unable to allocate or map rx "
1468 1.1 thorpej "buffer %d, error = %d\n",
1469 1.1 thorpej sc->sc_dev.dv_xname, i, error);
1470 1.1 thorpej /*
1471 1.1 thorpej * XXX Should attempt to run with fewer receive
1472 1.1 thorpej * XXX buffers instead of just failing.
1473 1.1 thorpej */
1474 1.1 thorpej stge_rxdrain(sc);
1475 1.1 thorpej goto out;
1476 1.1 thorpej }
1477 1.1 thorpej } else
1478 1.1 thorpej STGE_INIT_RXDESC(sc, i);
1479 1.1 thorpej }
1480 1.1 thorpej sc->sc_rxptr = 0;
1481 1.1 thorpej sc->sc_rxdiscard = 0;
1482 1.1 thorpej STGE_RXCHAIN_RESET(sc);
1483 1.1 thorpej
1484 1.1 thorpej /* Set the station address. */
1485 1.1 thorpej bus_space_write_2(st, sh, STGE_StationAddress0,
1486 1.1 thorpej LLADDR(ifp->if_sadl)[0] | (LLADDR(ifp->if_sadl)[1] << 8));
1487 1.1 thorpej bus_space_write_2(st, sh, STGE_StationAddress1,
1488 1.1 thorpej LLADDR(ifp->if_sadl)[2] | (LLADDR(ifp->if_sadl)[3] << 8));
1489 1.1 thorpej bus_space_write_2(st, sh, STGE_StationAddress2,
1490 1.1 thorpej LLADDR(ifp->if_sadl)[4] | (LLADDR(ifp->if_sadl)[5] << 8));
1491 1.1 thorpej
1492 1.1 thorpej /*
1493 1.1 thorpej * Set the statistics masks. Disable all the RMON stats,
1494 1.1 thorpej * and disable selected stats in the non-RMON stats registers.
1495 1.1 thorpej */
1496 1.1 thorpej bus_space_write_4(st, sh, STGE_RMONStatisticsMask, 0xffffffff);
1497 1.1 thorpej bus_space_write_4(st, sh, STGE_StatisticsMask,
1498 1.1 thorpej (1U << 1) | (1U << 2) | (1U << 3) | (1U << 4) | (1U << 5) |
1499 1.1 thorpej (1U << 6) | (1U << 7) | (1U << 8) | (1U << 9) | (1U << 10) |
1500 1.1 thorpej (1U << 13) | (1U << 14) | (1U << 15) | (1U << 19) | (1U << 20) |
1501 1.1 thorpej (1U << 21));
1502 1.1 thorpej
1503 1.1 thorpej /* Set up the receive filter. */
1504 1.1 thorpej stge_set_filter(sc);
1505 1.1 thorpej
1506 1.1 thorpej /*
1507 1.1 thorpej * Give the transmit and receive ring to the chip.
1508 1.1 thorpej */
1509 1.1 thorpej bus_space_write_4(st, sh, STGE_TFDListPtrHi, 0); /* NOTE: 32-bit DMA */
1510 1.1 thorpej bus_space_write_4(st, sh, STGE_TFDListPtrLo,
1511 1.1 thorpej STGE_CDTXADDR(sc, sc->sc_txdirty));
1512 1.1 thorpej
1513 1.1 thorpej bus_space_write_4(st, sh, STGE_RFDListPtrHi, 0); /* NOTE: 32-bit DMA */
1514 1.1 thorpej bus_space_write_4(st, sh, STGE_RFDListPtrLo,
1515 1.1 thorpej STGE_CDRXADDR(sc, sc->sc_rxptr));
1516 1.1 thorpej
1517 1.1 thorpej /*
1518 1.1 thorpej * Initialize the Tx auto-poll period. It's OK to make this number
1519 1.1 thorpej * large (255 is the max, but we use 127) -- we explicitly kick the
1520 1.1 thorpej * transmit engine when there's actually a packet.
1521 1.1 thorpej */
1522 1.1 thorpej bus_space_write_1(st, sh, STGE_TxDMAPollPeriod, 127);
1523 1.1 thorpej
1524 1.1 thorpej /* ..and the Rx auto-poll period. */
1525 1.1 thorpej bus_space_write_1(st, sh, STGE_RxDMAPollPeriod, 64);
1526 1.1 thorpej
1527 1.1 thorpej /* Initialize the Tx start threshold. */
1528 1.1 thorpej bus_space_write_2(st, sh, STGE_TxStartThresh, sc->sc_txthresh);
1529 1.1 thorpej
1530 1.1 thorpej /*
1531 1.1 thorpej * Initialize the Rx DMA interrupt control register. We
1532 1.1 thorpej * request an interrupt after every incoming packet, but
1533 1.1 thorpej * defer it for 32us (64 * 512 ns).
1534 1.1 thorpej */
1535 1.1 thorpej bus_space_write_4(st, sh, STGE_RxDMAIntCtrl,
1536 1.1 thorpej RDIC_RxFrameCount(1) | RDIC_RxDMAWaitTime(512));
1537 1.1 thorpej
1538 1.1 thorpej /*
1539 1.1 thorpej * Initialize the interrupt mask.
1540 1.1 thorpej */
1541 1.1 thorpej sc->sc_IntEnable = IE_HostError | IE_TxComplete | IE_UpdateStats |
1542 1.1 thorpej IE_TxDMAComplete | IE_RxDMAComplete | IE_RFDListEnd;
1543 1.1 thorpej bus_space_write_2(st, sh, STGE_IntStatus, 0xffff);
1544 1.1 thorpej bus_space_write_2(st, sh, STGE_IntEnable, sc->sc_IntEnable);
1545 1.1 thorpej
1546 1.1 thorpej /*
1547 1.1 thorpej * Configure the DMA engine.
1548 1.1 thorpej * XXX Should auto-tune TxBurstLimit.
1549 1.1 thorpej */
1550 1.1 thorpej bus_space_write_4(st, sh, STGE_DMACtrl, sc->sc_DMACtrl |
1551 1.1 thorpej DMAC_TxBurstLimit(3));
1552 1.1 thorpej
1553 1.1 thorpej /*
1554 1.1 thorpej * XXX Configure flow control thresholds.
1555 1.1 thorpej */
1556 1.1 thorpej
1557 1.1 thorpej /*
1558 1.1 thorpej * Set the maximum frame size.
1559 1.1 thorpej */
1560 1.1 thorpej bus_space_write_2(st, sh, STGE_MaxFrameSize,
1561 1.1 thorpej ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
1562 1.1 thorpej ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
1563 1.1 thorpej ETHER_VLAN_ENCAP_LEN : 0));
1564 1.1 thorpej
1565 1.1 thorpej /*
1566 1.1 thorpej * Initialize MacCtrl -- do it before setting the media,
1567 1.1 thorpej * as setting the media will actually program the register.
1568 1.1 thorpej *
1569 1.1 thorpej * Note: We have to poke the IFS value before poking
1570 1.1 thorpej * anything else.
1571 1.1 thorpej */
1572 1.1 thorpej sc->sc_MACCtrl = MC_IFSSelect(0);
1573 1.1 thorpej bus_space_write_4(st, sh, STGE_MACCtrl, sc->sc_MACCtrl);
1574 1.1 thorpej sc->sc_MACCtrl |= MC_StatisticsEnable | MC_TxEnable | MC_RxEnable;
1575 1.1 thorpej
1576 1.1 thorpej if (sc->sc_rev >= 6) { /* >= B.2 */
1577 1.1 thorpej /* Multi-frag frame bug work-around. */
1578 1.1 thorpej bus_space_write_2(st, sh, STGE_DebugCtrl,
1579 1.1 thorpej bus_space_read_2(st, sh, STGE_DebugCtrl) | 0x0200);
1580 1.1 thorpej
1581 1.1 thorpej /* Tx Poll Now bug work-around. */
1582 1.1 thorpej bus_space_write_2(st, sh, STGE_DebugCtrl,
1583 1.1 thorpej bus_space_read_2(st, sh, STGE_DebugCtrl) | 0x0010);
1584 1.1 thorpej }
1585 1.1 thorpej
1586 1.1 thorpej /*
1587 1.1 thorpej * Set the current media.
1588 1.1 thorpej */
1589 1.1 thorpej mii_mediachg(&sc->sc_mii);
1590 1.1 thorpej
1591 1.1 thorpej /*
1592 1.1 thorpej * Start the one second MII clock.
1593 1.1 thorpej */
1594 1.1 thorpej callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
1595 1.1 thorpej
1596 1.1 thorpej /*
1597 1.1 thorpej * ...all done!
1598 1.1 thorpej */
1599 1.1 thorpej ifp->if_flags |= IFF_RUNNING;
1600 1.1 thorpej ifp->if_flags &= ~IFF_OACTIVE;
1601 1.1 thorpej
1602 1.1 thorpej out:
1603 1.1 thorpej if (error)
1604 1.1 thorpej printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1605 1.1 thorpej return (error);
1606 1.1 thorpej }
1607 1.1 thorpej
1608 1.1 thorpej /*
1609 1.1 thorpej * stge_drain:
1610 1.1 thorpej *
1611 1.1 thorpej * Drain the receive queue.
1612 1.1 thorpej */
1613 1.1 thorpej void
1614 1.1 thorpej stge_rxdrain(struct stge_softc *sc)
1615 1.1 thorpej {
1616 1.1 thorpej struct stge_descsoft *ds;
1617 1.1 thorpej int i;
1618 1.1 thorpej
1619 1.1 thorpej for (i = 0; i < STGE_NRXDESC; i++) {
1620 1.1 thorpej ds = &sc->sc_rxsoft[i];
1621 1.1 thorpej if (ds->ds_mbuf != NULL) {
1622 1.1 thorpej bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1623 1.1 thorpej ds->ds_mbuf->m_next = NULL;
1624 1.1 thorpej m_freem(ds->ds_mbuf);
1625 1.1 thorpej ds->ds_mbuf = NULL;
1626 1.1 thorpej }
1627 1.1 thorpej }
1628 1.1 thorpej }
1629 1.1 thorpej
1630 1.1 thorpej /*
1631 1.1 thorpej * stge_stop: [ ifnet interface function ]
1632 1.1 thorpej *
1633 1.1 thorpej * Stop transmission on the interface.
1634 1.1 thorpej */
1635 1.1 thorpej void
1636 1.1 thorpej stge_stop(struct ifnet *ifp, int disable)
1637 1.1 thorpej {
1638 1.1 thorpej struct stge_softc *sc = ifp->if_softc;
1639 1.1 thorpej struct stge_descsoft *ds;
1640 1.1 thorpej int i;
1641 1.1 thorpej
1642 1.1 thorpej /*
1643 1.1 thorpej * Stop the one second clock.
1644 1.1 thorpej */
1645 1.1 thorpej callout_stop(&sc->sc_tick_ch);
1646 1.1 thorpej
1647 1.1 thorpej /* Down the MII. */
1648 1.1 thorpej mii_down(&sc->sc_mii);
1649 1.1 thorpej
1650 1.1 thorpej /*
1651 1.1 thorpej * Disable interrupts.
1652 1.1 thorpej */
1653 1.1 thorpej bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_IntEnable, 0);
1654 1.1 thorpej
1655 1.1 thorpej /*
1656 1.1 thorpej * Stop receiver, transmitter, and stats update.
1657 1.1 thorpej */
1658 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_MACCtrl,
1659 1.1 thorpej MC_StatisticsDisable | MC_TxDisable | MC_RxDisable);
1660 1.1 thorpej
1661 1.1 thorpej /*
1662 1.1 thorpej * Stop the transmit and receive DMA.
1663 1.1 thorpej */
1664 1.1 thorpej stge_dma_wait(sc);
1665 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_TFDListPtrHi, 0);
1666 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_TFDListPtrLo, 0);
1667 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_RFDListPtrHi, 0);
1668 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_RFDListPtrLo, 0);
1669 1.1 thorpej
1670 1.1 thorpej /*
1671 1.1 thorpej * Release any queued transmit buffers.
1672 1.1 thorpej */
1673 1.1 thorpej for (i = 0; i < STGE_NTXDESC; i++) {
1674 1.1 thorpej ds = &sc->sc_txsoft[i];
1675 1.1 thorpej if (ds->ds_mbuf != NULL) {
1676 1.1 thorpej bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1677 1.1 thorpej m_freem(ds->ds_mbuf);
1678 1.1 thorpej ds->ds_mbuf = NULL;
1679 1.1 thorpej }
1680 1.1 thorpej }
1681 1.1 thorpej
1682 1.1 thorpej if (disable)
1683 1.1 thorpej stge_rxdrain(sc);
1684 1.1 thorpej
1685 1.1 thorpej /*
1686 1.1 thorpej * Mark the interface down and cancel the watchdog timer.
1687 1.1 thorpej */
1688 1.1 thorpej ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1689 1.1 thorpej ifp->if_timer = 0;
1690 1.1 thorpej }
1691 1.1 thorpej
1692 1.1 thorpej #if 0
1693 1.1 thorpej static int
1694 1.1 thorpej stge_eeprom_wait(struct stge_softc *sc)
1695 1.1 thorpej {
1696 1.1 thorpej int i;
1697 1.1 thorpej
1698 1.1 thorpej for (i = 0; i < STGE_TIMEOUT; i++) {
1699 1.1 thorpej delay(1000);
1700 1.1 thorpej if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_EepromCtrl) &
1701 1.1 thorpej EC_EepromBusy) == 0)
1702 1.1 thorpej return (0);
1703 1.1 thorpej }
1704 1.1 thorpej return (1);
1705 1.1 thorpej }
1706 1.1 thorpej
1707 1.1 thorpej /*
1708 1.1 thorpej * stge_read_eeprom:
1709 1.1 thorpej *
1710 1.1 thorpej * Read data from the serial EEPROM.
1711 1.1 thorpej */
1712 1.1 thorpej void
1713 1.1 thorpej stge_read_eeprom(struct stge_softc *sc, int offset, uint16_t *data)
1714 1.1 thorpej {
1715 1.1 thorpej
1716 1.1 thorpej if (stge_eeprom_wait(sc))
1717 1.1 thorpej printf("%s: EEPROM failed to come ready\n",
1718 1.1 thorpej sc->sc_dev.dv_xname);
1719 1.1 thorpej
1720 1.1 thorpej bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_EepromCtrl,
1721 1.1 thorpej EC_EepromAddress(offset) | EC_EepromOpcode(EC_OP_RR));
1722 1.1 thorpej if (stge_eeprom_wait(sc))
1723 1.1 thorpej printf("%s: EEPROM read timed out\n",
1724 1.1 thorpej sc->sc_dev.dv_xname);
1725 1.1 thorpej *data = bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_EepromData);
1726 1.1 thorpej }
1727 1.1 thorpej #endif /* 0 */
1728 1.1 thorpej
1729 1.1 thorpej /*
1730 1.1 thorpej * stge_add_rxbuf:
1731 1.1 thorpej *
1732 1.1 thorpej * Add a receive buffer to the indicated descriptor.
1733 1.1 thorpej */
1734 1.1 thorpej int
1735 1.1 thorpej stge_add_rxbuf(struct stge_softc *sc, int idx)
1736 1.1 thorpej {
1737 1.1 thorpej struct stge_descsoft *ds = &sc->sc_rxsoft[idx];
1738 1.1 thorpej struct mbuf *m;
1739 1.1 thorpej int error;
1740 1.1 thorpej
1741 1.1 thorpej MGETHDR(m, M_DONTWAIT, MT_DATA);
1742 1.1 thorpej if (m == NULL)
1743 1.1 thorpej return (ENOBUFS);
1744 1.1 thorpej
1745 1.1 thorpej MCLGET(m, M_DONTWAIT);
1746 1.1 thorpej if ((m->m_flags & M_EXT) == 0) {
1747 1.1 thorpej m_freem(m);
1748 1.1 thorpej return (ENOBUFS);
1749 1.1 thorpej }
1750 1.1 thorpej
1751 1.1 thorpej m->m_data = m->m_ext.ext_buf + 2;
1752 1.1 thorpej m->m_len = MCLBYTES - 2;
1753 1.1 thorpej
1754 1.1 thorpej if (ds->ds_mbuf != NULL)
1755 1.1 thorpej bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1756 1.1 thorpej
1757 1.1 thorpej ds->ds_mbuf = m;
1758 1.1 thorpej
1759 1.1 thorpej error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
1760 1.1 thorpej m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
1761 1.1 thorpej if (error) {
1762 1.1 thorpej printf("%s: can't load rx DMA map %d, error = %d\n",
1763 1.1 thorpej sc->sc_dev.dv_xname, idx, error);
1764 1.1 thorpej panic("stge_add_rxbuf"); /* XXX */
1765 1.1 thorpej }
1766 1.1 thorpej
1767 1.1 thorpej bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1768 1.1 thorpej ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1769 1.1 thorpej
1770 1.1 thorpej STGE_INIT_RXDESC(sc, idx);
1771 1.1 thorpej
1772 1.1 thorpej return (0);
1773 1.1 thorpej }
1774 1.1 thorpej
1775 1.1 thorpej /*
1776 1.1 thorpej * stge_set_filter:
1777 1.1 thorpej *
1778 1.1 thorpej * Set up the receive filter.
1779 1.1 thorpej */
1780 1.1 thorpej void
1781 1.1 thorpej stge_set_filter(struct stge_softc *sc)
1782 1.1 thorpej {
1783 1.1 thorpej struct ethercom *ec = &sc->sc_ethercom;
1784 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1785 1.1 thorpej struct ether_multi *enm;
1786 1.1 thorpej struct ether_multistep step;
1787 1.1 thorpej uint32_t crc;
1788 1.1 thorpej uint32_t mchash[2];
1789 1.1 thorpej
1790 1.1 thorpej sc->sc_ReceiveMode = RM_ReceiveUnicast;
1791 1.1 thorpej if (ifp->if_flags & IFF_BROADCAST)
1792 1.1 thorpej sc->sc_ReceiveMode |= RM_ReceiveBroadcast;
1793 1.1 thorpej
1794 1.1 thorpej if (ifp->if_flags & IFF_PROMISC) {
1795 1.1 thorpej sc->sc_ReceiveMode |= RM_ReceiveAllFrames;
1796 1.1 thorpej goto allmulti;
1797 1.1 thorpej }
1798 1.1 thorpej
1799 1.1 thorpej /*
1800 1.1 thorpej * Set up the multicast address filter by passing all multicast
1801 1.1 thorpej * addresses through a CRC generator, and then using the low-order
1802 1.1 thorpej * 6 bits as an index into the 64 bit multicast hash table. The
1803 1.1 thorpej * high order bits select the register, while the rest of the bits
1804 1.1 thorpej * select the bit within the register.
1805 1.1 thorpej */
1806 1.1 thorpej
1807 1.1 thorpej memset(mchash, 0, sizeof(mchash));
1808 1.1 thorpej
1809 1.1 thorpej ETHER_FIRST_MULTI(step, ec, enm);
1810 1.1 thorpej if (enm == NULL)
1811 1.1 thorpej goto done;
1812 1.1 thorpej
1813 1.1 thorpej while (enm != NULL) {
1814 1.1 thorpej if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1815 1.1 thorpej /*
1816 1.1 thorpej * We must listen to a range of multicast addresses.
1817 1.1 thorpej * For now, just accept all multicasts, rather than
1818 1.1 thorpej * trying to set only those filter bits needed to match
1819 1.1 thorpej * the range. (At this time, the only use of address
1820 1.1 thorpej * ranges is for IP multicast routing, for which the
1821 1.1 thorpej * range is big enough to require all bits set.)
1822 1.1 thorpej */
1823 1.1 thorpej goto allmulti;
1824 1.1 thorpej }
1825 1.1 thorpej
1826 1.1 thorpej crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
1827 1.1 thorpej
1828 1.1 thorpej /* Just want the 6 least significant bits. */
1829 1.1 thorpej crc &= 0x3f;
1830 1.1 thorpej
1831 1.1 thorpej /* Set the corresponding bit in the hash table. */
1832 1.1 thorpej mchash[crc >> 5] |= 1 << (crc & 0x1f);
1833 1.1 thorpej
1834 1.1 thorpej ETHER_NEXT_MULTI(step, enm);
1835 1.1 thorpej }
1836 1.1 thorpej
1837 1.1 thorpej sc->sc_ReceiveMode |= RM_ReceiveMulticastHash;
1838 1.1 thorpej
1839 1.1 thorpej ifp->if_flags &= ~IFF_ALLMULTI;
1840 1.1 thorpej goto done;
1841 1.1 thorpej
1842 1.1 thorpej allmulti:
1843 1.1 thorpej ifp->if_flags |= IFF_ALLMULTI;
1844 1.1 thorpej sc->sc_ReceiveMode |= RM_ReceiveMulticast;
1845 1.1 thorpej
1846 1.1 thorpej done:
1847 1.1 thorpej if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
1848 1.1 thorpej /*
1849 1.1 thorpej * Program the multicast hash table.
1850 1.1 thorpej */
1851 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_HashTable0,
1852 1.1 thorpej mchash[0]);
1853 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_HashTable1,
1854 1.1 thorpej mchash[1]);
1855 1.1 thorpej }
1856 1.1 thorpej
1857 1.1 thorpej bus_space_write_1(sc->sc_st, sc->sc_sh, STGE_ReceiveMode,
1858 1.1 thorpej sc->sc_ReceiveMode);
1859 1.1 thorpej }
1860 1.1 thorpej
1861 1.1 thorpej /*
1862 1.1 thorpej * stge_mii_readreg: [mii interface function]
1863 1.1 thorpej *
1864 1.1 thorpej * Read a PHY register on the MII of the TC9021.
1865 1.1 thorpej */
1866 1.1 thorpej int
1867 1.1 thorpej stge_mii_readreg(struct device *self, int phy, int reg)
1868 1.1 thorpej {
1869 1.1 thorpej
1870 1.1 thorpej return (mii_bitbang_readreg(self, &stge_mii_bitbang_ops, phy, reg));
1871 1.1 thorpej }
1872 1.1 thorpej
1873 1.1 thorpej /*
1874 1.1 thorpej * stge_mii_writereg: [mii interface function]
1875 1.1 thorpej *
1876 1.1 thorpej * Write a PHY register on the MII of the TC9021.
1877 1.1 thorpej */
1878 1.1 thorpej void
1879 1.1 thorpej stge_mii_writereg(struct device *self, int phy, int reg, int val)
1880 1.1 thorpej {
1881 1.1 thorpej
1882 1.1 thorpej mii_bitbang_writereg(self, &stge_mii_bitbang_ops, phy, reg, val);
1883 1.1 thorpej }
1884 1.1 thorpej
1885 1.1 thorpej /*
1886 1.1 thorpej * stge_mii_statchg: [mii interface function]
1887 1.1 thorpej *
1888 1.1 thorpej * Callback from MII layer when media changes.
1889 1.1 thorpej */
1890 1.1 thorpej void
1891 1.1 thorpej stge_mii_statchg(struct device *self)
1892 1.1 thorpej {
1893 1.1 thorpej struct stge_softc *sc = (struct stge_softc *) self;
1894 1.1 thorpej
1895 1.1 thorpej if (sc->sc_mii.mii_media_active & IFM_FDX)
1896 1.1 thorpej sc->sc_MACCtrl |= MC_DuplexSelect;
1897 1.1 thorpej else
1898 1.1 thorpej sc->sc_MACCtrl &= ~MC_DuplexSelect;
1899 1.1 thorpej
1900 1.1 thorpej /* XXX 802.1x flow-control? */
1901 1.1 thorpej
1902 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_MACCtrl, sc->sc_MACCtrl);
1903 1.1 thorpej }
1904 1.1 thorpej
1905 1.1 thorpej /*
1906 1.1 thorpej * sste_mii_bitbang_read: [mii bit-bang interface function]
1907 1.1 thorpej *
1908 1.1 thorpej * Read the MII serial port for the MII bit-bang module.
1909 1.1 thorpej */
1910 1.1 thorpej uint32_t
1911 1.1 thorpej stge_mii_bitbang_read(struct device *self)
1912 1.1 thorpej {
1913 1.1 thorpej struct stge_softc *sc = (void *) self;
1914 1.1 thorpej
1915 1.1 thorpej return (bus_space_read_1(sc->sc_st, sc->sc_sh, STGE_PhyCtrl));
1916 1.1 thorpej }
1917 1.1 thorpej
1918 1.1 thorpej /*
1919 1.1 thorpej * stge_mii_bitbang_write: [mii big-bang interface function]
1920 1.1 thorpej *
1921 1.1 thorpej * Write the MII serial port for the MII bit-bang module.
1922 1.1 thorpej */
1923 1.1 thorpej void
1924 1.1 thorpej stge_mii_bitbang_write(struct device *self, uint32_t val)
1925 1.1 thorpej {
1926 1.1 thorpej struct stge_softc *sc = (void *) self;
1927 1.1 thorpej
1928 1.1 thorpej bus_space_write_1(sc->sc_st, sc->sc_sh, STGE_PhyCtrl,
1929 1.1 thorpej val | sc->sc_PhyCtrl);
1930 1.1 thorpej }
1931 1.1 thorpej
1932 1.1 thorpej /*
1933 1.1 thorpej * stge_mediastatus: [ifmedia interface function]
1934 1.1 thorpej *
1935 1.1 thorpej * Get the current interface media status.
1936 1.1 thorpej */
1937 1.1 thorpej void
1938 1.1 thorpej stge_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1939 1.1 thorpej {
1940 1.1 thorpej struct stge_softc *sc = ifp->if_softc;
1941 1.1 thorpej
1942 1.1 thorpej mii_pollstat(&sc->sc_mii);
1943 1.1 thorpej ifmr->ifm_status = sc->sc_mii.mii_media_status;
1944 1.1 thorpej ifmr->ifm_active = sc->sc_mii.mii_media_active;
1945 1.1 thorpej }
1946 1.1 thorpej
1947 1.1 thorpej /*
1948 1.1 thorpej * stge_mediachange: [ifmedia interface function]
1949 1.1 thorpej *
1950 1.1 thorpej * Set hardware to newly-selected media.
1951 1.1 thorpej */
1952 1.1 thorpej int
1953 1.1 thorpej stge_mediachange(struct ifnet *ifp)
1954 1.1 thorpej {
1955 1.1 thorpej struct stge_softc *sc = ifp->if_softc;
1956 1.1 thorpej
1957 1.1 thorpej if (ifp->if_flags & IFF_UP)
1958 1.1 thorpej mii_mediachg(&sc->sc_mii);
1959 1.1 thorpej return (0);
1960 1.1 thorpej }
1961