aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/crypto/caam_jr/caam_jr_hw.c
blob: 4a2b089955b088ee173baf873286f40c338e736f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright 2017-2018 NXP
 */

#include <fcntl.h>
#include <unistd.h>
#include <inttypes.h>
#include <rte_common.h>
#include <rte_memory.h>
#include <rte_malloc.h>
#include <rte_crypto.h>
#include <rte_security.h>

#include <caam_jr_config.h>
#include <caam_jr_hw_specific.h>
#include <caam_jr_pvt.h>
#include <caam_jr_log.h>

/* RTA header files */
#include <hw/desc/common.h>
#include <hw/desc/algo.h>
#include <hw/desc/ipsec.h>

/* Used to retry resetting a job ring in SEC hardware. */
#define SEC_TIMEOUT 100000

/* @brief Process Jump Halt Condition related errors
 *
 * @param [in]  error_code        The error code in the descriptor status word
 */
static inline void
hw_handle_jmp_halt_cond_err(union hw_error_code error_code)
{
	CAAM_JR_DEBUG("JMP: %d, Descriptor Index: 0x%x, Condition: 0x%x",
			error_code.error_desc.jmp_halt_cond_src.jmp,
			error_code.error_desc.jmp_halt_cond_src.desc_idx,
			error_code.error_desc.jmp_halt_cond_src.cond);
	(void)error_code;
}

/* @brief Process DECO related errors
 *
 * @param [in]  error_code        The error code in the descriptor status word
 */
static inline void
hw_handle_deco_err(union hw_error_code error_code)
{
	CAAM_JR_DEBUG("JMP: %d, Descriptor Index: 0x%x",
			error_code.error_desc.deco_src.jmp,
			error_code.error_desc.deco_src.desc_idx);

	switch (error_code.error_desc.deco_src.desc_err) {
	case SEC_HW_ERR_DECO_HFN_THRESHOLD:
		CAAM_JR_DEBUG(" Warning: Descriptor completed normally,"
			"but 3GPP HFN matches or exceeds the Threshold ");
		break;
	default:
		CAAM_JR_DEBUG("Error 0x%04x not implemented",
				error_code.error_desc.deco_src.desc_err);
		break;
	}
}

/* @brief Process  Jump Halt User Status related errors
 *
 * @param [in]  error_code        The error code in the descriptor status word
 */
static inline void
hw_handle_jmp_halt_user_err(union hw_error_code error_code __rte_unused)
{
	CAAM_JR_DEBUG(" Not implemented");
}

/* @brief Process CCB related errors
 *
 * @param [in]  error_code        The error code in the descriptor status word
 */
static inline void
hw_handle_ccb_err(union hw_error_code hw_error_code __rte_unused)
{
	CAAM_JR_DEBUG(" Not implemented");
}

/* @brief Process Job Ring related errors
 *
 * @param [in]  error_code        The error code in the descriptor status word
 */
static inline void
hw_handle_jr_err(union hw_error_code hw_error_code __rte_unused)
{
	CAAM_JR_DEBUG(" Not implemented");
}

int
hw_reset_job_ring(struct sec_job_ring_t *job_ring)
{
	int ret = 0;

	ASSERT(job_ring->register_base_addr != NULL);

	/* First reset the job ring in hw */
	ret = hw_shutdown_job_ring(job_ring);
	SEC_ASSERT(ret == 0, ret, "Failed resetting job ring in hardware");

	/* In order to have the HW JR in a workable state
	 * after a reset, I need to re-write the input
	 * queue size, input start address, output queue
	 * size and output start address
	 */
	/* Write the JR input queue size to the HW register */
	hw_set_input_ring_size(job_ring, SEC_JOB_RING_SIZE);

	/* Write the JR output queue size to the HW register */
	hw_set_output_ring_size(job_ring, SEC_JOB_RING_SIZE);

	/* Write the JR input queue start address */
	hw_set_input_ring_start_addr(job_ring,
			caam_jr_dma_vtop(job_ring->input_ring));
	CAAM_JR_DEBUG(" Set input ring base address to : Virtual: 0x%" PRIx64
		      ",Physical: 0x%" PRIx64 ", Read from HW: 0x%" PRIx64,
		      (uint64_t)(uintptr_t)job_ring->input_ring,
		      caam_jr_dma_vtop(job_ring->input_ring),
		      hw_get_inp_queue_base(job_ring));

	/* Write the JR output queue start address */
	hw_set_output_ring_start_addr(job_ring,
			caam_jr_dma_vtop(job_ring->output_ring));
	CAAM_JR_DEBUG(" Set output ring base address to: Virtual: 0x%" PRIx64
		      ",Physical: 0x%" PRIx64 ", Read from HW: 0x%" PRIx64,
		      (uint64_t)(uintptr_t)job_ring->output_ring,
		      caam_jr_dma_vtop(job_ring->output_ring),
		      hw_get_out_queue_base(job_ring));
	return ret;
}

