xref: /linux/drivers/net/ethernet/intel/ixgbe/devlink/devlink.c (revision a0285236ab93fdfdd1008afaa04561d142d6c276)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2025, Intel Corporation. */
3 
4 #include "ixgbe.h"
5 #include "devlink.h"
6 
7 static const struct devlink_ops ixgbe_devlink_ops = {
8 };
9 
10 /**
11  * ixgbe_allocate_devlink - Allocate devlink instance
12  * @dev: device to allocate devlink for
13  *
14  * Allocate a devlink instance for this physical function.
15  *
16  * Return: pointer to the device adapter structure on success,
17  * ERR_PTR(-ENOMEM) when allocation failed.
18  */
19 struct ixgbe_adapter *ixgbe_allocate_devlink(struct device *dev)
20 {
21 	struct ixgbe_adapter *adapter;
22 	struct devlink *devlink;
23 
24 	devlink = devlink_alloc(&ixgbe_devlink_ops, sizeof(*adapter), dev);
25 	if (!devlink)
26 		return ERR_PTR(-ENOMEM);
27 
28 	adapter = devlink_priv(devlink);
29 	adapter->devlink = devlink;
30 
31 	return adapter;
32 }
33 
34 /**
35  * ixgbe_devlink_set_switch_id - Set unique switch ID based on PCI DSN
36  * @adapter: pointer to the device adapter structure
37  * @ppid: struct with switch id information
38  */
39 static void ixgbe_devlink_set_switch_id(struct ixgbe_adapter *adapter,
40 					struct netdev_phys_item_id *ppid)
41 {
42 	u64 id = pci_get_dsn(adapter->pdev);
43 
44 	ppid->id_len = sizeof(id);
45 	put_unaligned_be64(id, &ppid->id);
46 }
47 
48 /**
49  * ixgbe_devlink_register_port - Register devlink port
50  * @adapter: pointer to the device adapter structure
51  *
52  * Create and register a devlink_port for this physical function.
53  *
54  * Return: 0 on success, error code on failure.
55  */
56 int ixgbe_devlink_register_port(struct ixgbe_adapter *adapter)
57 {
58 	struct devlink_port *devlink_port = &adapter->devlink_port;
59 	struct devlink *devlink = adapter->devlink;
60 	struct device *dev = &adapter->pdev->dev;
61 	struct devlink_port_attrs attrs = {};
62 	int err;
63 
64 	attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
65 	attrs.phys.port_number = adapter->hw.bus.func;
66 	ixgbe_devlink_set_switch_id(adapter, &attrs.switch_id);
67 
68 	devlink_port_attrs_set(devlink_port, &attrs);
69 
70 	err = devl_port_register(devlink, devlink_port, 0);
71 	if (err) {
72 		dev_err(dev,
73 			"devlink port registration failed, err %d\n", err);
74 	}
75 
76 	return err;
77 }
78