Skip to content

Commit 89f7a87

Browse files
shayshyiNipaLocal
authored andcommitted
driver core: auxiliary bus: show auxiliary device IRQs
PCI subfunctions (SF) are anchored on the auxiliary bus. PCI physical and virtual functions are anchored on the PCI bus. The irq information of each such function is visible to users via sysfs directory "msi_irqs" containing files for each irq entry. However, for PCI SFs such information is unavailable. Due to this users have no visibility on IRQs used by the SFs. Secondly, an SF can be multi function device supporting rdma, netdevice and more. Without irq information at the bus level, the user is unable to view or use the affinity of the SF IRQs. Hence to match to the equivalent PCI PFs and VFs, add "irqs" directory, for supporting auxiliary devices, containing file for each irq entry. For example: $ ls /sys/bus/auxiliary/devices/mlx5_core.sf.1/irqs/ 50 51 52 53 54 55 56 57 58 Cc: Simon Horman <horms@kernel.org> Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com> Reviewed-by: Parav Pandit <parav@nvidia.com> Signed-off-by: Shay Drory <shayd@nvidia.com> Signed-off-by: NipaLocal <nipa@local>
1 parent 721775d commit 89f7a87

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
What: /sys/bus/auxiliary/devices/.../irqs/
2+
Date: April, 2024
3+
Contact: Shay Drory <shayd@nvidia.com>
4+
Description:
5+
The /sys/devices/.../irqs directory contains a variable set of
6+
files, with each file is named as irq number similar to PCI PF
7+
or VF's irq number located in msi_irqs directory.
8+
These irq files are added and removed dynamically when an IRQ
9+
is requested and freed respectively for the PCI SF.

drivers/base/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ obj-$(CONFIG_NUMA) += node.o
1616
obj-$(CONFIG_MEMORY_HOTPLUG) += memory.o
1717
ifeq ($(CONFIG_SYSFS),y)
1818
obj-$(CONFIG_MODULES) += module.o
19+
obj-$(CONFIG_AUXILIARY_BUS) += auxiliary_sysfs.o
1920
endif
2021
obj-$(CONFIG_SYS_HYPERVISOR) += hypervisor.o
2122
obj-$(CONFIG_REGMAP) += regmap/

drivers/base/auxiliary.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ int auxiliary_device_init(struct auxiliary_device *auxdev)
287287

288288
dev->bus = &auxiliary_bus_type;
289289
device_initialize(&auxdev->dev);
290+
mutex_init(&auxdev->lock);
290291
return 0;
291292
}
292293
EXPORT_SYMBOL_GPL(auxiliary_device_init);

