Skip to content

Commit 5ee5211

Browse files
gcabidduherbertx
authored andcommitted
crypto: qat - expose device state through sysfs for 4xxx
Expose the device state through an attribute in sysfs and allow to change it. This is to stop and shutdown a QAT device in order to change its configuration. The state attribute has been added to a newly created `qat` attribute group which will contain all _QAT specific_ attributes. The logic that implements the sysfs entries is part of a new file, adf_sysfs.c. This exposes an entry point to allow the driver to create attributes. The function that creates the sysfs attributes is called from the probe function of the driver and not in the state machine init function to allow the change of states even if the device is in the down state. In order to restore the device configuration between a transition from down to up, the function that configures the devices has been abstracted into the HW data structure. The `state` attribute is only exposed for qat_4xxx devices. Signed-off-by: Giovanni Cabiddu <giovanni.cabiddu@intel.com> Co-developed-by: Tomasz Kowallik <tomaszx.kowalik@intel.com> Signed-off-by: Tomasz Kowallik <tomaszx.kowalik@intel.com> Reviewed-by: Adam Guerin <adam.guerin@intel.com> Reviewed-by: Fiona Trahe <fiona.trahe@intel.com> Reviewed-by: Wojciech Ziemba <wojciech.ziemba@intel.com> Reviewed-by: Vladis Dronov <vdronov@redhat.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent fac76f2 commit 5ee5211

File tree

8 files changed

+151
-1
lines changed

8 files changed

+151
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
What: /sys/bus/pci/devices/<BDF>/qat/state
2+
Date: June 2022
3+
KernelVersion: 5.20
4+
Contact: qat-linux@intel.com
5+
Description: Reports the current state of the QAT device and allows to
6+
change it.
7+
8+
This attribute is RW.
9+
10+
Returned values:
11+
up: the device is up and running
12+
down: the device is down
13+
14+
Allowed values:
15+
up: initialize and start the device
16+
down: stop the device and bring it down
17+
18+
It is possible to transition the device from up to down only
19+
if the device is up and vice versa.
20+
21+
This attribute is only available for qat_4xxx devices.

drivers/crypto/qat/qat_4xxx/adf_4xxx_hw_data.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ void adf_init_hw_data_4xxx(struct adf_hw_device_data *hw_data)
357357
hw_data->ring_pair_reset = adf_gen4_ring_pair_reset;
358358
hw_data->enable_pm = adf_gen4_enable_pm;
359359
hw_data->handle_pm_interrupt = adf_gen4_handle_pm_interrupt;
360+
hw_data->dev_config = adf_crypto_dev_config;
360361

361362
adf_gen4_init_hw_csr_ops(&hw_data->csr_ops);
362363
adf_gen4_init_pf_pfvf_ops(&hw_data->pfvf_ops);

drivers/crypto/qat/qat_4xxx/adf_4xxx_hw_data.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,6 @@ enum icp_qat_4xxx_slice_mask {
7070

7171
void adf_init_hw_data_4xxx(struct adf_hw_device_data *hw_data);
7272
void adf_clean_hw_data_4xxx(struct adf_hw_device_data *hw_data);
73+
int adf_crypto_dev_config(struct adf_accel_dev *accel_dev);
7374

7475
#endif

drivers/crypto/qat/qat_4xxx/adf_drv.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static int adf_cfg_dev_init(struct adf_accel_dev *accel_dev)
5353
return 0;
5454
}
5555

56-
static int adf_crypto_dev_config(struct adf_accel_dev *accel_dev)
56+
int adf_crypto_dev_config(struct adf_accel_dev *accel_dev)
5757
{
5858
char key[ADF_CFG_MAX_KEY_LEN_IN_BYTES];
5959
int banks = GET_MAX_BANKS(accel_dev);
@@ -289,6 +289,10 @@ static int adf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
289289
goto out_err_disable_aer;
290290
}
291291

292+
ret = adf_sysfs_init(accel_dev);
293+
if (ret)
294+
goto out_err_disable_aer;
295+
292296
ret = adf_crypto_dev_config(accel_dev);
293297
if (ret)
294298
goto out_err_disable_aer;

drivers/crypto/qat/qat_common/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ intel_qat-objs := adf_cfg.o \
1010
adf_transport.o \
1111
adf_admin.o \
1212
adf_hw_arbiter.o \
13+
adf_sysfs.o \
1314
adf_gen2_hw_data.o \
1415
adf_gen4_hw_data.o \
1516
adf_gen4_pm.o \

drivers/crypto/qat/qat_common/adf_accel_devices.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ struct adf_hw_device_data {
199199
char *(*uof_get_name)(struct adf_accel_dev *accel_dev, u32 obj_num);
200200
u32 (*uof_get_num_objs)(void);
201201
u32 (*uof_get_ae_mask)(struct adf_accel_dev *accel_dev, u32 obj_num);
202+
int (*dev_config)(struct adf_accel_dev *accel_dev);
202203
struct adf_pfvf_ops pfvf_ops;
203204
struct adf_hw_csr_ops csr_ops;
204205
const char *fw_name;

drivers/crypto/qat/qat_common/adf_common_drv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ void adf_vf_isr_resource_free(struct adf_accel_dev *accel_dev);
127127

128128
int adf_pfvf_comms_disabled(struct adf_accel_dev *accel_dev);
129129

130+
int adf_sysfs_init(struct adf_accel_dev *accel_dev);
131+
130132
int qat_hal_init(struct adf_accel_dev *accel_dev);
131133
void qat_hal_deinit(struct icp_qat_fw_loader_handle *handle);
132134
int qat_hal_start(struct icp_qat_fw_loader_handle *handle);
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)