drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c

Source file repositories/reference/linux-study-clean/drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c

File Facts

System
Linux kernel
Corpus path
drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c
Extension
.c
Size
1476 bytes
Lines
62
Domain
Driver Families
Bucket
drivers/net
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
/* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */

#include <linux/mlx5/driver.h>
#include <net/devlink.h>

#include "sh_devlink.h"

static const struct devlink_ops mlx5_shd_ops = {
};

int mlx5_shd_init(struct mlx5_core_dev *dev)
{
	u8 *vpd_data __free(kfree) = NULL;
	struct pci_dev *pdev = dev->pdev;
	unsigned int vpd_size, kw_len;
	struct devlink *devlink;
	char *sn, *end;
	int start;
	int err;

	if (!mlx5_core_is_pf(dev))
		return 0;

	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
	if (IS_ERR(vpd_data)) {
		err = PTR_ERR(vpd_data);
		return err == -ENODEV ? 0 : err;
	}
	start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, "V3", &kw_len);
	if (start < 0) {
		/* Fall-back to SN for older devices. */
		start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
						     PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);
		if (start < 0)
			return 0; /* No usable serial number found, ignore. */
	}
	sn = kstrndup(vpd_data + start, kw_len, GFP_KERNEL);
	if (!sn)
		return -ENOMEM;
	/* Firmware may return spaces at the end of the string, strip it. */
	end = strchrnul(sn, ' ');
	*end = '\0';

	/* Get or create shared devlink instance */
	devlink = devlink_shd_get(sn, &mlx5_shd_ops, 0, pdev->dev.driver);
	kfree(sn);
	if (!devlink)
		return -ENOMEM;

	dev->shd = devlink;
	return 0;
}

void mlx5_shd_uninit(struct mlx5_core_dev *dev)
{
	if (!dev->shd)
		return;

	devlink_shd_put(dev->shd);
}

Annotation

Implementation Notes