Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Cryptographic API. |
| 4 | * |
| 5 | * Support for SAHARA cryptographic accelerator. |
| 6 | * |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 7 | * Copyright (c) 2014 Steffen Trumtrar <s.trumtrar@pengutronix.de> |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 8 | * Copyright (c) 2013 Vista Silicon S.L. |
| 9 | * Author: Javier Martin <javier.martin@vista-silicon.com> |
| 10 | * |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 11 | * Based on omap-aes.c and tegra-aes.c |
| 12 | */ |
| 13 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 14 | #include <crypto/aes.h> |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 15 | #include <crypto/internal/hash.h> |
Herbert Xu | 678adec | 2016-06-29 18:04:05 +0800 | [diff] [blame] | 16 | #include <crypto/internal/skcipher.h> |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 17 | #include <crypto/scatterwalk.h> |
Eric Biggers | a24d22b | 2020-11-12 21:20:21 -0800 | [diff] [blame] | 18 | #include <crypto/sha1.h> |
| 19 | #include <crypto/sha2.h> |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 20 | |
| 21 | #include <linux/clk.h> |
Herbert Xu | 0c3dc78 | 2020-08-19 21:58:20 +1000 | [diff] [blame] | 22 | #include <linux/dma-mapping.h> |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/io.h> |
| 25 | #include <linux/irq.h> |
| 26 | #include <linux/kernel.h> |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 27 | #include <linux/kthread.h> |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/of.h> |
Steffen Trumtrar | 5ed903b | 2014-12-01 13:26:32 +0100 | [diff] [blame] | 30 | #include <linux/of_device.h> |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 31 | #include <linux/platform_device.h> |
Zhengchao Shao | 108586e | 2022-07-25 12:09:28 +0800 | [diff] [blame] | 32 | #include <linux/spinlock.h> |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 33 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 34 | #define SHA_BUFFER_LEN PAGE_SIZE |
| 35 | #define SAHARA_MAX_SHA_BLOCK_SIZE SHA256_BLOCK_SIZE |
| 36 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 37 | #define SAHARA_NAME "sahara" |
| 38 | #define SAHARA_VERSION_3 3 |
Steffen Trumtrar | 5ed903b | 2014-12-01 13:26:32 +0100 | [diff] [blame] | 39 | #define SAHARA_VERSION_4 4 |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 40 | #define SAHARA_TIMEOUT_MS 1000 |
| 41 | #define SAHARA_MAX_HW_DESC 2 |
| 42 | #define SAHARA_MAX_HW_LINK 20 |
| 43 | |
| 44 | #define FLAGS_MODE_MASK 0x000f |
| 45 | #define FLAGS_ENCRYPT BIT(0) |
| 46 | #define FLAGS_CBC BIT(1) |
| 47 | #define FLAGS_NEW_KEY BIT(3) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 48 | |
| 49 | #define SAHARA_HDR_BASE 0x00800000 |
| 50 | #define SAHARA_HDR_SKHA_ALG_AES 0 |
| 51 | #define SAHARA_HDR_SKHA_OP_ENC (1 << 2) |
| 52 | #define SAHARA_HDR_SKHA_MODE_ECB (0 << 3) |
| 53 | #define SAHARA_HDR_SKHA_MODE_CBC (1 << 3) |
| 54 | #define SAHARA_HDR_FORM_DATA (5 << 16) |
| 55 | #define SAHARA_HDR_FORM_KEY (8 << 16) |
| 56 | #define SAHARA_HDR_LLO (1 << 24) |
| 57 | #define SAHARA_HDR_CHA_SKHA (1 << 28) |
| 58 | #define SAHARA_HDR_CHA_MDHA (2 << 28) |
| 59 | #define SAHARA_HDR_PARITY_BIT (1 << 31) |
| 60 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 61 | #define SAHARA_HDR_MDHA_SET_MODE_MD_KEY 0x20880000 |
| 62 | #define SAHARA_HDR_MDHA_SET_MODE_HASH 0x208D0000 |
| 63 | #define SAHARA_HDR_MDHA_HASH 0xA0850000 |
| 64 | #define SAHARA_HDR_MDHA_STORE_DIGEST 0x20820000 |
| 65 | #define SAHARA_HDR_MDHA_ALG_SHA1 0 |
| 66 | #define SAHARA_HDR_MDHA_ALG_MD5 1 |
| 67 | #define SAHARA_HDR_MDHA_ALG_SHA256 2 |
| 68 | #define SAHARA_HDR_MDHA_ALG_SHA224 3 |
| 69 | #define SAHARA_HDR_MDHA_PDATA (1 << 2) |
| 70 | #define SAHARA_HDR_MDHA_HMAC (1 << 3) |
| 71 | #define SAHARA_HDR_MDHA_INIT (1 << 5) |
| 72 | #define SAHARA_HDR_MDHA_IPAD (1 << 6) |
| 73 | #define SAHARA_HDR_MDHA_OPAD (1 << 7) |
| 74 | #define SAHARA_HDR_MDHA_SWAP (1 << 8) |
| 75 | #define SAHARA_HDR_MDHA_MAC_FULL (1 << 9) |
| 76 | #define SAHARA_HDR_MDHA_SSL (1 << 10) |
| 77 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 78 | /* SAHARA can only process one request at a time */ |
| 79 | #define SAHARA_QUEUE_LENGTH 1 |
| 80 | |
| 81 | #define SAHARA_REG_VERSION 0x00 |
| 82 | #define SAHARA_REG_DAR 0x04 |
| 83 | #define SAHARA_REG_CONTROL 0x08 |
| 84 | #define SAHARA_CONTROL_SET_THROTTLE(x) (((x) & 0xff) << 24) |
| 85 | #define SAHARA_CONTROL_SET_MAXBURST(x) (((x) & 0xff) << 16) |
| 86 | #define SAHARA_CONTROL_RNG_AUTORSD (1 << 7) |
| 87 | #define SAHARA_CONTROL_ENABLE_INT (1 << 4) |
| 88 | #define SAHARA_REG_CMD 0x0C |
| 89 | #define SAHARA_CMD_RESET (1 << 0) |
| 90 | #define SAHARA_CMD_CLEAR_INT (1 << 8) |
| 91 | #define SAHARA_CMD_CLEAR_ERR (1 << 9) |
| 92 | #define SAHARA_CMD_SINGLE_STEP (1 << 10) |
| 93 | #define SAHARA_CMD_MODE_BATCH (1 << 16) |
| 94 | #define SAHARA_CMD_MODE_DEBUG (1 << 18) |
| 95 | #define SAHARA_REG_STATUS 0x10 |
| 96 | #define SAHARA_STATUS_GET_STATE(x) ((x) & 0x7) |
| 97 | #define SAHARA_STATE_IDLE 0 |
| 98 | #define SAHARA_STATE_BUSY 1 |
| 99 | #define SAHARA_STATE_ERR 2 |
| 100 | #define SAHARA_STATE_FAULT 3 |
| 101 | #define SAHARA_STATE_COMPLETE 4 |
| 102 | #define SAHARA_STATE_COMP_FLAG (1 << 2) |
| 103 | #define SAHARA_STATUS_DAR_FULL (1 << 3) |
| 104 | #define SAHARA_STATUS_ERROR (1 << 4) |
| 105 | #define SAHARA_STATUS_SECURE (1 << 5) |
| 106 | #define SAHARA_STATUS_FAIL (1 << 6) |
| 107 | #define SAHARA_STATUS_INIT (1 << 7) |
| 108 | #define SAHARA_STATUS_RNG_RESEED (1 << 8) |
| 109 | #define SAHARA_STATUS_ACTIVE_RNG (1 << 9) |
| 110 | #define SAHARA_STATUS_ACTIVE_MDHA (1 << 10) |
| 111 | #define SAHARA_STATUS_ACTIVE_SKHA (1 << 11) |
| 112 | #define SAHARA_STATUS_MODE_BATCH (1 << 16) |
| 113 | #define SAHARA_STATUS_MODE_DEDICATED (1 << 17) |
| 114 | #define SAHARA_STATUS_MODE_DEBUG (1 << 18) |
| 115 | #define SAHARA_STATUS_GET_ISTATE(x) (((x) >> 24) & 0xff) |
| 116 | #define SAHARA_REG_ERRSTATUS 0x14 |
| 117 | #define SAHARA_ERRSTATUS_GET_SOURCE(x) ((x) & 0xf) |
| 118 | #define SAHARA_ERRSOURCE_CHA 14 |
| 119 | #define SAHARA_ERRSOURCE_DMA 15 |
| 120 | #define SAHARA_ERRSTATUS_DMA_DIR (1 << 8) |
| 121 | #define SAHARA_ERRSTATUS_GET_DMASZ(x)(((x) >> 9) & 0x3) |
| 122 | #define SAHARA_ERRSTATUS_GET_DMASRC(x) (((x) >> 13) & 0x7) |
| 123 | #define SAHARA_ERRSTATUS_GET_CHASRC(x) (((x) >> 16) & 0xfff) |
| 124 | #define SAHARA_ERRSTATUS_GET_CHAERR(x) (((x) >> 28) & 0x3) |
| 125 | #define SAHARA_REG_FADDR 0x18 |
| 126 | #define SAHARA_REG_CDAR 0x1C |
| 127 | #define SAHARA_REG_IDAR 0x20 |
| 128 | |
| 129 | struct sahara_hw_desc { |
Arnd Bergmann | 75d3f81 | 2015-12-08 16:23:51 +0100 | [diff] [blame] | 130 | u32 hdr; |
| 131 | u32 len1; |
| 132 | u32 p1; |
| 133 | u32 len2; |
| 134 | u32 p2; |
| 135 | u32 next; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | struct sahara_hw_link { |
Arnd Bergmann | 75d3f81 | 2015-12-08 16:23:51 +0100 | [diff] [blame] | 139 | u32 len; |
| 140 | u32 p; |
| 141 | u32 next; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | struct sahara_ctx { |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 145 | unsigned long flags; |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 146 | |
| 147 | /* AES-specific context */ |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 148 | int keylen; |
| 149 | u8 key[AES_KEYSIZE_128]; |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 150 | struct crypto_skcipher *fallback; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | struct sahara_aes_reqctx { |
| 154 | unsigned long mode; |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 155 | struct skcipher_request fallback_req; // keep at the end |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 156 | }; |
| 157 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 158 | /* |
| 159 | * struct sahara_sha_reqctx - private data per request |
| 160 | * @buf: holds data for requests smaller than block_size |
| 161 | * @rembuf: used to prepare one block_size-aligned request |
| 162 | * @context: hw-specific context for request. Digest is extracted from this |
| 163 | * @mode: specifies what type of hw-descriptor needs to be built |
| 164 | * @digest_size: length of digest for this request |
| 165 | * @context_size: length of hw-context for this request. |
| 166 | * Always digest_size + 4 |
| 167 | * @buf_cnt: number of bytes saved in buf |
| 168 | * @sg_in_idx: number of hw links |
| 169 | * @in_sg: scatterlist for input data |
| 170 | * @in_sg_chain: scatterlists for chained input data |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 171 | * @total: total number of bytes for transfer |
| 172 | * @last: is this the last block |
| 173 | * @first: is this the first block |
| 174 | * @active: inside a transfer |
| 175 | */ |
| 176 | struct sahara_sha_reqctx { |
| 177 | u8 buf[SAHARA_MAX_SHA_BLOCK_SIZE]; |
| 178 | u8 rembuf[SAHARA_MAX_SHA_BLOCK_SIZE]; |
| 179 | u8 context[SHA256_DIGEST_SIZE + 4]; |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 180 | unsigned int mode; |
| 181 | unsigned int digest_size; |
| 182 | unsigned int context_size; |
| 183 | unsigned int buf_cnt; |
| 184 | unsigned int sg_in_idx; |
| 185 | struct scatterlist *in_sg; |
| 186 | struct scatterlist in_sg_chain[2]; |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 187 | size_t total; |
| 188 | unsigned int last; |
| 189 | unsigned int first; |
| 190 | unsigned int active; |
| 191 | }; |
| 192 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 193 | struct sahara_dev { |
| 194 | struct device *device; |
Steffen Trumtrar | 5ed903b | 2014-12-01 13:26:32 +0100 | [diff] [blame] | 195 | unsigned int version; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 196 | void __iomem *regs_base; |
| 197 | struct clk *clk_ipg; |
| 198 | struct clk *clk_ahb; |
Zhengchao Shao | 108586e | 2022-07-25 12:09:28 +0800 | [diff] [blame] | 199 | spinlock_t queue_spinlock; |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 200 | struct task_struct *kthread; |
| 201 | struct completion dma_completion; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 202 | |
| 203 | struct sahara_ctx *ctx; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 204 | struct crypto_queue queue; |
| 205 | unsigned long flags; |
| 206 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 207 | struct sahara_hw_desc *hw_desc[SAHARA_MAX_HW_DESC]; |
| 208 | dma_addr_t hw_phys_desc[SAHARA_MAX_HW_DESC]; |
| 209 | |
| 210 | u8 *key_base; |
| 211 | dma_addr_t key_phys_base; |
| 212 | |
| 213 | u8 *iv_base; |
| 214 | dma_addr_t iv_phys_base; |
| 215 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 216 | u8 *context_base; |
| 217 | dma_addr_t context_phys_base; |
| 218 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 219 | struct sahara_hw_link *hw_link[SAHARA_MAX_HW_LINK]; |
| 220 | dma_addr_t hw_phys_link[SAHARA_MAX_HW_LINK]; |
| 221 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 222 | size_t total; |
| 223 | struct scatterlist *in_sg; |
LABBE Corentin | f8e28a0 | 2015-11-19 13:38:17 +0100 | [diff] [blame] | 224 | int nb_in_sg; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 225 | struct scatterlist *out_sg; |
LABBE Corentin | f8e28a0 | 2015-11-19 13:38:17 +0100 | [diff] [blame] | 226 | int nb_out_sg; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 227 | |
| 228 | u32 error; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | static struct sahara_dev *dev_ptr; |
| 232 | |
| 233 | static inline void sahara_write(struct sahara_dev *dev, u32 data, u32 reg) |
| 234 | { |
| 235 | writel(data, dev->regs_base + reg); |
| 236 | } |
| 237 | |
| 238 | static inline unsigned int sahara_read(struct sahara_dev *dev, u32 reg) |
| 239 | { |
| 240 | return readl(dev->regs_base + reg); |
| 241 | } |
| 242 | |
| 243 | static u32 sahara_aes_key_hdr(struct sahara_dev *dev) |
| 244 | { |
| 245 | u32 hdr = SAHARA_HDR_BASE | SAHARA_HDR_SKHA_ALG_AES | |
| 246 | SAHARA_HDR_FORM_KEY | SAHARA_HDR_LLO | |
| 247 | SAHARA_HDR_CHA_SKHA | SAHARA_HDR_PARITY_BIT; |
| 248 | |
| 249 | if (dev->flags & FLAGS_CBC) { |
| 250 | hdr |= SAHARA_HDR_SKHA_MODE_CBC; |
| 251 | hdr ^= SAHARA_HDR_PARITY_BIT; |
| 252 | } |
| 253 | |
| 254 | if (dev->flags & FLAGS_ENCRYPT) { |
| 255 | hdr |= SAHARA_HDR_SKHA_OP_ENC; |
| 256 | hdr ^= SAHARA_HDR_PARITY_BIT; |
| 257 | } |
| 258 | |
| 259 | return hdr; |
| 260 | } |
| 261 | |
| 262 | static u32 sahara_aes_data_link_hdr(struct sahara_dev *dev) |
| 263 | { |
| 264 | return SAHARA_HDR_BASE | SAHARA_HDR_FORM_DATA | |
| 265 | SAHARA_HDR_CHA_SKHA | SAHARA_HDR_PARITY_BIT; |
| 266 | } |
| 267 | |
LABBE Corentin | cac367b | 2015-10-14 21:14:19 +0200 | [diff] [blame] | 268 | static const char *sahara_err_src[16] = { |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 269 | "No error", |
| 270 | "Header error", |
| 271 | "Descriptor length error", |
| 272 | "Descriptor length or pointer error", |
| 273 | "Link length error", |
| 274 | "Link pointer error", |
| 275 | "Input buffer error", |
| 276 | "Output buffer error", |
| 277 | "Output buffer starvation", |
| 278 | "Internal state fault", |
| 279 | "General descriptor problem", |
| 280 | "Reserved", |
| 281 | "Descriptor address error", |
| 282 | "Link address error", |
| 283 | "CHA error", |
| 284 | "DMA error" |
| 285 | }; |
| 286 | |
LABBE Corentin | cac367b | 2015-10-14 21:14:19 +0200 | [diff] [blame] | 287 | static const char *sahara_err_dmasize[4] = { |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 288 | "Byte transfer", |
| 289 | "Half-word transfer", |
| 290 | "Word transfer", |
| 291 | "Reserved" |
| 292 | }; |
| 293 | |
LABBE Corentin | cac367b | 2015-10-14 21:14:19 +0200 | [diff] [blame] | 294 | static const char *sahara_err_dmasrc[8] = { |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 295 | "No error", |
| 296 | "AHB bus error", |
| 297 | "Internal IP bus error", |
| 298 | "Parity error", |
| 299 | "DMA crosses 256 byte boundary", |
| 300 | "DMA is busy", |
| 301 | "Reserved", |
| 302 | "DMA HW error" |
| 303 | }; |
| 304 | |
LABBE Corentin | cac367b | 2015-10-14 21:14:19 +0200 | [diff] [blame] | 305 | static const char *sahara_cha_errsrc[12] = { |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 306 | "Input buffer non-empty", |
| 307 | "Illegal address", |
| 308 | "Illegal mode", |
| 309 | "Illegal data size", |
| 310 | "Illegal key size", |
| 311 | "Write during processing", |
| 312 | "CTX read during processing", |
| 313 | "HW error", |
| 314 | "Input buffer disabled/underflow", |
| 315 | "Output buffer disabled/overflow", |
| 316 | "DES key parity error", |
| 317 | "Reserved" |
| 318 | }; |
| 319 | |
LABBE Corentin | cac367b | 2015-10-14 21:14:19 +0200 | [diff] [blame] | 320 | static const char *sahara_cha_err[4] = { "No error", "SKHA", "MDHA", "RNG" }; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 321 | |
| 322 | static void sahara_decode_error(struct sahara_dev *dev, unsigned int error) |
| 323 | { |
| 324 | u8 source = SAHARA_ERRSTATUS_GET_SOURCE(error); |
| 325 | u16 chasrc = ffs(SAHARA_ERRSTATUS_GET_CHASRC(error)); |
| 326 | |
| 327 | dev_err(dev->device, "%s: Error Register = 0x%08x\n", __func__, error); |
| 328 | |
| 329 | dev_err(dev->device, " - %s.\n", sahara_err_src[source]); |
| 330 | |
| 331 | if (source == SAHARA_ERRSOURCE_DMA) { |
| 332 | if (error & SAHARA_ERRSTATUS_DMA_DIR) |
| 333 | dev_err(dev->device, " * DMA read.\n"); |
| 334 | else |
| 335 | dev_err(dev->device, " * DMA write.\n"); |
| 336 | |
| 337 | dev_err(dev->device, " * %s.\n", |
| 338 | sahara_err_dmasize[SAHARA_ERRSTATUS_GET_DMASZ(error)]); |
| 339 | dev_err(dev->device, " * %s.\n", |
| 340 | sahara_err_dmasrc[SAHARA_ERRSTATUS_GET_DMASRC(error)]); |
| 341 | } else if (source == SAHARA_ERRSOURCE_CHA) { |
| 342 | dev_err(dev->device, " * %s.\n", |
| 343 | sahara_cha_errsrc[chasrc]); |
| 344 | dev_err(dev->device, " * %s.\n", |
| 345 | sahara_cha_err[SAHARA_ERRSTATUS_GET_CHAERR(error)]); |
| 346 | } |
| 347 | dev_err(dev->device, "\n"); |
| 348 | } |
| 349 | |
LABBE Corentin | cac367b | 2015-10-14 21:14:19 +0200 | [diff] [blame] | 350 | static const char *sahara_state[4] = { "Idle", "Busy", "Error", "HW Fault" }; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 351 | |
| 352 | static void sahara_decode_status(struct sahara_dev *dev, unsigned int status) |
| 353 | { |
| 354 | u8 state; |
| 355 | |
Joe Perches | 222f6b8 | 2019-04-09 09:33:13 -0700 | [diff] [blame] | 356 | if (!__is_defined(DEBUG)) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 357 | return; |
| 358 | |
| 359 | state = SAHARA_STATUS_GET_STATE(status); |
| 360 | |
| 361 | dev_dbg(dev->device, "%s: Status Register = 0x%08x\n", |
| 362 | __func__, status); |
| 363 | |
| 364 | dev_dbg(dev->device, " - State = %d:\n", state); |
| 365 | if (state & SAHARA_STATE_COMP_FLAG) |
| 366 | dev_dbg(dev->device, " * Descriptor completed. IRQ pending.\n"); |
| 367 | |
| 368 | dev_dbg(dev->device, " * %s.\n", |
| 369 | sahara_state[state & ~SAHARA_STATE_COMP_FLAG]); |
| 370 | |
| 371 | if (status & SAHARA_STATUS_DAR_FULL) |
| 372 | dev_dbg(dev->device, " - DAR Full.\n"); |
| 373 | if (status & SAHARA_STATUS_ERROR) |
| 374 | dev_dbg(dev->device, " - Error.\n"); |
| 375 | if (status & SAHARA_STATUS_SECURE) |
| 376 | dev_dbg(dev->device, " - Secure.\n"); |
| 377 | if (status & SAHARA_STATUS_FAIL) |
| 378 | dev_dbg(dev->device, " - Fail.\n"); |
| 379 | if (status & SAHARA_STATUS_RNG_RESEED) |
| 380 | dev_dbg(dev->device, " - RNG Reseed Request.\n"); |
| 381 | if (status & SAHARA_STATUS_ACTIVE_RNG) |
| 382 | dev_dbg(dev->device, " - RNG Active.\n"); |
| 383 | if (status & SAHARA_STATUS_ACTIVE_MDHA) |
| 384 | dev_dbg(dev->device, " - MDHA Active.\n"); |
| 385 | if (status & SAHARA_STATUS_ACTIVE_SKHA) |
| 386 | dev_dbg(dev->device, " - SKHA Active.\n"); |
| 387 | |
| 388 | if (status & SAHARA_STATUS_MODE_BATCH) |
| 389 | dev_dbg(dev->device, " - Batch Mode.\n"); |
| 390 | else if (status & SAHARA_STATUS_MODE_DEDICATED) |
Colin Ian King | 9ae811f | 2016-10-25 12:07:27 +0100 | [diff] [blame] | 391 | dev_dbg(dev->device, " - Dedicated Mode.\n"); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 392 | else if (status & SAHARA_STATUS_MODE_DEBUG) |
| 393 | dev_dbg(dev->device, " - Debug Mode.\n"); |
| 394 | |
| 395 | dev_dbg(dev->device, " - Internal state = 0x%02x\n", |
| 396 | SAHARA_STATUS_GET_ISTATE(status)); |
| 397 | |
| 398 | dev_dbg(dev->device, "Current DAR: 0x%08x\n", |
| 399 | sahara_read(dev, SAHARA_REG_CDAR)); |
| 400 | dev_dbg(dev->device, "Initial DAR: 0x%08x\n\n", |
| 401 | sahara_read(dev, SAHARA_REG_IDAR)); |
| 402 | } |
| 403 | |
| 404 | static void sahara_dump_descriptors(struct sahara_dev *dev) |
| 405 | { |
| 406 | int i; |
| 407 | |
Joe Perches | 222f6b8 | 2019-04-09 09:33:13 -0700 | [diff] [blame] | 408 | if (!__is_defined(DEBUG)) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 409 | return; |
| 410 | |
| 411 | for (i = 0; i < SAHARA_MAX_HW_DESC; i++) { |
Arnd Bergmann | d4b98f2 | 2015-12-08 16:24:22 +0100 | [diff] [blame] | 412 | dev_dbg(dev->device, "Descriptor (%d) (%pad):\n", |
| 413 | i, &dev->hw_phys_desc[i]); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 414 | dev_dbg(dev->device, "\thdr = 0x%08x\n", dev->hw_desc[i]->hdr); |
| 415 | dev_dbg(dev->device, "\tlen1 = %u\n", dev->hw_desc[i]->len1); |
| 416 | dev_dbg(dev->device, "\tp1 = 0x%08x\n", dev->hw_desc[i]->p1); |
| 417 | dev_dbg(dev->device, "\tlen2 = %u\n", dev->hw_desc[i]->len2); |
| 418 | dev_dbg(dev->device, "\tp2 = 0x%08x\n", dev->hw_desc[i]->p2); |
| 419 | dev_dbg(dev->device, "\tnext = 0x%08x\n", |
| 420 | dev->hw_desc[i]->next); |
| 421 | } |
| 422 | dev_dbg(dev->device, "\n"); |
| 423 | } |
| 424 | |
| 425 | static void sahara_dump_links(struct sahara_dev *dev) |
| 426 | { |
| 427 | int i; |
| 428 | |
Joe Perches | 222f6b8 | 2019-04-09 09:33:13 -0700 | [diff] [blame] | 429 | if (!__is_defined(DEBUG)) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 430 | return; |
| 431 | |
| 432 | for (i = 0; i < SAHARA_MAX_HW_LINK; i++) { |
Arnd Bergmann | d4b98f2 | 2015-12-08 16:24:22 +0100 | [diff] [blame] | 433 | dev_dbg(dev->device, "Link (%d) (%pad):\n", |
| 434 | i, &dev->hw_phys_link[i]); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 435 | dev_dbg(dev->device, "\tlen = %u\n", dev->hw_link[i]->len); |
| 436 | dev_dbg(dev->device, "\tp = 0x%08x\n", dev->hw_link[i]->p); |
| 437 | dev_dbg(dev->device, "\tnext = 0x%08x\n", |
| 438 | dev->hw_link[i]->next); |
| 439 | } |
| 440 | dev_dbg(dev->device, "\n"); |
| 441 | } |
| 442 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 443 | static int sahara_hw_descriptor_create(struct sahara_dev *dev) |
| 444 | { |
| 445 | struct sahara_ctx *ctx = dev->ctx; |
| 446 | struct scatterlist *sg; |
| 447 | int ret; |
| 448 | int i, j; |
Steffen Trumtrar | 1711045 | 2015-04-07 17:13:42 +0200 | [diff] [blame] | 449 | int idx = 0; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 450 | |
| 451 | /* Copy new key if necessary */ |
| 452 | if (ctx->flags & FLAGS_NEW_KEY) { |
| 453 | memcpy(dev->key_base, ctx->key, ctx->keylen); |
| 454 | ctx->flags &= ~FLAGS_NEW_KEY; |
| 455 | |
| 456 | if (dev->flags & FLAGS_CBC) { |
Steffen Trumtrar | 1711045 | 2015-04-07 17:13:42 +0200 | [diff] [blame] | 457 | dev->hw_desc[idx]->len1 = AES_BLOCK_SIZE; |
| 458 | dev->hw_desc[idx]->p1 = dev->iv_phys_base; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 459 | } else { |
Steffen Trumtrar | 1711045 | 2015-04-07 17:13:42 +0200 | [diff] [blame] | 460 | dev->hw_desc[idx]->len1 = 0; |
| 461 | dev->hw_desc[idx]->p1 = 0; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 462 | } |
Steffen Trumtrar | 1711045 | 2015-04-07 17:13:42 +0200 | [diff] [blame] | 463 | dev->hw_desc[idx]->len2 = ctx->keylen; |
| 464 | dev->hw_desc[idx]->p2 = dev->key_phys_base; |
| 465 | dev->hw_desc[idx]->next = dev->hw_phys_desc[1]; |
| 466 | |
| 467 | dev->hw_desc[idx]->hdr = sahara_aes_key_hdr(dev); |
| 468 | |
| 469 | idx++; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 470 | } |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 471 | |
LABBE Corentin | d23afa1 | 2015-09-18 14:57:11 +0200 | [diff] [blame] | 472 | dev->nb_in_sg = sg_nents_for_len(dev->in_sg, dev->total); |
LABBE Corentin | 6c2b74d | 2015-11-04 21:13:35 +0100 | [diff] [blame] | 473 | if (dev->nb_in_sg < 0) { |
| 474 | dev_err(dev->device, "Invalid numbers of src SG.\n"); |
| 475 | return dev->nb_in_sg; |
| 476 | } |
LABBE Corentin | d23afa1 | 2015-09-18 14:57:11 +0200 | [diff] [blame] | 477 | dev->nb_out_sg = sg_nents_for_len(dev->out_sg, dev->total); |
LABBE Corentin | 6c2b74d | 2015-11-04 21:13:35 +0100 | [diff] [blame] | 478 | if (dev->nb_out_sg < 0) { |
| 479 | dev_err(dev->device, "Invalid numbers of dst SG.\n"); |
| 480 | return dev->nb_out_sg; |
| 481 | } |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 482 | if ((dev->nb_in_sg + dev->nb_out_sg) > SAHARA_MAX_HW_LINK) { |
| 483 | dev_err(dev->device, "not enough hw links (%d)\n", |
| 484 | dev->nb_in_sg + dev->nb_out_sg); |
| 485 | return -EINVAL; |
| 486 | } |
| 487 | |
| 488 | ret = dma_map_sg(dev->device, dev->in_sg, dev->nb_in_sg, |
| 489 | DMA_TO_DEVICE); |
Jack Wang | 66f0b6b | 2022-08-19 08:07:50 +0200 | [diff] [blame] | 490 | if (!ret) { |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 491 | dev_err(dev->device, "couldn't map in sg\n"); |
| 492 | goto unmap_in; |
| 493 | } |
| 494 | ret = dma_map_sg(dev->device, dev->out_sg, dev->nb_out_sg, |
| 495 | DMA_FROM_DEVICE); |
Jack Wang | 66f0b6b | 2022-08-19 08:07:50 +0200 | [diff] [blame] | 496 | if (!ret) { |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 497 | dev_err(dev->device, "couldn't map out sg\n"); |
| 498 | goto unmap_out; |
| 499 | } |
| 500 | |
| 501 | /* Create input links */ |
Steffen Trumtrar | 1711045 | 2015-04-07 17:13:42 +0200 | [diff] [blame] | 502 | dev->hw_desc[idx]->p1 = dev->hw_phys_link[0]; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 503 | sg = dev->in_sg; |
| 504 | for (i = 0; i < dev->nb_in_sg; i++) { |
| 505 | dev->hw_link[i]->len = sg->length; |
| 506 | dev->hw_link[i]->p = sg->dma_address; |
| 507 | if (i == (dev->nb_in_sg - 1)) { |
| 508 | dev->hw_link[i]->next = 0; |
| 509 | } else { |
| 510 | dev->hw_link[i]->next = dev->hw_phys_link[i + 1]; |
| 511 | sg = sg_next(sg); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /* Create output links */ |
Steffen Trumtrar | 1711045 | 2015-04-07 17:13:42 +0200 | [diff] [blame] | 516 | dev->hw_desc[idx]->p2 = dev->hw_phys_link[i]; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 517 | sg = dev->out_sg; |
| 518 | for (j = i; j < dev->nb_out_sg + i; j++) { |
| 519 | dev->hw_link[j]->len = sg->length; |
| 520 | dev->hw_link[j]->p = sg->dma_address; |
| 521 | if (j == (dev->nb_out_sg + i - 1)) { |
| 522 | dev->hw_link[j]->next = 0; |
| 523 | } else { |
| 524 | dev->hw_link[j]->next = dev->hw_phys_link[j + 1]; |
| 525 | sg = sg_next(sg); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | /* Fill remaining fields of hw_desc[1] */ |
Steffen Trumtrar | 1711045 | 2015-04-07 17:13:42 +0200 | [diff] [blame] | 530 | dev->hw_desc[idx]->hdr = sahara_aes_data_link_hdr(dev); |
| 531 | dev->hw_desc[idx]->len1 = dev->total; |
| 532 | dev->hw_desc[idx]->len2 = dev->total; |
| 533 | dev->hw_desc[idx]->next = 0; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 534 | |
| 535 | sahara_dump_descriptors(dev); |
| 536 | sahara_dump_links(dev); |
| 537 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 538 | sahara_write(dev, dev->hw_phys_desc[0], SAHARA_REG_DAR); |
| 539 | |
| 540 | return 0; |
| 541 | |
| 542 | unmap_out: |
| 543 | dma_unmap_sg(dev->device, dev->out_sg, dev->nb_out_sg, |
Mogens Lauridsen | 1e32041 | 2017-08-03 15:34:12 +0200 | [diff] [blame] | 544 | DMA_FROM_DEVICE); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 545 | unmap_in: |
| 546 | dma_unmap_sg(dev->device, dev->in_sg, dev->nb_in_sg, |
Mogens Lauridsen | 1e32041 | 2017-08-03 15:34:12 +0200 | [diff] [blame] | 547 | DMA_TO_DEVICE); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 548 | |
| 549 | return -EINVAL; |
| 550 | } |
| 551 | |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 552 | static int sahara_aes_process(struct skcipher_request *req) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 553 | { |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 554 | struct sahara_dev *dev = dev_ptr; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 555 | struct sahara_ctx *ctx; |
| 556 | struct sahara_aes_reqctx *rctx; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 557 | int ret; |
Nicholas Mc Guire | 58ed798 | 2015-02-07 06:17:13 -0500 | [diff] [blame] | 558 | unsigned long timeout; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 559 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 560 | /* Request is ready to be dispatched by the device */ |
| 561 | dev_dbg(dev->device, |
| 562 | "dispatch request (nbytes=%d, src=%p, dst=%p)\n", |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 563 | req->cryptlen, req->src, req->dst); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 564 | |
| 565 | /* assign new request to device */ |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 566 | dev->total = req->cryptlen; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 567 | dev->in_sg = req->src; |
| 568 | dev->out_sg = req->dst; |
| 569 | |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 570 | rctx = skcipher_request_ctx(req); |
| 571 | ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req)); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 572 | rctx->mode &= FLAGS_MODE_MASK; |
| 573 | dev->flags = (dev->flags & ~FLAGS_MODE_MASK) | rctx->mode; |
| 574 | |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 575 | if ((dev->flags & FLAGS_CBC) && req->iv) |
| 576 | memcpy(dev->iv_base, req->iv, AES_KEYSIZE_128); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 577 | |
| 578 | /* assign new context to device */ |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 579 | dev->ctx = ctx; |
| 580 | |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 581 | reinit_completion(&dev->dma_completion); |
| 582 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 583 | ret = sahara_hw_descriptor_create(dev); |
Nicholas Mc Guire | 6cf02fc | 2015-02-07 06:27:45 -0500 | [diff] [blame] | 584 | if (ret) |
| 585 | return -EINVAL; |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 586 | |
Nicholas Mc Guire | 58ed798 | 2015-02-07 06:17:13 -0500 | [diff] [blame] | 587 | timeout = wait_for_completion_timeout(&dev->dma_completion, |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 588 | msecs_to_jiffies(SAHARA_TIMEOUT_MS)); |
Nicholas Mc Guire | 58ed798 | 2015-02-07 06:17:13 -0500 | [diff] [blame] | 589 | if (!timeout) { |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 590 | dev_err(dev->device, "AES timeout\n"); |
| 591 | return -ETIMEDOUT; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 592 | } |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 593 | |
| 594 | dma_unmap_sg(dev->device, dev->out_sg, dev->nb_out_sg, |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 595 | DMA_FROM_DEVICE); |
Mogens Lauridsen | 1e32041 | 2017-08-03 15:34:12 +0200 | [diff] [blame] | 596 | dma_unmap_sg(dev->device, dev->in_sg, dev->nb_in_sg, |
| 597 | DMA_TO_DEVICE); |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 598 | |
| 599 | return 0; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 602 | static int sahara_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 603 | unsigned int keylen) |
| 604 | { |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 605 | struct sahara_ctx *ctx = crypto_skcipher_ctx(tfm); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 606 | |
| 607 | ctx->keylen = keylen; |
| 608 | |
| 609 | /* SAHARA only supports 128bit keys */ |
| 610 | if (keylen == AES_KEYSIZE_128) { |
| 611 | memcpy(ctx->key, key, keylen); |
| 612 | ctx->flags |= FLAGS_NEW_KEY; |
| 613 | return 0; |
| 614 | } |
| 615 | |
Herbert Xu | 678adec | 2016-06-29 18:04:05 +0800 | [diff] [blame] | 616 | if (keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 617 | return -EINVAL; |
| 618 | |
| 619 | /* |
| 620 | * The requested key size is not supported by HW, do a fallback. |
| 621 | */ |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 622 | crypto_skcipher_clear_flags(ctx->fallback, CRYPTO_TFM_REQ_MASK); |
| 623 | crypto_skcipher_set_flags(ctx->fallback, tfm->base.crt_flags & |
Herbert Xu | 678adec | 2016-06-29 18:04:05 +0800 | [diff] [blame] | 624 | CRYPTO_TFM_REQ_MASK); |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 625 | return crypto_skcipher_setkey(ctx->fallback, key, keylen); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 626 | } |
| 627 | |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 628 | static int sahara_aes_crypt(struct skcipher_request *req, unsigned long mode) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 629 | { |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 630 | struct sahara_aes_reqctx *rctx = skcipher_request_ctx(req); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 631 | struct sahara_dev *dev = dev_ptr; |
| 632 | int err = 0; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 633 | |
| 634 | dev_dbg(dev->device, "nbytes: %d, enc: %d, cbc: %d\n", |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 635 | req->cryptlen, !!(mode & FLAGS_ENCRYPT), !!(mode & FLAGS_CBC)); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 636 | |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 637 | if (!IS_ALIGNED(req->cryptlen, AES_BLOCK_SIZE)) { |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 638 | dev_err(dev->device, |
| 639 | "request size is not exact amount of AES blocks\n"); |
| 640 | return -EINVAL; |
| 641 | } |
| 642 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 643 | rctx->mode = mode; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 644 | |
Zhengchao Shao | 108586e | 2022-07-25 12:09:28 +0800 | [diff] [blame] | 645 | spin_lock_bh(&dev->queue_spinlock); |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 646 | err = crypto_enqueue_request(&dev->queue, &req->base); |
Zhengchao Shao | 108586e | 2022-07-25 12:09:28 +0800 | [diff] [blame] | 647 | spin_unlock_bh(&dev->queue_spinlock); |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 648 | |
| 649 | wake_up_process(dev->kthread); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 650 | |
| 651 | return err; |
| 652 | } |
| 653 | |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 654 | static int sahara_aes_ecb_encrypt(struct skcipher_request *req) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 655 | { |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 656 | struct sahara_aes_reqctx *rctx = skcipher_request_ctx(req); |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 657 | struct sahara_ctx *ctx = crypto_skcipher_ctx( |
| 658 | crypto_skcipher_reqtfm(req)); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 659 | |
| 660 | if (unlikely(ctx->keylen != AES_KEYSIZE_128)) { |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 661 | skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback); |
| 662 | skcipher_request_set_callback(&rctx->fallback_req, |
| 663 | req->base.flags, |
| 664 | req->base.complete, |
| 665 | req->base.data); |
| 666 | skcipher_request_set_crypt(&rctx->fallback_req, req->src, |
| 667 | req->dst, req->cryptlen, req->iv); |
| 668 | return crypto_skcipher_encrypt(&rctx->fallback_req); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | return sahara_aes_crypt(req, FLAGS_ENCRYPT); |
| 672 | } |
| 673 | |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 674 | static int sahara_aes_ecb_decrypt(struct skcipher_request *req) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 675 | { |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 676 | struct sahara_aes_reqctx *rctx = skcipher_request_ctx(req); |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 677 | struct sahara_ctx *ctx = crypto_skcipher_ctx( |
| 678 | crypto_skcipher_reqtfm(req)); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 679 | |
| 680 | if (unlikely(ctx->keylen != AES_KEYSIZE_128)) { |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 681 | skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback); |
| 682 | skcipher_request_set_callback(&rctx->fallback_req, |
| 683 | req->base.flags, |
| 684 | req->base.complete, |
| 685 | req->base.data); |
| 686 | skcipher_request_set_crypt(&rctx->fallback_req, req->src, |
| 687 | req->dst, req->cryptlen, req->iv); |
| 688 | return crypto_skcipher_decrypt(&rctx->fallback_req); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | return sahara_aes_crypt(req, 0); |
| 692 | } |
| 693 | |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 694 | static int sahara_aes_cbc_encrypt(struct skcipher_request *req) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 695 | { |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 696 | struct sahara_aes_reqctx *rctx = skcipher_request_ctx(req); |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 697 | struct sahara_ctx *ctx = crypto_skcipher_ctx( |
| 698 | crypto_skcipher_reqtfm(req)); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 699 | |
| 700 | if (unlikely(ctx->keylen != AES_KEYSIZE_128)) { |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 701 | skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback); |
| 702 | skcipher_request_set_callback(&rctx->fallback_req, |
| 703 | req->base.flags, |
| 704 | req->base.complete, |
| 705 | req->base.data); |
| 706 | skcipher_request_set_crypt(&rctx->fallback_req, req->src, |
| 707 | req->dst, req->cryptlen, req->iv); |
| 708 | return crypto_skcipher_encrypt(&rctx->fallback_req); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | return sahara_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC); |
| 712 | } |
| 713 | |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 714 | static int sahara_aes_cbc_decrypt(struct skcipher_request *req) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 715 | { |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 716 | struct sahara_aes_reqctx *rctx = skcipher_request_ctx(req); |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 717 | struct sahara_ctx *ctx = crypto_skcipher_ctx( |
| 718 | crypto_skcipher_reqtfm(req)); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 719 | |
| 720 | if (unlikely(ctx->keylen != AES_KEYSIZE_128)) { |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 721 | skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback); |
| 722 | skcipher_request_set_callback(&rctx->fallback_req, |
| 723 | req->base.flags, |
| 724 | req->base.complete, |
| 725 | req->base.data); |
| 726 | skcipher_request_set_crypt(&rctx->fallback_req, req->src, |
| 727 | req->dst, req->cryptlen, req->iv); |
| 728 | return crypto_skcipher_decrypt(&rctx->fallback_req); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | return sahara_aes_crypt(req, FLAGS_CBC); |
| 732 | } |
| 733 | |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 734 | static int sahara_aes_init_tfm(struct crypto_skcipher *tfm) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 735 | { |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 736 | const char *name = crypto_tfm_alg_name(&tfm->base); |
| 737 | struct sahara_ctx *ctx = crypto_skcipher_ctx(tfm); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 738 | |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 739 | ctx->fallback = crypto_alloc_skcipher(name, 0, |
Herbert Xu | 678adec | 2016-06-29 18:04:05 +0800 | [diff] [blame] | 740 | CRYPTO_ALG_NEED_FALLBACK); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 741 | if (IS_ERR(ctx->fallback)) { |
| 742 | pr_err("Error allocating fallback algo %s\n", name); |
| 743 | return PTR_ERR(ctx->fallback); |
| 744 | } |
| 745 | |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 746 | crypto_skcipher_set_reqsize(tfm, sizeof(struct sahara_aes_reqctx) + |
| 747 | crypto_skcipher_reqsize(ctx->fallback)); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 752 | static void sahara_aes_exit_tfm(struct crypto_skcipher *tfm) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 753 | { |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 754 | struct sahara_ctx *ctx = crypto_skcipher_ctx(tfm); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 755 | |
Ard Biesheuvel | 56ca499 | 2020-07-07 09:32:02 +0300 | [diff] [blame] | 756 | crypto_free_skcipher(ctx->fallback); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 757 | } |
| 758 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 759 | static u32 sahara_sha_init_hdr(struct sahara_dev *dev, |
| 760 | struct sahara_sha_reqctx *rctx) |
| 761 | { |
| 762 | u32 hdr = 0; |
| 763 | |
| 764 | hdr = rctx->mode; |
| 765 | |
| 766 | if (rctx->first) { |
| 767 | hdr |= SAHARA_HDR_MDHA_SET_MODE_HASH; |
| 768 | hdr |= SAHARA_HDR_MDHA_INIT; |
| 769 | } else { |
| 770 | hdr |= SAHARA_HDR_MDHA_SET_MODE_MD_KEY; |
| 771 | } |
| 772 | |
| 773 | if (rctx->last) |
| 774 | hdr |= SAHARA_HDR_MDHA_PDATA; |
| 775 | |
| 776 | if (hweight_long(hdr) % 2 == 0) |
| 777 | hdr |= SAHARA_HDR_PARITY_BIT; |
| 778 | |
| 779 | return hdr; |
| 780 | } |
| 781 | |
| 782 | static int sahara_sha_hw_links_create(struct sahara_dev *dev, |
| 783 | struct sahara_sha_reqctx *rctx, |
| 784 | int start) |
| 785 | { |
| 786 | struct scatterlist *sg; |
| 787 | unsigned int i; |
| 788 | int ret; |
| 789 | |
| 790 | dev->in_sg = rctx->in_sg; |
| 791 | |
LABBE Corentin | d23afa1 | 2015-09-18 14:57:11 +0200 | [diff] [blame] | 792 | dev->nb_in_sg = sg_nents_for_len(dev->in_sg, rctx->total); |
LABBE Corentin | 6c2b74d | 2015-11-04 21:13:35 +0100 | [diff] [blame] | 793 | if (dev->nb_in_sg < 0) { |
| 794 | dev_err(dev->device, "Invalid numbers of src SG.\n"); |
| 795 | return dev->nb_in_sg; |
| 796 | } |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 797 | if ((dev->nb_in_sg) > SAHARA_MAX_HW_LINK) { |
| 798 | dev_err(dev->device, "not enough hw links (%d)\n", |
| 799 | dev->nb_in_sg + dev->nb_out_sg); |
| 800 | return -EINVAL; |
| 801 | } |
| 802 | |
LABBE Corentin | 640eec5 | 2015-09-23 13:55:28 +0200 | [diff] [blame] | 803 | sg = dev->in_sg; |
| 804 | ret = dma_map_sg(dev->device, dev->in_sg, dev->nb_in_sg, DMA_TO_DEVICE); |
| 805 | if (!ret) |
| 806 | return -EFAULT; |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 807 | |
LABBE Corentin | 640eec5 | 2015-09-23 13:55:28 +0200 | [diff] [blame] | 808 | for (i = start; i < dev->nb_in_sg + start; i++) { |
| 809 | dev->hw_link[i]->len = sg->length; |
| 810 | dev->hw_link[i]->p = sg->dma_address; |
| 811 | if (i == (dev->nb_in_sg + start - 1)) { |
| 812 | dev->hw_link[i]->next = 0; |
| 813 | } else { |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 814 | dev->hw_link[i]->next = dev->hw_phys_link[i + 1]; |
| 815 | sg = sg_next(sg); |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
| 819 | return i; |
| 820 | } |
| 821 | |
| 822 | static int sahara_sha_hw_data_descriptor_create(struct sahara_dev *dev, |
| 823 | struct sahara_sha_reqctx *rctx, |
| 824 | struct ahash_request *req, |
| 825 | int index) |
| 826 | { |
| 827 | unsigned result_len; |
| 828 | int i = index; |
| 829 | |
| 830 | if (rctx->first) |
| 831 | /* Create initial descriptor: #8*/ |
| 832 | dev->hw_desc[index]->hdr = sahara_sha_init_hdr(dev, rctx); |
| 833 | else |
| 834 | /* Create hash descriptor: #10. Must follow #6. */ |
| 835 | dev->hw_desc[index]->hdr = SAHARA_HDR_MDHA_HASH; |
| 836 | |
| 837 | dev->hw_desc[index]->len1 = rctx->total; |
| 838 | if (dev->hw_desc[index]->len1 == 0) { |
| 839 | /* if len1 is 0, p1 must be 0, too */ |
| 840 | dev->hw_desc[index]->p1 = 0; |
| 841 | rctx->sg_in_idx = 0; |
| 842 | } else { |
| 843 | /* Create input links */ |
| 844 | dev->hw_desc[index]->p1 = dev->hw_phys_link[index]; |
| 845 | i = sahara_sha_hw_links_create(dev, rctx, index); |
| 846 | |
| 847 | rctx->sg_in_idx = index; |
| 848 | if (i < 0) |
| 849 | return i; |
| 850 | } |
| 851 | |
| 852 | dev->hw_desc[index]->p2 = dev->hw_phys_link[i]; |
| 853 | |
| 854 | /* Save the context for the next operation */ |
| 855 | result_len = rctx->context_size; |
| 856 | dev->hw_link[i]->p = dev->context_phys_base; |
| 857 | |
| 858 | dev->hw_link[i]->len = result_len; |
| 859 | dev->hw_desc[index]->len2 = result_len; |
| 860 | |
| 861 | dev->hw_link[i]->next = 0; |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * Load descriptor aka #6 |
| 868 | * |
| 869 | * To load a previously saved context back to the MDHA unit |
| 870 | * |
| 871 | * p1: Saved Context |
| 872 | * p2: NULL |
| 873 | * |
| 874 | */ |
| 875 | static int sahara_sha_hw_context_descriptor_create(struct sahara_dev *dev, |
| 876 | struct sahara_sha_reqctx *rctx, |
| 877 | struct ahash_request *req, |
| 878 | int index) |
| 879 | { |
| 880 | dev->hw_desc[index]->hdr = sahara_sha_init_hdr(dev, rctx); |
| 881 | |
| 882 | dev->hw_desc[index]->len1 = rctx->context_size; |
| 883 | dev->hw_desc[index]->p1 = dev->hw_phys_link[index]; |
| 884 | dev->hw_desc[index]->len2 = 0; |
| 885 | dev->hw_desc[index]->p2 = 0; |
| 886 | |
| 887 | dev->hw_link[index]->len = rctx->context_size; |
| 888 | dev->hw_link[index]->p = dev->context_phys_base; |
| 889 | dev->hw_link[index]->next = 0; |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | static int sahara_walk_and_recalc(struct scatterlist *sg, unsigned int nbytes) |
| 895 | { |
| 896 | if (!sg || !sg->length) |
| 897 | return nbytes; |
| 898 | |
| 899 | while (nbytes && sg) { |
| 900 | if (nbytes <= sg->length) { |
| 901 | sg->length = nbytes; |
| 902 | sg_mark_end(sg); |
| 903 | break; |
| 904 | } |
| 905 | nbytes -= sg->length; |
Cristian Stoica | 5be4d4c | 2015-01-20 10:06:16 +0200 | [diff] [blame] | 906 | sg = sg_next(sg); |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | return nbytes; |
| 910 | } |
| 911 | |
| 912 | static int sahara_sha_prepare_request(struct ahash_request *req) |
| 913 | { |
| 914 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 915 | struct sahara_sha_reqctx *rctx = ahash_request_ctx(req); |
| 916 | unsigned int hash_later; |
| 917 | unsigned int block_size; |
| 918 | unsigned int len; |
| 919 | |
| 920 | block_size = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm)); |
| 921 | |
| 922 | /* append bytes from previous operation */ |
| 923 | len = rctx->buf_cnt + req->nbytes; |
| 924 | |
| 925 | /* only the last transfer can be padded in hardware */ |
| 926 | if (!rctx->last && (len < block_size)) { |
| 927 | /* to few data, save for next operation */ |
| 928 | scatterwalk_map_and_copy(rctx->buf + rctx->buf_cnt, req->src, |
| 929 | 0, req->nbytes, 0); |
| 930 | rctx->buf_cnt += req->nbytes; |
| 931 | |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | /* add data from previous operation first */ |
| 936 | if (rctx->buf_cnt) |
| 937 | memcpy(rctx->rembuf, rctx->buf, rctx->buf_cnt); |
| 938 | |
| 939 | /* data must always be a multiple of block_size */ |
| 940 | hash_later = rctx->last ? 0 : len & (block_size - 1); |
| 941 | if (hash_later) { |
| 942 | unsigned int offset = req->nbytes - hash_later; |
| 943 | /* Save remaining bytes for later use */ |
| 944 | scatterwalk_map_and_copy(rctx->buf, req->src, offset, |
| 945 | hash_later, 0); |
| 946 | } |
| 947 | |
| 948 | /* nbytes should now be multiple of blocksize */ |
| 949 | req->nbytes = req->nbytes - hash_later; |
| 950 | |
| 951 | sahara_walk_and_recalc(req->src, req->nbytes); |
| 952 | |
| 953 | /* have data from previous operation and current */ |
| 954 | if (rctx->buf_cnt && req->nbytes) { |
| 955 | sg_init_table(rctx->in_sg_chain, 2); |
| 956 | sg_set_buf(rctx->in_sg_chain, rctx->rembuf, rctx->buf_cnt); |
| 957 | |
Dan Williams | c56f6d1 | 2015-08-07 18:15:13 +0200 | [diff] [blame] | 958 | sg_chain(rctx->in_sg_chain, 2, req->src); |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 959 | |
| 960 | rctx->total = req->nbytes + rctx->buf_cnt; |
| 961 | rctx->in_sg = rctx->in_sg_chain; |
| 962 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 963 | req->src = rctx->in_sg_chain; |
| 964 | /* only data from previous operation */ |
| 965 | } else if (rctx->buf_cnt) { |
| 966 | if (req->src) |
| 967 | rctx->in_sg = req->src; |
| 968 | else |
| 969 | rctx->in_sg = rctx->in_sg_chain; |
| 970 | /* buf was copied into rembuf above */ |
| 971 | sg_init_one(rctx->in_sg, rctx->rembuf, rctx->buf_cnt); |
| 972 | rctx->total = rctx->buf_cnt; |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 973 | /* no data from previous operation */ |
| 974 | } else { |
| 975 | rctx->in_sg = req->src; |
| 976 | rctx->total = req->nbytes; |
| 977 | req->src = rctx->in_sg; |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* on next call, we only have the remaining data in the buffer */ |
| 981 | rctx->buf_cnt = hash_later; |
| 982 | |
| 983 | return -EINPROGRESS; |
| 984 | } |
| 985 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 986 | static int sahara_sha_process(struct ahash_request *req) |
| 987 | { |
| 988 | struct sahara_dev *dev = dev_ptr; |
| 989 | struct sahara_sha_reqctx *rctx = ahash_request_ctx(req); |
Nicholas Mc Guire | df586cb | 2015-02-07 06:16:46 -0500 | [diff] [blame] | 990 | int ret; |
Nicholas Mc Guire | 58ed798 | 2015-02-07 06:17:13 -0500 | [diff] [blame] | 991 | unsigned long timeout; |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 992 | |
| 993 | ret = sahara_sha_prepare_request(req); |
| 994 | if (!ret) |
| 995 | return ret; |
| 996 | |
| 997 | if (rctx->first) { |
| 998 | sahara_sha_hw_data_descriptor_create(dev, rctx, req, 0); |
| 999 | dev->hw_desc[0]->next = 0; |
| 1000 | rctx->first = 0; |
| 1001 | } else { |
| 1002 | memcpy(dev->context_base, rctx->context, rctx->context_size); |
| 1003 | |
| 1004 | sahara_sha_hw_context_descriptor_create(dev, rctx, req, 0); |
| 1005 | dev->hw_desc[0]->next = dev->hw_phys_desc[1]; |
| 1006 | sahara_sha_hw_data_descriptor_create(dev, rctx, req, 1); |
| 1007 | dev->hw_desc[1]->next = 0; |
| 1008 | } |
| 1009 | |
| 1010 | sahara_dump_descriptors(dev); |
| 1011 | sahara_dump_links(dev); |
| 1012 | |
| 1013 | reinit_completion(&dev->dma_completion); |
| 1014 | |
| 1015 | sahara_write(dev, dev->hw_phys_desc[0], SAHARA_REG_DAR); |
| 1016 | |
Nicholas Mc Guire | 58ed798 | 2015-02-07 06:17:13 -0500 | [diff] [blame] | 1017 | timeout = wait_for_completion_timeout(&dev->dma_completion, |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1018 | msecs_to_jiffies(SAHARA_TIMEOUT_MS)); |
Nicholas Mc Guire | 58ed798 | 2015-02-07 06:17:13 -0500 | [diff] [blame] | 1019 | if (!timeout) { |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1020 | dev_err(dev->device, "SHA timeout\n"); |
| 1021 | return -ETIMEDOUT; |
| 1022 | } |
| 1023 | |
| 1024 | if (rctx->sg_in_idx) |
LABBE Corentin | 640eec5 | 2015-09-23 13:55:28 +0200 | [diff] [blame] | 1025 | dma_unmap_sg(dev->device, dev->in_sg, dev->nb_in_sg, |
| 1026 | DMA_TO_DEVICE); |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1027 | |
| 1028 | memcpy(rctx->context, dev->context_base, rctx->context_size); |
| 1029 | |
| 1030 | if (req->result) |
| 1031 | memcpy(req->result, rctx->context, rctx->digest_size); |
| 1032 | |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1036 | static int sahara_queue_manage(void *data) |
| 1037 | { |
Yu Zhe | aedf818 | 2023-03-17 14:26:03 +0800 | [diff] [blame] | 1038 | struct sahara_dev *dev = data; |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1039 | struct crypto_async_request *async_req; |
Steffen Trumtrar | ddacc62 | 2015-04-07 17:13:41 +0200 | [diff] [blame] | 1040 | struct crypto_async_request *backlog; |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1041 | int ret = 0; |
| 1042 | |
| 1043 | do { |
| 1044 | __set_current_state(TASK_INTERRUPTIBLE); |
| 1045 | |
Zhengchao Shao | 108586e | 2022-07-25 12:09:28 +0800 | [diff] [blame] | 1046 | spin_lock_bh(&dev->queue_spinlock); |
Steffen Trumtrar | ddacc62 | 2015-04-07 17:13:41 +0200 | [diff] [blame] | 1047 | backlog = crypto_get_backlog(&dev->queue); |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1048 | async_req = crypto_dequeue_request(&dev->queue); |
Zhengchao Shao | 108586e | 2022-07-25 12:09:28 +0800 | [diff] [blame] | 1049 | spin_unlock_bh(&dev->queue_spinlock); |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1050 | |
Steffen Trumtrar | ddacc62 | 2015-04-07 17:13:41 +0200 | [diff] [blame] | 1051 | if (backlog) |
Herbert Xu | 555c566 | 2023-01-31 16:02:48 +0800 | [diff] [blame] | 1052 | crypto_request_complete(backlog, -EINPROGRESS); |
Steffen Trumtrar | ddacc62 | 2015-04-07 17:13:41 +0200 | [diff] [blame] | 1053 | |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1054 | if (async_req) { |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1055 | if (crypto_tfm_alg_type(async_req->tfm) == |
| 1056 | CRYPTO_ALG_TYPE_AHASH) { |
| 1057 | struct ahash_request *req = |
| 1058 | ahash_request_cast(async_req); |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1059 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1060 | ret = sahara_sha_process(req); |
| 1061 | } else { |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 1062 | struct skcipher_request *req = |
| 1063 | skcipher_request_cast(async_req); |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1064 | |
| 1065 | ret = sahara_aes_process(req); |
| 1066 | } |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1067 | |
Herbert Xu | 555c566 | 2023-01-31 16:02:48 +0800 | [diff] [blame] | 1068 | crypto_request_complete(async_req, ret); |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1069 | |
| 1070 | continue; |
| 1071 | } |
| 1072 | |
| 1073 | schedule(); |
| 1074 | } while (!kthread_should_stop()); |
| 1075 | |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1079 | static int sahara_sha_enqueue(struct ahash_request *req, int last) |
| 1080 | { |
| 1081 | struct sahara_sha_reqctx *rctx = ahash_request_ctx(req); |
| 1082 | struct sahara_dev *dev = dev_ptr; |
| 1083 | int ret; |
| 1084 | |
| 1085 | if (!req->nbytes && !last) |
| 1086 | return 0; |
| 1087 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1088 | rctx->last = last; |
| 1089 | |
| 1090 | if (!rctx->active) { |
| 1091 | rctx->active = 1; |
| 1092 | rctx->first = 1; |
| 1093 | } |
| 1094 | |
Zhengchao Shao | 108586e | 2022-07-25 12:09:28 +0800 | [diff] [blame] | 1095 | spin_lock_bh(&dev->queue_spinlock); |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1096 | ret = crypto_enqueue_request(&dev->queue, &req->base); |
Zhengchao Shao | 108586e | 2022-07-25 12:09:28 +0800 | [diff] [blame] | 1097 | spin_unlock_bh(&dev->queue_spinlock); |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1098 | |
| 1099 | wake_up_process(dev->kthread); |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1100 | |
| 1101 | return ret; |
| 1102 | } |
| 1103 | |
| 1104 | static int sahara_sha_init(struct ahash_request *req) |
| 1105 | { |
| 1106 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 1107 | struct sahara_sha_reqctx *rctx = ahash_request_ctx(req); |
| 1108 | |
| 1109 | memset(rctx, 0, sizeof(*rctx)); |
| 1110 | |
| 1111 | switch (crypto_ahash_digestsize(tfm)) { |
| 1112 | case SHA1_DIGEST_SIZE: |
| 1113 | rctx->mode |= SAHARA_HDR_MDHA_ALG_SHA1; |
| 1114 | rctx->digest_size = SHA1_DIGEST_SIZE; |
| 1115 | break; |
| 1116 | case SHA256_DIGEST_SIZE: |
| 1117 | rctx->mode |= SAHARA_HDR_MDHA_ALG_SHA256; |
| 1118 | rctx->digest_size = SHA256_DIGEST_SIZE; |
| 1119 | break; |
| 1120 | default: |
| 1121 | return -EINVAL; |
| 1122 | } |
| 1123 | |
| 1124 | rctx->context_size = rctx->digest_size + 4; |
| 1125 | rctx->active = 0; |
| 1126 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1127 | return 0; |
| 1128 | } |
| 1129 | |
| 1130 | static int sahara_sha_update(struct ahash_request *req) |
| 1131 | { |
| 1132 | return sahara_sha_enqueue(req, 0); |
| 1133 | } |
| 1134 | |
| 1135 | static int sahara_sha_final(struct ahash_request *req) |
| 1136 | { |
| 1137 | req->nbytes = 0; |
| 1138 | return sahara_sha_enqueue(req, 1); |
| 1139 | } |
| 1140 | |
| 1141 | static int sahara_sha_finup(struct ahash_request *req) |
| 1142 | { |
| 1143 | return sahara_sha_enqueue(req, 1); |
| 1144 | } |
| 1145 | |
| 1146 | static int sahara_sha_digest(struct ahash_request *req) |
| 1147 | { |
| 1148 | sahara_sha_init(req); |
| 1149 | |
| 1150 | return sahara_sha_finup(req); |
| 1151 | } |
| 1152 | |
| 1153 | static int sahara_sha_export(struct ahash_request *req, void *out) |
| 1154 | { |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1155 | struct sahara_sha_reqctx *rctx = ahash_request_ctx(req); |
| 1156 | |
Fabio Estevam | bceab44 | 2016-02-03 10:46:51 -0200 | [diff] [blame] | 1157 | memcpy(out, rctx, sizeof(struct sahara_sha_reqctx)); |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1158 | |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | static int sahara_sha_import(struct ahash_request *req, const void *in) |
| 1163 | { |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1164 | struct sahara_sha_reqctx *rctx = ahash_request_ctx(req); |
| 1165 | |
Fabio Estevam | bceab44 | 2016-02-03 10:46:51 -0200 | [diff] [blame] | 1166 | memcpy(rctx, in, sizeof(struct sahara_sha_reqctx)); |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1167 | |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
| 1171 | static int sahara_sha_cra_init(struct crypto_tfm *tfm) |
| 1172 | { |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1173 | crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), |
| 1174 | sizeof(struct sahara_sha_reqctx) + |
| 1175 | SHA_BUFFER_LEN + SHA256_BLOCK_SIZE); |
| 1176 | |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 1180 | static struct skcipher_alg aes_algs[] = { |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1181 | { |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 1182 | .base.cra_name = "ecb(aes)", |
| 1183 | .base.cra_driver_name = "sahara-ecb-aes", |
| 1184 | .base.cra_priority = 300, |
| 1185 | .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK, |
| 1186 | .base.cra_blocksize = AES_BLOCK_SIZE, |
| 1187 | .base.cra_ctxsize = sizeof(struct sahara_ctx), |
| 1188 | .base.cra_alignmask = 0x0, |
| 1189 | .base.cra_module = THIS_MODULE, |
| 1190 | |
| 1191 | .init = sahara_aes_init_tfm, |
| 1192 | .exit = sahara_aes_exit_tfm, |
| 1193 | .min_keysize = AES_MIN_KEY_SIZE , |
| 1194 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1195 | .setkey = sahara_aes_setkey, |
| 1196 | .encrypt = sahara_aes_ecb_encrypt, |
| 1197 | .decrypt = sahara_aes_ecb_decrypt, |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1198 | }, { |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 1199 | .base.cra_name = "cbc(aes)", |
| 1200 | .base.cra_driver_name = "sahara-cbc-aes", |
| 1201 | .base.cra_priority = 300, |
| 1202 | .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK, |
| 1203 | .base.cra_blocksize = AES_BLOCK_SIZE, |
| 1204 | .base.cra_ctxsize = sizeof(struct sahara_ctx), |
| 1205 | .base.cra_alignmask = 0x0, |
| 1206 | .base.cra_module = THIS_MODULE, |
| 1207 | |
| 1208 | .init = sahara_aes_init_tfm, |
| 1209 | .exit = sahara_aes_exit_tfm, |
| 1210 | .min_keysize = AES_MIN_KEY_SIZE , |
| 1211 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1212 | .ivsize = AES_BLOCK_SIZE, |
| 1213 | .setkey = sahara_aes_setkey, |
| 1214 | .encrypt = sahara_aes_cbc_encrypt, |
| 1215 | .decrypt = sahara_aes_cbc_decrypt, |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1216 | } |
| 1217 | }; |
| 1218 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1219 | static struct ahash_alg sha_v3_algs[] = { |
| 1220 | { |
| 1221 | .init = sahara_sha_init, |
| 1222 | .update = sahara_sha_update, |
| 1223 | .final = sahara_sha_final, |
| 1224 | .finup = sahara_sha_finup, |
| 1225 | .digest = sahara_sha_digest, |
| 1226 | .export = sahara_sha_export, |
| 1227 | .import = sahara_sha_import, |
| 1228 | .halg.digestsize = SHA1_DIGEST_SIZE, |
Fabio Estevam | d42cf2f1 | 2016-02-03 10:46:52 -0200 | [diff] [blame] | 1229 | .halg.statesize = sizeof(struct sahara_sha_reqctx), |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1230 | .halg.base = { |
| 1231 | .cra_name = "sha1", |
| 1232 | .cra_driver_name = "sahara-sha1", |
| 1233 | .cra_priority = 300, |
Eric Biggers | 6a38f622 | 2018-06-30 15:16:12 -0700 | [diff] [blame] | 1234 | .cra_flags = CRYPTO_ALG_ASYNC | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1235 | CRYPTO_ALG_NEED_FALLBACK, |
| 1236 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 1237 | .cra_ctxsize = sizeof(struct sahara_ctx), |
| 1238 | .cra_alignmask = 0, |
| 1239 | .cra_module = THIS_MODULE, |
| 1240 | .cra_init = sahara_sha_cra_init, |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1241 | } |
| 1242 | }, |
| 1243 | }; |
| 1244 | |
| 1245 | static struct ahash_alg sha_v4_algs[] = { |
| 1246 | { |
| 1247 | .init = sahara_sha_init, |
| 1248 | .update = sahara_sha_update, |
| 1249 | .final = sahara_sha_final, |
| 1250 | .finup = sahara_sha_finup, |
| 1251 | .digest = sahara_sha_digest, |
| 1252 | .export = sahara_sha_export, |
| 1253 | .import = sahara_sha_import, |
| 1254 | .halg.digestsize = SHA256_DIGEST_SIZE, |
Fabio Estevam | d42cf2f1 | 2016-02-03 10:46:52 -0200 | [diff] [blame] | 1255 | .halg.statesize = sizeof(struct sahara_sha_reqctx), |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1256 | .halg.base = { |
| 1257 | .cra_name = "sha256", |
| 1258 | .cra_driver_name = "sahara-sha256", |
| 1259 | .cra_priority = 300, |
Eric Biggers | 6a38f622 | 2018-06-30 15:16:12 -0700 | [diff] [blame] | 1260 | .cra_flags = CRYPTO_ALG_ASYNC | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1261 | CRYPTO_ALG_NEED_FALLBACK, |
| 1262 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 1263 | .cra_ctxsize = sizeof(struct sahara_ctx), |
| 1264 | .cra_alignmask = 0, |
| 1265 | .cra_module = THIS_MODULE, |
| 1266 | .cra_init = sahara_sha_cra_init, |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1267 | } |
| 1268 | }, |
| 1269 | }; |
| 1270 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1271 | static irqreturn_t sahara_irq_handler(int irq, void *data) |
| 1272 | { |
Yu Zhe | aedf818 | 2023-03-17 14:26:03 +0800 | [diff] [blame] | 1273 | struct sahara_dev *dev = data; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1274 | unsigned int stat = sahara_read(dev, SAHARA_REG_STATUS); |
| 1275 | unsigned int err = sahara_read(dev, SAHARA_REG_ERRSTATUS); |
| 1276 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1277 | sahara_write(dev, SAHARA_CMD_CLEAR_INT | SAHARA_CMD_CLEAR_ERR, |
| 1278 | SAHARA_REG_CMD); |
| 1279 | |
| 1280 | sahara_decode_status(dev, stat); |
| 1281 | |
| 1282 | if (SAHARA_STATUS_GET_STATE(stat) == SAHARA_STATE_BUSY) { |
| 1283 | return IRQ_NONE; |
| 1284 | } else if (SAHARA_STATUS_GET_STATE(stat) == SAHARA_STATE_COMPLETE) { |
| 1285 | dev->error = 0; |
| 1286 | } else { |
| 1287 | sahara_decode_error(dev, err); |
| 1288 | dev->error = -EINVAL; |
| 1289 | } |
| 1290 | |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1291 | complete(&dev->dma_completion); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1292 | |
| 1293 | return IRQ_HANDLED; |
| 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | static int sahara_register_algs(struct sahara_dev *dev) |
| 1298 | { |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1299 | int err; |
| 1300 | unsigned int i, j, k, l; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1301 | |
| 1302 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) { |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 1303 | err = crypto_register_skcipher(&aes_algs[i]); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1304 | if (err) |
| 1305 | goto err_aes_algs; |
| 1306 | } |
| 1307 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1308 | for (k = 0; k < ARRAY_SIZE(sha_v3_algs); k++) { |
| 1309 | err = crypto_register_ahash(&sha_v3_algs[k]); |
| 1310 | if (err) |
| 1311 | goto err_sha_v3_algs; |
| 1312 | } |
| 1313 | |
| 1314 | if (dev->version > SAHARA_VERSION_3) |
| 1315 | for (l = 0; l < ARRAY_SIZE(sha_v4_algs); l++) { |
| 1316 | err = crypto_register_ahash(&sha_v4_algs[l]); |
| 1317 | if (err) |
| 1318 | goto err_sha_v4_algs; |
| 1319 | } |
| 1320 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1321 | return 0; |
| 1322 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1323 | err_sha_v4_algs: |
| 1324 | for (j = 0; j < l; j++) |
| 1325 | crypto_unregister_ahash(&sha_v4_algs[j]); |
| 1326 | |
| 1327 | err_sha_v3_algs: |
| 1328 | for (j = 0; j < k; j++) |
Michael Müller | 0e7d4d9 | 2018-07-15 00:27:06 +0200 | [diff] [blame] | 1329 | crypto_unregister_ahash(&sha_v3_algs[j]); |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1330 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1331 | err_aes_algs: |
| 1332 | for (j = 0; j < i; j++) |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 1333 | crypto_unregister_skcipher(&aes_algs[j]); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1334 | |
| 1335 | return err; |
| 1336 | } |
| 1337 | |
| 1338 | static void sahara_unregister_algs(struct sahara_dev *dev) |
| 1339 | { |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1340 | unsigned int i; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1341 | |
| 1342 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) |
Ard Biesheuvel | 44c10a83 | 2019-11-09 18:09:43 +0100 | [diff] [blame] | 1343 | crypto_unregister_skcipher(&aes_algs[i]); |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1344 | |
Michael Müller | 0e7d4d9 | 2018-07-15 00:27:06 +0200 | [diff] [blame] | 1345 | for (i = 0; i < ARRAY_SIZE(sha_v3_algs); i++) |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1346 | crypto_unregister_ahash(&sha_v3_algs[i]); |
| 1347 | |
| 1348 | if (dev->version > SAHARA_VERSION_3) |
| 1349 | for (i = 0; i < ARRAY_SIZE(sha_v4_algs); i++) |
| 1350 | crypto_unregister_ahash(&sha_v4_algs[i]); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1351 | } |
| 1352 | |
Arvind Yadav | 30aabe3 | 2017-06-27 17:11:23 +0530 | [diff] [blame] | 1353 | static const struct of_device_id sahara_dt_ids[] = { |
Steffen Trumtrar | 5ed903b | 2014-12-01 13:26:32 +0100 | [diff] [blame] | 1354 | { .compatible = "fsl,imx53-sahara" }, |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1355 | { .compatible = "fsl,imx27-sahara" }, |
| 1356 | { /* sentinel */ } |
| 1357 | }; |
Arnd Bergmann | 68be0b1a | 2013-06-03 23:57:37 +0200 | [diff] [blame] | 1358 | MODULE_DEVICE_TABLE(of, sahara_dt_ids); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1359 | |
| 1360 | static int sahara_probe(struct platform_device *pdev) |
| 1361 | { |
| 1362 | struct sahara_dev *dev; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1363 | u32 version; |
| 1364 | int irq; |
| 1365 | int err; |
| 1366 | int i; |
| 1367 | |
Markus Elfring | a8bc22f | 2018-02-14 14:14:05 +0100 | [diff] [blame] | 1368 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); |
Markus Elfring | 0d576d9 | 2018-02-14 14:10:03 +0100 | [diff] [blame] | 1369 | if (!dev) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1370 | return -ENOMEM; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1371 | |
| 1372 | dev->device = &pdev->dev; |
| 1373 | platform_set_drvdata(pdev, dev); |
| 1374 | |
| 1375 | /* Get the base address */ |
Fabio Estevam | b0d7652 | 2019-06-06 13:13:49 -0300 | [diff] [blame] | 1376 | dev->regs_base = devm_platform_ioremap_resource(pdev, 0); |
Jingoo Han | 9e95275 | 2014-02-12 13:23:37 +0900 | [diff] [blame] | 1377 | if (IS_ERR(dev->regs_base)) |
| 1378 | return PTR_ERR(dev->regs_base); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1379 | |
| 1380 | /* Get the IRQ */ |
| 1381 | irq = platform_get_irq(pdev, 0); |
Stephen Boyd | 514838e | 2019-07-30 11:15:05 -0700 | [diff] [blame] | 1382 | if (irq < 0) |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1383 | return irq; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1384 | |
Alexander Shiyan | 3d6f1d1 | 2014-03-10 20:13:32 +0800 | [diff] [blame] | 1385 | err = devm_request_irq(&pdev->dev, irq, sahara_irq_handler, |
| 1386 | 0, dev_name(&pdev->dev), dev); |
| 1387 | if (err) { |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1388 | dev_err(&pdev->dev, "failed to request irq\n"); |
Alexander Shiyan | 3d6f1d1 | 2014-03-10 20:13:32 +0800 | [diff] [blame] | 1389 | return err; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | /* clocks */ |
| 1393 | dev->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); |
| 1394 | if (IS_ERR(dev->clk_ipg)) { |
| 1395 | dev_err(&pdev->dev, "Could not get ipg clock\n"); |
| 1396 | return PTR_ERR(dev->clk_ipg); |
| 1397 | } |
| 1398 | |
| 1399 | dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); |
| 1400 | if (IS_ERR(dev->clk_ahb)) { |
| 1401 | dev_err(&pdev->dev, "Could not get ahb clock\n"); |
| 1402 | return PTR_ERR(dev->clk_ahb); |
| 1403 | } |
| 1404 | |
| 1405 | /* Allocate HW descriptors */ |
Vaishali Thakkar | 66c9a04 | 2015-08-18 11:36:05 +0530 | [diff] [blame] | 1406 | dev->hw_desc[0] = dmam_alloc_coherent(&pdev->dev, |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1407 | SAHARA_MAX_HW_DESC * sizeof(struct sahara_hw_desc), |
| 1408 | &dev->hw_phys_desc[0], GFP_KERNEL); |
| 1409 | if (!dev->hw_desc[0]) { |
| 1410 | dev_err(&pdev->dev, "Could not allocate hw descriptors\n"); |
| 1411 | return -ENOMEM; |
| 1412 | } |
| 1413 | dev->hw_desc[1] = dev->hw_desc[0] + 1; |
| 1414 | dev->hw_phys_desc[1] = dev->hw_phys_desc[0] + |
| 1415 | sizeof(struct sahara_hw_desc); |
| 1416 | |
| 1417 | /* Allocate space for iv and key */ |
Vaishali Thakkar | 66c9a04 | 2015-08-18 11:36:05 +0530 | [diff] [blame] | 1418 | dev->key_base = dmam_alloc_coherent(&pdev->dev, 2 * AES_KEYSIZE_128, |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1419 | &dev->key_phys_base, GFP_KERNEL); |
| 1420 | if (!dev->key_base) { |
| 1421 | dev_err(&pdev->dev, "Could not allocate memory for key\n"); |
Vaishali Thakkar | 66c9a04 | 2015-08-18 11:36:05 +0530 | [diff] [blame] | 1422 | return -ENOMEM; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1423 | } |
| 1424 | dev->iv_base = dev->key_base + AES_KEYSIZE_128; |
| 1425 | dev->iv_phys_base = dev->key_phys_base + AES_KEYSIZE_128; |
| 1426 | |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1427 | /* Allocate space for context: largest digest + message length field */ |
Vaishali Thakkar | 66c9a04 | 2015-08-18 11:36:05 +0530 | [diff] [blame] | 1428 | dev->context_base = dmam_alloc_coherent(&pdev->dev, |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1429 | SHA256_DIGEST_SIZE + 4, |
| 1430 | &dev->context_phys_base, GFP_KERNEL); |
| 1431 | if (!dev->context_base) { |
| 1432 | dev_err(&pdev->dev, "Could not allocate memory for MDHA context\n"); |
Vaishali Thakkar | 66c9a04 | 2015-08-18 11:36:05 +0530 | [diff] [blame] | 1433 | return -ENOMEM; |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1434 | } |
| 1435 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1436 | /* Allocate space for HW links */ |
Vaishali Thakkar | 66c9a04 | 2015-08-18 11:36:05 +0530 | [diff] [blame] | 1437 | dev->hw_link[0] = dmam_alloc_coherent(&pdev->dev, |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1438 | SAHARA_MAX_HW_LINK * sizeof(struct sahara_hw_link), |
| 1439 | &dev->hw_phys_link[0], GFP_KERNEL); |
Dan Carpenter | 393e661 | 2013-08-20 11:51:41 +0300 | [diff] [blame] | 1440 | if (!dev->hw_link[0]) { |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1441 | dev_err(&pdev->dev, "Could not allocate hw links\n"); |
Vaishali Thakkar | 66c9a04 | 2015-08-18 11:36:05 +0530 | [diff] [blame] | 1442 | return -ENOMEM; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1443 | } |
| 1444 | for (i = 1; i < SAHARA_MAX_HW_LINK; i++) { |
| 1445 | dev->hw_phys_link[i] = dev->hw_phys_link[i - 1] + |
| 1446 | sizeof(struct sahara_hw_link); |
| 1447 | dev->hw_link[i] = dev->hw_link[i - 1] + 1; |
| 1448 | } |
| 1449 | |
| 1450 | crypto_init_queue(&dev->queue, SAHARA_QUEUE_LENGTH); |
| 1451 | |
Zhengchao Shao | 108586e | 2022-07-25 12:09:28 +0800 | [diff] [blame] | 1452 | spin_lock_init(&dev->queue_spinlock); |
Steffen Trumtrar | 20ec9d8 | 2014-12-01 13:26:31 +0100 | [diff] [blame] | 1453 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1454 | dev_ptr = dev; |
| 1455 | |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1456 | dev->kthread = kthread_run(sahara_queue_manage, dev, "sahara_crypto"); |
| 1457 | if (IS_ERR(dev->kthread)) { |
Vaishali Thakkar | 66c9a04 | 2015-08-18 11:36:05 +0530 | [diff] [blame] | 1458 | return PTR_ERR(dev->kthread); |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1459 | } |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1460 | |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1461 | init_completion(&dev->dma_completion); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1462 | |
Fabio Estevam | 7eac714 | 2015-06-20 15:30:22 -0300 | [diff] [blame] | 1463 | err = clk_prepare_enable(dev->clk_ipg); |
| 1464 | if (err) |
Vaishali Thakkar | 66c9a04 | 2015-08-18 11:36:05 +0530 | [diff] [blame] | 1465 | return err; |
Fabio Estevam | 7eac714 | 2015-06-20 15:30:22 -0300 | [diff] [blame] | 1466 | err = clk_prepare_enable(dev->clk_ahb); |
| 1467 | if (err) |
| 1468 | goto clk_ipg_disable; |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1469 | |
| 1470 | version = sahara_read(dev, SAHARA_REG_VERSION); |
Steffen Trumtrar | 5ed903b | 2014-12-01 13:26:32 +0100 | [diff] [blame] | 1471 | if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx27-sahara")) { |
| 1472 | if (version != SAHARA_VERSION_3) |
| 1473 | err = -ENODEV; |
| 1474 | } else if (of_device_is_compatible(pdev->dev.of_node, |
| 1475 | "fsl,imx53-sahara")) { |
| 1476 | if (((version >> 8) & 0xff) != SAHARA_VERSION_4) |
| 1477 | err = -ENODEV; |
| 1478 | version = (version >> 8) & 0xff; |
| 1479 | } |
| 1480 | if (err == -ENODEV) { |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1481 | dev_err(&pdev->dev, "SAHARA version %d not supported\n", |
Steffen Trumtrar | 5ed903b | 2014-12-01 13:26:32 +0100 | [diff] [blame] | 1482 | version); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1483 | goto err_algs; |
| 1484 | } |
| 1485 | |
Steffen Trumtrar | 5ed903b | 2014-12-01 13:26:32 +0100 | [diff] [blame] | 1486 | dev->version = version; |
| 1487 | |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1488 | sahara_write(dev, SAHARA_CMD_RESET | SAHARA_CMD_MODE_BATCH, |
| 1489 | SAHARA_REG_CMD); |
| 1490 | sahara_write(dev, SAHARA_CONTROL_SET_THROTTLE(0) | |
| 1491 | SAHARA_CONTROL_SET_MAXBURST(8) | |
| 1492 | SAHARA_CONTROL_RNG_AUTORSD | |
| 1493 | SAHARA_CONTROL_ENABLE_INT, |
| 1494 | SAHARA_REG_CONTROL); |
| 1495 | |
| 1496 | err = sahara_register_algs(dev); |
| 1497 | if (err) |
| 1498 | goto err_algs; |
| 1499 | |
| 1500 | dev_info(&pdev->dev, "SAHARA version %d initialized\n", version); |
| 1501 | |
| 1502 | return 0; |
| 1503 | |
| 1504 | err_algs: |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1505 | kthread_stop(dev->kthread); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1506 | dev_ptr = NULL; |
Fabio Estevam | 7eac714 | 2015-06-20 15:30:22 -0300 | [diff] [blame] | 1507 | clk_disable_unprepare(dev->clk_ahb); |
| 1508 | clk_ipg_disable: |
| 1509 | clk_disable_unprepare(dev->clk_ipg); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1510 | |
| 1511 | return err; |
| 1512 | } |
| 1513 | |
| 1514 | static int sahara_remove(struct platform_device *pdev) |
| 1515 | { |
| 1516 | struct sahara_dev *dev = platform_get_drvdata(pdev); |
| 1517 | |
Steffen Trumtrar | c0c3c89 | 2014-12-01 13:26:33 +0100 | [diff] [blame] | 1518 | kthread_stop(dev->kthread); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1519 | |
| 1520 | sahara_unregister_algs(dev); |
| 1521 | |
| 1522 | clk_disable_unprepare(dev->clk_ipg); |
| 1523 | clk_disable_unprepare(dev->clk_ahb); |
| 1524 | |
| 1525 | dev_ptr = NULL; |
| 1526 | |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
| 1530 | static struct platform_driver sahara_driver = { |
| 1531 | .probe = sahara_probe, |
| 1532 | .remove = sahara_remove, |
| 1533 | .driver = { |
| 1534 | .name = SAHARA_NAME, |
Sachin Kamat | 1b0b260 | 2013-09-30 08:49:41 +0530 | [diff] [blame] | 1535 | .of_match_table = sahara_dt_ids, |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1536 | }, |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1537 | }; |
| 1538 | |
| 1539 | module_platform_driver(sahara_driver); |
| 1540 | |
| 1541 | MODULE_LICENSE("GPL"); |
| 1542 | MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>"); |
Steffen Trumtrar | 5a2bb93 | 2014-12-01 13:26:34 +0100 | [diff] [blame] | 1543 | MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>"); |
Javier Martin | 5de8875 | 2013-03-01 12:37:53 +0100 | [diff] [blame] | 1544 | MODULE_DESCRIPTION("SAHARA2 HW crypto accelerator"); |