blob: 07eddb5ea30fa14bd84e508b7cd7020236f43e21 [file] [log] [blame]
Mihai Carabasdb3a4f02021-03-24 16:49:16 +02001// 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
21MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>");
Andy Shevchenko84b0f122021-08-29 15:43:54 +030022MODULE_DESCRIPTION("pvpanic device driver");
Mihai Carabasdb3a4f02021-03-24 16:49:16 +020023MODULE_LICENSE("GPL");
24
Andy Shevchenko84b0f122021-08-29 15:43:54 +030025static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
Mihai Carabasdb3a4f02021-03-24 16:49:16 +020026{
27 struct pvpanic_instance *pi = dev_get_drvdata(dev);
28
29 return sysfs_emit(buf, "%x\n", pi->capability);
30}
31static DEVICE_ATTR_RO(capability);
32
Andy Shevchenko84b0f122021-08-29 15:43:54 +030033static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
Mihai Carabasdb3a4f02021-03-24 16:49:16 +020034{
35 struct pvpanic_instance *pi = dev_get_drvdata(dev);
36
37 return sysfs_emit(buf, "%x\n", pi->events);
38}
39
Andy Shevchenko84b0f122021-08-29 15:43:54 +030040static ssize_t events_store(struct device *dev, struct device_attribute *attr,
Mihai Carabasdb3a4f02021-03-24 16:49:16 +020041 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}
58static DEVICE_ATTR_RW(events);
59
60static struct attribute *pvpanic_pci_dev_attrs[] = {
61 &dev_attr_capability.attr,
62 &dev_attr_events.attr,
63 NULL
64};
65ATTRIBUTE_GROUPS(pvpanic_pci_dev);
66
Andy Shevchenko84b0f122021-08-29 15:43:54 +030067static int pvpanic_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Mihai Carabasdb3a4f02021-03-24 16:49:16 +020068{
Mihai Carabasdb3a4f02021-03-24 16:49:16 +020069 struct pvpanic_instance *pi;
70 void __iomem *base;
71 int ret;
72
Christophe JAILLET372dae82021-05-22 08:54:33 +020073 ret = pcim_enable_device(pdev);
Mihai Carabasdb3a4f02021-03-24 16:49:16 +020074 if (ret < 0)
75 return ret;
76
Christophe JAILLET372dae82021-05-22 08:54:33 +020077 base = pcim_iomap(pdev, 0, 0);
Qiheng Lin642fa282021-03-30 09:36:59 +080078 if (!base)
79 return -ENOMEM;
Mihai Carabasdb3a4f02021-03-24 16:49:16 +020080
Christophe JAILLETb647ceb2021-05-22 08:54:52 +020081 pi = devm_kmalloc(&pdev->dev, sizeof(*pi), GFP_KERNEL);
Mihai Carabasdb3a4f02021-03-24 16:49:16 +020082 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
Christophe JAILLET394febc2021-05-22 08:55:20 +020092 return devm_pvpanic_probe(&pdev->dev, pi);
Mihai Carabasdb3a4f02021-03-24 16:49:16 +020093}
94
Andy Shevchenko33a43042021-08-29 15:43:52 +030095static const struct pci_device_id pvpanic_pci_id_tbl[] = {
96 { PCI_DEVICE(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_PVPANIC)},
97 {}
98};
99MODULE_DEVICE_TABLE(pci, pvpanic_pci_id_tbl);
100
Mihai Carabasdb3a4f02021-03-24 16:49:16 +0200101static struct pci_driver pvpanic_pci_driver = {
102 .name = "pvpanic-pci",
103 .id_table = pvpanic_pci_id_tbl,
104 .probe = pvpanic_pci_probe,
Mihai Carabasdb3a4f02021-03-24 16:49:16 +0200105 .driver = {
106 .dev_groups = pvpanic_pci_dev_groups,
107 },
108};
Mihai Carabasdb3a4f02021-03-24 16:49:16 +0200109module_pci_driver(pvpanic_pci_driver);