Re: [PATCH v2 7/7] hwmon: (marvel-env) Notify userspace of environmental events

From: Magnus Lindholm

Date: Tue Nov 17 2026 - 06:31:06 EST


Hi Matt,

On Tue, Nov 17, 2026 at 3:59 AM Matt Turner <mattst88@xxxxxxxxx> wrote:
>
> The firmware reports each change of a sensor's state as a system event
> machine check. On one, reread every node from a workqueue and notify
> userspace of each alarm attribute whose value changed. The notifier runs
> in machine check context, so it only queues an irq_work, which queues
> the work. The work takes the hwmon device lock to serialize the refresh
> with the sysfs callbacks.
>
> Signed-off-by: Matt Turner <mattst88@xxxxxxxxx>
> ---
> Documentation/hwmon/marvel-env.rst | 4 ++
> drivers/hwmon/Kconfig | 1 +
> drivers/hwmon/marvel-env.c | 89 ++++++++++++++++++++++++++++++++++++--
> 3 files changed, 90 insertions(+), 4 deletions(-)
>
> diff --git a/Documentation/hwmon/marvel-env.rst b/Documentation/hwmon/marvel-env.rst
> index 7e0457a60094..ae3edda557ab 100644
> --- a/Documentation/hwmon/marvel-env.rst
> +++ b/Documentation/hwmon/marvel-env.rst
> @@ -27,6 +27,10 @@ A refresh keeps the boot CPU in the console with interrupts disabled for up
> to 300 ms per unit, so a unit is refreshed at most once every
> ``update_interval`` milliseconds (10000 by default).
>
> +The firmware also reports every change of state as a system event machine
> +check. When one arrives, the driver refreshes all units and notifies
> +userspace through the alarm attributes.
> +
> Sysfs entries
> -------------
>
> diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
> index c0ce5591c82d..99c5d84c8752 100644
> --- a/drivers/hwmon/Kconfig
> +++ b/drivers/hwmon/Kconfig
> @@ -1258,6 +1258,7 @@ config SENSORS_MACSMC_HWMON
> config SENSORS_MARVEL_ENV
> tristate "Marvel (EV7) AlphaServer environmental sensors"
> depends on ALPHA_MARVEL || (ALPHA_GENERIC && !ALPHA_LEGACY_START_ADDRESS)
> + select IRQ_WORK
> help
> If you say yes here you get support for the temperature, fan,
> voltage and power supply sensors of Marvel AlphaServers (the
> diff --git a/drivers/hwmon/marvel-env.c b/drivers/hwmon/marvel-env.c
> index e501e049d702..d319556929e3 100644
> --- a/drivers/hwmon/marvel-env.c
> +++ b/drivers/hwmon/marvel-env.c
> @@ -17,12 +17,15 @@
> */
>
> #include <linux/hwmon.h>
> +#include <linux/irq_work.h>
> #include <linux/jiffies.h>
> #include <linux/kernel.h>
> #include <linux/module.h>
> +#include <linux/notifier.h>
> #include <linux/platform_device.h>
> #include <linux/slab.h>
> #include <linux/string.h>
> +#include <linux/workqueue.h>
>
> #include <asm/gct.h>
> #include <asm/marvel_env.h>
> @@ -94,16 +97,21 @@ struct marvel_env_chan {
> unsigned int pkt; /* packet offset in the node */
> bool ps; /* power supply, as an in channel */
> bool present; /* known when the driver loaded */
> + u32 alarms; /* alarm attributes last reported */
> char label[32];
> };
>
> struct marvel_env_data {
> struct device *dev;
> + struct device *hwmon;
> unsigned int update_interval; /* ms */
> struct marvel_env_node *nodes;
> unsigned int nr_nodes;
> struct marvel_env_chan *chans[hwmon_max];
> unsigned int nr_chans[hwmon_max];
> + struct irq_work irq_work;
> + struct work_struct work;
> + struct notifier_block nb;
> };
>
> static const char * const pe_names[] = {
> @@ -290,6 +298,7 @@ static void marvel_env_add_chan(struct marvel_env_data *data,
> c->ps = ps;
> status = marvel_env_status(type, c);
> c->present = env_status_valid(status);
> + c->alarms = marvel_env_alarms(type, c, status);
>
> if (n->cabinet || n->drawer)
> len = scnprintf(c->label, sizeof(c->label), "Cab%u Dra%u ",
> @@ -512,6 +521,72 @@ static u32 marvel_env_config(enum hwmon_sensor_types type,
> }
> }
>
> +/* Reread everything after a system event and report changed alarms. */
> +static void marvel_env_event_work(struct work_struct *work)
> +{
> + struct marvel_env_data *data =
> + container_of(work, struct marvel_env_data, work);
> + unsigned int i, k;
> +
> + guard(hwmon_lock)(data->hwmon);
> +
> + for (i = 0; i < data->nr_nodes; i++) {
> + data->nodes[i].valid = false;
> + marvel_env_refresh(data, &data->nodes[i]);
> + }
> +
> + for (k = 0; k < ARRAY_SIZE(marvel_env_types); k++) {
> + enum hwmon_sensor_types type = marvel_env_types[k];
> +
> + for (i = 0; i < data->nr_chans[type]; i++) {
> + struct marvel_env_chan *c = &data->chans[type][i];
> + unsigned long changed;
> + unsigned int attr;
> + u8 status;
> + u32 alarms;
> +
> + if (!c->present)
> + continue;
> + status = marvel_env_status(type, c);
> + if (!env_status_valid(status))
> + continue;
> + alarms = marvel_env_alarms(type, c, status);
> + changed = alarms ^ c->alarms;
> + c->alarms = alarms;
> + for_each_set_bit(attr, &changed, 32)
> + hwmon_notify_event(data->hwmon, type, attr, i);
> + }
> + }
> +}
> +

This covers crit_alarm, fan_fault and the supplies' in_fault, and the
supply test on the ES47 exercises it end to end. Thanks.

> +static void marvel_env_irq_work(struct irq_work *irq_work)
> +{
> + struct marvel_env_data *data =
> + container_of(irq_work, struct marvel_env_data, irq_work);
> +
> + schedule_work(&data->work);
> +}
> +
> +/* Runs in machine check context, so only queue the irq_work. */
> +static int marvel_env_event(struct notifier_block *nb, unsigned long action,
> + void *unused)
> +{
> + struct marvel_env_data *data =
> + container_of(nb, struct marvel_env_data, nb);
> +
> + irq_work_queue(&data->irq_work);
> + return NOTIFY_OK;
> +}
> +
> +static void marvel_env_unregister(void *arg)
> +{
> + struct marvel_env_data *data = arg;
> +
> + atomic_notifier_chain_unregister(&marvel_env_notifier, &data->nb);
> + irq_work_sync(&data->irq_work);
> + cancel_work_sync(&data->work);
> +}
> +
> static const struct hwmon_channel_info * const marvel_env_chip_info =
> HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL);
>
> @@ -522,7 +597,6 @@ static int marvel_env_probe(struct platform_device *pdev)
> const struct hwmon_channel_info **info;
> struct marvel_env_data *data;
> struct hwmon_chip_info *chip;
> - struct device *hwmon;
> unsigned int i, k, nr_info = 0;
>
> data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
> @@ -591,9 +665,16 @@ static int marvel_env_probe(struct platform_device *pdev)
> chip->ops = &marvel_env_ops;
> chip->info = info;
>
> - hwmon = devm_hwmon_device_register_with_info(dev, DRVNAME, data, chip,
> - NULL);
> - return PTR_ERR_OR_ZERO(hwmon);
> + data->hwmon = devm_hwmon_device_register_with_info(dev, DRVNAME, data,
> + chip, NULL);
> + if (IS_ERR(data->hwmon))
> + return PTR_ERR(data->hwmon);
> +
> + init_irq_work(&data->irq_work, marvel_env_irq_work);
> + INIT_WORK(&data->work, marvel_env_event_work);
> + data->nb.notifier_call = marvel_env_event;
> + atomic_notifier_chain_register(&marvel_env_notifier, &data->nb);
> + return devm_add_action_or_reset(dev, marvel_env_unregister, data);
> }
>
> static struct platform_driver marvel_env_driver = {
>
> --
> 2.56.1
>

Reviewed-by: Magnus Lindholm <linmag7@xxxxxxxxx>