if_nfe.c revision 1.32 1 /* $NetBSD: if_nfe.c,v 1.32 2008/04/17 19:50:38 xtraeme 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.32 2008/04/17 19:50:38 xtraeme 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 bitmask_snprintf(flags, NFE_V1_TXERR,
972 buf, sizeof(buf)));
973 ifp->if_oerrors++;
974 } else
975 ifp->if_opackets++;
976 } else {
977 if ((flags & NFE_TX_LASTFRAG_V2) == 0 &&
978 data->m == NULL)
979 continue;
980
981 if ((flags & NFE_TX_ERROR_V2) != 0) {
982 aprint_error_dev(sc->sc_dev, "tx v2 error %s\n",
983 bitmask_snprintf(flags, NFE_V2_TXERR,
984 buf, sizeof(buf)));
985 ifp->if_oerrors++;
986 } else
987 ifp->if_opackets++;
988 }
989
990 if (data->m == NULL) { /* should not get there */
991 aprint_error_dev(sc->sc_dev,
992 "last fragment bit w/o associated mbuf!\n");
993 continue;
994 }
995
996 /* last fragment of the mbuf chain transmitted */
997 bus_dmamap_sync(sc->sc_dmat, data->active, 0,
998 data->active->dm_mapsize, BUS_DMASYNC_POSTWRITE);
999 bus_dmamap_unload(sc->sc_dmat, data->active);
1000 m_freem(data->m);
1001 data->m = NULL;
1002 }
1003
1004 sc->txq.next = i;
1005
1006 if (sc->txq.queued < NFE_TX_RING_COUNT) {
1007 /* at least one slot freed */
1008 ifp->if_flags &= ~IFF_OACTIVE;
1009 }
1010
1011 if (sc->txq.queued == 0) {
1012 /* all queued packets are sent */
1013 ifp->if_timer = 0;
1014 }
1015 }
1016
1017 int
1018 nfe_encap(struct nfe_softc *sc, struct mbuf *m0)
1019 {
1020 struct nfe_desc32 *desc32;
1021 struct nfe_desc64 *desc64;
1022 struct nfe_tx_data *data;
1023 bus_dmamap_t map;
1024 uint16_t flags, csumflags;
1025 #if NVLAN > 0
1026 struct m_tag *mtag;
1027 uint32_t vtag = 0;
1028 #endif
1029 int error, i, first;
1030
1031 desc32 = NULL;
1032 desc64 = NULL;
1033 data = NULL;
1034
1035 flags = 0;
1036 csumflags = 0;
1037 first = sc->txq.cur;
1038
1039 map = sc->txq.data[first].map;
1040
1041 error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m0, BUS_DMA_NOWAIT);
1042 if (error != 0) {
1043 aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
1044 error);
1045 return error;
1046 }
1047
1048 if (sc->txq.queued + map->dm_nsegs >= NFE_TX_RING_COUNT - 1) {
1049 bus_dmamap_unload(sc->sc_dmat, map);
1050 return ENOBUFS;
1051 }
1052
1053 #if NVLAN > 0
1054 /* setup h/w VLAN tagging */
1055 if ((mtag = VLAN_OUTPUT_TAG(&sc->sc_ethercom, m0)) != NULL)
1056 vtag = NFE_TX_VTAG | VLAN_TAG_VALUE(mtag);
1057 #endif
1058 if ((sc->sc_flags & NFE_HW_CSUM) != 0) {
1059 if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4)
1060 csumflags |= NFE_TX_IP_CSUM;
1061 if (m0->m_pkthdr.csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4))
1062 csumflags |= NFE_TX_TCP_UDP_CSUM;
1063 }
1064
1065 for (i = 0; i < map->dm_nsegs; i++) {
1066 data = &sc->txq.data[sc->txq.cur];
1067
1068 if (sc->sc_flags & NFE_40BIT_ADDR) {
1069 desc64 = &sc->txq.desc64[sc->txq.cur];
1070 #if defined(__LP64__)
1071 desc64->physaddr[0] =
1072 htole32(map->dm_segs[i].ds_addr >> 32);
1073 #endif
1074 desc64->physaddr[1] =
1075 htole32(map->dm_segs[i].ds_addr & 0xffffffff);
1076 desc64->length = htole16(map->dm_segs[i].ds_len - 1);
1077 desc64->flags = htole16(flags);
1078 desc64->vtag = 0;
1079 } else {
1080 desc32 = &sc->txq.desc32[sc->txq.cur];
1081
1082 desc32->physaddr = htole32(map->dm_segs[i].ds_addr);
1083 desc32->length = htole16(map->dm_segs[i].ds_len - 1);
1084 desc32->flags = htole16(flags);
1085 }
1086
1087 /*
1088 * Setting of the valid bit in the first descriptor is
1089 * deferred until the whole chain is fully setup.
1090 */
1091 flags |= NFE_TX_VALID;
1092
1093 sc->txq.queued++;
1094 sc->txq.cur = NFE_TX_NEXTDESC(sc->txq.cur);
1095 }
1096
1097 /* the whole mbuf chain has been setup */
1098 if (sc->sc_flags & NFE_40BIT_ADDR) {
1099 /* fix last descriptor */
1100 flags |= NFE_TX_LASTFRAG_V2;
1101 desc64->flags = htole16(flags);
1102
1103 /* Checksum flags and vtag belong to the first fragment only. */
1104 #if NVLAN > 0
1105 sc->txq.desc64[first].vtag = htole32(vtag);
1106 #endif
1107 sc->txq.desc64[first].flags |= htole16(csumflags);
1108
1109 /* finally, set the valid bit in the first descriptor */
1110 sc->txq.desc64[first].flags |= htole16(NFE_TX_VALID);
1111 } else {
1112 /* fix last descriptor */
1113 if (sc->sc_flags & NFE_JUMBO_SUP)
1114 flags |= NFE_TX_LASTFRAG_V2;
1115 else
1116 flags |= NFE_TX_LASTFRAG_V1;
1117 desc32->flags = htole16(flags);
1118
1119 /* Checksum flags belong to the first fragment only. */
1120 sc->txq.desc32[first].flags |= htole16(csumflags);
1121
1122 /* finally, set the valid bit in the first descriptor */
1123 sc->txq.desc32[first].flags |= htole16(NFE_TX_VALID);
1124 }
1125
1126 data->m = m0;
1127 data->active = map;
1128
1129 bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1130 BUS_DMASYNC_PREWRITE);
1131
1132 return 0;
1133 }
1134
1135 void
1136 nfe_start(struct ifnet *ifp)
1137 {
1138 struct nfe_softc *sc = ifp->if_softc;
1139 int old = sc->txq.queued;
1140 struct mbuf *m0;
1141
1142 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1143 return;
1144
1145 for (;;) {
1146 IFQ_POLL(&ifp->if_snd, m0);
1147 if (m0 == NULL)
1148 break;
1149
1150 if (nfe_encap(sc, m0) != 0) {
1151 ifp->if_flags |= IFF_OACTIVE;
1152 break;
1153 }
1154
1155 /* packet put in h/w queue, remove from s/w queue */
1156 IFQ_DEQUEUE(&ifp->if_snd, m0);
1157
1158 #if NBPFILTER > 0
1159 if (ifp->if_bpf != NULL)
1160 bpf_mtap(ifp->if_bpf, m0);
1161 #endif
1162 }
1163
1164 if (sc->txq.queued != old) {
1165 /* packets are queued */
1166 if (sc->sc_flags & NFE_40BIT_ADDR)
1167 nfe_txdesc64_rsync(sc, old, sc->txq.cur,
1168 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1169 else
1170 nfe_txdesc32_rsync(sc, old, sc->txq.cur,
1171 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1172 /* kick Tx */
1173 NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_KICKTX | sc->rxtxctl);
1174
1175 /*
1176 * Set a timeout in case the chip goes out to lunch.
1177 */
1178 ifp->if_timer = 5;
1179 }
1180 }
1181
1182 void
1183 nfe_watchdog(struct ifnet *ifp)
1184 {
1185 struct nfe_softc *sc = ifp->if_softc;
1186
1187 aprint_error_dev(sc->sc_dev, "watchdog timeout\n");
1188
1189 ifp->if_flags &= ~IFF_RUNNING;
1190 nfe_init(ifp);
1191
1192 ifp->if_oerrors++;
1193 }
1194
1195 int
1196 nfe_init(struct ifnet *ifp)
1197 {
1198 struct nfe_softc *sc = ifp->if_softc;
1199 uint32_t tmp;
1200 int rc = 0, s;
1201
1202 if (ifp->if_flags & IFF_RUNNING)
1203 return 0;
1204
1205 nfe_stop(ifp, 0);
1206
1207 NFE_WRITE(sc, NFE_TX_UNK, 0);
1208 NFE_WRITE(sc, NFE_STATUS, 0);
1209
1210 sc->rxtxctl = NFE_RXTX_BIT2;
1211 if (sc->sc_flags & NFE_40BIT_ADDR)
1212 sc->rxtxctl |= NFE_RXTX_V3MAGIC;
1213 else if (sc->sc_flags & NFE_JUMBO_SUP)
1214 sc->rxtxctl |= NFE_RXTX_V2MAGIC;
1215 if (sc->sc_flags & NFE_HW_CSUM)
1216 sc->rxtxctl |= NFE_RXTX_RXCSUM;
1217 #if NVLAN > 0
1218 /*
1219 * Although the adapter is capable of stripping VLAN tags from received
1220 * frames (NFE_RXTX_VTAG_STRIP), we do not enable this functionality on
1221 * purpose. This will be done in software by our network stack.
1222 */
1223 if (sc->sc_flags & NFE_HW_VLAN)
1224 sc->rxtxctl |= NFE_RXTX_VTAG_INSERT;
1225 #endif
1226 NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_RESET | sc->rxtxctl);
1227 DELAY(10);
1228 NFE_WRITE(sc, NFE_RXTX_CTL, sc->rxtxctl);
1229
1230 #if NVLAN
1231 if (sc->sc_flags & NFE_HW_VLAN)
1232 NFE_WRITE(sc, NFE_VTAG_CTL, NFE_VTAG_ENABLE);
1233 #endif
1234
1235 NFE_WRITE(sc, NFE_SETUP_R6, 0);
1236
1237 /* set MAC address */
1238 nfe_set_macaddr(sc, sc->sc_enaddr);
1239
1240 /* tell MAC where rings are in memory */
1241 #ifdef __LP64__
1242 NFE_WRITE(sc, NFE_RX_RING_ADDR_HI, sc->rxq.physaddr >> 32);
1243 #endif
1244 NFE_WRITE(sc, NFE_RX_RING_ADDR_LO, sc->rxq.physaddr & 0xffffffff);
1245 #ifdef __LP64__
1246 NFE_WRITE(sc, NFE_TX_RING_ADDR_HI, sc->txq.physaddr >> 32);
1247 #endif
1248 NFE_WRITE(sc, NFE_TX_RING_ADDR_LO, sc->txq.physaddr & 0xffffffff);
1249
1250 NFE_WRITE(sc, NFE_RING_SIZE,
1251 (NFE_RX_RING_COUNT - 1) << 16 |
1252 (NFE_TX_RING_COUNT - 1));
1253
1254 NFE_WRITE(sc, NFE_RXBUFSZ, sc->rxq.bufsz);
1255
1256 /* force MAC to wakeup */
1257 tmp = NFE_READ(sc, NFE_PWR_STATE);
1258 NFE_WRITE(sc, NFE_PWR_STATE, tmp | NFE_PWR_WAKEUP);
1259 DELAY(10);
1260 tmp = NFE_READ(sc, NFE_PWR_STATE);
1261 NFE_WRITE(sc, NFE_PWR_STATE, tmp | NFE_PWR_VALID);
1262
1263 s = splnet();
1264 nfe_intr(sc); /* XXX clear IRQ status registers */
1265 splx(s);
1266
1267 #if 1
1268 /* configure interrupts coalescing/mitigation */
1269 NFE_WRITE(sc, NFE_IMTIMER, NFE_IM_DEFAULT);
1270 #else
1271 /* no interrupt mitigation: one interrupt per packet */
1272 NFE_WRITE(sc, NFE_IMTIMER, 970);
1273 #endif
1274
1275 NFE_WRITE(sc, NFE_SETUP_R1, NFE_R1_MAGIC);
1276 NFE_WRITE(sc, NFE_SETUP_R2, NFE_R2_MAGIC);
1277 NFE_WRITE(sc, NFE_SETUP_R6, NFE_R6_MAGIC);
1278
1279 /* update MAC knowledge of PHY; generates a NFE_IRQ_LINK interrupt */
1280 NFE_WRITE(sc, NFE_STATUS, sc->mii_phyaddr << 24 | NFE_STATUS_MAGIC);
1281
1282 NFE_WRITE(sc, NFE_SETUP_R4, NFE_R4_MAGIC);
1283 NFE_WRITE(sc, NFE_WOL_CTL, NFE_WOL_ENABLE);
1284
1285 sc->rxtxctl &= ~NFE_RXTX_BIT2;
1286 NFE_WRITE(sc, NFE_RXTX_CTL, sc->rxtxctl);
1287 DELAY(10);
1288 NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_BIT1 | sc->rxtxctl);
1289
1290 /* set Rx filter */
1291 nfe_setmulti(sc);
1292
1293 if ((rc = ether_mediachange(ifp)) != 0)
1294 goto out;
1295
1296 nfe_tick(sc);
1297
1298 /* enable Rx */
1299 NFE_WRITE(sc, NFE_RX_CTL, NFE_RX_START);
1300
1301 /* enable Tx */
1302 NFE_WRITE(sc, NFE_TX_CTL, NFE_TX_START);
1303
1304 NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
1305
1306 /* enable interrupts */
1307 NFE_WRITE(sc, NFE_IRQ_MASK, NFE_IRQ_WANTED);
1308
1309 callout_schedule(&sc->sc_tick_ch, hz);
1310
1311 ifp->if_flags |= IFF_RUNNING;
1312 ifp->if_flags &= ~IFF_OACTIVE;
1313
1314 out:
1315 return rc;
1316 }
1317
1318 void
1319 nfe_stop(struct ifnet *ifp, int disable)
1320 {
1321 struct nfe_softc *sc = ifp->if_softc;
1322
1323 callout_stop(&sc->sc_tick_ch);
1324
1325 ifp->if_timer = 0;
1326 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1327
1328 mii_down(&sc->sc_mii);
1329
1330 /* abort Tx */
1331 NFE_WRITE(sc, NFE_TX_CTL, 0);
1332
1333 /* disable Rx */
1334 NFE_WRITE(sc, NFE_RX_CTL, 0);
1335
1336 /* disable interrupts */
1337 NFE_WRITE(sc, NFE_IRQ_MASK, 0);
1338
1339 /* reset Tx and Rx rings */
1340 nfe_reset_tx_ring(sc, &sc->txq);
1341 nfe_reset_rx_ring(sc, &sc->rxq);
1342 }
1343
1344 int
1345 nfe_alloc_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1346 {
1347 struct nfe_desc32 *desc32;
1348 struct nfe_desc64 *desc64;
1349 struct nfe_rx_data *data;
1350 struct nfe_jbuf *jbuf;
1351 void **desc;
1352 bus_addr_t physaddr;
1353 int i, nsegs, error, descsize;
1354
1355 if (sc->sc_flags & NFE_40BIT_ADDR) {
1356 desc = (void **)&ring->desc64;
1357 descsize = sizeof (struct nfe_desc64);
1358 } else {
1359 desc = (void **)&ring->desc32;
1360 descsize = sizeof (struct nfe_desc32);
1361 }
1362
1363 ring->cur = ring->next = 0;
1364 ring->bufsz = MCLBYTES;
1365
1366 error = bus_dmamap_create(sc->sc_dmat, NFE_RX_RING_COUNT * descsize, 1,
1367 NFE_RX_RING_COUNT * descsize, 0, BUS_DMA_NOWAIT, &ring->map);
1368 if (error != 0) {
1369 aprint_error_dev(sc->sc_dev,
1370 "could not create desc DMA map\n");
1371 goto fail;
1372 }
1373
1374 error = bus_dmamem_alloc(sc->sc_dmat, NFE_RX_RING_COUNT * descsize,
1375 PAGE_SIZE, 0, &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT);
1376 if (error != 0) {
1377 aprint_error_dev(sc->sc_dev,
1378 "could not allocate DMA memory\n");
1379 goto fail;
1380 }
1381
1382 error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs,
1383 NFE_RX_RING_COUNT * descsize, (void **)desc, BUS_DMA_NOWAIT);
1384 if (error != 0) {
1385 aprint_error_dev(sc->sc_dev,
1386 "could not map desc DMA memory\n");
1387 goto fail;
1388 }
1389
1390 error = bus_dmamap_load(sc->sc_dmat, ring->map, *desc,
1391 NFE_RX_RING_COUNT * descsize, NULL, BUS_DMA_NOWAIT);
1392 if (error != 0) {
1393 aprint_error_dev(sc->sc_dev, "could not load desc DMA map\n");
1394 goto fail;
1395 }
1396
1397 bzero(*desc, NFE_RX_RING_COUNT * descsize);
1398 ring->physaddr = ring->map->dm_segs[0].ds_addr;
1399
1400 if (sc->sc_flags & NFE_USE_JUMBO) {
1401 ring->bufsz = NFE_JBYTES;
1402 if ((error = nfe_jpool_alloc(sc)) != 0) {
1403 aprint_error_dev(sc->sc_dev,
1404 "could not allocate jumbo frames\n");
1405 goto fail;
1406 }
1407 }
1408
1409 /*
1410 * Pre-allocate Rx buffers and populate Rx ring.
1411 */
1412 for (i = 0; i < NFE_RX_RING_COUNT; i++) {
1413 data = &sc->rxq.data[i];
1414
1415 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
1416 if (data->m == NULL) {
1417 aprint_error_dev(sc->sc_dev,
1418 "could not allocate rx mbuf\n");
1419 error = ENOMEM;
1420 goto fail;
1421 }
1422
1423 if (sc->sc_flags & NFE_USE_JUMBO) {
1424 if ((jbuf = nfe_jalloc(sc, i)) == NULL) {
1425 aprint_error_dev(sc->sc_dev,
1426 "could not allocate jumbo buffer\n");
1427 goto fail;
1428 }
1429 MEXTADD(data->m, jbuf->buf, NFE_JBYTES, 0, nfe_jfree,
1430 sc);
1431
1432 physaddr = jbuf->physaddr;
1433 } else {
1434 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
1435 MCLBYTES, 0, BUS_DMA_NOWAIT, &data->map);
1436 if (error != 0) {
1437 aprint_error_dev(sc->sc_dev,
1438 "could not create DMA map\n");
1439 goto fail;
1440 }
1441 MCLGET(data->m, M_DONTWAIT);
1442 if (!(data->m->m_flags & M_EXT)) {
1443 aprint_error_dev(sc->sc_dev,
1444 "could not allocate mbuf cluster\n");
1445 error = ENOMEM;
1446 goto fail;
1447 }
1448
1449 error = bus_dmamap_load(sc->sc_dmat, data->map,
1450 mtod(data->m, void *), MCLBYTES, NULL,
1451 BUS_DMA_READ | BUS_DMA_NOWAIT);
1452 if (error != 0) {
1453 aprint_error_dev(sc->sc_dev,
1454 "could not load rx buf DMA map");
1455 goto fail;
1456 }
1457 physaddr = data->map->dm_segs[0].ds_addr;
1458 }
1459
1460 if (sc->sc_flags & NFE_40BIT_ADDR) {
1461 desc64 = &sc->rxq.desc64[i];
1462 #if defined(__LP64__)
1463 desc64->physaddr[0] = htole32(physaddr >> 32);
1464 #endif
1465 desc64->physaddr[1] = htole32(physaddr & 0xffffffff);
1466 desc64->length = htole16(sc->rxq.bufsz);
1467 desc64->flags = htole16(NFE_RX_READY);
1468 } else {
1469 desc32 = &sc->rxq.desc32[i];
1470 desc32->physaddr = htole32(physaddr);
1471 desc32->length = htole16(sc->rxq.bufsz);
1472 desc32->flags = htole16(NFE_RX_READY);
1473 }
1474 }
1475
1476 bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize,
1477 BUS_DMASYNC_PREWRITE);
1478
1479 return 0;
1480
1481 fail: nfe_free_rx_ring(sc, ring);
1482 return error;
1483 }
1484
1485 void
1486 nfe_reset_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1487 {
1488 int i;
1489
1490 for (i = 0; i < NFE_RX_RING_COUNT; i++) {
1491 if (sc->sc_flags & NFE_40BIT_ADDR) {
1492 ring->desc64[i].length = htole16(ring->bufsz);
1493 ring->desc64[i].flags = htole16(NFE_RX_READY);
1494 } else {
1495 ring->desc32[i].length = htole16(ring->bufsz);
1496 ring->desc32[i].flags = htole16(NFE_RX_READY);
1497 }
1498 }
1499
1500 bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize,
1501 BUS_DMASYNC_PREWRITE);
1502
1503 ring->cur = ring->next = 0;
1504 }
1505
1506 void
1507 nfe_free_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1508 {
1509 struct nfe_rx_data *data;
1510 void *desc;
1511 int i, descsize;
1512
1513 if (sc->sc_flags & NFE_40BIT_ADDR) {
1514 desc = ring->desc64;
1515 descsize = sizeof (struct nfe_desc64);
1516 } else {
1517 desc = ring->desc32;
1518 descsize = sizeof (struct nfe_desc32);
1519 }
1520
1521 if (desc != NULL) {
1522 bus_dmamap_sync(sc->sc_dmat, ring->map, 0,
1523 ring->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1524 bus_dmamap_unload(sc->sc_dmat, ring->map);
1525 bus_dmamem_unmap(sc->sc_dmat, (void *)desc,
1526 NFE_RX_RING_COUNT * descsize);
1527 bus_dmamem_free(sc->sc_dmat, &ring->seg, 1);
1528 }
1529
1530 for (i = 0; i < NFE_RX_RING_COUNT; i++) {
1531 data = &ring->data[i];
1532
1533 if (data->map != NULL) {
1534 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1535 data->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
1536 bus_dmamap_unload(sc->sc_dmat, data->map);
1537 bus_dmamap_destroy(sc->sc_dmat, data->map);
1538 }
1539 if (data->m != NULL)
1540 m_freem(data->m);
1541 }
1542 }
1543
1544 struct nfe_jbuf *
1545 nfe_jalloc(struct nfe_softc *sc, int i)
1546 {
1547 struct nfe_jbuf *jbuf;
1548
1549 jbuf = SLIST_FIRST(&sc->rxq.jfreelist);
1550 if (jbuf == NULL)
1551 return NULL;
1552 sc->rxq.jbufmap[i] =
1553 ((char *)jbuf->buf - (char *)sc->rxq.jpool) / NFE_JBYTES;
1554 SLIST_REMOVE_HEAD(&sc->rxq.jfreelist, jnext);
1555 return jbuf;
1556 }
1557
1558 /*
1559 * This is called automatically by the network stack when the mbuf is freed.
1560 * Caution must be taken that the NIC might be reset by the time the mbuf is
1561 * freed.
1562 */
1563 void
1564 nfe_jfree(struct mbuf *m, void *buf, size_t size, void *arg)
1565 {
1566 struct nfe_softc *sc = arg;
1567 struct nfe_jbuf *jbuf;
1568 int i;
1569
1570 /* find the jbuf from the base pointer */
1571 i = ((char *)buf - (char *)sc->rxq.jpool) / NFE_JBYTES;
1572 if (i < 0 || i >= NFE_JPOOL_COUNT) {
1573 aprint_error_dev(sc->sc_dev,
1574 "request to free a buffer (%p) not managed by us\n", buf);
1575 return;
1576 }
1577 jbuf = &sc->rxq.jbuf[i];
1578
1579 /* ..and put it back in the free list */
1580 SLIST_INSERT_HEAD(&sc->rxq.jfreelist, jbuf, jnext);
1581
1582 if (m != NULL)
1583 pool_cache_put(mb_cache, m);
1584 }
1585
1586 int
1587 nfe_jpool_alloc(struct nfe_softc *sc)
1588 {
1589 struct nfe_rx_ring *ring = &sc->rxq;
1590 struct nfe_jbuf *jbuf;
1591 bus_addr_t physaddr;
1592 char *buf;
1593 int i, nsegs, error;
1594
1595 /*
1596 * Allocate a big chunk of DMA'able memory.
1597 */
1598 error = bus_dmamap_create(sc->sc_dmat, NFE_JPOOL_SIZE, 1,
1599 NFE_JPOOL_SIZE, 0, BUS_DMA_NOWAIT, &ring->jmap);
1600 if (error != 0) {
1601 aprint_error_dev(sc->sc_dev,
1602 "could not create jumbo DMA map\n");
1603 goto fail;
1604 }
1605
1606 error = bus_dmamem_alloc(sc->sc_dmat, NFE_JPOOL_SIZE, PAGE_SIZE, 0,
1607 &ring->jseg, 1, &nsegs, BUS_DMA_NOWAIT);
1608 if (error != 0) {
1609 aprint_error_dev(sc->sc_dev,
1610 "could not allocate jumbo DMA memory\n");
1611 goto fail;
1612 }
1613
1614 error = bus_dmamem_map(sc->sc_dmat, &ring->jseg, nsegs, NFE_JPOOL_SIZE,
1615 &ring->jpool, BUS_DMA_NOWAIT);
1616 if (error != 0) {
1617 aprint_error_dev(sc->sc_dev,
1618 "could not map jumbo DMA memory\n");
1619 goto fail;
1620 }
1621
1622 error = bus_dmamap_load(sc->sc_dmat, ring->jmap, ring->jpool,
1623 NFE_JPOOL_SIZE, NULL, BUS_DMA_READ | BUS_DMA_NOWAIT);
1624 if (error != 0) {
1625 aprint_error_dev(sc->sc_dev,
1626 "could not load jumbo DMA map\n");
1627 goto fail;
1628 }
1629
1630 /* ..and split it into 9KB chunks */
1631 SLIST_INIT(&ring->jfreelist);
1632
1633 buf = ring->jpool;
1634 physaddr = ring->jmap->dm_segs[0].ds_addr;
1635 for (i = 0; i < NFE_JPOOL_COUNT; i++) {
1636 jbuf = &ring->jbuf[i];
1637
1638 jbuf->buf = buf;
1639 jbuf->physaddr = physaddr;
1640
1641 SLIST_INSERT_HEAD(&ring->jfreelist, jbuf, jnext);
1642
1643 buf += NFE_JBYTES;
1644 physaddr += NFE_JBYTES;
1645 }
1646
1647 return 0;
1648
1649 fail: nfe_jpool_free(sc);
1650 return error;
1651 }
1652
1653 void
1654 nfe_jpool_free(struct nfe_softc *sc)
1655 {
1656 struct nfe_rx_ring *ring = &sc->rxq;
1657
1658 if (ring->jmap != NULL) {
1659 bus_dmamap_sync(sc->sc_dmat, ring->jmap, 0,
1660 ring->jmap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1661 bus_dmamap_unload(sc->sc_dmat, ring->jmap);
1662 bus_dmamap_destroy(sc->sc_dmat, ring->jmap);
1663 }
1664 if (ring->jpool != NULL) {
1665 bus_dmamem_unmap(sc->sc_dmat, ring->jpool, NFE_JPOOL_SIZE);
1666 bus_dmamem_free(sc->sc_dmat, &ring->jseg, 1);
1667 }
1668 }
1669
1670 int
1671 nfe_alloc_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring)
1672 {
1673 int i, nsegs, error;
1674 void **desc;
1675 int descsize;
1676
1677 if (sc->sc_flags & NFE_40BIT_ADDR) {
1678 desc = (void **)&ring->desc64;
1679 descsize = sizeof (struct nfe_desc64);
1680 } else {
1681 desc = (void **)&ring->desc32;
1682 descsize = sizeof (struct nfe_desc32);
1683 }
1684
1685 ring->queued = 0;
1686 ring->cur = ring->next = 0;
1687
1688 error = bus_dmamap_create(sc->sc_dmat, NFE_TX_RING_COUNT * descsize, 1,
1689 NFE_TX_RING_COUNT * descsize, 0, BUS_DMA_NOWAIT, &ring->map);
1690
1691 if (error != 0) {
1692 aprint_error_dev(sc->sc_dev,
1693 "could not create desc DMA map\n");
1694 goto fail;
1695 }
1696
1697 error = bus_dmamem_alloc(sc->sc_dmat, NFE_TX_RING_COUNT * descsize,
1698 PAGE_SIZE, 0, &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT);
1699 if (error != 0) {
1700 aprint_error_dev(sc->sc_dev,
1701 "could not allocate DMA memory\n");
1702 goto fail;
1703 }
1704
1705 error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs,
1706 NFE_TX_RING_COUNT * descsize, (void **)desc, BUS_DMA_NOWAIT);
1707 if (error != 0) {
1708 aprint_error_dev(sc->sc_dev,
1709 "could not map desc DMA memory\n");
1710 goto fail;
1711 }
1712
1713 error = bus_dmamap_load(sc->sc_dmat, ring->map, *desc,
1714 NFE_TX_RING_COUNT * descsize, NULL, BUS_DMA_NOWAIT);
1715 if (error != 0) {
1716 aprint_error_dev(sc->sc_dev, "could not load desc DMA map\n");
1717 goto fail;
1718 }
1719
1720 bzero(*desc, NFE_TX_RING_COUNT * descsize);
1721 ring->physaddr = ring->map->dm_segs[0].ds_addr;
1722
1723 for (i = 0; i < NFE_TX_RING_COUNT; i++) {
1724 error = bus_dmamap_create(sc->sc_dmat, NFE_JBYTES,
1725 NFE_MAX_SCATTER, NFE_JBYTES, 0, BUS_DMA_NOWAIT,
1726 &ring->data[i].map);
1727 if (error != 0) {
1728 aprint_error_dev(sc->sc_dev,
1729 "could not create DMA map\n");
1730 goto fail;
1731 }
1732 }
1733
1734 return 0;
1735
1736 fail: nfe_free_tx_ring(sc, ring);
1737 return error;
1738 }
1739
1740 void
1741 nfe_reset_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring)
1742 {
1743 struct nfe_tx_data *data;
1744 int i;
1745
1746 for (i = 0; i < NFE_TX_RING_COUNT; i++) {
1747 if (sc->sc_flags & NFE_40BIT_ADDR)
1748 ring->desc64[i].flags = 0;
1749 else
1750 ring->desc32[i].flags = 0;
1751
1752 data = &ring->data[i];
1753
1754 if (data->m != NULL) {
1755 bus_dmamap_sync(sc->sc_dmat, data->active, 0,
1756 data->active->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1757 bus_dmamap_unload(sc->sc_dmat, data->active);
1758 m_freem(data->m);
1759 data->m = NULL;
1760 }
1761 }
1762
1763 bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize,
1764 BUS_DMASYNC_PREWRITE);
1765
1766 ring->queued = 0;
1767 ring->cur = ring->next = 0;
1768 }
1769
1770 void
1771 nfe_free_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring)
1772 {
1773 struct nfe_tx_data *data;
1774 void *desc;
1775 int i, descsize;
1776
1777 if (sc->sc_flags & NFE_40BIT_ADDR) {
1778 desc = ring->desc64;
1779 descsize = sizeof (struct nfe_desc64);
1780 } else {
1781 desc = ring->desc32;
1782 descsize = sizeof (struct nfe_desc32);
1783 }
1784
1785 if (desc != NULL) {
1786 bus_dmamap_sync(sc->sc_dmat, ring->map, 0,
1787 ring->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1788 bus_dmamap_unload(sc->sc_dmat, ring->map);
1789 bus_dmamem_unmap(sc->sc_dmat, (void *)desc,
1790 NFE_TX_RING_COUNT * descsize);
1791 bus_dmamem_free(sc->sc_dmat, &ring->seg, 1);
1792 }
1793
1794 for (i = 0; i < NFE_TX_RING_COUNT; i++) {
1795 data = &ring->data[i];
1796
1797 if (data->m != NULL) {
1798 bus_dmamap_sync(sc->sc_dmat, data->active, 0,
1799 data->active->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1800 bus_dmamap_unload(sc->sc_dmat, data->active);
1801 m_freem(data->m);
1802 }
1803 }
1804
1805 /* ..and now actually destroy the DMA mappings */
1806 for (i = 0; i < NFE_TX_RING_COUNT; i++) {
1807 data = &ring->data[i];
1808 if (data->map == NULL)
1809 continue;
1810 bus_dmamap_destroy(sc->sc_dmat, data->map);
1811 }
1812 }
1813
1814 void
1815 nfe_setmulti(struct nfe_softc *sc)
1816 {
1817 struct ethercom *ec = &sc->sc_ethercom;
1818 struct ifnet *ifp = &ec->ec_if;
1819 struct ether_multi *enm;
1820 struct ether_multistep step;
1821 uint8_t addr[ETHER_ADDR_LEN], mask[ETHER_ADDR_LEN];
1822 uint32_t filter = NFE_RXFILTER_MAGIC;
1823 int i;
1824
1825 if ((ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
1826 bzero(addr, ETHER_ADDR_LEN);
1827 bzero(mask, ETHER_ADDR_LEN);
1828 goto done;
1829 }
1830
1831 bcopy(etherbroadcastaddr, addr, ETHER_ADDR_LEN);
1832 bcopy(etherbroadcastaddr, mask, ETHER_ADDR_LEN);
1833
1834 ETHER_FIRST_MULTI(step, ec, enm);
1835 while (enm != NULL) {
1836 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1837 ifp->if_flags |= IFF_ALLMULTI;
1838 bzero(addr, ETHER_ADDR_LEN);
1839 bzero(mask, ETHER_ADDR_LEN);
1840 goto done;
1841 }
1842 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1843 addr[i] &= enm->enm_addrlo[i];
1844 mask[i] &= ~enm->enm_addrlo[i];
1845 }
1846 ETHER_NEXT_MULTI(step, enm);
1847 }
1848 for (i = 0; i < ETHER_ADDR_LEN; i++)
1849 mask[i] |= addr[i];
1850
1851 done:
1852 addr[0] |= 0x01; /* make sure multicast bit is set */
1853
1854 NFE_WRITE(sc, NFE_MULTIADDR_HI,
1855 addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]);
1856 NFE_WRITE(sc, NFE_MULTIADDR_LO,
1857 addr[5] << 8 | addr[4]);
1858 NFE_WRITE(sc, NFE_MULTIMASK_HI,
1859 mask[3] << 24 | mask[2] << 16 | mask[1] << 8 | mask[0]);
1860 NFE_WRITE(sc, NFE_MULTIMASK_LO,
1861 mask[5] << 8 | mask[4]);
1862
1863 filter |= (ifp->if_flags & IFF_PROMISC) ? NFE_PROMISC : NFE_U2M;
1864 NFE_WRITE(sc, NFE_RXFILTER, filter);
1865 }
1866
1867 void
1868 nfe_get_macaddr(struct nfe_softc *sc, uint8_t *addr)
1869 {
1870 uint32_t tmp;
1871
1872 if ((sc->sc_flags & NFE_CORRECT_MACADDR) != 0) {
1873 tmp = NFE_READ(sc, NFE_MACADDR_HI);
1874 addr[0] = (tmp & 0xff);
1875 addr[1] = (tmp >> 8) & 0xff;
1876 addr[2] = (tmp >> 16) & 0xff;
1877 addr[3] = (tmp >> 24) & 0xff;
1878
1879 tmp = NFE_READ(sc, NFE_MACADDR_LO);
1880 addr[4] = (tmp & 0xff);
1881 addr[5] = (tmp >> 8) & 0xff;
1882
1883 } else {
1884 tmp = NFE_READ(sc, NFE_MACADDR_LO);
1885 addr[0] = (tmp >> 8) & 0xff;
1886 addr[1] = (tmp & 0xff);
1887
1888 tmp = NFE_READ(sc, NFE_MACADDR_HI);
1889 addr[2] = (tmp >> 24) & 0xff;
1890 addr[3] = (tmp >> 16) & 0xff;
1891 addr[4] = (tmp >> 8) & 0xff;
1892 addr[5] = (tmp & 0xff);
1893 }
1894 }
1895
1896 void
1897 nfe_set_macaddr(struct nfe_softc *sc, const uint8_t *addr)
1898 {
1899 NFE_WRITE(sc, NFE_MACADDR_LO,
1900 addr[5] << 8 | addr[4]);
1901 NFE_WRITE(sc, NFE_MACADDR_HI,
1902 addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]);
1903 }
1904
1905 void
1906 nfe_tick(void *arg)
1907 {
1908 struct nfe_softc *sc = arg;
1909 int s;
1910
1911 s = splnet();
1912 mii_tick(&sc->sc_mii);
1913 splx(s);
1914
1915 callout_schedule(&sc->sc_tick_ch, hz);
1916 }
1917