mirror of
https://github.com/gnif/vendor-reset.git
synced 2025-12-26 22:09:28 +01:00
[core] refactored to centralize the lookup and reset code
This commit is contained in:
parent
edda7f02a5
commit
cacf13399f
@ -1,2 +1,7 @@
|
|||||||
vendor-reset-y += src/vendor-reset.o src/ftrace.o src/hooks.o
|
vendor-reset-y += \
|
||||||
|
src/vendor-reset.o \
|
||||||
|
src/vendor-reset-dev.o\
|
||||||
|
src/ftrace.o \
|
||||||
|
src/hooks.o
|
||||||
|
|
||||||
ccflags-y += -I$(src)/src
|
ccflags-y += -I$(src)/src
|
||||||
@ -62,7 +62,6 @@ static int vega20_baco_set_state(struct amd_fake_dev *adev, enum BACO_STATE stat
|
|||||||
uint32_t data;
|
uint32_t data;
|
||||||
|
|
||||||
vega20_baco_get_state(adev, &cur_state);
|
vega20_baco_get_state(adev, &cur_state);
|
||||||
|
|
||||||
if (cur_state == state)
|
if (cur_state == state)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|||||||
28
src/hooks.c
28
src/hooks.c
@ -20,7 +20,6 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
#include <linux/kernel.h>
|
#include <linux/kernel.h>
|
||||||
#include <linux/pci.h>
|
#include <linux/pci.h>
|
||||||
#include "vendor-reset-dev.h"
|
#include "vendor-reset-dev.h"
|
||||||
#include "device-db.h"
|
|
||||||
#include "ftrace.h"
|
#include "ftrace.h"
|
||||||
#include "hooks.h"
|
#include "hooks.h"
|
||||||
|
|
||||||
@ -30,35 +29,16 @@ static int hooked_pci_dev_specific_reset(struct pci_dev *dev, int probe)
|
|||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
struct vendor_reset_cfg *cfg;
|
struct vendor_reset_cfg *cfg;
|
||||||
struct vendor_reset_dev vdev = {0};
|
|
||||||
|
|
||||||
ret = orig_pci_dev_specific_reset(dev, probe);
|
ret = orig_pci_dev_specific_reset(dev, probe);
|
||||||
if (!ret || ret != -ENOTTY)
|
if (!ret || ret != -ENOTTY)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
for (cfg = vendor_reset_devices; cfg->vendor; ++cfg)
|
cfg = vendor_reset_cfg_find(dev->vendor, dev->device);
|
||||||
if ((cfg->vendor == dev->vendor || cfg->vendor == PCI_ANY_ID) && (cfg->device == dev->device || cfg->device == PCI_ANY_ID))
|
if (!cfg)
|
||||||
break;
|
|
||||||
|
|
||||||
if (cfg->vendor)
|
|
||||||
{
|
|
||||||
vdev.pdev = dev;
|
|
||||||
vdev.info = cfg->info;
|
|
||||||
|
|
||||||
if (cfg->ops->pre_reset && (ret = cfg->ops->pre_reset(&vdev)) && ret)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
ret = vdev.reset_ret = cfg->ops->reset(&vdev);
|
|
||||||
if (ret)
|
|
||||||
pr_warn("failed to reset device: %d\n", ret);
|
|
||||||
|
|
||||||
if (cfg->ops->post_reset)
|
|
||||||
ret = cfg->ops->post_reset(&vdev);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return -ENOTTY;
|
return -ENOTTY;
|
||||||
|
|
||||||
|
return vendor_reset_dev_locked(cfg, dev);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ftrace_hook fh_hooks[] = {
|
struct ftrace_hook fh_hooks[] = {
|
||||||
|
|||||||
71
src/vendor-reset-dev.c
Normal file
71
src/vendor-reset-dev.c
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
Vendor Reset - Vendor Specific Reset
|
||||||
|
Copyright (C) 2020 Geoffrey McRae <geoff@hostfission.com>
|
||||||
|
Copyright (C) 2020 Adam Madsen <adam@ajmadsen.com>
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify it under
|
||||||
|
the terms of the GNU General Public License as published by the Free Software
|
||||||
|
Foundation; either version 2 of the License, or (at your option) any later
|
||||||
|
version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along with
|
||||||
|
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||||
|
Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "vendor-reset-dev.h"
|
||||||
|
#include "device-db.h"
|
||||||
|
|
||||||
|
struct vendor_reset_cfg * vendor_reset_cfg_find(unsigned int vendor, unsigned
|
||||||
|
int device)
|
||||||
|
{
|
||||||
|
struct vendor_reset_cfg * cfg;
|
||||||
|
|
||||||
|
for(cfg = vendor_reset_devices; cfg->vendor; ++cfg)
|
||||||
|
{
|
||||||
|
if (cfg->vendor != vendor)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (device == PCI_ANY_ID || device == cfg->device)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cfg->vendor)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return cfg;
|
||||||
|
}
|
||||||
|
|
||||||
|
long vendor_reset_dev_locked(struct vendor_reset_cfg *cfg, struct pci_dev *dev)
|
||||||
|
{
|
||||||
|
struct vendor_reset_dev vdev =
|
||||||
|
{
|
||||||
|
.pdev = dev,
|
||||||
|
.info = cfg->info
|
||||||
|
};
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (cfg->ops->pre_reset)
|
||||||
|
{
|
||||||
|
ret = cfg->ops->pre_reset(&vdev);
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* expose return code to cleanup */
|
||||||
|
ret = vdev.reset_ret = cfg->ops->reset(&vdev);
|
||||||
|
if (ret)
|
||||||
|
{
|
||||||
|
pci_warn(dev, "Failed to reset device\n");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cfg->ops->post_reset)
|
||||||
|
ret = cfg->ops->post_reset(&vdev);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
@ -20,8 +20,6 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
#ifndef _H_VENDOR_RESET_DEV
|
#ifndef _H_VENDOR_RESET_DEV
|
||||||
#define _H_VENDOR_RESET_DEV
|
#define _H_VENDOR_RESET_DEV
|
||||||
|
|
||||||
#define VENDOR_RESET_DEVICE_ALL ((unsigned int)-1)
|
|
||||||
|
|
||||||
#include <linux/pci.h>
|
#include <linux/pci.h>
|
||||||
|
|
||||||
struct vendor_reset_dev
|
struct vendor_reset_dev
|
||||||
@ -49,8 +47,7 @@ struct vendor_reset_cfg
|
|||||||
/* the vendor ID */
|
/* the vendor ID */
|
||||||
unsigned int vendor;
|
unsigned int vendor;
|
||||||
|
|
||||||
/* the device ID or VENDOR_RESET_DEVICE_ALL to match all devices for the
|
/* the device ID or PCI_ANY_ID to match all devices for the vendor */
|
||||||
* vendor */
|
|
||||||
unsigned int device;
|
unsigned int device;
|
||||||
|
|
||||||
/* the reset operations */
|
/* the reset operations */
|
||||||
@ -60,4 +57,11 @@ struct vendor_reset_cfg
|
|||||||
unsigned long info;
|
unsigned long info;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* search the device table for the specified vendor and device id and return it */
|
||||||
|
struct vendor_reset_cfg * vendor_reset_cfg_find(unsigned int vendor, unsigned
|
||||||
|
int device);
|
||||||
|
|
||||||
|
/* perform the device reset */
|
||||||
|
long vendor_reset_dev_locked(struct vendor_reset_cfg *cfg, struct pci_dev *dev);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -24,9 +24,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
#include <linux/pci.h>
|
#include <linux/pci.h>
|
||||||
|
|
||||||
#include "vendor-reset-dev.h"
|
#include "vendor-reset-dev.h"
|
||||||
#include "vendor-reset.h"
|
#include "vendor-reset-ioctl.h"
|
||||||
|
|
||||||
#include "device-db.h"
|
|
||||||
|
|
||||||
#include "ftrace.h"
|
#include "ftrace.h"
|
||||||
#include "hooks.h"
|
#include "hooks.h"
|
||||||
@ -41,10 +39,9 @@ module_param(install_hook, bool, 0);
|
|||||||
static long vendor_reset_ioctl_reset(struct file * filp, unsigned long arg)
|
static long vendor_reset_ioctl_reset(struct file * filp, unsigned long arg)
|
||||||
{
|
{
|
||||||
struct vendor_reset_ioctl dev;
|
struct vendor_reset_ioctl dev;
|
||||||
struct vendor_reset_cfg *entry = vendor_reset_devices;
|
struct vendor_reset_cfg *cfg;
|
||||||
struct pci_dev * pcidev;
|
struct pci_dev * pcidev;
|
||||||
int ret;
|
int ret;
|
||||||
struct vendor_reset_dev vdev = {0};
|
|
||||||
|
|
||||||
if (copy_from_user(&dev, (void __user *)arg, sizeof(dev)))
|
if (copy_from_user(&dev, (void __user *)arg, sizeof(dev)))
|
||||||
return -EFAULT;
|
return -EFAULT;
|
||||||
@ -53,25 +50,13 @@ static long vendor_reset_ioctl_reset(struct file * filp, unsigned long arg)
|
|||||||
if (!pcidev)
|
if (!pcidev)
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
|
|
||||||
for(entry = vendor_reset_devices; entry->vendor; ++entry)
|
cfg = vendor_reset_cfg_find(pcidev->vendor, pcidev->device);
|
||||||
{
|
if (!cfg)
|
||||||
if (entry->vendor != pcidev->vendor)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (entry->device == VENDOR_RESET_DEVICE_ALL ||
|
|
||||||
entry->device == pcidev->device)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!entry->vendor)
|
|
||||||
{
|
{
|
||||||
ret = -EOPNOTSUPP;
|
ret = -EOPNOTSUPP;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
vdev.pdev = pcidev;
|
|
||||||
vdev.info = entry->info;
|
|
||||||
|
|
||||||
/* we probably always want to lock the device */
|
/* we probably always want to lock the device */
|
||||||
if (!pci_cfg_access_trylock(pcidev))
|
if (!pci_cfg_access_trylock(pcidev))
|
||||||
{
|
{
|
||||||
@ -79,36 +64,19 @@ static long vendor_reset_ioctl_reset(struct file * filp, unsigned long arg)
|
|||||||
ret = -EAGAIN;
|
ret = -EAGAIN;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!device_trylock(&pcidev->dev))
|
if (!device_trylock(&pcidev->dev))
|
||||||
{
|
{
|
||||||
pci_warn(pcidev, "Could not acquire device lock\n");
|
pci_warn(pcidev, "Could not acquire device lock\n");
|
||||||
pci_cfg_access_unlock(pcidev);
|
|
||||||
ret = -EAGAIN;
|
ret = -EAGAIN;
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entry->ops->pre_reset)
|
|
||||||
{
|
|
||||||
ret = entry->ops->pre_reset(&vdev);
|
|
||||||
if (ret)
|
|
||||||
goto unlock;
|
goto unlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* expose return code to cleanup */
|
ret = vendor_reset_dev_locked(cfg, pcidev);
|
||||||
ret = vdev.reset_ret = entry->ops->reset(&vdev);
|
device_unlock(&pcidev->dev);
|
||||||
if (ret)
|
|
||||||
pci_warn(pcidev, "Failed to reset device\n");
|
|
||||||
|
|
||||||
if (entry->ops->post_reset)
|
|
||||||
ret = entry->ops->post_reset(&vdev);
|
|
||||||
|
|
||||||
unlock:
|
unlock:
|
||||||
device_unlock(&pcidev->dev);
|
|
||||||
pci_cfg_access_unlock(pcidev);
|
pci_cfg_access_unlock(pcidev);
|
||||||
|
|
||||||
err:
|
err:
|
||||||
pci_dev_put(pcidev);
|
pci_dev_put(pcidev);
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
@ -22,7 +22,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include "vendor-reset.h"
|
#include "vendor-reset-ioctl.h"
|
||||||
#include "ucommon.h"
|
#include "ucommon.h"
|
||||||
|
|
||||||
void help_(const char *prog);
|
void help_(const char *prog);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user