// SPDX-License-Identifier: GPL-2.0-only OR BSD-3-Clause

/* Gigabit Ethernet driver for Mellanox BlueField SoC
 *
 * Copyright (C) 2020-2021 NVIDIA CORPORATION & AFFILIATES
 */

/* Standard method to enable kernel debug (e.g. dev_dbg) */
#define DEBUG

#include <linux/acpi.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/etherdevice.h>
#include <linux/interrupt.h>
#include <linux/iopoll.h>
#include <linux/module.h>
#include <linux/phy.h>
#include <linux/platform_device.h>
#include <linux/skbuff.h>
#include <linux/version.h>

/* The MLXBF_GIGE_INTERNAL setting is defined in the
 * "mlxbf_gige.h" header file, so this header file must
 * be included before any processing of that setting.
 */
#include "mlxbf_gige.h"
#include "mlxbf_gige_regs.h"

/* This setting enables driver logic that uses kernel APIs
 * to communicate with the board-level PHY device.  This
 * setting must be turned off when running code on Palladium.
 */
#define MLXBF_GIGE_USE_PHY

#define DRV_NAME    "mlxbf_gige"
#define DRV_VERSION 1.27

/* This setting defines the version of the ACPI table
 * content that is compatible with this driver version.
 */
#define MLXBF_GIGE_ACPI_TABLE_VERSION 2

/* Allocate SKB whose payload pointer aligns with the Bluefield
 * hardware DMA limitation, i.e. DMA operation can't cross
 * a 4KB boundary.  A maximum packet size of 2KB is assumed in the
 * alignment formula.  The alignment logic overallocates an SKB,
 * and then adjusts the headroom so that the SKB data pointer is
 * naturally aligned to a 2KB boundary.
 */
struct sk_buff *mlxbf_gige_alloc_skb(struct mlxbf_gige *priv,
				     unsigned int map_len,
				     dma_addr_t *buf_dma,
				     enum dma_data_direction dir)
{
	struct sk_buff *skb;
	u64 addr, offset;

	/* Overallocate the SKB so that any headroom adjustment (to
	 * provide 2KB natural alignment) does not exceed payload area
	 */
	skb = netdev_alloc_skb(priv->netdev, MLXBF_GIGE_DEFAULT_BUF_SZ * 2);
	if (!skb)
		return NULL;

	/* Adjust the headroom so that skb->data is naturally aligned to
	 * a 2KB boundary, which is the maximum packet size supported.
	 */
	addr = (long)skb->data;
	offset = (addr + MLXBF_GIGE_DEFAULT_BUF_SZ - 1) &
		~(MLXBF_GIGE_DEFAULT_BUF_SZ - 1);
	offset -= addr;
	if (offset)
		skb_reserve(skb, offset);

	/* Return streaming DMA mapping to caller */
	*buf_dma = dma_map_single(priv->dev, skb->data, map_len, dir);
	if (dma_mapping_error(priv->dev, *buf_dma)) {
		dev_kfree_skb(skb);
		*buf_dma = (dma_addr_t)0;
		return NULL;
	}

	return skb;
}

static void mlxbf_gige_initial_mac(struct mlxbf_gige *priv)
{
	u8 mac[ETH_ALEN];
	u64 local_mac;

	memset(mac, 0, ETH_ALEN);
	mlxbf_gige_get_mac_rx_filter(priv, MLXBF_GIGE_LOCAL_MAC_FILTER_IDX,
				     &local_mac);
	u64_to_ether_addr(local_mac, mac);

	if (is_valid_ether_addr(mac)) {
		ether_addr_copy(priv->netdev->dev_addr, mac);
#ifdef MLXBF_GIGE_INTERNAL
		dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
#endif
	} else {
		/* Provide a random MAC if for some reason the device has
		 * not been configured with a valid MAC address already.
		 */
		eth_hw_addr_random(priv->netdev);
#ifdef MLXBF_GIGE_INTERNAL
		dev_info(priv->dev, "Generated random MAC address %pM\n",
			 priv->netdev->dev_addr);
#endif
	}

	local_mac = ether_addr_to_u64(priv->netdev->dev_addr);
	mlxbf_gige_set_mac_rx_filter(priv, MLXBF_GIGE_LOCAL_MAC_FILTER_IDX,
				     local_mac);
}

