1d193ed0bSPyun YongHyeon /*-
2df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni *
4d193ed0bSPyun YongHyeon * Copyright (c) 2008-2010 Nikolay Denev <ndenev@gmail.com>
5d193ed0bSPyun YongHyeon * Copyright (c) 2007-2008 Alexander Pohoyda <alexander.pohoyda@gmx.net>
6d193ed0bSPyun YongHyeon * Copyright (c) 1997, 1998, 1999
7d193ed0bSPyun YongHyeon * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
8d193ed0bSPyun YongHyeon *
9d193ed0bSPyun YongHyeon * Redistribution and use in source and binary forms, with or without
10d193ed0bSPyun YongHyeon * modification, are permitted provided that the following conditions
11d193ed0bSPyun YongHyeon * are met:
12d193ed0bSPyun YongHyeon * 1. Redistributions of source code must retain the above copyright
13d193ed0bSPyun YongHyeon * notice, this list of conditions and the following disclaimer.
14d193ed0bSPyun YongHyeon * 2. Redistributions in binary form must reproduce the above copyright
15d193ed0bSPyun YongHyeon * notice, this list of conditions and the following disclaimer in the
16d193ed0bSPyun YongHyeon * documentation and/or other materials provided with the distribution.
17d193ed0bSPyun YongHyeon * 3. All advertising materials mentioning features or use of this software
18d193ed0bSPyun YongHyeon * must display the following acknowledgement:
19d193ed0bSPyun YongHyeon * This product includes software developed by Bill Paul.
20d193ed0bSPyun YongHyeon * 4. Neither the name of the author nor the names of any co-contributors
21d193ed0bSPyun YongHyeon * may be used to endorse or promote products derived from this software
22d193ed0bSPyun YongHyeon * without specific prior written permission.
23d193ed0bSPyun YongHyeon *
24d193ed0bSPyun YongHyeon * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS''
25d193ed0bSPyun YongHyeon * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26d193ed0bSPyun YongHyeon * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27d193ed0bSPyun YongHyeon * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AUTHORS OR
28d193ed0bSPyun YongHyeon * THE VOICES IN THEIR HEADS BE LIABLE FOR ANY DIRECT, INDIRECT,
29d193ed0bSPyun YongHyeon * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30d193ed0bSPyun YongHyeon * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31d193ed0bSPyun YongHyeon * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32d193ed0bSPyun YongHyeon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33d193ed0bSPyun YongHyeon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34d193ed0bSPyun YongHyeon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35d193ed0bSPyun YongHyeon * OF THE POSSIBILITY OF SUCH DAMAGE.
36d193ed0bSPyun YongHyeon */
37d193ed0bSPyun YongHyeon
38d193ed0bSPyun YongHyeon #include <sys/cdefs.h>
39d193ed0bSPyun YongHyeon /*
40d193ed0bSPyun YongHyeon * SiS 190/191 PCI Ethernet NIC driver.
41d193ed0bSPyun YongHyeon *
42d193ed0bSPyun YongHyeon * Adapted to SiS 190 NIC by Alexander Pohoyda based on the original
43d193ed0bSPyun YongHyeon * SiS 900 driver by Bill Paul, using SiS 190/191 Solaris driver by
44d193ed0bSPyun YongHyeon * Masayuki Murayama and SiS 190/191 GNU/Linux driver by K.M. Liu
45d193ed0bSPyun YongHyeon * <kmliu@sis.com>. Thanks to Pyun YongHyeon <pyunyh@gmail.com> for
46d193ed0bSPyun YongHyeon * review and very useful comments.
47d193ed0bSPyun YongHyeon *
48d193ed0bSPyun YongHyeon * Adapted to SiS 191 NIC by Nikolay Denev with further ideas from the
49d193ed0bSPyun YongHyeon * Linux and Solaris drivers.
50d193ed0bSPyun YongHyeon */
51d193ed0bSPyun YongHyeon
52d193ed0bSPyun YongHyeon #include <sys/param.h>
53d193ed0bSPyun YongHyeon #include <sys/systm.h>
54d193ed0bSPyun YongHyeon #include <sys/bus.h>
55d193ed0bSPyun YongHyeon #include <sys/endian.h>
56d193ed0bSPyun YongHyeon #include <sys/kernel.h>
57d193ed0bSPyun YongHyeon #include <sys/lock.h>
58d193ed0bSPyun YongHyeon #include <sys/malloc.h>
59d193ed0bSPyun YongHyeon #include <sys/mbuf.h>
60d193ed0bSPyun YongHyeon #include <sys/module.h>
61d193ed0bSPyun YongHyeon #include <sys/mutex.h>
62d193ed0bSPyun YongHyeon #include <sys/rman.h>
63d193ed0bSPyun YongHyeon #include <sys/socket.h>
64d193ed0bSPyun YongHyeon #include <sys/sockio.h>
65d193ed0bSPyun YongHyeon
66d193ed0bSPyun YongHyeon #include <net/bpf.h>
67d193ed0bSPyun YongHyeon #include <net/if.h>
6876039bc8SGleb Smirnoff #include <net/if_var.h>
69d193ed0bSPyun YongHyeon #include <net/if_arp.h>
70d193ed0bSPyun YongHyeon #include <net/ethernet.h>
71d193ed0bSPyun YongHyeon #include <net/if_dl.h>
72d193ed0bSPyun YongHyeon #include <net/if_media.h>
73d193ed0bSPyun YongHyeon #include <net/if_types.h>
74d193ed0bSPyun YongHyeon #include <net/if_vlan_var.h>
75d193ed0bSPyun YongHyeon
7665329b31SPyun YongHyeon #include <netinet/in.h>
7765329b31SPyun YongHyeon #include <netinet/in_systm.h>
7865329b31SPyun YongHyeon #include <netinet/ip.h>
7965329b31SPyun YongHyeon #include <netinet/tcp.h>
8065329b31SPyun YongHyeon
81d193ed0bSPyun YongHyeon #include <machine/bus.h>
8265329b31SPyun YongHyeon #include <machine/in_cksum.h>
83d193ed0bSPyun YongHyeon
84d193ed0bSPyun YongHyeon #include <dev/mii/mii.h>
85d193ed0bSPyun YongHyeon #include <dev/mii/miivar.h>
86d193ed0bSPyun YongHyeon
87d193ed0bSPyun YongHyeon #include <dev/pci/pcireg.h>
88d193ed0bSPyun YongHyeon #include <dev/pci/pcivar.h>
89d193ed0bSPyun YongHyeon
90c6491946SPyun YongHyeon #include <dev/sge/if_sgereg.h>
91d193ed0bSPyun YongHyeon
92d193ed0bSPyun YongHyeon MODULE_DEPEND(sge, pci, 1, 1, 1);
93d193ed0bSPyun YongHyeon MODULE_DEPEND(sge, ether, 1, 1, 1);
94d193ed0bSPyun YongHyeon MODULE_DEPEND(sge, miibus, 1, 1, 1);
95d193ed0bSPyun YongHyeon
96d193ed0bSPyun YongHyeon /* "device miibus0" required. See GENERIC if you get errors here. */
97d193ed0bSPyun YongHyeon #include "miibus_if.h"
98d193ed0bSPyun YongHyeon
99d193ed0bSPyun YongHyeon /*
100d193ed0bSPyun YongHyeon * Various supported device vendors/types and their names.
101d193ed0bSPyun YongHyeon */
102d193ed0bSPyun YongHyeon static struct sge_type sge_devs[] = {
103d193ed0bSPyun YongHyeon { SIS_VENDORID, SIS_DEVICEID_190, "SiS190 Fast Ethernet" },
104d193ed0bSPyun YongHyeon { SIS_VENDORID, SIS_DEVICEID_191, "SiS191 Fast/Gigabit Ethernet" },
105d193ed0bSPyun YongHyeon { 0, 0, NULL }
106d193ed0bSPyun YongHyeon };
107d193ed0bSPyun YongHyeon
108d193ed0bSPyun YongHyeon static int sge_probe(device_t);
109d193ed0bSPyun YongHyeon static int sge_attach(device_t);
110d193ed0bSPyun YongHyeon static int sge_detach(device_t);
111d193ed0bSPyun YongHyeon static int sge_shutdown(device_t);
112d193ed0bSPyun YongHyeon static int sge_suspend(device_t);
113d193ed0bSPyun YongHyeon static int sge_resume(device_t);
114d193ed0bSPyun YongHyeon
115d193ed0bSPyun YongHyeon static int sge_miibus_readreg(device_t, int, int);
116d193ed0bSPyun YongHyeon static int sge_miibus_writereg(device_t, int, int, int);
117d193ed0bSPyun YongHyeon static void sge_miibus_statchg(device_t);
118d193ed0bSPyun YongHyeon
119d193ed0bSPyun YongHyeon static int sge_newbuf(struct sge_softc *, int);
120d193ed0bSPyun YongHyeon static int sge_encap(struct sge_softc *, struct mbuf **);
121d193ed0bSPyun YongHyeon static __inline void
122d193ed0bSPyun YongHyeon sge_discard_rxbuf(struct sge_softc *, int);
123d193ed0bSPyun YongHyeon static void sge_rxeof(struct sge_softc *);
124d193ed0bSPyun YongHyeon static void sge_txeof(struct sge_softc *);
125d193ed0bSPyun YongHyeon static void sge_intr(void *);
126d193ed0bSPyun YongHyeon static void sge_tick(void *);
127*e948d066SJustin Hibbits static void sge_start(if_t);
128*e948d066SJustin Hibbits static void sge_start_locked(if_t);
129*e948d066SJustin Hibbits static int sge_ioctl(if_t, u_long, caddr_t);
130d193ed0bSPyun YongHyeon static void sge_init(void *);
131d193ed0bSPyun YongHyeon static void sge_init_locked(struct sge_softc *);
132d193ed0bSPyun YongHyeon static void sge_stop(struct sge_softc *);
133d193ed0bSPyun YongHyeon static void sge_watchdog(struct sge_softc *);
134*e948d066SJustin Hibbits static int sge_ifmedia_upd(if_t);
135*e948d066SJustin Hibbits static void sge_ifmedia_sts(if_t, struct ifmediareq *);
136d193ed0bSPyun YongHyeon
137d193ed0bSPyun YongHyeon static int sge_get_mac_addr_apc(struct sge_softc *, uint8_t *);
138d193ed0bSPyun YongHyeon static int sge_get_mac_addr_eeprom(struct sge_softc *, uint8_t *);
139d193ed0bSPyun YongHyeon static uint16_t sge_read_eeprom(struct sge_softc *, int);
140d193ed0bSPyun YongHyeon
141d193ed0bSPyun YongHyeon static void sge_rxfilter(struct sge_softc *);
142c186cf13SPyun YongHyeon static void sge_setvlan(struct sge_softc *);
143d193ed0bSPyun YongHyeon static void sge_reset(struct sge_softc *);
144d193ed0bSPyun YongHyeon static int sge_list_rx_init(struct sge_softc *);
145d193ed0bSPyun YongHyeon static int sge_list_rx_free(struct sge_softc *);
146d193ed0bSPyun YongHyeon static int sge_list_tx_init(struct sge_softc *);
147d193ed0bSPyun YongHyeon static int sge_list_tx_free(struct sge_softc *);
148d193ed0bSPyun YongHyeon
149d193ed0bSPyun YongHyeon static int sge_dma_alloc(struct sge_softc *);
150d193ed0bSPyun YongHyeon static void sge_dma_free(struct sge_softc *);
151d193ed0bSPyun YongHyeon static void sge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
152d193ed0bSPyun YongHyeon
153d193ed0bSPyun YongHyeon static device_method_t sge_methods[] = {
154d193ed0bSPyun YongHyeon /* Device interface */
155d193ed0bSPyun YongHyeon DEVMETHOD(device_probe, sge_probe),
156d193ed0bSPyun YongHyeon DEVMETHOD(device_attach, sge_attach),
157d193ed0bSPyun YongHyeon DEVMETHOD(device_detach, sge_detach),
158d193ed0bSPyun YongHyeon DEVMETHOD(device_suspend, sge_suspend),
159d193ed0bSPyun YongHyeon DEVMETHOD(device_resume, sge_resume),
160d193ed0bSPyun YongHyeon DEVMETHOD(device_shutdown, sge_shutdown),
161d193ed0bSPyun YongHyeon
162d193ed0bSPyun YongHyeon /* MII interface */
163d193ed0bSPyun YongHyeon DEVMETHOD(miibus_readreg, sge_miibus_readreg),
164d193ed0bSPyun YongHyeon DEVMETHOD(miibus_writereg, sge_miibus_writereg),
165d193ed0bSPyun YongHyeon DEVMETHOD(miibus_statchg, sge_miibus_statchg),
166d193ed0bSPyun YongHyeon
1674b7ec270SMarius Strobl DEVMETHOD_END
168d193ed0bSPyun YongHyeon };
169d193ed0bSPyun YongHyeon
170d193ed0bSPyun YongHyeon static driver_t sge_driver = {
171d193ed0bSPyun YongHyeon "sge", sge_methods, sizeof(struct sge_softc)
172d193ed0bSPyun YongHyeon };
173d193ed0bSPyun YongHyeon
174f451bab2SJohn Baldwin DRIVER_MODULE(sge, pci, sge_driver, 0, 0);
1753e38757dSJohn Baldwin DRIVER_MODULE(miibus, sge, miibus_driver, 0, 0);
176d193ed0bSPyun YongHyeon
177d193ed0bSPyun YongHyeon /*
178d193ed0bSPyun YongHyeon * Register space access macros.
179d193ed0bSPyun YongHyeon */
180d193ed0bSPyun YongHyeon #define CSR_WRITE_4(sc, reg, val) bus_write_4(sc->sge_res, reg, val)
181d193ed0bSPyun YongHyeon #define CSR_WRITE_2(sc, reg, val) bus_write_2(sc->sge_res, reg, val)
182d193ed0bSPyun YongHyeon #define CSR_WRITE_1(cs, reg, val) bus_write_1(sc->sge_res, reg, val)
183d193ed0bSPyun YongHyeon
184d193ed0bSPyun YongHyeon #define CSR_READ_4(sc, reg) bus_read_4(sc->sge_res, reg)
185d193ed0bSPyun YongHyeon #define CSR_READ_2(sc, reg) bus_read_2(sc->sge_res, reg)
186d193ed0bSPyun YongHyeon #define CSR_READ_1(sc, reg) bus_read_1(sc->sge_res, reg)
187d193ed0bSPyun YongHyeon
188d193ed0bSPyun YongHyeon /* Define to show Tx/Rx error status. */
189d193ed0bSPyun YongHyeon #undef SGE_SHOW_ERRORS
190d193ed0bSPyun YongHyeon
191d193ed0bSPyun YongHyeon #define SGE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
192d193ed0bSPyun YongHyeon
193d193ed0bSPyun YongHyeon static void
sge_dma_map_addr(void * arg,bus_dma_segment_t * segs,int nseg,int error)194d193ed0bSPyun YongHyeon sge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
195d193ed0bSPyun YongHyeon {
196d193ed0bSPyun YongHyeon bus_addr_t *p;
197d193ed0bSPyun YongHyeon
198d193ed0bSPyun YongHyeon if (error != 0)
199d193ed0bSPyun YongHyeon return;
200d193ed0bSPyun YongHyeon KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
201d193ed0bSPyun YongHyeon p = arg;
202d193ed0bSPyun YongHyeon *p = segs->ds_addr;
203d193ed0bSPyun YongHyeon }
204d193ed0bSPyun YongHyeon
205d193ed0bSPyun YongHyeon /*
206d193ed0bSPyun YongHyeon * Read a sequence of words from the EEPROM.
207d193ed0bSPyun YongHyeon */
208d193ed0bSPyun YongHyeon static uint16_t
sge_read_eeprom(struct sge_softc * sc,int offset)209d193ed0bSPyun YongHyeon sge_read_eeprom(struct sge_softc *sc, int offset)
210d193ed0bSPyun YongHyeon {
211d193ed0bSPyun YongHyeon uint32_t val;
212d193ed0bSPyun YongHyeon int i;
213d193ed0bSPyun YongHyeon
214d193ed0bSPyun YongHyeon KASSERT(offset <= EI_OFFSET, ("EEPROM offset too big"));
215d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, ROMInterface,
216d193ed0bSPyun YongHyeon EI_REQ | EI_OP_RD | (offset << EI_OFFSET_SHIFT));
217d193ed0bSPyun YongHyeon DELAY(500);
218d193ed0bSPyun YongHyeon for (i = 0; i < SGE_TIMEOUT; i++) {
219d193ed0bSPyun YongHyeon val = CSR_READ_4(sc, ROMInterface);
220d193ed0bSPyun YongHyeon if ((val & EI_REQ) == 0)
221d193ed0bSPyun YongHyeon break;
222d193ed0bSPyun YongHyeon DELAY(100);
223d193ed0bSPyun YongHyeon }
224d193ed0bSPyun YongHyeon if (i == SGE_TIMEOUT) {
225d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
226d193ed0bSPyun YongHyeon "EEPROM read timeout : 0x%08x\n", val);
227d193ed0bSPyun YongHyeon return (0xffff);
228d193ed0bSPyun YongHyeon }
229d193ed0bSPyun YongHyeon
230d193ed0bSPyun YongHyeon return ((val & EI_DATA) >> EI_DATA_SHIFT);
231d193ed0bSPyun YongHyeon }
232d193ed0bSPyun YongHyeon
233d193ed0bSPyun YongHyeon static int
sge_get_mac_addr_eeprom(struct sge_softc * sc,uint8_t * dest)234d193ed0bSPyun YongHyeon sge_get_mac_addr_eeprom(struct sge_softc *sc, uint8_t *dest)
235d193ed0bSPyun YongHyeon {
236d193ed0bSPyun YongHyeon uint16_t val;
237d193ed0bSPyun YongHyeon int i;
238d193ed0bSPyun YongHyeon
239d193ed0bSPyun YongHyeon val = sge_read_eeprom(sc, EEPROMSignature);
240d193ed0bSPyun YongHyeon if (val == 0xffff || val == 0) {
241d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
242d193ed0bSPyun YongHyeon "invalid EEPROM signature : 0x%04x\n", val);
243d193ed0bSPyun YongHyeon return (EINVAL);
244d193ed0bSPyun YongHyeon }
245d193ed0bSPyun YongHyeon
246d193ed0bSPyun YongHyeon for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
247d193ed0bSPyun YongHyeon val = sge_read_eeprom(sc, EEPROMMACAddr + i / 2);
248d193ed0bSPyun YongHyeon dest[i + 0] = (uint8_t)val;
249d193ed0bSPyun YongHyeon dest[i + 1] = (uint8_t)(val >> 8);
250d193ed0bSPyun YongHyeon }
251d193ed0bSPyun YongHyeon
252d193ed0bSPyun YongHyeon if ((sge_read_eeprom(sc, EEPROMInfo) & 0x80) != 0)
253d193ed0bSPyun YongHyeon sc->sge_flags |= SGE_FLAG_RGMII;
254d193ed0bSPyun YongHyeon return (0);
255d193ed0bSPyun YongHyeon }
256d193ed0bSPyun YongHyeon
257d193ed0bSPyun YongHyeon /*
258d193ed0bSPyun YongHyeon * For SiS96x, APC CMOS RAM is used to store ethernet address.
259d193ed0bSPyun YongHyeon * APC CMOS RAM is accessed through ISA bridge.
260d193ed0bSPyun YongHyeon */
261d193ed0bSPyun YongHyeon static int
sge_get_mac_addr_apc(struct sge_softc * sc,uint8_t * dest)262d193ed0bSPyun YongHyeon sge_get_mac_addr_apc(struct sge_softc *sc, uint8_t *dest)
263d193ed0bSPyun YongHyeon {
264d193ed0bSPyun YongHyeon #if defined(__amd64__) || defined(__i386__)
265d193ed0bSPyun YongHyeon devclass_t pci;
266d193ed0bSPyun YongHyeon device_t bus, dev = NULL;
267d193ed0bSPyun YongHyeon device_t *kids;
268d193ed0bSPyun YongHyeon struct apc_tbl {
269d193ed0bSPyun YongHyeon uint16_t vid;
270d193ed0bSPyun YongHyeon uint16_t did;
271d193ed0bSPyun YongHyeon } *tp, apc_tbls[] = {
272d193ed0bSPyun YongHyeon { SIS_VENDORID, 0x0965 },
273d193ed0bSPyun YongHyeon { SIS_VENDORID, 0x0966 },
274d193ed0bSPyun YongHyeon { SIS_VENDORID, 0x0968 }
275d193ed0bSPyun YongHyeon };
276d193ed0bSPyun YongHyeon uint8_t reg;
2778dfea464SPedro F. Giffuni int busnum, i, j, numkids;
278d193ed0bSPyun YongHyeon
279d193ed0bSPyun YongHyeon pci = devclass_find("pci");
280d193ed0bSPyun YongHyeon for (busnum = 0; busnum < devclass_get_maxunit(pci); busnum++) {
281d193ed0bSPyun YongHyeon bus = devclass_get_device(pci, busnum);
282d193ed0bSPyun YongHyeon if (!bus)
283d193ed0bSPyun YongHyeon continue;
284d193ed0bSPyun YongHyeon if (device_get_children(bus, &kids, &numkids) != 0)
285d193ed0bSPyun YongHyeon continue;
286d193ed0bSPyun YongHyeon for (i = 0; i < numkids; i++) {
287d193ed0bSPyun YongHyeon dev = kids[i];
288d193ed0bSPyun YongHyeon if (pci_get_class(dev) == PCIC_BRIDGE &&
289d193ed0bSPyun YongHyeon pci_get_subclass(dev) == PCIS_BRIDGE_ISA) {
290d193ed0bSPyun YongHyeon tp = apc_tbls;
2918dfea464SPedro F. Giffuni for (j = 0; j < nitems(apc_tbls); j++) {
292d193ed0bSPyun YongHyeon if (pci_get_vendor(dev) == tp->vid &&
293d193ed0bSPyun YongHyeon pci_get_device(dev) == tp->did) {
294d193ed0bSPyun YongHyeon free(kids, M_TEMP);
295d193ed0bSPyun YongHyeon goto apc_found;
296d193ed0bSPyun YongHyeon }
297d193ed0bSPyun YongHyeon tp++;
298d193ed0bSPyun YongHyeon }
299d193ed0bSPyun YongHyeon }
300d193ed0bSPyun YongHyeon }
301d193ed0bSPyun YongHyeon free(kids, M_TEMP);
302d193ed0bSPyun YongHyeon }
303d193ed0bSPyun YongHyeon device_printf(sc->sge_dev, "couldn't find PCI-ISA bridge\n");
304d193ed0bSPyun YongHyeon return (EINVAL);
305d193ed0bSPyun YongHyeon apc_found:
306d193ed0bSPyun YongHyeon /* Enable port 0x78 and 0x79 to access APC registers. */
307d193ed0bSPyun YongHyeon reg = pci_read_config(dev, 0x48, 1);
308d193ed0bSPyun YongHyeon pci_write_config(dev, 0x48, reg & ~0x02, 1);
309d193ed0bSPyun YongHyeon DELAY(50);
310d193ed0bSPyun YongHyeon pci_read_config(dev, 0x48, 1);
311d193ed0bSPyun YongHyeon /* Read stored ethernet address. */
312d193ed0bSPyun YongHyeon for (i = 0; i < ETHER_ADDR_LEN; i++) {
313d193ed0bSPyun YongHyeon outb(0x78, 0x09 + i);
314d193ed0bSPyun YongHyeon dest[i] = inb(0x79);
315d193ed0bSPyun YongHyeon }
316d193ed0bSPyun YongHyeon outb(0x78, 0x12);
317d193ed0bSPyun YongHyeon if ((inb(0x79) & 0x80) != 0)
318d193ed0bSPyun YongHyeon sc->sge_flags |= SGE_FLAG_RGMII;
319d193ed0bSPyun YongHyeon /* Restore access to APC registers. */
320d193ed0bSPyun YongHyeon pci_write_config(dev, 0x48, reg, 1);
321d193ed0bSPyun YongHyeon
322d193ed0bSPyun YongHyeon return (0);
323d193ed0bSPyun YongHyeon #else
324d193ed0bSPyun YongHyeon return (EINVAL);
325d193ed0bSPyun YongHyeon #endif
326d193ed0bSPyun YongHyeon }
327d193ed0bSPyun YongHyeon
328d193ed0bSPyun YongHyeon static int
sge_miibus_readreg(device_t dev,int phy,int reg)329d193ed0bSPyun YongHyeon sge_miibus_readreg(device_t dev, int phy, int reg)
330d193ed0bSPyun YongHyeon {
331d193ed0bSPyun YongHyeon struct sge_softc *sc;
332d193ed0bSPyun YongHyeon uint32_t val;
333d193ed0bSPyun YongHyeon int i;
334d193ed0bSPyun YongHyeon
335d193ed0bSPyun YongHyeon sc = device_get_softc(dev);
336d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, GMIIControl, (phy << GMI_PHY_SHIFT) |
337d193ed0bSPyun YongHyeon (reg << GMI_REG_SHIFT) | GMI_OP_RD | GMI_REQ);
338d193ed0bSPyun YongHyeon DELAY(10);
339d193ed0bSPyun YongHyeon for (i = 0; i < SGE_TIMEOUT; i++) {
340d193ed0bSPyun YongHyeon val = CSR_READ_4(sc, GMIIControl);
341d193ed0bSPyun YongHyeon if ((val & GMI_REQ) == 0)
342d193ed0bSPyun YongHyeon break;
343d193ed0bSPyun YongHyeon DELAY(10);
344d193ed0bSPyun YongHyeon }
345d193ed0bSPyun YongHyeon if (i == SGE_TIMEOUT) {
346d193ed0bSPyun YongHyeon device_printf(sc->sge_dev, "PHY read timeout : %d\n", reg);
347d193ed0bSPyun YongHyeon return (0);
348d193ed0bSPyun YongHyeon }
349d193ed0bSPyun YongHyeon return ((val & GMI_DATA) >> GMI_DATA_SHIFT);
350d193ed0bSPyun YongHyeon }
351d193ed0bSPyun YongHyeon
352d193ed0bSPyun YongHyeon static int
sge_miibus_writereg(device_t dev,int phy,int reg,int data)353d193ed0bSPyun YongHyeon sge_miibus_writereg(device_t dev, int phy, int reg, int data)
354d193ed0bSPyun YongHyeon {
355d193ed0bSPyun YongHyeon struct sge_softc *sc;
356d193ed0bSPyun YongHyeon uint32_t val;
357d193ed0bSPyun YongHyeon int i;
358d193ed0bSPyun YongHyeon
359d193ed0bSPyun YongHyeon sc = device_get_softc(dev);
360d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, GMIIControl, (phy << GMI_PHY_SHIFT) |
361d193ed0bSPyun YongHyeon (reg << GMI_REG_SHIFT) | (data << GMI_DATA_SHIFT) |
362d193ed0bSPyun YongHyeon GMI_OP_WR | GMI_REQ);
363d193ed0bSPyun YongHyeon DELAY(10);
364d193ed0bSPyun YongHyeon for (i = 0; i < SGE_TIMEOUT; i++) {
365d193ed0bSPyun YongHyeon val = CSR_READ_4(sc, GMIIControl);
366d193ed0bSPyun YongHyeon if ((val & GMI_REQ) == 0)
367d193ed0bSPyun YongHyeon break;
368d193ed0bSPyun YongHyeon DELAY(10);
369d193ed0bSPyun YongHyeon }
370d193ed0bSPyun YongHyeon if (i == SGE_TIMEOUT)
371d193ed0bSPyun YongHyeon device_printf(sc->sge_dev, "PHY write timeout : %d\n", reg);
372d193ed0bSPyun YongHyeon return (0);
373d193ed0bSPyun YongHyeon }
374d193ed0bSPyun YongHyeon
375d193ed0bSPyun YongHyeon static void
sge_miibus_statchg(device_t dev)376d193ed0bSPyun YongHyeon sge_miibus_statchg(device_t dev)
377d193ed0bSPyun YongHyeon {
378d193ed0bSPyun YongHyeon struct sge_softc *sc;
379d193ed0bSPyun YongHyeon struct mii_data *mii;
380*e948d066SJustin Hibbits if_t ifp;
381d193ed0bSPyun YongHyeon uint32_t ctl, speed;
382d193ed0bSPyun YongHyeon
383d193ed0bSPyun YongHyeon sc = device_get_softc(dev);
384d193ed0bSPyun YongHyeon mii = device_get_softc(sc->sge_miibus);
385d193ed0bSPyun YongHyeon ifp = sc->sge_ifp;
386d193ed0bSPyun YongHyeon if (mii == NULL || ifp == NULL ||
387*e948d066SJustin Hibbits (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
388d193ed0bSPyun YongHyeon return;
389d193ed0bSPyun YongHyeon speed = 0;
390d193ed0bSPyun YongHyeon sc->sge_flags &= ~SGE_FLAG_LINK;
391d193ed0bSPyun YongHyeon if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
392d193ed0bSPyun YongHyeon (IFM_ACTIVE | IFM_AVALID)) {
393d193ed0bSPyun YongHyeon switch (IFM_SUBTYPE(mii->mii_media_active)) {
394d193ed0bSPyun YongHyeon case IFM_10_T:
395d193ed0bSPyun YongHyeon sc->sge_flags |= SGE_FLAG_LINK;
396d193ed0bSPyun YongHyeon speed = SC_SPEED_10;
397d193ed0bSPyun YongHyeon break;
398d193ed0bSPyun YongHyeon case IFM_100_TX:
399d193ed0bSPyun YongHyeon sc->sge_flags |= SGE_FLAG_LINK;
400d193ed0bSPyun YongHyeon speed = SC_SPEED_100;
401d193ed0bSPyun YongHyeon break;
402d193ed0bSPyun YongHyeon case IFM_1000_T:
403d193ed0bSPyun YongHyeon if ((sc->sge_flags & SGE_FLAG_FASTETHER) == 0) {
404d193ed0bSPyun YongHyeon sc->sge_flags |= SGE_FLAG_LINK;
405d193ed0bSPyun YongHyeon speed = SC_SPEED_1000;
406d193ed0bSPyun YongHyeon }
407d193ed0bSPyun YongHyeon break;
408d193ed0bSPyun YongHyeon default:
409d193ed0bSPyun YongHyeon break;
410d193ed0bSPyun YongHyeon }
411d193ed0bSPyun YongHyeon }
412d193ed0bSPyun YongHyeon if ((sc->sge_flags & SGE_FLAG_LINK) == 0)
413d193ed0bSPyun YongHyeon return;
414d193ed0bSPyun YongHyeon /* Reprogram MAC to resolved speed/duplex/flow-control parameters. */
415d193ed0bSPyun YongHyeon ctl = CSR_READ_4(sc, StationControl);
416d193ed0bSPyun YongHyeon ctl &= ~(0x0f000000 | SC_FDX | SC_SPEED_MASK);
417d193ed0bSPyun YongHyeon if (speed == SC_SPEED_1000) {
418d193ed0bSPyun YongHyeon ctl |= 0x07000000;
419d193ed0bSPyun YongHyeon sc->sge_flags |= SGE_FLAG_SPEED_1000;
420d193ed0bSPyun YongHyeon } else {
421d193ed0bSPyun YongHyeon ctl |= 0x04000000;
422d193ed0bSPyun YongHyeon sc->sge_flags &= ~SGE_FLAG_SPEED_1000;
423d193ed0bSPyun YongHyeon }
424d193ed0bSPyun YongHyeon #ifdef notyet
425d193ed0bSPyun YongHyeon if ((sc->sge_flags & SGE_FLAG_GMII) != 0)
426d193ed0bSPyun YongHyeon ctl |= 0x03000000;
427d193ed0bSPyun YongHyeon #endif
428d193ed0bSPyun YongHyeon ctl |= speed;
429d193ed0bSPyun YongHyeon if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
430d193ed0bSPyun YongHyeon ctl |= SC_FDX;
431d193ed0bSPyun YongHyeon sc->sge_flags |= SGE_FLAG_FDX;
432d193ed0bSPyun YongHyeon } else
433d193ed0bSPyun YongHyeon sc->sge_flags &= ~SGE_FLAG_FDX;
434d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, StationControl, ctl);
435d193ed0bSPyun YongHyeon if ((sc->sge_flags & SGE_FLAG_RGMII) != 0) {
436d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, RGMIIDelay, 0x0441);
437d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, RGMIIDelay, 0x0440);
438d193ed0bSPyun YongHyeon }
439d193ed0bSPyun YongHyeon }
440d193ed0bSPyun YongHyeon
4419c0d6728SGleb Smirnoff static u_int
sge_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int count)4429c0d6728SGleb Smirnoff sge_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int count)
4439c0d6728SGleb Smirnoff {
4449c0d6728SGleb Smirnoff uint32_t crc, *hashes = arg;
4459c0d6728SGleb Smirnoff
4469c0d6728SGleb Smirnoff crc = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN);
4479c0d6728SGleb Smirnoff hashes[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
4489c0d6728SGleb Smirnoff
4499c0d6728SGleb Smirnoff return (1);
4509c0d6728SGleb Smirnoff }
4519c0d6728SGleb Smirnoff
452d193ed0bSPyun YongHyeon static void
sge_rxfilter(struct sge_softc * sc)453d193ed0bSPyun YongHyeon sge_rxfilter(struct sge_softc *sc)
454d193ed0bSPyun YongHyeon {
455*e948d066SJustin Hibbits if_t ifp;
4569c0d6728SGleb Smirnoff uint32_t hashes[2];
457d193ed0bSPyun YongHyeon uint16_t rxfilt;
458d193ed0bSPyun YongHyeon
459d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
460d193ed0bSPyun YongHyeon
461d193ed0bSPyun YongHyeon ifp = sc->sge_ifp;
4629c2851d2SPyun YongHyeon rxfilt = CSR_READ_2(sc, RxMacControl);
4639c2851d2SPyun YongHyeon rxfilt &= ~(AcceptBroadcast | AcceptAllPhys | AcceptMulticast);
4649c2851d2SPyun YongHyeon rxfilt |= AcceptMyPhys;
465*e948d066SJustin Hibbits if ((if_getflags(ifp) & IFF_BROADCAST) != 0)
466d193ed0bSPyun YongHyeon rxfilt |= AcceptBroadcast;
467*e948d066SJustin Hibbits if ((if_getflags(ifp) & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
468*e948d066SJustin Hibbits if ((if_getflags(ifp) & IFF_PROMISC) != 0)
469d193ed0bSPyun YongHyeon rxfilt |= AcceptAllPhys;
470d193ed0bSPyun YongHyeon rxfilt |= AcceptMulticast;
471d193ed0bSPyun YongHyeon hashes[0] = 0xFFFFFFFF;
472d193ed0bSPyun YongHyeon hashes[1] = 0xFFFFFFFF;
4739c2851d2SPyun YongHyeon } else {
474d193ed0bSPyun YongHyeon rxfilt |= AcceptMulticast;
4759c2851d2SPyun YongHyeon hashes[0] = hashes[1] = 0;
476d193ed0bSPyun YongHyeon /* Now program new ones. */
4779c0d6728SGleb Smirnoff if_foreach_llmaddr(ifp, sge_hash_maddr, hashes);
4789c2851d2SPyun YongHyeon }
47978b11406SPyun YongHyeon CSR_WRITE_2(sc, RxMacControl, rxfilt);
480d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, RxHashTable, hashes[0]);
481d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, RxHashTable2, hashes[1]);
482d193ed0bSPyun YongHyeon }
483d193ed0bSPyun YongHyeon
484d193ed0bSPyun YongHyeon static void
sge_setvlan(struct sge_softc * sc)485c186cf13SPyun YongHyeon sge_setvlan(struct sge_softc *sc)
486c186cf13SPyun YongHyeon {
487*e948d066SJustin Hibbits if_t ifp;
488c186cf13SPyun YongHyeon uint16_t rxfilt;
489c186cf13SPyun YongHyeon
490c186cf13SPyun YongHyeon SGE_LOCK_ASSERT(sc);
491c186cf13SPyun YongHyeon
492c186cf13SPyun YongHyeon ifp = sc->sge_ifp;
493*e948d066SJustin Hibbits if ((if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
494c186cf13SPyun YongHyeon return;
495c186cf13SPyun YongHyeon rxfilt = CSR_READ_2(sc, RxMacControl);
496*e948d066SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0)
497c186cf13SPyun YongHyeon rxfilt |= RXMAC_STRIP_VLAN;
498c186cf13SPyun YongHyeon else
499c186cf13SPyun YongHyeon rxfilt &= ~RXMAC_STRIP_VLAN;
500c186cf13SPyun YongHyeon CSR_WRITE_2(sc, RxMacControl, rxfilt);
501c186cf13SPyun YongHyeon }
502c186cf13SPyun YongHyeon
503c186cf13SPyun YongHyeon static void
sge_reset(struct sge_softc * sc)504d193ed0bSPyun YongHyeon sge_reset(struct sge_softc *sc)
505d193ed0bSPyun YongHyeon {
506d193ed0bSPyun YongHyeon
507d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrMask, 0);
508d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrStatus, 0xffffffff);
509d193ed0bSPyun YongHyeon
510d193ed0bSPyun YongHyeon /* Soft reset. */
511d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrControl, 0x8000);
512d193ed0bSPyun YongHyeon CSR_READ_4(sc, IntrControl);
513d193ed0bSPyun YongHyeon DELAY(100);
514d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrControl, 0);
515d193ed0bSPyun YongHyeon /* Stop MAC. */
516d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, TX_CTL, 0x1a00);
517d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, RX_CTL, 0x1a00);
518d193ed0bSPyun YongHyeon
519d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrMask, 0);
520d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrStatus, 0xffffffff);
521d193ed0bSPyun YongHyeon
522d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, GMIIControl, 0);
523d193ed0bSPyun YongHyeon }
524d193ed0bSPyun YongHyeon
525d193ed0bSPyun YongHyeon /*
526d193ed0bSPyun YongHyeon * Probe for an SiS chip. Check the PCI vendor and device
527d193ed0bSPyun YongHyeon * IDs against our list and return a device name if we find a match.
528d193ed0bSPyun YongHyeon */
529d193ed0bSPyun YongHyeon static int
sge_probe(device_t dev)530d193ed0bSPyun YongHyeon sge_probe(device_t dev)
531d193ed0bSPyun YongHyeon {
532d193ed0bSPyun YongHyeon struct sge_type *t;
533d193ed0bSPyun YongHyeon
534d193ed0bSPyun YongHyeon t = sge_devs;
535d193ed0bSPyun YongHyeon while (t->sge_name != NULL) {
536d193ed0bSPyun YongHyeon if ((pci_get_vendor(dev) == t->sge_vid) &&
537d193ed0bSPyun YongHyeon (pci_get_device(dev) == t->sge_did)) {
538d193ed0bSPyun YongHyeon device_set_desc(dev, t->sge_name);
539d193ed0bSPyun YongHyeon return (BUS_PROBE_DEFAULT);
540d193ed0bSPyun YongHyeon }
541d193ed0bSPyun YongHyeon t++;
542d193ed0bSPyun YongHyeon }
543d193ed0bSPyun YongHyeon
544d193ed0bSPyun YongHyeon return (ENXIO);
545d193ed0bSPyun YongHyeon }
546d193ed0bSPyun YongHyeon
547d193ed0bSPyun YongHyeon /*
548d193ed0bSPyun YongHyeon * Attach the interface. Allocate softc structures, do ifmedia
549d193ed0bSPyun YongHyeon * setup and ethernet/BPF attach.
550d193ed0bSPyun YongHyeon */
551d193ed0bSPyun YongHyeon static int
sge_attach(device_t dev)552d193ed0bSPyun YongHyeon sge_attach(device_t dev)
553d193ed0bSPyun YongHyeon {
554d193ed0bSPyun YongHyeon struct sge_softc *sc;
555*e948d066SJustin Hibbits if_t ifp;
556d193ed0bSPyun YongHyeon uint8_t eaddr[ETHER_ADDR_LEN];
557d193ed0bSPyun YongHyeon int error = 0, rid;
558d193ed0bSPyun YongHyeon
559d193ed0bSPyun YongHyeon sc = device_get_softc(dev);
560d193ed0bSPyun YongHyeon sc->sge_dev = dev;
561d193ed0bSPyun YongHyeon
562d193ed0bSPyun YongHyeon mtx_init(&sc->sge_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
563d193ed0bSPyun YongHyeon MTX_DEF);
564d193ed0bSPyun YongHyeon callout_init_mtx(&sc->sge_stat_ch, &sc->sge_mtx, 0);
565d193ed0bSPyun YongHyeon
566d193ed0bSPyun YongHyeon /*
567d193ed0bSPyun YongHyeon * Map control/status registers.
568d193ed0bSPyun YongHyeon */
569d193ed0bSPyun YongHyeon pci_enable_busmaster(dev);
570d193ed0bSPyun YongHyeon
571d193ed0bSPyun YongHyeon /* Allocate resources. */
572d193ed0bSPyun YongHyeon sc->sge_res_id = PCIR_BAR(0);
573d193ed0bSPyun YongHyeon sc->sge_res_type = SYS_RES_MEMORY;
574d193ed0bSPyun YongHyeon sc->sge_res = bus_alloc_resource_any(dev, sc->sge_res_type,
575d193ed0bSPyun YongHyeon &sc->sge_res_id, RF_ACTIVE);
576d193ed0bSPyun YongHyeon if (sc->sge_res == NULL) {
577d193ed0bSPyun YongHyeon device_printf(dev, "couldn't allocate resource\n");
578d193ed0bSPyun YongHyeon error = ENXIO;
579d193ed0bSPyun YongHyeon goto fail;
580d193ed0bSPyun YongHyeon }
581d193ed0bSPyun YongHyeon
582d193ed0bSPyun YongHyeon rid = 0;
583d193ed0bSPyun YongHyeon sc->sge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
584d193ed0bSPyun YongHyeon RF_SHAREABLE | RF_ACTIVE);
585d193ed0bSPyun YongHyeon if (sc->sge_irq == NULL) {
586d193ed0bSPyun YongHyeon device_printf(dev, "couldn't allocate IRQ resources\n");
587d193ed0bSPyun YongHyeon error = ENXIO;
588d193ed0bSPyun YongHyeon goto fail;
589d193ed0bSPyun YongHyeon }
590d193ed0bSPyun YongHyeon sc->sge_rev = pci_get_revid(dev);
591d193ed0bSPyun YongHyeon if (pci_get_device(dev) == SIS_DEVICEID_190)
5927f20f021SPyun YongHyeon sc->sge_flags |= SGE_FLAG_FASTETHER | SGE_FLAG_SIS190;
593d193ed0bSPyun YongHyeon /* Reset the adapter. */
594d193ed0bSPyun YongHyeon sge_reset(sc);
595d193ed0bSPyun YongHyeon
596d193ed0bSPyun YongHyeon /* Get MAC address from the EEPROM. */
597d193ed0bSPyun YongHyeon if ((pci_read_config(dev, 0x73, 1) & 0x01) != 0)
598d193ed0bSPyun YongHyeon sge_get_mac_addr_apc(sc, eaddr);
599d193ed0bSPyun YongHyeon else
600d193ed0bSPyun YongHyeon sge_get_mac_addr_eeprom(sc, eaddr);
601d193ed0bSPyun YongHyeon
602d193ed0bSPyun YongHyeon if ((error = sge_dma_alloc(sc)) != 0)
603d193ed0bSPyun YongHyeon goto fail;
604d193ed0bSPyun YongHyeon
605d193ed0bSPyun YongHyeon ifp = sc->sge_ifp = if_alloc(IFT_ETHER);
606*e948d066SJustin Hibbits if_setsoftc(ifp, sc);
607d193ed0bSPyun YongHyeon if_initname(ifp, device_get_name(dev), device_get_unit(dev));
608*e948d066SJustin Hibbits if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
609*e948d066SJustin Hibbits if_setioctlfn(ifp, sge_ioctl);
610*e948d066SJustin Hibbits if_setstartfn(ifp, sge_start);
611*e948d066SJustin Hibbits if_setinitfn(ifp, sge_init);
612*e948d066SJustin Hibbits if_setsendqlen(ifp, SGE_TX_RING_CNT - 1);
613*e948d066SJustin Hibbits if_setsendqready(ifp);
614*e948d066SJustin Hibbits if_setcapabilities(ifp, IFCAP_TXCSUM | IFCAP_RXCSUM | IFCAP_TSO4);
615*e948d066SJustin Hibbits if_sethwassist(ifp, SGE_CSUM_FEATURES | CSUM_TSO);
616*e948d066SJustin Hibbits if_setcapenable(ifp, if_getcapabilities(ifp));
617d193ed0bSPyun YongHyeon /*
618d193ed0bSPyun YongHyeon * Do MII setup.
619d193ed0bSPyun YongHyeon */
620d6c65d27SMarius Strobl error = mii_attach(dev, &sc->sge_miibus, ifp, sge_ifmedia_upd,
621d6c65d27SMarius Strobl sge_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
622d6c65d27SMarius Strobl if (error != 0) {
623d6c65d27SMarius Strobl device_printf(dev, "attaching PHYs failed\n");
624d193ed0bSPyun YongHyeon goto fail;
625d193ed0bSPyun YongHyeon }
626d193ed0bSPyun YongHyeon
627d193ed0bSPyun YongHyeon /*
628d193ed0bSPyun YongHyeon * Call MI attach routine.
629d193ed0bSPyun YongHyeon */
630d193ed0bSPyun YongHyeon ether_ifattach(ifp, eaddr);
631d193ed0bSPyun YongHyeon
632d193ed0bSPyun YongHyeon /* VLAN setup. */
633*e948d066SJustin Hibbits if_setcapabilities(ifp, IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM |
634*e948d066SJustin Hibbits IFCAP_VLAN_HWTSO | IFCAP_VLAN_MTU);
635*e948d066SJustin Hibbits if_setcapenable(ifp, if_getcapabilities(ifp));
636d193ed0bSPyun YongHyeon /* Tell the upper layer(s) we support long frames. */
637*e948d066SJustin Hibbits if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
638d193ed0bSPyun YongHyeon
639d193ed0bSPyun YongHyeon /* Hook interrupt last to avoid having to lock softc */
640d193ed0bSPyun YongHyeon error = bus_setup_intr(dev, sc->sge_irq, INTR_TYPE_NET | INTR_MPSAFE,
641d193ed0bSPyun YongHyeon NULL, sge_intr, sc, &sc->sge_intrhand);
642d193ed0bSPyun YongHyeon if (error) {
643d193ed0bSPyun YongHyeon device_printf(dev, "couldn't set up irq\n");
644d193ed0bSPyun YongHyeon ether_ifdetach(ifp);
645d193ed0bSPyun YongHyeon goto fail;
646d193ed0bSPyun YongHyeon }
647d193ed0bSPyun YongHyeon
648d193ed0bSPyun YongHyeon fail:
649d193ed0bSPyun YongHyeon if (error)
650d193ed0bSPyun YongHyeon sge_detach(dev);
651d193ed0bSPyun YongHyeon
652d193ed0bSPyun YongHyeon return (error);
653d193ed0bSPyun YongHyeon }
654d193ed0bSPyun YongHyeon
655d193ed0bSPyun YongHyeon /*
656d193ed0bSPyun YongHyeon * Shutdown hardware and free up resources. This can be called any
657d193ed0bSPyun YongHyeon * time after the mutex has been initialized. It is called in both
658d193ed0bSPyun YongHyeon * the error case in attach and the normal detach case so it needs
659d193ed0bSPyun YongHyeon * to be careful about only freeing resources that have actually been
660d193ed0bSPyun YongHyeon * allocated.
661d193ed0bSPyun YongHyeon */
662d193ed0bSPyun YongHyeon static int
sge_detach(device_t dev)663d193ed0bSPyun YongHyeon sge_detach(device_t dev)
664d193ed0bSPyun YongHyeon {
665d193ed0bSPyun YongHyeon struct sge_softc *sc;
666*e948d066SJustin Hibbits if_t ifp;
667d193ed0bSPyun YongHyeon
668d193ed0bSPyun YongHyeon sc = device_get_softc(dev);
669d193ed0bSPyun YongHyeon ifp = sc->sge_ifp;
670d193ed0bSPyun YongHyeon /* These should only be active if attach succeeded. */
671d193ed0bSPyun YongHyeon if (device_is_attached(dev)) {
672d193ed0bSPyun YongHyeon ether_ifdetach(ifp);
673d193ed0bSPyun YongHyeon SGE_LOCK(sc);
674d193ed0bSPyun YongHyeon sge_stop(sc);
675d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
676d193ed0bSPyun YongHyeon callout_drain(&sc->sge_stat_ch);
677d193ed0bSPyun YongHyeon }
678d193ed0bSPyun YongHyeon bus_generic_detach(dev);
679d193ed0bSPyun YongHyeon
680d193ed0bSPyun YongHyeon if (sc->sge_intrhand)
681d193ed0bSPyun YongHyeon bus_teardown_intr(dev, sc->sge_irq, sc->sge_intrhand);
682d193ed0bSPyun YongHyeon if (sc->sge_irq)
683d193ed0bSPyun YongHyeon bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sge_irq);
684d193ed0bSPyun YongHyeon if (sc->sge_res)
685d193ed0bSPyun YongHyeon bus_release_resource(dev, sc->sge_res_type, sc->sge_res_id,
686d193ed0bSPyun YongHyeon sc->sge_res);
687d193ed0bSPyun YongHyeon if (ifp)
688d193ed0bSPyun YongHyeon if_free(ifp);
689d193ed0bSPyun YongHyeon sge_dma_free(sc);
690d193ed0bSPyun YongHyeon mtx_destroy(&sc->sge_mtx);
691d193ed0bSPyun YongHyeon
692d193ed0bSPyun YongHyeon return (0);
693d193ed0bSPyun YongHyeon }
694d193ed0bSPyun YongHyeon
695d193ed0bSPyun YongHyeon /*
696d193ed0bSPyun YongHyeon * Stop all chip I/O so that the kernel's probe routines don't
697d193ed0bSPyun YongHyeon * get confused by errant DMAs when rebooting.
698d193ed0bSPyun YongHyeon */
699d193ed0bSPyun YongHyeon static int
sge_shutdown(device_t dev)700d193ed0bSPyun YongHyeon sge_shutdown(device_t dev)
701d193ed0bSPyun YongHyeon {
702d193ed0bSPyun YongHyeon struct sge_softc *sc;
703d193ed0bSPyun YongHyeon
704d193ed0bSPyun YongHyeon sc = device_get_softc(dev);
705d193ed0bSPyun YongHyeon SGE_LOCK(sc);
706d193ed0bSPyun YongHyeon sge_stop(sc);
707d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
708d193ed0bSPyun YongHyeon return (0);
709d193ed0bSPyun YongHyeon }
710d193ed0bSPyun YongHyeon
711d193ed0bSPyun YongHyeon static int
sge_suspend(device_t dev)712d193ed0bSPyun YongHyeon sge_suspend(device_t dev)
713d193ed0bSPyun YongHyeon {
714d193ed0bSPyun YongHyeon struct sge_softc *sc;
715*e948d066SJustin Hibbits if_t ifp;
716d193ed0bSPyun YongHyeon
717d193ed0bSPyun YongHyeon sc = device_get_softc(dev);
718d193ed0bSPyun YongHyeon SGE_LOCK(sc);
719d193ed0bSPyun YongHyeon ifp = sc->sge_ifp;
720*e948d066SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
721d193ed0bSPyun YongHyeon sge_stop(sc);
722d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
723d193ed0bSPyun YongHyeon return (0);
724d193ed0bSPyun YongHyeon }
725d193ed0bSPyun YongHyeon
726d193ed0bSPyun YongHyeon static int
sge_resume(device_t dev)727d193ed0bSPyun YongHyeon sge_resume(device_t dev)
728d193ed0bSPyun YongHyeon {
729d193ed0bSPyun YongHyeon struct sge_softc *sc;
730*e948d066SJustin Hibbits if_t ifp;
731d193ed0bSPyun YongHyeon
732d193ed0bSPyun YongHyeon sc = device_get_softc(dev);
733d193ed0bSPyun YongHyeon SGE_LOCK(sc);
734d193ed0bSPyun YongHyeon ifp = sc->sge_ifp;
735*e948d066SJustin Hibbits if ((if_getflags(ifp) & IFF_UP) != 0)
736d193ed0bSPyun YongHyeon sge_init_locked(sc);
737d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
738d193ed0bSPyun YongHyeon return (0);
739d193ed0bSPyun YongHyeon }
740d193ed0bSPyun YongHyeon
741d193ed0bSPyun YongHyeon static int
sge_dma_alloc(struct sge_softc * sc)742d193ed0bSPyun YongHyeon sge_dma_alloc(struct sge_softc *sc)
743d193ed0bSPyun YongHyeon {
744d193ed0bSPyun YongHyeon struct sge_chain_data *cd;
745d193ed0bSPyun YongHyeon struct sge_list_data *ld;
74655c978baSPyun YongHyeon struct sge_rxdesc *rxd;
74755c978baSPyun YongHyeon struct sge_txdesc *txd;
748d193ed0bSPyun YongHyeon int error, i;
749d193ed0bSPyun YongHyeon
750d193ed0bSPyun YongHyeon cd = &sc->sge_cdata;
751d193ed0bSPyun YongHyeon ld = &sc->sge_ldata;
752d193ed0bSPyun YongHyeon error = bus_dma_tag_create(bus_get_dma_tag(sc->sge_dev),
753d193ed0bSPyun YongHyeon 1, 0, /* alignment, boundary */
754d193ed0bSPyun YongHyeon BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
755d193ed0bSPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
756d193ed0bSPyun YongHyeon NULL, NULL, /* filter, filterarg */
757d193ed0bSPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
758d193ed0bSPyun YongHyeon 1, /* nsegments */
759d193ed0bSPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
760d193ed0bSPyun YongHyeon 0, /* flags */
761d193ed0bSPyun YongHyeon NULL, /* lockfunc */
762d193ed0bSPyun YongHyeon NULL, /* lockarg */
763d193ed0bSPyun YongHyeon &cd->sge_tag);
764d193ed0bSPyun YongHyeon if (error != 0) {
765d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
766d193ed0bSPyun YongHyeon "could not create parent DMA tag.\n");
767d193ed0bSPyun YongHyeon goto fail;
768d193ed0bSPyun YongHyeon }
769d193ed0bSPyun YongHyeon
770d193ed0bSPyun YongHyeon /* RX descriptor ring */
771d193ed0bSPyun YongHyeon error = bus_dma_tag_create(cd->sge_tag,
772d193ed0bSPyun YongHyeon SGE_DESC_ALIGN, 0, /* alignment, boundary */
773d193ed0bSPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */
774d193ed0bSPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
775d193ed0bSPyun YongHyeon NULL, NULL, /* filter, filterarg */
776d193ed0bSPyun YongHyeon SGE_RX_RING_SZ, 1, /* maxsize,nsegments */
777d193ed0bSPyun YongHyeon SGE_RX_RING_SZ, /* maxsegsize */
778d193ed0bSPyun YongHyeon 0, /* flags */
779d193ed0bSPyun YongHyeon NULL, /* lockfunc */
780d193ed0bSPyun YongHyeon NULL, /* lockarg */
781d193ed0bSPyun YongHyeon &cd->sge_rx_tag);
782d193ed0bSPyun YongHyeon if (error != 0) {
783d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
784d193ed0bSPyun YongHyeon "could not create Rx ring DMA tag.\n");
785d193ed0bSPyun YongHyeon goto fail;
786d193ed0bSPyun YongHyeon }
787d193ed0bSPyun YongHyeon /* Allocate DMA'able memory and load DMA map for RX ring. */
788d193ed0bSPyun YongHyeon error = bus_dmamem_alloc(cd->sge_rx_tag, (void **)&ld->sge_rx_ring,
789d193ed0bSPyun YongHyeon BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT,
790d193ed0bSPyun YongHyeon &cd->sge_rx_dmamap);
791d193ed0bSPyun YongHyeon if (error != 0) {
792d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
793d193ed0bSPyun YongHyeon "could not allocate DMA'able memory for Rx ring.\n");
794d193ed0bSPyun YongHyeon goto fail;
795d193ed0bSPyun YongHyeon }
796d193ed0bSPyun YongHyeon error = bus_dmamap_load(cd->sge_rx_tag, cd->sge_rx_dmamap,
797d193ed0bSPyun YongHyeon ld->sge_rx_ring, SGE_RX_RING_SZ, sge_dma_map_addr,
798d193ed0bSPyun YongHyeon &ld->sge_rx_paddr, BUS_DMA_NOWAIT);
799d193ed0bSPyun YongHyeon if (error != 0) {
800d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
801d193ed0bSPyun YongHyeon "could not load DMA'able memory for Rx ring.\n");
802d193ed0bSPyun YongHyeon }
803d193ed0bSPyun YongHyeon
804d193ed0bSPyun YongHyeon /* TX descriptor ring */
805d193ed0bSPyun YongHyeon error = bus_dma_tag_create(cd->sge_tag,
806d193ed0bSPyun YongHyeon SGE_DESC_ALIGN, 0, /* alignment, boundary */
807d193ed0bSPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */
808d193ed0bSPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
809d193ed0bSPyun YongHyeon NULL, NULL, /* filter, filterarg */
810d193ed0bSPyun YongHyeon SGE_TX_RING_SZ, 1, /* maxsize,nsegments */
811d193ed0bSPyun YongHyeon SGE_TX_RING_SZ, /* maxsegsize */
812d193ed0bSPyun YongHyeon 0, /* flags */
813d193ed0bSPyun YongHyeon NULL, /* lockfunc */
814d193ed0bSPyun YongHyeon NULL, /* lockarg */
815d193ed0bSPyun YongHyeon &cd->sge_tx_tag);
816d193ed0bSPyun YongHyeon if (error != 0) {
817d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
818d193ed0bSPyun YongHyeon "could not create Rx ring DMA tag.\n");
819d193ed0bSPyun YongHyeon goto fail;
820d193ed0bSPyun YongHyeon }
821d193ed0bSPyun YongHyeon /* Allocate DMA'able memory and load DMA map for TX ring. */
822d193ed0bSPyun YongHyeon error = bus_dmamem_alloc(cd->sge_tx_tag, (void **)&ld->sge_tx_ring,
823d193ed0bSPyun YongHyeon BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT,
824d193ed0bSPyun YongHyeon &cd->sge_tx_dmamap);
825d193ed0bSPyun YongHyeon if (error != 0) {
826d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
827d193ed0bSPyun YongHyeon "could not allocate DMA'able memory for Tx ring.\n");
828d193ed0bSPyun YongHyeon goto fail;
829d193ed0bSPyun YongHyeon }
830d193ed0bSPyun YongHyeon error = bus_dmamap_load(cd->sge_tx_tag, cd->sge_tx_dmamap,
831d193ed0bSPyun YongHyeon ld->sge_tx_ring, SGE_TX_RING_SZ, sge_dma_map_addr,
832d193ed0bSPyun YongHyeon &ld->sge_tx_paddr, BUS_DMA_NOWAIT);
833d193ed0bSPyun YongHyeon if (error != 0) {
834d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
835d193ed0bSPyun YongHyeon "could not load DMA'able memory for Rx ring.\n");
836d193ed0bSPyun YongHyeon goto fail;
837d193ed0bSPyun YongHyeon }
838d193ed0bSPyun YongHyeon
839d193ed0bSPyun YongHyeon /* Create DMA tag for Tx buffers. */
840d193ed0bSPyun YongHyeon error = bus_dma_tag_create(cd->sge_tag, 1, 0, BUS_SPACE_MAXADDR,
84165329b31SPyun YongHyeon BUS_SPACE_MAXADDR, NULL, NULL, SGE_TSO_MAXSIZE, SGE_MAXTXSEGS,
84265329b31SPyun YongHyeon SGE_TSO_MAXSEGSIZE, 0, NULL, NULL, &cd->sge_txmbuf_tag);
843d193ed0bSPyun YongHyeon if (error != 0) {
844d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
845d193ed0bSPyun YongHyeon "could not create Tx mbuf DMA tag.\n");
846d193ed0bSPyun YongHyeon goto fail;
847d193ed0bSPyun YongHyeon }
848d193ed0bSPyun YongHyeon
849d193ed0bSPyun YongHyeon /* Create DMA tag for Rx buffers. */
850d193ed0bSPyun YongHyeon error = bus_dma_tag_create(cd->sge_tag, SGE_RX_BUF_ALIGN, 0,
851d193ed0bSPyun YongHyeon BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1,
852d193ed0bSPyun YongHyeon MCLBYTES, 0, NULL, NULL, &cd->sge_rxmbuf_tag);
853d193ed0bSPyun YongHyeon if (error != 0) {
854d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
855d193ed0bSPyun YongHyeon "could not create Rx mbuf DMA tag.\n");
856d193ed0bSPyun YongHyeon goto fail;
857d193ed0bSPyun YongHyeon }
858d193ed0bSPyun YongHyeon
859d193ed0bSPyun YongHyeon /* Create DMA maps for Tx buffers. */
860d193ed0bSPyun YongHyeon for (i = 0; i < SGE_TX_RING_CNT; i++) {
86155c978baSPyun YongHyeon txd = &cd->sge_txdesc[i];
86255c978baSPyun YongHyeon txd->tx_m = NULL;
86355c978baSPyun YongHyeon txd->tx_dmamap = NULL;
86455c978baSPyun YongHyeon txd->tx_ndesc = 0;
865d193ed0bSPyun YongHyeon error = bus_dmamap_create(cd->sge_txmbuf_tag, 0,
86655c978baSPyun YongHyeon &txd->tx_dmamap);
867d193ed0bSPyun YongHyeon if (error != 0) {
868d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
869d193ed0bSPyun YongHyeon "could not create Tx DMA map.\n");
870d193ed0bSPyun YongHyeon goto fail;
871d193ed0bSPyun YongHyeon }
872d193ed0bSPyun YongHyeon }
873d193ed0bSPyun YongHyeon /* Create spare DMA map for Rx buffer. */
874d193ed0bSPyun YongHyeon error = bus_dmamap_create(cd->sge_rxmbuf_tag, 0, &cd->sge_rx_spare_map);
875d193ed0bSPyun YongHyeon if (error != 0) {
876d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
877d193ed0bSPyun YongHyeon "could not create spare Rx DMA map.\n");
878d193ed0bSPyun YongHyeon goto fail;
879d193ed0bSPyun YongHyeon }
880d193ed0bSPyun YongHyeon /* Create DMA maps for Rx buffers. */
881d193ed0bSPyun YongHyeon for (i = 0; i < SGE_RX_RING_CNT; i++) {
88255c978baSPyun YongHyeon rxd = &cd->sge_rxdesc[i];
88355c978baSPyun YongHyeon rxd->rx_m = NULL;
88455c978baSPyun YongHyeon rxd->rx_dmamap = NULL;
885d193ed0bSPyun YongHyeon error = bus_dmamap_create(cd->sge_rxmbuf_tag, 0,
88655c978baSPyun YongHyeon &rxd->rx_dmamap);
887d193ed0bSPyun YongHyeon if (error) {
888d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
889d193ed0bSPyun YongHyeon "could not create Rx DMA map.\n");
890d193ed0bSPyun YongHyeon goto fail;
891d193ed0bSPyun YongHyeon }
892d193ed0bSPyun YongHyeon }
893d193ed0bSPyun YongHyeon fail:
894d193ed0bSPyun YongHyeon return (error);
895d193ed0bSPyun YongHyeon }
896d193ed0bSPyun YongHyeon
897d193ed0bSPyun YongHyeon static void
sge_dma_free(struct sge_softc * sc)898d193ed0bSPyun YongHyeon sge_dma_free(struct sge_softc *sc)
899d193ed0bSPyun YongHyeon {
900d193ed0bSPyun YongHyeon struct sge_chain_data *cd;
901d193ed0bSPyun YongHyeon struct sge_list_data *ld;
90255c978baSPyun YongHyeon struct sge_rxdesc *rxd;
90355c978baSPyun YongHyeon struct sge_txdesc *txd;
904d193ed0bSPyun YongHyeon int i;
905d193ed0bSPyun YongHyeon
906d193ed0bSPyun YongHyeon cd = &sc->sge_cdata;
907d193ed0bSPyun YongHyeon ld = &sc->sge_ldata;
908d193ed0bSPyun YongHyeon /* Rx ring. */
909d193ed0bSPyun YongHyeon if (cd->sge_rx_tag != NULL) {
910068d8643SJohn Baldwin if (ld->sge_rx_paddr != 0)
911d193ed0bSPyun YongHyeon bus_dmamap_unload(cd->sge_rx_tag, cd->sge_rx_dmamap);
912068d8643SJohn Baldwin if (ld->sge_rx_ring != NULL)
913d193ed0bSPyun YongHyeon bus_dmamem_free(cd->sge_rx_tag, ld->sge_rx_ring,
914d193ed0bSPyun YongHyeon cd->sge_rx_dmamap);
915d193ed0bSPyun YongHyeon ld->sge_rx_ring = NULL;
916068d8643SJohn Baldwin ld->sge_rx_paddr = 0;
917d193ed0bSPyun YongHyeon bus_dma_tag_destroy(cd->sge_rx_tag);
918d193ed0bSPyun YongHyeon cd->sge_rx_tag = NULL;
919d193ed0bSPyun YongHyeon }
920d193ed0bSPyun YongHyeon /* Tx ring. */
921d193ed0bSPyun YongHyeon if (cd->sge_tx_tag != NULL) {
922068d8643SJohn Baldwin if (ld->sge_tx_paddr != 0)
923d193ed0bSPyun YongHyeon bus_dmamap_unload(cd->sge_tx_tag, cd->sge_tx_dmamap);
924068d8643SJohn Baldwin if (ld->sge_tx_ring != NULL)
925d193ed0bSPyun YongHyeon bus_dmamem_free(cd->sge_tx_tag, ld->sge_tx_ring,
926d193ed0bSPyun YongHyeon cd->sge_tx_dmamap);
927d193ed0bSPyun YongHyeon ld->sge_tx_ring = NULL;
928068d8643SJohn Baldwin ld->sge_tx_paddr = 0;
929d193ed0bSPyun YongHyeon bus_dma_tag_destroy(cd->sge_tx_tag);
930d193ed0bSPyun YongHyeon cd->sge_tx_tag = NULL;
931d193ed0bSPyun YongHyeon }
932d193ed0bSPyun YongHyeon /* Rx buffers. */
933d193ed0bSPyun YongHyeon if (cd->sge_rxmbuf_tag != NULL) {
934d193ed0bSPyun YongHyeon for (i = 0; i < SGE_RX_RING_CNT; i++) {
93555c978baSPyun YongHyeon rxd = &cd->sge_rxdesc[i];
93655c978baSPyun YongHyeon if (rxd->rx_dmamap != NULL) {
937d193ed0bSPyun YongHyeon bus_dmamap_destroy(cd->sge_rxmbuf_tag,
93855c978baSPyun YongHyeon rxd->rx_dmamap);
93955c978baSPyun YongHyeon rxd->rx_dmamap = NULL;
940d193ed0bSPyun YongHyeon }
941d193ed0bSPyun YongHyeon }
942d193ed0bSPyun YongHyeon if (cd->sge_rx_spare_map != NULL) {
943d193ed0bSPyun YongHyeon bus_dmamap_destroy(cd->sge_rxmbuf_tag,
944d193ed0bSPyun YongHyeon cd->sge_rx_spare_map);
945d193ed0bSPyun YongHyeon cd->sge_rx_spare_map = NULL;
946d193ed0bSPyun YongHyeon }
947d193ed0bSPyun YongHyeon bus_dma_tag_destroy(cd->sge_rxmbuf_tag);
948d193ed0bSPyun YongHyeon cd->sge_rxmbuf_tag = NULL;
949d193ed0bSPyun YongHyeon }
950d193ed0bSPyun YongHyeon /* Tx buffers. */
951d193ed0bSPyun YongHyeon if (cd->sge_txmbuf_tag != NULL) {
952d193ed0bSPyun YongHyeon for (i = 0; i < SGE_TX_RING_CNT; i++) {
95355c978baSPyun YongHyeon txd = &cd->sge_txdesc[i];
95455c978baSPyun YongHyeon if (txd->tx_dmamap != NULL) {
955d193ed0bSPyun YongHyeon bus_dmamap_destroy(cd->sge_txmbuf_tag,
95655c978baSPyun YongHyeon txd->tx_dmamap);
95755c978baSPyun YongHyeon txd->tx_dmamap = NULL;
958d193ed0bSPyun YongHyeon }
959d193ed0bSPyun YongHyeon }
960d193ed0bSPyun YongHyeon bus_dma_tag_destroy(cd->sge_txmbuf_tag);
961d193ed0bSPyun YongHyeon cd->sge_txmbuf_tag = NULL;
962d193ed0bSPyun YongHyeon }
963d193ed0bSPyun YongHyeon if (cd->sge_tag != NULL)
964d193ed0bSPyun YongHyeon bus_dma_tag_destroy(cd->sge_tag);
965d193ed0bSPyun YongHyeon cd->sge_tag = NULL;
966d193ed0bSPyun YongHyeon }
967d193ed0bSPyun YongHyeon
968d193ed0bSPyun YongHyeon /*
969d193ed0bSPyun YongHyeon * Initialize the TX descriptors.
970d193ed0bSPyun YongHyeon */
971d193ed0bSPyun YongHyeon static int
sge_list_tx_init(struct sge_softc * sc)972d193ed0bSPyun YongHyeon sge_list_tx_init(struct sge_softc *sc)
973d193ed0bSPyun YongHyeon {
974d193ed0bSPyun YongHyeon struct sge_list_data *ld;
975d193ed0bSPyun YongHyeon struct sge_chain_data *cd;
976d193ed0bSPyun YongHyeon
977d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
978d193ed0bSPyun YongHyeon ld = &sc->sge_ldata;
979d193ed0bSPyun YongHyeon cd = &sc->sge_cdata;
980d193ed0bSPyun YongHyeon bzero(ld->sge_tx_ring, SGE_TX_RING_SZ);
981d193ed0bSPyun YongHyeon ld->sge_tx_ring[SGE_TX_RING_CNT - 1].sge_flags = htole32(RING_END);
982d193ed0bSPyun YongHyeon bus_dmamap_sync(cd->sge_tx_tag, cd->sge_tx_dmamap,
983d193ed0bSPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
984d193ed0bSPyun YongHyeon cd->sge_tx_prod = 0;
985d193ed0bSPyun YongHyeon cd->sge_tx_cons = 0;
986d193ed0bSPyun YongHyeon cd->sge_tx_cnt = 0;
987d193ed0bSPyun YongHyeon return (0);
988d193ed0bSPyun YongHyeon }
989d193ed0bSPyun YongHyeon
990d193ed0bSPyun YongHyeon static int
sge_list_tx_free(struct sge_softc * sc)991d193ed0bSPyun YongHyeon sge_list_tx_free(struct sge_softc *sc)
992d193ed0bSPyun YongHyeon {
993d193ed0bSPyun YongHyeon struct sge_chain_data *cd;
99455c978baSPyun YongHyeon struct sge_txdesc *txd;
995d193ed0bSPyun YongHyeon int i;
996d193ed0bSPyun YongHyeon
997d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
998d193ed0bSPyun YongHyeon cd = &sc->sge_cdata;
999d193ed0bSPyun YongHyeon for (i = 0; i < SGE_TX_RING_CNT; i++) {
100055c978baSPyun YongHyeon txd = &cd->sge_txdesc[i];
100155c978baSPyun YongHyeon if (txd->tx_m != NULL) {
100255c978baSPyun YongHyeon bus_dmamap_sync(cd->sge_txmbuf_tag, txd->tx_dmamap,
100355c978baSPyun YongHyeon BUS_DMASYNC_POSTWRITE);
100455c978baSPyun YongHyeon bus_dmamap_unload(cd->sge_txmbuf_tag, txd->tx_dmamap);
1005f648f6bbSPyun YongHyeon m_freem(txd->tx_m);
100655c978baSPyun YongHyeon txd->tx_m = NULL;
100755c978baSPyun YongHyeon txd->tx_ndesc = 0;
1008d193ed0bSPyun YongHyeon }
1009d193ed0bSPyun YongHyeon }
1010d193ed0bSPyun YongHyeon
1011d193ed0bSPyun YongHyeon return (0);
1012d193ed0bSPyun YongHyeon }
1013d193ed0bSPyun YongHyeon
1014d193ed0bSPyun YongHyeon /*
1015d193ed0bSPyun YongHyeon * Initialize the RX descriptors and allocate mbufs for them. Note that
1016d193ed0bSPyun YongHyeon * we arrange the descriptors in a closed ring, so that the last descriptor
1017d193ed0bSPyun YongHyeon * has RING_END flag set.
1018d193ed0bSPyun YongHyeon */
1019d193ed0bSPyun YongHyeon static int
sge_list_rx_init(struct sge_softc * sc)1020d193ed0bSPyun YongHyeon sge_list_rx_init(struct sge_softc *sc)
1021d193ed0bSPyun YongHyeon {
1022d193ed0bSPyun YongHyeon struct sge_chain_data *cd;
1023d193ed0bSPyun YongHyeon int i;
1024d193ed0bSPyun YongHyeon
1025d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
1026d193ed0bSPyun YongHyeon cd = &sc->sge_cdata;
1027d193ed0bSPyun YongHyeon cd->sge_rx_cons = 0;
1028d193ed0bSPyun YongHyeon bzero(sc->sge_ldata.sge_rx_ring, SGE_RX_RING_SZ);
1029d193ed0bSPyun YongHyeon for (i = 0; i < SGE_RX_RING_CNT; i++) {
1030d193ed0bSPyun YongHyeon if (sge_newbuf(sc, i) != 0)
1031d193ed0bSPyun YongHyeon return (ENOBUFS);
1032d193ed0bSPyun YongHyeon }
1033d193ed0bSPyun YongHyeon bus_dmamap_sync(cd->sge_rx_tag, cd->sge_rx_dmamap,
1034d193ed0bSPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1035d193ed0bSPyun YongHyeon return (0);
1036d193ed0bSPyun YongHyeon }
1037d193ed0bSPyun YongHyeon
1038d193ed0bSPyun YongHyeon static int
sge_list_rx_free(struct sge_softc * sc)1039d193ed0bSPyun YongHyeon sge_list_rx_free(struct sge_softc *sc)
1040d193ed0bSPyun YongHyeon {
1041d193ed0bSPyun YongHyeon struct sge_chain_data *cd;
104255c978baSPyun YongHyeon struct sge_rxdesc *rxd;
1043d193ed0bSPyun YongHyeon int i;
1044d193ed0bSPyun YongHyeon
1045d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
1046d193ed0bSPyun YongHyeon cd = &sc->sge_cdata;
1047d193ed0bSPyun YongHyeon for (i = 0; i < SGE_RX_RING_CNT; i++) {
104855c978baSPyun YongHyeon rxd = &cd->sge_rxdesc[i];
104955c978baSPyun YongHyeon if (rxd->rx_m != NULL) {
105055c978baSPyun YongHyeon bus_dmamap_sync(cd->sge_rxmbuf_tag, rxd->rx_dmamap,
1051d193ed0bSPyun YongHyeon BUS_DMASYNC_POSTREAD);
1052d193ed0bSPyun YongHyeon bus_dmamap_unload(cd->sge_rxmbuf_tag,
105355c978baSPyun YongHyeon rxd->rx_dmamap);
1054f648f6bbSPyun YongHyeon m_freem(rxd->rx_m);
105555c978baSPyun YongHyeon rxd->rx_m = NULL;
1056d193ed0bSPyun YongHyeon }
1057d193ed0bSPyun YongHyeon }
1058d193ed0bSPyun YongHyeon return (0);
1059d193ed0bSPyun YongHyeon }
1060d193ed0bSPyun YongHyeon
1061d193ed0bSPyun YongHyeon /*
1062d193ed0bSPyun YongHyeon * Initialize an RX descriptor and attach an MBUF cluster.
1063d193ed0bSPyun YongHyeon */
1064d193ed0bSPyun YongHyeon static int
sge_newbuf(struct sge_softc * sc,int prod)1065d193ed0bSPyun YongHyeon sge_newbuf(struct sge_softc *sc, int prod)
1066d193ed0bSPyun YongHyeon {
1067d193ed0bSPyun YongHyeon struct mbuf *m;
1068d193ed0bSPyun YongHyeon struct sge_desc *desc;
1069d193ed0bSPyun YongHyeon struct sge_chain_data *cd;
107055c978baSPyun YongHyeon struct sge_rxdesc *rxd;
1071d193ed0bSPyun YongHyeon bus_dma_segment_t segs[1];
1072d193ed0bSPyun YongHyeon bus_dmamap_t map;
1073d193ed0bSPyun YongHyeon int error, nsegs;
1074d193ed0bSPyun YongHyeon
1075d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
1076d193ed0bSPyun YongHyeon
1077d193ed0bSPyun YongHyeon cd = &sc->sge_cdata;
1078c6499eccSGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1079d193ed0bSPyun YongHyeon if (m == NULL)
1080d193ed0bSPyun YongHyeon return (ENOBUFS);
1081d193ed0bSPyun YongHyeon m->m_len = m->m_pkthdr.len = MCLBYTES;
1082d193ed0bSPyun YongHyeon m_adj(m, SGE_RX_BUF_ALIGN);
1083d193ed0bSPyun YongHyeon error = bus_dmamap_load_mbuf_sg(cd->sge_rxmbuf_tag,
1084d193ed0bSPyun YongHyeon cd->sge_rx_spare_map, m, segs, &nsegs, 0);
1085d193ed0bSPyun YongHyeon if (error != 0) {
1086d193ed0bSPyun YongHyeon m_freem(m);
1087d193ed0bSPyun YongHyeon return (error);
1088d193ed0bSPyun YongHyeon }
1089d193ed0bSPyun YongHyeon KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
109055c978baSPyun YongHyeon rxd = &cd->sge_rxdesc[prod];
109155c978baSPyun YongHyeon if (rxd->rx_m != NULL) {
109255c978baSPyun YongHyeon bus_dmamap_sync(cd->sge_rxmbuf_tag, rxd->rx_dmamap,
1093d193ed0bSPyun YongHyeon BUS_DMASYNC_POSTREAD);
109455c978baSPyun YongHyeon bus_dmamap_unload(cd->sge_rxmbuf_tag, rxd->rx_dmamap);
1095d193ed0bSPyun YongHyeon }
109655c978baSPyun YongHyeon map = rxd->rx_dmamap;
109755c978baSPyun YongHyeon rxd->rx_dmamap = cd->sge_rx_spare_map;
1098d193ed0bSPyun YongHyeon cd->sge_rx_spare_map = map;
109955c978baSPyun YongHyeon bus_dmamap_sync(cd->sge_rxmbuf_tag, rxd->rx_dmamap,
1100d193ed0bSPyun YongHyeon BUS_DMASYNC_PREREAD);
110155c978baSPyun YongHyeon rxd->rx_m = m;
1102d193ed0bSPyun YongHyeon
1103d193ed0bSPyun YongHyeon desc = &sc->sge_ldata.sge_rx_ring[prod];
1104d193ed0bSPyun YongHyeon desc->sge_sts_size = 0;
1105d193ed0bSPyun YongHyeon desc->sge_ptr = htole32(SGE_ADDR_LO(segs[0].ds_addr));
1106d193ed0bSPyun YongHyeon desc->sge_flags = htole32(segs[0].ds_len);
1107d193ed0bSPyun YongHyeon if (prod == SGE_RX_RING_CNT - 1)
1108d193ed0bSPyun YongHyeon desc->sge_flags |= htole32(RING_END);
110978b11406SPyun YongHyeon desc->sge_cmdsts = htole32(RDC_OWN | RDC_INTR);
1110d193ed0bSPyun YongHyeon return (0);
1111d193ed0bSPyun YongHyeon }
1112d193ed0bSPyun YongHyeon
1113d193ed0bSPyun YongHyeon static __inline void
sge_discard_rxbuf(struct sge_softc * sc,int index)1114d193ed0bSPyun YongHyeon sge_discard_rxbuf(struct sge_softc *sc, int index)
1115d193ed0bSPyun YongHyeon {
1116d193ed0bSPyun YongHyeon struct sge_desc *desc;
1117d193ed0bSPyun YongHyeon
1118d193ed0bSPyun YongHyeon desc = &sc->sge_ldata.sge_rx_ring[index];
1119d193ed0bSPyun YongHyeon desc->sge_sts_size = 0;
1120d193ed0bSPyun YongHyeon desc->sge_flags = htole32(MCLBYTES - SGE_RX_BUF_ALIGN);
1121d193ed0bSPyun YongHyeon if (index == SGE_RX_RING_CNT - 1)
1122d193ed0bSPyun YongHyeon desc->sge_flags |= htole32(RING_END);
112378b11406SPyun YongHyeon desc->sge_cmdsts = htole32(RDC_OWN | RDC_INTR);
1124d193ed0bSPyun YongHyeon }
1125d193ed0bSPyun YongHyeon
1126d193ed0bSPyun YongHyeon /*
1127d193ed0bSPyun YongHyeon * A frame has been uploaded: pass the resulting mbuf chain up to
1128d193ed0bSPyun YongHyeon * the higher level protocols.
1129d193ed0bSPyun YongHyeon */
1130d193ed0bSPyun YongHyeon static void
sge_rxeof(struct sge_softc * sc)1131d193ed0bSPyun YongHyeon sge_rxeof(struct sge_softc *sc)
1132d193ed0bSPyun YongHyeon {
1133*e948d066SJustin Hibbits if_t ifp;
1134d193ed0bSPyun YongHyeon struct mbuf *m;
1135d193ed0bSPyun YongHyeon struct sge_chain_data *cd;
1136d193ed0bSPyun YongHyeon struct sge_desc *cur_rx;
1137d193ed0bSPyun YongHyeon uint32_t rxinfo, rxstat;
1138d193ed0bSPyun YongHyeon int cons, prog;
1139d193ed0bSPyun YongHyeon
1140d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
1141d193ed0bSPyun YongHyeon
1142d193ed0bSPyun YongHyeon ifp = sc->sge_ifp;
1143d193ed0bSPyun YongHyeon cd = &sc->sge_cdata;
1144d193ed0bSPyun YongHyeon
1145d193ed0bSPyun YongHyeon bus_dmamap_sync(cd->sge_rx_tag, cd->sge_rx_dmamap,
1146d193ed0bSPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1147d193ed0bSPyun YongHyeon cons = cd->sge_rx_cons;
1148d193ed0bSPyun YongHyeon for (prog = 0; prog < SGE_RX_RING_CNT; prog++,
1149d193ed0bSPyun YongHyeon SGE_INC(cons, SGE_RX_RING_CNT)) {
1150*e948d066SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
1151d193ed0bSPyun YongHyeon break;
1152d193ed0bSPyun YongHyeon cur_rx = &sc->sge_ldata.sge_rx_ring[cons];
1153d193ed0bSPyun YongHyeon rxinfo = le32toh(cur_rx->sge_cmdsts);
1154d193ed0bSPyun YongHyeon if ((rxinfo & RDC_OWN) != 0)
1155d193ed0bSPyun YongHyeon break;
1156d193ed0bSPyun YongHyeon rxstat = le32toh(cur_rx->sge_sts_size);
1157d1c5ee80SPyun YongHyeon if ((rxstat & RDS_CRCOK) == 0 || SGE_RX_ERROR(rxstat) != 0 ||
1158d1c5ee80SPyun YongHyeon SGE_RX_NSEGS(rxstat) != 1) {
1159d193ed0bSPyun YongHyeon /* XXX We don't support multi-segment frames yet. */
1160d193ed0bSPyun YongHyeon #ifdef SGE_SHOW_ERRORS
1161d193ed0bSPyun YongHyeon device_printf(sc->sge_dev, "Rx error : 0x%b\n", rxstat,
1162d193ed0bSPyun YongHyeon RX_ERR_BITS);
1163d193ed0bSPyun YongHyeon #endif
1164d193ed0bSPyun YongHyeon sge_discard_rxbuf(sc, cons);
1165c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1166d193ed0bSPyun YongHyeon continue;
1167d193ed0bSPyun YongHyeon }
116855c978baSPyun YongHyeon m = cd->sge_rxdesc[cons].rx_m;
1169d193ed0bSPyun YongHyeon if (sge_newbuf(sc, cons) != 0) {
1170d193ed0bSPyun YongHyeon sge_discard_rxbuf(sc, cons);
1171c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1172d193ed0bSPyun YongHyeon continue;
1173d193ed0bSPyun YongHyeon }
1174*e948d066SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0) {
1175d193ed0bSPyun YongHyeon if ((rxinfo & RDC_IP_CSUM) != 0 &&
1176d193ed0bSPyun YongHyeon (rxinfo & RDC_IP_CSUM_OK) != 0)
1177d193ed0bSPyun YongHyeon m->m_pkthdr.csum_flags |=
1178d193ed0bSPyun YongHyeon CSUM_IP_CHECKED | CSUM_IP_VALID;
1179d193ed0bSPyun YongHyeon if (((rxinfo & RDC_TCP_CSUM) != 0 &&
1180d193ed0bSPyun YongHyeon (rxinfo & RDC_TCP_CSUM_OK) != 0) ||
1181d193ed0bSPyun YongHyeon ((rxinfo & RDC_UDP_CSUM) != 0 &&
1182d193ed0bSPyun YongHyeon (rxinfo & RDC_UDP_CSUM_OK) != 0)) {
1183d193ed0bSPyun YongHyeon m->m_pkthdr.csum_flags |=
1184d193ed0bSPyun YongHyeon CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1185d193ed0bSPyun YongHyeon m->m_pkthdr.csum_data = 0xffff;
1186d193ed0bSPyun YongHyeon }
1187d193ed0bSPyun YongHyeon }
1188c186cf13SPyun YongHyeon /* Check for VLAN tagged frame. */
1189*e948d066SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0 &&
1190c186cf13SPyun YongHyeon (rxstat & RDS_VLAN) != 0) {
1191c186cf13SPyun YongHyeon m->m_pkthdr.ether_vtag = rxinfo & RDC_VLAN_MASK;
1192c186cf13SPyun YongHyeon m->m_flags |= M_VLANTAG;
1193c186cf13SPyun YongHyeon }
1194d1c5ee80SPyun YongHyeon /*
1195d1c5ee80SPyun YongHyeon * Account for 10bytes auto padding which is used
1196d1c5ee80SPyun YongHyeon * to align IP header on 32bit boundary. Also note,
1197d1c5ee80SPyun YongHyeon * CRC bytes is automatically removed by the
1198d1c5ee80SPyun YongHyeon * hardware.
1199d1c5ee80SPyun YongHyeon */
1200d1c5ee80SPyun YongHyeon m->m_data += SGE_RX_PAD_BYTES;
1201d1c5ee80SPyun YongHyeon m->m_pkthdr.len = m->m_len = SGE_RX_BYTES(rxstat) -
1202d1c5ee80SPyun YongHyeon SGE_RX_PAD_BYTES;
1203d193ed0bSPyun YongHyeon m->m_pkthdr.rcvif = ifp;
1204c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1205d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
1206*e948d066SJustin Hibbits if_input(ifp, m);
1207d193ed0bSPyun YongHyeon SGE_LOCK(sc);
1208d193ed0bSPyun YongHyeon }
1209d193ed0bSPyun YongHyeon
1210d193ed0bSPyun YongHyeon if (prog > 0) {
1211d193ed0bSPyun YongHyeon bus_dmamap_sync(cd->sge_rx_tag, cd->sge_rx_dmamap,
1212d193ed0bSPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1213d193ed0bSPyun YongHyeon cd->sge_rx_cons = cons;
1214d193ed0bSPyun YongHyeon }
1215d193ed0bSPyun YongHyeon }
1216d193ed0bSPyun YongHyeon
1217d193ed0bSPyun YongHyeon /*
1218d193ed0bSPyun YongHyeon * A frame was downloaded to the chip. It's safe for us to clean up
1219d193ed0bSPyun YongHyeon * the list buffers.
1220d193ed0bSPyun YongHyeon */
1221d193ed0bSPyun YongHyeon static void
sge_txeof(struct sge_softc * sc)1222d193ed0bSPyun YongHyeon sge_txeof(struct sge_softc *sc)
1223d193ed0bSPyun YongHyeon {
1224*e948d066SJustin Hibbits if_t ifp;
1225d193ed0bSPyun YongHyeon struct sge_list_data *ld;
1226d193ed0bSPyun YongHyeon struct sge_chain_data *cd;
122755c978baSPyun YongHyeon struct sge_txdesc *txd;
1228d193ed0bSPyun YongHyeon uint32_t txstat;
122955c978baSPyun YongHyeon int cons, nsegs, prod;
1230d193ed0bSPyun YongHyeon
1231d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
1232d193ed0bSPyun YongHyeon
1233d193ed0bSPyun YongHyeon ifp = sc->sge_ifp;
1234d193ed0bSPyun YongHyeon ld = &sc->sge_ldata;
1235d193ed0bSPyun YongHyeon cd = &sc->sge_cdata;
1236d193ed0bSPyun YongHyeon
1237d193ed0bSPyun YongHyeon if (cd->sge_tx_cnt == 0)
1238d193ed0bSPyun YongHyeon return;
1239d193ed0bSPyun YongHyeon bus_dmamap_sync(cd->sge_tx_tag, cd->sge_tx_dmamap,
1240d193ed0bSPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1241d193ed0bSPyun YongHyeon cons = cd->sge_tx_cons;
1242d193ed0bSPyun YongHyeon prod = cd->sge_tx_prod;
124355c978baSPyun YongHyeon for (; cons != prod;) {
1244d193ed0bSPyun YongHyeon txstat = le32toh(ld->sge_tx_ring[cons].sge_cmdsts);
1245d193ed0bSPyun YongHyeon if ((txstat & TDC_OWN) != 0)
1246d193ed0bSPyun YongHyeon break;
124755c978baSPyun YongHyeon /*
124855c978baSPyun YongHyeon * Only the first descriptor of multi-descriptor transmission
124955c978baSPyun YongHyeon * is updated by controller. Driver should skip entire
125055c978baSPyun YongHyeon * chained buffers for the transmitted frame. In other words
125155c978baSPyun YongHyeon * TDC_OWN bit is valid only at the first descriptor of a
125255c978baSPyun YongHyeon * multi-descriptor transmission.
125355c978baSPyun YongHyeon */
1254d193ed0bSPyun YongHyeon if (SGE_TX_ERROR(txstat) != 0) {
1255d193ed0bSPyun YongHyeon #ifdef SGE_SHOW_ERRORS
1256d193ed0bSPyun YongHyeon device_printf(sc->sge_dev, "Tx error : 0x%b\n",
1257d193ed0bSPyun YongHyeon txstat, TX_ERR_BITS);
1258d193ed0bSPyun YongHyeon #endif
1259c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1260d193ed0bSPyun YongHyeon } else {
1261d193ed0bSPyun YongHyeon #ifdef notyet
1262c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_COLLISIONS, (txstat & 0xFFFF) - 1);
1263d193ed0bSPyun YongHyeon #endif
1264c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1265d193ed0bSPyun YongHyeon }
126655c978baSPyun YongHyeon txd = &cd->sge_txdesc[cons];
126755c978baSPyun YongHyeon for (nsegs = 0; nsegs < txd->tx_ndesc; nsegs++) {
126855c978baSPyun YongHyeon ld->sge_tx_ring[cons].sge_cmdsts = 0;
126955c978baSPyun YongHyeon SGE_INC(cons, SGE_TX_RING_CNT);
1270d193ed0bSPyun YongHyeon }
127155c978baSPyun YongHyeon /* Reclaim transmitted mbuf. */
127255c978baSPyun YongHyeon KASSERT(txd->tx_m != NULL,
127355c978baSPyun YongHyeon ("%s: freeing NULL mbuf\n", __func__));
127455c978baSPyun YongHyeon bus_dmamap_sync(cd->sge_txmbuf_tag, txd->tx_dmamap,
127555c978baSPyun YongHyeon BUS_DMASYNC_POSTWRITE);
127655c978baSPyun YongHyeon bus_dmamap_unload(cd->sge_txmbuf_tag, txd->tx_dmamap);
127755c978baSPyun YongHyeon m_freem(txd->tx_m);
127855c978baSPyun YongHyeon txd->tx_m = NULL;
127955c978baSPyun YongHyeon cd->sge_tx_cnt -= txd->tx_ndesc;
128055c978baSPyun YongHyeon KASSERT(cd->sge_tx_cnt >= 0,
128155c978baSPyun YongHyeon ("%s: Active Tx desc counter was garbled\n", __func__));
128255c978baSPyun YongHyeon txd->tx_ndesc = 0;
1283*e948d066SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1284d193ed0bSPyun YongHyeon }
1285d193ed0bSPyun YongHyeon cd->sge_tx_cons = cons;
1286d193ed0bSPyun YongHyeon if (cd->sge_tx_cnt == 0)
1287d193ed0bSPyun YongHyeon sc->sge_timer = 0;
1288d193ed0bSPyun YongHyeon }
1289d193ed0bSPyun YongHyeon
1290d193ed0bSPyun YongHyeon static void
sge_tick(void * arg)1291d193ed0bSPyun YongHyeon sge_tick(void *arg)
1292d193ed0bSPyun YongHyeon {
1293d193ed0bSPyun YongHyeon struct sge_softc *sc;
1294d193ed0bSPyun YongHyeon struct mii_data *mii;
1295*e948d066SJustin Hibbits if_t ifp;
1296d193ed0bSPyun YongHyeon
1297d193ed0bSPyun YongHyeon sc = arg;
1298d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
1299d193ed0bSPyun YongHyeon
1300d193ed0bSPyun YongHyeon ifp = sc->sge_ifp;
1301d193ed0bSPyun YongHyeon mii = device_get_softc(sc->sge_miibus);
1302d193ed0bSPyun YongHyeon mii_tick(mii);
1303d193ed0bSPyun YongHyeon if ((sc->sge_flags & SGE_FLAG_LINK) == 0) {
1304d193ed0bSPyun YongHyeon sge_miibus_statchg(sc->sge_dev);
1305d193ed0bSPyun YongHyeon if ((sc->sge_flags & SGE_FLAG_LINK) != 0 &&
1306*e948d066SJustin Hibbits !if_sendq_empty(ifp))
1307d193ed0bSPyun YongHyeon sge_start_locked(ifp);
1308d193ed0bSPyun YongHyeon }
1309d193ed0bSPyun YongHyeon /*
1310d193ed0bSPyun YongHyeon * Reclaim transmitted frames here as we do not request
1311d193ed0bSPyun YongHyeon * Tx completion interrupt for every queued frames to
1312d193ed0bSPyun YongHyeon * reduce excessive interrupts.
1313d193ed0bSPyun YongHyeon */
1314d193ed0bSPyun YongHyeon sge_txeof(sc);
1315d193ed0bSPyun YongHyeon sge_watchdog(sc);
1316d193ed0bSPyun YongHyeon callout_reset(&sc->sge_stat_ch, hz, sge_tick, sc);
1317d193ed0bSPyun YongHyeon }
1318d193ed0bSPyun YongHyeon
1319d193ed0bSPyun YongHyeon static void
sge_intr(void * arg)1320d193ed0bSPyun YongHyeon sge_intr(void *arg)
1321d193ed0bSPyun YongHyeon {
1322d193ed0bSPyun YongHyeon struct sge_softc *sc;
1323*e948d066SJustin Hibbits if_t ifp;
1324d193ed0bSPyun YongHyeon uint32_t status;
1325d193ed0bSPyun YongHyeon
1326d193ed0bSPyun YongHyeon sc = arg;
1327d193ed0bSPyun YongHyeon SGE_LOCK(sc);
1328d193ed0bSPyun YongHyeon ifp = sc->sge_ifp;
1329d193ed0bSPyun YongHyeon
1330d193ed0bSPyun YongHyeon status = CSR_READ_4(sc, IntrStatus);
1331d193ed0bSPyun YongHyeon if (status == 0xFFFFFFFF || (status & SGE_INTRS) == 0) {
1332d193ed0bSPyun YongHyeon /* Not ours. */
1333d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
1334d193ed0bSPyun YongHyeon return;
1335d193ed0bSPyun YongHyeon }
1336d193ed0bSPyun YongHyeon /* Acknowledge interrupts. */
1337d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrStatus, status);
1338d193ed0bSPyun YongHyeon /* Disable further interrupts. */
1339d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrMask, 0);
1340d193ed0bSPyun YongHyeon /*
1341d193ed0bSPyun YongHyeon * It seems the controller supports some kind of interrupt
1342d193ed0bSPyun YongHyeon * moderation mechanism but we still don't know how to
1343d193ed0bSPyun YongHyeon * enable that. To reduce number of generated interrupts
1344d193ed0bSPyun YongHyeon * under load we check pending interrupts in a loop. This
1345d193ed0bSPyun YongHyeon * will increase number of register access and is not correct
1346d193ed0bSPyun YongHyeon * way to handle interrupt moderation but there seems to be
1347d193ed0bSPyun YongHyeon * no other way at this time.
1348d193ed0bSPyun YongHyeon */
1349d193ed0bSPyun YongHyeon for (;;) {
1350*e948d066SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
1351d193ed0bSPyun YongHyeon break;
1352d193ed0bSPyun YongHyeon if ((status & (INTR_RX_DONE | INTR_RX_IDLE)) != 0) {
1353d193ed0bSPyun YongHyeon sge_rxeof(sc);
1354d193ed0bSPyun YongHyeon /* Wakeup Rx MAC. */
1355d193ed0bSPyun YongHyeon if ((status & INTR_RX_IDLE) != 0)
1356d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, RX_CTL,
1357d193ed0bSPyun YongHyeon 0x1a00 | 0x000c | RX_CTL_POLL | RX_CTL_ENB);
1358d193ed0bSPyun YongHyeon }
1359d193ed0bSPyun YongHyeon if ((status & (INTR_TX_DONE | INTR_TX_IDLE)) != 0)
1360d193ed0bSPyun YongHyeon sge_txeof(sc);
1361d193ed0bSPyun YongHyeon status = CSR_READ_4(sc, IntrStatus);
1362d193ed0bSPyun YongHyeon if ((status & SGE_INTRS) == 0)
1363d193ed0bSPyun YongHyeon break;
1364d193ed0bSPyun YongHyeon /* Acknowledge interrupts. */
1365d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrStatus, status);
1366d193ed0bSPyun YongHyeon }
1367*e948d066SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
1368d193ed0bSPyun YongHyeon /* Re-enable interrupts */
1369d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrMask, SGE_INTRS);
1370*e948d066SJustin Hibbits if (!if_sendq_empty(ifp))
1371d193ed0bSPyun YongHyeon sge_start_locked(ifp);
1372d193ed0bSPyun YongHyeon }
1373d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
1374d193ed0bSPyun YongHyeon }
1375d193ed0bSPyun YongHyeon
1376d193ed0bSPyun YongHyeon /*
1377d193ed0bSPyun YongHyeon * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1378d193ed0bSPyun YongHyeon * pointers to the fragment pointers.
1379d193ed0bSPyun YongHyeon */
1380d193ed0bSPyun YongHyeon static int
sge_encap(struct sge_softc * sc,struct mbuf ** m_head)1381d193ed0bSPyun YongHyeon sge_encap(struct sge_softc *sc, struct mbuf **m_head)
1382d193ed0bSPyun YongHyeon {
1383d193ed0bSPyun YongHyeon struct mbuf *m;
1384d193ed0bSPyun YongHyeon struct sge_desc *desc;
138555c978baSPyun YongHyeon struct sge_txdesc *txd;
1386d193ed0bSPyun YongHyeon bus_dma_segment_t txsegs[SGE_MAXTXSEGS];
138765329b31SPyun YongHyeon uint32_t cflags, mss;
138855c978baSPyun YongHyeon int error, i, nsegs, prod, si;
1389d193ed0bSPyun YongHyeon
1390d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
1391d193ed0bSPyun YongHyeon
139255c978baSPyun YongHyeon si = prod = sc->sge_cdata.sge_tx_prod;
139355c978baSPyun YongHyeon txd = &sc->sge_cdata.sge_txdesc[prod];
139465329b31SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
139565329b31SPyun YongHyeon struct ether_header *eh;
139665329b31SPyun YongHyeon struct ip *ip;
139765329b31SPyun YongHyeon struct tcphdr *tcp;
139865329b31SPyun YongHyeon uint32_t ip_off, poff;
139965329b31SPyun YongHyeon
140065329b31SPyun YongHyeon if (M_WRITABLE(*m_head) == 0) {
140165329b31SPyun YongHyeon /* Get a writable copy. */
1402c6499eccSGleb Smirnoff m = m_dup(*m_head, M_NOWAIT);
140365329b31SPyun YongHyeon m_freem(*m_head);
140465329b31SPyun YongHyeon if (m == NULL) {
140565329b31SPyun YongHyeon *m_head = NULL;
140665329b31SPyun YongHyeon return (ENOBUFS);
140765329b31SPyun YongHyeon }
140865329b31SPyun YongHyeon *m_head = m;
140965329b31SPyun YongHyeon }
141065329b31SPyun YongHyeon ip_off = sizeof(struct ether_header);
141165329b31SPyun YongHyeon m = m_pullup(*m_head, ip_off);
141265329b31SPyun YongHyeon if (m == NULL) {
141365329b31SPyun YongHyeon *m_head = NULL;
141465329b31SPyun YongHyeon return (ENOBUFS);
141565329b31SPyun YongHyeon }
141665329b31SPyun YongHyeon eh = mtod(m, struct ether_header *);
141765329b31SPyun YongHyeon /* Check the existence of VLAN tag. */
141865329b31SPyun YongHyeon if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
141965329b31SPyun YongHyeon ip_off = sizeof(struct ether_vlan_header);
142065329b31SPyun YongHyeon m = m_pullup(m, ip_off);
142165329b31SPyun YongHyeon if (m == NULL) {
142265329b31SPyun YongHyeon *m_head = NULL;
142365329b31SPyun YongHyeon return (ENOBUFS);
142465329b31SPyun YongHyeon }
142565329b31SPyun YongHyeon }
142665329b31SPyun YongHyeon m = m_pullup(m, ip_off + sizeof(struct ip));
142765329b31SPyun YongHyeon if (m == NULL) {
142865329b31SPyun YongHyeon *m_head = NULL;
142965329b31SPyun YongHyeon return (ENOBUFS);
143065329b31SPyun YongHyeon }
143165329b31SPyun YongHyeon ip = (struct ip *)(mtod(m, char *) + ip_off);
143265329b31SPyun YongHyeon poff = ip_off + (ip->ip_hl << 2);
143365329b31SPyun YongHyeon m = m_pullup(m, poff + sizeof(struct tcphdr));
143465329b31SPyun YongHyeon if (m == NULL) {
143565329b31SPyun YongHyeon *m_head = NULL;
143665329b31SPyun YongHyeon return (ENOBUFS);
143765329b31SPyun YongHyeon }
143865329b31SPyun YongHyeon tcp = (struct tcphdr *)(mtod(m, char *) + poff);
143965329b31SPyun YongHyeon m = m_pullup(m, poff + (tcp->th_off << 2));
144065329b31SPyun YongHyeon if (m == NULL) {
144165329b31SPyun YongHyeon *m_head = NULL;
144265329b31SPyun YongHyeon return (ENOBUFS);
144365329b31SPyun YongHyeon }
144465329b31SPyun YongHyeon /*
144565329b31SPyun YongHyeon * Reset IP checksum and recompute TCP pseudo
144665329b31SPyun YongHyeon * checksum that NDIS specification requires.
144765329b31SPyun YongHyeon */
144896486faaSPyun YongHyeon ip = (struct ip *)(mtod(m, char *) + ip_off);
144965329b31SPyun YongHyeon ip->ip_sum = 0;
145096486faaSPyun YongHyeon tcp = (struct tcphdr *)(mtod(m, char *) + poff);
145165329b31SPyun YongHyeon tcp->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
145265329b31SPyun YongHyeon htons(IPPROTO_TCP));
145365329b31SPyun YongHyeon *m_head = m;
145465329b31SPyun YongHyeon }
145565329b31SPyun YongHyeon
145655c978baSPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->sge_cdata.sge_txmbuf_tag,
145755c978baSPyun YongHyeon txd->tx_dmamap, *m_head, txsegs, &nsegs, 0);
145855c978baSPyun YongHyeon if (error == EFBIG) {
1459c6499eccSGleb Smirnoff m = m_collapse(*m_head, M_NOWAIT, SGE_MAXTXSEGS);
1460d193ed0bSPyun YongHyeon if (m == NULL) {
1461d193ed0bSPyun YongHyeon m_freem(*m_head);
1462d193ed0bSPyun YongHyeon *m_head = NULL;
1463d193ed0bSPyun YongHyeon return (ENOBUFS);
1464d193ed0bSPyun YongHyeon }
1465d193ed0bSPyun YongHyeon *m_head = m;
146655c978baSPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->sge_cdata.sge_txmbuf_tag,
146755c978baSPyun YongHyeon txd->tx_dmamap, *m_head, txsegs, &nsegs, 0);
1468d193ed0bSPyun YongHyeon if (error != 0) {
1469d193ed0bSPyun YongHyeon m_freem(*m_head);
1470d193ed0bSPyun YongHyeon *m_head = NULL;
1471d193ed0bSPyun YongHyeon return (error);
1472d193ed0bSPyun YongHyeon }
147355c978baSPyun YongHyeon } else if (error != 0)
147455c978baSPyun YongHyeon return (error);
147555c978baSPyun YongHyeon
147655c978baSPyun YongHyeon KASSERT(nsegs != 0, ("zero segment returned"));
1477d193ed0bSPyun YongHyeon /* Check descriptor overrun. */
1478d193ed0bSPyun YongHyeon if (sc->sge_cdata.sge_tx_cnt + nsegs >= SGE_TX_RING_CNT) {
147955c978baSPyun YongHyeon bus_dmamap_unload(sc->sge_cdata.sge_txmbuf_tag, txd->tx_dmamap);
1480d193ed0bSPyun YongHyeon return (ENOBUFS);
1481d193ed0bSPyun YongHyeon }
148255c978baSPyun YongHyeon bus_dmamap_sync(sc->sge_cdata.sge_txmbuf_tag, txd->tx_dmamap,
1483464aa6d5SPyun YongHyeon BUS_DMASYNC_PREWRITE);
1484d193ed0bSPyun YongHyeon
148555c978baSPyun YongHyeon m = *m_head;
1486d193ed0bSPyun YongHyeon cflags = 0;
148765329b31SPyun YongHyeon mss = 0;
148865329b31SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
148965329b31SPyun YongHyeon cflags |= TDC_LS;
149065329b31SPyun YongHyeon mss = (uint32_t)m->m_pkthdr.tso_segsz;
149165329b31SPyun YongHyeon mss <<= 16;
149265329b31SPyun YongHyeon } else {
149355c978baSPyun YongHyeon if (m->m_pkthdr.csum_flags & CSUM_IP)
1494d193ed0bSPyun YongHyeon cflags |= TDC_IP_CSUM;
149555c978baSPyun YongHyeon if (m->m_pkthdr.csum_flags & CSUM_TCP)
1496d193ed0bSPyun YongHyeon cflags |= TDC_TCP_CSUM;
149755c978baSPyun YongHyeon if (m->m_pkthdr.csum_flags & CSUM_UDP)
1498d193ed0bSPyun YongHyeon cflags |= TDC_UDP_CSUM;
149965329b31SPyun YongHyeon }
150055c978baSPyun YongHyeon for (i = 0; i < nsegs; i++) {
1501d193ed0bSPyun YongHyeon desc = &sc->sge_ldata.sge_tx_ring[prod];
150255c978baSPyun YongHyeon if (i == 0) {
150365329b31SPyun YongHyeon desc->sge_sts_size = htole32(m->m_pkthdr.len | mss);
150455c978baSPyun YongHyeon desc->sge_cmdsts = 0;
150555c978baSPyun YongHyeon } else {
150655c978baSPyun YongHyeon desc->sge_sts_size = 0;
150755c978baSPyun YongHyeon desc->sge_cmdsts = htole32(TDC_OWN);
150855c978baSPyun YongHyeon }
150955c978baSPyun YongHyeon desc->sge_ptr = htole32(SGE_ADDR_LO(txsegs[i].ds_addr));
151055c978baSPyun YongHyeon desc->sge_flags = htole32(txsegs[i].ds_len);
1511d193ed0bSPyun YongHyeon if (prod == SGE_TX_RING_CNT - 1)
1512d193ed0bSPyun YongHyeon desc->sge_flags |= htole32(RING_END);
151355c978baSPyun YongHyeon sc->sge_cdata.sge_tx_cnt++;
151455c978baSPyun YongHyeon SGE_INC(prod, SGE_TX_RING_CNT);
151555c978baSPyun YongHyeon }
151655c978baSPyun YongHyeon /* Update producer index. */
151755c978baSPyun YongHyeon sc->sge_cdata.sge_tx_prod = prod;
151855c978baSPyun YongHyeon
151955c978baSPyun YongHyeon desc = &sc->sge_ldata.sge_tx_ring[si];
1520c186cf13SPyun YongHyeon /* Configure VLAN. */
152155c978baSPyun YongHyeon if((m->m_flags & M_VLANTAG) != 0) {
152255c978baSPyun YongHyeon cflags |= m->m_pkthdr.ether_vtag;
1523c186cf13SPyun YongHyeon desc->sge_sts_size |= htole32(TDS_INS_VLAN);
1524c186cf13SPyun YongHyeon }
152555c978baSPyun YongHyeon desc->sge_cmdsts |= htole32(TDC_DEF | TDC_CRC | TDC_PAD | cflags);
1526d193ed0bSPyun YongHyeon #if 1
1527d193ed0bSPyun YongHyeon if ((sc->sge_flags & SGE_FLAG_SPEED_1000) != 0)
1528d193ed0bSPyun YongHyeon desc->sge_cmdsts |= htole32(TDC_BST);
1529d193ed0bSPyun YongHyeon #else
1530d193ed0bSPyun YongHyeon if ((sc->sge_flags & SGE_FLAG_FDX) == 0) {
1531d193ed0bSPyun YongHyeon desc->sge_cmdsts |= htole32(TDC_COL | TDC_CRS | TDC_BKF);
1532d193ed0bSPyun YongHyeon if ((sc->sge_flags & SGE_FLAG_SPEED_1000) != 0)
1533d193ed0bSPyun YongHyeon desc->sge_cmdsts |= htole32(TDC_EXT | TDC_BST);
1534d193ed0bSPyun YongHyeon }
1535d193ed0bSPyun YongHyeon #endif
1536d193ed0bSPyun YongHyeon /* Request interrupt and give ownership to controller. */
1537d193ed0bSPyun YongHyeon desc->sge_cmdsts |= htole32(TDC_OWN | TDC_INTR);
153855c978baSPyun YongHyeon txd->tx_m = m;
153955c978baSPyun YongHyeon txd->tx_ndesc = nsegs;
1540d193ed0bSPyun YongHyeon return (0);
1541d193ed0bSPyun YongHyeon }
1542d193ed0bSPyun YongHyeon
1543d193ed0bSPyun YongHyeon static void
sge_start(if_t ifp)1544*e948d066SJustin Hibbits sge_start(if_t ifp)
1545d193ed0bSPyun YongHyeon {
1546d193ed0bSPyun YongHyeon struct sge_softc *sc;
1547d193ed0bSPyun YongHyeon
1548*e948d066SJustin Hibbits sc = if_getsoftc(ifp);
1549d193ed0bSPyun YongHyeon SGE_LOCK(sc);
1550d193ed0bSPyun YongHyeon sge_start_locked(ifp);
1551d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
1552d193ed0bSPyun YongHyeon }
1553d193ed0bSPyun YongHyeon
1554d193ed0bSPyun YongHyeon static void
sge_start_locked(if_t ifp)1555*e948d066SJustin Hibbits sge_start_locked(if_t ifp)
1556d193ed0bSPyun YongHyeon {
1557d193ed0bSPyun YongHyeon struct sge_softc *sc;
1558d193ed0bSPyun YongHyeon struct mbuf *m_head;
1559d193ed0bSPyun YongHyeon int queued = 0;
1560d193ed0bSPyun YongHyeon
1561*e948d066SJustin Hibbits sc = if_getsoftc(ifp);
1562d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
1563d193ed0bSPyun YongHyeon
1564d193ed0bSPyun YongHyeon if ((sc->sge_flags & SGE_FLAG_LINK) == 0 ||
1565*e948d066SJustin Hibbits (if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1566d193ed0bSPyun YongHyeon IFF_DRV_RUNNING)
1567d193ed0bSPyun YongHyeon return;
1568d193ed0bSPyun YongHyeon
1569*e948d066SJustin Hibbits for (queued = 0; !if_sendq_empty(ifp); ) {
157055c978baSPyun YongHyeon if (sc->sge_cdata.sge_tx_cnt > (SGE_TX_RING_CNT -
157155c978baSPyun YongHyeon SGE_MAXTXSEGS)) {
1572*e948d066SJustin Hibbits if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
1573d193ed0bSPyun YongHyeon break;
1574d193ed0bSPyun YongHyeon }
1575*e948d066SJustin Hibbits m_head = if_dequeue(ifp);
1576d193ed0bSPyun YongHyeon if (m_head == NULL)
1577d193ed0bSPyun YongHyeon break;
1578d193ed0bSPyun YongHyeon if (sge_encap(sc, &m_head)) {
15799def3574SPyun YongHyeon if (m_head == NULL)
15809def3574SPyun YongHyeon break;
1581*e948d066SJustin Hibbits if_sendq_prepend(ifp, m_head);
1582*e948d066SJustin Hibbits if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
1583d193ed0bSPyun YongHyeon break;
1584d193ed0bSPyun YongHyeon }
1585d193ed0bSPyun YongHyeon queued++;
1586d193ed0bSPyun YongHyeon /*
1587d193ed0bSPyun YongHyeon * If there's a BPF listener, bounce a copy of this frame
1588d193ed0bSPyun YongHyeon * to him.
1589d193ed0bSPyun YongHyeon */
1590d193ed0bSPyun YongHyeon BPF_MTAP(ifp, m_head);
1591d193ed0bSPyun YongHyeon }
1592d193ed0bSPyun YongHyeon
1593d193ed0bSPyun YongHyeon if (queued > 0) {
1594d193ed0bSPyun YongHyeon bus_dmamap_sync(sc->sge_cdata.sge_tx_tag,
1595d193ed0bSPyun YongHyeon sc->sge_cdata.sge_tx_dmamap,
1596d193ed0bSPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1597d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, TX_CTL, 0x1a00 | TX_CTL_ENB | TX_CTL_POLL);
1598d193ed0bSPyun YongHyeon sc->sge_timer = 5;
1599d193ed0bSPyun YongHyeon }
1600d193ed0bSPyun YongHyeon }
1601d193ed0bSPyun YongHyeon
1602d193ed0bSPyun YongHyeon static void
sge_init(void * arg)1603d193ed0bSPyun YongHyeon sge_init(void *arg)
1604d193ed0bSPyun YongHyeon {
1605d193ed0bSPyun YongHyeon struct sge_softc *sc;
1606d193ed0bSPyun YongHyeon
1607d193ed0bSPyun YongHyeon sc = arg;
1608d193ed0bSPyun YongHyeon SGE_LOCK(sc);
1609d193ed0bSPyun YongHyeon sge_init_locked(sc);
1610d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
1611d193ed0bSPyun YongHyeon }
1612d193ed0bSPyun YongHyeon
1613d193ed0bSPyun YongHyeon static void
sge_init_locked(struct sge_softc * sc)1614d193ed0bSPyun YongHyeon sge_init_locked(struct sge_softc *sc)
1615d193ed0bSPyun YongHyeon {
1616*e948d066SJustin Hibbits if_t ifp;
1617d193ed0bSPyun YongHyeon struct mii_data *mii;
1618d1c5ee80SPyun YongHyeon uint16_t rxfilt;
1619d193ed0bSPyun YongHyeon int i;
1620d193ed0bSPyun YongHyeon
1621d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
1622d193ed0bSPyun YongHyeon ifp = sc->sge_ifp;
1623d193ed0bSPyun YongHyeon mii = device_get_softc(sc->sge_miibus);
1624*e948d066SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
1625d193ed0bSPyun YongHyeon return;
1626d193ed0bSPyun YongHyeon /*
1627d193ed0bSPyun YongHyeon * Cancel pending I/O and free all RX/TX buffers.
1628d193ed0bSPyun YongHyeon */
1629d193ed0bSPyun YongHyeon sge_stop(sc);
1630d193ed0bSPyun YongHyeon sge_reset(sc);
1631d193ed0bSPyun YongHyeon
1632d193ed0bSPyun YongHyeon /* Init circular RX list. */
1633d193ed0bSPyun YongHyeon if (sge_list_rx_init(sc) == ENOBUFS) {
1634d193ed0bSPyun YongHyeon device_printf(sc->sge_dev, "no memory for Rx buffers\n");
1635d193ed0bSPyun YongHyeon sge_stop(sc);
1636d193ed0bSPyun YongHyeon return;
1637d193ed0bSPyun YongHyeon }
1638d193ed0bSPyun YongHyeon /* Init TX descriptors. */
1639d193ed0bSPyun YongHyeon sge_list_tx_init(sc);
1640d193ed0bSPyun YongHyeon /*
1641d193ed0bSPyun YongHyeon * Load the address of the RX and TX lists.
1642d193ed0bSPyun YongHyeon */
1643d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, TX_DESC, SGE_ADDR_LO(sc->sge_ldata.sge_tx_paddr));
1644d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, RX_DESC, SGE_ADDR_LO(sc->sge_ldata.sge_rx_paddr));
1645d193ed0bSPyun YongHyeon
1646d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, TxMacControl, 0x60);
1647d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, RxWakeOnLan, 0);
1648d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, RxWakeOnLanData, 0);
1649d193ed0bSPyun YongHyeon /* Allow receiving VLAN frames. */
16508775710aSPyun YongHyeon CSR_WRITE_2(sc, RxMPSControl, ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN +
16518775710aSPyun YongHyeon SGE_RX_PAD_BYTES);
1652d193ed0bSPyun YongHyeon
1653d193ed0bSPyun YongHyeon for (i = 0; i < ETHER_ADDR_LEN; i++)
1654*e948d066SJustin Hibbits CSR_WRITE_1(sc, RxMacAddr + i, if_getlladdr(ifp)[i]);
1655d1c5ee80SPyun YongHyeon /* Configure RX MAC. */
165678b11406SPyun YongHyeon rxfilt = RXMAC_STRIP_FCS | RXMAC_PAD_ENB | RXMAC_CSUM_ENB;
1657d1c5ee80SPyun YongHyeon CSR_WRITE_2(sc, RxMacControl, rxfilt);
1658d193ed0bSPyun YongHyeon sge_rxfilter(sc);
1659c186cf13SPyun YongHyeon sge_setvlan(sc);
1660d193ed0bSPyun YongHyeon
1661d193ed0bSPyun YongHyeon /* Initialize default speed/duplex information. */
1662d193ed0bSPyun YongHyeon if ((sc->sge_flags & SGE_FLAG_FASTETHER) == 0)
1663d193ed0bSPyun YongHyeon sc->sge_flags |= SGE_FLAG_SPEED_1000;
1664d193ed0bSPyun YongHyeon sc->sge_flags |= SGE_FLAG_FDX;
1665d193ed0bSPyun YongHyeon if ((sc->sge_flags & SGE_FLAG_RGMII) != 0)
1666d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, StationControl, 0x04008001);
1667d193ed0bSPyun YongHyeon else
1668d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, StationControl, 0x04000001);
1669d193ed0bSPyun YongHyeon /*
1670d193ed0bSPyun YongHyeon * XXX Try to mitigate interrupts.
1671d193ed0bSPyun YongHyeon */
1672a1a667ecSPyun YongHyeon CSR_WRITE_4(sc, IntrControl, 0x08880000);
1673a1a667ecSPyun YongHyeon #ifdef notyet
1674d193ed0bSPyun YongHyeon if (sc->sge_intrcontrol != 0)
1675d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrControl, sc->sge_intrcontrol);
1676d193ed0bSPyun YongHyeon if (sc->sge_intrtimer != 0)
1677d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrTimer, sc->sge_intrtimer);
1678a1a667ecSPyun YongHyeon #endif
1679d193ed0bSPyun YongHyeon
1680d193ed0bSPyun YongHyeon /*
1681d193ed0bSPyun YongHyeon * Clear and enable interrupts.
1682d193ed0bSPyun YongHyeon */
1683d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrStatus, 0xFFFFFFFF);
1684d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrMask, SGE_INTRS);
1685d193ed0bSPyun YongHyeon
1686d193ed0bSPyun YongHyeon /* Enable receiver and transmitter. */
1687d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, TX_CTL, 0x1a00 | TX_CTL_ENB);
1688d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, RX_CTL, 0x1a00 | 0x000c | RX_CTL_POLL | RX_CTL_ENB);
1689d193ed0bSPyun YongHyeon
1690*e948d066SJustin Hibbits if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
1691*e948d066SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1692d193ed0bSPyun YongHyeon
1693d193ed0bSPyun YongHyeon sc->sge_flags &= ~SGE_FLAG_LINK;
1694d193ed0bSPyun YongHyeon mii_mediachg(mii);
1695d193ed0bSPyun YongHyeon callout_reset(&sc->sge_stat_ch, hz, sge_tick, sc);
1696d193ed0bSPyun YongHyeon }
1697d193ed0bSPyun YongHyeon
1698d193ed0bSPyun YongHyeon /*
1699d193ed0bSPyun YongHyeon * Set media options.
1700d193ed0bSPyun YongHyeon */
1701d193ed0bSPyun YongHyeon static int
sge_ifmedia_upd(if_t ifp)1702*e948d066SJustin Hibbits sge_ifmedia_upd(if_t ifp)
1703d193ed0bSPyun YongHyeon {
1704d193ed0bSPyun YongHyeon struct sge_softc *sc;
1705d193ed0bSPyun YongHyeon struct mii_data *mii;
17063fcb7a53SMarius Strobl struct mii_softc *miisc;
1707d193ed0bSPyun YongHyeon int error;
1708d193ed0bSPyun YongHyeon
1709*e948d066SJustin Hibbits sc = if_getsoftc(ifp);
1710d193ed0bSPyun YongHyeon SGE_LOCK(sc);
1711d193ed0bSPyun YongHyeon mii = device_get_softc(sc->sge_miibus);
1712d193ed0bSPyun YongHyeon LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
17133fcb7a53SMarius Strobl PHY_RESET(miisc);
1714d193ed0bSPyun YongHyeon error = mii_mediachg(mii);
1715d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
1716d193ed0bSPyun YongHyeon
1717d193ed0bSPyun YongHyeon return (error);
1718d193ed0bSPyun YongHyeon }
1719d193ed0bSPyun YongHyeon
1720d193ed0bSPyun YongHyeon /*
1721d193ed0bSPyun YongHyeon * Report current media status.
1722d193ed0bSPyun YongHyeon */
1723d193ed0bSPyun YongHyeon static void
sge_ifmedia_sts(if_t ifp,struct ifmediareq * ifmr)1724*e948d066SJustin Hibbits sge_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
1725d193ed0bSPyun YongHyeon {
1726d193ed0bSPyun YongHyeon struct sge_softc *sc;
1727d193ed0bSPyun YongHyeon struct mii_data *mii;
1728d193ed0bSPyun YongHyeon
1729*e948d066SJustin Hibbits sc = if_getsoftc(ifp);
1730d193ed0bSPyun YongHyeon SGE_LOCK(sc);
1731d193ed0bSPyun YongHyeon mii = device_get_softc(sc->sge_miibus);
1732*e948d066SJustin Hibbits if ((if_getflags(ifp) & IFF_UP) == 0) {
1733d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
1734d193ed0bSPyun YongHyeon return;
1735d193ed0bSPyun YongHyeon }
1736d193ed0bSPyun YongHyeon mii_pollstat(mii);
1737d193ed0bSPyun YongHyeon ifmr->ifm_active = mii->mii_media_active;
1738d193ed0bSPyun YongHyeon ifmr->ifm_status = mii->mii_media_status;
173957c81d92SPyun YongHyeon SGE_UNLOCK(sc);
1740d193ed0bSPyun YongHyeon }
1741d193ed0bSPyun YongHyeon
1742d193ed0bSPyun YongHyeon static int
sge_ioctl(if_t ifp,u_long command,caddr_t data)1743*e948d066SJustin Hibbits sge_ioctl(if_t ifp, u_long command, caddr_t data)
1744d193ed0bSPyun YongHyeon {
1745d193ed0bSPyun YongHyeon struct sge_softc *sc;
1746d193ed0bSPyun YongHyeon struct ifreq *ifr;
1747d193ed0bSPyun YongHyeon struct mii_data *mii;
1748c186cf13SPyun YongHyeon int error = 0, mask, reinit;
1749d193ed0bSPyun YongHyeon
1750*e948d066SJustin Hibbits sc = if_getsoftc(ifp);
1751d193ed0bSPyun YongHyeon ifr = (struct ifreq *)data;
1752d193ed0bSPyun YongHyeon
1753d193ed0bSPyun YongHyeon switch(command) {
1754d193ed0bSPyun YongHyeon case SIOCSIFFLAGS:
1755d193ed0bSPyun YongHyeon SGE_LOCK(sc);
1756*e948d066SJustin Hibbits if ((if_getflags(ifp) & IFF_UP) != 0) {
1757*e948d066SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0 &&
1758*e948d066SJustin Hibbits ((if_getflags(ifp) ^ sc->sge_if_flags) &
1759d193ed0bSPyun YongHyeon (IFF_PROMISC | IFF_ALLMULTI)) != 0)
1760d193ed0bSPyun YongHyeon sge_rxfilter(sc);
1761d193ed0bSPyun YongHyeon else
1762d193ed0bSPyun YongHyeon sge_init_locked(sc);
1763*e948d066SJustin Hibbits } else if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
1764d193ed0bSPyun YongHyeon sge_stop(sc);
1765*e948d066SJustin Hibbits sc->sge_if_flags = if_getflags(ifp);
1766d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
1767d193ed0bSPyun YongHyeon break;
1768d193ed0bSPyun YongHyeon case SIOCSIFCAP:
1769d193ed0bSPyun YongHyeon SGE_LOCK(sc);
1770c186cf13SPyun YongHyeon reinit = 0;
1771*e948d066SJustin Hibbits mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
1772d193ed0bSPyun YongHyeon if ((mask & IFCAP_TXCSUM) != 0 &&
1773*e948d066SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
1774*e948d066SJustin Hibbits if_togglecapenable(ifp, IFCAP_TXCSUM);
1775*e948d066SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
1776*e948d066SJustin Hibbits if_sethwassistbits(ifp, SGE_CSUM_FEATURES, 0);
1777d193ed0bSPyun YongHyeon else
1778*e948d066SJustin Hibbits if_sethwassistbits(ifp, 0, SGE_CSUM_FEATURES);
1779d193ed0bSPyun YongHyeon }
1780d193ed0bSPyun YongHyeon if ((mask & IFCAP_RXCSUM) != 0 &&
1781*e948d066SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_RXCSUM) != 0)
1782*e948d066SJustin Hibbits if_togglecapenable(ifp, IFCAP_RXCSUM);
1783c186cf13SPyun YongHyeon if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
1784*e948d066SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_VLAN_HWCSUM) != 0)
1785*e948d066SJustin Hibbits if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
178665329b31SPyun YongHyeon if ((mask & IFCAP_TSO4) != 0 &&
1787*e948d066SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_TSO4) != 0) {
1788*e948d066SJustin Hibbits if_togglecapenable(ifp, IFCAP_TSO4);
1789*e948d066SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_TSO4) != 0)
1790*e948d066SJustin Hibbits if_sethwassistbits(ifp, CSUM_TSO, 0);
179165329b31SPyun YongHyeon else
1792*e948d066SJustin Hibbits if_sethwassistbits(ifp, 0, CSUM_TSO);
179365329b31SPyun YongHyeon }
179465329b31SPyun YongHyeon if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
1795*e948d066SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_VLAN_HWTSO) != 0)
1796*e948d066SJustin Hibbits if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
1797c186cf13SPyun YongHyeon if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
1798*e948d066SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
1799c186cf13SPyun YongHyeon /*
1800c186cf13SPyun YongHyeon * Due to unknown reason, toggling VLAN hardware
1801c186cf13SPyun YongHyeon * tagging require interface reinitialization.
1802c186cf13SPyun YongHyeon */
1803*e948d066SJustin Hibbits if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
1804*e948d066SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
1805*e948d066SJustin Hibbits if_setcapenablebit(ifp, 0,
1806*e948d066SJustin Hibbits IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM);
1807c186cf13SPyun YongHyeon reinit = 1;
1808c186cf13SPyun YongHyeon }
1809*e948d066SJustin Hibbits if (reinit > 0 && (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
1810*e948d066SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
1811c186cf13SPyun YongHyeon sge_init_locked(sc);
1812c186cf13SPyun YongHyeon }
1813d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
1814c186cf13SPyun YongHyeon VLAN_CAPABILITIES(ifp);
1815d193ed0bSPyun YongHyeon break;
1816d193ed0bSPyun YongHyeon case SIOCADDMULTI:
1817d193ed0bSPyun YongHyeon case SIOCDELMULTI:
1818d193ed0bSPyun YongHyeon SGE_LOCK(sc);
1819*e948d066SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
1820d193ed0bSPyun YongHyeon sge_rxfilter(sc);
1821d193ed0bSPyun YongHyeon SGE_UNLOCK(sc);
1822d193ed0bSPyun YongHyeon break;
1823d193ed0bSPyun YongHyeon case SIOCGIFMEDIA:
1824d193ed0bSPyun YongHyeon case SIOCSIFMEDIA:
1825d193ed0bSPyun YongHyeon mii = device_get_softc(sc->sge_miibus);
1826d193ed0bSPyun YongHyeon error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1827d193ed0bSPyun YongHyeon break;
1828d193ed0bSPyun YongHyeon default:
1829d193ed0bSPyun YongHyeon error = ether_ioctl(ifp, command, data);
1830d193ed0bSPyun YongHyeon break;
1831d193ed0bSPyun YongHyeon }
1832d193ed0bSPyun YongHyeon
1833d193ed0bSPyun YongHyeon return (error);
1834d193ed0bSPyun YongHyeon }
1835d193ed0bSPyun YongHyeon
1836d193ed0bSPyun YongHyeon static void
sge_watchdog(struct sge_softc * sc)1837d193ed0bSPyun YongHyeon sge_watchdog(struct sge_softc *sc)
1838d193ed0bSPyun YongHyeon {
1839*e948d066SJustin Hibbits if_t ifp;
1840d193ed0bSPyun YongHyeon
1841d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
1842d193ed0bSPyun YongHyeon if (sc->sge_timer == 0 || --sc->sge_timer > 0)
1843d193ed0bSPyun YongHyeon return;
1844d193ed0bSPyun YongHyeon
1845d193ed0bSPyun YongHyeon ifp = sc->sge_ifp;
1846d193ed0bSPyun YongHyeon if ((sc->sge_flags & SGE_FLAG_LINK) == 0) {
1847d193ed0bSPyun YongHyeon if (1 || bootverbose)
1848d193ed0bSPyun YongHyeon device_printf(sc->sge_dev,
1849d193ed0bSPyun YongHyeon "watchdog timeout (lost link)\n");
1850c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1851*e948d066SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
1852d193ed0bSPyun YongHyeon sge_init_locked(sc);
1853d193ed0bSPyun YongHyeon return;
1854d193ed0bSPyun YongHyeon }
1855d193ed0bSPyun YongHyeon device_printf(sc->sge_dev, "watchdog timeout\n");
1856c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1857d193ed0bSPyun YongHyeon
1858*e948d066SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
1859d193ed0bSPyun YongHyeon sge_init_locked(sc);
1860*e948d066SJustin Hibbits if (!if_sendq_empty(sc->sge_ifp))
1861d193ed0bSPyun YongHyeon sge_start_locked(ifp);
1862d193ed0bSPyun YongHyeon }
1863d193ed0bSPyun YongHyeon
1864d193ed0bSPyun YongHyeon /*
1865d193ed0bSPyun YongHyeon * Stop the adapter and free any mbufs allocated to the
1866d193ed0bSPyun YongHyeon * RX and TX lists.
1867d193ed0bSPyun YongHyeon */
1868d193ed0bSPyun YongHyeon static void
sge_stop(struct sge_softc * sc)1869d193ed0bSPyun YongHyeon sge_stop(struct sge_softc *sc)
1870d193ed0bSPyun YongHyeon {
1871*e948d066SJustin Hibbits if_t ifp;
1872d193ed0bSPyun YongHyeon
1873d193ed0bSPyun YongHyeon ifp = sc->sge_ifp;
1874d193ed0bSPyun YongHyeon
1875d193ed0bSPyun YongHyeon SGE_LOCK_ASSERT(sc);
1876d193ed0bSPyun YongHyeon
1877d193ed0bSPyun YongHyeon sc->sge_timer = 0;
1878d193ed0bSPyun YongHyeon callout_stop(&sc->sge_stat_ch);
1879*e948d066SJustin Hibbits if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
1880d193ed0bSPyun YongHyeon
1881d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrMask, 0);
1882d193ed0bSPyun YongHyeon CSR_READ_4(sc, IntrMask);
1883d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrStatus, 0xffffffff);
1884d193ed0bSPyun YongHyeon /* Stop TX/RX MAC. */
1885d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, TX_CTL, 0x1a00);
1886d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, RX_CTL, 0x1a00);
1887d193ed0bSPyun YongHyeon /* XXX Can we assume active DMA cycles gone? */
1888d193ed0bSPyun YongHyeon DELAY(2000);
1889d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrMask, 0);
1890d193ed0bSPyun YongHyeon CSR_WRITE_4(sc, IntrStatus, 0xffffffff);
1891d193ed0bSPyun YongHyeon
1892d193ed0bSPyun YongHyeon sc->sge_flags &= ~SGE_FLAG_LINK;
1893d193ed0bSPyun YongHyeon sge_list_rx_free(sc);
1894d193ed0bSPyun YongHyeon sge_list_tx_free(sc);
1895d193ed0bSPyun YongHyeon }
1896