drivers/base/auxiliary_sysfs.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES
4+
*/
5+
6+
#include <linux/auxiliary_bus.h>
7+
#include <linux/slab.h>
8+
9+
struct auxiliary_irq_info {
10+
struct device_attribute sysfs_attr;
11+
};
12+
13+
static struct attribute *auxiliary_irq_attrs[] = {
14+
NULL
15+
};
16+
17+
static const struct attribute_group auxiliary_irqs_group = {
18+
.name = "irqs",
19+
.attrs = auxiliary_irq_attrs,
20+
};
21+
22+
static int auxiliary_irq_dir_prepare(struct auxiliary_device *auxdev)
23+
{
24+
int ret = 0;
25+
26+
guard(mutex)(&auxdev->lock);
27+
if (auxdev->irq_dir_exists)
28+
return 0;
29+
30+
ret = devm_device_add_group(&auxdev->dev, &auxiliary_irqs_group);
31+
if (ret)
32+
return ret;
33+
34+
auxdev->irq_dir_exists = true;
35+
xa_init(&auxdev->irqs);
36+
return 0;
37+
}
38+
39+
/**
40+
* auxiliary_device_sysfs_irq_add - add a sysfs entry for the given IRQ
41+
* @auxdev: auxiliary bus device to add the sysfs entry.
42+
* @irq: The associated interrupt number.
43+
*
44+
* This function should be called after auxiliary device have successfully
45+
* received the irq.
46+
* The driver is responsible to add a unique irq for the auxiliary device. The
47+
* driver can invoke this function from multiple thread context safely for
48+
* unique irqs of the auxiliary devices. The driver must not invoke this API
49+
* multiple times if the irq is already added previously.
50+
*
51+
* Return: zero on success or an error code on failure.
52+
*/
53+
int auxiliary_device_sysfs_irq_add(struct auxiliary_device *auxdev, int irq)
54+
{
55+
struct auxiliary_irq_info *info __free(kfree) = NULL;
56+
struct device *dev = &auxdev->dev;
57+
char *name __free(kfree) = NULL;
58+
int ret;
59+
60+
ret = auxiliary_irq_dir_prepare(auxdev);
61+
if (ret)
62+
return ret;
63+
64+
info = kzalloc(sizeof(*info), GFP_KERNEL);
65+
if (!info)
66+
return -ENOMEM;
67+
68+
sysfs_attr_init(&info->sysfs_attr.attr);
69+
name = kasprintf(GFP_KERNEL, "%d", irq);
70+
if (!name)
71+
return -ENOMEM;
72+
73+
ret = xa_insert(&auxdev->irqs, irq, info, GFP_KERNEL);
74+
if (ret)
75+
return ret;
76+
77+
info->sysfs_attr.attr.name = name;
78+
ret = sysfs_add_file_to_group(&dev->kobj, &info->sysfs_attr.attr,
79+
auxiliary_irqs_group.name);
80+
if (ret)
81+
goto sysfs_add_err;
82+
83+
info->sysfs_attr.attr.name = no_free_ptr(name);
84+
xa_store(&auxdev->irqs, irq, no_free_ptr(info), GFP_KERNEL);
85+
return 0;
86+
87+
sysfs_add_err:
88+
xa_erase(&auxdev->irqs, irq);
89+
return ret;
90+
}
91+
EXPORT_SYMBOL_GPL(auxiliary_device_sysfs_irq_add);
92+
93+
/**
94+
* auxiliary_device_sysfs_irq_remove - remove a sysfs entry for the given IRQ
95+
* @auxdev: auxiliary bus device to add the sysfs entry.
96+
* @irq: the IRQ to remove.
97+
*
98+
* This function should be called to remove an IRQ sysfs entry.
99+
* The driver must invoke this API when IRQ is released by the device.
100+
*/
101+
void auxiliary_device_sysfs_irq_remove(struct auxiliary_device *auxdev, int irq)
102+
{
103+
struct auxiliary_irq_info *info __free(kfree) = xa_load(&auxdev->irqs, irq);
104+
const char *name __free(kfree) = info->sysfs_attr.attr.name;
105+
struct device *dev = &auxdev->dev;
106+
107+
sysfs_remove_file_from_group(&dev->kobj, &info->sysfs_attr.attr,
108+
auxiliary_irqs_group.name);
109+
xa_erase(&auxdev->irqs, irq);
110+
}
111+
EXPORT_SYMBOL_GPL(auxiliary_device_sysfs_irq_remove);

include/linux/auxiliary_bus.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
* in
5959
* @name: Match name found by the auxiliary device driver,
6060
* @id: unique identitier if multiple devices of the same name are exported,
61+
* @irqs: irqs xarray contains irq indices which are used by the device,
62+
* @lock: Synchronize irq sysfs creation,
63+
* @irq_dir_exists: whether "irqs" directory exists,
6164
*
6265
* An auxiliary_device represents a part of its parent device's functionality.
6366
* It is given a name that, combined with the registering drivers
@@ -138,7 +141,10 @@
138141
struct auxiliary_device {
139142
struct device dev;
140143
const char *name;
144+
struct xarray irqs;
145+
struct mutex lock; /* Synchronize irq sysfs creation */
141146
u32 id;
147+
bool irq_dir_exists;
142148
};
143149

144150
/**
@@ -212,8 +218,24 @@ int auxiliary_device_init(struct auxiliary_device *auxdev);
212218
int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname);
213219
#define auxiliary_device_add(auxdev) __auxiliary_device_add(auxdev, KBUILD_MODNAME)
214220

221+
#ifdef CONFIG_SYSFS
222+
int auxiliary_device_sysfs_irq_add(struct auxiliary_device *auxdev, int irq);
223+
void auxiliary_device_sysfs_irq_remove(struct auxiliary_device *auxdev,
224+
int irq);
225+
#else /* CONFIG_SYSFS */
226+
static inline int
227+
auxiliary_device_sysfs_irq_add(struct auxiliary_device *auxdev, int irq)
228+
{
229+
return 0;
230+
}
231+
232+
static inline void
233+
auxiliary_device_sysfs_irq_remove(struct auxiliary_device *auxdev, int irq) {}
234+
#endif
235+
215236
static inline void auxiliary_device_uninit(struct auxiliary_device *auxdev)
216237
{
238+
mutex_destroy(&auxdev->lock);
217239
put_device(&auxdev->dev);
218240
}
219241

0 commit comments

Comments
 (0)