static void mlxbf_gige_cache_stats(struct mlxbf_gige *priv)
{
	struct mlxbf_gige_stats *p;

	/* Cache stats that will be cleared by clean port operation */
	p = &priv->stats;
	p->rx_din_dropped_pkts += readq(priv->base +
					MLXBF_GIGE_RX_DIN_DROP_COUNTER);
	p->rx_filter_passed_pkts += readq(priv->base +
					  MLXBF_GIGE_RX_PASS_COUNTER_ALL);
	p->rx_filter_discard_pkts += readq(priv->base +
					   MLXBF_GIGE_RX_DISC_COUNTER_ALL);
}

static int mlxbf_gige_clean_port(struct mlxbf_gige *priv)
{
	u64 control;
	u64 temp;
	int err;

	/* Set the CLEAN_PORT_EN bit to trigger SW reset */
	control = readq(priv->base + MLXBF_GIGE_CONTROL);
	control |= MLXBF_GIGE_CONTROL_CLEAN_PORT_EN;
	writeq(control, priv->base + MLXBF_GIGE_CONTROL);

	/* Ensure completion of "clean port" write before polling status */
	mb();

	err = readq_poll_timeout_atomic(priv->base + MLXBF_GIGE_STATUS, temp,
					(temp & MLXBF_GIGE_STATUS_READY),
					100, 100000);

	/* Clear the CLEAN_PORT_EN bit at end of this loop */
	control = readq(priv->base + MLXBF_GIGE_CONTROL);
	control &= ~MLXBF_GIGE_CONTROL_CLEAN_PORT_EN;
	writeq(control, priv->base + MLXBF_GIGE_CONTROL);

	return err;
}

static int mlxbf_gige_open(struct net_device *netdev)
{
	struct mlxbf_gige *priv = netdev_priv(netdev);
#ifdef MLXBF_GIGE_USE_PHY
	struct phy_device *phydev = netdev->phydev;
#endif
	u64 int_en;
	int err;

#ifdef MLXBF_GIGE_INTERNAL
	netdev_printk(KERN_DEBUG, netdev, "open: priv=%llx\n",
		      (u64)priv);
#endif

	err = mlxbf_gige_request_irqs(priv);
	if (err)
		return err;
	mlxbf_gige_cache_stats(priv);
	err = mlxbf_gige_clean_port(priv);
	if (err)
		goto free_irqs;

	/* Clear driver's valid_polarity to match hardware,
	 * since the above call to clean_port() resets the
	 * receive polarity used by hardware.
	 */
	priv->valid_polarity = 0;

	err = mlxbf_gige_rx_init(priv);
	if (err)
		goto free_irqs;
	err = mlxbf_gige_tx_init(priv);
	if (err)
		goto rx_deinit;

#ifdef MLXBF_GIGE_USE_PHY
	phy_start(phydev);
#endif /* MLXBF_GIGE_USE_PHY */

	netif_napi_add(netdev, &priv->napi, mlxbf_gige_poll, NAPI_POLL_WEIGHT);
	napi_enable(&priv->napi);
	netif_start_queue(netdev);

	/* Set bits in INT_EN that we care about */
	int_en = MLXBF_GIGE_INT_EN_HW_ACCESS_ERROR |
		 MLXBF_GIGE_INT_EN_TX_CHECKSUM_INPUTS |
		 MLXBF_GIGE_INT_EN_TX_SMALL_FRAME_SIZE |
		 MLXBF_GIGE_INT_EN_TX_PI_CI_EXCEED_WQ_SIZE |
		 MLXBF_GIGE_INT_EN_SW_CONFIG_ERROR |
		 MLXBF_GIGE_INT_EN_SW_ACCESS_ERROR |
		 MLXBF_GIGE_INT_EN_RX_RECEIVE_PACKET;

	/* Ensure completion of all initialization before enabling interrupts */
	mb();

	writeq(int_en, priv->base + MLXBF_GIGE_INT_EN);

	return 0;

rx_deinit:
	mlxbf_gige_rx_deinit(priv);

free_irqs:
	mlxbf_gige_free_irqs(priv);
	return err;
}

static int mlxbf_gige_stop(struct net_device *netdev)
{
	struct mlxbf_gige *priv = netdev_priv(netdev);

#ifdef MLXBF_GIGE_INTERNAL
	netdev_printk(KERN_DEBUG, netdev, "stop: priv=%llx\n",
		      (u64)priv);
#endif

	writeq(0, priv->base + MLXBF_GIGE_INT_EN);
	netif_stop_queue(netdev);
	napi_disable(&priv->napi);
	netif_napi_del(&priv->napi);
	mlxbf_gige_free_irqs(priv);

#ifdef MLXBF_GIGE_USE_PHY
	phy_stop(netdev->phydev);
#endif /* MLXBF_GIGE_USE_PHY */

	mlxbf_gige_rx_deinit(priv);
	mlxbf_gige_tx_deinit(priv);
	mlxbf_gige_cache_stats(priv);
	mlxbf_gige_clean_port(priv);

	return 0;
}

