xref: /freebsd/sys/dev/qat/qat_hw/qat_c4xxx/adf_c4xxx_ae_config.c (revision 8aa51e6d7de0a828020de64560d1385e15955a1c)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2025 Intel Corporation */
3 #include "adf_c4xxx_hw_data.h"
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/fs.h>
7 #include <linux/errno.h>
8 #include <linux/device.h>
9 #include <linux/io.h>
10 #include <sys/sbuf.h>
11 #include <sys/sysctl.h>
12 #include <sys/priv.h>
13 #include <adf_accel_devices.h>
14 #include <adf_common_drv.h>
15 #include <adf_cfg.h>
16 
17 /* String buffer size */
18 #define AE_INFO_BUFFER_SIZE 50
19 
20 #define AE_CONFIG_DBG_FILE "ae_config"
21 
22 static u8
find_first_me_index(const u32 au_mask)23 find_first_me_index(const u32 au_mask)
24 {
25 	u8 i;
26 	u32 mask = au_mask;
27 
28 	/* Retrieve the index of the first ME of an accel unit */
29 	for (i = 0; i < ADF_C4XXX_MAX_ACCELENGINES; i++) {
30 		if (mask & BIT(i))
31 			return i;
32 	}
33 
34 	return 0;
35 }
36 
37 static u8
get_au_index(u8 au_mask)38 get_au_index(u8 au_mask)
39 {
40 	u8 au_index = 0;
41 
42 	while (au_mask) {
43 		if (au_mask == BIT(0))
44 			return au_index;
45 		au_index++;
46 		au_mask = au_mask >> 1;
47 	}
48 
49 	return 0;
50 }
51 
adf_ae_config_show(SYSCTL_HANDLER_ARGS)52 static int adf_ae_config_show(SYSCTL_HANDLER_ARGS)
53 {
54 	struct sbuf sb;
55 	struct adf_accel_dev *accel_dev = arg1;
56 	struct adf_hw_device_data *hw_data = accel_dev->hw_device;
57 	struct adf_accel_unit *accel_unit = accel_dev->au_info->au;
58 	u8 i, j;
59 	u8 au_index;
60 	u8 ae_index;
61 	u8 num_aes;
62 	int ret = 0;
63 
64 	if (priv_check(curthread, PRIV_DRIVER) != 0)
65 		return EPERM;
66 
67 	u32 num_au = hw_data->get_num_accel_units(hw_data);
68 
69 	sbuf_new_for_sysctl(&sb, NULL, 2048, req);
70 
71 	sbuf_printf(&sb, "\n");
72 	for (i = 0; i < num_au; i++) {
73 		/* Retrieve accel unit index */
74 		au_index = get_au_index(accel_unit[i].au_mask);
75 
76 		/* Retrieve index of fist ME in current accel unit */
77 		ae_index = find_first_me_index(accel_unit[i].ae_mask);
78 		num_aes = accel_unit[i].num_ae;
79 
80 		/* Retrieve accel unit type */
81 		switch (accel_unit[i].services) {
82 		case ADF_ACCEL_CRYPTO:
83 			sbuf_printf(&sb,
84 				    "\tAccel unit %d - CRYPTO\n",
85 				    au_index);
86 			/* Display ME assignment for a particular accel unit */
87 			for (j = ae_index; j < (num_aes + ae_index); j++)
88 				sbuf_printf(&sb, "\t\tAE[%d]: crypto\n", j);
89 			break;
90 		case ADF_ACCEL_COMPRESSION:
91 			sbuf_printf(&sb,
92 				    "\tAccel unit %d - COMPRESSION\n",
93 				    au_index);
94 			/* Display ME assignment for a particular accel unit */
95 			for (j = ae_index; j < (num_aes + ae_index); j++)
96 				sbuf_printf(&sb,
97 					    "\t\tAE[%d]: compression\n",
98 					    j);
99 			break;
100 		case ADF_ACCEL_SERVICE_NULL:
101 		default:
102 			break;
103 		}
104 	}
105 
106 	sbuf_finish(&sb);
107 	ret = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
108 	sbuf_delete(&sb);
109 
110 	return ret;
111 }
112 
113 static int
c4xxx_add_debugfs_ae_config(struct adf_accel_dev * accel_dev)114 c4xxx_add_debugfs_ae_config(struct adf_accel_dev *accel_dev)
115 {
116 	struct sysctl_ctx_list *qat_sysctl_ctx = NULL;
117 	struct sysctl_oid *qat_sysctl_tree = NULL;
118 	struct sysctl_oid *ae_conf_ctl = NULL;
119 
120 	qat_sysctl_ctx =
121 	    device_get_sysctl_ctx(accel_dev->accel_pci_dev.pci_dev);
122 	qat_sysctl_tree =
123 	    device_get_sysctl_tree(accel_dev->accel_pci_dev.pci_dev);
124 
125 	ae_conf_ctl = SYSCTL_ADD_PROC(qat_sysctl_ctx,
126 				      SYSCTL_CHILDREN(qat_sysctl_tree),
127 				      OID_AUTO,
128 				      AE_CONFIG_DBG_FILE,
129 				      CTLTYPE_STRING | CTLFLAG_RD,
130 				      accel_dev,
131 				      0,
132 				      adf_ae_config_show,
133 				      "A",
134 				      "AE config");
135 	accel_dev->debugfs_ae_config = ae_conf_ctl;
136 	if (!accel_dev->debugfs_ae_config) {
137 		device_printf(GET_DEV(accel_dev),
138 			      "Could not create debug ae config entry.\n");
139 		return EFAULT;
140 	}
141 	return 0;
142 }
143 
144 int
c4xxx_init_ae_config(struct adf_accel_dev * accel_dev)145 c4xxx_init_ae_config(struct adf_accel_dev *accel_dev)
146 {
147 	int ret = 0;
148 
149 	/* Add a new file in debug file system with h/w version. */
150 	ret = c4xxx_add_debugfs_ae_config(accel_dev);
151 	if (ret) {
152 		c4xxx_exit_ae_config(accel_dev);
153 		device_printf(GET_DEV(accel_dev),
154 			      "Could not create debugfs ae config file\n");
155 		return EINVAL;
156 	}
157 
158 	return 0;
159 }
160 
161 void
c4xxx_exit_ae_config(struct adf_accel_dev * accel_dev)162 c4xxx_exit_ae_config(struct adf_accel_dev *accel_dev)
163 {
164 	if (!accel_dev->debugfs_ae_config)
165 		return;
166 
167 	/* Delete ae configuration file */
168 	remove_oid(accel_dev, accel_dev->debugfs_ae_config);
169 
170 	accel_dev->debugfs_ae_config = NULL;
171 }
172