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