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