static int mlxbf_gige_do_ioctl(struct net_device *netdev,
			       struct ifreq *ifr, int cmd)
{
#ifdef MLXBF_GIGE_USE_PHY
	if (!(netif_running(netdev)))
		return -EINVAL;

	return phy_mii_ioctl(netdev->phydev, ifr, cmd);
#else
	return 0;

#endif /* MLXBF_GIGE_USE_PHY*/
}

static void mlxbf_gige_set_rx_mode(struct net_device *netdev)
{
	struct mlxbf_gige *priv = netdev_priv(netdev);
	bool new_promisc_enabled;

#ifdef MLXBF_GIGE_INTERNAL
	netdev_printk(KERN_DEBUG, netdev, "set_rx_mode: priv=%llx flags=%x\n",
		      (u64)priv, netdev->flags);
#endif

	new_promisc_enabled = netdev->flags & IFF_PROMISC;

	/* Only write to the hardware registers if the new setting
	 * of promiscuous mode is different from the current one.
	 */
	if (new_promisc_enabled != priv->promisc_enabled) {
		priv->promisc_enabled = new_promisc_enabled;

		if (new_promisc_enabled) {
#ifdef MLXBF_GIGE_INTERNAL
			netdev_printk(KERN_DEBUG, netdev,
				      "set_rx_mode: enable promisc\n");
#endif
			mlxbf_gige_enable_promisc(priv);
		} else {
#ifdef MLXBF_GIGE_INTERNAL
			netdev_printk(KERN_DEBUG, netdev,
				      "set_rx_mode: disable promisc\n");
#endif
			mlxbf_gige_disable_promisc(priv);
		}
	}
}

static void mlxbf_gige_get_stats64(struct net_device *netdev,
				   struct rtnl_link_stats64 *stats)
{
	struct mlxbf_gige *priv = netdev_priv(netdev);

	netdev_stats_to_stats64(stats, &netdev->stats);

	stats->rx_length_errors = priv->stats.rx_truncate_errors;
	stats->rx_fifo_errors = priv->stats.rx_din_dropped_pkts +
				readq(priv->base + MLXBF_GIGE_RX_DIN_DROP_COUNTER);
	stats->rx_crc_errors = priv->stats.rx_mac_errors;
	stats->rx_errors = stats->rx_length_errors +
			   stats->rx_fifo_errors +
			   stats->rx_crc_errors;

	stats->tx_fifo_errors = priv->stats.tx_fifo_full;
	stats->tx_errors = stats->tx_fifo_errors;
}

#ifdef MLXBF_GIGE_INTERNAL
static void mlxbf_gige_tx_timeout(struct net_device *netdev)
{
	/* TODO - add TX timeout logic */
}
#endif

static const struct net_device_ops mlxbf_gige_netdev_ops = {
	.ndo_open		= mlxbf_gige_open,
	.ndo_stop		= mlxbf_gige_stop,
	.ndo_start_xmit		= mlxbf_gige_start_xmit,
	.ndo_set_mac_address	= eth_mac_addr,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_do_ioctl		= mlxbf_gige_do_ioctl,
	.ndo_set_rx_mode        = mlxbf_gige_set_rx_mode,
	.ndo_get_stats64        = mlxbf_gige_get_stats64,
#ifdef MLXBF_GIGE_INTERNAL
	.ndo_tx_timeout         = mlxbf_gige_tx_timeout,
#endif
};

static void mlxbf_gige_adjust_link(struct net_device *netdev)
{
	struct phy_device *phydev = netdev->phydev;

	phy_print_status(phydev);
}

