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 const struct pci_device_id pvpanic_pci_id_tbl[]  = {
26 	{ PCI_DEVICE(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_PVPANIC)},
27 	{}
28 };
29 
30 static ssize_t capability_show(struct device *dev,
31 			       struct device_attribute *attr, char *buf)
32 {
33 	struct pvpanic_instance *pi = dev_get_drvdata(dev);
34 
35 	return sysfs_emit(buf, "%x\n", pi->capability);
36 }
37 static DEVICE_ATTR_RO(capability);
38 
39 static ssize_t events_show(struct device *dev,  struct device_attribute *attr, char *buf)
40 {
41 	struct pvpanic_instance *pi = dev_get_drvdata(dev);
42 
43 	return sysfs_emit(buf, "%x\n", pi->events);
44 }
45 
46 static ssize_t events_store(struct device *dev,  struct device_attribute *attr,
47 			    const char *buf, size_t count)
48 {
49 	struct pvpanic_instance *pi = dev_get_drvdata(dev);
50 	unsigned int tmp;
51 	int err;
52 
53 	err = kstrtouint(buf, 16, &tmp);
54 	if (err)
55 		return err;
56 
57 	if ((tmp & pi->capability) != tmp)
58 		return -EINVAL;
59 
60 	pi->events = tmp;
61 
62 	return count;
63 }
64 static DEVICE_ATTR_RW(events);
65 
66 static struct attribute *pvpanic_pci_dev_attrs[] = {
67 	&dev_attr_capability.attr,
68 	&dev_attr_events.attr,
69 	NULL
70 };
71 ATTRIBUTE_GROUPS(pvpanic_pci_dev);
72 
73 static int pvpanic_pci_probe(struct pci_dev *pdev,
74 			     const struct pci_device_id *ent)
75 {
76 	struct pvpanic_instance *pi;
77 	void __iomem *base;
78 	int ret;
79 
80 	ret = pcim_enable_device(pdev);
81 	if (ret < 0)
82 		return ret;
83 
84 	base = pcim_iomap(pdev, 0, 0);
85 	if (!base)
86 		return -ENOMEM;
87 
88 	pi = devm_kmalloc(&pdev->dev, sizeof(*pi), GFP_KERNEL);
89 	if (!pi)
90 		return -ENOMEM;
91 
92 	pi->base = base;
93 	pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
94 
95 	/* initlize capability by RDPT */
96 	pi->capability &= ioread8(base);
97 	pi->events = pi->capability;
98 
99 	return devm_pvpanic_probe(&pdev->dev, pi);
100 }
101 
102 static struct pci_driver pvpanic_pci_driver = {
103 	.name =         "pvpanic-pci",
104 	.id_table =     pvpanic_pci_id_tbl,
105 	.probe =        pvpanic_pci_probe,
106 	.driver = {
107 		.dev_groups = pvpanic_pci_dev_groups,
108 	},
109 };
110 
111 MODULE_DEVICE_TABLE(pci, pvpanic_pci_id_tbl);
112 
113 module_pci_driver(pvpanic_pci_driver);
114