1 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
2 /* Copyright(c) 2014 - 2020 Intel Corporation */
3 #include <linux/module.h>
4 #include <linux/mutex.h>
5 #include <linux/slab.h>
7 #include <linux/bitops.h>
9 #include <linux/cdev.h>
10 #include <linux/uaccess.h>
11 #include <linux/crypto.h>
13 #include "adf_accel_devices.h"
14 #include "adf_common_drv.h"
16 #include "adf_cfg_common.h"
17 #include "adf_cfg_user.h"
19 #define DEVICE_NAME "qat_adf_ctl"
21 static DEFINE_MUTEX(adf_ctl_lock
);
22 static long adf_ctl_ioctl(struct file
*fp
, unsigned int cmd
, unsigned long arg
);
24 static const struct file_operations adf_ctl_ops
= {
26 .unlocked_ioctl
= adf_ctl_ioctl
,
27 .compat_ioctl
= compat_ptr_ioctl
,
30 struct adf_ctl_drv_info
{
33 struct class *drv_class
;
36 static struct adf_ctl_drv_info adf_ctl_drv
;
38 static void adf_chr_drv_destroy(void)
40 device_destroy(adf_ctl_drv
.drv_class
, MKDEV(adf_ctl_drv
.major
, 0));
41 cdev_del(&adf_ctl_drv
.drv_cdev
);
42 class_destroy(adf_ctl_drv
.drv_class
);
43 unregister_chrdev_region(MKDEV(adf_ctl_drv
.major
, 0), 1);
46 static int adf_chr_drv_create(void)
49 struct device
*drv_device
;
51 if (alloc_chrdev_region(&dev_id
, 0, 1, DEVICE_NAME
)) {
52 pr_err("QAT: unable to allocate chrdev region\n");
56 adf_ctl_drv
.drv_class
= class_create(THIS_MODULE
, DEVICE_NAME
);
57 if (IS_ERR(adf_ctl_drv
.drv_class
)) {
58 pr_err("QAT: class_create failed for adf_ctl\n");
59 goto err_chrdev_unreg
;
61 adf_ctl_drv
.major
= MAJOR(dev_id
);
62 cdev_init(&adf_ctl_drv
.drv_cdev
, &adf_ctl_ops
);
63 if (cdev_add(&adf_ctl_drv
.drv_cdev
, dev_id
, 1)) {
64 pr_err("QAT: cdev add failed\n");
68 drv_device
= device_create(adf_ctl_drv
.drv_class
, NULL
,
69 MKDEV(adf_ctl_drv
.major
, 0),
71 if (IS_ERR(drv_device
)) {
72 pr_err("QAT: failed to create device\n");
77 cdev_del(&adf_ctl_drv
.drv_cdev
);
79 class_destroy(adf_ctl_drv
.drv_class
);
81 unregister_chrdev_region(dev_id
, 1);
85 static int adf_ctl_alloc_resources(struct adf_user_cfg_ctl_data
**ctl_data
,
88 struct adf_user_cfg_ctl_data
*cfg_data
;
90 cfg_data
= kzalloc(sizeof(*cfg_data
), GFP_KERNEL
);
94 /* Initialize device id to NO DEVICE as 0 is a valid device id */
95 cfg_data
->device_id
= ADF_CFG_NO_DEVICE
;
97 if (copy_from_user(cfg_data
, (void __user
*)arg
, sizeof(*cfg_data
))) {
98 pr_err("QAT: failed to copy from user cfg_data.\n");
103 *ctl_data
= cfg_data
;
107 static int adf_add_key_value_data(struct adf_accel_dev
*accel_dev
,
109 const struct adf_user_cfg_key_val
*key_val
)
111 if (key_val
->type
== ADF_HEX
) {
112 long *ptr
= (long *)key_val
->val
;
115 if (adf_cfg_add_key_value_param(accel_dev
, section
,
116 key_val
->key
, (void *)val
,
118 dev_err(&GET_DEV(accel_dev
),
119 "failed to add hex keyvalue.\n");
123 if (adf_cfg_add_key_value_param(accel_dev
, section
,
124 key_val
->key
, key_val
->val
,
126 dev_err(&GET_DEV(accel_dev
),
127 "failed to add keyvalue.\n");
134 static int adf_copy_key_value_data(struct adf_accel_dev
*accel_dev
,
135 struct adf_user_cfg_ctl_data
*ctl_data
)
137 struct adf_user_cfg_key_val key_val
;
138 struct adf_user_cfg_key_val
*params_head
;
139 struct adf_user_cfg_section section
, *section_head
;
141 section_head
= ctl_data
->config_section
;
143 while (section_head
) {
144 if (copy_from_user(§ion
, (void __user
*)section_head
,
145 sizeof(*section_head
))) {
146 dev_err(&GET_DEV(accel_dev
),
147 "failed to copy section info\n");
151 if (adf_cfg_section_add(accel_dev
, section
.name
)) {
152 dev_err(&GET_DEV(accel_dev
),
153 "failed to add section.\n");
157 params_head
= section
.params
;
159 while (params_head
) {
160 if (copy_from_user(&key_val
, (void __user
*)params_head
,
162 dev_err(&GET_DEV(accel_dev
),
163 "Failed to copy keyvalue.\n");
166 if (adf_add_key_value_data(accel_dev
, section
.name
,
170 params_head
= key_val
.next
;
172 section_head
= section
.next
;
176 adf_cfg_del_all(accel_dev
);
180 static int adf_ctl_ioctl_dev_config(struct file
*fp
, unsigned int cmd
,
184 struct adf_user_cfg_ctl_data
*ctl_data
;
185 struct adf_accel_dev
*accel_dev
;
187 ret
= adf_ctl_alloc_resources(&ctl_data
, arg
);
191 accel_dev
= adf_devmgr_get_dev_by_id(ctl_data
->device_id
);
197 if (adf_dev_started(accel_dev
)) {
202 if (adf_copy_key_value_data(accel_dev
, ctl_data
)) {
206 set_bit(ADF_STATUS_CONFIGURED
, &accel_dev
->status
);
212 static int adf_ctl_is_device_in_use(int id
)
214 struct adf_accel_dev
*dev
;
216 list_for_each_entry(dev
, adf_devmgr_get_head(), list
) {
217 if (id
== dev
->accel_id
|| id
== ADF_CFG_ALL_DEVICES
) {
218 if (adf_devmgr_in_reset(dev
) || adf_dev_in_use(dev
)) {
219 dev_info(&GET_DEV(dev
),
220 "device qat_dev%d is busy\n",
229 static void adf_ctl_stop_devices(u32 id
)
231 struct adf_accel_dev
*accel_dev
;
233 list_for_each_entry(accel_dev
, adf_devmgr_get_head(), list
) {
234 if (id
== accel_dev
->accel_id
|| id
== ADF_CFG_ALL_DEVICES
) {
235 if (!adf_dev_started(accel_dev
))
238 /* First stop all VFs */
239 if (!accel_dev
->is_vf
)
242 adf_dev_stop(accel_dev
);
243 adf_dev_shutdown(accel_dev
);
247 list_for_each_entry(accel_dev
, adf_devmgr_get_head(), list
) {
248 if (id
== accel_dev
->accel_id
|| id
== ADF_CFG_ALL_DEVICES
) {
249 if (!adf_dev_started(accel_dev
))
252 adf_dev_stop(accel_dev
);
253 adf_dev_shutdown(accel_dev
);
258 static int adf_ctl_ioctl_dev_stop(struct file
*fp
, unsigned int cmd
,
262 struct adf_user_cfg_ctl_data
*ctl_data
;
264 ret
= adf_ctl_alloc_resources(&ctl_data
, arg
);
268 if (adf_devmgr_verify_id(ctl_data
->device_id
)) {
269 pr_err("QAT: Device %d not found\n", ctl_data
->device_id
);
274 ret
= adf_ctl_is_device_in_use(ctl_data
->device_id
);
278 if (ctl_data
->device_id
== ADF_CFG_ALL_DEVICES
)
279 pr_info("QAT: Stopping all acceleration devices.\n");
281 pr_info("QAT: Stopping acceleration device qat_dev%d.\n",
282 ctl_data
->device_id
);
284 adf_ctl_stop_devices(ctl_data
->device_id
);
291 static int adf_ctl_ioctl_dev_start(struct file
*fp
, unsigned int cmd
,
295 struct adf_user_cfg_ctl_data
*ctl_data
;
296 struct adf_accel_dev
*accel_dev
;
298 ret
= adf_ctl_alloc_resources(&ctl_data
, arg
);
303 accel_dev
= adf_devmgr_get_dev_by_id(ctl_data
->device_id
);
307 if (!adf_dev_started(accel_dev
)) {
308 dev_info(&GET_DEV(accel_dev
),
309 "Starting acceleration device qat_dev%d.\n",
310 ctl_data
->device_id
);
311 ret
= adf_dev_init(accel_dev
);
313 ret
= adf_dev_start(accel_dev
);
315 dev_info(&GET_DEV(accel_dev
),
316 "Acceleration device qat_dev%d already started.\n",
317 ctl_data
->device_id
);
320 dev_err(&GET_DEV(accel_dev
), "Failed to start qat_dev%d\n",
321 ctl_data
->device_id
);
322 adf_dev_stop(accel_dev
);
323 adf_dev_shutdown(accel_dev
);
330 static int adf_ctl_ioctl_get_num_devices(struct file
*fp
, unsigned int cmd
,
335 adf_devmgr_get_num_dev(&num_devices
);
336 if (copy_to_user((void __user
*)arg
, &num_devices
, sizeof(num_devices
)))
342 static int adf_ctl_ioctl_get_status(struct file
*fp
, unsigned int cmd
,
345 struct adf_hw_device_data
*hw_data
;
346 struct adf_dev_status_info dev_info
;
347 struct adf_accel_dev
*accel_dev
;
349 if (copy_from_user(&dev_info
, (void __user
*)arg
,
350 sizeof(struct adf_dev_status_info
))) {
351 pr_err("QAT: failed to copy from user.\n");
355 accel_dev
= adf_devmgr_get_dev_by_id(dev_info
.accel_id
);
359 hw_data
= accel_dev
->hw_device
;
360 dev_info
.state
= adf_dev_started(accel_dev
) ? DEV_UP
: DEV_DOWN
;
361 dev_info
.num_ae
= hw_data
->get_num_aes(hw_data
);
362 dev_info
.num_accel
= hw_data
->get_num_accels(hw_data
);
363 dev_info
.num_logical_accel
= hw_data
->num_logical_accel
;
364 dev_info
.banks_per_accel
= hw_data
->num_banks
365 / hw_data
->num_logical_accel
;
366 strlcpy(dev_info
.name
, hw_data
->dev_class
->name
, sizeof(dev_info
.name
));
367 dev_info
.instance_id
= hw_data
->instance_id
;
368 dev_info
.type
= hw_data
->dev_class
->type
;
369 dev_info
.bus
= accel_to_pci_dev(accel_dev
)->bus
->number
;
370 dev_info
.dev
= PCI_SLOT(accel_to_pci_dev(accel_dev
)->devfn
);
371 dev_info
.fun
= PCI_FUNC(accel_to_pci_dev(accel_dev
)->devfn
);
373 if (copy_to_user((void __user
*)arg
, &dev_info
,
374 sizeof(struct adf_dev_status_info
))) {
375 dev_err(&GET_DEV(accel_dev
), "failed to copy status.\n");
381 static long adf_ctl_ioctl(struct file
*fp
, unsigned int cmd
, unsigned long arg
)
385 if (mutex_lock_interruptible(&adf_ctl_lock
))
389 case IOCTL_CONFIG_SYS_RESOURCE_PARAMETERS
:
390 ret
= adf_ctl_ioctl_dev_config(fp
, cmd
, arg
);
393 case IOCTL_STOP_ACCEL_DEV
:
394 ret
= adf_ctl_ioctl_dev_stop(fp
, cmd
, arg
);
397 case IOCTL_START_ACCEL_DEV
:
398 ret
= adf_ctl_ioctl_dev_start(fp
, cmd
, arg
);
401 case IOCTL_GET_NUM_DEVICES
:
402 ret
= adf_ctl_ioctl_get_num_devices(fp
, cmd
, arg
);
405 case IOCTL_STATUS_ACCEL_DEV
:
406 ret
= adf_ctl_ioctl_get_status(fp
, cmd
, arg
);
409 pr_err("QAT: Invalid ioctl\n");
413 mutex_unlock(&adf_ctl_lock
);
417 static int __init
adf_register_ctl_device_driver(void)
419 if (adf_chr_drv_create())
425 if (adf_init_pf_wq())
428 if (adf_init_vf_wq())
431 if (qat_crypto_register())
432 goto err_crypto_register
;
443 adf_chr_drv_destroy();
445 mutex_destroy(&adf_ctl_lock
);
449 static void __exit
adf_unregister_ctl_device_driver(void)
451 adf_chr_drv_destroy();
455 qat_crypto_unregister();
456 adf_clean_vf_map(false);
457 mutex_destroy(&adf_ctl_lock
);
460 module_init(adf_register_ctl_device_driver
);
461 module_exit(adf_unregister_ctl_device_driver
);
462 MODULE_LICENSE("Dual BSD/GPL");
463 MODULE_AUTHOR("Intel");
464 MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
465 MODULE_ALIAS_CRYPTO("intel_qat");
466 MODULE_VERSION(ADF_DRV_VERSION
);