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