Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Freescale i.MX23/i.MX28 Data Co-Processor driver |
| 3 | * |
| 4 | * Copyright (C) 2013 Marek Vasut <marex@denx.de> |
| 5 | * |
| 6 | * The code contained herein is licensed under the GNU General Public |
| 7 | * License. You may obtain a copy of the GNU General Public License |
| 8 | * Version 2 or later at the following locations: |
| 9 | * |
| 10 | * http://www.opensource.org/licenses/gpl-license.html |
| 11 | * http://www.gnu.org/copyleft/gpl.html |
| 12 | */ |
| 13 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 14 | #include <linux/dma-mapping.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/kthread.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/stmp_device.h> |
| 23 | |
| 24 | #include <crypto/aes.h> |
| 25 | #include <crypto/sha.h> |
| 26 | #include <crypto/internal/hash.h> |
Herbert Xu | 29406bb | 2016-06-29 18:04:02 +0800 | [diff] [blame] | 27 | #include <crypto/internal/skcipher.h> |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 28 | |
| 29 | #define DCP_MAX_CHANS 4 |
| 30 | #define DCP_BUF_SZ PAGE_SIZE |
| 31 | |
Marek Vasut | 1a7c685 | 2014-03-03 01:23:15 +0100 | [diff] [blame] | 32 | #define DCP_ALIGNMENT 64 |
| 33 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 34 | /* DCP DMA descriptor. */ |
| 35 | struct dcp_dma_desc { |
| 36 | uint32_t next_cmd_addr; |
| 37 | uint32_t control0; |
| 38 | uint32_t control1; |
| 39 | uint32_t source; |
| 40 | uint32_t destination; |
| 41 | uint32_t size; |
| 42 | uint32_t payload; |
| 43 | uint32_t status; |
| 44 | }; |
| 45 | |
| 46 | /* Coherent aligned block for bounce buffering. */ |
| 47 | struct dcp_coherent_block { |
| 48 | uint8_t aes_in_buf[DCP_BUF_SZ]; |
| 49 | uint8_t aes_out_buf[DCP_BUF_SZ]; |
| 50 | uint8_t sha_in_buf[DCP_BUF_SZ]; |
| 51 | |
| 52 | uint8_t aes_key[2 * AES_KEYSIZE_128]; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 53 | |
| 54 | struct dcp_dma_desc desc[DCP_MAX_CHANS]; |
| 55 | }; |
| 56 | |
| 57 | struct dcp { |
| 58 | struct device *dev; |
| 59 | void __iomem *base; |
| 60 | |
| 61 | uint32_t caps; |
| 62 | |
| 63 | struct dcp_coherent_block *coh; |
| 64 | |
| 65 | struct completion completion[DCP_MAX_CHANS]; |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 66 | spinlock_t lock[DCP_MAX_CHANS]; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 67 | struct task_struct *thread[DCP_MAX_CHANS]; |
| 68 | struct crypto_queue queue[DCP_MAX_CHANS]; |
| 69 | }; |
| 70 | |
| 71 | enum dcp_chan { |
| 72 | DCP_CHAN_HASH_SHA = 0, |
| 73 | DCP_CHAN_CRYPTO = 2, |
| 74 | }; |
| 75 | |
| 76 | struct dcp_async_ctx { |
| 77 | /* Common context */ |
| 78 | enum dcp_chan chan; |
| 79 | uint32_t fill; |
| 80 | |
| 81 | /* SHA Hash-specific context */ |
| 82 | struct mutex mutex; |
| 83 | uint32_t alg; |
| 84 | unsigned int hot:1; |
| 85 | |
| 86 | /* Crypto-specific context */ |
Herbert Xu | 29406bb | 2016-06-29 18:04:02 +0800 | [diff] [blame] | 87 | struct crypto_skcipher *fallback; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 88 | unsigned int key_len; |
| 89 | uint8_t key[AES_KEYSIZE_128]; |
| 90 | }; |
| 91 | |
Marek Vasut | 2021aba | 2014-01-14 18:31:01 +0100 | [diff] [blame] | 92 | struct dcp_aes_req_ctx { |
| 93 | unsigned int enc:1; |
| 94 | unsigned int ecb:1; |
| 95 | }; |
| 96 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 97 | struct dcp_sha_req_ctx { |
| 98 | unsigned int init:1; |
| 99 | unsigned int fini:1; |
| 100 | }; |
| 101 | |
| 102 | /* |
| 103 | * There can even be only one instance of the MXS DCP due to the |
| 104 | * design of Linux Crypto API. |
| 105 | */ |
| 106 | static struct dcp *global_sdcp; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 107 | |
| 108 | /* DCP register layout. */ |
| 109 | #define MXS_DCP_CTRL 0x00 |
| 110 | #define MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES (1 << 23) |
| 111 | #define MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING (1 << 22) |
| 112 | |
| 113 | #define MXS_DCP_STAT 0x10 |
| 114 | #define MXS_DCP_STAT_CLR 0x18 |
| 115 | #define MXS_DCP_STAT_IRQ_MASK 0xf |
| 116 | |
| 117 | #define MXS_DCP_CHANNELCTRL 0x20 |
| 118 | #define MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK 0xff |
| 119 | |
| 120 | #define MXS_DCP_CAPABILITY1 0x40 |
| 121 | #define MXS_DCP_CAPABILITY1_SHA256 (4 << 16) |
| 122 | #define MXS_DCP_CAPABILITY1_SHA1 (1 << 16) |
| 123 | #define MXS_DCP_CAPABILITY1_AES128 (1 << 0) |
| 124 | |
| 125 | #define MXS_DCP_CONTEXT 0x50 |
| 126 | |
| 127 | #define MXS_DCP_CH_N_CMDPTR(n) (0x100 + ((n) * 0x40)) |
| 128 | |
| 129 | #define MXS_DCP_CH_N_SEMA(n) (0x110 + ((n) * 0x40)) |
| 130 | |
| 131 | #define MXS_DCP_CH_N_STAT(n) (0x120 + ((n) * 0x40)) |
| 132 | #define MXS_DCP_CH_N_STAT_CLR(n) (0x128 + ((n) * 0x40)) |
| 133 | |
| 134 | /* DMA descriptor bits. */ |
| 135 | #define MXS_DCP_CONTROL0_HASH_TERM (1 << 13) |
| 136 | #define MXS_DCP_CONTROL0_HASH_INIT (1 << 12) |
| 137 | #define MXS_DCP_CONTROL0_PAYLOAD_KEY (1 << 11) |
| 138 | #define MXS_DCP_CONTROL0_CIPHER_ENCRYPT (1 << 8) |
| 139 | #define MXS_DCP_CONTROL0_CIPHER_INIT (1 << 9) |
| 140 | #define MXS_DCP_CONTROL0_ENABLE_HASH (1 << 6) |
| 141 | #define MXS_DCP_CONTROL0_ENABLE_CIPHER (1 << 5) |
| 142 | #define MXS_DCP_CONTROL0_DECR_SEMAPHORE (1 << 1) |
| 143 | #define MXS_DCP_CONTROL0_INTERRUPT (1 << 0) |
| 144 | |
| 145 | #define MXS_DCP_CONTROL1_HASH_SELECT_SHA256 (2 << 16) |
| 146 | #define MXS_DCP_CONTROL1_HASH_SELECT_SHA1 (0 << 16) |
| 147 | #define MXS_DCP_CONTROL1_CIPHER_MODE_CBC (1 << 4) |
| 148 | #define MXS_DCP_CONTROL1_CIPHER_MODE_ECB (0 << 4) |
| 149 | #define MXS_DCP_CONTROL1_CIPHER_SELECT_AES128 (0 << 0) |
| 150 | |
| 151 | static int mxs_dcp_start_dma(struct dcp_async_ctx *actx) |
| 152 | { |
| 153 | struct dcp *sdcp = global_sdcp; |
| 154 | const int chan = actx->chan; |
| 155 | uint32_t stat; |
Nicholas Mc Guire | dd0fff8 | 2015-02-07 03:09:41 -0500 | [diff] [blame] | 156 | unsigned long ret; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 157 | struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan]; |
| 158 | |
| 159 | dma_addr_t desc_phys = dma_map_single(sdcp->dev, desc, sizeof(*desc), |
| 160 | DMA_TO_DEVICE); |
| 161 | |
| 162 | reinit_completion(&sdcp->completion[chan]); |
| 163 | |
| 164 | /* Clear status register. */ |
| 165 | writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(chan)); |
| 166 | |
| 167 | /* Load the DMA descriptor. */ |
| 168 | writel(desc_phys, sdcp->base + MXS_DCP_CH_N_CMDPTR(chan)); |
| 169 | |
| 170 | /* Increment the semaphore to start the DMA transfer. */ |
| 171 | writel(1, sdcp->base + MXS_DCP_CH_N_SEMA(chan)); |
| 172 | |
| 173 | ret = wait_for_completion_timeout(&sdcp->completion[chan], |
| 174 | msecs_to_jiffies(1000)); |
| 175 | if (!ret) { |
| 176 | dev_err(sdcp->dev, "Channel %i timeout (DCP_STAT=0x%08x)\n", |
| 177 | chan, readl(sdcp->base + MXS_DCP_STAT)); |
| 178 | return -ETIMEDOUT; |
| 179 | } |
| 180 | |
| 181 | stat = readl(sdcp->base + MXS_DCP_CH_N_STAT(chan)); |
| 182 | if (stat & 0xff) { |
| 183 | dev_err(sdcp->dev, "Channel %i error (CH_STAT=0x%08x)\n", |
| 184 | chan, stat); |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | |
| 188 | dma_unmap_single(sdcp->dev, desc_phys, sizeof(*desc), DMA_TO_DEVICE); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Encryption (AES128) |
| 195 | */ |
Marek Vasut | 2021aba | 2014-01-14 18:31:01 +0100 | [diff] [blame] | 196 | static int mxs_dcp_run_aes(struct dcp_async_ctx *actx, |
| 197 | struct ablkcipher_request *req, int init) |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 198 | { |
| 199 | struct dcp *sdcp = global_sdcp; |
| 200 | struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan]; |
Marek Vasut | 2021aba | 2014-01-14 18:31:01 +0100 | [diff] [blame] | 201 | struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 202 | int ret; |
| 203 | |
| 204 | dma_addr_t key_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_key, |
| 205 | 2 * AES_KEYSIZE_128, |
| 206 | DMA_TO_DEVICE); |
| 207 | dma_addr_t src_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_in_buf, |
| 208 | DCP_BUF_SZ, DMA_TO_DEVICE); |
| 209 | dma_addr_t dst_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_out_buf, |
| 210 | DCP_BUF_SZ, DMA_FROM_DEVICE); |
| 211 | |
| 212 | /* Fill in the DMA descriptor. */ |
| 213 | desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE | |
| 214 | MXS_DCP_CONTROL0_INTERRUPT | |
| 215 | MXS_DCP_CONTROL0_ENABLE_CIPHER; |
| 216 | |
| 217 | /* Payload contains the key. */ |
| 218 | desc->control0 |= MXS_DCP_CONTROL0_PAYLOAD_KEY; |
| 219 | |
Marek Vasut | 2021aba | 2014-01-14 18:31:01 +0100 | [diff] [blame] | 220 | if (rctx->enc) |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 221 | desc->control0 |= MXS_DCP_CONTROL0_CIPHER_ENCRYPT; |
| 222 | if (init) |
| 223 | desc->control0 |= MXS_DCP_CONTROL0_CIPHER_INIT; |
| 224 | |
| 225 | desc->control1 = MXS_DCP_CONTROL1_CIPHER_SELECT_AES128; |
| 226 | |
Marek Vasut | 2021aba | 2014-01-14 18:31:01 +0100 | [diff] [blame] | 227 | if (rctx->ecb) |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 228 | desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_ECB; |
| 229 | else |
| 230 | desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_CBC; |
| 231 | |
| 232 | desc->next_cmd_addr = 0; |
| 233 | desc->source = src_phys; |
| 234 | desc->destination = dst_phys; |
| 235 | desc->size = actx->fill; |
| 236 | desc->payload = key_phys; |
| 237 | desc->status = 0; |
| 238 | |
| 239 | ret = mxs_dcp_start_dma(actx); |
| 240 | |
| 241 | dma_unmap_single(sdcp->dev, key_phys, 2 * AES_KEYSIZE_128, |
| 242 | DMA_TO_DEVICE); |
| 243 | dma_unmap_single(sdcp->dev, src_phys, DCP_BUF_SZ, DMA_TO_DEVICE); |
| 244 | dma_unmap_single(sdcp->dev, dst_phys, DCP_BUF_SZ, DMA_FROM_DEVICE); |
| 245 | |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | static int mxs_dcp_aes_block_crypt(struct crypto_async_request *arq) |
| 250 | { |
| 251 | struct dcp *sdcp = global_sdcp; |
| 252 | |
| 253 | struct ablkcipher_request *req = ablkcipher_request_cast(arq); |
| 254 | struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm); |
Marek Vasut | 2021aba | 2014-01-14 18:31:01 +0100 | [diff] [blame] | 255 | struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 256 | |
| 257 | struct scatterlist *dst = req->dst; |
| 258 | struct scatterlist *src = req->src; |
| 259 | const int nents = sg_nents(req->src); |
| 260 | |
| 261 | const int out_off = DCP_BUF_SZ; |
| 262 | uint8_t *in_buf = sdcp->coh->aes_in_buf; |
| 263 | uint8_t *out_buf = sdcp->coh->aes_out_buf; |
| 264 | |
| 265 | uint8_t *out_tmp, *src_buf, *dst_buf = NULL; |
| 266 | uint32_t dst_off = 0; |
| 267 | |
| 268 | uint8_t *key = sdcp->coh->aes_key; |
| 269 | |
| 270 | int ret = 0; |
| 271 | int split = 0; |
| 272 | unsigned int i, len, clen, rem = 0; |
| 273 | int init = 0; |
| 274 | |
| 275 | actx->fill = 0; |
| 276 | |
| 277 | /* Copy the key from the temporary location. */ |
| 278 | memcpy(key, actx->key, actx->key_len); |
| 279 | |
Marek Vasut | 2021aba | 2014-01-14 18:31:01 +0100 | [diff] [blame] | 280 | if (!rctx->ecb) { |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 281 | /* Copy the CBC IV just past the key. */ |
| 282 | memcpy(key + AES_KEYSIZE_128, req->info, AES_KEYSIZE_128); |
| 283 | /* CBC needs the INIT set. */ |
| 284 | init = 1; |
| 285 | } else { |
| 286 | memset(key + AES_KEYSIZE_128, 0, AES_KEYSIZE_128); |
| 287 | } |
| 288 | |
| 289 | for_each_sg(req->src, src, nents, i) { |
| 290 | src_buf = sg_virt(src); |
| 291 | len = sg_dma_len(src); |
| 292 | |
| 293 | do { |
| 294 | if (actx->fill + len > out_off) |
| 295 | clen = out_off - actx->fill; |
| 296 | else |
| 297 | clen = len; |
| 298 | |
| 299 | memcpy(in_buf + actx->fill, src_buf, clen); |
| 300 | len -= clen; |
| 301 | src_buf += clen; |
| 302 | actx->fill += clen; |
| 303 | |
| 304 | /* |
| 305 | * If we filled the buffer or this is the last SG, |
| 306 | * submit the buffer. |
| 307 | */ |
| 308 | if (actx->fill == out_off || sg_is_last(src)) { |
Marek Vasut | 2021aba | 2014-01-14 18:31:01 +0100 | [diff] [blame] | 309 | ret = mxs_dcp_run_aes(actx, req, init); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 310 | if (ret) |
| 311 | return ret; |
| 312 | init = 0; |
| 313 | |
| 314 | out_tmp = out_buf; |
| 315 | while (dst && actx->fill) { |
| 316 | if (!split) { |
| 317 | dst_buf = sg_virt(dst); |
| 318 | dst_off = 0; |
| 319 | } |
| 320 | rem = min(sg_dma_len(dst) - dst_off, |
| 321 | actx->fill); |
| 322 | |
| 323 | memcpy(dst_buf + dst_off, out_tmp, rem); |
| 324 | out_tmp += rem; |
| 325 | dst_off += rem; |
| 326 | actx->fill -= rem; |
| 327 | |
| 328 | if (dst_off == sg_dma_len(dst)) { |
| 329 | dst = sg_next(dst); |
| 330 | split = 0; |
| 331 | } else { |
| 332 | split = 1; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } while (len); |
| 337 | } |
| 338 | |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | static int dcp_chan_thread_aes(void *data) |
| 343 | { |
| 344 | struct dcp *sdcp = global_sdcp; |
| 345 | const int chan = DCP_CHAN_CRYPTO; |
| 346 | |
| 347 | struct crypto_async_request *backlog; |
| 348 | struct crypto_async_request *arq; |
| 349 | |
| 350 | int ret; |
| 351 | |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 352 | while (!kthread_should_stop()) { |
| 353 | set_current_state(TASK_INTERRUPTIBLE); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 354 | |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 355 | spin_lock(&sdcp->lock[chan]); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 356 | backlog = crypto_get_backlog(&sdcp->queue[chan]); |
| 357 | arq = crypto_dequeue_request(&sdcp->queue[chan]); |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 358 | spin_unlock(&sdcp->lock[chan]); |
| 359 | |
| 360 | if (!backlog && !arq) { |
| 361 | schedule(); |
| 362 | continue; |
| 363 | } |
| 364 | |
| 365 | set_current_state(TASK_RUNNING); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 366 | |
| 367 | if (backlog) |
| 368 | backlog->complete(backlog, -EINPROGRESS); |
| 369 | |
| 370 | if (arq) { |
| 371 | ret = mxs_dcp_aes_block_crypt(arq); |
| 372 | arq->complete(arq, ret); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 373 | } |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 374 | } |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static int mxs_dcp_block_fallback(struct ablkcipher_request *req, int enc) |
| 380 | { |
Herbert Xu | 29406bb | 2016-06-29 18:04:02 +0800 | [diff] [blame] | 381 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); |
| 382 | struct dcp_async_ctx *ctx = crypto_ablkcipher_ctx(tfm); |
| 383 | SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 384 | int ret; |
| 385 | |
Herbert Xu | 29406bb | 2016-06-29 18:04:02 +0800 | [diff] [blame] | 386 | skcipher_request_set_tfm(subreq, ctx->fallback); |
| 387 | skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL); |
| 388 | skcipher_request_set_crypt(subreq, req->src, req->dst, |
| 389 | req->nbytes, req->info); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 390 | |
| 391 | if (enc) |
Herbert Xu | 29406bb | 2016-06-29 18:04:02 +0800 | [diff] [blame] | 392 | ret = crypto_skcipher_encrypt(subreq); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 393 | else |
Herbert Xu | 29406bb | 2016-06-29 18:04:02 +0800 | [diff] [blame] | 394 | ret = crypto_skcipher_decrypt(subreq); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 395 | |
Herbert Xu | 29406bb | 2016-06-29 18:04:02 +0800 | [diff] [blame] | 396 | skcipher_request_zero(subreq); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 397 | |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | static int mxs_dcp_aes_enqueue(struct ablkcipher_request *req, int enc, int ecb) |
| 402 | { |
| 403 | struct dcp *sdcp = global_sdcp; |
| 404 | struct crypto_async_request *arq = &req->base; |
| 405 | struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm); |
Marek Vasut | 2021aba | 2014-01-14 18:31:01 +0100 | [diff] [blame] | 406 | struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 407 | int ret; |
| 408 | |
| 409 | if (unlikely(actx->key_len != AES_KEYSIZE_128)) |
| 410 | return mxs_dcp_block_fallback(req, enc); |
| 411 | |
Marek Vasut | 2021aba | 2014-01-14 18:31:01 +0100 | [diff] [blame] | 412 | rctx->enc = enc; |
| 413 | rctx->ecb = ecb; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 414 | actx->chan = DCP_CHAN_CRYPTO; |
| 415 | |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 416 | spin_lock(&sdcp->lock[actx->chan]); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 417 | ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base); |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 418 | spin_unlock(&sdcp->lock[actx->chan]); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 419 | |
| 420 | wake_up_process(sdcp->thread[actx->chan]); |
| 421 | |
| 422 | return -EINPROGRESS; |
| 423 | } |
| 424 | |
| 425 | static int mxs_dcp_aes_ecb_decrypt(struct ablkcipher_request *req) |
| 426 | { |
| 427 | return mxs_dcp_aes_enqueue(req, 0, 1); |
| 428 | } |
| 429 | |
| 430 | static int mxs_dcp_aes_ecb_encrypt(struct ablkcipher_request *req) |
| 431 | { |
| 432 | return mxs_dcp_aes_enqueue(req, 1, 1); |
| 433 | } |
| 434 | |
| 435 | static int mxs_dcp_aes_cbc_decrypt(struct ablkcipher_request *req) |
| 436 | { |
| 437 | return mxs_dcp_aes_enqueue(req, 0, 0); |
| 438 | } |
| 439 | |
| 440 | static int mxs_dcp_aes_cbc_encrypt(struct ablkcipher_request *req) |
| 441 | { |
| 442 | return mxs_dcp_aes_enqueue(req, 1, 0); |
| 443 | } |
| 444 | |
| 445 | static int mxs_dcp_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key, |
| 446 | unsigned int len) |
| 447 | { |
| 448 | struct dcp_async_ctx *actx = crypto_ablkcipher_ctx(tfm); |
| 449 | unsigned int ret; |
| 450 | |
| 451 | /* |
| 452 | * AES 128 is supposed by the hardware, store key into temporary |
| 453 | * buffer and exit. We must use the temporary buffer here, since |
| 454 | * there can still be an operation in progress. |
| 455 | */ |
| 456 | actx->key_len = len; |
| 457 | if (len == AES_KEYSIZE_128) { |
| 458 | memcpy(actx->key, key, len); |
| 459 | return 0; |
| 460 | } |
| 461 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 462 | /* |
| 463 | * If the requested AES key size is not supported by the hardware, |
| 464 | * but is supported by in-kernel software implementation, we use |
| 465 | * software fallback. |
| 466 | */ |
Herbert Xu | 29406bb | 2016-06-29 18:04:02 +0800 | [diff] [blame] | 467 | crypto_skcipher_clear_flags(actx->fallback, CRYPTO_TFM_REQ_MASK); |
| 468 | crypto_skcipher_set_flags(actx->fallback, |
| 469 | tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 470 | |
Herbert Xu | 29406bb | 2016-06-29 18:04:02 +0800 | [diff] [blame] | 471 | ret = crypto_skcipher_setkey(actx->fallback, key, len); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 472 | if (!ret) |
| 473 | return 0; |
| 474 | |
| 475 | tfm->base.crt_flags &= ~CRYPTO_TFM_RES_MASK; |
Herbert Xu | 29406bb | 2016-06-29 18:04:02 +0800 | [diff] [blame] | 476 | tfm->base.crt_flags |= crypto_skcipher_get_flags(actx->fallback) & |
| 477 | CRYPTO_TFM_RES_MASK; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 478 | |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | static int mxs_dcp_aes_fallback_init(struct crypto_tfm *tfm) |
| 483 | { |
Marek Vasut | 2231204 | 2014-05-14 11:41:00 +0200 | [diff] [blame] | 484 | const char *name = crypto_tfm_alg_name(tfm); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 485 | const uint32_t flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK; |
| 486 | struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm); |
Herbert Xu | 29406bb | 2016-06-29 18:04:02 +0800 | [diff] [blame] | 487 | struct crypto_skcipher *blk; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 488 | |
Herbert Xu | 29406bb | 2016-06-29 18:04:02 +0800 | [diff] [blame] | 489 | blk = crypto_alloc_skcipher(name, 0, flags); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 490 | if (IS_ERR(blk)) |
| 491 | return PTR_ERR(blk); |
| 492 | |
| 493 | actx->fallback = blk; |
Marek Vasut | 2021aba | 2014-01-14 18:31:01 +0100 | [diff] [blame] | 494 | tfm->crt_ablkcipher.reqsize = sizeof(struct dcp_aes_req_ctx); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static void mxs_dcp_aes_fallback_exit(struct crypto_tfm *tfm) |
| 499 | { |
| 500 | struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm); |
| 501 | |
Herbert Xu | 29406bb | 2016-06-29 18:04:02 +0800 | [diff] [blame] | 502 | crypto_free_skcipher(actx->fallback); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Hashing (SHA1/SHA256) |
| 507 | */ |
| 508 | static int mxs_dcp_run_sha(struct ahash_request *req) |
| 509 | { |
| 510 | struct dcp *sdcp = global_sdcp; |
| 511 | int ret; |
| 512 | |
| 513 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 514 | struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm); |
| 515 | struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req); |
Marek Vasut | 04d088c | 2014-03-03 13:40:30 +0100 | [diff] [blame] | 516 | struct hash_alg_common *halg = crypto_hash_alg_common(tfm); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 517 | |
| 518 | struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan]; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 519 | |
Marek Vasut | 04d088c | 2014-03-03 13:40:30 +0100 | [diff] [blame] | 520 | dma_addr_t digest_phys = 0; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 521 | dma_addr_t buf_phys = dma_map_single(sdcp->dev, sdcp->coh->sha_in_buf, |
| 522 | DCP_BUF_SZ, DMA_TO_DEVICE); |
| 523 | |
| 524 | /* Fill in the DMA descriptor. */ |
| 525 | desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE | |
| 526 | MXS_DCP_CONTROL0_INTERRUPT | |
| 527 | MXS_DCP_CONTROL0_ENABLE_HASH; |
| 528 | if (rctx->init) |
| 529 | desc->control0 |= MXS_DCP_CONTROL0_HASH_INIT; |
| 530 | |
| 531 | desc->control1 = actx->alg; |
| 532 | desc->next_cmd_addr = 0; |
| 533 | desc->source = buf_phys; |
| 534 | desc->destination = 0; |
| 535 | desc->size = actx->fill; |
| 536 | desc->payload = 0; |
| 537 | desc->status = 0; |
| 538 | |
| 539 | /* Set HASH_TERM bit for last transfer block. */ |
| 540 | if (rctx->fini) { |
Marek Vasut | 04d088c | 2014-03-03 13:40:30 +0100 | [diff] [blame] | 541 | digest_phys = dma_map_single(sdcp->dev, req->result, |
| 542 | halg->digestsize, DMA_FROM_DEVICE); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 543 | desc->control0 |= MXS_DCP_CONTROL0_HASH_TERM; |
| 544 | desc->payload = digest_phys; |
| 545 | } |
| 546 | |
| 547 | ret = mxs_dcp_start_dma(actx); |
| 548 | |
Marek Vasut | 04d088c | 2014-03-03 13:40:30 +0100 | [diff] [blame] | 549 | if (rctx->fini) |
| 550 | dma_unmap_single(sdcp->dev, digest_phys, halg->digestsize, |
| 551 | DMA_FROM_DEVICE); |
| 552 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 553 | dma_unmap_single(sdcp->dev, buf_phys, DCP_BUF_SZ, DMA_TO_DEVICE); |
| 554 | |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | static int dcp_sha_req_to_buf(struct crypto_async_request *arq) |
| 559 | { |
| 560 | struct dcp *sdcp = global_sdcp; |
| 561 | |
| 562 | struct ahash_request *req = ahash_request_cast(arq); |
| 563 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 564 | struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm); |
| 565 | struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 566 | struct hash_alg_common *halg = crypto_hash_alg_common(tfm); |
| 567 | const int nents = sg_nents(req->src); |
| 568 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 569 | uint8_t *in_buf = sdcp->coh->sha_in_buf; |
| 570 | |
| 571 | uint8_t *src_buf; |
| 572 | |
| 573 | struct scatterlist *src; |
| 574 | |
| 575 | unsigned int i, len, clen; |
| 576 | int ret; |
| 577 | |
| 578 | int fin = rctx->fini; |
| 579 | if (fin) |
| 580 | rctx->fini = 0; |
| 581 | |
| 582 | for_each_sg(req->src, src, nents, i) { |
| 583 | src_buf = sg_virt(src); |
| 584 | len = sg_dma_len(src); |
| 585 | |
| 586 | do { |
| 587 | if (actx->fill + len > DCP_BUF_SZ) |
| 588 | clen = DCP_BUF_SZ - actx->fill; |
| 589 | else |
| 590 | clen = len; |
| 591 | |
| 592 | memcpy(in_buf + actx->fill, src_buf, clen); |
| 593 | len -= clen; |
| 594 | src_buf += clen; |
| 595 | actx->fill += clen; |
| 596 | |
| 597 | /* |
| 598 | * If we filled the buffer and still have some |
| 599 | * more data, submit the buffer. |
| 600 | */ |
| 601 | if (len && actx->fill == DCP_BUF_SZ) { |
| 602 | ret = mxs_dcp_run_sha(req); |
| 603 | if (ret) |
| 604 | return ret; |
| 605 | actx->fill = 0; |
| 606 | rctx->init = 0; |
| 607 | } |
| 608 | } while (len); |
| 609 | } |
| 610 | |
| 611 | if (fin) { |
| 612 | rctx->fini = 1; |
| 613 | |
| 614 | /* Submit whatever is left. */ |
Marek Vasut | 04d088c | 2014-03-03 13:40:30 +0100 | [diff] [blame] | 615 | if (!req->result) |
| 616 | return -EINVAL; |
| 617 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 618 | ret = mxs_dcp_run_sha(req); |
Marek Vasut | 04d088c | 2014-03-03 13:40:30 +0100 | [diff] [blame] | 619 | if (ret) |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 620 | return ret; |
Marek Vasut | 04d088c | 2014-03-03 13:40:30 +0100 | [diff] [blame] | 621 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 622 | actx->fill = 0; |
| 623 | |
| 624 | /* For some reason, the result is flipped. */ |
Marek Vasut | 04d088c | 2014-03-03 13:40:30 +0100 | [diff] [blame] | 625 | for (i = 0; i < halg->digestsize / 2; i++) { |
| 626 | swap(req->result[i], |
| 627 | req->result[halg->digestsize - i - 1]); |
| 628 | } |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | static int dcp_chan_thread_sha(void *data) |
| 635 | { |
| 636 | struct dcp *sdcp = global_sdcp; |
| 637 | const int chan = DCP_CHAN_HASH_SHA; |
| 638 | |
| 639 | struct crypto_async_request *backlog; |
| 640 | struct crypto_async_request *arq; |
| 641 | |
| 642 | struct dcp_sha_req_ctx *rctx; |
| 643 | |
| 644 | struct ahash_request *req; |
| 645 | int ret, fini; |
| 646 | |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 647 | while (!kthread_should_stop()) { |
| 648 | set_current_state(TASK_INTERRUPTIBLE); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 649 | |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 650 | spin_lock(&sdcp->lock[chan]); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 651 | backlog = crypto_get_backlog(&sdcp->queue[chan]); |
| 652 | arq = crypto_dequeue_request(&sdcp->queue[chan]); |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 653 | spin_unlock(&sdcp->lock[chan]); |
| 654 | |
| 655 | if (!backlog && !arq) { |
| 656 | schedule(); |
| 657 | continue; |
| 658 | } |
| 659 | |
| 660 | set_current_state(TASK_RUNNING); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 661 | |
| 662 | if (backlog) |
| 663 | backlog->complete(backlog, -EINPROGRESS); |
| 664 | |
| 665 | if (arq) { |
| 666 | req = ahash_request_cast(arq); |
| 667 | rctx = ahash_request_ctx(req); |
| 668 | |
| 669 | ret = dcp_sha_req_to_buf(arq); |
| 670 | fini = rctx->fini; |
| 671 | arq->complete(arq, ret); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 672 | } |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 673 | } |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | static int dcp_sha_init(struct ahash_request *req) |
| 679 | { |
| 680 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 681 | struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm); |
| 682 | |
| 683 | struct hash_alg_common *halg = crypto_hash_alg_common(tfm); |
| 684 | |
| 685 | /* |
| 686 | * Start hashing session. The code below only inits the |
| 687 | * hashing session context, nothing more. |
| 688 | */ |
| 689 | memset(actx, 0, sizeof(*actx)); |
| 690 | |
| 691 | if (strcmp(halg->base.cra_name, "sha1") == 0) |
| 692 | actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA1; |
| 693 | else |
| 694 | actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA256; |
| 695 | |
| 696 | actx->fill = 0; |
| 697 | actx->hot = 0; |
| 698 | actx->chan = DCP_CHAN_HASH_SHA; |
| 699 | |
| 700 | mutex_init(&actx->mutex); |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int dcp_sha_update_fx(struct ahash_request *req, int fini) |
| 706 | { |
| 707 | struct dcp *sdcp = global_sdcp; |
| 708 | |
| 709 | struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 710 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 711 | struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm); |
| 712 | |
| 713 | int ret; |
| 714 | |
| 715 | /* |
| 716 | * Ignore requests that have no data in them and are not |
| 717 | * the trailing requests in the stream of requests. |
| 718 | */ |
| 719 | if (!req->nbytes && !fini) |
| 720 | return 0; |
| 721 | |
| 722 | mutex_lock(&actx->mutex); |
| 723 | |
| 724 | rctx->fini = fini; |
| 725 | |
| 726 | if (!actx->hot) { |
| 727 | actx->hot = 1; |
| 728 | rctx->init = 1; |
| 729 | } |
| 730 | |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 731 | spin_lock(&sdcp->lock[actx->chan]); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 732 | ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base); |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 733 | spin_unlock(&sdcp->lock[actx->chan]); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 734 | |
| 735 | wake_up_process(sdcp->thread[actx->chan]); |
| 736 | mutex_unlock(&actx->mutex); |
| 737 | |
| 738 | return -EINPROGRESS; |
| 739 | } |
| 740 | |
| 741 | static int dcp_sha_update(struct ahash_request *req) |
| 742 | { |
| 743 | return dcp_sha_update_fx(req, 0); |
| 744 | } |
| 745 | |
| 746 | static int dcp_sha_final(struct ahash_request *req) |
| 747 | { |
| 748 | ahash_request_set_crypt(req, NULL, req->result, 0); |
| 749 | req->nbytes = 0; |
| 750 | return dcp_sha_update_fx(req, 1); |
| 751 | } |
| 752 | |
| 753 | static int dcp_sha_finup(struct ahash_request *req) |
| 754 | { |
| 755 | return dcp_sha_update_fx(req, 1); |
| 756 | } |
| 757 | |
| 758 | static int dcp_sha_digest(struct ahash_request *req) |
| 759 | { |
| 760 | int ret; |
| 761 | |
| 762 | ret = dcp_sha_init(req); |
| 763 | if (ret) |
| 764 | return ret; |
| 765 | |
| 766 | return dcp_sha_finup(req); |
| 767 | } |
| 768 | |
| 769 | static int dcp_sha_cra_init(struct crypto_tfm *tfm) |
| 770 | { |
| 771 | crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), |
| 772 | sizeof(struct dcp_sha_req_ctx)); |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | static void dcp_sha_cra_exit(struct crypto_tfm *tfm) |
| 777 | { |
| 778 | } |
| 779 | |
| 780 | /* AES 128 ECB and AES 128 CBC */ |
| 781 | static struct crypto_alg dcp_aes_algs[] = { |
| 782 | { |
| 783 | .cra_name = "ecb(aes)", |
| 784 | .cra_driver_name = "ecb-aes-dcp", |
| 785 | .cra_priority = 400, |
| 786 | .cra_alignmask = 15, |
| 787 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 788 | CRYPTO_ALG_ASYNC | |
| 789 | CRYPTO_ALG_NEED_FALLBACK, |
| 790 | .cra_init = mxs_dcp_aes_fallback_init, |
| 791 | .cra_exit = mxs_dcp_aes_fallback_exit, |
| 792 | .cra_blocksize = AES_BLOCK_SIZE, |
| 793 | .cra_ctxsize = sizeof(struct dcp_async_ctx), |
| 794 | .cra_type = &crypto_ablkcipher_type, |
| 795 | .cra_module = THIS_MODULE, |
| 796 | .cra_u = { |
| 797 | .ablkcipher = { |
| 798 | .min_keysize = AES_MIN_KEY_SIZE, |
| 799 | .max_keysize = AES_MAX_KEY_SIZE, |
| 800 | .setkey = mxs_dcp_aes_setkey, |
| 801 | .encrypt = mxs_dcp_aes_ecb_encrypt, |
| 802 | .decrypt = mxs_dcp_aes_ecb_decrypt |
| 803 | }, |
| 804 | }, |
| 805 | }, { |
| 806 | .cra_name = "cbc(aes)", |
| 807 | .cra_driver_name = "cbc-aes-dcp", |
| 808 | .cra_priority = 400, |
| 809 | .cra_alignmask = 15, |
| 810 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 811 | CRYPTO_ALG_ASYNC | |
| 812 | CRYPTO_ALG_NEED_FALLBACK, |
| 813 | .cra_init = mxs_dcp_aes_fallback_init, |
| 814 | .cra_exit = mxs_dcp_aes_fallback_exit, |
| 815 | .cra_blocksize = AES_BLOCK_SIZE, |
| 816 | .cra_ctxsize = sizeof(struct dcp_async_ctx), |
| 817 | .cra_type = &crypto_ablkcipher_type, |
| 818 | .cra_module = THIS_MODULE, |
| 819 | .cra_u = { |
| 820 | .ablkcipher = { |
| 821 | .min_keysize = AES_MIN_KEY_SIZE, |
| 822 | .max_keysize = AES_MAX_KEY_SIZE, |
| 823 | .setkey = mxs_dcp_aes_setkey, |
| 824 | .encrypt = mxs_dcp_aes_cbc_encrypt, |
| 825 | .decrypt = mxs_dcp_aes_cbc_decrypt, |
| 826 | .ivsize = AES_BLOCK_SIZE, |
| 827 | }, |
| 828 | }, |
| 829 | }, |
| 830 | }; |
| 831 | |
| 832 | /* SHA1 */ |
| 833 | static struct ahash_alg dcp_sha1_alg = { |
| 834 | .init = dcp_sha_init, |
| 835 | .update = dcp_sha_update, |
| 836 | .final = dcp_sha_final, |
| 837 | .finup = dcp_sha_finup, |
| 838 | .digest = dcp_sha_digest, |
| 839 | .halg = { |
| 840 | .digestsize = SHA1_DIGEST_SIZE, |
| 841 | .base = { |
| 842 | .cra_name = "sha1", |
| 843 | .cra_driver_name = "sha1-dcp", |
| 844 | .cra_priority = 400, |
| 845 | .cra_alignmask = 63, |
| 846 | .cra_flags = CRYPTO_ALG_ASYNC, |
| 847 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 848 | .cra_ctxsize = sizeof(struct dcp_async_ctx), |
| 849 | .cra_module = THIS_MODULE, |
| 850 | .cra_init = dcp_sha_cra_init, |
| 851 | .cra_exit = dcp_sha_cra_exit, |
| 852 | }, |
| 853 | }, |
| 854 | }; |
| 855 | |
| 856 | /* SHA256 */ |
| 857 | static struct ahash_alg dcp_sha256_alg = { |
| 858 | .init = dcp_sha_init, |
| 859 | .update = dcp_sha_update, |
| 860 | .final = dcp_sha_final, |
| 861 | .finup = dcp_sha_finup, |
| 862 | .digest = dcp_sha_digest, |
| 863 | .halg = { |
| 864 | .digestsize = SHA256_DIGEST_SIZE, |
| 865 | .base = { |
| 866 | .cra_name = "sha256", |
| 867 | .cra_driver_name = "sha256-dcp", |
| 868 | .cra_priority = 400, |
| 869 | .cra_alignmask = 63, |
| 870 | .cra_flags = CRYPTO_ALG_ASYNC, |
| 871 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 872 | .cra_ctxsize = sizeof(struct dcp_async_ctx), |
| 873 | .cra_module = THIS_MODULE, |
| 874 | .cra_init = dcp_sha_cra_init, |
| 875 | .cra_exit = dcp_sha_cra_exit, |
| 876 | }, |
| 877 | }, |
| 878 | }; |
| 879 | |
| 880 | static irqreturn_t mxs_dcp_irq(int irq, void *context) |
| 881 | { |
| 882 | struct dcp *sdcp = context; |
| 883 | uint32_t stat; |
| 884 | int i; |
| 885 | |
| 886 | stat = readl(sdcp->base + MXS_DCP_STAT); |
| 887 | stat &= MXS_DCP_STAT_IRQ_MASK; |
| 888 | if (!stat) |
| 889 | return IRQ_NONE; |
| 890 | |
| 891 | /* Clear the interrupts. */ |
| 892 | writel(stat, sdcp->base + MXS_DCP_STAT_CLR); |
| 893 | |
| 894 | /* Complete the DMA requests that finished. */ |
| 895 | for (i = 0; i < DCP_MAX_CHANS; i++) |
| 896 | if (stat & (1 << i)) |
| 897 | complete(&sdcp->completion[i]); |
| 898 | |
| 899 | return IRQ_HANDLED; |
| 900 | } |
| 901 | |
| 902 | static int mxs_dcp_probe(struct platform_device *pdev) |
| 903 | { |
| 904 | struct device *dev = &pdev->dev; |
| 905 | struct dcp *sdcp = NULL; |
| 906 | int i, ret; |
| 907 | |
| 908 | struct resource *iores; |
| 909 | int dcp_vmi_irq, dcp_irq; |
| 910 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 911 | if (global_sdcp) { |
| 912 | dev_err(dev, "Only one DCP instance allowed!\n"); |
Fabio Estevam | 5fc8005 | 2014-05-12 08:44:28 -0300 | [diff] [blame] | 913 | return -ENODEV; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 917 | dcp_vmi_irq = platform_get_irq(pdev, 0); |
Fabio Estevam | 5fc8005 | 2014-05-12 08:44:28 -0300 | [diff] [blame] | 918 | if (dcp_vmi_irq < 0) |
| 919 | return dcp_vmi_irq; |
Fabio Estevam | d9588f8 | 2014-02-14 01:04:44 -0200 | [diff] [blame] | 920 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 921 | dcp_irq = platform_get_irq(pdev, 1); |
Fabio Estevam | 5fc8005 | 2014-05-12 08:44:28 -0300 | [diff] [blame] | 922 | if (dcp_irq < 0) |
| 923 | return dcp_irq; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 924 | |
| 925 | sdcp = devm_kzalloc(dev, sizeof(*sdcp), GFP_KERNEL); |
Fabio Estevam | 5fc8005 | 2014-05-12 08:44:28 -0300 | [diff] [blame] | 926 | if (!sdcp) |
| 927 | return -ENOMEM; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 928 | |
| 929 | sdcp->dev = dev; |
| 930 | sdcp->base = devm_ioremap_resource(dev, iores); |
Fabio Estevam | 5fc8005 | 2014-05-12 08:44:28 -0300 | [diff] [blame] | 931 | if (IS_ERR(sdcp->base)) |
| 932 | return PTR_ERR(sdcp->base); |
| 933 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 934 | |
| 935 | ret = devm_request_irq(dev, dcp_vmi_irq, mxs_dcp_irq, 0, |
| 936 | "dcp-vmi-irq", sdcp); |
| 937 | if (ret) { |
| 938 | dev_err(dev, "Failed to claim DCP VMI IRQ!\n"); |
Fabio Estevam | 5fc8005 | 2014-05-12 08:44:28 -0300 | [diff] [blame] | 939 | return ret; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | ret = devm_request_irq(dev, dcp_irq, mxs_dcp_irq, 0, |
| 943 | "dcp-irq", sdcp); |
| 944 | if (ret) { |
| 945 | dev_err(dev, "Failed to claim DCP IRQ!\n"); |
Fabio Estevam | 5fc8005 | 2014-05-12 08:44:28 -0300 | [diff] [blame] | 946 | return ret; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | /* Allocate coherent helper block. */ |
Marek Vasut | 1a7c685 | 2014-03-03 01:23:15 +0100 | [diff] [blame] | 950 | sdcp->coh = devm_kzalloc(dev, sizeof(*sdcp->coh) + DCP_ALIGNMENT, |
| 951 | GFP_KERNEL); |
Fabio Estevam | 5fc8005 | 2014-05-12 08:44:28 -0300 | [diff] [blame] | 952 | if (!sdcp->coh) |
| 953 | return -ENOMEM; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 954 | |
Marek Vasut | 1a7c685 | 2014-03-03 01:23:15 +0100 | [diff] [blame] | 955 | /* Re-align the structure so it fits the DCP constraints. */ |
| 956 | sdcp->coh = PTR_ALIGN(sdcp->coh, DCP_ALIGNMENT); |
| 957 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 958 | /* Restart the DCP block. */ |
Fabio Estevam | fecfd7f | 2014-01-28 22:36:12 -0200 | [diff] [blame] | 959 | ret = stmp_reset_block(sdcp->base); |
| 960 | if (ret) |
Fabio Estevam | 5fc8005 | 2014-05-12 08:44:28 -0300 | [diff] [blame] | 961 | return ret; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 962 | |
| 963 | /* Initialize control register. */ |
| 964 | writel(MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES | |
| 965 | MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING | 0xf, |
| 966 | sdcp->base + MXS_DCP_CTRL); |
| 967 | |
| 968 | /* Enable all DCP DMA channels. */ |
| 969 | writel(MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK, |
| 970 | sdcp->base + MXS_DCP_CHANNELCTRL); |
| 971 | |
| 972 | /* |
| 973 | * We do not enable context switching. Give the context buffer a |
| 974 | * pointer to an illegal address so if context switching is |
| 975 | * inadvertantly enabled, the DCP will return an error instead of |
| 976 | * trashing good memory. The DCP DMA cannot access ROM, so any ROM |
| 977 | * address will do. |
| 978 | */ |
| 979 | writel(0xffff0000, sdcp->base + MXS_DCP_CONTEXT); |
| 980 | for (i = 0; i < DCP_MAX_CHANS; i++) |
| 981 | writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(i)); |
| 982 | writel(0xffffffff, sdcp->base + MXS_DCP_STAT_CLR); |
| 983 | |
| 984 | global_sdcp = sdcp; |
| 985 | |
| 986 | platform_set_drvdata(pdev, sdcp); |
| 987 | |
| 988 | for (i = 0; i < DCP_MAX_CHANS; i++) { |
Leonard Crestez | d49c7bb | 2018-09-21 18:03:18 +0300 | [diff] [blame] | 989 | spin_lock_init(&sdcp->lock[i]); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 990 | init_completion(&sdcp->completion[i]); |
| 991 | crypto_init_queue(&sdcp->queue[i], 50); |
| 992 | } |
| 993 | |
| 994 | /* Create the SHA and AES handler threads. */ |
| 995 | sdcp->thread[DCP_CHAN_HASH_SHA] = kthread_run(dcp_chan_thread_sha, |
| 996 | NULL, "mxs_dcp_chan/sha"); |
| 997 | if (IS_ERR(sdcp->thread[DCP_CHAN_HASH_SHA])) { |
| 998 | dev_err(dev, "Error starting SHA thread!\n"); |
Fabio Estevam | 5fc8005 | 2014-05-12 08:44:28 -0300 | [diff] [blame] | 999 | return PTR_ERR(sdcp->thread[DCP_CHAN_HASH_SHA]); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | sdcp->thread[DCP_CHAN_CRYPTO] = kthread_run(dcp_chan_thread_aes, |
| 1003 | NULL, "mxs_dcp_chan/aes"); |
| 1004 | if (IS_ERR(sdcp->thread[DCP_CHAN_CRYPTO])) { |
| 1005 | dev_err(dev, "Error starting SHA thread!\n"); |
| 1006 | ret = PTR_ERR(sdcp->thread[DCP_CHAN_CRYPTO]); |
| 1007 | goto err_destroy_sha_thread; |
| 1008 | } |
| 1009 | |
| 1010 | /* Register the various crypto algorithms. */ |
| 1011 | sdcp->caps = readl(sdcp->base + MXS_DCP_CAPABILITY1); |
| 1012 | |
| 1013 | if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128) { |
| 1014 | ret = crypto_register_algs(dcp_aes_algs, |
| 1015 | ARRAY_SIZE(dcp_aes_algs)); |
| 1016 | if (ret) { |
| 1017 | /* Failed to register algorithm. */ |
| 1018 | dev_err(dev, "Failed to register AES crypto!\n"); |
| 1019 | goto err_destroy_aes_thread; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1) { |
| 1024 | ret = crypto_register_ahash(&dcp_sha1_alg); |
| 1025 | if (ret) { |
| 1026 | dev_err(dev, "Failed to register %s hash!\n", |
| 1027 | dcp_sha1_alg.halg.base.cra_name); |
| 1028 | goto err_unregister_aes; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256) { |
| 1033 | ret = crypto_register_ahash(&dcp_sha256_alg); |
| 1034 | if (ret) { |
| 1035 | dev_err(dev, "Failed to register %s hash!\n", |
| 1036 | dcp_sha256_alg.halg.base.cra_name); |
| 1037 | goto err_unregister_sha1; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | return 0; |
| 1042 | |
| 1043 | err_unregister_sha1: |
| 1044 | if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1) |
| 1045 | crypto_unregister_ahash(&dcp_sha1_alg); |
| 1046 | |
| 1047 | err_unregister_aes: |
| 1048 | if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128) |
| 1049 | crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs)); |
| 1050 | |
| 1051 | err_destroy_aes_thread: |
| 1052 | kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]); |
| 1053 | |
| 1054 | err_destroy_sha_thread: |
| 1055 | kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]); |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 1056 | return ret; |
| 1057 | } |
| 1058 | |
| 1059 | static int mxs_dcp_remove(struct platform_device *pdev) |
| 1060 | { |
| 1061 | struct dcp *sdcp = platform_get_drvdata(pdev); |
| 1062 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 1063 | if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256) |
| 1064 | crypto_unregister_ahash(&dcp_sha256_alg); |
| 1065 | |
| 1066 | if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1) |
| 1067 | crypto_unregister_ahash(&dcp_sha1_alg); |
| 1068 | |
| 1069 | if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128) |
| 1070 | crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs)); |
| 1071 | |
| 1072 | kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]); |
| 1073 | kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]); |
| 1074 | |
| 1075 | platform_set_drvdata(pdev, NULL); |
| 1076 | |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 1077 | global_sdcp = NULL; |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 1078 | |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | static const struct of_device_id mxs_dcp_dt_ids[] = { |
| 1083 | { .compatible = "fsl,imx23-dcp", .data = NULL, }, |
| 1084 | { .compatible = "fsl,imx28-dcp", .data = NULL, }, |
| 1085 | { /* sentinel */ } |
| 1086 | }; |
| 1087 | |
| 1088 | MODULE_DEVICE_TABLE(of, mxs_dcp_dt_ids); |
| 1089 | |
| 1090 | static struct platform_driver mxs_dcp_driver = { |
| 1091 | .probe = mxs_dcp_probe, |
| 1092 | .remove = mxs_dcp_remove, |
| 1093 | .driver = { |
| 1094 | .name = "mxs-dcp", |
Marek Vasut | 15b59e7 | 2013-12-10 20:26:21 +0100 | [diff] [blame] | 1095 | .of_match_table = mxs_dcp_dt_ids, |
| 1096 | }, |
| 1097 | }; |
| 1098 | |
| 1099 | module_platform_driver(mxs_dcp_driver); |
| 1100 | |
| 1101 | MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); |
| 1102 | MODULE_DESCRIPTION("Freescale MXS DCP Driver"); |
| 1103 | MODULE_LICENSE("GPL"); |
| 1104 | MODULE_ALIAS("platform:mxs-dcp"); |