static int mlxbf_gige_probe(struct platform_device *pdev)
{
	struct phy_device *phydev;
	struct net_device *netdev;
	struct resource *mac_res;
	struct resource *llu_res;
	struct resource *plu_res;
	struct mlxbf_gige *priv;
	void __iomem *llu_base;
	void __iomem *plu_base;
	void __iomem *base;
	int addr, phy_irq;
	u32 version;
	u64 control;
	int err;

#ifdef MLXBF_GIGE_INTERNAL
	u64 exp_data;

	dev_dbg(&pdev->dev, "probe: pdev=0x%llx pdev->dev=0x%llx\n",
		(u64)pdev, (u64)&pdev->dev);
#endif
	version = 0;
	err = device_property_read_u32(&pdev->dev, "version", &version);
	if (err) {
		dev_err(&pdev->dev, "ACPI table version not found\n");
		return -EINVAL;
	}

	if (version != MLXBF_GIGE_ACPI_TABLE_VERSION) {
		dev_err(&pdev->dev, "ACPI table version mismatch: expected %d found %d\n",
			MLXBF_GIGE_ACPI_TABLE_VERSION, version);
		return -EINVAL;
	}

	mac_res = platform_get_resource(pdev, IORESOURCE_MEM, MLXBF_GIGE_RES_MAC);
	if (!mac_res)
		return -ENXIO;

#ifdef MLXBF_GIGE_INTERNAL
	dev_dbg(&pdev->dev, "probe: resource %d (MAC) start=0x%llx end=0x%llx\n",
		MLXBF_GIGE_RES_MAC, mac_res->start, mac_res->end);
#endif

	base = devm_ioremap_resource(&pdev->dev, mac_res);
	if (IS_ERR(base))
		return PTR_ERR(base);

	llu_res = platform_get_resource(pdev, IORESOURCE_MEM, MLXBF_GIGE_RES_LLU);
	if (!llu_res)
		return -ENXIO;

#ifdef MLXBF_GIGE_INTERNAL
	dev_dbg(&pdev->dev, "probe: resource %d (LLU) start=0x%llx end=0x%llx\n",
		MLXBF_GIGE_RES_LLU, llu_res->start, llu_res->end);
#endif

	llu_base = devm_ioremap_resource(&pdev->dev, llu_res);
	if (IS_ERR(llu_base))
		return PTR_ERR(llu_base);

	plu_res = platform_get_resource(pdev, IORESOURCE_MEM, MLXBF_GIGE_RES_PLU);
	if (!plu_res)
		return -ENXIO;

#ifdef MLXBF_GIGE_INTERNAL
	dev_dbg(&pdev->dev, "probe: resource %d (PLU) start=0x%llx end=0x%llx\n",
		MLXBF_GIGE_RES_PLU, plu_res->start, plu_res->end);
#endif

	plu_base = devm_ioremap_resource(&pdev->dev, plu_res);
	if (IS_ERR(plu_base))
		return PTR_ERR(plu_base);

#ifdef MLXBF_GIGE_INTERNAL
	/* Read single MMIO register and compare to expected value */
	exp_data = (MLXBF_GIGE_CONFIG_MAX_PKT_SZ_RESET_VAL
		    << MLXBF_GIGE_CONFIG_MAX_PKT_SZ_SHIFT);
	if (readq(base + MLXBF_GIGE_CONFIG) != exp_data) {
		dev_err(&pdev->dev,
			"probe failed, unexpected value in MLXBF_GIGE_CONFIG\n");
		return -ENODEV;
	}
#endif

	/* Perform general init of GigE block */
	control = readq(base + MLXBF_GIGE_CONTROL);
	control |= MLXBF_GIGE_CONTROL_PORT_EN;
	writeq(control, base + MLXBF_GIGE_CONTROL);

	netdev = devm_alloc_etherdev(&pdev->dev, sizeof(*priv));
	if (!netdev) {
#ifdef MLXBF_GIGE_INTERNAL
		dev_err(&pdev->dev, "Failed to allocate etherdev\n");
#endif
		return -ENOMEM;
	}

#ifdef MLXBF_GIGE_INTERNAL
	dev_dbg(&pdev->dev, "probe: netdev=%llx\n", (u64)netdev);
#endif

	SET_NETDEV_DEV(netdev, &pdev->dev);
	netdev->netdev_ops = &mlxbf_gige_netdev_ops;
	netdev->ethtool_ops = &mlxbf_gige_ethtool_ops;
	priv = netdev_priv(netdev);
	priv->netdev = netdev;

	platform_set_drvdata(pdev, priv);
	priv->dev = &pdev->dev;
	priv->pdev = pdev;

	spin_lock_init(&priv->lock);

#ifdef MLXBF_GIGE_USE_PHY
	/*
	 * TODO: Palladium has no connection to the PHY hardware, so
	 * the MDIO probe will fail.
	 * This needs to be removed once palladium provides a connection
	 * to the PHY device.
	 */

	/* Attach MDIO device */
	err = mlxbf_gige_mdio_probe(pdev, priv);
	if (err)
		return err;

#endif /* MLXBF_GIGE_USE_PHY */

	priv->base = base;
	priv->llu_base = llu_base;
	priv->plu_base = plu_base;

#ifdef MLXBF_GIGE_INTERNAL
	dev_dbg(&pdev->dev, "probe: priv=0x%llx priv->base=0x%llx\n",
		(u64)priv, (u64)priv->base);
	dev_dbg(&pdev->dev, "probe: llu_base=0x%llx plu_base=0x%llx\n",
		(u64)priv->llu_base, (u64)priv->plu_base);

	/* Perform some self tests on MAC, PLU, LLU */
	mlxbf_gige_selftests(pdev, priv);
	mlxbf_gige_plu_selftests(pdev, priv);
	mlxbf_gige_llu_selftests(pdev, priv);
#endif

	priv->rx_q_entries = MLXBF_GIGE_DEFAULT_RXQ_SZ;
	priv->tx_q_entries = MLXBF_GIGE_DEFAULT_TXQ_SZ;

	/* Write initial MAC address to hardware */
	mlxbf_gige_initial_mac(priv);

#ifdef MLXBF_GIGE_INTERNAL
	/* Create sysfs entries for driver */
	mlxbf_gige_create_sysfs(&pdev->dev);
#endif

	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
	if (err) {
		dev_err(&pdev->dev, "DMA configuration failed: 0x%x\n", err);
		goto out;
	}

	priv->error_irq = platform_get_irq(pdev, MLXBF_GIGE_ERROR_INTR_IDX);
	priv->rx_irq = platform_get_irq(pdev, MLXBF_GIGE_RECEIVE_PKT_INTR_IDX);
	priv->llu_plu_irq = platform_get_irq(pdev, MLXBF_GIGE_LLU_PLU_INTR_IDX);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 24)
	phy_irq = acpi_dev_gpio_irq_get_by(ACPI_COMPANION(&pdev->dev), "phy-gpios", 0);
