xref: /linux/drivers/crypto/caam/dpseci.c (revision 31d1b7710262fba12282b24083f20dc76e0efc93)
1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /*
3  * Copyright 2013-2016 Freescale Semiconductor Inc.
4  * Copyright 2017-2018 NXP
5  */
6 
7 #include <linux/fsl/mc.h>
8 #include "dpseci.h"
9 #include "dpseci_cmd.h"
10 
11 /**
12  * dpseci_open() - Open a control session for the specified object
13  * @mc_io:	Pointer to MC portal's I/O object
14  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
15  * @dpseci_id:	DPSECI unique ID
16  * @token:	Returned token; use in subsequent API calls
17  *
18  * This function can be used to open a control session for an already created
19  * object; an object may have been declared statically in the DPL
20  * or created dynamically.
21  * This function returns a unique authentication token, associated with the
22  * specific object ID and the specific MC portal; this token must be used in all
23  * subsequent commands for this specific object.
24  *
25  * Return:	'0' on success, error code otherwise
26  */
27 int dpseci_open(struct fsl_mc_io *mc_io, u32 cmd_flags, int dpseci_id,
28 		u16 *token)
29 {
30 	struct fsl_mc_command cmd = { 0 };
31 	struct dpseci_cmd_open *cmd_params;
32 	int err;
33 
34 	cmd.header = mc_encode_cmd_header(DPSECI_CMDID_OPEN,
35 					  cmd_flags,
36 					  0);
37 	cmd_params = (struct dpseci_cmd_open *)cmd.params;
38 	cmd_params->dpseci_id = cpu_to_le32(dpseci_id);
39 	err = mc_send_command(mc_io, &cmd);
40 	if (err)
41 		return err;
42 
43 	*token = mc_cmd_hdr_read_token(&cmd);
44 
45 	return 0;
46 }
47 
48 /**
49  * dpseci_close() - Close the control session of the object
50  * @mc_io:	Pointer to MC portal's I/O object
51  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
52  * @token:	Token of DPSECI object
53  *
54  * After this function is called, no further operations are allowed on the
55  * object without opening a new control session.
56  *
57  * Return:	'0' on success, error code otherwise
58  */
59 int dpseci_close(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
60 {
61 	struct fsl_mc_command cmd = { 0 };
62 
63 	cmd.header = mc_encode_cmd_header(DPSECI_CMDID_CLOSE,
64 					  cmd_flags,
65 					  token);
66 	return mc_send_command(mc_io, &cmd);
67 }
68 
69 /**
70  * dpseci_enable() - Enable the DPSECI, allow sending and receiving frames
71  * @mc_io:	Pointer to MC portal's I/O object
72  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
73  * @token:	Token of DPSECI object
74  *
75  * Return:	'0' on success, error code otherwise
76  */
77 int dpseci_enable(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
78 {
79 	struct fsl_mc_command cmd = { 0 };
80 
81 	cmd.header = mc_encode_cmd_header(DPSECI_CMDID_ENABLE,
82 					  cmd_flags,
83 					  token);
84 	return mc_send_command(mc_io, &cmd);
85 }
86 
87 /**
88  * dpseci_disable() - Disable the DPSECI, stop sending and receiving frames
89  * @mc_io:	Pointer to MC portal's I/O object
90  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
91  * @token:	Token of DPSECI object
92  *
93  * Return:	'0' on success, error code otherwise
94  */
95 int dpseci_disable(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
96 {
97 	struct fsl_mc_command cmd = { 0 };
98 
99 	cmd.header = mc_encode_cmd_header(DPSECI_CMDID_DISABLE,
100 					  cmd_flags,
101 					  token);
102 
103 	return mc_send_command(mc_io, &cmd);
104 }
105 
106 /**
107  * dpseci_is_enabled() - Check if the DPSECI is enabled.
108  * @mc_io:	Pointer to MC portal's I/O object
109  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
110  * @token:	Token of DPSECI object
111  * @en:		Returns '1' if object is enabled; '0' otherwise
112  *
113  * Return:	'0' on success, error code otherwise
114  */
115 int dpseci_is_enabled(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
116 		      int *en)
117 {
118 	struct fsl_mc_command cmd = { 0 };
119 	struct dpseci_rsp_is_enabled *rsp_params;
120 	int err;
121 
122 	cmd.header = mc_encode_cmd_header(DPSECI_CMDID_IS_ENABLED,
123 					  cmd_flags,
124 					  token);
125 	err = mc_send_command(mc_io, &cmd);
126 	if (err)
127 		return err;
128 
129 	rsp_params = (struct dpseci_rsp_is_enabled *)cmd.params;
130 	*en = dpseci_get_field(rsp_params->is_enabled, ENABLE);
131 
132 	return 0;
133 }
134 
135 /**
136  * dpseci_get_attributes() - Retrieve DPSECI attributes
137  * @mc_io:	Pointer to MC portal's I/O object
138  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
139  * @token:	Token of DPSECI object
140  * @attr:	Returned object's attributes
141  *
142  * Return:	'0' on success, error code otherwise
143  */
144 int dpseci_get_attributes(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
145 			  struct dpseci_attr *attr)
146 {
147 	struct fsl_mc_command cmd = { 0 };
148 	struct dpseci_rsp_get_attributes *rsp_params;
149 	int err;
150 
151 	cmd.header = mc_encode_cmd_header(DPSECI_CMDID_GET_ATTR,
152 					  cmd_flags,
153 					  token);
154 	err = mc_send_command(mc_io, &cmd);
155 	if (err)
156 		return err;
157 
158 	rsp_params = (struct dpseci_rsp_get_attributes *)cmd.params;
159 	attr->id = le32_to_cpu(rsp_params->id);
160 	attr->num_tx_queues = rsp_params->num_tx_queues;
161 	attr->num_rx_queues = rsp_params->num_rx_queues;
162 	attr->options = le32_to_cpu(rsp_params->options);
163 
164 	return 0;
165 }
166 
167 /**
168  * dpseci_set_rx_queue() - Set Rx queue configuration
169  * @mc_io:	Pointer to MC portal's I/O object
170  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
171  * @token:	Token of DPSECI object
172  * @queue:	Select the queue relative to number of priorities configured at
173  *		DPSECI creation; use DPSECI_ALL_QUEUES to configure all
174  *		Rx queues identically.
175  * @cfg:	Rx queue configuration
176  *
177  * Return:	'0' on success, error code otherwise
178  */
179 int dpseci_set_rx_queue(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
180 			u8 queue, const struct dpseci_rx_queue_cfg *cfg)
181 {
182 	struct fsl_mc_command cmd = { 0 };
183 	struct dpseci_cmd_queue *cmd_params;
184 
185 	cmd.header = mc_encode_cmd_header(DPSECI_CMDID_SET_RX_QUEUE,
186 					  cmd_flags,
187 					  token);
188 	cmd_params = (struct dpseci_cmd_queue *)cmd.params;
189 	cmd_params->dest_id = cpu_to_le32(cfg->dest_cfg.dest_id);
190 	cmd_params->priority = cfg->dest_cfg.priority;
191 	cmd_params->queue = queue;
192 	dpseci_set_field(cmd_params->dest_type, DEST_TYPE,
193 			 cfg->dest_cfg.dest_type);
194 	cmd_params->user_ctx = cpu_to_le64(cfg->user_ctx);
195 	cmd_params->options = cpu_to_le32(cfg->options);
196 	dpseci_set_field(cmd_params->order_preservation_en, ORDER_PRESERVATION,
197 			 cfg->order_preservation_en);
198 
199 	return mc_send_command(mc_io, &cmd);
200 }
201 
202 /**
203  * dpseci_get_rx_queue() - Retrieve Rx queue attributes
204  * @mc_io:	Pointer to MC portal's I/O object
205  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
206  * @token:	Token of DPSECI object
207  * @queue:	Select the queue relative to number of priorities configured at
208  *		DPSECI creation
209  * @attr:	Returned Rx queue attributes
210  *
211  * Return:	'0' on success, error code otherwise
212  */
213 int dpseci_get_rx_queue(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
214 			u8 queue, struct dpseci_rx_queue_attr *attr)
215 {
216 	struct fsl_mc_command cmd = { 0 };
217 	struct dpseci_cmd_queue *cmd_params;
218 	int err;
219 
220 	cmd.header = mc_encode_cmd_header(DPSECI_CMDID_GET_RX_QUEUE,
221 					  cmd_flags,
222 					  token);
223 	cmd_params = (struct dpseci_cmd_queue *)cmd.params;
224 	cmd_params->queue = queue;
225 	err = mc_send_command(mc_io, &cmd);
226 	if (err)
227 		return err;
228 
229 	attr->dest_cfg.dest_id = le32_to_cpu(cmd_params->dest_id);
230 	attr->dest_cfg.priority = cmd_params->priority;
231 	attr->dest_cfg.dest_type = dpseci_get_field(cmd_params->dest_type,
232 						    DEST_TYPE);
233 	attr->user_ctx = le64_to_cpu(cmd_params->user_ctx);
234 	attr->fqid = le32_to_cpu(cmd_params->fqid);
235 	attr->order_preservation_en =
236 		dpseci_get_field(cmd_params->order_preservation_en,
237 				 ORDER_PRESERVATION);
238 
239 	return 0;
240 }
241 
242 /**
243  * dpseci_get_tx_queue() - Retrieve Tx queue attributes
244  * @mc_io:	Pointer to MC portal's I/O object
245  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
246  * @token:	Token of DPSECI object
247  * @queue:	Select the queue relative to number of priorities configured at
248  *		DPSECI creation
249  * @attr:	Returned Tx queue attributes
250  *
251  * Return:	'0' on success, error code otherwise
252  */
253 int dpseci_get_tx_queue(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
254 			u8 queue, struct dpseci_tx_queue_attr *attr)
255 {
256 	struct fsl_mc_command cmd = { 0 };
257 	struct dpseci_cmd_queue *cmd_params;
258 	struct dpseci_rsp_get_tx_queue *rsp_params;
259 	int err;
260 
261 	cmd.header = mc_encode_cmd_header(DPSECI_CMDID_GET_TX_QUEUE,
262 					  cmd_flags,
263 					  token);
264 	cmd_params = (struct dpseci_cmd_queue *)cmd.params;
265 	cmd_params->queue = queue;
266 	err = mc_send_command(mc_io, &cmd);
267 	if (err)
268 		return err;
269 
270 	rsp_params = (struct dpseci_rsp_get_tx_queue *)cmd.params;
271 	attr->fqid = le32_to_cpu(rsp_params->fqid);
272 	attr->priority = rsp_params->priority;
273 
274 	return 0;
275 }
276 
277 /**
278  * dpseci_get_sec_attr() - Retrieve SEC accelerator attributes
279  * @mc_io:	Pointer to MC portal's I/O object
280  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
281  * @token:	Token of DPSECI object
282  * @attr:	Returned SEC attributes
283  *
284  * Return:	'0' on success, error code otherwise
285  */
286 int dpseci_get_sec_attr(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
287 			struct dpseci_sec_attr *attr)
288 {
289 	struct fsl_mc_command cmd = { 0 };
290 	struct dpseci_rsp_get_sec_attr *rsp_params;
291 	int err;
292 
293 	cmd.header = mc_encode_cmd_header(DPSECI_CMDID_GET_SEC_ATTR,
294 					  cmd_flags,
295 					  token);
296 	err = mc_send_command(mc_io, &cmd);
297 	if (err)
298 		return err;
299 
300 	rsp_params = (struct dpseci_rsp_get_sec_attr *)cmd.params;
301 	attr->ip_id = le16_to_cpu(rsp_params->ip_id);
302 	attr->major_rev = rsp_params->major_rev;
303 	attr->minor_rev = rsp_params->minor_rev;
304 	attr->era = rsp_params->era;
305 	attr->deco_num = rsp_params->deco_num;
306 	attr->zuc_auth_acc_num = rsp_params->zuc_auth_acc_num;
307 	attr->zuc_enc_acc_num = rsp_params->zuc_enc_acc_num;
308 	attr->snow_f8_acc_num = rsp_params->snow_f8_acc_num;
309 	attr->snow_f9_acc_num = rsp_params->snow_f9_acc_num;
310 	attr->crc_acc_num = rsp_params->crc_acc_num;
311 	attr->pk_acc_num = rsp_params->pk_acc_num;
312 	attr->kasumi_acc_num = rsp_params->kasumi_acc_num;
313 	attr->rng_acc_num = rsp_params->rng_acc_num;
314 	attr->md_acc_num = rsp_params->md_acc_num;
315 	attr->arc4_acc_num = rsp_params->arc4_acc_num;
316 	attr->des_acc_num = rsp_params->des_acc_num;
317 	attr->aes_acc_num = rsp_params->aes_acc_num;
318 	attr->ccha_acc_num = rsp_params->ccha_acc_num;
319 	attr->ptha_acc_num = rsp_params->ptha_acc_num;
320 
321 	return 0;
322 }
323 
324 /**
325  * dpseci_get_api_version() - Get Data Path SEC Interface API version
326  * @mc_io:	Pointer to MC portal's I/O object
327  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
328  * @major_ver:	Major version of data path sec API
329  * @minor_ver:	Minor version of data path sec API
330  *
331  * Return:	'0' on success, error code otherwise
332  */
333 int dpseci_get_api_version(struct fsl_mc_io *mc_io, u32 cmd_flags,
334 			   u16 *major_ver, u16 *minor_ver)
335 {
336 	struct fsl_mc_command cmd = { 0 };
337 	struct dpseci_rsp_get_api_version *rsp_params;
338 	int err;
339 
340 	cmd.header = mc_encode_cmd_header(DPSECI_CMDID_GET_API_VERSION,
341 					  cmd_flags, 0);
342 	err = mc_send_command(mc_io, &cmd);
343 	if (err)
344 		return err;
345 
346 	rsp_params = (struct dpseci_rsp_get_api_version *)cmd.params;
347 	*major_ver = le16_to_cpu(rsp_params->major);
348 	*minor_ver = le16_to_cpu(rsp_params->minor);
349 
350 	return 0;
351 }
352 
353 /**
354  * dpseci_set_congestion_notification() - Set congestion group
355  *	notification configuration
356  * @mc_io:	Pointer to MC portal's I/O object
357  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
358  * @token:	Token of DPSECI object
359  * @cfg:	congestion notification configuration
360  *
361  * Return:	'0' on success, error code otherwise
362  */
363 int dpseci_set_congestion_notification(struct fsl_mc_io *mc_io, u32 cmd_flags,
364 	u16 token, const struct dpseci_congestion_notification_cfg *cfg)
365 {
366 	struct fsl_mc_command cmd = { 0 };
367 	struct dpseci_cmd_congestion_notification *cmd_params;
368 
369 	cmd.header = mc_encode_cmd_header(
370 			DPSECI_CMDID_SET_CONGESTION_NOTIFICATION,
371 			cmd_flags,
372 			token);
373 	cmd_params = (struct dpseci_cmd_congestion_notification *)cmd.params;
374 	cmd_params->dest_id = cpu_to_le32(cfg->dest_cfg.dest_id);
375 	cmd_params->notification_mode = cpu_to_le16(cfg->notification_mode);
376 	cmd_params->priority = cfg->dest_cfg.priority;
377 	dpseci_set_field(cmd_params->options, CGN_DEST_TYPE,
378 			 cfg->dest_cfg.dest_type);
379 	dpseci_set_field(cmd_params->options, CGN_UNITS, cfg->units);
380 	cmd_params->message_iova = cpu_to_le64(cfg->message_iova);
381 	cmd_params->message_ctx = cpu_to_le64(cfg->message_ctx);
382 	cmd_params->threshold_entry = cpu_to_le32(cfg->threshold_entry);
383 	cmd_params->threshold_exit = cpu_to_le32(cfg->threshold_exit);
384 
385 	return mc_send_command(mc_io, &cmd);
386 }
387 
388 /**
389  * dpseci_get_congestion_notification() - Get congestion group notification
390  *	configuration
391  * @mc_io:	Pointer to MC portal's I/O object
392  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
393  * @token:	Token of DPSECI object
394  * @cfg:	congestion notification configuration
395  *
396  * Return:	'0' on success, error code otherwise
397  */
398 int dpseci_get_congestion_notification(struct fsl_mc_io *mc_io, u32 cmd_flags,
399 	u16 token, struct dpseci_congestion_notification_cfg *cfg)
400 {
401 	struct fsl_mc_command cmd = { 0 };
402 	struct dpseci_cmd_congestion_notification *rsp_params;
403 	int err;
404 
405 	cmd.header = mc_encode_cmd_header(
406 			DPSECI_CMDID_GET_CONGESTION_NOTIFICATION,
407 			cmd_flags,
408 			token);
409 	err = mc_send_command(mc_io, &cmd);
410 	if (err)
411 		return err;
412 
413 	rsp_params = (struct dpseci_cmd_congestion_notification *)cmd.params;
414 	cfg->dest_cfg.dest_id = le32_to_cpu(rsp_params->dest_id);
415 	cfg->notification_mode = le16_to_cpu(rsp_params->notification_mode);
416 	cfg->dest_cfg.priority = rsp_params->priority;
417 	cfg->dest_cfg.dest_type = dpseci_get_field(rsp_params->options,
418 						   CGN_DEST_TYPE);
419 	cfg->units = dpseci_get_field(rsp_params->options, CGN_UNITS);
420 	cfg->message_iova = le64_to_cpu(rsp_params->message_iova);
421 	cfg->message_ctx = le64_to_cpu(rsp_params->message_ctx);
422 	cfg->threshold_entry = le32_to_cpu(rsp_params->threshold_entry);
423 	cfg->threshold_exit = le32_to_cpu(rsp_params->threshold_exit);
424 
425 	return 0;
426 }
427