xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/amd/xgbe/xgbe-main.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * AMD 10Gb Ethernet driver
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This file is available to you under your choice of the following two
5*4882a593Smuzhiyun  * licenses:
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * License 1: GPLv2
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This file is free software; you may copy, redistribute and/or modify
12*4882a593Smuzhiyun  * it under the terms of the GNU General Public License as published by
13*4882a593Smuzhiyun  * the Free Software Foundation, either version 2 of the License, or (at
14*4882a593Smuzhiyun  * your option) any later version.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * This file is distributed in the hope that it will be useful, but
17*4882a593Smuzhiyun  * WITHOUT ANY WARRANTY; without even the implied warranty of
18*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19*4882a593Smuzhiyun  * General Public License for more details.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
22*4882a593Smuzhiyun  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * This file incorporates work covered by the following copyright and
25*4882a593Smuzhiyun  * permission notice:
26*4882a593Smuzhiyun  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
27*4882a593Smuzhiyun  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
28*4882a593Smuzhiyun  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
29*4882a593Smuzhiyun  *     and you.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  *     The Software IS NOT an item of Licensed Software or Licensed Product
32*4882a593Smuzhiyun  *     under any End User Software License Agreement or Agreement for Licensed
33*4882a593Smuzhiyun  *     Product with Synopsys or any supplement thereto.  Permission is hereby
34*4882a593Smuzhiyun  *     granted, free of charge, to any person obtaining a copy of this software
35*4882a593Smuzhiyun  *     annotated with this license and the Software, to deal in the Software
36*4882a593Smuzhiyun  *     without restriction, including without limitation the rights to use,
37*4882a593Smuzhiyun  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38*4882a593Smuzhiyun  *     of the Software, and to permit persons to whom the Software is furnished
39*4882a593Smuzhiyun  *     to do so, subject to the following conditions:
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  *     The above copyright notice and this permission notice shall be included
42*4882a593Smuzhiyun  *     in all copies or substantial portions of the Software.
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
45*4882a593Smuzhiyun  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46*4882a593Smuzhiyun  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47*4882a593Smuzhiyun  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
48*4882a593Smuzhiyun  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49*4882a593Smuzhiyun  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50*4882a593Smuzhiyun  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51*4882a593Smuzhiyun  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52*4882a593Smuzhiyun  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53*4882a593Smuzhiyun  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54*4882a593Smuzhiyun  *     THE POSSIBILITY OF SUCH DAMAGE.
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * License 2: Modified BSD
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
60*4882a593Smuzhiyun  * All rights reserved.
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
63*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions are met:
64*4882a593Smuzhiyun  *     * Redistributions of source code must retain the above copyright
65*4882a593Smuzhiyun  *       notice, this list of conditions and the following disclaimer.
66*4882a593Smuzhiyun  *     * Redistributions in binary form must reproduce the above copyright
67*4882a593Smuzhiyun  *       notice, this list of conditions and the following disclaimer in the
68*4882a593Smuzhiyun  *       documentation and/or other materials provided with the distribution.
69*4882a593Smuzhiyun  *     * Neither the name of Advanced Micro Devices, Inc. nor the
70*4882a593Smuzhiyun  *       names of its contributors may be used to endorse or promote products
71*4882a593Smuzhiyun  *       derived from this software without specific prior written permission.
72*4882a593Smuzhiyun  *
73*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74*4882a593Smuzhiyun  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75*4882a593Smuzhiyun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76*4882a593Smuzhiyun  * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
77*4882a593Smuzhiyun  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78*4882a593Smuzhiyun  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79*4882a593Smuzhiyun  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
80*4882a593Smuzhiyun  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82*4882a593Smuzhiyun  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83*4882a593Smuzhiyun  *
84*4882a593Smuzhiyun  * This file incorporates work covered by the following copyright and
85*4882a593Smuzhiyun  * permission notice:
86*4882a593Smuzhiyun  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
87*4882a593Smuzhiyun  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
88*4882a593Smuzhiyun  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
89*4882a593Smuzhiyun  *     and you.
90*4882a593Smuzhiyun  *
91*4882a593Smuzhiyun  *     The Software IS NOT an item of Licensed Software or Licensed Product
92*4882a593Smuzhiyun  *     under any End User Software License Agreement or Agreement for Licensed
93*4882a593Smuzhiyun  *     Product with Synopsys or any supplement thereto.  Permission is hereby
94*4882a593Smuzhiyun  *     granted, free of charge, to any person obtaining a copy of this software
95*4882a593Smuzhiyun  *     annotated with this license and the Software, to deal in the Software
96*4882a593Smuzhiyun  *     without restriction, including without limitation the rights to use,
97*4882a593Smuzhiyun  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98*4882a593Smuzhiyun  *     of the Software, and to permit persons to whom the Software is furnished
99*4882a593Smuzhiyun  *     to do so, subject to the following conditions:
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  *     The above copyright notice and this permission notice shall be included
102*4882a593Smuzhiyun  *     in all copies or substantial portions of the Software.
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105*4882a593Smuzhiyun  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106*4882a593Smuzhiyun  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107*4882a593Smuzhiyun  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108*4882a593Smuzhiyun  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109*4882a593Smuzhiyun  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110*4882a593Smuzhiyun  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111*4882a593Smuzhiyun  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112*4882a593Smuzhiyun  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113*4882a593Smuzhiyun  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114*4882a593Smuzhiyun  *     THE POSSIBILITY OF SUCH DAMAGE.
115*4882a593Smuzhiyun  */
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun #include <linux/module.h>
118*4882a593Smuzhiyun #include <linux/device.h>
119*4882a593Smuzhiyun #include <linux/spinlock.h>
120*4882a593Smuzhiyun #include <linux/netdevice.h>
121*4882a593Smuzhiyun #include <linux/etherdevice.h>
122*4882a593Smuzhiyun #include <linux/io.h>
123*4882a593Smuzhiyun #include <linux/notifier.h>
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun #include "xgbe.h"
126*4882a593Smuzhiyun #include "xgbe-common.h"
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
129*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
130*4882a593Smuzhiyun MODULE_DESCRIPTION(XGBE_DRV_DESC);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun static int debug = -1;
133*4882a593Smuzhiyun module_param(debug, int, 0644);
134*4882a593Smuzhiyun MODULE_PARM_DESC(debug, " Network interface message level setting");
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun static const u32 default_msg_level = (NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
137*4882a593Smuzhiyun 				      NETIF_MSG_IFUP);
138*4882a593Smuzhiyun 
xgbe_default_config(struct xgbe_prv_data * pdata)139*4882a593Smuzhiyun static void xgbe_default_config(struct xgbe_prv_data *pdata)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun 	DBGPR("-->xgbe_default_config\n");
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	pdata->blen = DMA_SBMR_BLEN_64;
144*4882a593Smuzhiyun 	pdata->pbl = DMA_PBL_128;
145*4882a593Smuzhiyun 	pdata->aal = 1;
146*4882a593Smuzhiyun 	pdata->rd_osr_limit = 8;
147*4882a593Smuzhiyun 	pdata->wr_osr_limit = 8;
148*4882a593Smuzhiyun 	pdata->tx_sf_mode = MTL_TSF_ENABLE;
149*4882a593Smuzhiyun 	pdata->tx_threshold = MTL_TX_THRESHOLD_64;
150*4882a593Smuzhiyun 	pdata->tx_osp_mode = DMA_OSP_ENABLE;
151*4882a593Smuzhiyun 	pdata->rx_sf_mode = MTL_RSF_DISABLE;
152*4882a593Smuzhiyun 	pdata->rx_threshold = MTL_RX_THRESHOLD_64;
153*4882a593Smuzhiyun 	pdata->pause_autoneg = 1;
154*4882a593Smuzhiyun 	pdata->tx_pause = 1;
155*4882a593Smuzhiyun 	pdata->rx_pause = 1;
156*4882a593Smuzhiyun 	pdata->phy_speed = SPEED_UNKNOWN;
157*4882a593Smuzhiyun 	pdata->power_down = 0;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	DBGPR("<--xgbe_default_config\n");
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
xgbe_init_all_fptrs(struct xgbe_prv_data * pdata)162*4882a593Smuzhiyun static void xgbe_init_all_fptrs(struct xgbe_prv_data *pdata)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	xgbe_init_function_ptrs_dev(&pdata->hw_if);
165*4882a593Smuzhiyun 	xgbe_init_function_ptrs_phy(&pdata->phy_if);
166*4882a593Smuzhiyun 	xgbe_init_function_ptrs_i2c(&pdata->i2c_if);
167*4882a593Smuzhiyun 	xgbe_init_function_ptrs_desc(&pdata->desc_if);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	pdata->vdata->init_function_ptrs_phy_impl(&pdata->phy_if);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
xgbe_alloc_pdata(struct device * dev)172*4882a593Smuzhiyun struct xgbe_prv_data *xgbe_alloc_pdata(struct device *dev)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata;
175*4882a593Smuzhiyun 	struct net_device *netdev;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	netdev = alloc_etherdev_mq(sizeof(struct xgbe_prv_data),
178*4882a593Smuzhiyun 				   XGBE_MAX_DMA_CHANNELS);
179*4882a593Smuzhiyun 	if (!netdev) {
180*4882a593Smuzhiyun 		dev_err(dev, "alloc_etherdev_mq failed\n");
181*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
182*4882a593Smuzhiyun 	}
183*4882a593Smuzhiyun 	SET_NETDEV_DEV(netdev, dev);
184*4882a593Smuzhiyun 	pdata = netdev_priv(netdev);
185*4882a593Smuzhiyun 	pdata->netdev = netdev;
186*4882a593Smuzhiyun 	pdata->dev = dev;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	spin_lock_init(&pdata->lock);
189*4882a593Smuzhiyun 	spin_lock_init(&pdata->xpcs_lock);
190*4882a593Smuzhiyun 	mutex_init(&pdata->rss_mutex);
191*4882a593Smuzhiyun 	spin_lock_init(&pdata->tstamp_lock);
192*4882a593Smuzhiyun 	mutex_init(&pdata->i2c_mutex);
193*4882a593Smuzhiyun 	init_completion(&pdata->i2c_complete);
194*4882a593Smuzhiyun 	init_completion(&pdata->mdio_complete);
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	pdata->msg_enable = netif_msg_init(debug, default_msg_level);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	set_bit(XGBE_DOWN, &pdata->dev_state);
199*4882a593Smuzhiyun 	set_bit(XGBE_STOPPED, &pdata->dev_state);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	return pdata;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
xgbe_free_pdata(struct xgbe_prv_data * pdata)204*4882a593Smuzhiyun void xgbe_free_pdata(struct xgbe_prv_data *pdata)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	struct net_device *netdev = pdata->netdev;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	free_netdev(netdev);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun 
xgbe_set_counts(struct xgbe_prv_data * pdata)211*4882a593Smuzhiyun void xgbe_set_counts(struct xgbe_prv_data *pdata)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	/* Set all the function pointers */
214*4882a593Smuzhiyun 	xgbe_init_all_fptrs(pdata);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	/* Populate the hardware features */
217*4882a593Smuzhiyun 	xgbe_get_all_hw_features(pdata);
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	/* Set default max values if not provided */
220*4882a593Smuzhiyun 	if (!pdata->tx_max_channel_count)
221*4882a593Smuzhiyun 		pdata->tx_max_channel_count = pdata->hw_feat.tx_ch_cnt;
222*4882a593Smuzhiyun 	if (!pdata->rx_max_channel_count)
223*4882a593Smuzhiyun 		pdata->rx_max_channel_count = pdata->hw_feat.rx_ch_cnt;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	if (!pdata->tx_max_q_count)
226*4882a593Smuzhiyun 		pdata->tx_max_q_count = pdata->hw_feat.tx_q_cnt;
227*4882a593Smuzhiyun 	if (!pdata->rx_max_q_count)
228*4882a593Smuzhiyun 		pdata->rx_max_q_count = pdata->hw_feat.rx_q_cnt;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	/* Calculate the number of Tx and Rx rings to be created
231*4882a593Smuzhiyun 	 *  -Tx (DMA) Channels map 1-to-1 to Tx Queues so set
232*4882a593Smuzhiyun 	 *   the number of Tx queues to the number of Tx channels
233*4882a593Smuzhiyun 	 *   enabled
234*4882a593Smuzhiyun 	 *  -Rx (DMA) Channels do not map 1-to-1 so use the actual
235*4882a593Smuzhiyun 	 *   number of Rx queues or maximum allowed
236*4882a593Smuzhiyun 	 */
237*4882a593Smuzhiyun 	pdata->tx_ring_count = min_t(unsigned int, num_online_cpus(),
238*4882a593Smuzhiyun 				     pdata->hw_feat.tx_ch_cnt);
239*4882a593Smuzhiyun 	pdata->tx_ring_count = min_t(unsigned int, pdata->tx_ring_count,
240*4882a593Smuzhiyun 				     pdata->tx_max_channel_count);
241*4882a593Smuzhiyun 	pdata->tx_ring_count = min_t(unsigned int, pdata->tx_ring_count,
242*4882a593Smuzhiyun 				     pdata->tx_max_q_count);
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	pdata->tx_q_count = pdata->tx_ring_count;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	pdata->rx_ring_count = min_t(unsigned int, num_online_cpus(),
247*4882a593Smuzhiyun 				     pdata->hw_feat.rx_ch_cnt);
248*4882a593Smuzhiyun 	pdata->rx_ring_count = min_t(unsigned int, pdata->rx_ring_count,
249*4882a593Smuzhiyun 				     pdata->rx_max_channel_count);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	pdata->rx_q_count = min_t(unsigned int, pdata->hw_feat.rx_q_cnt,
252*4882a593Smuzhiyun 				  pdata->rx_max_q_count);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	if (netif_msg_probe(pdata)) {
255*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "TX/RX DMA channel count = %u/%u\n",
256*4882a593Smuzhiyun 			pdata->tx_ring_count, pdata->rx_ring_count);
257*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "TX/RX hardware queue count = %u/%u\n",
258*4882a593Smuzhiyun 			pdata->tx_q_count, pdata->rx_q_count);
259*4882a593Smuzhiyun 	}
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
xgbe_config_netdev(struct xgbe_prv_data * pdata)262*4882a593Smuzhiyun int xgbe_config_netdev(struct xgbe_prv_data *pdata)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	struct net_device *netdev = pdata->netdev;
265*4882a593Smuzhiyun 	struct device *dev = pdata->dev;
266*4882a593Smuzhiyun 	int ret;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	netdev->irq = pdata->dev_irq;
269*4882a593Smuzhiyun 	netdev->base_addr = (unsigned long)pdata->xgmac_regs;
270*4882a593Smuzhiyun 	memcpy(netdev->dev_addr, pdata->mac_addr, netdev->addr_len);
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	/* Initialize ECC timestamps */
273*4882a593Smuzhiyun 	pdata->tx_sec_period = jiffies;
274*4882a593Smuzhiyun 	pdata->tx_ded_period = jiffies;
275*4882a593Smuzhiyun 	pdata->rx_sec_period = jiffies;
276*4882a593Smuzhiyun 	pdata->rx_ded_period = jiffies;
277*4882a593Smuzhiyun 	pdata->desc_sec_period = jiffies;
278*4882a593Smuzhiyun 	pdata->desc_ded_period = jiffies;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	/* Issue software reset to device */
281*4882a593Smuzhiyun 	ret = pdata->hw_if.exit(pdata);
282*4882a593Smuzhiyun 	if (ret) {
283*4882a593Smuzhiyun 		dev_err(dev, "software reset failed\n");
284*4882a593Smuzhiyun 		return ret;
285*4882a593Smuzhiyun 	}
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	/* Set default configuration data */
288*4882a593Smuzhiyun 	xgbe_default_config(pdata);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	/* Set the DMA mask */
291*4882a593Smuzhiyun 	ret = dma_set_mask_and_coherent(dev,
292*4882a593Smuzhiyun 					DMA_BIT_MASK(pdata->hw_feat.dma_width));
293*4882a593Smuzhiyun 	if (ret) {
294*4882a593Smuzhiyun 		dev_err(dev, "dma_set_mask_and_coherent failed\n");
295*4882a593Smuzhiyun 		return ret;
296*4882a593Smuzhiyun 	}
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	/* Set default max values if not provided */
299*4882a593Smuzhiyun 	if (!pdata->tx_max_fifo_size)
300*4882a593Smuzhiyun 		pdata->tx_max_fifo_size = pdata->hw_feat.tx_fifo_size;
301*4882a593Smuzhiyun 	if (!pdata->rx_max_fifo_size)
302*4882a593Smuzhiyun 		pdata->rx_max_fifo_size = pdata->hw_feat.rx_fifo_size;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	/* Set and validate the number of descriptors for a ring */
305*4882a593Smuzhiyun 	BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_TX_DESC_CNT);
306*4882a593Smuzhiyun 	pdata->tx_desc_count = XGBE_TX_DESC_CNT;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_RX_DESC_CNT);
309*4882a593Smuzhiyun 	pdata->rx_desc_count = XGBE_RX_DESC_CNT;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	/* Adjust the number of queues based on interrupts assigned */
312*4882a593Smuzhiyun 	if (pdata->channel_irq_count) {
313*4882a593Smuzhiyun 		pdata->tx_ring_count = min_t(unsigned int, pdata->tx_ring_count,
314*4882a593Smuzhiyun 					     pdata->channel_irq_count);
315*4882a593Smuzhiyun 		pdata->rx_ring_count = min_t(unsigned int, pdata->rx_ring_count,
316*4882a593Smuzhiyun 					     pdata->channel_irq_count);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 		if (netif_msg_probe(pdata))
319*4882a593Smuzhiyun 			dev_dbg(pdata->dev,
320*4882a593Smuzhiyun 				"adjusted TX/RX DMA channel count = %u/%u\n",
321*4882a593Smuzhiyun 				pdata->tx_ring_count, pdata->rx_ring_count);
322*4882a593Smuzhiyun 	}
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	/* Initialize RSS hash key */
325*4882a593Smuzhiyun 	netdev_rss_key_fill(pdata->rss_key, sizeof(pdata->rss_key));
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, IP2TE, 1);
328*4882a593Smuzhiyun 	XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, TCP4TE, 1);
329*4882a593Smuzhiyun 	XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, UDP4TE, 1);
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	/* Call MDIO/PHY initialization routine */
332*4882a593Smuzhiyun 	pdata->debugfs_an_cdr_workaround = pdata->vdata->an_cdr_workaround;
333*4882a593Smuzhiyun 	ret = pdata->phy_if.phy_init(pdata);
334*4882a593Smuzhiyun 	if (ret)
335*4882a593Smuzhiyun 		return ret;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	/* Set device operations */
338*4882a593Smuzhiyun 	netdev->netdev_ops = xgbe_get_netdev_ops();
339*4882a593Smuzhiyun 	netdev->ethtool_ops = xgbe_get_ethtool_ops();
340*4882a593Smuzhiyun #ifdef CONFIG_AMD_XGBE_DCB
341*4882a593Smuzhiyun 	netdev->dcbnl_ops = xgbe_get_dcbnl_ops();
342*4882a593Smuzhiyun #endif
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	/* Set device features */
345*4882a593Smuzhiyun 	netdev->hw_features = NETIF_F_SG |
346*4882a593Smuzhiyun 			      NETIF_F_IP_CSUM |
347*4882a593Smuzhiyun 			      NETIF_F_IPV6_CSUM |
348*4882a593Smuzhiyun 			      NETIF_F_RXCSUM |
349*4882a593Smuzhiyun 			      NETIF_F_TSO |
350*4882a593Smuzhiyun 			      NETIF_F_TSO6 |
351*4882a593Smuzhiyun 			      NETIF_F_GRO |
352*4882a593Smuzhiyun 			      NETIF_F_HW_VLAN_CTAG_RX |
353*4882a593Smuzhiyun 			      NETIF_F_HW_VLAN_CTAG_TX |
354*4882a593Smuzhiyun 			      NETIF_F_HW_VLAN_CTAG_FILTER;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	if (pdata->hw_feat.rss)
357*4882a593Smuzhiyun 		netdev->hw_features |= NETIF_F_RXHASH;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	if (pdata->hw_feat.vxn) {
360*4882a593Smuzhiyun 		netdev->hw_enc_features = NETIF_F_SG |
361*4882a593Smuzhiyun 					  NETIF_F_IP_CSUM |
362*4882a593Smuzhiyun 					  NETIF_F_IPV6_CSUM |
363*4882a593Smuzhiyun 					  NETIF_F_RXCSUM |
364*4882a593Smuzhiyun 					  NETIF_F_TSO |
365*4882a593Smuzhiyun 					  NETIF_F_TSO6 |
366*4882a593Smuzhiyun 					  NETIF_F_GRO |
367*4882a593Smuzhiyun 					  NETIF_F_GSO_UDP_TUNNEL |
368*4882a593Smuzhiyun 					  NETIF_F_GSO_UDP_TUNNEL_CSUM;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 		netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
371*4882a593Smuzhiyun 				       NETIF_F_GSO_UDP_TUNNEL_CSUM;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 		netdev->udp_tunnel_nic_info = xgbe_get_udp_tunnel_info();
374*4882a593Smuzhiyun 	}
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	netdev->vlan_features |= NETIF_F_SG |
377*4882a593Smuzhiyun 				 NETIF_F_IP_CSUM |
378*4882a593Smuzhiyun 				 NETIF_F_IPV6_CSUM |
379*4882a593Smuzhiyun 				 NETIF_F_TSO |
380*4882a593Smuzhiyun 				 NETIF_F_TSO6;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	netdev->features |= netdev->hw_features;
383*4882a593Smuzhiyun 	pdata->netdev_features = netdev->features;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	netdev->priv_flags |= IFF_UNICAST_FLT;
386*4882a593Smuzhiyun 	netdev->min_mtu = 0;
387*4882a593Smuzhiyun 	netdev->max_mtu = XGMAC_JUMBO_PACKET_MTU;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	/* Use default watchdog timeout */
390*4882a593Smuzhiyun 	netdev->watchdog_timeo = 0;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	xgbe_init_rx_coalesce(pdata);
393*4882a593Smuzhiyun 	xgbe_init_tx_coalesce(pdata);
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	netif_carrier_off(netdev);
396*4882a593Smuzhiyun 	ret = register_netdev(netdev);
397*4882a593Smuzhiyun 	if (ret) {
398*4882a593Smuzhiyun 		dev_err(dev, "net device registration failed\n");
399*4882a593Smuzhiyun 		return ret;
400*4882a593Smuzhiyun 	}
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	if (IS_REACHABLE(CONFIG_PTP_1588_CLOCK))
403*4882a593Smuzhiyun 		xgbe_ptp_register(pdata);
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	xgbe_debugfs_init(pdata);
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	netif_dbg(pdata, drv, pdata->netdev, "%u Tx software queues\n",
408*4882a593Smuzhiyun 		  pdata->tx_ring_count);
409*4882a593Smuzhiyun 	netif_dbg(pdata, drv, pdata->netdev, "%u Rx software queues\n",
410*4882a593Smuzhiyun 		  pdata->rx_ring_count);
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	return 0;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
xgbe_deconfig_netdev(struct xgbe_prv_data * pdata)415*4882a593Smuzhiyun void xgbe_deconfig_netdev(struct xgbe_prv_data *pdata)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	struct net_device *netdev = pdata->netdev;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	xgbe_debugfs_exit(pdata);
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	if (IS_REACHABLE(CONFIG_PTP_1588_CLOCK))
422*4882a593Smuzhiyun 		xgbe_ptp_unregister(pdata);
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	unregister_netdev(netdev);
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	pdata->phy_if.phy_exit(pdata);
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun 
xgbe_netdev_event(struct notifier_block * nb,unsigned long event,void * data)429*4882a593Smuzhiyun static int xgbe_netdev_event(struct notifier_block *nb, unsigned long event,
430*4882a593Smuzhiyun 			     void *data)
431*4882a593Smuzhiyun {
432*4882a593Smuzhiyun 	struct net_device *netdev = netdev_notifier_info_to_dev(data);
433*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	if (netdev->netdev_ops != xgbe_get_netdev_ops())
436*4882a593Smuzhiyun 		goto out;
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	switch (event) {
439*4882a593Smuzhiyun 	case NETDEV_CHANGENAME:
440*4882a593Smuzhiyun 		xgbe_debugfs_rename(pdata);
441*4882a593Smuzhiyun 		break;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	default:
444*4882a593Smuzhiyun 		break;
445*4882a593Smuzhiyun 	}
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun out:
448*4882a593Smuzhiyun 	return NOTIFY_DONE;
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun static struct notifier_block xgbe_netdev_notifier = {
452*4882a593Smuzhiyun 	.notifier_call = xgbe_netdev_event,
453*4882a593Smuzhiyun };
454*4882a593Smuzhiyun 
xgbe_mod_init(void)455*4882a593Smuzhiyun static int __init xgbe_mod_init(void)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun 	int ret;
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	ret = register_netdevice_notifier(&xgbe_netdev_notifier);
460*4882a593Smuzhiyun 	if (ret)
461*4882a593Smuzhiyun 		return ret;
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	ret = xgbe_platform_init();
464*4882a593Smuzhiyun 	if (ret)
465*4882a593Smuzhiyun 		goto err_platform_init;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	ret = xgbe_pci_init();
468*4882a593Smuzhiyun 	if (ret)
469*4882a593Smuzhiyun 		goto err_pci_init;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	return 0;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun err_pci_init:
474*4882a593Smuzhiyun 	xgbe_platform_exit();
475*4882a593Smuzhiyun err_platform_init:
476*4882a593Smuzhiyun 	unregister_netdevice_notifier(&xgbe_netdev_notifier);
477*4882a593Smuzhiyun 	return ret;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun 
xgbe_mod_exit(void)480*4882a593Smuzhiyun static void __exit xgbe_mod_exit(void)
481*4882a593Smuzhiyun {
482*4882a593Smuzhiyun 	xgbe_pci_exit();
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	xgbe_platform_exit();
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	unregister_netdevice_notifier(&xgbe_netdev_notifier);
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun module_init(xgbe_mod_init);
490*4882a593Smuzhiyun module_exit(xgbe_mod_exit);
491