1098ca2bdSWarner Losh /*-
2df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni *
4a07bd003SBill Paul * Copyright (c) 2004
5a07bd003SBill Paul * Bill Paul <wpaul@windriver.com>. All rights reserved.
6a07bd003SBill Paul *
7a07bd003SBill Paul * Redistribution and use in source and binary forms, with or without
8a07bd003SBill Paul * modification, are permitted provided that the following conditions
9a07bd003SBill Paul * are met:
10a07bd003SBill Paul * 1. Redistributions of source code must retain the above copyright
11a07bd003SBill Paul * notice, this list of conditions and the following disclaimer.
12a07bd003SBill Paul * 2. Redistributions in binary form must reproduce the above copyright
13a07bd003SBill Paul * notice, this list of conditions and the following disclaimer in the
14a07bd003SBill Paul * documentation and/or other materials provided with the distribution.
15a07bd003SBill Paul * 3. All advertising materials mentioning features or use of this software
16a07bd003SBill Paul * must display the following acknowledgement:
17a07bd003SBill Paul * This product includes software developed by Bill Paul.
18a07bd003SBill Paul * 4. Neither the name of the author nor the names of any co-contributors
19a07bd003SBill Paul * may be used to endorse or promote products derived from this software
20a07bd003SBill Paul * without specific prior written permission.
21a07bd003SBill Paul *
22a07bd003SBill Paul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23a07bd003SBill Paul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24a07bd003SBill Paul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25a07bd003SBill Paul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26a07bd003SBill Paul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27a07bd003SBill Paul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28a07bd003SBill Paul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29a07bd003SBill Paul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30a07bd003SBill Paul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31a07bd003SBill Paul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32a07bd003SBill Paul * THE POSSIBILITY OF SUCH DAMAGE.
33a07bd003SBill Paul */
34a07bd003SBill Paul
35a07bd003SBill Paul #include <sys/cdefs.h>
36a07bd003SBill Paul /*
37a07bd003SBill Paul * VIA Networking Technologies VT612x PCI gigabit ethernet NIC driver.
38a07bd003SBill Paul *
39a07bd003SBill Paul * Written by Bill Paul <wpaul@windriver.com>
40a07bd003SBill Paul * Senior Networking Software Engineer
41a07bd003SBill Paul * Wind River Systems
42a07bd003SBill Paul */
43a07bd003SBill Paul
44a07bd003SBill Paul /*
45a07bd003SBill Paul * The VIA Networking VT6122 is a 32bit, 33/66Mhz PCI device that
46a07bd003SBill Paul * combines a tri-speed ethernet MAC and PHY, with the following
47a07bd003SBill Paul * features:
48a07bd003SBill Paul *
49a07bd003SBill Paul * o Jumbo frame support up to 16K
50a07bd003SBill Paul * o Transmit and receive flow control
51a07bd003SBill Paul * o IPv4 checksum offload
52a07bd003SBill Paul * o VLAN tag insertion and stripping
53a07bd003SBill Paul * o TCP large send
54a07bd003SBill Paul * o 64-bit multicast hash table filter
55a07bd003SBill Paul * o 64 entry CAM filter
56a07bd003SBill Paul * o 16K RX FIFO and 48K TX FIFO memory
57a07bd003SBill Paul * o Interrupt moderation
58a07bd003SBill Paul *
59a07bd003SBill Paul * The VT6122 supports up to four transmit DMA queues. The descriptors
60a07bd003SBill Paul * in the transmit ring can address up to 7 data fragments; frames which
61a07bd003SBill Paul * span more than 7 data buffers must be coalesced, but in general the
62a07bd003SBill Paul * BSD TCP/IP stack rarely generates frames more than 2 or 3 fragments
63a07bd003SBill Paul * long. The receive descriptors address only a single buffer.
64a07bd003SBill Paul *
65a07bd003SBill Paul * There are two peculiar design issues with the VT6122. One is that
66a07bd003SBill Paul * receive data buffers must be aligned on a 32-bit boundary. This is
67a07bd003SBill Paul * not a problem where the VT6122 is used as a LOM device in x86-based
68a07bd003SBill Paul * systems, but on architectures that generate unaligned access traps, we
69a07bd003SBill Paul * have to do some copying.
70a07bd003SBill Paul *
71a07bd003SBill Paul * The other issue has to do with the way 64-bit addresses are handled.
72a07bd003SBill Paul * The DMA descriptors only allow you to specify 48 bits of addressing
73a07bd003SBill Paul * information. The remaining 16 bits are specified using one of the
74a07bd003SBill Paul * I/O registers. If you only have a 32-bit system, then this isn't
75a07bd003SBill Paul * an issue, but if you have a 64-bit system and more than 4GB of
76a07bd003SBill Paul * memory, you must have to make sure your network data buffers reside
77a07bd003SBill Paul * in the same 48-bit 'segment.'
78a07bd003SBill Paul *
79a07bd003SBill Paul * Special thanks to Ryan Fu at VIA Networking for providing documentation
80a07bd003SBill Paul * and sample NICs for testing.
81a07bd003SBill Paul */
82a07bd003SBill Paul
83f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
84f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
85f0796cd2SGleb Smirnoff #endif
86f0796cd2SGleb Smirnoff
87a07bd003SBill Paul #include <sys/param.h>
88a07bd003SBill Paul #include <sys/endian.h>
89a07bd003SBill Paul #include <sys/systm.h>
90a07bd003SBill Paul #include <sys/sockio.h>
91a07bd003SBill Paul #include <sys/mbuf.h>
92a07bd003SBill Paul #include <sys/malloc.h>
93a07bd003SBill Paul #include <sys/module.h>
94a07bd003SBill Paul #include <sys/kernel.h>
95a07bd003SBill Paul #include <sys/socket.h>
967129fb20SPyun YongHyeon #include <sys/sysctl.h>
97a07bd003SBill Paul
98a07bd003SBill Paul #include <net/if.h>
99a07bd003SBill Paul #include <net/if_arp.h>
100a07bd003SBill Paul #include <net/ethernet.h>
101a07bd003SBill Paul #include <net/if_dl.h>
10276039bc8SGleb Smirnoff #include <net/if_var.h>
103a07bd003SBill Paul #include <net/if_media.h>
104fc74a9f9SBrooks Davis #include <net/if_types.h>
105a07bd003SBill Paul #include <net/if_vlan_var.h>
106a07bd003SBill Paul
107a07bd003SBill Paul #include <net/bpf.h>
108a07bd003SBill Paul
109a07bd003SBill Paul #include <machine/bus.h>
110a07bd003SBill Paul #include <machine/resource.h>
111a07bd003SBill Paul #include <sys/bus.h>
112a07bd003SBill Paul #include <sys/rman.h>
113a07bd003SBill Paul
114a07bd003SBill Paul #include <dev/mii/mii.h>
115a07bd003SBill Paul #include <dev/mii/miivar.h>
116a07bd003SBill Paul
117a07bd003SBill Paul #include <dev/pci/pcireg.h>
118a07bd003SBill Paul #include <dev/pci/pcivar.h>
119a07bd003SBill Paul
120a07bd003SBill Paul MODULE_DEPEND(vge, pci, 1, 1, 1);
121a07bd003SBill Paul MODULE_DEPEND(vge, ether, 1, 1, 1);
122a07bd003SBill Paul MODULE_DEPEND(vge, miibus, 1, 1, 1);
123a07bd003SBill Paul
1247b279558SWarner Losh /* "device miibus" required. See GENERIC if you get errors here. */
125a07bd003SBill Paul #include "miibus_if.h"
126a07bd003SBill Paul
127a07bd003SBill Paul #include <dev/vge/if_vgereg.h>
128a07bd003SBill Paul #include <dev/vge/if_vgevar.h>
129a07bd003SBill Paul
130a07bd003SBill Paul #define VGE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
131a07bd003SBill Paul
1325957cc2aSPyun YongHyeon /* Tunables */
1335957cc2aSPyun YongHyeon static int msi_disable = 0;
1345957cc2aSPyun YongHyeon TUNABLE_INT("hw.vge.msi_disable", &msi_disable);
1355957cc2aSPyun YongHyeon
136a07bd003SBill Paul /*
1377129fb20SPyun YongHyeon * The SQE error counter of MIB seems to report bogus value.
1387129fb20SPyun YongHyeon * Vendor's workaround does not seem to work on PCIe based
1397129fb20SPyun YongHyeon * controllers. Disable it until we find better workaround.
1407129fb20SPyun YongHyeon */
1417129fb20SPyun YongHyeon #undef VGE_ENABLE_SQEERR
1427129fb20SPyun YongHyeon
1437129fb20SPyun YongHyeon /*
144a07bd003SBill Paul * Various supported device vendors/types and their names.
145a07bd003SBill Paul */
146a07bd003SBill Paul static struct vge_type vge_devs[] = {
147a07bd003SBill Paul { VIA_VENDORID, VIA_DEVICEID_61XX,
14883accfdbSPyun YongHyeon "VIA Networking Velocity Gigabit Ethernet" },
149a07bd003SBill Paul { 0, 0, NULL }
150a07bd003SBill Paul };
151a07bd003SBill Paul
152a07bd003SBill Paul static int vge_attach(device_t);
153a07bd003SBill Paul static int vge_detach(device_t);
154e4027c49SPyun YongHyeon static int vge_probe(device_t);
155a07bd003SBill Paul static int vge_resume(device_t);
1566a087a87SPyun YongHyeon static int vge_shutdown(device_t);
157e4027c49SPyun YongHyeon static int vge_suspend(device_t);
158a07bd003SBill Paul
159a07bd003SBill Paul static void vge_cam_clear(struct vge_softc *);
160a07bd003SBill Paul static int vge_cam_set(struct vge_softc *, uint8_t *);
1617fc94bc4SPyun YongHyeon static void vge_clrwol(struct vge_softc *);
162e4027c49SPyun YongHyeon static void vge_discard_rxbuf(struct vge_softc *, int);
163e4027c49SPyun YongHyeon static int vge_dma_alloc(struct vge_softc *);
164e4027c49SPyun YongHyeon static void vge_dma_free(struct vge_softc *);
165e4027c49SPyun YongHyeon static void vge_dmamap_cb(void *, bus_dma_segment_t *, int, int);
166e4027c49SPyun YongHyeon #ifdef VGE_EEPROM
167e4027c49SPyun YongHyeon static void vge_eeprom_getword(struct vge_softc *, int, uint16_t *);
168e4027c49SPyun YongHyeon #endif
169e4027c49SPyun YongHyeon static int vge_encap(struct vge_softc *, struct mbuf **);
170e4027c49SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
171e4027c49SPyun YongHyeon static __inline void
172e4027c49SPyun YongHyeon vge_fixup_rx(struct mbuf *);
173e4027c49SPyun YongHyeon #endif
174e4027c49SPyun YongHyeon static void vge_freebufs(struct vge_softc *);
1753ac0cb32SJustin Hibbits static void vge_ifmedia_sts(if_t, struct ifmediareq *);
1763ac0cb32SJustin Hibbits static int vge_ifmedia_upd(if_t);
17766c6108dSPyun YongHyeon static int vge_ifmedia_upd_locked(struct vge_softc *);
178e4027c49SPyun YongHyeon static void vge_init(void *);
179e4027c49SPyun YongHyeon static void vge_init_locked(struct vge_softc *);
180e4027c49SPyun YongHyeon static void vge_intr(void *);
1813b2b8afbSPyun YongHyeon static void vge_intr_holdoff(struct vge_softc *);
1823ac0cb32SJustin Hibbits static int vge_ioctl(if_t, u_long, caddr_t);
183e7b2d9b8SPyun YongHyeon static void vge_link_statchg(void *);
184e4027c49SPyun YongHyeon static int vge_miibus_readreg(device_t, int, int);
185e4027c49SPyun YongHyeon static int vge_miibus_writereg(device_t, int, int, int);
186e4027c49SPyun YongHyeon static void vge_miipoll_start(struct vge_softc *);
187e4027c49SPyun YongHyeon static void vge_miipoll_stop(struct vge_softc *);
188e4027c49SPyun YongHyeon static int vge_newbuf(struct vge_softc *, int);
189e4027c49SPyun YongHyeon static void vge_read_eeprom(struct vge_softc *, caddr_t, int, int, int);
190a07bd003SBill Paul static void vge_reset(struct vge_softc *);
191e4027c49SPyun YongHyeon static int vge_rx_list_init(struct vge_softc *);
192e4027c49SPyun YongHyeon static int vge_rxeof(struct vge_softc *, int);
1935f07fd19SPyun YongHyeon static void vge_rxfilter(struct vge_softc *);
19466c6108dSPyun YongHyeon static void vge_setmedia(struct vge_softc *);
19538aa43c5SPyun YongHyeon static void vge_setvlan(struct vge_softc *);
1967fc94bc4SPyun YongHyeon static void vge_setwol(struct vge_softc *);
1973ac0cb32SJustin Hibbits static void vge_start(if_t);
1983ac0cb32SJustin Hibbits static void vge_start_locked(if_t);
1997129fb20SPyun YongHyeon static void vge_stats_clear(struct vge_softc *);
2007129fb20SPyun YongHyeon static void vge_stats_update(struct vge_softc *);
201e4027c49SPyun YongHyeon static void vge_stop(struct vge_softc *);
2027129fb20SPyun YongHyeon static void vge_sysctl_node(struct vge_softc *);
203e4027c49SPyun YongHyeon static int vge_tx_list_init(struct vge_softc *);
204e4027c49SPyun YongHyeon static void vge_txeof(struct vge_softc *);
205e4027c49SPyun YongHyeon static void vge_watchdog(void *);
206a07bd003SBill Paul
207a07bd003SBill Paul static device_method_t vge_methods[] = {
208a07bd003SBill Paul /* Device interface */
209a07bd003SBill Paul DEVMETHOD(device_probe, vge_probe),
210a07bd003SBill Paul DEVMETHOD(device_attach, vge_attach),
211a07bd003SBill Paul DEVMETHOD(device_detach, vge_detach),
212a07bd003SBill Paul DEVMETHOD(device_suspend, vge_suspend),
213a07bd003SBill Paul DEVMETHOD(device_resume, vge_resume),
214a07bd003SBill Paul DEVMETHOD(device_shutdown, vge_shutdown),
215a07bd003SBill Paul
216a07bd003SBill Paul /* MII interface */
217a07bd003SBill Paul DEVMETHOD(miibus_readreg, vge_miibus_readreg),
218a07bd003SBill Paul DEVMETHOD(miibus_writereg, vge_miibus_writereg),
219a07bd003SBill Paul
2204b7ec270SMarius Strobl DEVMETHOD_END
221a07bd003SBill Paul };
222a07bd003SBill Paul
223a07bd003SBill Paul static driver_t vge_driver = {
224a07bd003SBill Paul "vge",
225a07bd003SBill Paul vge_methods,
226a07bd003SBill Paul sizeof(struct vge_softc)
227a07bd003SBill Paul };
228a07bd003SBill Paul
229d20c52b6SJohn Baldwin DRIVER_MODULE(vge, pci, vge_driver, 0, 0);
2303e38757dSJohn Baldwin DRIVER_MODULE(miibus, vge, miibus_driver, 0, 0);
231a07bd003SBill Paul
232bb74e5f6SBill Paul #ifdef VGE_EEPROM
233a07bd003SBill Paul /*
234a07bd003SBill Paul * Read a word of data stored in the EEPROM at address 'addr.'
235a07bd003SBill Paul */
236a07bd003SBill Paul static void
vge_eeprom_getword(struct vge_softc * sc,int addr,uint16_t * dest)237c3c74c61SPyun YongHyeon vge_eeprom_getword(struct vge_softc *sc, int addr, uint16_t *dest)
238a07bd003SBill Paul {
239b534dcd5SPyun YongHyeon int i;
240c3c74c61SPyun YongHyeon uint16_t word = 0;
241a07bd003SBill Paul
242a07bd003SBill Paul /*
243a07bd003SBill Paul * Enter EEPROM embedded programming mode. In order to
244a07bd003SBill Paul * access the EEPROM at all, we first have to set the
245a07bd003SBill Paul * EELOAD bit in the CHIPCFG2 register.
246a07bd003SBill Paul */
247a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
248a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
249a07bd003SBill Paul
250a07bd003SBill Paul /* Select the address of the word we want to read */
251a07bd003SBill Paul CSR_WRITE_1(sc, VGE_EEADDR, addr);
252a07bd003SBill Paul
253a07bd003SBill Paul /* Issue read command */
254a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_EECMD, VGE_EECMD_ERD);
255a07bd003SBill Paul
256a07bd003SBill Paul /* Wait for the done bit to be set. */
257a07bd003SBill Paul for (i = 0; i < VGE_TIMEOUT; i++) {
258a07bd003SBill Paul if (CSR_READ_1(sc, VGE_EECMD) & VGE_EECMD_EDONE)
259a07bd003SBill Paul break;
260a07bd003SBill Paul }
261a07bd003SBill Paul
262a07bd003SBill Paul if (i == VGE_TIMEOUT) {
263a07bd003SBill Paul device_printf(sc->vge_dev, "EEPROM read timed out\n");
264a07bd003SBill Paul *dest = 0;
265a07bd003SBill Paul return;
266a07bd003SBill Paul }
267a07bd003SBill Paul
268a07bd003SBill Paul /* Read the result */
269a07bd003SBill Paul word = CSR_READ_2(sc, VGE_EERDDAT);
270a07bd003SBill Paul
271a07bd003SBill Paul /* Turn off EEPROM access mode. */
272a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
273a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
274a07bd003SBill Paul
275a07bd003SBill Paul *dest = word;
276a07bd003SBill Paul }
277bb74e5f6SBill Paul #endif
278a07bd003SBill Paul
279a07bd003SBill Paul /*
280a07bd003SBill Paul * Read a sequence of words from the EEPROM.
281a07bd003SBill Paul */
282a07bd003SBill Paul static void
vge_read_eeprom(struct vge_softc * sc,caddr_t dest,int off,int cnt,int swap)2836afe22a8SPyun YongHyeon vge_read_eeprom(struct vge_softc *sc, caddr_t dest, int off, int cnt, int swap)
284a07bd003SBill Paul {
285a07bd003SBill Paul int i;
286bb74e5f6SBill Paul #ifdef VGE_EEPROM
287c3c74c61SPyun YongHyeon uint16_t word = 0, *ptr;
288a07bd003SBill Paul
289a07bd003SBill Paul for (i = 0; i < cnt; i++) {
290a07bd003SBill Paul vge_eeprom_getword(sc, off + i, &word);
291c3c74c61SPyun YongHyeon ptr = (uint16_t *)(dest + (i * 2));
292a07bd003SBill Paul if (swap)
293a07bd003SBill Paul *ptr = ntohs(word);
294a07bd003SBill Paul else
295a07bd003SBill Paul *ptr = word;
296a07bd003SBill Paul }
297bb74e5f6SBill Paul #else
298bb74e5f6SBill Paul for (i = 0; i < ETHER_ADDR_LEN; i++)
299bb74e5f6SBill Paul dest[i] = CSR_READ_1(sc, VGE_PAR0 + i);
300bb74e5f6SBill Paul #endif
301a07bd003SBill Paul }
302a07bd003SBill Paul
303a07bd003SBill Paul static void
vge_miipoll_stop(struct vge_softc * sc)3046afe22a8SPyun YongHyeon vge_miipoll_stop(struct vge_softc *sc)
305a07bd003SBill Paul {
306a07bd003SBill Paul int i;
307a07bd003SBill Paul
308a07bd003SBill Paul CSR_WRITE_1(sc, VGE_MIICMD, 0);
309a07bd003SBill Paul
310a07bd003SBill Paul for (i = 0; i < VGE_TIMEOUT; i++) {
311a07bd003SBill Paul DELAY(1);
312a07bd003SBill Paul if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
313a07bd003SBill Paul break;
314a07bd003SBill Paul }
315a07bd003SBill Paul
316a07bd003SBill Paul if (i == VGE_TIMEOUT)
317a07bd003SBill Paul device_printf(sc->vge_dev, "failed to idle MII autopoll\n");
318a07bd003SBill Paul }
319a07bd003SBill Paul
320a07bd003SBill Paul static void
vge_miipoll_start(struct vge_softc * sc)3216afe22a8SPyun YongHyeon vge_miipoll_start(struct vge_softc *sc)
322a07bd003SBill Paul {
323a07bd003SBill Paul int i;
324a07bd003SBill Paul
325a07bd003SBill Paul /* First, make sure we're idle. */
326a07bd003SBill Paul
327a07bd003SBill Paul CSR_WRITE_1(sc, VGE_MIICMD, 0);
328a07bd003SBill Paul CSR_WRITE_1(sc, VGE_MIIADDR, VGE_MIIADDR_SWMPL);
329a07bd003SBill Paul
330a07bd003SBill Paul for (i = 0; i < VGE_TIMEOUT; i++) {
331a07bd003SBill Paul DELAY(1);
332a07bd003SBill Paul if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
333a07bd003SBill Paul break;
334a07bd003SBill Paul }
335a07bd003SBill Paul
336a07bd003SBill Paul if (i == VGE_TIMEOUT) {
337a07bd003SBill Paul device_printf(sc->vge_dev, "failed to idle MII autopoll\n");
338a07bd003SBill Paul return;
339a07bd003SBill Paul }
340a07bd003SBill Paul
341a07bd003SBill Paul /* Now enable auto poll mode. */
342a07bd003SBill Paul
343a07bd003SBill Paul CSR_WRITE_1(sc, VGE_MIICMD, VGE_MIICMD_MAUTO);
344a07bd003SBill Paul
345a07bd003SBill Paul /* And make sure it started. */
346a07bd003SBill Paul
347a07bd003SBill Paul for (i = 0; i < VGE_TIMEOUT; i++) {
348a07bd003SBill Paul DELAY(1);
349a07bd003SBill Paul if ((CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL) == 0)
350a07bd003SBill Paul break;
351a07bd003SBill Paul }
352a07bd003SBill Paul
353a07bd003SBill Paul if (i == VGE_TIMEOUT)
354a07bd003SBill Paul device_printf(sc->vge_dev, "failed to start MII autopoll\n");
355a07bd003SBill Paul }
356a07bd003SBill Paul
357a07bd003SBill Paul static int
vge_miibus_readreg(device_t dev,int phy,int reg)3586afe22a8SPyun YongHyeon vge_miibus_readreg(device_t dev, int phy, int reg)
359a07bd003SBill Paul {
360a07bd003SBill Paul struct vge_softc *sc;
361a07bd003SBill Paul int i;
362c3c74c61SPyun YongHyeon uint16_t rval = 0;
363a07bd003SBill Paul
364a07bd003SBill Paul sc = device_get_softc(dev);
365a07bd003SBill Paul
366a07bd003SBill Paul vge_miipoll_stop(sc);
367a07bd003SBill Paul
368a07bd003SBill Paul /* Specify the register we want to read. */
369a07bd003SBill Paul CSR_WRITE_1(sc, VGE_MIIADDR, reg);
370a07bd003SBill Paul
371a07bd003SBill Paul /* Issue read command. */
372a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_RCMD);
373a07bd003SBill Paul
374a07bd003SBill Paul /* Wait for the read command bit to self-clear. */
375a07bd003SBill Paul for (i = 0; i < VGE_TIMEOUT; i++) {
376a07bd003SBill Paul DELAY(1);
377a07bd003SBill Paul if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_RCMD) == 0)
378a07bd003SBill Paul break;
379a07bd003SBill Paul }
380a07bd003SBill Paul
381a07bd003SBill Paul if (i == VGE_TIMEOUT)
382a07bd003SBill Paul device_printf(sc->vge_dev, "MII read timed out\n");
383a07bd003SBill Paul else
384a07bd003SBill Paul rval = CSR_READ_2(sc, VGE_MIIDATA);
385a07bd003SBill Paul
386a07bd003SBill Paul vge_miipoll_start(sc);
387a07bd003SBill Paul
388a07bd003SBill Paul return (rval);
389a07bd003SBill Paul }
390a07bd003SBill Paul
391a07bd003SBill Paul static int
vge_miibus_writereg(device_t dev,int phy,int reg,int data)3926afe22a8SPyun YongHyeon vge_miibus_writereg(device_t dev, int phy, int reg, int data)
393a07bd003SBill Paul {
394a07bd003SBill Paul struct vge_softc *sc;
395a07bd003SBill Paul int i, rval = 0;
396a07bd003SBill Paul
397a07bd003SBill Paul sc = device_get_softc(dev);
398a07bd003SBill Paul
399a07bd003SBill Paul vge_miipoll_stop(sc);
400a07bd003SBill Paul
401a07bd003SBill Paul /* Specify the register we want to write. */
402a07bd003SBill Paul CSR_WRITE_1(sc, VGE_MIIADDR, reg);
403a07bd003SBill Paul
404a07bd003SBill Paul /* Specify the data we want to write. */
405a07bd003SBill Paul CSR_WRITE_2(sc, VGE_MIIDATA, data);
406a07bd003SBill Paul
407a07bd003SBill Paul /* Issue write command. */
408a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_WCMD);
409a07bd003SBill Paul
410a07bd003SBill Paul /* Wait for the write command bit to self-clear. */
411a07bd003SBill Paul for (i = 0; i < VGE_TIMEOUT; i++) {
412a07bd003SBill Paul DELAY(1);
413a07bd003SBill Paul if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_WCMD) == 0)
414a07bd003SBill Paul break;
415a07bd003SBill Paul }
416a07bd003SBill Paul
417a07bd003SBill Paul if (i == VGE_TIMEOUT) {
418a07bd003SBill Paul device_printf(sc->vge_dev, "MII write timed out\n");
419a07bd003SBill Paul rval = EIO;
420a07bd003SBill Paul }
421a07bd003SBill Paul
422a07bd003SBill Paul vge_miipoll_start(sc);
423a07bd003SBill Paul
424a07bd003SBill Paul return (rval);
425a07bd003SBill Paul }
426a07bd003SBill Paul
427a07bd003SBill Paul static void
vge_cam_clear(struct vge_softc * sc)4286afe22a8SPyun YongHyeon vge_cam_clear(struct vge_softc *sc)
429a07bd003SBill Paul {
430a07bd003SBill Paul int i;
431a07bd003SBill Paul
432a07bd003SBill Paul /*
433a07bd003SBill Paul * Turn off all the mask bits. This tells the chip
434a07bd003SBill Paul * that none of the entries in the CAM filter are valid.
435a07bd003SBill Paul * desired entries will be enabled as we fill the filter in.
436a07bd003SBill Paul */
437a07bd003SBill Paul
438a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
439a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
440a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE);
441a07bd003SBill Paul for (i = 0; i < 8; i++)
442a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
443a07bd003SBill Paul
444a07bd003SBill Paul /* Clear the VLAN filter too. */
445a07bd003SBill Paul
446a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|VGE_CAMADDR_AVSEL|0);
447a07bd003SBill Paul for (i = 0; i < 8; i++)
448a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
449a07bd003SBill Paul
450a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CAMADDR, 0);
451a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
452a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
453a07bd003SBill Paul
454a07bd003SBill Paul sc->vge_camidx = 0;
455a07bd003SBill Paul }
456a07bd003SBill Paul
457a07bd003SBill Paul static int
vge_cam_set(struct vge_softc * sc,uint8_t * addr)4586afe22a8SPyun YongHyeon vge_cam_set(struct vge_softc *sc, uint8_t *addr)
459a07bd003SBill Paul {
460a07bd003SBill Paul int i, error = 0;
461a07bd003SBill Paul
462a07bd003SBill Paul if (sc->vge_camidx == VGE_CAM_MAXADDRS)
463a07bd003SBill Paul return (ENOSPC);
464a07bd003SBill Paul
465a07bd003SBill Paul /* Select the CAM data page. */
466a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
467a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMDATA);
468a07bd003SBill Paul
469a07bd003SBill Paul /* Set the filter entry we want to update and enable writing. */
470a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|sc->vge_camidx);
471a07bd003SBill Paul
472a07bd003SBill Paul /* Write the address to the CAM registers */
473a07bd003SBill Paul for (i = 0; i < ETHER_ADDR_LEN; i++)
474a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CAM0 + i, addr[i]);
475a07bd003SBill Paul
476a07bd003SBill Paul /* Issue a write command. */
477a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_WRITE);
478a07bd003SBill Paul
479a07bd003SBill Paul /* Wake for it to clear. */
480a07bd003SBill Paul for (i = 0; i < VGE_TIMEOUT; i++) {
481a07bd003SBill Paul DELAY(1);
482a07bd003SBill Paul if ((CSR_READ_1(sc, VGE_CAMCTL) & VGE_CAMCTL_WRITE) == 0)
483a07bd003SBill Paul break;
484a07bd003SBill Paul }
485a07bd003SBill Paul
486a07bd003SBill Paul if (i == VGE_TIMEOUT) {
487a07bd003SBill Paul device_printf(sc->vge_dev, "setting CAM filter failed\n");
488a07bd003SBill Paul error = EIO;
489a07bd003SBill Paul goto fail;
490a07bd003SBill Paul }
491a07bd003SBill Paul
492a07bd003SBill Paul /* Select the CAM mask page. */
493a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
494a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
495a07bd003SBill Paul
496a07bd003SBill Paul /* Set the mask bit that enables this filter. */
497a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_CAM0 + (sc->vge_camidx/8),
498a07bd003SBill Paul 1<<(sc->vge_camidx & 7));
499a07bd003SBill Paul
500a07bd003SBill Paul sc->vge_camidx++;
501a07bd003SBill Paul
502a07bd003SBill Paul fail:
503a07bd003SBill Paul /* Turn off access to CAM. */
504a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CAMADDR, 0);
505a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
506a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
507a07bd003SBill Paul
508a07bd003SBill Paul return (error);
509a07bd003SBill Paul }
510a07bd003SBill Paul
51138aa43c5SPyun YongHyeon static void
vge_setvlan(struct vge_softc * sc)51238aa43c5SPyun YongHyeon vge_setvlan(struct vge_softc *sc)
51338aa43c5SPyun YongHyeon {
5143ac0cb32SJustin Hibbits if_t ifp;
51538aa43c5SPyun YongHyeon uint8_t cfg;
51638aa43c5SPyun YongHyeon
51738aa43c5SPyun YongHyeon VGE_LOCK_ASSERT(sc);
51838aa43c5SPyun YongHyeon
51938aa43c5SPyun YongHyeon ifp = sc->vge_ifp;
52038aa43c5SPyun YongHyeon cfg = CSR_READ_1(sc, VGE_RXCFG);
5213ac0cb32SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0)
52238aa43c5SPyun YongHyeon cfg |= VGE_VTAG_OPT2;
52338aa43c5SPyun YongHyeon else
52438aa43c5SPyun YongHyeon cfg &= ~VGE_VTAG_OPT2;
52538aa43c5SPyun YongHyeon CSR_WRITE_1(sc, VGE_RXCFG, cfg);
52638aa43c5SPyun YongHyeon }
52738aa43c5SPyun YongHyeon
52844a30c62SGleb Smirnoff static u_int
vge_set_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)52944a30c62SGleb Smirnoff vge_set_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
53044a30c62SGleb Smirnoff {
53144a30c62SGleb Smirnoff struct vge_softc *sc = arg;
53244a30c62SGleb Smirnoff
53344a30c62SGleb Smirnoff if (sc->vge_camidx == VGE_CAM_MAXADDRS)
53444a30c62SGleb Smirnoff return (0);
53544a30c62SGleb Smirnoff
53644a30c62SGleb Smirnoff (void )vge_cam_set(sc, LLADDR(sdl));
53744a30c62SGleb Smirnoff
53844a30c62SGleb Smirnoff return (1);
53944a30c62SGleb Smirnoff }
54044a30c62SGleb Smirnoff
54144a30c62SGleb Smirnoff static u_int
vge_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)54244a30c62SGleb Smirnoff vge_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
54344a30c62SGleb Smirnoff {
54444a30c62SGleb Smirnoff uint32_t h, *hashes = arg;
54544a30c62SGleb Smirnoff
54644a30c62SGleb Smirnoff h = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN) >> 26;
54744a30c62SGleb Smirnoff if (h < 32)
54844a30c62SGleb Smirnoff hashes[0] |= (1 << h);
54944a30c62SGleb Smirnoff else
55044a30c62SGleb Smirnoff hashes[1] |= (1 << (h - 32));
55144a30c62SGleb Smirnoff
55244a30c62SGleb Smirnoff return (1);
55344a30c62SGleb Smirnoff }
55444a30c62SGleb Smirnoff
555a07bd003SBill Paul /*
556a07bd003SBill Paul * Program the multicast filter. We use the 64-entry CAM filter
557a07bd003SBill Paul * for perfect filtering. If there's more than 64 multicast addresses,
5588170b243SPyun YongHyeon * we use the hash filter instead.
559a07bd003SBill Paul */
560a07bd003SBill Paul static void
vge_rxfilter(struct vge_softc * sc)5615f07fd19SPyun YongHyeon vge_rxfilter(struct vge_softc *sc)
562a07bd003SBill Paul {
5633ac0cb32SJustin Hibbits if_t ifp;
56444a30c62SGleb Smirnoff uint32_t hashes[2];
5655f07fd19SPyun YongHyeon uint8_t rxcfg;
566a07bd003SBill Paul
567410f4c60SPyun YongHyeon VGE_LOCK_ASSERT(sc);
568410f4c60SPyun YongHyeon
569a07bd003SBill Paul /* First, zot all the multicast entries. */
5705f07fd19SPyun YongHyeon hashes[0] = 0;
5715f07fd19SPyun YongHyeon hashes[1] = 0;
572a07bd003SBill Paul
5735f07fd19SPyun YongHyeon rxcfg = CSR_READ_1(sc, VGE_RXCTL);
5745f07fd19SPyun YongHyeon rxcfg &= ~(VGE_RXCTL_RX_MCAST | VGE_RXCTL_RX_BCAST |
5755f07fd19SPyun YongHyeon VGE_RXCTL_RX_PROMISC);
576a07bd003SBill Paul /*
5775f07fd19SPyun YongHyeon * Always allow VLAN oversized frames and frames for
5785f07fd19SPyun YongHyeon * this host.
579a07bd003SBill Paul */
5805f07fd19SPyun YongHyeon rxcfg |= VGE_RXCTL_RX_GIANT | VGE_RXCTL_RX_UCAST;
5815f07fd19SPyun YongHyeon
5825f07fd19SPyun YongHyeon ifp = sc->vge_ifp;
5833ac0cb32SJustin Hibbits if ((if_getflags(ifp) & IFF_BROADCAST) != 0)
5845f07fd19SPyun YongHyeon rxcfg |= VGE_RXCTL_RX_BCAST;
5853ac0cb32SJustin Hibbits if ((if_getflags(ifp) & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
5863ac0cb32SJustin Hibbits if ((if_getflags(ifp) & IFF_PROMISC) != 0)
5875f07fd19SPyun YongHyeon rxcfg |= VGE_RXCTL_RX_PROMISC;
5883ac0cb32SJustin Hibbits if ((if_getflags(ifp) & IFF_ALLMULTI) != 0) {
5895f07fd19SPyun YongHyeon hashes[0] = 0xFFFFFFFF;
5905f07fd19SPyun YongHyeon hashes[1] = 0xFFFFFFFF;
5915f07fd19SPyun YongHyeon }
5925f07fd19SPyun YongHyeon goto done;
593a07bd003SBill Paul }
594a07bd003SBill Paul
5955f07fd19SPyun YongHyeon vge_cam_clear(sc);
59644a30c62SGleb Smirnoff
597a07bd003SBill Paul /* Now program new ones */
59844a30c62SGleb Smirnoff if_foreach_llmaddr(ifp, vge_set_maddr, sc);
599a07bd003SBill Paul
600a07bd003SBill Paul /* If there were too many addresses, use the hash filter. */
60144a30c62SGleb Smirnoff if (sc->vge_camidx == VGE_CAM_MAXADDRS) {
602a07bd003SBill Paul vge_cam_clear(sc);
60344a30c62SGleb Smirnoff if_foreach_llmaddr(ifp, vge_hash_maddr, hashes);
604a07bd003SBill Paul }
6055f07fd19SPyun YongHyeon
6065f07fd19SPyun YongHyeon done:
6075f07fd19SPyun YongHyeon if (hashes[0] != 0 || hashes[1] != 0)
6085f07fd19SPyun YongHyeon rxcfg |= VGE_RXCTL_RX_MCAST;
6095f07fd19SPyun YongHyeon CSR_WRITE_4(sc, VGE_MAR0, hashes[0]);
6105f07fd19SPyun YongHyeon CSR_WRITE_4(sc, VGE_MAR1, hashes[1]);
6115f07fd19SPyun YongHyeon CSR_WRITE_1(sc, VGE_RXCTL, rxcfg);
612a07bd003SBill Paul }
613a07bd003SBill Paul
614a07bd003SBill Paul static void
vge_reset(struct vge_softc * sc)6156afe22a8SPyun YongHyeon vge_reset(struct vge_softc *sc)
616a07bd003SBill Paul {
617b534dcd5SPyun YongHyeon int i;
618a07bd003SBill Paul
619a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_SOFTRESET);
620a07bd003SBill Paul
621a07bd003SBill Paul for (i = 0; i < VGE_TIMEOUT; i++) {
622a07bd003SBill Paul DELAY(5);
623a07bd003SBill Paul if ((CSR_READ_1(sc, VGE_CRS1) & VGE_CR1_SOFTRESET) == 0)
624a07bd003SBill Paul break;
625a07bd003SBill Paul }
626a07bd003SBill Paul
627a07bd003SBill Paul if (i == VGE_TIMEOUT) {
62820c3cb15SPyun YongHyeon device_printf(sc->vge_dev, "soft reset timed out\n");
629a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_STOP_FORCE);
630a07bd003SBill Paul DELAY(2000);
631a07bd003SBill Paul }
632a07bd003SBill Paul
633a07bd003SBill Paul DELAY(5000);
634a07bd003SBill Paul }
635a07bd003SBill Paul
636a07bd003SBill Paul /*
637a07bd003SBill Paul * Probe for a VIA gigabit chip. Check the PCI vendor and device
638a07bd003SBill Paul * IDs against our list and return a device name if we find a match.
639a07bd003SBill Paul */
640a07bd003SBill Paul static int
vge_probe(device_t dev)6416afe22a8SPyun YongHyeon vge_probe(device_t dev)
642a07bd003SBill Paul {
643a07bd003SBill Paul struct vge_type *t;
644a07bd003SBill Paul
645a07bd003SBill Paul t = vge_devs;
646a07bd003SBill Paul
647a07bd003SBill Paul while (t->vge_name != NULL) {
648a07bd003SBill Paul if ((pci_get_vendor(dev) == t->vge_vid) &&
649a07bd003SBill Paul (pci_get_device(dev) == t->vge_did)) {
650a07bd003SBill Paul device_set_desc(dev, t->vge_name);
6512ece8174SWarner Losh return (BUS_PROBE_DEFAULT);
652a07bd003SBill Paul }
653a07bd003SBill Paul t++;
654a07bd003SBill Paul }
655a07bd003SBill Paul
656a07bd003SBill Paul return (ENXIO);
657a07bd003SBill Paul }
658a07bd003SBill Paul
659a07bd003SBill Paul /*
660a07bd003SBill Paul * Map a single buffer address.
661a07bd003SBill Paul */
662a07bd003SBill Paul
663410f4c60SPyun YongHyeon struct vge_dmamap_arg {
664410f4c60SPyun YongHyeon bus_addr_t vge_busaddr;
665410f4c60SPyun YongHyeon };
666410f4c60SPyun YongHyeon
667a07bd003SBill Paul static void
vge_dmamap_cb(void * arg,bus_dma_segment_t * segs,int nsegs,int error)6686afe22a8SPyun YongHyeon vge_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
669a07bd003SBill Paul {
670410f4c60SPyun YongHyeon struct vge_dmamap_arg *ctx;
671a07bd003SBill Paul
672410f4c60SPyun YongHyeon if (error != 0)
673a07bd003SBill Paul return;
674a07bd003SBill Paul
675410f4c60SPyun YongHyeon KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
676a07bd003SBill Paul
677410f4c60SPyun YongHyeon ctx = (struct vge_dmamap_arg *)arg;
678410f4c60SPyun YongHyeon ctx->vge_busaddr = segs[0].ds_addr;
679a07bd003SBill Paul }
680a07bd003SBill Paul
681a07bd003SBill Paul static int
vge_dma_alloc(struct vge_softc * sc)6826afe22a8SPyun YongHyeon vge_dma_alloc(struct vge_softc *sc)
683a07bd003SBill Paul {
684410f4c60SPyun YongHyeon struct vge_dmamap_arg ctx;
685410f4c60SPyun YongHyeon struct vge_txdesc *txd;
686410f4c60SPyun YongHyeon struct vge_rxdesc *rxd;
687410f4c60SPyun YongHyeon bus_addr_t lowaddr, tx_ring_end, rx_ring_end;
688410f4c60SPyun YongHyeon int error, i;
689410f4c60SPyun YongHyeon
6907ba75dc4SPyun YongHyeon /*
6917ba75dc4SPyun YongHyeon * It seems old PCI controllers do not support DAC. DAC
6927ba75dc4SPyun YongHyeon * configuration can be enabled by accessing VGE_CHIPCFG3
6937ba75dc4SPyun YongHyeon * register but honor EEPROM configuration instead of
6947ba75dc4SPyun YongHyeon * blindly overriding DAC configuration. PCIe based
6957ba75dc4SPyun YongHyeon * controllers are supposed to support 64bit DMA so enable
6967ba75dc4SPyun YongHyeon * 64bit DMA on these controllers.
6977ba75dc4SPyun YongHyeon */
6987ba75dc4SPyun YongHyeon if ((sc->vge_flags & VGE_FLAG_PCIE) != 0)
699410f4c60SPyun YongHyeon lowaddr = BUS_SPACE_MAXADDR;
7007ba75dc4SPyun YongHyeon else
7017ba75dc4SPyun YongHyeon lowaddr = BUS_SPACE_MAXADDR_32BIT;
702410f4c60SPyun YongHyeon
703410f4c60SPyun YongHyeon again:
704410f4c60SPyun YongHyeon /* Create parent ring tag. */
705410f4c60SPyun YongHyeon error = bus_dma_tag_create(bus_get_dma_tag(sc->vge_dev),/* parent */
706410f4c60SPyun YongHyeon 1, 0, /* algnmnt, boundary */
707410f4c60SPyun YongHyeon lowaddr, /* lowaddr */
708410f4c60SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
709410f4c60SPyun YongHyeon NULL, NULL, /* filter, filterarg */
710410f4c60SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
711410f4c60SPyun YongHyeon 0, /* nsegments */
712410f4c60SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
713410f4c60SPyun YongHyeon 0, /* flags */
714410f4c60SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
715410f4c60SPyun YongHyeon &sc->vge_cdata.vge_ring_tag);
716410f4c60SPyun YongHyeon if (error != 0) {
717410f4c60SPyun YongHyeon device_printf(sc->vge_dev,
718410f4c60SPyun YongHyeon "could not create parent DMA tag.\n");
719410f4c60SPyun YongHyeon goto fail;
720410f4c60SPyun YongHyeon }
721410f4c60SPyun YongHyeon
722410f4c60SPyun YongHyeon /* Create tag for Tx ring. */
723410f4c60SPyun YongHyeon error = bus_dma_tag_create(sc->vge_cdata.vge_ring_tag,/* parent */
724410f4c60SPyun YongHyeon VGE_TX_RING_ALIGN, 0, /* algnmnt, boundary */
725410f4c60SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */
726410f4c60SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
727410f4c60SPyun YongHyeon NULL, NULL, /* filter, filterarg */
728410f4c60SPyun YongHyeon VGE_TX_LIST_SZ, /* maxsize */
729410f4c60SPyun YongHyeon 1, /* nsegments */
730410f4c60SPyun YongHyeon VGE_TX_LIST_SZ, /* maxsegsize */
731410f4c60SPyun YongHyeon 0, /* flags */
732410f4c60SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
733410f4c60SPyun YongHyeon &sc->vge_cdata.vge_tx_ring_tag);
734410f4c60SPyun YongHyeon if (error != 0) {
735410f4c60SPyun YongHyeon device_printf(sc->vge_dev,
736410f4c60SPyun YongHyeon "could not allocate Tx ring DMA tag.\n");
737410f4c60SPyun YongHyeon goto fail;
738410f4c60SPyun YongHyeon }
739410f4c60SPyun YongHyeon
740410f4c60SPyun YongHyeon /* Create tag for Rx ring. */
741410f4c60SPyun YongHyeon error = bus_dma_tag_create(sc->vge_cdata.vge_ring_tag,/* parent */
742410f4c60SPyun YongHyeon VGE_RX_RING_ALIGN, 0, /* algnmnt, boundary */
743410f4c60SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */
744410f4c60SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
745410f4c60SPyun YongHyeon NULL, NULL, /* filter, filterarg */
746410f4c60SPyun YongHyeon VGE_RX_LIST_SZ, /* maxsize */
747410f4c60SPyun YongHyeon 1, /* nsegments */
748410f4c60SPyun YongHyeon VGE_RX_LIST_SZ, /* maxsegsize */
749410f4c60SPyun YongHyeon 0, /* flags */
750410f4c60SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
751410f4c60SPyun YongHyeon &sc->vge_cdata.vge_rx_ring_tag);
752410f4c60SPyun YongHyeon if (error != 0) {
753410f4c60SPyun YongHyeon device_printf(sc->vge_dev,
754410f4c60SPyun YongHyeon "could not allocate Rx ring DMA tag.\n");
755410f4c60SPyun YongHyeon goto fail;
756410f4c60SPyun YongHyeon }
757410f4c60SPyun YongHyeon
758410f4c60SPyun YongHyeon /* Allocate DMA'able memory and load the DMA map for Tx ring. */
759410f4c60SPyun YongHyeon error = bus_dmamem_alloc(sc->vge_cdata.vge_tx_ring_tag,
760410f4c60SPyun YongHyeon (void **)&sc->vge_rdata.vge_tx_ring,
761410f4c60SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
762410f4c60SPyun YongHyeon &sc->vge_cdata.vge_tx_ring_map);
763410f4c60SPyun YongHyeon if (error != 0) {
764410f4c60SPyun YongHyeon device_printf(sc->vge_dev,
765410f4c60SPyun YongHyeon "could not allocate DMA'able memory for Tx ring.\n");
766410f4c60SPyun YongHyeon goto fail;
767410f4c60SPyun YongHyeon }
768410f4c60SPyun YongHyeon
769410f4c60SPyun YongHyeon ctx.vge_busaddr = 0;
770410f4c60SPyun YongHyeon error = bus_dmamap_load(sc->vge_cdata.vge_tx_ring_tag,
771410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_ring_map, sc->vge_rdata.vge_tx_ring,
772410f4c60SPyun YongHyeon VGE_TX_LIST_SZ, vge_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
773410f4c60SPyun YongHyeon if (error != 0 || ctx.vge_busaddr == 0) {
774410f4c60SPyun YongHyeon device_printf(sc->vge_dev,
775410f4c60SPyun YongHyeon "could not load DMA'able memory for Tx ring.\n");
776410f4c60SPyun YongHyeon goto fail;
777410f4c60SPyun YongHyeon }
778410f4c60SPyun YongHyeon sc->vge_rdata.vge_tx_ring_paddr = ctx.vge_busaddr;
779410f4c60SPyun YongHyeon
780410f4c60SPyun YongHyeon /* Allocate DMA'able memory and load the DMA map for Rx ring. */
781410f4c60SPyun YongHyeon error = bus_dmamem_alloc(sc->vge_cdata.vge_rx_ring_tag,
782410f4c60SPyun YongHyeon (void **)&sc->vge_rdata.vge_rx_ring,
783410f4c60SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
784410f4c60SPyun YongHyeon &sc->vge_cdata.vge_rx_ring_map);
785410f4c60SPyun YongHyeon if (error != 0) {
786410f4c60SPyun YongHyeon device_printf(sc->vge_dev,
787410f4c60SPyun YongHyeon "could not allocate DMA'able memory for Rx ring.\n");
788410f4c60SPyun YongHyeon goto fail;
789410f4c60SPyun YongHyeon }
790410f4c60SPyun YongHyeon
791410f4c60SPyun YongHyeon ctx.vge_busaddr = 0;
792410f4c60SPyun YongHyeon error = bus_dmamap_load(sc->vge_cdata.vge_rx_ring_tag,
793410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_ring_map, sc->vge_rdata.vge_rx_ring,
794410f4c60SPyun YongHyeon VGE_RX_LIST_SZ, vge_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
795410f4c60SPyun YongHyeon if (error != 0 || ctx.vge_busaddr == 0) {
796410f4c60SPyun YongHyeon device_printf(sc->vge_dev,
797410f4c60SPyun YongHyeon "could not load DMA'able memory for Rx ring.\n");
798410f4c60SPyun YongHyeon goto fail;
799410f4c60SPyun YongHyeon }
800410f4c60SPyun YongHyeon sc->vge_rdata.vge_rx_ring_paddr = ctx.vge_busaddr;
801410f4c60SPyun YongHyeon
802410f4c60SPyun YongHyeon /* Tx/Rx descriptor queue should reside within 4GB boundary. */
803410f4c60SPyun YongHyeon tx_ring_end = sc->vge_rdata.vge_tx_ring_paddr + VGE_TX_LIST_SZ;
804410f4c60SPyun YongHyeon rx_ring_end = sc->vge_rdata.vge_rx_ring_paddr + VGE_RX_LIST_SZ;
805410f4c60SPyun YongHyeon if ((VGE_ADDR_HI(tx_ring_end) !=
806410f4c60SPyun YongHyeon VGE_ADDR_HI(sc->vge_rdata.vge_tx_ring_paddr)) ||
807410f4c60SPyun YongHyeon (VGE_ADDR_HI(rx_ring_end) !=
808410f4c60SPyun YongHyeon VGE_ADDR_HI(sc->vge_rdata.vge_rx_ring_paddr)) ||
809410f4c60SPyun YongHyeon VGE_ADDR_HI(tx_ring_end) != VGE_ADDR_HI(rx_ring_end)) {
810410f4c60SPyun YongHyeon device_printf(sc->vge_dev, "4GB boundary crossed, "
811410f4c60SPyun YongHyeon "switching to 32bit DMA address mode.\n");
812410f4c60SPyun YongHyeon vge_dma_free(sc);
813410f4c60SPyun YongHyeon /* Limit DMA address space to 32bit and try again. */
814410f4c60SPyun YongHyeon lowaddr = BUS_SPACE_MAXADDR_32BIT;
815410f4c60SPyun YongHyeon goto again;
816410f4c60SPyun YongHyeon }
817410f4c60SPyun YongHyeon
8187ba75dc4SPyun YongHyeon if ((sc->vge_flags & VGE_FLAG_PCIE) != 0)
8197ba75dc4SPyun YongHyeon lowaddr = VGE_BUF_DMA_MAXADDR;
8207ba75dc4SPyun YongHyeon else
8217ba75dc4SPyun YongHyeon lowaddr = BUS_SPACE_MAXADDR_32BIT;
822410f4c60SPyun YongHyeon /* Create parent buffer tag. */
823410f4c60SPyun YongHyeon error = bus_dma_tag_create(bus_get_dma_tag(sc->vge_dev),/* parent */
824410f4c60SPyun YongHyeon 1, 0, /* algnmnt, boundary */
8257ba75dc4SPyun YongHyeon lowaddr, /* lowaddr */
826410f4c60SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
827410f4c60SPyun YongHyeon NULL, NULL, /* filter, filterarg */
828410f4c60SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
829410f4c60SPyun YongHyeon 0, /* nsegments */
830410f4c60SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
831410f4c60SPyun YongHyeon 0, /* flags */
832410f4c60SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
833410f4c60SPyun YongHyeon &sc->vge_cdata.vge_buffer_tag);
834410f4c60SPyun YongHyeon if (error != 0) {
835410f4c60SPyun YongHyeon device_printf(sc->vge_dev,
836410f4c60SPyun YongHyeon "could not create parent buffer DMA tag.\n");
837410f4c60SPyun YongHyeon goto fail;
838410f4c60SPyun YongHyeon }
839410f4c60SPyun YongHyeon
840410f4c60SPyun YongHyeon /* Create tag for Tx buffers. */
841410f4c60SPyun YongHyeon error = bus_dma_tag_create(sc->vge_cdata.vge_buffer_tag,/* parent */
842410f4c60SPyun YongHyeon 1, 0, /* algnmnt, boundary */
843410f4c60SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */
844410f4c60SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
845410f4c60SPyun YongHyeon NULL, NULL, /* filter, filterarg */
846410f4c60SPyun YongHyeon MCLBYTES * VGE_MAXTXSEGS, /* maxsize */
847410f4c60SPyun YongHyeon VGE_MAXTXSEGS, /* nsegments */
848410f4c60SPyun YongHyeon MCLBYTES, /* maxsegsize */
849410f4c60SPyun YongHyeon 0, /* flags */
850410f4c60SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
851410f4c60SPyun YongHyeon &sc->vge_cdata.vge_tx_tag);
852410f4c60SPyun YongHyeon if (error != 0) {
853410f4c60SPyun YongHyeon device_printf(sc->vge_dev, "could not create Tx DMA tag.\n");
854410f4c60SPyun YongHyeon goto fail;
855410f4c60SPyun YongHyeon }
856410f4c60SPyun YongHyeon
857410f4c60SPyun YongHyeon /* Create tag for Rx buffers. */
858410f4c60SPyun YongHyeon error = bus_dma_tag_create(sc->vge_cdata.vge_buffer_tag,/* parent */
859410f4c60SPyun YongHyeon VGE_RX_BUF_ALIGN, 0, /* algnmnt, boundary */
860410f4c60SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */
861410f4c60SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
862410f4c60SPyun YongHyeon NULL, NULL, /* filter, filterarg */
863410f4c60SPyun YongHyeon MCLBYTES, /* maxsize */
864410f4c60SPyun YongHyeon 1, /* nsegments */
865410f4c60SPyun YongHyeon MCLBYTES, /* maxsegsize */
866410f4c60SPyun YongHyeon 0, /* flags */
867410f4c60SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
868410f4c60SPyun YongHyeon &sc->vge_cdata.vge_rx_tag);
869410f4c60SPyun YongHyeon if (error != 0) {
870410f4c60SPyun YongHyeon device_printf(sc->vge_dev, "could not create Rx DMA tag.\n");
871410f4c60SPyun YongHyeon goto fail;
872410f4c60SPyun YongHyeon }
873410f4c60SPyun YongHyeon
874410f4c60SPyun YongHyeon /* Create DMA maps for Tx buffers. */
875410f4c60SPyun YongHyeon for (i = 0; i < VGE_TX_DESC_CNT; i++) {
876410f4c60SPyun YongHyeon txd = &sc->vge_cdata.vge_txdesc[i];
877410f4c60SPyun YongHyeon txd->tx_m = NULL;
878410f4c60SPyun YongHyeon txd->tx_dmamap = NULL;
879410f4c60SPyun YongHyeon error = bus_dmamap_create(sc->vge_cdata.vge_tx_tag, 0,
880410f4c60SPyun YongHyeon &txd->tx_dmamap);
881410f4c60SPyun YongHyeon if (error != 0) {
882410f4c60SPyun YongHyeon device_printf(sc->vge_dev,
883410f4c60SPyun YongHyeon "could not create Tx dmamap.\n");
884410f4c60SPyun YongHyeon goto fail;
885410f4c60SPyun YongHyeon }
886410f4c60SPyun YongHyeon }
887410f4c60SPyun YongHyeon /* Create DMA maps for Rx buffers. */
888410f4c60SPyun YongHyeon if ((error = bus_dmamap_create(sc->vge_cdata.vge_rx_tag, 0,
889410f4c60SPyun YongHyeon &sc->vge_cdata.vge_rx_sparemap)) != 0) {
890410f4c60SPyun YongHyeon device_printf(sc->vge_dev,
891410f4c60SPyun YongHyeon "could not create spare Rx dmamap.\n");
892410f4c60SPyun YongHyeon goto fail;
893410f4c60SPyun YongHyeon }
894410f4c60SPyun YongHyeon for (i = 0; i < VGE_RX_DESC_CNT; i++) {
895410f4c60SPyun YongHyeon rxd = &sc->vge_cdata.vge_rxdesc[i];
896410f4c60SPyun YongHyeon rxd->rx_m = NULL;
897410f4c60SPyun YongHyeon rxd->rx_dmamap = NULL;
898410f4c60SPyun YongHyeon error = bus_dmamap_create(sc->vge_cdata.vge_rx_tag, 0,
899410f4c60SPyun YongHyeon &rxd->rx_dmamap);
900410f4c60SPyun YongHyeon if (error != 0) {
901410f4c60SPyun YongHyeon device_printf(sc->vge_dev,
902410f4c60SPyun YongHyeon "could not create Rx dmamap.\n");
903410f4c60SPyun YongHyeon goto fail;
904410f4c60SPyun YongHyeon }
905410f4c60SPyun YongHyeon }
906410f4c60SPyun YongHyeon
907410f4c60SPyun YongHyeon fail:
908410f4c60SPyun YongHyeon return (error);
909410f4c60SPyun YongHyeon }
910410f4c60SPyun YongHyeon
911410f4c60SPyun YongHyeon static void
vge_dma_free(struct vge_softc * sc)9126afe22a8SPyun YongHyeon vge_dma_free(struct vge_softc *sc)
913410f4c60SPyun YongHyeon {
914410f4c60SPyun YongHyeon struct vge_txdesc *txd;
915410f4c60SPyun YongHyeon struct vge_rxdesc *rxd;
916a07bd003SBill Paul int i;
917a07bd003SBill Paul
918410f4c60SPyun YongHyeon /* Tx ring. */
919410f4c60SPyun YongHyeon if (sc->vge_cdata.vge_tx_ring_tag != NULL) {
920068d8643SJohn Baldwin if (sc->vge_rdata.vge_tx_ring_paddr)
921410f4c60SPyun YongHyeon bus_dmamap_unload(sc->vge_cdata.vge_tx_ring_tag,
922410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_ring_map);
923068d8643SJohn Baldwin if (sc->vge_rdata.vge_tx_ring)
924410f4c60SPyun YongHyeon bus_dmamem_free(sc->vge_cdata.vge_tx_ring_tag,
925410f4c60SPyun YongHyeon sc->vge_rdata.vge_tx_ring,
926410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_ring_map);
927410f4c60SPyun YongHyeon sc->vge_rdata.vge_tx_ring = NULL;
928068d8643SJohn Baldwin sc->vge_rdata.vge_tx_ring_paddr = 0;
929410f4c60SPyun YongHyeon bus_dma_tag_destroy(sc->vge_cdata.vge_tx_ring_tag);
930410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_ring_tag = NULL;
931a07bd003SBill Paul }
932410f4c60SPyun YongHyeon /* Rx ring. */
933410f4c60SPyun YongHyeon if (sc->vge_cdata.vge_rx_ring_tag != NULL) {
934068d8643SJohn Baldwin if (sc->vge_rdata.vge_rx_ring_paddr)
935410f4c60SPyun YongHyeon bus_dmamap_unload(sc->vge_cdata.vge_rx_ring_tag,
936410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_ring_map);
937068d8643SJohn Baldwin if (sc->vge_rdata.vge_rx_ring)
938410f4c60SPyun YongHyeon bus_dmamem_free(sc->vge_cdata.vge_rx_ring_tag,
939410f4c60SPyun YongHyeon sc->vge_rdata.vge_rx_ring,
940410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_ring_map);
941410f4c60SPyun YongHyeon sc->vge_rdata.vge_rx_ring = NULL;
942068d8643SJohn Baldwin sc->vge_rdata.vge_rx_ring_paddr = 0;
943410f4c60SPyun YongHyeon bus_dma_tag_destroy(sc->vge_cdata.vge_rx_ring_tag);
944410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_ring_tag = NULL;
945a07bd003SBill Paul }
946410f4c60SPyun YongHyeon /* Tx buffers. */
947410f4c60SPyun YongHyeon if (sc->vge_cdata.vge_tx_tag != NULL) {
948a07bd003SBill Paul for (i = 0; i < VGE_TX_DESC_CNT; i++) {
949410f4c60SPyun YongHyeon txd = &sc->vge_cdata.vge_txdesc[i];
950410f4c60SPyun YongHyeon if (txd->tx_dmamap != NULL) {
951410f4c60SPyun YongHyeon bus_dmamap_destroy(sc->vge_cdata.vge_tx_tag,
952410f4c60SPyun YongHyeon txd->tx_dmamap);
953410f4c60SPyun YongHyeon txd->tx_dmamap = NULL;
954a07bd003SBill Paul }
955a07bd003SBill Paul }
956410f4c60SPyun YongHyeon bus_dma_tag_destroy(sc->vge_cdata.vge_tx_tag);
957410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_tag = NULL;
958a07bd003SBill Paul }
959410f4c60SPyun YongHyeon /* Rx buffers. */
960410f4c60SPyun YongHyeon if (sc->vge_cdata.vge_rx_tag != NULL) {
961a07bd003SBill Paul for (i = 0; i < VGE_RX_DESC_CNT; i++) {
962410f4c60SPyun YongHyeon rxd = &sc->vge_cdata.vge_rxdesc[i];
963410f4c60SPyun YongHyeon if (rxd->rx_dmamap != NULL) {
964410f4c60SPyun YongHyeon bus_dmamap_destroy(sc->vge_cdata.vge_rx_tag,
965410f4c60SPyun YongHyeon rxd->rx_dmamap);
966410f4c60SPyun YongHyeon rxd->rx_dmamap = NULL;
967a07bd003SBill Paul }
968a07bd003SBill Paul }
969410f4c60SPyun YongHyeon if (sc->vge_cdata.vge_rx_sparemap != NULL) {
970410f4c60SPyun YongHyeon bus_dmamap_destroy(sc->vge_cdata.vge_rx_tag,
971410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_sparemap);
972410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_sparemap = NULL;
973410f4c60SPyun YongHyeon }
974410f4c60SPyun YongHyeon bus_dma_tag_destroy(sc->vge_cdata.vge_rx_tag);
975410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_tag = NULL;
976410f4c60SPyun YongHyeon }
977a07bd003SBill Paul
978410f4c60SPyun YongHyeon if (sc->vge_cdata.vge_buffer_tag != NULL) {
979410f4c60SPyun YongHyeon bus_dma_tag_destroy(sc->vge_cdata.vge_buffer_tag);
980410f4c60SPyun YongHyeon sc->vge_cdata.vge_buffer_tag = NULL;
981410f4c60SPyun YongHyeon }
982410f4c60SPyun YongHyeon if (sc->vge_cdata.vge_ring_tag != NULL) {
983410f4c60SPyun YongHyeon bus_dma_tag_destroy(sc->vge_cdata.vge_ring_tag);
984410f4c60SPyun YongHyeon sc->vge_cdata.vge_ring_tag = NULL;
985410f4c60SPyun YongHyeon }
986a07bd003SBill Paul }
987a07bd003SBill Paul
988a07bd003SBill Paul /*
989a07bd003SBill Paul * Attach the interface. Allocate softc structures, do ifmedia
990a07bd003SBill Paul * setup and ethernet/BPF attach.
991a07bd003SBill Paul */
992a07bd003SBill Paul static int
vge_attach(device_t dev)9936afe22a8SPyun YongHyeon vge_attach(device_t dev)
994a07bd003SBill Paul {
995a07bd003SBill Paul u_char eaddr[ETHER_ADDR_LEN];
996a07bd003SBill Paul struct vge_softc *sc;
9973ac0cb32SJustin Hibbits if_t ifp;
99820c3cb15SPyun YongHyeon int error = 0, cap, i, msic, rid;
999a07bd003SBill Paul
1000a07bd003SBill Paul sc = device_get_softc(dev);
1001a07bd003SBill Paul sc->vge_dev = dev;
1002a07bd003SBill Paul
1003a07bd003SBill Paul mtx_init(&sc->vge_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
100467e1dfa7SJohn Baldwin MTX_DEF);
100567e1dfa7SJohn Baldwin callout_init_mtx(&sc->vge_watchdog, &sc->vge_mtx, 0);
100667e1dfa7SJohn Baldwin
1007a07bd003SBill Paul /*
1008a07bd003SBill Paul * Map control/status registers.
1009a07bd003SBill Paul */
1010a07bd003SBill Paul pci_enable_busmaster(dev);
1011a07bd003SBill Paul
10124baee897SPyun YongHyeon rid = PCIR_BAR(1);
10138b3433dcSPyun YongHyeon sc->vge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
10148b3433dcSPyun YongHyeon RF_ACTIVE);
1015a07bd003SBill Paul
1016a07bd003SBill Paul if (sc->vge_res == NULL) {
1017481402e1SPyun YongHyeon device_printf(dev, "couldn't map ports/memory\n");
1018a07bd003SBill Paul error = ENXIO;
1019a07bd003SBill Paul goto fail;
1020a07bd003SBill Paul }
1021a07bd003SBill Paul
10223b0a4aefSJohn Baldwin if (pci_find_cap(dev, PCIY_EXPRESS, &cap) == 0) {
1023643e9ee9SPyun YongHyeon sc->vge_flags |= VGE_FLAG_PCIE;
1024643e9ee9SPyun YongHyeon sc->vge_expcap = cap;
102533a0d70bSPyun YongHyeon } else
102633a0d70bSPyun YongHyeon sc->vge_flags |= VGE_FLAG_JUMBO;
1027*ddaf6524SJohn Baldwin if (pci_has_pm(dev))
10287fc94bc4SPyun YongHyeon sc->vge_flags |= VGE_FLAG_PMCAP;
10295957cc2aSPyun YongHyeon rid = 0;
10305957cc2aSPyun YongHyeon msic = pci_msi_count(dev);
10315957cc2aSPyun YongHyeon if (msi_disable == 0 && msic > 0) {
10325957cc2aSPyun YongHyeon msic = 1;
10335957cc2aSPyun YongHyeon if (pci_alloc_msi(dev, &msic) == 0) {
10345957cc2aSPyun YongHyeon if (msic == 1) {
10355957cc2aSPyun YongHyeon sc->vge_flags |= VGE_FLAG_MSI;
10365957cc2aSPyun YongHyeon device_printf(dev, "Using %d MSI message\n",
10375957cc2aSPyun YongHyeon msic);
10385957cc2aSPyun YongHyeon rid = 1;
10395957cc2aSPyun YongHyeon } else
10405957cc2aSPyun YongHyeon pci_release_msi(dev);
10415957cc2aSPyun YongHyeon }
10425957cc2aSPyun YongHyeon }
1043643e9ee9SPyun YongHyeon
1044a07bd003SBill Paul /* Allocate interrupt */
10458b3433dcSPyun YongHyeon sc->vge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
10465957cc2aSPyun YongHyeon ((sc->vge_flags & VGE_FLAG_MSI) ? 0 : RF_SHAREABLE) | RF_ACTIVE);
1047a07bd003SBill Paul if (sc->vge_irq == NULL) {
1048481402e1SPyun YongHyeon device_printf(dev, "couldn't map interrupt\n");
1049a07bd003SBill Paul error = ENXIO;
1050a07bd003SBill Paul goto fail;
1051a07bd003SBill Paul }
1052a07bd003SBill Paul
1053a07bd003SBill Paul /* Reset the adapter. */
1054a07bd003SBill Paul vge_reset(sc);
105520c3cb15SPyun YongHyeon /* Reload EEPROM. */
105620c3cb15SPyun YongHyeon CSR_WRITE_1(sc, VGE_EECSR, VGE_EECSR_RELOAD);
105720c3cb15SPyun YongHyeon for (i = 0; i < VGE_TIMEOUT; i++) {
105820c3cb15SPyun YongHyeon DELAY(5);
105920c3cb15SPyun YongHyeon if ((CSR_READ_1(sc, VGE_EECSR) & VGE_EECSR_RELOAD) == 0)
106020c3cb15SPyun YongHyeon break;
106120c3cb15SPyun YongHyeon }
106220c3cb15SPyun YongHyeon if (i == VGE_TIMEOUT)
106320c3cb15SPyun YongHyeon device_printf(dev, "EEPROM reload timed out\n");
106420c3cb15SPyun YongHyeon /*
106520c3cb15SPyun YongHyeon * Clear PACPI as EEPROM reload will set the bit. Otherwise
106620c3cb15SPyun YongHyeon * MAC will receive magic packet which in turn confuses
106720c3cb15SPyun YongHyeon * controller.
106820c3cb15SPyun YongHyeon */
106920c3cb15SPyun YongHyeon CSR_CLRBIT_1(sc, VGE_CHIPCFG0, VGE_CHIPCFG0_PACPI);
1070a07bd003SBill Paul
1071a07bd003SBill Paul /*
1072a07bd003SBill Paul * Get station address from the EEPROM.
1073a07bd003SBill Paul */
1074a07bd003SBill Paul vge_read_eeprom(sc, (caddr_t)eaddr, VGE_EE_EADDR, 3, 0);
1075643e9ee9SPyun YongHyeon /*
1076643e9ee9SPyun YongHyeon * Save configured PHY address.
1077643e9ee9SPyun YongHyeon * It seems the PHY address of PCIe controllers just
1078643e9ee9SPyun YongHyeon * reflects media jump strapping status so we assume the
1079643e9ee9SPyun YongHyeon * internal PHY address of PCIe controller is at 1.
1080643e9ee9SPyun YongHyeon */
1081643e9ee9SPyun YongHyeon if ((sc->vge_flags & VGE_FLAG_PCIE) != 0)
1082643e9ee9SPyun YongHyeon sc->vge_phyaddr = 1;
1083643e9ee9SPyun YongHyeon else
1084643e9ee9SPyun YongHyeon sc->vge_phyaddr = CSR_READ_1(sc, VGE_MIICFG) &
1085643e9ee9SPyun YongHyeon VGE_MIICFG_PHYADDR;
10867fc94bc4SPyun YongHyeon /* Clear WOL and take hardware from powerdown. */
10877fc94bc4SPyun YongHyeon vge_clrwol(sc);
10887129fb20SPyun YongHyeon vge_sysctl_node(sc);
1089410f4c60SPyun YongHyeon error = vge_dma_alloc(sc);
1090a07bd003SBill Paul if (error)
1091a07bd003SBill Paul goto fail;
1092a07bd003SBill Paul
1093cd036ec1SBrooks Davis ifp = sc->vge_ifp = if_alloc(IFT_ETHER);
1094471ad1d0SPyun YongHyeon vge_miipoll_start(sc);
1095a07bd003SBill Paul /* Do MII setup */
10968e5d93dbSMarius Strobl error = mii_attach(dev, &sc->vge_miibus, ifp, vge_ifmedia_upd,
10978e5d93dbSMarius Strobl vge_ifmedia_sts, BMSR_DEFCAPMASK, sc->vge_phyaddr, MII_OFFSET_ANY,
109817ff418dSPyun YongHyeon MIIF_DOPAUSE);
10998e5d93dbSMarius Strobl if (error != 0) {
11008e5d93dbSMarius Strobl device_printf(dev, "attaching PHYs failed\n");
1101a07bd003SBill Paul goto fail;
1102a07bd003SBill Paul }
1103a07bd003SBill Paul
11043ac0cb32SJustin Hibbits if_setsoftc(ifp, sc);
1105a07bd003SBill Paul if_initname(ifp, device_get_name(dev), device_get_unit(dev));
11063ac0cb32SJustin Hibbits if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
11073ac0cb32SJustin Hibbits if_setioctlfn(ifp, vge_ioctl);
11083ac0cb32SJustin Hibbits if_setcapabilities(ifp, IFCAP_VLAN_MTU);
11093ac0cb32SJustin Hibbits if_setstartfn(ifp, vge_start);
11103ac0cb32SJustin Hibbits if_sethwassist(ifp, VGE_CSUM_FEATURES);
11113ac0cb32SJustin Hibbits if_setcapabilitiesbit(ifp, IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM |
11123ac0cb32SJustin Hibbits IFCAP_VLAN_HWTAGGING, 0);
11137fc94bc4SPyun YongHyeon if ((sc->vge_flags & VGE_FLAG_PMCAP) != 0)
11143ac0cb32SJustin Hibbits if_setcapabilitiesbit(ifp, IFCAP_WOL, 0);
11153ac0cb32SJustin Hibbits if_setcapenable(ifp, if_getcapabilities(ifp));
1116a07bd003SBill Paul #ifdef DEVICE_POLLING
11173ac0cb32SJustin Hibbits if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
1118a07bd003SBill Paul #endif
11193ac0cb32SJustin Hibbits if_setinitfn(ifp, vge_init);
11203ac0cb32SJustin Hibbits if_setsendqlen(ifp, VGE_TX_DESC_CNT - 1);
11213ac0cb32SJustin Hibbits if_setsendqready(ifp);
1122a07bd003SBill Paul
1123a07bd003SBill Paul /*
1124a07bd003SBill Paul * Call MI attach routine.
1125a07bd003SBill Paul */
1126a07bd003SBill Paul ether_ifattach(ifp, eaddr);
1127a07bd003SBill Paul
11280c003e99SPyun YongHyeon /* Tell the upper layer(s) we support long frames. */
11293ac0cb32SJustin Hibbits if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
11300c003e99SPyun YongHyeon
1131a07bd003SBill Paul /* Hook interrupt last to avoid having to lock softc */
1132a07bd003SBill Paul error = bus_setup_intr(dev, sc->vge_irq, INTR_TYPE_NET|INTR_MPSAFE,
1133ef544f63SPaolo Pisati NULL, vge_intr, sc, &sc->vge_intrhand);
1134a07bd003SBill Paul
1135a07bd003SBill Paul if (error) {
1136481402e1SPyun YongHyeon device_printf(dev, "couldn't set up irq\n");
1137a07bd003SBill Paul ether_ifdetach(ifp);
1138a07bd003SBill Paul goto fail;
1139a07bd003SBill Paul }
1140a07bd003SBill Paul
1141a07bd003SBill Paul fail:
1142a07bd003SBill Paul if (error)
1143a07bd003SBill Paul vge_detach(dev);
1144a07bd003SBill Paul
1145a07bd003SBill Paul return (error);
1146a07bd003SBill Paul }
1147a07bd003SBill Paul
1148a07bd003SBill Paul /*
1149a07bd003SBill Paul * Shutdown hardware and free up resources. This can be called any
1150a07bd003SBill Paul * time after the mutex has been initialized. It is called in both
1151a07bd003SBill Paul * the error case in attach and the normal detach case so it needs
1152a07bd003SBill Paul * to be careful about only freeing resources that have actually been
1153a07bd003SBill Paul * allocated.
1154a07bd003SBill Paul */
1155a07bd003SBill Paul static int
vge_detach(device_t dev)11566afe22a8SPyun YongHyeon vge_detach(device_t dev)
1157a07bd003SBill Paul {
1158a07bd003SBill Paul struct vge_softc *sc;
11593ac0cb32SJustin Hibbits if_t ifp;
1160a07bd003SBill Paul
1161a07bd003SBill Paul sc = device_get_softc(dev);
1162a07bd003SBill Paul KASSERT(mtx_initialized(&sc->vge_mtx), ("vge mutex not initialized"));
1163fc74a9f9SBrooks Davis ifp = sc->vge_ifp;
1164a07bd003SBill Paul
116540929967SGleb Smirnoff #ifdef DEVICE_POLLING
11663ac0cb32SJustin Hibbits if (if_getcapenable(ifp) & IFCAP_POLLING)
116740929967SGleb Smirnoff ether_poll_deregister(ifp);
116840929967SGleb Smirnoff #endif
116940929967SGleb Smirnoff
1170a07bd003SBill Paul /* These should only be active if attach succeeded */
1171a07bd003SBill Paul if (device_is_attached(dev)) {
1172a07bd003SBill Paul ether_ifdetach(ifp);
117367e1dfa7SJohn Baldwin VGE_LOCK(sc);
117467e1dfa7SJohn Baldwin vge_stop(sc);
117567e1dfa7SJohn Baldwin VGE_UNLOCK(sc);
117667e1dfa7SJohn Baldwin callout_drain(&sc->vge_watchdog);
1177a07bd003SBill Paul }
1178a07bd003SBill Paul bus_generic_detach(dev);
1179a07bd003SBill Paul
1180a07bd003SBill Paul if (sc->vge_intrhand)
1181a07bd003SBill Paul bus_teardown_intr(dev, sc->vge_irq, sc->vge_intrhand);
1182a07bd003SBill Paul if (sc->vge_irq)
11835957cc2aSPyun YongHyeon bus_release_resource(dev, SYS_RES_IRQ,
11845957cc2aSPyun YongHyeon sc->vge_flags & VGE_FLAG_MSI ? 1 : 0, sc->vge_irq);
11855957cc2aSPyun YongHyeon if (sc->vge_flags & VGE_FLAG_MSI)
11865957cc2aSPyun YongHyeon pci_release_msi(dev);
1187a07bd003SBill Paul if (sc->vge_res)
1188a07bd003SBill Paul bus_release_resource(dev, SYS_RES_MEMORY,
11894baee897SPyun YongHyeon PCIR_BAR(1), sc->vge_res);
1190ad4f426eSWarner Losh if (ifp)
1191ad4f426eSWarner Losh if_free(ifp);
1192a07bd003SBill Paul
1193410f4c60SPyun YongHyeon vge_dma_free(sc);
1194a07bd003SBill Paul mtx_destroy(&sc->vge_mtx);
1195a07bd003SBill Paul
1196a07bd003SBill Paul return (0);
1197a07bd003SBill Paul }
1198a07bd003SBill Paul
1199410f4c60SPyun YongHyeon static void
vge_discard_rxbuf(struct vge_softc * sc,int prod)12006afe22a8SPyun YongHyeon vge_discard_rxbuf(struct vge_softc *sc, int prod)
1201a07bd003SBill Paul {
1202410f4c60SPyun YongHyeon struct vge_rxdesc *rxd;
1203410f4c60SPyun YongHyeon int i;
1204a07bd003SBill Paul
1205410f4c60SPyun YongHyeon rxd = &sc->vge_cdata.vge_rxdesc[prod];
1206410f4c60SPyun YongHyeon rxd->rx_desc->vge_sts = 0;
1207410f4c60SPyun YongHyeon rxd->rx_desc->vge_ctl = 0;
1208a07bd003SBill Paul
1209a07bd003SBill Paul /*
1210410f4c60SPyun YongHyeon * Note: the manual fails to document the fact that for
121104d5a8aeSGordon Bergling * proper operation, the driver needs to replentish the RX
1212410f4c60SPyun YongHyeon * DMA ring 4 descriptors at a time (rather than one at a
1213410f4c60SPyun YongHyeon * time, like most chips). We can allocate the new buffers
1214410f4c60SPyun YongHyeon * but we should not set the OWN bits until we're ready
1215410f4c60SPyun YongHyeon * to hand back 4 of them in one shot.
1216a07bd003SBill Paul */
1217410f4c60SPyun YongHyeon if ((prod % VGE_RXCHUNK) == (VGE_RXCHUNK - 1)) {
1218410f4c60SPyun YongHyeon for (i = VGE_RXCHUNK; i > 0; i--) {
1219410f4c60SPyun YongHyeon rxd->rx_desc->vge_sts = htole32(VGE_RDSTS_OWN);
1220410f4c60SPyun YongHyeon rxd = rxd->rxd_prev;
1221a07bd003SBill Paul }
1222410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_commit += VGE_RXCHUNK;
1223410f4c60SPyun YongHyeon }
1224410f4c60SPyun YongHyeon }
1225410f4c60SPyun YongHyeon
1226410f4c60SPyun YongHyeon static int
vge_newbuf(struct vge_softc * sc,int prod)12276afe22a8SPyun YongHyeon vge_newbuf(struct vge_softc *sc, int prod)
1228410f4c60SPyun YongHyeon {
1229410f4c60SPyun YongHyeon struct vge_rxdesc *rxd;
1230410f4c60SPyun YongHyeon struct mbuf *m;
1231410f4c60SPyun YongHyeon bus_dma_segment_t segs[1];
1232410f4c60SPyun YongHyeon bus_dmamap_t map;
1233410f4c60SPyun YongHyeon int i, nsegs;
1234410f4c60SPyun YongHyeon
1235c6499eccSGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1236410f4c60SPyun YongHyeon if (m == NULL)
1237410f4c60SPyun YongHyeon return (ENOBUFS);
1238410f4c60SPyun YongHyeon /*
1239410f4c60SPyun YongHyeon * This is part of an evil trick to deal with strict-alignment
1240410f4c60SPyun YongHyeon * architectures. The VIA chip requires RX buffers to be aligned
1241410f4c60SPyun YongHyeon * on 32-bit boundaries, but that will hose strict-alignment
1242410f4c60SPyun YongHyeon * architectures. To get around this, we leave some empty space
1243410f4c60SPyun YongHyeon * at the start of each buffer and for non-strict-alignment hosts,
1244410f4c60SPyun YongHyeon * we copy the buffer back two bytes to achieve word alignment.
1245410f4c60SPyun YongHyeon * This is slightly more efficient than allocating a new buffer,
1246410f4c60SPyun YongHyeon * copying the contents, and discarding the old buffer.
1247410f4c60SPyun YongHyeon */
1248410f4c60SPyun YongHyeon m->m_len = m->m_pkthdr.len = MCLBYTES;
1249410f4c60SPyun YongHyeon m_adj(m, VGE_RX_BUF_ALIGN);
1250410f4c60SPyun YongHyeon
1251410f4c60SPyun YongHyeon if (bus_dmamap_load_mbuf_sg(sc->vge_cdata.vge_rx_tag,
1252410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_sparemap, m, segs, &nsegs, 0) != 0) {
1253410f4c60SPyun YongHyeon m_freem(m);
1254410f4c60SPyun YongHyeon return (ENOBUFS);
1255410f4c60SPyun YongHyeon }
1256410f4c60SPyun YongHyeon KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1257410f4c60SPyun YongHyeon
1258410f4c60SPyun YongHyeon rxd = &sc->vge_cdata.vge_rxdesc[prod];
1259410f4c60SPyun YongHyeon if (rxd->rx_m != NULL) {
1260410f4c60SPyun YongHyeon bus_dmamap_sync(sc->vge_cdata.vge_rx_tag, rxd->rx_dmamap,
1261410f4c60SPyun YongHyeon BUS_DMASYNC_POSTREAD);
1262410f4c60SPyun YongHyeon bus_dmamap_unload(sc->vge_cdata.vge_rx_tag, rxd->rx_dmamap);
1263410f4c60SPyun YongHyeon }
1264410f4c60SPyun YongHyeon map = rxd->rx_dmamap;
1265410f4c60SPyun YongHyeon rxd->rx_dmamap = sc->vge_cdata.vge_rx_sparemap;
1266410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_sparemap = map;
1267410f4c60SPyun YongHyeon bus_dmamap_sync(sc->vge_cdata.vge_rx_tag, rxd->rx_dmamap,
1268410f4c60SPyun YongHyeon BUS_DMASYNC_PREREAD);
1269410f4c60SPyun YongHyeon rxd->rx_m = m;
1270410f4c60SPyun YongHyeon
1271410f4c60SPyun YongHyeon rxd->rx_desc->vge_sts = 0;
1272410f4c60SPyun YongHyeon rxd->rx_desc->vge_ctl = 0;
1273410f4c60SPyun YongHyeon rxd->rx_desc->vge_addrlo = htole32(VGE_ADDR_LO(segs[0].ds_addr));
1274410f4c60SPyun YongHyeon rxd->rx_desc->vge_addrhi = htole32(VGE_ADDR_HI(segs[0].ds_addr) |
1275410f4c60SPyun YongHyeon (VGE_BUFLEN(segs[0].ds_len) << 16) | VGE_RXDESC_I);
1276a07bd003SBill Paul
1277a07bd003SBill Paul /*
1278a07bd003SBill Paul * Note: the manual fails to document the fact that for
12798170b243SPyun YongHyeon * proper operation, the driver needs to replenish the RX
1280a07bd003SBill Paul * DMA ring 4 descriptors at a time (rather than one at a
1281a07bd003SBill Paul * time, like most chips). We can allocate the new buffers
1282a07bd003SBill Paul * but we should not set the OWN bits until we're ready
1283a07bd003SBill Paul * to hand back 4 of them in one shot.
1284a07bd003SBill Paul */
1285410f4c60SPyun YongHyeon if ((prod % VGE_RXCHUNK) == (VGE_RXCHUNK - 1)) {
1286410f4c60SPyun YongHyeon for (i = VGE_RXCHUNK; i > 0; i--) {
1287410f4c60SPyun YongHyeon rxd->rx_desc->vge_sts = htole32(VGE_RDSTS_OWN);
1288410f4c60SPyun YongHyeon rxd = rxd->rxd_prev;
1289a07bd003SBill Paul }
1290410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_commit += VGE_RXCHUNK;
1291410f4c60SPyun YongHyeon }
1292a07bd003SBill Paul
1293a07bd003SBill Paul return (0);
1294a07bd003SBill Paul }
1295a07bd003SBill Paul
1296a07bd003SBill Paul static int
vge_tx_list_init(struct vge_softc * sc)12976afe22a8SPyun YongHyeon vge_tx_list_init(struct vge_softc *sc)
1298a07bd003SBill Paul {
1299410f4c60SPyun YongHyeon struct vge_ring_data *rd;
1300410f4c60SPyun YongHyeon struct vge_txdesc *txd;
1301410f4c60SPyun YongHyeon int i;
1302a07bd003SBill Paul
1303410f4c60SPyun YongHyeon VGE_LOCK_ASSERT(sc);
1304410f4c60SPyun YongHyeon
1305410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_prodidx = 0;
1306410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_considx = 0;
1307410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_cnt = 0;
1308410f4c60SPyun YongHyeon
1309410f4c60SPyun YongHyeon rd = &sc->vge_rdata;
1310410f4c60SPyun YongHyeon bzero(rd->vge_tx_ring, VGE_TX_LIST_SZ);
1311410f4c60SPyun YongHyeon for (i = 0; i < VGE_TX_DESC_CNT; i++) {
1312410f4c60SPyun YongHyeon txd = &sc->vge_cdata.vge_txdesc[i];
1313410f4c60SPyun YongHyeon txd->tx_m = NULL;
1314410f4c60SPyun YongHyeon txd->tx_desc = &rd->vge_tx_ring[i];
1315410f4c60SPyun YongHyeon }
1316410f4c60SPyun YongHyeon
1317410f4c60SPyun YongHyeon bus_dmamap_sync(sc->vge_cdata.vge_tx_ring_tag,
1318410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_ring_map,
1319410f4c60SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1320a07bd003SBill Paul
1321a07bd003SBill Paul return (0);
1322a07bd003SBill Paul }
1323a07bd003SBill Paul
1324a07bd003SBill Paul static int
vge_rx_list_init(struct vge_softc * sc)13256afe22a8SPyun YongHyeon vge_rx_list_init(struct vge_softc *sc)
1326a07bd003SBill Paul {
1327410f4c60SPyun YongHyeon struct vge_ring_data *rd;
1328410f4c60SPyun YongHyeon struct vge_rxdesc *rxd;
1329a07bd003SBill Paul int i;
1330a07bd003SBill Paul
1331410f4c60SPyun YongHyeon VGE_LOCK_ASSERT(sc);
1332a07bd003SBill Paul
1333410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_prodidx = 0;
1334410f4c60SPyun YongHyeon sc->vge_cdata.vge_head = NULL;
1335410f4c60SPyun YongHyeon sc->vge_cdata.vge_tail = NULL;
1336410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_commit = 0;
1337a07bd003SBill Paul
1338410f4c60SPyun YongHyeon rd = &sc->vge_rdata;
1339410f4c60SPyun YongHyeon bzero(rd->vge_rx_ring, VGE_RX_LIST_SZ);
1340a07bd003SBill Paul for (i = 0; i < VGE_RX_DESC_CNT; i++) {
1341410f4c60SPyun YongHyeon rxd = &sc->vge_cdata.vge_rxdesc[i];
1342410f4c60SPyun YongHyeon rxd->rx_m = NULL;
1343410f4c60SPyun YongHyeon rxd->rx_desc = &rd->vge_rx_ring[i];
1344410f4c60SPyun YongHyeon if (i == 0)
1345410f4c60SPyun YongHyeon rxd->rxd_prev =
1346410f4c60SPyun YongHyeon &sc->vge_cdata.vge_rxdesc[VGE_RX_DESC_CNT - 1];
1347410f4c60SPyun YongHyeon else
1348410f4c60SPyun YongHyeon rxd->rxd_prev = &sc->vge_cdata.vge_rxdesc[i - 1];
1349410f4c60SPyun YongHyeon if (vge_newbuf(sc, i) != 0)
1350a07bd003SBill Paul return (ENOBUFS);
1351a07bd003SBill Paul }
1352a07bd003SBill Paul
1353410f4c60SPyun YongHyeon bus_dmamap_sync(sc->vge_cdata.vge_rx_ring_tag,
1354410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_ring_map,
1355410f4c60SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1356a07bd003SBill Paul
1357410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_commit = 0;
1358a07bd003SBill Paul
1359a07bd003SBill Paul return (0);
1360a07bd003SBill Paul }
1361a07bd003SBill Paul
1362410f4c60SPyun YongHyeon static void
vge_freebufs(struct vge_softc * sc)13636afe22a8SPyun YongHyeon vge_freebufs(struct vge_softc *sc)
1364410f4c60SPyun YongHyeon {
1365410f4c60SPyun YongHyeon struct vge_txdesc *txd;
1366410f4c60SPyun YongHyeon struct vge_rxdesc *rxd;
13673ac0cb32SJustin Hibbits if_t ifp;
1368410f4c60SPyun YongHyeon int i;
1369410f4c60SPyun YongHyeon
1370410f4c60SPyun YongHyeon VGE_LOCK_ASSERT(sc);
1371410f4c60SPyun YongHyeon
1372410f4c60SPyun YongHyeon ifp = sc->vge_ifp;
1373410f4c60SPyun YongHyeon /*
1374410f4c60SPyun YongHyeon * Free RX and TX mbufs still in the queues.
1375410f4c60SPyun YongHyeon */
1376410f4c60SPyun YongHyeon for (i = 0; i < VGE_RX_DESC_CNT; i++) {
1377410f4c60SPyun YongHyeon rxd = &sc->vge_cdata.vge_rxdesc[i];
1378410f4c60SPyun YongHyeon if (rxd->rx_m != NULL) {
1379410f4c60SPyun YongHyeon bus_dmamap_sync(sc->vge_cdata.vge_rx_tag,
1380410f4c60SPyun YongHyeon rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
1381410f4c60SPyun YongHyeon bus_dmamap_unload(sc->vge_cdata.vge_rx_tag,
1382410f4c60SPyun YongHyeon rxd->rx_dmamap);
1383410f4c60SPyun YongHyeon m_freem(rxd->rx_m);
1384410f4c60SPyun YongHyeon rxd->rx_m = NULL;
1385410f4c60SPyun YongHyeon }
1386410f4c60SPyun YongHyeon }
1387410f4c60SPyun YongHyeon
1388410f4c60SPyun YongHyeon for (i = 0; i < VGE_TX_DESC_CNT; i++) {
1389410f4c60SPyun YongHyeon txd = &sc->vge_cdata.vge_txdesc[i];
1390410f4c60SPyun YongHyeon if (txd->tx_m != NULL) {
1391410f4c60SPyun YongHyeon bus_dmamap_sync(sc->vge_cdata.vge_tx_tag,
1392410f4c60SPyun YongHyeon txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
1393410f4c60SPyun YongHyeon bus_dmamap_unload(sc->vge_cdata.vge_tx_tag,
1394410f4c60SPyun YongHyeon txd->tx_dmamap);
1395410f4c60SPyun YongHyeon m_freem(txd->tx_m);
1396410f4c60SPyun YongHyeon txd->tx_m = NULL;
139741acb7e1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1398410f4c60SPyun YongHyeon }
1399410f4c60SPyun YongHyeon }
1400410f4c60SPyun YongHyeon }
1401410f4c60SPyun YongHyeon
1402410f4c60SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
1403a07bd003SBill Paul static __inline void
vge_fixup_rx(struct mbuf * m)14046afe22a8SPyun YongHyeon vge_fixup_rx(struct mbuf *m)
1405a07bd003SBill Paul {
1406a07bd003SBill Paul int i;
1407a07bd003SBill Paul uint16_t *src, *dst;
1408a07bd003SBill Paul
1409a07bd003SBill Paul src = mtod(m, uint16_t *);
1410a07bd003SBill Paul dst = src - 1;
1411a07bd003SBill Paul
1412a07bd003SBill Paul for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1413a07bd003SBill Paul *dst++ = *src++;
1414a07bd003SBill Paul
1415a07bd003SBill Paul m->m_data -= ETHER_ALIGN;
1416a07bd003SBill Paul }
1417a07bd003SBill Paul #endif
1418a07bd003SBill Paul
1419a07bd003SBill Paul /*
1420a07bd003SBill Paul * RX handler. We support the reception of jumbo frames that have
1421a07bd003SBill Paul * been fragmented across multiple 2K mbuf cluster buffers.
1422a07bd003SBill Paul */
14231abcdbd1SAttilio Rao static int
vge_rxeof(struct vge_softc * sc,int count)14246afe22a8SPyun YongHyeon vge_rxeof(struct vge_softc *sc, int count)
1425a07bd003SBill Paul {
1426a07bd003SBill Paul struct mbuf *m;
14273ac0cb32SJustin Hibbits if_t ifp;
1428410f4c60SPyun YongHyeon int prod, prog, total_len;
1429410f4c60SPyun YongHyeon struct vge_rxdesc *rxd;
1430a07bd003SBill Paul struct vge_rx_desc *cur_rx;
1431410f4c60SPyun YongHyeon uint32_t rxstat, rxctl;
1432a07bd003SBill Paul
1433a07bd003SBill Paul VGE_LOCK_ASSERT(sc);
1434410f4c60SPyun YongHyeon
1435fc74a9f9SBrooks Davis ifp = sc->vge_ifp;
1436a07bd003SBill Paul
1437410f4c60SPyun YongHyeon bus_dmamap_sync(sc->vge_cdata.vge_rx_ring_tag,
1438410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_ring_map,
1439410f4c60SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1440a07bd003SBill Paul
1441410f4c60SPyun YongHyeon prod = sc->vge_cdata.vge_rx_prodidx;
1442410f4c60SPyun YongHyeon for (prog = 0; count > 0 &&
14433ac0cb32SJustin Hibbits (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0;
1444410f4c60SPyun YongHyeon VGE_RX_DESC_INC(prod)) {
1445410f4c60SPyun YongHyeon cur_rx = &sc->vge_rdata.vge_rx_ring[prod];
1446a07bd003SBill Paul rxstat = le32toh(cur_rx->vge_sts);
1447410f4c60SPyun YongHyeon if ((rxstat & VGE_RDSTS_OWN) != 0)
1448410f4c60SPyun YongHyeon break;
1449410f4c60SPyun YongHyeon count--;
1450410f4c60SPyun YongHyeon prog++;
1451a07bd003SBill Paul rxctl = le32toh(cur_rx->vge_ctl);
1452410f4c60SPyun YongHyeon total_len = VGE_RXBYTES(rxstat);
1453410f4c60SPyun YongHyeon rxd = &sc->vge_cdata.vge_rxdesc[prod];
1454410f4c60SPyun YongHyeon m = rxd->rx_m;
1455a07bd003SBill Paul
1456a07bd003SBill Paul /*
1457a07bd003SBill Paul * If the 'start of frame' bit is set, this indicates
1458a07bd003SBill Paul * either the first fragment in a multi-fragment receive,
1459a07bd003SBill Paul * or an intermediate fragment. Either way, we want to
1460a07bd003SBill Paul * accumulate the buffers.
1461a07bd003SBill Paul */
1462410f4c60SPyun YongHyeon if ((rxstat & VGE_RXPKT_SOF) != 0) {
1463410f4c60SPyun YongHyeon if (vge_newbuf(sc, prod) != 0) {
146441acb7e1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1465410f4c60SPyun YongHyeon VGE_CHAIN_RESET(sc);
1466410f4c60SPyun YongHyeon vge_discard_rxbuf(sc, prod);
1467410f4c60SPyun YongHyeon continue;
1468a07bd003SBill Paul }
1469410f4c60SPyun YongHyeon m->m_len = MCLBYTES - VGE_RX_BUF_ALIGN;
1470410f4c60SPyun YongHyeon if (sc->vge_cdata.vge_head == NULL) {
1471410f4c60SPyun YongHyeon sc->vge_cdata.vge_head = m;
1472410f4c60SPyun YongHyeon sc->vge_cdata.vge_tail = m;
1473410f4c60SPyun YongHyeon } else {
1474410f4c60SPyun YongHyeon m->m_flags &= ~M_PKTHDR;
1475410f4c60SPyun YongHyeon sc->vge_cdata.vge_tail->m_next = m;
1476410f4c60SPyun YongHyeon sc->vge_cdata.vge_tail = m;
1477410f4c60SPyun YongHyeon }
1478a07bd003SBill Paul continue;
1479a07bd003SBill Paul }
1480a07bd003SBill Paul
1481a07bd003SBill Paul /*
1482a07bd003SBill Paul * Bad/error frames will have the RXOK bit cleared.
1483a07bd003SBill Paul * However, there's one error case we want to allow:
1484a07bd003SBill Paul * if a VLAN tagged frame arrives and the chip can't
1485a07bd003SBill Paul * match it against the CAM filter, it considers this
1486a07bd003SBill Paul * a 'VLAN CAM filter miss' and clears the 'RXOK' bit.
1487a07bd003SBill Paul * We don't want to drop the frame though: our VLAN
1488a07bd003SBill Paul * filtering is done in software.
1489410f4c60SPyun YongHyeon * We also want to receive bad-checksummed frames and
1490410f4c60SPyun YongHyeon * and frames with bad-length.
1491a07bd003SBill Paul */
1492410f4c60SPyun YongHyeon if ((rxstat & VGE_RDSTS_RXOK) == 0 &&
1493410f4c60SPyun YongHyeon (rxstat & (VGE_RDSTS_VIDM | VGE_RDSTS_RLERR |
1494410f4c60SPyun YongHyeon VGE_RDSTS_CSUMERR)) == 0) {
149541acb7e1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1496a07bd003SBill Paul /*
1497a07bd003SBill Paul * If this is part of a multi-fragment packet,
1498a07bd003SBill Paul * discard all the pieces.
1499a07bd003SBill Paul */
1500410f4c60SPyun YongHyeon VGE_CHAIN_RESET(sc);
1501410f4c60SPyun YongHyeon vge_discard_rxbuf(sc, prod);
1502a07bd003SBill Paul continue;
1503a07bd003SBill Paul }
1504a07bd003SBill Paul
1505410f4c60SPyun YongHyeon if (vge_newbuf(sc, prod) != 0) {
150641acb7e1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1507410f4c60SPyun YongHyeon VGE_CHAIN_RESET(sc);
1508410f4c60SPyun YongHyeon vge_discard_rxbuf(sc, prod);
1509a07bd003SBill Paul continue;
1510a07bd003SBill Paul }
1511a07bd003SBill Paul
1512410f4c60SPyun YongHyeon /* Chain received mbufs. */
1513410f4c60SPyun YongHyeon if (sc->vge_cdata.vge_head != NULL) {
1514410f4c60SPyun YongHyeon m->m_len = total_len % (MCLBYTES - VGE_RX_BUF_ALIGN);
1515a07bd003SBill Paul /*
1516a07bd003SBill Paul * Special case: if there's 4 bytes or less
1517a07bd003SBill Paul * in this buffer, the mbuf can be discarded:
1518a07bd003SBill Paul * the last 4 bytes is the CRC, which we don't
1519a07bd003SBill Paul * care about anyway.
1520a07bd003SBill Paul */
1521a07bd003SBill Paul if (m->m_len <= ETHER_CRC_LEN) {
1522410f4c60SPyun YongHyeon sc->vge_cdata.vge_tail->m_len -=
1523a07bd003SBill Paul (ETHER_CRC_LEN - m->m_len);
1524a07bd003SBill Paul m_freem(m);
1525a07bd003SBill Paul } else {
1526a07bd003SBill Paul m->m_len -= ETHER_CRC_LEN;
1527a07bd003SBill Paul m->m_flags &= ~M_PKTHDR;
1528410f4c60SPyun YongHyeon sc->vge_cdata.vge_tail->m_next = m;
1529a07bd003SBill Paul }
1530410f4c60SPyun YongHyeon m = sc->vge_cdata.vge_head;
1531410f4c60SPyun YongHyeon m->m_flags |= M_PKTHDR;
1532a07bd003SBill Paul m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1533410f4c60SPyun YongHyeon } else {
1534410f4c60SPyun YongHyeon m->m_flags |= M_PKTHDR;
1535a07bd003SBill Paul m->m_pkthdr.len = m->m_len =
1536a07bd003SBill Paul (total_len - ETHER_CRC_LEN);
1537410f4c60SPyun YongHyeon }
1538a07bd003SBill Paul
1539410f4c60SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
1540a07bd003SBill Paul vge_fixup_rx(m);
1541a07bd003SBill Paul #endif
1542a07bd003SBill Paul m->m_pkthdr.rcvif = ifp;
1543a07bd003SBill Paul
1544a07bd003SBill Paul /* Do RX checksumming if enabled */
15453ac0cb32SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0 &&
1546410f4c60SPyun YongHyeon (rxctl & VGE_RDCTL_FRAG) == 0) {
1547a07bd003SBill Paul /* Check IP header checksum */
1548410f4c60SPyun YongHyeon if ((rxctl & VGE_RDCTL_IPPKT) != 0)
1549a07bd003SBill Paul m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1550410f4c60SPyun YongHyeon if ((rxctl & VGE_RDCTL_IPCSUMOK) != 0)
1551a07bd003SBill Paul m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1552a07bd003SBill Paul
1553a07bd003SBill Paul /* Check TCP/UDP checksum */
1554a07bd003SBill Paul if (rxctl & (VGE_RDCTL_TCPPKT | VGE_RDCTL_UDPPKT) &&
1555a07bd003SBill Paul rxctl & VGE_RDCTL_PROTOCSUMOK) {
1556a07bd003SBill Paul m->m_pkthdr.csum_flags |=
1557a07bd003SBill Paul CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1558a07bd003SBill Paul m->m_pkthdr.csum_data = 0xffff;
1559a07bd003SBill Paul }
1560a07bd003SBill Paul }
1561a07bd003SBill Paul
1562410f4c60SPyun YongHyeon if ((rxstat & VGE_RDSTS_VTAG) != 0) {
156303eab9f7SRuslan Ermilov /*
156403eab9f7SRuslan Ermilov * The 32-bit rxctl register is stored in little-endian.
156503eab9f7SRuslan Ermilov * However, the 16-bit vlan tag is stored in big-endian,
156603eab9f7SRuslan Ermilov * so we have to byte swap it.
156703eab9f7SRuslan Ermilov */
156878ba57b9SAndre Oppermann m->m_pkthdr.ether_vtag =
156903eab9f7SRuslan Ermilov bswap16(rxctl & VGE_RDCTL_VLANID);
157078ba57b9SAndre Oppermann m->m_flags |= M_VLANTAG;
1571d147662cSGleb Smirnoff }
1572a07bd003SBill Paul
1573a07bd003SBill Paul VGE_UNLOCK(sc);
15743ac0cb32SJustin Hibbits if_input(ifp, m);
1575a07bd003SBill Paul VGE_LOCK(sc);
1576410f4c60SPyun YongHyeon sc->vge_cdata.vge_head = NULL;
1577410f4c60SPyun YongHyeon sc->vge_cdata.vge_tail = NULL;
1578a07bd003SBill Paul }
1579a07bd003SBill Paul
1580410f4c60SPyun YongHyeon if (prog > 0) {
1581410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_prodidx = prod;
1582410f4c60SPyun YongHyeon bus_dmamap_sync(sc->vge_cdata.vge_rx_ring_tag,
1583410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_ring_map,
1584410f4c60SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1585410f4c60SPyun YongHyeon /* Update residue counter. */
1586410f4c60SPyun YongHyeon if (sc->vge_cdata.vge_rx_commit != 0) {
1587410f4c60SPyun YongHyeon CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT,
1588410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_commit);
1589410f4c60SPyun YongHyeon sc->vge_cdata.vge_rx_commit = 0;
1590410f4c60SPyun YongHyeon }
1591410f4c60SPyun YongHyeon }
1592410f4c60SPyun YongHyeon return (prog);
1593a07bd003SBill Paul }
1594a07bd003SBill Paul
1595a07bd003SBill Paul static void
vge_txeof(struct vge_softc * sc)15966afe22a8SPyun YongHyeon vge_txeof(struct vge_softc *sc)
1597a07bd003SBill Paul {
15983ac0cb32SJustin Hibbits if_t ifp;
1599410f4c60SPyun YongHyeon struct vge_tx_desc *cur_tx;
1600410f4c60SPyun YongHyeon struct vge_txdesc *txd;
1601410f4c60SPyun YongHyeon uint32_t txstat;
1602410f4c60SPyun YongHyeon int cons, prod;
1603410f4c60SPyun YongHyeon
1604410f4c60SPyun YongHyeon VGE_LOCK_ASSERT(sc);
1605a07bd003SBill Paul
1606fc74a9f9SBrooks Davis ifp = sc->vge_ifp;
1607a07bd003SBill Paul
1608410f4c60SPyun YongHyeon if (sc->vge_cdata.vge_tx_cnt == 0)
1609410f4c60SPyun YongHyeon return;
1610a07bd003SBill Paul
1611410f4c60SPyun YongHyeon bus_dmamap_sync(sc->vge_cdata.vge_tx_ring_tag,
1612410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_ring_map,
1613410f4c60SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1614a07bd003SBill Paul
1615410f4c60SPyun YongHyeon /*
1616410f4c60SPyun YongHyeon * Go through our tx list and free mbufs for those
1617410f4c60SPyun YongHyeon * frames that have been transmitted.
1618410f4c60SPyun YongHyeon */
1619410f4c60SPyun YongHyeon cons = sc->vge_cdata.vge_tx_considx;
1620410f4c60SPyun YongHyeon prod = sc->vge_cdata.vge_tx_prodidx;
1621410f4c60SPyun YongHyeon for (; cons != prod; VGE_TX_DESC_INC(cons)) {
1622410f4c60SPyun YongHyeon cur_tx = &sc->vge_rdata.vge_tx_ring[cons];
1623410f4c60SPyun YongHyeon txstat = le32toh(cur_tx->vge_sts);
1624410f4c60SPyun YongHyeon if ((txstat & VGE_TDSTS_OWN) != 0)
1625a07bd003SBill Paul break;
1626410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_cnt--;
16273ac0cb32SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1628410f4c60SPyun YongHyeon
1629410f4c60SPyun YongHyeon txd = &sc->vge_cdata.vge_txdesc[cons];
1630410f4c60SPyun YongHyeon bus_dmamap_sync(sc->vge_cdata.vge_tx_tag, txd->tx_dmamap,
1631410f4c60SPyun YongHyeon BUS_DMASYNC_POSTWRITE);
1632410f4c60SPyun YongHyeon bus_dmamap_unload(sc->vge_cdata.vge_tx_tag, txd->tx_dmamap);
1633410f4c60SPyun YongHyeon
1634410f4c60SPyun YongHyeon KASSERT(txd->tx_m != NULL, ("%s: freeing NULL mbuf!\n",
1635410f4c60SPyun YongHyeon __func__));
1636410f4c60SPyun YongHyeon m_freem(txd->tx_m);
1637410f4c60SPyun YongHyeon txd->tx_m = NULL;
1638420d0abfSPyun YongHyeon txd->tx_desc->vge_frag[0].vge_addrhi = 0;
1639a07bd003SBill Paul }
1640420d0abfSPyun YongHyeon bus_dmamap_sync(sc->vge_cdata.vge_tx_ring_tag,
1641420d0abfSPyun YongHyeon sc->vge_cdata.vge_tx_ring_map,
1642420d0abfSPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1643410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_considx = cons;
1644410f4c60SPyun YongHyeon if (sc->vge_cdata.vge_tx_cnt == 0)
1645410f4c60SPyun YongHyeon sc->vge_timer = 0;
1646a07bd003SBill Paul }
1647a07bd003SBill Paul
1648a07bd003SBill Paul static void
vge_link_statchg(void * xsc)1649e7b2d9b8SPyun YongHyeon vge_link_statchg(void *xsc)
1650a07bd003SBill Paul {
1651a07bd003SBill Paul struct vge_softc *sc;
16523ac0cb32SJustin Hibbits if_t ifp;
165366c6108dSPyun YongHyeon uint8_t physts;
1654a07bd003SBill Paul
1655a07bd003SBill Paul sc = xsc;
1656fc74a9f9SBrooks Davis ifp = sc->vge_ifp;
165767e1dfa7SJohn Baldwin VGE_LOCK_ASSERT(sc);
1658a07bd003SBill Paul
165966c6108dSPyun YongHyeon physts = CSR_READ_1(sc, VGE_PHYSTS0);
166066c6108dSPyun YongHyeon if ((physts & VGE_PHYSTS_RESETSTS) == 0) {
166166c6108dSPyun YongHyeon if ((physts & VGE_PHYSTS_LINK) == 0) {
16624d7235ddSPyun YongHyeon sc->vge_flags &= ~VGE_FLAG_LINK;
1663fc74a9f9SBrooks Davis if_link_state_change(sc->vge_ifp,
166442559cd2SBill Paul LINK_STATE_DOWN);
1665a07bd003SBill Paul } else {
16664d7235ddSPyun YongHyeon sc->vge_flags |= VGE_FLAG_LINK;
1667fc74a9f9SBrooks Davis if_link_state_change(sc->vge_ifp,
166842559cd2SBill Paul LINK_STATE_UP);
166966c6108dSPyun YongHyeon CSR_WRITE_1(sc, VGE_CRC2, VGE_CR2_FDX_TXFLOWCTL_ENABLE |
167066c6108dSPyun YongHyeon VGE_CR2_FDX_RXFLOWCTL_ENABLE);
167166c6108dSPyun YongHyeon if ((physts & VGE_PHYSTS_FDX) != 0) {
167266c6108dSPyun YongHyeon if ((physts & VGE_PHYSTS_TXFLOWCAP) != 0)
167366c6108dSPyun YongHyeon CSR_WRITE_1(sc, VGE_CRS2,
167466c6108dSPyun YongHyeon VGE_CR2_FDX_TXFLOWCTL_ENABLE);
167566c6108dSPyun YongHyeon if ((physts & VGE_PHYSTS_RXFLOWCAP) != 0)
167666c6108dSPyun YongHyeon CSR_WRITE_1(sc, VGE_CRS2,
167766c6108dSPyun YongHyeon VGE_CR2_FDX_RXFLOWCTL_ENABLE);
167866c6108dSPyun YongHyeon }
16793ac0cb32SJustin Hibbits if (!if_sendq_empty(ifp))
168067e1dfa7SJohn Baldwin vge_start_locked(ifp);
1681a07bd003SBill Paul }
1682a07bd003SBill Paul }
168366c6108dSPyun YongHyeon /*
168466c6108dSPyun YongHyeon * Restart MII auto-polling because link state change interrupt
168566c6108dSPyun YongHyeon * will disable it.
168666c6108dSPyun YongHyeon */
168766c6108dSPyun YongHyeon vge_miipoll_start(sc);
1688a07bd003SBill Paul }
1689a07bd003SBill Paul
1690a07bd003SBill Paul #ifdef DEVICE_POLLING
16911abcdbd1SAttilio Rao static int
vge_poll(if_t ifp,enum poll_cmd cmd,int count)16923ac0cb32SJustin Hibbits vge_poll (if_t ifp, enum poll_cmd cmd, int count)
1693a07bd003SBill Paul {
16943ac0cb32SJustin Hibbits struct vge_softc *sc = if_getsoftc(ifp);
16951abcdbd1SAttilio Rao int rx_npkts = 0;
1696a07bd003SBill Paul
1697a07bd003SBill Paul VGE_LOCK(sc);
16983ac0cb32SJustin Hibbits if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
1699a07bd003SBill Paul goto done;
1700a07bd003SBill Paul
1701410f4c60SPyun YongHyeon rx_npkts = vge_rxeof(sc, count);
1702a07bd003SBill Paul vge_txeof(sc);
1703a07bd003SBill Paul
17043ac0cb32SJustin Hibbits if (!if_sendq_empty(ifp))
170567e1dfa7SJohn Baldwin vge_start_locked(ifp);
1706a07bd003SBill Paul
1707a07bd003SBill Paul if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
1708c3c74c61SPyun YongHyeon uint32_t status;
1709a07bd003SBill Paul status = CSR_READ_4(sc, VGE_ISR);
1710a07bd003SBill Paul if (status == 0xFFFFFFFF)
1711a07bd003SBill Paul goto done;
1712a07bd003SBill Paul if (status)
1713a07bd003SBill Paul CSR_WRITE_4(sc, VGE_ISR, status);
1714a07bd003SBill Paul
1715a07bd003SBill Paul /*
1716a07bd003SBill Paul * XXX check behaviour on receiver stalls.
1717a07bd003SBill Paul */
1718a07bd003SBill Paul
1719a07bd003SBill Paul if (status & VGE_ISR_TXDMA_STALL ||
1720410f4c60SPyun YongHyeon status & VGE_ISR_RXDMA_STALL) {
17213ac0cb32SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
172267e1dfa7SJohn Baldwin vge_init_locked(sc);
1723410f4c60SPyun YongHyeon }
1724a07bd003SBill Paul
1725a07bd003SBill Paul if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) {
1726410f4c60SPyun YongHyeon vge_rxeof(sc, count);
1727a07bd003SBill Paul CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1728a07bd003SBill Paul CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1729a07bd003SBill Paul }
1730a07bd003SBill Paul }
1731a07bd003SBill Paul done:
1732a07bd003SBill Paul VGE_UNLOCK(sc);
17331abcdbd1SAttilio Rao return (rx_npkts);
1734a07bd003SBill Paul }
1735a07bd003SBill Paul #endif /* DEVICE_POLLING */
1736a07bd003SBill Paul
1737a07bd003SBill Paul static void
vge_intr(void * arg)17386afe22a8SPyun YongHyeon vge_intr(void *arg)
1739a07bd003SBill Paul {
1740a07bd003SBill Paul struct vge_softc *sc;
17413ac0cb32SJustin Hibbits if_t ifp;
1742c3c74c61SPyun YongHyeon uint32_t status;
1743a07bd003SBill Paul
1744a07bd003SBill Paul sc = arg;
1745a07bd003SBill Paul VGE_LOCK(sc);
1746a07bd003SBill Paul
1747a931e549SPyun YongHyeon ifp = sc->vge_ifp;
1748a931e549SPyun YongHyeon if ((sc->vge_flags & VGE_FLAG_SUSPENDED) != 0 ||
17493ac0cb32SJustin Hibbits (if_getflags(ifp) & IFF_UP) == 0) {
1750a07bd003SBill Paul VGE_UNLOCK(sc);
1751a07bd003SBill Paul return;
1752a07bd003SBill Paul }
1753a07bd003SBill Paul
1754a07bd003SBill Paul #ifdef DEVICE_POLLING
17553ac0cb32SJustin Hibbits if (if_getcapenable(ifp) & IFCAP_POLLING) {
1756a3f4b452SPyun YongHyeon status = CSR_READ_4(sc, VGE_ISR);
1757a3f4b452SPyun YongHyeon CSR_WRITE_4(sc, VGE_ISR, status);
1758a3f4b452SPyun YongHyeon if (status != 0xFFFFFFFF && (status & VGE_ISR_LINKSTS) != 0)
1759a3f4b452SPyun YongHyeon vge_link_statchg(sc);
176040929967SGleb Smirnoff VGE_UNLOCK(sc);
176140929967SGleb Smirnoff return;
1762a07bd003SBill Paul }
176340929967SGleb Smirnoff #endif
1764a07bd003SBill Paul
1765a07bd003SBill Paul /* Disable interrupts */
1766a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1767a07bd003SBill Paul status = CSR_READ_4(sc, VGE_ISR);
17683b2b8afbSPyun YongHyeon CSR_WRITE_4(sc, VGE_ISR, status | VGE_ISR_HOLDOFF_RELOAD);
1769a07bd003SBill Paul /* If the card has gone away the read returns 0xffff. */
17703b2b8afbSPyun YongHyeon if (status == 0xFFFFFFFF || (status & VGE_INTRS) == 0)
17713b2b8afbSPyun YongHyeon goto done;
17723ac0cb32SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
1773a07bd003SBill Paul if (status & (VGE_ISR_RXOK|VGE_ISR_RXOK_HIPRIO))
1774410f4c60SPyun YongHyeon vge_rxeof(sc, VGE_RX_DESC_CNT);
1775a07bd003SBill Paul if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) {
1776410f4c60SPyun YongHyeon vge_rxeof(sc, VGE_RX_DESC_CNT);
1777a07bd003SBill Paul CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1778a07bd003SBill Paul CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1779a07bd003SBill Paul }
1780a07bd003SBill Paul
17813b2b8afbSPyun YongHyeon if (status & (VGE_ISR_TXOK0|VGE_ISR_TXOK_HIPRIO))
1782a07bd003SBill Paul vge_txeof(sc);
1783a07bd003SBill Paul
1784410f4c60SPyun YongHyeon if (status & (VGE_ISR_TXDMA_STALL|VGE_ISR_RXDMA_STALL)) {
17853ac0cb32SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
178667e1dfa7SJohn Baldwin vge_init_locked(sc);
1787410f4c60SPyun YongHyeon }
1788a07bd003SBill Paul
1789a07bd003SBill Paul if (status & VGE_ISR_LINKSTS)
1790e7b2d9b8SPyun YongHyeon vge_link_statchg(sc);
1791a07bd003SBill Paul }
17923b2b8afbSPyun YongHyeon done:
17933ac0cb32SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
1794a07bd003SBill Paul /* Re-enable interrupts */
1795a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1796a07bd003SBill Paul
17973ac0cb32SJustin Hibbits if (!if_sendq_empty(ifp))
179867e1dfa7SJohn Baldwin vge_start_locked(ifp);
17993b2b8afbSPyun YongHyeon }
180067e1dfa7SJohn Baldwin VGE_UNLOCK(sc);
1801a07bd003SBill Paul }
1802a07bd003SBill Paul
1803a07bd003SBill Paul static int
vge_encap(struct vge_softc * sc,struct mbuf ** m_head)18046afe22a8SPyun YongHyeon vge_encap(struct vge_softc *sc, struct mbuf **m_head)
1805a07bd003SBill Paul {
1806410f4c60SPyun YongHyeon struct vge_txdesc *txd;
1807410f4c60SPyun YongHyeon struct vge_tx_frag *frag;
1808410f4c60SPyun YongHyeon struct mbuf *m;
1809410f4c60SPyun YongHyeon bus_dma_segment_t txsegs[VGE_MAXTXSEGS];
1810410f4c60SPyun YongHyeon int error, i, nsegs, padlen;
1811410f4c60SPyun YongHyeon uint32_t cflags;
1812a07bd003SBill Paul
1813410f4c60SPyun YongHyeon VGE_LOCK_ASSERT(sc);
1814a07bd003SBill Paul
1815410f4c60SPyun YongHyeon M_ASSERTPKTHDR((*m_head));
1816a07bd003SBill Paul
1817410f4c60SPyun YongHyeon /* Argh. This chip does not autopad short frames. */
1818410f4c60SPyun YongHyeon if ((*m_head)->m_pkthdr.len < VGE_MIN_FRAMELEN) {
1819410f4c60SPyun YongHyeon m = *m_head;
1820410f4c60SPyun YongHyeon padlen = VGE_MIN_FRAMELEN - m->m_pkthdr.len;
1821410f4c60SPyun YongHyeon if (M_WRITABLE(m) == 0) {
1822410f4c60SPyun YongHyeon /* Get a writable copy. */
1823c6499eccSGleb Smirnoff m = m_dup(*m_head, M_NOWAIT);
1824410f4c60SPyun YongHyeon m_freem(*m_head);
1825410f4c60SPyun YongHyeon if (m == NULL) {
1826410f4c60SPyun YongHyeon *m_head = NULL;
1827a07bd003SBill Paul return (ENOBUFS);
1828a07bd003SBill Paul }
1829410f4c60SPyun YongHyeon *m_head = m;
1830410f4c60SPyun YongHyeon }
1831410f4c60SPyun YongHyeon if (M_TRAILINGSPACE(m) < padlen) {
1832c6499eccSGleb Smirnoff m = m_defrag(m, M_NOWAIT);
1833410f4c60SPyun YongHyeon if (m == NULL) {
1834410f4c60SPyun YongHyeon m_freem(*m_head);
1835410f4c60SPyun YongHyeon *m_head = NULL;
1836410f4c60SPyun YongHyeon return (ENOBUFS);
1837a07bd003SBill Paul }
1838a07bd003SBill Paul }
1839410f4c60SPyun YongHyeon /*
1840410f4c60SPyun YongHyeon * Manually pad short frames, and zero the pad space
1841410f4c60SPyun YongHyeon * to avoid leaking data.
1842410f4c60SPyun YongHyeon */
1843410f4c60SPyun YongHyeon bzero(mtod(m, char *) + m->m_pkthdr.len, padlen);
1844410f4c60SPyun YongHyeon m->m_pkthdr.len += padlen;
1845410f4c60SPyun YongHyeon m->m_len = m->m_pkthdr.len;
1846410f4c60SPyun YongHyeon *m_head = m;
1847410f4c60SPyun YongHyeon }
1848a07bd003SBill Paul
1849410f4c60SPyun YongHyeon txd = &sc->vge_cdata.vge_txdesc[sc->vge_cdata.vge_tx_prodidx];
1850410f4c60SPyun YongHyeon
1851410f4c60SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->vge_cdata.vge_tx_tag,
1852410f4c60SPyun YongHyeon txd->tx_dmamap, *m_head, txsegs, &nsegs, 0);
1853410f4c60SPyun YongHyeon if (error == EFBIG) {
1854c6499eccSGleb Smirnoff m = m_collapse(*m_head, M_NOWAIT, VGE_MAXTXSEGS);
1855410f4c60SPyun YongHyeon if (m == NULL) {
1856410f4c60SPyun YongHyeon m_freem(*m_head);
1857410f4c60SPyun YongHyeon *m_head = NULL;
1858410f4c60SPyun YongHyeon return (ENOMEM);
1859410f4c60SPyun YongHyeon }
1860410f4c60SPyun YongHyeon *m_head = m;
1861410f4c60SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->vge_cdata.vge_tx_tag,
1862410f4c60SPyun YongHyeon txd->tx_dmamap, *m_head, txsegs, &nsegs, 0);
1863410f4c60SPyun YongHyeon if (error != 0) {
1864410f4c60SPyun YongHyeon m_freem(*m_head);
1865410f4c60SPyun YongHyeon *m_head = NULL;
1866410f4c60SPyun YongHyeon return (error);
1867410f4c60SPyun YongHyeon }
1868410f4c60SPyun YongHyeon } else if (error != 0)
1869410f4c60SPyun YongHyeon return (error);
1870410f4c60SPyun YongHyeon bus_dmamap_sync(sc->vge_cdata.vge_tx_tag, txd->tx_dmamap,
1871410f4c60SPyun YongHyeon BUS_DMASYNC_PREWRITE);
1872410f4c60SPyun YongHyeon
1873410f4c60SPyun YongHyeon m = *m_head;
1874410f4c60SPyun YongHyeon cflags = 0;
1875410f4c60SPyun YongHyeon
1876410f4c60SPyun YongHyeon /* Configure checksum offload. */
1877410f4c60SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0)
1878410f4c60SPyun YongHyeon cflags |= VGE_TDCTL_IPCSUM;
1879410f4c60SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
1880410f4c60SPyun YongHyeon cflags |= VGE_TDCTL_TCPCSUM;
1881410f4c60SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
1882410f4c60SPyun YongHyeon cflags |= VGE_TDCTL_UDPCSUM;
1883410f4c60SPyun YongHyeon
1884410f4c60SPyun YongHyeon /* Configure VLAN. */
1885410f4c60SPyun YongHyeon if ((m->m_flags & M_VLANTAG) != 0)
1886410f4c60SPyun YongHyeon cflags |= m->m_pkthdr.ether_vtag | VGE_TDCTL_VTAG;
1887410f4c60SPyun YongHyeon txd->tx_desc->vge_sts = htole32(m->m_pkthdr.len << 16);
1888410f4c60SPyun YongHyeon /*
1889410f4c60SPyun YongHyeon * XXX
1890410f4c60SPyun YongHyeon * Velocity family seems to support TSO but no information
1891410f4c60SPyun YongHyeon * for MSS configuration is available. Also the number of
1892410f4c60SPyun YongHyeon * fragments supported by a descriptor is too small to hold
1893410f4c60SPyun YongHyeon * entire 64KB TCP/IP segment. Maybe VGE_TD_LS_MOF,
1894410f4c60SPyun YongHyeon * VGE_TD_LS_SOF and VGE_TD_LS_EOF could be used to build
1895410f4c60SPyun YongHyeon * longer chain of buffers but no additional information is
1896410f4c60SPyun YongHyeon * available.
1897410f4c60SPyun YongHyeon *
1898410f4c60SPyun YongHyeon * When telling the chip how many segments there are, we
1899410f4c60SPyun YongHyeon * must use nsegs + 1 instead of just nsegs. Darned if I
1900410f4c60SPyun YongHyeon * know why. This also means we can't use the last fragment
1901410f4c60SPyun YongHyeon * field of Tx descriptor.
1902410f4c60SPyun YongHyeon */
1903410f4c60SPyun YongHyeon txd->tx_desc->vge_ctl = htole32(cflags | ((nsegs + 1) << 28) |
1904410f4c60SPyun YongHyeon VGE_TD_LS_NORM);
1905410f4c60SPyun YongHyeon for (i = 0; i < nsegs; i++) {
1906410f4c60SPyun YongHyeon frag = &txd->tx_desc->vge_frag[i];
1907410f4c60SPyun YongHyeon frag->vge_addrlo = htole32(VGE_ADDR_LO(txsegs[i].ds_addr));
1908410f4c60SPyun YongHyeon frag->vge_addrhi = htole32(VGE_ADDR_HI(txsegs[i].ds_addr) |
1909410f4c60SPyun YongHyeon (VGE_BUFLEN(txsegs[i].ds_len) << 16));
1910410f4c60SPyun YongHyeon }
1911410f4c60SPyun YongHyeon
1912410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_cnt++;
1913410f4c60SPyun YongHyeon VGE_TX_DESC_INC(sc->vge_cdata.vge_tx_prodidx);
1914a07bd003SBill Paul
1915a07bd003SBill Paul /*
1916410f4c60SPyun YongHyeon * Finally request interrupt and give the first descriptor
1917410f4c60SPyun YongHyeon * ownership to hardware.
1918a07bd003SBill Paul */
1919410f4c60SPyun YongHyeon txd->tx_desc->vge_ctl |= htole32(VGE_TDCTL_TIC);
1920410f4c60SPyun YongHyeon txd->tx_desc->vge_sts |= htole32(VGE_TDSTS_OWN);
1921410f4c60SPyun YongHyeon txd->tx_m = m;
1922a07bd003SBill Paul
1923a07bd003SBill Paul return (0);
1924a07bd003SBill Paul }
1925a07bd003SBill Paul
1926a07bd003SBill Paul /*
1927a07bd003SBill Paul * Main transmit routine.
1928a07bd003SBill Paul */
1929a07bd003SBill Paul
1930a07bd003SBill Paul static void
vge_start(if_t ifp)19313ac0cb32SJustin Hibbits vge_start(if_t ifp)
1932a07bd003SBill Paul {
1933a07bd003SBill Paul struct vge_softc *sc;
193467e1dfa7SJohn Baldwin
19353ac0cb32SJustin Hibbits sc = if_getsoftc(ifp);
193667e1dfa7SJohn Baldwin VGE_LOCK(sc);
193767e1dfa7SJohn Baldwin vge_start_locked(ifp);
193867e1dfa7SJohn Baldwin VGE_UNLOCK(sc);
193967e1dfa7SJohn Baldwin }
194067e1dfa7SJohn Baldwin
194167e1dfa7SJohn Baldwin static void
vge_start_locked(if_t ifp)19423ac0cb32SJustin Hibbits vge_start_locked(if_t ifp)
194367e1dfa7SJohn Baldwin {
194467e1dfa7SJohn Baldwin struct vge_softc *sc;
1945410f4c60SPyun YongHyeon struct vge_txdesc *txd;
1946410f4c60SPyun YongHyeon struct mbuf *m_head;
1947410f4c60SPyun YongHyeon int enq, idx;
1948a07bd003SBill Paul
19493ac0cb32SJustin Hibbits sc = if_getsoftc(ifp);
1950410f4c60SPyun YongHyeon
195167e1dfa7SJohn Baldwin VGE_LOCK_ASSERT(sc);
1952a07bd003SBill Paul
19534d7235ddSPyun YongHyeon if ((sc->vge_flags & VGE_FLAG_LINK) == 0 ||
19543ac0cb32SJustin Hibbits (if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1955410f4c60SPyun YongHyeon IFF_DRV_RUNNING)
1956a07bd003SBill Paul return;
1957a07bd003SBill Paul
1958410f4c60SPyun YongHyeon idx = sc->vge_cdata.vge_tx_prodidx;
1959410f4c60SPyun YongHyeon VGE_TX_DESC_DEC(idx);
19603ac0cb32SJustin Hibbits for (enq = 0; !if_sendq_empty(ifp) &&
1961410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_cnt < VGE_TX_DESC_CNT - 1; ) {
19623ac0cb32SJustin Hibbits m_head = if_dequeue(ifp);
1963a07bd003SBill Paul if (m_head == NULL)
1964a07bd003SBill Paul break;
1965410f4c60SPyun YongHyeon /*
1966410f4c60SPyun YongHyeon * Pack the data into the transmit ring. If we
1967410f4c60SPyun YongHyeon * don't have room, set the OACTIVE flag and wait
1968410f4c60SPyun YongHyeon * for the NIC to drain the ring.
1969410f4c60SPyun YongHyeon */
1970410f4c60SPyun YongHyeon if (vge_encap(sc, &m_head)) {
1971410f4c60SPyun YongHyeon if (m_head == NULL)
1972410f4c60SPyun YongHyeon break;
19733ac0cb32SJustin Hibbits if_sendq_prepend(ifp, m_head);
19743ac0cb32SJustin Hibbits if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
1975a07bd003SBill Paul break;
1976a07bd003SBill Paul }
1977a07bd003SBill Paul
1978410f4c60SPyun YongHyeon txd = &sc->vge_cdata.vge_txdesc[idx];
1979410f4c60SPyun YongHyeon txd->tx_desc->vge_frag[0].vge_addrhi |= htole32(VGE_TXDESC_Q);
1980a07bd003SBill Paul VGE_TX_DESC_INC(idx);
1981a07bd003SBill Paul
1982410f4c60SPyun YongHyeon enq++;
1983a07bd003SBill Paul /*
1984a07bd003SBill Paul * If there's a BPF listener, bounce a copy of this frame
1985a07bd003SBill Paul * to him.
1986a07bd003SBill Paul */
198759a0d28bSChristian S.J. Peron ETHER_BPF_MTAP(ifp, m_head);
1988a07bd003SBill Paul }
1989a07bd003SBill Paul
1990410f4c60SPyun YongHyeon if (enq > 0) {
1991410f4c60SPyun YongHyeon bus_dmamap_sync(sc->vge_cdata.vge_tx_ring_tag,
1992410f4c60SPyun YongHyeon sc->vge_cdata.vge_tx_ring_map,
1993410f4c60SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1994a07bd003SBill Paul /* Issue a transmit command. */
1995a07bd003SBill Paul CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_WAK0);
1996a07bd003SBill Paul /*
1997a07bd003SBill Paul * Set a timeout in case the chip goes out to lunch.
1998a07bd003SBill Paul */
199967e1dfa7SJohn Baldwin sc->vge_timer = 5;
2000410f4c60SPyun YongHyeon }
2001a07bd003SBill Paul }
2002a07bd003SBill Paul
2003a07bd003SBill Paul static void
vge_init(void * xsc)20046afe22a8SPyun YongHyeon vge_init(void *xsc)
2005a07bd003SBill Paul {
2006a07bd003SBill Paul struct vge_softc *sc = xsc;
200767e1dfa7SJohn Baldwin
200867e1dfa7SJohn Baldwin VGE_LOCK(sc);
200967e1dfa7SJohn Baldwin vge_init_locked(sc);
201067e1dfa7SJohn Baldwin VGE_UNLOCK(sc);
201167e1dfa7SJohn Baldwin }
201267e1dfa7SJohn Baldwin
201367e1dfa7SJohn Baldwin static void
vge_init_locked(struct vge_softc * sc)201467e1dfa7SJohn Baldwin vge_init_locked(struct vge_softc *sc)
201567e1dfa7SJohn Baldwin {
20163ac0cb32SJustin Hibbits if_t ifp = sc->vge_ifp;
2017410f4c60SPyun YongHyeon int error, i;
2018a07bd003SBill Paul
201967e1dfa7SJohn Baldwin VGE_LOCK_ASSERT(sc);
2020a07bd003SBill Paul
20213ac0cb32SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
2022410f4c60SPyun YongHyeon return;
2023410f4c60SPyun YongHyeon
2024a07bd003SBill Paul /*
2025a07bd003SBill Paul * Cancel pending I/O and free all RX/TX buffers.
2026a07bd003SBill Paul */
2027a07bd003SBill Paul vge_stop(sc);
2028a07bd003SBill Paul vge_reset(sc);
2029471ad1d0SPyun YongHyeon vge_miipoll_start(sc);
2030a07bd003SBill Paul
2031a07bd003SBill Paul /*
2032a07bd003SBill Paul * Initialize the RX and TX descriptors and mbufs.
2033a07bd003SBill Paul */
2034a07bd003SBill Paul
2035410f4c60SPyun YongHyeon error = vge_rx_list_init(sc);
2036410f4c60SPyun YongHyeon if (error != 0) {
2037410f4c60SPyun YongHyeon device_printf(sc->vge_dev, "no memory for Rx buffers.\n");
2038410f4c60SPyun YongHyeon return;
2039410f4c60SPyun YongHyeon }
2040a07bd003SBill Paul vge_tx_list_init(sc);
20417129fb20SPyun YongHyeon /* Clear MAC statistics. */
20427129fb20SPyun YongHyeon vge_stats_clear(sc);
2043a07bd003SBill Paul /* Set our station address */
2044a07bd003SBill Paul for (i = 0; i < ETHER_ADDR_LEN; i++)
20453ac0cb32SJustin Hibbits CSR_WRITE_1(sc, VGE_PAR0 + i, if_getlladdr(sc->vge_ifp)[i]);
2046a07bd003SBill Paul
2047a07bd003SBill Paul /*
2048a07bd003SBill Paul * Set receive FIFO threshold. Also allow transmission and
2049a07bd003SBill Paul * reception of VLAN tagged frames.
2050a07bd003SBill Paul */
2051a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_RXCFG, VGE_RXCFG_FIFO_THR|VGE_RXCFG_VTAGOPT);
205238aa43c5SPyun YongHyeon CSR_SETBIT_1(sc, VGE_RXCFG, VGE_RXFIFOTHR_128BYTES);
2053a07bd003SBill Paul
2054a07bd003SBill Paul /* Set DMA burst length */
2055a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_DMACFG0, VGE_DMACFG0_BURSTLEN);
2056a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_DMACFG0, VGE_DMABURST_128);
2057a07bd003SBill Paul
2058a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_TXCFG, VGE_TXCFG_ARB_PRIO|VGE_TXCFG_NONBLK);
2059a07bd003SBill Paul
2060a07bd003SBill Paul /* Set collision backoff algorithm */
2061a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_CRANDOM|
2062a07bd003SBill Paul VGE_CHIPCFG1_CAP|VGE_CHIPCFG1_MBA|VGE_CHIPCFG1_BAKOPT);
2063a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_OFSET);
2064a07bd003SBill Paul
2065a07bd003SBill Paul /* Disable LPSEL field in priority resolution */
2066a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_LPSEL_DIS);
2067a07bd003SBill Paul
2068a07bd003SBill Paul /*
2069a07bd003SBill Paul * Load the addresses of the DMA queues into the chip.
2070a07bd003SBill Paul * Note that we only use one transmit queue.
2071a07bd003SBill Paul */
2072a07bd003SBill Paul
2073410f4c60SPyun YongHyeon CSR_WRITE_4(sc, VGE_TXDESC_HIADDR,
2074410f4c60SPyun YongHyeon VGE_ADDR_HI(sc->vge_rdata.vge_tx_ring_paddr));
2075a07bd003SBill Paul CSR_WRITE_4(sc, VGE_TXDESC_ADDR_LO0,
2076410f4c60SPyun YongHyeon VGE_ADDR_LO(sc->vge_rdata.vge_tx_ring_paddr));
2077a07bd003SBill Paul CSR_WRITE_2(sc, VGE_TXDESCNUM, VGE_TX_DESC_CNT - 1);
2078a07bd003SBill Paul
2079a07bd003SBill Paul CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO,
2080410f4c60SPyun YongHyeon VGE_ADDR_LO(sc->vge_rdata.vge_rx_ring_paddr));
2081a07bd003SBill Paul CSR_WRITE_2(sc, VGE_RXDESCNUM, VGE_RX_DESC_CNT - 1);
2082a07bd003SBill Paul CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, VGE_RX_DESC_CNT);
2083a07bd003SBill Paul
20843b2b8afbSPyun YongHyeon /* Configure interrupt moderation. */
20853b2b8afbSPyun YongHyeon vge_intr_holdoff(sc);
20863b2b8afbSPyun YongHyeon
2087a07bd003SBill Paul /* Enable and wake up the RX descriptor queue */
2088a07bd003SBill Paul CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
2089a07bd003SBill Paul CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
2090a07bd003SBill Paul
2091a07bd003SBill Paul /* Enable the TX descriptor queue */
2092a07bd003SBill Paul CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_RUN0);
2093a07bd003SBill Paul
2094a07bd003SBill Paul /* Init the cam filter. */
2095a07bd003SBill Paul vge_cam_clear(sc);
2096a07bd003SBill Paul
20975f07fd19SPyun YongHyeon /* Set up receiver filter. */
20985f07fd19SPyun YongHyeon vge_rxfilter(sc);
209938aa43c5SPyun YongHyeon vge_setvlan(sc);
2100a07bd003SBill Paul
210117ff418dSPyun YongHyeon /* Initialize pause timer. */
210217ff418dSPyun YongHyeon CSR_WRITE_2(sc, VGE_TX_PAUSE_TIMER, 0xFFFF);
210317ff418dSPyun YongHyeon /*
210417ff418dSPyun YongHyeon * Initialize flow control parameters.
210517ff418dSPyun YongHyeon * TX XON high threshold : 48
210617ff418dSPyun YongHyeon * TX pause low threshold : 24
210717ff418dSPyun YongHyeon * Disable hald-duplex flow control
210817ff418dSPyun YongHyeon */
210917ff418dSPyun YongHyeon CSR_WRITE_1(sc, VGE_CRC2, 0xFF);
211017ff418dSPyun YongHyeon CSR_WRITE_1(sc, VGE_CRS2, VGE_CR2_XON_ENABLE | 0x0B);
2111a07bd003SBill Paul
2112a07bd003SBill Paul /* Enable jumbo frame reception (if desired) */
2113a07bd003SBill Paul
2114a07bd003SBill Paul /* Start the MAC. */
2115a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CRC0, VGE_CR0_STOP);
2116a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_NOPOLL);
2117a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CRS0,
2118a07bd003SBill Paul VGE_CR0_TX_ENABLE|VGE_CR0_RX_ENABLE|VGE_CR0_START);
2119a07bd003SBill Paul
2120a07bd003SBill Paul #ifdef DEVICE_POLLING
2121a07bd003SBill Paul /*
2122a3f4b452SPyun YongHyeon * Disable interrupts except link state change if we are polling.
2123a07bd003SBill Paul */
21243ac0cb32SJustin Hibbits if (if_getcapenable(ifp) & IFCAP_POLLING) {
2125a3f4b452SPyun YongHyeon CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS_POLLING);
2126a07bd003SBill Paul } else /* otherwise ... */
212740929967SGleb Smirnoff #endif
2128a07bd003SBill Paul {
2129a07bd003SBill Paul /*
2130a07bd003SBill Paul * Enable interrupts.
2131a07bd003SBill Paul */
2132a07bd003SBill Paul CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
2133a3f4b452SPyun YongHyeon }
2134610dfa93SPyun YongHyeon CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
2135a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
2136a07bd003SBill Paul
21374d7235ddSPyun YongHyeon sc->vge_flags &= ~VGE_FLAG_LINK;
213866c6108dSPyun YongHyeon vge_ifmedia_upd_locked(sc);
2139a07bd003SBill Paul
21403ac0cb32SJustin Hibbits if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
21413ac0cb32SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
214267e1dfa7SJohn Baldwin callout_reset(&sc->vge_watchdog, hz, vge_watchdog, sc);
2143a07bd003SBill Paul }
2144a07bd003SBill Paul
2145a07bd003SBill Paul /*
2146a07bd003SBill Paul * Set media options.
2147a07bd003SBill Paul */
2148a07bd003SBill Paul static int
vge_ifmedia_upd(if_t ifp)21493ac0cb32SJustin Hibbits vge_ifmedia_upd(if_t ifp)
2150a07bd003SBill Paul {
2151a07bd003SBill Paul struct vge_softc *sc;
21526f530983SPyun YongHyeon int error;
2153a07bd003SBill Paul
21543ac0cb32SJustin Hibbits sc = if_getsoftc(ifp);
2155592777f6SMichael Reifenberger VGE_LOCK(sc);
215666c6108dSPyun YongHyeon error = vge_ifmedia_upd_locked(sc);
2157592777f6SMichael Reifenberger VGE_UNLOCK(sc);
2158a07bd003SBill Paul
21596f530983SPyun YongHyeon return (error);
2160a07bd003SBill Paul }
2161a07bd003SBill Paul
216266c6108dSPyun YongHyeon static int
vge_ifmedia_upd_locked(struct vge_softc * sc)216366c6108dSPyun YongHyeon vge_ifmedia_upd_locked(struct vge_softc *sc)
216466c6108dSPyun YongHyeon {
216566c6108dSPyun YongHyeon struct mii_data *mii;
216666c6108dSPyun YongHyeon struct mii_softc *miisc;
216766c6108dSPyun YongHyeon int error;
216866c6108dSPyun YongHyeon
216966c6108dSPyun YongHyeon mii = device_get_softc(sc->vge_miibus);
217066c6108dSPyun YongHyeon LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
217166c6108dSPyun YongHyeon PHY_RESET(miisc);
217266c6108dSPyun YongHyeon vge_setmedia(sc);
217366c6108dSPyun YongHyeon error = mii_mediachg(mii);
217466c6108dSPyun YongHyeon
217566c6108dSPyun YongHyeon return (error);
217666c6108dSPyun YongHyeon }
217766c6108dSPyun YongHyeon
2178a07bd003SBill Paul /*
2179a07bd003SBill Paul * Report current media status.
2180a07bd003SBill Paul */
2181a07bd003SBill Paul static void
vge_ifmedia_sts(if_t ifp,struct ifmediareq * ifmr)21823ac0cb32SJustin Hibbits vge_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
2183a07bd003SBill Paul {
2184a07bd003SBill Paul struct vge_softc *sc;
2185a07bd003SBill Paul struct mii_data *mii;
2186a07bd003SBill Paul
21873ac0cb32SJustin Hibbits sc = if_getsoftc(ifp);
2188a07bd003SBill Paul mii = device_get_softc(sc->vge_miibus);
2189a07bd003SBill Paul
219067e1dfa7SJohn Baldwin VGE_LOCK(sc);
21913ac0cb32SJustin Hibbits if ((if_getflags(ifp) & IFF_UP) == 0) {
21925f26dcd8SPyun YongHyeon VGE_UNLOCK(sc);
21935f26dcd8SPyun YongHyeon return;
21945f26dcd8SPyun YongHyeon }
2195a07bd003SBill Paul mii_pollstat(mii);
2196a07bd003SBill Paul ifmr->ifm_active = mii->mii_media_active;
2197a07bd003SBill Paul ifmr->ifm_status = mii->mii_media_status;
219857c81d92SPyun YongHyeon VGE_UNLOCK(sc);
2199a07bd003SBill Paul }
2200a07bd003SBill Paul
2201a07bd003SBill Paul static void
vge_setmedia(struct vge_softc * sc)220266c6108dSPyun YongHyeon vge_setmedia(struct vge_softc *sc)
2203a07bd003SBill Paul {
2204a07bd003SBill Paul struct mii_data *mii;
2205a07bd003SBill Paul struct ifmedia_entry *ife;
2206a07bd003SBill Paul
2207a07bd003SBill Paul mii = device_get_softc(sc->vge_miibus);
2208a07bd003SBill Paul ife = mii->mii_media.ifm_cur;
2209a07bd003SBill Paul
2210a07bd003SBill Paul /*
2211a07bd003SBill Paul * If the user manually selects a media mode, we need to turn
2212a07bd003SBill Paul * on the forced MAC mode bit in the DIAGCTL register. If the
2213a07bd003SBill Paul * user happens to choose a full duplex mode, we also need to
2214a07bd003SBill Paul * set the 'force full duplex' bit. This applies only to
2215a07bd003SBill Paul * 10Mbps and 100Mbps speeds. In autoselect mode, forced MAC
2216a07bd003SBill Paul * mode is disabled, and in 1000baseT mode, full duplex is
2217a07bd003SBill Paul * always implied, so we turn on the forced mode bit but leave
2218a07bd003SBill Paul * the FDX bit cleared.
2219a07bd003SBill Paul */
2220a07bd003SBill Paul
2221a07bd003SBill Paul switch (IFM_SUBTYPE(ife->ifm_media)) {
2222a07bd003SBill Paul case IFM_AUTO:
2223a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2224a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2225a07bd003SBill Paul break;
2226a07bd003SBill Paul case IFM_1000_T:
2227a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2228a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2229a07bd003SBill Paul break;
2230a07bd003SBill Paul case IFM_100_TX:
2231a07bd003SBill Paul case IFM_10_T:
2232a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2233a07bd003SBill Paul if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
2234a07bd003SBill Paul CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2235a07bd003SBill Paul } else {
2236a07bd003SBill Paul CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2237a07bd003SBill Paul }
2238a07bd003SBill Paul break;
2239a07bd003SBill Paul default:
224066c6108dSPyun YongHyeon device_printf(sc->vge_dev, "unknown media type: %x\n",
2241a07bd003SBill Paul IFM_SUBTYPE(ife->ifm_media));
2242a07bd003SBill Paul break;
2243a07bd003SBill Paul }
2244a07bd003SBill Paul }
2245a07bd003SBill Paul
2246a07bd003SBill Paul static int
vge_ioctl(if_t ifp,u_long command,caddr_t data)22473ac0cb32SJustin Hibbits vge_ioctl(if_t ifp, u_long command, caddr_t data)
2248a07bd003SBill Paul {
22493ac0cb32SJustin Hibbits struct vge_softc *sc = if_getsoftc(ifp);
2250a07bd003SBill Paul struct ifreq *ifr = (struct ifreq *) data;
2251a07bd003SBill Paul struct mii_data *mii;
225238aa43c5SPyun YongHyeon int error = 0, mask;
2253a07bd003SBill Paul
2254a07bd003SBill Paul switch (command) {
2255a07bd003SBill Paul case SIOCSIFMTU:
225633a0d70bSPyun YongHyeon VGE_LOCK(sc);
225733a0d70bSPyun YongHyeon if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > VGE_JUMBO_MTU)
2258a07bd003SBill Paul error = EINVAL;
22593ac0cb32SJustin Hibbits else if (if_getmtu(ifp) != ifr->ifr_mtu) {
226033a0d70bSPyun YongHyeon if (ifr->ifr_mtu > ETHERMTU &&
226133a0d70bSPyun YongHyeon (sc->vge_flags & VGE_FLAG_JUMBO) == 0)
226233a0d70bSPyun YongHyeon error = EINVAL;
226333a0d70bSPyun YongHyeon else
22643ac0cb32SJustin Hibbits if_setmtu(ifp, ifr->ifr_mtu);
226533a0d70bSPyun YongHyeon }
226633a0d70bSPyun YongHyeon VGE_UNLOCK(sc);
2267a07bd003SBill Paul break;
2268a07bd003SBill Paul case SIOCSIFFLAGS:
226967e1dfa7SJohn Baldwin VGE_LOCK(sc);
22703ac0cb32SJustin Hibbits if ((if_getflags(ifp) & IFF_UP) != 0) {
22713ac0cb32SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0 &&
22723ac0cb32SJustin Hibbits ((if_getflags(ifp) ^ sc->vge_if_flags) &
22735f07fd19SPyun YongHyeon (IFF_PROMISC | IFF_ALLMULTI)) != 0)
22745f07fd19SPyun YongHyeon vge_rxfilter(sc);
22755f07fd19SPyun YongHyeon else
227667e1dfa7SJohn Baldwin vge_init_locked(sc);
22773ac0cb32SJustin Hibbits } else if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
2278a07bd003SBill Paul vge_stop(sc);
22793ac0cb32SJustin Hibbits sc->vge_if_flags = if_getflags(ifp);
228067e1dfa7SJohn Baldwin VGE_UNLOCK(sc);
2281a07bd003SBill Paul break;
2282a07bd003SBill Paul case SIOCADDMULTI:
2283a07bd003SBill Paul case SIOCDELMULTI:
228467e1dfa7SJohn Baldwin VGE_LOCK(sc);
22853ac0cb32SJustin Hibbits if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
22865f07fd19SPyun YongHyeon vge_rxfilter(sc);
228767e1dfa7SJohn Baldwin VGE_UNLOCK(sc);
2288a07bd003SBill Paul break;
2289a07bd003SBill Paul case SIOCGIFMEDIA:
2290a07bd003SBill Paul case SIOCSIFMEDIA:
2291a07bd003SBill Paul mii = device_get_softc(sc->vge_miibus);
2292a07bd003SBill Paul error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2293a07bd003SBill Paul break;
2294a07bd003SBill Paul case SIOCSIFCAP:
22953ac0cb32SJustin Hibbits mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
229640929967SGleb Smirnoff #ifdef DEVICE_POLLING
229740929967SGleb Smirnoff if (mask & IFCAP_POLLING) {
229840929967SGleb Smirnoff if (ifr->ifr_reqcap & IFCAP_POLLING) {
229940929967SGleb Smirnoff error = ether_poll_register(vge_poll, ifp);
230040929967SGleb Smirnoff if (error)
230140929967SGleb Smirnoff return (error);
230240929967SGleb Smirnoff VGE_LOCK(sc);
230340929967SGleb Smirnoff /* Disable interrupts */
2304a3f4b452SPyun YongHyeon CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS_POLLING);
2305a3f4b452SPyun YongHyeon CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
2306a3f4b452SPyun YongHyeon CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
23073ac0cb32SJustin Hibbits if_setcapenablebit(ifp, IFCAP_POLLING, 0);
230840929967SGleb Smirnoff VGE_UNLOCK(sc);
230940929967SGleb Smirnoff } else {
231040929967SGleb Smirnoff error = ether_poll_deregister(ifp);
231140929967SGleb Smirnoff /* Enable interrupts. */
231240929967SGleb Smirnoff VGE_LOCK(sc);
231340929967SGleb Smirnoff CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
231440929967SGleb Smirnoff CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
231540929967SGleb Smirnoff CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
23163ac0cb32SJustin Hibbits if_setcapenablebit(ifp, 0, IFCAP_POLLING);
231740929967SGleb Smirnoff VGE_UNLOCK(sc);
231840929967SGleb Smirnoff }
231940929967SGleb Smirnoff }
232040929967SGleb Smirnoff #endif /* DEVICE_POLLING */
232167e1dfa7SJohn Baldwin VGE_LOCK(sc);
232220f9ef43SPyun YongHyeon if ((mask & IFCAP_TXCSUM) != 0 &&
23233ac0cb32SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
23243ac0cb32SJustin Hibbits if_togglecapenable(ifp, IFCAP_TXCSUM);
23253ac0cb32SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
23263ac0cb32SJustin Hibbits if_sethwassistbits(ifp, VGE_CSUM_FEATURES, 0);
2327a07bd003SBill Paul else
23283ac0cb32SJustin Hibbits if_sethwassistbits(ifp, 0, VGE_CSUM_FEATURES);
232940929967SGleb Smirnoff }
233020f9ef43SPyun YongHyeon if ((mask & IFCAP_RXCSUM) != 0 &&
23313ac0cb32SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_RXCSUM) != 0)
23323ac0cb32SJustin Hibbits if_togglecapenable(ifp, IFCAP_RXCSUM);
23337fc94bc4SPyun YongHyeon if ((mask & IFCAP_WOL_UCAST) != 0 &&
23343ac0cb32SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_WOL_UCAST) != 0)
23353ac0cb32SJustin Hibbits if_togglecapenable(ifp, IFCAP_WOL_UCAST);
23367fc94bc4SPyun YongHyeon if ((mask & IFCAP_WOL_MCAST) != 0 &&
23373ac0cb32SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_WOL_MCAST) != 0)
23383ac0cb32SJustin Hibbits if_togglecapenable(ifp, IFCAP_WOL_MCAST);
23397fc94bc4SPyun YongHyeon if ((mask & IFCAP_WOL_MAGIC) != 0 &&
23403ac0cb32SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_WOL_MAGIC) != 0)
23413ac0cb32SJustin Hibbits if_togglecapenable(ifp, IFCAP_WOL_MAGIC);
234238aa43c5SPyun YongHyeon if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
23433ac0cb32SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_VLAN_HWCSUM) != 0)
23443ac0cb32SJustin Hibbits if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
234538aa43c5SPyun YongHyeon if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
23463ac0cb32SJustin Hibbits (IFCAP_VLAN_HWTAGGING & if_getcapabilities(ifp)) != 0) {
23473ac0cb32SJustin Hibbits if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
234838aa43c5SPyun YongHyeon vge_setvlan(sc);
234940929967SGleb Smirnoff }
235038aa43c5SPyun YongHyeon VGE_UNLOCK(sc);
235138aa43c5SPyun YongHyeon VLAN_CAPABILITIES(ifp);
2352a07bd003SBill Paul break;
2353a07bd003SBill Paul default:
2354a07bd003SBill Paul error = ether_ioctl(ifp, command, data);
2355a07bd003SBill Paul break;
2356a07bd003SBill Paul }
2357a07bd003SBill Paul
2358a07bd003SBill Paul return (error);
2359a07bd003SBill Paul }
2360a07bd003SBill Paul
2361a07bd003SBill Paul static void
vge_watchdog(void * arg)236267e1dfa7SJohn Baldwin vge_watchdog(void *arg)
2363a07bd003SBill Paul {
2364a07bd003SBill Paul struct vge_softc *sc;
23653ac0cb32SJustin Hibbits if_t ifp;
2366a07bd003SBill Paul
236767e1dfa7SJohn Baldwin sc = arg;
236867e1dfa7SJohn Baldwin VGE_LOCK_ASSERT(sc);
23697129fb20SPyun YongHyeon vge_stats_update(sc);
237067e1dfa7SJohn Baldwin callout_reset(&sc->vge_watchdog, hz, vge_watchdog, sc);
237167e1dfa7SJohn Baldwin if (sc->vge_timer == 0 || --sc->vge_timer > 0)
237267e1dfa7SJohn Baldwin return;
237367e1dfa7SJohn Baldwin
237467e1dfa7SJohn Baldwin ifp = sc->vge_ifp;
2375f1b21184SJohn Baldwin if_printf(ifp, "watchdog timeout\n");
237641acb7e1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2377a07bd003SBill Paul
2378a07bd003SBill Paul vge_txeof(sc);
2379410f4c60SPyun YongHyeon vge_rxeof(sc, VGE_RX_DESC_CNT);
2380a07bd003SBill Paul
23813ac0cb32SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
238267e1dfa7SJohn Baldwin vge_init_locked(sc);
2383a07bd003SBill Paul }
2384a07bd003SBill Paul
2385a07bd003SBill Paul /*
2386a07bd003SBill Paul * Stop the adapter and free any mbufs allocated to the
2387a07bd003SBill Paul * RX and TX lists.
2388a07bd003SBill Paul */
2389a07bd003SBill Paul static void
vge_stop(struct vge_softc * sc)23906afe22a8SPyun YongHyeon vge_stop(struct vge_softc *sc)
2391a07bd003SBill Paul {
23923ac0cb32SJustin Hibbits if_t ifp;
2393a07bd003SBill Paul
239467e1dfa7SJohn Baldwin VGE_LOCK_ASSERT(sc);
2395fc74a9f9SBrooks Davis ifp = sc->vge_ifp;
239667e1dfa7SJohn Baldwin sc->vge_timer = 0;
239767e1dfa7SJohn Baldwin callout_stop(&sc->vge_watchdog);
2398a07bd003SBill Paul
23993ac0cb32SJustin Hibbits if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
2400a07bd003SBill Paul
2401a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
2402a07bd003SBill Paul CSR_WRITE_1(sc, VGE_CRS0, VGE_CR0_STOP);
2403a07bd003SBill Paul CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
2404a07bd003SBill Paul CSR_WRITE_2(sc, VGE_TXQCSRC, 0xFFFF);
2405a07bd003SBill Paul CSR_WRITE_1(sc, VGE_RXQCSRC, 0xFF);
2406a07bd003SBill Paul CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, 0);
2407a07bd003SBill Paul
24087129fb20SPyun YongHyeon vge_stats_update(sc);
2409410f4c60SPyun YongHyeon VGE_CHAIN_RESET(sc);
2410410f4c60SPyun YongHyeon vge_txeof(sc);
2411410f4c60SPyun YongHyeon vge_freebufs(sc);
2412a07bd003SBill Paul }
2413a07bd003SBill Paul
2414a07bd003SBill Paul /*
2415a07bd003SBill Paul * Device suspend routine. Stop the interface and save some PCI
2416a07bd003SBill Paul * settings in case the BIOS doesn't restore them properly on
2417a07bd003SBill Paul * resume.
2418a07bd003SBill Paul */
2419a07bd003SBill Paul static int
vge_suspend(device_t dev)24206afe22a8SPyun YongHyeon vge_suspend(device_t dev)
2421a07bd003SBill Paul {
2422a07bd003SBill Paul struct vge_softc *sc;
2423a07bd003SBill Paul
2424a07bd003SBill Paul sc = device_get_softc(dev);
2425a07bd003SBill Paul
242667e1dfa7SJohn Baldwin VGE_LOCK(sc);
2427a07bd003SBill Paul vge_stop(sc);
24287fc94bc4SPyun YongHyeon vge_setwol(sc);
2429a931e549SPyun YongHyeon sc->vge_flags |= VGE_FLAG_SUSPENDED;
243067e1dfa7SJohn Baldwin VGE_UNLOCK(sc);
2431a07bd003SBill Paul
2432a07bd003SBill Paul return (0);
2433a07bd003SBill Paul }
2434a07bd003SBill Paul
2435a07bd003SBill Paul /*
2436a07bd003SBill Paul * Device resume routine. Restore some PCI settings in case the BIOS
2437a07bd003SBill Paul * doesn't, re-enable busmastering, and restart the interface if
2438a07bd003SBill Paul * appropriate.
2439a07bd003SBill Paul */
2440a07bd003SBill Paul static int
vge_resume(device_t dev)24416afe22a8SPyun YongHyeon vge_resume(device_t dev)
2442a07bd003SBill Paul {
2443a07bd003SBill Paul struct vge_softc *sc;
24443ac0cb32SJustin Hibbits if_t ifp;
2445a07bd003SBill Paul
2446a07bd003SBill Paul sc = device_get_softc(dev);
244767e1dfa7SJohn Baldwin VGE_LOCK(sc);
24487fc94bc4SPyun YongHyeon vge_clrwol(sc);
24497fc94bc4SPyun YongHyeon /* Restart MII auto-polling. */
24507fc94bc4SPyun YongHyeon vge_miipoll_start(sc);
24517fc94bc4SPyun YongHyeon ifp = sc->vge_ifp;
24527fc94bc4SPyun YongHyeon /* Reinitialize interface if necessary. */
24533ac0cb32SJustin Hibbits if ((if_getflags(ifp) & IFF_UP) != 0) {
24543ac0cb32SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
245567e1dfa7SJohn Baldwin vge_init_locked(sc);
2456410f4c60SPyun YongHyeon }
2457a931e549SPyun YongHyeon sc->vge_flags &= ~VGE_FLAG_SUSPENDED;
245867e1dfa7SJohn Baldwin VGE_UNLOCK(sc);
2459a07bd003SBill Paul
2460a07bd003SBill Paul return (0);
2461a07bd003SBill Paul }
2462a07bd003SBill Paul
2463a07bd003SBill Paul /*
2464a07bd003SBill Paul * Stop all chip I/O so that the kernel's probe routines don't
2465a07bd003SBill Paul * get confused by errant DMAs when rebooting.
2466a07bd003SBill Paul */
24676a087a87SPyun YongHyeon static int
vge_shutdown(device_t dev)24686afe22a8SPyun YongHyeon vge_shutdown(device_t dev)
2469a07bd003SBill Paul {
2470a07bd003SBill Paul
24717fc94bc4SPyun YongHyeon return (vge_suspend(dev));
2472a07bd003SBill Paul }
24737129fb20SPyun YongHyeon
24747129fb20SPyun YongHyeon #define VGE_SYSCTL_STAT_ADD32(c, h, n, p, d) \
24757129fb20SPyun YongHyeon SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
24767129fb20SPyun YongHyeon
24777129fb20SPyun YongHyeon static void
vge_sysctl_node(struct vge_softc * sc)24787129fb20SPyun YongHyeon vge_sysctl_node(struct vge_softc *sc)
24797129fb20SPyun YongHyeon {
24807129fb20SPyun YongHyeon struct sysctl_ctx_list *ctx;
24817129fb20SPyun YongHyeon struct sysctl_oid_list *child, *parent;
24827129fb20SPyun YongHyeon struct sysctl_oid *tree;
24837129fb20SPyun YongHyeon struct vge_hw_stats *stats;
24847129fb20SPyun YongHyeon
24857129fb20SPyun YongHyeon stats = &sc->vge_stats;
24867129fb20SPyun YongHyeon ctx = device_get_sysctl_ctx(sc->vge_dev);
24877129fb20SPyun YongHyeon child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->vge_dev));
24883b2b8afbSPyun YongHyeon
24893b2b8afbSPyun YongHyeon SYSCTL_ADD_INT(ctx, child, OID_AUTO, "int_holdoff",
24903b2b8afbSPyun YongHyeon CTLFLAG_RW, &sc->vge_int_holdoff, 0, "interrupt holdoff");
24913b2b8afbSPyun YongHyeon SYSCTL_ADD_INT(ctx, child, OID_AUTO, "rx_coal_pkt",
24923b2b8afbSPyun YongHyeon CTLFLAG_RW, &sc->vge_rx_coal_pkt, 0, "rx coalescing packet");
24933b2b8afbSPyun YongHyeon SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_coal_pkt",
24943b2b8afbSPyun YongHyeon CTLFLAG_RW, &sc->vge_tx_coal_pkt, 0, "tx coalescing packet");
24953b2b8afbSPyun YongHyeon
24963b2b8afbSPyun YongHyeon /* Pull in device tunables. */
24973b2b8afbSPyun YongHyeon sc->vge_int_holdoff = VGE_INT_HOLDOFF_DEFAULT;
24983b2b8afbSPyun YongHyeon resource_int_value(device_get_name(sc->vge_dev),
24993b2b8afbSPyun YongHyeon device_get_unit(sc->vge_dev), "int_holdoff", &sc->vge_int_holdoff);
25003b2b8afbSPyun YongHyeon sc->vge_rx_coal_pkt = VGE_RX_COAL_PKT_DEFAULT;
25013b2b8afbSPyun YongHyeon resource_int_value(device_get_name(sc->vge_dev),
25023b2b8afbSPyun YongHyeon device_get_unit(sc->vge_dev), "rx_coal_pkt", &sc->vge_rx_coal_pkt);
25033b2b8afbSPyun YongHyeon sc->vge_tx_coal_pkt = VGE_TX_COAL_PKT_DEFAULT;
25043b2b8afbSPyun YongHyeon resource_int_value(device_get_name(sc->vge_dev),
25053b2b8afbSPyun YongHyeon device_get_unit(sc->vge_dev), "tx_coal_pkt", &sc->vge_tx_coal_pkt);
25063b2b8afbSPyun YongHyeon
25077029da5cSPawel Biernacki tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats",
25087029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "VGE statistics");
25097129fb20SPyun YongHyeon parent = SYSCTL_CHILDREN(tree);
25107129fb20SPyun YongHyeon
25117129fb20SPyun YongHyeon /* Rx statistics. */
25127029da5cSPawel Biernacki tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx",
25137029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "RX MAC statistics");
25147129fb20SPyun YongHyeon child = SYSCTL_CHILDREN(tree);
25157129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames",
25167129fb20SPyun YongHyeon &stats->rx_frames, "frames");
25177129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
25187129fb20SPyun YongHyeon &stats->rx_good_frames, "Good frames");
25197129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "fifo_oflows",
25207129fb20SPyun YongHyeon &stats->rx_fifo_oflows, "FIFO overflows");
25217129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "runts",
25227129fb20SPyun YongHyeon &stats->rx_runts, "Too short frames");
25237129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "runts_errs",
25247129fb20SPyun YongHyeon &stats->rx_runts_errs, "Too short frames with errors");
25257129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
25267129fb20SPyun YongHyeon &stats->rx_pkts_64, "64 bytes frames");
25277129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
25287129fb20SPyun YongHyeon &stats->rx_pkts_65_127, "65 to 127 bytes frames");
25297129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
25307129fb20SPyun YongHyeon &stats->rx_pkts_128_255, "128 to 255 bytes frames");
25317129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
25327129fb20SPyun YongHyeon &stats->rx_pkts_256_511, "256 to 511 bytes frames");
25337129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
25347129fb20SPyun YongHyeon &stats->rx_pkts_512_1023, "512 to 1023 bytes frames");
25357129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
25367129fb20SPyun YongHyeon &stats->rx_pkts_1024_1518, "1024 to 1518 bytes frames");
25377129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max",
25387129fb20SPyun YongHyeon &stats->rx_pkts_1519_max, "1519 to max frames");
25397129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max_errs",
25407129fb20SPyun YongHyeon &stats->rx_pkts_1519_max_errs, "1519 to max frames with error");
25417129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_jumbo",
25427129fb20SPyun YongHyeon &stats->rx_jumbos, "Jumbo frames");
25437129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "crcerrs",
25447129fb20SPyun YongHyeon &stats->rx_crcerrs, "CRC errors");
25457129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
25460a5d2802SPriit Trees &stats->rx_pause_frames, "Pause frames");
25477129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "align_errs",
25487129fb20SPyun YongHyeon &stats->rx_alignerrs, "Alignment errors");
25497129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "nobufs",
25507129fb20SPyun YongHyeon &stats->rx_nobufs, "Frames with no buffer event");
25517129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "sym_errs",
25527129fb20SPyun YongHyeon &stats->rx_symerrs, "Frames with symbol errors");
25537129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
25547129fb20SPyun YongHyeon &stats->rx_lenerrs, "Frames with length mismatched");
25557129fb20SPyun YongHyeon
25567129fb20SPyun YongHyeon /* Tx statistics. */
25577029da5cSPawel Biernacki tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx",
25587029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TX MAC statistics");
25597129fb20SPyun YongHyeon child = SYSCTL_CHILDREN(tree);
25607129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
25617129fb20SPyun YongHyeon &stats->tx_good_frames, "Good frames");
25627129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
25637129fb20SPyun YongHyeon &stats->tx_pkts_64, "64 bytes frames");
25647129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
25657129fb20SPyun YongHyeon &stats->tx_pkts_65_127, "65 to 127 bytes frames");
25667129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
25677129fb20SPyun YongHyeon &stats->tx_pkts_128_255, "128 to 255 bytes frames");
25687129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
25697129fb20SPyun YongHyeon &stats->tx_pkts_256_511, "256 to 511 bytes frames");
25707129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
25717129fb20SPyun YongHyeon &stats->tx_pkts_512_1023, "512 to 1023 bytes frames");
25727129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
25737129fb20SPyun YongHyeon &stats->tx_pkts_1024_1518, "1024 to 1518 bytes frames");
25747129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_jumbo",
25757129fb20SPyun YongHyeon &stats->tx_jumbos, "Jumbo frames");
25767129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "colls",
25777129fb20SPyun YongHyeon &stats->tx_colls, "Collisions");
25787129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "late_colls",
25797129fb20SPyun YongHyeon &stats->tx_latecolls, "Late collisions");
25807129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
25817129fb20SPyun YongHyeon &stats->tx_pause, "Pause frames");
25827129fb20SPyun YongHyeon #ifdef VGE_ENABLE_SQEERR
25837129fb20SPyun YongHyeon VGE_SYSCTL_STAT_ADD32(ctx, child, "sqeerrs",
25847129fb20SPyun YongHyeon &stats->tx_sqeerrs, "SQE errors");
25857129fb20SPyun YongHyeon #endif
25867129fb20SPyun YongHyeon /* Clear MAC statistics. */
25877129fb20SPyun YongHyeon vge_stats_clear(sc);
25887129fb20SPyun YongHyeon }
25897129fb20SPyun YongHyeon
25907129fb20SPyun YongHyeon #undef VGE_SYSCTL_STAT_ADD32
25917129fb20SPyun YongHyeon
25927129fb20SPyun YongHyeon static void
vge_stats_clear(struct vge_softc * sc)25937129fb20SPyun YongHyeon vge_stats_clear(struct vge_softc *sc)
25947129fb20SPyun YongHyeon {
25957129fb20SPyun YongHyeon int i;
25967129fb20SPyun YongHyeon
25977129fb20SPyun YongHyeon CSR_WRITE_1(sc, VGE_MIBCSR,
25987129fb20SPyun YongHyeon CSR_READ_1(sc, VGE_MIBCSR) | VGE_MIBCSR_FREEZE);
25997129fb20SPyun YongHyeon CSR_WRITE_1(sc, VGE_MIBCSR,
26007129fb20SPyun YongHyeon CSR_READ_1(sc, VGE_MIBCSR) | VGE_MIBCSR_CLR);
26017129fb20SPyun YongHyeon for (i = VGE_TIMEOUT; i > 0; i--) {
26027129fb20SPyun YongHyeon DELAY(1);
26037129fb20SPyun YongHyeon if ((CSR_READ_1(sc, VGE_MIBCSR) & VGE_MIBCSR_CLR) == 0)
26047129fb20SPyun YongHyeon break;
26057129fb20SPyun YongHyeon }
26067129fb20SPyun YongHyeon if (i == 0)
26077129fb20SPyun YongHyeon device_printf(sc->vge_dev, "MIB clear timed out!\n");
26087129fb20SPyun YongHyeon CSR_WRITE_1(sc, VGE_MIBCSR, CSR_READ_1(sc, VGE_MIBCSR) &
26097129fb20SPyun YongHyeon ~VGE_MIBCSR_FREEZE);
26107129fb20SPyun YongHyeon }
26117129fb20SPyun YongHyeon
26127129fb20SPyun YongHyeon static void
vge_stats_update(struct vge_softc * sc)26137129fb20SPyun YongHyeon vge_stats_update(struct vge_softc *sc)
26147129fb20SPyun YongHyeon {
26157129fb20SPyun YongHyeon struct vge_hw_stats *stats;
26163ac0cb32SJustin Hibbits if_t ifp;
26177129fb20SPyun YongHyeon uint32_t mib[VGE_MIB_CNT], val;
26187129fb20SPyun YongHyeon int i;
26197129fb20SPyun YongHyeon
26207129fb20SPyun YongHyeon VGE_LOCK_ASSERT(sc);
26217129fb20SPyun YongHyeon
26227129fb20SPyun YongHyeon stats = &sc->vge_stats;
26237129fb20SPyun YongHyeon ifp = sc->vge_ifp;
26247129fb20SPyun YongHyeon
26257129fb20SPyun YongHyeon CSR_WRITE_1(sc, VGE_MIBCSR,
26267129fb20SPyun YongHyeon CSR_READ_1(sc, VGE_MIBCSR) | VGE_MIBCSR_FLUSH);
26277129fb20SPyun YongHyeon for (i = VGE_TIMEOUT; i > 0; i--) {
26287129fb20SPyun YongHyeon DELAY(1);
26297129fb20SPyun YongHyeon if ((CSR_READ_1(sc, VGE_MIBCSR) & VGE_MIBCSR_FLUSH) == 0)
26307129fb20SPyun YongHyeon break;
26317129fb20SPyun YongHyeon }
26327129fb20SPyun YongHyeon if (i == 0) {
26337129fb20SPyun YongHyeon device_printf(sc->vge_dev, "MIB counter dump timed out!\n");
26347129fb20SPyun YongHyeon vge_stats_clear(sc);
26357129fb20SPyun YongHyeon return;
26367129fb20SPyun YongHyeon }
26377129fb20SPyun YongHyeon
26387129fb20SPyun YongHyeon bzero(mib, sizeof(mib));
26397129fb20SPyun YongHyeon reset_idx:
26407129fb20SPyun YongHyeon /* Set MIB read index to 0. */
26417129fb20SPyun YongHyeon CSR_WRITE_1(sc, VGE_MIBCSR,
26427129fb20SPyun YongHyeon CSR_READ_1(sc, VGE_MIBCSR) | VGE_MIBCSR_RINI);
26437129fb20SPyun YongHyeon for (i = 0; i < VGE_MIB_CNT; i++) {
26447129fb20SPyun YongHyeon val = CSR_READ_4(sc, VGE_MIBDATA);
26457129fb20SPyun YongHyeon if (i != VGE_MIB_DATA_IDX(val)) {
26467129fb20SPyun YongHyeon /* Reading interrupted. */
26477129fb20SPyun YongHyeon goto reset_idx;
26487129fb20SPyun YongHyeon }
26497129fb20SPyun YongHyeon mib[i] = val & VGE_MIB_DATA_MASK;
26507129fb20SPyun YongHyeon }
26517129fb20SPyun YongHyeon
26527129fb20SPyun YongHyeon /* Rx stats. */
26537129fb20SPyun YongHyeon stats->rx_frames += mib[VGE_MIB_RX_FRAMES];
26547129fb20SPyun YongHyeon stats->rx_good_frames += mib[VGE_MIB_RX_GOOD_FRAMES];
26557129fb20SPyun YongHyeon stats->rx_fifo_oflows += mib[VGE_MIB_RX_FIFO_OVERRUNS];
26567129fb20SPyun YongHyeon stats->rx_runts += mib[VGE_MIB_RX_RUNTS];
26577129fb20SPyun YongHyeon stats->rx_runts_errs += mib[VGE_MIB_RX_RUNTS_ERRS];
26587129fb20SPyun YongHyeon stats->rx_pkts_64 += mib[VGE_MIB_RX_PKTS_64];
26597129fb20SPyun YongHyeon stats->rx_pkts_65_127 += mib[VGE_MIB_RX_PKTS_65_127];
26607129fb20SPyun YongHyeon stats->rx_pkts_128_255 += mib[VGE_MIB_RX_PKTS_128_255];
26617129fb20SPyun YongHyeon stats->rx_pkts_256_511 += mib[VGE_MIB_RX_PKTS_256_511];
26627129fb20SPyun YongHyeon stats->rx_pkts_512_1023 += mib[VGE_MIB_RX_PKTS_512_1023];
26637129fb20SPyun YongHyeon stats->rx_pkts_1024_1518 += mib[VGE_MIB_RX_PKTS_1024_1518];
26647129fb20SPyun YongHyeon stats->rx_pkts_1519_max += mib[VGE_MIB_RX_PKTS_1519_MAX];
26657129fb20SPyun YongHyeon stats->rx_pkts_1519_max_errs += mib[VGE_MIB_RX_PKTS_1519_MAX_ERRS];
26667129fb20SPyun YongHyeon stats->rx_jumbos += mib[VGE_MIB_RX_JUMBOS];
26677129fb20SPyun YongHyeon stats->rx_crcerrs += mib[VGE_MIB_RX_CRCERRS];
26687129fb20SPyun YongHyeon stats->rx_pause_frames += mib[VGE_MIB_RX_PAUSE];
26697129fb20SPyun YongHyeon stats->rx_alignerrs += mib[VGE_MIB_RX_ALIGNERRS];
26707129fb20SPyun YongHyeon stats->rx_nobufs += mib[VGE_MIB_RX_NOBUFS];
26717129fb20SPyun YongHyeon stats->rx_symerrs += mib[VGE_MIB_RX_SYMERRS];
26727129fb20SPyun YongHyeon stats->rx_lenerrs += mib[VGE_MIB_RX_LENERRS];
26737129fb20SPyun YongHyeon
26747129fb20SPyun YongHyeon /* Tx stats. */
26757129fb20SPyun YongHyeon stats->tx_good_frames += mib[VGE_MIB_TX_GOOD_FRAMES];
26767129fb20SPyun YongHyeon stats->tx_pkts_64 += mib[VGE_MIB_TX_PKTS_64];
26777129fb20SPyun YongHyeon stats->tx_pkts_65_127 += mib[VGE_MIB_TX_PKTS_65_127];
26787129fb20SPyun YongHyeon stats->tx_pkts_128_255 += mib[VGE_MIB_TX_PKTS_128_255];
26797129fb20SPyun YongHyeon stats->tx_pkts_256_511 += mib[VGE_MIB_TX_PKTS_256_511];
26807129fb20SPyun YongHyeon stats->tx_pkts_512_1023 += mib[VGE_MIB_TX_PKTS_512_1023];
26817129fb20SPyun YongHyeon stats->tx_pkts_1024_1518 += mib[VGE_MIB_TX_PKTS_1024_1518];
26827129fb20SPyun YongHyeon stats->tx_jumbos += mib[VGE_MIB_TX_JUMBOS];
26837129fb20SPyun YongHyeon stats->tx_colls += mib[VGE_MIB_TX_COLLS];
26847129fb20SPyun YongHyeon stats->tx_pause += mib[VGE_MIB_TX_PAUSE];
26857129fb20SPyun YongHyeon #ifdef VGE_ENABLE_SQEERR
26867129fb20SPyun YongHyeon stats->tx_sqeerrs += mib[VGE_MIB_TX_SQEERRS];
26877129fb20SPyun YongHyeon #endif
26887129fb20SPyun YongHyeon stats->tx_latecolls += mib[VGE_MIB_TX_LATECOLLS];
26897129fb20SPyun YongHyeon
26907129fb20SPyun YongHyeon /* Update counters in ifnet. */
269141acb7e1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, mib[VGE_MIB_TX_GOOD_FRAMES]);
26927129fb20SPyun YongHyeon
269341acb7e1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_COLLISIONS,
269441acb7e1SGleb Smirnoff mib[VGE_MIB_TX_COLLS] + mib[VGE_MIB_TX_LATECOLLS]);
26957129fb20SPyun YongHyeon
269641acb7e1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS,
269741acb7e1SGleb Smirnoff mib[VGE_MIB_TX_COLLS] + mib[VGE_MIB_TX_LATECOLLS]);
26987129fb20SPyun YongHyeon
269941acb7e1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, mib[VGE_MIB_RX_GOOD_FRAMES]);
27007129fb20SPyun YongHyeon
270141acb7e1SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IERRORS,
270241acb7e1SGleb Smirnoff mib[VGE_MIB_RX_FIFO_OVERRUNS] +
27037129fb20SPyun YongHyeon mib[VGE_MIB_RX_RUNTS] +
27047129fb20SPyun YongHyeon mib[VGE_MIB_RX_RUNTS_ERRS] +
27057129fb20SPyun YongHyeon mib[VGE_MIB_RX_CRCERRS] +
27067129fb20SPyun YongHyeon mib[VGE_MIB_RX_ALIGNERRS] +
27077129fb20SPyun YongHyeon mib[VGE_MIB_RX_NOBUFS] +
27087129fb20SPyun YongHyeon mib[VGE_MIB_RX_SYMERRS] +
270941acb7e1SGleb Smirnoff mib[VGE_MIB_RX_LENERRS]);
27107129fb20SPyun YongHyeon }
27113b2b8afbSPyun YongHyeon
27123b2b8afbSPyun YongHyeon static void
vge_intr_holdoff(struct vge_softc * sc)27133b2b8afbSPyun YongHyeon vge_intr_holdoff(struct vge_softc *sc)
27143b2b8afbSPyun YongHyeon {
27153b2b8afbSPyun YongHyeon uint8_t intctl;
27163b2b8afbSPyun YongHyeon
27173b2b8afbSPyun YongHyeon VGE_LOCK_ASSERT(sc);
27183b2b8afbSPyun YongHyeon
27193b2b8afbSPyun YongHyeon /*
27203b2b8afbSPyun YongHyeon * Set Tx interrupt supression threshold.
27213b2b8afbSPyun YongHyeon * It's possible to use single-shot timer in VGE_CRS1 register
27223b2b8afbSPyun YongHyeon * in Tx path such that driver can remove most of Tx completion
27233b2b8afbSPyun YongHyeon * interrupts. However this requires additional access to
27243b2b8afbSPyun YongHyeon * VGE_CRS1 register to reload the timer in addintion to
27253b2b8afbSPyun YongHyeon * activating Tx kick command. Another downside is we don't know
27263b2b8afbSPyun YongHyeon * what single-shot timer value should be used in advance so
27273b2b8afbSPyun YongHyeon * reclaiming transmitted mbufs could be delayed a lot which in
27283b2b8afbSPyun YongHyeon * turn slows down Tx operation.
27293b2b8afbSPyun YongHyeon */
27303b2b8afbSPyun YongHyeon CSR_WRITE_1(sc, VGE_CAMCTL, VGE_PAGESEL_TXSUPPTHR);
27313b2b8afbSPyun YongHyeon CSR_WRITE_1(sc, VGE_TXSUPPTHR, sc->vge_tx_coal_pkt);
27323b2b8afbSPyun YongHyeon
27333b2b8afbSPyun YongHyeon /* Set Rx interrupt suppresion threshold. */
27343b2b8afbSPyun YongHyeon CSR_WRITE_1(sc, VGE_CAMCTL, VGE_PAGESEL_RXSUPPTHR);
27353b2b8afbSPyun YongHyeon CSR_WRITE_1(sc, VGE_RXSUPPTHR, sc->vge_rx_coal_pkt);
27363b2b8afbSPyun YongHyeon
27373b2b8afbSPyun YongHyeon intctl = CSR_READ_1(sc, VGE_INTCTL1);
27383b2b8afbSPyun YongHyeon intctl &= ~VGE_INTCTL_SC_RELOAD;
27393b2b8afbSPyun YongHyeon intctl |= VGE_INTCTL_HC_RELOAD;
27403b2b8afbSPyun YongHyeon if (sc->vge_tx_coal_pkt <= 0)
27413b2b8afbSPyun YongHyeon intctl |= VGE_INTCTL_TXINTSUP_DISABLE;
27423b2b8afbSPyun YongHyeon else
27433b2b8afbSPyun YongHyeon intctl &= ~VGE_INTCTL_TXINTSUP_DISABLE;
27443b2b8afbSPyun YongHyeon if (sc->vge_rx_coal_pkt <= 0)
27453b2b8afbSPyun YongHyeon intctl |= VGE_INTCTL_RXINTSUP_DISABLE;
27463b2b8afbSPyun YongHyeon else
27473b2b8afbSPyun YongHyeon intctl &= ~VGE_INTCTL_RXINTSUP_DISABLE;
27483b2b8afbSPyun YongHyeon CSR_WRITE_1(sc, VGE_INTCTL1, intctl);
27493b2b8afbSPyun YongHyeon CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_HOLDOFF);
27503b2b8afbSPyun YongHyeon if (sc->vge_int_holdoff > 0) {
27513b2b8afbSPyun YongHyeon /* Set interrupt holdoff timer. */
27523b2b8afbSPyun YongHyeon CSR_WRITE_1(sc, VGE_CAMCTL, VGE_PAGESEL_INTHLDOFF);
27533b2b8afbSPyun YongHyeon CSR_WRITE_1(sc, VGE_INTHOLDOFF,
27543b2b8afbSPyun YongHyeon VGE_INT_HOLDOFF_USEC(sc->vge_int_holdoff));
27553b2b8afbSPyun YongHyeon /* Enable holdoff timer. */
27563b2b8afbSPyun YongHyeon CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_HOLDOFF);
27573b2b8afbSPyun YongHyeon }
27583b2b8afbSPyun YongHyeon }
27597fc94bc4SPyun YongHyeon
27607fc94bc4SPyun YongHyeon static void
vge_setlinkspeed(struct vge_softc * sc)27617fc94bc4SPyun YongHyeon vge_setlinkspeed(struct vge_softc *sc)
27627fc94bc4SPyun YongHyeon {
27637fc94bc4SPyun YongHyeon struct mii_data *mii;
27647fc94bc4SPyun YongHyeon int aneg, i;
27657fc94bc4SPyun YongHyeon
27667fc94bc4SPyun YongHyeon VGE_LOCK_ASSERT(sc);
27677fc94bc4SPyun YongHyeon
27687fc94bc4SPyun YongHyeon mii = device_get_softc(sc->vge_miibus);
27697fc94bc4SPyun YongHyeon mii_pollstat(mii);
27707fc94bc4SPyun YongHyeon aneg = 0;
27717fc94bc4SPyun YongHyeon if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
27727fc94bc4SPyun YongHyeon (IFM_ACTIVE | IFM_AVALID)) {
27737fc94bc4SPyun YongHyeon switch IFM_SUBTYPE(mii->mii_media_active) {
27747fc94bc4SPyun YongHyeon case IFM_10_T:
27757fc94bc4SPyun YongHyeon case IFM_100_TX:
27767fc94bc4SPyun YongHyeon return;
27777fc94bc4SPyun YongHyeon case IFM_1000_T:
27787fc94bc4SPyun YongHyeon aneg++;
27797fc94bc4SPyun YongHyeon default:
27807fc94bc4SPyun YongHyeon break;
27817fc94bc4SPyun YongHyeon }
27827fc94bc4SPyun YongHyeon }
278366c6108dSPyun YongHyeon /* Clear forced MAC speed/duplex configuration. */
278466c6108dSPyun YongHyeon CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
278566c6108dSPyun YongHyeon CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
27867fc94bc4SPyun YongHyeon vge_miibus_writereg(sc->vge_dev, sc->vge_phyaddr, MII_100T2CR, 0);
27877fc94bc4SPyun YongHyeon vge_miibus_writereg(sc->vge_dev, sc->vge_phyaddr, MII_ANAR,
27887fc94bc4SPyun YongHyeon ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
27897fc94bc4SPyun YongHyeon vge_miibus_writereg(sc->vge_dev, sc->vge_phyaddr, MII_BMCR,
27907fc94bc4SPyun YongHyeon BMCR_AUTOEN | BMCR_STARTNEG);
27917fc94bc4SPyun YongHyeon DELAY(1000);
27927fc94bc4SPyun YongHyeon if (aneg != 0) {
27937fc94bc4SPyun YongHyeon /* Poll link state until vge(4) get a 10/100 link. */
27947fc94bc4SPyun YongHyeon for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
27957fc94bc4SPyun YongHyeon mii_pollstat(mii);
27967fc94bc4SPyun YongHyeon if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
27977fc94bc4SPyun YongHyeon == (IFM_ACTIVE | IFM_AVALID)) {
27987fc94bc4SPyun YongHyeon switch (IFM_SUBTYPE(mii->mii_media_active)) {
27997fc94bc4SPyun YongHyeon case IFM_10_T:
28007fc94bc4SPyun YongHyeon case IFM_100_TX:
28017fc94bc4SPyun YongHyeon return;
28027fc94bc4SPyun YongHyeon default:
28037fc94bc4SPyun YongHyeon break;
28047fc94bc4SPyun YongHyeon }
28057fc94bc4SPyun YongHyeon }
28067fc94bc4SPyun YongHyeon VGE_UNLOCK(sc);
28077fc94bc4SPyun YongHyeon pause("vgelnk", hz);
28087fc94bc4SPyun YongHyeon VGE_LOCK(sc);
28097fc94bc4SPyun YongHyeon }
28107fc94bc4SPyun YongHyeon if (i == MII_ANEGTICKS_GIGE)
28117fc94bc4SPyun YongHyeon device_printf(sc->vge_dev, "establishing link failed, "
28127fc94bc4SPyun YongHyeon "WOL may not work!");
28137fc94bc4SPyun YongHyeon }
28147fc94bc4SPyun YongHyeon /*
28157fc94bc4SPyun YongHyeon * No link, force MAC to have 100Mbps, full-duplex link.
28167fc94bc4SPyun YongHyeon * This is the last resort and may/may not work.
28177fc94bc4SPyun YongHyeon */
28187fc94bc4SPyun YongHyeon mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
28197fc94bc4SPyun YongHyeon mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
28207fc94bc4SPyun YongHyeon }
28217fc94bc4SPyun YongHyeon
28227fc94bc4SPyun YongHyeon static void
vge_setwol(struct vge_softc * sc)28237fc94bc4SPyun YongHyeon vge_setwol(struct vge_softc *sc)
28247fc94bc4SPyun YongHyeon {
28253ac0cb32SJustin Hibbits if_t ifp;
28267fc94bc4SPyun YongHyeon uint8_t val;
28277fc94bc4SPyun YongHyeon
28287fc94bc4SPyun YongHyeon VGE_LOCK_ASSERT(sc);
28297fc94bc4SPyun YongHyeon
28307fc94bc4SPyun YongHyeon if ((sc->vge_flags & VGE_FLAG_PMCAP) == 0) {
28317fc94bc4SPyun YongHyeon /* No PME capability, PHY power down. */
28327fc94bc4SPyun YongHyeon vge_miibus_writereg(sc->vge_dev, sc->vge_phyaddr, MII_BMCR,
28337fc94bc4SPyun YongHyeon BMCR_PDOWN);
28347fc94bc4SPyun YongHyeon vge_miipoll_stop(sc);
28357fc94bc4SPyun YongHyeon return;
28367fc94bc4SPyun YongHyeon }
28377fc94bc4SPyun YongHyeon
28387fc94bc4SPyun YongHyeon ifp = sc->vge_ifp;
28397fc94bc4SPyun YongHyeon
28407fc94bc4SPyun YongHyeon /* Clear WOL on pattern match. */
28417fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_WOLCR0C, VGE_WOLCR0_PATTERN_ALL);
28427fc94bc4SPyun YongHyeon /* Disable WOL on magic/unicast packet. */
28437fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_WOLCR1C, 0x0F);
28447fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_WOLCFGC, VGE_WOLCFG_SAB | VGE_WOLCFG_SAM |
28457fc94bc4SPyun YongHyeon VGE_WOLCFG_PMEOVR);
28463ac0cb32SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_WOL) != 0) {
28477fc94bc4SPyun YongHyeon vge_setlinkspeed(sc);
28487fc94bc4SPyun YongHyeon val = 0;
28493ac0cb32SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_WOL_UCAST) != 0)
28507fc94bc4SPyun YongHyeon val |= VGE_WOLCR1_UCAST;
28513ac0cb32SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_WOL_MAGIC) != 0)
28527fc94bc4SPyun YongHyeon val |= VGE_WOLCR1_MAGIC;
28537fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_WOLCR1S, val);
28547fc94bc4SPyun YongHyeon val = 0;
28553ac0cb32SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_WOL_MCAST) != 0)
28567fc94bc4SPyun YongHyeon val |= VGE_WOLCFG_SAM | VGE_WOLCFG_SAB;
28577fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_WOLCFGS, val | VGE_WOLCFG_PMEOVR);
28587fc94bc4SPyun YongHyeon /* Disable MII auto-polling. */
28597fc94bc4SPyun YongHyeon vge_miipoll_stop(sc);
28607fc94bc4SPyun YongHyeon }
28617fc94bc4SPyun YongHyeon CSR_SETBIT_1(sc, VGE_DIAGCTL,
28627fc94bc4SPyun YongHyeon VGE_DIAGCTL_MACFORCE | VGE_DIAGCTL_FDXFORCE);
28637fc94bc4SPyun YongHyeon CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_GMII);
28647fc94bc4SPyun YongHyeon
28657fc94bc4SPyun YongHyeon /* Clear WOL status on pattern match. */
28667fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_WOLSR0C, 0xFF);
28677fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_WOLSR1C, 0xFF);
28687fc94bc4SPyun YongHyeon
28697fc94bc4SPyun YongHyeon val = CSR_READ_1(sc, VGE_PWRSTAT);
28707fc94bc4SPyun YongHyeon val |= VGE_STICKHW_SWPTAG;
28717fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_PWRSTAT, val);
28727fc94bc4SPyun YongHyeon /* Put hardware into sleep. */
28737fc94bc4SPyun YongHyeon val = CSR_READ_1(sc, VGE_PWRSTAT);
28747fc94bc4SPyun YongHyeon val |= VGE_STICKHW_DS0 | VGE_STICKHW_DS1;
28757fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_PWRSTAT, val);
28767fc94bc4SPyun YongHyeon /* Request PME if WOL is requested. */
28773ac0cb32SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_WOL) != 0)
2878*ddaf6524SJohn Baldwin pci_enable_pme(sc->vge_dev);
28797fc94bc4SPyun YongHyeon }
28807fc94bc4SPyun YongHyeon
28817fc94bc4SPyun YongHyeon static void
vge_clrwol(struct vge_softc * sc)28827fc94bc4SPyun YongHyeon vge_clrwol(struct vge_softc *sc)
28837fc94bc4SPyun YongHyeon {
28847fc94bc4SPyun YongHyeon uint8_t val;
28857fc94bc4SPyun YongHyeon
28867fc94bc4SPyun YongHyeon val = CSR_READ_1(sc, VGE_PWRSTAT);
28877fc94bc4SPyun YongHyeon val &= ~VGE_STICKHW_SWPTAG;
28887fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_PWRSTAT, val);
28897fc94bc4SPyun YongHyeon /* Disable WOL and clear power state indicator. */
28907fc94bc4SPyun YongHyeon val = CSR_READ_1(sc, VGE_PWRSTAT);
28917fc94bc4SPyun YongHyeon val &= ~(VGE_STICKHW_DS0 | VGE_STICKHW_DS1);
28927fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_PWRSTAT, val);
28937fc94bc4SPyun YongHyeon
28947fc94bc4SPyun YongHyeon CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_GMII);
28957fc94bc4SPyun YongHyeon CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
28967fc94bc4SPyun YongHyeon
28977fc94bc4SPyun YongHyeon /* Clear WOL on pattern match. */
28987fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_WOLCR0C, VGE_WOLCR0_PATTERN_ALL);
28997fc94bc4SPyun YongHyeon /* Disable WOL on magic/unicast packet. */
29007fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_WOLCR1C, 0x0F);
29017fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_WOLCFGC, VGE_WOLCFG_SAB | VGE_WOLCFG_SAM |
29027fc94bc4SPyun YongHyeon VGE_WOLCFG_PMEOVR);
29037fc94bc4SPyun YongHyeon /* Clear WOL status on pattern match. */
29047fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_WOLSR0C, 0xFF);
29057fc94bc4SPyun YongHyeon CSR_WRITE_1(sc, VGE_WOLSR1C, 0xFF);
29067fc94bc4SPyun YongHyeon }
2907