int
hw_shutdown_job_ring(struct sec_job_ring_t *job_ring)
{
	unsigned int timeout = SEC_TIMEOUT;
	uint32_t tmp = 0;
	int usleep_interval = 10;

	if (job_ring->register_base_addr == NULL) {
		CAAM_JR_ERR("Jr[%p] has reg base addr as NULL.driver not init",
			job_ring);
		return 0;
	}

	CAAM_JR_INFO("Resetting Job ring %p", job_ring);

	/*
	 * Mask interrupts since we are going to poll
	 * for reset completion status
	 * Also, at POR, interrupts are ENABLED on a JR, thus
	 * this is the point where I can disable them without
	 * changing the code logic too much
	 */
	caam_jr_disable_irqs(job_ring->irq_fd);

	/* initiate flush (required prior to reset) */
	SET_JR_REG(JRCR, job_ring, JR_REG_JRCR_VAL_RESET);

	/* dummy read */
	tmp = GET_JR_REG(JRCR, job_ring);

	do {
		tmp = GET_JR_REG(JRINT, job_ring);
		usleep(usleep_interval);
	} while (((tmp & JRINT_ERR_HALT_MASK) ==
			JRINT_ERR_HALT_INPROGRESS) && --timeout);

	CAAM_JR_INFO("JRINT is %x", tmp);
	if ((tmp & JRINT_ERR_HALT_MASK) != JRINT_ERR_HALT_COMPLETE ||
		timeout == 0) {
		CAAM_JR_ERR("0x%x, %d", tmp, timeout);
		/* unmask interrupts */
		if (job_ring->jr_mode != SEC_NOTIFICATION_TYPE_POLL)
			caam_jr_enable_irqs(job_ring->irq_fd);
		return -1;
	}

	/* Initiate reset */
	timeout = SEC_TIMEOUT;
	SET_JR_REG(JRCR, job_ring, JR_REG_JRCR_VAL_RESET);

	do {
		tmp = GET_JR_REG(JRCR, job_ring);
		usleep(usleep_interval);
	} while ((tmp & JR_REG_JRCR_VAL_RESET) && --timeout);

	CAAM_JR_DEBUG("JRCR is %x", tmp);
	if (timeout == 0) {
		CAAM_JR_ERR("Failed to reset hw job ring %p", job_ring);
		/* unmask interrupts */
		if (job_ring->jr_mode != SEC_NOTIFICATION_TYPE_POLL)
			caam_jr_enable_irqs(job_ring->irq_fd);
		return -1;
	}
	/* unmask interrupts */
	if (job_ring->jr_mode != SEC_NOTIFICATION_TYPE_POLL)
		caam_jr_enable_irqs(job_ring->irq_fd);
	return 0;

}

void
hw_handle_job_ring_error(struct sec_job_ring_t *job_ring __rte_unused,
			 uint32_t error_code)
{
	union hw_error_code hw_err_code;

	hw_err_code.error = error_code;
	switch (hw_err_code.error_desc.value.ssrc) {
	case SEC_HW_ERR_SSRC_NO_SRC:
		ASSERT(hw_err_code.error_desc.no_status_src.res == 0);
		CAAM_JR_ERR("No Status Source ");
		break;
	case SEC_HW_ERR_SSRC_CCB_ERR:
		CAAM_JR_ERR("CCB Status Source");
		hw_handle_ccb_err(hw_err_code);
		break;
	case SEC_HW_ERR_SSRC_JMP_HALT_U:
		CAAM_JR_ERR("Jump Halt User Status Source");
		hw_handle_jmp_halt_user_err(hw_err_code);
		break;
	case SEC_HW_ERR_SSRC_DECO:
		CAAM_JR_ERR("DECO Status Source");
		hw_handle_deco_err(hw_err_code);
		break;
	case SEC_HW_ERR_SSRC_JR:
		CAAM_JR_ERR("Job Ring Status Source");
		hw_handle_jr_err(hw_err_code);
		break;
	case SEC_HW_ERR_SSRC_JMP_HALT_COND:
		CAAM_JR_ERR("Jump Halt Condition Codes");
		hw_handle_jmp_halt_cond_err(hw_err_code);
		break;
	default:
		ASSERT(0);
		CAAM_JR_ERR("Unknown SSRC");
		break;
	}
}

