summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/mellanox/mlx5/core/sh_devlink.c
blob: b925364765ac27911b2ef42e36aaf28ff5323eb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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);
}