1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017-2018 NXP
8 #include <rte_common.h>
9 #include <rte_memory.h>
10 #include <rte_malloc.h>
11 #include <rte_crypto.h>
12 #include <rte_security.h>
14 #include <caam_jr_config.h>
15 #include <caam_jr_hw_specific.h>
16 #include <caam_jr_pvt.h>
17 #include <caam_jr_log.h>
19 /* RTA header files */
20 #include <hw/desc/common.h>
21 #include <hw/desc/algo.h>
22 #include <hw/desc/ipsec.h>
24 /* Used to retry resetting a job ring in SEC hardware. */
25 #define SEC_TIMEOUT 100000
27 /* @brief Process Jump Halt Condition related errors
29 * @param [in] error_code The error code in the descriptor status word
32 hw_handle_jmp_halt_cond_err(union hw_error_code error_code)
34 CAAM_JR_DEBUG("JMP: %d, Descriptor Index: 0x%x, Condition: 0x%x",
35 error_code.error_desc.jmp_halt_cond_src.jmp,
36 error_code.error_desc.jmp_halt_cond_src.desc_idx,
37 error_code.error_desc.jmp_halt_cond_src.cond);
41 /* @brief Process DECO related errors
43 * @param [in] error_code The error code in the descriptor status word
46 hw_handle_deco_err(union hw_error_code error_code)
48 CAAM_JR_DEBUG("JMP: %d, Descriptor Index: 0x%x",
49 error_code.error_desc.deco_src.jmp,
50 error_code.error_desc.deco_src.desc_idx);
52 switch (error_code.error_desc.deco_src.desc_err) {
53 case SEC_HW_ERR_DECO_HFN_THRESHOLD:
54 CAAM_JR_DEBUG(" Warning: Descriptor completed normally,"
55 "but 3GPP HFN matches or exceeds the Threshold ");
58 CAAM_JR_DEBUG("Error 0x%04x not implemented",
59 error_code.error_desc.deco_src.desc_err);
64 /* @brief Process Jump Halt User Status related errors
66 * @param [in] error_code The error code in the descriptor status word
69 hw_handle_jmp_halt_user_err(union hw_error_code error_code __rte_unused)
71 CAAM_JR_DEBUG(" Not implemented");
74 /* @brief Process CCB related errors
76 * @param [in] error_code The error code in the descriptor status word
79 hw_handle_ccb_err(union hw_error_code hw_error_code __rte_unused)
81 CAAM_JR_DEBUG(" Not implemented");
84 /* @brief Process Job Ring related errors
86 * @param [in] error_code The error code in the descriptor status word
89 hw_handle_jr_err(union hw_error_code hw_error_code __rte_unused)
91 CAAM_JR_DEBUG(" Not implemented");
95 hw_reset_job_ring(struct sec_job_ring_t *job_ring)
99 ASSERT(job_ring->register_base_addr != NULL);
101 /* First reset the job ring in hw */
102 ret = hw_shutdown_job_ring(job_ring);
103 SEC_ASSERT(ret == 0, ret, "Failed resetting job ring in hardware");
105 /* In order to have the HW JR in a workable state
106 * after a reset, I need to re-write the input
107 * queue size, input start address, output queue
108 * size and output start address
110 /* Write the JR input queue size to the HW register */
111 hw_set_input_ring_size(job_ring, SEC_JOB_RING_SIZE);
113 /* Write the JR output queue size to the HW register */
114 hw_set_output_ring_size(job_ring, SEC_JOB_RING_SIZE);
116 /* Write the JR input queue start address */
117 hw_set_input_ring_start_addr(job_ring,
118 caam_jr_dma_vtop(job_ring->input_ring));
119 CAAM_JR_DEBUG(" Set input ring base address to : Virtual: 0x%" PRIx64
120 ",Physical: 0x%" PRIx64 ", Read from HW: 0x%" PRIx64,
121 (uint64_t)(uintptr_t)job_ring->input_ring,
122 caam_jr_dma_vtop(job_ring->input_ring),
123 hw_get_inp_queue_base(job_ring));
125 /* Write the JR output queue start address */
126 hw_set_output_ring_start_addr(job_ring,
127 caam_jr_dma_vtop(job_ring->output_ring));
128 CAAM_JR_DEBUG(" Set output ring base address to: Virtual: 0x%" PRIx64
129 ",Physical: 0x%" PRIx64 ", Read from HW: 0x%" PRIx64,
130 (uint64_t)(uintptr_t)job_ring->output_ring,
131 caam_jr_dma_vtop(job_ring->output_ring),
132 hw_get_out_queue_base(job_ring));
137 hw_shutdown_job_ring(struct sec_job_ring_t *job_ring)
139 unsigned int timeout = SEC_TIMEOUT;
141 int usleep_interval = 10;
143 if (job_ring->register_base_addr == NULL) {
144 CAAM_JR_ERR("Jr[%p] has reg base addr as NULL.driver not init",
149 CAAM_JR_INFO("Resetting Job ring %p", job_ring);
152 * Mask interrupts since we are going to poll
153 * for reset completion status
154 * Also, at POR, interrupts are ENABLED on a JR, thus
155 * this is the point where I can disable them without
156 * changing the code logic too much
158 caam_jr_disable_irqs(job_ring->irq_fd);
160 /* initiate flush (required prior to reset) */
161 SET_JR_REG(JRCR, job_ring, JR_REG_JRCR_VAL_RESET);
164 tmp = GET_JR_REG(JRCR, job_ring);
167 tmp = GET_JR_REG(JRINT, job_ring);
168 usleep(usleep_interval);
169 } while (((tmp & JRINT_ERR_HALT_MASK) ==
170 JRINT_ERR_HALT_INPROGRESS) && --timeout);
172 CAAM_JR_INFO("JRINT is %x", tmp);
173 if ((tmp & JRINT_ERR_HALT_MASK) != JRINT_ERR_HALT_COMPLETE ||
175 CAAM_JR_ERR("0x%x, %d", tmp, timeout);
176 /* unmask interrupts */
177 if (job_ring->jr_mode != SEC_NOTIFICATION_TYPE_POLL)
178 caam_jr_enable_irqs(job_ring->irq_fd);
183 timeout = SEC_TIMEOUT;
184 SET_JR_REG(JRCR, job_ring, JR_REG_JRCR_VAL_RESET);
187 tmp = GET_JR_REG(JRCR, job_ring);
188 usleep(usleep_interval);
189 } while ((tmp & JR_REG_JRCR_VAL_RESET) && --timeout);
191 CAAM_JR_DEBUG("JRCR is %x", tmp);
193 CAAM_JR_ERR("Failed to reset hw job ring %p", job_ring);
194 /* unmask interrupts */
195 if (job_ring->jr_mode != SEC_NOTIFICATION_TYPE_POLL)
196 caam_jr_enable_irqs(job_ring->irq_fd);
199 /* unmask interrupts */
200 if (job_ring->jr_mode != SEC_NOTIFICATION_TYPE_POLL)
201 caam_jr_enable_irqs(job_ring->irq_fd);
207 hw_handle_job_ring_error(struct sec_job_ring_t *job_ring __rte_unused,
210 union hw_error_code hw_err_code;
212 hw_err_code.error = error_code;
213 switch (hw_err_code.error_desc.value.ssrc) {
214 case SEC_HW_ERR_SSRC_NO_SRC:
215 ASSERT(hw_err_code.error_desc.no_status_src.res == 0);
216 CAAM_JR_ERR("No Status Source ");
218 case SEC_HW_ERR_SSRC_CCB_ERR:
219 CAAM_JR_ERR("CCB Status Source");
220 hw_handle_ccb_err(hw_err_code);
222 case SEC_HW_ERR_SSRC_JMP_HALT_U:
223 CAAM_JR_ERR("Jump Halt User Status Source");
224 hw_handle_jmp_halt_user_err(hw_err_code);
226 case SEC_HW_ERR_SSRC_DECO:
227 CAAM_JR_ERR("DECO Status Source");
228 hw_handle_deco_err(hw_err_code);
230 case SEC_HW_ERR_SSRC_JR:
231 CAAM_JR_ERR("Job Ring Status Source");
232 hw_handle_jr_err(hw_err_code);
234 case SEC_HW_ERR_SSRC_JMP_HALT_COND:
235 CAAM_JR_ERR("Jump Halt Condition Codes");
236 hw_handle_jmp_halt_cond_err(hw_err_code);
240 CAAM_JR_ERR("Unknown SSRC");
246 hw_job_ring_error_print(struct sec_job_ring_t *job_ring, int code)
249 case JRINT_ERR_WRITE_STATUS:
250 CAAM_JR_ERR("Error writing status to Output Ring ");
252 case JRINT_ERR_BAD_INPUT_BASE:
254 "Bad Input Ring Base (%p) (not on a 4-byte boundary) ",
257 case JRINT_ERR_BAD_OUTPUT_BASE:
259 "Bad Output Ring Base (%p) (not on a 4-byte boundary) ",
262 case JRINT_ERR_WRITE_2_IRBA:
264 "Invalid write to Input Ring Base Address Register ");
266 case JRINT_ERR_WRITE_2_ORBA:
268 "Invalid write to Output Ring Base Address Register ");
270 case JRINT_ERR_RES_B4_HALT:
272 "Job Ring [%p] released before Job Ring is halted",
275 case JRINT_ERR_REM_TOO_MANY:
276 CAAM_JR_ERR("Removed too many jobs from job ring [%p]",
279 case JRINT_ERR_ADD_TOO_MANY:
280 CAAM_JR_ERR("Added too many jobs on job ring [%p]", job_ring);
283 CAAM_JR_ERR(" Unknown SEC JR Error :%d",
290 hw_job_ring_set_coalescing_param(struct sec_job_ring_t *job_ring,
291 uint16_t irq_coalescing_timer,
292 uint8_t irq_coalescing_count)
294 uint32_t reg_val = 0;
296 ASSERT(job_ring != NULL);
297 if (job_ring->register_base_addr == NULL) {
298 CAAM_JR_ERR("Jr[%p] has reg base addr as NULL.driver not init",
302 /* Set descriptor count coalescing */
303 reg_val |= (irq_coalescing_count << JR_REG_JRCFG_LO_ICDCT_SHIFT);
305 /* Set coalescing timer value */
306 reg_val |= (irq_coalescing_timer << JR_REG_JRCFG_LO_ICTT_SHIFT);
308 /* Update parameters in HW */
309 SET_JR_REG_LO(JRCFG, job_ring, reg_val);
310 CAAM_JR_DEBUG("Set coalescing params on jr %p timer:%d, desc count: %d",
311 job_ring, irq_coalescing_timer, irq_coalescing_timer);
317 hw_job_ring_enable_coalescing(struct sec_job_ring_t *job_ring)
319 uint32_t reg_val = 0;
321 ASSERT(job_ring != NULL);
322 if (job_ring->register_base_addr == NULL) {
323 CAAM_JR_ERR("Jr[%p] has reg base addr as NULL.driver not init",
328 /* Get the current value of the register */
329 reg_val = GET_JR_REG_LO(JRCFG, job_ring);
331 /* Enable coalescing */
332 reg_val |= JR_REG_JRCFG_LO_ICEN_EN;
335 SET_JR_REG_LO(JRCFG, job_ring, reg_val);
337 CAAM_JR_DEBUG("Enabled coalescing on jr %p ",
344 hw_job_ring_disable_coalescing(struct sec_job_ring_t *job_ring)
346 uint32_t reg_val = 0;
348 ASSERT(job_ring != NULL);
350 if (job_ring->register_base_addr == NULL) {
351 CAAM_JR_ERR("Jr[%p] has reg base addr as NULL.driver not init",
356 /* Get the current value of the register */
357 reg_val = GET_JR_REG_LO(JRCFG, job_ring);
359 /* Disable coalescing */
360 reg_val &= ~JR_REG_JRCFG_LO_ICEN_EN;
363 SET_JR_REG_LO(JRCFG, job_ring, reg_val);
364 CAAM_JR_DEBUG("Disabled coalescing on jr %p ", job_ring);