void
hw_job_ring_error_print(struct sec_job_ring_t *job_ring, int code)
{
	switch (code) {
	case JRINT_ERR_WRITE_STATUS:
		CAAM_JR_ERR("Error writing status to Output Ring ");
		break;
	case JRINT_ERR_BAD_INPUT_BASE:
		CAAM_JR_ERR(
		"Bad Input Ring Base (%p) (not on a 4-byte boundary) ",
		(void *)job_ring);
		break;
	case JRINT_ERR_BAD_OUTPUT_BASE:
		CAAM_JR_ERR(
		"Bad Output Ring Base (%p) (not on a 4-byte boundary) ",
		(void *)job_ring);
		break;
	case JRINT_ERR_WRITE_2_IRBA:
		CAAM_JR_ERR(
		"Invalid write to Input Ring Base Address Register ");
		break;
	case JRINT_ERR_WRITE_2_ORBA:
		CAAM_JR_ERR(
		"Invalid write to Output Ring Base Address Register ");
		break;
	case JRINT_ERR_RES_B4_HALT:
		CAAM_JR_ERR(
		"Job Ring [%p] released before Job Ring is halted",
		(void *)job_ring);
		break;
	case JRINT_ERR_REM_TOO_MANY:
		CAAM_JR_ERR("Removed too many jobs from job ring [%p]",
			(void *)job_ring);
		break;
	case JRINT_ERR_ADD_TOO_MANY:
		CAAM_JR_ERR("Added too many jobs on job ring [%p]", job_ring);
		break;
	default:
		CAAM_JR_ERR(" Unknown SEC JR Error :%d",
				code);
		break;
	}
}

int
hw_job_ring_set_coalescing_param(struct sec_job_ring_t *job_ring,
				 uint16_t irq_coalescing_timer,
				 uint8_t irq_coalescing_count)
{
	uint32_t reg_val = 0;

	ASSERT(job_ring != NULL);
	if (job_ring->register_base_addr == NULL) {
		CAAM_JR_ERR("Jr[%p] has reg base addr as NULL.driver not init",
			job_ring);
		return -1;
	}
	/* Set descriptor count coalescing */
	reg_val |= (irq_coalescing_count << JR_REG_JRCFG_LO_ICDCT_SHIFT);

	/* Set coalescing timer value */
	reg_val |= (irq_coalescing_timer << JR_REG_JRCFG_LO_ICTT_SHIFT);

	/* Update parameters in HW */
	SET_JR_REG_LO(JRCFG, job_ring, reg_val);
	CAAM_JR_DEBUG("Set coalescing params on jr %p timer:%d, desc count: %d",
			job_ring, irq_coalescing_timer, irq_coalescing_timer);

	return 0;
}

int
hw_job_ring_enable_coalescing(struct sec_job_ring_t *job_ring)
{
	uint32_t reg_val = 0;

	ASSERT(job_ring != NULL);
	if (job_ring->register_base_addr == NULL) {
		CAAM_JR_ERR("Jr[%p] has reg base addr as NULL.driver not init",
			job_ring);
		return -1;
	}

	/* Get the current value of the register */
	reg_val = GET_JR_REG_LO(JRCFG, job_ring);

	/* Enable coalescing */
	reg_val |= JR_REG_JRCFG_LO_ICEN_EN;

	/* Write in hw */
	SET_JR_REG_LO(JRCFG, job_ring, reg_val);

	CAAM_JR_DEBUG("Enabled coalescing on jr %p ",
			job_ring);

	return 0;
}

int
hw_job_ring_disable_coalescing(struct sec_job_ring_t *job_ring)
{
	uint32_t reg_val = 0;

	ASSERT(job_ring != NULL);

	if (job_ring->register_base_addr == NULL) {
		CAAM_JR_ERR("Jr[%p] has reg base addr as NULL.driver not init",
			job_ring);
		return -1;
	}

	/* Get the current value of the register */
	reg_val = GET_JR_REG_LO(JRCFG, job_ring);

	/* Disable coalescing */
	reg_val &= ~JR_REG_JRCFG_LO_ICEN_EN;

	/* Write in hw */
	SET_JR_REG_LO(JRCFG, job_ring, reg_val);
	CAAM_JR_DEBUG("Disabled coalescing on jr %p ", job_ring);

	return 0;
}