1132f7629SAshley Lai /* 2132f7629SAshley Lai * Copyright (C) 2012 IBM Corporation 3132f7629SAshley Lai * 41a0f1b27SAshley Lai * Author: Ashley Lai <ashleydlai@gmail.com> 5132f7629SAshley Lai * 6132f7629SAshley Lai * Maintained by: <tpmdd-devel@lists.sourceforge.net> 7132f7629SAshley Lai * 8132f7629SAshley Lai * Device driver for TCG/TCPA TPM (trusted platform module). 9132f7629SAshley Lai * Specifications at www.trustedcomputinggroup.org 10132f7629SAshley Lai * 11132f7629SAshley Lai * This program is free software; you can redistribute it and/or 12132f7629SAshley Lai * modify it under the terms of the GNU General Public License as 13132f7629SAshley Lai * published by the Free Software Foundation, version 2 of the 14132f7629SAshley Lai * License. 15132f7629SAshley Lai * 16132f7629SAshley Lai */ 17132f7629SAshley Lai 18132f7629SAshley Lai #include <linux/dma-mapping.h> 19132f7629SAshley Lai #include <linux/dmapool.h> 20132f7629SAshley Lai #include <linux/slab.h> 21132f7629SAshley Lai #include <asm/vio.h> 22132f7629SAshley Lai #include <asm/irq.h> 23132f7629SAshley Lai #include <linux/types.h> 24132f7629SAshley Lai #include <linux/list.h> 25132f7629SAshley Lai #include <linux/spinlock.h> 26132f7629SAshley Lai #include <linux/interrupt.h> 27132f7629SAshley Lai #include <linux/wait.h> 28132f7629SAshley Lai #include <asm/prom.h> 29132f7629SAshley Lai 30132f7629SAshley Lai #include "tpm.h" 31132f7629SAshley Lai #include "tpm_ibmvtpm.h" 32132f7629SAshley Lai 33132f7629SAshley Lai static const char tpm_ibmvtpm_driver_name[] = "tpm_ibmvtpm"; 34132f7629SAshley Lai 350bbed20eSBill Pemberton static struct vio_device_id tpm_ibmvtpm_device_table[] = { 36132f7629SAshley Lai { "IBM,vtpm", "IBM,vtpm"}, 37132f7629SAshley Lai { "", "" } 38132f7629SAshley Lai }; 39132f7629SAshley Lai MODULE_DEVICE_TABLE(vio, tpm_ibmvtpm_device_table); 40132f7629SAshley Lai 41132f7629SAshley Lai /** 42132f7629SAshley Lai * ibmvtpm_send_crq - Send a CRQ request 43132f7629SAshley Lai * @vdev: vio device struct 44132f7629SAshley Lai * @w1: first word 45132f7629SAshley Lai * @w2: second word 46132f7629SAshley Lai * 47132f7629SAshley Lai * Return value: 48132f7629SAshley Lai * 0 -Sucess 49132f7629SAshley Lai * Non-zero - Failure 50132f7629SAshley Lai */ 51132f7629SAshley Lai static int ibmvtpm_send_crq(struct vio_dev *vdev, u64 w1, u64 w2) 52132f7629SAshley Lai { 53132f7629SAshley Lai return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, w1, w2); 54132f7629SAshley Lai } 55132f7629SAshley Lai 56132f7629SAshley Lai /** 57132f7629SAshley Lai * ibmvtpm_get_data - Retrieve ibm vtpm data 58132f7629SAshley Lai * @dev: device struct 59132f7629SAshley Lai * 60132f7629SAshley Lai * Return value: 61132f7629SAshley Lai * vtpm device struct 62132f7629SAshley Lai */ 63132f7629SAshley Lai static struct ibmvtpm_dev *ibmvtpm_get_data(const struct device *dev) 64132f7629SAshley Lai { 65132f7629SAshley Lai struct tpm_chip *chip = dev_get_drvdata(dev); 66132f7629SAshley Lai if (chip) 67775585e4SKent Yoder return (struct ibmvtpm_dev *)TPM_VPRIV(chip); 68132f7629SAshley Lai return NULL; 69132f7629SAshley Lai } 70132f7629SAshley Lai 71132f7629SAshley Lai /** 72132f7629SAshley Lai * tpm_ibmvtpm_recv - Receive data after send 73132f7629SAshley Lai * @chip: tpm chip struct 74132f7629SAshley Lai * @buf: buffer to read 75132f7629SAshley Lai * count: size of buffer 76132f7629SAshley Lai * 77132f7629SAshley Lai * Return value: 78132f7629SAshley Lai * Number of bytes read 79132f7629SAshley Lai */ 80132f7629SAshley Lai static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count) 81132f7629SAshley Lai { 82132f7629SAshley Lai struct ibmvtpm_dev *ibmvtpm; 83132f7629SAshley Lai u16 len; 84b5666502SAshley Lai int sig; 85132f7629SAshley Lai 86775585e4SKent Yoder ibmvtpm = (struct ibmvtpm_dev *)TPM_VPRIV(chip); 87132f7629SAshley Lai 88132f7629SAshley Lai if (!ibmvtpm->rtce_buf) { 89132f7629SAshley Lai dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n"); 90132f7629SAshley Lai return 0; 91132f7629SAshley Lai } 92132f7629SAshley Lai 93*6674ff14SStefan Berger sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd); 94b5666502SAshley Lai if (sig) 95b5666502SAshley Lai return -EINTR; 96132f7629SAshley Lai 97b5666502SAshley Lai len = ibmvtpm->res_len; 98b5666502SAshley Lai 99b5666502SAshley Lai if (count < len) { 100132f7629SAshley Lai dev_err(ibmvtpm->dev, 10137ab0341SJason Gunthorpe "Invalid size in recv: count=%zd, crq_size=%d\n", 102b5666502SAshley Lai count, len); 103132f7629SAshley Lai return -EIO; 104132f7629SAshley Lai } 105132f7629SAshley Lai 106132f7629SAshley Lai spin_lock(&ibmvtpm->rtce_lock); 107b5666502SAshley Lai memcpy((void *)buf, (void *)ibmvtpm->rtce_buf, len); 108b5666502SAshley Lai memset(ibmvtpm->rtce_buf, 0, len); 109b5666502SAshley Lai ibmvtpm->res_len = 0; 110132f7629SAshley Lai spin_unlock(&ibmvtpm->rtce_lock); 111132f7629SAshley Lai return len; 112132f7629SAshley Lai } 113132f7629SAshley Lai 114132f7629SAshley Lai /** 115132f7629SAshley Lai * tpm_ibmvtpm_send - Send tpm request 116132f7629SAshley Lai * @chip: tpm chip struct 117132f7629SAshley Lai * @buf: buffer contains data to send 118132f7629SAshley Lai * count: size of buffer 119132f7629SAshley Lai * 120132f7629SAshley Lai * Return value: 121132f7629SAshley Lai * Number of bytes sent 122132f7629SAshley Lai */ 123132f7629SAshley Lai static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count) 124132f7629SAshley Lai { 125132f7629SAshley Lai struct ibmvtpm_dev *ibmvtpm; 126132f7629SAshley Lai struct ibmvtpm_crq crq; 12762dfd912Sjmlatten@linux.vnet.ibm.com __be64 *word = (__be64 *)&crq; 128*6674ff14SStefan Berger int rc, sig; 129132f7629SAshley Lai 130775585e4SKent Yoder ibmvtpm = (struct ibmvtpm_dev *)TPM_VPRIV(chip); 131132f7629SAshley Lai 132132f7629SAshley Lai if (!ibmvtpm->rtce_buf) { 133132f7629SAshley Lai dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n"); 134132f7629SAshley Lai return 0; 135132f7629SAshley Lai } 136132f7629SAshley Lai 137132f7629SAshley Lai if (count > ibmvtpm->rtce_size) { 138132f7629SAshley Lai dev_err(ibmvtpm->dev, 13937ab0341SJason Gunthorpe "Invalid size in send: count=%zd, rtce_size=%d\n", 140132f7629SAshley Lai count, ibmvtpm->rtce_size); 141132f7629SAshley Lai return -EIO; 142132f7629SAshley Lai } 143132f7629SAshley Lai 144*6674ff14SStefan Berger if (ibmvtpm->tpm_processing_cmd) { 145*6674ff14SStefan Berger dev_info(ibmvtpm->dev, 146*6674ff14SStefan Berger "Need to wait for TPM to finish\n"); 147*6674ff14SStefan Berger /* wait for previous command to finish */ 148*6674ff14SStefan Berger sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd); 149*6674ff14SStefan Berger if (sig) 150*6674ff14SStefan Berger return -EINTR; 151*6674ff14SStefan Berger } 152*6674ff14SStefan Berger 153132f7629SAshley Lai spin_lock(&ibmvtpm->rtce_lock); 154*6674ff14SStefan Berger ibmvtpm->res_len = 0; 155132f7629SAshley Lai memcpy((void *)ibmvtpm->rtce_buf, (void *)buf, count); 156132f7629SAshley Lai crq.valid = (u8)IBMVTPM_VALID_CMD; 157132f7629SAshley Lai crq.msg = (u8)VTPM_TPM_COMMAND; 15862dfd912Sjmlatten@linux.vnet.ibm.com crq.len = cpu_to_be16(count); 15962dfd912Sjmlatten@linux.vnet.ibm.com crq.data = cpu_to_be32(ibmvtpm->rtce_dma_handle); 160132f7629SAshley Lai 161*6674ff14SStefan Berger /* 162*6674ff14SStefan Berger * set the processing flag before the Hcall, since we may get the 163*6674ff14SStefan Berger * result (interrupt) before even being able to check rc. 164*6674ff14SStefan Berger */ 165*6674ff14SStefan Berger ibmvtpm->tpm_processing_cmd = true; 166*6674ff14SStefan Berger 16762dfd912Sjmlatten@linux.vnet.ibm.com rc = ibmvtpm_send_crq(ibmvtpm->vdev, be64_to_cpu(word[0]), 16862dfd912Sjmlatten@linux.vnet.ibm.com be64_to_cpu(word[1])); 169132f7629SAshley Lai if (rc != H_SUCCESS) { 170132f7629SAshley Lai dev_err(ibmvtpm->dev, "tpm_ibmvtpm_send failed rc=%d\n", rc); 171132f7629SAshley Lai rc = 0; 172*6674ff14SStefan Berger ibmvtpm->tpm_processing_cmd = false; 173132f7629SAshley Lai } else 174132f7629SAshley Lai rc = count; 175132f7629SAshley Lai 176132f7629SAshley Lai spin_unlock(&ibmvtpm->rtce_lock); 177132f7629SAshley Lai return rc; 178132f7629SAshley Lai } 179132f7629SAshley Lai 180132f7629SAshley Lai static void tpm_ibmvtpm_cancel(struct tpm_chip *chip) 181132f7629SAshley Lai { 182132f7629SAshley Lai return; 183132f7629SAshley Lai } 184132f7629SAshley Lai 185132f7629SAshley Lai static u8 tpm_ibmvtpm_status(struct tpm_chip *chip) 186132f7629SAshley Lai { 187132f7629SAshley Lai return 0; 188132f7629SAshley Lai } 189132f7629SAshley Lai 190132f7629SAshley Lai /** 191132f7629SAshley Lai * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size 192132f7629SAshley Lai * @ibmvtpm: vtpm device struct 193132f7629SAshley Lai * 194132f7629SAshley Lai * Return value: 195132f7629SAshley Lai * 0 - Success 196132f7629SAshley Lai * Non-zero - Failure 197132f7629SAshley Lai */ 198132f7629SAshley Lai static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm) 199132f7629SAshley Lai { 200132f7629SAshley Lai struct ibmvtpm_crq crq; 201132f7629SAshley Lai u64 *buf = (u64 *) &crq; 202132f7629SAshley Lai int rc; 203132f7629SAshley Lai 204132f7629SAshley Lai crq.valid = (u8)IBMVTPM_VALID_CMD; 205132f7629SAshley Lai crq.msg = (u8)VTPM_GET_RTCE_BUFFER_SIZE; 206132f7629SAshley Lai 207eb71f8a5Shonclo rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]), 208eb71f8a5Shonclo cpu_to_be64(buf[1])); 209132f7629SAshley Lai if (rc != H_SUCCESS) 210132f7629SAshley Lai dev_err(ibmvtpm->dev, 211132f7629SAshley Lai "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc); 212132f7629SAshley Lai 213132f7629SAshley Lai return rc; 214132f7629SAshley Lai } 215132f7629SAshley Lai 216132f7629SAshley Lai /** 217132f7629SAshley Lai * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version 218132f7629SAshley Lai * - Note that this is vtpm version and not tpm version 219132f7629SAshley Lai * @ibmvtpm: vtpm device struct 220132f7629SAshley Lai * 221132f7629SAshley Lai * Return value: 222132f7629SAshley Lai * 0 - Success 223132f7629SAshley Lai * Non-zero - Failure 224132f7629SAshley Lai */ 225132f7629SAshley Lai static int ibmvtpm_crq_get_version(struct ibmvtpm_dev *ibmvtpm) 226132f7629SAshley Lai { 227132f7629SAshley Lai struct ibmvtpm_crq crq; 228132f7629SAshley Lai u64 *buf = (u64 *) &crq; 229132f7629SAshley Lai int rc; 230132f7629SAshley Lai 231132f7629SAshley Lai crq.valid = (u8)IBMVTPM_VALID_CMD; 232132f7629SAshley Lai crq.msg = (u8)VTPM_GET_VERSION; 233132f7629SAshley Lai 234eb71f8a5Shonclo rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]), 235eb71f8a5Shonclo cpu_to_be64(buf[1])); 236132f7629SAshley Lai if (rc != H_SUCCESS) 237132f7629SAshley Lai dev_err(ibmvtpm->dev, 238132f7629SAshley Lai "ibmvtpm_crq_get_version failed rc=%d\n", rc); 239132f7629SAshley Lai 240132f7629SAshley Lai return rc; 241132f7629SAshley Lai } 242132f7629SAshley Lai 243132f7629SAshley Lai /** 244132f7629SAshley Lai * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message 245132f7629SAshley Lai * @ibmvtpm: vtpm device struct 246132f7629SAshley Lai * 247132f7629SAshley Lai * Return value: 248132f7629SAshley Lai * 0 - Success 249132f7629SAshley Lai * Non-zero - Failure 250132f7629SAshley Lai */ 251132f7629SAshley Lai static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev *ibmvtpm) 252132f7629SAshley Lai { 253132f7629SAshley Lai int rc; 254132f7629SAshley Lai 255132f7629SAshley Lai rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_COMP_CMD, 0); 256132f7629SAshley Lai if (rc != H_SUCCESS) 257132f7629SAshley Lai dev_err(ibmvtpm->dev, 258132f7629SAshley Lai "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc); 259132f7629SAshley Lai 260132f7629SAshley Lai return rc; 261132f7629SAshley Lai } 262132f7629SAshley Lai 263132f7629SAshley Lai /** 264132f7629SAshley Lai * ibmvtpm_crq_send_init - Send a CRQ initialize message 265132f7629SAshley Lai * @ibmvtpm: vtpm device struct 266132f7629SAshley Lai * 267132f7629SAshley Lai * Return value: 268132f7629SAshley Lai * 0 - Success 269132f7629SAshley Lai * Non-zero - Failure 270132f7629SAshley Lai */ 271132f7629SAshley Lai static int ibmvtpm_crq_send_init(struct ibmvtpm_dev *ibmvtpm) 272132f7629SAshley Lai { 273132f7629SAshley Lai int rc; 274132f7629SAshley Lai 275132f7629SAshley Lai rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_CMD, 0); 276132f7629SAshley Lai if (rc != H_SUCCESS) 277132f7629SAshley Lai dev_err(ibmvtpm->dev, 278132f7629SAshley Lai "ibmvtpm_crq_send_init failed rc=%d\n", rc); 279132f7629SAshley Lai 280132f7629SAshley Lai return rc; 281132f7629SAshley Lai } 282132f7629SAshley Lai 283132f7629SAshley Lai /** 284132f7629SAshley Lai * tpm_ibmvtpm_remove - ibm vtpm remove entry point 285132f7629SAshley Lai * @vdev: vio device struct 286132f7629SAshley Lai * 287132f7629SAshley Lai * Return value: 288132f7629SAshley Lai * 0 289132f7629SAshley Lai */ 29039af33fcSBill Pemberton static int tpm_ibmvtpm_remove(struct vio_dev *vdev) 291132f7629SAshley Lai { 292132f7629SAshley Lai struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(&vdev->dev); 293afb5abc2SJarkko Sakkinen struct tpm_chip *chip = dev_get_drvdata(ibmvtpm->dev); 294132f7629SAshley Lai int rc = 0; 295132f7629SAshley Lai 296afb5abc2SJarkko Sakkinen tpm_chip_unregister(chip); 297afb5abc2SJarkko Sakkinen 298132f7629SAshley Lai free_irq(vdev->irq, ibmvtpm); 299132f7629SAshley Lai 300132f7629SAshley Lai do { 301132f7629SAshley Lai if (rc) 302132f7629SAshley Lai msleep(100); 303132f7629SAshley Lai rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); 304132f7629SAshley Lai } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); 305132f7629SAshley Lai 306132f7629SAshley Lai dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle, 307132f7629SAshley Lai CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL); 308132f7629SAshley Lai free_page((unsigned long)ibmvtpm->crq_queue.crq_addr); 309132f7629SAshley Lai 310132f7629SAshley Lai if (ibmvtpm->rtce_buf) { 311132f7629SAshley Lai dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle, 312132f7629SAshley Lai ibmvtpm->rtce_size, DMA_BIDIRECTIONAL); 313132f7629SAshley Lai kfree(ibmvtpm->rtce_buf); 314132f7629SAshley Lai } 315132f7629SAshley Lai 316132f7629SAshley Lai kfree(ibmvtpm); 317132f7629SAshley Lai 318132f7629SAshley Lai return 0; 319132f7629SAshley Lai } 320132f7629SAshley Lai 321132f7629SAshley Lai /** 322132f7629SAshley Lai * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver 323132f7629SAshley Lai * @vdev: vio device struct 324132f7629SAshley Lai * 325132f7629SAshley Lai * Return value: 326132f7629SAshley Lai * Number of bytes the driver needs to DMA map 327132f7629SAshley Lai */ 328132f7629SAshley Lai static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev *vdev) 329132f7629SAshley Lai { 330132f7629SAshley Lai struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(&vdev->dev); 33184eb186bSHon Ching (Vicky) Lo 33284eb186bSHon Ching (Vicky) Lo /* ibmvtpm initializes at probe time, so the data we are 33384eb186bSHon Ching (Vicky) Lo * asking for may not be set yet. Estimate that 4K required 33484eb186bSHon Ching (Vicky) Lo * for TCE-mapped buffer in addition to CRQ. 33584eb186bSHon Ching (Vicky) Lo */ 33684eb186bSHon Ching (Vicky) Lo if (!ibmvtpm) 33784eb186bSHon Ching (Vicky) Lo return CRQ_RES_BUF_SIZE + PAGE_SIZE; 33884eb186bSHon Ching (Vicky) Lo 339132f7629SAshley Lai return CRQ_RES_BUF_SIZE + ibmvtpm->rtce_size; 340132f7629SAshley Lai } 341132f7629SAshley Lai 342132f7629SAshley Lai /** 343132f7629SAshley Lai * tpm_ibmvtpm_suspend - Suspend 344132f7629SAshley Lai * @dev: device struct 345132f7629SAshley Lai * 346132f7629SAshley Lai * Return value: 347132f7629SAshley Lai * 0 348132f7629SAshley Lai */ 349132f7629SAshley Lai static int tpm_ibmvtpm_suspend(struct device *dev) 350132f7629SAshley Lai { 351132f7629SAshley Lai struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(dev); 352132f7629SAshley Lai struct ibmvtpm_crq crq; 353132f7629SAshley Lai u64 *buf = (u64 *) &crq; 354132f7629SAshley Lai int rc = 0; 355132f7629SAshley Lai 356132f7629SAshley Lai crq.valid = (u8)IBMVTPM_VALID_CMD; 357132f7629SAshley Lai crq.msg = (u8)VTPM_PREPARE_TO_SUSPEND; 358132f7629SAshley Lai 359eb71f8a5Shonclo rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]), 360eb71f8a5Shonclo cpu_to_be64(buf[1])); 361132f7629SAshley Lai if (rc != H_SUCCESS) 362132f7629SAshley Lai dev_err(ibmvtpm->dev, 363132f7629SAshley Lai "tpm_ibmvtpm_suspend failed rc=%d\n", rc); 364132f7629SAshley Lai 365132f7629SAshley Lai return rc; 366132f7629SAshley Lai } 367132f7629SAshley Lai 368132f7629SAshley Lai /** 369132f7629SAshley Lai * ibmvtpm_reset_crq - Reset CRQ 370132f7629SAshley Lai * @ibmvtpm: ibm vtpm struct 371132f7629SAshley Lai * 372132f7629SAshley Lai * Return value: 373132f7629SAshley Lai * 0 - Success 374132f7629SAshley Lai * Non-zero - Failure 375132f7629SAshley Lai */ 376132f7629SAshley Lai static int ibmvtpm_reset_crq(struct ibmvtpm_dev *ibmvtpm) 377132f7629SAshley Lai { 378132f7629SAshley Lai int rc = 0; 379132f7629SAshley Lai 380132f7629SAshley Lai do { 381132f7629SAshley Lai if (rc) 382132f7629SAshley Lai msleep(100); 383132f7629SAshley Lai rc = plpar_hcall_norets(H_FREE_CRQ, 384132f7629SAshley Lai ibmvtpm->vdev->unit_address); 385132f7629SAshley Lai } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); 386132f7629SAshley Lai 387132f7629SAshley Lai memset(ibmvtpm->crq_queue.crq_addr, 0, CRQ_RES_BUF_SIZE); 388132f7629SAshley Lai ibmvtpm->crq_queue.index = 0; 389132f7629SAshley Lai 390132f7629SAshley Lai return plpar_hcall_norets(H_REG_CRQ, ibmvtpm->vdev->unit_address, 391132f7629SAshley Lai ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE); 392132f7629SAshley Lai } 393132f7629SAshley Lai 394132f7629SAshley Lai /** 395132f7629SAshley Lai * tpm_ibmvtpm_resume - Resume from suspend 396132f7629SAshley Lai * @dev: device struct 397132f7629SAshley Lai * 398132f7629SAshley Lai * Return value: 399132f7629SAshley Lai * 0 400132f7629SAshley Lai */ 401132f7629SAshley Lai static int tpm_ibmvtpm_resume(struct device *dev) 402132f7629SAshley Lai { 403132f7629SAshley Lai struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(dev); 404132f7629SAshley Lai int rc = 0; 405132f7629SAshley Lai 406132f7629SAshley Lai do { 407132f7629SAshley Lai if (rc) 408132f7629SAshley Lai msleep(100); 409132f7629SAshley Lai rc = plpar_hcall_norets(H_ENABLE_CRQ, 410132f7629SAshley Lai ibmvtpm->vdev->unit_address); 411132f7629SAshley Lai } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc)); 412132f7629SAshley Lai 413132f7629SAshley Lai if (rc) { 414132f7629SAshley Lai dev_err(dev, "Error enabling ibmvtpm rc=%d\n", rc); 415132f7629SAshley Lai return rc; 416132f7629SAshley Lai } 417132f7629SAshley Lai 418b5666502SAshley Lai rc = vio_enable_interrupts(ibmvtpm->vdev); 419b5666502SAshley Lai if (rc) { 420b5666502SAshley Lai dev_err(dev, "Error vio_enable_interrupts rc=%d\n", rc); 421b5666502SAshley Lai return rc; 422b5666502SAshley Lai } 423132f7629SAshley Lai 424132f7629SAshley Lai rc = ibmvtpm_crq_send_init(ibmvtpm); 425132f7629SAshley Lai if (rc) 426132f7629SAshley Lai dev_err(dev, "Error send_init rc=%d\n", rc); 427132f7629SAshley Lai 428132f7629SAshley Lai return rc; 429132f7629SAshley Lai } 430132f7629SAshley Lai 4311f866057SStefan Berger static bool tpm_ibmvtpm_req_canceled(struct tpm_chip *chip, u8 status) 4321f866057SStefan Berger { 4331f866057SStefan Berger return (status == 0); 4341f866057SStefan Berger } 4351f866057SStefan Berger 43601ad1fa7SJason Gunthorpe static const struct tpm_class_ops tpm_ibmvtpm = { 437132f7629SAshley Lai .recv = tpm_ibmvtpm_recv, 438132f7629SAshley Lai .send = tpm_ibmvtpm_send, 439132f7629SAshley Lai .cancel = tpm_ibmvtpm_cancel, 440132f7629SAshley Lai .status = tpm_ibmvtpm_status, 441132f7629SAshley Lai .req_complete_mask = 0, 442132f7629SAshley Lai .req_complete_val = 0, 4431f866057SStefan Berger .req_canceled = tpm_ibmvtpm_req_canceled, 444132f7629SAshley Lai }; 445132f7629SAshley Lai 446132f7629SAshley Lai static const struct dev_pm_ops tpm_ibmvtpm_pm_ops = { 447132f7629SAshley Lai .suspend = tpm_ibmvtpm_suspend, 448132f7629SAshley Lai .resume = tpm_ibmvtpm_resume, 449132f7629SAshley Lai }; 450132f7629SAshley Lai 451132f7629SAshley Lai /** 452132f7629SAshley Lai * ibmvtpm_crq_get_next - Get next responded crq 453132f7629SAshley Lai * @ibmvtpm vtpm device struct 454132f7629SAshley Lai * 455132f7629SAshley Lai * Return value: 456132f7629SAshley Lai * vtpm crq pointer 457132f7629SAshley Lai */ 458132f7629SAshley Lai static struct ibmvtpm_crq *ibmvtpm_crq_get_next(struct ibmvtpm_dev *ibmvtpm) 459132f7629SAshley Lai { 460132f7629SAshley Lai struct ibmvtpm_crq_queue *crq_q = &ibmvtpm->crq_queue; 461132f7629SAshley Lai struct ibmvtpm_crq *crq = &crq_q->crq_addr[crq_q->index]; 462132f7629SAshley Lai 463132f7629SAshley Lai if (crq->valid & VTPM_MSG_RES) { 464132f7629SAshley Lai if (++crq_q->index == crq_q->num_entry) 465132f7629SAshley Lai crq_q->index = 0; 466b5666502SAshley Lai smp_rmb(); 467132f7629SAshley Lai } else 468132f7629SAshley Lai crq = NULL; 469132f7629SAshley Lai return crq; 470132f7629SAshley Lai } 471132f7629SAshley Lai 472132f7629SAshley Lai /** 473132f7629SAshley Lai * ibmvtpm_crq_process - Process responded crq 474132f7629SAshley Lai * @crq crq to be processed 475132f7629SAshley Lai * @ibmvtpm vtpm device struct 476132f7629SAshley Lai * 477132f7629SAshley Lai * Return value: 478132f7629SAshley Lai * Nothing 479132f7629SAshley Lai */ 480132f7629SAshley Lai static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq, 481132f7629SAshley Lai struct ibmvtpm_dev *ibmvtpm) 482132f7629SAshley Lai { 483132f7629SAshley Lai int rc = 0; 484132f7629SAshley Lai 485132f7629SAshley Lai switch (crq->valid) { 486132f7629SAshley Lai case VALID_INIT_CRQ: 487132f7629SAshley Lai switch (crq->msg) { 488132f7629SAshley Lai case INIT_CRQ_RES: 489132f7629SAshley Lai dev_info(ibmvtpm->dev, "CRQ initialized\n"); 490132f7629SAshley Lai rc = ibmvtpm_crq_send_init_complete(ibmvtpm); 491132f7629SAshley Lai if (rc) 492132f7629SAshley Lai dev_err(ibmvtpm->dev, "Unable to send CRQ init complete rc=%d\n", rc); 493132f7629SAshley Lai return; 494132f7629SAshley Lai case INIT_CRQ_COMP_RES: 495132f7629SAshley Lai dev_info(ibmvtpm->dev, 496132f7629SAshley Lai "CRQ initialization completed\n"); 497132f7629SAshley Lai return; 498132f7629SAshley Lai default: 499132f7629SAshley Lai dev_err(ibmvtpm->dev, "Unknown crq message type: %d\n", crq->msg); 500132f7629SAshley Lai return; 501132f7629SAshley Lai } 502132f7629SAshley Lai case IBMVTPM_VALID_CMD: 503132f7629SAshley Lai switch (crq->msg) { 504132f7629SAshley Lai case VTPM_GET_RTCE_BUFFER_SIZE_RES: 505eb71f8a5Shonclo if (be16_to_cpu(crq->len) <= 0) { 506132f7629SAshley Lai dev_err(ibmvtpm->dev, "Invalid rtce size\n"); 507132f7629SAshley Lai return; 508132f7629SAshley Lai } 509eb71f8a5Shonclo ibmvtpm->rtce_size = be16_to_cpu(crq->len); 510132f7629SAshley Lai ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size, 51160ecd86cSHon Ching \(Vicky\) Lo GFP_ATOMIC); 512132f7629SAshley Lai if (!ibmvtpm->rtce_buf) { 513132f7629SAshley Lai dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n"); 514132f7629SAshley Lai return; 515132f7629SAshley Lai } 516132f7629SAshley Lai 517132f7629SAshley Lai ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev, 518132f7629SAshley Lai ibmvtpm->rtce_buf, ibmvtpm->rtce_size, 519132f7629SAshley Lai DMA_BIDIRECTIONAL); 520132f7629SAshley Lai 521132f7629SAshley Lai if (dma_mapping_error(ibmvtpm->dev, 522132f7629SAshley Lai ibmvtpm->rtce_dma_handle)) { 523132f7629SAshley Lai kfree(ibmvtpm->rtce_buf); 524132f7629SAshley Lai ibmvtpm->rtce_buf = NULL; 525132f7629SAshley Lai dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n"); 526132f7629SAshley Lai } 527132f7629SAshley Lai 528132f7629SAshley Lai return; 529132f7629SAshley Lai case VTPM_GET_VERSION_RES: 530eb71f8a5Shonclo ibmvtpm->vtpm_version = be32_to_cpu(crq->data); 531132f7629SAshley Lai return; 532132f7629SAshley Lai case VTPM_TPM_COMMAND_RES: 533b5666502SAshley Lai /* len of the data in rtce buffer */ 534eb71f8a5Shonclo ibmvtpm->res_len = be16_to_cpu(crq->len); 535*6674ff14SStefan Berger ibmvtpm->tpm_processing_cmd = false; 536b5666502SAshley Lai wake_up_interruptible(&ibmvtpm->wq); 537132f7629SAshley Lai return; 538132f7629SAshley Lai default: 539132f7629SAshley Lai return; 540132f7629SAshley Lai } 541132f7629SAshley Lai } 542132f7629SAshley Lai return; 543132f7629SAshley Lai } 544132f7629SAshley Lai 545132f7629SAshley Lai /** 546132f7629SAshley Lai * ibmvtpm_interrupt - Interrupt handler 547132f7629SAshley Lai * @irq: irq number to handle 548132f7629SAshley Lai * @vtpm_instance: vtpm that received interrupt 549132f7629SAshley Lai * 550132f7629SAshley Lai * Returns: 551132f7629SAshley Lai * IRQ_HANDLED 552132f7629SAshley Lai **/ 553132f7629SAshley Lai static irqreturn_t ibmvtpm_interrupt(int irq, void *vtpm_instance) 554132f7629SAshley Lai { 555132f7629SAshley Lai struct ibmvtpm_dev *ibmvtpm = (struct ibmvtpm_dev *) vtpm_instance; 556132f7629SAshley Lai struct ibmvtpm_crq *crq; 557132f7629SAshley Lai 558b5666502SAshley Lai /* while loop is needed for initial setup (get version and 559b5666502SAshley Lai * get rtce_size). There should be only one tpm request at any 560b5666502SAshley Lai * given time. 561b5666502SAshley Lai */ 562132f7629SAshley Lai while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) { 563132f7629SAshley Lai ibmvtpm_crq_process(crq, ibmvtpm); 564132f7629SAshley Lai crq->valid = 0; 565b5666502SAshley Lai smp_wmb(); 566132f7629SAshley Lai } 567132f7629SAshley Lai 568b5666502SAshley Lai return IRQ_HANDLED; 569132f7629SAshley Lai } 570132f7629SAshley Lai 571132f7629SAshley Lai /** 572132f7629SAshley Lai * tpm_ibmvtpm_probe - ibm vtpm initialize entry point 573132f7629SAshley Lai * @vio_dev: vio device struct 574132f7629SAshley Lai * @id: vio device id struct 575132f7629SAshley Lai * 576132f7629SAshley Lai * Return value: 577132f7629SAshley Lai * 0 - Success 578132f7629SAshley Lai * Non-zero - Failure 579132f7629SAshley Lai */ 580afc6d369SBill Pemberton static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev, 581132f7629SAshley Lai const struct vio_device_id *id) 582132f7629SAshley Lai { 583132f7629SAshley Lai struct ibmvtpm_dev *ibmvtpm; 584132f7629SAshley Lai struct device *dev = &vio_dev->dev; 585132f7629SAshley Lai struct ibmvtpm_crq_queue *crq_q; 586132f7629SAshley Lai struct tpm_chip *chip; 587132f7629SAshley Lai int rc = -ENOMEM, rc1; 588132f7629SAshley Lai 589afb5abc2SJarkko Sakkinen chip = tpmm_chip_alloc(dev, &tpm_ibmvtpm); 590afb5abc2SJarkko Sakkinen if (IS_ERR(chip)) 591afb5abc2SJarkko Sakkinen return PTR_ERR(chip); 592132f7629SAshley Lai 593132f7629SAshley Lai ibmvtpm = kzalloc(sizeof(struct ibmvtpm_dev), GFP_KERNEL); 594132f7629SAshley Lai if (!ibmvtpm) { 595132f7629SAshley Lai dev_err(dev, "kzalloc for ibmvtpm failed\n"); 596132f7629SAshley Lai goto cleanup; 597132f7629SAshley Lai } 598132f7629SAshley Lai 5999d75f089SHon Ching \(Vicky\) Lo ibmvtpm->dev = dev; 6009d75f089SHon Ching \(Vicky\) Lo ibmvtpm->vdev = vio_dev; 6019d75f089SHon Ching \(Vicky\) Lo 602132f7629SAshley Lai crq_q = &ibmvtpm->crq_queue; 603132f7629SAshley Lai crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL); 604132f7629SAshley Lai if (!crq_q->crq_addr) { 605132f7629SAshley Lai dev_err(dev, "Unable to allocate memory for crq_addr\n"); 606132f7629SAshley Lai goto cleanup; 607132f7629SAshley Lai } 608132f7629SAshley Lai 609132f7629SAshley Lai crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr); 610132f7629SAshley Lai ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr, 611132f7629SAshley Lai CRQ_RES_BUF_SIZE, 612132f7629SAshley Lai DMA_BIDIRECTIONAL); 613132f7629SAshley Lai 614132f7629SAshley Lai if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) { 615132f7629SAshley Lai dev_err(dev, "dma mapping failed\n"); 616132f7629SAshley Lai goto cleanup; 617132f7629SAshley Lai } 618132f7629SAshley Lai 619132f7629SAshley Lai rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address, 620132f7629SAshley Lai ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE); 621132f7629SAshley Lai if (rc == H_RESOURCE) 622132f7629SAshley Lai rc = ibmvtpm_reset_crq(ibmvtpm); 623132f7629SAshley Lai 624132f7629SAshley Lai if (rc) { 625132f7629SAshley Lai dev_err(dev, "Unable to register CRQ rc=%d\n", rc); 626132f7629SAshley Lai goto reg_crq_cleanup; 627132f7629SAshley Lai } 628132f7629SAshley Lai 629132f7629SAshley Lai rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0, 630132f7629SAshley Lai tpm_ibmvtpm_driver_name, ibmvtpm); 631132f7629SAshley Lai if (rc) { 632132f7629SAshley Lai dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq); 633132f7629SAshley Lai goto init_irq_cleanup; 634132f7629SAshley Lai } 635132f7629SAshley Lai 636132f7629SAshley Lai rc = vio_enable_interrupts(vio_dev); 637132f7629SAshley Lai if (rc) { 638132f7629SAshley Lai dev_err(dev, "Error %d enabling interrupts\n", rc); 639132f7629SAshley Lai goto init_irq_cleanup; 640132f7629SAshley Lai } 641132f7629SAshley Lai 642b5666502SAshley Lai init_waitqueue_head(&ibmvtpm->wq); 643b5666502SAshley Lai 644132f7629SAshley Lai crq_q->index = 0; 645132f7629SAshley Lai 646775585e4SKent Yoder TPM_VPRIV(chip) = (void *)ibmvtpm; 647132f7629SAshley Lai 648132f7629SAshley Lai spin_lock_init(&ibmvtpm->rtce_lock); 649132f7629SAshley Lai 650132f7629SAshley Lai rc = ibmvtpm_crq_send_init(ibmvtpm); 651132f7629SAshley Lai if (rc) 652132f7629SAshley Lai goto init_irq_cleanup; 653132f7629SAshley Lai 654132f7629SAshley Lai rc = ibmvtpm_crq_get_version(ibmvtpm); 655132f7629SAshley Lai if (rc) 656132f7629SAshley Lai goto init_irq_cleanup; 657132f7629SAshley Lai 658132f7629SAshley Lai rc = ibmvtpm_crq_get_rtce_size(ibmvtpm); 659132f7629SAshley Lai if (rc) 660132f7629SAshley Lai goto init_irq_cleanup; 661132f7629SAshley Lai 662afb5abc2SJarkko Sakkinen return tpm_chip_register(chip); 663132f7629SAshley Lai init_irq_cleanup: 664132f7629SAshley Lai do { 665132f7629SAshley Lai rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address); 666132f7629SAshley Lai } while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1)); 667132f7629SAshley Lai reg_crq_cleanup: 668132f7629SAshley Lai dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE, 669132f7629SAshley Lai DMA_BIDIRECTIONAL); 670132f7629SAshley Lai cleanup: 671132f7629SAshley Lai if (ibmvtpm) { 672132f7629SAshley Lai if (crq_q->crq_addr) 673132f7629SAshley Lai free_page((unsigned long)crq_q->crq_addr); 674132f7629SAshley Lai kfree(ibmvtpm); 675132f7629SAshley Lai } 676132f7629SAshley Lai 677132f7629SAshley Lai return rc; 678132f7629SAshley Lai } 679132f7629SAshley Lai 680132f7629SAshley Lai static struct vio_driver ibmvtpm_driver = { 681132f7629SAshley Lai .id_table = tpm_ibmvtpm_device_table, 682132f7629SAshley Lai .probe = tpm_ibmvtpm_probe, 683132f7629SAshley Lai .remove = tpm_ibmvtpm_remove, 684132f7629SAshley Lai .get_desired_dma = tpm_ibmvtpm_get_desired_dma, 685132f7629SAshley Lai .name = tpm_ibmvtpm_driver_name, 686132f7629SAshley Lai .pm = &tpm_ibmvtpm_pm_ops, 687132f7629SAshley Lai }; 688132f7629SAshley Lai 689132f7629SAshley Lai /** 690132f7629SAshley Lai * ibmvtpm_module_init - Initialize ibm vtpm module 691132f7629SAshley Lai * 692132f7629SAshley Lai * Return value: 693132f7629SAshley Lai * 0 -Success 694132f7629SAshley Lai * Non-zero - Failure 695132f7629SAshley Lai */ 696132f7629SAshley Lai static int __init ibmvtpm_module_init(void) 697132f7629SAshley Lai { 698132f7629SAshley Lai return vio_register_driver(&ibmvtpm_driver); 699132f7629SAshley Lai } 700132f7629SAshley Lai 701132f7629SAshley Lai /** 702132f7629SAshley Lai * ibmvtpm_module_exit - Teardown ibm vtpm module 703132f7629SAshley Lai * 704132f7629SAshley Lai * Return value: 705132f7629SAshley Lai * Nothing 706132f7629SAshley Lai */ 707132f7629SAshley Lai static void __exit ibmvtpm_module_exit(void) 708132f7629SAshley Lai { 709132f7629SAshley Lai vio_unregister_driver(&ibmvtpm_driver); 710132f7629SAshley Lai } 711132f7629SAshley Lai 712132f7629SAshley Lai module_init(ibmvtpm_module_init); 713132f7629SAshley Lai module_exit(ibmvtpm_module_exit); 714132f7629SAshley Lai 715132f7629SAshley Lai MODULE_AUTHOR("adlai@us.ibm.com"); 716132f7629SAshley Lai MODULE_DESCRIPTION("IBM vTPM Driver"); 717132f7629SAshley Lai MODULE_VERSION("1.0"); 718132f7629SAshley Lai MODULE_LICENSE("GPL"); 719