Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 IBM Corporation |
| 3 | * |
| 4 | * Author: Ashley Lai <adlai@us.ibm.com> |
| 5 | * |
| 6 | * Maintained by: <tpmdd-devel@lists.sourceforge.net> |
| 7 | * |
| 8 | * Device driver for TCG/TCPA TPM (trusted platform module). |
| 9 | * Specifications at www.trustedcomputinggroup.org |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation, version 2 of the |
| 14 | * License. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/dma-mapping.h> |
| 19 | #include <linux/dmapool.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <asm/vio.h> |
| 22 | #include <asm/irq.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/wait.h> |
| 28 | #include <asm/prom.h> |
| 29 | |
| 30 | #include "tpm.h" |
| 31 | #include "tpm_ibmvtpm.h" |
| 32 | |
| 33 | static const char tpm_ibmvtpm_driver_name[] = "tpm_ibmvtpm"; |
| 34 | |
| 35 | static struct vio_device_id tpm_ibmvtpm_device_table[] __devinitdata = { |
| 36 | { "IBM,vtpm", "IBM,vtpm"}, |
| 37 | { "", "" } |
| 38 | }; |
| 39 | MODULE_DEVICE_TABLE(vio, tpm_ibmvtpm_device_table); |
| 40 | |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 41 | /** |
| 42 | * ibmvtpm_send_crq - Send a CRQ request |
| 43 | * @vdev: vio device struct |
| 44 | * @w1: first word |
| 45 | * @w2: second word |
| 46 | * |
| 47 | * Return value: |
| 48 | * 0 -Sucess |
| 49 | * Non-zero - Failure |
| 50 | */ |
| 51 | static int ibmvtpm_send_crq(struct vio_dev *vdev, u64 w1, u64 w2) |
| 52 | { |
| 53 | return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, w1, w2); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * ibmvtpm_get_data - Retrieve ibm vtpm data |
| 58 | * @dev: device struct |
| 59 | * |
| 60 | * Return value: |
| 61 | * vtpm device struct |
| 62 | */ |
| 63 | static struct ibmvtpm_dev *ibmvtpm_get_data(const struct device *dev) |
| 64 | { |
| 65 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 66 | if (chip) |
| 67 | return (struct ibmvtpm_dev *)chip->vendor.data; |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * tpm_ibmvtpm_recv - Receive data after send |
| 73 | * @chip: tpm chip struct |
| 74 | * @buf: buffer to read |
| 75 | * count: size of buffer |
| 76 | * |
| 77 | * Return value: |
| 78 | * Number of bytes read |
| 79 | */ |
| 80 | static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count) |
| 81 | { |
| 82 | struct ibmvtpm_dev *ibmvtpm; |
| 83 | u16 len; |
Ashley Lai | b566650 | 2012-09-12 12:49:50 -0500 | [diff] [blame^] | 84 | int sig; |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 85 | |
| 86 | ibmvtpm = (struct ibmvtpm_dev *)chip->vendor.data; |
| 87 | |
| 88 | if (!ibmvtpm->rtce_buf) { |
| 89 | dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n"); |
| 90 | return 0; |
| 91 | } |
| 92 | |
Ashley Lai | b566650 | 2012-09-12 12:49:50 -0500 | [diff] [blame^] | 93 | sig = wait_event_interruptible(ibmvtpm->wq, ibmvtpm->res_len != 0); |
| 94 | if (sig) |
| 95 | return -EINTR; |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 96 | |
Ashley Lai | b566650 | 2012-09-12 12:49:50 -0500 | [diff] [blame^] | 97 | len = ibmvtpm->res_len; |
| 98 | |
| 99 | if (count < len) { |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 100 | dev_err(ibmvtpm->dev, |
| 101 | "Invalid size in recv: count=%ld, crq_size=%d\n", |
Ashley Lai | b566650 | 2012-09-12 12:49:50 -0500 | [diff] [blame^] | 102 | count, len); |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 103 | return -EIO; |
| 104 | } |
| 105 | |
| 106 | spin_lock(&ibmvtpm->rtce_lock); |
Ashley Lai | b566650 | 2012-09-12 12:49:50 -0500 | [diff] [blame^] | 107 | memcpy((void *)buf, (void *)ibmvtpm->rtce_buf, len); |
| 108 | memset(ibmvtpm->rtce_buf, 0, len); |
| 109 | ibmvtpm->res_len = 0; |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 110 | spin_unlock(&ibmvtpm->rtce_lock); |
| 111 | return len; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * tpm_ibmvtpm_send - Send tpm request |
| 116 | * @chip: tpm chip struct |
| 117 | * @buf: buffer contains data to send |
| 118 | * count: size of buffer |
| 119 | * |
| 120 | * Return value: |
| 121 | * Number of bytes sent |
| 122 | */ |
| 123 | static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count) |
| 124 | { |
| 125 | struct ibmvtpm_dev *ibmvtpm; |
| 126 | struct ibmvtpm_crq crq; |
| 127 | u64 *word = (u64 *) &crq; |
| 128 | int rc; |
| 129 | |
| 130 | ibmvtpm = (struct ibmvtpm_dev *)chip->vendor.data; |
| 131 | |
| 132 | if (!ibmvtpm->rtce_buf) { |
| 133 | dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n"); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | if (count > ibmvtpm->rtce_size) { |
| 138 | dev_err(ibmvtpm->dev, |
| 139 | "Invalid size in send: count=%ld, rtce_size=%d\n", |
| 140 | count, ibmvtpm->rtce_size); |
| 141 | return -EIO; |
| 142 | } |
| 143 | |
| 144 | spin_lock(&ibmvtpm->rtce_lock); |
| 145 | memcpy((void *)ibmvtpm->rtce_buf, (void *)buf, count); |
| 146 | crq.valid = (u8)IBMVTPM_VALID_CMD; |
| 147 | crq.msg = (u8)VTPM_TPM_COMMAND; |
| 148 | crq.len = (u16)count; |
| 149 | crq.data = ibmvtpm->rtce_dma_handle; |
| 150 | |
| 151 | rc = ibmvtpm_send_crq(ibmvtpm->vdev, word[0], word[1]); |
| 152 | if (rc != H_SUCCESS) { |
| 153 | dev_err(ibmvtpm->dev, "tpm_ibmvtpm_send failed rc=%d\n", rc); |
| 154 | rc = 0; |
| 155 | } else |
| 156 | rc = count; |
| 157 | |
| 158 | spin_unlock(&ibmvtpm->rtce_lock); |
| 159 | return rc; |
| 160 | } |
| 161 | |
| 162 | static void tpm_ibmvtpm_cancel(struct tpm_chip *chip) |
| 163 | { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | static u8 tpm_ibmvtpm_status(struct tpm_chip *chip) |
| 168 | { |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size |
| 174 | * @ibmvtpm: vtpm device struct |
| 175 | * |
| 176 | * Return value: |
| 177 | * 0 - Success |
| 178 | * Non-zero - Failure |
| 179 | */ |
| 180 | static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm) |
| 181 | { |
| 182 | struct ibmvtpm_crq crq; |
| 183 | u64 *buf = (u64 *) &crq; |
| 184 | int rc; |
| 185 | |
| 186 | crq.valid = (u8)IBMVTPM_VALID_CMD; |
| 187 | crq.msg = (u8)VTPM_GET_RTCE_BUFFER_SIZE; |
| 188 | |
| 189 | rc = ibmvtpm_send_crq(ibmvtpm->vdev, buf[0], buf[1]); |
| 190 | if (rc != H_SUCCESS) |
| 191 | dev_err(ibmvtpm->dev, |
| 192 | "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc); |
| 193 | |
| 194 | return rc; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version |
| 199 | * - Note that this is vtpm version and not tpm version |
| 200 | * @ibmvtpm: vtpm device struct |
| 201 | * |
| 202 | * Return value: |
| 203 | * 0 - Success |
| 204 | * Non-zero - Failure |
| 205 | */ |
| 206 | static int ibmvtpm_crq_get_version(struct ibmvtpm_dev *ibmvtpm) |
| 207 | { |
| 208 | struct ibmvtpm_crq crq; |
| 209 | u64 *buf = (u64 *) &crq; |
| 210 | int rc; |
| 211 | |
| 212 | crq.valid = (u8)IBMVTPM_VALID_CMD; |
| 213 | crq.msg = (u8)VTPM_GET_VERSION; |
| 214 | |
| 215 | rc = ibmvtpm_send_crq(ibmvtpm->vdev, buf[0], buf[1]); |
| 216 | if (rc != H_SUCCESS) |
| 217 | dev_err(ibmvtpm->dev, |
| 218 | "ibmvtpm_crq_get_version failed rc=%d\n", rc); |
| 219 | |
| 220 | return rc; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message |
| 225 | * @ibmvtpm: vtpm device struct |
| 226 | * |
| 227 | * Return value: |
| 228 | * 0 - Success |
| 229 | * Non-zero - Failure |
| 230 | */ |
| 231 | static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev *ibmvtpm) |
| 232 | { |
| 233 | int rc; |
| 234 | |
| 235 | rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_COMP_CMD, 0); |
| 236 | if (rc != H_SUCCESS) |
| 237 | dev_err(ibmvtpm->dev, |
| 238 | "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc); |
| 239 | |
| 240 | return rc; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * ibmvtpm_crq_send_init - Send a CRQ initialize message |
| 245 | * @ibmvtpm: vtpm device struct |
| 246 | * |
| 247 | * Return value: |
| 248 | * 0 - Success |
| 249 | * Non-zero - Failure |
| 250 | */ |
| 251 | static int ibmvtpm_crq_send_init(struct ibmvtpm_dev *ibmvtpm) |
| 252 | { |
| 253 | int rc; |
| 254 | |
| 255 | rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_CMD, 0); |
| 256 | if (rc != H_SUCCESS) |
| 257 | dev_err(ibmvtpm->dev, |
| 258 | "ibmvtpm_crq_send_init failed rc=%d\n", rc); |
| 259 | |
| 260 | return rc; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * tpm_ibmvtpm_remove - ibm vtpm remove entry point |
| 265 | * @vdev: vio device struct |
| 266 | * |
| 267 | * Return value: |
| 268 | * 0 |
| 269 | */ |
| 270 | static int __devexit tpm_ibmvtpm_remove(struct vio_dev *vdev) |
| 271 | { |
| 272 | struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(&vdev->dev); |
| 273 | int rc = 0; |
| 274 | |
| 275 | free_irq(vdev->irq, ibmvtpm); |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 276 | |
| 277 | do { |
| 278 | if (rc) |
| 279 | msleep(100); |
| 280 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); |
| 281 | } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); |
| 282 | |
| 283 | dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle, |
| 284 | CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL); |
| 285 | free_page((unsigned long)ibmvtpm->crq_queue.crq_addr); |
| 286 | |
| 287 | if (ibmvtpm->rtce_buf) { |
| 288 | dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle, |
| 289 | ibmvtpm->rtce_size, DMA_BIDIRECTIONAL); |
| 290 | kfree(ibmvtpm->rtce_buf); |
| 291 | } |
| 292 | |
| 293 | tpm_remove_hardware(ibmvtpm->dev); |
| 294 | |
| 295 | kfree(ibmvtpm); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver |
| 302 | * @vdev: vio device struct |
| 303 | * |
| 304 | * Return value: |
| 305 | * Number of bytes the driver needs to DMA map |
| 306 | */ |
| 307 | static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev *vdev) |
| 308 | { |
| 309 | struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(&vdev->dev); |
| 310 | return CRQ_RES_BUF_SIZE + ibmvtpm->rtce_size; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * tpm_ibmvtpm_suspend - Suspend |
| 315 | * @dev: device struct |
| 316 | * |
| 317 | * Return value: |
| 318 | * 0 |
| 319 | */ |
| 320 | static int tpm_ibmvtpm_suspend(struct device *dev) |
| 321 | { |
| 322 | struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(dev); |
| 323 | struct ibmvtpm_crq crq; |
| 324 | u64 *buf = (u64 *) &crq; |
| 325 | int rc = 0; |
| 326 | |
| 327 | crq.valid = (u8)IBMVTPM_VALID_CMD; |
| 328 | crq.msg = (u8)VTPM_PREPARE_TO_SUSPEND; |
| 329 | |
| 330 | rc = ibmvtpm_send_crq(ibmvtpm->vdev, buf[0], buf[1]); |
| 331 | if (rc != H_SUCCESS) |
| 332 | dev_err(ibmvtpm->dev, |
| 333 | "tpm_ibmvtpm_suspend failed rc=%d\n", rc); |
| 334 | |
| 335 | return rc; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * ibmvtpm_reset_crq - Reset CRQ |
| 340 | * @ibmvtpm: ibm vtpm struct |
| 341 | * |
| 342 | * Return value: |
| 343 | * 0 - Success |
| 344 | * Non-zero - Failure |
| 345 | */ |
| 346 | static int ibmvtpm_reset_crq(struct ibmvtpm_dev *ibmvtpm) |
| 347 | { |
| 348 | int rc = 0; |
| 349 | |
| 350 | do { |
| 351 | if (rc) |
| 352 | msleep(100); |
| 353 | rc = plpar_hcall_norets(H_FREE_CRQ, |
| 354 | ibmvtpm->vdev->unit_address); |
| 355 | } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); |
| 356 | |
| 357 | memset(ibmvtpm->crq_queue.crq_addr, 0, CRQ_RES_BUF_SIZE); |
| 358 | ibmvtpm->crq_queue.index = 0; |
| 359 | |
| 360 | return plpar_hcall_norets(H_REG_CRQ, ibmvtpm->vdev->unit_address, |
| 361 | ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * tpm_ibmvtpm_resume - Resume from suspend |
| 366 | * @dev: device struct |
| 367 | * |
| 368 | * Return value: |
| 369 | * 0 |
| 370 | */ |
| 371 | static int tpm_ibmvtpm_resume(struct device *dev) |
| 372 | { |
| 373 | struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(dev); |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 374 | int rc = 0; |
| 375 | |
| 376 | do { |
| 377 | if (rc) |
| 378 | msleep(100); |
| 379 | rc = plpar_hcall_norets(H_ENABLE_CRQ, |
| 380 | ibmvtpm->vdev->unit_address); |
| 381 | } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc)); |
| 382 | |
| 383 | if (rc) { |
| 384 | dev_err(dev, "Error enabling ibmvtpm rc=%d\n", rc); |
| 385 | return rc; |
| 386 | } |
| 387 | |
Ashley Lai | b566650 | 2012-09-12 12:49:50 -0500 | [diff] [blame^] | 388 | rc = vio_enable_interrupts(ibmvtpm->vdev); |
| 389 | if (rc) { |
| 390 | dev_err(dev, "Error vio_enable_interrupts rc=%d\n", rc); |
| 391 | return rc; |
| 392 | } |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 393 | |
| 394 | rc = ibmvtpm_crq_send_init(ibmvtpm); |
| 395 | if (rc) |
| 396 | dev_err(dev, "Error send_init rc=%d\n", rc); |
| 397 | |
| 398 | return rc; |
| 399 | } |
| 400 | |
| 401 | static const struct file_operations ibmvtpm_ops = { |
| 402 | .owner = THIS_MODULE, |
| 403 | .llseek = no_llseek, |
| 404 | .open = tpm_open, |
| 405 | .read = tpm_read, |
| 406 | .write = tpm_write, |
| 407 | .release = tpm_release, |
| 408 | }; |
| 409 | |
| 410 | static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL); |
| 411 | static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL); |
| 412 | static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL); |
| 413 | static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL); |
| 414 | static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL); |
| 415 | static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, |
| 416 | NULL); |
| 417 | static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL); |
| 418 | static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel); |
| 419 | static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL); |
| 420 | static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL); |
| 421 | |
| 422 | static struct attribute *ibmvtpm_attrs[] = { |
| 423 | &dev_attr_pubek.attr, |
| 424 | &dev_attr_pcrs.attr, |
| 425 | &dev_attr_enabled.attr, |
| 426 | &dev_attr_active.attr, |
| 427 | &dev_attr_owned.attr, |
| 428 | &dev_attr_temp_deactivated.attr, |
| 429 | &dev_attr_caps.attr, |
| 430 | &dev_attr_cancel.attr, |
| 431 | &dev_attr_durations.attr, |
| 432 | &dev_attr_timeouts.attr, NULL, |
| 433 | }; |
| 434 | |
| 435 | static struct attribute_group ibmvtpm_attr_grp = { .attrs = ibmvtpm_attrs }; |
| 436 | |
| 437 | static const struct tpm_vendor_specific tpm_ibmvtpm = { |
| 438 | .recv = tpm_ibmvtpm_recv, |
| 439 | .send = tpm_ibmvtpm_send, |
| 440 | .cancel = tpm_ibmvtpm_cancel, |
| 441 | .status = tpm_ibmvtpm_status, |
| 442 | .req_complete_mask = 0, |
| 443 | .req_complete_val = 0, |
| 444 | .req_canceled = 0, |
| 445 | .attr_group = &ibmvtpm_attr_grp, |
| 446 | .miscdev = { .fops = &ibmvtpm_ops, }, |
| 447 | }; |
| 448 | |
| 449 | static const struct dev_pm_ops tpm_ibmvtpm_pm_ops = { |
| 450 | .suspend = tpm_ibmvtpm_suspend, |
| 451 | .resume = tpm_ibmvtpm_resume, |
| 452 | }; |
| 453 | |
| 454 | /** |
| 455 | * ibmvtpm_crq_get_next - Get next responded crq |
| 456 | * @ibmvtpm vtpm device struct |
| 457 | * |
| 458 | * Return value: |
| 459 | * vtpm crq pointer |
| 460 | */ |
| 461 | static struct ibmvtpm_crq *ibmvtpm_crq_get_next(struct ibmvtpm_dev *ibmvtpm) |
| 462 | { |
| 463 | struct ibmvtpm_crq_queue *crq_q = &ibmvtpm->crq_queue; |
| 464 | struct ibmvtpm_crq *crq = &crq_q->crq_addr[crq_q->index]; |
| 465 | |
| 466 | if (crq->valid & VTPM_MSG_RES) { |
| 467 | if (++crq_q->index == crq_q->num_entry) |
| 468 | crq_q->index = 0; |
Ashley Lai | b566650 | 2012-09-12 12:49:50 -0500 | [diff] [blame^] | 469 | smp_rmb(); |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 470 | } else |
| 471 | crq = NULL; |
| 472 | return crq; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * ibmvtpm_crq_process - Process responded crq |
| 477 | * @crq crq to be processed |
| 478 | * @ibmvtpm vtpm device struct |
| 479 | * |
| 480 | * Return value: |
| 481 | * Nothing |
| 482 | */ |
| 483 | static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq, |
| 484 | struct ibmvtpm_dev *ibmvtpm) |
| 485 | { |
| 486 | int rc = 0; |
| 487 | |
| 488 | switch (crq->valid) { |
| 489 | case VALID_INIT_CRQ: |
| 490 | switch (crq->msg) { |
| 491 | case INIT_CRQ_RES: |
| 492 | dev_info(ibmvtpm->dev, "CRQ initialized\n"); |
| 493 | rc = ibmvtpm_crq_send_init_complete(ibmvtpm); |
| 494 | if (rc) |
| 495 | dev_err(ibmvtpm->dev, "Unable to send CRQ init complete rc=%d\n", rc); |
| 496 | return; |
| 497 | case INIT_CRQ_COMP_RES: |
| 498 | dev_info(ibmvtpm->dev, |
| 499 | "CRQ initialization completed\n"); |
| 500 | return; |
| 501 | default: |
| 502 | dev_err(ibmvtpm->dev, "Unknown crq message type: %d\n", crq->msg); |
| 503 | return; |
| 504 | } |
| 505 | return; |
| 506 | case IBMVTPM_VALID_CMD: |
| 507 | switch (crq->msg) { |
| 508 | case VTPM_GET_RTCE_BUFFER_SIZE_RES: |
| 509 | if (crq->len <= 0) { |
| 510 | dev_err(ibmvtpm->dev, "Invalid rtce size\n"); |
| 511 | return; |
| 512 | } |
| 513 | ibmvtpm->rtce_size = crq->len; |
| 514 | ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size, |
| 515 | GFP_KERNEL); |
| 516 | if (!ibmvtpm->rtce_buf) { |
| 517 | dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n"); |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev, |
| 522 | ibmvtpm->rtce_buf, ibmvtpm->rtce_size, |
| 523 | DMA_BIDIRECTIONAL); |
| 524 | |
| 525 | if (dma_mapping_error(ibmvtpm->dev, |
| 526 | ibmvtpm->rtce_dma_handle)) { |
| 527 | kfree(ibmvtpm->rtce_buf); |
| 528 | ibmvtpm->rtce_buf = NULL; |
| 529 | dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n"); |
| 530 | } |
| 531 | |
| 532 | return; |
| 533 | case VTPM_GET_VERSION_RES: |
| 534 | ibmvtpm->vtpm_version = crq->data; |
| 535 | return; |
| 536 | case VTPM_TPM_COMMAND_RES: |
Ashley Lai | b566650 | 2012-09-12 12:49:50 -0500 | [diff] [blame^] | 537 | /* len of the data in rtce buffer */ |
| 538 | ibmvtpm->res_len = crq->len; |
| 539 | wake_up_interruptible(&ibmvtpm->wq); |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 540 | return; |
| 541 | default: |
| 542 | return; |
| 543 | } |
| 544 | } |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * ibmvtpm_interrupt - Interrupt handler |
| 550 | * @irq: irq number to handle |
| 551 | * @vtpm_instance: vtpm that received interrupt |
| 552 | * |
| 553 | * Returns: |
| 554 | * IRQ_HANDLED |
| 555 | **/ |
| 556 | static irqreturn_t ibmvtpm_interrupt(int irq, void *vtpm_instance) |
| 557 | { |
| 558 | struct ibmvtpm_dev *ibmvtpm = (struct ibmvtpm_dev *) vtpm_instance; |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 559 | struct ibmvtpm_crq *crq; |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 560 | |
Ashley Lai | b566650 | 2012-09-12 12:49:50 -0500 | [diff] [blame^] | 561 | /* while loop is needed for initial setup (get version and |
| 562 | * get rtce_size). There should be only one tpm request at any |
| 563 | * given time. |
| 564 | */ |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 565 | while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) { |
| 566 | ibmvtpm_crq_process(crq, ibmvtpm); |
| 567 | crq->valid = 0; |
Ashley Lai | b566650 | 2012-09-12 12:49:50 -0500 | [diff] [blame^] | 568 | smp_wmb(); |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 569 | } |
| 570 | |
Ashley Lai | b566650 | 2012-09-12 12:49:50 -0500 | [diff] [blame^] | 571 | return IRQ_HANDLED; |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** |
| 575 | * tpm_ibmvtpm_probe - ibm vtpm initialize entry point |
| 576 | * @vio_dev: vio device struct |
| 577 | * @id: vio device id struct |
| 578 | * |
| 579 | * Return value: |
| 580 | * 0 - Success |
| 581 | * Non-zero - Failure |
| 582 | */ |
| 583 | static int __devinit tpm_ibmvtpm_probe(struct vio_dev *vio_dev, |
| 584 | const struct vio_device_id *id) |
| 585 | { |
| 586 | struct ibmvtpm_dev *ibmvtpm; |
| 587 | struct device *dev = &vio_dev->dev; |
| 588 | struct ibmvtpm_crq_queue *crq_q; |
| 589 | struct tpm_chip *chip; |
| 590 | int rc = -ENOMEM, rc1; |
| 591 | |
| 592 | chip = tpm_register_hardware(dev, &tpm_ibmvtpm); |
| 593 | if (!chip) { |
| 594 | dev_err(dev, "tpm_register_hardware failed\n"); |
| 595 | return -ENODEV; |
| 596 | } |
| 597 | |
| 598 | ibmvtpm = kzalloc(sizeof(struct ibmvtpm_dev), GFP_KERNEL); |
| 599 | if (!ibmvtpm) { |
| 600 | dev_err(dev, "kzalloc for ibmvtpm failed\n"); |
| 601 | goto cleanup; |
| 602 | } |
| 603 | |
| 604 | crq_q = &ibmvtpm->crq_queue; |
| 605 | crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL); |
| 606 | if (!crq_q->crq_addr) { |
| 607 | dev_err(dev, "Unable to allocate memory for crq_addr\n"); |
| 608 | goto cleanup; |
| 609 | } |
| 610 | |
| 611 | crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr); |
| 612 | ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr, |
| 613 | CRQ_RES_BUF_SIZE, |
| 614 | DMA_BIDIRECTIONAL); |
| 615 | |
| 616 | if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) { |
| 617 | dev_err(dev, "dma mapping failed\n"); |
| 618 | goto cleanup; |
| 619 | } |
| 620 | |
| 621 | rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address, |
| 622 | ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE); |
| 623 | if (rc == H_RESOURCE) |
| 624 | rc = ibmvtpm_reset_crq(ibmvtpm); |
| 625 | |
| 626 | if (rc) { |
| 627 | dev_err(dev, "Unable to register CRQ rc=%d\n", rc); |
| 628 | goto reg_crq_cleanup; |
| 629 | } |
| 630 | |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 631 | rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0, |
| 632 | tpm_ibmvtpm_driver_name, ibmvtpm); |
| 633 | if (rc) { |
| 634 | dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq); |
| 635 | goto init_irq_cleanup; |
| 636 | } |
| 637 | |
| 638 | rc = vio_enable_interrupts(vio_dev); |
| 639 | if (rc) { |
| 640 | dev_err(dev, "Error %d enabling interrupts\n", rc); |
| 641 | goto init_irq_cleanup; |
| 642 | } |
| 643 | |
Ashley Lai | b566650 | 2012-09-12 12:49:50 -0500 | [diff] [blame^] | 644 | init_waitqueue_head(&ibmvtpm->wq); |
| 645 | |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 646 | crq_q->index = 0; |
| 647 | |
| 648 | ibmvtpm->dev = dev; |
| 649 | ibmvtpm->vdev = vio_dev; |
| 650 | chip->vendor.data = (void *)ibmvtpm; |
| 651 | |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 652 | spin_lock_init(&ibmvtpm->rtce_lock); |
| 653 | |
| 654 | rc = ibmvtpm_crq_send_init(ibmvtpm); |
| 655 | if (rc) |
| 656 | goto init_irq_cleanup; |
| 657 | |
| 658 | rc = ibmvtpm_crq_get_version(ibmvtpm); |
| 659 | if (rc) |
| 660 | goto init_irq_cleanup; |
| 661 | |
| 662 | rc = ibmvtpm_crq_get_rtce_size(ibmvtpm); |
| 663 | if (rc) |
| 664 | goto init_irq_cleanup; |
| 665 | |
| 666 | return rc; |
| 667 | init_irq_cleanup: |
Ashley Lai | 132f762 | 2012-08-22 16:17:43 -0500 | [diff] [blame] | 668 | do { |
| 669 | rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address); |
| 670 | } while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1)); |
| 671 | reg_crq_cleanup: |
| 672 | dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE, |
| 673 | DMA_BIDIRECTIONAL); |
| 674 | cleanup: |
| 675 | if (ibmvtpm) { |
| 676 | if (crq_q->crq_addr) |
| 677 | free_page((unsigned long)crq_q->crq_addr); |
| 678 | kfree(ibmvtpm); |
| 679 | } |
| 680 | |
| 681 | tpm_remove_hardware(dev); |
| 682 | |
| 683 | return rc; |
| 684 | } |
| 685 | |
| 686 | static struct vio_driver ibmvtpm_driver = { |
| 687 | .id_table = tpm_ibmvtpm_device_table, |
| 688 | .probe = tpm_ibmvtpm_probe, |
| 689 | .remove = tpm_ibmvtpm_remove, |
| 690 | .get_desired_dma = tpm_ibmvtpm_get_desired_dma, |
| 691 | .name = tpm_ibmvtpm_driver_name, |
| 692 | .pm = &tpm_ibmvtpm_pm_ops, |
| 693 | }; |
| 694 | |
| 695 | /** |
| 696 | * ibmvtpm_module_init - Initialize ibm vtpm module |
| 697 | * |
| 698 | * Return value: |
| 699 | * 0 -Success |
| 700 | * Non-zero - Failure |
| 701 | */ |
| 702 | static int __init ibmvtpm_module_init(void) |
| 703 | { |
| 704 | return vio_register_driver(&ibmvtpm_driver); |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * ibmvtpm_module_exit - Teardown ibm vtpm module |
| 709 | * |
| 710 | * Return value: |
| 711 | * Nothing |
| 712 | */ |
| 713 | static void __exit ibmvtpm_module_exit(void) |
| 714 | { |
| 715 | vio_unregister_driver(&ibmvtpm_driver); |
| 716 | } |
| 717 | |
| 718 | module_init(ibmvtpm_module_init); |
| 719 | module_exit(ibmvtpm_module_exit); |
| 720 | |
| 721 | MODULE_AUTHOR("adlai@us.ibm.com"); |
| 722 | MODULE_DESCRIPTION("IBM vTPM Driver"); |
| 723 | MODULE_VERSION("1.0"); |
| 724 | MODULE_LICENSE("GPL"); |