if_nfe.c revision 1.11 1 1.11 tsutsui /* $NetBSD: if_nfe.c,v 1.11 2007/01/01 04:13:25 tsutsui Exp $ */
2 1.1 chs /* $OpenBSD: if_nfe.c,v 1.52 2006/03/02 09:04:00 jsg Exp $ */
3 1.1 chs
4 1.1 chs /*-
5 1.1 chs * Copyright (c) 2006 Damien Bergamini <damien.bergamini (at) free.fr>
6 1.1 chs * Copyright (c) 2005, 2006 Jonathan Gray <jsg (at) openbsd.org>
7 1.1 chs *
8 1.1 chs * Permission to use, copy, modify, and distribute this software for any
9 1.1 chs * purpose with or without fee is hereby granted, provided that the above
10 1.1 chs * copyright notice and this permission notice appear in all copies.
11 1.1 chs *
12 1.1 chs * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 1.1 chs * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 1.1 chs * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 1.1 chs * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 1.1 chs * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 1.1 chs * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 1.1 chs * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 1.1 chs */
20 1.1 chs
21 1.1 chs /* Driver for NVIDIA nForce MCP Fast Ethernet and Gigabit Ethernet */
22 1.1 chs
23 1.1 chs #include <sys/cdefs.h>
24 1.11 tsutsui __KERNEL_RCSID(0, "$NetBSD: if_nfe.c,v 1.11 2007/01/01 04:13:25 tsutsui Exp $");
25 1.1 chs
26 1.1 chs #include "opt_inet.h"
27 1.1 chs #include "bpfilter.h"
28 1.1 chs #include "vlan.h"
29 1.1 chs
30 1.1 chs #include <sys/param.h>
31 1.1 chs #include <sys/endian.h>
32 1.1 chs #include <sys/systm.h>
33 1.1 chs #include <sys/types.h>
34 1.1 chs #include <sys/sockio.h>
35 1.1 chs #include <sys/mbuf.h>
36 1.1 chs #include <sys/queue.h>
37 1.1 chs #include <sys/malloc.h>
38 1.1 chs #include <sys/kernel.h>
39 1.1 chs #include <sys/device.h>
40 1.1 chs #include <sys/socket.h>
41 1.1 chs
42 1.1 chs #include <machine/bus.h>
43 1.1 chs
44 1.1 chs #include <net/if.h>
45 1.1 chs #include <net/if_dl.h>
46 1.1 chs #include <net/if_media.h>
47 1.1 chs #include <net/if_ether.h>
48 1.1 chs #include <net/if_arp.h>
49 1.1 chs
50 1.1 chs #ifdef INET
51 1.1 chs #include <netinet/in.h>
52 1.1 chs #include <netinet/in_systm.h>
53 1.1 chs #include <netinet/in_var.h>
54 1.1 chs #include <netinet/ip.h>
55 1.1 chs #include <netinet/if_inarp.h>
56 1.1 chs #endif
57 1.1 chs
58 1.1 chs #if NVLAN > 0
59 1.1 chs #include <net/if_types.h>
60 1.1 chs #endif
61 1.1 chs
62 1.1 chs #if NBPFILTER > 0
63 1.1 chs #include <net/bpf.h>
64 1.1 chs #endif
65 1.1 chs
66 1.1 chs #include <dev/mii/mii.h>
67 1.1 chs #include <dev/mii/miivar.h>
68 1.1 chs
69 1.1 chs #include <dev/pci/pcireg.h>
70 1.1 chs #include <dev/pci/pcivar.h>
71 1.1 chs #include <dev/pci/pcidevs.h>
72 1.1 chs
73 1.1 chs #include <dev/pci/if_nfereg.h>
74 1.1 chs #include <dev/pci/if_nfevar.h>
75 1.1 chs
76 1.1 chs int nfe_match(struct device *, struct cfdata *, void *);
77 1.1 chs void nfe_attach(struct device *, struct device *, void *);
78 1.1 chs void nfe_power(int, void *);
79 1.1 chs void nfe_miibus_statchg(struct device *);
80 1.1 chs int nfe_miibus_readreg(struct device *, int, int);
81 1.1 chs void nfe_miibus_writereg(struct device *, int, int, int);
82 1.1 chs int nfe_intr(void *);
83 1.1 chs int nfe_ioctl(struct ifnet *, u_long, caddr_t);
84 1.1 chs void nfe_txdesc32_sync(struct nfe_softc *, struct nfe_desc32 *, int);
85 1.1 chs void nfe_txdesc64_sync(struct nfe_softc *, struct nfe_desc64 *, int);
86 1.1 chs void nfe_txdesc32_rsync(struct nfe_softc *, int, int, int);
87 1.1 chs void nfe_txdesc64_rsync(struct nfe_softc *, int, int, int);
88 1.1 chs void nfe_rxdesc32_sync(struct nfe_softc *, struct nfe_desc32 *, int);
89 1.1 chs void nfe_rxdesc64_sync(struct nfe_softc *, struct nfe_desc64 *, int);
90 1.1 chs void nfe_rxeof(struct nfe_softc *);
91 1.1 chs void nfe_txeof(struct nfe_softc *);
92 1.1 chs int nfe_encap(struct nfe_softc *, struct mbuf *);
93 1.1 chs void nfe_start(struct ifnet *);
94 1.1 chs void nfe_watchdog(struct ifnet *);
95 1.1 chs int nfe_init(struct ifnet *);
96 1.1 chs void nfe_stop(struct ifnet *, int);
97 1.1 chs struct nfe_jbuf *nfe_jalloc(struct nfe_softc *);
98 1.1 chs void nfe_jfree(struct mbuf *, caddr_t, size_t, void *);
99 1.1 chs int nfe_jpool_alloc(struct nfe_softc *);
100 1.1 chs void nfe_jpool_free(struct nfe_softc *);
101 1.1 chs int nfe_alloc_rx_ring(struct nfe_softc *, struct nfe_rx_ring *);
102 1.1 chs void nfe_reset_rx_ring(struct nfe_softc *, struct nfe_rx_ring *);
103 1.1 chs void nfe_free_rx_ring(struct nfe_softc *, struct nfe_rx_ring *);
104 1.1 chs int nfe_alloc_tx_ring(struct nfe_softc *, struct nfe_tx_ring *);
105 1.1 chs void nfe_reset_tx_ring(struct nfe_softc *, struct nfe_tx_ring *);
106 1.1 chs void nfe_free_tx_ring(struct nfe_softc *, struct nfe_tx_ring *);
107 1.1 chs int nfe_ifmedia_upd(struct ifnet *);
108 1.1 chs void nfe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
109 1.1 chs void nfe_setmulti(struct nfe_softc *);
110 1.1 chs void nfe_get_macaddr(struct nfe_softc *, uint8_t *);
111 1.1 chs void nfe_set_macaddr(struct nfe_softc *, const uint8_t *);
112 1.1 chs void nfe_tick(void *);
113 1.1 chs
114 1.1 chs CFATTACH_DECL(nfe, sizeof(struct nfe_softc), nfe_match, nfe_attach, NULL, NULL);
115 1.1 chs
116 1.1 chs /*#define NFE_NO_JUMBO*/
117 1.1 chs
118 1.1 chs #ifdef NFE_DEBUG
119 1.1 chs int nfedebug = 0;
120 1.1 chs #define DPRINTF(x) do { if (nfedebug) printf x; } while (0)
121 1.1 chs #define DPRINTFN(n,x) do { if (nfedebug >= (n)) printf x; } while (0)
122 1.1 chs #else
123 1.1 chs #define DPRINTF(x)
124 1.1 chs #define DPRINTFN(n,x)
125 1.1 chs #endif
126 1.1 chs
127 1.1 chs /* deal with naming differences */
128 1.1 chs
129 1.1 chs #define PCI_PRODUCT_NVIDIA_NFORCE3_LAN2 \
130 1.1 chs PCI_PRODUCT_NVIDIA_NFORCE2_400_LAN1
131 1.1 chs #define PCI_PRODUCT_NVIDIA_NFORCE3_LAN3 \
132 1.1 chs PCI_PRODUCT_NVIDIA_NFORCE2_400_LAN2
133 1.1 chs #define PCI_PRODUCT_NVIDIA_NFORCE3_LAN5 \
134 1.1 chs PCI_PRODUCT_NVIDIA_NFORCE3_250_LAN
135 1.1 chs
136 1.1 chs #define PCI_PRODUCT_NVIDIA_CK804_LAN1 \
137 1.1 chs PCI_PRODUCT_NVIDIA_NFORCE4_LAN1
138 1.1 chs #define PCI_PRODUCT_NVIDIA_CK804_LAN2 \
139 1.1 chs PCI_PRODUCT_NVIDIA_NFORCE4_LAN2
140 1.1 chs
141 1.1 chs #define PCI_PRODUCT_NVIDIA_MCP51_LAN1 \
142 1.1 chs PCI_PRODUCT_NVIDIA_NFORCE430_LAN1
143 1.1 chs #define PCI_PRODUCT_NVIDIA_MCP51_LAN2 \
144 1.1 chs PCI_PRODUCT_NVIDIA_NFORCE430_LAN2
145 1.1 chs
146 1.1 chs #ifdef _LP64
147 1.1 chs #define __LP64__ 1
148 1.1 chs #endif
149 1.1 chs
150 1.1 chs const struct nfe_product {
151 1.1 chs pci_vendor_id_t vendor;
152 1.1 chs pci_product_id_t product;
153 1.1 chs } nfe_devices[] = {
154 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE_LAN },
155 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE2_LAN },
156 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN1 },
157 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN2 },
158 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN3 },
159 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN4 },
160 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN5 },
161 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_CK804_LAN1 },
162 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_CK804_LAN2 },
163 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP04_LAN1 },
164 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP04_LAN2 },
165 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP51_LAN1 },
166 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP51_LAN2 },
167 1.1 chs { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP55_LAN1 },
168 1.4 xtraeme { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP55_LAN2 },
169 1.4 xtraeme { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN1 },
170 1.4 xtraeme { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN2 },
171 1.4 xtraeme { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN3 },
172 1.4 xtraeme { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN4 },
173 1.4 xtraeme { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN1 },
174 1.4 xtraeme { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN2 },
175 1.4 xtraeme { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN3 },
176 1.4 xtraeme { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN4 }
177 1.1 chs };
178 1.1 chs
179 1.1 chs int
180 1.7 christos nfe_match(struct device *dev, struct cfdata *match, void *aux)
181 1.1 chs {
182 1.1 chs struct pci_attach_args *pa = aux;
183 1.1 chs const struct nfe_product *np;
184 1.1 chs int i;
185 1.1 chs
186 1.1 chs for (i = 0; i < sizeof(nfe_devices) / sizeof(nfe_devices[0]); i++) {
187 1.1 chs np = &nfe_devices[i];
188 1.1 chs if (PCI_VENDOR(pa->pa_id) == np->vendor &&
189 1.1 chs PCI_PRODUCT(pa->pa_id) == np->product)
190 1.1 chs return 1;
191 1.1 chs }
192 1.1 chs return 0;
193 1.1 chs }
194 1.1 chs
195 1.1 chs void
196 1.7 christos nfe_attach(struct device *parent, struct device *self, void *aux)
197 1.1 chs {
198 1.1 chs struct nfe_softc *sc = (struct nfe_softc *)self;
199 1.1 chs struct pci_attach_args *pa = aux;
200 1.1 chs pci_chipset_tag_t pc = pa->pa_pc;
201 1.1 chs pci_intr_handle_t ih;
202 1.1 chs const char *intrstr;
203 1.1 chs struct ifnet *ifp;
204 1.1 chs bus_size_t memsize;
205 1.1 chs pcireg_t memtype;
206 1.10 tsutsui char devinfo[256];
207 1.10 tsutsui
208 1.10 tsutsui pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
209 1.10 tsutsui aprint_normal(": %s (rev. 0x%02x)\n",
210 1.10 tsutsui devinfo, PCI_REVISION(pa->pa_class));
211 1.1 chs
212 1.1 chs memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, NFE_PCI_BA);
213 1.1 chs switch (memtype) {
214 1.1 chs case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
215 1.1 chs case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
216 1.1 chs if (pci_mapreg_map(pa, NFE_PCI_BA, memtype, 0, &sc->sc_memt,
217 1.1 chs &sc->sc_memh, NULL, &memsize) == 0)
218 1.1 chs break;
219 1.1 chs /* FALLTHROUGH */
220 1.1 chs default:
221 1.10 tsutsui printf("%s: could not map mem space\n", sc->sc_dev.dv_xname);
222 1.1 chs return;
223 1.1 chs }
224 1.1 chs
225 1.1 chs if (pci_intr_map(pa, &ih) != 0) {
226 1.10 tsutsui printf("%s: could not map interrupt\n", sc->sc_dev.dv_xname);
227 1.1 chs return;
228 1.1 chs }
229 1.1 chs
230 1.1 chs intrstr = pci_intr_string(pc, ih);
231 1.1 chs sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, nfe_intr, sc);
232 1.1 chs if (sc->sc_ih == NULL) {
233 1.10 tsutsui printf("%s: could not establish interrupt",
234 1.10 tsutsui sc->sc_dev.dv_xname);
235 1.1 chs if (intrstr != NULL)
236 1.1 chs printf(" at %s", intrstr);
237 1.1 chs printf("\n");
238 1.1 chs return;
239 1.1 chs }
240 1.10 tsutsui printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
241 1.1 chs
242 1.1 chs sc->sc_dmat = pa->pa_dmat;
243 1.1 chs
244 1.1 chs nfe_get_macaddr(sc, sc->sc_enaddr);
245 1.10 tsutsui printf("%s: Ethernet address %s\n",
246 1.10 tsutsui sc->sc_dev.dv_xname, ether_sprintf(sc->sc_enaddr));
247 1.1 chs
248 1.1 chs sc->sc_flags = 0;
249 1.1 chs
250 1.1 chs switch (PCI_PRODUCT(pa->pa_id)) {
251 1.1 chs case PCI_PRODUCT_NVIDIA_NFORCE3_LAN2:
252 1.1 chs case PCI_PRODUCT_NVIDIA_NFORCE3_LAN3:
253 1.1 chs case PCI_PRODUCT_NVIDIA_NFORCE3_LAN4:
254 1.1 chs case PCI_PRODUCT_NVIDIA_NFORCE3_LAN5:
255 1.1 chs sc->sc_flags |= NFE_JUMBO_SUP | NFE_HW_CSUM;
256 1.1 chs break;
257 1.1 chs case PCI_PRODUCT_NVIDIA_MCP51_LAN1:
258 1.1 chs case PCI_PRODUCT_NVIDIA_MCP51_LAN2:
259 1.4 xtraeme case PCI_PRODUCT_NVIDIA_MCP61_LAN1:
260 1.4 xtraeme case PCI_PRODUCT_NVIDIA_MCP61_LAN2:
261 1.4 xtraeme case PCI_PRODUCT_NVIDIA_MCP61_LAN3:
262 1.4 xtraeme case PCI_PRODUCT_NVIDIA_MCP61_LAN4:
263 1.1 chs sc->sc_flags |= NFE_40BIT_ADDR;
264 1.1 chs break;
265 1.1 chs case PCI_PRODUCT_NVIDIA_CK804_LAN1:
266 1.1 chs case PCI_PRODUCT_NVIDIA_CK804_LAN2:
267 1.1 chs case PCI_PRODUCT_NVIDIA_MCP04_LAN1:
268 1.1 chs case PCI_PRODUCT_NVIDIA_MCP04_LAN2:
269 1.1 chs sc->sc_flags |= NFE_JUMBO_SUP | NFE_40BIT_ADDR | NFE_HW_CSUM;
270 1.1 chs break;
271 1.1 chs case PCI_PRODUCT_NVIDIA_MCP55_LAN1:
272 1.1 chs case PCI_PRODUCT_NVIDIA_MCP55_LAN2:
273 1.4 xtraeme case PCI_PRODUCT_NVIDIA_MCP65_LAN1:
274 1.4 xtraeme case PCI_PRODUCT_NVIDIA_MCP65_LAN2:
275 1.4 xtraeme case PCI_PRODUCT_NVIDIA_MCP65_LAN3:
276 1.4 xtraeme case PCI_PRODUCT_NVIDIA_MCP65_LAN4:
277 1.1 chs sc->sc_flags |= NFE_JUMBO_SUP | NFE_40BIT_ADDR | NFE_HW_CSUM |
278 1.1 chs NFE_HW_VLAN;
279 1.1 chs break;
280 1.1 chs }
281 1.1 chs
282 1.1 chs #ifndef NFE_NO_JUMBO
283 1.1 chs /* enable jumbo frames for adapters that support it */
284 1.1 chs if (sc->sc_flags & NFE_JUMBO_SUP)
285 1.1 chs sc->sc_flags |= NFE_USE_JUMBO;
286 1.1 chs #endif
287 1.1 chs
288 1.1 chs /*
289 1.1 chs * Allocate Tx and Rx rings.
290 1.1 chs */
291 1.1 chs if (nfe_alloc_tx_ring(sc, &sc->txq) != 0) {
292 1.1 chs printf("%s: could not allocate Tx ring\n",
293 1.1 chs sc->sc_dev.dv_xname);
294 1.1 chs return;
295 1.1 chs }
296 1.1 chs
297 1.1 chs if (nfe_alloc_rx_ring(sc, &sc->rxq) != 0) {
298 1.1 chs printf("%s: could not allocate Rx ring\n",
299 1.1 chs sc->sc_dev.dv_xname);
300 1.1 chs nfe_free_tx_ring(sc, &sc->txq);
301 1.1 chs return;
302 1.1 chs }
303 1.1 chs
304 1.1 chs ifp = &sc->sc_ethercom.ec_if;
305 1.1 chs ifp->if_softc = sc;
306 1.1 chs ifp->if_mtu = ETHERMTU;
307 1.1 chs ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
308 1.1 chs ifp->if_ioctl = nfe_ioctl;
309 1.1 chs ifp->if_start = nfe_start;
310 1.1 chs ifp->if_watchdog = nfe_watchdog;
311 1.1 chs ifp->if_init = nfe_init;
312 1.1 chs ifp->if_baudrate = IF_Gbps(1);
313 1.1 chs IFQ_SET_MAXLEN(&ifp->if_snd, NFE_IFQ_MAXLEN);
314 1.1 chs IFQ_SET_READY(&ifp->if_snd);
315 1.1 chs strlcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
316 1.1 chs
317 1.1 chs #if NVLAN > 0
318 1.1 chs if (sc->sc_flags & NFE_HW_VLAN)
319 1.1 chs sc->sc_ethercom.ec_capabilities |=
320 1.1 chs ETHERCAP_VLAN_HWTAGGING | ETHERCAP_VLAN_MTU;
321 1.1 chs #endif
322 1.1 chs #ifdef NFE_CSUM
323 1.1 chs if (sc->sc_flags & NFE_HW_CSUM) {
324 1.1 chs ifp->if_capabilities |= IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 |
325 1.1 chs IFCAP_CSUM_UDPv4;
326 1.1 chs }
327 1.1 chs #endif
328 1.1 chs
329 1.1 chs sc->sc_mii.mii_ifp = ifp;
330 1.1 chs sc->sc_mii.mii_readreg = nfe_miibus_readreg;
331 1.1 chs sc->sc_mii.mii_writereg = nfe_miibus_writereg;
332 1.1 chs sc->sc_mii.mii_statchg = nfe_miibus_statchg;
333 1.1 chs
334 1.1 chs ifmedia_init(&sc->sc_mii.mii_media, 0, nfe_ifmedia_upd,
335 1.1 chs nfe_ifmedia_sts);
336 1.1 chs mii_attach(self, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
337 1.1 chs MII_OFFSET_ANY, 0);
338 1.1 chs if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
339 1.1 chs printf("%s: no PHY found!\n", sc->sc_dev.dv_xname);
340 1.1 chs ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL,
341 1.1 chs 0, NULL);
342 1.1 chs ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL);
343 1.1 chs } else
344 1.1 chs ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
345 1.1 chs
346 1.1 chs if_attach(ifp);
347 1.1 chs ether_ifattach(ifp, sc->sc_enaddr);
348 1.1 chs
349 1.1 chs callout_init(&sc->sc_tick_ch);
350 1.1 chs callout_setfunc(&sc->sc_tick_ch, nfe_tick, sc);
351 1.1 chs
352 1.5 jmcneill sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
353 1.5 jmcneill nfe_power, sc);
354 1.1 chs }
355 1.1 chs
356 1.1 chs void
357 1.1 chs nfe_power(int why, void *arg)
358 1.1 chs {
359 1.1 chs struct nfe_softc *sc = arg;
360 1.1 chs struct ifnet *ifp;
361 1.1 chs
362 1.1 chs if (why == PWR_RESUME) {
363 1.1 chs ifp = &sc->sc_ethercom.ec_if;
364 1.1 chs if (ifp->if_flags & IFF_UP) {
365 1.1 chs ifp->if_flags &= ~IFF_RUNNING;
366 1.1 chs nfe_init(ifp);
367 1.1 chs if (ifp->if_flags & IFF_RUNNING)
368 1.1 chs nfe_start(ifp);
369 1.1 chs }
370 1.1 chs }
371 1.1 chs }
372 1.1 chs
373 1.1 chs void
374 1.1 chs nfe_miibus_statchg(struct device *dev)
375 1.1 chs {
376 1.1 chs struct nfe_softc *sc = (struct nfe_softc *)dev;
377 1.1 chs struct mii_data *mii = &sc->sc_mii;
378 1.1 chs uint32_t phy, seed, misc = NFE_MISC1_MAGIC, link = NFE_MEDIA_SET;
379 1.1 chs
380 1.1 chs phy = NFE_READ(sc, NFE_PHY_IFACE);
381 1.1 chs phy &= ~(NFE_PHY_HDX | NFE_PHY_100TX | NFE_PHY_1000T);
382 1.1 chs
383 1.1 chs seed = NFE_READ(sc, NFE_RNDSEED);
384 1.1 chs seed &= ~NFE_SEED_MASK;
385 1.1 chs
386 1.1 chs if ((mii->mii_media_active & IFM_GMASK) == IFM_HDX) {
387 1.1 chs phy |= NFE_PHY_HDX; /* half-duplex */
388 1.1 chs misc |= NFE_MISC1_HDX;
389 1.1 chs }
390 1.1 chs
391 1.1 chs switch (IFM_SUBTYPE(mii->mii_media_active)) {
392 1.1 chs case IFM_1000_T: /* full-duplex only */
393 1.1 chs link |= NFE_MEDIA_1000T;
394 1.1 chs seed |= NFE_SEED_1000T;
395 1.1 chs phy |= NFE_PHY_1000T;
396 1.1 chs break;
397 1.1 chs case IFM_100_TX:
398 1.1 chs link |= NFE_MEDIA_100TX;
399 1.1 chs seed |= NFE_SEED_100TX;
400 1.1 chs phy |= NFE_PHY_100TX;
401 1.1 chs break;
402 1.1 chs case IFM_10_T:
403 1.1 chs link |= NFE_MEDIA_10T;
404 1.1 chs seed |= NFE_SEED_10T;
405 1.1 chs break;
406 1.1 chs }
407 1.1 chs
408 1.1 chs NFE_WRITE(sc, NFE_RNDSEED, seed); /* XXX: gigabit NICs only? */
409 1.1 chs
410 1.1 chs NFE_WRITE(sc, NFE_PHY_IFACE, phy);
411 1.1 chs NFE_WRITE(sc, NFE_MISC1, misc);
412 1.1 chs NFE_WRITE(sc, NFE_LINKSPEED, link);
413 1.1 chs }
414 1.1 chs
415 1.1 chs int
416 1.1 chs nfe_miibus_readreg(struct device *dev, int phy, int reg)
417 1.1 chs {
418 1.1 chs struct nfe_softc *sc = (struct nfe_softc *)dev;
419 1.1 chs uint32_t val;
420 1.1 chs int ntries;
421 1.1 chs
422 1.1 chs NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
423 1.1 chs
424 1.1 chs if (NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY) {
425 1.1 chs NFE_WRITE(sc, NFE_PHY_CTL, NFE_PHY_BUSY);
426 1.1 chs DELAY(100);
427 1.1 chs }
428 1.1 chs
429 1.1 chs NFE_WRITE(sc, NFE_PHY_CTL, (phy << NFE_PHYADD_SHIFT) | reg);
430 1.1 chs
431 1.1 chs for (ntries = 0; ntries < 1000; ntries++) {
432 1.1 chs DELAY(100);
433 1.1 chs if (!(NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY))
434 1.1 chs break;
435 1.1 chs }
436 1.1 chs if (ntries == 1000) {
437 1.1 chs DPRINTFN(2, ("%s: timeout waiting for PHY\n",
438 1.1 chs sc->sc_dev.dv_xname));
439 1.1 chs return 0;
440 1.1 chs }
441 1.1 chs
442 1.1 chs if (NFE_READ(sc, NFE_PHY_STATUS) & NFE_PHY_ERROR) {
443 1.1 chs DPRINTFN(2, ("%s: could not read PHY\n",
444 1.1 chs sc->sc_dev.dv_xname));
445 1.1 chs return 0;
446 1.1 chs }
447 1.1 chs
448 1.1 chs val = NFE_READ(sc, NFE_PHY_DATA);
449 1.1 chs if (val != 0xffffffff && val != 0)
450 1.1 chs sc->mii_phyaddr = phy;
451 1.1 chs
452 1.1 chs DPRINTFN(2, ("%s: mii read phy %d reg 0x%x ret 0x%x\n",
453 1.1 chs sc->sc_dev.dv_xname, phy, reg, val));
454 1.1 chs
455 1.1 chs return val;
456 1.1 chs }
457 1.1 chs
458 1.1 chs void
459 1.1 chs nfe_miibus_writereg(struct device *dev, int phy, int reg, int val)
460 1.1 chs {
461 1.1 chs struct nfe_softc *sc = (struct nfe_softc *)dev;
462 1.1 chs uint32_t ctl;
463 1.1 chs int ntries;
464 1.1 chs
465 1.1 chs NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
466 1.1 chs
467 1.1 chs if (NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY) {
468 1.1 chs NFE_WRITE(sc, NFE_PHY_CTL, NFE_PHY_BUSY);
469 1.1 chs DELAY(100);
470 1.1 chs }
471 1.1 chs
472 1.1 chs NFE_WRITE(sc, NFE_PHY_DATA, val);
473 1.1 chs ctl = NFE_PHY_WRITE | (phy << NFE_PHYADD_SHIFT) | reg;
474 1.1 chs NFE_WRITE(sc, NFE_PHY_CTL, ctl);
475 1.1 chs
476 1.1 chs for (ntries = 0; ntries < 1000; ntries++) {
477 1.1 chs DELAY(100);
478 1.1 chs if (!(NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY))
479 1.1 chs break;
480 1.1 chs }
481 1.1 chs #ifdef NFE_DEBUG
482 1.1 chs if (nfedebug >= 2 && ntries == 1000)
483 1.1 chs printf("could not write to PHY\n");
484 1.1 chs #endif
485 1.1 chs }
486 1.1 chs
487 1.1 chs int
488 1.1 chs nfe_intr(void *arg)
489 1.1 chs {
490 1.1 chs struct nfe_softc *sc = arg;
491 1.1 chs struct ifnet *ifp = &sc->sc_ethercom.ec_if;
492 1.1 chs uint32_t r;
493 1.1 chs
494 1.1 chs if ((r = NFE_READ(sc, NFE_IRQ_STATUS)) == 0)
495 1.1 chs return 0; /* not for us */
496 1.1 chs NFE_WRITE(sc, NFE_IRQ_STATUS, r);
497 1.1 chs
498 1.1 chs DPRINTFN(5, ("nfe_intr: interrupt register %x\n", r));
499 1.1 chs
500 1.1 chs if (r & NFE_IRQ_LINK) {
501 1.1 chs NFE_READ(sc, NFE_PHY_STATUS);
502 1.1 chs NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
503 1.1 chs DPRINTF(("%s: link state changed\n", sc->sc_dev.dv_xname));
504 1.1 chs }
505 1.1 chs
506 1.1 chs if (ifp->if_flags & IFF_RUNNING) {
507 1.1 chs /* check Rx ring */
508 1.1 chs nfe_rxeof(sc);
509 1.1 chs
510 1.1 chs /* check Tx ring */
511 1.1 chs nfe_txeof(sc);
512 1.1 chs }
513 1.1 chs
514 1.1 chs return 1;
515 1.1 chs }
516 1.1 chs
517 1.1 chs int
518 1.1 chs nfe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
519 1.1 chs {
520 1.1 chs struct nfe_softc *sc = ifp->if_softc;
521 1.1 chs struct ifreq *ifr = (struct ifreq *)data;
522 1.1 chs struct ifaddr *ifa = (struct ifaddr *)data;
523 1.1 chs int s, error = 0;
524 1.1 chs
525 1.1 chs s = splnet();
526 1.1 chs
527 1.1 chs switch (cmd) {
528 1.1 chs case SIOCSIFADDR:
529 1.1 chs ifp->if_flags |= IFF_UP;
530 1.1 chs nfe_init(ifp);
531 1.1 chs switch (ifa->ifa_addr->sa_family) {
532 1.1 chs #ifdef INET
533 1.1 chs case AF_INET:
534 1.1 chs arp_ifinit(ifp, ifa);
535 1.1 chs break;
536 1.1 chs #endif
537 1.1 chs default:
538 1.1 chs break;
539 1.1 chs }
540 1.1 chs break;
541 1.1 chs case SIOCSIFMTU:
542 1.1 chs if (ifr->ifr_mtu < ETHERMIN ||
543 1.1 chs ((sc->sc_flags & NFE_USE_JUMBO) &&
544 1.1 chs ifr->ifr_mtu > ETHERMTU_JUMBO) ||
545 1.1 chs (!(sc->sc_flags & NFE_USE_JUMBO) &&
546 1.1 chs ifr->ifr_mtu > ETHERMTU))
547 1.1 chs error = EINVAL;
548 1.1 chs else if (ifp->if_mtu != ifr->ifr_mtu)
549 1.1 chs ifp->if_mtu = ifr->ifr_mtu;
550 1.1 chs break;
551 1.1 chs case SIOCSIFFLAGS:
552 1.1 chs if (ifp->if_flags & IFF_UP) {
553 1.1 chs /*
554 1.1 chs * If only the PROMISC or ALLMULTI flag changes, then
555 1.1 chs * don't do a full re-init of the chip, just update
556 1.1 chs * the Rx filter.
557 1.1 chs */
558 1.1 chs if ((ifp->if_flags & IFF_RUNNING) &&
559 1.1 chs ((ifp->if_flags ^ sc->sc_if_flags) &
560 1.1 chs (IFF_ALLMULTI | IFF_PROMISC)) != 0)
561 1.1 chs nfe_setmulti(sc);
562 1.1 chs else
563 1.1 chs nfe_init(ifp);
564 1.1 chs } else {
565 1.1 chs if (ifp->if_flags & IFF_RUNNING)
566 1.1 chs nfe_stop(ifp, 1);
567 1.1 chs }
568 1.1 chs sc->sc_if_flags = ifp->if_flags;
569 1.1 chs break;
570 1.1 chs case SIOCADDMULTI:
571 1.1 chs case SIOCDELMULTI:
572 1.1 chs error = (cmd == SIOCADDMULTI) ?
573 1.1 chs ether_addmulti(ifr, &sc->sc_ethercom) :
574 1.1 chs ether_delmulti(ifr, &sc->sc_ethercom);
575 1.1 chs
576 1.1 chs if (error == ENETRESET) {
577 1.1 chs if (ifp->if_flags & IFF_RUNNING)
578 1.1 chs nfe_setmulti(sc);
579 1.1 chs error = 0;
580 1.1 chs }
581 1.1 chs break;
582 1.1 chs case SIOCSIFMEDIA:
583 1.1 chs case SIOCGIFMEDIA:
584 1.1 chs error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
585 1.1 chs break;
586 1.1 chs default:
587 1.1 chs error = ether_ioctl(ifp, cmd, data);
588 1.1 chs if (error == ENETRESET) {
589 1.1 chs if (ifp->if_flags & IFF_RUNNING)
590 1.1 chs nfe_setmulti(sc);
591 1.1 chs error = 0;
592 1.1 chs }
593 1.1 chs break;
594 1.1 chs
595 1.1 chs }
596 1.1 chs
597 1.1 chs splx(s);
598 1.1 chs
599 1.1 chs return error;
600 1.1 chs }
601 1.1 chs
602 1.1 chs void
603 1.1 chs nfe_txdesc32_sync(struct nfe_softc *sc, struct nfe_desc32 *desc32, int ops)
604 1.1 chs {
605 1.1 chs bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
606 1.1 chs (caddr_t)desc32 - (caddr_t)sc->txq.desc32,
607 1.1 chs sizeof (struct nfe_desc32), ops);
608 1.1 chs }
609 1.1 chs
610 1.1 chs void
611 1.1 chs nfe_txdesc64_sync(struct nfe_softc *sc, struct nfe_desc64 *desc64, int ops)
612 1.1 chs {
613 1.1 chs bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
614 1.1 chs (caddr_t)desc64 - (caddr_t)sc->txq.desc64,
615 1.1 chs sizeof (struct nfe_desc64), ops);
616 1.1 chs }
617 1.1 chs
618 1.1 chs void
619 1.1 chs nfe_txdesc32_rsync(struct nfe_softc *sc, int start, int end, int ops)
620 1.1 chs {
621 1.1 chs if (end > start) {
622 1.1 chs bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
623 1.1 chs (caddr_t)&sc->txq.desc32[start] - (caddr_t)sc->txq.desc32,
624 1.1 chs (caddr_t)&sc->txq.desc32[end] -
625 1.1 chs (caddr_t)&sc->txq.desc32[start], ops);
626 1.1 chs return;
627 1.1 chs }
628 1.1 chs /* sync from 'start' to end of ring */
629 1.1 chs bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
630 1.1 chs (caddr_t)&sc->txq.desc32[start] - (caddr_t)sc->txq.desc32,
631 1.1 chs (caddr_t)&sc->txq.desc32[NFE_TX_RING_COUNT] -
632 1.1 chs (caddr_t)&sc->txq.desc32[start], ops);
633 1.1 chs
634 1.1 chs /* sync from start of ring to 'end' */
635 1.1 chs bus_dmamap_sync(sc->sc_dmat, sc->txq.map, 0,
636 1.1 chs (caddr_t)&sc->txq.desc32[end] - (caddr_t)sc->txq.desc32, ops);
637 1.1 chs }
638 1.1 chs
639 1.1 chs void
640 1.1 chs nfe_txdesc64_rsync(struct nfe_softc *sc, int start, int end, int ops)
641 1.1 chs {
642 1.1 chs if (end > start) {
643 1.1 chs bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
644 1.1 chs (caddr_t)&sc->txq.desc64[start] - (caddr_t)sc->txq.desc64,
645 1.1 chs (caddr_t)&sc->txq.desc64[end] -
646 1.1 chs (caddr_t)&sc->txq.desc64[start], ops);
647 1.1 chs return;
648 1.1 chs }
649 1.1 chs /* sync from 'start' to end of ring */
650 1.1 chs bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
651 1.1 chs (caddr_t)&sc->txq.desc64[start] - (caddr_t)sc->txq.desc64,
652 1.1 chs (caddr_t)&sc->txq.desc64[NFE_TX_RING_COUNT] -
653 1.1 chs (caddr_t)&sc->txq.desc64[start], ops);
654 1.1 chs
655 1.1 chs /* sync from start of ring to 'end' */
656 1.1 chs bus_dmamap_sync(sc->sc_dmat, sc->txq.map, 0,
657 1.1 chs (caddr_t)&sc->txq.desc64[end] - (caddr_t)sc->txq.desc64, ops);
658 1.1 chs }
659 1.1 chs
660 1.1 chs void
661 1.1 chs nfe_rxdesc32_sync(struct nfe_softc *sc, struct nfe_desc32 *desc32, int ops)
662 1.1 chs {
663 1.1 chs bus_dmamap_sync(sc->sc_dmat, sc->rxq.map,
664 1.1 chs (caddr_t)desc32 - (caddr_t)sc->rxq.desc32,
665 1.1 chs sizeof (struct nfe_desc32), ops);
666 1.1 chs }
667 1.1 chs
668 1.1 chs void
669 1.1 chs nfe_rxdesc64_sync(struct nfe_softc *sc, struct nfe_desc64 *desc64, int ops)
670 1.1 chs {
671 1.1 chs bus_dmamap_sync(sc->sc_dmat, sc->rxq.map,
672 1.1 chs (caddr_t)desc64 - (caddr_t)sc->rxq.desc64,
673 1.1 chs sizeof (struct nfe_desc64), ops);
674 1.1 chs }
675 1.1 chs
676 1.1 chs void
677 1.1 chs nfe_rxeof(struct nfe_softc *sc)
678 1.1 chs {
679 1.1 chs struct ifnet *ifp = &sc->sc_ethercom.ec_if;
680 1.1 chs struct nfe_desc32 *desc32;
681 1.1 chs struct nfe_desc64 *desc64;
682 1.1 chs struct nfe_rx_data *data;
683 1.1 chs struct nfe_jbuf *jbuf;
684 1.1 chs struct mbuf *m, *mnew;
685 1.1 chs bus_addr_t physaddr;
686 1.1 chs uint16_t flags;
687 1.1 chs int error, len;
688 1.1 chs
689 1.1 chs desc32 = NULL;
690 1.1 chs desc64 = NULL;
691 1.1 chs for (;;) {
692 1.1 chs data = &sc->rxq.data[sc->rxq.cur];
693 1.1 chs
694 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR) {
695 1.1 chs desc64 = &sc->rxq.desc64[sc->rxq.cur];
696 1.1 chs nfe_rxdesc64_sync(sc, desc64, BUS_DMASYNC_POSTREAD);
697 1.1 chs
698 1.1 chs flags = le16toh(desc64->flags);
699 1.1 chs len = le16toh(desc64->length) & 0x3fff;
700 1.1 chs } else {
701 1.1 chs desc32 = &sc->rxq.desc32[sc->rxq.cur];
702 1.1 chs nfe_rxdesc32_sync(sc, desc32, BUS_DMASYNC_POSTREAD);
703 1.1 chs
704 1.1 chs flags = le16toh(desc32->flags);
705 1.1 chs len = le16toh(desc32->length) & 0x3fff;
706 1.1 chs }
707 1.1 chs
708 1.1 chs if (flags & NFE_RX_READY)
709 1.1 chs break;
710 1.1 chs
711 1.1 chs if ((sc->sc_flags & (NFE_JUMBO_SUP | NFE_40BIT_ADDR)) == 0) {
712 1.1 chs if (!(flags & NFE_RX_VALID_V1))
713 1.1 chs goto skip;
714 1.1 chs
715 1.1 chs if ((flags & NFE_RX_FIXME_V1) == NFE_RX_FIXME_V1) {
716 1.1 chs flags &= ~NFE_RX_ERROR;
717 1.1 chs len--; /* fix buffer length */
718 1.1 chs }
719 1.1 chs } else {
720 1.1 chs if (!(flags & NFE_RX_VALID_V2))
721 1.1 chs goto skip;
722 1.1 chs
723 1.1 chs if ((flags & NFE_RX_FIXME_V2) == NFE_RX_FIXME_V2) {
724 1.1 chs flags &= ~NFE_RX_ERROR;
725 1.1 chs len--; /* fix buffer length */
726 1.1 chs }
727 1.1 chs }
728 1.1 chs
729 1.1 chs if (flags & NFE_RX_ERROR) {
730 1.1 chs ifp->if_ierrors++;
731 1.1 chs goto skip;
732 1.1 chs }
733 1.1 chs
734 1.1 chs /*
735 1.1 chs * Try to allocate a new mbuf for this ring element and load
736 1.1 chs * it before processing the current mbuf. If the ring element
737 1.1 chs * cannot be loaded, drop the received packet and reuse the
738 1.1 chs * old mbuf. In the unlikely case that the old mbuf can't be
739 1.1 chs * reloaded either, explicitly panic.
740 1.1 chs */
741 1.1 chs MGETHDR(mnew, M_DONTWAIT, MT_DATA);
742 1.1 chs if (mnew == NULL) {
743 1.1 chs ifp->if_ierrors++;
744 1.1 chs goto skip;
745 1.1 chs }
746 1.1 chs
747 1.1 chs if (sc->sc_flags & NFE_USE_JUMBO) {
748 1.1 chs if ((jbuf = nfe_jalloc(sc)) == NULL) {
749 1.1 chs m_freem(mnew);
750 1.1 chs ifp->if_ierrors++;
751 1.1 chs goto skip;
752 1.1 chs }
753 1.1 chs MEXTADD(mnew, jbuf->buf, NFE_JBYTES, 0, nfe_jfree, sc);
754 1.1 chs
755 1.1 chs bus_dmamap_sync(sc->sc_dmat, sc->rxq.jmap,
756 1.1 chs mtod(data->m, caddr_t) - sc->rxq.jpool, NFE_JBYTES,
757 1.1 chs BUS_DMASYNC_POSTREAD);
758 1.1 chs
759 1.1 chs physaddr = jbuf->physaddr;
760 1.1 chs } else {
761 1.1 chs MCLGET(mnew, M_DONTWAIT);
762 1.1 chs if (!(mnew->m_flags & M_EXT)) {
763 1.1 chs m_freem(mnew);
764 1.1 chs ifp->if_ierrors++;
765 1.1 chs goto skip;
766 1.1 chs }
767 1.1 chs
768 1.1 chs bus_dmamap_sync(sc->sc_dmat, data->map, 0,
769 1.1 chs data->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
770 1.1 chs bus_dmamap_unload(sc->sc_dmat, data->map);
771 1.1 chs
772 1.1 chs error = bus_dmamap_load(sc->sc_dmat, data->map,
773 1.1 chs mtod(mnew, void *), MCLBYTES, NULL,
774 1.1 chs BUS_DMA_READ | BUS_DMA_NOWAIT);
775 1.1 chs if (error != 0) {
776 1.1 chs m_freem(mnew);
777 1.1 chs
778 1.1 chs /* try to reload the old mbuf */
779 1.1 chs error = bus_dmamap_load(sc->sc_dmat, data->map,
780 1.1 chs mtod(data->m, void *), MCLBYTES, NULL,
781 1.1 chs BUS_DMA_READ | BUS_DMA_NOWAIT);
782 1.1 chs if (error != 0) {
783 1.1 chs /* very unlikely that it will fail.. */
784 1.1 chs panic("%s: could not load old rx mbuf",
785 1.1 chs sc->sc_dev.dv_xname);
786 1.1 chs }
787 1.1 chs ifp->if_ierrors++;
788 1.1 chs goto skip;
789 1.1 chs }
790 1.1 chs physaddr = data->map->dm_segs[0].ds_addr;
791 1.1 chs }
792 1.1 chs
793 1.1 chs /*
794 1.1 chs * New mbuf successfully loaded, update Rx ring and continue
795 1.1 chs * processing.
796 1.1 chs */
797 1.1 chs m = data->m;
798 1.1 chs data->m = mnew;
799 1.1 chs
800 1.1 chs /* finalize mbuf */
801 1.1 chs m->m_pkthdr.len = m->m_len = len;
802 1.1 chs m->m_pkthdr.rcvif = ifp;
803 1.1 chs
804 1.1 chs #ifdef notyet
805 1.1 chs if (sc->sc_flags & NFE_HW_CSUM) {
806 1.1 chs if (flags & NFE_RX_IP_CSUMOK)
807 1.1 chs m->m_pkthdr.csum_flags |= M_IPV4_CSUM_IN_OK;
808 1.1 chs if (flags & NFE_RX_UDP_CSUMOK)
809 1.1 chs m->m_pkthdr.csum_flags |= M_UDP_CSUM_IN_OK;
810 1.1 chs if (flags & NFE_RX_TCP_CSUMOK)
811 1.1 chs m->m_pkthdr.csum_flags |= M_TCP_CSUM_IN_OK;
812 1.1 chs }
813 1.1 chs #elif defined(NFE_CSUM)
814 1.1 chs if ((sc->sc_flags & NFE_HW_CSUM) && (flags & NFE_RX_CSUMOK))
815 1.1 chs m->m_pkthdr.csum_flags = M_IPV4_CSUM_IN_OK;
816 1.1 chs #endif
817 1.1 chs
818 1.1 chs #if NBPFILTER > 0
819 1.1 chs if (ifp->if_bpf)
820 1.1 chs bpf_mtap(ifp->if_bpf, m);
821 1.1 chs #endif
822 1.1 chs ifp->if_ipackets++;
823 1.1 chs (*ifp->if_input)(ifp, m);
824 1.1 chs
825 1.1 chs /* update mapping address in h/w descriptor */
826 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR) {
827 1.1 chs #if defined(__LP64__)
828 1.1 chs desc64->physaddr[0] = htole32(physaddr >> 32);
829 1.1 chs #endif
830 1.1 chs desc64->physaddr[1] = htole32(physaddr & 0xffffffff);
831 1.1 chs } else {
832 1.1 chs desc32->physaddr = htole32(physaddr);
833 1.1 chs }
834 1.1 chs
835 1.1 chs skip: if (sc->sc_flags & NFE_40BIT_ADDR) {
836 1.1 chs desc64->length = htole16(sc->rxq.bufsz);
837 1.1 chs desc64->flags = htole16(NFE_RX_READY);
838 1.1 chs
839 1.1 chs nfe_rxdesc64_sync(sc, desc64, BUS_DMASYNC_PREWRITE);
840 1.1 chs } else {
841 1.1 chs desc32->length = htole16(sc->rxq.bufsz);
842 1.1 chs desc32->flags = htole16(NFE_RX_READY);
843 1.1 chs
844 1.1 chs nfe_rxdesc32_sync(sc, desc32, BUS_DMASYNC_PREWRITE);
845 1.1 chs }
846 1.1 chs
847 1.1 chs sc->rxq.cur = (sc->rxq.cur + 1) % NFE_RX_RING_COUNT;
848 1.1 chs }
849 1.1 chs }
850 1.1 chs
851 1.1 chs void
852 1.1 chs nfe_txeof(struct nfe_softc *sc)
853 1.1 chs {
854 1.1 chs struct ifnet *ifp = &sc->sc_ethercom.ec_if;
855 1.1 chs struct nfe_desc32 *desc32;
856 1.1 chs struct nfe_desc64 *desc64;
857 1.1 chs struct nfe_tx_data *data = NULL;
858 1.1 chs uint16_t flags;
859 1.1 chs
860 1.1 chs while (sc->txq.next != sc->txq.cur) {
861 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR) {
862 1.1 chs desc64 = &sc->txq.desc64[sc->txq.next];
863 1.1 chs nfe_txdesc64_sync(sc, desc64, BUS_DMASYNC_POSTREAD);
864 1.1 chs
865 1.1 chs flags = le16toh(desc64->flags);
866 1.1 chs } else {
867 1.1 chs desc32 = &sc->txq.desc32[sc->txq.next];
868 1.1 chs nfe_txdesc32_sync(sc, desc32, BUS_DMASYNC_POSTREAD);
869 1.1 chs
870 1.1 chs flags = le16toh(desc32->flags);
871 1.1 chs }
872 1.1 chs
873 1.1 chs if (flags & NFE_TX_VALID)
874 1.1 chs break;
875 1.1 chs
876 1.1 chs data = &sc->txq.data[sc->txq.next];
877 1.1 chs
878 1.1 chs if ((sc->sc_flags & (NFE_JUMBO_SUP | NFE_40BIT_ADDR)) == 0) {
879 1.3 chs if (!(flags & NFE_TX_LASTFRAG_V1) && data->m == NULL)
880 1.1 chs goto skip;
881 1.1 chs
882 1.1 chs if ((flags & NFE_TX_ERROR_V1) != 0) {
883 1.1 chs printf("%s: tx v1 error 0x%04x\n",
884 1.1 chs sc->sc_dev.dv_xname, flags);
885 1.1 chs ifp->if_oerrors++;
886 1.1 chs } else
887 1.1 chs ifp->if_opackets++;
888 1.1 chs } else {
889 1.3 chs if (!(flags & NFE_TX_LASTFRAG_V2) && data->m == NULL)
890 1.1 chs goto skip;
891 1.1 chs
892 1.1 chs if ((flags & NFE_TX_ERROR_V2) != 0) {
893 1.1 chs printf("%s: tx v2 error 0x%04x\n",
894 1.1 chs sc->sc_dev.dv_xname, flags);
895 1.1 chs ifp->if_oerrors++;
896 1.1 chs } else
897 1.1 chs ifp->if_opackets++;
898 1.1 chs }
899 1.1 chs
900 1.1 chs if (data->m == NULL) { /* should not get there */
901 1.1 chs printf("%s: last fragment bit w/o associated mbuf!\n",
902 1.1 chs sc->sc_dev.dv_xname);
903 1.1 chs goto skip;
904 1.1 chs }
905 1.1 chs
906 1.1 chs /* last fragment of the mbuf chain transmitted */
907 1.1 chs bus_dmamap_sync(sc->sc_dmat, data->active, 0,
908 1.1 chs data->active->dm_mapsize, BUS_DMASYNC_POSTWRITE);
909 1.1 chs bus_dmamap_unload(sc->sc_dmat, data->active);
910 1.1 chs m_freem(data->m);
911 1.1 chs data->m = NULL;
912 1.1 chs
913 1.1 chs ifp->if_timer = 0;
914 1.1 chs
915 1.1 chs skip: sc->txq.queued--;
916 1.1 chs sc->txq.next = (sc->txq.next + 1) % NFE_TX_RING_COUNT;
917 1.1 chs }
918 1.1 chs
919 1.1 chs if (data != NULL) { /* at least one slot freed */
920 1.1 chs ifp->if_flags &= ~IFF_OACTIVE;
921 1.1 chs nfe_start(ifp);
922 1.1 chs }
923 1.1 chs }
924 1.1 chs
925 1.1 chs int
926 1.1 chs nfe_encap(struct nfe_softc *sc, struct mbuf *m0)
927 1.1 chs {
928 1.1 chs struct nfe_desc32 *desc32;
929 1.1 chs struct nfe_desc64 *desc64;
930 1.1 chs struct nfe_tx_data *data;
931 1.1 chs bus_dmamap_t map;
932 1.11 tsutsui uint16_t flags;
933 1.1 chs #if NVLAN > 0
934 1.1 chs struct m_tag *mtag;
935 1.1 chs uint32_t vtag = 0;
936 1.1 chs #endif
937 1.11 tsutsui int error, i, first;
938 1.1 chs
939 1.1 chs desc32 = NULL;
940 1.1 chs desc64 = NULL;
941 1.1 chs data = NULL;
942 1.11 tsutsui
943 1.11 tsutsui flags = 0;
944 1.11 tsutsui first = sc->txq.cur;
945 1.11 tsutsui
946 1.11 tsutsui map = sc->txq.data[first].map;
947 1.1 chs
948 1.1 chs error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m0, BUS_DMA_NOWAIT);
949 1.1 chs if (error != 0) {
950 1.1 chs printf("%s: could not map mbuf (error %d)\n",
951 1.1 chs sc->sc_dev.dv_xname, error);
952 1.1 chs return error;
953 1.1 chs }
954 1.1 chs
955 1.1 chs if (sc->txq.queued + map->dm_nsegs >= NFE_TX_RING_COUNT - 1) {
956 1.1 chs bus_dmamap_unload(sc->sc_dmat, map);
957 1.1 chs return ENOBUFS;
958 1.1 chs }
959 1.1 chs
960 1.1 chs #if NVLAN > 0
961 1.1 chs /* setup h/w VLAN tagging */
962 1.9 alc if ((mtag = VLAN_OUTPUT_TAG(&sc->sc_ethercom, m0)) != NULL)
963 1.1 chs vtag = NFE_TX_VTAG | VLAN_TAG_VALUE(mtag);
964 1.1 chs #endif
965 1.1 chs #ifdef NFE_CSUM
966 1.1 chs if (m0->m_pkthdr.csum_flags & M_IPV4_CSUM_OUT)
967 1.1 chs flags |= NFE_TX_IP_CSUM;
968 1.1 chs if (m0->m_pkthdr.csum_flags & (M_TCPV4_CSUM_OUT | M_UDPV4_CSUM_OUT))
969 1.1 chs flags |= NFE_TX_TCP_CSUM;
970 1.1 chs #endif
971 1.1 chs
972 1.1 chs for (i = 0; i < map->dm_nsegs; i++) {
973 1.1 chs data = &sc->txq.data[sc->txq.cur];
974 1.1 chs
975 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR) {
976 1.1 chs desc64 = &sc->txq.desc64[sc->txq.cur];
977 1.1 chs #if defined(__LP64__)
978 1.1 chs desc64->physaddr[0] =
979 1.1 chs htole32(map->dm_segs[i].ds_addr >> 32);
980 1.1 chs #endif
981 1.1 chs desc64->physaddr[1] =
982 1.1 chs htole32(map->dm_segs[i].ds_addr & 0xffffffff);
983 1.1 chs desc64->length = htole16(map->dm_segs[i].ds_len - 1);
984 1.1 chs desc64->flags = htole16(flags);
985 1.1 chs #if NVLAN > 0
986 1.1 chs desc64->vtag = htole32(vtag);
987 1.1 chs #endif
988 1.1 chs } else {
989 1.1 chs desc32 = &sc->txq.desc32[sc->txq.cur];
990 1.1 chs
991 1.1 chs desc32->physaddr = htole32(map->dm_segs[i].ds_addr);
992 1.1 chs desc32->length = htole16(map->dm_segs[i].ds_len - 1);
993 1.1 chs desc32->flags = htole16(flags);
994 1.1 chs }
995 1.1 chs
996 1.1 chs if (map->dm_nsegs > 1) {
997 1.11 tsutsui /*
998 1.11 tsutsui * Checksum flags and vtag belong to the first fragment
999 1.11 tsutsui * only.
1000 1.11 tsutsui */
1001 1.1 chs flags &= ~(NFE_TX_IP_CSUM | NFE_TX_TCP_CSUM);
1002 1.1 chs #if NVLAN > 0
1003 1.1 chs vtag = 0;
1004 1.1 chs #endif
1005 1.11 tsutsui /*
1006 1.11 tsutsui * Setting of the valid bit in the first descriptor is
1007 1.11 tsutsui * deferred until the whole chain is fully setup.
1008 1.11 tsutsui */
1009 1.11 tsutsui flags |= NFE_TX_VALID;
1010 1.1 chs }
1011 1.1 chs
1012 1.1 chs sc->txq.queued++;
1013 1.1 chs sc->txq.cur = (sc->txq.cur + 1) % NFE_TX_RING_COUNT;
1014 1.1 chs }
1015 1.1 chs
1016 1.11 tsutsui /* the whole mbuf chain has been setup */
1017 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR) {
1018 1.11 tsutsui /* fix last descriptor */
1019 1.1 chs flags |= NFE_TX_LASTFRAG_V2;
1020 1.1 chs desc64->flags = htole16(flags);
1021 1.11 tsutsui
1022 1.11 tsutsui /* finally, set the valid bit in the first descriptor */
1023 1.11 tsutsui sc->txq.desc64[first].flags |= htole16(NFE_TX_VALID);
1024 1.1 chs } else {
1025 1.11 tsutsui /* fix last descriptor */
1026 1.1 chs if (sc->sc_flags & NFE_JUMBO_SUP)
1027 1.1 chs flags |= NFE_TX_LASTFRAG_V2;
1028 1.1 chs else
1029 1.1 chs flags |= NFE_TX_LASTFRAG_V1;
1030 1.1 chs desc32->flags = htole16(flags);
1031 1.11 tsutsui
1032 1.11 tsutsui /* finally, set the valid bit in the first descriptor */
1033 1.11 tsutsui sc->txq.desc32[first].flags |= htole16(NFE_TX_VALID);
1034 1.1 chs }
1035 1.1 chs
1036 1.1 chs data->m = m0;
1037 1.1 chs data->active = map;
1038 1.1 chs
1039 1.1 chs bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1040 1.1 chs BUS_DMASYNC_PREWRITE);
1041 1.1 chs
1042 1.1 chs return 0;
1043 1.1 chs }
1044 1.1 chs
1045 1.1 chs void
1046 1.1 chs nfe_start(struct ifnet *ifp)
1047 1.1 chs {
1048 1.1 chs struct nfe_softc *sc = ifp->if_softc;
1049 1.1 chs int old = sc->txq.cur;
1050 1.1 chs struct mbuf *m0;
1051 1.1 chs
1052 1.1 chs for (;;) {
1053 1.1 chs IFQ_POLL(&ifp->if_snd, m0);
1054 1.1 chs if (m0 == NULL)
1055 1.1 chs break;
1056 1.1 chs
1057 1.1 chs if (nfe_encap(sc, m0) != 0) {
1058 1.1 chs ifp->if_flags |= IFF_OACTIVE;
1059 1.1 chs break;
1060 1.1 chs }
1061 1.1 chs
1062 1.1 chs /* packet put in h/w queue, remove from s/w queue */
1063 1.1 chs IFQ_DEQUEUE(&ifp->if_snd, m0);
1064 1.1 chs
1065 1.1 chs #if NBPFILTER > 0
1066 1.1 chs if (ifp->if_bpf != NULL)
1067 1.1 chs bpf_mtap(ifp->if_bpf, m0);
1068 1.1 chs #endif
1069 1.1 chs }
1070 1.1 chs if (sc->txq.cur == old) /* nothing sent */
1071 1.1 chs return;
1072 1.1 chs
1073 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR)
1074 1.1 chs nfe_txdesc64_rsync(sc, old, sc->txq.cur, BUS_DMASYNC_PREWRITE);
1075 1.1 chs else
1076 1.1 chs nfe_txdesc32_rsync(sc, old, sc->txq.cur, BUS_DMASYNC_PREWRITE);
1077 1.1 chs
1078 1.1 chs /* kick Tx */
1079 1.1 chs NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_KICKTX | sc->rxtxctl);
1080 1.1 chs
1081 1.1 chs /*
1082 1.1 chs * Set a timeout in case the chip goes out to lunch.
1083 1.1 chs */
1084 1.1 chs ifp->if_timer = 5;
1085 1.1 chs }
1086 1.1 chs
1087 1.1 chs void
1088 1.1 chs nfe_watchdog(struct ifnet *ifp)
1089 1.1 chs {
1090 1.1 chs struct nfe_softc *sc = ifp->if_softc;
1091 1.1 chs
1092 1.1 chs printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
1093 1.1 chs
1094 1.1 chs ifp->if_flags &= ~IFF_RUNNING;
1095 1.1 chs nfe_init(ifp);
1096 1.1 chs
1097 1.1 chs ifp->if_oerrors++;
1098 1.1 chs }
1099 1.1 chs
1100 1.1 chs int
1101 1.1 chs nfe_init(struct ifnet *ifp)
1102 1.1 chs {
1103 1.1 chs struct nfe_softc *sc = ifp->if_softc;
1104 1.1 chs uint32_t tmp;
1105 1.1 chs
1106 1.1 chs if (ifp->if_flags & IFF_RUNNING)
1107 1.1 chs return 0;
1108 1.1 chs
1109 1.1 chs nfe_stop(ifp, 0);
1110 1.1 chs
1111 1.1 chs NFE_WRITE(sc, NFE_TX_UNK, 0);
1112 1.1 chs NFE_WRITE(sc, NFE_STATUS, 0);
1113 1.1 chs
1114 1.1 chs sc->rxtxctl = NFE_RXTX_BIT2;
1115 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR)
1116 1.1 chs sc->rxtxctl |= NFE_RXTX_V3MAGIC;
1117 1.1 chs else if (sc->sc_flags & NFE_JUMBO_SUP)
1118 1.1 chs sc->rxtxctl |= NFE_RXTX_V2MAGIC;
1119 1.1 chs #ifdef NFE_CSUM
1120 1.1 chs if (sc->sc_flags & NFE_HW_CSUM)
1121 1.1 chs sc->rxtxctl |= NFE_RXTX_RXCSUM;
1122 1.1 chs #endif
1123 1.1 chs #if NVLAN > 0
1124 1.1 chs /*
1125 1.1 chs * Although the adapter is capable of stripping VLAN tags from received
1126 1.1 chs * frames (NFE_RXTX_VTAG_STRIP), we do not enable this functionality on
1127 1.1 chs * purpose. This will be done in software by our network stack.
1128 1.1 chs */
1129 1.1 chs if (sc->sc_flags & NFE_HW_VLAN)
1130 1.1 chs sc->rxtxctl |= NFE_RXTX_VTAG_INSERT;
1131 1.1 chs #endif
1132 1.1 chs NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_RESET | sc->rxtxctl);
1133 1.1 chs DELAY(10);
1134 1.1 chs NFE_WRITE(sc, NFE_RXTX_CTL, sc->rxtxctl);
1135 1.1 chs
1136 1.1 chs #if NVLAN
1137 1.1 chs if (sc->sc_flags & NFE_HW_VLAN)
1138 1.1 chs NFE_WRITE(sc, NFE_VTAG_CTL, NFE_VTAG_ENABLE);
1139 1.1 chs #endif
1140 1.1 chs
1141 1.1 chs NFE_WRITE(sc, NFE_SETUP_R6, 0);
1142 1.1 chs
1143 1.1 chs /* set MAC address */
1144 1.1 chs nfe_set_macaddr(sc, sc->sc_enaddr);
1145 1.1 chs
1146 1.1 chs /* tell MAC where rings are in memory */
1147 1.1 chs #ifdef __LP64__
1148 1.1 chs NFE_WRITE(sc, NFE_RX_RING_ADDR_HI, sc->rxq.physaddr >> 32);
1149 1.1 chs #endif
1150 1.1 chs NFE_WRITE(sc, NFE_RX_RING_ADDR_LO, sc->rxq.physaddr & 0xffffffff);
1151 1.1 chs #ifdef __LP64__
1152 1.1 chs NFE_WRITE(sc, NFE_TX_RING_ADDR_HI, sc->txq.physaddr >> 32);
1153 1.1 chs #endif
1154 1.1 chs NFE_WRITE(sc, NFE_TX_RING_ADDR_LO, sc->txq.physaddr & 0xffffffff);
1155 1.1 chs
1156 1.1 chs NFE_WRITE(sc, NFE_RING_SIZE,
1157 1.1 chs (NFE_RX_RING_COUNT - 1) << 16 |
1158 1.1 chs (NFE_TX_RING_COUNT - 1));
1159 1.1 chs
1160 1.1 chs NFE_WRITE(sc, NFE_RXBUFSZ, sc->rxq.bufsz);
1161 1.1 chs
1162 1.1 chs /* force MAC to wakeup */
1163 1.1 chs tmp = NFE_READ(sc, NFE_PWR_STATE);
1164 1.1 chs NFE_WRITE(sc, NFE_PWR_STATE, tmp | NFE_PWR_WAKEUP);
1165 1.1 chs DELAY(10);
1166 1.1 chs tmp = NFE_READ(sc, NFE_PWR_STATE);
1167 1.1 chs NFE_WRITE(sc, NFE_PWR_STATE, tmp | NFE_PWR_VALID);
1168 1.1 chs
1169 1.1 chs #if 1
1170 1.1 chs /* configure interrupts coalescing/mitigation */
1171 1.1 chs NFE_WRITE(sc, NFE_IMTIMER, NFE_IM_DEFAULT);
1172 1.1 chs #else
1173 1.1 chs /* no interrupt mitigation: one interrupt per packet */
1174 1.1 chs NFE_WRITE(sc, NFE_IMTIMER, 970);
1175 1.1 chs #endif
1176 1.1 chs
1177 1.1 chs NFE_WRITE(sc, NFE_SETUP_R1, NFE_R1_MAGIC);
1178 1.1 chs NFE_WRITE(sc, NFE_SETUP_R2, NFE_R2_MAGIC);
1179 1.1 chs NFE_WRITE(sc, NFE_SETUP_R6, NFE_R6_MAGIC);
1180 1.1 chs
1181 1.1 chs /* update MAC knowledge of PHY; generates a NFE_IRQ_LINK interrupt */
1182 1.1 chs NFE_WRITE(sc, NFE_STATUS, sc->mii_phyaddr << 24 | NFE_STATUS_MAGIC);
1183 1.1 chs
1184 1.1 chs NFE_WRITE(sc, NFE_SETUP_R4, NFE_R4_MAGIC);
1185 1.1 chs NFE_WRITE(sc, NFE_WOL_CTL, NFE_WOL_MAGIC);
1186 1.1 chs
1187 1.1 chs sc->rxtxctl &= ~NFE_RXTX_BIT2;
1188 1.1 chs NFE_WRITE(sc, NFE_RXTX_CTL, sc->rxtxctl);
1189 1.1 chs DELAY(10);
1190 1.1 chs NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_BIT1 | sc->rxtxctl);
1191 1.1 chs
1192 1.1 chs /* set Rx filter */
1193 1.1 chs nfe_setmulti(sc);
1194 1.1 chs
1195 1.1 chs nfe_ifmedia_upd(ifp);
1196 1.1 chs
1197 1.1 chs /* enable Rx */
1198 1.1 chs NFE_WRITE(sc, NFE_RX_CTL, NFE_RX_START);
1199 1.1 chs
1200 1.1 chs /* enable Tx */
1201 1.1 chs NFE_WRITE(sc, NFE_TX_CTL, NFE_TX_START);
1202 1.1 chs
1203 1.1 chs NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
1204 1.1 chs
1205 1.1 chs /* enable interrupts */
1206 1.1 chs NFE_WRITE(sc, NFE_IRQ_MASK, NFE_IRQ_WANTED);
1207 1.1 chs
1208 1.1 chs callout_schedule(&sc->sc_tick_ch, hz);
1209 1.1 chs
1210 1.1 chs ifp->if_flags |= IFF_RUNNING;
1211 1.1 chs ifp->if_flags &= ~IFF_OACTIVE;
1212 1.1 chs
1213 1.1 chs return 0;
1214 1.1 chs }
1215 1.1 chs
1216 1.1 chs void
1217 1.7 christos nfe_stop(struct ifnet *ifp, int disable)
1218 1.1 chs {
1219 1.1 chs struct nfe_softc *sc = ifp->if_softc;
1220 1.1 chs
1221 1.1 chs callout_stop(&sc->sc_tick_ch);
1222 1.1 chs
1223 1.1 chs ifp->if_timer = 0;
1224 1.1 chs ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1225 1.1 chs
1226 1.1 chs mii_down(&sc->sc_mii);
1227 1.1 chs
1228 1.1 chs /* abort Tx */
1229 1.1 chs NFE_WRITE(sc, NFE_TX_CTL, 0);
1230 1.1 chs
1231 1.1 chs /* disable Rx */
1232 1.1 chs NFE_WRITE(sc, NFE_RX_CTL, 0);
1233 1.1 chs
1234 1.1 chs /* disable interrupts */
1235 1.1 chs NFE_WRITE(sc, NFE_IRQ_MASK, 0);
1236 1.1 chs
1237 1.1 chs /* reset Tx and Rx rings */
1238 1.1 chs nfe_reset_tx_ring(sc, &sc->txq);
1239 1.1 chs nfe_reset_rx_ring(sc, &sc->rxq);
1240 1.1 chs }
1241 1.1 chs
1242 1.1 chs int
1243 1.1 chs nfe_alloc_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1244 1.1 chs {
1245 1.1 chs struct nfe_desc32 *desc32;
1246 1.1 chs struct nfe_desc64 *desc64;
1247 1.1 chs struct nfe_rx_data *data;
1248 1.1 chs struct nfe_jbuf *jbuf;
1249 1.1 chs void **desc;
1250 1.1 chs bus_addr_t physaddr;
1251 1.1 chs int i, nsegs, error, descsize;
1252 1.1 chs
1253 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR) {
1254 1.1 chs desc = (void **)&ring->desc64;
1255 1.1 chs descsize = sizeof (struct nfe_desc64);
1256 1.1 chs } else {
1257 1.1 chs desc = (void **)&ring->desc32;
1258 1.1 chs descsize = sizeof (struct nfe_desc32);
1259 1.1 chs }
1260 1.1 chs
1261 1.1 chs ring->cur = ring->next = 0;
1262 1.1 chs ring->bufsz = MCLBYTES;
1263 1.1 chs
1264 1.1 chs error = bus_dmamap_create(sc->sc_dmat, NFE_RX_RING_COUNT * descsize, 1,
1265 1.1 chs NFE_RX_RING_COUNT * descsize, 0, BUS_DMA_NOWAIT, &ring->map);
1266 1.1 chs if (error != 0) {
1267 1.1 chs printf("%s: could not create desc DMA map\n",
1268 1.1 chs sc->sc_dev.dv_xname);
1269 1.1 chs goto fail;
1270 1.1 chs }
1271 1.1 chs
1272 1.1 chs error = bus_dmamem_alloc(sc->sc_dmat, NFE_RX_RING_COUNT * descsize,
1273 1.1 chs PAGE_SIZE, 0, &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT);
1274 1.1 chs if (error != 0) {
1275 1.1 chs printf("%s: could not allocate DMA memory\n",
1276 1.1 chs sc->sc_dev.dv_xname);
1277 1.1 chs goto fail;
1278 1.1 chs }
1279 1.1 chs
1280 1.1 chs error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs,
1281 1.1 chs NFE_RX_RING_COUNT * descsize, (caddr_t *)desc, BUS_DMA_NOWAIT);
1282 1.1 chs if (error != 0) {
1283 1.1 chs printf("%s: could not map desc DMA memory\n",
1284 1.1 chs sc->sc_dev.dv_xname);
1285 1.1 chs goto fail;
1286 1.1 chs }
1287 1.1 chs
1288 1.1 chs error = bus_dmamap_load(sc->sc_dmat, ring->map, *desc,
1289 1.1 chs NFE_RX_RING_COUNT * descsize, NULL, BUS_DMA_NOWAIT);
1290 1.1 chs if (error != 0) {
1291 1.1 chs printf("%s: could not load desc DMA map\n",
1292 1.1 chs sc->sc_dev.dv_xname);
1293 1.1 chs goto fail;
1294 1.1 chs }
1295 1.1 chs
1296 1.1 chs bzero(*desc, NFE_RX_RING_COUNT * descsize);
1297 1.1 chs ring->physaddr = ring->map->dm_segs[0].ds_addr;
1298 1.1 chs
1299 1.1 chs if (sc->sc_flags & NFE_USE_JUMBO) {
1300 1.1 chs ring->bufsz = NFE_JBYTES;
1301 1.1 chs if ((error = nfe_jpool_alloc(sc)) != 0) {
1302 1.1 chs printf("%s: could not allocate jumbo frames\n",
1303 1.1 chs sc->sc_dev.dv_xname);
1304 1.1 chs goto fail;
1305 1.1 chs }
1306 1.1 chs }
1307 1.1 chs
1308 1.1 chs /*
1309 1.1 chs * Pre-allocate Rx buffers and populate Rx ring.
1310 1.1 chs */
1311 1.1 chs for (i = 0; i < NFE_RX_RING_COUNT; i++) {
1312 1.1 chs data = &sc->rxq.data[i];
1313 1.1 chs
1314 1.1 chs MGETHDR(data->m, M_DONTWAIT, MT_DATA);
1315 1.1 chs if (data->m == NULL) {
1316 1.1 chs printf("%s: could not allocate rx mbuf\n",
1317 1.1 chs sc->sc_dev.dv_xname);
1318 1.1 chs error = ENOMEM;
1319 1.1 chs goto fail;
1320 1.1 chs }
1321 1.1 chs
1322 1.1 chs if (sc->sc_flags & NFE_USE_JUMBO) {
1323 1.1 chs if ((jbuf = nfe_jalloc(sc)) == NULL) {
1324 1.1 chs printf("%s: could not allocate jumbo buffer\n",
1325 1.1 chs sc->sc_dev.dv_xname);
1326 1.1 chs goto fail;
1327 1.1 chs }
1328 1.1 chs MEXTADD(data->m, jbuf->buf, NFE_JBYTES, 0, nfe_jfree,
1329 1.1 chs sc);
1330 1.1 chs
1331 1.1 chs physaddr = jbuf->physaddr;
1332 1.1 chs } else {
1333 1.1 chs error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
1334 1.1 chs MCLBYTES, 0, BUS_DMA_NOWAIT, &data->map);
1335 1.1 chs if (error != 0) {
1336 1.1 chs printf("%s: could not create DMA map\n",
1337 1.1 chs sc->sc_dev.dv_xname);
1338 1.1 chs goto fail;
1339 1.1 chs }
1340 1.1 chs MCLGET(data->m, M_DONTWAIT);
1341 1.1 chs if (!(data->m->m_flags & M_EXT)) {
1342 1.1 chs printf("%s: could not allocate mbuf cluster\n",
1343 1.1 chs sc->sc_dev.dv_xname);
1344 1.1 chs error = ENOMEM;
1345 1.1 chs goto fail;
1346 1.1 chs }
1347 1.1 chs
1348 1.1 chs error = bus_dmamap_load(sc->sc_dmat, data->map,
1349 1.1 chs mtod(data->m, void *), MCLBYTES, NULL,
1350 1.1 chs BUS_DMA_READ | BUS_DMA_NOWAIT);
1351 1.1 chs if (error != 0) {
1352 1.1 chs printf("%s: could not load rx buf DMA map",
1353 1.1 chs sc->sc_dev.dv_xname);
1354 1.1 chs goto fail;
1355 1.1 chs }
1356 1.1 chs physaddr = data->map->dm_segs[0].ds_addr;
1357 1.1 chs }
1358 1.1 chs
1359 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR) {
1360 1.1 chs desc64 = &sc->rxq.desc64[i];
1361 1.1 chs #if defined(__LP64__)
1362 1.1 chs desc64->physaddr[0] = htole32(physaddr >> 32);
1363 1.1 chs #endif
1364 1.1 chs desc64->physaddr[1] = htole32(physaddr & 0xffffffff);
1365 1.1 chs desc64->length = htole16(sc->rxq.bufsz);
1366 1.1 chs desc64->flags = htole16(NFE_RX_READY);
1367 1.1 chs } else {
1368 1.1 chs desc32 = &sc->rxq.desc32[i];
1369 1.1 chs desc32->physaddr = htole32(physaddr);
1370 1.1 chs desc32->length = htole16(sc->rxq.bufsz);
1371 1.1 chs desc32->flags = htole16(NFE_RX_READY);
1372 1.1 chs }
1373 1.1 chs }
1374 1.1 chs
1375 1.1 chs bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize,
1376 1.1 chs BUS_DMASYNC_PREWRITE);
1377 1.1 chs
1378 1.1 chs return 0;
1379 1.1 chs
1380 1.1 chs fail: nfe_free_rx_ring(sc, ring);
1381 1.1 chs return error;
1382 1.1 chs }
1383 1.1 chs
1384 1.1 chs void
1385 1.1 chs nfe_reset_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1386 1.1 chs {
1387 1.1 chs int i;
1388 1.1 chs
1389 1.1 chs for (i = 0; i < NFE_RX_RING_COUNT; i++) {
1390 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR) {
1391 1.1 chs ring->desc64[i].length = htole16(ring->bufsz);
1392 1.1 chs ring->desc64[i].flags = htole16(NFE_RX_READY);
1393 1.1 chs } else {
1394 1.1 chs ring->desc32[i].length = htole16(ring->bufsz);
1395 1.1 chs ring->desc32[i].flags = htole16(NFE_RX_READY);
1396 1.1 chs }
1397 1.1 chs }
1398 1.1 chs
1399 1.1 chs bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize,
1400 1.1 chs BUS_DMASYNC_PREWRITE);
1401 1.1 chs
1402 1.1 chs ring->cur = ring->next = 0;
1403 1.1 chs }
1404 1.1 chs
1405 1.1 chs void
1406 1.1 chs nfe_free_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1407 1.1 chs {
1408 1.1 chs struct nfe_rx_data *data;
1409 1.1 chs void *desc;
1410 1.1 chs int i, descsize;
1411 1.1 chs
1412 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR) {
1413 1.1 chs desc = ring->desc64;
1414 1.1 chs descsize = sizeof (struct nfe_desc64);
1415 1.1 chs } else {
1416 1.1 chs desc = ring->desc32;
1417 1.1 chs descsize = sizeof (struct nfe_desc32);
1418 1.1 chs }
1419 1.1 chs
1420 1.1 chs if (desc != NULL) {
1421 1.1 chs bus_dmamap_sync(sc->sc_dmat, ring->map, 0,
1422 1.1 chs ring->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1423 1.1 chs bus_dmamap_unload(sc->sc_dmat, ring->map);
1424 1.1 chs bus_dmamem_unmap(sc->sc_dmat, (caddr_t)desc,
1425 1.1 chs NFE_RX_RING_COUNT * descsize);
1426 1.1 chs bus_dmamem_free(sc->sc_dmat, &ring->seg, 1);
1427 1.1 chs }
1428 1.1 chs
1429 1.1 chs for (i = 0; i < NFE_RX_RING_COUNT; i++) {
1430 1.1 chs data = &ring->data[i];
1431 1.1 chs
1432 1.1 chs if (data->map != NULL) {
1433 1.1 chs bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1434 1.1 chs data->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
1435 1.1 chs bus_dmamap_unload(sc->sc_dmat, data->map);
1436 1.1 chs bus_dmamap_destroy(sc->sc_dmat, data->map);
1437 1.1 chs }
1438 1.1 chs if (data->m != NULL)
1439 1.1 chs m_freem(data->m);
1440 1.1 chs }
1441 1.1 chs }
1442 1.1 chs
1443 1.1 chs struct nfe_jbuf *
1444 1.1 chs nfe_jalloc(struct nfe_softc *sc)
1445 1.1 chs {
1446 1.1 chs struct nfe_jbuf *jbuf;
1447 1.1 chs
1448 1.1 chs jbuf = SLIST_FIRST(&sc->rxq.jfreelist);
1449 1.1 chs if (jbuf == NULL)
1450 1.1 chs return NULL;
1451 1.1 chs SLIST_REMOVE_HEAD(&sc->rxq.jfreelist, jnext);
1452 1.1 chs return jbuf;
1453 1.1 chs }
1454 1.1 chs
1455 1.1 chs /*
1456 1.1 chs * This is called automatically by the network stack when the mbuf is freed.
1457 1.1 chs * Caution must be taken that the NIC might be reset by the time the mbuf is
1458 1.1 chs * freed.
1459 1.1 chs */
1460 1.1 chs void
1461 1.7 christos nfe_jfree(struct mbuf *m, caddr_t buf, size_t size, void *arg)
1462 1.1 chs {
1463 1.1 chs struct nfe_softc *sc = arg;
1464 1.1 chs struct nfe_jbuf *jbuf;
1465 1.1 chs int i;
1466 1.1 chs
1467 1.1 chs /* find the jbuf from the base pointer */
1468 1.1 chs i = (buf - sc->rxq.jpool) / NFE_JBYTES;
1469 1.1 chs if (i < 0 || i >= NFE_JPOOL_COUNT) {
1470 1.1 chs printf("%s: request to free a buffer (%p) not managed by us\n",
1471 1.1 chs sc->sc_dev.dv_xname, buf);
1472 1.1 chs return;
1473 1.1 chs }
1474 1.1 chs jbuf = &sc->rxq.jbuf[i];
1475 1.1 chs
1476 1.1 chs /* ..and put it back in the free list */
1477 1.1 chs SLIST_INSERT_HEAD(&sc->rxq.jfreelist, jbuf, jnext);
1478 1.2 chs
1479 1.2 chs if (m != NULL)
1480 1.2 chs pool_cache_put(&mbpool_cache, m);
1481 1.1 chs }
1482 1.1 chs
1483 1.1 chs int
1484 1.1 chs nfe_jpool_alloc(struct nfe_softc *sc)
1485 1.1 chs {
1486 1.1 chs struct nfe_rx_ring *ring = &sc->rxq;
1487 1.1 chs struct nfe_jbuf *jbuf;
1488 1.1 chs bus_addr_t physaddr;
1489 1.1 chs caddr_t buf;
1490 1.1 chs int i, nsegs, error;
1491 1.1 chs
1492 1.1 chs /*
1493 1.1 chs * Allocate a big chunk of DMA'able memory.
1494 1.1 chs */
1495 1.1 chs error = bus_dmamap_create(sc->sc_dmat, NFE_JPOOL_SIZE, 1,
1496 1.1 chs NFE_JPOOL_SIZE, 0, BUS_DMA_NOWAIT, &ring->jmap);
1497 1.1 chs if (error != 0) {
1498 1.1 chs printf("%s: could not create jumbo DMA map\n",
1499 1.1 chs sc->sc_dev.dv_xname);
1500 1.1 chs goto fail;
1501 1.1 chs }
1502 1.1 chs
1503 1.1 chs error = bus_dmamem_alloc(sc->sc_dmat, NFE_JPOOL_SIZE, PAGE_SIZE, 0,
1504 1.1 chs &ring->jseg, 1, &nsegs, BUS_DMA_NOWAIT);
1505 1.1 chs if (error != 0) {
1506 1.1 chs printf("%s could not allocate jumbo DMA memory\n",
1507 1.1 chs sc->sc_dev.dv_xname);
1508 1.1 chs goto fail;
1509 1.1 chs }
1510 1.1 chs
1511 1.1 chs error = bus_dmamem_map(sc->sc_dmat, &ring->jseg, nsegs, NFE_JPOOL_SIZE,
1512 1.1 chs &ring->jpool, BUS_DMA_NOWAIT);
1513 1.1 chs if (error != 0) {
1514 1.1 chs printf("%s: could not map jumbo DMA memory\n",
1515 1.1 chs sc->sc_dev.dv_xname);
1516 1.1 chs goto fail;
1517 1.1 chs }
1518 1.1 chs
1519 1.1 chs error = bus_dmamap_load(sc->sc_dmat, ring->jmap, ring->jpool,
1520 1.1 chs NFE_JPOOL_SIZE, NULL, BUS_DMA_READ | BUS_DMA_NOWAIT);
1521 1.1 chs if (error != 0) {
1522 1.1 chs printf("%s: could not load jumbo DMA map\n",
1523 1.1 chs sc->sc_dev.dv_xname);
1524 1.1 chs goto fail;
1525 1.1 chs }
1526 1.1 chs
1527 1.1 chs /* ..and split it into 9KB chunks */
1528 1.1 chs SLIST_INIT(&ring->jfreelist);
1529 1.1 chs
1530 1.1 chs buf = ring->jpool;
1531 1.1 chs physaddr = ring->jmap->dm_segs[0].ds_addr;
1532 1.1 chs for (i = 0; i < NFE_JPOOL_COUNT; i++) {
1533 1.1 chs jbuf = &ring->jbuf[i];
1534 1.1 chs
1535 1.1 chs jbuf->buf = buf;
1536 1.1 chs jbuf->physaddr = physaddr;
1537 1.1 chs
1538 1.1 chs SLIST_INSERT_HEAD(&ring->jfreelist, jbuf, jnext);
1539 1.1 chs
1540 1.1 chs buf += NFE_JBYTES;
1541 1.1 chs physaddr += NFE_JBYTES;
1542 1.1 chs }
1543 1.1 chs
1544 1.1 chs return 0;
1545 1.1 chs
1546 1.1 chs fail: nfe_jpool_free(sc);
1547 1.1 chs return error;
1548 1.1 chs }
1549 1.1 chs
1550 1.1 chs void
1551 1.1 chs nfe_jpool_free(struct nfe_softc *sc)
1552 1.1 chs {
1553 1.1 chs struct nfe_rx_ring *ring = &sc->rxq;
1554 1.1 chs
1555 1.1 chs if (ring->jmap != NULL) {
1556 1.1 chs bus_dmamap_sync(sc->sc_dmat, ring->jmap, 0,
1557 1.1 chs ring->jmap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1558 1.1 chs bus_dmamap_unload(sc->sc_dmat, ring->jmap);
1559 1.1 chs bus_dmamap_destroy(sc->sc_dmat, ring->jmap);
1560 1.1 chs }
1561 1.1 chs if (ring->jpool != NULL) {
1562 1.1 chs bus_dmamem_unmap(sc->sc_dmat, ring->jpool, NFE_JPOOL_SIZE);
1563 1.1 chs bus_dmamem_free(sc->sc_dmat, &ring->jseg, 1);
1564 1.1 chs }
1565 1.1 chs }
1566 1.1 chs
1567 1.1 chs int
1568 1.1 chs nfe_alloc_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring)
1569 1.1 chs {
1570 1.1 chs int i, nsegs, error;
1571 1.1 chs void **desc;
1572 1.1 chs int descsize;
1573 1.1 chs
1574 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR) {
1575 1.1 chs desc = (void **)&ring->desc64;
1576 1.1 chs descsize = sizeof (struct nfe_desc64);
1577 1.1 chs } else {
1578 1.1 chs desc = (void **)&ring->desc32;
1579 1.1 chs descsize = sizeof (struct nfe_desc32);
1580 1.1 chs }
1581 1.1 chs
1582 1.1 chs ring->queued = 0;
1583 1.1 chs ring->cur = ring->next = 0;
1584 1.1 chs
1585 1.1 chs error = bus_dmamap_create(sc->sc_dmat, NFE_TX_RING_COUNT * descsize, 1,
1586 1.1 chs NFE_TX_RING_COUNT * descsize, 0, BUS_DMA_NOWAIT, &ring->map);
1587 1.1 chs
1588 1.1 chs if (error != 0) {
1589 1.1 chs printf("%s: could not create desc DMA map\n",
1590 1.1 chs sc->sc_dev.dv_xname);
1591 1.1 chs goto fail;
1592 1.1 chs }
1593 1.1 chs
1594 1.1 chs error = bus_dmamem_alloc(sc->sc_dmat, NFE_TX_RING_COUNT * descsize,
1595 1.1 chs PAGE_SIZE, 0, &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT);
1596 1.1 chs if (error != 0) {
1597 1.1 chs printf("%s: could not allocate DMA memory\n",
1598 1.1 chs sc->sc_dev.dv_xname);
1599 1.1 chs goto fail;
1600 1.1 chs }
1601 1.1 chs
1602 1.1 chs error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs,
1603 1.1 chs NFE_TX_RING_COUNT * descsize, (caddr_t *)desc, BUS_DMA_NOWAIT);
1604 1.1 chs if (error != 0) {
1605 1.1 chs printf("%s: could not map desc DMA memory\n",
1606 1.1 chs sc->sc_dev.dv_xname);
1607 1.1 chs goto fail;
1608 1.1 chs }
1609 1.1 chs
1610 1.1 chs error = bus_dmamap_load(sc->sc_dmat, ring->map, *desc,
1611 1.1 chs NFE_TX_RING_COUNT * descsize, NULL, BUS_DMA_NOWAIT);
1612 1.1 chs if (error != 0) {
1613 1.1 chs printf("%s: could not load desc DMA map\n",
1614 1.1 chs sc->sc_dev.dv_xname);
1615 1.1 chs goto fail;
1616 1.1 chs }
1617 1.1 chs
1618 1.1 chs bzero(*desc, NFE_TX_RING_COUNT * descsize);
1619 1.1 chs ring->physaddr = ring->map->dm_segs[0].ds_addr;
1620 1.1 chs
1621 1.1 chs for (i = 0; i < NFE_TX_RING_COUNT; i++) {
1622 1.1 chs error = bus_dmamap_create(sc->sc_dmat, NFE_JBYTES,
1623 1.1 chs NFE_MAX_SCATTER, NFE_JBYTES, 0, BUS_DMA_NOWAIT,
1624 1.1 chs &ring->data[i].map);
1625 1.1 chs if (error != 0) {
1626 1.1 chs printf("%s: could not create DMA map\n",
1627 1.1 chs sc->sc_dev.dv_xname);
1628 1.1 chs goto fail;
1629 1.1 chs }
1630 1.1 chs }
1631 1.1 chs
1632 1.1 chs return 0;
1633 1.1 chs
1634 1.1 chs fail: nfe_free_tx_ring(sc, ring);
1635 1.1 chs return error;
1636 1.1 chs }
1637 1.1 chs
1638 1.1 chs void
1639 1.1 chs nfe_reset_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring)
1640 1.1 chs {
1641 1.1 chs struct nfe_tx_data *data;
1642 1.1 chs int i;
1643 1.1 chs
1644 1.1 chs for (i = 0; i < NFE_TX_RING_COUNT; i++) {
1645 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR)
1646 1.1 chs ring->desc64[i].flags = 0;
1647 1.1 chs else
1648 1.1 chs ring->desc32[i].flags = 0;
1649 1.1 chs
1650 1.1 chs data = &ring->data[i];
1651 1.1 chs
1652 1.1 chs if (data->m != NULL) {
1653 1.1 chs bus_dmamap_sync(sc->sc_dmat, data->active, 0,
1654 1.1 chs data->active->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1655 1.1 chs bus_dmamap_unload(sc->sc_dmat, data->active);
1656 1.1 chs m_freem(data->m);
1657 1.1 chs data->m = NULL;
1658 1.1 chs }
1659 1.1 chs }
1660 1.1 chs
1661 1.1 chs bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize,
1662 1.1 chs BUS_DMASYNC_PREWRITE);
1663 1.1 chs
1664 1.1 chs ring->queued = 0;
1665 1.1 chs ring->cur = ring->next = 0;
1666 1.1 chs }
1667 1.1 chs
1668 1.1 chs void
1669 1.1 chs nfe_free_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring)
1670 1.1 chs {
1671 1.1 chs struct nfe_tx_data *data;
1672 1.1 chs void *desc;
1673 1.1 chs int i, descsize;
1674 1.1 chs
1675 1.1 chs if (sc->sc_flags & NFE_40BIT_ADDR) {
1676 1.1 chs desc = ring->desc64;
1677 1.1 chs descsize = sizeof (struct nfe_desc64);
1678 1.1 chs } else {
1679 1.1 chs desc = ring->desc32;
1680 1.1 chs descsize = sizeof (struct nfe_desc32);
1681 1.1 chs }
1682 1.1 chs
1683 1.1 chs if (desc != NULL) {
1684 1.1 chs bus_dmamap_sync(sc->sc_dmat, ring->map, 0,
1685 1.1 chs ring->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1686 1.1 chs bus_dmamap_unload(sc->sc_dmat, ring->map);
1687 1.1 chs bus_dmamem_unmap(sc->sc_dmat, (caddr_t)desc,
1688 1.1 chs NFE_TX_RING_COUNT * descsize);
1689 1.1 chs bus_dmamem_free(sc->sc_dmat, &ring->seg, 1);
1690 1.1 chs }
1691 1.1 chs
1692 1.1 chs for (i = 0; i < NFE_TX_RING_COUNT; i++) {
1693 1.1 chs data = &ring->data[i];
1694 1.1 chs
1695 1.1 chs if (data->m != NULL) {
1696 1.1 chs bus_dmamap_sync(sc->sc_dmat, data->active, 0,
1697 1.1 chs data->active->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1698 1.1 chs bus_dmamap_unload(sc->sc_dmat, data->active);
1699 1.1 chs m_freem(data->m);
1700 1.1 chs }
1701 1.1 chs }
1702 1.1 chs
1703 1.1 chs /* ..and now actually destroy the DMA mappings */
1704 1.1 chs for (i = 0; i < NFE_TX_RING_COUNT; i++) {
1705 1.1 chs data = &ring->data[i];
1706 1.1 chs if (data->map == NULL)
1707 1.1 chs continue;
1708 1.1 chs bus_dmamap_destroy(sc->sc_dmat, data->map);
1709 1.1 chs }
1710 1.1 chs }
1711 1.1 chs
1712 1.1 chs int
1713 1.1 chs nfe_ifmedia_upd(struct ifnet *ifp)
1714 1.1 chs {
1715 1.1 chs struct nfe_softc *sc = ifp->if_softc;
1716 1.1 chs struct mii_data *mii = &sc->sc_mii;
1717 1.1 chs struct mii_softc *miisc;
1718 1.1 chs
1719 1.1 chs if (mii->mii_instance != 0) {
1720 1.1 chs LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1721 1.1 chs mii_phy_reset(miisc);
1722 1.1 chs }
1723 1.1 chs return mii_mediachg(mii);
1724 1.1 chs }
1725 1.1 chs
1726 1.1 chs void
1727 1.1 chs nfe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1728 1.1 chs {
1729 1.1 chs struct nfe_softc *sc = ifp->if_softc;
1730 1.1 chs struct mii_data *mii = &sc->sc_mii;
1731 1.1 chs
1732 1.1 chs mii_pollstat(mii);
1733 1.1 chs ifmr->ifm_status = mii->mii_media_status;
1734 1.1 chs ifmr->ifm_active = mii->mii_media_active;
1735 1.1 chs }
1736 1.1 chs
1737 1.1 chs void
1738 1.1 chs nfe_setmulti(struct nfe_softc *sc)
1739 1.1 chs {
1740 1.1 chs struct ethercom *ec = &sc->sc_ethercom;
1741 1.1 chs struct ifnet *ifp = &ec->ec_if;
1742 1.1 chs struct ether_multi *enm;
1743 1.1 chs struct ether_multistep step;
1744 1.1 chs uint8_t addr[ETHER_ADDR_LEN], mask[ETHER_ADDR_LEN];
1745 1.1 chs uint32_t filter = NFE_RXFILTER_MAGIC;
1746 1.1 chs int i;
1747 1.1 chs
1748 1.1 chs if ((ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
1749 1.1 chs bzero(addr, ETHER_ADDR_LEN);
1750 1.1 chs bzero(mask, ETHER_ADDR_LEN);
1751 1.1 chs goto done;
1752 1.1 chs }
1753 1.1 chs
1754 1.1 chs bcopy(etherbroadcastaddr, addr, ETHER_ADDR_LEN);
1755 1.1 chs bcopy(etherbroadcastaddr, mask, ETHER_ADDR_LEN);
1756 1.1 chs
1757 1.1 chs ETHER_FIRST_MULTI(step, ec, enm);
1758 1.1 chs while (enm != NULL) {
1759 1.1 chs if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1760 1.1 chs ifp->if_flags |= IFF_ALLMULTI;
1761 1.1 chs bzero(addr, ETHER_ADDR_LEN);
1762 1.1 chs bzero(mask, ETHER_ADDR_LEN);
1763 1.1 chs goto done;
1764 1.1 chs }
1765 1.1 chs for (i = 0; i < ETHER_ADDR_LEN; i++) {
1766 1.1 chs addr[i] &= enm->enm_addrlo[i];
1767 1.1 chs mask[i] &= ~enm->enm_addrlo[i];
1768 1.1 chs }
1769 1.1 chs ETHER_NEXT_MULTI(step, enm);
1770 1.1 chs }
1771 1.1 chs for (i = 0; i < ETHER_ADDR_LEN; i++)
1772 1.1 chs mask[i] |= addr[i];
1773 1.1 chs
1774 1.1 chs done:
1775 1.1 chs addr[0] |= 0x01; /* make sure multicast bit is set */
1776 1.1 chs
1777 1.1 chs NFE_WRITE(sc, NFE_MULTIADDR_HI,
1778 1.1 chs addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]);
1779 1.1 chs NFE_WRITE(sc, NFE_MULTIADDR_LO,
1780 1.1 chs addr[5] << 8 | addr[4]);
1781 1.1 chs NFE_WRITE(sc, NFE_MULTIMASK_HI,
1782 1.1 chs mask[3] << 24 | mask[2] << 16 | mask[1] << 8 | mask[0]);
1783 1.1 chs NFE_WRITE(sc, NFE_MULTIMASK_LO,
1784 1.1 chs mask[5] << 8 | mask[4]);
1785 1.1 chs
1786 1.1 chs filter |= (ifp->if_flags & IFF_PROMISC) ? NFE_PROMISC : NFE_U2M;
1787 1.1 chs NFE_WRITE(sc, NFE_RXFILTER, filter);
1788 1.1 chs }
1789 1.1 chs
1790 1.1 chs void
1791 1.1 chs nfe_get_macaddr(struct nfe_softc *sc, uint8_t *addr)
1792 1.1 chs {
1793 1.1 chs uint32_t tmp;
1794 1.1 chs
1795 1.1 chs tmp = NFE_READ(sc, NFE_MACADDR_LO);
1796 1.1 chs addr[0] = (tmp >> 8) & 0xff;
1797 1.1 chs addr[1] = (tmp & 0xff);
1798 1.1 chs
1799 1.1 chs tmp = NFE_READ(sc, NFE_MACADDR_HI);
1800 1.1 chs addr[2] = (tmp >> 24) & 0xff;
1801 1.1 chs addr[3] = (tmp >> 16) & 0xff;
1802 1.1 chs addr[4] = (tmp >> 8) & 0xff;
1803 1.1 chs addr[5] = (tmp & 0xff);
1804 1.1 chs }
1805 1.1 chs
1806 1.1 chs void
1807 1.1 chs nfe_set_macaddr(struct nfe_softc *sc, const uint8_t *addr)
1808 1.1 chs {
1809 1.1 chs NFE_WRITE(sc, NFE_MACADDR_LO,
1810 1.1 chs addr[5] << 8 | addr[4]);
1811 1.1 chs NFE_WRITE(sc, NFE_MACADDR_HI,
1812 1.1 chs addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]);
1813 1.1 chs }
1814 1.1 chs
1815 1.1 chs void
1816 1.1 chs nfe_tick(void *arg)
1817 1.1 chs {
1818 1.1 chs struct nfe_softc *sc = arg;
1819 1.1 chs int s;
1820 1.1 chs
1821 1.1 chs s = splnet();
1822 1.1 chs mii_tick(&sc->sc_mii);
1823 1.1 chs splx(s);
1824 1.1 chs
1825 1.1 chs callout_schedule(&sc->sc_tick_ch, hz);
1826 1.1 chs }
1827