Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Qualcomm CE device driver. |
| 2 | * |
| 3 | * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 and |
| 7 | * only version 2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | #include <linux/mman.h> |
| 15 | #include <linux/android_pmem.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/dma-mapping.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/dmapool.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/fs.h> |
| 26 | #include <linux/miscdevice.h> |
| 27 | #include <linux/uaccess.h> |
| 28 | #include <linux/debugfs.h> |
| 29 | #include <linux/scatterlist.h> |
| 30 | #include <linux/crypto.h> |
| 31 | #include <crypto/hash.h> |
| 32 | #include <linux/platform_data/qcom_crypto_device.h> |
| 33 | #include <mach/scm.h> |
Mona Hossain | 5c8ea1f | 2011-07-28 15:11:29 -0700 | [diff] [blame] | 34 | #include <linux/qcedev.h> |
| 35 | #include "qce.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 36 | |
| 37 | |
| 38 | #define CACHE_LINE_SIZE 32 |
| 39 | #define CE_SHA_BLOCK_SIZE SHA256_BLOCK_SIZE |
| 40 | |
| 41 | static uint8_t _std_init_vector_sha1_uint8[] = { |
| 42 | 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB, 0x89, |
| 43 | 0x98, 0xBA, 0xDC, 0xFE, 0x10, 0x32, 0x54, 0x76, |
| 44 | 0xC3, 0xD2, 0xE1, 0xF0 |
| 45 | }; |
| 46 | /* standard initialization vector for SHA-256, source: FIPS 180-2 */ |
| 47 | static uint8_t _std_init_vector_sha256_uint8[] = { |
| 48 | 0x6A, 0x09, 0xE6, 0x67, 0xBB, 0x67, 0xAE, 0x85, |
| 49 | 0x3C, 0x6E, 0xF3, 0x72, 0xA5, 0x4F, 0xF5, 0x3A, |
| 50 | 0x51, 0x0E, 0x52, 0x7F, 0x9B, 0x05, 0x68, 0x8C, |
| 51 | 0x1F, 0x83, 0xD9, 0xAB, 0x5B, 0xE0, 0xCD, 0x19 |
| 52 | }; |
| 53 | |
| 54 | enum qcedev_crypto_oper_type { |
| 55 | QCEDEV_CRYPTO_OPER_CIPHER = 0, |
| 56 | QCEDEV_CRYPTO_OPER_SHA = 1, |
| 57 | QCEDEV_CRYPTO_OPER_LAST |
| 58 | }; |
| 59 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 60 | struct qcedev_handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 61 | |
| 62 | struct qcedev_cipher_req { |
| 63 | struct ablkcipher_request creq; |
| 64 | void *cookie; |
| 65 | }; |
| 66 | |
| 67 | struct qcedev_sha_req { |
| 68 | struct ahash_request sreq; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 69 | void *cookie; |
| 70 | }; |
| 71 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 72 | struct qcedev_sha_ctxt { |
| 73 | uint32_t auth_data[4]; |
| 74 | uint8_t digest[QCEDEV_MAX_SHA_DIGEST]; |
| 75 | uint32_t diglen; |
| 76 | uint8_t trailing_buf[64]; |
| 77 | uint32_t trailing_buf_len; |
| 78 | uint8_t first_blk; |
| 79 | uint8_t last_blk; |
| 80 | uint8_t authkey[QCEDEV_MAX_SHA_BLOCK_SIZE]; |
| 81 | }; |
| 82 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 83 | struct qcedev_async_req { |
| 84 | struct list_head list; |
| 85 | struct completion complete; |
| 86 | enum qcedev_crypto_oper_type op_type; |
| 87 | union { |
| 88 | struct qcedev_cipher_op_req cipher_op_req; |
| 89 | struct qcedev_sha_op_req sha_op_req; |
| 90 | }; |
| 91 | union{ |
| 92 | struct qcedev_cipher_req cipher_req; |
| 93 | struct qcedev_sha_req sha_req; |
| 94 | }; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 95 | struct qcedev_handle *handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 96 | int err; |
| 97 | }; |
| 98 | |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 99 | static DEFINE_MUTEX(send_cmd_lock); |
| 100 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 101 | /********************************************************************** |
| 102 | * Register ourselves as a misc device to be able to access the dev driver |
| 103 | * from userspace. */ |
| 104 | |
| 105 | |
| 106 | #define QCEDEV_DEV "qcedev" |
| 107 | |
| 108 | struct qcedev_control{ |
| 109 | |
| 110 | /* CE features supported by platform */ |
| 111 | struct msm_ce_hw_support platform_support; |
| 112 | |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 113 | uint32_t ce_lock_count; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 114 | /* CE features/algorithms supported by HW engine*/ |
| 115 | struct ce_hw_support ce_support; |
| 116 | |
| 117 | /* misc device */ |
| 118 | struct miscdevice miscdevice; |
| 119 | |
| 120 | /* qce handle */ |
| 121 | void *qce; |
| 122 | |
| 123 | /* platform device */ |
| 124 | struct platform_device *pdev; |
| 125 | |
| 126 | unsigned magic; |
| 127 | |
| 128 | struct list_head ready_commands; |
| 129 | struct qcedev_async_req *active_command; |
| 130 | spinlock_t lock; |
| 131 | struct tasklet_struct done_tasklet; |
| 132 | }; |
| 133 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 134 | struct qcedev_handle { |
| 135 | /* qcedev control handle */ |
| 136 | struct qcedev_control *cntl; |
| 137 | /* qce internal sha context*/ |
| 138 | struct qcedev_sha_ctxt sha_ctxt; |
| 139 | }; |
| 140 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 141 | /*------------------------------------------------------------------------- |
| 142 | * Resource Locking Service |
| 143 | * ------------------------------------------------------------------------*/ |
| 144 | #define QCEDEV_CMD_ID 1 |
| 145 | #define QCEDEV_CE_LOCK_CMD 1 |
| 146 | #define QCEDEV_CE_UNLOCK_CMD 0 |
| 147 | #define NUM_RETRY 1000 |
| 148 | #define CE_BUSY 55 |
| 149 | |
| 150 | static int qcedev_scm_cmd(int resource, int cmd, int *response) |
| 151 | { |
| 152 | #ifdef CONFIG_MSM_SCM |
| 153 | |
| 154 | struct { |
| 155 | int resource; |
| 156 | int cmd; |
| 157 | } cmd_buf; |
| 158 | |
| 159 | cmd_buf.resource = resource; |
| 160 | cmd_buf.cmd = cmd; |
| 161 | |
| 162 | return scm_call(SCM_SVC_TZ, QCEDEV_CMD_ID, &cmd_buf, |
| 163 | sizeof(cmd_buf), response, sizeof(*response)); |
| 164 | |
| 165 | #else |
| 166 | return 0; |
| 167 | #endif |
| 168 | } |
| 169 | |
| 170 | static int qcedev_unlock_ce(struct qcedev_control *podev) |
| 171 | { |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 172 | int ret = 0; |
| 173 | |
| 174 | mutex_lock(&send_cmd_lock); |
| 175 | if (podev->ce_lock_count == 1) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 176 | int response = 0; |
| 177 | |
| 178 | if (qcedev_scm_cmd(podev->platform_support.shared_ce_resource, |
| 179 | QCEDEV_CE_UNLOCK_CMD, &response)) { |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 180 | pr_err("Failed to release CE lock\n"); |
| 181 | ret = -EIO; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 182 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 183 | } |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 184 | if (ret == 0) { |
| 185 | if (podev->ce_lock_count) |
| 186 | podev->ce_lock_count--; |
| 187 | else { |
| 188 | /* We should never be here */ |
| 189 | ret = -EIO; |
| 190 | pr_err("CE hardware is already unlocked\n"); |
| 191 | } |
| 192 | } |
| 193 | mutex_unlock(&send_cmd_lock); |
| 194 | |
| 195 | return ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static int qcedev_lock_ce(struct qcedev_control *podev) |
| 199 | { |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 200 | int ret = 0; |
| 201 | |
| 202 | mutex_lock(&send_cmd_lock); |
| 203 | if (podev->ce_lock_count == 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 204 | int response = -CE_BUSY; |
| 205 | int i = 0; |
| 206 | |
| 207 | do { |
| 208 | if (qcedev_scm_cmd( |
| 209 | podev->platform_support.shared_ce_resource, |
| 210 | QCEDEV_CE_LOCK_CMD, &response)) { |
| 211 | response = -EINVAL; |
| 212 | break; |
| 213 | } |
| 214 | } while ((response == -CE_BUSY) && (i++ < NUM_RETRY)); |
| 215 | |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 216 | if ((response == -CE_BUSY) && (i >= NUM_RETRY)) { |
| 217 | ret = -EUSERS; |
| 218 | } else { |
| 219 | if (response < 0) |
| 220 | ret = -EINVAL; |
| 221 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 222 | } |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 223 | if (ret == 0) |
| 224 | podev->ce_lock_count++; |
| 225 | mutex_unlock(&send_cmd_lock); |
| 226 | return ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | #define QCEDEV_MAGIC 0x56434544 /* "qced" */ |
| 230 | |
| 231 | static long qcedev_ioctl(struct file *file, unsigned cmd, unsigned long arg); |
| 232 | static int qcedev_open(struct inode *inode, struct file *file); |
| 233 | static int qcedev_release(struct inode *inode, struct file *file); |
| 234 | static int start_cipher_req(struct qcedev_control *podev); |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 235 | static int start_sha_req(struct qcedev_control *podev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 236 | |
| 237 | static const struct file_operations qcedev_fops = { |
| 238 | .owner = THIS_MODULE, |
| 239 | .unlocked_ioctl = qcedev_ioctl, |
| 240 | .open = qcedev_open, |
| 241 | .release = qcedev_release, |
| 242 | }; |
| 243 | |
| 244 | static struct qcedev_control qce_dev[] = { |
| 245 | { |
| 246 | .miscdevice = { |
| 247 | .minor = MISC_DYNAMIC_MINOR, |
| 248 | .name = "qce", |
| 249 | .fops = &qcedev_fops, |
| 250 | }, |
| 251 | .magic = QCEDEV_MAGIC, |
| 252 | }, |
| 253 | }; |
| 254 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 255 | #define MAX_QCE_DEVICE ARRAY_SIZE(qce_dev) |
| 256 | #define DEBUG_MAX_FNAME 16 |
| 257 | #define DEBUG_MAX_RW_BUF 1024 |
| 258 | |
| 259 | struct qcedev_stat { |
| 260 | u32 qcedev_dec_success; |
| 261 | u32 qcedev_dec_fail; |
| 262 | u32 qcedev_enc_success; |
| 263 | u32 qcedev_enc_fail; |
| 264 | u32 qcedev_sha_success; |
| 265 | u32 qcedev_sha_fail; |
| 266 | }; |
| 267 | |
| 268 | static struct qcedev_stat _qcedev_stat[MAX_QCE_DEVICE]; |
| 269 | static struct dentry *_debug_dent; |
| 270 | static char _debug_read_buf[DEBUG_MAX_RW_BUF]; |
| 271 | static int _debug_qcedev[MAX_QCE_DEVICE]; |
| 272 | |
| 273 | static struct qcedev_control *qcedev_minor_to_control(unsigned n) |
| 274 | { |
| 275 | int i; |
| 276 | |
| 277 | for (i = 0; i < MAX_QCE_DEVICE; i++) { |
| 278 | if (qce_dev[i].miscdevice.minor == n) |
| 279 | return &qce_dev[i]; |
| 280 | } |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | static int qcedev_open(struct inode *inode, struct file *file) |
| 285 | { |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 286 | struct qcedev_handle *handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 287 | struct qcedev_control *podev; |
| 288 | |
| 289 | podev = qcedev_minor_to_control(MINOR(inode->i_rdev)); |
| 290 | if (podev == NULL) { |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 291 | pr_err("%s: no such device %d\n", __func__, |
| 292 | MINOR(inode->i_rdev)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 293 | return -ENOENT; |
| 294 | } |
| 295 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 296 | handle = kzalloc(sizeof(struct qcedev_handle), GFP_KERNEL); |
| 297 | if (handle == NULL) { |
| 298 | pr_err("Failed to allocate memory %ld\n", |
| 299 | PTR_ERR(handle)); |
| 300 | return -ENOMEM; |
| 301 | } |
| 302 | |
| 303 | handle->cntl = podev; |
| 304 | file->private_data = handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static int qcedev_release(struct inode *inode, struct file *file) |
| 310 | { |
| 311 | struct qcedev_control *podev; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 312 | struct qcedev_handle *handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 313 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 314 | handle = file->private_data; |
| 315 | podev = handle->cntl; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 316 | if (podev != NULL && podev->magic != QCEDEV_MAGIC) { |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 317 | pr_err("%s: invalid handle %p\n", |
| 318 | __func__, podev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 319 | } |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 320 | kzfree(handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 321 | file->private_data = NULL; |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static void req_done(unsigned long data) |
| 327 | { |
| 328 | struct qcedev_control *podev = (struct qcedev_control *)data; |
| 329 | struct qcedev_async_req *areq; |
| 330 | unsigned long flags = 0; |
| 331 | struct qcedev_async_req *new_req = NULL; |
| 332 | int ret = 0; |
| 333 | |
| 334 | spin_lock_irqsave(&podev->lock, flags); |
| 335 | areq = podev->active_command; |
| 336 | podev->active_command = NULL; |
| 337 | |
| 338 | again: |
| 339 | if (!list_empty(&podev->ready_commands)) { |
| 340 | new_req = container_of(podev->ready_commands.next, |
| 341 | struct qcedev_async_req, list); |
| 342 | list_del(&new_req->list); |
| 343 | podev->active_command = new_req; |
| 344 | new_req->err = 0; |
| 345 | if (new_req->op_type == QCEDEV_CRYPTO_OPER_CIPHER) |
| 346 | ret = start_cipher_req(podev); |
| 347 | else |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 348 | ret = start_sha_req(podev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | spin_unlock_irqrestore(&podev->lock, flags); |
| 352 | |
| 353 | if (areq) |
| 354 | complete(&areq->complete); |
| 355 | |
| 356 | if (new_req && ret) { |
| 357 | complete(&new_req->complete); |
| 358 | spin_lock_irqsave(&podev->lock, flags); |
| 359 | podev->active_command = NULL; |
| 360 | areq = NULL; |
| 361 | ret = 0; |
| 362 | new_req = NULL; |
| 363 | goto again; |
| 364 | } |
| 365 | |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | static void qcedev_sha_req_cb(void *cookie, unsigned char *digest, |
| 370 | unsigned char *authdata, int ret) |
| 371 | { |
| 372 | struct qcedev_sha_req *areq; |
| 373 | struct qcedev_control *pdev; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 374 | struct qcedev_handle *handle; |
| 375 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 376 | uint32_t *auth32 = (uint32_t *)authdata; |
| 377 | |
| 378 | areq = (struct qcedev_sha_req *) cookie; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 379 | handle = (struct qcedev_handle *) areq->cookie; |
| 380 | pdev = handle->cntl; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 381 | |
| 382 | if (digest) |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 383 | memcpy(&handle->sha_ctxt.digest[0], digest, 32); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 384 | |
| 385 | if (authdata) { |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 386 | handle->sha_ctxt.auth_data[0] = auth32[0]; |
| 387 | handle->sha_ctxt.auth_data[1] = auth32[1]; |
| 388 | handle->sha_ctxt.auth_data[2] = auth32[2]; |
| 389 | handle->sha_ctxt.auth_data[3] = auth32[3]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | tasklet_schedule(&pdev->done_tasklet); |
| 393 | }; |
| 394 | |
| 395 | |
| 396 | static void qcedev_cipher_req_cb(void *cookie, unsigned char *icv, |
| 397 | unsigned char *iv, int ret) |
| 398 | { |
| 399 | struct qcedev_cipher_req *areq; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 400 | struct qcedev_handle *handle; |
| 401 | struct qcedev_control *podev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 402 | struct qcedev_async_req *qcedev_areq; |
| 403 | |
| 404 | areq = (struct qcedev_cipher_req *) cookie; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 405 | handle = (struct qcedev_handle *) areq->cookie; |
| 406 | podev = handle->cntl; |
| 407 | qcedev_areq = podev->active_command; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 408 | |
| 409 | if (iv) |
| 410 | memcpy(&qcedev_areq->cipher_op_req.iv[0], iv, |
| 411 | qcedev_areq->cipher_op_req.ivlen); |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 412 | tasklet_schedule(&podev->done_tasklet); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 413 | }; |
| 414 | |
| 415 | static int start_cipher_req(struct qcedev_control *podev) |
| 416 | { |
| 417 | struct qcedev_async_req *qcedev_areq; |
| 418 | struct qce_req creq; |
| 419 | int ret = 0; |
| 420 | |
| 421 | /* start the command on the podev->active_command */ |
| 422 | qcedev_areq = podev->active_command; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 423 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 424 | qcedev_areq->cipher_req.cookie = qcedev_areq->handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 425 | creq.use_pmem = qcedev_areq->cipher_op_req.use_pmem; |
| 426 | if (qcedev_areq->cipher_op_req.use_pmem == QCEDEV_USE_PMEM) |
| 427 | creq.pmem = &qcedev_areq->cipher_op_req.pmem; |
| 428 | else |
| 429 | creq.pmem = NULL; |
| 430 | |
| 431 | switch (qcedev_areq->cipher_op_req.alg) { |
| 432 | case QCEDEV_ALG_DES: |
| 433 | creq.alg = CIPHER_ALG_DES; |
| 434 | break; |
| 435 | case QCEDEV_ALG_3DES: |
| 436 | creq.alg = CIPHER_ALG_3DES; |
| 437 | break; |
| 438 | case QCEDEV_ALG_AES: |
| 439 | creq.alg = CIPHER_ALG_AES; |
| 440 | break; |
| 441 | default: |
| 442 | break; |
| 443 | }; |
| 444 | |
| 445 | switch (qcedev_areq->cipher_op_req.mode) { |
| 446 | case QCEDEV_AES_MODE_CBC: |
| 447 | case QCEDEV_DES_MODE_CBC: |
| 448 | creq.mode = QCE_MODE_CBC; |
| 449 | break; |
| 450 | case QCEDEV_AES_MODE_ECB: |
| 451 | case QCEDEV_DES_MODE_ECB: |
| 452 | creq.mode = QCE_MODE_ECB; |
| 453 | break; |
| 454 | case QCEDEV_AES_MODE_CTR: |
| 455 | creq.mode = QCE_MODE_CTR; |
| 456 | break; |
| 457 | case QCEDEV_AES_MODE_XTS: |
| 458 | creq.mode = QCE_MODE_XTS; |
| 459 | break; |
| 460 | default: |
| 461 | break; |
| 462 | }; |
| 463 | |
| 464 | if ((creq.alg == CIPHER_ALG_AES) && |
| 465 | (creq.mode == QCE_MODE_CTR)) { |
| 466 | creq.dir = QCE_ENCRYPT; |
| 467 | } else { |
| 468 | if (QCEDEV_OPER_ENC == qcedev_areq->cipher_op_req.op) |
| 469 | creq.dir = QCE_ENCRYPT; |
| 470 | else |
| 471 | creq.dir = QCE_DECRYPT; |
| 472 | } |
| 473 | |
| 474 | creq.iv = &qcedev_areq->cipher_op_req.iv[0]; |
| 475 | creq.ivsize = qcedev_areq->cipher_op_req.ivlen; |
| 476 | |
| 477 | creq.enckey = &qcedev_areq->cipher_op_req.enckey[0]; |
| 478 | creq.encklen = qcedev_areq->cipher_op_req.encklen; |
| 479 | |
| 480 | creq.cryptlen = qcedev_areq->cipher_op_req.data_len; |
| 481 | |
| 482 | if (qcedev_areq->cipher_op_req.encklen == 0) { |
| 483 | if ((qcedev_areq->cipher_op_req.op == QCEDEV_OPER_ENC_NO_KEY) |
| 484 | || (qcedev_areq->cipher_op_req.op == |
| 485 | QCEDEV_OPER_DEC_NO_KEY)) |
| 486 | creq.op = QCE_REQ_ABLK_CIPHER_NO_KEY; |
| 487 | else { |
| 488 | int i; |
| 489 | |
| 490 | for (i = 0; i < QCEDEV_MAX_KEY_SIZE; i++) { |
| 491 | if (qcedev_areq->cipher_op_req.enckey[i] != 0) |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | if ((podev->platform_support.hw_key_support == 1) && |
| 496 | (i == QCEDEV_MAX_KEY_SIZE)) |
| 497 | creq.op = QCE_REQ_ABLK_CIPHER; |
| 498 | else { |
| 499 | ret = -EINVAL; |
| 500 | goto unsupported; |
| 501 | } |
| 502 | } |
| 503 | } else { |
| 504 | creq.op = QCE_REQ_ABLK_CIPHER; |
| 505 | } |
| 506 | |
| 507 | creq.qce_cb = qcedev_cipher_req_cb; |
| 508 | creq.areq = (void *)&qcedev_areq->cipher_req; |
| 509 | |
| 510 | ret = qce_ablk_cipher_req(podev->qce, &creq); |
| 511 | unsupported: |
| 512 | if (ret) |
| 513 | qcedev_areq->err = -ENXIO; |
| 514 | else |
| 515 | qcedev_areq->err = 0; |
| 516 | return ret; |
| 517 | }; |
| 518 | |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 519 | static int start_sha_req(struct qcedev_control *podev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 520 | { |
| 521 | struct qcedev_async_req *qcedev_areq; |
| 522 | struct qce_sha_req sreq; |
| 523 | int ret = 0; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 524 | struct qcedev_handle *handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 525 | |
| 526 | /* start the command on the podev->active_command */ |
| 527 | qcedev_areq = podev->active_command; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 528 | handle = qcedev_areq->handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 529 | |
| 530 | switch (qcedev_areq->sha_op_req.alg) { |
| 531 | case QCEDEV_ALG_SHA1: |
| 532 | sreq.alg = QCE_HASH_SHA1; |
| 533 | break; |
| 534 | case QCEDEV_ALG_SHA256: |
| 535 | sreq.alg = QCE_HASH_SHA256; |
| 536 | break; |
| 537 | case QCEDEV_ALG_SHA1_HMAC: |
| 538 | if (podev->ce_support.sha_hmac) { |
| 539 | sreq.alg = QCE_HASH_SHA1_HMAC; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 540 | sreq.authkey = &handle->sha_ctxt.authkey[0]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 541 | |
| 542 | } else { |
| 543 | sreq.alg = QCE_HASH_SHA1; |
| 544 | sreq.authkey = NULL; |
| 545 | } |
| 546 | break; |
| 547 | case QCEDEV_ALG_SHA256_HMAC: |
| 548 | if (podev->ce_support.sha_hmac) { |
| 549 | sreq.alg = QCE_HASH_SHA256_HMAC; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 550 | sreq.authkey = &handle->sha_ctxt.authkey[0]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 551 | |
| 552 | } else { |
| 553 | sreq.alg = QCE_HASH_SHA256; |
| 554 | sreq.authkey = NULL; |
| 555 | } |
| 556 | break; |
| 557 | case QCEDEV_ALG_AES_CMAC: |
| 558 | sreq.alg = QCE_HASH_AES_CMAC; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 559 | sreq.authkey = &handle->sha_ctxt.authkey[0]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 560 | sreq.authklen = qcedev_areq->sha_op_req.authklen; |
| 561 | break; |
| 562 | default: |
| 563 | break; |
| 564 | }; |
| 565 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 566 | qcedev_areq->sha_req.cookie = handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 567 | |
| 568 | sreq.qce_cb = qcedev_sha_req_cb; |
| 569 | if (qcedev_areq->sha_op_req.alg != QCEDEV_ALG_AES_CMAC) { |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 570 | sreq.auth_data[0] = handle->sha_ctxt.auth_data[0]; |
| 571 | sreq.auth_data[1] = handle->sha_ctxt.auth_data[1]; |
| 572 | sreq.auth_data[2] = handle->sha_ctxt.auth_data[2]; |
| 573 | sreq.auth_data[3] = handle->sha_ctxt.auth_data[3]; |
| 574 | sreq.digest = &handle->sha_ctxt.digest[0]; |
| 575 | sreq.first_blk = handle->sha_ctxt.first_blk; |
| 576 | sreq.last_blk = handle->sha_ctxt.last_blk; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 577 | } |
| 578 | sreq.size = qcedev_areq->sha_req.sreq.nbytes; |
| 579 | sreq.src = qcedev_areq->sha_req.sreq.src; |
| 580 | sreq.areq = (void *)&qcedev_areq->sha_req; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 581 | |
| 582 | ret = qce_process_sha_req(podev->qce, &sreq); |
| 583 | |
| 584 | if (ret) |
| 585 | qcedev_areq->err = -ENXIO; |
| 586 | else |
| 587 | qcedev_areq->err = 0; |
| 588 | return ret; |
| 589 | }; |
| 590 | |
| 591 | static int submit_req(struct qcedev_async_req *qcedev_areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 592 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 593 | { |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 594 | struct qcedev_control *podev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 595 | unsigned long flags = 0; |
| 596 | int ret = 0; |
| 597 | struct qcedev_stat *pstat; |
| 598 | |
| 599 | qcedev_areq->err = 0; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 600 | podev = handle->cntl; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 601 | |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 602 | if (podev->platform_support.ce_shared) { |
| 603 | ret = qcedev_lock_ce(podev); |
| 604 | if (ret) |
| 605 | return ret; |
| 606 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 607 | |
| 608 | spin_lock_irqsave(&podev->lock, flags); |
| 609 | |
| 610 | if (podev->active_command == NULL) { |
| 611 | podev->active_command = qcedev_areq; |
| 612 | if (qcedev_areq->op_type == QCEDEV_CRYPTO_OPER_CIPHER) |
| 613 | ret = start_cipher_req(podev); |
| 614 | else |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 615 | ret = start_sha_req(podev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 616 | } else { |
| 617 | list_add_tail(&qcedev_areq->list, &podev->ready_commands); |
| 618 | } |
| 619 | |
| 620 | if (ret != 0) |
| 621 | podev->active_command = NULL; |
| 622 | |
| 623 | spin_unlock_irqrestore(&podev->lock, flags); |
| 624 | |
| 625 | if (ret == 0) |
| 626 | wait_for_completion(&qcedev_areq->complete); |
| 627 | |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 628 | if (podev->platform_support.ce_shared) |
| 629 | ret = qcedev_unlock_ce(podev); |
| 630 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 631 | if (ret) |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 632 | qcedev_areq->err = -EIO; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 633 | |
| 634 | pstat = &_qcedev_stat[podev->pdev->id]; |
| 635 | if (qcedev_areq->op_type == QCEDEV_CRYPTO_OPER_CIPHER) { |
| 636 | switch (qcedev_areq->cipher_op_req.op) { |
| 637 | case QCEDEV_OPER_DEC: |
| 638 | if (qcedev_areq->err) |
| 639 | pstat->qcedev_dec_fail++; |
| 640 | else |
| 641 | pstat->qcedev_dec_success++; |
| 642 | break; |
| 643 | case QCEDEV_OPER_ENC: |
| 644 | if (qcedev_areq->err) |
| 645 | pstat->qcedev_enc_fail++; |
| 646 | else |
| 647 | pstat->qcedev_enc_success++; |
| 648 | break; |
| 649 | default: |
| 650 | break; |
| 651 | }; |
| 652 | } else { |
| 653 | if (qcedev_areq->err) |
| 654 | pstat->qcedev_sha_fail++; |
| 655 | else |
| 656 | pstat->qcedev_sha_success++; |
| 657 | } |
| 658 | |
| 659 | return qcedev_areq->err; |
| 660 | } |
| 661 | |
| 662 | static int qcedev_sha_init(struct qcedev_async_req *areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 663 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 664 | { |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 665 | struct qcedev_sha_ctxt *sha_ctxt = &handle->sha_ctxt; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 666 | |
| 667 | memset(sha_ctxt, 0, sizeof(struct qcedev_sha_ctxt)); |
| 668 | sha_ctxt->first_blk = 1; |
| 669 | |
| 670 | if ((areq->sha_op_req.alg == QCEDEV_ALG_SHA1) || |
| 671 | (areq->sha_op_req.alg == QCEDEV_ALG_SHA1_HMAC)) { |
| 672 | memcpy(&sha_ctxt->digest[0], |
| 673 | &_std_init_vector_sha1_uint8[0], SHA1_DIGEST_SIZE); |
| 674 | sha_ctxt->diglen = SHA1_DIGEST_SIZE; |
| 675 | } else { |
| 676 | if ((areq->sha_op_req.alg == QCEDEV_ALG_SHA256) || |
| 677 | (areq->sha_op_req.alg == QCEDEV_ALG_SHA256_HMAC)) { |
| 678 | memcpy(&sha_ctxt->digest[0], |
| 679 | &_std_init_vector_sha256_uint8[0], |
| 680 | SHA256_DIGEST_SIZE); |
| 681 | sha_ctxt->diglen = SHA256_DIGEST_SIZE; |
| 682 | } |
| 683 | } |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | |
| 688 | static int qcedev_sha_update_max_xfer(struct qcedev_async_req *qcedev_areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 689 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 690 | { |
| 691 | int err = 0; |
| 692 | int i = 0; |
| 693 | struct scatterlist sg_src[2]; |
| 694 | uint32_t total; |
| 695 | |
| 696 | uint8_t *user_src = NULL; |
| 697 | uint8_t *k_src = NULL; |
| 698 | uint8_t *k_buf_src = NULL; |
| 699 | uint8_t *k_align_src = NULL; |
| 700 | |
| 701 | uint32_t sha_pad_len = 0; |
| 702 | uint32_t trailing_buf_len = 0; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 703 | uint32_t t_buf = handle->sha_ctxt.trailing_buf_len; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 704 | uint32_t sha_block_size; |
| 705 | |
| 706 | total = qcedev_areq->sha_op_req.data_len + t_buf; |
| 707 | |
| 708 | if (qcedev_areq->sha_op_req.alg == QCEDEV_ALG_SHA1) |
| 709 | sha_block_size = SHA1_BLOCK_SIZE; |
| 710 | else |
| 711 | sha_block_size = SHA256_BLOCK_SIZE; |
| 712 | |
| 713 | if (total <= sha_block_size) { |
| 714 | uint32_t len = qcedev_areq->sha_op_req.data_len; |
| 715 | |
| 716 | i = 0; |
| 717 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 718 | k_src = &handle->sha_ctxt.trailing_buf[t_buf]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 719 | |
| 720 | /* Copy data from user src(s) */ |
| 721 | while (len > 0) { |
| 722 | user_src = |
| 723 | (void __user *)qcedev_areq->sha_op_req.data[i].vaddr; |
| 724 | if (user_src && __copy_from_user(k_src, |
| 725 | (void __user *)user_src, |
| 726 | qcedev_areq->sha_op_req.data[i].len)) |
| 727 | return -EFAULT; |
| 728 | |
| 729 | len -= qcedev_areq->sha_op_req.data[i].len; |
| 730 | k_src += qcedev_areq->sha_op_req.data[i].len; |
| 731 | i++; |
| 732 | } |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 733 | handle->sha_ctxt.trailing_buf_len = total; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | |
| 739 | k_buf_src = kmalloc(total + CACHE_LINE_SIZE * 2, |
| 740 | GFP_KERNEL); |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 741 | if (k_buf_src == NULL) { |
| 742 | pr_err("%s: Can't Allocate memory: k_buf_src 0x%x\n", |
| 743 | __func__, (uint32_t)k_buf_src); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 744 | return -ENOMEM; |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 745 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 746 | |
| 747 | k_align_src = (uint8_t *) ALIGN(((unsigned int)k_buf_src), |
| 748 | CACHE_LINE_SIZE); |
| 749 | k_src = k_align_src; |
| 750 | |
| 751 | /* check for trailing buffer from previous updates and append it */ |
| 752 | if (t_buf > 0) { |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 753 | memcpy(k_src, &handle->sha_ctxt.trailing_buf[0], |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 754 | t_buf); |
| 755 | k_src += t_buf; |
| 756 | } |
| 757 | |
| 758 | /* Copy data from user src(s) */ |
| 759 | user_src = (void __user *)qcedev_areq->sha_op_req.data[0].vaddr; |
| 760 | if (user_src && __copy_from_user(k_src, |
| 761 | (void __user *)user_src, |
| 762 | qcedev_areq->sha_op_req.data[0].len)) { |
| 763 | kfree(k_buf_src); |
| 764 | return -EFAULT; |
| 765 | } |
| 766 | k_src += qcedev_areq->sha_op_req.data[0].len; |
| 767 | for (i = 1; i < qcedev_areq->sha_op_req.entries; i++) { |
| 768 | user_src = (void __user *)qcedev_areq->sha_op_req.data[i].vaddr; |
| 769 | if (user_src && __copy_from_user(k_src, |
| 770 | (void __user *)user_src, |
| 771 | qcedev_areq->sha_op_req.data[i].len)) { |
| 772 | kfree(k_buf_src); |
| 773 | return -EFAULT; |
| 774 | } |
| 775 | k_src += qcedev_areq->sha_op_req.data[i].len; |
| 776 | } |
| 777 | |
| 778 | /* get new trailing buffer */ |
| 779 | sha_pad_len = ALIGN(total, CE_SHA_BLOCK_SIZE) - total; |
| 780 | trailing_buf_len = CE_SHA_BLOCK_SIZE - sha_pad_len; |
| 781 | |
| 782 | qcedev_areq->sha_req.sreq.src = (struct scatterlist *) &sg_src[0]; |
| 783 | sg_set_buf(qcedev_areq->sha_req.sreq.src, k_align_src, |
| 784 | total-trailing_buf_len); |
| 785 | sg_mark_end(qcedev_areq->sha_req.sreq.src); |
| 786 | |
| 787 | qcedev_areq->sha_req.sreq.nbytes = total - trailing_buf_len; |
| 788 | |
| 789 | /* update sha_ctxt trailing buf content to new trailing buf */ |
| 790 | if (trailing_buf_len > 0) { |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 791 | memset(&handle->sha_ctxt.trailing_buf[0], 0, 64); |
| 792 | memcpy(&handle->sha_ctxt.trailing_buf[0], |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 793 | (k_src - trailing_buf_len), |
| 794 | trailing_buf_len); |
| 795 | } |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 796 | handle->sha_ctxt.trailing_buf_len = trailing_buf_len; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 797 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 798 | err = submit_req(qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 799 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 800 | handle->sha_ctxt.last_blk = 0; |
| 801 | handle->sha_ctxt.first_blk = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 802 | |
| 803 | kfree(k_buf_src); |
| 804 | return err; |
| 805 | } |
| 806 | |
| 807 | static int qcedev_sha_update(struct qcedev_async_req *qcedev_areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 808 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 809 | { |
| 810 | int err = 0; |
| 811 | int i = 0; |
| 812 | int j = 0; |
| 813 | int k = 0; |
| 814 | int num_entries = 0; |
| 815 | uint32_t total = 0; |
| 816 | |
| 817 | /* verify address src(s) */ |
| 818 | for (i = 0; i < qcedev_areq->sha_op_req.entries; i++) |
| 819 | if (!access_ok(VERIFY_READ, |
| 820 | (void __user *)qcedev_areq->sha_op_req.data[i].vaddr, |
| 821 | qcedev_areq->sha_op_req.data[i].len)) |
| 822 | return -EFAULT; |
| 823 | |
| 824 | if (qcedev_areq->sha_op_req.data_len > QCE_MAX_OPER_DATA) { |
| 825 | |
| 826 | struct qcedev_sha_op_req *saved_req; |
| 827 | struct qcedev_sha_op_req req; |
| 828 | struct qcedev_sha_op_req *sreq = &qcedev_areq->sha_op_req; |
| 829 | |
| 830 | /* save the original req structure */ |
| 831 | saved_req = |
| 832 | kmalloc(sizeof(struct qcedev_sha_op_req), GFP_KERNEL); |
| 833 | if (saved_req == NULL) { |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 834 | pr_err("%s:Can't Allocate mem:saved_req 0x%x\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 835 | __func__, (uint32_t)saved_req); |
| 836 | return -ENOMEM; |
| 837 | } |
| 838 | memcpy(&req, sreq, sizeof(struct qcedev_sha_op_req)); |
| 839 | memcpy(saved_req, sreq, sizeof(struct qcedev_sha_op_req)); |
| 840 | |
| 841 | i = 0; |
| 842 | /* Address 32 KB at a time */ |
| 843 | while ((i < req.entries) && (err == 0)) { |
| 844 | if (sreq->data[i].len > QCE_MAX_OPER_DATA) { |
| 845 | sreq->data[0].len = QCE_MAX_OPER_DATA; |
| 846 | if (i > 0) { |
| 847 | sreq->data[0].vaddr = |
| 848 | sreq->data[i].vaddr; |
| 849 | } |
| 850 | |
| 851 | sreq->data_len = QCE_MAX_OPER_DATA; |
| 852 | sreq->entries = 1; |
| 853 | |
| 854 | err = qcedev_sha_update_max_xfer(qcedev_areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 855 | handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 856 | |
| 857 | sreq->data[i].len = req.data[i].len - |
| 858 | QCE_MAX_OPER_DATA; |
| 859 | sreq->data[i].vaddr = req.data[i].vaddr + |
| 860 | QCE_MAX_OPER_DATA; |
| 861 | req.data[i].vaddr = sreq->data[i].vaddr; |
| 862 | req.data[i].len = sreq->data[i].len; |
| 863 | } else { |
| 864 | total = 0; |
| 865 | for (j = i; j < req.entries; j++) { |
| 866 | num_entries++; |
| 867 | if ((total + sreq->data[j].len) >= |
| 868 | QCE_MAX_OPER_DATA) { |
| 869 | sreq->data[j].len = |
| 870 | (QCE_MAX_OPER_DATA - total); |
| 871 | total = QCE_MAX_OPER_DATA; |
| 872 | break; |
| 873 | } |
| 874 | total += sreq->data[j].len; |
| 875 | } |
| 876 | |
| 877 | sreq->data_len = total; |
| 878 | if (i > 0) |
| 879 | for (k = 0; k < num_entries; k++) { |
| 880 | sreq->data[k].len = |
| 881 | sreq->data[i+k].len; |
| 882 | sreq->data[k].vaddr = |
| 883 | sreq->data[i+k].vaddr; |
| 884 | } |
| 885 | sreq->entries = num_entries; |
| 886 | |
| 887 | i = j; |
| 888 | err = qcedev_sha_update_max_xfer(qcedev_areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 889 | handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 890 | num_entries = 0; |
| 891 | |
| 892 | sreq->data[i].vaddr = req.data[i].vaddr + |
| 893 | sreq->data[i].len; |
| 894 | sreq->data[i].len = req.data[i].len - |
| 895 | sreq->data[i].len; |
| 896 | req.data[i].vaddr = sreq->data[i].vaddr; |
| 897 | req.data[i].len = sreq->data[i].len; |
| 898 | |
| 899 | if (sreq->data[i].len == 0) |
| 900 | i++; |
| 901 | } |
| 902 | } /* end of while ((i < req.entries) && (err == 0)) */ |
| 903 | |
| 904 | /* Restore the original req structure */ |
| 905 | for (i = 0; i < saved_req->entries; i++) { |
| 906 | sreq->data[i].len = saved_req->data[i].len; |
| 907 | sreq->data[i].vaddr = saved_req->data[i].vaddr; |
| 908 | } |
| 909 | sreq->entries = saved_req->entries; |
| 910 | sreq->data_len = saved_req->data_len; |
| 911 | kfree(saved_req); |
| 912 | } else |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 913 | err = qcedev_sha_update_max_xfer(qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 914 | |
| 915 | return err; |
| 916 | } |
| 917 | |
| 918 | static int qcedev_sha_final(struct qcedev_async_req *qcedev_areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 919 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 920 | { |
| 921 | int err = 0; |
| 922 | struct scatterlist sg_src; |
| 923 | uint32_t total; |
| 924 | |
| 925 | uint8_t *k_buf_src = NULL; |
| 926 | uint8_t *k_align_src = NULL; |
| 927 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 928 | handle->sha_ctxt.first_blk = 0; |
| 929 | handle->sha_ctxt.last_blk = 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 930 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 931 | total = handle->sha_ctxt.trailing_buf_len; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 932 | |
| 933 | if (total) { |
| 934 | k_buf_src = kmalloc(total + CACHE_LINE_SIZE * 2, |
| 935 | GFP_KERNEL); |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 936 | if (k_buf_src == NULL) { |
| 937 | pr_err("%s: Can't Allocate memory: k_buf_src 0x%x\n", |
| 938 | __func__, (uint32_t)k_buf_src); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 939 | return -ENOMEM; |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 940 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 941 | |
| 942 | k_align_src = (uint8_t *) ALIGN(((unsigned int)k_buf_src), |
| 943 | CACHE_LINE_SIZE); |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 944 | memcpy(k_align_src, &handle->sha_ctxt.trailing_buf[0], total); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 945 | } |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 946 | handle->sha_ctxt.last_blk = 1; |
| 947 | handle->sha_ctxt.first_blk = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 948 | |
| 949 | qcedev_areq->sha_req.sreq.src = (struct scatterlist *) &sg_src; |
| 950 | sg_set_buf(qcedev_areq->sha_req.sreq.src, k_align_src, total); |
| 951 | sg_mark_end(qcedev_areq->sha_req.sreq.src); |
| 952 | |
| 953 | qcedev_areq->sha_req.sreq.nbytes = total; |
| 954 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 955 | err = submit_req(qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 956 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 957 | handle->sha_ctxt.first_blk = 0; |
| 958 | handle->sha_ctxt.last_blk = 0; |
| 959 | handle->sha_ctxt.auth_data[0] = 0; |
| 960 | handle->sha_ctxt.auth_data[1] = 0; |
| 961 | handle->sha_ctxt.trailing_buf_len = 0; |
| 962 | memset(&handle->sha_ctxt.trailing_buf[0], 0, 64); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 963 | |
| 964 | kfree(k_buf_src); |
| 965 | return err; |
| 966 | } |
| 967 | |
| 968 | static int qcedev_hash_cmac(struct qcedev_async_req *qcedev_areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 969 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 970 | { |
| 971 | int err = 0; |
| 972 | int i = 0; |
| 973 | struct scatterlist sg_src[2]; |
| 974 | uint32_t total; |
| 975 | |
| 976 | uint8_t *user_src = NULL; |
| 977 | uint8_t *k_src = NULL; |
| 978 | uint8_t *k_buf_src = NULL; |
| 979 | |
| 980 | total = qcedev_areq->sha_op_req.data_len; |
| 981 | |
| 982 | /* verify address src(s) */ |
| 983 | for (i = 0; i < qcedev_areq->sha_op_req.entries; i++) |
| 984 | if (!access_ok(VERIFY_READ, |
| 985 | (void __user *)qcedev_areq->sha_op_req.data[i].vaddr, |
| 986 | qcedev_areq->sha_op_req.data[i].len)) |
| 987 | return -EFAULT; |
| 988 | |
| 989 | /* Verify Source Address */ |
| 990 | if (!access_ok(VERIFY_READ, |
| 991 | (void __user *)qcedev_areq->sha_op_req.authkey, |
| 992 | qcedev_areq->sha_op_req.authklen)) |
| 993 | return -EFAULT; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 994 | if (__copy_from_user(&handle->sha_ctxt.authkey[0], |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 995 | (void __user *)qcedev_areq->sha_op_req.authkey, |
| 996 | qcedev_areq->sha_op_req.authklen)) |
| 997 | return -EFAULT; |
| 998 | |
| 999 | |
| 1000 | k_buf_src = kmalloc(total, GFP_KERNEL); |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 1001 | if (k_buf_src == NULL) { |
| 1002 | pr_err("%s: Can't Allocate memory: k_buf_src 0x%x\n", |
| 1003 | __func__, (uint32_t)k_buf_src); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1004 | return -ENOMEM; |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 1005 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1006 | |
| 1007 | k_src = k_buf_src; |
| 1008 | |
| 1009 | /* Copy data from user src(s) */ |
| 1010 | user_src = (void __user *)qcedev_areq->sha_op_req.data[0].vaddr; |
| 1011 | for (i = 0; i < qcedev_areq->sha_op_req.entries; i++) { |
| 1012 | user_src = |
| 1013 | (void __user *)qcedev_areq->sha_op_req.data[i].vaddr; |
| 1014 | if (user_src && __copy_from_user(k_src, (void __user *)user_src, |
| 1015 | qcedev_areq->sha_op_req.data[i].len)) { |
| 1016 | kfree(k_buf_src); |
| 1017 | return -EFAULT; |
| 1018 | } |
| 1019 | k_src += qcedev_areq->sha_op_req.data[i].len; |
| 1020 | } |
| 1021 | |
| 1022 | qcedev_areq->sha_req.sreq.src = (struct scatterlist *) &sg_src[0]; |
| 1023 | sg_set_buf(qcedev_areq->sha_req.sreq.src, k_buf_src, total); |
| 1024 | sg_mark_end(qcedev_areq->sha_req.sreq.src); |
| 1025 | |
| 1026 | qcedev_areq->sha_req.sreq.nbytes = total; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1027 | handle->sha_ctxt.diglen = qcedev_areq->sha_op_req.diglen; |
| 1028 | err = submit_req(qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1029 | |
| 1030 | kfree(k_buf_src); |
| 1031 | return err; |
| 1032 | } |
| 1033 | |
| 1034 | static int qcedev_set_hmac_auth_key(struct qcedev_async_req *areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1035 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1036 | { |
| 1037 | int err = 0; |
| 1038 | |
| 1039 | if (areq->sha_op_req.authklen <= QCEDEV_MAX_KEY_SIZE) { |
| 1040 | /* Verify Source Address */ |
| 1041 | if (!access_ok(VERIFY_READ, |
| 1042 | (void __user *)areq->sha_op_req.authkey, |
| 1043 | areq->sha_op_req.authklen)) |
| 1044 | return -EFAULT; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1045 | if (__copy_from_user(&handle->sha_ctxt.authkey[0], |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1046 | (void __user *)areq->sha_op_req.authkey, |
| 1047 | areq->sha_op_req.authklen)) |
| 1048 | return -EFAULT; |
| 1049 | } else { |
| 1050 | struct qcedev_async_req authkey_areq; |
| 1051 | |
| 1052 | init_completion(&authkey_areq.complete); |
| 1053 | |
| 1054 | authkey_areq.sha_op_req.entries = 1; |
| 1055 | authkey_areq.sha_op_req.data[0].vaddr = |
| 1056 | areq->sha_op_req.authkey; |
| 1057 | authkey_areq.sha_op_req.data[0].len = areq->sha_op_req.authklen; |
| 1058 | authkey_areq.sha_op_req.data_len = areq->sha_op_req.authklen; |
| 1059 | authkey_areq.sha_op_req.diglen = 0; |
| 1060 | memset(&authkey_areq.sha_op_req.digest[0], 0, |
| 1061 | QCEDEV_MAX_SHA_DIGEST); |
| 1062 | if (areq->sha_op_req.alg == QCEDEV_ALG_SHA1_HMAC) |
| 1063 | authkey_areq.sha_op_req.alg = QCEDEV_ALG_SHA1; |
| 1064 | if (areq->sha_op_req.alg == QCEDEV_ALG_SHA256_HMAC) |
| 1065 | authkey_areq.sha_op_req.alg = QCEDEV_ALG_SHA256; |
| 1066 | |
| 1067 | authkey_areq.op_type = QCEDEV_CRYPTO_OPER_SHA; |
| 1068 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1069 | qcedev_sha_init(&authkey_areq, handle); |
| 1070 | err = qcedev_sha_update(&authkey_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1071 | if (!err) |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1072 | err = qcedev_sha_final(&authkey_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1073 | else |
| 1074 | return err; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1075 | memcpy(&handle->sha_ctxt.authkey[0], |
| 1076 | &handle->sha_ctxt.digest[0], |
| 1077 | handle->sha_ctxt.diglen); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1078 | } |
| 1079 | return err; |
| 1080 | } |
| 1081 | |
| 1082 | static int qcedev_hmac_get_ohash(struct qcedev_async_req *qcedev_areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1083 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1084 | { |
| 1085 | int err = 0; |
| 1086 | struct scatterlist sg_src; |
| 1087 | uint8_t *k_src = NULL; |
| 1088 | uint32_t sha_block_size = 0; |
| 1089 | uint32_t sha_digest_size = 0; |
| 1090 | |
| 1091 | if (qcedev_areq->sha_op_req.alg == QCEDEV_ALG_SHA1_HMAC) { |
| 1092 | sha_digest_size = SHA1_DIGEST_SIZE; |
| 1093 | sha_block_size = SHA1_BLOCK_SIZE; |
| 1094 | } else { |
| 1095 | if (qcedev_areq->sha_op_req.alg == QCEDEV_ALG_SHA256_HMAC) { |
| 1096 | sha_digest_size = SHA256_DIGEST_SIZE; |
| 1097 | sha_block_size = SHA256_BLOCK_SIZE; |
| 1098 | } |
| 1099 | } |
| 1100 | k_src = kmalloc(sha_block_size, GFP_KERNEL); |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 1101 | if (k_src == NULL) { |
| 1102 | pr_err("%s: Can't Allocate memory: k_src 0x%x\n", |
| 1103 | __func__, (uint32_t)k_src); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1104 | return -ENOMEM; |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 1105 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1106 | |
| 1107 | /* check for trailing buffer from previous updates and append it */ |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1108 | memcpy(k_src, &handle->sha_ctxt.trailing_buf[0], |
| 1109 | handle->sha_ctxt.trailing_buf_len); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1110 | |
| 1111 | qcedev_areq->sha_req.sreq.src = (struct scatterlist *) &sg_src; |
| 1112 | sg_set_buf(qcedev_areq->sha_req.sreq.src, k_src, sha_block_size); |
| 1113 | sg_mark_end(qcedev_areq->sha_req.sreq.src); |
| 1114 | |
| 1115 | qcedev_areq->sha_req.sreq.nbytes = sha_block_size; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1116 | memset(&handle->sha_ctxt.trailing_buf[0], 0, sha_block_size); |
| 1117 | memcpy(&handle->sha_ctxt.trailing_buf[0], &handle->sha_ctxt.digest[0], |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1118 | sha_digest_size); |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1119 | handle->sha_ctxt.trailing_buf_len = sha_digest_size; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1120 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1121 | handle->sha_ctxt.first_blk = 1; |
| 1122 | handle->sha_ctxt.last_blk = 0; |
| 1123 | handle->sha_ctxt.auth_data[0] = 0; |
| 1124 | handle->sha_ctxt.auth_data[1] = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1125 | |
| 1126 | if (qcedev_areq->sha_op_req.alg == QCEDEV_ALG_SHA1_HMAC) { |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1127 | memcpy(&handle->sha_ctxt.digest[0], |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1128 | &_std_init_vector_sha1_uint8[0], SHA1_DIGEST_SIZE); |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1129 | handle->sha_ctxt.diglen = SHA1_DIGEST_SIZE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | if (qcedev_areq->sha_op_req.alg == QCEDEV_ALG_SHA256_HMAC) { |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1133 | memcpy(&handle->sha_ctxt.digest[0], |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1134 | &_std_init_vector_sha256_uint8[0], SHA256_DIGEST_SIZE); |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1135 | handle->sha_ctxt.diglen = SHA256_DIGEST_SIZE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1136 | } |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1137 | err = submit_req(qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1138 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1139 | handle->sha_ctxt.last_blk = 0; |
| 1140 | handle->sha_ctxt.first_blk = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1141 | |
| 1142 | kfree(k_src); |
| 1143 | return err; |
| 1144 | } |
| 1145 | |
| 1146 | static int qcedev_hmac_update_iokey(struct qcedev_async_req *areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1147 | struct qcedev_handle *handle, bool ikey) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1148 | { |
| 1149 | int i; |
| 1150 | uint32_t constant; |
| 1151 | uint32_t sha_block_size; |
| 1152 | |
| 1153 | if (ikey) |
| 1154 | constant = 0x36; |
| 1155 | else |
| 1156 | constant = 0x5c; |
| 1157 | |
| 1158 | if (areq->sha_op_req.alg == QCEDEV_ALG_SHA1_HMAC) |
| 1159 | sha_block_size = SHA1_BLOCK_SIZE; |
| 1160 | else |
| 1161 | sha_block_size = SHA256_BLOCK_SIZE; |
| 1162 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1163 | memset(&handle->sha_ctxt.trailing_buf[0], 0, sha_block_size); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1164 | for (i = 0; i < sha_block_size; i++) |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1165 | handle->sha_ctxt.trailing_buf[i] = |
| 1166 | (handle->sha_ctxt.authkey[i] ^ constant); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1167 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1168 | handle->sha_ctxt.trailing_buf_len = sha_block_size; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | static int qcedev_hmac_init(struct qcedev_async_req *areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1173 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1174 | { |
| 1175 | int err; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1176 | struct qcedev_control *podev = handle->cntl; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1177 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1178 | qcedev_sha_init(areq, handle); |
| 1179 | err = qcedev_set_hmac_auth_key(areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1180 | if (err) |
| 1181 | return err; |
| 1182 | if (!podev->ce_support.sha_hmac) |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1183 | qcedev_hmac_update_iokey(areq, handle, true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | static int qcedev_hmac_final(struct qcedev_async_req *areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1188 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1189 | { |
| 1190 | int err; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1191 | struct qcedev_control *podev = handle->cntl; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1192 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1193 | err = qcedev_sha_final(areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1194 | if (podev->ce_support.sha_hmac) |
| 1195 | return err; |
| 1196 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1197 | qcedev_hmac_update_iokey(areq, handle, false); |
| 1198 | err = qcedev_hmac_get_ohash(areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1199 | if (err) |
| 1200 | return err; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1201 | err = qcedev_sha_final(areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1202 | |
| 1203 | return err; |
| 1204 | } |
| 1205 | |
| 1206 | static int qcedev_hash_init(struct qcedev_async_req *areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1207 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1208 | { |
| 1209 | if ((areq->sha_op_req.alg == QCEDEV_ALG_SHA1) || |
| 1210 | (areq->sha_op_req.alg == QCEDEV_ALG_SHA256)) |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1211 | return qcedev_sha_init(areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1212 | else |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1213 | return qcedev_hmac_init(areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | static int qcedev_hash_update(struct qcedev_async_req *qcedev_areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1217 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1218 | { |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1219 | return qcedev_sha_update(qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | static int qcedev_hash_final(struct qcedev_async_req *areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1223 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1224 | { |
| 1225 | if ((areq->sha_op_req.alg == QCEDEV_ALG_SHA1) || |
| 1226 | (areq->sha_op_req.alg == QCEDEV_ALG_SHA256)) |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1227 | return qcedev_sha_final(areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1228 | else |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1229 | return qcedev_hmac_final(areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Ramesh Masavarapu | fa679d9 | 2011-10-13 23:42:59 -0700 | [diff] [blame] | 1232 | #ifdef CONFIG_ANDROID_PMEM |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1233 | static int qcedev_pmem_ablk_cipher_max_xfer(struct qcedev_async_req *areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1234 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1235 | { |
| 1236 | int i = 0; |
| 1237 | int err = 0; |
| 1238 | struct scatterlist *sg_src = NULL; |
| 1239 | struct scatterlist *sg_dst = NULL; |
| 1240 | struct scatterlist *sg_ndex = NULL; |
| 1241 | struct file *file_src = NULL; |
| 1242 | struct file *file_dst = NULL; |
| 1243 | unsigned long paddr; |
| 1244 | unsigned long kvaddr; |
| 1245 | unsigned long len; |
| 1246 | |
| 1247 | sg_src = kmalloc((sizeof(struct scatterlist) * |
| 1248 | areq->cipher_op_req.entries), GFP_KERNEL); |
| 1249 | if (sg_src == NULL) { |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 1250 | pr_err("%s: Can't Allocate memory:sg_src 0x%x\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1251 | __func__, (uint32_t)sg_src); |
| 1252 | return -ENOMEM; |
| 1253 | |
| 1254 | } |
| 1255 | memset(sg_src, 0, (sizeof(struct scatterlist) * |
| 1256 | areq->cipher_op_req.entries)); |
| 1257 | sg_ndex = sg_src; |
| 1258 | areq->cipher_req.creq.src = sg_src; |
| 1259 | |
| 1260 | /* address src */ |
| 1261 | get_pmem_file(areq->cipher_op_req.pmem.fd_src, &paddr, |
| 1262 | &kvaddr, &len, &file_src); |
| 1263 | |
| 1264 | for (i = 0; i < areq->cipher_op_req.entries; i++) { |
| 1265 | sg_set_buf(sg_ndex, |
| 1266 | ((uint8_t *)(areq->cipher_op_req.pmem.src[i].offset) + kvaddr), |
| 1267 | areq->cipher_op_req.pmem.src[i].len); |
| 1268 | sg_ndex++; |
| 1269 | } |
| 1270 | sg_mark_end(--sg_ndex); |
| 1271 | |
| 1272 | for (i = 0; i < areq->cipher_op_req.entries; i++) |
| 1273 | areq->cipher_op_req.pmem.src[i].offset += (uint32_t)paddr; |
| 1274 | |
| 1275 | /* address dst */ |
| 1276 | /* If not place encryption/decryption */ |
| 1277 | if (areq->cipher_op_req.in_place_op != 1) { |
| 1278 | sg_dst = kmalloc((sizeof(struct scatterlist) * |
| 1279 | areq->cipher_op_req.entries), GFP_KERNEL); |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 1280 | if (sg_dst == NULL) { |
| 1281 | pr_err("%s: Can't Allocate memory: sg_dst 0x%x\n", |
| 1282 | __func__, (uint32_t)sg_dst); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1283 | return -ENOMEM; |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 1284 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1285 | memset(sg_dst, 0, (sizeof(struct scatterlist) * |
| 1286 | areq->cipher_op_req.entries)); |
| 1287 | areq->cipher_req.creq.dst = sg_dst; |
| 1288 | sg_ndex = sg_dst; |
| 1289 | |
| 1290 | get_pmem_file(areq->cipher_op_req.pmem.fd_dst, &paddr, |
| 1291 | &kvaddr, &len, &file_dst); |
| 1292 | for (i = 0; i < areq->cipher_op_req.entries; i++) |
| 1293 | sg_set_buf(sg_ndex++, |
| 1294 | ((uint8_t *)(areq->cipher_op_req.pmem.dst[i].offset) |
| 1295 | + kvaddr), areq->cipher_op_req.pmem.dst[i].len); |
| 1296 | sg_mark_end(--sg_ndex); |
| 1297 | |
| 1298 | for (i = 0; i < areq->cipher_op_req.entries; i++) |
| 1299 | areq->cipher_op_req.pmem.dst[i].offset += |
| 1300 | (uint32_t)paddr; |
| 1301 | } else { |
| 1302 | areq->cipher_req.creq.dst = sg_src; |
| 1303 | for (i = 0; i < areq->cipher_op_req.entries; i++) { |
| 1304 | areq->cipher_op_req.pmem.dst[i].offset = |
| 1305 | areq->cipher_op_req.pmem.src[i].offset; |
| 1306 | areq->cipher_op_req.pmem.dst[i].len = |
| 1307 | areq->cipher_op_req.pmem.src[i].len; |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | areq->cipher_req.creq.nbytes = areq->cipher_op_req.data_len; |
| 1312 | areq->cipher_req.creq.info = areq->cipher_op_req.iv; |
| 1313 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1314 | err = submit_req(areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1315 | |
| 1316 | kfree(sg_src); |
| 1317 | kfree(sg_dst); |
| 1318 | |
| 1319 | if (file_dst) |
| 1320 | put_pmem_file(file_dst); |
| 1321 | if (file_src) |
| 1322 | put_pmem_file(file_src); |
| 1323 | |
| 1324 | return err; |
| 1325 | }; |
| 1326 | |
| 1327 | |
| 1328 | static int qcedev_pmem_ablk_cipher(struct qcedev_async_req *qcedev_areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1329 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1330 | { |
| 1331 | int err = 0; |
| 1332 | int i = 0; |
| 1333 | int j = 0; |
| 1334 | int k = 0; |
| 1335 | int num_entries = 0; |
| 1336 | uint32_t total = 0; |
| 1337 | struct qcedev_cipher_op_req *saved_req; |
| 1338 | struct qcedev_cipher_op_req *creq = &qcedev_areq->cipher_op_req; |
| 1339 | |
| 1340 | saved_req = kmalloc(sizeof(struct qcedev_cipher_op_req), GFP_KERNEL); |
| 1341 | if (saved_req == NULL) { |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 1342 | pr_err(KERN_ERR "%s:Can't Allocate mem:saved_req 0x%x\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1343 | __func__, (uint32_t)saved_req); |
| 1344 | return -ENOMEM; |
| 1345 | } |
| 1346 | memcpy(saved_req, creq, sizeof(struct qcedev_cipher_op_req)); |
| 1347 | |
| 1348 | if (qcedev_areq->cipher_op_req.data_len > QCE_MAX_OPER_DATA) { |
| 1349 | |
| 1350 | struct qcedev_cipher_op_req req; |
| 1351 | |
| 1352 | /* save the original req structure */ |
| 1353 | memcpy(&req, creq, sizeof(struct qcedev_cipher_op_req)); |
| 1354 | |
| 1355 | i = 0; |
| 1356 | /* Address 32 KB at a time */ |
| 1357 | while ((i < req.entries) && (err == 0)) { |
| 1358 | if (creq->pmem.src[i].len > QCE_MAX_OPER_DATA) { |
| 1359 | creq->pmem.src[0].len = QCE_MAX_OPER_DATA; |
| 1360 | if (i > 0) { |
| 1361 | creq->pmem.src[0].offset = |
| 1362 | creq->pmem.src[i].offset; |
| 1363 | } |
| 1364 | |
| 1365 | creq->data_len = QCE_MAX_OPER_DATA; |
| 1366 | creq->entries = 1; |
| 1367 | |
| 1368 | err = |
| 1369 | qcedev_pmem_ablk_cipher_max_xfer(qcedev_areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1370 | handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1371 | |
| 1372 | creq->pmem.src[i].len = req.pmem.src[i].len - |
| 1373 | QCE_MAX_OPER_DATA; |
| 1374 | creq->pmem.src[i].offset = |
| 1375 | req.pmem.src[i].offset + |
| 1376 | QCE_MAX_OPER_DATA; |
| 1377 | req.pmem.src[i].offset = |
| 1378 | creq->pmem.src[i].offset; |
| 1379 | req.pmem.src[i].len = creq->pmem.src[i].len; |
| 1380 | } else { |
| 1381 | total = 0; |
| 1382 | for (j = i; j < req.entries; j++) { |
| 1383 | num_entries++; |
| 1384 | if ((total + creq->pmem.src[j].len) |
| 1385 | >= QCE_MAX_OPER_DATA) { |
| 1386 | creq->pmem.src[j].len = |
| 1387 | QCE_MAX_OPER_DATA - total; |
| 1388 | total = QCE_MAX_OPER_DATA; |
| 1389 | break; |
| 1390 | } |
| 1391 | total += creq->pmem.src[j].len; |
| 1392 | } |
| 1393 | |
| 1394 | creq->data_len = total; |
| 1395 | if (i > 0) |
| 1396 | for (k = 0; k < num_entries; k++) { |
| 1397 | creq->pmem.src[k].len = |
| 1398 | creq->pmem.src[i+k].len; |
| 1399 | creq->pmem.src[k].offset = |
| 1400 | creq->pmem.src[i+k].offset; |
| 1401 | } |
| 1402 | creq->entries = num_entries; |
| 1403 | |
| 1404 | i = j; |
| 1405 | err = |
| 1406 | qcedev_pmem_ablk_cipher_max_xfer(qcedev_areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1407 | handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1408 | num_entries = 0; |
| 1409 | |
| 1410 | creq->pmem.src[i].offset = |
| 1411 | req.pmem.src[i].offset + |
| 1412 | creq->pmem.src[i].len; |
| 1413 | creq->pmem.src[i].len = |
| 1414 | req.pmem.src[i].len - |
| 1415 | creq->pmem.src[i].len; |
| 1416 | req.pmem.src[i].offset = |
| 1417 | creq->pmem.src[i].offset; |
| 1418 | req.pmem.src[i].len = |
| 1419 | creq->pmem.src[i].len; |
| 1420 | |
| 1421 | if (creq->pmem.src[i].len == 0) |
| 1422 | i++; |
| 1423 | } |
| 1424 | |
| 1425 | } /* end of while ((i < req.entries) && (err == 0)) */ |
| 1426 | |
| 1427 | } else |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1428 | err = qcedev_pmem_ablk_cipher_max_xfer(qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1429 | |
| 1430 | /* Restore the original req structure */ |
| 1431 | for (i = 0; i < saved_req->entries; i++) { |
| 1432 | creq->pmem.src[i].len = saved_req->pmem.src[i].len; |
| 1433 | creq->pmem.src[i].offset = saved_req->pmem.src[i].offset; |
| 1434 | } |
| 1435 | creq->entries = saved_req->entries; |
| 1436 | creq->data_len = saved_req->data_len; |
| 1437 | kfree(saved_req); |
| 1438 | |
| 1439 | return err; |
| 1440 | |
| 1441 | } |
Ramesh Masavarapu | fa679d9 | 2011-10-13 23:42:59 -0700 | [diff] [blame] | 1442 | #else |
Ramesh Masavarapu | fa679d9 | 2011-10-13 23:42:59 -0700 | [diff] [blame] | 1443 | static int qcedev_pmem_ablk_cipher(struct qcedev_async_req *qcedev_areq, |
| 1444 | struct qcedev_handle *handle) |
| 1445 | { |
| 1446 | return -EPERM; |
| 1447 | } |
| 1448 | #endif/*CONFIG_ANDROID_PMEM*/ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1449 | |
| 1450 | static int qcedev_vbuf_ablk_cipher_max_xfer(struct qcedev_async_req *areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1451 | int *di, struct qcedev_handle *handle, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1452 | uint8_t *k_align_src) |
| 1453 | { |
| 1454 | int err = 0; |
| 1455 | int i = 0; |
| 1456 | int dst_i = *di; |
| 1457 | struct scatterlist sg_src; |
| 1458 | uint32_t byteoffset = 0; |
| 1459 | uint8_t *user_src = NULL; |
| 1460 | uint8_t *k_align_dst = k_align_src; |
| 1461 | struct qcedev_cipher_op_req *creq = &areq->cipher_op_req; |
| 1462 | |
| 1463 | |
| 1464 | if (areq->cipher_op_req.mode == QCEDEV_AES_MODE_CTR) |
| 1465 | byteoffset = areq->cipher_op_req.byteoffset; |
| 1466 | |
| 1467 | user_src = (void __user *)areq->cipher_op_req.vbuf.src[0].vaddr; |
| 1468 | if (user_src && __copy_from_user((k_align_src + byteoffset), |
| 1469 | (void __user *)user_src, |
| 1470 | areq->cipher_op_req.vbuf.src[0].len)) |
| 1471 | return -EFAULT; |
| 1472 | |
| 1473 | k_align_src += areq->cipher_op_req.vbuf.src[0].len; |
| 1474 | |
| 1475 | for (i = 1; i < areq->cipher_op_req.entries; i++) { |
| 1476 | user_src = |
| 1477 | (void __user *)areq->cipher_op_req.vbuf.src[i].vaddr; |
| 1478 | if (user_src && __copy_from_user(k_align_src, |
| 1479 | (void __user *)user_src, |
| 1480 | areq->cipher_op_req.vbuf.src[i].len)) { |
| 1481 | return -EFAULT; |
| 1482 | } |
| 1483 | k_align_src += areq->cipher_op_req.vbuf.src[i].len; |
| 1484 | } |
| 1485 | |
| 1486 | /* restore src beginning */ |
| 1487 | k_align_src = k_align_dst; |
| 1488 | areq->cipher_op_req.data_len += byteoffset; |
| 1489 | |
| 1490 | areq->cipher_req.creq.src = (struct scatterlist *) &sg_src; |
| 1491 | areq->cipher_req.creq.dst = (struct scatterlist *) &sg_src; |
| 1492 | |
| 1493 | /* In place encryption/decryption */ |
| 1494 | sg_set_buf(areq->cipher_req.creq.src, |
| 1495 | k_align_dst, |
| 1496 | areq->cipher_op_req.data_len); |
| 1497 | sg_mark_end(areq->cipher_req.creq.src); |
| 1498 | |
| 1499 | areq->cipher_req.creq.nbytes = areq->cipher_op_req.data_len; |
| 1500 | areq->cipher_req.creq.info = areq->cipher_op_req.iv; |
| 1501 | areq->cipher_op_req.entries = 1; |
| 1502 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1503 | err = submit_req(areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1504 | |
| 1505 | /* copy data to destination buffer*/ |
| 1506 | creq->data_len -= byteoffset; |
| 1507 | |
| 1508 | while (creq->data_len > 0) { |
| 1509 | if (creq->vbuf.dst[dst_i].len <= creq->data_len) { |
| 1510 | if (err == 0 && __copy_to_user( |
| 1511 | (void __user *)creq->vbuf.dst[dst_i].vaddr, |
| 1512 | (k_align_dst + byteoffset), |
| 1513 | creq->vbuf.dst[dst_i].len)) |
| 1514 | return -EFAULT; |
| 1515 | |
| 1516 | k_align_dst += creq->vbuf.dst[dst_i].len + |
| 1517 | byteoffset; |
| 1518 | creq->data_len -= creq->vbuf.dst[dst_i].len; |
| 1519 | dst_i++; |
| 1520 | } else { |
| 1521 | if (err == 0 && __copy_to_user( |
| 1522 | (void __user *)creq->vbuf.dst[dst_i].vaddr, |
| 1523 | (k_align_dst + byteoffset), |
| 1524 | creq->data_len)) |
| 1525 | return -EFAULT; |
| 1526 | |
| 1527 | k_align_dst += creq->data_len; |
| 1528 | creq->vbuf.dst[dst_i].len -= creq->data_len; |
| 1529 | creq->vbuf.dst[dst_i].vaddr += creq->data_len; |
| 1530 | creq->data_len = 0; |
| 1531 | } |
| 1532 | } |
| 1533 | *di = dst_i; |
| 1534 | |
| 1535 | return err; |
| 1536 | }; |
| 1537 | |
| 1538 | static int qcedev_vbuf_ablk_cipher(struct qcedev_async_req *areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1539 | struct qcedev_handle *handle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1540 | { |
| 1541 | int err = 0; |
| 1542 | int di = 0; |
| 1543 | int i = 0; |
| 1544 | int j = 0; |
| 1545 | int k = 0; |
| 1546 | uint32_t byteoffset = 0; |
| 1547 | int num_entries = 0; |
| 1548 | uint32_t total = 0; |
| 1549 | uint32_t len; |
| 1550 | uint8_t *k_buf_src = NULL; |
| 1551 | uint8_t *k_align_src = NULL; |
| 1552 | uint32_t max_data_xfer; |
| 1553 | struct qcedev_cipher_op_req *saved_req; |
| 1554 | struct qcedev_cipher_op_req *creq = &areq->cipher_op_req; |
| 1555 | |
| 1556 | /* Verify Source Address's */ |
| 1557 | for (i = 0; i < areq->cipher_op_req.entries; i++) |
| 1558 | if (!access_ok(VERIFY_READ, |
| 1559 | (void __user *)areq->cipher_op_req.vbuf.src[i].vaddr, |
| 1560 | areq->cipher_op_req.vbuf.src[i].len)) |
| 1561 | return -EFAULT; |
| 1562 | |
| 1563 | /* Verify Destination Address's */ |
| 1564 | if (areq->cipher_op_req.in_place_op != 1) |
| 1565 | for (i = 0; i < areq->cipher_op_req.entries; i++) |
| 1566 | if (!access_ok(VERIFY_READ, |
| 1567 | (void __user *)areq->cipher_op_req.vbuf.dst[i].vaddr, |
| 1568 | areq->cipher_op_req.vbuf.dst[i].len)) |
| 1569 | return -EFAULT; |
| 1570 | |
| 1571 | if (areq->cipher_op_req.mode == QCEDEV_AES_MODE_CTR) |
| 1572 | byteoffset = areq->cipher_op_req.byteoffset; |
| 1573 | k_buf_src = kmalloc(QCE_MAX_OPER_DATA + CACHE_LINE_SIZE * 2, |
| 1574 | GFP_KERNEL); |
| 1575 | if (k_buf_src == NULL) { |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 1576 | pr_err("%s: Can't Allocate memory: k_buf_src 0x%x\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1577 | __func__, (uint32_t)k_buf_src); |
| 1578 | return -ENOMEM; |
| 1579 | } |
| 1580 | k_align_src = (uint8_t *) ALIGN(((unsigned int)k_buf_src), |
| 1581 | CACHE_LINE_SIZE); |
| 1582 | max_data_xfer = QCE_MAX_OPER_DATA - byteoffset; |
| 1583 | |
| 1584 | saved_req = kmalloc(sizeof(struct qcedev_cipher_op_req), GFP_KERNEL); |
| 1585 | if (saved_req == NULL) { |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 1586 | pr_err("%s: Can't Allocate memory:saved_req 0x%x\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1587 | __func__, (uint32_t)saved_req); |
| 1588 | kfree(k_buf_src); |
| 1589 | return -ENOMEM; |
| 1590 | |
| 1591 | } |
| 1592 | memcpy(saved_req, creq, sizeof(struct qcedev_cipher_op_req)); |
| 1593 | |
| 1594 | if (areq->cipher_op_req.data_len > max_data_xfer) { |
| 1595 | struct qcedev_cipher_op_req req; |
| 1596 | |
| 1597 | /* save the original req structure */ |
| 1598 | memcpy(&req, creq, sizeof(struct qcedev_cipher_op_req)); |
| 1599 | |
| 1600 | i = 0; |
| 1601 | /* Address 32 KB at a time */ |
| 1602 | while ((i < req.entries) && (err == 0)) { |
| 1603 | if (creq->vbuf.src[i].len > max_data_xfer) { |
| 1604 | creq->vbuf.src[0].len = max_data_xfer; |
| 1605 | if (i > 0) { |
| 1606 | creq->vbuf.src[0].vaddr = |
| 1607 | creq->vbuf.src[i].vaddr; |
| 1608 | } |
| 1609 | |
| 1610 | creq->data_len = max_data_xfer; |
| 1611 | creq->entries = 1; |
| 1612 | |
| 1613 | err = qcedev_vbuf_ablk_cipher_max_xfer(areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1614 | &di, handle, k_align_src); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1615 | if (err < 0) { |
| 1616 | kfree(k_buf_src); |
| 1617 | kfree(saved_req); |
| 1618 | return err; |
| 1619 | } |
| 1620 | |
| 1621 | creq->vbuf.src[i].len = req.vbuf.src[i].len - |
| 1622 | max_data_xfer; |
| 1623 | creq->vbuf.src[i].vaddr = |
| 1624 | req.vbuf.src[i].vaddr + |
| 1625 | max_data_xfer; |
| 1626 | req.vbuf.src[i].vaddr = |
| 1627 | creq->vbuf.src[i].vaddr; |
| 1628 | req.vbuf.src[i].len = creq->vbuf.src[i].len; |
| 1629 | |
| 1630 | } else { |
| 1631 | total = areq->cipher_op_req.byteoffset; |
| 1632 | for (j = i; j < req.entries; j++) { |
| 1633 | num_entries++; |
| 1634 | if ((total + creq->vbuf.src[j].len) |
| 1635 | >= max_data_xfer) { |
| 1636 | creq->vbuf.src[j].len = |
| 1637 | max_data_xfer - total; |
| 1638 | total = max_data_xfer; |
| 1639 | break; |
| 1640 | } |
| 1641 | total += creq->vbuf.src[j].len; |
| 1642 | } |
| 1643 | |
| 1644 | creq->data_len = total; |
| 1645 | if (i > 0) |
| 1646 | for (k = 0; k < num_entries; k++) { |
| 1647 | creq->vbuf.src[k].len = |
| 1648 | creq->vbuf.src[i+k].len; |
| 1649 | creq->vbuf.src[k].vaddr = |
| 1650 | creq->vbuf.src[i+k].vaddr; |
| 1651 | } |
| 1652 | creq->entries = num_entries; |
| 1653 | |
| 1654 | i = j; |
| 1655 | err = qcedev_vbuf_ablk_cipher_max_xfer(areq, |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1656 | &di, handle, k_align_src); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1657 | if (err < 0) { |
| 1658 | kfree(k_buf_src); |
| 1659 | kfree(saved_req); |
| 1660 | return err; |
| 1661 | } |
| 1662 | |
| 1663 | num_entries = 0; |
| 1664 | areq->cipher_op_req.byteoffset = 0; |
| 1665 | |
| 1666 | creq->vbuf.src[i].vaddr = req.vbuf.src[i].vaddr |
| 1667 | + creq->vbuf.src[i].len; |
| 1668 | creq->vbuf.src[i].len = req.vbuf.src[i].len - |
| 1669 | creq->vbuf.src[i].len; |
| 1670 | |
| 1671 | req.vbuf.src[i].vaddr = |
| 1672 | creq->vbuf.src[i].vaddr; |
| 1673 | req.vbuf.src[i].len = creq->vbuf.src[i].len; |
| 1674 | |
| 1675 | if (creq->vbuf.src[i].len == 0) |
| 1676 | i++; |
| 1677 | } |
| 1678 | |
| 1679 | areq->cipher_op_req.byteoffset = 0; |
| 1680 | max_data_xfer = QCE_MAX_OPER_DATA; |
| 1681 | byteoffset = 0; |
| 1682 | |
| 1683 | } /* end of while ((i < req.entries) && (err == 0)) */ |
| 1684 | } else |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1685 | err = qcedev_vbuf_ablk_cipher_max_xfer(areq, &di, handle, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1686 | k_align_src); |
| 1687 | |
| 1688 | /* Restore the original req structure */ |
| 1689 | for (i = 0; i < saved_req->entries; i++) { |
| 1690 | creq->vbuf.src[i].len = saved_req->vbuf.src[i].len; |
| 1691 | creq->vbuf.src[i].vaddr = saved_req->vbuf.src[i].vaddr; |
| 1692 | } |
| 1693 | for (len = 0, i = 0; len < saved_req->data_len; i++) { |
| 1694 | creq->vbuf.dst[i].len = saved_req->vbuf.dst[i].len; |
| 1695 | creq->vbuf.dst[i].vaddr = saved_req->vbuf.dst[i].vaddr; |
| 1696 | len += saved_req->vbuf.dst[i].len; |
| 1697 | } |
| 1698 | creq->entries = saved_req->entries; |
| 1699 | creq->data_len = saved_req->data_len; |
| 1700 | creq->byteoffset = saved_req->byteoffset; |
| 1701 | |
| 1702 | kfree(saved_req); |
| 1703 | kfree(k_buf_src); |
| 1704 | return err; |
| 1705 | |
| 1706 | } |
| 1707 | |
| 1708 | static int qcedev_check_cipher_params(struct qcedev_cipher_op_req *req, |
| 1709 | struct qcedev_control *podev) |
| 1710 | { |
| 1711 | if ((req->entries == 0) || (req->data_len == 0)) |
| 1712 | goto error; |
| 1713 | if ((req->alg >= QCEDEV_ALG_LAST) || |
| 1714 | (req->mode >= QCEDEV_AES_DES_MODE_LAST)) |
| 1715 | goto error; |
| 1716 | if (req->alg == QCEDEV_ALG_AES) { |
| 1717 | if ((req->mode == QCEDEV_AES_MODE_XTS) && |
| 1718 | (!podev->ce_support.aes_xts)) |
| 1719 | goto error; |
| 1720 | /* if intending to use HW key make sure key fields are set |
| 1721 | * correctly and HW key is indeed supported in target |
| 1722 | */ |
| 1723 | if (req->encklen == 0) { |
| 1724 | int i; |
| 1725 | for (i = 0; i < QCEDEV_MAX_KEY_SIZE; i++) |
| 1726 | if (req->enckey[i]) |
| 1727 | goto error; |
| 1728 | if ((req->op != QCEDEV_OPER_ENC_NO_KEY) && |
| 1729 | (req->op != QCEDEV_OPER_DEC_NO_KEY)) |
| 1730 | if (!podev->platform_support.hw_key_support) |
| 1731 | goto error; |
| 1732 | } else { |
| 1733 | if (req->encklen == QCEDEV_AES_KEY_192) { |
| 1734 | if (!podev->ce_support.aes_key_192) |
| 1735 | goto error; |
| 1736 | } else { |
| 1737 | /* if not using HW key make sure key |
| 1738 | * length is valid |
| 1739 | */ |
| 1740 | if (!((req->encklen == QCEDEV_AES_KEY_128) || |
| 1741 | (req->encklen == QCEDEV_AES_KEY_256))) |
| 1742 | goto error; |
| 1743 | } |
| 1744 | } |
| 1745 | } |
| 1746 | /* if using a byteoffset, make sure it is CTR mode using vbuf */ |
| 1747 | if (req->byteoffset) { |
| 1748 | if (req->mode != QCEDEV_AES_MODE_CTR) |
| 1749 | goto error; |
| 1750 | else { /* if using CTR mode make sure not using Pmem */ |
| 1751 | if (req->use_pmem) |
| 1752 | goto error; |
| 1753 | } |
| 1754 | } |
| 1755 | /* if using PMEM with non-zero byteoffset, ensure it is in_place_op */ |
| 1756 | if (req->use_pmem) { |
| 1757 | if (!req->in_place_op) |
| 1758 | goto error; |
| 1759 | } |
| 1760 | /* Ensure zer ivlen for ECB mode */ |
| 1761 | if (req->ivlen != 0) { |
| 1762 | if ((req->mode == QCEDEV_AES_MODE_ECB) || |
| 1763 | (req->mode == QCEDEV_DES_MODE_ECB)) |
| 1764 | goto error; |
| 1765 | } else { |
| 1766 | if ((req->mode != QCEDEV_AES_MODE_ECB) && |
| 1767 | (req->mode != QCEDEV_DES_MODE_ECB)) |
| 1768 | goto error; |
| 1769 | } |
| 1770 | |
| 1771 | return 0; |
| 1772 | error: |
| 1773 | return -EINVAL; |
| 1774 | |
| 1775 | } |
| 1776 | |
| 1777 | static int qcedev_check_sha_params(struct qcedev_sha_op_req *req, |
| 1778 | struct qcedev_control *podev) |
| 1779 | { |
| 1780 | if ((req->alg == QCEDEV_ALG_AES_CMAC) && |
| 1781 | (!podev->ce_support.cmac)) |
| 1782 | goto sha_error; |
| 1783 | |
| 1784 | if ((req->entries == 0) || (req->data_len == 0)) |
| 1785 | goto sha_error; |
| 1786 | |
| 1787 | if (req->alg >= QCEDEV_ALG_SHA_ALG_LAST) |
| 1788 | goto sha_error; |
| 1789 | |
| 1790 | return 0; |
| 1791 | sha_error: |
| 1792 | return -EINVAL; |
| 1793 | } |
| 1794 | |
| 1795 | static long qcedev_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
| 1796 | { |
| 1797 | int err = 0; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1798 | struct qcedev_handle *handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1799 | struct qcedev_control *podev; |
| 1800 | struct qcedev_async_req qcedev_areq; |
| 1801 | struct qcedev_stat *pstat; |
| 1802 | |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1803 | handle = file->private_data; |
| 1804 | podev = handle->cntl; |
| 1805 | qcedev_areq.handle = handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1806 | if (podev == NULL || podev->magic != QCEDEV_MAGIC) { |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 1807 | pr_err("%s: invalid handle %p\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1808 | __func__, podev); |
| 1809 | return -ENOENT; |
| 1810 | } |
| 1811 | |
| 1812 | /* Verify user arguments. */ |
| 1813 | if (_IOC_TYPE(cmd) != QCEDEV_IOC_MAGIC) |
| 1814 | return -ENOTTY; |
| 1815 | |
| 1816 | init_completion(&qcedev_areq.complete); |
| 1817 | pstat = &_qcedev_stat[podev->pdev->id]; |
| 1818 | |
| 1819 | switch (cmd) { |
| 1820 | case QCEDEV_IOCTL_LOCK_CE: |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 1821 | if (podev->platform_support.ce_shared) |
| 1822 | err = qcedev_lock_ce(podev); |
| 1823 | else |
| 1824 | err = -ENOTTY; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1825 | break; |
| 1826 | case QCEDEV_IOCTL_UNLOCK_CE: |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 1827 | if (podev->platform_support.ce_shared) |
| 1828 | err = qcedev_unlock_ce(podev); |
| 1829 | else |
| 1830 | err = -ENOTTY; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1831 | break; |
| 1832 | case QCEDEV_IOCTL_ENC_REQ: |
| 1833 | case QCEDEV_IOCTL_DEC_REQ: |
| 1834 | if (!access_ok(VERIFY_WRITE, (void __user *)arg, |
| 1835 | sizeof(struct qcedev_cipher_op_req))) |
| 1836 | return -EFAULT; |
| 1837 | |
| 1838 | if (__copy_from_user(&qcedev_areq.cipher_op_req, |
| 1839 | (void __user *)arg, |
| 1840 | sizeof(struct qcedev_cipher_op_req))) |
| 1841 | return -EFAULT; |
| 1842 | qcedev_areq.op_type = QCEDEV_CRYPTO_OPER_CIPHER; |
| 1843 | |
| 1844 | if (qcedev_check_cipher_params(&qcedev_areq.cipher_op_req, |
| 1845 | podev)) |
| 1846 | return -EINVAL; |
| 1847 | |
Ramesh Masavarapu | a63ff1e | 2011-10-20 10:51:25 -0700 | [diff] [blame^] | 1848 | if (qcedev_areq.cipher_op_req.use_pmem) |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1849 | err = qcedev_pmem_ablk_cipher(&qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1850 | else |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1851 | err = qcedev_vbuf_ablk_cipher(&qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1852 | if (err) |
| 1853 | return err; |
| 1854 | if (__copy_to_user((void __user *)arg, |
| 1855 | &qcedev_areq.cipher_op_req, |
| 1856 | sizeof(struct qcedev_cipher_op_req))) |
| 1857 | return -EFAULT; |
| 1858 | break; |
| 1859 | |
| 1860 | case QCEDEV_IOCTL_SHA_INIT_REQ: |
| 1861 | |
| 1862 | if (!access_ok(VERIFY_WRITE, (void __user *)arg, |
| 1863 | sizeof(struct qcedev_sha_op_req))) |
| 1864 | return -EFAULT; |
| 1865 | |
| 1866 | if (__copy_from_user(&qcedev_areq.sha_op_req, |
| 1867 | (void __user *)arg, |
| 1868 | sizeof(struct qcedev_sha_op_req))) |
| 1869 | return -EFAULT; |
| 1870 | if (qcedev_check_sha_params(&qcedev_areq.sha_op_req, podev)) |
| 1871 | return -EINVAL; |
| 1872 | qcedev_areq.op_type = QCEDEV_CRYPTO_OPER_SHA; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1873 | err = qcedev_hash_init(&qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1874 | if (err) |
| 1875 | return err; |
| 1876 | if (__copy_to_user((void __user *)arg, &qcedev_areq.sha_op_req, |
| 1877 | sizeof(struct qcedev_sha_op_req))) |
| 1878 | return -EFAULT; |
| 1879 | break; |
| 1880 | case QCEDEV_IOCTL_GET_CMAC_REQ: |
| 1881 | if (!podev->ce_support.cmac) |
| 1882 | return -ENOTTY; |
| 1883 | case QCEDEV_IOCTL_SHA_UPDATE_REQ: |
| 1884 | if (!access_ok(VERIFY_WRITE, (void __user *)arg, |
| 1885 | sizeof(struct qcedev_sha_op_req))) |
| 1886 | return -EFAULT; |
| 1887 | |
| 1888 | if (__copy_from_user(&qcedev_areq.sha_op_req, |
| 1889 | (void __user *)arg, |
| 1890 | sizeof(struct qcedev_sha_op_req))) |
| 1891 | return -EFAULT; |
| 1892 | if (qcedev_check_sha_params(&qcedev_areq.sha_op_req, podev)) |
| 1893 | return -EINVAL; |
| 1894 | qcedev_areq.op_type = QCEDEV_CRYPTO_OPER_SHA; |
| 1895 | |
| 1896 | if (qcedev_areq.sha_op_req.alg == QCEDEV_ALG_AES_CMAC) { |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1897 | err = qcedev_hash_cmac(&qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1898 | if (err) |
| 1899 | return err; |
| 1900 | } else { |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1901 | err = qcedev_hash_update(&qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1902 | if (err) |
| 1903 | return err; |
| 1904 | } |
| 1905 | |
| 1906 | memcpy(&qcedev_areq.sha_op_req.digest[0], |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1907 | &handle->sha_ctxt.digest[0], |
| 1908 | handle->sha_ctxt.diglen); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1909 | if (__copy_to_user((void __user *)arg, &qcedev_areq.sha_op_req, |
| 1910 | sizeof(struct qcedev_sha_op_req))) |
| 1911 | return -EFAULT; |
| 1912 | break; |
| 1913 | |
| 1914 | case QCEDEV_IOCTL_SHA_FINAL_REQ: |
| 1915 | |
| 1916 | if (!access_ok(VERIFY_WRITE, (void __user *)arg, |
| 1917 | sizeof(struct qcedev_sha_op_req))) |
| 1918 | return -EFAULT; |
| 1919 | |
| 1920 | if (__copy_from_user(&qcedev_areq.sha_op_req, |
| 1921 | (void __user *)arg, |
| 1922 | sizeof(struct qcedev_sha_op_req))) |
| 1923 | return -EFAULT; |
| 1924 | if (qcedev_check_sha_params(&qcedev_areq.sha_op_req, podev)) |
| 1925 | return -EINVAL; |
| 1926 | qcedev_areq.op_type = QCEDEV_CRYPTO_OPER_SHA; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1927 | err = qcedev_hash_final(&qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1928 | if (err) |
| 1929 | return err; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1930 | qcedev_areq.sha_op_req.diglen = handle->sha_ctxt.diglen; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1931 | memcpy(&qcedev_areq.sha_op_req.digest[0], |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1932 | &handle->sha_ctxt.digest[0], |
| 1933 | handle->sha_ctxt.diglen); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1934 | if (__copy_to_user((void __user *)arg, &qcedev_areq.sha_op_req, |
| 1935 | sizeof(struct qcedev_sha_op_req))) |
| 1936 | return -EFAULT; |
| 1937 | break; |
| 1938 | |
| 1939 | case QCEDEV_IOCTL_GET_SHA_REQ: |
| 1940 | |
| 1941 | if (!access_ok(VERIFY_WRITE, (void __user *)arg, |
| 1942 | sizeof(struct qcedev_sha_op_req))) |
| 1943 | return -EFAULT; |
| 1944 | |
| 1945 | if (__copy_from_user(&qcedev_areq.sha_op_req, |
| 1946 | (void __user *)arg, |
| 1947 | sizeof(struct qcedev_sha_op_req))) |
| 1948 | return -EFAULT; |
| 1949 | if (qcedev_check_sha_params(&qcedev_areq.sha_op_req, podev)) |
| 1950 | return -EINVAL; |
| 1951 | qcedev_areq.op_type = QCEDEV_CRYPTO_OPER_SHA; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1952 | qcedev_hash_init(&qcedev_areq, handle); |
| 1953 | err = qcedev_hash_update(&qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1954 | if (err) |
| 1955 | return err; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1956 | err = qcedev_hash_final(&qcedev_areq, handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1957 | if (err) |
| 1958 | return err; |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1959 | qcedev_areq.sha_op_req.diglen = handle->sha_ctxt.diglen; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1960 | memcpy(&qcedev_areq.sha_op_req.digest[0], |
Mona Hossain | 087c60b | 2011-07-20 10:34:57 -0700 | [diff] [blame] | 1961 | &handle->sha_ctxt.digest[0], |
| 1962 | handle->sha_ctxt.diglen); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1963 | if (__copy_to_user((void __user *)arg, &qcedev_areq.sha_op_req, |
| 1964 | sizeof(struct qcedev_sha_op_req))) |
| 1965 | return -EFAULT; |
| 1966 | break; |
| 1967 | |
| 1968 | default: |
| 1969 | return -ENOTTY; |
| 1970 | } |
| 1971 | |
| 1972 | return err; |
| 1973 | } |
| 1974 | |
| 1975 | static int qcedev_probe(struct platform_device *pdev) |
| 1976 | { |
| 1977 | void *handle = NULL; |
| 1978 | int rc = 0; |
| 1979 | struct qcedev_control *podev; |
| 1980 | struct msm_ce_hw_support *platform_support; |
| 1981 | |
| 1982 | if (pdev->id >= MAX_QCE_DEVICE) { |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 1983 | pr_err("%s: device id %d exceeds allowed %d\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1984 | __func__, pdev->id, MAX_QCE_DEVICE); |
| 1985 | return -ENOENT; |
| 1986 | } |
| 1987 | podev = &qce_dev[pdev->id]; |
| 1988 | |
| 1989 | platform_support = (struct msm_ce_hw_support *)pdev->dev.platform_data; |
| 1990 | podev->platform_support.ce_shared = platform_support->ce_shared; |
| 1991 | podev->platform_support.shared_ce_resource = |
| 1992 | platform_support->shared_ce_resource; |
| 1993 | podev->platform_support.hw_key_support = |
| 1994 | platform_support->hw_key_support; |
Mona Hossain | 650c22c | 2011-07-19 09:54:19 -0700 | [diff] [blame] | 1995 | podev->ce_lock_count = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1996 | INIT_LIST_HEAD(&podev->ready_commands); |
| 1997 | podev->active_command = NULL; |
| 1998 | |
| 1999 | spin_lock_init(&podev->lock); |
| 2000 | |
| 2001 | tasklet_init(&podev->done_tasklet, req_done, (unsigned long)podev); |
| 2002 | |
| 2003 | /* open qce */ |
| 2004 | handle = qce_open(pdev, &rc); |
| 2005 | if (handle == NULL) { |
| 2006 | platform_set_drvdata(pdev, NULL); |
| 2007 | return rc; |
| 2008 | } |
| 2009 | |
| 2010 | podev->qce = handle; |
| 2011 | podev->pdev = pdev; |
| 2012 | platform_set_drvdata(pdev, podev); |
| 2013 | qce_hw_support(podev->qce, &podev->ce_support); |
| 2014 | rc = misc_register(&podev->miscdevice); |
| 2015 | |
| 2016 | if (rc >= 0) |
| 2017 | return 0; |
| 2018 | |
| 2019 | if (handle) |
| 2020 | qce_close(handle); |
| 2021 | platform_set_drvdata(pdev, NULL); |
| 2022 | podev->qce = NULL; |
| 2023 | podev->pdev = NULL; |
| 2024 | return rc; |
| 2025 | }; |
| 2026 | |
| 2027 | static int qcedev_remove(struct platform_device *pdev) |
| 2028 | { |
| 2029 | struct qcedev_control *podev; |
| 2030 | |
| 2031 | podev = platform_get_drvdata(pdev); |
| 2032 | if (!podev) |
| 2033 | return 0; |
| 2034 | if (podev->qce) |
| 2035 | qce_close(podev->qce); |
| 2036 | |
| 2037 | if (podev->miscdevice.minor != MISC_DYNAMIC_MINOR) |
| 2038 | misc_deregister(&podev->miscdevice); |
| 2039 | tasklet_kill(&podev->done_tasklet); |
| 2040 | return 0; |
| 2041 | }; |
| 2042 | |
| 2043 | static struct platform_driver qcedev_plat_driver = { |
| 2044 | .probe = qcedev_probe, |
| 2045 | .remove = qcedev_remove, |
| 2046 | .driver = { |
| 2047 | .name = "qce", |
| 2048 | .owner = THIS_MODULE, |
| 2049 | }, |
| 2050 | }; |
| 2051 | |
| 2052 | static int _disp_stats(int id) |
| 2053 | { |
| 2054 | struct qcedev_stat *pstat; |
| 2055 | int len = 0; |
| 2056 | |
| 2057 | pstat = &_qcedev_stat[id]; |
| 2058 | len = snprintf(_debug_read_buf, DEBUG_MAX_RW_BUF - 1, |
| 2059 | "\nQualcomm QCE dev driver %d Statistics:\n", |
| 2060 | id + 1); |
| 2061 | |
| 2062 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 2063 | " Encryption operation success : %d\n", |
| 2064 | pstat->qcedev_enc_success); |
| 2065 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 2066 | " Encryption operation fail : %d\n", |
| 2067 | pstat->qcedev_enc_fail); |
| 2068 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 2069 | " Decryption operation success : %d\n", |
| 2070 | pstat->qcedev_dec_success); |
| 2071 | |
| 2072 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 2073 | " Encryption operation fail : %d\n", |
| 2074 | pstat->qcedev_dec_fail); |
| 2075 | |
| 2076 | return len; |
| 2077 | } |
| 2078 | |
| 2079 | static int _debug_stats_open(struct inode *inode, struct file *file) |
| 2080 | { |
| 2081 | file->private_data = inode->i_private; |
| 2082 | return 0; |
| 2083 | } |
| 2084 | |
| 2085 | static ssize_t _debug_stats_read(struct file *file, char __user *buf, |
| 2086 | size_t count, loff_t *ppos) |
| 2087 | { |
| 2088 | int rc = -EINVAL; |
| 2089 | int qcedev = *((int *) file->private_data); |
| 2090 | int len; |
| 2091 | |
| 2092 | len = _disp_stats(qcedev); |
| 2093 | |
| 2094 | rc = simple_read_from_buffer((void __user *) buf, len, |
| 2095 | ppos, (void *) _debug_read_buf, len); |
| 2096 | |
| 2097 | return rc; |
| 2098 | } |
| 2099 | |
| 2100 | static ssize_t _debug_stats_write(struct file *file, const char __user *buf, |
| 2101 | size_t count, loff_t *ppos) |
| 2102 | { |
| 2103 | |
| 2104 | int qcedev = *((int *) file->private_data); |
| 2105 | |
| 2106 | memset((char *)&_qcedev_stat[qcedev], 0, sizeof(struct qcedev_stat)); |
| 2107 | return count; |
| 2108 | }; |
| 2109 | |
| 2110 | static const struct file_operations _debug_stats_ops = { |
| 2111 | .open = _debug_stats_open, |
| 2112 | .read = _debug_stats_read, |
| 2113 | .write = _debug_stats_write, |
| 2114 | }; |
| 2115 | |
| 2116 | static int _qcedev_debug_init(void) |
| 2117 | { |
| 2118 | int rc; |
| 2119 | char name[DEBUG_MAX_FNAME]; |
| 2120 | int i; |
| 2121 | struct dentry *dent; |
| 2122 | |
| 2123 | _debug_dent = debugfs_create_dir("qcedev", NULL); |
| 2124 | if (IS_ERR(_debug_dent)) { |
| 2125 | pr_err("qcedev debugfs_create_dir fail, error %ld\n", |
| 2126 | PTR_ERR(_debug_dent)); |
| 2127 | return PTR_ERR(_debug_dent); |
| 2128 | } |
| 2129 | |
| 2130 | for (i = 0; i < MAX_QCE_DEVICE; i++) { |
| 2131 | snprintf(name, DEBUG_MAX_FNAME-1, "stats-%d", i+1); |
| 2132 | _debug_qcedev[i] = i; |
| 2133 | dent = debugfs_create_file(name, 0644, _debug_dent, |
| 2134 | &_debug_qcedev[i], &_debug_stats_ops); |
| 2135 | if (dent == NULL) { |
| 2136 | pr_err("qcedev debugfs_create_file fail, error %ld\n", |
| 2137 | PTR_ERR(dent)); |
| 2138 | rc = PTR_ERR(dent); |
| 2139 | goto err; |
| 2140 | } |
| 2141 | } |
| 2142 | return 0; |
| 2143 | err: |
| 2144 | debugfs_remove_recursive(_debug_dent); |
| 2145 | return rc; |
| 2146 | } |
| 2147 | |
| 2148 | static int qcedev_init(void) |
| 2149 | { |
| 2150 | int rc; |
| 2151 | |
| 2152 | rc = _qcedev_debug_init(); |
| 2153 | if (rc) |
| 2154 | return rc; |
| 2155 | return platform_driver_register(&qcedev_plat_driver); |
| 2156 | } |
| 2157 | |
| 2158 | static void qcedev_exit(void) |
| 2159 | { |
| 2160 | debugfs_remove_recursive(_debug_dent); |
| 2161 | platform_driver_unregister(&qcedev_plat_driver); |
| 2162 | } |
| 2163 | |
| 2164 | MODULE_LICENSE("GPL v2"); |
| 2165 | MODULE_AUTHOR("Mona Hossain <mhossain@codeaurora.org>"); |
| 2166 | MODULE_DESCRIPTION("Qualcomm DEV Crypto driver"); |
Ramesh Masavarapu | fa679d9 | 2011-10-13 23:42:59 -0700 | [diff] [blame] | 2167 | MODULE_VERSION("1.24"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2168 | |
| 2169 | module_init(qcedev_init); |
| 2170 | module_exit(qcedev_exit); |