xref: /linux/drivers/misc/pvpanic/pvpanic-pci.c (revision 172cdcaefea5c297fdb3d20b7d5aff60ae4fbce6)
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 device *dev = &pdev->dev;
77 	struct pvpanic_instance *pi;
78 	void __iomem *base;
79 	int ret;
80 
81 	ret = pci_enable_device(pdev);
82 	if (ret < 0)
83 		return ret;
84 
85 	base = pci_iomap(pdev, 0, 0);
86 	if (!base)
87 		return -ENOMEM;
88 
89 	pi = kmalloc(sizeof(*pi), GFP_ATOMIC);
90 	if (!pi)
91 		return -ENOMEM;
92 
93 	pi->base = base;
94 	pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
95 
96 	/* initlize capability by RDPT */
97 	pi->capability &= ioread8(base);
98 	pi->events = pi->capability;
99 
100 	dev_set_drvdata(dev, pi);
101 
102 	return pvpanic_probe(pi);
103 }
104 
105 static void pvpanic_pci_remove(struct pci_dev *pdev)
106 {
107 	struct pvpanic_instance *pi = dev_get_drvdata(&pdev->dev);
108 
109 	pvpanic_remove(pi);
110 	iounmap(pi->base);
111 	kfree(pi);
112 	pci_disable_device(pdev);
113 }
114 
115 static struct pci_driver pvpanic_pci_driver = {
116 	.name =         "pvpanic-pci",
117 	.id_table =     pvpanic_pci_id_tbl,
118 	.probe =        pvpanic_pci_probe,
119 	.remove =       pvpanic_pci_remove,
120 	.driver = {
121 		.dev_groups = pvpanic_pci_dev_groups,
122 	},
123 };
124 
125 module_pci_driver(pvpanic_pci_driver);
126