#else
	phy_irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(&pdev->dev), 0);
#endif
	if (phy_irq < 0) {
		dev_err(&pdev->dev, "Error getting PHY irq. Use polling instead");
		phy_irq = PHY_POLL;
	}

#ifdef MLXBF_GIGE_INTERNAL
	dev_dbg(&pdev->dev, "probe: irq[] = %d %d %d\n",
		priv->error_irq, priv->rx_irq, priv->llu_plu_irq);
#endif

#ifdef MLXBF_GIGE_USE_PHY
	phydev = phy_find_first(priv->mdiobus);
	if (!phydev) {
		err = -ENODEV;
		goto out;
	}

	addr = phydev->mdio.addr;
	priv->mdiobus->irq[addr] = phy_irq;
	phydev->irq = phy_irq;

	err = phy_connect_direct(netdev, phydev,
				 mlxbf_gige_adjust_link,
				 PHY_INTERFACE_MODE_GMII);
	if (err) {
		dev_err(&pdev->dev, "Could not attach to PHY\n");
		goto out;
	}

/* Check if driver should use u32 or link mode bitmap for PHY device fields
 * The following software versions use the new method, i.e. link mode bitmap.
 *   a) RHEL version >= 8.2
 *   b) Linux version >= 5.0.0
 *
 * NOTE: Neither RHEL_RELEASE_VERSION nor RHEL_RELEASE_CODE are defined
 *       for 4.11.0 driver disk build or OOT driver build.
 */

#if defined(RHEL_RELEASE_VERSION) && defined(RHEL_RELEASE_CODE)
#if RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(8, 2)
#define USE_PHY_LINK_MODE_BITMAP
#endif
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
#define USE_PHY_LINK_MODE_BITMAP
#endif
#endif

#ifdef USE_PHY_LINK_MODE_BITMAP
	/* MAC only supports 1000T full duplex mode */
	phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
	phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Full_BIT);
	phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Half_BIT);
	phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Full_BIT);
	phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Half_BIT);

	/* Only symmetric pause with flow control enabled is supported so no
	 * need to negotiate pause.
	 */
	linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->advertising);
	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->advertising);

