Skip to content

Commit a808878

Browse files
shayshyiSaeed Mahameed
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 <[email protected]> Reviewed-by: Przemek Kitszel <[email protected]> Reviewed-by: Parav Pandit <[email protected]> Reviewed-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Shay Drory <[email protected]> Signed-off-by: Saeed Mahameed <[email protected]> --- v9-v10: - remove Przemek RB - add name field to auxiliary_irq_info (Greg and Przemek) - handle bogus IRQ in auxiliary_device_sysfs_irq_remove (Greg) v8-v9: - add Przemek RB - use guard() in auxiliary_irq_dir_prepare (Paolo) v7-v8: - use cleanup.h for info and name fields (Greg) - correct error flow in auxiliary_irq_dir_prepare (Przemek) - add documentation for new fields of auxiliary_device (Simon) v6-v7: - dynamically creating irqs directory when first irq file created (Greg) - removed irqs flag and simplified the dev_add() API (Greg) - move sysfs related new code to a new auxiliary_sysfs.c file (Greg) v5-v6: - removed concept of shared and exclusive and hence global xarray (Greg) v4-v5: - restore global mutex and replace refcount_t with simple integer (Greg) v3->4: - remove global mutex (Przemek) v2->v3: - fix function declaration in case SYSFS isn't defined v1->v2: - move #ifdefs from drivers/base/auxiliary.c to include/linux/auxiliary_bus.h (Greg) - use EXPORT_SYMBOL_GPL instead of EXPORT_SYMBOL (Greg) - Fix kzalloc(ref) to kzalloc(*ref) (Simon) - Add return description in auxiliary_device_sysfs_irq_add() kdoc (Simon) - Fix auxiliary_irq_mode_show doc (kernel test boot)
1 parent b339e0a commit a808878

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-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 <[email protected]>
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->sysfs.lock);
290291
return 0;
291292
}
292293
EXPORT_SYMBOL_GPL(auxiliary_device_init);

drivers/base/auxiliary_sysfs.c

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

include/linux/auxiliary_bus.h

Lines changed: 24 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
@@ -139,6 +142,11 @@ struct auxiliary_device {
139142
struct device dev;
140143
const char *name;
141144
u32 id;
145+
struct {
146+
struct xarray irqs;
147+
struct mutex lock; /* Synchronize irq sysfs creation */
148+
bool irq_dir_exists;
149+
} sysfs;
142150
};
143151

144152
/**
@@ -212,8 +220,24 @@ int auxiliary_device_init(struct auxiliary_device *auxdev);
212220
int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname);
213221
#define auxiliary_device_add(auxdev) __auxiliary_device_add(auxdev, KBUILD_MODNAME)
214222

223+
#ifdef CONFIG_SYSFS
224+
int auxiliary_device_sysfs_irq_add(struct auxiliary_device *auxdev, int irq);
225+
void auxiliary_device_sysfs_irq_remove(struct auxiliary_device *auxdev,
226+
int irq);
227+
#else /* CONFIG_SYSFS */
228+
static inline int
229+
auxiliary_device_sysfs_irq_add(struct auxiliary_device *auxdev, int irq)
230+
{
231+
return 0;
232+
}
233+
234+
static inline void
235+
auxiliary_device_sysfs_irq_remove(struct auxiliary_device *auxdev, int irq) {}
236+
#endif
237+
215238
static inline void auxiliary_device_uninit(struct auxiliary_device *auxdev)
216239
{
240+
mutex_destroy(&auxdev->sysfs.lock);
217241
put_device(&auxdev->dev);
218242
}
219243

0 commit comments

Comments
 (0)