xref: /freebsd/sys/dev/msk/if_msk.c (revision 4e62c3cafa4c4e41efd6f87b7fe559cf819cf3e4)
10dbe28b3SPyun YongHyeon /******************************************************************************
20dbe28b3SPyun YongHyeon  *
30dbe28b3SPyun YongHyeon  * Name   : sky2.c
40dbe28b3SPyun YongHyeon  * Project: Gigabit Ethernet Driver for FreeBSD 5.x/6.x
50dbe28b3SPyun YongHyeon  * Version: $Revision: 1.23 $
60dbe28b3SPyun YongHyeon  * Date   : $Date: 2005/12/22 09:04:11 $
70dbe28b3SPyun YongHyeon  * Purpose: Main driver source file
80dbe28b3SPyun YongHyeon  *
90dbe28b3SPyun YongHyeon  *****************************************************************************/
100dbe28b3SPyun YongHyeon 
110dbe28b3SPyun YongHyeon /******************************************************************************
120dbe28b3SPyun YongHyeon  *
130dbe28b3SPyun YongHyeon  *	LICENSE:
140dbe28b3SPyun YongHyeon  *	Copyright (C) Marvell International Ltd. and/or its affiliates
150dbe28b3SPyun YongHyeon  *
160dbe28b3SPyun YongHyeon  *	The computer program files contained in this folder ("Files")
170dbe28b3SPyun YongHyeon  *	are provided to you under the BSD-type license terms provided
180dbe28b3SPyun YongHyeon  *	below, and any use of such Files and any derivative works
190dbe28b3SPyun YongHyeon  *	thereof created by you shall be governed by the following terms
200dbe28b3SPyun YongHyeon  *	and conditions:
210dbe28b3SPyun YongHyeon  *
220dbe28b3SPyun YongHyeon  *	- Redistributions of source code must retain the above copyright
230dbe28b3SPyun YongHyeon  *	  notice, this list of conditions and the following disclaimer.
240dbe28b3SPyun YongHyeon  *	- Redistributions in binary form must reproduce the above
250dbe28b3SPyun YongHyeon  *	  copyright notice, this list of conditions and the following
260dbe28b3SPyun YongHyeon  *	  disclaimer in the documentation and/or other materials provided
270dbe28b3SPyun YongHyeon  *	  with the distribution.
280dbe28b3SPyun YongHyeon  *	- Neither the name of Marvell nor the names of its contributors
290dbe28b3SPyun YongHyeon  *	  may be used to endorse or promote products derived from this
300dbe28b3SPyun YongHyeon  *	  software without specific prior written permission.
310dbe28b3SPyun YongHyeon  *
320dbe28b3SPyun YongHyeon  *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
330dbe28b3SPyun YongHyeon  *	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
340dbe28b3SPyun YongHyeon  *	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
350dbe28b3SPyun YongHyeon  *	FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
360dbe28b3SPyun YongHyeon  *	COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
370dbe28b3SPyun YongHyeon  *	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
380dbe28b3SPyun YongHyeon  *	BUT NOT LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR SERVICES;
390dbe28b3SPyun YongHyeon  *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
400dbe28b3SPyun YongHyeon  *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
410dbe28b3SPyun YongHyeon  *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
420dbe28b3SPyun YongHyeon  *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
430dbe28b3SPyun YongHyeon  *	OF THE POSSIBILITY OF SUCH DAMAGE.
440dbe28b3SPyun YongHyeon  *	/LICENSE
450dbe28b3SPyun YongHyeon  *
460dbe28b3SPyun YongHyeon  *****************************************************************************/
470dbe28b3SPyun YongHyeon 
480dbe28b3SPyun YongHyeon /*-
49df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause AND BSD-3-Clause
50df57947fSPedro F. Giffuni  *
510dbe28b3SPyun YongHyeon  * Copyright (c) 1997, 1998, 1999, 2000
520dbe28b3SPyun YongHyeon  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
530dbe28b3SPyun YongHyeon  *
540dbe28b3SPyun YongHyeon  * Redistribution and use in source and binary forms, with or without
550dbe28b3SPyun YongHyeon  * modification, are permitted provided that the following conditions
560dbe28b3SPyun YongHyeon  * are met:
570dbe28b3SPyun YongHyeon  * 1. Redistributions of source code must retain the above copyright
580dbe28b3SPyun YongHyeon  *    notice, this list of conditions and the following disclaimer.
590dbe28b3SPyun YongHyeon  * 2. Redistributions in binary form must reproduce the above copyright
600dbe28b3SPyun YongHyeon  *    notice, this list of conditions and the following disclaimer in the
610dbe28b3SPyun YongHyeon  *    documentation and/or other materials provided with the distribution.
620dbe28b3SPyun YongHyeon  * 3. All advertising materials mentioning features or use of this software
630dbe28b3SPyun YongHyeon  *    must display the following acknowledgement:
640dbe28b3SPyun YongHyeon  *	This product includes software developed by Bill Paul.
650dbe28b3SPyun YongHyeon  * 4. Neither the name of the author nor the names of any co-contributors
660dbe28b3SPyun YongHyeon  *    may be used to endorse or promote products derived from this software
670dbe28b3SPyun YongHyeon  *    without specific prior written permission.
680dbe28b3SPyun YongHyeon  *
690dbe28b3SPyun YongHyeon  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
700dbe28b3SPyun YongHyeon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
710dbe28b3SPyun YongHyeon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
720dbe28b3SPyun YongHyeon  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
730dbe28b3SPyun YongHyeon  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
740dbe28b3SPyun YongHyeon  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
750dbe28b3SPyun YongHyeon  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
760dbe28b3SPyun YongHyeon  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
770dbe28b3SPyun YongHyeon  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
780dbe28b3SPyun YongHyeon  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
790dbe28b3SPyun YongHyeon  * THE POSSIBILITY OF SUCH DAMAGE.
800dbe28b3SPyun YongHyeon  */
810dbe28b3SPyun YongHyeon /*-
820dbe28b3SPyun YongHyeon  * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu>
830dbe28b3SPyun YongHyeon  *
840dbe28b3SPyun YongHyeon  * Permission to use, copy, modify, and distribute this software for any
850dbe28b3SPyun YongHyeon  * purpose with or without fee is hereby granted, provided that the above
860dbe28b3SPyun YongHyeon  * copyright notice and this permission notice appear in all copies.
870dbe28b3SPyun YongHyeon  *
880dbe28b3SPyun YongHyeon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
890dbe28b3SPyun YongHyeon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
900dbe28b3SPyun YongHyeon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
910dbe28b3SPyun YongHyeon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
920dbe28b3SPyun YongHyeon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
930dbe28b3SPyun YongHyeon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
940dbe28b3SPyun YongHyeon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
950dbe28b3SPyun YongHyeon  */
960dbe28b3SPyun YongHyeon 
970dbe28b3SPyun YongHyeon /*
980dbe28b3SPyun YongHyeon  * Device driver for the Marvell Yukon II Ethernet controller.
990dbe28b3SPyun YongHyeon  * Due to lack of documentation, this driver is based on the code from
1000dbe28b3SPyun YongHyeon  * sk(4) and Marvell's myk(4) driver for FreeBSD 5.x.
1010dbe28b3SPyun YongHyeon  */
1020dbe28b3SPyun YongHyeon 
1030dbe28b3SPyun YongHyeon #include <sys/param.h>
1040dbe28b3SPyun YongHyeon #include <sys/systm.h>
1050dbe28b3SPyun YongHyeon #include <sys/bus.h>
1060dbe28b3SPyun YongHyeon #include <sys/endian.h>
1070dbe28b3SPyun YongHyeon #include <sys/mbuf.h>
1080dbe28b3SPyun YongHyeon #include <sys/malloc.h>
1090dbe28b3SPyun YongHyeon #include <sys/kernel.h>
1100dbe28b3SPyun YongHyeon #include <sys/module.h>
1110dbe28b3SPyun YongHyeon #include <sys/socket.h>
1120dbe28b3SPyun YongHyeon #include <sys/sockio.h>
1130dbe28b3SPyun YongHyeon #include <sys/queue.h>
1140dbe28b3SPyun YongHyeon #include <sys/sysctl.h>
1150dbe28b3SPyun YongHyeon 
1160dbe28b3SPyun YongHyeon #include <net/bpf.h>
1170dbe28b3SPyun YongHyeon #include <net/ethernet.h>
1180dbe28b3SPyun YongHyeon #include <net/if.h>
11976039bc8SGleb Smirnoff #include <net/if_var.h>
12067784314SPoul-Henning Kamp #include <net/if_arp.h>
1210dbe28b3SPyun YongHyeon #include <net/if_dl.h>
1220dbe28b3SPyun YongHyeon #include <net/if_media.h>
1230dbe28b3SPyun YongHyeon #include <net/if_types.h>
1240dbe28b3SPyun YongHyeon #include <net/if_vlan_var.h>
1250dbe28b3SPyun YongHyeon 
1260dbe28b3SPyun YongHyeon #include <netinet/in.h>
12767784314SPoul-Henning Kamp #include <netinet/in_systm.h>
1280dbe28b3SPyun YongHyeon #include <netinet/ip.h>
1290dbe28b3SPyun YongHyeon #include <netinet/tcp.h>
13067784314SPoul-Henning Kamp #include <netinet/udp.h>
1310dbe28b3SPyun YongHyeon 
1320dbe28b3SPyun YongHyeon #include <machine/bus.h>
133b5898b80SPyun YongHyeon #include <machine/in_cksum.h>
1340dbe28b3SPyun YongHyeon #include <machine/resource.h>
1350dbe28b3SPyun YongHyeon #include <sys/rman.h>
1360dbe28b3SPyun YongHyeon 
13767784314SPoul-Henning Kamp #include <dev/mii/mii.h>
1380dbe28b3SPyun YongHyeon #include <dev/mii/miivar.h>
1390dbe28b3SPyun YongHyeon 
1400dbe28b3SPyun YongHyeon #include <dev/pci/pcireg.h>
1410dbe28b3SPyun YongHyeon #include <dev/pci/pcivar.h>
1420dbe28b3SPyun YongHyeon 
1430dbe28b3SPyun YongHyeon #include <dev/msk/if_mskreg.h>
1440dbe28b3SPyun YongHyeon 
1450dbe28b3SPyun YongHyeon MODULE_DEPEND(msk, pci, 1, 1, 1);
1460dbe28b3SPyun YongHyeon MODULE_DEPEND(msk, ether, 1, 1, 1);
1470dbe28b3SPyun YongHyeon MODULE_DEPEND(msk, miibus, 1, 1, 1);
1480dbe28b3SPyun YongHyeon 
1490dbe28b3SPyun YongHyeon /* "device miibus" required.  See GENERIC if you get errors here. */
1500dbe28b3SPyun YongHyeon #include "miibus_if.h"
1510dbe28b3SPyun YongHyeon 
1520dbe28b3SPyun YongHyeon /* Tunables. */
1530dbe28b3SPyun YongHyeon static int msi_disable = 0;
1540dbe28b3SPyun YongHyeon TUNABLE_INT("hw.msk.msi_disable", &msi_disable);
15553dcfbd1SPyun YongHyeon static int legacy_intr = 0;
15653dcfbd1SPyun YongHyeon TUNABLE_INT("hw.msk.legacy_intr", &legacy_intr);
15785b340cbSPyun YongHyeon static int jumbo_disable = 0;
15885b340cbSPyun YongHyeon TUNABLE_INT("hw.msk.jumbo_disable", &jumbo_disable);
1590dbe28b3SPyun YongHyeon 
1600dbe28b3SPyun YongHyeon #define MSK_CSUM_FEATURES	(CSUM_TCP | CSUM_UDP)
1610dbe28b3SPyun YongHyeon 
1620dbe28b3SPyun YongHyeon /*
1630dbe28b3SPyun YongHyeon  * Devices supported by this driver.
1640dbe28b3SPyun YongHyeon  */
1652dc26832SMarius Strobl static const struct msk_product {
1660dbe28b3SPyun YongHyeon 	uint16_t	msk_vendorid;
1670dbe28b3SPyun YongHyeon 	uint16_t	msk_deviceid;
1680dbe28b3SPyun YongHyeon 	const char	*msk_name;
1690dbe28b3SPyun YongHyeon } msk_products[] = {
1700dbe28b3SPyun YongHyeon 	{ VENDORID_SK, DEVICEID_SK_YUKON2,
1710dbe28b3SPyun YongHyeon 	    "SK-9Sxx Gigabit Ethernet" },
1720dbe28b3SPyun YongHyeon 	{ VENDORID_SK, DEVICEID_SK_YUKON2_EXPR,
1730dbe28b3SPyun YongHyeon 	    "SK-9Exx Gigabit Ethernet"},
1740dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8021CU,
1750dbe28b3SPyun YongHyeon 	    "Marvell Yukon 88E8021CU Gigabit Ethernet" },
1760dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8021X,
1770dbe28b3SPyun YongHyeon 	    "Marvell Yukon 88E8021 SX/LX Gigabit Ethernet" },
1780dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8022CU,
1790dbe28b3SPyun YongHyeon 	    "Marvell Yukon 88E8022CU Gigabit Ethernet" },
1800dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8022X,
1810dbe28b3SPyun YongHyeon 	    "Marvell Yukon 88E8022 SX/LX Gigabit Ethernet" },
1820dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8061CU,
1830dbe28b3SPyun YongHyeon 	    "Marvell Yukon 88E8061CU Gigabit Ethernet" },
1840dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8061X,
1850dbe28b3SPyun YongHyeon 	    "Marvell Yukon 88E8061 SX/LX Gigabit Ethernet" },
1860dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8062CU,
1870dbe28b3SPyun YongHyeon 	    "Marvell Yukon 88E8062CU Gigabit Ethernet" },
1880dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8062X,
1890dbe28b3SPyun YongHyeon 	    "Marvell Yukon 88E8062 SX/LX Gigabit Ethernet" },
1900dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8035,
191f972d4c6SPyun YongHyeon 	    "Marvell Yukon 88E8035 Fast Ethernet" },
1920dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8036,
193f972d4c6SPyun YongHyeon 	    "Marvell Yukon 88E8036 Fast Ethernet" },
1940dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8038,
195f972d4c6SPyun YongHyeon 	    "Marvell Yukon 88E8038 Fast Ethernet" },
19628d34c0eSRemko Lodder 	{ VENDORID_MARVELL, DEVICEID_MRVL_8039,
197f972d4c6SPyun YongHyeon 	    "Marvell Yukon 88E8039 Fast Ethernet" },
19812909985SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8040,
19912909985SPyun YongHyeon 	    "Marvell Yukon 88E8040 Fast Ethernet" },
20012909985SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8040T,
20112909985SPyun YongHyeon 	    "Marvell Yukon 88E8040T Fast Ethernet" },
2020e0ed74fSUlf Lilleengen 	{ VENDORID_MARVELL, DEVICEID_MRVL_8042,
2030e0ed74fSUlf Lilleengen 	    "Marvell Yukon 88E8042 Fast Ethernet" },
20412909985SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_8048,
20512909985SPyun YongHyeon 	    "Marvell Yukon 88E8048 Fast Ethernet" },
2060dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_4361,
2070dbe28b3SPyun YongHyeon 	    "Marvell Yukon 88E8050 Gigabit Ethernet" },
2080dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_4360,
2090dbe28b3SPyun YongHyeon 	    "Marvell Yukon 88E8052 Gigabit Ethernet" },
2100dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_4362,
2110dbe28b3SPyun YongHyeon 	    "Marvell Yukon 88E8053 Gigabit Ethernet" },
2120dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_4363,
2130dbe28b3SPyun YongHyeon 	    "Marvell Yukon 88E8055 Gigabit Ethernet" },
2140dbe28b3SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_4364,
2150dbe28b3SPyun YongHyeon 	    "Marvell Yukon 88E8056 Gigabit Ethernet" },
216a56fe1f0SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_4365,
217a56fe1f0SPyun YongHyeon 	    "Marvell Yukon 88E8070 Gigabit Ethernet" },
21875ef16dfSPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_436A,
21975ef16dfSPyun YongHyeon 	    "Marvell Yukon 88E8058 Gigabit Ethernet" },
220a56fe1f0SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_436B,
221a56fe1f0SPyun YongHyeon 	    "Marvell Yukon 88E8071 Gigabit Ethernet" },
222a56fe1f0SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_436C,
223a56fe1f0SPyun YongHyeon 	    "Marvell Yukon 88E8072 Gigabit Ethernet" },
224e0029a72SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_436D,
225e0029a72SPyun YongHyeon 	    "Marvell Yukon 88E8055 Gigabit Ethernet" },
226e0029a72SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_4370,
227e0029a72SPyun YongHyeon 	    "Marvell Yukon 88E8075 Gigabit Ethernet" },
22876202a16SPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_4380,
22976202a16SPyun YongHyeon 	    "Marvell Yukon 88E8057 Gigabit Ethernet" },
230e19bd6eeSPyun YongHyeon 	{ VENDORID_MARVELL, DEVICEID_MRVL_4381,
231e19bd6eeSPyun YongHyeon 	    "Marvell Yukon 88E8059 Gigabit Ethernet" },
2320dbe28b3SPyun YongHyeon 	{ VENDORID_DLINK, DEVICEID_DLINK_DGE550SX,
2330dbe28b3SPyun YongHyeon 	    "D-Link 550SX Gigabit Ethernet" },
23460d3251aSPyun YongHyeon 	{ VENDORID_DLINK, DEVICEID_DLINK_DGE560SX,
23560d3251aSPyun YongHyeon 	    "D-Link 560SX Gigabit Ethernet" },
2360dbe28b3SPyun YongHyeon 	{ VENDORID_DLINK, DEVICEID_DLINK_DGE560T,
2370dbe28b3SPyun YongHyeon 	    "D-Link 560T Gigabit Ethernet" }
2380dbe28b3SPyun YongHyeon };
2390dbe28b3SPyun YongHyeon 
2400dbe28b3SPyun YongHyeon static const char *model_name[] = {
2410dbe28b3SPyun YongHyeon 	"Yukon XL",
2420dbe28b3SPyun YongHyeon         "Yukon EC Ultra",
243daf29227SPyun YongHyeon         "Yukon EX",
2440dbe28b3SPyun YongHyeon         "Yukon EC",
24561708f4cSPyun YongHyeon         "Yukon FE",
24676202a16SPyun YongHyeon         "Yukon FE+",
24776202a16SPyun YongHyeon         "Yukon Supreme",
248e19bd6eeSPyun YongHyeon         "Yukon Ultra 2",
249e19bd6eeSPyun YongHyeon         "Yukon Unknown",
250e19bd6eeSPyun YongHyeon         "Yukon Optima",
2510dbe28b3SPyun YongHyeon };
2520dbe28b3SPyun YongHyeon 
2530dbe28b3SPyun YongHyeon static int mskc_probe(device_t);
2540dbe28b3SPyun YongHyeon static int mskc_attach(device_t);
2552ff5c850SJohn Baldwin static void mskc_child_deleted(device_t, device_t);
2560dbe28b3SPyun YongHyeon static int mskc_detach(device_t);
2576a087a87SPyun YongHyeon static int mskc_shutdown(device_t);
2580dbe28b3SPyun YongHyeon static int mskc_setup_rambuffer(struct msk_softc *);
2590dbe28b3SPyun YongHyeon static int mskc_suspend(device_t);
2600dbe28b3SPyun YongHyeon static int mskc_resume(device_t);
2612dc26832SMarius Strobl static bus_dma_tag_t mskc_get_dma_tag(device_t, device_t);
2620dbe28b3SPyun YongHyeon static void mskc_reset(struct msk_softc *);
2630dbe28b3SPyun YongHyeon 
2640dbe28b3SPyun YongHyeon static int msk_probe(device_t);
2650dbe28b3SPyun YongHyeon static int msk_attach(device_t);
2660dbe28b3SPyun YongHyeon static int msk_detach(device_t);
2670dbe28b3SPyun YongHyeon 
2680dbe28b3SPyun YongHyeon static void msk_tick(void *);
269c876b43fSPyun YongHyeon static void msk_intr(void *);
2700dbe28b3SPyun YongHyeon static void msk_intr_phy(struct msk_if_softc *);
2710dbe28b3SPyun YongHyeon static void msk_intr_gmac(struct msk_if_softc *);
2720dbe28b3SPyun YongHyeon static __inline void msk_rxput(struct msk_if_softc *);
2730dbe28b3SPyun YongHyeon static int msk_handle_events(struct msk_softc *);
2740dbe28b3SPyun YongHyeon static void msk_handle_hwerr(struct msk_if_softc *, uint32_t);
2750dbe28b3SPyun YongHyeon static void msk_intr_hwerr(struct msk_softc *);
27683c04c93SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
27783c04c93SPyun YongHyeon static __inline void msk_fixup_rx(struct mbuf *);
27883c04c93SPyun YongHyeon #endif
279388214e4SPyun YongHyeon static __inline void msk_rxcsum(struct msk_if_softc *, uint32_t, struct mbuf *);
280efb74172SPyun YongHyeon static void msk_rxeof(struct msk_if_softc *, uint32_t, uint32_t, int);
281efb74172SPyun YongHyeon static void msk_jumbo_rxeof(struct msk_if_softc *, uint32_t, uint32_t, int);
2820dbe28b3SPyun YongHyeon static void msk_txeof(struct msk_if_softc *, int);
2830dbe28b3SPyun YongHyeon static int msk_encap(struct msk_if_softc *, struct mbuf **);
2845ab8c4b8SJustin Hibbits static void msk_start(if_t);
2855ab8c4b8SJustin Hibbits static void msk_start_locked(if_t);
2865ab8c4b8SJustin Hibbits static int msk_ioctl(if_t, u_long, caddr_t);
2870dbe28b3SPyun YongHyeon static void msk_set_prefetch(struct msk_softc *, int, bus_addr_t, uint32_t);
2880dbe28b3SPyun YongHyeon static void msk_set_rambuffer(struct msk_if_softc *);
289efb74172SPyun YongHyeon static void msk_set_tx_stfwd(struct msk_if_softc *);
2900dbe28b3SPyun YongHyeon static void msk_init(void *);
2910dbe28b3SPyun YongHyeon static void msk_init_locked(struct msk_if_softc *);
2920dbe28b3SPyun YongHyeon static void msk_stop(struct msk_if_softc *);
2932271eac7SPyun YongHyeon static void msk_watchdog(struct msk_if_softc *);
2945ab8c4b8SJustin Hibbits static int msk_mediachange(if_t);
2955ab8c4b8SJustin Hibbits static void msk_mediastatus(if_t, struct ifmediareq *);
2960dbe28b3SPyun YongHyeon static void msk_phy_power(struct msk_softc *, int);
2970dbe28b3SPyun YongHyeon static void msk_dmamap_cb(void *, bus_dma_segment_t *, int, int);
2980dbe28b3SPyun YongHyeon static int msk_status_dma_alloc(struct msk_softc *);
2990dbe28b3SPyun YongHyeon static void msk_status_dma_free(struct msk_softc *);
3000dbe28b3SPyun YongHyeon static int msk_txrx_dma_alloc(struct msk_if_softc *);
30185b340cbSPyun YongHyeon static int msk_rx_dma_jalloc(struct msk_if_softc *);
3020dbe28b3SPyun YongHyeon static void msk_txrx_dma_free(struct msk_if_softc *);
30385b340cbSPyun YongHyeon static void msk_rx_dma_jfree(struct msk_if_softc *);
304388214e4SPyun YongHyeon static int msk_rx_fill(struct msk_if_softc *, int);
3050dbe28b3SPyun YongHyeon static int msk_init_rx_ring(struct msk_if_softc *);
3060dbe28b3SPyun YongHyeon static int msk_init_jumbo_rx_ring(struct msk_if_softc *);
3070dbe28b3SPyun YongHyeon static void msk_init_tx_ring(struct msk_if_softc *);
3080dbe28b3SPyun YongHyeon static __inline void msk_discard_rxbuf(struct msk_if_softc *, int);
3090dbe28b3SPyun YongHyeon static __inline void msk_discard_jumbo_rxbuf(struct msk_if_softc *, int);
3100dbe28b3SPyun YongHyeon static int msk_newbuf(struct msk_if_softc *, int);
3110dbe28b3SPyun YongHyeon static int msk_jumbo_newbuf(struct msk_if_softc *, int);
3120dbe28b3SPyun YongHyeon 
3130dbe28b3SPyun YongHyeon static int msk_phy_readreg(struct msk_if_softc *, int, int);
3140dbe28b3SPyun YongHyeon static int msk_phy_writereg(struct msk_if_softc *, int, int, int);
3150dbe28b3SPyun YongHyeon static int msk_miibus_readreg(device_t, int, int);
3160dbe28b3SPyun YongHyeon static int msk_miibus_writereg(device_t, int, int, int);
3170dbe28b3SPyun YongHyeon static void msk_miibus_statchg(device_t);
3180dbe28b3SPyun YongHyeon 
3196d6588a1SPyun YongHyeon static void msk_rxfilter(struct msk_if_softc *);
3205ab8c4b8SJustin Hibbits static void msk_setvlan(struct msk_if_softc *, if_t);
3210dbe28b3SPyun YongHyeon 
3223a91ee71SPyun YongHyeon static void msk_stats_clear(struct msk_if_softc *);
3233a91ee71SPyun YongHyeon static void msk_stats_update(struct msk_if_softc *);
3243a91ee71SPyun YongHyeon static int msk_sysctl_stat32(SYSCTL_HANDLER_ARGS);
3253a91ee71SPyun YongHyeon static int msk_sysctl_stat64(SYSCTL_HANDLER_ARGS);
3263a91ee71SPyun YongHyeon static void msk_sysctl_node(struct msk_if_softc *);
3270dbe28b3SPyun YongHyeon static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
3280dbe28b3SPyun YongHyeon static int sysctl_hw_msk_proc_limit(SYSCTL_HANDLER_ARGS);
3290dbe28b3SPyun YongHyeon 
3300dbe28b3SPyun YongHyeon static device_method_t mskc_methods[] = {
3310dbe28b3SPyun YongHyeon 	/* Device interface */
3320dbe28b3SPyun YongHyeon 	DEVMETHOD(device_probe,		mskc_probe),
3330dbe28b3SPyun YongHyeon 	DEVMETHOD(device_attach,	mskc_attach),
3340dbe28b3SPyun YongHyeon 	DEVMETHOD(device_detach,	mskc_detach),
3350dbe28b3SPyun YongHyeon 	DEVMETHOD(device_suspend,	mskc_suspend),
3360dbe28b3SPyun YongHyeon 	DEVMETHOD(device_resume,	mskc_resume),
3370dbe28b3SPyun YongHyeon 	DEVMETHOD(device_shutdown,	mskc_shutdown),
3380dbe28b3SPyun YongHyeon 
3392ff5c850SJohn Baldwin 	DEVMETHOD(bus_child_deleted,	mskc_child_deleted),
3402dc26832SMarius Strobl 	DEVMETHOD(bus_get_dma_tag,	mskc_get_dma_tag),
3412dc26832SMarius Strobl 
3424b7ec270SMarius Strobl 	DEVMETHOD_END
3430dbe28b3SPyun YongHyeon };
3440dbe28b3SPyun YongHyeon 
3450dbe28b3SPyun YongHyeon static driver_t mskc_driver = {
3460dbe28b3SPyun YongHyeon 	"mskc",
3470dbe28b3SPyun YongHyeon 	mskc_methods,
3480dbe28b3SPyun YongHyeon 	sizeof(struct msk_softc)
3490dbe28b3SPyun YongHyeon };
3500dbe28b3SPyun YongHyeon 
3510dbe28b3SPyun YongHyeon static device_method_t msk_methods[] = {
3520dbe28b3SPyun YongHyeon 	/* Device interface */
3530dbe28b3SPyun YongHyeon 	DEVMETHOD(device_probe,		msk_probe),
3540dbe28b3SPyun YongHyeon 	DEVMETHOD(device_attach,	msk_attach),
3550dbe28b3SPyun YongHyeon 	DEVMETHOD(device_detach,	msk_detach),
3560dbe28b3SPyun YongHyeon 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
3570dbe28b3SPyun YongHyeon 
3580dbe28b3SPyun YongHyeon 	/* MII interface */
3590dbe28b3SPyun YongHyeon 	DEVMETHOD(miibus_readreg,	msk_miibus_readreg),
3600dbe28b3SPyun YongHyeon 	DEVMETHOD(miibus_writereg,	msk_miibus_writereg),
3610dbe28b3SPyun YongHyeon 	DEVMETHOD(miibus_statchg,	msk_miibus_statchg),
3620dbe28b3SPyun YongHyeon 
3634b7ec270SMarius Strobl 	DEVMETHOD_END
3640dbe28b3SPyun YongHyeon };
3650dbe28b3SPyun YongHyeon 
3660dbe28b3SPyun YongHyeon static driver_t msk_driver = {
3670dbe28b3SPyun YongHyeon 	"msk",
3680dbe28b3SPyun YongHyeon 	msk_methods,
3690dbe28b3SPyun YongHyeon 	sizeof(struct msk_if_softc)
3700dbe28b3SPyun YongHyeon };
3710dbe28b3SPyun YongHyeon 
3727fcc3449SJohn Baldwin DRIVER_MODULE(mskc, pci, mskc_driver, NULL, NULL);
3737fcc3449SJohn Baldwin DRIVER_MODULE(msk, mskc, msk_driver, NULL, NULL);
3743e38757dSJohn Baldwin DRIVER_MODULE(miibus, msk, miibus_driver, NULL, NULL);
3750dbe28b3SPyun YongHyeon 
3760dbe28b3SPyun YongHyeon static struct resource_spec msk_res_spec_io[] = {
3770dbe28b3SPyun YongHyeon 	{ SYS_RES_IOPORT,	PCIR_BAR(1),	RF_ACTIVE },
3780dbe28b3SPyun YongHyeon 	{ -1,			0,		0 }
3790dbe28b3SPyun YongHyeon };
3800dbe28b3SPyun YongHyeon 
3810dbe28b3SPyun YongHyeon static struct resource_spec msk_res_spec_mem[] = {
3820dbe28b3SPyun YongHyeon 	{ SYS_RES_MEMORY,	PCIR_BAR(0),	RF_ACTIVE },
383298946a9SPyun YongHyeon 	{ -1,			0,		0 }
384298946a9SPyun YongHyeon };
385298946a9SPyun YongHyeon 
386298946a9SPyun YongHyeon static struct resource_spec msk_irq_spec_legacy[] = {
3870dbe28b3SPyun YongHyeon 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
3880dbe28b3SPyun YongHyeon 	{ -1,			0,		0 }
3890dbe28b3SPyun YongHyeon };
3900dbe28b3SPyun YongHyeon 
391298946a9SPyun YongHyeon static struct resource_spec msk_irq_spec_msi[] = {
392298946a9SPyun YongHyeon 	{ SYS_RES_IRQ,		1,		RF_ACTIVE },
3938463d7a0SPyun YongHyeon 	{ -1,			0,		0 }
3948463d7a0SPyun YongHyeon };
3958463d7a0SPyun YongHyeon 
3960dbe28b3SPyun YongHyeon static int
msk_miibus_readreg(device_t dev,int phy,int reg)3970dbe28b3SPyun YongHyeon msk_miibus_readreg(device_t dev, int phy, int reg)
3980dbe28b3SPyun YongHyeon {
3990dbe28b3SPyun YongHyeon 	struct msk_if_softc *sc_if;
4000dbe28b3SPyun YongHyeon 
4010dbe28b3SPyun YongHyeon 	sc_if = device_get_softc(dev);
4020dbe28b3SPyun YongHyeon 
4030dbe28b3SPyun YongHyeon 	return (msk_phy_readreg(sc_if, phy, reg));
4040dbe28b3SPyun YongHyeon }
4050dbe28b3SPyun YongHyeon 
4060dbe28b3SPyun YongHyeon static int
msk_phy_readreg(struct msk_if_softc * sc_if,int phy,int reg)4070dbe28b3SPyun YongHyeon msk_phy_readreg(struct msk_if_softc *sc_if, int phy, int reg)
4080dbe28b3SPyun YongHyeon {
4090dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
4100dbe28b3SPyun YongHyeon 	int i, val;
4110dbe28b3SPyun YongHyeon 
4120dbe28b3SPyun YongHyeon 	sc = sc_if->msk_softc;
4130dbe28b3SPyun YongHyeon 
4140dbe28b3SPyun YongHyeon         GMAC_WRITE_2(sc, sc_if->msk_port, GM_SMI_CTRL,
4150dbe28b3SPyun YongHyeon 	    GM_SMI_CT_PHY_AD(phy) | GM_SMI_CT_REG_AD(reg) | GM_SMI_CT_OP_RD);
4160dbe28b3SPyun YongHyeon 
4170dbe28b3SPyun YongHyeon 	for (i = 0; i < MSK_TIMEOUT; i++) {
4180dbe28b3SPyun YongHyeon 		DELAY(1);
4190dbe28b3SPyun YongHyeon 		val = GMAC_READ_2(sc, sc_if->msk_port, GM_SMI_CTRL);
4200dbe28b3SPyun YongHyeon 		if ((val & GM_SMI_CT_RD_VAL) != 0) {
4210dbe28b3SPyun YongHyeon 			val = GMAC_READ_2(sc, sc_if->msk_port, GM_SMI_DATA);
4220dbe28b3SPyun YongHyeon 			break;
4230dbe28b3SPyun YongHyeon 		}
4240dbe28b3SPyun YongHyeon 	}
4250dbe28b3SPyun YongHyeon 
4260dbe28b3SPyun YongHyeon 	if (i == MSK_TIMEOUT) {
4270dbe28b3SPyun YongHyeon 		if_printf(sc_if->msk_ifp, "phy failed to come ready\n");
4280dbe28b3SPyun YongHyeon 		val = 0;
4290dbe28b3SPyun YongHyeon 	}
4300dbe28b3SPyun YongHyeon 
4310dbe28b3SPyun YongHyeon 	return (val);
4320dbe28b3SPyun YongHyeon }
4330dbe28b3SPyun YongHyeon 
4340dbe28b3SPyun YongHyeon static int
msk_miibus_writereg(device_t dev,int phy,int reg,int val)4350dbe28b3SPyun YongHyeon msk_miibus_writereg(device_t dev, int phy, int reg, int val)
4360dbe28b3SPyun YongHyeon {
4370dbe28b3SPyun YongHyeon 	struct msk_if_softc *sc_if;
4380dbe28b3SPyun YongHyeon 
4390dbe28b3SPyun YongHyeon 	sc_if = device_get_softc(dev);
4400dbe28b3SPyun YongHyeon 
4410dbe28b3SPyun YongHyeon 	return (msk_phy_writereg(sc_if, phy, reg, val));
4420dbe28b3SPyun YongHyeon }
4430dbe28b3SPyun YongHyeon 
4440dbe28b3SPyun YongHyeon static int
msk_phy_writereg(struct msk_if_softc * sc_if,int phy,int reg,int val)4450dbe28b3SPyun YongHyeon msk_phy_writereg(struct msk_if_softc *sc_if, int phy, int reg, int val)
4460dbe28b3SPyun YongHyeon {
4470dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
4480dbe28b3SPyun YongHyeon 	int i;
4490dbe28b3SPyun YongHyeon 
4500dbe28b3SPyun YongHyeon 	sc = sc_if->msk_softc;
4510dbe28b3SPyun YongHyeon 
4520dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_SMI_DATA, val);
4530dbe28b3SPyun YongHyeon         GMAC_WRITE_2(sc, sc_if->msk_port, GM_SMI_CTRL,
4540dbe28b3SPyun YongHyeon 	    GM_SMI_CT_PHY_AD(phy) | GM_SMI_CT_REG_AD(reg));
4550dbe28b3SPyun YongHyeon 	for (i = 0; i < MSK_TIMEOUT; i++) {
4560dbe28b3SPyun YongHyeon 		DELAY(1);
4570dbe28b3SPyun YongHyeon 		if ((GMAC_READ_2(sc, sc_if->msk_port, GM_SMI_CTRL) &
4580dbe28b3SPyun YongHyeon 		    GM_SMI_CT_BUSY) == 0)
4590dbe28b3SPyun YongHyeon 			break;
4600dbe28b3SPyun YongHyeon 	}
4610dbe28b3SPyun YongHyeon 	if (i == MSK_TIMEOUT)
4620dbe28b3SPyun YongHyeon 		if_printf(sc_if->msk_ifp, "phy write timeout\n");
4630dbe28b3SPyun YongHyeon 
4640dbe28b3SPyun YongHyeon 	return (0);
4650dbe28b3SPyun YongHyeon }
4660dbe28b3SPyun YongHyeon 
4670dbe28b3SPyun YongHyeon static void
msk_miibus_statchg(device_t dev)4680dbe28b3SPyun YongHyeon msk_miibus_statchg(device_t dev)
4690dbe28b3SPyun YongHyeon {
4700dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
4710dbe28b3SPyun YongHyeon 	struct msk_if_softc *sc_if;
4720dbe28b3SPyun YongHyeon 	struct mii_data *mii;
4735ab8c4b8SJustin Hibbits 	if_t ifp;
474bf59599fSPyun YongHyeon 	uint32_t gmac;
4750dbe28b3SPyun YongHyeon 
47619585f45SPyun YongHyeon 	sc_if = device_get_softc(dev);
4770dbe28b3SPyun YongHyeon 	sc = sc_if->msk_softc;
4780dbe28b3SPyun YongHyeon 
4794b76fe63SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
4800dbe28b3SPyun YongHyeon 
4810dbe28b3SPyun YongHyeon 	mii = device_get_softc(sc_if->msk_miibus);
4820dbe28b3SPyun YongHyeon 	ifp = sc_if->msk_ifp;
48319585f45SPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
4845ab8c4b8SJustin Hibbits 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
4850dbe28b3SPyun YongHyeon 		return;
4860dbe28b3SPyun YongHyeon 
487ab7df1e4SPyun YongHyeon 	sc_if->msk_flags &= ~MSK_FLAG_LINK;
4886c4d62e1SPyun YongHyeon 	if ((mii->mii_media_status & (IFM_AVALID | IFM_ACTIVE)) ==
4896c4d62e1SPyun YongHyeon 	    (IFM_AVALID | IFM_ACTIVE)) {
4906c4d62e1SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
4916c4d62e1SPyun YongHyeon 		case IFM_10_T:
4926c4d62e1SPyun YongHyeon 		case IFM_100_TX:
4936c4d62e1SPyun YongHyeon 			sc_if->msk_flags |= MSK_FLAG_LINK;
4946c4d62e1SPyun YongHyeon 			break;
4956c4d62e1SPyun YongHyeon 		case IFM_1000_T:
4966c4d62e1SPyun YongHyeon 		case IFM_1000_SX:
4976c4d62e1SPyun YongHyeon 		case IFM_1000_LX:
4986c4d62e1SPyun YongHyeon 		case IFM_1000_CX:
4996c4d62e1SPyun YongHyeon 			if ((sc_if->msk_flags & MSK_FLAG_FASTETHER) == 0)
5006c4d62e1SPyun YongHyeon 				sc_if->msk_flags |= MSK_FLAG_LINK;
5016c4d62e1SPyun YongHyeon 			break;
5026c4d62e1SPyun YongHyeon 		default:
5036c4d62e1SPyun YongHyeon 			break;
5046c4d62e1SPyun YongHyeon 		}
5056c4d62e1SPyun YongHyeon 	}
5060dbe28b3SPyun YongHyeon 
507ab7df1e4SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_LINK) != 0) {
5080dbe28b3SPyun YongHyeon 		/* Enable Tx FIFO Underrun. */
5090dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, MR_ADDR(sc_if->msk_port, GMAC_IRQ_MSK),
5100dbe28b3SPyun YongHyeon 		    GM_IS_TX_FF_UR | GM_IS_RX_FF_OR);
511bf59599fSPyun YongHyeon 		/*
512bf59599fSPyun YongHyeon 		 * Because mii(4) notify msk(4) that it detected link status
513bf59599fSPyun YongHyeon 		 * change, there is no need to enable automatic
514bf59599fSPyun YongHyeon 		 * speed/flow-control/duplex updates.
515bf59599fSPyun YongHyeon 		 */
516bf59599fSPyun YongHyeon 		gmac = GM_GPCR_AU_ALL_DIS;
5170dbe28b3SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
5180dbe28b3SPyun YongHyeon 		case IFM_1000_SX:
5190dbe28b3SPyun YongHyeon 		case IFM_1000_T:
5200dbe28b3SPyun YongHyeon 			gmac |= GM_GPCR_SPEED_1000;
5210dbe28b3SPyun YongHyeon 			break;
5220dbe28b3SPyun YongHyeon 		case IFM_100_TX:
5230dbe28b3SPyun YongHyeon 			gmac |= GM_GPCR_SPEED_100;
5240dbe28b3SPyun YongHyeon 			break;
5250dbe28b3SPyun YongHyeon 		case IFM_10_T:
5260dbe28b3SPyun YongHyeon 			break;
5270dbe28b3SPyun YongHyeon 		}
5280dbe28b3SPyun YongHyeon 
529efd4fc3fSMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
530efd4fc3fSMarius Strobl 		    IFM_ETH_RXPAUSE) == 0)
531bf59599fSPyun YongHyeon 			gmac |= GM_GPCR_FC_RX_DIS;
532efd4fc3fSMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
533efd4fc3fSMarius Strobl 		     IFM_ETH_TXPAUSE) == 0)
534bf59599fSPyun YongHyeon 			gmac |= GM_GPCR_FC_TX_DIS;
53542f3ea9fSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
53642f3ea9fSPyun YongHyeon 			gmac |= GM_GPCR_DUP_FULL;
53742f3ea9fSPyun YongHyeon 		else
53842f3ea9fSPyun YongHyeon 			gmac |= GM_GPCR_FC_RX_DIS | GM_GPCR_FC_TX_DIS;
5390dbe28b3SPyun YongHyeon 		gmac |= GM_GPCR_RX_ENA | GM_GPCR_TX_ENA;
5400dbe28b3SPyun YongHyeon 		GMAC_WRITE_2(sc, sc_if->msk_port, GM_GP_CTRL, gmac);
5410dbe28b3SPyun YongHyeon 		/* Read again to ensure writing. */
5420dbe28b3SPyun YongHyeon 		GMAC_READ_2(sc, sc_if->msk_port, GM_GP_CTRL);
54342f3ea9fSPyun YongHyeon 		gmac = GMC_PAUSE_OFF;
54442f3ea9fSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
545efd4fc3fSMarius Strobl 			if ((IFM_OPTIONS(mii->mii_media_active) &
546efd4fc3fSMarius Strobl 			    IFM_ETH_RXPAUSE) != 0)
5470dbe28b3SPyun YongHyeon 				gmac = GMC_PAUSE_ON;
54842f3ea9fSPyun YongHyeon 		}
5490dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, GMAC_CTRL), gmac);
5500dbe28b3SPyun YongHyeon 
5510dbe28b3SPyun YongHyeon 		/* Enable PHY interrupt for FIFO underrun/overflow. */
5520dbe28b3SPyun YongHyeon 		msk_phy_writereg(sc_if, PHY_ADDR_MARV,
5530dbe28b3SPyun YongHyeon 		    PHY_MARV_INT_MASK, PHY_M_IS_FIFO_ERROR);
5540dbe28b3SPyun YongHyeon 	} else {
5550dbe28b3SPyun YongHyeon 		/*
5560dbe28b3SPyun YongHyeon 		 * Link state changed to down.
5570dbe28b3SPyun YongHyeon 		 * Disable PHY interrupts.
5580dbe28b3SPyun YongHyeon 		 */
559431e606dSPyun YongHyeon 		msk_phy_writereg(sc_if, PHY_ADDR_MARV, PHY_MARV_INT_MASK, 0);
5600dbe28b3SPyun YongHyeon 		/* Disable Rx/Tx MAC. */
561bf59599fSPyun YongHyeon 		gmac = GMAC_READ_2(sc, sc_if->msk_port, GM_GP_CTRL);
5627c017a71SPyun YongHyeon 		if ((gmac & (GM_GPCR_RX_ENA | GM_GPCR_TX_ENA)) != 0) {
5630dbe28b3SPyun YongHyeon 			gmac &= ~(GM_GPCR_RX_ENA | GM_GPCR_TX_ENA);
5640dbe28b3SPyun YongHyeon 			GMAC_WRITE_2(sc, sc_if->msk_port, GM_GP_CTRL, gmac);
5650dbe28b3SPyun YongHyeon 			/* Read again to ensure writing. */
5660dbe28b3SPyun YongHyeon 			GMAC_READ_2(sc, sc_if->msk_port, GM_GP_CTRL);
5670dbe28b3SPyun YongHyeon 		}
5680dbe28b3SPyun YongHyeon 	}
5696c4d62e1SPyun YongHyeon }
5700dbe28b3SPyun YongHyeon 
571ad4cb014SGleb Smirnoff static u_int
msk_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)572ad4cb014SGleb Smirnoff msk_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
573ad4cb014SGleb Smirnoff {
574ad4cb014SGleb Smirnoff 	uint32_t *mchash = arg;
575ad4cb014SGleb Smirnoff 	uint32_t crc;
576ad4cb014SGleb Smirnoff 
577ad4cb014SGleb Smirnoff 	crc = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN);
578ad4cb014SGleb Smirnoff 	/* Just want the 6 least significant bits. */
579ad4cb014SGleb Smirnoff 	crc &= 0x3f;
580ad4cb014SGleb Smirnoff 	/* Set the corresponding bit in the hash table. */
581ad4cb014SGleb Smirnoff 	mchash[crc >> 5] |= 1 << (crc & 0x1f);
582ad4cb014SGleb Smirnoff 
583ad4cb014SGleb Smirnoff 	return (1);
584ad4cb014SGleb Smirnoff }
585ad4cb014SGleb Smirnoff 
5860dbe28b3SPyun YongHyeon static void
msk_rxfilter(struct msk_if_softc * sc_if)5876d6588a1SPyun YongHyeon msk_rxfilter(struct msk_if_softc *sc_if)
5880dbe28b3SPyun YongHyeon {
5890dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
5905ab8c4b8SJustin Hibbits 	if_t ifp;
5910dbe28b3SPyun YongHyeon 	uint32_t mchash[2];
5920dbe28b3SPyun YongHyeon 	uint16_t mode;
5930dbe28b3SPyun YongHyeon 
5940dbe28b3SPyun YongHyeon 	sc = sc_if->msk_softc;
5950dbe28b3SPyun YongHyeon 
5960dbe28b3SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
5970dbe28b3SPyun YongHyeon 
5980dbe28b3SPyun YongHyeon 	ifp = sc_if->msk_ifp;
5990dbe28b3SPyun YongHyeon 
6000dbe28b3SPyun YongHyeon 	bzero(mchash, sizeof(mchash));
6010dbe28b3SPyun YongHyeon 	mode = GMAC_READ_2(sc, sc_if->msk_port, GM_RX_CTRL);
6025ab8c4b8SJustin Hibbits 	if ((if_getflags(ifp) & IFF_PROMISC) != 0)
6030dbe28b3SPyun YongHyeon 		mode &= ~(GM_RXCR_UCF_ENA | GM_RXCR_MCF_ENA);
6045ab8c4b8SJustin Hibbits 	else if ((if_getflags(ifp) & IFF_ALLMULTI) != 0) {
6056d6588a1SPyun YongHyeon 		mode |= GM_RXCR_UCF_ENA | GM_RXCR_MCF_ENA;
6060dbe28b3SPyun YongHyeon 		mchash[0] = 0xffff;
6070dbe28b3SPyun YongHyeon 		mchash[1] = 0xffff;
6080dbe28b3SPyun YongHyeon 	} else {
6096d6588a1SPyun YongHyeon 		mode |= GM_RXCR_UCF_ENA;
610ad4cb014SGleb Smirnoff 		if_foreach_llmaddr(ifp, msk_hash_maddr, mchash);
6116d6588a1SPyun YongHyeon 		if (mchash[0] != 0 || mchash[1] != 0)
6120dbe28b3SPyun YongHyeon 			mode |= GM_RXCR_MCF_ENA;
6130dbe28b3SPyun YongHyeon 	}
6140dbe28b3SPyun YongHyeon 
6150dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_MC_ADDR_H1,
6160dbe28b3SPyun YongHyeon 	    mchash[0] & 0xffff);
6170dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_MC_ADDR_H2,
6180dbe28b3SPyun YongHyeon 	    (mchash[0] >> 16) & 0xffff);
6190dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_MC_ADDR_H3,
6200dbe28b3SPyun YongHyeon 	    mchash[1] & 0xffff);
6210dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_MC_ADDR_H4,
6220dbe28b3SPyun YongHyeon 	    (mchash[1] >> 16) & 0xffff);
6230dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_RX_CTRL, mode);
6240dbe28b3SPyun YongHyeon }
6250dbe28b3SPyun YongHyeon 
6260dbe28b3SPyun YongHyeon static void
msk_setvlan(struct msk_if_softc * sc_if,if_t ifp)6275ab8c4b8SJustin Hibbits msk_setvlan(struct msk_if_softc *sc_if, if_t ifp)
6280dbe28b3SPyun YongHyeon {
6290dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
6300dbe28b3SPyun YongHyeon 
6310dbe28b3SPyun YongHyeon 	sc = sc_if->msk_softc;
6325ab8c4b8SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
6330dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T),
6340dbe28b3SPyun YongHyeon 		    RX_VLAN_STRIP_ON);
6350dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T),
6360dbe28b3SPyun YongHyeon 		    TX_VLAN_TAG_ON);
6370dbe28b3SPyun YongHyeon 	} else {
6380dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T),
6390dbe28b3SPyun YongHyeon 		    RX_VLAN_STRIP_OFF);
6400dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T),
6410dbe28b3SPyun YongHyeon 		    TX_VLAN_TAG_OFF);
6420dbe28b3SPyun YongHyeon 	}
6430dbe28b3SPyun YongHyeon }
6440dbe28b3SPyun YongHyeon 
6450dbe28b3SPyun YongHyeon static int
msk_rx_fill(struct msk_if_softc * sc_if,int jumbo)646388214e4SPyun YongHyeon msk_rx_fill(struct msk_if_softc *sc_if, int jumbo)
647388214e4SPyun YongHyeon {
648388214e4SPyun YongHyeon 	uint16_t idx;
649388214e4SPyun YongHyeon 	int i;
650388214e4SPyun YongHyeon 
651388214e4SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0 &&
6525ab8c4b8SJustin Hibbits 	    (if_getcapenable(sc_if->msk_ifp) & IFCAP_RXCSUM) != 0) {
653388214e4SPyun YongHyeon 		/* Wait until controller executes OP_TCPSTART command. */
6547659f3c3SPyun YongHyeon 		for (i = 100; i > 0; i--) {
6557659f3c3SPyun YongHyeon 			DELAY(100);
656388214e4SPyun YongHyeon 			idx = CSR_READ_2(sc_if->msk_softc,
657388214e4SPyun YongHyeon 			    Y2_PREF_Q_ADDR(sc_if->msk_rxq,
658388214e4SPyun YongHyeon 			    PREF_UNIT_GET_IDX_REG));
659388214e4SPyun YongHyeon 			if (idx != 0)
660388214e4SPyun YongHyeon 				break;
661388214e4SPyun YongHyeon 		}
662388214e4SPyun YongHyeon 		if (i == 0) {
663388214e4SPyun YongHyeon 			device_printf(sc_if->msk_if_dev,
664388214e4SPyun YongHyeon 			    "prefetch unit stuck?\n");
665388214e4SPyun YongHyeon 			return (ETIMEDOUT);
666388214e4SPyun YongHyeon 		}
667388214e4SPyun YongHyeon 		/*
668388214e4SPyun YongHyeon 		 * Fill consumed LE with free buffer. This can be done
669388214e4SPyun YongHyeon 		 * in Rx handler but we don't want to add special code
670388214e4SPyun YongHyeon 		 * in fast handler.
671388214e4SPyun YongHyeon 		 */
672388214e4SPyun YongHyeon 		if (jumbo > 0) {
673388214e4SPyun YongHyeon 			if (msk_jumbo_newbuf(sc_if, 0) != 0)
674388214e4SPyun YongHyeon 				return (ENOBUFS);
675388214e4SPyun YongHyeon 			bus_dmamap_sync(sc_if->msk_cdata.msk_jumbo_rx_ring_tag,
676388214e4SPyun YongHyeon 			    sc_if->msk_cdata.msk_jumbo_rx_ring_map,
677388214e4SPyun YongHyeon 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
678388214e4SPyun YongHyeon 		} else {
679388214e4SPyun YongHyeon 			if (msk_newbuf(sc_if, 0) != 0)
680388214e4SPyun YongHyeon 				return (ENOBUFS);
681388214e4SPyun YongHyeon 			bus_dmamap_sync(sc_if->msk_cdata.msk_rx_ring_tag,
682388214e4SPyun YongHyeon 			    sc_if->msk_cdata.msk_rx_ring_map,
683388214e4SPyun YongHyeon 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
684388214e4SPyun YongHyeon 		}
685388214e4SPyun YongHyeon 		sc_if->msk_cdata.msk_rx_prod = 0;
686388214e4SPyun YongHyeon 		CSR_WRITE_2(sc_if->msk_softc,
687388214e4SPyun YongHyeon 		    Y2_PREF_Q_ADDR(sc_if->msk_rxq, PREF_UNIT_PUT_IDX_REG),
688388214e4SPyun YongHyeon 		    sc_if->msk_cdata.msk_rx_prod);
689388214e4SPyun YongHyeon 	}
690388214e4SPyun YongHyeon 	return (0);
691388214e4SPyun YongHyeon }
692388214e4SPyun YongHyeon 
693388214e4SPyun YongHyeon static int
msk_init_rx_ring(struct msk_if_softc * sc_if)6940dbe28b3SPyun YongHyeon msk_init_rx_ring(struct msk_if_softc *sc_if)
6950dbe28b3SPyun YongHyeon {
6960dbe28b3SPyun YongHyeon 	struct msk_ring_data *rd;
6970dbe28b3SPyun YongHyeon 	struct msk_rxdesc *rxd;
698355a415eSPyun YongHyeon 	int i, nbuf, prod;
6990dbe28b3SPyun YongHyeon 
7000dbe28b3SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
7010dbe28b3SPyun YongHyeon 
7020dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_rx_cons = 0;
7030dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_rx_prod = 0;
7040dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_rx_putwm = MSK_PUT_WM;
7050dbe28b3SPyun YongHyeon 
7060dbe28b3SPyun YongHyeon 	rd = &sc_if->msk_rdata;
7070dbe28b3SPyun YongHyeon 	bzero(rd->msk_rx_ring, sizeof(struct msk_rx_desc) * MSK_RX_RING_CNT);
708355a415eSPyun YongHyeon 	for (i = prod = 0; i < MSK_RX_RING_CNT; i++) {
709355a415eSPyun YongHyeon 		rxd = &sc_if->msk_cdata.msk_rxdesc[prod];
710355a415eSPyun YongHyeon 		rxd->rx_m = NULL;
711355a415eSPyun YongHyeon 		rxd->rx_le = &rd->msk_rx_ring[prod];
712355a415eSPyun YongHyeon 		MSK_INC(prod, MSK_RX_RING_CNT);
713355a415eSPyun YongHyeon 	}
714355a415eSPyun YongHyeon 	nbuf = MSK_RX_BUF_CNT;
715355a415eSPyun YongHyeon 	prod = 0;
716388214e4SPyun YongHyeon 	/* Have controller know how to compute Rx checksum. */
717388214e4SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0 &&
7185ab8c4b8SJustin Hibbits 	    (if_getcapenable(sc_if->msk_ifp) & IFCAP_RXCSUM) != 0) {
719355a415eSPyun YongHyeon #ifdef MSK_64BIT_DMA
720388214e4SPyun YongHyeon 		rxd = &sc_if->msk_cdata.msk_rxdesc[prod];
721388214e4SPyun YongHyeon 		rxd->rx_m = NULL;
722388214e4SPyun YongHyeon 		rxd->rx_le = &rd->msk_rx_ring[prod];
723388214e4SPyun YongHyeon 		rxd->rx_le->msk_addr = htole32(ETHER_HDR_LEN << 16 |
724388214e4SPyun YongHyeon 		    ETHER_HDR_LEN);
725388214e4SPyun YongHyeon 		rxd->rx_le->msk_control = htole32(OP_TCPSTART | HW_OWNER);
726388214e4SPyun YongHyeon 		MSK_INC(prod, MSK_RX_RING_CNT);
727388214e4SPyun YongHyeon 		MSK_INC(sc_if->msk_cdata.msk_rx_cons, MSK_RX_RING_CNT);
728355a415eSPyun YongHyeon #endif
7290dbe28b3SPyun YongHyeon 		rxd = &sc_if->msk_cdata.msk_rxdesc[prod];
7300dbe28b3SPyun YongHyeon 		rxd->rx_m = NULL;
7310dbe28b3SPyun YongHyeon 		rxd->rx_le = &rd->msk_rx_ring[prod];
732355a415eSPyun YongHyeon 		rxd->rx_le->msk_addr = htole32(ETHER_HDR_LEN << 16 |
733355a415eSPyun YongHyeon 		    ETHER_HDR_LEN);
734355a415eSPyun YongHyeon 		rxd->rx_le->msk_control = htole32(OP_TCPSTART | HW_OWNER);
735355a415eSPyun YongHyeon 		MSK_INC(prod, MSK_RX_RING_CNT);
736355a415eSPyun YongHyeon 		MSK_INC(sc_if->msk_cdata.msk_rx_cons, MSK_RX_RING_CNT);
737355a415eSPyun YongHyeon 		nbuf--;
738355a415eSPyun YongHyeon 	}
739355a415eSPyun YongHyeon 	for (i = 0; i < nbuf; i++) {
7400dbe28b3SPyun YongHyeon 		if (msk_newbuf(sc_if, prod) != 0)
7410dbe28b3SPyun YongHyeon 			return (ENOBUFS);
742355a415eSPyun YongHyeon 		MSK_RX_INC(prod, MSK_RX_RING_CNT);
7430dbe28b3SPyun YongHyeon 	}
7440dbe28b3SPyun YongHyeon 
7450dbe28b3SPyun YongHyeon 	bus_dmamap_sync(sc_if->msk_cdata.msk_rx_ring_tag,
7460dbe28b3SPyun YongHyeon 	    sc_if->msk_cdata.msk_rx_ring_map,
7470dbe28b3SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
7480dbe28b3SPyun YongHyeon 
7490dbe28b3SPyun YongHyeon 	/* Update prefetch unit. */
750355a415eSPyun YongHyeon 	sc_if->msk_cdata.msk_rx_prod = prod;
7510dbe28b3SPyun YongHyeon 	CSR_WRITE_2(sc_if->msk_softc,
7520dbe28b3SPyun YongHyeon 	    Y2_PREF_Q_ADDR(sc_if->msk_rxq, PREF_UNIT_PUT_IDX_REG),
753355a415eSPyun YongHyeon 	    (sc_if->msk_cdata.msk_rx_prod + MSK_RX_RING_CNT - 1) %
754355a415eSPyun YongHyeon 	    MSK_RX_RING_CNT);
755388214e4SPyun YongHyeon 	if (msk_rx_fill(sc_if, 0) != 0)
756388214e4SPyun YongHyeon 		return (ENOBUFS);
7570dbe28b3SPyun YongHyeon 	return (0);
7580dbe28b3SPyun YongHyeon }
7590dbe28b3SPyun YongHyeon 
7600dbe28b3SPyun YongHyeon static int
msk_init_jumbo_rx_ring(struct msk_if_softc * sc_if)7610dbe28b3SPyun YongHyeon msk_init_jumbo_rx_ring(struct msk_if_softc *sc_if)
7620dbe28b3SPyun YongHyeon {
7630dbe28b3SPyun YongHyeon 	struct msk_ring_data *rd;
7640dbe28b3SPyun YongHyeon 	struct msk_rxdesc *rxd;
765355a415eSPyun YongHyeon 	int i, nbuf, prod;
7660dbe28b3SPyun YongHyeon 
7670dbe28b3SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
7680dbe28b3SPyun YongHyeon 
7690dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_rx_cons = 0;
7700dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_rx_prod = 0;
7710dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_rx_putwm = MSK_PUT_WM;
7720dbe28b3SPyun YongHyeon 
7730dbe28b3SPyun YongHyeon 	rd = &sc_if->msk_rdata;
7740dbe28b3SPyun YongHyeon 	bzero(rd->msk_jumbo_rx_ring,
7750dbe28b3SPyun YongHyeon 	    sizeof(struct msk_rx_desc) * MSK_JUMBO_RX_RING_CNT);
776355a415eSPyun YongHyeon 	for (i = prod = 0; i < MSK_JUMBO_RX_RING_CNT; i++) {
777355a415eSPyun YongHyeon 		rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[prod];
778355a415eSPyun YongHyeon 		rxd->rx_m = NULL;
779355a415eSPyun YongHyeon 		rxd->rx_le = &rd->msk_jumbo_rx_ring[prod];
780355a415eSPyun YongHyeon 		MSK_INC(prod, MSK_JUMBO_RX_RING_CNT);
781355a415eSPyun YongHyeon 	}
782355a415eSPyun YongHyeon 	nbuf = MSK_RX_BUF_CNT;
783355a415eSPyun YongHyeon 	prod = 0;
784388214e4SPyun YongHyeon 	/* Have controller know how to compute Rx checksum. */
785388214e4SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0 &&
7865ab8c4b8SJustin Hibbits 	    (if_getcapenable(sc_if->msk_ifp) & IFCAP_RXCSUM) != 0) {
787355a415eSPyun YongHyeon #ifdef MSK_64BIT_DMA
788388214e4SPyun YongHyeon 		rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[prod];
789388214e4SPyun YongHyeon 		rxd->rx_m = NULL;
790388214e4SPyun YongHyeon 		rxd->rx_le = &rd->msk_jumbo_rx_ring[prod];
791388214e4SPyun YongHyeon 		rxd->rx_le->msk_addr = htole32(ETHER_HDR_LEN << 16 |
792388214e4SPyun YongHyeon 		    ETHER_HDR_LEN);
793388214e4SPyun YongHyeon 		rxd->rx_le->msk_control = htole32(OP_TCPSTART | HW_OWNER);
794388214e4SPyun YongHyeon 		MSK_INC(prod, MSK_JUMBO_RX_RING_CNT);
795388214e4SPyun YongHyeon 		MSK_INC(sc_if->msk_cdata.msk_rx_cons, MSK_JUMBO_RX_RING_CNT);
796355a415eSPyun YongHyeon #endif
7970dbe28b3SPyun YongHyeon 		rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[prod];
7980dbe28b3SPyun YongHyeon 		rxd->rx_m = NULL;
7990dbe28b3SPyun YongHyeon 		rxd->rx_le = &rd->msk_jumbo_rx_ring[prod];
800355a415eSPyun YongHyeon 		rxd->rx_le->msk_addr = htole32(ETHER_HDR_LEN << 16 |
801355a415eSPyun YongHyeon 		    ETHER_HDR_LEN);
802355a415eSPyun YongHyeon 		rxd->rx_le->msk_control = htole32(OP_TCPSTART | HW_OWNER);
803355a415eSPyun YongHyeon 		MSK_INC(prod, MSK_JUMBO_RX_RING_CNT);
804355a415eSPyun YongHyeon 		MSK_INC(sc_if->msk_cdata.msk_rx_cons, MSK_JUMBO_RX_RING_CNT);
805355a415eSPyun YongHyeon 		nbuf--;
806355a415eSPyun YongHyeon 	}
807355a415eSPyun YongHyeon 	for (i = 0; i < nbuf; i++) {
8080dbe28b3SPyun YongHyeon 		if (msk_jumbo_newbuf(sc_if, prod) != 0)
8090dbe28b3SPyun YongHyeon 			return (ENOBUFS);
810355a415eSPyun YongHyeon 		MSK_RX_INC(prod, MSK_JUMBO_RX_RING_CNT);
8110dbe28b3SPyun YongHyeon 	}
8120dbe28b3SPyun YongHyeon 
8130dbe28b3SPyun YongHyeon 	bus_dmamap_sync(sc_if->msk_cdata.msk_jumbo_rx_ring_tag,
8140dbe28b3SPyun YongHyeon 	    sc_if->msk_cdata.msk_jumbo_rx_ring_map,
8150dbe28b3SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
8160dbe28b3SPyun YongHyeon 
817355a415eSPyun YongHyeon 	/* Update prefetch unit. */
818355a415eSPyun YongHyeon 	sc_if->msk_cdata.msk_rx_prod = prod;
8190dbe28b3SPyun YongHyeon 	CSR_WRITE_2(sc_if->msk_softc,
8200dbe28b3SPyun YongHyeon 	    Y2_PREF_Q_ADDR(sc_if->msk_rxq, PREF_UNIT_PUT_IDX_REG),
821355a415eSPyun YongHyeon 	    (sc_if->msk_cdata.msk_rx_prod + MSK_JUMBO_RX_RING_CNT - 1) %
822355a415eSPyun YongHyeon 	    MSK_JUMBO_RX_RING_CNT);
823388214e4SPyun YongHyeon 	if (msk_rx_fill(sc_if, 1) != 0)
824388214e4SPyun YongHyeon 		return (ENOBUFS);
8250dbe28b3SPyun YongHyeon 	return (0);
8260dbe28b3SPyun YongHyeon }
8270dbe28b3SPyun YongHyeon 
8280dbe28b3SPyun YongHyeon static void
msk_init_tx_ring(struct msk_if_softc * sc_if)8290dbe28b3SPyun YongHyeon msk_init_tx_ring(struct msk_if_softc *sc_if)
8300dbe28b3SPyun YongHyeon {
8310dbe28b3SPyun YongHyeon 	struct msk_ring_data *rd;
8320dbe28b3SPyun YongHyeon 	struct msk_txdesc *txd;
8330dbe28b3SPyun YongHyeon 	int i;
8340dbe28b3SPyun YongHyeon 
8350dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_tso_mtu = 0;
8361b7757c0SPyun YongHyeon 	sc_if->msk_cdata.msk_last_csum = 0;
8370dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_tx_prod = 0;
8380dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_tx_cons = 0;
8390dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_tx_cnt = 0;
840355a415eSPyun YongHyeon 	sc_if->msk_cdata.msk_tx_high_addr = 0;
8410dbe28b3SPyun YongHyeon 
8420dbe28b3SPyun YongHyeon 	rd = &sc_if->msk_rdata;
8430dbe28b3SPyun YongHyeon 	bzero(rd->msk_tx_ring, sizeof(struct msk_tx_desc) * MSK_TX_RING_CNT);
8440dbe28b3SPyun YongHyeon 	for (i = 0; i < MSK_TX_RING_CNT; i++) {
8450dbe28b3SPyun YongHyeon 		txd = &sc_if->msk_cdata.msk_txdesc[i];
8460dbe28b3SPyun YongHyeon 		txd->tx_m = NULL;
8470dbe28b3SPyun YongHyeon 		txd->tx_le = &rd->msk_tx_ring[i];
8480dbe28b3SPyun YongHyeon 	}
8490dbe28b3SPyun YongHyeon 
8500dbe28b3SPyun YongHyeon 	bus_dmamap_sync(sc_if->msk_cdata.msk_tx_ring_tag,
8510dbe28b3SPyun YongHyeon 	    sc_if->msk_cdata.msk_tx_ring_map,
8520dbe28b3SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
8530dbe28b3SPyun YongHyeon }
8540dbe28b3SPyun YongHyeon 
8550dbe28b3SPyun YongHyeon static __inline void
msk_discard_rxbuf(struct msk_if_softc * sc_if,int idx)8560dbe28b3SPyun YongHyeon msk_discard_rxbuf(struct msk_if_softc *sc_if, int idx)
8570dbe28b3SPyun YongHyeon {
8580dbe28b3SPyun YongHyeon 	struct msk_rx_desc *rx_le;
8590dbe28b3SPyun YongHyeon 	struct msk_rxdesc *rxd;
8600dbe28b3SPyun YongHyeon 	struct mbuf *m;
8610dbe28b3SPyun YongHyeon 
862355a415eSPyun YongHyeon #ifdef MSK_64BIT_DMA
863355a415eSPyun YongHyeon 	rxd = &sc_if->msk_cdata.msk_rxdesc[idx];
864355a415eSPyun YongHyeon 	rx_le = rxd->rx_le;
865355a415eSPyun YongHyeon 	rx_le->msk_control = htole32(OP_ADDR64 | HW_OWNER);
866355a415eSPyun YongHyeon 	MSK_INC(idx, MSK_RX_RING_CNT);
867355a415eSPyun YongHyeon #endif
8680dbe28b3SPyun YongHyeon 	rxd = &sc_if->msk_cdata.msk_rxdesc[idx];
8690dbe28b3SPyun YongHyeon 	m = rxd->rx_m;
8700dbe28b3SPyun YongHyeon 	rx_le = rxd->rx_le;
8710dbe28b3SPyun YongHyeon 	rx_le->msk_control = htole32(m->m_len | OP_PACKET | HW_OWNER);
8720dbe28b3SPyun YongHyeon }
8730dbe28b3SPyun YongHyeon 
8740dbe28b3SPyun YongHyeon static __inline void
msk_discard_jumbo_rxbuf(struct msk_if_softc * sc_if,int idx)8750dbe28b3SPyun YongHyeon msk_discard_jumbo_rxbuf(struct msk_if_softc *sc_if, int	idx)
8760dbe28b3SPyun YongHyeon {
8770dbe28b3SPyun YongHyeon 	struct msk_rx_desc *rx_le;
8780dbe28b3SPyun YongHyeon 	struct msk_rxdesc *rxd;
8790dbe28b3SPyun YongHyeon 	struct mbuf *m;
8800dbe28b3SPyun YongHyeon 
881355a415eSPyun YongHyeon #ifdef MSK_64BIT_DMA
882355a415eSPyun YongHyeon 	rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[idx];
883355a415eSPyun YongHyeon 	rx_le = rxd->rx_le;
884355a415eSPyun YongHyeon 	rx_le->msk_control = htole32(OP_ADDR64 | HW_OWNER);
885355a415eSPyun YongHyeon 	MSK_INC(idx, MSK_JUMBO_RX_RING_CNT);
886355a415eSPyun YongHyeon #endif
8870dbe28b3SPyun YongHyeon 	rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[idx];
8880dbe28b3SPyun YongHyeon 	m = rxd->rx_m;
8890dbe28b3SPyun YongHyeon 	rx_le = rxd->rx_le;
8900dbe28b3SPyun YongHyeon 	rx_le->msk_control = htole32(m->m_len | OP_PACKET | HW_OWNER);
8910dbe28b3SPyun YongHyeon }
8920dbe28b3SPyun YongHyeon 
8930dbe28b3SPyun YongHyeon static int
msk_newbuf(struct msk_if_softc * sc_if,int idx)8940dbe28b3SPyun YongHyeon msk_newbuf(struct msk_if_softc *sc_if, int idx)
8950dbe28b3SPyun YongHyeon {
8960dbe28b3SPyun YongHyeon 	struct msk_rx_desc *rx_le;
8970dbe28b3SPyun YongHyeon 	struct msk_rxdesc *rxd;
8980dbe28b3SPyun YongHyeon 	struct mbuf *m;
8990dbe28b3SPyun YongHyeon 	bus_dma_segment_t segs[1];
9000dbe28b3SPyun YongHyeon 	bus_dmamap_t map;
9010dbe28b3SPyun YongHyeon 	int nsegs;
9020dbe28b3SPyun YongHyeon 
903c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
9040dbe28b3SPyun YongHyeon 	if (m == NULL)
9050dbe28b3SPyun YongHyeon 		return (ENOBUFS);
9060dbe28b3SPyun YongHyeon 
9070dbe28b3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
90883c04c93SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) == 0)
9090dbe28b3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
91083c04c93SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
91183c04c93SPyun YongHyeon 	else
91283c04c93SPyun YongHyeon 		m_adj(m, MSK_RX_BUF_ALIGN);
91383c04c93SPyun YongHyeon #endif
9140dbe28b3SPyun YongHyeon 
9150dbe28b3SPyun YongHyeon 	if (bus_dmamap_load_mbuf_sg(sc_if->msk_cdata.msk_rx_tag,
9160dbe28b3SPyun YongHyeon 	    sc_if->msk_cdata.msk_rx_sparemap, m, segs, &nsegs,
9170dbe28b3SPyun YongHyeon 	    BUS_DMA_NOWAIT) != 0) {
9180dbe28b3SPyun YongHyeon 		m_freem(m);
9190dbe28b3SPyun YongHyeon 		return (ENOBUFS);
9200dbe28b3SPyun YongHyeon 	}
9210dbe28b3SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
9220dbe28b3SPyun YongHyeon 
9230dbe28b3SPyun YongHyeon 	rxd = &sc_if->msk_cdata.msk_rxdesc[idx];
924355a415eSPyun YongHyeon #ifdef MSK_64BIT_DMA
925355a415eSPyun YongHyeon 	rx_le = rxd->rx_le;
926355a415eSPyun YongHyeon 	rx_le->msk_addr = htole32(MSK_ADDR_HI(segs[0].ds_addr));
927355a415eSPyun YongHyeon 	rx_le->msk_control = htole32(OP_ADDR64 | HW_OWNER);
928355a415eSPyun YongHyeon 	MSK_INC(idx, MSK_RX_RING_CNT);
929355a415eSPyun YongHyeon 	rxd = &sc_if->msk_cdata.msk_rxdesc[idx];
930355a415eSPyun YongHyeon #endif
9310dbe28b3SPyun YongHyeon 	if (rxd->rx_m != NULL) {
9320dbe28b3SPyun YongHyeon 		bus_dmamap_sync(sc_if->msk_cdata.msk_rx_tag, rxd->rx_dmamap,
9330dbe28b3SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
9340dbe28b3SPyun YongHyeon 		bus_dmamap_unload(sc_if->msk_cdata.msk_rx_tag, rxd->rx_dmamap);
935355a415eSPyun YongHyeon 		rxd->rx_m = NULL;
9360dbe28b3SPyun YongHyeon 	}
9370dbe28b3SPyun YongHyeon 	map = rxd->rx_dmamap;
9380dbe28b3SPyun YongHyeon 	rxd->rx_dmamap = sc_if->msk_cdata.msk_rx_sparemap;
9390dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_rx_sparemap = map;
9400dbe28b3SPyun YongHyeon 	bus_dmamap_sync(sc_if->msk_cdata.msk_rx_tag, rxd->rx_dmamap,
9410dbe28b3SPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
9420dbe28b3SPyun YongHyeon 	rxd->rx_m = m;
9430dbe28b3SPyun YongHyeon 	rx_le = rxd->rx_le;
9440dbe28b3SPyun YongHyeon 	rx_le->msk_addr = htole32(MSK_ADDR_LO(segs[0].ds_addr));
9450dbe28b3SPyun YongHyeon 	rx_le->msk_control =
9460dbe28b3SPyun YongHyeon 	    htole32(segs[0].ds_len | OP_PACKET | HW_OWNER);
9470dbe28b3SPyun YongHyeon 
9480dbe28b3SPyun YongHyeon 	return (0);
9490dbe28b3SPyun YongHyeon }
9500dbe28b3SPyun YongHyeon 
9510dbe28b3SPyun YongHyeon static int
msk_jumbo_newbuf(struct msk_if_softc * sc_if,int idx)9520dbe28b3SPyun YongHyeon msk_jumbo_newbuf(struct msk_if_softc *sc_if, int idx)
9530dbe28b3SPyun YongHyeon {
9540dbe28b3SPyun YongHyeon 	struct msk_rx_desc *rx_le;
9550dbe28b3SPyun YongHyeon 	struct msk_rxdesc *rxd;
9560dbe28b3SPyun YongHyeon 	struct mbuf *m;
9570dbe28b3SPyun YongHyeon 	bus_dma_segment_t segs[1];
9580dbe28b3SPyun YongHyeon 	bus_dmamap_t map;
9590dbe28b3SPyun YongHyeon 	int nsegs;
9600dbe28b3SPyun YongHyeon 
961c6499eccSGleb Smirnoff 	m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
9620dbe28b3SPyun YongHyeon 	if (m == NULL)
9630dbe28b3SPyun YongHyeon 		return (ENOBUFS);
96485b340cbSPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
96583c04c93SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) == 0)
9660dbe28b3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
96783c04c93SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
96883c04c93SPyun YongHyeon 	else
96983c04c93SPyun YongHyeon 		m_adj(m, MSK_RX_BUF_ALIGN);
97083c04c93SPyun YongHyeon #endif
9710dbe28b3SPyun YongHyeon 
9720dbe28b3SPyun YongHyeon 	if (bus_dmamap_load_mbuf_sg(sc_if->msk_cdata.msk_jumbo_rx_tag,
9730dbe28b3SPyun YongHyeon 	    sc_if->msk_cdata.msk_jumbo_rx_sparemap, m, segs, &nsegs,
9740dbe28b3SPyun YongHyeon 	    BUS_DMA_NOWAIT) != 0) {
9750dbe28b3SPyun YongHyeon 		m_freem(m);
9760dbe28b3SPyun YongHyeon 		return (ENOBUFS);
9770dbe28b3SPyun YongHyeon 	}
9780dbe28b3SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
9790dbe28b3SPyun YongHyeon 
9800dbe28b3SPyun YongHyeon 	rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[idx];
981355a415eSPyun YongHyeon #ifdef MSK_64BIT_DMA
982355a415eSPyun YongHyeon 	rx_le = rxd->rx_le;
983355a415eSPyun YongHyeon 	rx_le->msk_addr = htole32(MSK_ADDR_HI(segs[0].ds_addr));
984355a415eSPyun YongHyeon 	rx_le->msk_control = htole32(OP_ADDR64 | HW_OWNER);
985355a415eSPyun YongHyeon 	MSK_INC(idx, MSK_JUMBO_RX_RING_CNT);
986355a415eSPyun YongHyeon 	rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[idx];
987355a415eSPyun YongHyeon #endif
9880dbe28b3SPyun YongHyeon 	if (rxd->rx_m != NULL) {
9890dbe28b3SPyun YongHyeon 		bus_dmamap_sync(sc_if->msk_cdata.msk_jumbo_rx_tag,
9900dbe28b3SPyun YongHyeon 		    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
9910dbe28b3SPyun YongHyeon 		bus_dmamap_unload(sc_if->msk_cdata.msk_jumbo_rx_tag,
9920dbe28b3SPyun YongHyeon 		    rxd->rx_dmamap);
993355a415eSPyun YongHyeon 		rxd->rx_m = NULL;
9940dbe28b3SPyun YongHyeon 	}
9950dbe28b3SPyun YongHyeon 	map = rxd->rx_dmamap;
9960dbe28b3SPyun YongHyeon 	rxd->rx_dmamap = sc_if->msk_cdata.msk_jumbo_rx_sparemap;
9970dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_jumbo_rx_sparemap = map;
9980dbe28b3SPyun YongHyeon 	bus_dmamap_sync(sc_if->msk_cdata.msk_jumbo_rx_tag, rxd->rx_dmamap,
9990dbe28b3SPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
10000dbe28b3SPyun YongHyeon 	rxd->rx_m = m;
10010dbe28b3SPyun YongHyeon 	rx_le = rxd->rx_le;
10020dbe28b3SPyun YongHyeon 	rx_le->msk_addr = htole32(MSK_ADDR_LO(segs[0].ds_addr));
10030dbe28b3SPyun YongHyeon 	rx_le->msk_control =
10040dbe28b3SPyun YongHyeon 	    htole32(segs[0].ds_len | OP_PACKET | HW_OWNER);
10050dbe28b3SPyun YongHyeon 
10060dbe28b3SPyun YongHyeon 	return (0);
10070dbe28b3SPyun YongHyeon }
10080dbe28b3SPyun YongHyeon 
10090dbe28b3SPyun YongHyeon /*
10100dbe28b3SPyun YongHyeon  * Set media options.
10110dbe28b3SPyun YongHyeon  */
10120dbe28b3SPyun YongHyeon static int
msk_mediachange(if_t ifp)10135ab8c4b8SJustin Hibbits msk_mediachange(if_t ifp)
10140dbe28b3SPyun YongHyeon {
10150dbe28b3SPyun YongHyeon 	struct msk_if_softc *sc_if;
10160dbe28b3SPyun YongHyeon 	struct mii_data	*mii;
1017325c534eSPyun YongHyeon 	int error;
10180dbe28b3SPyun YongHyeon 
10195ab8c4b8SJustin Hibbits 	sc_if = if_getsoftc(ifp);
10200dbe28b3SPyun YongHyeon 
10210dbe28b3SPyun YongHyeon 	MSK_IF_LOCK(sc_if);
10220dbe28b3SPyun YongHyeon 	mii = device_get_softc(sc_if->msk_miibus);
1023325c534eSPyun YongHyeon 	error = mii_mediachg(mii);
10240dbe28b3SPyun YongHyeon 	MSK_IF_UNLOCK(sc_if);
10250dbe28b3SPyun YongHyeon 
1026325c534eSPyun YongHyeon 	return (error);
10270dbe28b3SPyun YongHyeon }
10280dbe28b3SPyun YongHyeon 
10290dbe28b3SPyun YongHyeon /*
10300dbe28b3SPyun YongHyeon  * Report current media status.
10310dbe28b3SPyun YongHyeon  */
10320dbe28b3SPyun YongHyeon static void
msk_mediastatus(if_t ifp,struct ifmediareq * ifmr)10335ab8c4b8SJustin Hibbits msk_mediastatus(if_t ifp, struct ifmediareq *ifmr)
10340dbe28b3SPyun YongHyeon {
10350dbe28b3SPyun YongHyeon 	struct msk_if_softc *sc_if;
10360dbe28b3SPyun YongHyeon 	struct mii_data	*mii;
10370dbe28b3SPyun YongHyeon 
10385ab8c4b8SJustin Hibbits 	sc_if = if_getsoftc(ifp);
10390dbe28b3SPyun YongHyeon 	MSK_IF_LOCK(sc_if);
10405ab8c4b8SJustin Hibbits 	if ((if_getflags(ifp) & IFF_UP) == 0) {
10416f5a0d1fSPyun YongHyeon 		MSK_IF_UNLOCK(sc_if);
10426f5a0d1fSPyun YongHyeon 		return;
10436f5a0d1fSPyun YongHyeon 	}
10440dbe28b3SPyun YongHyeon 	mii = device_get_softc(sc_if->msk_miibus);
10450dbe28b3SPyun YongHyeon 
10460dbe28b3SPyun YongHyeon 	mii_pollstat(mii);
10470dbe28b3SPyun YongHyeon 	ifmr->ifm_active = mii->mii_media_active;
10480dbe28b3SPyun YongHyeon 	ifmr->ifm_status = mii->mii_media_status;
104957c81d92SPyun YongHyeon 	MSK_IF_UNLOCK(sc_if);
10500dbe28b3SPyun YongHyeon }
10510dbe28b3SPyun YongHyeon 
10520dbe28b3SPyun YongHyeon static int
msk_ioctl(if_t ifp,u_long command,caddr_t data)10535ab8c4b8SJustin Hibbits msk_ioctl(if_t ifp, u_long command, caddr_t data)
10540dbe28b3SPyun YongHyeon {
10550dbe28b3SPyun YongHyeon 	struct msk_if_softc *sc_if;
10560dbe28b3SPyun YongHyeon 	struct ifreq *ifr;
10570dbe28b3SPyun YongHyeon 	struct mii_data	*mii;
1058388214e4SPyun YongHyeon 	int error, mask, reinit;
10590dbe28b3SPyun YongHyeon 
10605ab8c4b8SJustin Hibbits 	sc_if = if_getsoftc(ifp);
10610dbe28b3SPyun YongHyeon 	ifr = (struct ifreq *)data;
10620dbe28b3SPyun YongHyeon 	error = 0;
10630dbe28b3SPyun YongHyeon 
10640dbe28b3SPyun YongHyeon 	switch(command) {
10650dbe28b3SPyun YongHyeon 	case SIOCSIFMTU:
1066e2b16603SPyun YongHyeon 		MSK_IF_LOCK(sc_if);
106785b340cbSPyun YongHyeon 		if (ifr->ifr_mtu > MSK_JUMBO_MTU || ifr->ifr_mtu < ETHERMIN)
10680dbe28b3SPyun YongHyeon 			error = EINVAL;
10695ab8c4b8SJustin Hibbits 		else if (if_getmtu(ifp) != ifr->ifr_mtu) {
1070e2b16603SPyun YongHyeon 			if (ifr->ifr_mtu > ETHERMTU) {
1071e2b16603SPyun YongHyeon 				if ((sc_if->msk_flags & MSK_FLAG_JUMBO) == 0) {
10720dbe28b3SPyun YongHyeon 					error = EINVAL;
10730dbe28b3SPyun YongHyeon 					MSK_IF_UNLOCK(sc_if);
1074e2b16603SPyun YongHyeon 					break;
1075e2b16603SPyun YongHyeon 				}
1076e2b16603SPyun YongHyeon 				if ((sc_if->msk_flags &
1077e2b16603SPyun YongHyeon 				    MSK_FLAG_JUMBO_NOCSUM) != 0) {
10785ab8c4b8SJustin Hibbits 					if_sethwassistbits(ifp, 0,
10795ab8c4b8SJustin Hibbits 					    MSK_CSUM_FEATURES | CSUM_TSO);
10805ab8c4b8SJustin Hibbits 					if_setcapenablebit(ifp, 0,
10815ab8c4b8SJustin Hibbits 					    IFCAP_TSO4 | IFCAP_TXCSUM);
1082e2b16603SPyun YongHyeon 					VLAN_CAPABILITIES(ifp);
108385b340cbSPyun YongHyeon 				}
108485b340cbSPyun YongHyeon 			}
10855ab8c4b8SJustin Hibbits 			if_setmtu(ifp, ifr->ifr_mtu);
10865ab8c4b8SJustin Hibbits 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
10875ab8c4b8SJustin Hibbits 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
1088e2b16603SPyun YongHyeon 				msk_init_locked(sc_if);
1089e2b16603SPyun YongHyeon 			}
10908be664b8SPyun YongHyeon 		}
1091e2b16603SPyun YongHyeon 		MSK_IF_UNLOCK(sc_if);
10920dbe28b3SPyun YongHyeon 		break;
10930dbe28b3SPyun YongHyeon 	case SIOCSIFFLAGS:
10940dbe28b3SPyun YongHyeon 		MSK_IF_LOCK(sc_if);
10955ab8c4b8SJustin Hibbits 		if ((if_getflags(ifp) & IFF_UP) != 0) {
10965ab8c4b8SJustin Hibbits 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0 &&
10975ab8c4b8SJustin Hibbits 			    ((if_getflags(ifp) ^ sc_if->msk_if_flags) &
1098b7e1e144SPyun YongHyeon 			    (IFF_PROMISC | IFF_ALLMULTI)) != 0)
10996d6588a1SPyun YongHyeon 				msk_rxfilter(sc_if);
1100b7e1e144SPyun YongHyeon 			else if ((sc_if->msk_flags & MSK_FLAG_DETACH) == 0)
11010dbe28b3SPyun YongHyeon 				msk_init_locked(sc_if);
11025ab8c4b8SJustin Hibbits 		} else if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
11030dbe28b3SPyun YongHyeon 			msk_stop(sc_if);
11045ab8c4b8SJustin Hibbits 		sc_if->msk_if_flags = if_getflags(ifp);
11050dbe28b3SPyun YongHyeon 		MSK_IF_UNLOCK(sc_if);
11060dbe28b3SPyun YongHyeon 		break;
11070dbe28b3SPyun YongHyeon 	case SIOCADDMULTI:
11080dbe28b3SPyun YongHyeon 	case SIOCDELMULTI:
11090dbe28b3SPyun YongHyeon 		MSK_IF_LOCK(sc_if);
11105ab8c4b8SJustin Hibbits 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
11116d6588a1SPyun YongHyeon 			msk_rxfilter(sc_if);
11120dbe28b3SPyun YongHyeon 		MSK_IF_UNLOCK(sc_if);
11130dbe28b3SPyun YongHyeon 		break;
11140dbe28b3SPyun YongHyeon 	case SIOCGIFMEDIA:
11150dbe28b3SPyun YongHyeon 	case SIOCSIFMEDIA:
11160dbe28b3SPyun YongHyeon 		mii = device_get_softc(sc_if->msk_miibus);
11170dbe28b3SPyun YongHyeon 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
11180dbe28b3SPyun YongHyeon 		break;
11190dbe28b3SPyun YongHyeon 	case SIOCSIFCAP:
1120388214e4SPyun YongHyeon 		reinit = 0;
11210dbe28b3SPyun YongHyeon 		MSK_IF_LOCK(sc_if);
11225ab8c4b8SJustin Hibbits 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
112398e02aebSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
11245ab8c4b8SJustin Hibbits 		    (IFCAP_TXCSUM & if_getcapabilities(ifp)) != 0) {
11255ab8c4b8SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_TXCSUM);
11265ab8c4b8SJustin Hibbits 			if ((IFCAP_TXCSUM & if_getcapenable(ifp)) != 0)
11275ab8c4b8SJustin Hibbits 				if_sethwassistbits(ifp, MSK_CSUM_FEATURES, 0);
11280dbe28b3SPyun YongHyeon 			else
11295ab8c4b8SJustin Hibbits 				if_sethwassistbits(ifp, 0, MSK_CSUM_FEATURES);
11300dbe28b3SPyun YongHyeon 		}
1131efb74172SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
11325ab8c4b8SJustin Hibbits 		    (IFCAP_RXCSUM & if_getcapabilities(ifp)) != 0) {
11335ab8c4b8SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_RXCSUM);
1134388214e4SPyun YongHyeon 			if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0)
1135388214e4SPyun YongHyeon 				reinit = 1;
1136388214e4SPyun YongHyeon 		}
1137efb74172SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
11385ab8c4b8SJustin Hibbits 		    (IFCAP_VLAN_HWCSUM & if_getcapabilities(ifp)) != 0)
11395ab8c4b8SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
114098e02aebSPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
11415ab8c4b8SJustin Hibbits 		    (IFCAP_TSO4 & if_getcapabilities(ifp)) != 0) {
11425ab8c4b8SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_TSO4);
11435ab8c4b8SJustin Hibbits 			if ((IFCAP_TSO4 & if_getcapenable(ifp)) != 0)
11445ab8c4b8SJustin Hibbits 				if_sethwassistbits(ifp, CSUM_TSO, 0);
11450dbe28b3SPyun YongHyeon 			else
11465ab8c4b8SJustin Hibbits 				if_sethwassistbits(ifp, 0, CSUM_TSO);
11470dbe28b3SPyun YongHyeon 		}
11484858893bSPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
11495ab8c4b8SJustin Hibbits 		    (IFCAP_VLAN_HWTSO & if_getcapabilities(ifp)) != 0)
11505ab8c4b8SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
11514858893bSPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
11525ab8c4b8SJustin Hibbits 		    (IFCAP_VLAN_HWTAGGING & if_getcapabilities(ifp)) != 0) {
11535ab8c4b8SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
11545ab8c4b8SJustin Hibbits 			if ((IFCAP_VLAN_HWTAGGING & if_getcapenable(ifp)) == 0)
11555ab8c4b8SJustin Hibbits 				if_setcapenablebit(ifp, 0,
11565ab8c4b8SJustin Hibbits 				    IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM);
11574858893bSPyun YongHyeon 			msk_setvlan(sc_if, ifp);
11584858893bSPyun YongHyeon 		}
11595ab8c4b8SJustin Hibbits 		if (if_getmtu(ifp) > ETHERMTU &&
1160e2b16603SPyun YongHyeon 		    (sc_if->msk_flags & MSK_FLAG_JUMBO_NOCSUM) != 0) {
11615ab8c4b8SJustin Hibbits 			if_sethwassistbits(ifp, 0, (MSK_CSUM_FEATURES | CSUM_TSO));
11625ab8c4b8SJustin Hibbits 			if_setcapenablebit(ifp, 0, (IFCAP_TSO4 | IFCAP_TXCSUM));
1163a109c74fSPyun YongHyeon 		}
11640dbe28b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
11655ab8c4b8SJustin Hibbits 		if (reinit > 0 && (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
11665ab8c4b8SJustin Hibbits 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
1167388214e4SPyun YongHyeon 			msk_init_locked(sc_if);
1168388214e4SPyun YongHyeon 		}
11690dbe28b3SPyun YongHyeon 		MSK_IF_UNLOCK(sc_if);
11700dbe28b3SPyun YongHyeon 		break;
11710dbe28b3SPyun YongHyeon 	default:
11720dbe28b3SPyun YongHyeon 		error = ether_ioctl(ifp, command, data);
11730dbe28b3SPyun YongHyeon 		break;
11740dbe28b3SPyun YongHyeon 	}
11750dbe28b3SPyun YongHyeon 
11760dbe28b3SPyun YongHyeon 	return (error);
11770dbe28b3SPyun YongHyeon }
11780dbe28b3SPyun YongHyeon 
11790dbe28b3SPyun YongHyeon static int
mskc_probe(device_t dev)11800dbe28b3SPyun YongHyeon mskc_probe(device_t dev)
11810dbe28b3SPyun YongHyeon {
11822dc26832SMarius Strobl 	const struct msk_product *mp;
11830dbe28b3SPyun YongHyeon 	uint16_t vendor, devid;
11840dbe28b3SPyun YongHyeon 	int i;
11850dbe28b3SPyun YongHyeon 
11860dbe28b3SPyun YongHyeon 	vendor = pci_get_vendor(dev);
11870dbe28b3SPyun YongHyeon 	devid = pci_get_device(dev);
11880dbe28b3SPyun YongHyeon 	mp = msk_products;
11892dc26832SMarius Strobl 	for (i = 0; i < nitems(msk_products); i++, mp++) {
11900dbe28b3SPyun YongHyeon 		if (vendor == mp->msk_vendorid && devid == mp->msk_deviceid) {
11910dbe28b3SPyun YongHyeon 			device_set_desc(dev, mp->msk_name);
11920dbe28b3SPyun YongHyeon 			return (BUS_PROBE_DEFAULT);
11930dbe28b3SPyun YongHyeon 		}
11940dbe28b3SPyun YongHyeon 	}
11950dbe28b3SPyun YongHyeon 
11960dbe28b3SPyun YongHyeon 	return (ENXIO);
11970dbe28b3SPyun YongHyeon }
11980dbe28b3SPyun YongHyeon 
11990dbe28b3SPyun YongHyeon static int
mskc_setup_rambuffer(struct msk_softc * sc)12000dbe28b3SPyun YongHyeon mskc_setup_rambuffer(struct msk_softc *sc)
12010dbe28b3SPyun YongHyeon {
1202e4a5f4e0SPyun YongHyeon 	int next;
12030dbe28b3SPyun YongHyeon 	int i;
12040dbe28b3SPyun YongHyeon 
12050dbe28b3SPyun YongHyeon 	/* Get adapter SRAM size. */
120683c04c93SPyun YongHyeon 	sc->msk_ramsize = CSR_READ_1(sc, B2_E_0) * 4;
12070dbe28b3SPyun YongHyeon 	if (bootverbose)
12080dbe28b3SPyun YongHyeon 		device_printf(sc->msk_dev,
12090dbe28b3SPyun YongHyeon 		    "RAM buffer size : %dKB\n", sc->msk_ramsize);
121083c04c93SPyun YongHyeon 	if (sc->msk_ramsize == 0)
121183c04c93SPyun YongHyeon 		return (0);
121283c04c93SPyun YongHyeon 
121383c04c93SPyun YongHyeon 	sc->msk_pflags |= MSK_FLAG_RAMBUF;
12140dbe28b3SPyun YongHyeon 	/*
1215e4a5f4e0SPyun YongHyeon 	 * Give receiver 2/3 of memory and round down to the multiple
1216b1ce21c6SRebecca Cran 	 * of 1024. Tx/Rx RAM buffer size of Yukon II should be multiple
1217e4a5f4e0SPyun YongHyeon 	 * of 1024.
12180dbe28b3SPyun YongHyeon 	 */
1219e4a5f4e0SPyun YongHyeon 	sc->msk_rxqsize = rounddown((sc->msk_ramsize * 1024 * 2) / 3, 1024);
1220e4a5f4e0SPyun YongHyeon 	sc->msk_txqsize = (sc->msk_ramsize * 1024) - sc->msk_rxqsize;
12210dbe28b3SPyun YongHyeon 	for (i = 0, next = 0; i < sc->msk_num_port; i++) {
12220dbe28b3SPyun YongHyeon 		sc->msk_rxqstart[i] = next;
1223e4a5f4e0SPyun YongHyeon 		sc->msk_rxqend[i] = next + sc->msk_rxqsize - 1;
12240dbe28b3SPyun YongHyeon 		next = sc->msk_rxqend[i] + 1;
12250dbe28b3SPyun YongHyeon 		sc->msk_txqstart[i] = next;
1226e4a5f4e0SPyun YongHyeon 		sc->msk_txqend[i] = next + sc->msk_txqsize - 1;
12270dbe28b3SPyun YongHyeon 		next = sc->msk_txqend[i] + 1;
12280dbe28b3SPyun YongHyeon 		if (bootverbose) {
12290dbe28b3SPyun YongHyeon 			device_printf(sc->msk_dev,
12300dbe28b3SPyun YongHyeon 			    "Port %d : Rx Queue %dKB(0x%08x:0x%08x)\n", i,
1231e4a5f4e0SPyun YongHyeon 			    sc->msk_rxqsize / 1024, sc->msk_rxqstart[i],
12320dbe28b3SPyun YongHyeon 			    sc->msk_rxqend[i]);
12330dbe28b3SPyun YongHyeon 			device_printf(sc->msk_dev,
12340dbe28b3SPyun YongHyeon 			    "Port %d : Tx Queue %dKB(0x%08x:0x%08x)\n", i,
1235e4a5f4e0SPyun YongHyeon 			    sc->msk_txqsize / 1024, sc->msk_txqstart[i],
12360dbe28b3SPyun YongHyeon 			    sc->msk_txqend[i]);
12370dbe28b3SPyun YongHyeon 		}
12380dbe28b3SPyun YongHyeon 	}
12390dbe28b3SPyun YongHyeon 
12400dbe28b3SPyun YongHyeon 	return (0);
12410dbe28b3SPyun YongHyeon }
12420dbe28b3SPyun YongHyeon 
12430dbe28b3SPyun YongHyeon static void
msk_phy_power(struct msk_softc * sc,int mode)12440dbe28b3SPyun YongHyeon msk_phy_power(struct msk_softc *sc, int mode)
12450dbe28b3SPyun YongHyeon {
1246846e6d79SPyun YongHyeon 	uint32_t our, val;
12470dbe28b3SPyun YongHyeon 	int i;
12480dbe28b3SPyun YongHyeon 
12490dbe28b3SPyun YongHyeon 	switch (mode) {
12500dbe28b3SPyun YongHyeon 	case MSK_PHY_POWERUP:
12510dbe28b3SPyun YongHyeon 		/* Switch power to VCC (WA for VAUX problem). */
12520dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, B0_POWER_CTRL,
12530dbe28b3SPyun YongHyeon 		    PC_VAUX_ENA | PC_VCC_ENA | PC_VAUX_OFF | PC_VCC_ON);
12540dbe28b3SPyun YongHyeon 		/* Disable Core Clock Division, set Clock Select to 0. */
12550dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, B2_Y2_CLK_CTRL, Y2_CLK_DIV_DIS);
12560dbe28b3SPyun YongHyeon 
12570dbe28b3SPyun YongHyeon 		val = 0;
12580dbe28b3SPyun YongHyeon 		if (sc->msk_hw_id == CHIP_ID_YUKON_XL &&
12590dbe28b3SPyun YongHyeon 		    sc->msk_hw_rev > CHIP_REV_YU_XL_A1) {
12600dbe28b3SPyun YongHyeon 			/* Enable bits are inverted. */
12610dbe28b3SPyun YongHyeon 			val = Y2_PCI_CLK_LNK1_DIS | Y2_COR_CLK_LNK1_DIS |
12620dbe28b3SPyun YongHyeon 			      Y2_CLK_GAT_LNK1_DIS | Y2_PCI_CLK_LNK2_DIS |
12630dbe28b3SPyun YongHyeon 			      Y2_COR_CLK_LNK2_DIS | Y2_CLK_GAT_LNK2_DIS;
12640dbe28b3SPyun YongHyeon 		}
12650dbe28b3SPyun YongHyeon 		/*
12660dbe28b3SPyun YongHyeon 		 * Enable PCI & Core Clock, enable clock gating for both Links.
12670dbe28b3SPyun YongHyeon 		 */
12680dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, B2_Y2_CLK_GATE, val);
12690dbe28b3SPyun YongHyeon 
1270c6a34f76SPyun YongHyeon 		our = CSR_PCI_READ_4(sc, PCI_OUR_REG_1);
1271c6a34f76SPyun YongHyeon 		our &= ~(PCI_Y2_PHY1_POWD | PCI_Y2_PHY2_POWD);
1272daf29227SPyun YongHyeon 		if (sc->msk_hw_id == CHIP_ID_YUKON_XL) {
1273846e6d79SPyun YongHyeon 			if (sc->msk_hw_rev > CHIP_REV_YU_XL_A1) {
12740dbe28b3SPyun YongHyeon 				/* Deassert Low Power for 1st PHY. */
1275c6a34f76SPyun YongHyeon 				our |= PCI_Y2_PHY1_COMA;
12760dbe28b3SPyun YongHyeon 				if (sc->msk_num_port > 1)
1277c6a34f76SPyun YongHyeon 					our |= PCI_Y2_PHY2_COMA;
1278846e6d79SPyun YongHyeon 			}
1279daf29227SPyun YongHyeon 		}
1280c6a34f76SPyun YongHyeon 		if (sc->msk_hw_id == CHIP_ID_YUKON_EC_U ||
1281c6a34f76SPyun YongHyeon 		    sc->msk_hw_id == CHIP_ID_YUKON_EX ||
1282c6a34f76SPyun YongHyeon 		    sc->msk_hw_id >= CHIP_ID_YUKON_FE_P) {
1283c6a34f76SPyun YongHyeon 			val = CSR_PCI_READ_4(sc, PCI_OUR_REG_4);
1284c6a34f76SPyun YongHyeon 			val &= (PCI_FORCE_ASPM_REQUEST |
1285c6a34f76SPyun YongHyeon 			    PCI_ASPM_GPHY_LINK_DOWN | PCI_ASPM_INT_FIFO_EMPTY |
1286c6a34f76SPyun YongHyeon 			    PCI_ASPM_CLKRUN_REQUEST);
12870dbe28b3SPyun YongHyeon 			/* Set all bits to 0 except bits 15..12. */
1288c6a34f76SPyun YongHyeon 			CSR_PCI_WRITE_4(sc, PCI_OUR_REG_4, val);
1289c6a34f76SPyun YongHyeon 			val = CSR_PCI_READ_4(sc, PCI_OUR_REG_5);
1290c6a34f76SPyun YongHyeon 			val &= PCI_CTL_TIM_VMAIN_AV_MSK;
1291c6a34f76SPyun YongHyeon 			CSR_PCI_WRITE_4(sc, PCI_OUR_REG_5, val);
1292b45923a6SPyun YongHyeon 			CSR_PCI_WRITE_4(sc, PCI_CFG_REG_1, 0);
1293c6a34f76SPyun YongHyeon 			CSR_WRITE_2(sc, B0_CTST, Y2_HW_WOL_ON);
1294daf29227SPyun YongHyeon 			/*
1295daf29227SPyun YongHyeon 			 * Disable status race, workaround for
1296daf29227SPyun YongHyeon 			 * Yukon EC Ultra & Yukon EX.
1297daf29227SPyun YongHyeon 			 */
1298daf29227SPyun YongHyeon 			val = CSR_READ_4(sc, B2_GP_IO);
1299daf29227SPyun YongHyeon 			val |= GLB_GPIO_STAT_RACE_DIS;
1300daf29227SPyun YongHyeon 			CSR_WRITE_4(sc, B2_GP_IO, val);
1301daf29227SPyun YongHyeon 			CSR_READ_4(sc, B2_GP_IO);
13020dbe28b3SPyun YongHyeon 		}
1303c6a34f76SPyun YongHyeon 		/* Release PHY from PowerDown/COMA mode. */
1304c6a34f76SPyun YongHyeon 		CSR_PCI_WRITE_4(sc, PCI_OUR_REG_1, our);
1305c6a34f76SPyun YongHyeon 
13060dbe28b3SPyun YongHyeon 		for (i = 0; i < sc->msk_num_port; i++) {
13070dbe28b3SPyun YongHyeon 			CSR_WRITE_2(sc, MR_ADDR(i, GMAC_LINK_CTRL),
13080dbe28b3SPyun YongHyeon 			    GMLC_RST_SET);
13090dbe28b3SPyun YongHyeon 			CSR_WRITE_2(sc, MR_ADDR(i, GMAC_LINK_CTRL),
13100dbe28b3SPyun YongHyeon 			    GMLC_RST_CLR);
13110dbe28b3SPyun YongHyeon 		}
13120dbe28b3SPyun YongHyeon 		break;
13130dbe28b3SPyun YongHyeon 	case MSK_PHY_POWERDOWN:
1314b45923a6SPyun YongHyeon 		val = CSR_PCI_READ_4(sc, PCI_OUR_REG_1);
13150dbe28b3SPyun YongHyeon 		val |= PCI_Y2_PHY1_POWD | PCI_Y2_PHY2_POWD;
13160dbe28b3SPyun YongHyeon 		if (sc->msk_hw_id == CHIP_ID_YUKON_XL &&
13170dbe28b3SPyun YongHyeon 		    sc->msk_hw_rev > CHIP_REV_YU_XL_A1) {
13180dbe28b3SPyun YongHyeon 			val &= ~PCI_Y2_PHY1_COMA;
13190dbe28b3SPyun YongHyeon 			if (sc->msk_num_port > 1)
13200dbe28b3SPyun YongHyeon 				val &= ~PCI_Y2_PHY2_COMA;
13210dbe28b3SPyun YongHyeon 		}
1322b45923a6SPyun YongHyeon 		CSR_PCI_WRITE_4(sc, PCI_OUR_REG_1, val);
13230dbe28b3SPyun YongHyeon 
13240dbe28b3SPyun YongHyeon 		val = Y2_PCI_CLK_LNK1_DIS | Y2_COR_CLK_LNK1_DIS |
13250dbe28b3SPyun YongHyeon 		      Y2_CLK_GAT_LNK1_DIS | Y2_PCI_CLK_LNK2_DIS |
13260dbe28b3SPyun YongHyeon 		      Y2_COR_CLK_LNK2_DIS | Y2_CLK_GAT_LNK2_DIS;
13270dbe28b3SPyun YongHyeon 		if (sc->msk_hw_id == CHIP_ID_YUKON_XL &&
13280dbe28b3SPyun YongHyeon 		    sc->msk_hw_rev > CHIP_REV_YU_XL_A1) {
13290dbe28b3SPyun YongHyeon 			/* Enable bits are inverted. */
13300dbe28b3SPyun YongHyeon 			val = 0;
13310dbe28b3SPyun YongHyeon 		}
13320dbe28b3SPyun YongHyeon 		/*
13330dbe28b3SPyun YongHyeon 		 * Disable PCI & Core Clock, disable clock gating for
13340dbe28b3SPyun YongHyeon 		 * both Links.
13350dbe28b3SPyun YongHyeon 		 */
13360dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, B2_Y2_CLK_GATE, val);
13370dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, B0_POWER_CTRL,
13380dbe28b3SPyun YongHyeon 		    PC_VAUX_ENA | PC_VCC_ENA | PC_VAUX_ON | PC_VCC_OFF);
13390dbe28b3SPyun YongHyeon 		break;
13400dbe28b3SPyun YongHyeon 	default:
13410dbe28b3SPyun YongHyeon 		break;
13420dbe28b3SPyun YongHyeon 	}
13430dbe28b3SPyun YongHyeon }
13440dbe28b3SPyun YongHyeon 
13450dbe28b3SPyun YongHyeon static void
mskc_reset(struct msk_softc * sc)13460dbe28b3SPyun YongHyeon mskc_reset(struct msk_softc *sc)
13470dbe28b3SPyun YongHyeon {
13480dbe28b3SPyun YongHyeon 	bus_addr_t addr;
13490dbe28b3SPyun YongHyeon 	uint16_t status;
13500dbe28b3SPyun YongHyeon 	uint32_t val;
1351d91192e3SPyun YongHyeon 	int i, initram;
13520dbe28b3SPyun YongHyeon 
13530dbe28b3SPyun YongHyeon 	/* Disable ASF. */
1354fe0b141eSPyun YongHyeon 	if (sc->msk_hw_id >= CHIP_ID_YUKON_XL &&
1355fe0b141eSPyun YongHyeon 	    sc->msk_hw_id <= CHIP_ID_YUKON_SUPR) {
1356fe0b141eSPyun YongHyeon 		if (sc->msk_hw_id == CHIP_ID_YUKON_EX ||
1357fe0b141eSPyun YongHyeon 		    sc->msk_hw_id == CHIP_ID_YUKON_SUPR) {
1358fe0b141eSPyun YongHyeon 			CSR_WRITE_4(sc, B28_Y2_CPU_WDOG, 0);
1359daf29227SPyun YongHyeon 			status = CSR_READ_2(sc, B28_Y2_ASF_HCU_CCSR);
1360daf29227SPyun YongHyeon 			/* Clear AHB bridge & microcontroller reset. */
1361daf29227SPyun YongHyeon 			status &= ~(Y2_ASF_HCU_CCSR_AHB_RST |
1362daf29227SPyun YongHyeon 			    Y2_ASF_HCU_CCSR_CPU_RST_MODE);
1363daf29227SPyun YongHyeon 			/* Clear ASF microcontroller state. */
1364daf29227SPyun YongHyeon 			status &= ~Y2_ASF_HCU_CCSR_UC_STATE_MSK;
1365fe0b141eSPyun YongHyeon 			status &= ~Y2_ASF_HCU_CCSR_CPU_CLK_DIVIDE_MSK;
1366daf29227SPyun YongHyeon 			CSR_WRITE_2(sc, B28_Y2_ASF_HCU_CCSR, status);
1367fe0b141eSPyun YongHyeon 			CSR_WRITE_4(sc, B28_Y2_CPU_WDOG, 0);
1368daf29227SPyun YongHyeon 		} else
1369daf29227SPyun YongHyeon 			CSR_WRITE_1(sc, B28_Y2_ASF_STAT_CMD, Y2_ASF_RESET);
13700dbe28b3SPyun YongHyeon 		CSR_WRITE_2(sc, B0_CTST, Y2_ASF_DISABLE);
13710dbe28b3SPyun YongHyeon 		/*
1372fe0b141eSPyun YongHyeon 		 * Since we disabled ASF, S/W reset is required for
1373fe0b141eSPyun YongHyeon 		 * Power Management.
13740dbe28b3SPyun YongHyeon 		 */
13750dbe28b3SPyun YongHyeon 		CSR_WRITE_2(sc, B0_CTST, CS_RST_SET);
13760dbe28b3SPyun YongHyeon 		CSR_WRITE_2(sc, B0_CTST, CS_RST_CLR);
1377fe0b141eSPyun YongHyeon 	}
13780dbe28b3SPyun YongHyeon 
13790dbe28b3SPyun YongHyeon 	/* Clear all error bits in the PCI status register. */
13800dbe28b3SPyun YongHyeon 	status = pci_read_config(sc->msk_dev, PCIR_STATUS, 2);
13810dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_ON);
13820dbe28b3SPyun YongHyeon 
13830dbe28b3SPyun YongHyeon 	pci_write_config(sc->msk_dev, PCIR_STATUS, status |
13840dbe28b3SPyun YongHyeon 	    PCIM_STATUS_PERR | PCIM_STATUS_SERR | PCIM_STATUS_RMABORT |
1385d1a02e09SJohn Baldwin 	    PCIM_STATUS_RTABORT | PCIM_STATUS_MDPERR, 2);
13860dbe28b3SPyun YongHyeon 	CSR_WRITE_2(sc, B0_CTST, CS_MRST_CLR);
13870dbe28b3SPyun YongHyeon 
13880dbe28b3SPyun YongHyeon 	switch (sc->msk_bustype) {
13890dbe28b3SPyun YongHyeon 	case MSK_PEX_BUS:
13900dbe28b3SPyun YongHyeon 		/* Clear all PEX errors. */
13910dbe28b3SPyun YongHyeon 		CSR_PCI_WRITE_4(sc, PEX_UNC_ERR_STAT, 0xffffffff);
13920dbe28b3SPyun YongHyeon 		val = CSR_PCI_READ_4(sc, PEX_UNC_ERR_STAT);
13930dbe28b3SPyun YongHyeon 		if ((val & PEX_RX_OV) != 0) {
13940dbe28b3SPyun YongHyeon 			sc->msk_intrmask &= ~Y2_IS_HW_ERR;
13950dbe28b3SPyun YongHyeon 			sc->msk_intrhwemask &= ~Y2_IS_PCI_EXP;
13960dbe28b3SPyun YongHyeon 		}
13970dbe28b3SPyun YongHyeon 		break;
13980dbe28b3SPyun YongHyeon 	case MSK_PCI_BUS:
13990dbe28b3SPyun YongHyeon 	case MSK_PCIX_BUS:
14000dbe28b3SPyun YongHyeon 		/* Set Cache Line Size to 2(8bytes) if configured to 0. */
14010dbe28b3SPyun YongHyeon 		val = pci_read_config(sc->msk_dev, PCIR_CACHELNSZ, 1);
14020dbe28b3SPyun YongHyeon 		if (val == 0)
14030dbe28b3SPyun YongHyeon 			pci_write_config(sc->msk_dev, PCIR_CACHELNSZ, 2, 1);
14040dbe28b3SPyun YongHyeon 		if (sc->msk_bustype == MSK_PCIX_BUS) {
14050dbe28b3SPyun YongHyeon 			/* Set Cache Line Size opt. */
14060dbe28b3SPyun YongHyeon 			val = pci_read_config(sc->msk_dev, PCI_OUR_REG_1, 4);
14070dbe28b3SPyun YongHyeon 			val |= PCI_CLS_OPT;
14080dbe28b3SPyun YongHyeon 			pci_write_config(sc->msk_dev, PCI_OUR_REG_1, val, 4);
14090dbe28b3SPyun YongHyeon 		}
14100dbe28b3SPyun YongHyeon 		break;
14110dbe28b3SPyun YongHyeon 	}
14120dbe28b3SPyun YongHyeon 	/* Set PHY power state. */
14130dbe28b3SPyun YongHyeon 	msk_phy_power(sc, MSK_PHY_POWERUP);
14140dbe28b3SPyun YongHyeon 
14150dbe28b3SPyun YongHyeon 	/* Reset GPHY/GMAC Control */
14160dbe28b3SPyun YongHyeon 	for (i = 0; i < sc->msk_num_port; i++) {
14170dbe28b3SPyun YongHyeon 		/* GPHY Control reset. */
141810e71e22SPyun YongHyeon 		CSR_WRITE_1(sc, MR_ADDR(i, GPHY_CTRL), GPC_RST_SET);
141910e71e22SPyun YongHyeon 		CSR_WRITE_1(sc, MR_ADDR(i, GPHY_CTRL), GPC_RST_CLR);
14200dbe28b3SPyun YongHyeon 		/* GMAC Control reset. */
14210dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(i, GMAC_CTRL), GMC_RST_SET);
14220dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(i, GMAC_CTRL), GMC_RST_CLR);
14230dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(i, GMAC_CTRL), GMC_F_LOOPB_OFF);
1424e0029a72SPyun YongHyeon 		if (sc->msk_hw_id == CHIP_ID_YUKON_EX ||
1425e0029a72SPyun YongHyeon 		    sc->msk_hw_id == CHIP_ID_YUKON_SUPR)
1426daf29227SPyun YongHyeon 			CSR_WRITE_4(sc, MR_ADDR(i, GMAC_CTRL),
1427daf29227SPyun YongHyeon 			    GMC_BYP_MACSECRX_ON | GMC_BYP_MACSECTX_ON |
1428daf29227SPyun YongHyeon 			    GMC_BYP_RETR_ON);
14290dbe28b3SPyun YongHyeon 	}
1430e0029a72SPyun YongHyeon 
1431e0029a72SPyun YongHyeon 	if (sc->msk_hw_id == CHIP_ID_YUKON_SUPR &&
1432e0029a72SPyun YongHyeon 	    sc->msk_hw_rev > CHIP_REV_YU_SU_B0)
1433e0029a72SPyun YongHyeon 		CSR_PCI_WRITE_4(sc, PCI_OUR_REG_3, PCI_CLK_MACSEC_DIS);
1434e19bd6eeSPyun YongHyeon 	if (sc->msk_hw_id == CHIP_ID_YUKON_OPT && sc->msk_hw_rev == 0) {
1435e19bd6eeSPyun YongHyeon 		/* Disable PCIe PHY powerdown(reg 0x80, bit7). */
1436e19bd6eeSPyun YongHyeon 		CSR_WRITE_4(sc, Y2_PEX_PHY_DATA, (0x0080 << 16) | 0x0080);
1437e19bd6eeSPyun YongHyeon 	}
14380dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
14390dbe28b3SPyun YongHyeon 
14400dbe28b3SPyun YongHyeon 	/* LED On. */
14410dbe28b3SPyun YongHyeon 	CSR_WRITE_2(sc, B0_CTST, Y2_LED_STAT_ON);
14420dbe28b3SPyun YongHyeon 
14430dbe28b3SPyun YongHyeon 	/* Clear TWSI IRQ. */
14440dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, B2_I2C_IRQ, I2C_CLR_IRQ);
14450dbe28b3SPyun YongHyeon 
14460dbe28b3SPyun YongHyeon 	/* Turn off hardware timer. */
14470dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, B2_TI_CTRL, TIM_STOP);
14480dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, B2_TI_CTRL, TIM_CLR_IRQ);
14490dbe28b3SPyun YongHyeon 
14500dbe28b3SPyun YongHyeon 	/* Turn off descriptor polling. */
14510dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, B28_DPT_CTRL, DPT_STOP);
14520dbe28b3SPyun YongHyeon 
14530dbe28b3SPyun YongHyeon 	/* Turn off time stamps. */
14540dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, GMAC_TI_ST_CTRL, GMT_ST_STOP);
14550dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, GMAC_TI_ST_CTRL, GMT_ST_CLR_IRQ);
14560dbe28b3SPyun YongHyeon 
1457d91192e3SPyun YongHyeon 	initram = 0;
1458d91192e3SPyun YongHyeon 	if (sc->msk_hw_id == CHIP_ID_YUKON_XL ||
1459d91192e3SPyun YongHyeon 	    sc->msk_hw_id == CHIP_ID_YUKON_EC ||
1460d91192e3SPyun YongHyeon 	    sc->msk_hw_id == CHIP_ID_YUKON_FE)
1461d91192e3SPyun YongHyeon 		initram++;
1462d91192e3SPyun YongHyeon 
14630dbe28b3SPyun YongHyeon 	/* Configure timeout values. */
1464d91192e3SPyun YongHyeon 	for (i = 0; initram > 0 && i < sc->msk_num_port; i++) {
14650dbe28b3SPyun YongHyeon 		CSR_WRITE_2(sc, SELECT_RAM_BUFFER(i, B3_RI_CTRL), RI_RST_SET);
14660dbe28b3SPyun YongHyeon 		CSR_WRITE_2(sc, SELECT_RAM_BUFFER(i, B3_RI_CTRL), RI_RST_CLR);
14670dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_WTO_R1),
14680dbe28b3SPyun YongHyeon 		    MSK_RI_TO_53);
14690dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_WTO_XA1),
14700dbe28b3SPyun YongHyeon 		    MSK_RI_TO_53);
14710dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_WTO_XS1),
14720dbe28b3SPyun YongHyeon 		    MSK_RI_TO_53);
14730dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_RTO_R1),
14740dbe28b3SPyun YongHyeon 		    MSK_RI_TO_53);
14750dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_RTO_XA1),
14760dbe28b3SPyun YongHyeon 		    MSK_RI_TO_53);
14770dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_RTO_XS1),
14780dbe28b3SPyun YongHyeon 		    MSK_RI_TO_53);
14790dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_WTO_R2),
14800dbe28b3SPyun YongHyeon 		    MSK_RI_TO_53);
14810dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_WTO_XA2),
14820dbe28b3SPyun YongHyeon 		    MSK_RI_TO_53);
14830dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_WTO_XS2),
14840dbe28b3SPyun YongHyeon 		    MSK_RI_TO_53);
14850dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_RTO_R2),
14860dbe28b3SPyun YongHyeon 		    MSK_RI_TO_53);
14870dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_RTO_XA2),
14880dbe28b3SPyun YongHyeon 		    MSK_RI_TO_53);
14890dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_RTO_XS2),
14900dbe28b3SPyun YongHyeon 		    MSK_RI_TO_53);
14910dbe28b3SPyun YongHyeon 	}
14920dbe28b3SPyun YongHyeon 
14930dbe28b3SPyun YongHyeon 	/* Disable all interrupts. */
14940dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, B0_HWE_IMSK, 0);
14950dbe28b3SPyun YongHyeon 	CSR_READ_4(sc, B0_HWE_IMSK);
14960dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, B0_IMSK, 0);
14970dbe28b3SPyun YongHyeon 	CSR_READ_4(sc, B0_IMSK);
14980dbe28b3SPyun YongHyeon 
14990dbe28b3SPyun YongHyeon         /*
15000dbe28b3SPyun YongHyeon          * On dual port PCI-X card, there is an problem where status
15010dbe28b3SPyun YongHyeon          * can be received out of order due to split transactions.
15020dbe28b3SPyun YongHyeon          */
15037420e9dcSPyun YongHyeon 	if (sc->msk_pcixcap != 0 && sc->msk_num_port > 1) {
15040dbe28b3SPyun YongHyeon 		uint16_t pcix_cmd;
15050dbe28b3SPyun YongHyeon 
15067420e9dcSPyun YongHyeon 		pcix_cmd = pci_read_config(sc->msk_dev,
15077420e9dcSPyun YongHyeon 		    sc->msk_pcixcap + PCIXR_COMMAND, 2);
15080dbe28b3SPyun YongHyeon 		/* Clear Max Outstanding Split Transactions. */
15097420e9dcSPyun YongHyeon 		pcix_cmd &= ~PCIXM_COMMAND_MAX_SPLITS;
15100dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_ON);
15117420e9dcSPyun YongHyeon 		pci_write_config(sc->msk_dev,
15127420e9dcSPyun YongHyeon 		    sc->msk_pcixcap + PCIXR_COMMAND, pcix_cmd, 2);
15130dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
15140dbe28b3SPyun YongHyeon         }
15157420e9dcSPyun YongHyeon 	if (sc->msk_expcap != 0) {
15167420e9dcSPyun YongHyeon 		/* Change Max. Read Request Size to 2048 bytes. */
15177420e9dcSPyun YongHyeon 		if (pci_get_max_read_req(sc->msk_dev) == 512)
15187420e9dcSPyun YongHyeon 			pci_set_max_read_req(sc->msk_dev, 2048);
15190dbe28b3SPyun YongHyeon 	}
15200dbe28b3SPyun YongHyeon 
15210dbe28b3SPyun YongHyeon 	/* Clear status list. */
15220dbe28b3SPyun YongHyeon 	bzero(sc->msk_stat_ring,
1523355a415eSPyun YongHyeon 	    sizeof(struct msk_stat_desc) * sc->msk_stat_count);
15240dbe28b3SPyun YongHyeon 	sc->msk_stat_cons = 0;
15250dbe28b3SPyun YongHyeon 	bus_dmamap_sync(sc->msk_stat_tag, sc->msk_stat_map,
15260dbe28b3SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
15270dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, STAT_CTRL, SC_STAT_RST_SET);
15280dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, STAT_CTRL, SC_STAT_RST_CLR);
15290dbe28b3SPyun YongHyeon 	/* Set the status list base address. */
15300dbe28b3SPyun YongHyeon 	addr = sc->msk_stat_ring_paddr;
15310dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, STAT_LIST_ADDR_LO, MSK_ADDR_LO(addr));
15320dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, STAT_LIST_ADDR_HI, MSK_ADDR_HI(addr));
15330dbe28b3SPyun YongHyeon 	/* Set the status list last index. */
1534355a415eSPyun YongHyeon 	CSR_WRITE_2(sc, STAT_LAST_IDX, sc->msk_stat_count - 1);
1535cfd540e7SPyun YongHyeon 	if (sc->msk_hw_id == CHIP_ID_YUKON_EC &&
1536cfd540e7SPyun YongHyeon 	    sc->msk_hw_rev == CHIP_REV_YU_EC_A1) {
15370dbe28b3SPyun YongHyeon 		/* WA for dev. #4.3 */
15380dbe28b3SPyun YongHyeon 		CSR_WRITE_2(sc, STAT_TX_IDX_TH, ST_TXTH_IDX_MASK);
15390dbe28b3SPyun YongHyeon 		/* WA for dev. #4.18 */
15400dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, STAT_FIFO_WM, 0x21);
15410dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, STAT_FIFO_ISR_WM, 0x07);
15420dbe28b3SPyun YongHyeon 	} else {
15430dbe28b3SPyun YongHyeon 		CSR_WRITE_2(sc, STAT_TX_IDX_TH, 0x0a);
15440dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, STAT_FIFO_WM, 0x10);
1545cfd540e7SPyun YongHyeon 		if (sc->msk_hw_id == CHIP_ID_YUKON_XL &&
1546cfd540e7SPyun YongHyeon 		    sc->msk_hw_rev == CHIP_REV_YU_XL_A0)
1547cfd540e7SPyun YongHyeon 			CSR_WRITE_1(sc, STAT_FIFO_ISR_WM, 0x04);
1548cfd540e7SPyun YongHyeon 		else
1549cfd540e7SPyun YongHyeon 			CSR_WRITE_1(sc, STAT_FIFO_ISR_WM, 0x10);
15500dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, STAT_ISR_TIMER_INI, 0x0190);
15510dbe28b3SPyun YongHyeon 	}
15520dbe28b3SPyun YongHyeon 	/*
15530dbe28b3SPyun YongHyeon 	 * Use default value for STAT_ISR_TIMER_INI, STAT_LEV_TIMER_INI.
15540dbe28b3SPyun YongHyeon 	 */
15550dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, STAT_TX_TIMER_INI, MSK_USECS(sc, 1000));
15560dbe28b3SPyun YongHyeon 
15570dbe28b3SPyun YongHyeon 	/* Enable status unit. */
15580dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, STAT_CTRL, SC_STAT_OP_ON);
15590dbe28b3SPyun YongHyeon 
15600dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, STAT_TX_TIMER_CTRL, TIM_START);
15610dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, STAT_LEV_TIMER_CTRL, TIM_START);
15620dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, STAT_ISR_TIMER_CTRL, TIM_START);
15630dbe28b3SPyun YongHyeon }
15640dbe28b3SPyun YongHyeon 
15650dbe28b3SPyun YongHyeon static int
msk_probe(device_t dev)15660dbe28b3SPyun YongHyeon msk_probe(device_t dev)
15670dbe28b3SPyun YongHyeon {
15680dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
15690dbe28b3SPyun YongHyeon 
15700dbe28b3SPyun YongHyeon 	sc = device_get_softc(device_get_parent(dev));
15710dbe28b3SPyun YongHyeon 	/*
15720dbe28b3SPyun YongHyeon 	 * Not much to do here. We always know there will be
15730dbe28b3SPyun YongHyeon 	 * at least one GMAC present, and if there are two,
15740dbe28b3SPyun YongHyeon 	 * mskc_attach() will create a second device instance
15750dbe28b3SPyun YongHyeon 	 * for us.
15760dbe28b3SPyun YongHyeon 	 */
1577443f3348SMark Johnston 	device_set_descf(dev,
15780dbe28b3SPyun YongHyeon 	    "Marvell Technology Group Ltd. %s Id 0x%02x Rev 0x%02x",
15790dbe28b3SPyun YongHyeon 	    model_name[sc->msk_hw_id - CHIP_ID_YUKON_XL], sc->msk_hw_id,
15800dbe28b3SPyun YongHyeon 	    sc->msk_hw_rev);
15810dbe28b3SPyun YongHyeon 
15820dbe28b3SPyun YongHyeon 	return (BUS_PROBE_DEFAULT);
15830dbe28b3SPyun YongHyeon }
15840dbe28b3SPyun YongHyeon 
15850dbe28b3SPyun YongHyeon static int
msk_attach(device_t dev)15860dbe28b3SPyun YongHyeon msk_attach(device_t dev)
15870dbe28b3SPyun YongHyeon {
15880dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
15890dbe28b3SPyun YongHyeon 	struct msk_if_softc *sc_if;
15905ab8c4b8SJustin Hibbits 	if_t ifp;
1591fcb62a8bSPyun YongHyeon 	struct msk_mii_data *mmd;
15920dbe28b3SPyun YongHyeon 	int i, port, error;
15930dbe28b3SPyun YongHyeon 	uint8_t eaddr[6];
15940dbe28b3SPyun YongHyeon 
15950dbe28b3SPyun YongHyeon 	if (dev == NULL)
15960dbe28b3SPyun YongHyeon 		return (EINVAL);
15970dbe28b3SPyun YongHyeon 
15980dbe28b3SPyun YongHyeon 	error = 0;
15990dbe28b3SPyun YongHyeon 	sc_if = device_get_softc(dev);
16000dbe28b3SPyun YongHyeon 	sc = device_get_softc(device_get_parent(dev));
1601fcb62a8bSPyun YongHyeon 	mmd = device_get_ivars(dev);
1602fcb62a8bSPyun YongHyeon 	port = mmd->port;
16030dbe28b3SPyun YongHyeon 
16040dbe28b3SPyun YongHyeon 	sc_if->msk_if_dev = dev;
16050dbe28b3SPyun YongHyeon 	sc_if->msk_port = port;
16060dbe28b3SPyun YongHyeon 	sc_if->msk_softc = sc;
160783c04c93SPyun YongHyeon 	sc_if->msk_flags = sc->msk_pflags;
16080dbe28b3SPyun YongHyeon 	sc->msk_if[port] = sc_if;
16090dbe28b3SPyun YongHyeon 	/* Setup Tx/Rx queue register offsets. */
16100dbe28b3SPyun YongHyeon 	if (port == MSK_PORT_A) {
16110dbe28b3SPyun YongHyeon 		sc_if->msk_txq = Q_XA1;
16120dbe28b3SPyun YongHyeon 		sc_if->msk_txsq = Q_XS1;
16130dbe28b3SPyun YongHyeon 		sc_if->msk_rxq = Q_R1;
16140dbe28b3SPyun YongHyeon 	} else {
16150dbe28b3SPyun YongHyeon 		sc_if->msk_txq = Q_XA2;
16160dbe28b3SPyun YongHyeon 		sc_if->msk_txsq = Q_XS2;
16170dbe28b3SPyun YongHyeon 		sc_if->msk_rxq = Q_R2;
16180dbe28b3SPyun YongHyeon 	}
16190dbe28b3SPyun YongHyeon 
16200dbe28b3SPyun YongHyeon 	callout_init_mtx(&sc_if->msk_tick_ch, &sc_if->msk_softc->msk_mtx, 0);
16213a91ee71SPyun YongHyeon 	msk_sysctl_node(sc_if);
16220dbe28b3SPyun YongHyeon 
16239dda5c8fSPyun YongHyeon 	if ((error = msk_txrx_dma_alloc(sc_if)) != 0)
16240dbe28b3SPyun YongHyeon 		goto fail;
162585b340cbSPyun YongHyeon 	msk_rx_dma_jalloc(sc_if);
16260dbe28b3SPyun YongHyeon 
16270dbe28b3SPyun YongHyeon 	ifp = sc_if->msk_ifp = if_alloc(IFT_ETHER);
16285ab8c4b8SJustin Hibbits 	if_setsoftc(ifp, sc_if);
16290dbe28b3SPyun YongHyeon 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
16305ab8c4b8SJustin Hibbits 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
16315ab8c4b8SJustin Hibbits 	if_setcapabilities(ifp, IFCAP_TXCSUM | IFCAP_TSO4);
1632efb74172SPyun YongHyeon 	/*
1633388214e4SPyun YongHyeon 	 * Enable Rx checksum offloading if controller supports
1634388214e4SPyun YongHyeon 	 * new descriptor formant and controller is not Yukon XL.
1635efb74172SPyun YongHyeon 	 */
1636388214e4SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0 &&
1637388214e4SPyun YongHyeon 	    sc->msk_hw_id != CHIP_ID_YUKON_XL)
16385ab8c4b8SJustin Hibbits 		if_setcapabilitiesbit(ifp, IFCAP_RXCSUM, 0);
1639efb74172SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_DESCV2) != 0 &&
1640efb74172SPyun YongHyeon 	    (sc_if->msk_flags & MSK_FLAG_NORX_CSUM) == 0)
16415ab8c4b8SJustin Hibbits 		if_setcapabilitiesbit(ifp, IFCAP_RXCSUM, 0);
16425ab8c4b8SJustin Hibbits 	if_sethwassist(ifp, MSK_CSUM_FEATURES | CSUM_TSO);
16435ab8c4b8SJustin Hibbits 	if_setcapenable(ifp, if_getcapabilities(ifp));
16445ab8c4b8SJustin Hibbits 	if_setioctlfn(ifp, msk_ioctl);
16455ab8c4b8SJustin Hibbits 	if_setstartfn(ifp, msk_start);
16465ab8c4b8SJustin Hibbits 	if_setinitfn(ifp, msk_init);
16475ab8c4b8SJustin Hibbits 	if_setsendqlen(ifp, MSK_TX_RING_CNT - 1);
16485ab8c4b8SJustin Hibbits 	if_setsendqready(ifp);
16490dbe28b3SPyun YongHyeon 	/*
16500dbe28b3SPyun YongHyeon 	 * Get station address for this interface. Note that
16510dbe28b3SPyun YongHyeon 	 * dual port cards actually come with three station
16520dbe28b3SPyun YongHyeon 	 * addresses: one for each port, plus an extra. The
16530dbe28b3SPyun YongHyeon 	 * extra one is used by the SysKonnect driver software
16540dbe28b3SPyun YongHyeon 	 * as a 'virtual' station address for when both ports
16550dbe28b3SPyun YongHyeon 	 * are operating in failover mode. Currently we don't
16560dbe28b3SPyun YongHyeon 	 * use this extra address.
16570dbe28b3SPyun YongHyeon 	 */
16580dbe28b3SPyun YongHyeon 	MSK_IF_LOCK(sc_if);
16590dbe28b3SPyun YongHyeon 	for (i = 0; i < ETHER_ADDR_LEN; i++)
16600dbe28b3SPyun YongHyeon 		eaddr[i] = CSR_READ_1(sc, B2_MAC_1 + (port * 8) + i);
16610dbe28b3SPyun YongHyeon 
16620dbe28b3SPyun YongHyeon 	/*
16630dbe28b3SPyun YongHyeon 	 * Call MI attach routine.  Can't hold locks when calling into ether_*.
16640dbe28b3SPyun YongHyeon 	 */
16650dbe28b3SPyun YongHyeon 	MSK_IF_UNLOCK(sc_if);
16660dbe28b3SPyun YongHyeon 	ether_ifattach(ifp, eaddr);
16670dbe28b3SPyun YongHyeon 	MSK_IF_LOCK(sc_if);
16680dbe28b3SPyun YongHyeon 
1669224003b7SPyun YongHyeon 	/* VLAN capability setup */
16705ab8c4b8SJustin Hibbits 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU, 0);
1671224003b7SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_NOHWVLAN) == 0) {
167206ff0944SPyun YongHyeon 		/*
167306ff0944SPyun YongHyeon 		 * Due to Tx checksum offload hardware bugs, msk(4) manually
167406ff0944SPyun YongHyeon 		 * computes checksum for short frames. For VLAN tagged frames
167506ff0944SPyun YongHyeon 		 * this workaround does not work so disable checksum offload
167606ff0944SPyun YongHyeon 		 * for VLAN interface.
167706ff0944SPyun YongHyeon 		 */
16785ab8c4b8SJustin Hibbits 		if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWTSO, 0);
1679efb74172SPyun YongHyeon 		/*
1680b1ce21c6SRebecca Cran 		 * Enable Rx checksum offloading for VLAN tagged frames
1681efb74172SPyun YongHyeon 		 * if controller support new descriptor format.
1682efb74172SPyun YongHyeon 		 */
1683efb74172SPyun YongHyeon 		if ((sc_if->msk_flags & MSK_FLAG_DESCV2) != 0 &&
1684efb74172SPyun YongHyeon 		    (sc_if->msk_flags & MSK_FLAG_NORX_CSUM) == 0)
16855ab8c4b8SJustin Hibbits 			if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWCSUM, 0);
1686224003b7SPyun YongHyeon 	}
16875ab8c4b8SJustin Hibbits 	if_setcapenable(ifp, if_getcapabilities(ifp));
16881c3515d2SPyun YongHyeon 	/*
16891c3515d2SPyun YongHyeon 	 * Disable RX checksum offloading on controllers that don't use
16901c3515d2SPyun YongHyeon 	 * new descriptor format but give chance to enable it.
16911c3515d2SPyun YongHyeon 	 */
16921c3515d2SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0)
16935ab8c4b8SJustin Hibbits 		if_setcapenablebit(ifp, 0, IFCAP_RXCSUM);
16940dbe28b3SPyun YongHyeon 
16950dbe28b3SPyun YongHyeon 	/*
16960dbe28b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
16970dbe28b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
16980dbe28b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
16990dbe28b3SPyun YongHyeon 	 */
17005ab8c4b8SJustin Hibbits         if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
17010dbe28b3SPyun YongHyeon 
17020dbe28b3SPyun YongHyeon 	/*
17030dbe28b3SPyun YongHyeon 	 * Do miibus setup.
17040dbe28b3SPyun YongHyeon 	 */
17050dbe28b3SPyun YongHyeon 	MSK_IF_UNLOCK(sc_if);
17068e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc_if->msk_miibus, ifp, msk_mediachange,
17078e5d93dbSMarius Strobl 	    msk_mediastatus, BMSR_DEFCAPMASK, PHY_ADDR_MARV, MII_OFFSET_ANY,
17088e5d93dbSMarius Strobl 	    mmd->mii_flags);
17090dbe28b3SPyun YongHyeon 	if (error != 0) {
17108e5d93dbSMarius Strobl 		device_printf(sc_if->msk_if_dev, "attaching PHYs failed\n");
17110dbe28b3SPyun YongHyeon 		ether_ifdetach(ifp);
17120dbe28b3SPyun YongHyeon 		error = ENXIO;
17130dbe28b3SPyun YongHyeon 		goto fail;
17140dbe28b3SPyun YongHyeon 	}
17150dbe28b3SPyun YongHyeon 
17160dbe28b3SPyun YongHyeon fail:
17170dbe28b3SPyun YongHyeon 	if (error != 0) {
17180dbe28b3SPyun YongHyeon 		/* Access should be ok even though lock has been dropped */
17190dbe28b3SPyun YongHyeon 		sc->msk_if[port] = NULL;
17200dbe28b3SPyun YongHyeon 		msk_detach(dev);
17210dbe28b3SPyun YongHyeon 	}
17220dbe28b3SPyun YongHyeon 
17230dbe28b3SPyun YongHyeon 	return (error);
17240dbe28b3SPyun YongHyeon }
17250dbe28b3SPyun YongHyeon 
17260dbe28b3SPyun YongHyeon /*
17270dbe28b3SPyun YongHyeon  * Attach the interface. Allocate softc structures, do ifmedia
17280dbe28b3SPyun YongHyeon  * setup and ethernet/BPF attach.
17290dbe28b3SPyun YongHyeon  */
17300dbe28b3SPyun YongHyeon static int
mskc_attach(device_t dev)17310dbe28b3SPyun YongHyeon mskc_attach(device_t dev)
17320dbe28b3SPyun YongHyeon {
17330dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
1734fcb62a8bSPyun YongHyeon 	struct msk_mii_data *mmd;
1735fcb62a8bSPyun YongHyeon 	int error, msic, msir, reg;
17360dbe28b3SPyun YongHyeon 
17370dbe28b3SPyun YongHyeon 	sc = device_get_softc(dev);
17380dbe28b3SPyun YongHyeon 	sc->msk_dev = dev;
17390dbe28b3SPyun YongHyeon 	mtx_init(&sc->msk_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
17400dbe28b3SPyun YongHyeon 	    MTX_DEF);
17410dbe28b3SPyun YongHyeon 
17420dbe28b3SPyun YongHyeon 	/*
17430dbe28b3SPyun YongHyeon 	 * Map control/status registers.
17440dbe28b3SPyun YongHyeon 	 */
17450dbe28b3SPyun YongHyeon 	pci_enable_busmaster(dev);
17460dbe28b3SPyun YongHyeon 
1747298946a9SPyun YongHyeon 	/* Allocate I/O resource */
17480dbe28b3SPyun YongHyeon #ifdef MSK_USEIOSPACE
17490dbe28b3SPyun YongHyeon 	sc->msk_res_spec = msk_res_spec_io;
17500dbe28b3SPyun YongHyeon #else
17510dbe28b3SPyun YongHyeon 	sc->msk_res_spec = msk_res_spec_mem;
17520dbe28b3SPyun YongHyeon #endif
1753a485f97aSPyun YongHyeon 	sc->msk_irq_spec = msk_irq_spec_legacy;
17540dbe28b3SPyun YongHyeon 	error = bus_alloc_resources(dev, sc->msk_res_spec, sc->msk_res);
17550dbe28b3SPyun YongHyeon 	if (error) {
17560dbe28b3SPyun YongHyeon 		if (sc->msk_res_spec == msk_res_spec_mem)
17570dbe28b3SPyun YongHyeon 			sc->msk_res_spec = msk_res_spec_io;
17580dbe28b3SPyun YongHyeon 		else
17590dbe28b3SPyun YongHyeon 			sc->msk_res_spec = msk_res_spec_mem;
17600dbe28b3SPyun YongHyeon 		error = bus_alloc_resources(dev, sc->msk_res_spec, sc->msk_res);
17610dbe28b3SPyun YongHyeon 		if (error) {
17620dbe28b3SPyun YongHyeon 			device_printf(dev, "couldn't allocate %s resources\n",
17630dbe28b3SPyun YongHyeon 			    sc->msk_res_spec == msk_res_spec_mem ? "memory" :
17640dbe28b3SPyun YongHyeon 			    "I/O");
17650dbe28b3SPyun YongHyeon 			mtx_destroy(&sc->msk_mtx);
17660dbe28b3SPyun YongHyeon 			return (ENXIO);
17670dbe28b3SPyun YongHyeon 		}
17680dbe28b3SPyun YongHyeon 	}
17690dbe28b3SPyun YongHyeon 
1770c6a34f76SPyun YongHyeon 	/* Enable all clocks before accessing any registers. */
1771c6a34f76SPyun YongHyeon 	CSR_PCI_WRITE_4(sc, PCI_OUR_REG_3, 0);
1772c6a34f76SPyun YongHyeon 
17730dbe28b3SPyun YongHyeon 	CSR_WRITE_2(sc, B0_CTST, CS_RST_CLR);
17740dbe28b3SPyun YongHyeon 	sc->msk_hw_id = CSR_READ_1(sc, B2_CHIP_ID);
17750dbe28b3SPyun YongHyeon 	sc->msk_hw_rev = (CSR_READ_1(sc, B2_MAC_CFG) >> 4) & 0x0f;
17760dbe28b3SPyun YongHyeon 	/* Bail out if chip is not recognized. */
17770dbe28b3SPyun YongHyeon 	if (sc->msk_hw_id < CHIP_ID_YUKON_XL ||
1778e19bd6eeSPyun YongHyeon 	    sc->msk_hw_id > CHIP_ID_YUKON_OPT ||
1779e19bd6eeSPyun YongHyeon 	    sc->msk_hw_id == CHIP_ID_YUKON_UNKNOWN) {
17800dbe28b3SPyun YongHyeon 		device_printf(dev, "unknown device: id=0x%02x, rev=0x%02x\n",
17810dbe28b3SPyun YongHyeon 		    sc->msk_hw_id, sc->msk_hw_rev);
1782ad6d01d1SPyun YongHyeon 		mtx_destroy(&sc->msk_mtx);
1783ad6d01d1SPyun YongHyeon 		return (ENXIO);
17840dbe28b3SPyun YongHyeon 	}
17850dbe28b3SPyun YongHyeon 
17860dbe28b3SPyun YongHyeon 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
17870dbe28b3SPyun YongHyeon 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
17887029da5cSPawel Biernacki 	    OID_AUTO, "process_limit",
17897029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
17900dbe28b3SPyun YongHyeon 	    &sc->msk_process_limit, 0, sysctl_hw_msk_proc_limit, "I",
17910dbe28b3SPyun YongHyeon 	    "max number of Rx events to process");
17920dbe28b3SPyun YongHyeon 
17930dbe28b3SPyun YongHyeon 	sc->msk_process_limit = MSK_PROC_DEFAULT;
17940dbe28b3SPyun YongHyeon 	error = resource_int_value(device_get_name(dev), device_get_unit(dev),
17950dbe28b3SPyun YongHyeon 	    "process_limit", &sc->msk_process_limit);
17960dbe28b3SPyun YongHyeon 	if (error == 0) {
17970dbe28b3SPyun YongHyeon 		if (sc->msk_process_limit < MSK_PROC_MIN ||
17980dbe28b3SPyun YongHyeon 		    sc->msk_process_limit > MSK_PROC_MAX) {
17990dbe28b3SPyun YongHyeon 			device_printf(dev, "process_limit value out of range; "
18000dbe28b3SPyun YongHyeon 			    "using default: %d\n", MSK_PROC_DEFAULT);
18010dbe28b3SPyun YongHyeon 			sc->msk_process_limit = MSK_PROC_DEFAULT;
18020dbe28b3SPyun YongHyeon 		}
18030dbe28b3SPyun YongHyeon 	}
18040dbe28b3SPyun YongHyeon 
1805cf570c1fSPyun YongHyeon 	sc->msk_int_holdoff = MSK_INT_HOLDOFF_DEFAULT;
1806cf570c1fSPyun YongHyeon 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
1807cf570c1fSPyun YongHyeon 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1808cf570c1fSPyun YongHyeon 	    "int_holdoff", CTLFLAG_RW, &sc->msk_int_holdoff, 0,
1809cf570c1fSPyun YongHyeon 	    "Maximum number of time to delay interrupts");
1810cf570c1fSPyun YongHyeon 	resource_int_value(device_get_name(dev), device_get_unit(dev),
1811cf570c1fSPyun YongHyeon 	    "int_holdoff", &sc->msk_int_holdoff);
1812cf570c1fSPyun YongHyeon 
18130dbe28b3SPyun YongHyeon 	sc->msk_pmd = CSR_READ_1(sc, B2_PMD_TYP);
18140dbe28b3SPyun YongHyeon 	/* Check number of MACs. */
18150dbe28b3SPyun YongHyeon 	sc->msk_num_port = 1;
18160dbe28b3SPyun YongHyeon 	if ((CSR_READ_1(sc, B2_Y2_HW_RES) & CFG_DUAL_MAC_MSK) ==
18170dbe28b3SPyun YongHyeon 	    CFG_DUAL_MAC_MSK) {
18180dbe28b3SPyun YongHyeon 		if (!(CSR_READ_1(sc, B2_Y2_CLK_GATE) & Y2_STATUS_LNK2_INAC))
18190dbe28b3SPyun YongHyeon 			sc->msk_num_port++;
18200dbe28b3SPyun YongHyeon 	}
18210dbe28b3SPyun YongHyeon 
18220dbe28b3SPyun YongHyeon 	/* Check bus type. */
18233b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->msk_dev, PCIY_EXPRESS, &reg) == 0) {
18240dbe28b3SPyun YongHyeon 		sc->msk_bustype = MSK_PEX_BUS;
18257420e9dcSPyun YongHyeon 		sc->msk_expcap = reg;
18263b0a4aefSJohn Baldwin 	} else if (pci_find_cap(sc->msk_dev, PCIY_PCIX, &reg) == 0) {
18270dbe28b3SPyun YongHyeon 		sc->msk_bustype = MSK_PCIX_BUS;
18287420e9dcSPyun YongHyeon 		sc->msk_pcixcap = reg;
18297420e9dcSPyun YongHyeon 	} else
18300dbe28b3SPyun YongHyeon 		sc->msk_bustype = MSK_PCI_BUS;
18310dbe28b3SPyun YongHyeon 
18320dbe28b3SPyun YongHyeon 	switch (sc->msk_hw_id) {
18330dbe28b3SPyun YongHyeon 	case CHIP_ID_YUKON_EC:
1834a91981e4SPyun YongHyeon 		sc->msk_clock = 125;	/* 125 MHz */
1835e2b16603SPyun YongHyeon 		sc->msk_pflags |= MSK_FLAG_JUMBO;
1836e2b16603SPyun YongHyeon 		break;
18370dbe28b3SPyun YongHyeon 	case CHIP_ID_YUKON_EC_U:
1838a91981e4SPyun YongHyeon 		sc->msk_clock = 125;	/* 125 MHz */
1839e2b16603SPyun YongHyeon 		sc->msk_pflags |= MSK_FLAG_JUMBO | MSK_FLAG_JUMBO_NOCSUM;
18400dbe28b3SPyun YongHyeon 		break;
1841daf29227SPyun YongHyeon 	case CHIP_ID_YUKON_EX:
1842a91981e4SPyun YongHyeon 		sc->msk_clock = 125;	/* 125 MHz */
1843ebb25bfaSPyun YongHyeon 		sc->msk_pflags |= MSK_FLAG_JUMBO | MSK_FLAG_DESCV2 |
1844ebb25bfaSPyun YongHyeon 		    MSK_FLAG_AUTOTX_CSUM;
1845ebb25bfaSPyun YongHyeon 		/*
1846ebb25bfaSPyun YongHyeon 		 * Yukon Extreme seems to have silicon bug for
1847ebb25bfaSPyun YongHyeon 		 * automatic Tx checksum calculation capability.
1848ebb25bfaSPyun YongHyeon 		 */
1849ebb25bfaSPyun YongHyeon 		if (sc->msk_hw_rev == CHIP_REV_YU_EX_B0)
1850ebb25bfaSPyun YongHyeon 			sc->msk_pflags &= ~MSK_FLAG_AUTOTX_CSUM;
1851ebb25bfaSPyun YongHyeon 		/*
1852ebb25bfaSPyun YongHyeon 		 * Yukon Extreme A0 could not use store-and-forward
1853ebb25bfaSPyun YongHyeon 		 * for jumbo frames, so disable Tx checksum
1854ebb25bfaSPyun YongHyeon 		 * offloading for jumbo frames.
1855ebb25bfaSPyun YongHyeon 		 */
1856ebb25bfaSPyun YongHyeon 		if (sc->msk_hw_rev == CHIP_REV_YU_EX_A0)
1857ebb25bfaSPyun YongHyeon 			sc->msk_pflags |= MSK_FLAG_JUMBO_NOCSUM;
1858daf29227SPyun YongHyeon 		break;
18590dbe28b3SPyun YongHyeon 	case CHIP_ID_YUKON_FE:
1860a91981e4SPyun YongHyeon 		sc->msk_clock = 100;	/* 100 MHz */
1861e2b16603SPyun YongHyeon 		sc->msk_pflags |= MSK_FLAG_FASTETHER;
18620dbe28b3SPyun YongHyeon 		break;
186361708f4cSPyun YongHyeon 	case CHIP_ID_YUKON_FE_P:
1864a91981e4SPyun YongHyeon 		sc->msk_clock = 50;	/* 50 MHz */
1865ebb25bfaSPyun YongHyeon 		sc->msk_pflags |= MSK_FLAG_FASTETHER | MSK_FLAG_DESCV2 |
1866ebb25bfaSPyun YongHyeon 		    MSK_FLAG_AUTOTX_CSUM;
1867224003b7SPyun YongHyeon 		if (sc->msk_hw_rev == CHIP_REV_YU_FE_P_A0) {
1868224003b7SPyun YongHyeon 			/*
1869224003b7SPyun YongHyeon 			 * XXX
1870224003b7SPyun YongHyeon 			 * FE+ A0 has status LE writeback bug so msk(4)
1871224003b7SPyun YongHyeon 			 * does not rely on status word of received frame
1872224003b7SPyun YongHyeon 			 * in msk_rxeof() which in turn disables all
1873224003b7SPyun YongHyeon 			 * hardware assistance bits reported by the status
1874b1ce21c6SRebecca Cran 			 * word as well as validity of the received frame.
1875224003b7SPyun YongHyeon 			 * Just pass received frames to upper stack with
1876224003b7SPyun YongHyeon 			 * minimal test and let upper stack handle them.
1877224003b7SPyun YongHyeon 			 */
1878efb74172SPyun YongHyeon 			sc->msk_pflags |= MSK_FLAG_NOHWVLAN |
1879efb74172SPyun YongHyeon 			    MSK_FLAG_NORXCHK | MSK_FLAG_NORX_CSUM;
1880224003b7SPyun YongHyeon 		}
188161708f4cSPyun YongHyeon 		break;
18820dbe28b3SPyun YongHyeon 	case CHIP_ID_YUKON_XL:
1883a91981e4SPyun YongHyeon 		sc->msk_clock = 156;	/* 156 MHz */
1884e2b16603SPyun YongHyeon 		sc->msk_pflags |= MSK_FLAG_JUMBO;
18850dbe28b3SPyun YongHyeon 		break;
1886e0029a72SPyun YongHyeon 	case CHIP_ID_YUKON_SUPR:
1887e0029a72SPyun YongHyeon 		sc->msk_clock = 125;	/* 125 MHz */
1888e0029a72SPyun YongHyeon 		sc->msk_pflags |= MSK_FLAG_JUMBO | MSK_FLAG_DESCV2 |
1889e0029a72SPyun YongHyeon 		    MSK_FLAG_AUTOTX_CSUM;
1890e0029a72SPyun YongHyeon 		break;
189176202a16SPyun YongHyeon 	case CHIP_ID_YUKON_UL_2:
189284e3651eSPyun YongHyeon 		sc->msk_clock = 125;	/* 125 MHz */
189376202a16SPyun YongHyeon 		sc->msk_pflags |= MSK_FLAG_JUMBO;
189476202a16SPyun YongHyeon 		break;
1895e19bd6eeSPyun YongHyeon 	case CHIP_ID_YUKON_OPT:
1896e19bd6eeSPyun YongHyeon 		sc->msk_clock = 125;	/* 125 MHz */
1897e19bd6eeSPyun YongHyeon 		sc->msk_pflags |= MSK_FLAG_JUMBO | MSK_FLAG_DESCV2;
1898e19bd6eeSPyun YongHyeon 		break;
18990dbe28b3SPyun YongHyeon 	default:
1900a91981e4SPyun YongHyeon 		sc->msk_clock = 156;	/* 156 MHz */
1901cfd540e7SPyun YongHyeon 		break;
19020dbe28b3SPyun YongHyeon 	}
19030dbe28b3SPyun YongHyeon 
1904298946a9SPyun YongHyeon 	/* Allocate IRQ resources. */
1905298946a9SPyun YongHyeon 	msic = pci_msi_count(dev);
1906298946a9SPyun YongHyeon 	if (bootverbose)
1907298946a9SPyun YongHyeon 		device_printf(dev, "MSI count : %d\n", msic);
190853dcfbd1SPyun YongHyeon 	if (legacy_intr != 0)
190953dcfbd1SPyun YongHyeon 		msi_disable = 1;
1910c72f075aSPyun YongHyeon 	if (msi_disable == 0 && msic > 0) {
1911c72f075aSPyun YongHyeon 		msir = 1;
1912c72f075aSPyun YongHyeon 		if (pci_alloc_msi(dev, &msir) == 0) {
1913c72f075aSPyun YongHyeon 			if (msir == 1) {
19147a76e8a4SPyun YongHyeon 				sc->msk_pflags |= MSK_FLAG_MSI;
1915c72f075aSPyun YongHyeon 				sc->msk_irq_spec = msk_irq_spec_msi;
19166ec27c17SPyun YongHyeon 			} else
1917298946a9SPyun YongHyeon 				pci_release_msi(dev);
1918298946a9SPyun YongHyeon 		}
19198463d7a0SPyun YongHyeon 	}
1920298946a9SPyun YongHyeon 
1921298946a9SPyun YongHyeon 	error = bus_alloc_resources(dev, sc->msk_irq_spec, sc->msk_irq);
1922298946a9SPyun YongHyeon 	if (error) {
1923298946a9SPyun YongHyeon 		device_printf(dev, "couldn't allocate IRQ resources\n");
1924298946a9SPyun YongHyeon 		goto fail;
1925298946a9SPyun YongHyeon 	}
1926298946a9SPyun YongHyeon 
19270dbe28b3SPyun YongHyeon 	if ((error = msk_status_dma_alloc(sc)) != 0)
19280dbe28b3SPyun YongHyeon 		goto fail;
19290dbe28b3SPyun YongHyeon 
19300dbe28b3SPyun YongHyeon 	/* Set base interrupt mask. */
19310dbe28b3SPyun YongHyeon 	sc->msk_intrmask = Y2_IS_HW_ERR | Y2_IS_STAT_BMU;
19320dbe28b3SPyun YongHyeon 	sc->msk_intrhwemask = Y2_IS_TIST_OV | Y2_IS_MST_ERR |
19330dbe28b3SPyun YongHyeon 	    Y2_IS_IRQ_STAT | Y2_IS_PCI_EXP | Y2_IS_PCI_NEXP;
19340dbe28b3SPyun YongHyeon 
19350dbe28b3SPyun YongHyeon 	/* Reset the adapter. */
19360dbe28b3SPyun YongHyeon 	mskc_reset(sc);
19370dbe28b3SPyun YongHyeon 
19380dbe28b3SPyun YongHyeon 	if ((error = mskc_setup_rambuffer(sc)) != 0)
19390dbe28b3SPyun YongHyeon 		goto fail;
19400dbe28b3SPyun YongHyeon 
19415b56413dSWarner Losh 	sc->msk_devs[MSK_PORT_A] = device_add_child(dev, "msk", DEVICE_UNIT_ANY);
19420dbe28b3SPyun YongHyeon 	if (sc->msk_devs[MSK_PORT_A] == NULL) {
19430dbe28b3SPyun YongHyeon 		device_printf(dev, "failed to add child for PORT_A\n");
19440dbe28b3SPyun YongHyeon 		error = ENXIO;
19450dbe28b3SPyun YongHyeon 		goto fail;
19460dbe28b3SPyun YongHyeon 	}
1947fcb62a8bSPyun YongHyeon 	mmd = malloc(sizeof(struct msk_mii_data), M_DEVBUF, M_WAITOK | M_ZERO);
1948fcb62a8bSPyun YongHyeon 	mmd->port = MSK_PORT_A;
1949fcb62a8bSPyun YongHyeon 	mmd->pmd = sc->msk_pmd;
1950efd4fc3fSMarius Strobl 	mmd->mii_flags |= MIIF_DOPAUSE;
19518e5d93dbSMarius Strobl 	if (sc->msk_pmd == 'L' || sc->msk_pmd == 'S')
1952fcb62a8bSPyun YongHyeon 		mmd->mii_flags |= MIIF_HAVEFIBER;
19538e5d93dbSMarius Strobl 	if (sc->msk_pmd == 'P')
19548e5d93dbSMarius Strobl 		mmd->mii_flags |= MIIF_HAVEFIBER | MIIF_MACPRIV0;
1955fcb62a8bSPyun YongHyeon 	device_set_ivars(sc->msk_devs[MSK_PORT_A], mmd);
19560dbe28b3SPyun YongHyeon 
19570dbe28b3SPyun YongHyeon 	if (sc->msk_num_port > 1) {
19585b56413dSWarner Losh 		sc->msk_devs[MSK_PORT_B] = device_add_child(dev, "msk", DEVICE_UNIT_ANY);
19590dbe28b3SPyun YongHyeon 		if (sc->msk_devs[MSK_PORT_B] == NULL) {
19600dbe28b3SPyun YongHyeon 			device_printf(dev, "failed to add child for PORT_B\n");
19610dbe28b3SPyun YongHyeon 			error = ENXIO;
19620dbe28b3SPyun YongHyeon 			goto fail;
19630dbe28b3SPyun YongHyeon 		}
196481e2a01aSPyun YongHyeon 		mmd = malloc(sizeof(struct msk_mii_data), M_DEVBUF, M_WAITOK |
196581e2a01aSPyun YongHyeon 		    M_ZERO);
1966fcb62a8bSPyun YongHyeon 		mmd->port = MSK_PORT_B;
1967fcb62a8bSPyun YongHyeon 		mmd->pmd = sc->msk_pmd;
19688e5d93dbSMarius Strobl 		if (sc->msk_pmd == 'L' || sc->msk_pmd == 'S')
1969fcb62a8bSPyun YongHyeon 			mmd->mii_flags |= MIIF_HAVEFIBER;
19708e5d93dbSMarius Strobl 		if (sc->msk_pmd == 'P')
19718e5d93dbSMarius Strobl 			mmd->mii_flags |= MIIF_HAVEFIBER | MIIF_MACPRIV0;
1972fcb62a8bSPyun YongHyeon 		device_set_ivars(sc->msk_devs[MSK_PORT_B], mmd);
19730dbe28b3SPyun YongHyeon 	}
19740dbe28b3SPyun YongHyeon 
1975*18250ec6SJohn Baldwin 	bus_attach_children(dev);
19760dbe28b3SPyun YongHyeon 
197753dcfbd1SPyun YongHyeon 	/* Hook interrupt last to avoid having to lock softc. */
197853dcfbd1SPyun YongHyeon 	error = bus_setup_intr(dev, sc->msk_irq[0], INTR_TYPE_NET |
1979c876b43fSPyun YongHyeon 	    INTR_MPSAFE, NULL, msk_intr, sc, &sc->msk_intrhand);
19800dbe28b3SPyun YongHyeon 	if (error != 0) {
19810dbe28b3SPyun YongHyeon 		device_printf(dev, "couldn't set up interrupt handler\n");
19820dbe28b3SPyun YongHyeon 		goto fail;
19830dbe28b3SPyun YongHyeon 	}
19840dbe28b3SPyun YongHyeon fail:
19850dbe28b3SPyun YongHyeon 	if (error != 0)
19860dbe28b3SPyun YongHyeon 		mskc_detach(dev);
19870dbe28b3SPyun YongHyeon 
19880dbe28b3SPyun YongHyeon 	return (error);
19890dbe28b3SPyun YongHyeon }
19900dbe28b3SPyun YongHyeon 
19910dbe28b3SPyun YongHyeon /*
19920dbe28b3SPyun YongHyeon  * Shutdown hardware and free up resources. This can be called any
19930dbe28b3SPyun YongHyeon  * time after the mutex has been initialized. It is called in both
19940dbe28b3SPyun YongHyeon  * the error case in attach and the normal detach case so it needs
19950dbe28b3SPyun YongHyeon  * to be careful about only freeing resources that have actually been
19960dbe28b3SPyun YongHyeon  * allocated.
19970dbe28b3SPyun YongHyeon  */
19980dbe28b3SPyun YongHyeon static int
msk_detach(device_t dev)19990dbe28b3SPyun YongHyeon msk_detach(device_t dev)
20000dbe28b3SPyun YongHyeon {
20010dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
20020dbe28b3SPyun YongHyeon 	struct msk_if_softc *sc_if;
20035ab8c4b8SJustin Hibbits 	if_t ifp;
20040dbe28b3SPyun YongHyeon 
20050dbe28b3SPyun YongHyeon 	sc_if = device_get_softc(dev);
20060dbe28b3SPyun YongHyeon 	KASSERT(mtx_initialized(&sc_if->msk_softc->msk_mtx),
20070dbe28b3SPyun YongHyeon 	    ("msk mutex not initialized in msk_detach"));
20080dbe28b3SPyun YongHyeon 	MSK_IF_LOCK(sc_if);
20090dbe28b3SPyun YongHyeon 
20100dbe28b3SPyun YongHyeon 	ifp = sc_if->msk_ifp;
20110dbe28b3SPyun YongHyeon 	if (device_is_attached(dev)) {
20120dbe28b3SPyun YongHyeon 		/* XXX */
20137a76e8a4SPyun YongHyeon 		sc_if->msk_flags |= MSK_FLAG_DETACH;
20140dbe28b3SPyun YongHyeon 		msk_stop(sc_if);
20150dbe28b3SPyun YongHyeon 		/* Can't hold locks while calling detach. */
20160dbe28b3SPyun YongHyeon 		MSK_IF_UNLOCK(sc_if);
20170dbe28b3SPyun YongHyeon 		callout_drain(&sc_if->msk_tick_ch);
20184c5a247bSGleb Smirnoff 		if (ifp)
20190dbe28b3SPyun YongHyeon 			ether_ifdetach(ifp);
20200dbe28b3SPyun YongHyeon 		MSK_IF_LOCK(sc_if);
20210dbe28b3SPyun YongHyeon 	}
20220dbe28b3SPyun YongHyeon 
202385b340cbSPyun YongHyeon 	msk_rx_dma_jfree(sc_if);
20240dbe28b3SPyun YongHyeon 	msk_txrx_dma_free(sc_if);
20250dbe28b3SPyun YongHyeon 	bus_generic_detach(dev);
20260dbe28b3SPyun YongHyeon 
20270dbe28b3SPyun YongHyeon 	sc = sc_if->msk_softc;
20280dbe28b3SPyun YongHyeon 	sc->msk_if[sc_if->msk_port] = NULL;
20290dbe28b3SPyun YongHyeon 	MSK_IF_UNLOCK(sc_if);
203015b18534SPyun YongHyeon 	if (ifp)
203115b18534SPyun YongHyeon 		if_free(ifp);
20320dbe28b3SPyun YongHyeon 
20330dbe28b3SPyun YongHyeon 	return (0);
20340dbe28b3SPyun YongHyeon }
20350dbe28b3SPyun YongHyeon 
20362ff5c850SJohn Baldwin static void
mskc_child_deleted(device_t dev,device_t child)20372ff5c850SJohn Baldwin mskc_child_deleted(device_t dev, device_t child)
20382ff5c850SJohn Baldwin {
20392ff5c850SJohn Baldwin 	free(device_get_ivars(child), M_DEVBUF);
20402ff5c850SJohn Baldwin }
20412ff5c850SJohn Baldwin 
20420dbe28b3SPyun YongHyeon static int
mskc_detach(device_t dev)20430dbe28b3SPyun YongHyeon mskc_detach(device_t dev)
20440dbe28b3SPyun YongHyeon {
20450dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
20460dbe28b3SPyun YongHyeon 
20470dbe28b3SPyun YongHyeon 	sc = device_get_softc(dev);
20480dbe28b3SPyun YongHyeon 	KASSERT(mtx_initialized(&sc->msk_mtx), ("msk mutex not initialized"));
20490dbe28b3SPyun YongHyeon 
20500dbe28b3SPyun YongHyeon 	bus_generic_detach(dev);
20510dbe28b3SPyun YongHyeon 
20520dbe28b3SPyun YongHyeon 	/* Disable all interrupts. */
20530dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, B0_IMSK, 0);
20540dbe28b3SPyun YongHyeon 	CSR_READ_4(sc, B0_IMSK);
20550dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, B0_HWE_IMSK, 0);
20560dbe28b3SPyun YongHyeon 	CSR_READ_4(sc, B0_HWE_IMSK);
20570dbe28b3SPyun YongHyeon 
20580dbe28b3SPyun YongHyeon 	/* LED Off. */
20590dbe28b3SPyun YongHyeon 	CSR_WRITE_2(sc, B0_CTST, Y2_LED_STAT_OFF);
20600dbe28b3SPyun YongHyeon 
20610dbe28b3SPyun YongHyeon 	/* Put hardware reset. */
20620dbe28b3SPyun YongHyeon 	CSR_WRITE_2(sc, B0_CTST, CS_RST_SET);
20630dbe28b3SPyun YongHyeon 
20640dbe28b3SPyun YongHyeon 	msk_status_dma_free(sc);
20650dbe28b3SPyun YongHyeon 
2066c72f075aSPyun YongHyeon 	if (sc->msk_intrhand) {
2067c72f075aSPyun YongHyeon 		bus_teardown_intr(dev, sc->msk_irq[0], sc->msk_intrhand);
2068c72f075aSPyun YongHyeon 		sc->msk_intrhand = NULL;
2069298946a9SPyun YongHyeon 	}
2070298946a9SPyun YongHyeon 	bus_release_resources(dev, sc->msk_irq_spec, sc->msk_irq);
20717a76e8a4SPyun YongHyeon 	if ((sc->msk_pflags & MSK_FLAG_MSI) != 0)
20720dbe28b3SPyun YongHyeon 		pci_release_msi(dev);
20730dbe28b3SPyun YongHyeon 	bus_release_resources(dev, sc->msk_res_spec, sc->msk_res);
20740dbe28b3SPyun YongHyeon 	mtx_destroy(&sc->msk_mtx);
20750dbe28b3SPyun YongHyeon 
20760dbe28b3SPyun YongHyeon 	return (0);
20770dbe28b3SPyun YongHyeon }
20780dbe28b3SPyun YongHyeon 
20792dc26832SMarius Strobl static bus_dma_tag_t
mskc_get_dma_tag(device_t bus,device_t child __unused)20802dc26832SMarius Strobl mskc_get_dma_tag(device_t bus, device_t child __unused)
20812dc26832SMarius Strobl {
20822dc26832SMarius Strobl 
20832dc26832SMarius Strobl 	return (bus_get_dma_tag(bus));
20842dc26832SMarius Strobl }
20852dc26832SMarius Strobl 
20860dbe28b3SPyun YongHyeon struct msk_dmamap_arg {
20870dbe28b3SPyun YongHyeon 	bus_addr_t	msk_busaddr;
20880dbe28b3SPyun YongHyeon };
20890dbe28b3SPyun YongHyeon 
20900dbe28b3SPyun YongHyeon static void
msk_dmamap_cb(void * arg,bus_dma_segment_t * segs,int nseg,int error)20910dbe28b3SPyun YongHyeon msk_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
20920dbe28b3SPyun YongHyeon {
20930dbe28b3SPyun YongHyeon 	struct msk_dmamap_arg *ctx;
20940dbe28b3SPyun YongHyeon 
20950dbe28b3SPyun YongHyeon 	if (error != 0)
20960dbe28b3SPyun YongHyeon 		return;
20970dbe28b3SPyun YongHyeon 	ctx = arg;
20980dbe28b3SPyun YongHyeon 	ctx->msk_busaddr = segs[0].ds_addr;
20990dbe28b3SPyun YongHyeon }
21000dbe28b3SPyun YongHyeon 
21010dbe28b3SPyun YongHyeon /* Create status DMA region. */
21020dbe28b3SPyun YongHyeon static int
msk_status_dma_alloc(struct msk_softc * sc)21030dbe28b3SPyun YongHyeon msk_status_dma_alloc(struct msk_softc *sc)
21040dbe28b3SPyun YongHyeon {
21050dbe28b3SPyun YongHyeon 	struct msk_dmamap_arg ctx;
2106355a415eSPyun YongHyeon 	bus_size_t stat_sz;
2107355a415eSPyun YongHyeon 	int count, error;
21080dbe28b3SPyun YongHyeon 
2109355a415eSPyun YongHyeon 	/*
2110355a415eSPyun YongHyeon 	 * It seems controller requires number of status LE entries
2111355a415eSPyun YongHyeon 	 * is power of 2 and the maximum number of status LE entries
2112355a415eSPyun YongHyeon 	 * is 4096.  For dual-port controllers, the number of status
2113355a415eSPyun YongHyeon 	 * LE entries should be large enough to hold both port's
2114355a415eSPyun YongHyeon 	 * status updates.
2115355a415eSPyun YongHyeon 	 */
2116355a415eSPyun YongHyeon 	count = 3 * MSK_RX_RING_CNT + MSK_TX_RING_CNT;
2117355a415eSPyun YongHyeon 	count = imin(4096, roundup2(count, 1024));
2118355a415eSPyun YongHyeon 	sc->msk_stat_count = count;
2119355a415eSPyun YongHyeon 	stat_sz = count * sizeof(struct msk_stat_desc);
21200dbe28b3SPyun YongHyeon 	error = bus_dma_tag_create(
21210dbe28b3SPyun YongHyeon 		    bus_get_dma_tag(sc->msk_dev),	/* parent */
21220dbe28b3SPyun YongHyeon 		    MSK_STAT_ALIGN, 0,		/* alignment, boundary */
21230dbe28b3SPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* lowaddr */
21240dbe28b3SPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* highaddr */
21250dbe28b3SPyun YongHyeon 		    NULL, NULL,			/* filter, filterarg */
2126355a415eSPyun YongHyeon 		    stat_sz,			/* maxsize */
21270dbe28b3SPyun YongHyeon 		    1,				/* nsegments */
2128355a415eSPyun YongHyeon 		    stat_sz,			/* maxsegsize */
21290dbe28b3SPyun YongHyeon 		    0,				/* flags */
21300dbe28b3SPyun YongHyeon 		    NULL, NULL,			/* lockfunc, lockarg */
21310dbe28b3SPyun YongHyeon 		    &sc->msk_stat_tag);
21320dbe28b3SPyun YongHyeon 	if (error != 0) {
21330dbe28b3SPyun YongHyeon 		device_printf(sc->msk_dev,
21340dbe28b3SPyun YongHyeon 		    "failed to create status DMA tag\n");
21350dbe28b3SPyun YongHyeon 		return (error);
21360dbe28b3SPyun YongHyeon 	}
21370dbe28b3SPyun YongHyeon 
21380dbe28b3SPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for status ring. */
21390dbe28b3SPyun YongHyeon 	error = bus_dmamem_alloc(sc->msk_stat_tag,
21400dbe28b3SPyun YongHyeon 	    (void **)&sc->msk_stat_ring, BUS_DMA_WAITOK | BUS_DMA_COHERENT |
21410dbe28b3SPyun YongHyeon 	    BUS_DMA_ZERO, &sc->msk_stat_map);
21420dbe28b3SPyun YongHyeon 	if (error != 0) {
21430dbe28b3SPyun YongHyeon 		device_printf(sc->msk_dev,
21440dbe28b3SPyun YongHyeon 		    "failed to allocate DMA'able memory for status ring\n");
21450dbe28b3SPyun YongHyeon 		return (error);
21460dbe28b3SPyun YongHyeon 	}
21470dbe28b3SPyun YongHyeon 
21480dbe28b3SPyun YongHyeon 	ctx.msk_busaddr = 0;
2149355a415eSPyun YongHyeon 	error = bus_dmamap_load(sc->msk_stat_tag, sc->msk_stat_map,
2150355a415eSPyun YongHyeon 	    sc->msk_stat_ring, stat_sz, msk_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
21510dbe28b3SPyun YongHyeon 	if (error != 0) {
21520dbe28b3SPyun YongHyeon 		device_printf(sc->msk_dev,
21530dbe28b3SPyun YongHyeon 		    "failed to load DMA'able memory for status ring\n");
21540dbe28b3SPyun YongHyeon 		return (error);
21550dbe28b3SPyun YongHyeon 	}
21560dbe28b3SPyun YongHyeon 	sc->msk_stat_ring_paddr = ctx.msk_busaddr;
21570dbe28b3SPyun YongHyeon 
21580dbe28b3SPyun YongHyeon 	return (0);
21590dbe28b3SPyun YongHyeon }
21600dbe28b3SPyun YongHyeon 
21610dbe28b3SPyun YongHyeon static void
msk_status_dma_free(struct msk_softc * sc)21620dbe28b3SPyun YongHyeon msk_status_dma_free(struct msk_softc *sc)
21630dbe28b3SPyun YongHyeon {
21640dbe28b3SPyun YongHyeon 
21650dbe28b3SPyun YongHyeon 	/* Destroy status block. */
21660dbe28b3SPyun YongHyeon 	if (sc->msk_stat_tag) {
2167068d8643SJohn Baldwin 		if (sc->msk_stat_ring_paddr) {
21680dbe28b3SPyun YongHyeon 			bus_dmamap_unload(sc->msk_stat_tag, sc->msk_stat_map);
2169068d8643SJohn Baldwin 			sc->msk_stat_ring_paddr = 0;
2170068d8643SJohn Baldwin 		}
21710dbe28b3SPyun YongHyeon 		if (sc->msk_stat_ring) {
21720dbe28b3SPyun YongHyeon 			bus_dmamem_free(sc->msk_stat_tag,
21730dbe28b3SPyun YongHyeon 			    sc->msk_stat_ring, sc->msk_stat_map);
21740dbe28b3SPyun YongHyeon 			sc->msk_stat_ring = NULL;
21750dbe28b3SPyun YongHyeon 		}
21760dbe28b3SPyun YongHyeon 		bus_dma_tag_destroy(sc->msk_stat_tag);
21770dbe28b3SPyun YongHyeon 		sc->msk_stat_tag = NULL;
21780dbe28b3SPyun YongHyeon 	}
21790dbe28b3SPyun YongHyeon }
21800dbe28b3SPyun YongHyeon 
21810dbe28b3SPyun YongHyeon static int
msk_txrx_dma_alloc(struct msk_if_softc * sc_if)21820dbe28b3SPyun YongHyeon msk_txrx_dma_alloc(struct msk_if_softc *sc_if)
21830dbe28b3SPyun YongHyeon {
21840dbe28b3SPyun YongHyeon 	struct msk_dmamap_arg ctx;
21850dbe28b3SPyun YongHyeon 	struct msk_txdesc *txd;
21860dbe28b3SPyun YongHyeon 	struct msk_rxdesc *rxd;
218783c04c93SPyun YongHyeon 	bus_size_t rxalign;
21880dbe28b3SPyun YongHyeon 	int error, i;
21890dbe28b3SPyun YongHyeon 
21900dbe28b3SPyun YongHyeon 	/* Create parent DMA tag. */
21910dbe28b3SPyun YongHyeon 	error = bus_dma_tag_create(
21920dbe28b3SPyun YongHyeon 		    bus_get_dma_tag(sc_if->msk_if_dev),	/* parent */
21930dbe28b3SPyun YongHyeon 		    1, 0,			/* alignment, boundary */
2194355a415eSPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* lowaddr */
21950dbe28b3SPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* highaddr */
21960dbe28b3SPyun YongHyeon 		    NULL, NULL,			/* filter, filterarg */
21970dbe28b3SPyun YongHyeon 		    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
21980dbe28b3SPyun YongHyeon 		    0,				/* nsegments */
21990dbe28b3SPyun YongHyeon 		    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
22000dbe28b3SPyun YongHyeon 		    0,				/* flags */
22010dbe28b3SPyun YongHyeon 		    NULL, NULL,			/* lockfunc, lockarg */
22020dbe28b3SPyun YongHyeon 		    &sc_if->msk_cdata.msk_parent_tag);
22030dbe28b3SPyun YongHyeon 	if (error != 0) {
22040dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
22050dbe28b3SPyun YongHyeon 		    "failed to create parent DMA tag\n");
22060dbe28b3SPyun YongHyeon 		goto fail;
22070dbe28b3SPyun YongHyeon 	}
22080dbe28b3SPyun YongHyeon 	/* Create tag for Tx ring. */
22090dbe28b3SPyun YongHyeon 	error = bus_dma_tag_create(sc_if->msk_cdata.msk_parent_tag,/* parent */
22100dbe28b3SPyun YongHyeon 		    MSK_RING_ALIGN, 0,		/* alignment, boundary */
22110dbe28b3SPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* lowaddr */
22120dbe28b3SPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* highaddr */
22130dbe28b3SPyun YongHyeon 		    NULL, NULL,			/* filter, filterarg */
22140dbe28b3SPyun YongHyeon 		    MSK_TX_RING_SZ,		/* maxsize */
22150dbe28b3SPyun YongHyeon 		    1,				/* nsegments */
22160dbe28b3SPyun YongHyeon 		    MSK_TX_RING_SZ,		/* maxsegsize */
22170dbe28b3SPyun YongHyeon 		    0,				/* flags */
22180dbe28b3SPyun YongHyeon 		    NULL, NULL,			/* lockfunc, lockarg */
22190dbe28b3SPyun YongHyeon 		    &sc_if->msk_cdata.msk_tx_ring_tag);
22200dbe28b3SPyun YongHyeon 	if (error != 0) {
22210dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
22220dbe28b3SPyun YongHyeon 		    "failed to create Tx ring DMA tag\n");
22230dbe28b3SPyun YongHyeon 		goto fail;
22240dbe28b3SPyun YongHyeon 	}
22250dbe28b3SPyun YongHyeon 
22260dbe28b3SPyun YongHyeon 	/* Create tag for Rx ring. */
22270dbe28b3SPyun YongHyeon 	error = bus_dma_tag_create(sc_if->msk_cdata.msk_parent_tag,/* parent */
22280dbe28b3SPyun YongHyeon 		    MSK_RING_ALIGN, 0,		/* alignment, boundary */
22290dbe28b3SPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* lowaddr */
22300dbe28b3SPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* highaddr */
22310dbe28b3SPyun YongHyeon 		    NULL, NULL,			/* filter, filterarg */
22320dbe28b3SPyun YongHyeon 		    MSK_RX_RING_SZ,		/* maxsize */
22330dbe28b3SPyun YongHyeon 		    1,				/* nsegments */
22340dbe28b3SPyun YongHyeon 		    MSK_RX_RING_SZ,		/* maxsegsize */
22350dbe28b3SPyun YongHyeon 		    0,				/* flags */
22360dbe28b3SPyun YongHyeon 		    NULL, NULL,			/* lockfunc, lockarg */
22370dbe28b3SPyun YongHyeon 		    &sc_if->msk_cdata.msk_rx_ring_tag);
22380dbe28b3SPyun YongHyeon 	if (error != 0) {
22390dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
22400dbe28b3SPyun YongHyeon 		    "failed to create Rx ring DMA tag\n");
22410dbe28b3SPyun YongHyeon 		goto fail;
22420dbe28b3SPyun YongHyeon 	}
22430dbe28b3SPyun YongHyeon 
22440dbe28b3SPyun YongHyeon 	/* Create tag for Tx buffers. */
22450dbe28b3SPyun YongHyeon 	error = bus_dma_tag_create(sc_if->msk_cdata.msk_parent_tag,/* parent */
22460dbe28b3SPyun YongHyeon 		    1, 0,			/* alignment, boundary */
22470dbe28b3SPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* lowaddr */
22480dbe28b3SPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* highaddr */
22490dbe28b3SPyun YongHyeon 		    NULL, NULL,			/* filter, filterarg */
22508b51df84SPyun YongHyeon 		    MSK_TSO_MAXSIZE,		/* maxsize */
22510dbe28b3SPyun YongHyeon 		    MSK_MAXTXSEGS,		/* nsegments */
22528b51df84SPyun YongHyeon 		    MSK_TSO_MAXSGSIZE,		/* maxsegsize */
22530dbe28b3SPyun YongHyeon 		    0,				/* flags */
22540dbe28b3SPyun YongHyeon 		    NULL, NULL,			/* lockfunc, lockarg */
22550dbe28b3SPyun YongHyeon 		    &sc_if->msk_cdata.msk_tx_tag);
22560dbe28b3SPyun YongHyeon 	if (error != 0) {
22570dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
22580dbe28b3SPyun YongHyeon 		    "failed to create Tx DMA tag\n");
22590dbe28b3SPyun YongHyeon 		goto fail;
22600dbe28b3SPyun YongHyeon 	}
22610dbe28b3SPyun YongHyeon 
226283c04c93SPyun YongHyeon 	rxalign = 1;
226383c04c93SPyun YongHyeon 	/*
226483c04c93SPyun YongHyeon 	 * Workaround hardware hang which seems to happen when Rx buffer
226583c04c93SPyun YongHyeon 	 * is not aligned on multiple of FIFO word(8 bytes).
226683c04c93SPyun YongHyeon 	 */
226783c04c93SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) != 0)
226883c04c93SPyun YongHyeon 		rxalign = MSK_RX_BUF_ALIGN;
22690dbe28b3SPyun YongHyeon 	/* Create tag for Rx buffers. */
22700dbe28b3SPyun YongHyeon 	error = bus_dma_tag_create(sc_if->msk_cdata.msk_parent_tag,/* parent */
227183c04c93SPyun YongHyeon 		    rxalign, 0,			/* alignment, boundary */
22720dbe28b3SPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* lowaddr */
22730dbe28b3SPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* highaddr */
22740dbe28b3SPyun YongHyeon 		    NULL, NULL,			/* filter, filterarg */
22750dbe28b3SPyun YongHyeon 		    MCLBYTES,			/* maxsize */
22760dbe28b3SPyun YongHyeon 		    1,				/* nsegments */
22770dbe28b3SPyun YongHyeon 		    MCLBYTES,			/* maxsegsize */
22780dbe28b3SPyun YongHyeon 		    0,				/* flags */
22790dbe28b3SPyun YongHyeon 		    NULL, NULL,			/* lockfunc, lockarg */
22800dbe28b3SPyun YongHyeon 		    &sc_if->msk_cdata.msk_rx_tag);
22810dbe28b3SPyun YongHyeon 	if (error != 0) {
22820dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
22830dbe28b3SPyun YongHyeon 		    "failed to create Rx DMA tag\n");
22840dbe28b3SPyun YongHyeon 		goto fail;
22850dbe28b3SPyun YongHyeon 	}
22860dbe28b3SPyun YongHyeon 
22870dbe28b3SPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Tx ring. */
22880dbe28b3SPyun YongHyeon 	error = bus_dmamem_alloc(sc_if->msk_cdata.msk_tx_ring_tag,
22890dbe28b3SPyun YongHyeon 	    (void **)&sc_if->msk_rdata.msk_tx_ring, BUS_DMA_WAITOK |
22900dbe28b3SPyun YongHyeon 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc_if->msk_cdata.msk_tx_ring_map);
22910dbe28b3SPyun YongHyeon 	if (error != 0) {
22920dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
22930dbe28b3SPyun YongHyeon 		    "failed to allocate DMA'able memory for Tx ring\n");
22940dbe28b3SPyun YongHyeon 		goto fail;
22950dbe28b3SPyun YongHyeon 	}
22960dbe28b3SPyun YongHyeon 
22970dbe28b3SPyun YongHyeon 	ctx.msk_busaddr = 0;
22980dbe28b3SPyun YongHyeon 	error = bus_dmamap_load(sc_if->msk_cdata.msk_tx_ring_tag,
22990dbe28b3SPyun YongHyeon 	    sc_if->msk_cdata.msk_tx_ring_map, sc_if->msk_rdata.msk_tx_ring,
2300355a415eSPyun YongHyeon 	    MSK_TX_RING_SZ, msk_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
23010dbe28b3SPyun YongHyeon 	if (error != 0) {
23020dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
23030dbe28b3SPyun YongHyeon 		    "failed to load DMA'able memory for Tx ring\n");
23040dbe28b3SPyun YongHyeon 		goto fail;
23050dbe28b3SPyun YongHyeon 	}
23060dbe28b3SPyun YongHyeon 	sc_if->msk_rdata.msk_tx_ring_paddr = ctx.msk_busaddr;
23070dbe28b3SPyun YongHyeon 
23080dbe28b3SPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Rx ring. */
23090dbe28b3SPyun YongHyeon 	error = bus_dmamem_alloc(sc_if->msk_cdata.msk_rx_ring_tag,
23100dbe28b3SPyun YongHyeon 	    (void **)&sc_if->msk_rdata.msk_rx_ring, BUS_DMA_WAITOK |
23110dbe28b3SPyun YongHyeon 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc_if->msk_cdata.msk_rx_ring_map);
23120dbe28b3SPyun YongHyeon 	if (error != 0) {
23130dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
23140dbe28b3SPyun YongHyeon 		    "failed to allocate DMA'able memory for Rx ring\n");
23150dbe28b3SPyun YongHyeon 		goto fail;
23160dbe28b3SPyun YongHyeon 	}
23170dbe28b3SPyun YongHyeon 
23180dbe28b3SPyun YongHyeon 	ctx.msk_busaddr = 0;
23190dbe28b3SPyun YongHyeon 	error = bus_dmamap_load(sc_if->msk_cdata.msk_rx_ring_tag,
23200dbe28b3SPyun YongHyeon 	    sc_if->msk_cdata.msk_rx_ring_map, sc_if->msk_rdata.msk_rx_ring,
2321355a415eSPyun YongHyeon 	    MSK_RX_RING_SZ, msk_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
23220dbe28b3SPyun YongHyeon 	if (error != 0) {
23230dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
23240dbe28b3SPyun YongHyeon 		    "failed to load DMA'able memory for Rx ring\n");
23250dbe28b3SPyun YongHyeon 		goto fail;
23260dbe28b3SPyun YongHyeon 	}
23270dbe28b3SPyun YongHyeon 	sc_if->msk_rdata.msk_rx_ring_paddr = ctx.msk_busaddr;
23280dbe28b3SPyun YongHyeon 
23290dbe28b3SPyun YongHyeon 	/* Create DMA maps for Tx buffers. */
23300dbe28b3SPyun YongHyeon 	for (i = 0; i < MSK_TX_RING_CNT; i++) {
23310dbe28b3SPyun YongHyeon 		txd = &sc_if->msk_cdata.msk_txdesc[i];
23320dbe28b3SPyun YongHyeon 		txd->tx_m = NULL;
23330dbe28b3SPyun YongHyeon 		txd->tx_dmamap = NULL;
23340dbe28b3SPyun YongHyeon 		error = bus_dmamap_create(sc_if->msk_cdata.msk_tx_tag, 0,
23350dbe28b3SPyun YongHyeon 		    &txd->tx_dmamap);
23360dbe28b3SPyun YongHyeon 		if (error != 0) {
23370dbe28b3SPyun YongHyeon 			device_printf(sc_if->msk_if_dev,
23380dbe28b3SPyun YongHyeon 			    "failed to create Tx dmamap\n");
23390dbe28b3SPyun YongHyeon 			goto fail;
23400dbe28b3SPyun YongHyeon 		}
23410dbe28b3SPyun YongHyeon 	}
23420dbe28b3SPyun YongHyeon 	/* Create DMA maps for Rx buffers. */
23430dbe28b3SPyun YongHyeon 	if ((error = bus_dmamap_create(sc_if->msk_cdata.msk_rx_tag, 0,
23440dbe28b3SPyun YongHyeon 	    &sc_if->msk_cdata.msk_rx_sparemap)) != 0) {
23450dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
23460dbe28b3SPyun YongHyeon 		    "failed to create spare Rx dmamap\n");
23470dbe28b3SPyun YongHyeon 		goto fail;
23480dbe28b3SPyun YongHyeon 	}
23490dbe28b3SPyun YongHyeon 	for (i = 0; i < MSK_RX_RING_CNT; i++) {
23500dbe28b3SPyun YongHyeon 		rxd = &sc_if->msk_cdata.msk_rxdesc[i];
23510dbe28b3SPyun YongHyeon 		rxd->rx_m = NULL;
23520dbe28b3SPyun YongHyeon 		rxd->rx_dmamap = NULL;
23530dbe28b3SPyun YongHyeon 		error = bus_dmamap_create(sc_if->msk_cdata.msk_rx_tag, 0,
23540dbe28b3SPyun YongHyeon 		    &rxd->rx_dmamap);
23550dbe28b3SPyun YongHyeon 		if (error != 0) {
23560dbe28b3SPyun YongHyeon 			device_printf(sc_if->msk_if_dev,
23570dbe28b3SPyun YongHyeon 			    "failed to create Rx dmamap\n");
23580dbe28b3SPyun YongHyeon 			goto fail;
23590dbe28b3SPyun YongHyeon 		}
23600dbe28b3SPyun YongHyeon 	}
236185b340cbSPyun YongHyeon 
236285b340cbSPyun YongHyeon fail:
236385b340cbSPyun YongHyeon 	return (error);
236485b340cbSPyun YongHyeon }
236585b340cbSPyun YongHyeon 
236685b340cbSPyun YongHyeon static int
msk_rx_dma_jalloc(struct msk_if_softc * sc_if)236785b340cbSPyun YongHyeon msk_rx_dma_jalloc(struct msk_if_softc *sc_if)
236885b340cbSPyun YongHyeon {
236985b340cbSPyun YongHyeon 	struct msk_dmamap_arg ctx;
237085b340cbSPyun YongHyeon 	struct msk_rxdesc *jrxd;
237185b340cbSPyun YongHyeon 	bus_size_t rxalign;
237285b340cbSPyun YongHyeon 	int error, i;
237385b340cbSPyun YongHyeon 
2374e2b16603SPyun YongHyeon 	if (jumbo_disable != 0 || (sc_if->msk_flags & MSK_FLAG_JUMBO) == 0) {
2375e2b16603SPyun YongHyeon 		sc_if->msk_flags &= ~MSK_FLAG_JUMBO;
237685b340cbSPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
237785b340cbSPyun YongHyeon 		    "disabling jumbo frame support\n");
237885b340cbSPyun YongHyeon 		return (0);
237985b340cbSPyun YongHyeon 	}
238085b340cbSPyun YongHyeon 	/* Create tag for jumbo Rx ring. */
238185b340cbSPyun YongHyeon 	error = bus_dma_tag_create(sc_if->msk_cdata.msk_parent_tag,/* parent */
238285b340cbSPyun YongHyeon 		    MSK_RING_ALIGN, 0,		/* alignment, boundary */
238385b340cbSPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* lowaddr */
238485b340cbSPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* highaddr */
238585b340cbSPyun YongHyeon 		    NULL, NULL,			/* filter, filterarg */
238685b340cbSPyun YongHyeon 		    MSK_JUMBO_RX_RING_SZ,	/* maxsize */
238785b340cbSPyun YongHyeon 		    1,				/* nsegments */
238885b340cbSPyun YongHyeon 		    MSK_JUMBO_RX_RING_SZ,	/* maxsegsize */
238985b340cbSPyun YongHyeon 		    0,				/* flags */
239085b340cbSPyun YongHyeon 		    NULL, NULL,			/* lockfunc, lockarg */
239185b340cbSPyun YongHyeon 		    &sc_if->msk_cdata.msk_jumbo_rx_ring_tag);
239285b340cbSPyun YongHyeon 	if (error != 0) {
239385b340cbSPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
239485b340cbSPyun YongHyeon 		    "failed to create jumbo Rx ring DMA tag\n");
239585b340cbSPyun YongHyeon 		goto jumbo_fail;
239685b340cbSPyun YongHyeon 	}
239785b340cbSPyun YongHyeon 
239885b340cbSPyun YongHyeon 	rxalign = 1;
239985b340cbSPyun YongHyeon 	/*
240085b340cbSPyun YongHyeon 	 * Workaround hardware hang which seems to happen when Rx buffer
240185b340cbSPyun YongHyeon 	 * is not aligned on multiple of FIFO word(8 bytes).
240285b340cbSPyun YongHyeon 	 */
240385b340cbSPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) != 0)
240485b340cbSPyun YongHyeon 		rxalign = MSK_RX_BUF_ALIGN;
240585b340cbSPyun YongHyeon 	/* Create tag for jumbo Rx buffers. */
240685b340cbSPyun YongHyeon 	error = bus_dma_tag_create(sc_if->msk_cdata.msk_parent_tag,/* parent */
240785b340cbSPyun YongHyeon 		    rxalign, 0,			/* alignment, boundary */
240885b340cbSPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* lowaddr */
240985b340cbSPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* highaddr */
241085b340cbSPyun YongHyeon 		    NULL, NULL,			/* filter, filterarg */
241185b340cbSPyun YongHyeon 		    MJUM9BYTES,			/* maxsize */
241285b340cbSPyun YongHyeon 		    1,				/* nsegments */
241385b340cbSPyun YongHyeon 		    MJUM9BYTES,			/* maxsegsize */
241485b340cbSPyun YongHyeon 		    0,				/* flags */
241585b340cbSPyun YongHyeon 		    NULL, NULL,			/* lockfunc, lockarg */
241685b340cbSPyun YongHyeon 		    &sc_if->msk_cdata.msk_jumbo_rx_tag);
241785b340cbSPyun YongHyeon 	if (error != 0) {
241885b340cbSPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
241985b340cbSPyun YongHyeon 		    "failed to create jumbo Rx DMA tag\n");
242085b340cbSPyun YongHyeon 		goto jumbo_fail;
242185b340cbSPyun YongHyeon 	}
242285b340cbSPyun YongHyeon 
242385b340cbSPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for jumbo Rx ring. */
242485b340cbSPyun YongHyeon 	error = bus_dmamem_alloc(sc_if->msk_cdata.msk_jumbo_rx_ring_tag,
242585b340cbSPyun YongHyeon 	    (void **)&sc_if->msk_rdata.msk_jumbo_rx_ring,
242685b340cbSPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
242785b340cbSPyun YongHyeon 	    &sc_if->msk_cdata.msk_jumbo_rx_ring_map);
242885b340cbSPyun YongHyeon 	if (error != 0) {
242985b340cbSPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
243085b340cbSPyun YongHyeon 		    "failed to allocate DMA'able memory for jumbo Rx ring\n");
243185b340cbSPyun YongHyeon 		goto jumbo_fail;
243285b340cbSPyun YongHyeon 	}
243385b340cbSPyun YongHyeon 
243485b340cbSPyun YongHyeon 	ctx.msk_busaddr = 0;
243585b340cbSPyun YongHyeon 	error = bus_dmamap_load(sc_if->msk_cdata.msk_jumbo_rx_ring_tag,
243685b340cbSPyun YongHyeon 	    sc_if->msk_cdata.msk_jumbo_rx_ring_map,
243785b340cbSPyun YongHyeon 	    sc_if->msk_rdata.msk_jumbo_rx_ring, MSK_JUMBO_RX_RING_SZ,
2438355a415eSPyun YongHyeon 	    msk_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
243985b340cbSPyun YongHyeon 	if (error != 0) {
244085b340cbSPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
244185b340cbSPyun YongHyeon 		    "failed to load DMA'able memory for jumbo Rx ring\n");
244285b340cbSPyun YongHyeon 		goto jumbo_fail;
244385b340cbSPyun YongHyeon 	}
244485b340cbSPyun YongHyeon 	sc_if->msk_rdata.msk_jumbo_rx_ring_paddr = ctx.msk_busaddr;
244585b340cbSPyun YongHyeon 
24460dbe28b3SPyun YongHyeon 	/* Create DMA maps for jumbo Rx buffers. */
24470dbe28b3SPyun YongHyeon 	if ((error = bus_dmamap_create(sc_if->msk_cdata.msk_jumbo_rx_tag, 0,
24480dbe28b3SPyun YongHyeon 	    &sc_if->msk_cdata.msk_jumbo_rx_sparemap)) != 0) {
24490dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
24500dbe28b3SPyun YongHyeon 		    "failed to create spare jumbo Rx dmamap\n");
245185b340cbSPyun YongHyeon 		goto jumbo_fail;
24520dbe28b3SPyun YongHyeon 	}
24530dbe28b3SPyun YongHyeon 	for (i = 0; i < MSK_JUMBO_RX_RING_CNT; i++) {
24540dbe28b3SPyun YongHyeon 		jrxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[i];
24550dbe28b3SPyun YongHyeon 		jrxd->rx_m = NULL;
24560dbe28b3SPyun YongHyeon 		jrxd->rx_dmamap = NULL;
24570dbe28b3SPyun YongHyeon 		error = bus_dmamap_create(sc_if->msk_cdata.msk_jumbo_rx_tag, 0,
24580dbe28b3SPyun YongHyeon 		    &jrxd->rx_dmamap);
24590dbe28b3SPyun YongHyeon 		if (error != 0) {
24600dbe28b3SPyun YongHyeon 			device_printf(sc_if->msk_if_dev,
24610dbe28b3SPyun YongHyeon 			    "failed to create jumbo Rx dmamap\n");
246285b340cbSPyun YongHyeon 			goto jumbo_fail;
24630dbe28b3SPyun YongHyeon 		}
24640dbe28b3SPyun YongHyeon 	}
24650dbe28b3SPyun YongHyeon 
246685b340cbSPyun YongHyeon 	return (0);
24670dbe28b3SPyun YongHyeon 
246885b340cbSPyun YongHyeon jumbo_fail:
246985b340cbSPyun YongHyeon 	msk_rx_dma_jfree(sc_if);
247085b340cbSPyun YongHyeon 	device_printf(sc_if->msk_if_dev, "disabling jumbo frame support "
247185b340cbSPyun YongHyeon 	    "due to resource shortage\n");
2472e2b16603SPyun YongHyeon 	sc_if->msk_flags &= ~MSK_FLAG_JUMBO;
24730dbe28b3SPyun YongHyeon 	return (error);
24740dbe28b3SPyun YongHyeon }
24750dbe28b3SPyun YongHyeon 
24760dbe28b3SPyun YongHyeon static void
msk_txrx_dma_free(struct msk_if_softc * sc_if)24770dbe28b3SPyun YongHyeon msk_txrx_dma_free(struct msk_if_softc *sc_if)
24780dbe28b3SPyun YongHyeon {
24790dbe28b3SPyun YongHyeon 	struct msk_txdesc *txd;
24800dbe28b3SPyun YongHyeon 	struct msk_rxdesc *rxd;
24810dbe28b3SPyun YongHyeon 	int i;
24820dbe28b3SPyun YongHyeon 
24830dbe28b3SPyun YongHyeon 	/* Tx ring. */
24840dbe28b3SPyun YongHyeon 	if (sc_if->msk_cdata.msk_tx_ring_tag) {
2485068d8643SJohn Baldwin 		if (sc_if->msk_rdata.msk_tx_ring_paddr)
24860dbe28b3SPyun YongHyeon 			bus_dmamap_unload(sc_if->msk_cdata.msk_tx_ring_tag,
24870dbe28b3SPyun YongHyeon 			    sc_if->msk_cdata.msk_tx_ring_map);
2488068d8643SJohn Baldwin 		if (sc_if->msk_rdata.msk_tx_ring)
24890dbe28b3SPyun YongHyeon 			bus_dmamem_free(sc_if->msk_cdata.msk_tx_ring_tag,
24900dbe28b3SPyun YongHyeon 			    sc_if->msk_rdata.msk_tx_ring,
24910dbe28b3SPyun YongHyeon 			    sc_if->msk_cdata.msk_tx_ring_map);
24920dbe28b3SPyun YongHyeon 		sc_if->msk_rdata.msk_tx_ring = NULL;
2493068d8643SJohn Baldwin 		sc_if->msk_rdata.msk_tx_ring_paddr = 0;
24940dbe28b3SPyun YongHyeon 		bus_dma_tag_destroy(sc_if->msk_cdata.msk_tx_ring_tag);
24950dbe28b3SPyun YongHyeon 		sc_if->msk_cdata.msk_tx_ring_tag = NULL;
24960dbe28b3SPyun YongHyeon 	}
24970dbe28b3SPyun YongHyeon 	/* Rx ring. */
24980dbe28b3SPyun YongHyeon 	if (sc_if->msk_cdata.msk_rx_ring_tag) {
2499068d8643SJohn Baldwin 		if (sc_if->msk_rdata.msk_rx_ring_paddr)
25000dbe28b3SPyun YongHyeon 			bus_dmamap_unload(sc_if->msk_cdata.msk_rx_ring_tag,
25010dbe28b3SPyun YongHyeon 			    sc_if->msk_cdata.msk_rx_ring_map);
2502068d8643SJohn Baldwin 		if (sc_if->msk_rdata.msk_rx_ring)
25030dbe28b3SPyun YongHyeon 			bus_dmamem_free(sc_if->msk_cdata.msk_rx_ring_tag,
25040dbe28b3SPyun YongHyeon 			    sc_if->msk_rdata.msk_rx_ring,
25050dbe28b3SPyun YongHyeon 			    sc_if->msk_cdata.msk_rx_ring_map);
25060dbe28b3SPyun YongHyeon 		sc_if->msk_rdata.msk_rx_ring = NULL;
2507068d8643SJohn Baldwin 		sc_if->msk_rdata.msk_rx_ring_paddr = 0;
25080dbe28b3SPyun YongHyeon 		bus_dma_tag_destroy(sc_if->msk_cdata.msk_rx_ring_tag);
25090dbe28b3SPyun YongHyeon 		sc_if->msk_cdata.msk_rx_ring_tag = NULL;
25100dbe28b3SPyun YongHyeon 	}
25110dbe28b3SPyun YongHyeon 	/* Tx buffers. */
25120dbe28b3SPyun YongHyeon 	if (sc_if->msk_cdata.msk_tx_tag) {
25130dbe28b3SPyun YongHyeon 		for (i = 0; i < MSK_TX_RING_CNT; i++) {
25140dbe28b3SPyun YongHyeon 			txd = &sc_if->msk_cdata.msk_txdesc[i];
25150dbe28b3SPyun YongHyeon 			if (txd->tx_dmamap) {
25160dbe28b3SPyun YongHyeon 				bus_dmamap_destroy(sc_if->msk_cdata.msk_tx_tag,
25170dbe28b3SPyun YongHyeon 				    txd->tx_dmamap);
25180dbe28b3SPyun YongHyeon 				txd->tx_dmamap = NULL;
25190dbe28b3SPyun YongHyeon 			}
25200dbe28b3SPyun YongHyeon 		}
25210dbe28b3SPyun YongHyeon 		bus_dma_tag_destroy(sc_if->msk_cdata.msk_tx_tag);
25220dbe28b3SPyun YongHyeon 		sc_if->msk_cdata.msk_tx_tag = NULL;
25230dbe28b3SPyun YongHyeon 	}
25240dbe28b3SPyun YongHyeon 	/* Rx buffers. */
25250dbe28b3SPyun YongHyeon 	if (sc_if->msk_cdata.msk_rx_tag) {
25260dbe28b3SPyun YongHyeon 		for (i = 0; i < MSK_RX_RING_CNT; i++) {
25270dbe28b3SPyun YongHyeon 			rxd = &sc_if->msk_cdata.msk_rxdesc[i];
25280dbe28b3SPyun YongHyeon 			if (rxd->rx_dmamap) {
25290dbe28b3SPyun YongHyeon 				bus_dmamap_destroy(sc_if->msk_cdata.msk_rx_tag,
25300dbe28b3SPyun YongHyeon 				    rxd->rx_dmamap);
25310dbe28b3SPyun YongHyeon 				rxd->rx_dmamap = NULL;
25320dbe28b3SPyun YongHyeon 			}
25330dbe28b3SPyun YongHyeon 		}
25340dbe28b3SPyun YongHyeon 		if (sc_if->msk_cdata.msk_rx_sparemap) {
25350dbe28b3SPyun YongHyeon 			bus_dmamap_destroy(sc_if->msk_cdata.msk_rx_tag,
25360dbe28b3SPyun YongHyeon 			    sc_if->msk_cdata.msk_rx_sparemap);
25370dbe28b3SPyun YongHyeon 			sc_if->msk_cdata.msk_rx_sparemap = 0;
25380dbe28b3SPyun YongHyeon 		}
25390dbe28b3SPyun YongHyeon 		bus_dma_tag_destroy(sc_if->msk_cdata.msk_rx_tag);
25400dbe28b3SPyun YongHyeon 		sc_if->msk_cdata.msk_rx_tag = NULL;
25410dbe28b3SPyun YongHyeon 	}
254285b340cbSPyun YongHyeon 	if (sc_if->msk_cdata.msk_parent_tag) {
254385b340cbSPyun YongHyeon 		bus_dma_tag_destroy(sc_if->msk_cdata.msk_parent_tag);
254485b340cbSPyun YongHyeon 		sc_if->msk_cdata.msk_parent_tag = NULL;
254585b340cbSPyun YongHyeon 	}
254685b340cbSPyun YongHyeon }
254785b340cbSPyun YongHyeon 
254885b340cbSPyun YongHyeon static void
msk_rx_dma_jfree(struct msk_if_softc * sc_if)254985b340cbSPyun YongHyeon msk_rx_dma_jfree(struct msk_if_softc *sc_if)
255085b340cbSPyun YongHyeon {
255185b340cbSPyun YongHyeon 	struct msk_rxdesc *jrxd;
255285b340cbSPyun YongHyeon 	int i;
255385b340cbSPyun YongHyeon 
255485b340cbSPyun YongHyeon 	/* Jumbo Rx ring. */
255585b340cbSPyun YongHyeon 	if (sc_if->msk_cdata.msk_jumbo_rx_ring_tag) {
2556068d8643SJohn Baldwin 		if (sc_if->msk_rdata.msk_jumbo_rx_ring_paddr)
255785b340cbSPyun YongHyeon 			bus_dmamap_unload(sc_if->msk_cdata.msk_jumbo_rx_ring_tag,
255885b340cbSPyun YongHyeon 			    sc_if->msk_cdata.msk_jumbo_rx_ring_map);
2559068d8643SJohn Baldwin 		if (sc_if->msk_rdata.msk_jumbo_rx_ring)
256085b340cbSPyun YongHyeon 			bus_dmamem_free(sc_if->msk_cdata.msk_jumbo_rx_ring_tag,
256185b340cbSPyun YongHyeon 			    sc_if->msk_rdata.msk_jumbo_rx_ring,
256285b340cbSPyun YongHyeon 			    sc_if->msk_cdata.msk_jumbo_rx_ring_map);
256385b340cbSPyun YongHyeon 		sc_if->msk_rdata.msk_jumbo_rx_ring = NULL;
2564068d8643SJohn Baldwin 		sc_if->msk_rdata.msk_jumbo_rx_ring_paddr = 0;
256585b340cbSPyun YongHyeon 		bus_dma_tag_destroy(sc_if->msk_cdata.msk_jumbo_rx_ring_tag);
256685b340cbSPyun YongHyeon 		sc_if->msk_cdata.msk_jumbo_rx_ring_tag = NULL;
256785b340cbSPyun YongHyeon 	}
25680dbe28b3SPyun YongHyeon 	/* Jumbo Rx buffers. */
25690dbe28b3SPyun YongHyeon 	if (sc_if->msk_cdata.msk_jumbo_rx_tag) {
25700dbe28b3SPyun YongHyeon 		for (i = 0; i < MSK_JUMBO_RX_RING_CNT; i++) {
25710dbe28b3SPyun YongHyeon 			jrxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[i];
25720dbe28b3SPyun YongHyeon 			if (jrxd->rx_dmamap) {
25730dbe28b3SPyun YongHyeon 				bus_dmamap_destroy(
25740dbe28b3SPyun YongHyeon 				    sc_if->msk_cdata.msk_jumbo_rx_tag,
25750dbe28b3SPyun YongHyeon 				    jrxd->rx_dmamap);
25760dbe28b3SPyun YongHyeon 				jrxd->rx_dmamap = NULL;
25770dbe28b3SPyun YongHyeon 			}
25780dbe28b3SPyun YongHyeon 		}
25790dbe28b3SPyun YongHyeon 		if (sc_if->msk_cdata.msk_jumbo_rx_sparemap) {
25800dbe28b3SPyun YongHyeon 			bus_dmamap_destroy(sc_if->msk_cdata.msk_jumbo_rx_tag,
25810dbe28b3SPyun YongHyeon 			    sc_if->msk_cdata.msk_jumbo_rx_sparemap);
25820dbe28b3SPyun YongHyeon 			sc_if->msk_cdata.msk_jumbo_rx_sparemap = 0;
25830dbe28b3SPyun YongHyeon 		}
25840dbe28b3SPyun YongHyeon 		bus_dma_tag_destroy(sc_if->msk_cdata.msk_jumbo_rx_tag);
25850dbe28b3SPyun YongHyeon 		sc_if->msk_cdata.msk_jumbo_rx_tag = NULL;
25860dbe28b3SPyun YongHyeon 	}
25870dbe28b3SPyun YongHyeon }
25880dbe28b3SPyun YongHyeon 
25890dbe28b3SPyun YongHyeon static int
msk_encap(struct msk_if_softc * sc_if,struct mbuf ** m_head)25900dbe28b3SPyun YongHyeon msk_encap(struct msk_if_softc *sc_if, struct mbuf **m_head)
25910dbe28b3SPyun YongHyeon {
25920dbe28b3SPyun YongHyeon 	struct msk_txdesc *txd, *txd_last;
25930dbe28b3SPyun YongHyeon 	struct msk_tx_desc *tx_le;
25940dbe28b3SPyun YongHyeon 	struct mbuf *m;
25950dbe28b3SPyun YongHyeon 	bus_dmamap_t map;
25960dbe28b3SPyun YongHyeon 	bus_dma_segment_t txsegs[MSK_MAXTXSEGS];
25971b7757c0SPyun YongHyeon 	uint32_t control, csum, prod, si;
25980dbe28b3SPyun YongHyeon 	uint16_t offset, tcp_offset, tso_mtu;
25990dbe28b3SPyun YongHyeon 	int error, i, nseg, tso;
26000dbe28b3SPyun YongHyeon 
26010dbe28b3SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
26020dbe28b3SPyun YongHyeon 
26030dbe28b3SPyun YongHyeon 	tcp_offset = offset = 0;
26040dbe28b3SPyun YongHyeon 	m = *m_head;
2605ebb25bfaSPyun YongHyeon 	if (((sc_if->msk_flags & MSK_FLAG_AUTOTX_CSUM) == 0 &&
2606ebb25bfaSPyun YongHyeon 	    (m->m_pkthdr.csum_flags & MSK_CSUM_FEATURES) != 0) ||
2607ebb25bfaSPyun YongHyeon 	    ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0 &&
2608ebb25bfaSPyun YongHyeon 	    (m->m_pkthdr.csum_flags & CSUM_TSO) != 0)) {
26090dbe28b3SPyun YongHyeon 		/*
26100dbe28b3SPyun YongHyeon 		 * Since mbuf has no protocol specific structure information
26110dbe28b3SPyun YongHyeon 		 * in it we have to inspect protocol information here to
26120dbe28b3SPyun YongHyeon 		 * setup TSO and checksum offload. I don't know why Marvell
26130dbe28b3SPyun YongHyeon 		 * made a such decision in chip design because other GigE
26140dbe28b3SPyun YongHyeon 		 * hardwares normally takes care of all these chores in
26150dbe28b3SPyun YongHyeon 		 * hardware. However, TSO performance of Yukon II is very
26160dbe28b3SPyun YongHyeon 		 * good such that it's worth to implement it.
26170dbe28b3SPyun YongHyeon 		 */
26180dbe28b3SPyun YongHyeon 		struct ether_header *eh;
26190dbe28b3SPyun YongHyeon 		struct ip *ip;
26200dbe28b3SPyun YongHyeon 		struct tcphdr *tcp;
26210dbe28b3SPyun YongHyeon 
2622ad415775SPyun YongHyeon 		if (M_WRITABLE(m) == 0) {
2623ad415775SPyun YongHyeon 			/* Get a writable copy. */
2624c6499eccSGleb Smirnoff 			m = m_dup(*m_head, M_NOWAIT);
2625ad415775SPyun YongHyeon 			m_freem(*m_head);
2626ad415775SPyun YongHyeon 			if (m == NULL) {
2627ad415775SPyun YongHyeon 				*m_head = NULL;
2628ad415775SPyun YongHyeon 				return (ENOBUFS);
2629ad415775SPyun YongHyeon 			}
2630ad415775SPyun YongHyeon 			*m_head = m;
2631ad415775SPyun YongHyeon 		}
26320dbe28b3SPyun YongHyeon 
26330dbe28b3SPyun YongHyeon 		offset = sizeof(struct ether_header);
26340dbe28b3SPyun YongHyeon 		m = m_pullup(m, offset);
26350dbe28b3SPyun YongHyeon 		if (m == NULL) {
26360dbe28b3SPyun YongHyeon 			*m_head = NULL;
26370dbe28b3SPyun YongHyeon 			return (ENOBUFS);
26380dbe28b3SPyun YongHyeon 		}
26390dbe28b3SPyun YongHyeon 		eh = mtod(m, struct ether_header *);
26400dbe28b3SPyun YongHyeon 		/* Check if hardware VLAN insertion is off. */
26410dbe28b3SPyun YongHyeon 		if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
26420dbe28b3SPyun YongHyeon 			offset = sizeof(struct ether_vlan_header);
26430dbe28b3SPyun YongHyeon 			m = m_pullup(m, offset);
26440dbe28b3SPyun YongHyeon 			if (m == NULL) {
26450dbe28b3SPyun YongHyeon 				*m_head = NULL;
26460dbe28b3SPyun YongHyeon 				return (ENOBUFS);
26470dbe28b3SPyun YongHyeon 			}
2648b5898b80SPyun YongHyeon 		}
26490dbe28b3SPyun YongHyeon 		m = m_pullup(m, offset + sizeof(struct ip));
26500dbe28b3SPyun YongHyeon 		if (m == NULL) {
26510dbe28b3SPyun YongHyeon 			*m_head = NULL;
26520dbe28b3SPyun YongHyeon 			return (ENOBUFS);
26530dbe28b3SPyun YongHyeon 		}
2654b5898b80SPyun YongHyeon 		ip = (struct ip *)(mtod(m, char *) + offset);
26550dbe28b3SPyun YongHyeon 		offset += (ip->ip_hl << 2);
26560dbe28b3SPyun YongHyeon 		tcp_offset = offset;
26576da6d0a9SPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
26586da6d0a9SPyun YongHyeon 			m = m_pullup(m, offset + sizeof(struct tcphdr));
26596da6d0a9SPyun YongHyeon 			if (m == NULL) {
26606da6d0a9SPyun YongHyeon 				*m_head = NULL;
26616da6d0a9SPyun YongHyeon 				return (ENOBUFS);
26626da6d0a9SPyun YongHyeon 			}
26636da6d0a9SPyun YongHyeon 			tcp = (struct tcphdr *)(mtod(m, char *) + offset);
26646da6d0a9SPyun YongHyeon 			offset += (tcp->th_off << 2);
26656da6d0a9SPyun YongHyeon 		} else if ((sc_if->msk_flags & MSK_FLAG_AUTOTX_CSUM) == 0 &&
26666da6d0a9SPyun YongHyeon 		    (m->m_pkthdr.len < MSK_MIN_FRAMELEN) &&
26676da6d0a9SPyun YongHyeon 		    (m->m_pkthdr.csum_flags & CSUM_TCP) != 0) {
2668b5898b80SPyun YongHyeon 			/*
26696da6d0a9SPyun YongHyeon 			 * It seems that Yukon II has Tx checksum offload bug
26706da6d0a9SPyun YongHyeon 			 * for small TCP packets that's less than 60 bytes in
26716da6d0a9SPyun YongHyeon 			 * size (e.g. TCP window probe packet, pure ACK packet).
26726da6d0a9SPyun YongHyeon 			 * Common work around like padding with zeros to make
26736da6d0a9SPyun YongHyeon 			 * the frame minimum ethernet frame size didn't work at
26746da6d0a9SPyun YongHyeon 			 * all.
2675b5898b80SPyun YongHyeon 			 * Instead of disabling checksum offload completely we
26766da6d0a9SPyun YongHyeon 			 * resort to S/W checksum routine when we encounter
26776da6d0a9SPyun YongHyeon 			 * short TCP frames.
2678b5898b80SPyun YongHyeon 			 * Short UDP packets appear to be handled correctly by
2679ebb25bfaSPyun YongHyeon 			 * Yukon II. Also I assume this bug does not happen on
2680ebb25bfaSPyun YongHyeon 			 * controllers that use newer descriptor format or
2681b1ce21c6SRebecca Cran 			 * automatic Tx checksum calculation.
2682b5898b80SPyun YongHyeon 			 */
2683925da971SPyun YongHyeon 			m = m_pullup(m, offset + sizeof(struct tcphdr));
2684925da971SPyun YongHyeon 			if (m == NULL) {
2685925da971SPyun YongHyeon 				*m_head = NULL;
2686925da971SPyun YongHyeon 				return (ENOBUFS);
2687925da971SPyun YongHyeon 			}
2688b5898b80SPyun YongHyeon 			*(uint16_t *)(m->m_data + offset +
2689f9ad2b2fSPyun YongHyeon 			    m->m_pkthdr.csum_data) = in_cksum_skip(m,
2690f9ad2b2fSPyun YongHyeon 			    m->m_pkthdr.len, offset);
2691b5898b80SPyun YongHyeon 			m->m_pkthdr.csum_flags &= ~CSUM_TCP;
2692b5898b80SPyun YongHyeon 		}
26930dbe28b3SPyun YongHyeon 		*m_head = m;
26940dbe28b3SPyun YongHyeon 	}
26950dbe28b3SPyun YongHyeon 
26960dbe28b3SPyun YongHyeon 	prod = sc_if->msk_cdata.msk_tx_prod;
26970dbe28b3SPyun YongHyeon 	txd = &sc_if->msk_cdata.msk_txdesc[prod];
26980dbe28b3SPyun YongHyeon 	txd_last = txd;
26990dbe28b3SPyun YongHyeon 	map = txd->tx_dmamap;
27000dbe28b3SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc_if->msk_cdata.msk_tx_tag, map,
27010dbe28b3SPyun YongHyeon 	    *m_head, txsegs, &nseg, BUS_DMA_NOWAIT);
27020dbe28b3SPyun YongHyeon 	if (error == EFBIG) {
2703c6499eccSGleb Smirnoff 		m = m_collapse(*m_head, M_NOWAIT, MSK_MAXTXSEGS);
27040dbe28b3SPyun YongHyeon 		if (m == NULL) {
27050dbe28b3SPyun YongHyeon 			m_freem(*m_head);
27060dbe28b3SPyun YongHyeon 			*m_head = NULL;
27070dbe28b3SPyun YongHyeon 			return (ENOBUFS);
27080dbe28b3SPyun YongHyeon 		}
27090dbe28b3SPyun YongHyeon 		*m_head = m;
27100dbe28b3SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc_if->msk_cdata.msk_tx_tag,
27110dbe28b3SPyun YongHyeon 		    map, *m_head, txsegs, &nseg, BUS_DMA_NOWAIT);
27120dbe28b3SPyun YongHyeon 		if (error != 0) {
27130dbe28b3SPyun YongHyeon 			m_freem(*m_head);
27140dbe28b3SPyun YongHyeon 			*m_head = NULL;
27150dbe28b3SPyun YongHyeon 			return (error);
27160dbe28b3SPyun YongHyeon 		}
27170dbe28b3SPyun YongHyeon 	} else if (error != 0)
27180dbe28b3SPyun YongHyeon 		return (error);
27190dbe28b3SPyun YongHyeon 	if (nseg == 0) {
27200dbe28b3SPyun YongHyeon 		m_freem(*m_head);
27210dbe28b3SPyun YongHyeon 		*m_head = NULL;
27220dbe28b3SPyun YongHyeon 		return (EIO);
27230dbe28b3SPyun YongHyeon 	}
27240dbe28b3SPyun YongHyeon 
27250dbe28b3SPyun YongHyeon 	/* Check number of available descriptors. */
27260dbe28b3SPyun YongHyeon 	if (sc_if->msk_cdata.msk_tx_cnt + nseg >=
27270dbe28b3SPyun YongHyeon 	    (MSK_TX_RING_CNT - MSK_RESERVED_TX_DESC_CNT)) {
27280dbe28b3SPyun YongHyeon 		bus_dmamap_unload(sc_if->msk_cdata.msk_tx_tag, map);
27290dbe28b3SPyun YongHyeon 		return (ENOBUFS);
27300dbe28b3SPyun YongHyeon 	}
27310dbe28b3SPyun YongHyeon 
27320dbe28b3SPyun YongHyeon 	control = 0;
27330dbe28b3SPyun YongHyeon 	tso = 0;
27340dbe28b3SPyun YongHyeon 	tx_le = NULL;
27350dbe28b3SPyun YongHyeon 
27360dbe28b3SPyun YongHyeon 	/* Check TSO support. */
27370dbe28b3SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2738262e9dcfSPyun YongHyeon 		if ((sc_if->msk_flags & MSK_FLAG_DESCV2) != 0)
2739262e9dcfSPyun YongHyeon 			tso_mtu = m->m_pkthdr.tso_segsz;
2740262e9dcfSPyun YongHyeon 		else
27410dbe28b3SPyun YongHyeon 			tso_mtu = offset + m->m_pkthdr.tso_segsz;
27420dbe28b3SPyun YongHyeon 		if (tso_mtu != sc_if->msk_cdata.msk_tso_mtu) {
27430dbe28b3SPyun YongHyeon 			tx_le = &sc_if->msk_rdata.msk_tx_ring[prod];
27440dbe28b3SPyun YongHyeon 			tx_le->msk_addr = htole32(tso_mtu);
2745262e9dcfSPyun YongHyeon 			if ((sc_if->msk_flags & MSK_FLAG_DESCV2) != 0)
2746262e9dcfSPyun YongHyeon 				tx_le->msk_control = htole32(OP_MSS | HW_OWNER);
2747262e9dcfSPyun YongHyeon 			else
2748262e9dcfSPyun YongHyeon 				tx_le->msk_control =
2749262e9dcfSPyun YongHyeon 				    htole32(OP_LRGLEN | HW_OWNER);
27500dbe28b3SPyun YongHyeon 			sc_if->msk_cdata.msk_tx_cnt++;
27510dbe28b3SPyun YongHyeon 			MSK_INC(prod, MSK_TX_RING_CNT);
27520dbe28b3SPyun YongHyeon 			sc_if->msk_cdata.msk_tso_mtu = tso_mtu;
27530dbe28b3SPyun YongHyeon 		}
27540dbe28b3SPyun YongHyeon 		tso++;
27550dbe28b3SPyun YongHyeon 	}
27560dbe28b3SPyun YongHyeon 	/* Check if we have a VLAN tag to insert. */
27570dbe28b3SPyun YongHyeon 	if ((m->m_flags & M_VLANTAG) != 0) {
2758d06930afSPyun YongHyeon 		if (tx_le == NULL) {
27590dbe28b3SPyun YongHyeon 			tx_le = &sc_if->msk_rdata.msk_tx_ring[prod];
27600dbe28b3SPyun YongHyeon 			tx_le->msk_addr = htole32(0);
27610dbe28b3SPyun YongHyeon 			tx_le->msk_control = htole32(OP_VLAN | HW_OWNER |
27620dbe28b3SPyun YongHyeon 			    htons(m->m_pkthdr.ether_vtag));
27630dbe28b3SPyun YongHyeon 			sc_if->msk_cdata.msk_tx_cnt++;
27640dbe28b3SPyun YongHyeon 			MSK_INC(prod, MSK_TX_RING_CNT);
27650dbe28b3SPyun YongHyeon 		} else {
27660dbe28b3SPyun YongHyeon 			tx_le->msk_control |= htole32(OP_VLAN |
27670dbe28b3SPyun YongHyeon 			    htons(m->m_pkthdr.ether_vtag));
27680dbe28b3SPyun YongHyeon 		}
27690dbe28b3SPyun YongHyeon 		control |= INS_VLAN;
27700dbe28b3SPyun YongHyeon 	}
27710dbe28b3SPyun YongHyeon 	/* Check if we have to handle checksum offload. */
27720dbe28b3SPyun YongHyeon 	if (tso == 0 && (m->m_pkthdr.csum_flags & MSK_CSUM_FEATURES) != 0) {
2773ebb25bfaSPyun YongHyeon 		if ((sc_if->msk_flags & MSK_FLAG_AUTOTX_CSUM) != 0)
2774262e9dcfSPyun YongHyeon 			control |= CALSUM;
2775262e9dcfSPyun YongHyeon 		else {
27761b7757c0SPyun YongHyeon 			control |= CALSUM | WR_SUM | INIT_SUM | LOCK_SUM;
27770dbe28b3SPyun YongHyeon 			if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
27780dbe28b3SPyun YongHyeon 				control |= UDPTCP;
27791b7757c0SPyun YongHyeon 			/* Checksum write position. */
27801b7757c0SPyun YongHyeon 			csum = (tcp_offset + m->m_pkthdr.csum_data) & 0xffff;
27811b7757c0SPyun YongHyeon 			/* Checksum start position. */
27821b7757c0SPyun YongHyeon 			csum |= (uint32_t)tcp_offset << 16;
27831b7757c0SPyun YongHyeon 			if (csum != sc_if->msk_cdata.msk_last_csum) {
27841b7757c0SPyun YongHyeon 				tx_le = &sc_if->msk_rdata.msk_tx_ring[prod];
27851b7757c0SPyun YongHyeon 				tx_le->msk_addr = htole32(csum);
27861b7757c0SPyun YongHyeon 				tx_le->msk_control = htole32(1 << 16 |
27871b7757c0SPyun YongHyeon 				    (OP_TCPLISW | HW_OWNER));
27880dbe28b3SPyun YongHyeon 				sc_if->msk_cdata.msk_tx_cnt++;
27890dbe28b3SPyun YongHyeon 				MSK_INC(prod, MSK_TX_RING_CNT);
27901b7757c0SPyun YongHyeon 				sc_if->msk_cdata.msk_last_csum = csum;
27911b7757c0SPyun YongHyeon 			}
27920dbe28b3SPyun YongHyeon 		}
2793262e9dcfSPyun YongHyeon 	}
27940dbe28b3SPyun YongHyeon 
2795355a415eSPyun YongHyeon #ifdef MSK_64BIT_DMA
2796355a415eSPyun YongHyeon 	if (MSK_ADDR_HI(txsegs[0].ds_addr) !=
2797355a415eSPyun YongHyeon 	    sc_if->msk_cdata.msk_tx_high_addr) {
2798355a415eSPyun YongHyeon 		sc_if->msk_cdata.msk_tx_high_addr =
2799355a415eSPyun YongHyeon 		    MSK_ADDR_HI(txsegs[0].ds_addr);
2800355a415eSPyun YongHyeon 		tx_le = &sc_if->msk_rdata.msk_tx_ring[prod];
2801355a415eSPyun YongHyeon 		tx_le->msk_addr = htole32(MSK_ADDR_HI(txsegs[0].ds_addr));
2802355a415eSPyun YongHyeon 		tx_le->msk_control = htole32(OP_ADDR64 | HW_OWNER);
2803355a415eSPyun YongHyeon 		sc_if->msk_cdata.msk_tx_cnt++;
2804355a415eSPyun YongHyeon 		MSK_INC(prod, MSK_TX_RING_CNT);
2805355a415eSPyun YongHyeon 	}
2806355a415eSPyun YongHyeon #endif
28070dbe28b3SPyun YongHyeon 	si = prod;
28080dbe28b3SPyun YongHyeon 	tx_le = &sc_if->msk_rdata.msk_tx_ring[prod];
28090dbe28b3SPyun YongHyeon 	tx_le->msk_addr = htole32(MSK_ADDR_LO(txsegs[0].ds_addr));
28100dbe28b3SPyun YongHyeon 	if (tso == 0)
28110dbe28b3SPyun YongHyeon 		tx_le->msk_control = htole32(txsegs[0].ds_len | control |
28120dbe28b3SPyun YongHyeon 		    OP_PACKET);
28130dbe28b3SPyun YongHyeon 	else
28140dbe28b3SPyun YongHyeon 		tx_le->msk_control = htole32(txsegs[0].ds_len | control |
28150dbe28b3SPyun YongHyeon 		    OP_LARGESEND);
28160dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_tx_cnt++;
28170dbe28b3SPyun YongHyeon 	MSK_INC(prod, MSK_TX_RING_CNT);
28180dbe28b3SPyun YongHyeon 
28190dbe28b3SPyun YongHyeon 	for (i = 1; i < nseg; i++) {
28200dbe28b3SPyun YongHyeon 		tx_le = &sc_if->msk_rdata.msk_tx_ring[prod];
2821355a415eSPyun YongHyeon #ifdef MSK_64BIT_DMA
2822355a415eSPyun YongHyeon 		if (MSK_ADDR_HI(txsegs[i].ds_addr) !=
2823355a415eSPyun YongHyeon 		    sc_if->msk_cdata.msk_tx_high_addr) {
2824355a415eSPyun YongHyeon 			sc_if->msk_cdata.msk_tx_high_addr =
2825355a415eSPyun YongHyeon 			    MSK_ADDR_HI(txsegs[i].ds_addr);
2826355a415eSPyun YongHyeon 			tx_le = &sc_if->msk_rdata.msk_tx_ring[prod];
2827355a415eSPyun YongHyeon 			tx_le->msk_addr =
2828355a415eSPyun YongHyeon 			    htole32(MSK_ADDR_HI(txsegs[i].ds_addr));
2829355a415eSPyun YongHyeon 			tx_le->msk_control = htole32(OP_ADDR64 | HW_OWNER);
2830355a415eSPyun YongHyeon 			sc_if->msk_cdata.msk_tx_cnt++;
2831355a415eSPyun YongHyeon 			MSK_INC(prod, MSK_TX_RING_CNT);
2832355a415eSPyun YongHyeon 			tx_le = &sc_if->msk_rdata.msk_tx_ring[prod];
2833355a415eSPyun YongHyeon 		}
2834355a415eSPyun YongHyeon #endif
28350dbe28b3SPyun YongHyeon 		tx_le->msk_addr = htole32(MSK_ADDR_LO(txsegs[i].ds_addr));
28360dbe28b3SPyun YongHyeon 		tx_le->msk_control = htole32(txsegs[i].ds_len | control |
28370dbe28b3SPyun YongHyeon 		    OP_BUFFER | HW_OWNER);
28380dbe28b3SPyun YongHyeon 		sc_if->msk_cdata.msk_tx_cnt++;
28390dbe28b3SPyun YongHyeon 		MSK_INC(prod, MSK_TX_RING_CNT);
28400dbe28b3SPyun YongHyeon 	}
28410dbe28b3SPyun YongHyeon 	/* Update producer index. */
28420dbe28b3SPyun YongHyeon 	sc_if->msk_cdata.msk_tx_prod = prod;
28430dbe28b3SPyun YongHyeon 
2844b1ce21c6SRebecca Cran 	/* Set EOP on the last descriptor. */
28450dbe28b3SPyun YongHyeon 	prod = (prod + MSK_TX_RING_CNT - 1) % MSK_TX_RING_CNT;
28460dbe28b3SPyun YongHyeon 	tx_le = &sc_if->msk_rdata.msk_tx_ring[prod];
28470dbe28b3SPyun YongHyeon 	tx_le->msk_control |= htole32(EOP);
28480dbe28b3SPyun YongHyeon 
28490dbe28b3SPyun YongHyeon 	/* Turn the first descriptor ownership to hardware. */
28500dbe28b3SPyun YongHyeon 	tx_le = &sc_if->msk_rdata.msk_tx_ring[si];
28510dbe28b3SPyun YongHyeon 	tx_le->msk_control |= htole32(HW_OWNER);
28520dbe28b3SPyun YongHyeon 
28530dbe28b3SPyun YongHyeon 	txd = &sc_if->msk_cdata.msk_txdesc[prod];
28540dbe28b3SPyun YongHyeon 	map = txd_last->tx_dmamap;
28550dbe28b3SPyun YongHyeon 	txd_last->tx_dmamap = txd->tx_dmamap;
28560dbe28b3SPyun YongHyeon 	txd->tx_dmamap = map;
28570dbe28b3SPyun YongHyeon 	txd->tx_m = m;
28580dbe28b3SPyun YongHyeon 
28590dbe28b3SPyun YongHyeon 	/* Sync descriptors. */
28600dbe28b3SPyun YongHyeon 	bus_dmamap_sync(sc_if->msk_cdata.msk_tx_tag, map, BUS_DMASYNC_PREWRITE);
28610dbe28b3SPyun YongHyeon 	bus_dmamap_sync(sc_if->msk_cdata.msk_tx_ring_tag,
28620dbe28b3SPyun YongHyeon 	    sc_if->msk_cdata.msk_tx_ring_map,
28630dbe28b3SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
28640dbe28b3SPyun YongHyeon 
28650dbe28b3SPyun YongHyeon 	return (0);
28660dbe28b3SPyun YongHyeon }
28670dbe28b3SPyun YongHyeon 
28680dbe28b3SPyun YongHyeon static void
msk_start(if_t ifp)28695ab8c4b8SJustin Hibbits msk_start(if_t ifp)
28700dbe28b3SPyun YongHyeon {
2871c876b43fSPyun YongHyeon 	struct msk_if_softc *sc_if;
28720dbe28b3SPyun YongHyeon 
28735ab8c4b8SJustin Hibbits 	sc_if = if_getsoftc(ifp);
2874c876b43fSPyun YongHyeon 	MSK_IF_LOCK(sc_if);
2875c876b43fSPyun YongHyeon 	msk_start_locked(ifp);
2876c876b43fSPyun YongHyeon 	MSK_IF_UNLOCK(sc_if);
28770dbe28b3SPyun YongHyeon }
28780dbe28b3SPyun YongHyeon 
28790dbe28b3SPyun YongHyeon static void
msk_start_locked(if_t ifp)28805ab8c4b8SJustin Hibbits msk_start_locked(if_t ifp)
28810dbe28b3SPyun YongHyeon {
28820dbe28b3SPyun YongHyeon 	struct msk_if_softc *sc_if;
28830dbe28b3SPyun YongHyeon 	struct mbuf *m_head;
28840dbe28b3SPyun YongHyeon 	int enq;
28850dbe28b3SPyun YongHyeon 
28865ab8c4b8SJustin Hibbits 	sc_if = if_getsoftc(ifp);
2887c876b43fSPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
28880dbe28b3SPyun YongHyeon 
28895ab8c4b8SJustin Hibbits 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2890c876b43fSPyun YongHyeon 	    IFF_DRV_RUNNING || (sc_if->msk_flags & MSK_FLAG_LINK) == 0)
28910dbe28b3SPyun YongHyeon 		return;
28920dbe28b3SPyun YongHyeon 
28935ab8c4b8SJustin Hibbits 	for (enq = 0; !if_sendq_empty(ifp) &&
28940dbe28b3SPyun YongHyeon 	    sc_if->msk_cdata.msk_tx_cnt <
28950dbe28b3SPyun YongHyeon 	    (MSK_TX_RING_CNT - MSK_RESERVED_TX_DESC_CNT); ) {
28965ab8c4b8SJustin Hibbits 		m_head = if_dequeue(ifp);
28970dbe28b3SPyun YongHyeon 		if (m_head == NULL)
28980dbe28b3SPyun YongHyeon 			break;
28990dbe28b3SPyun YongHyeon 		/*
29000dbe28b3SPyun YongHyeon 		 * Pack the data into the transmit ring. If we
29010dbe28b3SPyun YongHyeon 		 * don't have room, set the OACTIVE flag and wait
29020dbe28b3SPyun YongHyeon 		 * for the NIC to drain the ring.
29030dbe28b3SPyun YongHyeon 		 */
29040dbe28b3SPyun YongHyeon 		if (msk_encap(sc_if, &m_head) != 0) {
29050dbe28b3SPyun YongHyeon 			if (m_head == NULL)
29060dbe28b3SPyun YongHyeon 				break;
29075ab8c4b8SJustin Hibbits 			if_sendq_prepend(ifp, m_head);
29085ab8c4b8SJustin Hibbits 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
29090dbe28b3SPyun YongHyeon 			break;
29100dbe28b3SPyun YongHyeon 		}
29110dbe28b3SPyun YongHyeon 
29120dbe28b3SPyun YongHyeon 		enq++;
29130dbe28b3SPyun YongHyeon 		/*
29140dbe28b3SPyun YongHyeon 		 * If there's a BPF listener, bounce a copy of this frame
29150dbe28b3SPyun YongHyeon 		 * to him.
29160dbe28b3SPyun YongHyeon 		 */
291759a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
29180dbe28b3SPyun YongHyeon 	}
29190dbe28b3SPyun YongHyeon 
29200dbe28b3SPyun YongHyeon 	if (enq > 0) {
29210dbe28b3SPyun YongHyeon 		/* Transmit */
29220dbe28b3SPyun YongHyeon 		CSR_WRITE_2(sc_if->msk_softc,
29230dbe28b3SPyun YongHyeon 		    Y2_PREF_Q_ADDR(sc_if->msk_txq, PREF_UNIT_PUT_IDX_REG),
29240dbe28b3SPyun YongHyeon 		    sc_if->msk_cdata.msk_tx_prod);
29250dbe28b3SPyun YongHyeon 
29260dbe28b3SPyun YongHyeon 		/* Set a timeout in case the chip goes out to lunch. */
29272271eac7SPyun YongHyeon 		sc_if->msk_watchdog_timer = MSK_TX_TIMEOUT;
29280dbe28b3SPyun YongHyeon 	}
29290dbe28b3SPyun YongHyeon }
29300dbe28b3SPyun YongHyeon 
29310dbe28b3SPyun YongHyeon static void
msk_watchdog(struct msk_if_softc * sc_if)29322271eac7SPyun YongHyeon msk_watchdog(struct msk_if_softc *sc_if)
29330dbe28b3SPyun YongHyeon {
29345ab8c4b8SJustin Hibbits 	if_t ifp;
29350dbe28b3SPyun YongHyeon 
29360dbe28b3SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
29370dbe28b3SPyun YongHyeon 
29382271eac7SPyun YongHyeon 	if (sc_if->msk_watchdog_timer == 0 || --sc_if->msk_watchdog_timer)
29392271eac7SPyun YongHyeon 		return;
29400dbe28b3SPyun YongHyeon 	ifp = sc_if->msk_ifp;
2941ab7df1e4SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_LINK) == 0) {
29420dbe28b3SPyun YongHyeon 		if (bootverbose)
29430dbe28b3SPyun YongHyeon 			if_printf(sc_if->msk_ifp, "watchdog timeout "
29440dbe28b3SPyun YongHyeon 			   "(missed link)\n");
29451162f065SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
29465ab8c4b8SJustin Hibbits 		if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
29470dbe28b3SPyun YongHyeon 		msk_init_locked(sc_if);
29480dbe28b3SPyun YongHyeon 		return;
29490dbe28b3SPyun YongHyeon 	}
29500dbe28b3SPyun YongHyeon 
29510dbe28b3SPyun YongHyeon 	if_printf(ifp, "watchdog timeout\n");
29521162f065SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
29535ab8c4b8SJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
29540dbe28b3SPyun YongHyeon 	msk_init_locked(sc_if);
29555ab8c4b8SJustin Hibbits 	if (!if_sendq_empty(ifp))
2956c876b43fSPyun YongHyeon 		msk_start_locked(ifp);
29570dbe28b3SPyun YongHyeon }
29580dbe28b3SPyun YongHyeon 
29596a087a87SPyun YongHyeon static int
mskc_shutdown(device_t dev)29600dbe28b3SPyun YongHyeon mskc_shutdown(device_t dev)
29610dbe28b3SPyun YongHyeon {
29620dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
29630dbe28b3SPyun YongHyeon 	int i;
29640dbe28b3SPyun YongHyeon 
29650dbe28b3SPyun YongHyeon 	sc = device_get_softc(dev);
29660dbe28b3SPyun YongHyeon 	MSK_LOCK(sc);
29670dbe28b3SPyun YongHyeon 	for (i = 0; i < sc->msk_num_port; i++) {
296831fefd0dSPyun YongHyeon 		if (sc->msk_if[i] != NULL && sc->msk_if[i]->msk_ifp != NULL &&
29695ab8c4b8SJustin Hibbits 		    ((if_getdrvflags(sc->msk_if[i]->msk_ifp) &
297031fefd0dSPyun YongHyeon 		    IFF_DRV_RUNNING) != 0))
29710dbe28b3SPyun YongHyeon 			msk_stop(sc->msk_if[i]);
29720dbe28b3SPyun YongHyeon 	}
297331fefd0dSPyun YongHyeon 	MSK_UNLOCK(sc);
29740dbe28b3SPyun YongHyeon 
29750dbe28b3SPyun YongHyeon 	/* Put hardware reset. */
29760dbe28b3SPyun YongHyeon 	CSR_WRITE_2(sc, B0_CTST, CS_RST_SET);
29776a087a87SPyun YongHyeon 	return (0);
29780dbe28b3SPyun YongHyeon }
29790dbe28b3SPyun YongHyeon 
29800dbe28b3SPyun YongHyeon static int
mskc_suspend(device_t dev)29810dbe28b3SPyun YongHyeon mskc_suspend(device_t dev)
29820dbe28b3SPyun YongHyeon {
29830dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
29840dbe28b3SPyun YongHyeon 	int i;
29850dbe28b3SPyun YongHyeon 
29860dbe28b3SPyun YongHyeon 	sc = device_get_softc(dev);
29870dbe28b3SPyun YongHyeon 
29880dbe28b3SPyun YongHyeon 	MSK_LOCK(sc);
29890dbe28b3SPyun YongHyeon 
29900dbe28b3SPyun YongHyeon 	for (i = 0; i < sc->msk_num_port; i++) {
29910dbe28b3SPyun YongHyeon 		if (sc->msk_if[i] != NULL && sc->msk_if[i]->msk_ifp != NULL &&
29925ab8c4b8SJustin Hibbits 		    ((if_getdrvflags(sc->msk_if[i]->msk_ifp) &
29930dbe28b3SPyun YongHyeon 		    IFF_DRV_RUNNING) != 0))
29940dbe28b3SPyun YongHyeon 			msk_stop(sc->msk_if[i]);
29950dbe28b3SPyun YongHyeon 	}
29960dbe28b3SPyun YongHyeon 
29970dbe28b3SPyun YongHyeon 	/* Disable all interrupts. */
29980dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, B0_IMSK, 0);
29990dbe28b3SPyun YongHyeon 	CSR_READ_4(sc, B0_IMSK);
30000dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, B0_HWE_IMSK, 0);
30010dbe28b3SPyun YongHyeon 	CSR_READ_4(sc, B0_HWE_IMSK);
30020dbe28b3SPyun YongHyeon 
30030dbe28b3SPyun YongHyeon 	msk_phy_power(sc, MSK_PHY_POWERDOWN);
30040dbe28b3SPyun YongHyeon 
30050dbe28b3SPyun YongHyeon 	/* Put hardware reset. */
30060dbe28b3SPyun YongHyeon 	CSR_WRITE_2(sc, B0_CTST, CS_RST_SET);
3007ab7df1e4SPyun YongHyeon 	sc->msk_pflags |= MSK_FLAG_SUSPEND;
30080dbe28b3SPyun YongHyeon 
30090dbe28b3SPyun YongHyeon 	MSK_UNLOCK(sc);
30100dbe28b3SPyun YongHyeon 
30110dbe28b3SPyun YongHyeon 	return (0);
30120dbe28b3SPyun YongHyeon }
30130dbe28b3SPyun YongHyeon 
30140dbe28b3SPyun YongHyeon static int
mskc_resume(device_t dev)30150dbe28b3SPyun YongHyeon mskc_resume(device_t dev)
30160dbe28b3SPyun YongHyeon {
30170dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
30180dbe28b3SPyun YongHyeon 	int i;
30190dbe28b3SPyun YongHyeon 
30200dbe28b3SPyun YongHyeon 	sc = device_get_softc(dev);
30210dbe28b3SPyun YongHyeon 
30220dbe28b3SPyun YongHyeon 	MSK_LOCK(sc);
30230dbe28b3SPyun YongHyeon 
3024c6a34f76SPyun YongHyeon 	CSR_PCI_WRITE_4(sc, PCI_OUR_REG_3, 0);
30250dbe28b3SPyun YongHyeon 	mskc_reset(sc);
30260dbe28b3SPyun YongHyeon 	for (i = 0; i < sc->msk_num_port; i++) {
30270dbe28b3SPyun YongHyeon 		if (sc->msk_if[i] != NULL && sc->msk_if[i]->msk_ifp != NULL &&
30285ab8c4b8SJustin Hibbits 		    ((if_getflags(sc->msk_if[i]->msk_ifp) & IFF_UP) != 0)) {
30295ab8c4b8SJustin Hibbits 			if_setdrvflagbits(sc->msk_if[i]->msk_ifp, 0,
30305ab8c4b8SJustin Hibbits 			    IFF_DRV_RUNNING);
30310dbe28b3SPyun YongHyeon 			msk_init_locked(sc->msk_if[i]);
30320dbe28b3SPyun YongHyeon 		}
303389e22666SPyun YongHyeon 	}
303440d6bed8SPyun YongHyeon 	sc->msk_pflags &= ~MSK_FLAG_SUSPEND;
30350dbe28b3SPyun YongHyeon 
30360dbe28b3SPyun YongHyeon 	MSK_UNLOCK(sc);
30370dbe28b3SPyun YongHyeon 
30380dbe28b3SPyun YongHyeon 	return (0);
30390dbe28b3SPyun YongHyeon }
30400dbe28b3SPyun YongHyeon 
304183c04c93SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
304283c04c93SPyun YongHyeon static __inline void
msk_fixup_rx(struct mbuf * m)304383c04c93SPyun YongHyeon msk_fixup_rx(struct mbuf *m)
304483c04c93SPyun YongHyeon {
304583c04c93SPyun YongHyeon         int i;
304683c04c93SPyun YongHyeon         uint16_t *src, *dst;
304783c04c93SPyun YongHyeon 
304883c04c93SPyun YongHyeon 	src = mtod(m, uint16_t *);
304983c04c93SPyun YongHyeon 	dst = src - 3;
305083c04c93SPyun YongHyeon 
305183c04c93SPyun YongHyeon 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
305283c04c93SPyun YongHyeon 		*dst++ = *src++;
305383c04c93SPyun YongHyeon 
305483c04c93SPyun YongHyeon 	m->m_data -= (MSK_RX_BUF_ALIGN - ETHER_ALIGN);
305583c04c93SPyun YongHyeon }
305683c04c93SPyun YongHyeon #endif
305783c04c93SPyun YongHyeon 
3058388214e4SPyun YongHyeon static __inline void
msk_rxcsum(struct msk_if_softc * sc_if,uint32_t control,struct mbuf * m)3059388214e4SPyun YongHyeon msk_rxcsum(struct msk_if_softc *sc_if, uint32_t control, struct mbuf *m)
3060388214e4SPyun YongHyeon {
3061388214e4SPyun YongHyeon 	struct ether_header *eh;
3062388214e4SPyun YongHyeon 	struct ip *ip;
3063388214e4SPyun YongHyeon 	struct udphdr *uh;
3064388214e4SPyun YongHyeon 	int32_t hlen, len, pktlen, temp32;
3065388214e4SPyun YongHyeon 	uint16_t csum, *opts;
3066388214e4SPyun YongHyeon 
3067388214e4SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_DESCV2) != 0) {
3068388214e4SPyun YongHyeon 		if ((control & (CSS_IPV4 | CSS_IPFRAG)) == CSS_IPV4) {
3069388214e4SPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
3070388214e4SPyun YongHyeon 			if ((control & CSS_IPV4_CSUM_OK) != 0)
3071388214e4SPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
3072388214e4SPyun YongHyeon 			if ((control & (CSS_TCP | CSS_UDP)) != 0 &&
3073388214e4SPyun YongHyeon 			    (control & (CSS_TCPUDP_CSUM_OK)) != 0) {
3074388214e4SPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
3075388214e4SPyun YongHyeon 				    CSUM_PSEUDO_HDR;
3076388214e4SPyun YongHyeon 				m->m_pkthdr.csum_data = 0xffff;
3077388214e4SPyun YongHyeon 			}
3078388214e4SPyun YongHyeon 		}
3079388214e4SPyun YongHyeon 		return;
3080388214e4SPyun YongHyeon 	}
3081388214e4SPyun YongHyeon 	/*
3082388214e4SPyun YongHyeon 	 * Marvell Yukon controllers that support OP_RXCHKS has known
3083388214e4SPyun YongHyeon 	 * to have various Rx checksum offloading bugs. These
3084388214e4SPyun YongHyeon 	 * controllers can be configured to compute simple checksum
3085388214e4SPyun YongHyeon 	 * at two different positions. So we can compute IP and TCP/UDP
3086388214e4SPyun YongHyeon 	 * checksum at the same time. We intentionally have controller
3087388214e4SPyun YongHyeon 	 * compute TCP/UDP checksum twice by specifying the same
3088388214e4SPyun YongHyeon 	 * checksum start position and compare the result. If the value
3089388214e4SPyun YongHyeon 	 * is different it would indicate the hardware logic was wrong.
3090388214e4SPyun YongHyeon 	 */
3091388214e4SPyun YongHyeon 	if ((sc_if->msk_csum & 0xFFFF) != (sc_if->msk_csum >> 16)) {
3092388214e4SPyun YongHyeon 		if (bootverbose)
3093388214e4SPyun YongHyeon 			device_printf(sc_if->msk_if_dev,
3094388214e4SPyun YongHyeon 			    "Rx checksum value mismatch!\n");
3095388214e4SPyun YongHyeon 		return;
3096388214e4SPyun YongHyeon 	}
3097388214e4SPyun YongHyeon 	pktlen = m->m_pkthdr.len;
3098388214e4SPyun YongHyeon 	if (pktlen < sizeof(struct ether_header) + sizeof(struct ip))
3099388214e4SPyun YongHyeon 		return;
3100388214e4SPyun YongHyeon 	eh = mtod(m, struct ether_header *);
3101388214e4SPyun YongHyeon 	if (eh->ether_type != htons(ETHERTYPE_IP))
3102388214e4SPyun YongHyeon 		return;
3103388214e4SPyun YongHyeon 	ip = (struct ip *)(eh + 1);
3104388214e4SPyun YongHyeon 	if (ip->ip_v != IPVERSION)
3105388214e4SPyun YongHyeon 		return;
3106388214e4SPyun YongHyeon 
3107388214e4SPyun YongHyeon 	hlen = ip->ip_hl << 2;
3108388214e4SPyun YongHyeon 	pktlen -= sizeof(struct ether_header);
3109388214e4SPyun YongHyeon 	if (hlen < sizeof(struct ip))
3110388214e4SPyun YongHyeon 		return;
3111388214e4SPyun YongHyeon 	if (ntohs(ip->ip_len) < hlen)
3112388214e4SPyun YongHyeon 		return;
3113388214e4SPyun YongHyeon 	if (ntohs(ip->ip_len) != pktlen)
3114388214e4SPyun YongHyeon 		return;
3115388214e4SPyun YongHyeon 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
3116388214e4SPyun YongHyeon 		return;	/* can't handle fragmented packet. */
3117388214e4SPyun YongHyeon 
3118388214e4SPyun YongHyeon 	switch (ip->ip_p) {
3119388214e4SPyun YongHyeon 	case IPPROTO_TCP:
3120388214e4SPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct tcphdr)))
3121388214e4SPyun YongHyeon 			return;
3122388214e4SPyun YongHyeon 		break;
3123388214e4SPyun YongHyeon 	case IPPROTO_UDP:
3124388214e4SPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct udphdr)))
3125388214e4SPyun YongHyeon 			return;
3126388214e4SPyun YongHyeon 		uh = (struct udphdr *)((caddr_t)ip + hlen);
3127388214e4SPyun YongHyeon 		if (uh->uh_sum == 0)
3128388214e4SPyun YongHyeon 			return; /* no checksum */
3129388214e4SPyun YongHyeon 		break;
3130388214e4SPyun YongHyeon 	default:
3131388214e4SPyun YongHyeon 		return;
3132388214e4SPyun YongHyeon 	}
31333c5571b3SPyun YongHyeon 	csum = bswap16(sc_if->msk_csum & 0xFFFF);
3134388214e4SPyun YongHyeon 	/* Checksum fixup for IP options. */
3135388214e4SPyun YongHyeon 	len = hlen - sizeof(struct ip);
3136388214e4SPyun YongHyeon 	if (len > 0) {
3137388214e4SPyun YongHyeon 		opts = (uint16_t *)(ip + 1);
3138388214e4SPyun YongHyeon 		for (; len > 0; len -= sizeof(uint16_t), opts++) {
3139388214e4SPyun YongHyeon 			temp32 = csum - *opts;
3140388214e4SPyun YongHyeon 			temp32 = (temp32 >> 16) + (temp32 & 65535);
3141388214e4SPyun YongHyeon 			csum = temp32 & 65535;
3142388214e4SPyun YongHyeon 		}
3143388214e4SPyun YongHyeon 	}
3144388214e4SPyun YongHyeon 	m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
3145388214e4SPyun YongHyeon 	m->m_pkthdr.csum_data = csum;
3146388214e4SPyun YongHyeon }
3147388214e4SPyun YongHyeon 
31480dbe28b3SPyun YongHyeon static void
msk_rxeof(struct msk_if_softc * sc_if,uint32_t status,uint32_t control,int len)3149efb74172SPyun YongHyeon msk_rxeof(struct msk_if_softc *sc_if, uint32_t status, uint32_t control,
3150efb74172SPyun YongHyeon     int len)
31510dbe28b3SPyun YongHyeon {
31520dbe28b3SPyun YongHyeon 	struct mbuf *m;
31535ab8c4b8SJustin Hibbits 	if_t ifp;
31540dbe28b3SPyun YongHyeon 	struct msk_rxdesc *rxd;
31550dbe28b3SPyun YongHyeon 	int cons, rxlen;
31560dbe28b3SPyun YongHyeon 
31570dbe28b3SPyun YongHyeon 	ifp = sc_if->msk_ifp;
31580dbe28b3SPyun YongHyeon 
31590dbe28b3SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
31600dbe28b3SPyun YongHyeon 
31610dbe28b3SPyun YongHyeon 	cons = sc_if->msk_cdata.msk_rx_cons;
31620dbe28b3SPyun YongHyeon 	do {
31630dbe28b3SPyun YongHyeon 		rxlen = status >> 16;
316471e88667SPyun YongHyeon 		if ((status & GMR_FS_VLAN) != 0 &&
31655ab8c4b8SJustin Hibbits 		    (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0)
31660dbe28b3SPyun YongHyeon 			rxlen -= ETHER_VLAN_ENCAP_LEN;
3167224003b7SPyun YongHyeon 		if ((sc_if->msk_flags & MSK_FLAG_NORXCHK) != 0) {
3168224003b7SPyun YongHyeon 			/*
3169224003b7SPyun YongHyeon 			 * For controllers that returns bogus status code
3170224003b7SPyun YongHyeon 			 * just do minimal check and let upper stack
3171224003b7SPyun YongHyeon 			 * handle this frame.
3172224003b7SPyun YongHyeon 			 */
3173224003b7SPyun YongHyeon 			if (len > MSK_MAX_FRAMELEN || len < ETHER_HDR_LEN) {
31741162f065SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
3175224003b7SPyun YongHyeon 				msk_discard_rxbuf(sc_if, cons);
3176224003b7SPyun YongHyeon 				break;
3177224003b7SPyun YongHyeon 			}
3178224003b7SPyun YongHyeon 		} else if (len > sc_if->msk_framesize ||
31790dbe28b3SPyun YongHyeon 		    ((status & GMR_FS_ANY_ERR) != 0) ||
31800dbe28b3SPyun YongHyeon 		    ((status & GMR_FS_RX_OK) == 0) || (rxlen != len)) {
31810dbe28b3SPyun YongHyeon 			/* Don't count flow-control packet as errors. */
31820dbe28b3SPyun YongHyeon 			if ((status & GMR_FS_GOOD_FC) == 0)
31831162f065SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
31840dbe28b3SPyun YongHyeon 			msk_discard_rxbuf(sc_if, cons);
31850dbe28b3SPyun YongHyeon 			break;
31860dbe28b3SPyun YongHyeon 		}
3187355a415eSPyun YongHyeon #ifdef MSK_64BIT_DMA
3188355a415eSPyun YongHyeon 		rxd = &sc_if->msk_cdata.msk_rxdesc[(cons + 1) %
3189355a415eSPyun YongHyeon 		    MSK_RX_RING_CNT];
3190355a415eSPyun YongHyeon #else
31910dbe28b3SPyun YongHyeon 		rxd = &sc_if->msk_cdata.msk_rxdesc[cons];
3192355a415eSPyun YongHyeon #endif
31930dbe28b3SPyun YongHyeon 		m = rxd->rx_m;
31940dbe28b3SPyun YongHyeon 		if (msk_newbuf(sc_if, cons) != 0) {
31951162f065SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
31960dbe28b3SPyun YongHyeon 			/* Reuse old buffer. */
31970dbe28b3SPyun YongHyeon 			msk_discard_rxbuf(sc_if, cons);
31980dbe28b3SPyun YongHyeon 			break;
31990dbe28b3SPyun YongHyeon 		}
32000dbe28b3SPyun YongHyeon 		m->m_pkthdr.rcvif = ifp;
32010dbe28b3SPyun YongHyeon 		m->m_pkthdr.len = m->m_len = len;
320283c04c93SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
320383c04c93SPyun YongHyeon 		if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) != 0)
320483c04c93SPyun YongHyeon 			msk_fixup_rx(m);
320583c04c93SPyun YongHyeon #endif
32061162f065SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
32075ab8c4b8SJustin Hibbits 		if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0)
3208388214e4SPyun YongHyeon 			msk_rxcsum(sc_if, control, m);
32090dbe28b3SPyun YongHyeon 		/* Check for VLAN tagged packets. */
32100dbe28b3SPyun YongHyeon 		if ((status & GMR_FS_VLAN) != 0 &&
32115ab8c4b8SJustin Hibbits 		    (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
32120dbe28b3SPyun YongHyeon 			m->m_pkthdr.ether_vtag = sc_if->msk_vtag;
32130dbe28b3SPyun YongHyeon 			m->m_flags |= M_VLANTAG;
32140dbe28b3SPyun YongHyeon 		}
32150dbe28b3SPyun YongHyeon 		MSK_IF_UNLOCK(sc_if);
32165ab8c4b8SJustin Hibbits 		if_input(ifp, m);
32170dbe28b3SPyun YongHyeon 		MSK_IF_LOCK(sc_if);
32180dbe28b3SPyun YongHyeon 	} while (0);
32190dbe28b3SPyun YongHyeon 
3220355a415eSPyun YongHyeon 	MSK_RX_INC(sc_if->msk_cdata.msk_rx_cons, MSK_RX_RING_CNT);
3221355a415eSPyun YongHyeon 	MSK_RX_INC(sc_if->msk_cdata.msk_rx_prod, MSK_RX_RING_CNT);
32220dbe28b3SPyun YongHyeon }
32230dbe28b3SPyun YongHyeon 
32240dbe28b3SPyun YongHyeon static void
msk_jumbo_rxeof(struct msk_if_softc * sc_if,uint32_t status,uint32_t control,int len)3225efb74172SPyun YongHyeon msk_jumbo_rxeof(struct msk_if_softc *sc_if, uint32_t status, uint32_t control,
3226efb74172SPyun YongHyeon     int len)
32270dbe28b3SPyun YongHyeon {
32280dbe28b3SPyun YongHyeon 	struct mbuf *m;
32295ab8c4b8SJustin Hibbits 	if_t ifp;
32300dbe28b3SPyun YongHyeon 	struct msk_rxdesc *jrxd;
32310dbe28b3SPyun YongHyeon 	int cons, rxlen;
32320dbe28b3SPyun YongHyeon 
32330dbe28b3SPyun YongHyeon 	ifp = sc_if->msk_ifp;
32340dbe28b3SPyun YongHyeon 
32350dbe28b3SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
32360dbe28b3SPyun YongHyeon 
32370dbe28b3SPyun YongHyeon 	cons = sc_if->msk_cdata.msk_rx_cons;
32380dbe28b3SPyun YongHyeon 	do {
32390dbe28b3SPyun YongHyeon 		rxlen = status >> 16;
324071e88667SPyun YongHyeon 		if ((status & GMR_FS_VLAN) != 0 &&
32415ab8c4b8SJustin Hibbits 		    (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0)
32420dbe28b3SPyun YongHyeon 			rxlen -= ETHER_VLAN_ENCAP_LEN;
32430dbe28b3SPyun YongHyeon 		if (len > sc_if->msk_framesize ||
32440dbe28b3SPyun YongHyeon 		    ((status & GMR_FS_ANY_ERR) != 0) ||
32450dbe28b3SPyun YongHyeon 		    ((status & GMR_FS_RX_OK) == 0) || (rxlen != len)) {
32460dbe28b3SPyun YongHyeon 			/* Don't count flow-control packet as errors. */
32470dbe28b3SPyun YongHyeon 			if ((status & GMR_FS_GOOD_FC) == 0)
32481162f065SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
32490dbe28b3SPyun YongHyeon 			msk_discard_jumbo_rxbuf(sc_if, cons);
32500dbe28b3SPyun YongHyeon 			break;
32510dbe28b3SPyun YongHyeon 		}
3252355a415eSPyun YongHyeon #ifdef MSK_64BIT_DMA
3253355a415eSPyun YongHyeon 		jrxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[(cons + 1) %
3254355a415eSPyun YongHyeon 		    MSK_JUMBO_RX_RING_CNT];
3255355a415eSPyun YongHyeon #else
32560dbe28b3SPyun YongHyeon 		jrxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[cons];
3257355a415eSPyun YongHyeon #endif
32580dbe28b3SPyun YongHyeon 		m = jrxd->rx_m;
32590dbe28b3SPyun YongHyeon 		if (msk_jumbo_newbuf(sc_if, cons) != 0) {
32601162f065SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
32610dbe28b3SPyun YongHyeon 			/* Reuse old buffer. */
32620dbe28b3SPyun YongHyeon 			msk_discard_jumbo_rxbuf(sc_if, cons);
32630dbe28b3SPyun YongHyeon 			break;
32640dbe28b3SPyun YongHyeon 		}
32650dbe28b3SPyun YongHyeon 		m->m_pkthdr.rcvif = ifp;
32660dbe28b3SPyun YongHyeon 		m->m_pkthdr.len = m->m_len = len;
326783c04c93SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
326883c04c93SPyun YongHyeon 		if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) != 0)
326983c04c93SPyun YongHyeon 			msk_fixup_rx(m);
327083c04c93SPyun YongHyeon #endif
32711162f065SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
32725ab8c4b8SJustin Hibbits 		if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0)
3273388214e4SPyun YongHyeon 			msk_rxcsum(sc_if, control, m);
32740dbe28b3SPyun YongHyeon 		/* Check for VLAN tagged packets. */
32750dbe28b3SPyun YongHyeon 		if ((status & GMR_FS_VLAN) != 0 &&
32765ab8c4b8SJustin Hibbits 		    (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
32770dbe28b3SPyun YongHyeon 			m->m_pkthdr.ether_vtag = sc_if->msk_vtag;
32780dbe28b3SPyun YongHyeon 			m->m_flags |= M_VLANTAG;
32790dbe28b3SPyun YongHyeon 		}
32800dbe28b3SPyun YongHyeon 		MSK_IF_UNLOCK(sc_if);
32815ab8c4b8SJustin Hibbits 		if_input(ifp, m);
32820dbe28b3SPyun YongHyeon 		MSK_IF_LOCK(sc_if);
32830dbe28b3SPyun YongHyeon 	} while (0);
32840dbe28b3SPyun YongHyeon 
3285355a415eSPyun YongHyeon 	MSK_RX_INC(sc_if->msk_cdata.msk_rx_cons, MSK_JUMBO_RX_RING_CNT);
3286355a415eSPyun YongHyeon 	MSK_RX_INC(sc_if->msk_cdata.msk_rx_prod, MSK_JUMBO_RX_RING_CNT);
32870dbe28b3SPyun YongHyeon }
32880dbe28b3SPyun YongHyeon 
32890dbe28b3SPyun YongHyeon static void
msk_txeof(struct msk_if_softc * sc_if,int idx)32900dbe28b3SPyun YongHyeon msk_txeof(struct msk_if_softc *sc_if, int idx)
32910dbe28b3SPyun YongHyeon {
32920dbe28b3SPyun YongHyeon 	struct msk_txdesc *txd;
32930dbe28b3SPyun YongHyeon 	struct msk_tx_desc *cur_tx;
32945ab8c4b8SJustin Hibbits 	if_t ifp;
32950dbe28b3SPyun YongHyeon 	uint32_t control;
32960dbe28b3SPyun YongHyeon 	int cons, prog;
32970dbe28b3SPyun YongHyeon 
32980dbe28b3SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
32990dbe28b3SPyun YongHyeon 
33000dbe28b3SPyun YongHyeon 	ifp = sc_if->msk_ifp;
33010dbe28b3SPyun YongHyeon 
33020dbe28b3SPyun YongHyeon 	bus_dmamap_sync(sc_if->msk_cdata.msk_tx_ring_tag,
33030dbe28b3SPyun YongHyeon 	    sc_if->msk_cdata.msk_tx_ring_map,
33040dbe28b3SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
33050dbe28b3SPyun YongHyeon 	/*
33060dbe28b3SPyun YongHyeon 	 * Go through our tx ring and free mbufs for those
33070dbe28b3SPyun YongHyeon 	 * frames that have been sent.
33080dbe28b3SPyun YongHyeon 	 */
33090dbe28b3SPyun YongHyeon 	cons = sc_if->msk_cdata.msk_tx_cons;
33100dbe28b3SPyun YongHyeon 	prog = 0;
33110dbe28b3SPyun YongHyeon 	for (; cons != idx; MSK_INC(cons, MSK_TX_RING_CNT)) {
33120dbe28b3SPyun YongHyeon 		if (sc_if->msk_cdata.msk_tx_cnt <= 0)
33130dbe28b3SPyun YongHyeon 			break;
33140dbe28b3SPyun YongHyeon 		prog++;
33150dbe28b3SPyun YongHyeon 		cur_tx = &sc_if->msk_rdata.msk_tx_ring[cons];
33160dbe28b3SPyun YongHyeon 		control = le32toh(cur_tx->msk_control);
33170dbe28b3SPyun YongHyeon 		sc_if->msk_cdata.msk_tx_cnt--;
33185ab8c4b8SJustin Hibbits 		if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
33190dbe28b3SPyun YongHyeon 		if ((control & EOP) == 0)
33200dbe28b3SPyun YongHyeon 			continue;
33210dbe28b3SPyun YongHyeon 		txd = &sc_if->msk_cdata.msk_txdesc[cons];
33220dbe28b3SPyun YongHyeon 		bus_dmamap_sync(sc_if->msk_cdata.msk_tx_tag, txd->tx_dmamap,
33230dbe28b3SPyun YongHyeon 		    BUS_DMASYNC_POSTWRITE);
33240dbe28b3SPyun YongHyeon 		bus_dmamap_unload(sc_if->msk_cdata.msk_tx_tag, txd->tx_dmamap);
33250dbe28b3SPyun YongHyeon 
33261162f065SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
33270dbe28b3SPyun YongHyeon 		KASSERT(txd->tx_m != NULL, ("%s: freeing NULL mbuf!",
33280dbe28b3SPyun YongHyeon 		    __func__));
33290dbe28b3SPyun YongHyeon 		m_freem(txd->tx_m);
33300dbe28b3SPyun YongHyeon 		txd->tx_m = NULL;
33310dbe28b3SPyun YongHyeon 	}
33320dbe28b3SPyun YongHyeon 
33330dbe28b3SPyun YongHyeon 	if (prog > 0) {
33340dbe28b3SPyun YongHyeon 		sc_if->msk_cdata.msk_tx_cons = cons;
33350dbe28b3SPyun YongHyeon 		if (sc_if->msk_cdata.msk_tx_cnt == 0)
33362271eac7SPyun YongHyeon 			sc_if->msk_watchdog_timer = 0;
33370dbe28b3SPyun YongHyeon 		/* No need to sync LEs as we didn't update LEs. */
33380dbe28b3SPyun YongHyeon 	}
33390dbe28b3SPyun YongHyeon }
33400dbe28b3SPyun YongHyeon 
33410dbe28b3SPyun YongHyeon static void
msk_tick(void * xsc_if)33420dbe28b3SPyun YongHyeon msk_tick(void *xsc_if)
33430dbe28b3SPyun YongHyeon {
33448227d65bSAlexander Kabaev 	struct epoch_tracker et;
33450dbe28b3SPyun YongHyeon 	struct msk_if_softc *sc_if;
33460dbe28b3SPyun YongHyeon 	struct mii_data *mii;
33470dbe28b3SPyun YongHyeon 
33480dbe28b3SPyun YongHyeon 	sc_if = xsc_if;
33490dbe28b3SPyun YongHyeon 
33500dbe28b3SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
33510dbe28b3SPyun YongHyeon 
33520dbe28b3SPyun YongHyeon 	mii = device_get_softc(sc_if->msk_miibus);
33530dbe28b3SPyun YongHyeon 
33540dbe28b3SPyun YongHyeon 	mii_tick(mii);
335577e6010fSPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_LINK) == 0)
335677e6010fSPyun YongHyeon 		msk_miibus_statchg(sc_if->msk_if_dev);
33578227d65bSAlexander Kabaev 	NET_EPOCH_ENTER(et);
3358cf570c1fSPyun YongHyeon 	msk_handle_events(sc_if->msk_softc);
33598227d65bSAlexander Kabaev 	NET_EPOCH_EXIT(et);
33602271eac7SPyun YongHyeon 	msk_watchdog(sc_if);
33610dbe28b3SPyun YongHyeon 	callout_reset(&sc_if->msk_tick_ch, hz, msk_tick, sc_if);
33620dbe28b3SPyun YongHyeon }
33630dbe28b3SPyun YongHyeon 
33640dbe28b3SPyun YongHyeon static void
msk_intr_phy(struct msk_if_softc * sc_if)33650dbe28b3SPyun YongHyeon msk_intr_phy(struct msk_if_softc *sc_if)
33660dbe28b3SPyun YongHyeon {
33670dbe28b3SPyun YongHyeon 	uint16_t status;
33680dbe28b3SPyun YongHyeon 
33690dbe28b3SPyun YongHyeon 	msk_phy_readreg(sc_if, PHY_ADDR_MARV, PHY_MARV_INT_STAT);
3370431e606dSPyun YongHyeon 	status = msk_phy_readreg(sc_if, PHY_ADDR_MARV, PHY_MARV_INT_STAT);
33710dbe28b3SPyun YongHyeon 	/* Handle FIFO Underrun/Overflow? */
33720dbe28b3SPyun YongHyeon 	if ((status & PHY_M_IS_FIFO_ERROR))
33730dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
33740dbe28b3SPyun YongHyeon 		    "PHY FIFO underrun/overflow.\n");
33750dbe28b3SPyun YongHyeon }
33760dbe28b3SPyun YongHyeon 
33770dbe28b3SPyun YongHyeon static void
msk_intr_gmac(struct msk_if_softc * sc_if)33780dbe28b3SPyun YongHyeon msk_intr_gmac(struct msk_if_softc *sc_if)
33790dbe28b3SPyun YongHyeon {
33800dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
33810dbe28b3SPyun YongHyeon 	uint8_t status;
33820dbe28b3SPyun YongHyeon 
33830dbe28b3SPyun YongHyeon 	sc = sc_if->msk_softc;
33840dbe28b3SPyun YongHyeon 	status = CSR_READ_1(sc, MR_ADDR(sc_if->msk_port, GMAC_IRQ_SRC));
33850dbe28b3SPyun YongHyeon 
33860dbe28b3SPyun YongHyeon 	/* GMAC Rx FIFO overrun. */
3387ff080216SPyun YongHyeon 	if ((status & GM_IS_RX_FF_OR) != 0)
33880dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T),
33890dbe28b3SPyun YongHyeon 		    GMF_CLI_RX_FO);
33900dbe28b3SPyun YongHyeon 	/* GMAC Tx FIFO underrun. */
33910dbe28b3SPyun YongHyeon 	if ((status & GM_IS_TX_FF_UR) != 0) {
33920dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T),
33930dbe28b3SPyun YongHyeon 		    GMF_CLI_TX_FU);
33940dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev, "Tx FIFO underrun!\n");
33950dbe28b3SPyun YongHyeon 		/*
33960dbe28b3SPyun YongHyeon 		 * XXX
33970dbe28b3SPyun YongHyeon 		 * In case of Tx underrun, we may need to flush/reset
33980dbe28b3SPyun YongHyeon 		 * Tx MAC but that would also require resynchronization
3399b1ce21c6SRebecca Cran 		 * with status LEs. Reinitializing status LEs would
34000dbe28b3SPyun YongHyeon 		 * affect other port in dual MAC configuration so it
34010dbe28b3SPyun YongHyeon 		 * should be avoided as possible as we can.
34020dbe28b3SPyun YongHyeon 		 * Due to lack of documentation it's all vague guess but
34030dbe28b3SPyun YongHyeon 		 * it needs more investigation.
34040dbe28b3SPyun YongHyeon 		 */
34050dbe28b3SPyun YongHyeon 	}
34060dbe28b3SPyun YongHyeon }
34070dbe28b3SPyun YongHyeon 
34080dbe28b3SPyun YongHyeon static void
msk_handle_hwerr(struct msk_if_softc * sc_if,uint32_t status)34090dbe28b3SPyun YongHyeon msk_handle_hwerr(struct msk_if_softc *sc_if, uint32_t status)
34100dbe28b3SPyun YongHyeon {
34110dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
34120dbe28b3SPyun YongHyeon 
34130dbe28b3SPyun YongHyeon 	sc = sc_if->msk_softc;
34140dbe28b3SPyun YongHyeon 	if ((status & Y2_IS_PAR_RD1) != 0) {
34150dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
34160dbe28b3SPyun YongHyeon 		    "RAM buffer read parity error\n");
34170dbe28b3SPyun YongHyeon 		/* Clear IRQ. */
34180dbe28b3SPyun YongHyeon 		CSR_WRITE_2(sc, SELECT_RAM_BUFFER(sc_if->msk_port, B3_RI_CTRL),
34190dbe28b3SPyun YongHyeon 		    RI_CLR_RD_PERR);
34200dbe28b3SPyun YongHyeon 	}
34210dbe28b3SPyun YongHyeon 	if ((status & Y2_IS_PAR_WR1) != 0) {
34220dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
34230dbe28b3SPyun YongHyeon 		    "RAM buffer write parity error\n");
34240dbe28b3SPyun YongHyeon 		/* Clear IRQ. */
34250dbe28b3SPyun YongHyeon 		CSR_WRITE_2(sc, SELECT_RAM_BUFFER(sc_if->msk_port, B3_RI_CTRL),
34260dbe28b3SPyun YongHyeon 		    RI_CLR_WR_PERR);
34270dbe28b3SPyun YongHyeon 	}
34280dbe28b3SPyun YongHyeon 	if ((status & Y2_IS_PAR_MAC1) != 0) {
34290dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev, "Tx MAC parity error\n");
34300dbe28b3SPyun YongHyeon 		/* Clear IRQ. */
34310dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T),
34320dbe28b3SPyun YongHyeon 		    GMF_CLI_TX_PE);
34330dbe28b3SPyun YongHyeon 	}
34340dbe28b3SPyun YongHyeon 	if ((status & Y2_IS_PAR_RX1) != 0) {
34350dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev, "Rx parity error\n");
34360dbe28b3SPyun YongHyeon 		/* Clear IRQ. */
34370dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_CSR), BMU_CLR_IRQ_PAR);
34380dbe28b3SPyun YongHyeon 	}
34390dbe28b3SPyun YongHyeon 	if ((status & (Y2_IS_TCP_TXS1 | Y2_IS_TCP_TXA1)) != 0) {
34400dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev, "TCP segmentation error\n");
34410dbe28b3SPyun YongHyeon 		/* Clear IRQ. */
34420dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR), BMU_CLR_IRQ_TCP);
34430dbe28b3SPyun YongHyeon 	}
34440dbe28b3SPyun YongHyeon }
34450dbe28b3SPyun YongHyeon 
34460dbe28b3SPyun YongHyeon static void
msk_intr_hwerr(struct msk_softc * sc)34470dbe28b3SPyun YongHyeon msk_intr_hwerr(struct msk_softc *sc)
34480dbe28b3SPyun YongHyeon {
34490dbe28b3SPyun YongHyeon 	uint32_t status;
34500dbe28b3SPyun YongHyeon 	uint32_t tlphead[4];
34510dbe28b3SPyun YongHyeon 
34520dbe28b3SPyun YongHyeon 	status = CSR_READ_4(sc, B0_HWE_ISRC);
34530dbe28b3SPyun YongHyeon 	/* Time Stamp timer overflow. */
34540dbe28b3SPyun YongHyeon 	if ((status & Y2_IS_TIST_OV) != 0)
34550dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, GMAC_TI_ST_CTRL, GMT_ST_CLR_IRQ);
34560dbe28b3SPyun YongHyeon 	if ((status & Y2_IS_PCI_NEXP) != 0) {
34570dbe28b3SPyun YongHyeon 		/*
3458453130d9SPedro F. Giffuni 		 * PCI Express Error occurred which is not described in PEX
34590dbe28b3SPyun YongHyeon 		 * spec.
34600dbe28b3SPyun YongHyeon 		 * This error is also mapped either to Master Abort(
34610dbe28b3SPyun YongHyeon 		 * Y2_IS_MST_ERR) or Target Abort (Y2_IS_IRQ_STAT) bit and
34620dbe28b3SPyun YongHyeon 		 * can only be cleared there.
34630dbe28b3SPyun YongHyeon                  */
34640dbe28b3SPyun YongHyeon 		device_printf(sc->msk_dev,
34650dbe28b3SPyun YongHyeon 		    "PCI Express protocol violation error\n");
34660dbe28b3SPyun YongHyeon 	}
34670dbe28b3SPyun YongHyeon 
34680dbe28b3SPyun YongHyeon 	if ((status & (Y2_IS_MST_ERR | Y2_IS_IRQ_STAT)) != 0) {
34690dbe28b3SPyun YongHyeon 		uint16_t v16;
34700dbe28b3SPyun YongHyeon 
34710dbe28b3SPyun YongHyeon 		if ((status & Y2_IS_MST_ERR) != 0)
34720dbe28b3SPyun YongHyeon 			device_printf(sc->msk_dev,
34730dbe28b3SPyun YongHyeon 			    "unexpected IRQ Status error\n");
34740dbe28b3SPyun YongHyeon 		else
34750dbe28b3SPyun YongHyeon 			device_printf(sc->msk_dev,
34760dbe28b3SPyun YongHyeon 			    "unexpected IRQ Master error\n");
34770dbe28b3SPyun YongHyeon 		/* Reset all bits in the PCI status register. */
34780dbe28b3SPyun YongHyeon 		v16 = pci_read_config(sc->msk_dev, PCIR_STATUS, 2);
34790dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_ON);
34800dbe28b3SPyun YongHyeon 		pci_write_config(sc->msk_dev, PCIR_STATUS, v16 |
34810dbe28b3SPyun YongHyeon 		    PCIM_STATUS_PERR | PCIM_STATUS_SERR | PCIM_STATUS_RMABORT |
3482d1a02e09SJohn Baldwin 		    PCIM_STATUS_RTABORT | PCIM_STATUS_MDPERR, 2);
34830dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
34840dbe28b3SPyun YongHyeon 	}
34850dbe28b3SPyun YongHyeon 
34860dbe28b3SPyun YongHyeon 	/* Check for PCI Express Uncorrectable Error. */
34870dbe28b3SPyun YongHyeon 	if ((status & Y2_IS_PCI_EXP) != 0) {
34880dbe28b3SPyun YongHyeon 		uint32_t v32;
34890dbe28b3SPyun YongHyeon 
34900dbe28b3SPyun YongHyeon 		/*
34910dbe28b3SPyun YongHyeon 		 * On PCI Express bus bridges are called root complexes (RC).
34920dbe28b3SPyun YongHyeon 		 * PCI Express errors are recognized by the root complex too,
34930dbe28b3SPyun YongHyeon 		 * which requests the system to handle the problem. After
3494ab3f6b34SGabor Kovesdan 		 * error occurrence it may be that no access to the adapter
34950dbe28b3SPyun YongHyeon 		 * may be performed any longer.
34960dbe28b3SPyun YongHyeon 		 */
34970dbe28b3SPyun YongHyeon 
34980dbe28b3SPyun YongHyeon 		v32 = CSR_PCI_READ_4(sc, PEX_UNC_ERR_STAT);
34990dbe28b3SPyun YongHyeon 		if ((v32 & PEX_UNSUP_REQ) != 0) {
35000dbe28b3SPyun YongHyeon 			/* Ignore unsupported request error. */
35010dbe28b3SPyun YongHyeon 			device_printf(sc->msk_dev,
35020dbe28b3SPyun YongHyeon 			    "Uncorrectable PCI Express error\n");
35030dbe28b3SPyun YongHyeon 		}
35040dbe28b3SPyun YongHyeon 		if ((v32 & (PEX_FATAL_ERRORS | PEX_POIS_TLP)) != 0) {
35050dbe28b3SPyun YongHyeon 			int i;
35060dbe28b3SPyun YongHyeon 
35070dbe28b3SPyun YongHyeon 			/* Get TLP header form Log Registers. */
35080dbe28b3SPyun YongHyeon 			for (i = 0; i < 4; i++)
35090dbe28b3SPyun YongHyeon 				tlphead[i] = CSR_PCI_READ_4(sc,
35100dbe28b3SPyun YongHyeon 				    PEX_HEADER_LOG + i * 4);
35110dbe28b3SPyun YongHyeon 			/* Check for vendor defined broadcast message. */
35120dbe28b3SPyun YongHyeon 			if (!(tlphead[0] == 0x73004001 && tlphead[1] == 0x7f)) {
35130dbe28b3SPyun YongHyeon 				sc->msk_intrhwemask &= ~Y2_IS_PCI_EXP;
35140dbe28b3SPyun YongHyeon 				CSR_WRITE_4(sc, B0_HWE_IMSK,
35150dbe28b3SPyun YongHyeon 				    sc->msk_intrhwemask);
35160dbe28b3SPyun YongHyeon 				CSR_READ_4(sc, B0_HWE_IMSK);
35170dbe28b3SPyun YongHyeon 			}
35180dbe28b3SPyun YongHyeon 		}
35190dbe28b3SPyun YongHyeon 		/* Clear the interrupt. */
35200dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_ON);
35210dbe28b3SPyun YongHyeon 		CSR_PCI_WRITE_4(sc, PEX_UNC_ERR_STAT, 0xffffffff);
35220dbe28b3SPyun YongHyeon 		CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
35230dbe28b3SPyun YongHyeon 	}
35240dbe28b3SPyun YongHyeon 
35250dbe28b3SPyun YongHyeon 	if ((status & Y2_HWE_L1_MASK) != 0 && sc->msk_if[MSK_PORT_A] != NULL)
35260dbe28b3SPyun YongHyeon 		msk_handle_hwerr(sc->msk_if[MSK_PORT_A], status);
35270dbe28b3SPyun YongHyeon 	if ((status & Y2_HWE_L2_MASK) != 0 && sc->msk_if[MSK_PORT_B] != NULL)
35280dbe28b3SPyun YongHyeon 		msk_handle_hwerr(sc->msk_if[MSK_PORT_B], status >> 8);
35290dbe28b3SPyun YongHyeon }
35300dbe28b3SPyun YongHyeon 
35310dbe28b3SPyun YongHyeon static __inline void
msk_rxput(struct msk_if_softc * sc_if)35320dbe28b3SPyun YongHyeon msk_rxput(struct msk_if_softc *sc_if)
35330dbe28b3SPyun YongHyeon {
35340dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
35350dbe28b3SPyun YongHyeon 
35360dbe28b3SPyun YongHyeon 	sc = sc_if->msk_softc;
353785b340cbSPyun YongHyeon 	if (sc_if->msk_framesize > (MCLBYTES - MSK_RX_BUF_ALIGN))
35380dbe28b3SPyun YongHyeon 		bus_dmamap_sync(
35390dbe28b3SPyun YongHyeon 		    sc_if->msk_cdata.msk_jumbo_rx_ring_tag,
35400dbe28b3SPyun YongHyeon 		    sc_if->msk_cdata.msk_jumbo_rx_ring_map,
35410dbe28b3SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
35420dbe28b3SPyun YongHyeon 	else
35430dbe28b3SPyun YongHyeon 		bus_dmamap_sync(
35440dbe28b3SPyun YongHyeon 		    sc_if->msk_cdata.msk_rx_ring_tag,
35450dbe28b3SPyun YongHyeon 		    sc_if->msk_cdata.msk_rx_ring_map,
35460dbe28b3SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
35470dbe28b3SPyun YongHyeon 	CSR_WRITE_2(sc, Y2_PREF_Q_ADDR(sc_if->msk_rxq,
35480dbe28b3SPyun YongHyeon 	    PREF_UNIT_PUT_IDX_REG), sc_if->msk_cdata.msk_rx_prod);
35490dbe28b3SPyun YongHyeon }
35500dbe28b3SPyun YongHyeon 
35510dbe28b3SPyun YongHyeon static int
msk_handle_events(struct msk_softc * sc)35520dbe28b3SPyun YongHyeon msk_handle_events(struct msk_softc *sc)
35530dbe28b3SPyun YongHyeon {
35540dbe28b3SPyun YongHyeon 	struct msk_if_softc *sc_if;
35550dbe28b3SPyun YongHyeon 	int rxput[2];
35560dbe28b3SPyun YongHyeon 	struct msk_stat_desc *sd;
35570dbe28b3SPyun YongHyeon 	uint32_t control, status;
3558c876b43fSPyun YongHyeon 	int cons, len, port, rxprog;
35590dbe28b3SPyun YongHyeon 
356007fa0751SPyun YongHyeon 	if (sc->msk_stat_cons == CSR_READ_2(sc, STAT_PUT_IDX))
356107fa0751SPyun YongHyeon 		return (0);
356207fa0751SPyun YongHyeon 
35630dbe28b3SPyun YongHyeon 	/* Sync status LEs. */
35640dbe28b3SPyun YongHyeon 	bus_dmamap_sync(sc->msk_stat_tag, sc->msk_stat_map,
35650dbe28b3SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
35660dbe28b3SPyun YongHyeon 
35670dbe28b3SPyun YongHyeon 	rxput[MSK_PORT_A] = rxput[MSK_PORT_B] = 0;
35680dbe28b3SPyun YongHyeon 	rxprog = 0;
3569c876b43fSPyun YongHyeon 	cons = sc->msk_stat_cons;
3570c876b43fSPyun YongHyeon 	for (;;) {
35710dbe28b3SPyun YongHyeon 		sd = &sc->msk_stat_ring[cons];
35720dbe28b3SPyun YongHyeon 		control = le32toh(sd->msk_control);
35730dbe28b3SPyun YongHyeon 		if ((control & HW_OWNER) == 0)
35740dbe28b3SPyun YongHyeon 			break;
35750dbe28b3SPyun YongHyeon 		control &= ~HW_OWNER;
35760dbe28b3SPyun YongHyeon 		sd->msk_control = htole32(control);
35770dbe28b3SPyun YongHyeon 		status = le32toh(sd->msk_status);
35780dbe28b3SPyun YongHyeon 		len = control & STLE_LEN_MASK;
35790dbe28b3SPyun YongHyeon 		port = (control >> 16) & 0x01;
35800dbe28b3SPyun YongHyeon 		sc_if = sc->msk_if[port];
35810dbe28b3SPyun YongHyeon 		if (sc_if == NULL) {
35820dbe28b3SPyun YongHyeon 			device_printf(sc->msk_dev, "invalid port opcode "
35830dbe28b3SPyun YongHyeon 			    "0x%08x\n", control & STLE_OP_MASK);
35840dbe28b3SPyun YongHyeon 			continue;
35850dbe28b3SPyun YongHyeon 		}
35860dbe28b3SPyun YongHyeon 
35870dbe28b3SPyun YongHyeon 		switch (control & STLE_OP_MASK) {
35880dbe28b3SPyun YongHyeon 		case OP_RXVLAN:
35890dbe28b3SPyun YongHyeon 			sc_if->msk_vtag = ntohs(len);
35900dbe28b3SPyun YongHyeon 			break;
35910dbe28b3SPyun YongHyeon 		case OP_RXCHKSVLAN:
35920dbe28b3SPyun YongHyeon 			sc_if->msk_vtag = ntohs(len);
3593388214e4SPyun YongHyeon 			/* FALLTHROUGH */
3594388214e4SPyun YongHyeon 		case OP_RXCHKS:
3595388214e4SPyun YongHyeon 			sc_if->msk_csum = status;
35960dbe28b3SPyun YongHyeon 			break;
35970dbe28b3SPyun YongHyeon 		case OP_RXSTAT:
35985ab8c4b8SJustin Hibbits 			if (!(if_getdrvflags(sc_if->msk_ifp) & IFF_DRV_RUNNING))
359931fefd0dSPyun YongHyeon 				break;
360085b340cbSPyun YongHyeon 			if (sc_if->msk_framesize >
360185b340cbSPyun YongHyeon 			    (MCLBYTES - MSK_RX_BUF_ALIGN))
3602efb74172SPyun YongHyeon 				msk_jumbo_rxeof(sc_if, status, control, len);
36030dbe28b3SPyun YongHyeon 			else
3604efb74172SPyun YongHyeon 				msk_rxeof(sc_if, status, control, len);
36050dbe28b3SPyun YongHyeon 			rxprog++;
36060dbe28b3SPyun YongHyeon 			/*
36070dbe28b3SPyun YongHyeon 			 * Because there is no way to sync single Rx LE
36080dbe28b3SPyun YongHyeon 			 * put the DMA sync operation off until the end of
36090dbe28b3SPyun YongHyeon 			 * event processing.
36100dbe28b3SPyun YongHyeon 			 */
36110dbe28b3SPyun YongHyeon 			rxput[port]++;
36120dbe28b3SPyun YongHyeon 			/* Update prefetch unit if we've passed water mark. */
36130dbe28b3SPyun YongHyeon 			if (rxput[port] >= sc_if->msk_cdata.msk_rx_putwm) {
36140dbe28b3SPyun YongHyeon 				msk_rxput(sc_if);
36150dbe28b3SPyun YongHyeon 				rxput[port] = 0;
36160dbe28b3SPyun YongHyeon 			}
36170dbe28b3SPyun YongHyeon 			break;
36180dbe28b3SPyun YongHyeon 		case OP_TXINDEXLE:
36190dbe28b3SPyun YongHyeon 			if (sc->msk_if[MSK_PORT_A] != NULL)
36200dbe28b3SPyun YongHyeon 				msk_txeof(sc->msk_if[MSK_PORT_A],
36210dbe28b3SPyun YongHyeon 				    status & STLE_TXA1_MSKL);
36220dbe28b3SPyun YongHyeon 			if (sc->msk_if[MSK_PORT_B] != NULL)
36230dbe28b3SPyun YongHyeon 				msk_txeof(sc->msk_if[MSK_PORT_B],
36240dbe28b3SPyun YongHyeon 				    ((status & STLE_TXA2_MSKL) >>
36250dbe28b3SPyun YongHyeon 				    STLE_TXA2_SHIFTL) |
36260dbe28b3SPyun YongHyeon 				    ((len & STLE_TXA2_MSKH) <<
36270dbe28b3SPyun YongHyeon 				    STLE_TXA2_SHIFTH));
36280dbe28b3SPyun YongHyeon 			break;
36290dbe28b3SPyun YongHyeon 		default:
36300dbe28b3SPyun YongHyeon 			device_printf(sc->msk_dev, "unhandled opcode 0x%08x\n",
36310dbe28b3SPyun YongHyeon 			    control & STLE_OP_MASK);
36320dbe28b3SPyun YongHyeon 			break;
36330dbe28b3SPyun YongHyeon 		}
3634355a415eSPyun YongHyeon 		MSK_INC(cons, sc->msk_stat_count);
36350dbe28b3SPyun YongHyeon 		if (rxprog > sc->msk_process_limit)
36360dbe28b3SPyun YongHyeon 			break;
36370dbe28b3SPyun YongHyeon 	}
36380dbe28b3SPyun YongHyeon 
36390dbe28b3SPyun YongHyeon 	sc->msk_stat_cons = cons;
364017f6f326SPyun YongHyeon 	bus_dmamap_sync(sc->msk_stat_tag, sc->msk_stat_map,
364117f6f326SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
36420dbe28b3SPyun YongHyeon 
36430dbe28b3SPyun YongHyeon 	if (rxput[MSK_PORT_A] > 0)
36440dbe28b3SPyun YongHyeon 		msk_rxput(sc->msk_if[MSK_PORT_A]);
36450dbe28b3SPyun YongHyeon 	if (rxput[MSK_PORT_B] > 0)
36460dbe28b3SPyun YongHyeon 		msk_rxput(sc->msk_if[MSK_PORT_B]);
36470dbe28b3SPyun YongHyeon 
364807fa0751SPyun YongHyeon 	return (sc->msk_stat_cons != CSR_READ_2(sc, STAT_PUT_IDX));
36490dbe28b3SPyun YongHyeon }
36500dbe28b3SPyun YongHyeon 
365153dcfbd1SPyun YongHyeon static void
msk_intr(void * xsc)3652c876b43fSPyun YongHyeon msk_intr(void *xsc)
365353dcfbd1SPyun YongHyeon {
365453dcfbd1SPyun YongHyeon 	struct msk_softc *sc;
365553dcfbd1SPyun YongHyeon 	struct msk_if_softc *sc_if0, *sc_if1;
36565ab8c4b8SJustin Hibbits 	if_t ifp0, ifp1;
365753dcfbd1SPyun YongHyeon 	uint32_t status;
3658c876b43fSPyun YongHyeon 	int domore;
365953dcfbd1SPyun YongHyeon 
366053dcfbd1SPyun YongHyeon 	sc = xsc;
366153dcfbd1SPyun YongHyeon 	MSK_LOCK(sc);
366253dcfbd1SPyun YongHyeon 
366353dcfbd1SPyun YongHyeon 	/* Reading B0_Y2_SP_ISRC2 masks further interrupts. */
366453dcfbd1SPyun YongHyeon 	status = CSR_READ_4(sc, B0_Y2_SP_ISRC2);
3665ab7df1e4SPyun YongHyeon 	if (status == 0 || status == 0xffffffff ||
3666ab7df1e4SPyun YongHyeon 	    (sc->msk_pflags & MSK_FLAG_SUSPEND) != 0 ||
366753dcfbd1SPyun YongHyeon 	    (status & sc->msk_intrmask) == 0) {
366853dcfbd1SPyun YongHyeon 		CSR_WRITE_4(sc, B0_Y2_SP_ICR, 2);
36693d763c31SPyun YongHyeon 		MSK_UNLOCK(sc);
367053dcfbd1SPyun YongHyeon 		return;
367153dcfbd1SPyun YongHyeon 	}
367253dcfbd1SPyun YongHyeon 
367353dcfbd1SPyun YongHyeon 	sc_if0 = sc->msk_if[MSK_PORT_A];
367453dcfbd1SPyun YongHyeon 	sc_if1 = sc->msk_if[MSK_PORT_B];
367553dcfbd1SPyun YongHyeon 	ifp0 = ifp1 = NULL;
367653dcfbd1SPyun YongHyeon 	if (sc_if0 != NULL)
367753dcfbd1SPyun YongHyeon 		ifp0 = sc_if0->msk_ifp;
367853dcfbd1SPyun YongHyeon 	if (sc_if1 != NULL)
367953dcfbd1SPyun YongHyeon 		ifp1 = sc_if1->msk_ifp;
368053dcfbd1SPyun YongHyeon 
368153dcfbd1SPyun YongHyeon 	if ((status & Y2_IS_IRQ_PHY1) != 0 && sc_if0 != NULL)
368253dcfbd1SPyun YongHyeon 		msk_intr_phy(sc_if0);
368353dcfbd1SPyun YongHyeon 	if ((status & Y2_IS_IRQ_PHY2) != 0 && sc_if1 != NULL)
368453dcfbd1SPyun YongHyeon 		msk_intr_phy(sc_if1);
368553dcfbd1SPyun YongHyeon 	if ((status & Y2_IS_IRQ_MAC1) != 0 && sc_if0 != NULL)
368653dcfbd1SPyun YongHyeon 		msk_intr_gmac(sc_if0);
368753dcfbd1SPyun YongHyeon 	if ((status & Y2_IS_IRQ_MAC2) != 0 && sc_if1 != NULL)
368853dcfbd1SPyun YongHyeon 		msk_intr_gmac(sc_if1);
368953dcfbd1SPyun YongHyeon 	if ((status & (Y2_IS_CHK_RX1 | Y2_IS_CHK_RX2)) != 0) {
369053dcfbd1SPyun YongHyeon 		device_printf(sc->msk_dev, "Rx descriptor error\n");
369153dcfbd1SPyun YongHyeon 		sc->msk_intrmask &= ~(Y2_IS_CHK_RX1 | Y2_IS_CHK_RX2);
369253dcfbd1SPyun YongHyeon 		CSR_WRITE_4(sc, B0_IMSK, sc->msk_intrmask);
369353dcfbd1SPyun YongHyeon 		CSR_READ_4(sc, B0_IMSK);
369453dcfbd1SPyun YongHyeon 	}
369553dcfbd1SPyun YongHyeon         if ((status & (Y2_IS_CHK_TXA1 | Y2_IS_CHK_TXA2)) != 0) {
369653dcfbd1SPyun YongHyeon 		device_printf(sc->msk_dev, "Tx descriptor error\n");
369753dcfbd1SPyun YongHyeon 		sc->msk_intrmask &= ~(Y2_IS_CHK_TXA1 | Y2_IS_CHK_TXA2);
369853dcfbd1SPyun YongHyeon 		CSR_WRITE_4(sc, B0_IMSK, sc->msk_intrmask);
369953dcfbd1SPyun YongHyeon 		CSR_READ_4(sc, B0_IMSK);
370053dcfbd1SPyun YongHyeon 	}
370153dcfbd1SPyun YongHyeon 	if ((status & Y2_IS_HW_ERR) != 0)
370253dcfbd1SPyun YongHyeon 		msk_intr_hwerr(sc);
370353dcfbd1SPyun YongHyeon 
37040dbe28b3SPyun YongHyeon 	domore = msk_handle_events(sc);
3705c876b43fSPyun YongHyeon 	if ((status & Y2_IS_STAT_BMU) != 0 && domore == 0)
37060dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, STAT_CTRL, SC_STAT_CLR_IRQ);
37070dbe28b3SPyun YongHyeon 
37080dbe28b3SPyun YongHyeon 	/* Reenable interrupts. */
37090dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, B0_Y2_SP_ICR, 2);
3710c876b43fSPyun YongHyeon 
37115ab8c4b8SJustin Hibbits 	if (ifp0 != NULL && (if_getdrvflags(ifp0) & IFF_DRV_RUNNING) != 0 &&
37125ab8c4b8SJustin Hibbits 	    !if_sendq_empty(ifp0))
3713c876b43fSPyun YongHyeon 		msk_start_locked(ifp0);
37145ab8c4b8SJustin Hibbits 	if (ifp1 != NULL && (if_getdrvflags(ifp1) & IFF_DRV_RUNNING) != 0 &&
37155ab8c4b8SJustin Hibbits 	    !if_sendq_empty(ifp1))
3716c876b43fSPyun YongHyeon 		msk_start_locked(ifp1);
3717c876b43fSPyun YongHyeon 
3718c876b43fSPyun YongHyeon 	MSK_UNLOCK(sc);
37190dbe28b3SPyun YongHyeon }
37200dbe28b3SPyun YongHyeon 
37210dbe28b3SPyun YongHyeon static void
msk_set_tx_stfwd(struct msk_if_softc * sc_if)3722daf29227SPyun YongHyeon msk_set_tx_stfwd(struct msk_if_softc *sc_if)
3723daf29227SPyun YongHyeon {
3724daf29227SPyun YongHyeon 	struct msk_softc *sc;
37255ab8c4b8SJustin Hibbits 	if_t ifp;
3726daf29227SPyun YongHyeon 
3727daf29227SPyun YongHyeon 	ifp = sc_if->msk_ifp;
3728daf29227SPyun YongHyeon 	sc = sc_if->msk_softc;
37297b4f47c1SPyun YongHyeon 	if ((sc->msk_hw_id == CHIP_ID_YUKON_EX &&
37307b4f47c1SPyun YongHyeon 	    sc->msk_hw_rev != CHIP_REV_YU_EX_A0) ||
37317b4f47c1SPyun YongHyeon 	    sc->msk_hw_id >= CHIP_ID_YUKON_SUPR) {
37327b4f47c1SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T),
37337b4f47c1SPyun YongHyeon 		    TX_STFW_ENA);
37347b4f47c1SPyun YongHyeon 	} else {
37355ab8c4b8SJustin Hibbits 		if (if_getmtu(ifp) > ETHERMTU) {
3736daf29227SPyun YongHyeon 			/* Set Tx GMAC FIFO Almost Empty Threshold. */
3737daf29227SPyun YongHyeon 			CSR_WRITE_4(sc,
3738daf29227SPyun YongHyeon 			    MR_ADDR(sc_if->msk_port, TX_GMF_AE_THR),
3739daf29227SPyun YongHyeon 			    MSK_ECU_JUMBO_WM << 16 | MSK_ECU_AE_THR);
3740daf29227SPyun YongHyeon 			/* Disable Store & Forward mode for Tx. */
37417b4f47c1SPyun YongHyeon 			CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T),
37427b4f47c1SPyun YongHyeon 			    TX_STFW_DIS);
3743daf29227SPyun YongHyeon 		} else {
37447b4f47c1SPyun YongHyeon 			CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T),
37457b4f47c1SPyun YongHyeon 			    TX_STFW_ENA);
3746daf29227SPyun YongHyeon 		}
3747daf29227SPyun YongHyeon 	}
3748daf29227SPyun YongHyeon }
3749daf29227SPyun YongHyeon 
3750daf29227SPyun YongHyeon static void
msk_init(void * xsc)37510dbe28b3SPyun YongHyeon msk_init(void *xsc)
37520dbe28b3SPyun YongHyeon {
37530dbe28b3SPyun YongHyeon 	struct msk_if_softc *sc_if = xsc;
37540dbe28b3SPyun YongHyeon 
37550dbe28b3SPyun YongHyeon 	MSK_IF_LOCK(sc_if);
37560dbe28b3SPyun YongHyeon 	msk_init_locked(sc_if);
37570dbe28b3SPyun YongHyeon 	MSK_IF_UNLOCK(sc_if);
37580dbe28b3SPyun YongHyeon }
37590dbe28b3SPyun YongHyeon 
37600dbe28b3SPyun YongHyeon static void
msk_init_locked(struct msk_if_softc * sc_if)37610dbe28b3SPyun YongHyeon msk_init_locked(struct msk_if_softc *sc_if)
37620dbe28b3SPyun YongHyeon {
37630dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
37645ab8c4b8SJustin Hibbits 	if_t ifp;
37650dbe28b3SPyun YongHyeon 	struct mii_data	 *mii;
3766cf5756a6SPyun YongHyeon 	uint8_t *eaddr;
37670dbe28b3SPyun YongHyeon 	uint16_t gmac;
376861708f4cSPyun YongHyeon 	uint32_t reg;
3769cf5756a6SPyun YongHyeon 	int error;
37700dbe28b3SPyun YongHyeon 
37710dbe28b3SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
37720dbe28b3SPyun YongHyeon 
37730dbe28b3SPyun YongHyeon 	ifp = sc_if->msk_ifp;
37740dbe28b3SPyun YongHyeon 	sc = sc_if->msk_softc;
37750dbe28b3SPyun YongHyeon 	mii = device_get_softc(sc_if->msk_miibus);
37760dbe28b3SPyun YongHyeon 
37775ab8c4b8SJustin Hibbits 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
377889e22666SPyun YongHyeon 		return;
377989e22666SPyun YongHyeon 
37800dbe28b3SPyun YongHyeon 	error = 0;
37810dbe28b3SPyun YongHyeon 	/* Cancel pending I/O and free all Rx/Tx buffers. */
37820dbe28b3SPyun YongHyeon 	msk_stop(sc_if);
37830dbe28b3SPyun YongHyeon 
37845ab8c4b8SJustin Hibbits 	if (if_getmtu(ifp) < ETHERMTU)
378585b340cbSPyun YongHyeon 		sc_if->msk_framesize = ETHERMTU;
378685b340cbSPyun YongHyeon 	else
37875ab8c4b8SJustin Hibbits 		sc_if->msk_framesize = if_getmtu(ifp);
378885b340cbSPyun YongHyeon 	sc_if->msk_framesize += ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
37895ab8c4b8SJustin Hibbits 	if (if_getmtu(ifp) > ETHERMTU &&
3790e2b16603SPyun YongHyeon 	    (sc_if->msk_flags & MSK_FLAG_JUMBO_NOCSUM) != 0) {
37915ab8c4b8SJustin Hibbits 		if_sethwassistbits(ifp, 0, (MSK_CSUM_FEATURES | CSUM_TSO));
37925ab8c4b8SJustin Hibbits 		if_setcapenablebit(ifp, 0, (IFCAP_TSO4 | IFCAP_TXCSUM));
3793a109c74fSPyun YongHyeon 	}
37940dbe28b3SPyun YongHyeon 
3795e6e23ffeSPyun YongHyeon 	/* GMAC Control reset. */
3796e6e23ffeSPyun YongHyeon 	CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, GMAC_CTRL), GMC_RST_SET);
3797e6e23ffeSPyun YongHyeon 	CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, GMAC_CTRL), GMC_RST_CLR);
3798e6e23ffeSPyun YongHyeon 	CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, GMAC_CTRL), GMC_F_LOOPB_OFF);
3799e0029a72SPyun YongHyeon 	if (sc->msk_hw_id == CHIP_ID_YUKON_EX ||
3800e0029a72SPyun YongHyeon 	    sc->msk_hw_id == CHIP_ID_YUKON_SUPR)
3801daf29227SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, GMAC_CTRL),
3802daf29227SPyun YongHyeon 		    GMC_BYP_MACSECRX_ON | GMC_BYP_MACSECTX_ON |
3803daf29227SPyun YongHyeon 		    GMC_BYP_RETR_ON);
3804e6e23ffeSPyun YongHyeon 
38050dbe28b3SPyun YongHyeon 	/*
3806e6e23ffeSPyun YongHyeon 	 * Initialize GMAC first such that speed/duplex/flow-control
3807e6e23ffeSPyun YongHyeon 	 * parameters are renegotiated when interface is brought up.
38080dbe28b3SPyun YongHyeon 	 */
3809e6e23ffeSPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_GP_CTRL, 0);
38100dbe28b3SPyun YongHyeon 
38110dbe28b3SPyun YongHyeon 	/* Dummy read the Interrupt Source Register. */
38120dbe28b3SPyun YongHyeon 	CSR_READ_1(sc, MR_ADDR(sc_if->msk_port, GMAC_IRQ_SRC));
38130dbe28b3SPyun YongHyeon 
38143a91ee71SPyun YongHyeon 	/* Clear MIB stats. */
38153a91ee71SPyun YongHyeon 	msk_stats_clear(sc_if);
38160dbe28b3SPyun YongHyeon 
38170dbe28b3SPyun YongHyeon 	/* Disable FCS. */
38180dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_RX_CTRL, GM_RXCR_CRC_DIS);
38190dbe28b3SPyun YongHyeon 
38200dbe28b3SPyun YongHyeon 	/* Setup Transmit Control Register. */
38210dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_TX_CTRL, TX_COL_THR(TX_COL_DEF));
38220dbe28b3SPyun YongHyeon 
38230dbe28b3SPyun YongHyeon 	/* Setup Transmit Flow Control Register. */
38240dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_TX_FLOW_CTRL, 0xffff);
38250dbe28b3SPyun YongHyeon 
38260dbe28b3SPyun YongHyeon 	/* Setup Transmit Parameter Register. */
38270dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_TX_PARAM,
38280dbe28b3SPyun YongHyeon 	    TX_JAM_LEN_VAL(TX_JAM_LEN_DEF) | TX_JAM_IPG_VAL(TX_JAM_IPG_DEF) |
38290dbe28b3SPyun YongHyeon 	    TX_IPG_JAM_DATA(TX_IPG_JAM_DEF) | TX_BACK_OFF_LIM(TX_BOF_LIM_DEF));
38300dbe28b3SPyun YongHyeon 
38310dbe28b3SPyun YongHyeon 	gmac = DATA_BLIND_VAL(DATA_BLIND_DEF) |
38320dbe28b3SPyun YongHyeon 	    GM_SMOD_VLAN_ENA | IPG_DATA_VAL(IPG_DATA_DEF);
38330dbe28b3SPyun YongHyeon 
38345ab8c4b8SJustin Hibbits 	if (if_getmtu(ifp) > ETHERMTU)
38350dbe28b3SPyun YongHyeon 		gmac |= GM_SMOD_JUMBO_ENA;
38360dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_SERIAL_MODE, gmac);
38370dbe28b3SPyun YongHyeon 
38380dbe28b3SPyun YongHyeon 	/* Set station address. */
38395ab8c4b8SJustin Hibbits 	eaddr = if_getlladdr(ifp);
3840cf5756a6SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_SRC_ADDR_1L,
3841cf5756a6SPyun YongHyeon 	    eaddr[0] | (eaddr[1] << 8));
3842cf5756a6SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_SRC_ADDR_1M,
3843cf5756a6SPyun YongHyeon 	    eaddr[2] | (eaddr[3] << 8));
3844cf5756a6SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_SRC_ADDR_1H,
3845cf5756a6SPyun YongHyeon 	    eaddr[4] | (eaddr[5] << 8));
3846cf5756a6SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_SRC_ADDR_2L,
3847cf5756a6SPyun YongHyeon 	    eaddr[0] | (eaddr[1] << 8));
3848cf5756a6SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_SRC_ADDR_2M,
3849cf5756a6SPyun YongHyeon 	    eaddr[2] | (eaddr[3] << 8));
3850cf5756a6SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_SRC_ADDR_2H,
3851cf5756a6SPyun YongHyeon 	    eaddr[4] | (eaddr[5] << 8));
38520dbe28b3SPyun YongHyeon 
38530dbe28b3SPyun YongHyeon 	/* Disable interrupts for counter overflows. */
38540dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_TX_IRQ_MSK, 0);
38550dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_RX_IRQ_MSK, 0);
38560dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_TR_IRQ_MSK, 0);
38570dbe28b3SPyun YongHyeon 
38580dbe28b3SPyun YongHyeon 	/* Configure Rx MAC FIFO. */
38590dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T), GMF_RST_SET);
38600dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T), GMF_RST_CLR);
386161708f4cSPyun YongHyeon 	reg = GMF_OPER_ON | GMF_RX_F_FL_ON;
3862daf29227SPyun YongHyeon 	if (sc->msk_hw_id == CHIP_ID_YUKON_FE_P ||
3863daf29227SPyun YongHyeon 	    sc->msk_hw_id == CHIP_ID_YUKON_EX)
386461708f4cSPyun YongHyeon 		reg |= GMF_RX_OVER_ON;
386561708f4cSPyun YongHyeon 	CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T), reg);
38660dbe28b3SPyun YongHyeon 
38676d6588a1SPyun YongHyeon 	/* Set receive filter. */
38686d6588a1SPyun YongHyeon 	msk_rxfilter(sc_if);
38690dbe28b3SPyun YongHyeon 
3870cde64af3SPyun YongHyeon 	if (sc->msk_hw_id == CHIP_ID_YUKON_XL) {
3871cde64af3SPyun YongHyeon 		/* Clear flush mask - HW bug. */
3872cde64af3SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_FL_MSK), 0);
3873cde64af3SPyun YongHyeon 	} else {
38740dbe28b3SPyun YongHyeon 		/* Flush Rx MAC FIFO on any flow control or error. */
38750dbe28b3SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_FL_MSK),
38760dbe28b3SPyun YongHyeon 		    GMR_FS_ANY_ERR);
3877cde64af3SPyun YongHyeon 	}
38780dbe28b3SPyun YongHyeon 
3879d5d60164SPyun YongHyeon 	/*
3880d5d60164SPyun YongHyeon 	 * Set Rx FIFO flush threshold to 64 bytes + 1 FIFO word
3881d5d60164SPyun YongHyeon 	 * due to hardware hang on receipt of pause frames.
3882d5d60164SPyun YongHyeon 	 */
3883224003b7SPyun YongHyeon 	reg = RX_GMF_FL_THR_DEF + 1;
3884224003b7SPyun YongHyeon 	/* Another magic for Yukon FE+ - From Linux. */
3885224003b7SPyun YongHyeon 	if (sc->msk_hw_id == CHIP_ID_YUKON_FE_P &&
3886224003b7SPyun YongHyeon 	    sc->msk_hw_rev == CHIP_REV_YU_FE_P_A0)
3887224003b7SPyun YongHyeon 		reg = 0x178;
3888224003b7SPyun YongHyeon 	CSR_WRITE_2(sc, MR_ADDR(sc_if->msk_port, RX_GMF_FL_THR), reg);
38890dbe28b3SPyun YongHyeon 
38900dbe28b3SPyun YongHyeon 	/* Configure Tx MAC FIFO. */
38910dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), GMF_RST_SET);
38920dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), GMF_RST_CLR);
38930dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), GMF_OPER_ON);
38940dbe28b3SPyun YongHyeon 
38950dbe28b3SPyun YongHyeon 	/* Configure hardware VLAN tag insertion/stripping. */
38960dbe28b3SPyun YongHyeon 	msk_setvlan(sc_if, ifp);
38970dbe28b3SPyun YongHyeon 
389883c04c93SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) == 0) {
3899b1ce21c6SRebecca Cran 		/* Set Rx Pause threshold. */
3900106b2e2fSPyun YongHyeon 		CSR_WRITE_2(sc, MR_ADDR(sc_if->msk_port, RX_GMF_LP_THR),
39010dbe28b3SPyun YongHyeon 		    MSK_ECU_LLPP);
3902106b2e2fSPyun YongHyeon 		CSR_WRITE_2(sc, MR_ADDR(sc_if->msk_port, RX_GMF_UP_THR),
39030dbe28b3SPyun YongHyeon 		    MSK_ECU_ULPP);
3904daf29227SPyun YongHyeon 		/* Configure store-and-forward for Tx. */
3905daf29227SPyun YongHyeon 		msk_set_tx_stfwd(sc_if);
39060dbe28b3SPyun YongHyeon 	}
39070dbe28b3SPyun YongHyeon 
3908224003b7SPyun YongHyeon 	if (sc->msk_hw_id == CHIP_ID_YUKON_FE_P &&
3909224003b7SPyun YongHyeon 	    sc->msk_hw_rev == CHIP_REV_YU_FE_P_A0) {
3910224003b7SPyun YongHyeon 		/* Disable dynamic watermark - from Linux. */
3911224003b7SPyun YongHyeon 		reg = CSR_READ_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_EA));
3912224003b7SPyun YongHyeon 		reg &= ~0x03;
3913224003b7SPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_EA), reg);
3914224003b7SPyun YongHyeon 	}
3915224003b7SPyun YongHyeon 
39160dbe28b3SPyun YongHyeon 	/*
39170dbe28b3SPyun YongHyeon 	 * Disable Force Sync bit and Alloc bit in Tx RAM interface
39180dbe28b3SPyun YongHyeon 	 * arbiter as we don't use Sync Tx queue.
39190dbe28b3SPyun YongHyeon 	 */
39200dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, MR_ADDR(sc_if->msk_port, TXA_CTRL),
39210dbe28b3SPyun YongHyeon 	    TXA_DIS_FSYNC | TXA_DIS_ALLOC | TXA_STOP_RC);
39220dbe28b3SPyun YongHyeon 	/* Enable the RAM Interface Arbiter. */
39230dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, MR_ADDR(sc_if->msk_port, TXA_CTRL), TXA_ENA_ARB);
39240dbe28b3SPyun YongHyeon 
39250dbe28b3SPyun YongHyeon 	/* Setup RAM buffer. */
39260dbe28b3SPyun YongHyeon 	msk_set_rambuffer(sc_if);
39270dbe28b3SPyun YongHyeon 
39280dbe28b3SPyun YongHyeon 	/* Disable Tx sync Queue. */
39290dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_txsq, RB_CTRL), RB_RST_SET);
39300dbe28b3SPyun YongHyeon 
39310dbe28b3SPyun YongHyeon 	/* Setup Tx Queue Bus Memory Interface. */
39320dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR), BMU_CLR_RESET);
39330dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR), BMU_OPER_INIT);
39340dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR), BMU_FIFO_OP_ON);
39350dbe28b3SPyun YongHyeon 	CSR_WRITE_2(sc, Q_ADDR(sc_if->msk_txq, Q_WM), MSK_BMU_TX_WM);
3936ebb25bfaSPyun YongHyeon 	switch (sc->msk_hw_id) {
3937ebb25bfaSPyun YongHyeon 	case CHIP_ID_YUKON_EC_U:
3938ebb25bfaSPyun YongHyeon 		if (sc->msk_hw_rev == CHIP_REV_YU_EC_U_A0) {
39390dbe28b3SPyun YongHyeon 			/* Fix for Yukon-EC Ultra: set BMU FIFO level */
3940ebb25bfaSPyun YongHyeon 			CSR_WRITE_2(sc, Q_ADDR(sc_if->msk_txq, Q_AL),
3941ebb25bfaSPyun YongHyeon 			    MSK_ECU_TXFF_LEV);
3942ebb25bfaSPyun YongHyeon 		}
3943ebb25bfaSPyun YongHyeon 		break;
3944ebb25bfaSPyun YongHyeon 	case CHIP_ID_YUKON_EX:
3945ebb25bfaSPyun YongHyeon 		/*
3946ebb25bfaSPyun YongHyeon 		 * Yukon Extreme seems to have silicon bug for
3947ebb25bfaSPyun YongHyeon 		 * automatic Tx checksum calculation capability.
3948ebb25bfaSPyun YongHyeon 		 */
3949ebb25bfaSPyun YongHyeon 		if (sc->msk_hw_rev == CHIP_REV_YU_EX_B0)
3950ebb25bfaSPyun YongHyeon 			CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_F),
3951ebb25bfaSPyun YongHyeon 			    F_TX_CHK_AUTO_OFF);
3952ebb25bfaSPyun YongHyeon 		break;
39530dbe28b3SPyun YongHyeon 	}
39540dbe28b3SPyun YongHyeon 
39550dbe28b3SPyun YongHyeon 	/* Setup Rx Queue Bus Memory Interface. */
39560dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_CSR), BMU_CLR_RESET);
39570dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_CSR), BMU_OPER_INIT);
39580dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_CSR), BMU_FIFO_OP_ON);
39590dbe28b3SPyun YongHyeon 	CSR_WRITE_2(sc, Q_ADDR(sc_if->msk_rxq, Q_WM), MSK_BMU_RX_WM);
39600dbe28b3SPyun YongHyeon         if (sc->msk_hw_id == CHIP_ID_YUKON_EC_U &&
39610dbe28b3SPyun YongHyeon 	    sc->msk_hw_rev >= CHIP_REV_YU_EC_U_A1) {
39620dbe28b3SPyun YongHyeon 		/* MAC Rx RAM Read is controlled by hardware. */
39630dbe28b3SPyun YongHyeon                 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_F), F_M_RX_RAM_DIS);
39640dbe28b3SPyun YongHyeon 	}
39650dbe28b3SPyun YongHyeon 
39660dbe28b3SPyun YongHyeon 	msk_set_prefetch(sc, sc_if->msk_txq,
39670dbe28b3SPyun YongHyeon 	    sc_if->msk_rdata.msk_tx_ring_paddr, MSK_TX_RING_CNT - 1);
39680dbe28b3SPyun YongHyeon 	msk_init_tx_ring(sc_if);
39690dbe28b3SPyun YongHyeon 
39700dbe28b3SPyun YongHyeon 	/* Disable Rx checksum offload and RSS hash. */
3971388214e4SPyun YongHyeon 	reg = BMU_DIS_RX_RSS_HASH;
3972388214e4SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0 &&
39735ab8c4b8SJustin Hibbits 	    (if_getcapenable(ifp) & IFCAP_RXCSUM) != 0)
3974388214e4SPyun YongHyeon 		reg |= BMU_ENA_RX_CHKSUM;
3975388214e4SPyun YongHyeon 	else
3976388214e4SPyun YongHyeon 		reg |= BMU_DIS_RX_CHKSUM;
3977388214e4SPyun YongHyeon 	CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_CSR), reg);
397885b340cbSPyun YongHyeon 	if (sc_if->msk_framesize > (MCLBYTES - MSK_RX_BUF_ALIGN)) {
39790dbe28b3SPyun YongHyeon 		msk_set_prefetch(sc, sc_if->msk_rxq,
39800dbe28b3SPyun YongHyeon 		    sc_if->msk_rdata.msk_jumbo_rx_ring_paddr,
39810dbe28b3SPyun YongHyeon 		    MSK_JUMBO_RX_RING_CNT - 1);
39820dbe28b3SPyun YongHyeon 		error = msk_init_jumbo_rx_ring(sc_if);
39830dbe28b3SPyun YongHyeon 	 } else {
39840dbe28b3SPyun YongHyeon 		msk_set_prefetch(sc, sc_if->msk_rxq,
39850dbe28b3SPyun YongHyeon 		    sc_if->msk_rdata.msk_rx_ring_paddr,
39860dbe28b3SPyun YongHyeon 		    MSK_RX_RING_CNT - 1);
39870dbe28b3SPyun YongHyeon 		error = msk_init_rx_ring(sc_if);
39880dbe28b3SPyun YongHyeon 	}
39890dbe28b3SPyun YongHyeon 	if (error != 0) {
39900dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev,
39910dbe28b3SPyun YongHyeon 		    "initialization failed: no memory for Rx buffers\n");
39920dbe28b3SPyun YongHyeon 		msk_stop(sc_if);
39930dbe28b3SPyun YongHyeon 		return;
39940dbe28b3SPyun YongHyeon 	}
3995e0029a72SPyun YongHyeon 	if (sc->msk_hw_id == CHIP_ID_YUKON_EX ||
3996e0029a72SPyun YongHyeon 	    sc->msk_hw_id == CHIP_ID_YUKON_SUPR) {
39977c8db6fdSPyun YongHyeon 		/* Disable flushing of non-ASF packets. */
39987c8db6fdSPyun YongHyeon 		CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T),
39997c8db6fdSPyun YongHyeon 		    GMF_RX_MACSEC_FLUSH_OFF);
40007c8db6fdSPyun YongHyeon 	}
40010dbe28b3SPyun YongHyeon 
40020dbe28b3SPyun YongHyeon 	/* Configure interrupt handling. */
40030dbe28b3SPyun YongHyeon 	if (sc_if->msk_port == MSK_PORT_A) {
40040dbe28b3SPyun YongHyeon 		sc->msk_intrmask |= Y2_IS_PORT_A;
40050dbe28b3SPyun YongHyeon 		sc->msk_intrhwemask |= Y2_HWE_L1_MASK;
40060dbe28b3SPyun YongHyeon 	} else {
40070dbe28b3SPyun YongHyeon 		sc->msk_intrmask |= Y2_IS_PORT_B;
40080dbe28b3SPyun YongHyeon 		sc->msk_intrhwemask |= Y2_HWE_L2_MASK;
40090dbe28b3SPyun YongHyeon 	}
4010cf570c1fSPyun YongHyeon 	/* Configure IRQ moderation mask. */
4011cf570c1fSPyun YongHyeon 	CSR_WRITE_4(sc, B2_IRQM_MSK, sc->msk_intrmask);
4012cf570c1fSPyun YongHyeon 	if (sc->msk_int_holdoff > 0) {
4013cf570c1fSPyun YongHyeon 		/* Configure initial IRQ moderation timer value. */
4014cf570c1fSPyun YongHyeon 		CSR_WRITE_4(sc, B2_IRQM_INI,
4015cf570c1fSPyun YongHyeon 		    MSK_USECS(sc, sc->msk_int_holdoff));
4016cf570c1fSPyun YongHyeon 		CSR_WRITE_4(sc, B2_IRQM_VAL,
4017cf570c1fSPyun YongHyeon 		    MSK_USECS(sc, sc->msk_int_holdoff));
4018cf570c1fSPyun YongHyeon 		/* Start IRQ moderation. */
4019cf570c1fSPyun YongHyeon 		CSR_WRITE_1(sc, B2_IRQM_CTRL, TIM_START);
4020cf570c1fSPyun YongHyeon 	}
40210dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, B0_HWE_IMSK, sc->msk_intrhwemask);
40220dbe28b3SPyun YongHyeon 	CSR_READ_4(sc, B0_HWE_IMSK);
40230dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, B0_IMSK, sc->msk_intrmask);
40240dbe28b3SPyun YongHyeon 	CSR_READ_4(sc, B0_IMSK);
40250dbe28b3SPyun YongHyeon 
40265ab8c4b8SJustin Hibbits 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
40275ab8c4b8SJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
40280dbe28b3SPyun YongHyeon 
4029b52d3ddbSPyun YongHyeon 	sc_if->msk_flags &= ~MSK_FLAG_LINK;
4030b52d3ddbSPyun YongHyeon 	mii_mediachg(mii);
4031b52d3ddbSPyun YongHyeon 
40320dbe28b3SPyun YongHyeon 	callout_reset(&sc_if->msk_tick_ch, hz, msk_tick, sc_if);
40330dbe28b3SPyun YongHyeon }
40340dbe28b3SPyun YongHyeon 
40350dbe28b3SPyun YongHyeon static void
msk_set_rambuffer(struct msk_if_softc * sc_if)40360dbe28b3SPyun YongHyeon msk_set_rambuffer(struct msk_if_softc *sc_if)
40370dbe28b3SPyun YongHyeon {
40380dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
40390dbe28b3SPyun YongHyeon 	int ltpp, utpp;
40400dbe28b3SPyun YongHyeon 
40410dbe28b3SPyun YongHyeon 	sc = sc_if->msk_softc;
404283c04c93SPyun YongHyeon 	if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) == 0)
404383c04c93SPyun YongHyeon 		return;
40440dbe28b3SPyun YongHyeon 
40450dbe28b3SPyun YongHyeon 	/* Setup Rx Queue. */
40460dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_rxq, RB_CTRL), RB_RST_CLR);
40470dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_rxq, RB_START),
40480dbe28b3SPyun YongHyeon 	    sc->msk_rxqstart[sc_if->msk_port] / 8);
40490dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_rxq, RB_END),
40500dbe28b3SPyun YongHyeon 	    sc->msk_rxqend[sc_if->msk_port] / 8);
40510dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_rxq, RB_WP),
40520dbe28b3SPyun YongHyeon 	    sc->msk_rxqstart[sc_if->msk_port] / 8);
40530dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_rxq, RB_RP),
40540dbe28b3SPyun YongHyeon 	    sc->msk_rxqstart[sc_if->msk_port] / 8);
40550dbe28b3SPyun YongHyeon 
40560dbe28b3SPyun YongHyeon 	utpp = (sc->msk_rxqend[sc_if->msk_port] + 1 -
40570dbe28b3SPyun YongHyeon 	    sc->msk_rxqstart[sc_if->msk_port] - MSK_RB_ULPP) / 8;
40580dbe28b3SPyun YongHyeon 	ltpp = (sc->msk_rxqend[sc_if->msk_port] + 1 -
40590dbe28b3SPyun YongHyeon 	    sc->msk_rxqstart[sc_if->msk_port] - MSK_RB_LLPP_B) / 8;
40600dbe28b3SPyun YongHyeon 	if (sc->msk_rxqsize < MSK_MIN_RXQ_SIZE)
40610dbe28b3SPyun YongHyeon 		ltpp += (MSK_RB_LLPP_B - MSK_RB_LLPP_S) / 8;
40620dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_rxq, RB_RX_UTPP), utpp);
40630dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_rxq, RB_RX_LTPP), ltpp);
40640dbe28b3SPyun YongHyeon 	/* Set Rx priority(RB_RX_UTHP/RB_RX_LTHP) thresholds? */
40650dbe28b3SPyun YongHyeon 
40660dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_rxq, RB_CTRL), RB_ENA_OP_MD);
40670dbe28b3SPyun YongHyeon 	CSR_READ_1(sc, RB_ADDR(sc_if->msk_rxq, RB_CTRL));
40680dbe28b3SPyun YongHyeon 
40690dbe28b3SPyun YongHyeon 	/* Setup Tx Queue. */
40700dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_txq, RB_CTRL), RB_RST_CLR);
40710dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_txq, RB_START),
40720dbe28b3SPyun YongHyeon 	    sc->msk_txqstart[sc_if->msk_port] / 8);
40730dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_txq, RB_END),
40740dbe28b3SPyun YongHyeon 	    sc->msk_txqend[sc_if->msk_port] / 8);
40750dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_txq, RB_WP),
40760dbe28b3SPyun YongHyeon 	    sc->msk_txqstart[sc_if->msk_port] / 8);
40770dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_txq, RB_RP),
40780dbe28b3SPyun YongHyeon 	    sc->msk_txqstart[sc_if->msk_port] / 8);
40790dbe28b3SPyun YongHyeon 	/* Enable Store & Forward for Tx side. */
40800dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_txq, RB_CTRL), RB_ENA_STFWD);
40810dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_txq, RB_CTRL), RB_ENA_OP_MD);
40820dbe28b3SPyun YongHyeon 	CSR_READ_1(sc, RB_ADDR(sc_if->msk_txq, RB_CTRL));
40830dbe28b3SPyun YongHyeon }
40840dbe28b3SPyun YongHyeon 
40850dbe28b3SPyun YongHyeon static void
msk_set_prefetch(struct msk_softc * sc,int qaddr,bus_addr_t addr,uint32_t count)40860dbe28b3SPyun YongHyeon msk_set_prefetch(struct msk_softc *sc, int qaddr, bus_addr_t addr,
40870dbe28b3SPyun YongHyeon     uint32_t count)
40880dbe28b3SPyun YongHyeon {
40890dbe28b3SPyun YongHyeon 
40900dbe28b3SPyun YongHyeon 	/* Reset the prefetch unit. */
40910dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_CTRL_REG),
40920dbe28b3SPyun YongHyeon 	    PREF_UNIT_RST_SET);
40930dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_CTRL_REG),
40940dbe28b3SPyun YongHyeon 	    PREF_UNIT_RST_CLR);
40950dbe28b3SPyun YongHyeon 	/* Set LE base address. */
40960dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_ADDR_LOW_REG),
40970dbe28b3SPyun YongHyeon 	    MSK_ADDR_LO(addr));
40980dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_ADDR_HI_REG),
40990dbe28b3SPyun YongHyeon 	    MSK_ADDR_HI(addr));
41000dbe28b3SPyun YongHyeon 	/* Set the list last index. */
41010dbe28b3SPyun YongHyeon 	CSR_WRITE_2(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_LAST_IDX_REG),
41020dbe28b3SPyun YongHyeon 	    count);
41030dbe28b3SPyun YongHyeon 	/* Turn on prefetch unit. */
41040dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_CTRL_REG),
41050dbe28b3SPyun YongHyeon 	    PREF_UNIT_OP_ON);
41060dbe28b3SPyun YongHyeon 	/* Dummy read to ensure write. */
41070dbe28b3SPyun YongHyeon 	CSR_READ_4(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_CTRL_REG));
41080dbe28b3SPyun YongHyeon }
41090dbe28b3SPyun YongHyeon 
41100dbe28b3SPyun YongHyeon static void
msk_stop(struct msk_if_softc * sc_if)41110dbe28b3SPyun YongHyeon msk_stop(struct msk_if_softc *sc_if)
41120dbe28b3SPyun YongHyeon {
41130dbe28b3SPyun YongHyeon 	struct msk_softc *sc;
41140dbe28b3SPyun YongHyeon 	struct msk_txdesc *txd;
41150dbe28b3SPyun YongHyeon 	struct msk_rxdesc *rxd;
41160dbe28b3SPyun YongHyeon 	struct msk_rxdesc *jrxd;
41175ab8c4b8SJustin Hibbits 	if_t ifp;
41180dbe28b3SPyun YongHyeon 	uint32_t val;
41190dbe28b3SPyun YongHyeon 	int i;
41200dbe28b3SPyun YongHyeon 
41210dbe28b3SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
41220dbe28b3SPyun YongHyeon 	sc = sc_if->msk_softc;
41230dbe28b3SPyun YongHyeon 	ifp = sc_if->msk_ifp;
41240dbe28b3SPyun YongHyeon 
41250dbe28b3SPyun YongHyeon 	callout_stop(&sc_if->msk_tick_ch);
41262271eac7SPyun YongHyeon 	sc_if->msk_watchdog_timer = 0;
41270dbe28b3SPyun YongHyeon 
41280dbe28b3SPyun YongHyeon 	/* Disable interrupts. */
41290dbe28b3SPyun YongHyeon 	if (sc_if->msk_port == MSK_PORT_A) {
41300dbe28b3SPyun YongHyeon 		sc->msk_intrmask &= ~Y2_IS_PORT_A;
41310dbe28b3SPyun YongHyeon 		sc->msk_intrhwemask &= ~Y2_HWE_L1_MASK;
41320dbe28b3SPyun YongHyeon 	} else {
41330dbe28b3SPyun YongHyeon 		sc->msk_intrmask &= ~Y2_IS_PORT_B;
41340dbe28b3SPyun YongHyeon 		sc->msk_intrhwemask &= ~Y2_HWE_L2_MASK;
41350dbe28b3SPyun YongHyeon 	}
41360dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, B0_HWE_IMSK, sc->msk_intrhwemask);
41370dbe28b3SPyun YongHyeon 	CSR_READ_4(sc, B0_HWE_IMSK);
41380dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, B0_IMSK, sc->msk_intrmask);
41390dbe28b3SPyun YongHyeon 	CSR_READ_4(sc, B0_IMSK);
41400dbe28b3SPyun YongHyeon 
41410dbe28b3SPyun YongHyeon 	/* Disable Tx/Rx MAC. */
41420dbe28b3SPyun YongHyeon 	val = GMAC_READ_2(sc, sc_if->msk_port, GM_GP_CTRL);
41430dbe28b3SPyun YongHyeon 	val &= ~(GM_GPCR_RX_ENA | GM_GPCR_TX_ENA);
41440dbe28b3SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_GP_CTRL, val);
41450dbe28b3SPyun YongHyeon 	/* Read again to ensure writing. */
41460dbe28b3SPyun YongHyeon 	GMAC_READ_2(sc, sc_if->msk_port, GM_GP_CTRL);
41473a91ee71SPyun YongHyeon 	/* Update stats and clear counters. */
41483a91ee71SPyun YongHyeon 	msk_stats_update(sc_if);
41490dbe28b3SPyun YongHyeon 
41500dbe28b3SPyun YongHyeon 	/* Stop Tx BMU. */
41510dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR), BMU_STOP);
41520dbe28b3SPyun YongHyeon 	val = CSR_READ_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR));
41530dbe28b3SPyun YongHyeon 	for (i = 0; i < MSK_TIMEOUT; i++) {
41540dbe28b3SPyun YongHyeon 		if ((val & (BMU_STOP | BMU_IDLE)) == 0) {
41550dbe28b3SPyun YongHyeon 			CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR),
41560dbe28b3SPyun YongHyeon 			    BMU_STOP);
4157e4816325SPyun YongHyeon 			val = CSR_READ_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR));
41580dbe28b3SPyun YongHyeon 		} else
41590dbe28b3SPyun YongHyeon 			break;
41600dbe28b3SPyun YongHyeon 		DELAY(1);
41610dbe28b3SPyun YongHyeon 	}
41620dbe28b3SPyun YongHyeon 	if (i == MSK_TIMEOUT)
41630dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev, "Tx BMU stop failed\n");
41640dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_txq, RB_CTRL),
41650dbe28b3SPyun YongHyeon 	    RB_RST_SET | RB_DIS_OP_MD);
41660dbe28b3SPyun YongHyeon 
41670dbe28b3SPyun YongHyeon 	/* Disable all GMAC interrupt. */
41680dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, MR_ADDR(sc_if->msk_port, GMAC_IRQ_MSK), 0);
41690dbe28b3SPyun YongHyeon 	/* Disable PHY interrupt. */
41700dbe28b3SPyun YongHyeon 	msk_phy_writereg(sc_if, PHY_ADDR_MARV, PHY_MARV_INT_MASK, 0);
41710dbe28b3SPyun YongHyeon 
41720dbe28b3SPyun YongHyeon 	/* Disable the RAM Interface Arbiter. */
41730dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, MR_ADDR(sc_if->msk_port, TXA_CTRL), TXA_DIS_ARB);
41740dbe28b3SPyun YongHyeon 
41750dbe28b3SPyun YongHyeon 	/* Reset the PCI FIFO of the async Tx queue */
41760dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR),
41770dbe28b3SPyun YongHyeon 	    BMU_RST_SET | BMU_FIFO_RST);
41780dbe28b3SPyun YongHyeon 
41790dbe28b3SPyun YongHyeon 	/* Reset the Tx prefetch units. */
41800dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(sc_if->msk_txq, PREF_UNIT_CTRL_REG),
41810dbe28b3SPyun YongHyeon 	    PREF_UNIT_RST_SET);
41820dbe28b3SPyun YongHyeon 
41830dbe28b3SPyun YongHyeon 	/* Reset the RAM Buffer async Tx queue. */
41840dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_txq, RB_CTRL), RB_RST_SET);
41850dbe28b3SPyun YongHyeon 
41860dbe28b3SPyun YongHyeon 	/* Reset Tx MAC FIFO. */
41870dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), GMF_RST_SET);
41880dbe28b3SPyun YongHyeon 	/* Set Pause Off. */
41890dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, GMAC_CTRL), GMC_PAUSE_OFF);
41900dbe28b3SPyun YongHyeon 
41910dbe28b3SPyun YongHyeon 	/*
41920dbe28b3SPyun YongHyeon 	 * The Rx Stop command will not work for Yukon-2 if the BMU does not
41930dbe28b3SPyun YongHyeon 	 * reach the end of packet and since we can't make sure that we have
41940dbe28b3SPyun YongHyeon 	 * incoming data, we must reset the BMU while it is not during a DMA
41950dbe28b3SPyun YongHyeon 	 * transfer. Since it is possible that the Rx path is still active,
41960dbe28b3SPyun YongHyeon 	 * the Rx RAM buffer will be stopped first, so any possible incoming
41970dbe28b3SPyun YongHyeon 	 * data will not trigger a DMA. After the RAM buffer is stopped, the
41980dbe28b3SPyun YongHyeon 	 * BMU is polled until any DMA in progress is ended and only then it
41990dbe28b3SPyun YongHyeon 	 * will be reset.
42000dbe28b3SPyun YongHyeon 	 */
42010dbe28b3SPyun YongHyeon 
42020dbe28b3SPyun YongHyeon 	/* Disable the RAM Buffer receive queue. */
42030dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_rxq, RB_CTRL), RB_DIS_OP_MD);
42040dbe28b3SPyun YongHyeon 	for (i = 0; i < MSK_TIMEOUT; i++) {
42050dbe28b3SPyun YongHyeon 		if (CSR_READ_1(sc, RB_ADDR(sc_if->msk_rxq, Q_RSL)) ==
42060dbe28b3SPyun YongHyeon 		    CSR_READ_1(sc, RB_ADDR(sc_if->msk_rxq, Q_RL)))
42070dbe28b3SPyun YongHyeon 			break;
42080dbe28b3SPyun YongHyeon 		DELAY(1);
42090dbe28b3SPyun YongHyeon 	}
42100dbe28b3SPyun YongHyeon 	if (i == MSK_TIMEOUT)
42110dbe28b3SPyun YongHyeon 		device_printf(sc_if->msk_if_dev, "Rx BMU stop failed\n");
42120dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_CSR),
42130dbe28b3SPyun YongHyeon 	    BMU_RST_SET | BMU_FIFO_RST);
42140dbe28b3SPyun YongHyeon 	/* Reset the Rx prefetch unit. */
42150dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(sc_if->msk_rxq, PREF_UNIT_CTRL_REG),
42160dbe28b3SPyun YongHyeon 	    PREF_UNIT_RST_SET);
42170dbe28b3SPyun YongHyeon 	/* Reset the RAM Buffer receive queue. */
42180dbe28b3SPyun YongHyeon 	CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_rxq, RB_CTRL), RB_RST_SET);
42190dbe28b3SPyun YongHyeon 	/* Reset Rx MAC FIFO. */
42200dbe28b3SPyun YongHyeon 	CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T), GMF_RST_SET);
42210dbe28b3SPyun YongHyeon 
42220dbe28b3SPyun YongHyeon 	/* Free Rx and Tx mbufs still in the queues. */
42230dbe28b3SPyun YongHyeon 	for (i = 0; i < MSK_RX_RING_CNT; i++) {
42240dbe28b3SPyun YongHyeon 		rxd = &sc_if->msk_cdata.msk_rxdesc[i];
42250dbe28b3SPyun YongHyeon 		if (rxd->rx_m != NULL) {
42260dbe28b3SPyun YongHyeon 			bus_dmamap_sync(sc_if->msk_cdata.msk_rx_tag,
42270dbe28b3SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
42280dbe28b3SPyun YongHyeon 			bus_dmamap_unload(sc_if->msk_cdata.msk_rx_tag,
42290dbe28b3SPyun YongHyeon 			    rxd->rx_dmamap);
42300dbe28b3SPyun YongHyeon 			m_freem(rxd->rx_m);
42310dbe28b3SPyun YongHyeon 			rxd->rx_m = NULL;
42320dbe28b3SPyun YongHyeon 		}
42330dbe28b3SPyun YongHyeon 	}
42340dbe28b3SPyun YongHyeon 	for (i = 0; i < MSK_JUMBO_RX_RING_CNT; i++) {
42350dbe28b3SPyun YongHyeon 		jrxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[i];
42360dbe28b3SPyun YongHyeon 		if (jrxd->rx_m != NULL) {
42370dbe28b3SPyun YongHyeon 			bus_dmamap_sync(sc_if->msk_cdata.msk_jumbo_rx_tag,
42380dbe28b3SPyun YongHyeon 			    jrxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
42390dbe28b3SPyun YongHyeon 			bus_dmamap_unload(sc_if->msk_cdata.msk_jumbo_rx_tag,
42400dbe28b3SPyun YongHyeon 			    jrxd->rx_dmamap);
42410dbe28b3SPyun YongHyeon 			m_freem(jrxd->rx_m);
42420dbe28b3SPyun YongHyeon 			jrxd->rx_m = NULL;
42430dbe28b3SPyun YongHyeon 		}
42440dbe28b3SPyun YongHyeon 	}
42450dbe28b3SPyun YongHyeon 	for (i = 0; i < MSK_TX_RING_CNT; i++) {
42460dbe28b3SPyun YongHyeon 		txd = &sc_if->msk_cdata.msk_txdesc[i];
42470dbe28b3SPyun YongHyeon 		if (txd->tx_m != NULL) {
42480dbe28b3SPyun YongHyeon 			bus_dmamap_sync(sc_if->msk_cdata.msk_tx_tag,
42490dbe28b3SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
42500dbe28b3SPyun YongHyeon 			bus_dmamap_unload(sc_if->msk_cdata.msk_tx_tag,
42510dbe28b3SPyun YongHyeon 			    txd->tx_dmamap);
42520dbe28b3SPyun YongHyeon 			m_freem(txd->tx_m);
42530dbe28b3SPyun YongHyeon 			txd->tx_m = NULL;
42540dbe28b3SPyun YongHyeon 		}
42550dbe28b3SPyun YongHyeon 	}
42560dbe28b3SPyun YongHyeon 
42570dbe28b3SPyun YongHyeon 	/*
42580dbe28b3SPyun YongHyeon 	 * Mark the interface down.
42590dbe28b3SPyun YongHyeon 	 */
42605ab8c4b8SJustin Hibbits 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
4261ab7df1e4SPyun YongHyeon 	sc_if->msk_flags &= ~MSK_FLAG_LINK;
42620dbe28b3SPyun YongHyeon }
42630dbe28b3SPyun YongHyeon 
42643a91ee71SPyun YongHyeon /*
42653a91ee71SPyun YongHyeon  * When GM_PAR_MIB_CLR bit of GM_PHY_ADDR is set, reading lower
42663a91ee71SPyun YongHyeon  * counter clears high 16 bits of the counter such that accessing
42673a91ee71SPyun YongHyeon  * lower 16 bits should be the last operation.
42683a91ee71SPyun YongHyeon  */
42693a91ee71SPyun YongHyeon #define	MSK_READ_MIB32(x, y)					\
4270ae70e883SJohn Baldwin 	((((uint32_t)GMAC_READ_2(sc, x, (y) + 4)) << 16) +	\
4271ae70e883SJohn Baldwin 	(uint32_t)GMAC_READ_2(sc, x, y))
42723a91ee71SPyun YongHyeon #define	MSK_READ_MIB64(x, y)					\
4273ae70e883SJohn Baldwin 	((((uint64_t)MSK_READ_MIB32(x, (y) + 8)) << 32) +	\
4274ae70e883SJohn Baldwin 	(uint64_t)MSK_READ_MIB32(x, y))
42753a91ee71SPyun YongHyeon 
42763a91ee71SPyun YongHyeon static void
msk_stats_clear(struct msk_if_softc * sc_if)42773a91ee71SPyun YongHyeon msk_stats_clear(struct msk_if_softc *sc_if)
42783a91ee71SPyun YongHyeon {
42793a91ee71SPyun YongHyeon 	struct msk_softc *sc;
42803a91ee71SPyun YongHyeon 	uint16_t gmac;
42813a91ee71SPyun YongHyeon 	int i;
42823a91ee71SPyun YongHyeon 
42833a91ee71SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
42843a91ee71SPyun YongHyeon 
42853a91ee71SPyun YongHyeon 	sc = sc_if->msk_softc;
42863a91ee71SPyun YongHyeon 	/* Set MIB Clear Counter Mode. */
42873a91ee71SPyun YongHyeon 	gmac = GMAC_READ_2(sc, sc_if->msk_port, GM_PHY_ADDR);
42883a91ee71SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_PHY_ADDR, gmac | GM_PAR_MIB_CLR);
42893a91ee71SPyun YongHyeon 	/* Read all MIB Counters with Clear Mode set. */
429040d7192bSPyun YongHyeon 	for (i = GM_RXF_UC_OK; i <= GM_TXE_FIFO_UR; i += sizeof(uint32_t))
4291ae70e883SJohn Baldwin 		(void)MSK_READ_MIB32(sc_if->msk_port, i);
42923a91ee71SPyun YongHyeon 	/* Clear MIB Clear Counter Mode. */
42933a91ee71SPyun YongHyeon 	gmac &= ~GM_PAR_MIB_CLR;
42943a91ee71SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_PHY_ADDR, gmac);
42953a91ee71SPyun YongHyeon }
42963a91ee71SPyun YongHyeon 
42973a91ee71SPyun YongHyeon static void
msk_stats_update(struct msk_if_softc * sc_if)42983a91ee71SPyun YongHyeon msk_stats_update(struct msk_if_softc *sc_if)
42993a91ee71SPyun YongHyeon {
43003a91ee71SPyun YongHyeon 	struct msk_softc *sc;
43015ab8c4b8SJustin Hibbits 	if_t ifp;
43023a91ee71SPyun YongHyeon 	struct msk_hw_stats *stats;
43033a91ee71SPyun YongHyeon 	uint16_t gmac;
43043a91ee71SPyun YongHyeon 
43053a91ee71SPyun YongHyeon 	MSK_IF_LOCK_ASSERT(sc_if);
43063a91ee71SPyun YongHyeon 
43073a91ee71SPyun YongHyeon 	ifp = sc_if->msk_ifp;
43085ab8c4b8SJustin Hibbits 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
43093a91ee71SPyun YongHyeon 		return;
43103a91ee71SPyun YongHyeon 	sc = sc_if->msk_softc;
43113a91ee71SPyun YongHyeon 	stats = &sc_if->msk_stats;
43123a91ee71SPyun YongHyeon 	/* Set MIB Clear Counter Mode. */
43133a91ee71SPyun YongHyeon 	gmac = GMAC_READ_2(sc, sc_if->msk_port, GM_PHY_ADDR);
43143a91ee71SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_PHY_ADDR, gmac | GM_PAR_MIB_CLR);
43153a91ee71SPyun YongHyeon 
43163a91ee71SPyun YongHyeon 	/* Rx stats. */
43173a91ee71SPyun YongHyeon 	stats->rx_ucast_frames +=
43183a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_UC_OK);
43193a91ee71SPyun YongHyeon 	stats->rx_bcast_frames +=
43203a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_BC_OK);
43213a91ee71SPyun YongHyeon 	stats->rx_pause_frames +=
43223a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_MPAUSE);
43233a91ee71SPyun YongHyeon 	stats->rx_mcast_frames +=
43243a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_MC_OK);
43253a91ee71SPyun YongHyeon 	stats->rx_crc_errs +=
43263a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_FCS_ERR);
43273a91ee71SPyun YongHyeon 	stats->rx_good_octets +=
43283a91ee71SPyun YongHyeon 	    MSK_READ_MIB64(sc_if->msk_port, GM_RXO_OK_LO);
43293a91ee71SPyun YongHyeon 	stats->rx_bad_octets +=
43303a91ee71SPyun YongHyeon 	    MSK_READ_MIB64(sc_if->msk_port, GM_RXO_ERR_LO);
43313a91ee71SPyun YongHyeon 	stats->rx_runts +=
43323a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_SHT);
43333a91ee71SPyun YongHyeon 	stats->rx_runt_errs +=
43343a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXE_FRAG);
43353a91ee71SPyun YongHyeon 	stats->rx_pkts_64 +=
43363a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_64B);
43373a91ee71SPyun YongHyeon 	stats->rx_pkts_65_127 +=
43383a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_127B);
43393a91ee71SPyun YongHyeon 	stats->rx_pkts_128_255 +=
43403a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_255B);
43413a91ee71SPyun YongHyeon 	stats->rx_pkts_256_511 +=
43423a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_511B);
43433a91ee71SPyun YongHyeon 	stats->rx_pkts_512_1023 +=
43443a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_1023B);
43453a91ee71SPyun YongHyeon 	stats->rx_pkts_1024_1518 +=
43463a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_1518B);
43473a91ee71SPyun YongHyeon 	stats->rx_pkts_1519_max +=
43483a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_MAX_SZ);
43493a91ee71SPyun YongHyeon 	stats->rx_pkts_too_long +=
43503a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_LNG_ERR);
43513a91ee71SPyun YongHyeon 	stats->rx_pkts_jabbers +=
43523a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXF_JAB_PKT);
43533a91ee71SPyun YongHyeon 	stats->rx_fifo_oflows +=
43543a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_RXE_FIFO_OV);
43553a91ee71SPyun YongHyeon 
43563a91ee71SPyun YongHyeon 	/* Tx stats. */
43573a91ee71SPyun YongHyeon 	stats->tx_ucast_frames +=
43583a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_UC_OK);
43593a91ee71SPyun YongHyeon 	stats->tx_bcast_frames +=
43603a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_BC_OK);
43613a91ee71SPyun YongHyeon 	stats->tx_pause_frames +=
43623a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_MPAUSE);
43633a91ee71SPyun YongHyeon 	stats->tx_mcast_frames +=
43643a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_MC_OK);
43653a91ee71SPyun YongHyeon 	stats->tx_octets +=
43663a91ee71SPyun YongHyeon 	    MSK_READ_MIB64(sc_if->msk_port, GM_TXO_OK_LO);
43673a91ee71SPyun YongHyeon 	stats->tx_pkts_64 +=
43683a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_64B);
43693a91ee71SPyun YongHyeon 	stats->tx_pkts_65_127 +=
43703a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_127B);
43713a91ee71SPyun YongHyeon 	stats->tx_pkts_128_255 +=
43723a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_255B);
43733a91ee71SPyun YongHyeon 	stats->tx_pkts_256_511 +=
43743a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_511B);
43753a91ee71SPyun YongHyeon 	stats->tx_pkts_512_1023 +=
43763a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_1023B);
43773a91ee71SPyun YongHyeon 	stats->tx_pkts_1024_1518 +=
43783a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_1518B);
43793a91ee71SPyun YongHyeon 	stats->tx_pkts_1519_max +=
43803a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_MAX_SZ);
43813a91ee71SPyun YongHyeon 	stats->tx_colls +=
43823a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_COL);
43833a91ee71SPyun YongHyeon 	stats->tx_late_colls +=
43843a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_LAT_COL);
43853a91ee71SPyun YongHyeon 	stats->tx_excess_colls +=
43863a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_ABO_COL);
43873a91ee71SPyun YongHyeon 	stats->tx_multi_colls +=
43883a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_MUL_COL);
43893a91ee71SPyun YongHyeon 	stats->tx_single_colls +=
43903a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXF_SNG_COL);
43913a91ee71SPyun YongHyeon 	stats->tx_underflows +=
43923a91ee71SPyun YongHyeon 	    MSK_READ_MIB32(sc_if->msk_port, GM_TXE_FIFO_UR);
43933a91ee71SPyun YongHyeon 	/* Clear MIB Clear Counter Mode. */
43943a91ee71SPyun YongHyeon 	gmac &= ~GM_PAR_MIB_CLR;
43953a91ee71SPyun YongHyeon 	GMAC_WRITE_2(sc, sc_if->msk_port, GM_PHY_ADDR, gmac);
43963a91ee71SPyun YongHyeon }
43973a91ee71SPyun YongHyeon 
43983a91ee71SPyun YongHyeon static int
msk_sysctl_stat32(SYSCTL_HANDLER_ARGS)43993a91ee71SPyun YongHyeon msk_sysctl_stat32(SYSCTL_HANDLER_ARGS)
44003a91ee71SPyun YongHyeon {
44013a91ee71SPyun YongHyeon 	struct msk_softc *sc;
44023a91ee71SPyun YongHyeon 	struct msk_if_softc *sc_if;
44033a91ee71SPyun YongHyeon 	uint32_t result, *stat;
44043a91ee71SPyun YongHyeon 	int off;
44053a91ee71SPyun YongHyeon 
44063a91ee71SPyun YongHyeon 	sc_if = (struct msk_if_softc *)arg1;
44073a91ee71SPyun YongHyeon 	sc = sc_if->msk_softc;
44083a91ee71SPyun YongHyeon 	off = arg2;
44093a91ee71SPyun YongHyeon 	stat = (uint32_t *)((uint8_t *)&sc_if->msk_stats + off);
44103a91ee71SPyun YongHyeon 
44113a91ee71SPyun YongHyeon 	MSK_IF_LOCK(sc_if);
44123a91ee71SPyun YongHyeon 	result = MSK_READ_MIB32(sc_if->msk_port, GM_MIB_CNT_BASE + off * 2);
44133a91ee71SPyun YongHyeon 	result += *stat;
44143a91ee71SPyun YongHyeon 	MSK_IF_UNLOCK(sc_if);
44153a91ee71SPyun YongHyeon 
44163a91ee71SPyun YongHyeon 	return (sysctl_handle_int(oidp, &result, 0, req));
44173a91ee71SPyun YongHyeon }
44183a91ee71SPyun YongHyeon 
44193a91ee71SPyun YongHyeon static int
msk_sysctl_stat64(SYSCTL_HANDLER_ARGS)44203a91ee71SPyun YongHyeon msk_sysctl_stat64(SYSCTL_HANDLER_ARGS)
44213a91ee71SPyun YongHyeon {
44223a91ee71SPyun YongHyeon 	struct msk_softc *sc;
44233a91ee71SPyun YongHyeon 	struct msk_if_softc *sc_if;
44243a91ee71SPyun YongHyeon 	uint64_t result, *stat;
44253a91ee71SPyun YongHyeon 	int off;
44263a91ee71SPyun YongHyeon 
44273a91ee71SPyun YongHyeon 	sc_if = (struct msk_if_softc *)arg1;
44283a91ee71SPyun YongHyeon 	sc = sc_if->msk_softc;
44293a91ee71SPyun YongHyeon 	off = arg2;
44303a91ee71SPyun YongHyeon 	stat = (uint64_t *)((uint8_t *)&sc_if->msk_stats + off);
44313a91ee71SPyun YongHyeon 
44323a91ee71SPyun YongHyeon 	MSK_IF_LOCK(sc_if);
44333a91ee71SPyun YongHyeon 	result = MSK_READ_MIB64(sc_if->msk_port, GM_MIB_CNT_BASE + off * 2);
44343a91ee71SPyun YongHyeon 	result += *stat;
44353a91ee71SPyun YongHyeon 	MSK_IF_UNLOCK(sc_if);
44363a91ee71SPyun YongHyeon 
4437cbc134adSMatthew D Fleming 	return (sysctl_handle_64(oidp, &result, 0, req));
44383a91ee71SPyun YongHyeon }
44393a91ee71SPyun YongHyeon 
44403a91ee71SPyun YongHyeon #undef MSK_READ_MIB32
44413a91ee71SPyun YongHyeon #undef MSK_READ_MIB64
44423a91ee71SPyun YongHyeon 
44433a91ee71SPyun YongHyeon #define MSK_SYSCTL_STAT32(sc, c, o, p, n, d) 				\
44447029da5cSPawel Biernacki 	SYSCTL_ADD_PROC(c, p, OID_AUTO, o,				\
44457029da5cSPawel Biernacki 	    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,	 	\
44463a91ee71SPyun YongHyeon 	    sc, offsetof(struct msk_hw_stats, n), msk_sysctl_stat32,	\
44473a91ee71SPyun YongHyeon 	    "IU", d)
44483a91ee71SPyun YongHyeon #define MSK_SYSCTL_STAT64(sc, c, o, p, n, d) 				\
44497029da5cSPawel Biernacki 	SYSCTL_ADD_PROC(c, p, OID_AUTO, o,				\
44507029da5cSPawel Biernacki 	    CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT,	 	\
44513a91ee71SPyun YongHyeon 	    sc, offsetof(struct msk_hw_stats, n), msk_sysctl_stat64,	\
4452cbc134adSMatthew D Fleming 	    "QU", d)
44533a91ee71SPyun YongHyeon 
44543a91ee71SPyun YongHyeon static void
msk_sysctl_node(struct msk_if_softc * sc_if)44553a91ee71SPyun YongHyeon msk_sysctl_node(struct msk_if_softc *sc_if)
44563a91ee71SPyun YongHyeon {
44573a91ee71SPyun YongHyeon 	struct sysctl_ctx_list *ctx;
44583a91ee71SPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
44593a91ee71SPyun YongHyeon 	struct sysctl_oid *tree;
44603a91ee71SPyun YongHyeon 
44613a91ee71SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc_if->msk_if_dev);
44623a91ee71SPyun YongHyeon 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc_if->msk_if_dev));
44633a91ee71SPyun YongHyeon 
44647029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats",
44657029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "MSK Statistics");
44669935c65aSPyun YongHyeon 	schild = SYSCTL_CHILDREN(tree);
44677029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx",
44687029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "MSK RX Statistics");
44693a91ee71SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
44703a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "ucast_frames",
44713a91ee71SPyun YongHyeon 	    child, rx_ucast_frames, "Good unicast frames");
44723a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "bcast_frames",
44733a91ee71SPyun YongHyeon 	    child, rx_bcast_frames, "Good broadcast frames");
44743a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "pause_frames",
44753a91ee71SPyun YongHyeon 	    child, rx_pause_frames, "Pause frames");
44763a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "mcast_frames",
44773a91ee71SPyun YongHyeon 	    child, rx_mcast_frames, "Multicast frames");
44783a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "crc_errs",
44793a91ee71SPyun YongHyeon 	    child, rx_crc_errs, "CRC errors");
44803a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT64(sc_if, ctx, "good_octets",
44813a91ee71SPyun YongHyeon 	    child, rx_good_octets, "Good octets");
44823a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT64(sc_if, ctx, "bad_octets",
44833a91ee71SPyun YongHyeon 	    child, rx_bad_octets, "Bad octets");
44843a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_64",
44853a91ee71SPyun YongHyeon 	    child, rx_pkts_64, "64 bytes frames");
44863a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_65_127",
44873a91ee71SPyun YongHyeon 	    child, rx_pkts_65_127, "65 to 127 bytes frames");
44883a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_128_255",
44893a91ee71SPyun YongHyeon 	    child, rx_pkts_128_255, "128 to 255 bytes frames");
44903a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_256_511",
44913a91ee71SPyun YongHyeon 	    child, rx_pkts_256_511, "256 to 511 bytes frames");
44923a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_512_1023",
44933a91ee71SPyun YongHyeon 	    child, rx_pkts_512_1023, "512 to 1023 bytes frames");
44943a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_1024_1518",
44953a91ee71SPyun YongHyeon 	    child, rx_pkts_1024_1518, "1024 to 1518 bytes frames");
44963a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_1519_max",
44973a91ee71SPyun YongHyeon 	    child, rx_pkts_1519_max, "1519 to max frames");
44983a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_too_long",
44993a91ee71SPyun YongHyeon 	    child, rx_pkts_too_long, "frames too long");
45003a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "jabbers",
45013a91ee71SPyun YongHyeon 	    child, rx_pkts_jabbers, "Jabber errors");
450279dd979aSPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "overflows",
45033a91ee71SPyun YongHyeon 	    child, rx_fifo_oflows, "FIFO overflows");
45043a91ee71SPyun YongHyeon 
45057029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx",
45067029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "MSK TX Statistics");
45073a91ee71SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
45083a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "ucast_frames",
45093a91ee71SPyun YongHyeon 	    child, tx_ucast_frames, "Unicast frames");
45103a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "bcast_frames",
45113a91ee71SPyun YongHyeon 	    child, tx_bcast_frames, "Broadcast frames");
45123a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "pause_frames",
45133a91ee71SPyun YongHyeon 	    child, tx_pause_frames, "Pause frames");
45143a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "mcast_frames",
45153a91ee71SPyun YongHyeon 	    child, tx_mcast_frames, "Multicast frames");
45163a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT64(sc_if, ctx, "octets",
45173a91ee71SPyun YongHyeon 	    child, tx_octets, "Octets");
45183a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_64",
45193a91ee71SPyun YongHyeon 	    child, tx_pkts_64, "64 bytes frames");
45203a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_65_127",
45213a91ee71SPyun YongHyeon 	    child, tx_pkts_65_127, "65 to 127 bytes frames");
45223a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_128_255",
45233a91ee71SPyun YongHyeon 	    child, tx_pkts_128_255, "128 to 255 bytes frames");
45243a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_256_511",
45253a91ee71SPyun YongHyeon 	    child, tx_pkts_256_511, "256 to 511 bytes frames");
45263a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_512_1023",
45273a91ee71SPyun YongHyeon 	    child, tx_pkts_512_1023, "512 to 1023 bytes frames");
45283a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_1024_1518",
45293a91ee71SPyun YongHyeon 	    child, tx_pkts_1024_1518, "1024 to 1518 bytes frames");
45303a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "frames_1519_max",
45313a91ee71SPyun YongHyeon 	    child, tx_pkts_1519_max, "1519 to max frames");
45323a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "colls",
45333a91ee71SPyun YongHyeon 	    child, tx_colls, "Collisions");
45343a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "late_colls",
45353a91ee71SPyun YongHyeon 	    child, tx_late_colls, "Late collisions");
45363a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "excess_colls",
45373a91ee71SPyun YongHyeon 	    child, tx_excess_colls, "Excessive collisions");
45383a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "multi_colls",
45393a91ee71SPyun YongHyeon 	    child, tx_multi_colls, "Multiple collisions");
45403a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "single_colls",
45413a91ee71SPyun YongHyeon 	    child, tx_single_colls, "Single collisions");
45423a91ee71SPyun YongHyeon 	MSK_SYSCTL_STAT32(sc_if, ctx, "underflows",
45433a91ee71SPyun YongHyeon 	    child, tx_underflows, "FIFO underflows");
45443a91ee71SPyun YongHyeon }
45453a91ee71SPyun YongHyeon 
45463a91ee71SPyun YongHyeon #undef MSK_SYSCTL_STAT32
45473a91ee71SPyun YongHyeon #undef MSK_SYSCTL_STAT64
45483a91ee71SPyun YongHyeon 
45490dbe28b3SPyun YongHyeon static int
sysctl_int_range(SYSCTL_HANDLER_ARGS,int low,int high)45500dbe28b3SPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
45510dbe28b3SPyun YongHyeon {
45520dbe28b3SPyun YongHyeon 	int error, value;
45530dbe28b3SPyun YongHyeon 
45540dbe28b3SPyun YongHyeon 	if (!arg1)
45550dbe28b3SPyun YongHyeon 		return (EINVAL);
45560dbe28b3SPyun YongHyeon 	value = *(int *)arg1;
45570dbe28b3SPyun YongHyeon 	error = sysctl_handle_int(oidp, &value, 0, req);
45580dbe28b3SPyun YongHyeon 	if (error || !req->newptr)
45590dbe28b3SPyun YongHyeon 		return (error);
45600dbe28b3SPyun YongHyeon 	if (value < low || value > high)
45610dbe28b3SPyun YongHyeon 		return (EINVAL);
45620dbe28b3SPyun YongHyeon 	*(int *)arg1 = value;
45630dbe28b3SPyun YongHyeon 
45640dbe28b3SPyun YongHyeon 	return (0);
45650dbe28b3SPyun YongHyeon }
45660dbe28b3SPyun YongHyeon 
45670dbe28b3SPyun YongHyeon static int
sysctl_hw_msk_proc_limit(SYSCTL_HANDLER_ARGS)45680dbe28b3SPyun YongHyeon sysctl_hw_msk_proc_limit(SYSCTL_HANDLER_ARGS)
45690dbe28b3SPyun YongHyeon {
45700dbe28b3SPyun YongHyeon 
45710dbe28b3SPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req, MSK_PROC_MIN,
45720dbe28b3SPyun YongHyeon 	    MSK_PROC_MAX));
45730dbe28b3SPyun YongHyeon }
4574