Re: [RFC net-next 1/2] net: ethernet: add MEN Z87 driver

From: Andrew Lunn

Date: Tue Dec 01 2026 - 13:27:29 EST


> +static void z87_ethtool_get_drvinfo(struct net_device *dev,
> + struct ethtool_drvinfo *info)
> +{
> + struct z87_private *np = netdev_priv(dev);
> + unsigned long flags;
> +
> + raw_spin_lock_irqsave(&np->lock, flags);
> +
> + strscpy(info->driver, np->model_name, sizeof(info->driver));
> + strscpy(info->bus_info, dev_name(&dev->dev), sizeof(info->bus_info));
> +
> + raw_spin_unlock_irqrestore(&np->lock, flags);

Why is locking needed here? I don't know of any other driver which
does this.

> +static const struct ethtool_ops men_z87_ethtool_ops = {
> + .get_drvinfo = z87_ethtool_get_drvinfo,
> + .get_link_ksettings = phy_ethtool_get_link_ksettings,
> + .set_link_ksettings = phy_ethtool_set_link_ksettings,
> + .nway_reset = phy_ethtool_nway_reset,

Ideally you should use phylink, not phylib. The API is better, you are
less likely to make the errors everybody else does with pause, WoL,
EEE etc.

> +const struct ethtool_ops *z87_get_ethtool_ops(void)
> +{
> + return &men_z87_ethtool_ops;
> +}

Global function vs global structure. I would keep it KISS and just
make men_z87_ethtool_ops global.

> +/* Generic BD accessors */
> +
> +static inline void z87_bd_set_addr(const struct z87_private *np, struct z87_desc *desc, u32 addr)
> +{
> + WRITE_ONCE(desc->addr, cpu_to_le32(addr));
> + /* ensure DMA ordering */
> + dma_wmb();

Getting the memory barrier correct is not simple. So they should not
be hidden in include files. Please put them in the C code where they
can be seen and reviewed easily.

> +struct z87_i2c *z87_i2c_alloc(struct device *dev, unsigned long addr)
> +{
> + struct z87_i2c *i2c;
> + int ret;
> +
> + i2c = kzalloc_obj(struct z87_i2c);
> +
> + if (!i2c)
> + return ERR_PTR(-ENOMEM);
> +
> + // Initialize z87_i2c stuff
> + i2c->base_addr = addr;
> + i2c->i2c_bug = 0;
> +
> + // Prepare i2c adapter
> + i2c->adapter.owner = THIS_MODULE;
> + i2c->adapter.dev.parent = dev;
> + i2c->adapter.algo_data = &i2c->algo_bit;
> + snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
> + "I2C adapter at %s", dev_name(dev));
> +
> + // Prepare i2c bit-banging functions, parameters and private data.
> + i2c->algo_bit.setsda = z87_set_sda;
> + i2c->algo_bit.setscl = z87_set_scl;
> + i2c->algo_bit.getsda = z87_get_sda;
> + i2c->algo_bit.getscl = z87_get_scl;
> + i2c->algo_bit.udelay = 10; /* Recommended for SMBus */
> + i2c->algo_bit.timeout = usecs_to_jiffies(1000);
> + i2c->algo_bit.data = i2c;

Is this an existing open cores I2C bus master? Or is it specific to
this device? I'm just wondering if the whole thing exists somewhere
else and you just need to instantiate the driver.

> +/* Typedefs */
> +static int z87_open(struct net_device *dev);
> +static int z87_start_xmit(struct sk_buff *skb, struct net_device *dev);
> +static irqreturn_t z87_irq(int irq, void *dev_id);
> +static int z87_close(struct net_device *dev);
> +static void z87_tx_timeout(struct net_device *dev, unsigned int txqueue);
> +static int z87_poll(struct napi_struct *napi, int budget);
> +static void z87_pass_packet(struct napi_struct *napi, unsigned int idx);
> +static int z87_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
> +static void z87_hash_table_setup(struct net_device *dev);
> +static int z87_ether_gen_crc(const u8 *data);
> +static const struct board_config *z87_get_board_config(struct mcb_device *mdev);
> +static void z87_load_mac_addr(struct net_device *dev);
> +static int z87_alloc_rx_bd(struct net_device *dev);
> +static void z87_free_rx_bd(struct net_device *dev);
> +static int z87_hw_stop(struct net_device *dev);
> +static int z87_hw_start(struct net_device *dev);

Don't do this. Put the functions in the correct order so they are
declared before they are used. That allows the compiler to inline, so
you get better executable code.

> +static int z87_change_mtu(struct net_device *netdev, int new_mtu)
> +{
> + if (new_mtu < ETH_MIN_MTU || new_mtu > ETH_DATA_LEN)
> + return -EINVAL;

If you set netdev->max_mtu and netdev->min_mtu, the core will do this
validation for you.

> +static void z87_regdump(struct net_device *dev)
> +{
> + struct z87_private *np = netdev_priv(dev);
> + struct z87_desc tx_desc[Z087_TBD_NUM];
> + u32 cur_tx, dty_tx, cur_rx;
> + unsigned long i = 0;
> + u32 tmp = 0;
> +
> + /* Snapshot software TX state; hardware may still update descriptors. */
> + spin_lock_bh(&np->tx_lock);
> + cur_tx = np->cur_tx;
> + dty_tx = np->dty_tx;
> + cur_rx = READ_ONCE(np->cur_rx);
> +
> + spin_unlock_bh(&np->tx_lock);
> +
> + netdev_info(dev, "(netdevice '%s') base Addr: 0x%08lx\n",
> + dev->name, dev->base_addr);
> +
> + while (z87_reginfo[i].addr != 0xffff) {
> + tmp = z87_read32(dev, z87_reginfo[i].addr);
> + netdev_info(dev, "%s 0x%08x\n", z87_reginfo[i].name, tmp);
> + i++;
> + }
> +
> + z87_phy_regdump(dev);
> +
> + netdev_info(dev, "cur_tx 0x%02x, dty_tx 0x%02x, cur_rx 0x%02x\n",
> + cur_tx, dty_tx, cur_rx);
> +
> + if (READ_ONCE(np->msg_enable) > 0) {
> + netdev_info(dev,
> + "------------------ TX BDs: -------------------\n");
> + for (i = 0; i < Z087_TBD_NUM; i++) {
> + tx_desc[i].flags = READ_ONCE(np->tx_desc[i].flags);
> + tx_desc[i].addr = READ_ONCE(np->tx_desc[i].addr);
> +
> + netdev_info(dev,
> + "%02lx STAT: 0x%04x LEN: 0x%04x ADR 0x%08x\n",
> + i,
> + z87_bd_get_flags(np, &tx_desc[i], 0xffff),
> + z87_bd_get_len(np, &tx_desc[i]),
> + z87_bd_get_addr(np, &tx_desc[i]));
> + }
> +
> + netdev_info(dev,
> + "------------------ RX BDs: -------------------\n");
> + for (i = 0; i < Z087_RBD_NUM; i++) {
> + netdev_info(dev,
> + "%02lx STAT: 0x%04x LEN: 0x%04x ADR 0x%08x\n",
> + i,
> + z87_rbd_get_flags(np, i, 0xffff),
> + z87_rbd_get_len(np, i),
> + z87_rbd_get_addr(np, i));
> + }
> + }
> +}

This seems like debug code. It should not be in a mainline driver.

> +static void z87_load_mac_addr(struct net_device *dev)
> +{
> + struct z87_private *np = netdev_priv(dev);
> + u8 mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
> + struct sockaddr addr;
> + int res;
> +
> + z87_load_mac(dev, mac);
> +
> + if (is_valid_ether_addr(mac)) {
> + netdev_info(dev,
> + "using MAC %02x:%02x:%02x:%02x:%02x:%02x from registers.\n",
> + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

This driver seems to be quite verbose. Generally, there is maybe one
call to netdev_info() in a driver. Consider change some of these to
netdev_dbg(), or removing them.

> +static bool z87_rx_bd_errors(struct net_device *dev, unsigned int idx)
> +{
> + const u16 rx_err_mask = OETH_RX_BD_OVERRUN | OETH_RX_BD_INVSYMB |
> + OETH_RX_BD_DRIBBLE | OETH_RX_BD_TOOLONG |
> + OETH_RX_BD_SHORT | OETH_RX_BD_CRCERR |
> + OETH_RX_BD_LATECOL;
> + struct z87_private *np = netdev_priv(dev);
> + u16 rx_err_flags;
> +
> + rx_err_flags = z87_rbd_get_flags(np, idx, rx_err_mask);
> + if (!rx_err_flags)
> + return false;
> +
> + if (rx_err_flags & OETH_RX_BD_OVERRUN) {
> + netdev_warn(dev, "*** RX: overrun[%u]\n", idx);

These sorts of prints can spam the log, act as a DoS, if they can be
triggered from a remote device.

> + DEV_STATS_INC(dev, rx_over_errors);

The counter should be sufficient.

> +static int z87_hw_stop(struct net_device *dev)
> +{
> + struct z87_private *np = netdev_priv(dev);
> + unsigned long flags;
> + int res;
> +
> + /* already stopped by a previous failed attempt (napi disabled, phy stopped) */
> + if (test_bit(__Z87_HW_DEAD, &np->state))
> + return -EIO;
> +
> + spin_lock_bh(&np->tx_lock);
> + set_bit(__Z87_DOWN, &np->state);
> + spin_unlock_bh(&np->tx_lock);
> + netif_tx_disable(dev);
> +
> + netif_carrier_off(dev);
> +
> + if (dev->phydev)
> + phy_stop(dev->phydev);

It seems like z87_hw_start() always assumes there is a PHY, or it
errors out. So there is no need for the test.

> +static int z87_hw_start(struct net_device *dev)
> +{

> + res = z87_phy_init(dev);
> + if (res < 0) {
> + netdev_err(dev, "*** PHY Initialization failed!\n");
> + return res;
> + }
> +
> + phy_start(dev->phydev);


> +static int z87_probe(struct mcb_device *mdev,
> + const struct mcb_device_id *id)
> +{
> + const struct board_config *cfg = NULL;
> + struct net_device *dev = NULL;
> + struct z87_private *np = NULL;
> + void *mem_virt_dma = NULL;
> + dma_addr_t mem_hw_dma;
> + struct resource *mem;
> + int res = -ENXIO;
> +
> + cfg = z87_get_board_config(mdev);
> + if (!cfg)
> + return -EOPNOTSUPP;
> +
> + /**
> + * mdev->inst is defined in a per-board basis, so lets check
> + * if there are more devices that defined for the board. In that
> + * case, something is wrong.
> + */
> + if (mdev->inst >= cfg->num_devs) {
> + return dev_err_probe(&mdev->dev, -EINVAL,
> + "detected more devices than configured for board: %s",
> + cfg->board_name);
> + }

Does something actually go wrong if there are more?

> + * if more than one Z87 ethernet core is implemented in one PCI function
> + * on one chameleon FPGA, the device port is important for distinct ethernet
> + * device identification during interface renaming via systemd/udev. We start
> + * counting at 1 to have a constant naming scheme; 0 is omitted during renaming
> + */
> + dev->dev_port = mdev->inst + 1;

This often goes wrong, if the module can be unloaded, or the device
unbound and bound again. We also see LLM generated patches changing
code like this, because in theory, it cover wrap around and then bad
things happened. The fact it will take years does not stop the script
kiddies using the LLM from submitting the patches.

> diff --git a/drivers/net/ethernet/men/z87/men_z87_phy.c b/drivers/net/ethernet/men/z87/men_z87_phy.c
> new file mode 100644
> index 000000000000..355eca9a3409
> --- /dev/null
> +++ b/drivers/net/ethernet/men/z87/men_z87_phy.c
> @@ -0,0 +1,673 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (c) 2026 duagon Germany GmbH
> + *
> + * See MAINTAINERS file for support contact information.
> + */
> +
> +#include <linux/mii.h>
> +#include "men_z87_main.h"
> +#include "men_z87_hw.h"
> +#include "men_z87_phy.h"
> +
> +/* timeout to wait for reply from MII */
> +#define MII_ACCESS_TIMEOUT 10000
> +
> +/* The expected values for a Micrel KSZ8041 PHY in the PHY Identifier Register #2 (PHYIDR2). */
> +#define PHY_ID2_KSZ8041_1 0x1512
> +#define PHY_ID2_KSZ8041_2 0x1513

The MAC driver should not care about what the PHY is. phylib drivers
driver the PHY, the MAC driver should not touch it.

> +static int z87_mdio_read(struct mii_bus *bus, int addr, int regnum)
> +{
> + struct net_device *dev = bus->priv;
> +
> + return __mdio_read(dev, addr, regnum);

Pointless wrapper.

> +static int dp83822_conf_led(struct net_device *dev, u16 val)

Sorry, but no. The PHY driver should export the LEDs via
/sys/class/leds.

> +static void z87_adjust_link(struct net_device *dev)
> +{
> + struct z87_private *np = netdev_priv(dev);
> + struct phy_device *phydev;
> + bool changed;
> + int res;
> +
> + phydev = np->phy.phydev;
> +
> + if (!phydev)
> + return;
> +
> + changed = (np->phy.link != phydev->link);
> +
> + if (phydev->link) {
> + if (changed ||
> + np->phy.speed != phydev->speed ||
> + np->phy.duplex != phydev->duplex) {
> + /*
> + * Reconfigure the MAC when relevant parameters change.
> + */
> + np->phy.speed = phydev->speed;
> + np->phy.duplex = phydev->duplex;
> + res = z87_set_duplex(dev, phydev->duplex);
> + if (res < 0) {
> + netdev_warn(dev, "half duplex not supported. link down\n");

Something is broken if you get asked to do half duplex and it is not
supported.

> +int z87_phy_register_mdio(struct net_device *dev)
> +{
> + struct z87_private *np = netdev_priv(dev);
> + struct mii_bus *bus;
> + int ret;
> +
> + bus = mdiobus_alloc();
> + if (!bus)
> + return -ENOMEM;
> +
> + bus->name = "men-z87-mdio";
> + bus->read = z87_mdio_read;
> + bus->write = z87_mdio_write;
> + bus->parent = np->parent;
> + bus->priv = dev;
> +
> + snprintf(bus->id, MII_BUS_ID_SIZE, "phy-%s", dev_name(np->parent));
> +
> + if (np->phy.phy_addr >= 0 && np->phy.phy_addr < 32)
> + bus->phy_mask = ~BIT(np->phy.phy_addr);

This condition should never happen. If it does, something is broken,
and you probably want to error out.

> +int z87_phy_unregister_mdio(struct net_device *dev)
> +{
> + struct z87_private *np = netdev_priv(dev);
> +
> + if (np->phy.mdiobus) {

Can this be false?

> + mdiobus_unregister(np->phy.mdiobus);
> + mdiobus_free(np->phy.mdiobus);
> + np->phy.mdiobus = NULL;
> + }
> +
> + return 0;
> +}
> +
> +int z87_phy_connect(struct net_device *dev)
> +{
> + struct z87_private *np = netdev_priv(dev);
> + struct phy_device *phydev;
> + int ret;
> +
> + phydev = mdiobus_get_phy(np->phy.mdiobus, np->phy.phy_addr);
> + if (!phydev) {
> + netdev_err(dev, "PHY not found at MDIO address: %d\n",
> + np->phy.phy_addr);
> + return -ENODEV;
> + }
> +
> + ret = phy_connect_direct(dev, phydev,
> + z87_adjust_link,
> + np->phy.phy_interface);
> + if (ret) {
> + netdev_err(dev, "Unable to connect PHY: %d\n", ret);
> + return ret;
> + }
> +
> + np->phy.phydev = phydev;
> +
> + /* Limit to the actual MAC capability */
> + phy_set_max_speed(phydev, SPEED_100);

Do the PHYs actually support > 100Mbps? Generally, this is not
needed. Occasionally you see a Fast Ethernet device using RGMII,
because the PHYs can be cheaper, but it is not common. In that case,
calling phy_set_max_speed() does make sense.

> +int z87_phy_disconnect(struct net_device *dev)
> +{
> + struct z87_private *np = netdev_priv(dev);
> +
> + if (np->phy.phydev) {

Can this be false?

Kernel code generally does not have such tests. If z87_phy_connect()
works, you know you have a PHY, there is no need to do this test. If
z87_phy_connect() fails, z87_phy_disconnect() should never be called.

So we don't like defensive code like this, it hides bugs.

> + /* disable powerdown mode */
> + reg_val = phy_read(dev->phydev, MII_100_BASE_TX_PHY);

The PHY driver should do this. If it does not, something is broken in
the PHY driver.

> +static int z87_phy_reset(struct net_device *dev)
> +{
> + struct z87_private *np = netdev_priv(dev);
> + u8 phy_addr = np->phy.phy_addr;
> + unsigned int wait_cnt = 1000;
> + s16 reg_val;
> + int res;
> +
> + phy_read(dev->phydev, MII_BMCR);
> + z87_read32(dev, Z087_REG_MIISTATUS);

All this code should go. Let phylink/phylib drives the PHY.

> +int z87_phy_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
> +{
> + if (!dev->phydev)
> + return -ENODEV;
> +
> + return phy_mii_ioctl(dev->phydev, ifr, cmd);

There is a helper which does the dev->phydev test. However, is that
even possible?

> +}
> +
> +void z87_phy_regdump(struct net_device *dev)
> +{
> + struct z87_private *np = netdev_priv(dev);
> + const char *link_type;
> + s16 dat = 0;
> +
> + netdev_info(dev, "----- MII Registers -----\n");
> + netdev_info(dev, "instance\t\t\t0x%02x\n", np->instance);
> + netdev_info(dev, "PHY ADR\t\t\t0x%02x\n", np->phy.phy_addr);

With the ioctl handler, you can use miitool(1) to dump the PHY
registers.

Andrew