#else
	/* MAC only supports 1000T full duplex mode */
	phydev->supported &= ~SUPPORTED_1000baseT_Half;
	phydev->supported &= ~SUPPORTED_100baseT_Full;
	phydev->supported &= ~SUPPORTED_100baseT_Half;
	phydev->supported &= ~SUPPORTED_10baseT_Full;
	phydev->supported &= ~SUPPORTED_10baseT_Half;
	/* Only symmetric pause with flow control enabled is supported so no
	 * need to negotiate pause.
	 */
	phydev->advertising &= ~ADVERTISED_Pause;
	phydev->advertising &= ~ADVERTISED_Asym_Pause;
#endif

#ifdef MLXBF_GIGE_INTERNAL
#ifdef USE_PHY_LINK_MODE_BITMAP
	dev_dbg(&pdev->dev, "supported: %*pb, advertising: %*pb",
		__ETHTOOL_LINK_MODE_MASK_NBITS, phydev->supported,
		__ETHTOOL_LINK_MODE_MASK_NBITS, phydev->advertising);
	dev_dbg(&pdev->dev, "speed: 0x%x, duplex: 0x%x, autoneg: 0x%x,"
		" pause: 0x%x, asym_pause: 0x%x\n",
		phydev->speed, phydev->duplex, phydev->autoneg,
		phydev->pause, phydev->asym_pause);
#else
	dev_dbg(&pdev->dev, "supported: 0x%x, advertising: 0x%x,"
		" speed: 0x%x, duplex: 0x%x, autoneg: 0x%x, pause: 0x%x,"
		" asym_pause: 0x%x\n",
		phydev->supported, phydev->advertising, phydev->speed,
		phydev->duplex, phydev->autoneg, phydev->pause,
		phydev->asym_pause);
#endif
#endif /* MLXBF_GIGE_INTERNAL */

	/* Display information about attached PHY device */
	phy_attached_info(phydev);
#endif /* MLXBF_GIGE_USE_PHY */

	err = register_netdev(netdev);
	if (err) {
		dev_err(&pdev->dev, "Failed to register netdev\n");
		phy_disconnect(phydev);
		goto out;
	}

#ifdef MLXBF_GIGE_INTERNAL
	dev_info(&pdev->dev, "probed\n");

	priv->tx_data_seed = 0xB0;
#endif

	return 0;

out:
	mlxbf_gige_mdio_remove(priv);
	return err;
}

static int mlxbf_gige_remove(struct platform_device *pdev)
{
	struct mlxbf_gige *priv = platform_get_drvdata(pdev);

#ifdef MLXBF_GIGE_INTERNAL
	dev_dbg(&pdev->dev, "remove: pdev=%llx\n", (u64)pdev);
#endif

	unregister_netdev(priv->netdev);
	phy_disconnect(priv->netdev->phydev);

#ifdef MLXBF_GIGE_INTERNAL
	/* Remove driver sysfs entries */
	mlxbf_gige_remove_sysfs(&pdev->dev);
#endif

#ifdef MLXBF_GIGE_USE_PHY
	mlxbf_gige_mdio_remove(priv);
#endif /* MLXBF_GIGE_USE_PHY */

	platform_set_drvdata(pdev, NULL);

	return 0;
}

static void mlxbf_gige_shutdown(struct platform_device *pdev)
{
	struct mlxbf_gige *priv = platform_get_drvdata(pdev);

#ifdef MLXBF_GIGE_INTERNAL
	dev_info(&pdev->dev, "shutdown\n");
#endif

	writeq(0, priv->base + MLXBF_GIGE_INT_EN);
	mlxbf_gige_clean_port(priv);
}

static const struct acpi_device_id __maybe_unused mlxbf_gige_acpi_match[] = {
	{ "MLNXBF17", 0 },
	{},
};
MODULE_DEVICE_TABLE(acpi, mlxbf_gige_acpi_match);

static struct platform_driver mlxbf_gige_driver = {
	.probe = mlxbf_gige_probe,
	.remove = mlxbf_gige_remove,
	.shutdown = mlxbf_gige_shutdown,
	.driver = {
		.name = DRV_NAME,
		.acpi_match_table = ACPI_PTR(mlxbf_gige_acpi_match),
	},
};

module_platform_driver(mlxbf_gige_driver);

MODULE_SOFTDEP("pre: gpio_mlxbf2");
MODULE_DESCRIPTION("Mellanox BlueField SoC Gigabit Ethernet Driver");
MODULE_AUTHOR("David Thompson <davthompson@nvidia.com>");
MODULE_AUTHOR("Asmaa Mnebhi <asmaa@nvidia.com>");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_VERSION(__stringify(DRV_VERSION));
