|
| 1 | +// SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only) |
| 2 | +/* Copyright(c) 2022 Intel Corporation */ |
| 3 | +#include <linux/device.h> |
| 4 | +#include <linux/errno.h> |
| 5 | +#include <linux/pci.h> |
| 6 | +#include "adf_accel_devices.h" |
| 7 | +#include "adf_cfg.h" |
| 8 | +#include "adf_common_drv.h" |
| 9 | + |
| 10 | +static const char * const state_operations[] = { |
| 11 | + [DEV_DOWN] = "down", |
| 12 | + [DEV_UP] = "up", |
| 13 | +}; |
| 14 | + |
| 15 | +static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 16 | + char *buf) |
| 17 | +{ |
| 18 | + struct adf_accel_dev *accel_dev; |
| 19 | + char *state; |
| 20 | + |
| 21 | + accel_dev = adf_devmgr_pci_to_accel_dev(to_pci_dev(dev)); |
| 22 | + if (!accel_dev) |
| 23 | + return -EINVAL; |
| 24 | + |
| 25 | + state = adf_dev_started(accel_dev) ? "up" : "down"; |
| 26 | + return sysfs_emit(buf, "%s\n", state); |
| 27 | +} |
| 28 | + |
| 29 | +static ssize_t state_store(struct device *dev, struct device_attribute *attr, |
| 30 | + const char *buf, size_t count) |
| 31 | +{ |
| 32 | + struct adf_accel_dev *accel_dev; |
| 33 | + u32 accel_id; |
| 34 | + int ret; |
| 35 | + |
| 36 | + accel_dev = adf_devmgr_pci_to_accel_dev(to_pci_dev(dev)); |
| 37 | + if (!accel_dev) |
| 38 | + return -EINVAL; |
| 39 | + |
| 40 | + accel_id = accel_dev->accel_id; |
| 41 | + |
| 42 | + if (adf_devmgr_in_reset(accel_dev) || adf_dev_in_use(accel_dev)) { |
| 43 | + dev_info(dev, "Device qat_dev%d is busy\n", accel_id); |
| 44 | + return -EBUSY; |
| 45 | + } |
| 46 | + |
| 47 | + ret = sysfs_match_string(state_operations, buf); |
| 48 | + if (ret < 0) |
| 49 | + return ret; |
| 50 | + |
| 51 | + switch (ret) { |
| 52 | + case DEV_DOWN: |
| 53 | + if (!adf_dev_started(accel_dev)) { |
| 54 | + dev_info(dev, "Device qat_dev%d already down\n", |
| 55 | + accel_id); |
| 56 | + return -EINVAL; |
| 57 | + } |
| 58 | + |
| 59 | + dev_info(dev, "Stopping device qat_dev%d\n", accel_id); |
| 60 | + |
| 61 | + adf_dev_stop(accel_dev); |
| 62 | + adf_dev_shutdown(accel_dev); |
| 63 | + |
| 64 | + break; |
| 65 | + case DEV_UP: |
| 66 | + if (adf_dev_started(accel_dev)) { |
| 67 | + dev_info(dev, "Device qat_dev%d already up\n", |
| 68 | + accel_id); |
| 69 | + return -EINVAL; |
| 70 | + } |
| 71 | + |
| 72 | + dev_info(dev, "Starting device qat_dev%d\n", accel_id); |
| 73 | + |
| 74 | + ret = GET_HW_DATA(accel_dev)->dev_config(accel_dev); |
| 75 | + if (!ret) |
| 76 | + ret = adf_dev_init(accel_dev); |
| 77 | + if (!ret) |
| 78 | + ret = adf_dev_start(accel_dev); |
| 79 | + |
| 80 | + if (ret < 0) { |
| 81 | + dev_err(dev, "Failed to start device qat_dev%d\n", |
| 82 | + accel_id); |
| 83 | + adf_dev_stop(accel_dev); |
| 84 | + adf_dev_shutdown(accel_dev); |
| 85 | + return ret; |
| 86 | + } |
| 87 | + break; |
| 88 | + default: |
| 89 | + return -EINVAL; |
| 90 | + } |
| 91 | + |
| 92 | + return count; |
| 93 | +} |
| 94 | + |
| 95 | +static DEVICE_ATTR_RW(state); |
| 96 | + |
| 97 | +static struct attribute *qat_attrs[] = { |
| 98 | + &dev_attr_state.attr, |
| 99 | + NULL, |
| 100 | +}; |
| 101 | + |
| 102 | +static struct attribute_group qat_group = { |
| 103 | + .attrs = qat_attrs, |
| 104 | + .name = "qat", |
| 105 | +}; |
| 106 | + |
| 107 | +int adf_sysfs_init(struct adf_accel_dev *accel_dev) |
| 108 | +{ |
| 109 | + int ret; |
| 110 | + |
| 111 | + ret = devm_device_add_group(&GET_DEV(accel_dev), &qat_group); |
| 112 | + if (ret) { |
| 113 | + dev_err(&GET_DEV(accel_dev), |
| 114 | + "Failed to create qat attribute group: %d\n", ret); |
| 115 | + } |
| 116 | + |
| 117 | + return ret; |
| 118 | +} |
| 119 | +EXPORT_SYMBOL_GPL(adf_sysfs_init); |
0 commit comments