162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * AMD 10Gb Ethernet driver
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * This file is available to you under your choice of the following two
562306a36Sopenharmony_ci * licenses:
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * License 1: GPLv2
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * This file is free software; you may copy, redistribute and/or modify
1262306a36Sopenharmony_ci * it under the terms of the GNU General Public License as published by
1362306a36Sopenharmony_ci * the Free Software Foundation, either version 2 of the License, or (at
1462306a36Sopenharmony_ci * your option) any later version.
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * This file is distributed in the hope that it will be useful, but
1762306a36Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
1862306a36Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1962306a36Sopenharmony_ci * General Public License for more details.
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * You should have received a copy of the GNU General Public License
2262306a36Sopenharmony_ci * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * This file incorporates work covered by the following copyright and
2562306a36Sopenharmony_ci * permission notice:
2662306a36Sopenharmony_ci *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
2762306a36Sopenharmony_ci *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
2862306a36Sopenharmony_ci *     Inc. unless otherwise expressly agreed to in writing between Synopsys
2962306a36Sopenharmony_ci *     and you.
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci *     The Software IS NOT an item of Licensed Software or Licensed Product
3262306a36Sopenharmony_ci *     under any End User Software License Agreement or Agreement for Licensed
3362306a36Sopenharmony_ci *     Product with Synopsys or any supplement thereto.  Permission is hereby
3462306a36Sopenharmony_ci *     granted, free of charge, to any person obtaining a copy of this software
3562306a36Sopenharmony_ci *     annotated with this license and the Software, to deal in the Software
3662306a36Sopenharmony_ci *     without restriction, including without limitation the rights to use,
3762306a36Sopenharmony_ci *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
3862306a36Sopenharmony_ci *     of the Software, and to permit persons to whom the Software is furnished
3962306a36Sopenharmony_ci *     to do so, subject to the following conditions:
4062306a36Sopenharmony_ci *
4162306a36Sopenharmony_ci *     The above copyright notice and this permission notice shall be included
4262306a36Sopenharmony_ci *     in all copies or substantial portions of the Software.
4362306a36Sopenharmony_ci *
4462306a36Sopenharmony_ci *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
4562306a36Sopenharmony_ci *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
4662306a36Sopenharmony_ci *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
4762306a36Sopenharmony_ci *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
4862306a36Sopenharmony_ci *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4962306a36Sopenharmony_ci *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5062306a36Sopenharmony_ci *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5162306a36Sopenharmony_ci *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5262306a36Sopenharmony_ci *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5362306a36Sopenharmony_ci *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
5462306a36Sopenharmony_ci *     THE POSSIBILITY OF SUCH DAMAGE.
5562306a36Sopenharmony_ci *
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci * License 2: Modified BSD
5862306a36Sopenharmony_ci *
5962306a36Sopenharmony_ci * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
6062306a36Sopenharmony_ci * All rights reserved.
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
6362306a36Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
6462306a36Sopenharmony_ci *     * Redistributions of source code must retain the above copyright
6562306a36Sopenharmony_ci *       notice, this list of conditions and the following disclaimer.
6662306a36Sopenharmony_ci *     * Redistributions in binary form must reproduce the above copyright
6762306a36Sopenharmony_ci *       notice, this list of conditions and the following disclaimer in the
6862306a36Sopenharmony_ci *       documentation and/or other materials provided with the distribution.
6962306a36Sopenharmony_ci *     * Neither the name of Advanced Micro Devices, Inc. nor the
7062306a36Sopenharmony_ci *       names of its contributors may be used to endorse or promote products
7162306a36Sopenharmony_ci *       derived from this software without specific prior written permission.
7262306a36Sopenharmony_ci *
7362306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
7462306a36Sopenharmony_ci * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7562306a36Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7662306a36Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
7762306a36Sopenharmony_ci * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
7862306a36Sopenharmony_ci * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
7962306a36Sopenharmony_ci * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
8062306a36Sopenharmony_ci * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8162306a36Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
8262306a36Sopenharmony_ci * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8362306a36Sopenharmony_ci *
8462306a36Sopenharmony_ci * This file incorporates work covered by the following copyright and
8562306a36Sopenharmony_ci * permission notice:
8662306a36Sopenharmony_ci *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
8762306a36Sopenharmony_ci *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
8862306a36Sopenharmony_ci *     Inc. unless otherwise expressly agreed to in writing between Synopsys
8962306a36Sopenharmony_ci *     and you.
9062306a36Sopenharmony_ci *
9162306a36Sopenharmony_ci *     The Software IS NOT an item of Licensed Software or Licensed Product
9262306a36Sopenharmony_ci *     under any End User Software License Agreement or Agreement for Licensed
9362306a36Sopenharmony_ci *     Product with Synopsys or any supplement thereto.  Permission is hereby
9462306a36Sopenharmony_ci *     granted, free of charge, to any person obtaining a copy of this software
9562306a36Sopenharmony_ci *     annotated with this license and the Software, to deal in the Software
9662306a36Sopenharmony_ci *     without restriction, including without limitation the rights to use,
9762306a36Sopenharmony_ci *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9862306a36Sopenharmony_ci *     of the Software, and to permit persons to whom the Software is furnished
9962306a36Sopenharmony_ci *     to do so, subject to the following conditions:
10062306a36Sopenharmony_ci *
10162306a36Sopenharmony_ci *     The above copyright notice and this permission notice shall be included
10262306a36Sopenharmony_ci *     in all copies or substantial portions of the Software.
10362306a36Sopenharmony_ci *
10462306a36Sopenharmony_ci *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10562306a36Sopenharmony_ci *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
10662306a36Sopenharmony_ci *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
10762306a36Sopenharmony_ci *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
10862306a36Sopenharmony_ci *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
10962306a36Sopenharmony_ci *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
11062306a36Sopenharmony_ci *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
11162306a36Sopenharmony_ci *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
11262306a36Sopenharmony_ci *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
11362306a36Sopenharmony_ci *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
11462306a36Sopenharmony_ci *     THE POSSIBILITY OF SUCH DAMAGE.
11562306a36Sopenharmony_ci */
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci#include <linux/module.h>
11862306a36Sopenharmony_ci#include <linux/device.h>
11962306a36Sopenharmony_ci#include <linux/spinlock.h>
12062306a36Sopenharmony_ci#include <linux/netdevice.h>
12162306a36Sopenharmony_ci#include <linux/etherdevice.h>
12262306a36Sopenharmony_ci#include <linux/io.h>
12362306a36Sopenharmony_ci#include <linux/notifier.h>
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci#include "xgbe.h"
12662306a36Sopenharmony_ci#include "xgbe-common.h"
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciMODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
12962306a36Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
13062306a36Sopenharmony_ciMODULE_DESCRIPTION(XGBE_DRV_DESC);
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_cistatic int debug = -1;
13362306a36Sopenharmony_cimodule_param(debug, int, 0644);
13462306a36Sopenharmony_ciMODULE_PARM_DESC(debug, " Network interface message level setting");
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_cistatic const u32 default_msg_level = (NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
13762306a36Sopenharmony_ci				      NETIF_MSG_IFUP);
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_cistatic void xgbe_default_config(struct xgbe_prv_data *pdata)
14062306a36Sopenharmony_ci{
14162306a36Sopenharmony_ci	DBGPR("-->xgbe_default_config\n");
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	pdata->blen = DMA_SBMR_BLEN_64;
14462306a36Sopenharmony_ci	pdata->pbl = DMA_PBL_128;
14562306a36Sopenharmony_ci	pdata->aal = 1;
14662306a36Sopenharmony_ci	pdata->rd_osr_limit = 8;
14762306a36Sopenharmony_ci	pdata->wr_osr_limit = 8;
14862306a36Sopenharmony_ci	pdata->tx_sf_mode = MTL_TSF_ENABLE;
14962306a36Sopenharmony_ci	pdata->tx_threshold = MTL_TX_THRESHOLD_64;
15062306a36Sopenharmony_ci	pdata->tx_osp_mode = DMA_OSP_ENABLE;
15162306a36Sopenharmony_ci	pdata->rx_sf_mode = MTL_RSF_DISABLE;
15262306a36Sopenharmony_ci	pdata->rx_threshold = MTL_RX_THRESHOLD_64;
15362306a36Sopenharmony_ci	pdata->pause_autoneg = 1;
15462306a36Sopenharmony_ci	pdata->tx_pause = 1;
15562306a36Sopenharmony_ci	pdata->rx_pause = 1;
15662306a36Sopenharmony_ci	pdata->phy_speed = SPEED_UNKNOWN;
15762306a36Sopenharmony_ci	pdata->power_down = 0;
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	DBGPR("<--xgbe_default_config\n");
16062306a36Sopenharmony_ci}
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cistatic void xgbe_init_all_fptrs(struct xgbe_prv_data *pdata)
16362306a36Sopenharmony_ci{
16462306a36Sopenharmony_ci	xgbe_init_function_ptrs_dev(&pdata->hw_if);
16562306a36Sopenharmony_ci	xgbe_init_function_ptrs_phy(&pdata->phy_if);
16662306a36Sopenharmony_ci	xgbe_init_function_ptrs_i2c(&pdata->i2c_if);
16762306a36Sopenharmony_ci	xgbe_init_function_ptrs_desc(&pdata->desc_if);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	pdata->vdata->init_function_ptrs_phy_impl(&pdata->phy_if);
17062306a36Sopenharmony_ci}
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_cistruct xgbe_prv_data *xgbe_alloc_pdata(struct device *dev)
17362306a36Sopenharmony_ci{
17462306a36Sopenharmony_ci	struct xgbe_prv_data *pdata;
17562306a36Sopenharmony_ci	struct net_device *netdev;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	netdev = alloc_etherdev_mq(sizeof(struct xgbe_prv_data),
17862306a36Sopenharmony_ci				   XGBE_MAX_DMA_CHANNELS);
17962306a36Sopenharmony_ci	if (!netdev) {
18062306a36Sopenharmony_ci		dev_err(dev, "alloc_etherdev_mq failed\n");
18162306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
18262306a36Sopenharmony_ci	}
18362306a36Sopenharmony_ci	SET_NETDEV_DEV(netdev, dev);
18462306a36Sopenharmony_ci	pdata = netdev_priv(netdev);
18562306a36Sopenharmony_ci	pdata->netdev = netdev;
18662306a36Sopenharmony_ci	pdata->dev = dev;
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci	spin_lock_init(&pdata->lock);
18962306a36Sopenharmony_ci	spin_lock_init(&pdata->xpcs_lock);
19062306a36Sopenharmony_ci	mutex_init(&pdata->rss_mutex);
19162306a36Sopenharmony_ci	spin_lock_init(&pdata->tstamp_lock);
19262306a36Sopenharmony_ci	mutex_init(&pdata->i2c_mutex);
19362306a36Sopenharmony_ci	init_completion(&pdata->i2c_complete);
19462306a36Sopenharmony_ci	init_completion(&pdata->mdio_complete);
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	pdata->msg_enable = netif_msg_init(debug, default_msg_level);
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	set_bit(XGBE_DOWN, &pdata->dev_state);
19962306a36Sopenharmony_ci	set_bit(XGBE_STOPPED, &pdata->dev_state);
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci	return pdata;
20262306a36Sopenharmony_ci}
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_civoid xgbe_free_pdata(struct xgbe_prv_data *pdata)
20562306a36Sopenharmony_ci{
20662306a36Sopenharmony_ci	struct net_device *netdev = pdata->netdev;
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	free_netdev(netdev);
20962306a36Sopenharmony_ci}
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_civoid xgbe_set_counts(struct xgbe_prv_data *pdata)
21262306a36Sopenharmony_ci{
21362306a36Sopenharmony_ci	/* Set all the function pointers */
21462306a36Sopenharmony_ci	xgbe_init_all_fptrs(pdata);
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	/* Populate the hardware features */
21762306a36Sopenharmony_ci	xgbe_get_all_hw_features(pdata);
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	/* Set default max values if not provided */
22062306a36Sopenharmony_ci	if (!pdata->tx_max_channel_count)
22162306a36Sopenharmony_ci		pdata->tx_max_channel_count = pdata->hw_feat.tx_ch_cnt;
22262306a36Sopenharmony_ci	if (!pdata->rx_max_channel_count)
22362306a36Sopenharmony_ci		pdata->rx_max_channel_count = pdata->hw_feat.rx_ch_cnt;
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	if (!pdata->tx_max_q_count)
22662306a36Sopenharmony_ci		pdata->tx_max_q_count = pdata->hw_feat.tx_q_cnt;
22762306a36Sopenharmony_ci	if (!pdata->rx_max_q_count)
22862306a36Sopenharmony_ci		pdata->rx_max_q_count = pdata->hw_feat.rx_q_cnt;
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	/* Calculate the number of Tx and Rx rings to be created
23162306a36Sopenharmony_ci	 *  -Tx (DMA) Channels map 1-to-1 to Tx Queues so set
23262306a36Sopenharmony_ci	 *   the number of Tx queues to the number of Tx channels
23362306a36Sopenharmony_ci	 *   enabled
23462306a36Sopenharmony_ci	 *  -Rx (DMA) Channels do not map 1-to-1 so use the actual
23562306a36Sopenharmony_ci	 *   number of Rx queues or maximum allowed
23662306a36Sopenharmony_ci	 */
23762306a36Sopenharmony_ci	pdata->tx_ring_count = min_t(unsigned int, num_online_cpus(),
23862306a36Sopenharmony_ci				     pdata->hw_feat.tx_ch_cnt);
23962306a36Sopenharmony_ci	pdata->tx_ring_count = min_t(unsigned int, pdata->tx_ring_count,
24062306a36Sopenharmony_ci				     pdata->tx_max_channel_count);
24162306a36Sopenharmony_ci	pdata->tx_ring_count = min_t(unsigned int, pdata->tx_ring_count,
24262306a36Sopenharmony_ci				     pdata->tx_max_q_count);
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	pdata->tx_q_count = pdata->tx_ring_count;
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci	pdata->rx_ring_count = min_t(unsigned int, num_online_cpus(),
24762306a36Sopenharmony_ci				     pdata->hw_feat.rx_ch_cnt);
24862306a36Sopenharmony_ci	pdata->rx_ring_count = min_t(unsigned int, pdata->rx_ring_count,
24962306a36Sopenharmony_ci				     pdata->rx_max_channel_count);
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	pdata->rx_q_count = min_t(unsigned int, pdata->hw_feat.rx_q_cnt,
25262306a36Sopenharmony_ci				  pdata->rx_max_q_count);
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	if (netif_msg_probe(pdata)) {
25562306a36Sopenharmony_ci		dev_dbg(pdata->dev, "TX/RX DMA channel count = %u/%u\n",
25662306a36Sopenharmony_ci			pdata->tx_ring_count, pdata->rx_ring_count);
25762306a36Sopenharmony_ci		dev_dbg(pdata->dev, "TX/RX hardware queue count = %u/%u\n",
25862306a36Sopenharmony_ci			pdata->tx_q_count, pdata->rx_q_count);
25962306a36Sopenharmony_ci	}
26062306a36Sopenharmony_ci}
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ciint xgbe_config_netdev(struct xgbe_prv_data *pdata)
26362306a36Sopenharmony_ci{
26462306a36Sopenharmony_ci	struct net_device *netdev = pdata->netdev;
26562306a36Sopenharmony_ci	struct device *dev = pdata->dev;
26662306a36Sopenharmony_ci	int ret;
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	netdev->irq = pdata->dev_irq;
26962306a36Sopenharmony_ci	netdev->base_addr = (unsigned long)pdata->xgmac_regs;
27062306a36Sopenharmony_ci	eth_hw_addr_set(netdev, pdata->mac_addr);
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	/* Initialize ECC timestamps */
27362306a36Sopenharmony_ci	pdata->tx_sec_period = jiffies;
27462306a36Sopenharmony_ci	pdata->tx_ded_period = jiffies;
27562306a36Sopenharmony_ci	pdata->rx_sec_period = jiffies;
27662306a36Sopenharmony_ci	pdata->rx_ded_period = jiffies;
27762306a36Sopenharmony_ci	pdata->desc_sec_period = jiffies;
27862306a36Sopenharmony_ci	pdata->desc_ded_period = jiffies;
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci	/* Issue software reset to device */
28162306a36Sopenharmony_ci	ret = pdata->hw_if.exit(pdata);
28262306a36Sopenharmony_ci	if (ret) {
28362306a36Sopenharmony_ci		dev_err(dev, "software reset failed\n");
28462306a36Sopenharmony_ci		return ret;
28562306a36Sopenharmony_ci	}
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci	/* Set default configuration data */
28862306a36Sopenharmony_ci	xgbe_default_config(pdata);
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	/* Set the DMA mask */
29162306a36Sopenharmony_ci	ret = dma_set_mask_and_coherent(dev,
29262306a36Sopenharmony_ci					DMA_BIT_MASK(pdata->hw_feat.dma_width));
29362306a36Sopenharmony_ci	if (ret) {
29462306a36Sopenharmony_ci		dev_err(dev, "dma_set_mask_and_coherent failed\n");
29562306a36Sopenharmony_ci		return ret;
29662306a36Sopenharmony_ci	}
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci	/* Set default max values if not provided */
29962306a36Sopenharmony_ci	if (!pdata->tx_max_fifo_size)
30062306a36Sopenharmony_ci		pdata->tx_max_fifo_size = pdata->hw_feat.tx_fifo_size;
30162306a36Sopenharmony_ci	if (!pdata->rx_max_fifo_size)
30262306a36Sopenharmony_ci		pdata->rx_max_fifo_size = pdata->hw_feat.rx_fifo_size;
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci	/* Set and validate the number of descriptors for a ring */
30562306a36Sopenharmony_ci	BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_TX_DESC_CNT);
30662306a36Sopenharmony_ci	pdata->tx_desc_count = XGBE_TX_DESC_CNT;
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci	BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_RX_DESC_CNT);
30962306a36Sopenharmony_ci	pdata->rx_desc_count = XGBE_RX_DESC_CNT;
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci	/* Adjust the number of queues based on interrupts assigned */
31262306a36Sopenharmony_ci	if (pdata->channel_irq_count) {
31362306a36Sopenharmony_ci		pdata->tx_ring_count = min_t(unsigned int, pdata->tx_ring_count,
31462306a36Sopenharmony_ci					     pdata->channel_irq_count);
31562306a36Sopenharmony_ci		pdata->rx_ring_count = min_t(unsigned int, pdata->rx_ring_count,
31662306a36Sopenharmony_ci					     pdata->channel_irq_count);
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci		if (netif_msg_probe(pdata))
31962306a36Sopenharmony_ci			dev_dbg(pdata->dev,
32062306a36Sopenharmony_ci				"adjusted TX/RX DMA channel count = %u/%u\n",
32162306a36Sopenharmony_ci				pdata->tx_ring_count, pdata->rx_ring_count);
32262306a36Sopenharmony_ci	}
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci	/* Initialize RSS hash key */
32562306a36Sopenharmony_ci	netdev_rss_key_fill(pdata->rss_key, sizeof(pdata->rss_key));
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci	XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, IP2TE, 1);
32862306a36Sopenharmony_ci	XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, TCP4TE, 1);
32962306a36Sopenharmony_ci	XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, UDP4TE, 1);
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci	/* Call MDIO/PHY initialization routine */
33262306a36Sopenharmony_ci	pdata->debugfs_an_cdr_workaround = pdata->vdata->an_cdr_workaround;
33362306a36Sopenharmony_ci	ret = pdata->phy_if.phy_init(pdata);
33462306a36Sopenharmony_ci	if (ret)
33562306a36Sopenharmony_ci		return ret;
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci	/* Set device operations */
33862306a36Sopenharmony_ci	netdev->netdev_ops = xgbe_get_netdev_ops();
33962306a36Sopenharmony_ci	netdev->ethtool_ops = xgbe_get_ethtool_ops();
34062306a36Sopenharmony_ci#ifdef CONFIG_AMD_XGBE_DCB
34162306a36Sopenharmony_ci	netdev->dcbnl_ops = xgbe_get_dcbnl_ops();
34262306a36Sopenharmony_ci#endif
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci	/* Set device features */
34562306a36Sopenharmony_ci	netdev->hw_features = NETIF_F_SG |
34662306a36Sopenharmony_ci			      NETIF_F_IP_CSUM |
34762306a36Sopenharmony_ci			      NETIF_F_IPV6_CSUM |
34862306a36Sopenharmony_ci			      NETIF_F_RXCSUM |
34962306a36Sopenharmony_ci			      NETIF_F_TSO |
35062306a36Sopenharmony_ci			      NETIF_F_TSO6 |
35162306a36Sopenharmony_ci			      NETIF_F_GRO |
35262306a36Sopenharmony_ci			      NETIF_F_HW_VLAN_CTAG_RX |
35362306a36Sopenharmony_ci			      NETIF_F_HW_VLAN_CTAG_TX |
35462306a36Sopenharmony_ci			      NETIF_F_HW_VLAN_CTAG_FILTER;
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	if (pdata->hw_feat.rss)
35762306a36Sopenharmony_ci		netdev->hw_features |= NETIF_F_RXHASH;
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_ci	if (pdata->hw_feat.vxn) {
36062306a36Sopenharmony_ci		netdev->hw_enc_features = NETIF_F_SG |
36162306a36Sopenharmony_ci					  NETIF_F_IP_CSUM |
36262306a36Sopenharmony_ci					  NETIF_F_IPV6_CSUM |
36362306a36Sopenharmony_ci					  NETIF_F_RXCSUM |
36462306a36Sopenharmony_ci					  NETIF_F_TSO |
36562306a36Sopenharmony_ci					  NETIF_F_TSO6 |
36662306a36Sopenharmony_ci					  NETIF_F_GRO |
36762306a36Sopenharmony_ci					  NETIF_F_GSO_UDP_TUNNEL |
36862306a36Sopenharmony_ci					  NETIF_F_GSO_UDP_TUNNEL_CSUM;
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci		netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
37162306a36Sopenharmony_ci				       NETIF_F_GSO_UDP_TUNNEL_CSUM;
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci		netdev->udp_tunnel_nic_info = xgbe_get_udp_tunnel_info();
37462306a36Sopenharmony_ci	}
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci	netdev->vlan_features |= NETIF_F_SG |
37762306a36Sopenharmony_ci				 NETIF_F_IP_CSUM |
37862306a36Sopenharmony_ci				 NETIF_F_IPV6_CSUM |
37962306a36Sopenharmony_ci				 NETIF_F_TSO |
38062306a36Sopenharmony_ci				 NETIF_F_TSO6;
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci	netdev->features |= netdev->hw_features;
38362306a36Sopenharmony_ci	pdata->netdev_features = netdev->features;
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci	netdev->priv_flags |= IFF_UNICAST_FLT;
38662306a36Sopenharmony_ci	netdev->min_mtu = 0;
38762306a36Sopenharmony_ci	netdev->max_mtu = XGMAC_JUMBO_PACKET_MTU;
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci	/* Use default watchdog timeout */
39062306a36Sopenharmony_ci	netdev->watchdog_timeo = 0;
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci	xgbe_init_rx_coalesce(pdata);
39362306a36Sopenharmony_ci	xgbe_init_tx_coalesce(pdata);
39462306a36Sopenharmony_ci
39562306a36Sopenharmony_ci	netif_carrier_off(netdev);
39662306a36Sopenharmony_ci	ret = register_netdev(netdev);
39762306a36Sopenharmony_ci	if (ret) {
39862306a36Sopenharmony_ci		dev_err(dev, "net device registration failed\n");
39962306a36Sopenharmony_ci		return ret;
40062306a36Sopenharmony_ci	}
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ci	if (IS_REACHABLE(CONFIG_PTP_1588_CLOCK))
40362306a36Sopenharmony_ci		xgbe_ptp_register(pdata);
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci	xgbe_debugfs_init(pdata);
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci	netif_dbg(pdata, drv, pdata->netdev, "%u Tx software queues\n",
40862306a36Sopenharmony_ci		  pdata->tx_ring_count);
40962306a36Sopenharmony_ci	netif_dbg(pdata, drv, pdata->netdev, "%u Rx software queues\n",
41062306a36Sopenharmony_ci		  pdata->rx_ring_count);
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci	return 0;
41362306a36Sopenharmony_ci}
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_civoid xgbe_deconfig_netdev(struct xgbe_prv_data *pdata)
41662306a36Sopenharmony_ci{
41762306a36Sopenharmony_ci	struct net_device *netdev = pdata->netdev;
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci	xgbe_debugfs_exit(pdata);
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci	if (IS_REACHABLE(CONFIG_PTP_1588_CLOCK))
42262306a36Sopenharmony_ci		xgbe_ptp_unregister(pdata);
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci	unregister_netdev(netdev);
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci	pdata->phy_if.phy_exit(pdata);
42762306a36Sopenharmony_ci}
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_cistatic int xgbe_netdev_event(struct notifier_block *nb, unsigned long event,
43062306a36Sopenharmony_ci			     void *data)
43162306a36Sopenharmony_ci{
43262306a36Sopenharmony_ci	struct net_device *netdev = netdev_notifier_info_to_dev(data);
43362306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = netdev_priv(netdev);
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci	if (netdev->netdev_ops != xgbe_get_netdev_ops())
43662306a36Sopenharmony_ci		goto out;
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci	switch (event) {
43962306a36Sopenharmony_ci	case NETDEV_CHANGENAME:
44062306a36Sopenharmony_ci		xgbe_debugfs_rename(pdata);
44162306a36Sopenharmony_ci		break;
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci	default:
44462306a36Sopenharmony_ci		break;
44562306a36Sopenharmony_ci	}
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ciout:
44862306a36Sopenharmony_ci	return NOTIFY_DONE;
44962306a36Sopenharmony_ci}
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_cistatic struct notifier_block xgbe_netdev_notifier = {
45262306a36Sopenharmony_ci	.notifier_call = xgbe_netdev_event,
45362306a36Sopenharmony_ci};
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_cistatic int __init xgbe_mod_init(void)
45662306a36Sopenharmony_ci{
45762306a36Sopenharmony_ci	int ret;
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci	ret = register_netdevice_notifier(&xgbe_netdev_notifier);
46062306a36Sopenharmony_ci	if (ret)
46162306a36Sopenharmony_ci		return ret;
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci	ret = xgbe_platform_init();
46462306a36Sopenharmony_ci	if (ret)
46562306a36Sopenharmony_ci		goto err_platform_init;
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci	ret = xgbe_pci_init();
46862306a36Sopenharmony_ci	if (ret)
46962306a36Sopenharmony_ci		goto err_pci_init;
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci	return 0;
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_cierr_pci_init:
47462306a36Sopenharmony_ci	xgbe_platform_exit();
47562306a36Sopenharmony_cierr_platform_init:
47662306a36Sopenharmony_ci	unregister_netdevice_notifier(&xgbe_netdev_notifier);
47762306a36Sopenharmony_ci	return ret;
47862306a36Sopenharmony_ci}
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_cistatic void __exit xgbe_mod_exit(void)
48162306a36Sopenharmony_ci{
48262306a36Sopenharmony_ci	xgbe_pci_exit();
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci	xgbe_platform_exit();
48562306a36Sopenharmony_ci
48662306a36Sopenharmony_ci	unregister_netdevice_notifier(&xgbe_netdev_notifier);
48762306a36Sopenharmony_ci}
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_cimodule_init(xgbe_mod_init);
49062306a36Sopenharmony_cimodule_exit(xgbe_mod_exit);
491