AnilKumar Chimata | 7214d7e | 2017-06-23 03:09:59 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * DM request based crypto driver |
| 3 | * |
| 4 | * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 and |
| 8 | * only version 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/completion.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/bio.h> |
| 22 | #include <linux/blkdev.h> |
| 23 | #include <linux/mempool.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/crypto.h> |
| 26 | #include <linux/qcrypto.h> |
| 27 | #include <linux/workqueue.h> |
| 28 | #include <linux/backing-dev.h> |
| 29 | #include <linux/atomic.h> |
| 30 | #include <linux/scatterlist.h> |
| 31 | #include <linux/device-mapper.h> |
| 32 | #include <linux/printk.h> |
| 33 | |
| 34 | #include <asm/page.h> |
| 35 | #include <asm/unaligned.h> |
| 36 | #include <crypto/scatterwalk.h> |
| 37 | #include <crypto/hash.h> |
| 38 | #include <crypto/md5.h> |
| 39 | #include <crypto/algapi.h> |
| 40 | #include <crypto/ice.h> |
| 41 | |
| 42 | #define DM_MSG_PREFIX "req-crypt" |
| 43 | |
| 44 | #define MAX_SG_LIST 1024 |
| 45 | #define REQ_DM_512_KB (512*1024) |
| 46 | #define MAX_ENCRYPTION_BUFFERS 1 |
| 47 | #define MIN_IOS 256 |
| 48 | #define MIN_POOL_PAGES 32 |
| 49 | #define KEY_SIZE_XTS 32 |
| 50 | #define AES_XTS_IV_LEN 16 |
| 51 | #define MAX_MSM_ICE_KEY_LUT_SIZE 32 |
| 52 | #define SECTOR_SIZE 512 |
| 53 | #define MIN_CRYPTO_TRANSFER_SIZE (4 * 1024) |
| 54 | |
| 55 | #define DM_REQ_CRYPT_ERROR -1 |
| 56 | #define DM_REQ_CRYPT_ERROR_AFTER_PAGE_MALLOC -2 |
| 57 | |
| 58 | /* |
| 59 | * ENCRYPTION_MODE_CRYPTO means dm-req-crypt would invoke crypto operations |
| 60 | * for all of the requests. Crypto operations are performed by crypto engine |
| 61 | * plugged with Linux Kernel Crypto APIs |
| 62 | */ |
| 63 | #define DM_REQ_CRYPT_ENCRYPTION_MODE_CRYPTO 0 |
| 64 | /* |
| 65 | * ENCRYPTION_MODE_TRANSPARENT means dm-req-crypt would not invoke crypto |
| 66 | * operations for any of the requests. Data would be encrypted or decrypted |
| 67 | * using Inline Crypto Engine(ICE) embedded in storage hardware |
| 68 | */ |
| 69 | #define DM_REQ_CRYPT_ENCRYPTION_MODE_TRANSPARENT 1 |
| 70 | |
| 71 | #define DM_REQ_CRYPT_QUEUE_SIZE 256 |
| 72 | |
| 73 | struct req_crypt_result { |
| 74 | struct completion completion; |
| 75 | int err; |
| 76 | }; |
| 77 | |
| 78 | #define FDE_KEY_ID 0 |
| 79 | #define PFE_KEY_ID 1 |
| 80 | |
| 81 | static struct dm_dev *dev; |
| 82 | static struct kmem_cache *_req_crypt_io_pool; |
| 83 | static struct kmem_cache *_req_dm_scatterlist_pool; |
| 84 | static sector_t start_sector_orig; |
| 85 | static struct workqueue_struct *req_crypt_queue; |
| 86 | static struct workqueue_struct *req_crypt_split_io_queue; |
| 87 | static mempool_t *req_io_pool; |
| 88 | static mempool_t *req_page_pool; |
| 89 | static mempool_t *req_scatterlist_pool; |
| 90 | static bool is_fde_enabled; |
| 91 | static struct crypto_ablkcipher *tfm; |
| 92 | static unsigned int encryption_mode; |
| 93 | static struct ice_crypto_setting *ice_settings; |
| 94 | |
| 95 | unsigned int num_engines; |
| 96 | unsigned int num_engines_fde, fde_cursor; |
| 97 | unsigned int num_engines_pfe, pfe_cursor; |
| 98 | struct crypto_engine_entry *fde_eng, *pfe_eng; |
| 99 | DEFINE_MUTEX(engine_list_mutex); |
| 100 | |
| 101 | struct req_dm_crypt_io { |
| 102 | struct ice_crypto_setting ice_settings; |
| 103 | struct work_struct work; |
| 104 | struct request *cloned_request; |
| 105 | int error; |
| 106 | atomic_t pending; |
| 107 | struct timespec start_time; |
| 108 | bool should_encrypt; |
| 109 | bool should_decrypt; |
| 110 | u32 key_id; |
| 111 | }; |
| 112 | |
| 113 | struct req_dm_split_req_io { |
| 114 | struct work_struct work; |
| 115 | struct scatterlist *req_split_sg_read; |
| 116 | struct req_crypt_result result; |
| 117 | struct crypto_engine_entry *engine; |
| 118 | u8 IV[AES_XTS_IV_LEN]; |
| 119 | int size; |
| 120 | struct request *clone; |
| 121 | }; |
| 122 | |
| 123 | #ifdef CONFIG_FIPS_ENABLE |
| 124 | static struct qcrypto_func_set dm_qcrypto_func; |
| 125 | #else |
| 126 | static struct qcrypto_func_set dm_qcrypto_func = { |
| 127 | qcrypto_cipher_set_device_hw, |
| 128 | qcrypto_cipher_set_flag, |
| 129 | qcrypto_get_num_engines, |
| 130 | qcrypto_get_engine_list |
| 131 | }; |
| 132 | #endif |
| 133 | static void req_crypt_cipher_complete |
| 134 | (struct crypto_async_request *req, int err); |
| 135 | static void req_cryptd_split_req_queue_cb |
| 136 | (struct work_struct *work); |
| 137 | static void req_cryptd_split_req_queue |
| 138 | (struct req_dm_split_req_io *io); |
| 139 | static void req_crypt_split_io_complete |
| 140 | (struct req_crypt_result *res, int err); |
| 141 | |
| 142 | static bool req_crypt_should_encrypt(struct req_dm_crypt_io *req) |
| 143 | { |
| 144 | int ret = 0; |
| 145 | bool should_encrypt = false; |
| 146 | struct bio *bio = NULL; |
| 147 | bool is_encrypted = false; |
| 148 | bool is_inplace = false; |
| 149 | |
| 150 | if (!req || !req->cloned_request || !req->cloned_request->bio) |
| 151 | return false; |
| 152 | |
| 153 | if (encryption_mode == DM_REQ_CRYPT_ENCRYPTION_MODE_TRANSPARENT) |
| 154 | return false; |
| 155 | bio = req->cloned_request->bio; |
| 156 | |
| 157 | /* req->key_id = key_id; @todo support more than 1 pfe key */ |
| 158 | if ((ret == 0) && (is_encrypted || is_inplace)) { |
| 159 | should_encrypt = true; |
| 160 | req->key_id = PFE_KEY_ID; |
| 161 | } else if (is_fde_enabled) { |
| 162 | should_encrypt = true; |
| 163 | req->key_id = FDE_KEY_ID; |
| 164 | } |
| 165 | |
| 166 | return should_encrypt; |
| 167 | } |
| 168 | |
| 169 | static bool req_crypt_should_deccrypt(struct req_dm_crypt_io *req) |
| 170 | { |
| 171 | int ret = 0; |
| 172 | bool should_deccrypt = false; |
| 173 | struct bio *bio = NULL; |
| 174 | bool is_encrypted = false; |
| 175 | bool is_inplace = false; |
| 176 | |
| 177 | if (!req || !req->cloned_request || !req->cloned_request->bio) |
| 178 | return false; |
| 179 | if (encryption_mode == DM_REQ_CRYPT_ENCRYPTION_MODE_TRANSPARENT) |
| 180 | return false; |
| 181 | |
| 182 | bio = req->cloned_request->bio; |
| 183 | |
| 184 | /* req->key_id = key_id; @todo support more than 1 pfe key */ |
| 185 | if ((ret == 0) && (is_encrypted && !is_inplace)) { |
| 186 | should_deccrypt = true; |
| 187 | req->key_id = PFE_KEY_ID; |
| 188 | } else if (is_fde_enabled) { |
| 189 | should_deccrypt = true; |
| 190 | req->key_id = FDE_KEY_ID; |
| 191 | } |
| 192 | |
| 193 | return should_deccrypt; |
| 194 | } |
| 195 | |
| 196 | static void req_crypt_inc_pending(struct req_dm_crypt_io *io) |
| 197 | { |
| 198 | atomic_inc(&io->pending); |
| 199 | } |
| 200 | |
| 201 | static void req_crypt_dec_pending_encrypt(struct req_dm_crypt_io *io) |
| 202 | { |
| 203 | int error = 0; |
| 204 | struct request *clone = NULL; |
| 205 | |
| 206 | if (io) { |
| 207 | error = io->error; |
| 208 | if (io->cloned_request) { |
| 209 | clone = io->cloned_request; |
| 210 | } else { |
| 211 | DMERR("%s io->cloned_request is NULL\n", |
| 212 | __func__); |
| 213 | /* |
| 214 | * If Clone is NULL we cannot do anything, |
| 215 | * this should never happen |
| 216 | */ |
| 217 | WARN_ON(1); |
| 218 | } |
| 219 | } else { |
| 220 | DMERR("%s io is NULL\n", __func__); |
| 221 | /* |
| 222 | * If Clone is NULL we cannot do anything, |
| 223 | * this should never happen |
| 224 | */ |
| 225 | WARN_ON(1); |
| 226 | } |
| 227 | |
| 228 | atomic_dec(&io->pending); |
| 229 | |
| 230 | if (error < 0) { |
| 231 | dm_kill_unmapped_request(clone, error); |
| 232 | mempool_free(io, req_io_pool); |
| 233 | } else |
| 234 | dm_dispatch_request(clone); |
| 235 | } |
| 236 | |
| 237 | static void req_crypt_dec_pending_decrypt(struct req_dm_crypt_io *io) |
| 238 | { |
| 239 | int error = 0; |
| 240 | struct request *clone = NULL; |
| 241 | |
| 242 | if (io) { |
| 243 | error = io->error; |
| 244 | if (io->cloned_request) { |
| 245 | clone = io->cloned_request; |
| 246 | } else { |
| 247 | DMERR("%s io->cloned_request is NULL\n", |
| 248 | __func__); |
| 249 | /* |
| 250 | * If Clone is NULL we cannot do anything, |
| 251 | * this should never happen |
| 252 | */ |
| 253 | WARN_ON(1); |
| 254 | } |
| 255 | } else { |
| 256 | DMERR("%s io is NULL\n", |
| 257 | __func__); |
| 258 | /* |
| 259 | * If Clone is NULL we cannot do anything, |
| 260 | * this should never happen |
| 261 | */ |
| 262 | WARN_ON(1); |
| 263 | } |
| 264 | |
| 265 | /* Should never get here if io or Clone is NULL */ |
| 266 | dm_end_request(clone, error); |
| 267 | atomic_dec(&io->pending); |
| 268 | mempool_free(io, req_io_pool); |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * The callback that will be called by the worker queue to perform Decryption |
| 273 | * for reads and use the dm function to complete the bios and requests. |
| 274 | */ |
| 275 | static void req_cryptd_crypt_read_convert(struct req_dm_crypt_io *io) |
| 276 | { |
| 277 | struct request *clone = NULL; |
| 278 | int error = DM_REQ_CRYPT_ERROR; |
| 279 | int total_sg_len = 0, total_bytes_in_req = 0, temp_size = 0, i = 0; |
| 280 | struct scatterlist *sg = NULL; |
| 281 | struct scatterlist *req_sg_read = NULL; |
| 282 | |
| 283 | unsigned int engine_list_total = 0; |
| 284 | struct crypto_engine_entry *curr_engine_list = NULL; |
| 285 | bool split_transfers = 0; |
| 286 | sector_t tempiv; |
| 287 | struct req_dm_split_req_io *split_io = NULL; |
| 288 | |
| 289 | if (io) { |
| 290 | error = io->error; |
| 291 | if (io->cloned_request) { |
| 292 | clone = io->cloned_request; |
| 293 | } else { |
| 294 | DMERR("%s io->cloned_request is NULL\n", |
| 295 | __func__); |
| 296 | error = DM_REQ_CRYPT_ERROR; |
| 297 | goto submit_request; |
| 298 | } |
| 299 | } else { |
| 300 | DMERR("%s io is NULL\n", |
| 301 | __func__); |
| 302 | error = DM_REQ_CRYPT_ERROR; |
| 303 | goto submit_request; |
| 304 | } |
| 305 | |
| 306 | req_crypt_inc_pending(io); |
| 307 | |
| 308 | mutex_lock(&engine_list_mutex); |
| 309 | |
| 310 | engine_list_total = (io->key_id == FDE_KEY_ID ? num_engines_fde : |
| 311 | (io->key_id == PFE_KEY_ID ? |
| 312 | num_engines_pfe : 0)); |
| 313 | |
| 314 | curr_engine_list = (io->key_id == FDE_KEY_ID ? fde_eng : |
| 315 | (io->key_id == PFE_KEY_ID ? |
| 316 | pfe_eng : NULL)); |
| 317 | |
| 318 | mutex_unlock(&engine_list_mutex); |
| 319 | |
| 320 | req_sg_read = (struct scatterlist *)mempool_alloc(req_scatterlist_pool, |
| 321 | GFP_KERNEL); |
| 322 | if (!req_sg_read) { |
| 323 | DMERR("%s req_sg_read allocation failed\n", |
| 324 | __func__); |
| 325 | error = DM_REQ_CRYPT_ERROR; |
| 326 | goto ablkcipher_req_alloc_failure; |
| 327 | } |
| 328 | memset(req_sg_read, 0, sizeof(struct scatterlist) * MAX_SG_LIST); |
| 329 | |
| 330 | total_sg_len = blk_rq_map_sg_no_cluster(clone->q, clone, req_sg_read); |
| 331 | if ((total_sg_len <= 0) || (total_sg_len > MAX_SG_LIST)) { |
| 332 | DMERR("%s Request Error%d", __func__, total_sg_len); |
| 333 | error = DM_REQ_CRYPT_ERROR; |
| 334 | goto ablkcipher_req_alloc_failure; |
| 335 | } |
| 336 | |
| 337 | total_bytes_in_req = clone->__data_len; |
| 338 | if (total_bytes_in_req > REQ_DM_512_KB) { |
| 339 | DMERR("%s total_bytes_in_req > 512 MB %d", |
| 340 | __func__, total_bytes_in_req); |
| 341 | error = DM_REQ_CRYPT_ERROR; |
| 342 | goto ablkcipher_req_alloc_failure; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | if ((clone->__data_len >= (MIN_CRYPTO_TRANSFER_SIZE * |
| 347 | engine_list_total)) |
| 348 | && (engine_list_total > 1)) |
| 349 | split_transfers = 1; |
| 350 | |
| 351 | if (split_transfers) { |
| 352 | split_io = kzalloc(sizeof(struct req_dm_split_req_io) |
| 353 | * engine_list_total, GFP_KERNEL); |
| 354 | if (!split_io) { |
| 355 | DMERR("%s split_io allocation failed\n", __func__); |
| 356 | error = DM_REQ_CRYPT_ERROR; |
| 357 | goto ablkcipher_req_alloc_failure; |
| 358 | } |
| 359 | |
| 360 | split_io[0].req_split_sg_read = sg = req_sg_read; |
| 361 | split_io[engine_list_total - 1].size = total_bytes_in_req; |
| 362 | for (i = 0; i < (engine_list_total); i++) { |
| 363 | while ((sg) && i < (engine_list_total - 1)) { |
| 364 | split_io[i].size += sg->length; |
| 365 | split_io[engine_list_total - 1].size -= |
| 366 | sg->length; |
| 367 | if (split_io[i].size >= |
| 368 | (total_bytes_in_req / |
| 369 | engine_list_total)) { |
| 370 | split_io[i + 1].req_split_sg_read = |
| 371 | sg_next(sg); |
| 372 | sg_mark_end(sg); |
| 373 | break; |
| 374 | } |
| 375 | sg = sg_next(sg); |
| 376 | } |
| 377 | split_io[i].engine = &curr_engine_list[i]; |
| 378 | init_completion(&split_io[i].result.completion); |
| 379 | memset(&split_io[i].IV, 0, AES_XTS_IV_LEN); |
| 380 | tempiv = clone->__sector + (temp_size / SECTOR_SIZE); |
| 381 | memcpy(&split_io[i].IV, &tempiv, sizeof(sector_t)); |
| 382 | temp_size += split_io[i].size; |
| 383 | split_io[i].clone = clone; |
| 384 | req_cryptd_split_req_queue(&split_io[i]); |
| 385 | } |
| 386 | } else { |
| 387 | split_io = kzalloc(sizeof(struct req_dm_split_req_io), |
| 388 | GFP_KERNEL); |
| 389 | if (!split_io) { |
| 390 | DMERR("%s split_io allocation failed\n", __func__); |
| 391 | error = DM_REQ_CRYPT_ERROR; |
| 392 | goto ablkcipher_req_alloc_failure; |
| 393 | } |
| 394 | split_io->engine = &curr_engine_list[0]; |
| 395 | init_completion(&split_io->result.completion); |
| 396 | memcpy(split_io->IV, &clone->__sector, sizeof(sector_t)); |
| 397 | split_io->req_split_sg_read = req_sg_read; |
| 398 | split_io->size = total_bytes_in_req; |
| 399 | split_io->clone = clone; |
| 400 | req_cryptd_split_req_queue(split_io); |
| 401 | } |
| 402 | |
| 403 | if (!split_transfers) { |
| 404 | wait_for_completion_interruptible(&split_io->result.completion); |
| 405 | if (split_io->result.err) { |
| 406 | DMERR("%s error = %d for request\n", |
| 407 | __func__, split_io->result.err); |
| 408 | error = DM_REQ_CRYPT_ERROR; |
| 409 | goto ablkcipher_req_alloc_failure; |
| 410 | } |
| 411 | } else { |
| 412 | for (i = 0; i < (engine_list_total); i++) { |
| 413 | wait_for_completion_interruptible( |
| 414 | &split_io[i].result.completion); |
| 415 | if (split_io[i].result.err) { |
| 416 | DMERR("%s error = %d for %dst request\n", |
| 417 | __func__, split_io[i].result.err, i); |
| 418 | error = DM_REQ_CRYPT_ERROR; |
| 419 | goto ablkcipher_req_alloc_failure; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | error = 0; |
| 424 | ablkcipher_req_alloc_failure: |
| 425 | |
| 426 | mempool_free(req_sg_read, req_scatterlist_pool); |
| 427 | kfree(split_io); |
| 428 | submit_request: |
| 429 | if (io) |
| 430 | io->error = error; |
| 431 | req_crypt_dec_pending_decrypt(io); |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * This callback is called by the worker queue to perform non-decrypt reads |
| 436 | * and use the dm function to complete the bios and requests. |
| 437 | */ |
| 438 | static void req_cryptd_crypt_read_plain(struct req_dm_crypt_io *io) |
| 439 | { |
| 440 | struct request *clone = NULL; |
| 441 | int error = 0; |
| 442 | |
| 443 | if (!io || !io->cloned_request) { |
| 444 | DMERR("%s io is invalid\n", __func__); |
| 445 | WARN_ON(1); /* should not happen */ |
| 446 | } |
| 447 | |
| 448 | clone = io->cloned_request; |
| 449 | |
| 450 | dm_end_request(clone, error); |
| 451 | mempool_free(io, req_io_pool); |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * The callback that will be called by the worker queue to perform Encryption |
| 456 | * for writes and submit the request using the elevelator. |
| 457 | */ |
| 458 | static void req_cryptd_crypt_write_convert(struct req_dm_crypt_io *io) |
| 459 | { |
| 460 | struct request *clone = NULL; |
| 461 | struct bio *bio_src = NULL; |
| 462 | unsigned int total_sg_len_req_in = 0, total_sg_len_req_out = 0, |
| 463 | total_bytes_in_req = 0, error = DM_MAPIO_REMAPPED, rc = 0; |
| 464 | struct req_iterator iter; |
| 465 | struct req_iterator iter1; |
| 466 | struct ablkcipher_request *req = NULL; |
| 467 | struct req_crypt_result result; |
| 468 | struct bio_vec bvec; |
| 469 | struct scatterlist *req_sg_in = NULL; |
| 470 | struct scatterlist *req_sg_out = NULL; |
| 471 | int copy_bio_sector_to_req = 0; |
| 472 | gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM; |
| 473 | struct page *page = NULL; |
| 474 | u8 IV[AES_XTS_IV_LEN]; |
| 475 | int remaining_size = 0, err = 0; |
| 476 | struct crypto_engine_entry engine; |
| 477 | unsigned int engine_list_total = 0; |
| 478 | struct crypto_engine_entry *curr_engine_list = NULL; |
| 479 | unsigned int *engine_cursor = NULL; |
| 480 | |
| 481 | |
| 482 | if (io) { |
| 483 | if (io->cloned_request) { |
| 484 | clone = io->cloned_request; |
| 485 | } else { |
| 486 | DMERR("%s io->cloned_request is NULL\n", |
| 487 | __func__); |
| 488 | error = DM_REQ_CRYPT_ERROR; |
| 489 | goto submit_request; |
| 490 | } |
| 491 | } else { |
| 492 | DMERR("%s io is NULL\n", |
| 493 | __func__); |
| 494 | error = DM_REQ_CRYPT_ERROR; |
| 495 | goto submit_request; |
| 496 | } |
| 497 | |
| 498 | req_crypt_inc_pending(io); |
| 499 | |
| 500 | req = ablkcipher_request_alloc(tfm, GFP_KERNEL); |
| 501 | if (!req) { |
| 502 | DMERR("%s ablkcipher request allocation failed\n", |
| 503 | __func__); |
| 504 | error = DM_REQ_CRYPT_ERROR; |
| 505 | goto ablkcipher_req_alloc_failure; |
| 506 | } |
| 507 | |
| 508 | ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 509 | req_crypt_cipher_complete, &result); |
| 510 | |
| 511 | mutex_lock(&engine_list_mutex); |
| 512 | engine_list_total = (io->key_id == FDE_KEY_ID ? num_engines_fde : |
| 513 | (io->key_id == PFE_KEY_ID ? |
| 514 | num_engines_pfe : 0)); |
| 515 | |
| 516 | curr_engine_list = (io->key_id == FDE_KEY_ID ? fde_eng : |
| 517 | (io->key_id == PFE_KEY_ID ? |
| 518 | pfe_eng : NULL)); |
| 519 | |
| 520 | engine_cursor = (io->key_id == FDE_KEY_ID ? &fde_cursor : |
| 521 | (io->key_id == PFE_KEY_ID ? &pfe_cursor |
| 522 | : NULL)); |
| 523 | if ((engine_list_total < 1) || (curr_engine_list == NULL) || |
| 524 | (engine_cursor == NULL)) { |
| 525 | DMERR("%s Unknown Key ID!\n", __func__); |
| 526 | error = DM_REQ_CRYPT_ERROR; |
| 527 | mutex_unlock(&engine_list_mutex); |
| 528 | goto ablkcipher_req_alloc_failure; |
| 529 | } |
| 530 | |
| 531 | engine = curr_engine_list[*engine_cursor]; |
| 532 | (*engine_cursor)++; |
| 533 | (*engine_cursor) %= engine_list_total; |
| 534 | |
| 535 | err = (dm_qcrypto_func.cipher_set)(req, engine.ce_device, |
| 536 | engine.hw_instance); |
| 537 | if (err) { |
| 538 | DMERR("%s qcrypto_cipher_set_device_hw failed with err %d\n", |
| 539 | __func__, err); |
| 540 | mutex_unlock(&engine_list_mutex); |
| 541 | goto ablkcipher_req_alloc_failure; |
| 542 | } |
| 543 | mutex_unlock(&engine_list_mutex); |
| 544 | |
| 545 | init_completion(&result.completion); |
| 546 | |
| 547 | (dm_qcrypto_func.cipher_flag)(req, |
| 548 | QCRYPTO_CTX_USE_PIPE_KEY | QCRYPTO_CTX_XTS_DU_SIZE_512B); |
| 549 | crypto_ablkcipher_clear_flags(tfm, ~0); |
| 550 | crypto_ablkcipher_setkey(tfm, NULL, KEY_SIZE_XTS); |
| 551 | |
| 552 | req_sg_in = (struct scatterlist *)mempool_alloc(req_scatterlist_pool, |
| 553 | GFP_KERNEL); |
| 554 | if (!req_sg_in) { |
| 555 | DMERR("%s req_sg_in allocation failed\n", |
| 556 | __func__); |
| 557 | error = DM_REQ_CRYPT_ERROR; |
| 558 | goto ablkcipher_req_alloc_failure; |
| 559 | } |
| 560 | memset(req_sg_in, 0, sizeof(struct scatterlist) * MAX_SG_LIST); |
| 561 | |
| 562 | req_sg_out = (struct scatterlist *)mempool_alloc(req_scatterlist_pool, |
| 563 | GFP_KERNEL); |
| 564 | if (!req_sg_out) { |
| 565 | DMERR("%s req_sg_out allocation failed\n", |
| 566 | __func__); |
| 567 | error = DM_REQ_CRYPT_ERROR; |
| 568 | goto ablkcipher_req_alloc_failure; |
| 569 | } |
| 570 | memset(req_sg_out, 0, sizeof(struct scatterlist) * MAX_SG_LIST); |
| 571 | |
| 572 | total_sg_len_req_in = blk_rq_map_sg(clone->q, clone, req_sg_in); |
| 573 | if ((total_sg_len_req_in <= 0) || |
| 574 | (total_sg_len_req_in > MAX_SG_LIST)) { |
| 575 | DMERR("%s Request Error%d", __func__, total_sg_len_req_in); |
| 576 | error = DM_REQ_CRYPT_ERROR; |
| 577 | goto ablkcipher_req_alloc_failure; |
| 578 | } |
| 579 | |
| 580 | total_bytes_in_req = clone->__data_len; |
| 581 | if (total_bytes_in_req > REQ_DM_512_KB) { |
| 582 | DMERR("%s total_bytes_in_req > 512 MB %d", |
| 583 | __func__, total_bytes_in_req); |
| 584 | error = DM_REQ_CRYPT_ERROR; |
| 585 | goto ablkcipher_req_alloc_failure; |
| 586 | } |
| 587 | |
| 588 | rq_for_each_segment(bvec, clone, iter) { |
| 589 | if (bvec.bv_len > remaining_size) { |
| 590 | page = NULL; |
| 591 | while (page == NULL) { |
| 592 | page = mempool_alloc(req_page_pool, gfp_mask); |
| 593 | if (!page) { |
| 594 | DMERR("%s Crypt page alloc failed", |
| 595 | __func__); |
| 596 | congestion_wait(BLK_RW_ASYNC, HZ/100); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | bvec.bv_page = page; |
| 601 | bvec.bv_offset = 0; |
| 602 | remaining_size = PAGE_SIZE - bvec.bv_len; |
| 603 | if (remaining_size < 0) |
| 604 | WARN_ON(1); |
| 605 | } else { |
| 606 | bvec.bv_page = page; |
| 607 | bvec.bv_offset = PAGE_SIZE - remaining_size; |
| 608 | remaining_size = remaining_size - bvec.bv_len; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | total_sg_len_req_out = blk_rq_map_sg(clone->q, clone, req_sg_out); |
| 613 | if ((total_sg_len_req_out <= 0) || |
| 614 | (total_sg_len_req_out > MAX_SG_LIST)) { |
| 615 | DMERR("%s Request Error %d", __func__, total_sg_len_req_out); |
| 616 | error = DM_REQ_CRYPT_ERROR_AFTER_PAGE_MALLOC; |
| 617 | goto ablkcipher_req_alloc_failure; |
| 618 | } |
| 619 | |
| 620 | memset(IV, 0, AES_XTS_IV_LEN); |
| 621 | memcpy(IV, &clone->__sector, sizeof(sector_t)); |
| 622 | |
| 623 | ablkcipher_request_set_crypt(req, req_sg_in, req_sg_out, |
| 624 | total_bytes_in_req, (void *) IV); |
| 625 | |
| 626 | rc = crypto_ablkcipher_encrypt(req); |
| 627 | |
| 628 | switch (rc) { |
| 629 | case 0: |
| 630 | break; |
| 631 | |
| 632 | case -EBUSY: |
| 633 | /* |
| 634 | * Lets make this synchronous request by waiting on |
| 635 | * in progress as well |
| 636 | */ |
| 637 | case -EINPROGRESS: |
| 638 | wait_for_completion_interruptible(&result.completion); |
| 639 | if (result.err) { |
| 640 | DMERR("%s error = %d encrypting the request\n", |
| 641 | __func__, result.err); |
| 642 | error = DM_REQ_CRYPT_ERROR_AFTER_PAGE_MALLOC; |
| 643 | goto ablkcipher_req_alloc_failure; |
| 644 | } |
| 645 | break; |
| 646 | |
| 647 | default: |
| 648 | error = DM_REQ_CRYPT_ERROR_AFTER_PAGE_MALLOC; |
| 649 | goto ablkcipher_req_alloc_failure; |
| 650 | } |
| 651 | |
| 652 | __rq_for_each_bio(bio_src, clone) { |
| 653 | if (copy_bio_sector_to_req == 0) |
| 654 | copy_bio_sector_to_req++; |
| 655 | blk_queue_bounce(clone->q, &bio_src); |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Recalculate the phy_segments as we allocate new pages |
| 660 | * This is used by storage driver to fill the sg list. |
| 661 | */ |
| 662 | blk_recalc_rq_segments(clone); |
| 663 | |
| 664 | ablkcipher_req_alloc_failure: |
| 665 | if (req) |
| 666 | ablkcipher_request_free(req); |
| 667 | |
| 668 | if (error == DM_REQ_CRYPT_ERROR_AFTER_PAGE_MALLOC) { |
| 669 | rq_for_each_segment(bvec, clone, iter1) { |
| 670 | if (bvec.bv_offset == 0) { |
| 671 | mempool_free(bvec.bv_page, req_page_pool); |
| 672 | bvec.bv_page = NULL; |
| 673 | } else |
| 674 | bvec.bv_page = NULL; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | mempool_free(req_sg_in, req_scatterlist_pool); |
| 679 | mempool_free(req_sg_out, req_scatterlist_pool); |
| 680 | submit_request: |
| 681 | if (io) |
| 682 | io->error = error; |
| 683 | req_crypt_dec_pending_encrypt(io); |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * This callback is called by the worker queue to perform non-encrypted writes |
| 688 | * and submit the request using the elevelator. |
| 689 | */ |
| 690 | static void req_cryptd_crypt_write_plain(struct req_dm_crypt_io *io) |
| 691 | { |
| 692 | struct request *clone = NULL; |
| 693 | |
| 694 | if (!io || !io->cloned_request) { |
| 695 | DMERR("%s io is invalid\n", __func__); |
| 696 | WARN_ON(1); /* should not happen */ |
| 697 | } |
| 698 | |
| 699 | clone = io->cloned_request; |
| 700 | io->error = 0; |
| 701 | dm_dispatch_request(clone); |
| 702 | } |
| 703 | |
| 704 | /* Queue callback function that will get triggered */ |
| 705 | static void req_cryptd_crypt(struct work_struct *work) |
| 706 | { |
| 707 | struct req_dm_crypt_io *io = |
| 708 | container_of(work, struct req_dm_crypt_io, work); |
| 709 | |
| 710 | if (rq_data_dir(io->cloned_request) == WRITE) { |
| 711 | if (io->should_encrypt) |
| 712 | req_cryptd_crypt_write_convert(io); |
| 713 | else |
| 714 | req_cryptd_crypt_write_plain(io); |
| 715 | } else if (rq_data_dir(io->cloned_request) == READ) { |
| 716 | if (io->should_decrypt) |
| 717 | req_cryptd_crypt_read_convert(io); |
| 718 | else |
| 719 | req_cryptd_crypt_read_plain(io); |
| 720 | } else { |
| 721 | DMERR("%s received non-write request for Clone 0x%p\n", |
| 722 | __func__, io->cloned_request); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | static void req_cryptd_split_req_queue_cb(struct work_struct *work) |
| 727 | { |
| 728 | struct req_dm_split_req_io *io = |
| 729 | container_of(work, struct req_dm_split_req_io, work); |
| 730 | struct ablkcipher_request *req = NULL; |
| 731 | struct req_crypt_result result; |
| 732 | int err = 0; |
| 733 | struct crypto_engine_entry *engine = NULL; |
| 734 | |
| 735 | if ((!io) || (!io->req_split_sg_read) || (!io->engine)) { |
| 736 | DMERR("%s Input invalid\n", |
| 737 | __func__); |
| 738 | err = DM_REQ_CRYPT_ERROR; |
| 739 | /* If io is not populated this should not be called */ |
| 740 | WARN_ON(1); |
| 741 | } |
| 742 | req = ablkcipher_request_alloc(tfm, GFP_KERNEL); |
| 743 | if (!req) { |
| 744 | DMERR("%s ablkcipher request allocation failed\n", __func__); |
| 745 | err = DM_REQ_CRYPT_ERROR; |
| 746 | goto ablkcipher_req_alloc_failure; |
| 747 | } |
| 748 | |
| 749 | ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 750 | req_crypt_cipher_complete, &result); |
| 751 | |
| 752 | engine = io->engine; |
| 753 | |
| 754 | err = (dm_qcrypto_func.cipher_set)(req, engine->ce_device, |
| 755 | engine->hw_instance); |
| 756 | if (err) { |
| 757 | DMERR("%s qcrypto_cipher_set_device_hw failed with err %d\n", |
| 758 | __func__, err); |
| 759 | goto ablkcipher_req_alloc_failure; |
| 760 | } |
| 761 | init_completion(&result.completion); |
| 762 | (dm_qcrypto_func.cipher_flag)(req, |
| 763 | QCRYPTO_CTX_USE_PIPE_KEY | QCRYPTO_CTX_XTS_DU_SIZE_512B); |
| 764 | |
| 765 | crypto_ablkcipher_clear_flags(tfm, ~0); |
| 766 | crypto_ablkcipher_setkey(tfm, NULL, KEY_SIZE_XTS); |
| 767 | |
| 768 | ablkcipher_request_set_crypt(req, io->req_split_sg_read, |
| 769 | io->req_split_sg_read, io->size, (void *) io->IV); |
| 770 | |
| 771 | err = crypto_ablkcipher_decrypt(req); |
| 772 | switch (err) { |
| 773 | case 0: |
| 774 | break; |
| 775 | |
| 776 | case -EBUSY: |
| 777 | /* |
| 778 | * Lets make this synchronous request by waiting on |
| 779 | * in progress as well |
| 780 | */ |
| 781 | case -EINPROGRESS: |
| 782 | wait_for_completion_io(&result.completion); |
| 783 | if (result.err) { |
| 784 | DMERR("%s error = %d encrypting the request\n", |
| 785 | __func__, result.err); |
| 786 | err = DM_REQ_CRYPT_ERROR; |
| 787 | goto ablkcipher_req_alloc_failure; |
| 788 | } |
| 789 | break; |
| 790 | |
| 791 | default: |
| 792 | err = DM_REQ_CRYPT_ERROR; |
| 793 | goto ablkcipher_req_alloc_failure; |
| 794 | } |
| 795 | err = 0; |
| 796 | ablkcipher_req_alloc_failure: |
| 797 | if (req) |
| 798 | ablkcipher_request_free(req); |
| 799 | |
| 800 | req_crypt_split_io_complete(&io->result, err); |
| 801 | } |
| 802 | |
| 803 | static void req_cryptd_split_req_queue(struct req_dm_split_req_io *io) |
| 804 | { |
| 805 | INIT_WORK(&io->work, req_cryptd_split_req_queue_cb); |
| 806 | queue_work(req_crypt_split_io_queue, &io->work); |
| 807 | } |
| 808 | |
| 809 | static void req_cryptd_queue_crypt(struct req_dm_crypt_io *io) |
| 810 | { |
| 811 | INIT_WORK(&io->work, req_cryptd_crypt); |
| 812 | queue_work(req_crypt_queue, &io->work); |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * Cipher complete callback, this is triggered by the Linux crypto api once |
| 817 | * the operation is done. This signals the waiting thread that the crypto |
| 818 | * operation is complete. |
| 819 | */ |
| 820 | static void req_crypt_cipher_complete(struct crypto_async_request *req, int err) |
| 821 | { |
| 822 | struct req_crypt_result *res = req->data; |
| 823 | |
| 824 | if (err == -EINPROGRESS) |
| 825 | return; |
| 826 | |
| 827 | res->err = err; |
| 828 | complete(&res->completion); |
| 829 | } |
| 830 | |
| 831 | static void req_crypt_split_io_complete(struct req_crypt_result *res, int err) |
| 832 | { |
| 833 | if (err == -EINPROGRESS) |
| 834 | return; |
| 835 | |
| 836 | res->err = err; |
| 837 | complete(&res->completion); |
| 838 | } |
| 839 | /* |
| 840 | * If bio->bi_dev is a partition, remap the location |
| 841 | */ |
| 842 | static inline void req_crypt_blk_partition_remap(struct bio *bio) |
| 843 | { |
| 844 | struct block_device *bdev = bio->bi_bdev; |
| 845 | |
| 846 | if (bio_sectors(bio) && bdev != bdev->bd_contains) { |
| 847 | struct hd_struct *p = bdev->bd_part; |
| 848 | /* |
| 849 | * Check for integer overflow, should never happen. |
| 850 | */ |
| 851 | if (p->start_sect > (UINT_MAX - bio->bi_iter.bi_sector)) |
| 852 | WARN_ON(1); |
| 853 | |
| 854 | bio->bi_iter.bi_sector += p->start_sect; |
| 855 | bio->bi_bdev = bdev->bd_contains; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * The endio function is called from ksoftirqd context (atomic). |
| 861 | * For write operations the new pages created form the mempool |
| 862 | * is freed and returned. * For read operations, decryption is |
| 863 | * required, since this is called in a atomic * context, the |
| 864 | * request is sent to a worker queue to complete decryptiona and |
| 865 | * free the request once done. |
| 866 | */ |
| 867 | static int req_crypt_endio(struct dm_target *ti, struct request *clone, |
| 868 | int error, union map_info *map_context) |
| 869 | { |
| 870 | int err = 0; |
| 871 | struct req_iterator iter1; |
| 872 | struct bio_vec bvec; |
| 873 | struct req_dm_crypt_io *req_io = map_context->ptr; |
| 874 | |
| 875 | /* If it is for ICE, free up req_io and return */ |
| 876 | if (encryption_mode == DM_REQ_CRYPT_ENCRYPTION_MODE_TRANSPARENT) { |
| 877 | mempool_free(req_io, req_io_pool); |
| 878 | err = error; |
| 879 | goto submit_request; |
| 880 | } |
| 881 | |
| 882 | if (rq_data_dir(clone) == WRITE) { |
| 883 | rq_for_each_segment(bvec, clone, iter1) { |
| 884 | if (req_io->should_encrypt && bvec.bv_offset == 0) { |
| 885 | mempool_free(bvec.bv_page, req_page_pool); |
| 886 | bvec.bv_page = NULL; |
| 887 | } else |
| 888 | bvec.bv_page = NULL; |
| 889 | } |
| 890 | mempool_free(req_io, req_io_pool); |
| 891 | goto submit_request; |
| 892 | } else if (rq_data_dir(clone) == READ) { |
| 893 | req_io->error = error; |
| 894 | req_cryptd_queue_crypt(req_io); |
| 895 | err = DM_ENDIO_INCOMPLETE; |
| 896 | goto submit_request; |
| 897 | } |
| 898 | |
| 899 | submit_request: |
| 900 | return err; |
| 901 | } |
| 902 | |
| 903 | /* |
| 904 | * This function is called with interrupts disabled |
| 905 | * The function remaps the clone for the underlying device. |
| 906 | * If it is a write request, it calls into the worker queue to |
| 907 | * encrypt the data |
| 908 | * and submit the request directly using the elevator |
| 909 | * For a read request no pre-processing is required the request |
| 910 | * is returned to dm once mapping is done |
| 911 | */ |
| 912 | static int req_crypt_map(struct dm_target *ti, struct request *clone, |
| 913 | union map_info *map_context) |
| 914 | { |
| 915 | struct req_dm_crypt_io *req_io = NULL; |
| 916 | int error = DM_REQ_CRYPT_ERROR, copy_bio_sector_to_req = 0; |
| 917 | struct bio *bio_src = NULL; |
| 918 | gfp_t gfp_flag = GFP_KERNEL; |
| 919 | |
| 920 | if (in_interrupt() || irqs_disabled()) |
| 921 | gfp_flag = GFP_NOWAIT; |
| 922 | |
| 923 | req_io = mempool_alloc(req_io_pool, gfp_flag); |
| 924 | if (!req_io) { |
| 925 | WARN_ON(1); |
| 926 | error = DM_REQ_CRYPT_ERROR; |
| 927 | goto submit_request; |
| 928 | } |
| 929 | |
| 930 | /* Save the clone in the req_io, the callback to the worker |
| 931 | * queue will get the req_io |
| 932 | */ |
| 933 | req_io->cloned_request = clone; |
| 934 | map_context->ptr = req_io; |
| 935 | atomic_set(&req_io->pending, 0); |
| 936 | |
| 937 | if (rq_data_dir(clone) == WRITE) |
| 938 | req_io->should_encrypt = req_crypt_should_encrypt(req_io); |
| 939 | if (rq_data_dir(clone) == READ) |
| 940 | req_io->should_decrypt = req_crypt_should_deccrypt(req_io); |
| 941 | |
| 942 | /* Get the queue of the underlying original device */ |
| 943 | clone->q = bdev_get_queue(dev->bdev); |
| 944 | clone->rq_disk = dev->bdev->bd_disk; |
| 945 | |
| 946 | __rq_for_each_bio(bio_src, clone) { |
| 947 | bio_src->bi_bdev = dev->bdev; |
| 948 | /* Currently the way req-dm works is that once the underlying |
| 949 | * device driver completes the request by calling into the |
| 950 | * block layer. The block layer completes the bios (clones) and |
| 951 | * then the cloned request. This is undesirable for req-dm-crypt |
| 952 | * hence added a flag BIO_DONTFREE, this flag will ensure that |
| 953 | * blk layer does not complete the cloned bios before completing |
| 954 | * the request. When the crypt endio is called, post-processing |
| 955 | * is done and then the dm layer will complete the bios (clones) |
| 956 | * and free them. |
| 957 | */ |
| 958 | if (encryption_mode == DM_REQ_CRYPT_ENCRYPTION_MODE_TRANSPARENT) |
| 959 | bio_src->bi_flags |= 1 << BIO_INLINECRYPT; |
| 960 | else |
| 961 | bio_src->bi_flags |= 1 << BIO_DONTFREE; |
| 962 | |
| 963 | /* |
| 964 | * If this device has partitions, remap block n |
| 965 | * of partition p to block n+start(p) of the disk. |
| 966 | */ |
| 967 | req_crypt_blk_partition_remap(bio_src); |
| 968 | if (copy_bio_sector_to_req == 0) { |
| 969 | clone->__sector = bio_src->bi_iter.bi_sector; |
| 970 | copy_bio_sector_to_req++; |
| 971 | } |
| 972 | blk_queue_bounce(clone->q, &bio_src); |
| 973 | } |
| 974 | |
| 975 | if (encryption_mode == DM_REQ_CRYPT_ENCRYPTION_MODE_TRANSPARENT) { |
| 976 | /* Set all crypto parameters for inline crypto engine */ |
| 977 | memcpy(&req_io->ice_settings, ice_settings, |
| 978 | sizeof(struct ice_crypto_setting)); |
| 979 | } else { |
| 980 | /* ICE checks for key_index which could be >= 0. If a chip has |
| 981 | * both ICE and GPCE and wanted to use GPCE, there could be |
| 982 | * issue. Storage driver send all requests to ICE driver. If |
| 983 | * it sees key_index as 0, it would assume it is for ICE while |
| 984 | * it is not. Hence set invalid key index by default. |
| 985 | */ |
| 986 | req_io->ice_settings.key_index = -1; |
| 987 | |
| 988 | } |
| 989 | |
| 990 | if (rq_data_dir(clone) == READ || |
| 991 | encryption_mode == DM_REQ_CRYPT_ENCRYPTION_MODE_TRANSPARENT) { |
| 992 | error = DM_MAPIO_REMAPPED; |
| 993 | goto submit_request; |
| 994 | } else if (rq_data_dir(clone) == WRITE) { |
| 995 | req_cryptd_queue_crypt(req_io); |
| 996 | error = DM_MAPIO_SUBMITTED; |
| 997 | goto submit_request; |
| 998 | } |
| 999 | |
| 1000 | submit_request: |
| 1001 | return error; |
| 1002 | |
| 1003 | } |
| 1004 | |
| 1005 | static void deconfigure_qcrypto(void) |
| 1006 | { |
| 1007 | mempool_destroy(req_page_pool); |
| 1008 | req_page_pool = NULL; |
| 1009 | |
| 1010 | mempool_destroy(req_scatterlist_pool); |
| 1011 | req_scatterlist_pool = NULL; |
| 1012 | |
| 1013 | if (req_crypt_split_io_queue) { |
| 1014 | destroy_workqueue(req_crypt_split_io_queue); |
| 1015 | req_crypt_split_io_queue = NULL; |
| 1016 | } |
| 1017 | if (req_crypt_queue) { |
| 1018 | destroy_workqueue(req_crypt_queue); |
| 1019 | req_crypt_queue = NULL; |
| 1020 | } |
| 1021 | |
| 1022 | kmem_cache_destroy(_req_dm_scatterlist_pool); |
| 1023 | |
| 1024 | mutex_lock(&engine_list_mutex); |
| 1025 | kfree(pfe_eng); |
| 1026 | pfe_eng = NULL; |
| 1027 | kfree(fde_eng); |
| 1028 | fde_eng = NULL; |
| 1029 | mutex_unlock(&engine_list_mutex); |
| 1030 | |
| 1031 | if (tfm) { |
| 1032 | crypto_free_ablkcipher(tfm); |
| 1033 | tfm = NULL; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | static void req_crypt_dtr(struct dm_target *ti) |
| 1038 | { |
| 1039 | DMDEBUG("dm-req-crypt Destructor.\n"); |
| 1040 | |
| 1041 | mempool_destroy(req_io_pool); |
| 1042 | req_io_pool = NULL; |
| 1043 | |
| 1044 | if (encryption_mode == DM_REQ_CRYPT_ENCRYPTION_MODE_TRANSPARENT) { |
| 1045 | kfree(ice_settings); |
| 1046 | ice_settings = NULL; |
| 1047 | } else { |
| 1048 | deconfigure_qcrypto(); |
| 1049 | } |
| 1050 | |
| 1051 | kmem_cache_destroy(_req_crypt_io_pool); |
| 1052 | |
| 1053 | if (dev) { |
| 1054 | dm_put_device(ti, dev); |
| 1055 | dev = NULL; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | static int configure_qcrypto(void) |
| 1060 | { |
| 1061 | struct crypto_engine_entry *eng_list = NULL; |
| 1062 | struct block_device *bdev = NULL; |
| 1063 | int err = DM_REQ_CRYPT_ERROR, i; |
| 1064 | struct request_queue *q = NULL; |
| 1065 | |
| 1066 | bdev = dev->bdev; |
| 1067 | q = bdev_get_queue(bdev); |
| 1068 | blk_queue_max_hw_sectors(q, DM_REQ_CRYPT_QUEUE_SIZE); |
| 1069 | |
| 1070 | /* Allocate the crypto alloc blk cipher and keep the handle */ |
| 1071 | tfm = crypto_alloc_ablkcipher("qcom-xts(aes)", 0, 0); |
| 1072 | if (IS_ERR(tfm)) { |
| 1073 | DMERR("%s ablkcipher tfm allocation failed : error\n", |
| 1074 | __func__); |
| 1075 | tfm = NULL; |
| 1076 | goto exit_err; |
| 1077 | } |
| 1078 | |
| 1079 | num_engines_fde = num_engines_pfe = 0; |
| 1080 | |
| 1081 | mutex_lock(&engine_list_mutex); |
| 1082 | num_engines = (dm_qcrypto_func.get_num_engines)(); |
| 1083 | if (!num_engines) { |
| 1084 | DMERR(KERN_INFO "%s qcrypto_get_num_engines failed\n", |
| 1085 | __func__); |
| 1086 | err = DM_REQ_CRYPT_ERROR; |
| 1087 | mutex_unlock(&engine_list_mutex); |
| 1088 | goto exit_err; |
| 1089 | } |
| 1090 | |
| 1091 | eng_list = kcalloc(num_engines, sizeof(*eng_list), GFP_KERNEL); |
| 1092 | if (eng_list == NULL) { |
| 1093 | DMERR("%s engine list allocation failed\n", __func__); |
| 1094 | err = DM_REQ_CRYPT_ERROR; |
| 1095 | mutex_unlock(&engine_list_mutex); |
| 1096 | goto exit_err; |
| 1097 | } |
| 1098 | |
| 1099 | (dm_qcrypto_func.get_engine_list)(num_engines, eng_list); |
| 1100 | |
| 1101 | for (i = 0; i < num_engines; i++) { |
| 1102 | if (eng_list[i].ce_device == FDE_KEY_ID) |
| 1103 | num_engines_fde++; |
| 1104 | if (eng_list[i].ce_device == PFE_KEY_ID) |
| 1105 | num_engines_pfe++; |
| 1106 | } |
| 1107 | |
| 1108 | fde_eng = kcalloc(num_engines_fde, sizeof(*fde_eng), GFP_KERNEL); |
| 1109 | if (fde_eng == NULL) { |
| 1110 | DMERR("%s fde engine list allocation failed\n", __func__); |
| 1111 | mutex_unlock(&engine_list_mutex); |
| 1112 | goto exit_err; |
| 1113 | } |
| 1114 | |
| 1115 | pfe_eng = kcalloc(num_engines_pfe, sizeof(*pfe_eng), GFP_KERNEL); |
| 1116 | if (pfe_eng == NULL) { |
| 1117 | DMERR("%s pfe engine list allocation failed\n", __func__); |
| 1118 | mutex_unlock(&engine_list_mutex); |
| 1119 | goto exit_err; |
| 1120 | } |
| 1121 | |
| 1122 | fde_cursor = 0; |
| 1123 | pfe_cursor = 0; |
| 1124 | |
| 1125 | for (i = 0; i < num_engines; i++) { |
| 1126 | if (eng_list[i].ce_device == FDE_KEY_ID) |
| 1127 | fde_eng[fde_cursor++] = eng_list[i]; |
| 1128 | if (eng_list[i].ce_device == PFE_KEY_ID) |
| 1129 | pfe_eng[pfe_cursor++] = eng_list[i]; |
| 1130 | } |
| 1131 | |
| 1132 | fde_cursor = 0; |
| 1133 | pfe_cursor = 0; |
| 1134 | mutex_unlock(&engine_list_mutex); |
| 1135 | |
| 1136 | _req_dm_scatterlist_pool = kmem_cache_create("req_dm_scatterlist", |
| 1137 | sizeof(struct scatterlist) * MAX_SG_LIST, |
| 1138 | __alignof__(struct scatterlist), 0, NULL); |
| 1139 | if (!_req_dm_scatterlist_pool) |
| 1140 | goto exit_err; |
| 1141 | |
| 1142 | req_crypt_queue = alloc_workqueue("req_cryptd", |
| 1143 | WQ_UNBOUND | |
| 1144 | WQ_CPU_INTENSIVE | |
| 1145 | WQ_MEM_RECLAIM, |
| 1146 | 0); |
| 1147 | if (!req_crypt_queue) { |
| 1148 | DMERR("%s req_crypt_queue not allocated\n", __func__); |
| 1149 | goto exit_err; |
| 1150 | } |
| 1151 | |
| 1152 | req_crypt_split_io_queue = alloc_workqueue("req_crypt_split", |
| 1153 | WQ_UNBOUND | |
| 1154 | WQ_CPU_INTENSIVE | |
| 1155 | WQ_MEM_RECLAIM, |
| 1156 | 0); |
| 1157 | if (!req_crypt_split_io_queue) { |
| 1158 | DMERR("%s req_crypt_split_io_queue not allocated\n", __func__); |
| 1159 | goto exit_err; |
| 1160 | } |
| 1161 | req_scatterlist_pool = mempool_create_slab_pool(MIN_IOS, |
| 1162 | _req_dm_scatterlist_pool); |
| 1163 | if (!req_scatterlist_pool) { |
| 1164 | DMERR("%s req_scatterlist_pool is not allocated\n", __func__); |
| 1165 | err = -ENOMEM; |
| 1166 | goto exit_err; |
| 1167 | } |
| 1168 | |
| 1169 | req_page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0); |
| 1170 | if (!req_page_pool) { |
| 1171 | DMERR("%s req_page_pool not allocated\n", __func__); |
| 1172 | goto exit_err; |
| 1173 | } |
| 1174 | |
| 1175 | err = 0; |
| 1176 | |
| 1177 | exit_err: |
| 1178 | kfree(eng_list); |
| 1179 | return err; |
| 1180 | } |
| 1181 | |
| 1182 | /* |
| 1183 | * Construct an encryption mapping: |
| 1184 | * <cipher> <key> <iv_offset> <dev_path> <start> |
| 1185 | */ |
| 1186 | static int req_crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 1187 | { |
| 1188 | int err = DM_REQ_CRYPT_ERROR; |
| 1189 | unsigned long long tmpll; |
| 1190 | char dummy; |
| 1191 | int ret; |
| 1192 | |
| 1193 | DMDEBUG("dm-req-crypt Constructor.\n"); |
| 1194 | |
| 1195 | if (argc < 5) { |
| 1196 | DMERR(" %s Not enough args\n", __func__); |
| 1197 | err = DM_REQ_CRYPT_ERROR; |
| 1198 | goto ctr_exit; |
| 1199 | } |
| 1200 | |
| 1201 | if (argv[3]) { |
| 1202 | if (dm_get_device(ti, argv[3], |
| 1203 | dm_table_get_mode(ti->table), &dev)) { |
| 1204 | DMERR(" %s Device Lookup failed\n", __func__); |
| 1205 | err = DM_REQ_CRYPT_ERROR; |
| 1206 | goto ctr_exit; |
| 1207 | } |
| 1208 | } else { |
| 1209 | DMERR(" %s Arg[3] invalid\n", __func__); |
| 1210 | err = DM_REQ_CRYPT_ERROR; |
| 1211 | goto ctr_exit; |
| 1212 | } |
| 1213 | |
| 1214 | if (argv[4]) { |
| 1215 | if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) { |
| 1216 | DMERR("%s Invalid device sector\n", __func__); |
| 1217 | err = DM_REQ_CRYPT_ERROR; |
| 1218 | goto ctr_exit; |
| 1219 | } |
| 1220 | } else { |
| 1221 | DMERR(" %s Arg[4] invalid\n", __func__); |
| 1222 | err = DM_REQ_CRYPT_ERROR; |
| 1223 | goto ctr_exit; |
| 1224 | } |
| 1225 | start_sector_orig = tmpll; |
| 1226 | |
| 1227 | /* Allow backward compatible */ |
| 1228 | if (argc >= 6) { |
| 1229 | if (argv[5]) { |
| 1230 | if (!strcmp(argv[5], "fde_enabled")) |
| 1231 | is_fde_enabled = true; |
| 1232 | else |
| 1233 | is_fde_enabled = false; |
| 1234 | } else { |
| 1235 | DMERR(" %s Arg[5] invalid\n", __func__); |
| 1236 | err = DM_REQ_CRYPT_ERROR; |
| 1237 | goto ctr_exit; |
| 1238 | } |
| 1239 | } else { |
| 1240 | DMERR(" %s Arg[5] missing, set FDE enabled.\n", __func__); |
| 1241 | is_fde_enabled = true; /* backward compatible */ |
| 1242 | } |
| 1243 | |
| 1244 | _req_crypt_io_pool = KMEM_CACHE(req_dm_crypt_io, 0); |
| 1245 | if (!_req_crypt_io_pool) { |
| 1246 | err = DM_REQ_CRYPT_ERROR; |
| 1247 | goto ctr_exit; |
| 1248 | } |
| 1249 | |
| 1250 | encryption_mode = DM_REQ_CRYPT_ENCRYPTION_MODE_CRYPTO; |
| 1251 | if (argc >= 7 && argv[6]) { |
| 1252 | if (!strcmp(argv[6], "ice")) |
| 1253 | encryption_mode = |
| 1254 | DM_REQ_CRYPT_ENCRYPTION_MODE_TRANSPARENT; |
| 1255 | } |
| 1256 | |
| 1257 | if (encryption_mode == DM_REQ_CRYPT_ENCRYPTION_MODE_TRANSPARENT) { |
| 1258 | /* configure ICE settings */ |
| 1259 | ice_settings = |
| 1260 | kzalloc(sizeof(struct ice_crypto_setting), GFP_KERNEL); |
| 1261 | if (!ice_settings) { |
| 1262 | err = -ENOMEM; |
| 1263 | goto ctr_exit; |
| 1264 | } |
| 1265 | ice_settings->key_size = ICE_CRYPTO_KEY_SIZE_128; |
| 1266 | ice_settings->algo_mode = ICE_CRYPTO_ALGO_MODE_AES_XTS; |
| 1267 | ice_settings->key_mode = ICE_CRYPTO_USE_LUT_SW_KEY; |
| 1268 | if (kstrtou16(argv[1], 0, &ice_settings->key_index) || |
| 1269 | ice_settings->key_index < 0 || |
| 1270 | ice_settings->key_index > MAX_MSM_ICE_KEY_LUT_SIZE) { |
| 1271 | DMERR("%s Err: key index %d received for ICE\n", |
| 1272 | __func__, ice_settings->key_index); |
| 1273 | err = DM_REQ_CRYPT_ERROR; |
| 1274 | goto ctr_exit; |
| 1275 | } |
| 1276 | } else { |
| 1277 | ret = configure_qcrypto(); |
| 1278 | if (ret) { |
| 1279 | DMERR("%s failed to configure qcrypto\n", __func__); |
| 1280 | err = ret; |
| 1281 | goto ctr_exit; |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | req_io_pool = mempool_create_slab_pool(MIN_IOS, _req_crypt_io_pool); |
| 1286 | if (!req_io_pool) { |
| 1287 | DMERR("%s req_io_pool not allocated\n", __func__); |
| 1288 | err = -ENOMEM; |
| 1289 | goto ctr_exit; |
| 1290 | } |
| 1291 | |
| 1292 | /* |
| 1293 | * If underlying device supports flush/discard, mapped target |
| 1294 | * should also allow it |
| 1295 | */ |
| 1296 | ti->num_flush_bios = 1; |
| 1297 | ti->num_discard_bios = 1; |
| 1298 | |
| 1299 | err = 0; |
| 1300 | DMINFO("%s: Mapping block_device %s to dm-req-crypt ok!\n", |
| 1301 | __func__, argv[3]); |
| 1302 | ctr_exit: |
| 1303 | if (err) |
| 1304 | req_crypt_dtr(ti); |
| 1305 | |
| 1306 | return err; |
| 1307 | } |
| 1308 | |
| 1309 | static int req_crypt_iterate_devices(struct dm_target *ti, |
| 1310 | iterate_devices_callout_fn fn, void *data) |
| 1311 | { |
| 1312 | return fn(ti, dev, start_sector_orig, ti->len, data); |
| 1313 | } |
| 1314 | void set_qcrypto_func_dm(void *dev, |
| 1315 | void *flag, |
| 1316 | void *engines, |
| 1317 | void *engine_list) |
| 1318 | { |
| 1319 | dm_qcrypto_func.cipher_set = dev; |
| 1320 | dm_qcrypto_func.cipher_flag = flag; |
| 1321 | dm_qcrypto_func.get_num_engines = engines; |
| 1322 | dm_qcrypto_func.get_engine_list = engine_list; |
| 1323 | } |
| 1324 | EXPORT_SYMBOL(set_qcrypto_func_dm); |
| 1325 | |
| 1326 | static struct target_type req_crypt_target = { |
| 1327 | .name = "req-crypt", |
| 1328 | .version = {1, 0, 0}, |
| 1329 | .module = THIS_MODULE, |
| 1330 | .ctr = req_crypt_ctr, |
| 1331 | .dtr = req_crypt_dtr, |
| 1332 | .map_rq = req_crypt_map, |
| 1333 | .rq_end_io = req_crypt_endio, |
| 1334 | .iterate_devices = req_crypt_iterate_devices, |
| 1335 | }; |
| 1336 | |
| 1337 | static int __init req_dm_crypt_init(void) |
| 1338 | { |
| 1339 | int r; |
| 1340 | |
| 1341 | |
| 1342 | r = dm_register_target(&req_crypt_target); |
| 1343 | if (r < 0) { |
| 1344 | DMERR("register failed %d", r); |
| 1345 | return r; |
| 1346 | } |
| 1347 | |
| 1348 | DMINFO("dm-req-crypt successfully initalized.\n"); |
| 1349 | |
| 1350 | return r; |
| 1351 | } |
| 1352 | |
| 1353 | static void __exit req_dm_crypt_exit(void) |
| 1354 | { |
| 1355 | dm_unregister_target(&req_crypt_target); |
| 1356 | } |
| 1357 | |
| 1358 | module_init(req_dm_crypt_init); |
| 1359 | module_exit(req_dm_crypt_exit); |
| 1360 | |
| 1361 | MODULE_DESCRIPTION(DM_NAME " target for request based transparent encryption / decryption"); |
| 1362 | MODULE_LICENSE("GPL v2"); |