1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Pvpanic PCI Device Support
4  *
5  *  Copyright (C) 2021 Oracle.
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/types.h>
12 #include <linux/slab.h>
13 
14 #include <uapi/misc/pvpanic.h>
15 
16 #include "pvpanic.h"
17 
18 #define PCI_VENDOR_ID_REDHAT             0x1b36
19 #define PCI_DEVICE_ID_REDHAT_PVPANIC     0x0011
20 
21 MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>");
22 MODULE_DESCRIPTION("pvpanic device driver");
23 MODULE_LICENSE("GPL");
24 
25 static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
26 {
27 	struct pvpanic_instance *pi = dev_get_drvdata(dev);
28 
29 	return sysfs_emit(buf, "%x\n", pi->capability);
30 }
31 static DEVICE_ATTR_RO(capability);
32 
33 static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
34 {
35 	struct pvpanic_instance *pi = dev_get_drvdata(dev);
36 
37 	return sysfs_emit(buf, "%x\n", pi->events);
38 }
39 
40 static ssize_t events_store(struct device *dev, struct device_attribute *attr,
41 			    const char *buf, size_t count)
42 {
43 	struct pvpanic_instance *pi = dev_get_drvdata(dev);
44 	unsigned int tmp;
45 	int err;
46 
47 	err = kstrtouint(buf, 16, &tmp);
48 	if (err)
49 		return err;
50 
51 	if ((tmp & pi->capability) != tmp)
52 		return -EINVAL;
53 
54 	pi->events = tmp;
55 
56 	return count;
57 }
58 static DEVICE_ATTR_RW(events);
59 
60 static struct attribute *pvpanic_pci_dev_attrs[] = {
61 	&dev_attr_capability.attr,
62 	&dev_attr_events.attr,
63 	NULL
64 };
65 ATTRIBUTE_GROUPS(pvpanic_pci_dev);
66 
67 static int pvpanic_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
68 {
69 	struct pvpanic_instance *pi;
70 	void __iomem *base;
71 	int ret;
72 
73 	ret = pcim_enable_device(pdev);
74 	if (ret < 0)
75 		return ret;
76 
77 	base = pcim_iomap(pdev, 0, 0);
78 	if (!base)
79 		return -ENOMEM;
80 
81 	pi = devm_kmalloc(&pdev->dev, sizeof(*pi), GFP_KERNEL);
82 	if (!pi)
83 		return -ENOMEM;
84 
85 	pi->base = base;
86 	pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
87 
88 	/* initlize capability by RDPT */
89 	pi->capability &= ioread8(base);
90 	pi->events = pi->capability;
91 
92 	return devm_pvpanic_probe(&pdev->dev, pi);
93 }
94 
95 static const struct pci_device_id pvpanic_pci_id_tbl[]  = {
96 	{ PCI_DEVICE(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_PVPANIC)},
97 	{}
98 };
99 MODULE_DEVICE_TABLE(pci, pvpanic_pci_id_tbl);
100 
101 static struct pci_driver pvpanic_pci_driver = {
102 	.name =         "pvpanic-pci",
103 	.id_table =     pvpanic_pci_id_tbl,
104 	.probe =        pvpanic_pci_probe,
105 	.driver = {
106 		.dev_groups = pvpanic_pci_dev_groups,
107 	},
108 };
109 module_pci_driver(pvpanic_pci_driver);
110