| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Microchip / Atmel ECC (I2C) driver. |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) 2017, Microchip Technology Inc. |
|---|
| 5 | 6 | * Author: Tudor Ambarus <tudor.ambarus@microchip.com> |
|---|
| 6 | | - * |
|---|
| 7 | | - * This software is licensed under the terms of the GNU General Public |
|---|
| 8 | | - * License version 2, as published by the Free Software Foundation, and |
|---|
| 9 | | - * may be copied, distributed, and modified under those terms. |
|---|
| 10 | | - * |
|---|
| 11 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 12 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | | - * GNU General Public License for more details. |
|---|
| 15 | | - * |
|---|
| 16 | 7 | */ |
|---|
| 17 | 8 | |
|---|
| 18 | | -#include <linux/bitrev.h> |
|---|
| 19 | | -#include <linux/crc16.h> |
|---|
| 20 | 9 | #include <linux/delay.h> |
|---|
| 21 | 10 | #include <linux/device.h> |
|---|
| 22 | 11 | #include <linux/err.h> |
|---|
| .. | .. |
|---|
| 32 | 21 | #include <crypto/internal/kpp.h> |
|---|
| 33 | 22 | #include <crypto/ecdh.h> |
|---|
| 34 | 23 | #include <crypto/kpp.h> |
|---|
| 35 | | -#include "atmel-ecc.h" |
|---|
| 36 | | - |
|---|
| 37 | | -/* Used for binding tfm objects to i2c clients. */ |
|---|
| 38 | | -struct atmel_ecc_driver_data { |
|---|
| 39 | | - struct list_head i2c_client_list; |
|---|
| 40 | | - spinlock_t i2c_list_lock; |
|---|
| 41 | | -} ____cacheline_aligned; |
|---|
| 24 | +#include "atmel-i2c.h" |
|---|
| 42 | 25 | |
|---|
| 43 | 26 | static struct atmel_ecc_driver_data driver_data; |
|---|
| 44 | | - |
|---|
| 45 | | -/** |
|---|
| 46 | | - * atmel_ecc_i2c_client_priv - i2c_client private data |
|---|
| 47 | | - * @client : pointer to i2c client device |
|---|
| 48 | | - * @i2c_client_list_node: part of i2c_client_list |
|---|
| 49 | | - * @lock : lock for sending i2c commands |
|---|
| 50 | | - * @wake_token : wake token array of zeros |
|---|
| 51 | | - * @wake_token_sz : size in bytes of the wake_token |
|---|
| 52 | | - * @tfm_count : number of active crypto transformations on i2c client |
|---|
| 53 | | - * |
|---|
| 54 | | - * Reads and writes from/to the i2c client are sequential. The first byte |
|---|
| 55 | | - * transmitted to the device is treated as the byte size. Any attempt to send |
|---|
| 56 | | - * more than this number of bytes will cause the device to not ACK those bytes. |
|---|
| 57 | | - * After the host writes a single command byte to the input buffer, reads are |
|---|
| 58 | | - * prohibited until after the device completes command execution. Use a mutex |
|---|
| 59 | | - * when sending i2c commands. |
|---|
| 60 | | - */ |
|---|
| 61 | | -struct atmel_ecc_i2c_client_priv { |
|---|
| 62 | | - struct i2c_client *client; |
|---|
| 63 | | - struct list_head i2c_client_list_node; |
|---|
| 64 | | - struct mutex lock; |
|---|
| 65 | | - u8 wake_token[WAKE_TOKEN_MAX_SIZE]; |
|---|
| 66 | | - size_t wake_token_sz; |
|---|
| 67 | | - atomic_t tfm_count ____cacheline_aligned; |
|---|
| 68 | | -}; |
|---|
| 69 | 27 | |
|---|
| 70 | 28 | /** |
|---|
| 71 | 29 | * atmel_ecdh_ctx - transformation context |
|---|
| .. | .. |
|---|
| 89 | 47 | bool do_fallback; |
|---|
| 90 | 48 | }; |
|---|
| 91 | 49 | |
|---|
| 92 | | -/** |
|---|
| 93 | | - * atmel_ecc_work_data - data structure representing the work |
|---|
| 94 | | - * @ctx : transformation context. |
|---|
| 95 | | - * @cbk : pointer to a callback function to be invoked upon completion of this |
|---|
| 96 | | - * request. This has the form: |
|---|
| 97 | | - * callback(struct atmel_ecc_work_data *work_data, void *areq, u8 status) |
|---|
| 98 | | - * where: |
|---|
| 99 | | - * @work_data: data structure representing the work |
|---|
| 100 | | - * @areq : optional pointer to an argument passed with the original |
|---|
| 101 | | - * request. |
|---|
| 102 | | - * @status : status returned from the i2c client device or i2c error. |
|---|
| 103 | | - * @areq: optional pointer to a user argument for use at callback time. |
|---|
| 104 | | - * @work: describes the task to be executed. |
|---|
| 105 | | - * @cmd : structure used for communicating with the device. |
|---|
| 106 | | - */ |
|---|
| 107 | | -struct atmel_ecc_work_data { |
|---|
| 108 | | - struct atmel_ecdh_ctx *ctx; |
|---|
| 109 | | - void (*cbk)(struct atmel_ecc_work_data *work_data, void *areq, |
|---|
| 110 | | - int status); |
|---|
| 111 | | - void *areq; |
|---|
| 112 | | - struct work_struct work; |
|---|
| 113 | | - struct atmel_ecc_cmd cmd; |
|---|
| 114 | | -}; |
|---|
| 115 | | - |
|---|
| 116 | | -static u16 atmel_ecc_crc16(u16 crc, const u8 *buffer, size_t len) |
|---|
| 117 | | -{ |
|---|
| 118 | | - return cpu_to_le16(bitrev16(crc16(crc, buffer, len))); |
|---|
| 119 | | -} |
|---|
| 120 | | - |
|---|
| 121 | | -/** |
|---|
| 122 | | - * atmel_ecc_checksum() - Generate 16-bit CRC as required by ATMEL ECC. |
|---|
| 123 | | - * CRC16 verification of the count, opcode, param1, param2 and data bytes. |
|---|
| 124 | | - * The checksum is saved in little-endian format in the least significant |
|---|
| 125 | | - * two bytes of the command. CRC polynomial is 0x8005 and the initial register |
|---|
| 126 | | - * value should be zero. |
|---|
| 127 | | - * |
|---|
| 128 | | - * @cmd : structure used for communicating with the device. |
|---|
| 129 | | - */ |
|---|
| 130 | | -static void atmel_ecc_checksum(struct atmel_ecc_cmd *cmd) |
|---|
| 131 | | -{ |
|---|
| 132 | | - u8 *data = &cmd->count; |
|---|
| 133 | | - size_t len = cmd->count - CRC_SIZE; |
|---|
| 134 | | - u16 *crc16 = (u16 *)(data + len); |
|---|
| 135 | | - |
|---|
| 136 | | - *crc16 = atmel_ecc_crc16(0, data, len); |
|---|
| 137 | | -} |
|---|
| 138 | | - |
|---|
| 139 | | -static void atmel_ecc_init_read_cmd(struct atmel_ecc_cmd *cmd) |
|---|
| 140 | | -{ |
|---|
| 141 | | - cmd->word_addr = COMMAND; |
|---|
| 142 | | - cmd->opcode = OPCODE_READ; |
|---|
| 143 | | - /* |
|---|
| 144 | | - * Read the word from Configuration zone that contains the lock bytes |
|---|
| 145 | | - * (UserExtra, Selector, LockValue, LockConfig). |
|---|
| 146 | | - */ |
|---|
| 147 | | - cmd->param1 = CONFIG_ZONE; |
|---|
| 148 | | - cmd->param2 = DEVICE_LOCK_ADDR; |
|---|
| 149 | | - cmd->count = READ_COUNT; |
|---|
| 150 | | - |
|---|
| 151 | | - atmel_ecc_checksum(cmd); |
|---|
| 152 | | - |
|---|
| 153 | | - cmd->msecs = MAX_EXEC_TIME_READ; |
|---|
| 154 | | - cmd->rxsize = READ_RSP_SIZE; |
|---|
| 155 | | -} |
|---|
| 156 | | - |
|---|
| 157 | | -static void atmel_ecc_init_genkey_cmd(struct atmel_ecc_cmd *cmd, u16 keyid) |
|---|
| 158 | | -{ |
|---|
| 159 | | - cmd->word_addr = COMMAND; |
|---|
| 160 | | - cmd->count = GENKEY_COUNT; |
|---|
| 161 | | - cmd->opcode = OPCODE_GENKEY; |
|---|
| 162 | | - cmd->param1 = GENKEY_MODE_PRIVATE; |
|---|
| 163 | | - /* a random private key will be generated and stored in slot keyID */ |
|---|
| 164 | | - cmd->param2 = cpu_to_le16(keyid); |
|---|
| 165 | | - |
|---|
| 166 | | - atmel_ecc_checksum(cmd); |
|---|
| 167 | | - |
|---|
| 168 | | - cmd->msecs = MAX_EXEC_TIME_GENKEY; |
|---|
| 169 | | - cmd->rxsize = GENKEY_RSP_SIZE; |
|---|
| 170 | | -} |
|---|
| 171 | | - |
|---|
| 172 | | -static int atmel_ecc_init_ecdh_cmd(struct atmel_ecc_cmd *cmd, |
|---|
| 173 | | - struct scatterlist *pubkey) |
|---|
| 174 | | -{ |
|---|
| 175 | | - size_t copied; |
|---|
| 176 | | - |
|---|
| 177 | | - cmd->word_addr = COMMAND; |
|---|
| 178 | | - cmd->count = ECDH_COUNT; |
|---|
| 179 | | - cmd->opcode = OPCODE_ECDH; |
|---|
| 180 | | - cmd->param1 = ECDH_PREFIX_MODE; |
|---|
| 181 | | - /* private key slot */ |
|---|
| 182 | | - cmd->param2 = cpu_to_le16(DATA_SLOT_2); |
|---|
| 183 | | - |
|---|
| 184 | | - /* |
|---|
| 185 | | - * The device only supports NIST P256 ECC keys. The public key size will |
|---|
| 186 | | - * always be the same. Use a macro for the key size to avoid unnecessary |
|---|
| 187 | | - * computations. |
|---|
| 188 | | - */ |
|---|
| 189 | | - copied = sg_copy_to_buffer(pubkey, |
|---|
| 190 | | - sg_nents_for_len(pubkey, |
|---|
| 191 | | - ATMEL_ECC_PUBKEY_SIZE), |
|---|
| 192 | | - cmd->data, ATMEL_ECC_PUBKEY_SIZE); |
|---|
| 193 | | - if (copied != ATMEL_ECC_PUBKEY_SIZE) |
|---|
| 194 | | - return -EINVAL; |
|---|
| 195 | | - |
|---|
| 196 | | - atmel_ecc_checksum(cmd); |
|---|
| 197 | | - |
|---|
| 198 | | - cmd->msecs = MAX_EXEC_TIME_ECDH; |
|---|
| 199 | | - cmd->rxsize = ECDH_RSP_SIZE; |
|---|
| 200 | | - |
|---|
| 201 | | - return 0; |
|---|
| 202 | | -} |
|---|
| 203 | | - |
|---|
| 204 | | -/* |
|---|
| 205 | | - * After wake and after execution of a command, there will be error, status, or |
|---|
| 206 | | - * result bytes in the device's output register that can be retrieved by the |
|---|
| 207 | | - * system. When the length of that group is four bytes, the codes returned are |
|---|
| 208 | | - * detailed in error_list. |
|---|
| 209 | | - */ |
|---|
| 210 | | -static int atmel_ecc_status(struct device *dev, u8 *status) |
|---|
| 211 | | -{ |
|---|
| 212 | | - size_t err_list_len = ARRAY_SIZE(error_list); |
|---|
| 213 | | - int i; |
|---|
| 214 | | - u8 err_id = status[1]; |
|---|
| 215 | | - |
|---|
| 216 | | - if (*status != STATUS_SIZE) |
|---|
| 217 | | - return 0; |
|---|
| 218 | | - |
|---|
| 219 | | - if (err_id == STATUS_WAKE_SUCCESSFUL || err_id == STATUS_NOERR) |
|---|
| 220 | | - return 0; |
|---|
| 221 | | - |
|---|
| 222 | | - for (i = 0; i < err_list_len; i++) |
|---|
| 223 | | - if (error_list[i].value == err_id) |
|---|
| 224 | | - break; |
|---|
| 225 | | - |
|---|
| 226 | | - /* if err_id is not in the error_list then ignore it */ |
|---|
| 227 | | - if (i != err_list_len) { |
|---|
| 228 | | - dev_err(dev, "%02x: %s:\n", err_id, error_list[i].error_text); |
|---|
| 229 | | - return err_id; |
|---|
| 230 | | - } |
|---|
| 231 | | - |
|---|
| 232 | | - return 0; |
|---|
| 233 | | -} |
|---|
| 234 | | - |
|---|
| 235 | | -static int atmel_ecc_wakeup(struct i2c_client *client) |
|---|
| 236 | | -{ |
|---|
| 237 | | - struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client); |
|---|
| 238 | | - u8 status[STATUS_RSP_SIZE]; |
|---|
| 239 | | - int ret; |
|---|
| 240 | | - |
|---|
| 241 | | - /* |
|---|
| 242 | | - * The device ignores any levels or transitions on the SCL pin when the |
|---|
| 243 | | - * device is idle, asleep or during waking up. Don't check for error |
|---|
| 244 | | - * when waking up the device. |
|---|
| 245 | | - */ |
|---|
| 246 | | - i2c_master_send(client, i2c_priv->wake_token, i2c_priv->wake_token_sz); |
|---|
| 247 | | - |
|---|
| 248 | | - /* |
|---|
| 249 | | - * Wait to wake the device. Typical execution times for ecdh and genkey |
|---|
| 250 | | - * are around tens of milliseconds. Delta is chosen to 50 microseconds. |
|---|
| 251 | | - */ |
|---|
| 252 | | - usleep_range(TWHI_MIN, TWHI_MAX); |
|---|
| 253 | | - |
|---|
| 254 | | - ret = i2c_master_recv(client, status, STATUS_SIZE); |
|---|
| 255 | | - if (ret < 0) |
|---|
| 256 | | - return ret; |
|---|
| 257 | | - |
|---|
| 258 | | - return atmel_ecc_status(&client->dev, status); |
|---|
| 259 | | -} |
|---|
| 260 | | - |
|---|
| 261 | | -static int atmel_ecc_sleep(struct i2c_client *client) |
|---|
| 262 | | -{ |
|---|
| 263 | | - u8 sleep = SLEEP_TOKEN; |
|---|
| 264 | | - |
|---|
| 265 | | - return i2c_master_send(client, &sleep, 1); |
|---|
| 266 | | -} |
|---|
| 267 | | - |
|---|
| 268 | | -static void atmel_ecdh_done(struct atmel_ecc_work_data *work_data, void *areq, |
|---|
| 50 | +static void atmel_ecdh_done(struct atmel_i2c_work_data *work_data, void *areq, |
|---|
| 269 | 51 | int status) |
|---|
| 270 | 52 | { |
|---|
| 271 | 53 | struct kpp_request *req = areq; |
|---|
| 272 | 54 | struct atmel_ecdh_ctx *ctx = work_data->ctx; |
|---|
| 273 | | - struct atmel_ecc_cmd *cmd = &work_data->cmd; |
|---|
| 55 | + struct atmel_i2c_cmd *cmd = &work_data->cmd; |
|---|
| 274 | 56 | size_t copied, n_sz; |
|---|
| 275 | 57 | |
|---|
| 276 | 58 | if (status) |
|---|
| .. | .. |
|---|
| 287 | 69 | |
|---|
| 288 | 70 | /* fall through */ |
|---|
| 289 | 71 | free_work_data: |
|---|
| 290 | | - kzfree(work_data); |
|---|
| 72 | + kfree_sensitive(work_data); |
|---|
| 291 | 73 | kpp_request_complete(req, status); |
|---|
| 292 | | -} |
|---|
| 293 | | - |
|---|
| 294 | | -/* |
|---|
| 295 | | - * atmel_ecc_send_receive() - send a command to the device and receive its |
|---|
| 296 | | - * response. |
|---|
| 297 | | - * @client: i2c client device |
|---|
| 298 | | - * @cmd : structure used to communicate with the device |
|---|
| 299 | | - * |
|---|
| 300 | | - * After the device receives a Wake token, a watchdog counter starts within the |
|---|
| 301 | | - * device. After the watchdog timer expires, the device enters sleep mode |
|---|
| 302 | | - * regardless of whether some I/O transmission or command execution is in |
|---|
| 303 | | - * progress. If a command is attempted when insufficient time remains prior to |
|---|
| 304 | | - * watchdog timer execution, the device will return the watchdog timeout error |
|---|
| 305 | | - * code without attempting to execute the command. There is no way to reset the |
|---|
| 306 | | - * counter other than to put the device into sleep or idle mode and then |
|---|
| 307 | | - * wake it up again. |
|---|
| 308 | | - */ |
|---|
| 309 | | -static int atmel_ecc_send_receive(struct i2c_client *client, |
|---|
| 310 | | - struct atmel_ecc_cmd *cmd) |
|---|
| 311 | | -{ |
|---|
| 312 | | - struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client); |
|---|
| 313 | | - int ret; |
|---|
| 314 | | - |
|---|
| 315 | | - mutex_lock(&i2c_priv->lock); |
|---|
| 316 | | - |
|---|
| 317 | | - ret = atmel_ecc_wakeup(client); |
|---|
| 318 | | - if (ret) |
|---|
| 319 | | - goto err; |
|---|
| 320 | | - |
|---|
| 321 | | - /* send the command */ |
|---|
| 322 | | - ret = i2c_master_send(client, (u8 *)cmd, cmd->count + WORD_ADDR_SIZE); |
|---|
| 323 | | - if (ret < 0) |
|---|
| 324 | | - goto err; |
|---|
| 325 | | - |
|---|
| 326 | | - /* delay the appropriate amount of time for command to execute */ |
|---|
| 327 | | - msleep(cmd->msecs); |
|---|
| 328 | | - |
|---|
| 329 | | - /* receive the response */ |
|---|
| 330 | | - ret = i2c_master_recv(client, cmd->data, cmd->rxsize); |
|---|
| 331 | | - if (ret < 0) |
|---|
| 332 | | - goto err; |
|---|
| 333 | | - |
|---|
| 334 | | - /* put the device into low-power mode */ |
|---|
| 335 | | - ret = atmel_ecc_sleep(client); |
|---|
| 336 | | - if (ret < 0) |
|---|
| 337 | | - goto err; |
|---|
| 338 | | - |
|---|
| 339 | | - mutex_unlock(&i2c_priv->lock); |
|---|
| 340 | | - return atmel_ecc_status(&client->dev, cmd->data); |
|---|
| 341 | | -err: |
|---|
| 342 | | - mutex_unlock(&i2c_priv->lock); |
|---|
| 343 | | - return ret; |
|---|
| 344 | | -} |
|---|
| 345 | | - |
|---|
| 346 | | -static void atmel_ecc_work_handler(struct work_struct *work) |
|---|
| 347 | | -{ |
|---|
| 348 | | - struct atmel_ecc_work_data *work_data = |
|---|
| 349 | | - container_of(work, struct atmel_ecc_work_data, work); |
|---|
| 350 | | - struct atmel_ecc_cmd *cmd = &work_data->cmd; |
|---|
| 351 | | - struct i2c_client *client = work_data->ctx->client; |
|---|
| 352 | | - int status; |
|---|
| 353 | | - |
|---|
| 354 | | - status = atmel_ecc_send_receive(client, cmd); |
|---|
| 355 | | - work_data->cbk(work_data, work_data->areq, status); |
|---|
| 356 | | -} |
|---|
| 357 | | - |
|---|
| 358 | | -static void atmel_ecc_enqueue(struct atmel_ecc_work_data *work_data, |
|---|
| 359 | | - void (*cbk)(struct atmel_ecc_work_data *work_data, |
|---|
| 360 | | - void *areq, int status), |
|---|
| 361 | | - void *areq) |
|---|
| 362 | | -{ |
|---|
| 363 | | - work_data->cbk = (void *)cbk; |
|---|
| 364 | | - work_data->areq = areq; |
|---|
| 365 | | - |
|---|
| 366 | | - INIT_WORK(&work_data->work, atmel_ecc_work_handler); |
|---|
| 367 | | - schedule_work(&work_data->work); |
|---|
| 368 | 74 | } |
|---|
| 369 | 75 | |
|---|
| 370 | 76 | static unsigned int atmel_ecdh_supported_curve(unsigned int curve_id) |
|---|
| .. | .. |
|---|
| 383 | 89 | unsigned int len) |
|---|
| 384 | 90 | { |
|---|
| 385 | 91 | struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm); |
|---|
| 386 | | - struct atmel_ecc_cmd *cmd; |
|---|
| 92 | + struct atmel_i2c_cmd *cmd; |
|---|
| 387 | 93 | void *public_key; |
|---|
| 388 | 94 | struct ecdh params; |
|---|
| 389 | 95 | int ret = -ENOMEM; |
|---|
| .. | .. |
|---|
| 421 | 127 | ctx->do_fallback = false; |
|---|
| 422 | 128 | ctx->curve_id = params.curve_id; |
|---|
| 423 | 129 | |
|---|
| 424 | | - atmel_ecc_init_genkey_cmd(cmd, DATA_SLOT_2); |
|---|
| 130 | + atmel_i2c_init_genkey_cmd(cmd, DATA_SLOT_2); |
|---|
| 425 | 131 | |
|---|
| 426 | | - ret = atmel_ecc_send_receive(ctx->client, cmd); |
|---|
| 132 | + ret = atmel_i2c_send_receive(ctx->client, cmd); |
|---|
| 427 | 133 | if (ret) |
|---|
| 428 | 134 | goto free_public_key; |
|---|
| 429 | 135 | |
|---|
| .. | .. |
|---|
| 453 | 159 | return crypto_kpp_generate_public_key(req); |
|---|
| 454 | 160 | } |
|---|
| 455 | 161 | |
|---|
| 162 | + if (!ctx->public_key) |
|---|
| 163 | + return -EINVAL; |
|---|
| 164 | + |
|---|
| 456 | 165 | /* might want less than we've got */ |
|---|
| 457 | 166 | nbytes = min_t(size_t, ATMEL_ECC_PUBKEY_SIZE, req->dst_len); |
|---|
| 458 | 167 | |
|---|
| .. | .. |
|---|
| 470 | 179 | { |
|---|
| 471 | 180 | struct crypto_kpp *tfm = crypto_kpp_reqtfm(req); |
|---|
| 472 | 181 | struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm); |
|---|
| 473 | | - struct atmel_ecc_work_data *work_data; |
|---|
| 182 | + struct atmel_i2c_work_data *work_data; |
|---|
| 474 | 183 | gfp_t gfp; |
|---|
| 475 | 184 | int ret; |
|---|
| 476 | 185 | |
|---|
| .. | .. |
|---|
| 491 | 200 | return -ENOMEM; |
|---|
| 492 | 201 | |
|---|
| 493 | 202 | work_data->ctx = ctx; |
|---|
| 203 | + work_data->client = ctx->client; |
|---|
| 494 | 204 | |
|---|
| 495 | | - ret = atmel_ecc_init_ecdh_cmd(&work_data->cmd, req->src); |
|---|
| 205 | + ret = atmel_i2c_init_ecdh_cmd(&work_data->cmd, req->src); |
|---|
| 496 | 206 | if (ret) |
|---|
| 497 | 207 | goto free_work_data; |
|---|
| 498 | 208 | |
|---|
| 499 | | - atmel_ecc_enqueue(work_data, atmel_ecdh_done, req); |
|---|
| 209 | + atmel_i2c_enqueue(work_data, atmel_ecdh_done, req); |
|---|
| 500 | 210 | |
|---|
| 501 | 211 | return -EINPROGRESS; |
|---|
| 502 | 212 | |
|---|
| .. | .. |
|---|
| 507 | 217 | |
|---|
| 508 | 218 | static struct i2c_client *atmel_ecc_i2c_client_alloc(void) |
|---|
| 509 | 219 | { |
|---|
| 510 | | - struct atmel_ecc_i2c_client_priv *i2c_priv, *min_i2c_priv = NULL; |
|---|
| 220 | + struct atmel_i2c_client_priv *i2c_priv, *min_i2c_priv = NULL; |
|---|
| 511 | 221 | struct i2c_client *client = ERR_PTR(-ENODEV); |
|---|
| 512 | 222 | int min_tfm_cnt = INT_MAX; |
|---|
| 513 | 223 | int tfm_cnt; |
|---|
| .. | .. |
|---|
| 542 | 252 | |
|---|
| 543 | 253 | static void atmel_ecc_i2c_client_free(struct i2c_client *client) |
|---|
| 544 | 254 | { |
|---|
| 545 | | - struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client); |
|---|
| 255 | + struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client); |
|---|
| 546 | 256 | |
|---|
| 547 | 257 | atomic_dec(&i2c_priv->tfm_count); |
|---|
| 548 | 258 | } |
|---|
| .. | .. |
|---|
| 613 | 323 | }, |
|---|
| 614 | 324 | }; |
|---|
| 615 | 325 | |
|---|
| 616 | | -static inline size_t atmel_ecc_wake_token_sz(u32 bus_clk_rate) |
|---|
| 617 | | -{ |
|---|
| 618 | | - u32 no_of_bits = DIV_ROUND_UP(TWLO_USEC * bus_clk_rate, USEC_PER_SEC); |
|---|
| 619 | | - |
|---|
| 620 | | - /* return the size of the wake_token in bytes */ |
|---|
| 621 | | - return DIV_ROUND_UP(no_of_bits, 8); |
|---|
| 622 | | -} |
|---|
| 623 | | - |
|---|
| 624 | | -static int device_sanity_check(struct i2c_client *client) |
|---|
| 625 | | -{ |
|---|
| 626 | | - struct atmel_ecc_cmd *cmd; |
|---|
| 627 | | - int ret; |
|---|
| 628 | | - |
|---|
| 629 | | - cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); |
|---|
| 630 | | - if (!cmd) |
|---|
| 631 | | - return -ENOMEM; |
|---|
| 632 | | - |
|---|
| 633 | | - atmel_ecc_init_read_cmd(cmd); |
|---|
| 634 | | - |
|---|
| 635 | | - ret = atmel_ecc_send_receive(client, cmd); |
|---|
| 636 | | - if (ret) |
|---|
| 637 | | - goto free_cmd; |
|---|
| 638 | | - |
|---|
| 639 | | - /* |
|---|
| 640 | | - * It is vital that the Configuration, Data and OTP zones be locked |
|---|
| 641 | | - * prior to release into the field of the system containing the device. |
|---|
| 642 | | - * Failure to lock these zones may permit modification of any secret |
|---|
| 643 | | - * keys and may lead to other security problems. |
|---|
| 644 | | - */ |
|---|
| 645 | | - if (cmd->data[LOCK_CONFIG_IDX] || cmd->data[LOCK_VALUE_IDX]) { |
|---|
| 646 | | - dev_err(&client->dev, "Configuration or Data and OTP zones are unlocked!\n"); |
|---|
| 647 | | - ret = -ENOTSUPP; |
|---|
| 648 | | - } |
|---|
| 649 | | - |
|---|
| 650 | | - /* fall through */ |
|---|
| 651 | | -free_cmd: |
|---|
| 652 | | - kfree(cmd); |
|---|
| 653 | | - return ret; |
|---|
| 654 | | -} |
|---|
| 655 | | - |
|---|
| 656 | 326 | static int atmel_ecc_probe(struct i2c_client *client, |
|---|
| 657 | 327 | const struct i2c_device_id *id) |
|---|
| 658 | 328 | { |
|---|
| 659 | | - struct atmel_ecc_i2c_client_priv *i2c_priv; |
|---|
| 660 | | - struct device *dev = &client->dev; |
|---|
| 329 | + struct atmel_i2c_client_priv *i2c_priv; |
|---|
| 661 | 330 | int ret; |
|---|
| 662 | | - u32 bus_clk_rate; |
|---|
| 663 | 331 | |
|---|
| 664 | | - if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
|---|
| 665 | | - dev_err(dev, "I2C_FUNC_I2C not supported\n"); |
|---|
| 666 | | - return -ENODEV; |
|---|
| 667 | | - } |
|---|
| 668 | | - |
|---|
| 669 | | - ret = of_property_read_u32(client->adapter->dev.of_node, |
|---|
| 670 | | - "clock-frequency", &bus_clk_rate); |
|---|
| 671 | | - if (ret) { |
|---|
| 672 | | - dev_err(dev, "of: failed to read clock-frequency property\n"); |
|---|
| 673 | | - return ret; |
|---|
| 674 | | - } |
|---|
| 675 | | - |
|---|
| 676 | | - if (bus_clk_rate > 1000000L) { |
|---|
| 677 | | - dev_err(dev, "%d exceeds maximum supported clock frequency (1MHz)\n", |
|---|
| 678 | | - bus_clk_rate); |
|---|
| 679 | | - return -EINVAL; |
|---|
| 680 | | - } |
|---|
| 681 | | - |
|---|
| 682 | | - i2c_priv = devm_kmalloc(dev, sizeof(*i2c_priv), GFP_KERNEL); |
|---|
| 683 | | - if (!i2c_priv) |
|---|
| 684 | | - return -ENOMEM; |
|---|
| 685 | | - |
|---|
| 686 | | - i2c_priv->client = client; |
|---|
| 687 | | - mutex_init(&i2c_priv->lock); |
|---|
| 688 | | - |
|---|
| 689 | | - /* |
|---|
| 690 | | - * WAKE_TOKEN_MAX_SIZE was calculated for the maximum bus_clk_rate - |
|---|
| 691 | | - * 1MHz. The previous bus_clk_rate check ensures us that wake_token_sz |
|---|
| 692 | | - * will always be smaller than or equal to WAKE_TOKEN_MAX_SIZE. |
|---|
| 693 | | - */ |
|---|
| 694 | | - i2c_priv->wake_token_sz = atmel_ecc_wake_token_sz(bus_clk_rate); |
|---|
| 695 | | - |
|---|
| 696 | | - memset(i2c_priv->wake_token, 0, sizeof(i2c_priv->wake_token)); |
|---|
| 697 | | - |
|---|
| 698 | | - atomic_set(&i2c_priv->tfm_count, 0); |
|---|
| 699 | | - |
|---|
| 700 | | - i2c_set_clientdata(client, i2c_priv); |
|---|
| 701 | | - |
|---|
| 702 | | - ret = device_sanity_check(client); |
|---|
| 332 | + ret = atmel_i2c_probe(client, id); |
|---|
| 703 | 333 | if (ret) |
|---|
| 704 | 334 | return ret; |
|---|
| 335 | + |
|---|
| 336 | + i2c_priv = i2c_get_clientdata(client); |
|---|
| 705 | 337 | |
|---|
| 706 | 338 | spin_lock(&driver_data.i2c_list_lock); |
|---|
| 707 | 339 | list_add_tail(&i2c_priv->i2c_client_list_node, |
|---|
| .. | .. |
|---|
| 714 | 346 | list_del(&i2c_priv->i2c_client_list_node); |
|---|
| 715 | 347 | spin_unlock(&driver_data.i2c_list_lock); |
|---|
| 716 | 348 | |
|---|
| 717 | | - dev_err(dev, "%s alg registration failed\n", |
|---|
| 349 | + dev_err(&client->dev, "%s alg registration failed\n", |
|---|
| 718 | 350 | atmel_ecdh.base.cra_driver_name); |
|---|
| 719 | 351 | } else { |
|---|
| 720 | | - dev_info(dev, "atmel ecc algorithms registered in /proc/crypto\n"); |
|---|
| 352 | + dev_info(&client->dev, "atmel ecc algorithms registered in /proc/crypto\n"); |
|---|
| 721 | 353 | } |
|---|
| 722 | 354 | |
|---|
| 723 | 355 | return ret; |
|---|
| .. | .. |
|---|
| 725 | 357 | |
|---|
| 726 | 358 | static int atmel_ecc_remove(struct i2c_client *client) |
|---|
| 727 | 359 | { |
|---|
| 728 | | - struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client); |
|---|
| 360 | + struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client); |
|---|
| 729 | 361 | |
|---|
| 730 | 362 | /* Return EBUSY if i2c client already allocated. */ |
|---|
| 731 | 363 | if (atomic_read(&i2c_priv->tfm_count)) { |
|---|