Commit c1426d39 authored by Thomas Weißschuh's avatar Thomas Weißschuh Committed by Greg Kroah-Hartman

misc/pvpanic: deduplicate common code

pvpanic-mmio.c and pvpanic-pci.c share a lot of code.
Refactor it into pvpanic.c where it doesn't have to be kept in sync
manually and where the core logic can be understood more easily.

No functional change.
Signed-off-by: default avatarThomas Weißschuh <linux@weissschuh.net>
Link: https://lore.kernel.org/r/20231011-pvpanic-cleanup-v2-1-4b21d56f779f@weissschuh.netSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 67237183
...@@ -24,52 +24,9 @@ MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>"); ...@@ -24,52 +24,9 @@ MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>");
MODULE_DESCRIPTION("pvpanic-mmio device driver"); MODULE_DESCRIPTION("pvpanic-mmio device driver");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pvpanic_instance *pi = dev_get_drvdata(dev);
return sysfs_emit(buf, "%x\n", pi->capability);
}
static DEVICE_ATTR_RO(capability);
static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pvpanic_instance *pi = dev_get_drvdata(dev);
return sysfs_emit(buf, "%x\n", pi->events);
}
static ssize_t events_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct pvpanic_instance *pi = dev_get_drvdata(dev);
unsigned int tmp;
int err;
err = kstrtouint(buf, 16, &tmp);
if (err)
return err;
if ((tmp & pi->capability) != tmp)
return -EINVAL;
pi->events = tmp;
return count;
}
static DEVICE_ATTR_RW(events);
static struct attribute *pvpanic_mmio_dev_attrs[] = {
&dev_attr_capability.attr,
&dev_attr_events.attr,
NULL
};
ATTRIBUTE_GROUPS(pvpanic_mmio_dev);
static int pvpanic_mmio_probe(struct platform_device *pdev) static int pvpanic_mmio_probe(struct platform_device *pdev)
{ {
struct device *dev = &pdev->dev; struct device *dev = &pdev->dev;
struct pvpanic_instance *pi;
struct resource *res; struct resource *res;
void __iomem *base; void __iomem *base;
...@@ -92,18 +49,7 @@ static int pvpanic_mmio_probe(struct platform_device *pdev) ...@@ -92,18 +49,7 @@ static int pvpanic_mmio_probe(struct platform_device *pdev)
return -EINVAL; return -EINVAL;
} }
pi = devm_kmalloc(dev, sizeof(*pi), GFP_KERNEL); return devm_pvpanic_probe(dev, base);
if (!pi)
return -ENOMEM;
pi->base = base;
pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
/* initialize capability by RDPT */
pi->capability &= ioread8(base);
pi->events = pi->capability;
return devm_pvpanic_probe(dev, pi);
} }
static const struct of_device_id pvpanic_mmio_match[] = { static const struct of_device_id pvpanic_mmio_match[] = {
...@@ -123,7 +69,7 @@ static struct platform_driver pvpanic_mmio_driver = { ...@@ -123,7 +69,7 @@ static struct platform_driver pvpanic_mmio_driver = {
.name = "pvpanic-mmio", .name = "pvpanic-mmio",
.of_match_table = pvpanic_mmio_match, .of_match_table = pvpanic_mmio_match,
.acpi_match_table = pvpanic_device_ids, .acpi_match_table = pvpanic_device_ids,
.dev_groups = pvpanic_mmio_dev_groups, .dev_groups = pvpanic_dev_groups,
}, },
.probe = pvpanic_mmio_probe, .probe = pvpanic_mmio_probe,
}; };
......
...@@ -22,51 +22,8 @@ MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>"); ...@@ -22,51 +22,8 @@ MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>");
MODULE_DESCRIPTION("pvpanic device driver"); MODULE_DESCRIPTION("pvpanic device driver");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pvpanic_instance *pi = dev_get_drvdata(dev);
return sysfs_emit(buf, "%x\n", pi->capability);
}
static DEVICE_ATTR_RO(capability);
static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pvpanic_instance *pi = dev_get_drvdata(dev);
return sysfs_emit(buf, "%x\n", pi->events);
}
static ssize_t events_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct pvpanic_instance *pi = dev_get_drvdata(dev);
unsigned int tmp;
int err;
err = kstrtouint(buf, 16, &tmp);
if (err)
return err;
if ((tmp & pi->capability) != tmp)
return -EINVAL;
pi->events = tmp;
return count;
}
static DEVICE_ATTR_RW(events);
static struct attribute *pvpanic_pci_dev_attrs[] = {
&dev_attr_capability.attr,
&dev_attr_events.attr,
NULL
};
ATTRIBUTE_GROUPS(pvpanic_pci_dev);
static int pvpanic_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) static int pvpanic_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{ {
struct pvpanic_instance *pi;
void __iomem *base; void __iomem *base;
int ret; int ret;
...@@ -78,18 +35,7 @@ static int pvpanic_pci_probe(struct pci_dev *pdev, const struct pci_device_id *e ...@@ -78,18 +35,7 @@ static int pvpanic_pci_probe(struct pci_dev *pdev, const struct pci_device_id *e
if (!base) if (!base)
return -ENOMEM; return -ENOMEM;
pi = devm_kmalloc(&pdev->dev, sizeof(*pi), GFP_KERNEL); return devm_pvpanic_probe(&pdev->dev, base);
if (!pi)
return -ENOMEM;
pi->base = base;
pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
/* initlize capability by RDPT */
pi->capability &= ioread8(base);
pi->events = pi->capability;
return devm_pvpanic_probe(&pdev->dev, pi);
} }
static const struct pci_device_id pvpanic_pci_id_tbl[] = { static const struct pci_device_id pvpanic_pci_id_tbl[] = {
...@@ -103,7 +49,7 @@ static struct pci_driver pvpanic_pci_driver = { ...@@ -103,7 +49,7 @@ static struct pci_driver pvpanic_pci_driver = {
.id_table = pvpanic_pci_id_tbl, .id_table = pvpanic_pci_id_tbl,
.probe = pvpanic_pci_probe, .probe = pvpanic_pci_probe,
.driver = { .driver = {
.dev_groups = pvpanic_pci_dev_groups, .dev_groups = pvpanic_dev_groups,
}, },
}; };
module_pci_driver(pvpanic_pci_driver); module_pci_driver(pvpanic_pci_driver);
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
* Copyright (C) 2021 Oracle. * Copyright (C) 2021 Oracle.
*/ */
#include <linux/device.h>
#include <linux/io.h> #include <linux/io.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/kexec.h> #include <linux/kexec.h>
...@@ -26,6 +27,13 @@ MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>"); ...@@ -26,6 +27,13 @@ MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>");
MODULE_DESCRIPTION("pvpanic device driver"); MODULE_DESCRIPTION("pvpanic device driver");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
struct pvpanic_instance {
void __iomem *base;
unsigned int capability;
unsigned int events;
struct list_head list;
};
static struct list_head pvpanic_list; static struct list_head pvpanic_list;
static spinlock_t pvpanic_lock; static spinlock_t pvpanic_lock;
...@@ -81,11 +89,75 @@ static void pvpanic_remove(void *param) ...@@ -81,11 +89,75 @@ static void pvpanic_remove(void *param)
spin_unlock(&pvpanic_lock); spin_unlock(&pvpanic_lock);
} }
int devm_pvpanic_probe(struct device *dev, struct pvpanic_instance *pi) static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pvpanic_instance *pi = dev_get_drvdata(dev);
return sysfs_emit(buf, "%x\n", pi->capability);
}
static DEVICE_ATTR_RO(capability);
static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct pvpanic_instance *pi = dev_get_drvdata(dev);
return sysfs_emit(buf, "%x\n", pi->events);
}
static ssize_t events_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct pvpanic_instance *pi = dev_get_drvdata(dev);
unsigned int tmp;
int err;
err = kstrtouint(buf, 16, &tmp);
if (err)
return err;
if ((tmp & pi->capability) != tmp)
return -EINVAL;
pi->events = tmp;
return count;
}
static DEVICE_ATTR_RW(events);
static struct attribute *pvpanic_dev_attrs[] = {
&dev_attr_capability.attr,
&dev_attr_events.attr,
NULL
};
static const struct attribute_group pvpanic_dev_group = {
.attrs = pvpanic_dev_attrs,
};
const struct attribute_group *pvpanic_dev_groups[] = {
&pvpanic_dev_group,
NULL
};
EXPORT_SYMBOL_GPL(pvpanic_dev_groups);
int devm_pvpanic_probe(struct device *dev, void __iomem *base)
{ {
if (!pi || !pi->base) struct pvpanic_instance *pi;
if (!base)
return -EINVAL; return -EINVAL;
pi = devm_kmalloc(dev, sizeof(*pi), GFP_KERNEL);
if (!pi)
return -ENOMEM;
pi->base = base;
pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
/* initlize capability by RDPT */
pi->capability &= ioread8(base);
pi->events = pi->capability;
spin_lock(&pvpanic_lock); spin_lock(&pvpanic_lock);
list_add(&pi->list, &pvpanic_list); list_add(&pi->list, &pvpanic_list);
spin_unlock(&pvpanic_lock); spin_unlock(&pvpanic_lock);
......
...@@ -8,13 +8,7 @@ ...@@ -8,13 +8,7 @@
#ifndef PVPANIC_H_ #ifndef PVPANIC_H_
#define PVPANIC_H_ #define PVPANIC_H_
struct pvpanic_instance { int devm_pvpanic_probe(struct device *dev, void __iomem *base);
void __iomem *base; extern const struct attribute_group *pvpanic_dev_groups[];
unsigned int capability;
unsigned int events;
struct list_head list;
};
int devm_pvpanic_probe(struct device *dev, struct pvpanic_instance *pi);
#endif /* PVPANIC_H_ */ #endif /* PVPANIC_H_ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment