1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2132f7629SAshley Lai /* 3eba5cf3dSGeorge Wilson * Copyright (C) 2012-2020 IBM Corporation 4132f7629SAshley Lai * 51a0f1b27SAshley Lai * Author: Ashley Lai <ashleydlai@gmail.com> 6132f7629SAshley Lai * 7132f7629SAshley Lai * Maintained by: <tpmdd-devel@lists.sourceforge.net> 8132f7629SAshley Lai * 9132f7629SAshley Lai * Device driver for TCG/TCPA TPM (trusted platform module). 10132f7629SAshley Lai * Specifications at www.trustedcomputinggroup.org 11132f7629SAshley Lai */ 12132f7629SAshley Lai 13132f7629SAshley Lai #include <linux/dma-mapping.h> 14132f7629SAshley Lai #include <linux/dmapool.h> 15132f7629SAshley Lai #include <linux/slab.h> 16132f7629SAshley Lai #include <asm/vio.h> 17132f7629SAshley Lai #include <asm/irq.h> 18132f7629SAshley Lai #include <linux/types.h> 19132f7629SAshley Lai #include <linux/list.h> 20132f7629SAshley Lai #include <linux/spinlock.h> 21132f7629SAshley Lai #include <linux/interrupt.h> 22132f7629SAshley Lai #include <linux/wait.h> 23132f7629SAshley Lai #include <asm/prom.h> 24132f7629SAshley Lai 25132f7629SAshley Lai #include "tpm.h" 26132f7629SAshley Lai #include "tpm_ibmvtpm.h" 27132f7629SAshley Lai 28132f7629SAshley Lai static const char tpm_ibmvtpm_driver_name[] = "tpm_ibmvtpm"; 29132f7629SAshley Lai 30c2a9c4bfSArvind Yadav static const struct vio_device_id tpm_ibmvtpm_device_table[] = { 31132f7629SAshley Lai { "IBM,vtpm", "IBM,vtpm"}, 3218b3670dSStefan Berger { "IBM,vtpm", "IBM,vtpm20"}, 33132f7629SAshley Lai { "", "" } 34132f7629SAshley Lai }; 35132f7629SAshley Lai MODULE_DEVICE_TABLE(vio, tpm_ibmvtpm_device_table); 36132f7629SAshley Lai 37132f7629SAshley Lai /** 3809c573abSTomas Winkler * ibmvtpm_send_crq_word() - Send a CRQ request 39fb154e0eSMichal Suchanek * @vdev: vio device struct 40fb154e0eSMichal Suchanek * @w1: pre-constructed first word of tpm crq (second word is reserved) 41fb154e0eSMichal Suchanek * 42fb154e0eSMichal Suchanek * Return: 43fb154e0eSMichal Suchanek * 0 - Success 44fb154e0eSMichal Suchanek * Non-zero - Failure 45fb154e0eSMichal Suchanek */ 46fb154e0eSMichal Suchanek static int ibmvtpm_send_crq_word(struct vio_dev *vdev, u64 w1) 47fb154e0eSMichal Suchanek { 48fb154e0eSMichal Suchanek return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, w1, 0); 49fb154e0eSMichal Suchanek } 50fb154e0eSMichal Suchanek 51fb154e0eSMichal Suchanek /** 5209c573abSTomas Winkler * ibmvtpm_send_crq() - Send a CRQ request 5393c12f29SWinkler, Tomas * 54132f7629SAshley Lai * @vdev: vio device struct 55fb154e0eSMichal Suchanek * @valid: Valid field 56fb154e0eSMichal Suchanek * @msg: Type field 57fb154e0eSMichal Suchanek * @len: Length field 58fb154e0eSMichal Suchanek * @data: Data field 59fb154e0eSMichal Suchanek * 60fb154e0eSMichal Suchanek * The ibmvtpm crq is defined as follows: 61fb154e0eSMichal Suchanek * 62fb154e0eSMichal Suchanek * Byte | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 63fb154e0eSMichal Suchanek * ----------------------------------------------------------------------- 64fb154e0eSMichal Suchanek * Word0 | Valid | Type | Length | Data 65fb154e0eSMichal Suchanek * ----------------------------------------------------------------------- 66fb154e0eSMichal Suchanek * Word1 | Reserved 67fb154e0eSMichal Suchanek * ----------------------------------------------------------------------- 68fb154e0eSMichal Suchanek * 69fb154e0eSMichal Suchanek * Which matches the following structure (on bigendian host): 70fb154e0eSMichal Suchanek * 71fb154e0eSMichal Suchanek * struct ibmvtpm_crq { 72fb154e0eSMichal Suchanek * u8 valid; 73fb154e0eSMichal Suchanek * u8 msg; 74fb154e0eSMichal Suchanek * __be16 len; 75fb154e0eSMichal Suchanek * __be32 data; 76fb154e0eSMichal Suchanek * __be64 reserved; 77fb154e0eSMichal Suchanek * } __attribute__((packed, aligned(8))); 78fb154e0eSMichal Suchanek * 79fb154e0eSMichal Suchanek * However, the value is passed in a register so just compute the numeric value 80fb154e0eSMichal Suchanek * to load into the register avoiding byteswap altogether. Endian only affects 81fb154e0eSMichal Suchanek * memory loads and stores - registers are internally represented the same. 82132f7629SAshley Lai * 8393c12f29SWinkler, Tomas * Return: 84fb154e0eSMichal Suchanek * 0 (H_SUCCESS) - Success 85132f7629SAshley Lai * Non-zero - Failure 86132f7629SAshley Lai */ 87fb154e0eSMichal Suchanek static int ibmvtpm_send_crq(struct vio_dev *vdev, 88fb154e0eSMichal Suchanek u8 valid, u8 msg, u16 len, u32 data) 89132f7629SAshley Lai { 90fb154e0eSMichal Suchanek u64 w1 = ((u64)valid << 56) | ((u64)msg << 48) | ((u64)len << 32) | 91fb154e0eSMichal Suchanek (u64)data; 92fb154e0eSMichal Suchanek return ibmvtpm_send_crq_word(vdev, w1); 93132f7629SAshley Lai } 94132f7629SAshley Lai 95132f7629SAshley Lai /** 96132f7629SAshley Lai * tpm_ibmvtpm_recv - Receive data after send 9793c12f29SWinkler, Tomas * 98132f7629SAshley Lai * @chip: tpm chip struct 99132f7629SAshley Lai * @buf: buffer to read 10093c12f29SWinkler, Tomas * @count: size of buffer 101132f7629SAshley Lai * 10293c12f29SWinkler, Tomas * Return: 103132f7629SAshley Lai * Number of bytes read 104132f7629SAshley Lai */ 105132f7629SAshley Lai static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count) 106132f7629SAshley Lai { 1079e0d39d8SChristophe Ricard struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev); 108132f7629SAshley Lai u16 len; 109132f7629SAshley Lai 110132f7629SAshley Lai if (!ibmvtpm->rtce_buf) { 111132f7629SAshley Lai dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n"); 112132f7629SAshley Lai return 0; 113132f7629SAshley Lai } 114132f7629SAshley Lai 115b5666502SAshley Lai len = ibmvtpm->res_len; 116b5666502SAshley Lai 117b5666502SAshley Lai if (count < len) { 118132f7629SAshley Lai dev_err(ibmvtpm->dev, 11937ab0341SJason Gunthorpe "Invalid size in recv: count=%zd, crq_size=%d\n", 120b5666502SAshley Lai count, len); 121132f7629SAshley Lai return -EIO; 122132f7629SAshley Lai } 123132f7629SAshley Lai 124132f7629SAshley Lai spin_lock(&ibmvtpm->rtce_lock); 125b5666502SAshley Lai memcpy((void *)buf, (void *)ibmvtpm->rtce_buf, len); 126b5666502SAshley Lai memset(ibmvtpm->rtce_buf, 0, len); 127b5666502SAshley Lai ibmvtpm->res_len = 0; 128132f7629SAshley Lai spin_unlock(&ibmvtpm->rtce_lock); 129132f7629SAshley Lai return len; 130132f7629SAshley Lai } 131132f7629SAshley Lai 132132f7629SAshley Lai /** 133eba5cf3dSGeorge Wilson * ibmvtpm_crq_send_init - Send a CRQ initialize message 134eba5cf3dSGeorge Wilson * @ibmvtpm: vtpm device struct 135eba5cf3dSGeorge Wilson * 136eba5cf3dSGeorge Wilson * Return: 137eba5cf3dSGeorge Wilson * 0 on success. 138eba5cf3dSGeorge Wilson * Non-zero on failure. 139eba5cf3dSGeorge Wilson */ 140eba5cf3dSGeorge Wilson static int ibmvtpm_crq_send_init(struct ibmvtpm_dev *ibmvtpm) 141eba5cf3dSGeorge Wilson { 142eba5cf3dSGeorge Wilson int rc; 143eba5cf3dSGeorge Wilson 144eba5cf3dSGeorge Wilson rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_CMD); 145eba5cf3dSGeorge Wilson if (rc != H_SUCCESS) 146eba5cf3dSGeorge Wilson dev_err(ibmvtpm->dev, 147eba5cf3dSGeorge Wilson "%s failed rc=%d\n", __func__, rc); 148eba5cf3dSGeorge Wilson 149eba5cf3dSGeorge Wilson return rc; 150eba5cf3dSGeorge Wilson } 151eba5cf3dSGeorge Wilson 152eba5cf3dSGeorge Wilson /** 153eba5cf3dSGeorge Wilson * tpm_ibmvtpm_resume - Resume from suspend 154eba5cf3dSGeorge Wilson * 155eba5cf3dSGeorge Wilson * @dev: device struct 156eba5cf3dSGeorge Wilson * 157eba5cf3dSGeorge Wilson * Return: Always 0. 158eba5cf3dSGeorge Wilson */ 159eba5cf3dSGeorge Wilson static int tpm_ibmvtpm_resume(struct device *dev) 160eba5cf3dSGeorge Wilson { 161eba5cf3dSGeorge Wilson struct tpm_chip *chip = dev_get_drvdata(dev); 162eba5cf3dSGeorge Wilson struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev); 163eba5cf3dSGeorge Wilson int rc = 0; 164eba5cf3dSGeorge Wilson 165eba5cf3dSGeorge Wilson do { 166eba5cf3dSGeorge Wilson if (rc) 167eba5cf3dSGeorge Wilson msleep(100); 168eba5cf3dSGeorge Wilson rc = plpar_hcall_norets(H_ENABLE_CRQ, 169eba5cf3dSGeorge Wilson ibmvtpm->vdev->unit_address); 170eba5cf3dSGeorge Wilson } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc)); 171eba5cf3dSGeorge Wilson 172eba5cf3dSGeorge Wilson if (rc) { 173eba5cf3dSGeorge Wilson dev_err(dev, "Error enabling ibmvtpm rc=%d\n", rc); 174eba5cf3dSGeorge Wilson return rc; 175eba5cf3dSGeorge Wilson } 176eba5cf3dSGeorge Wilson 177eba5cf3dSGeorge Wilson rc = vio_enable_interrupts(ibmvtpm->vdev); 178eba5cf3dSGeorge Wilson if (rc) { 179eba5cf3dSGeorge Wilson dev_err(dev, "Error vio_enable_interrupts rc=%d\n", rc); 180eba5cf3dSGeorge Wilson return rc; 181eba5cf3dSGeorge Wilson } 182eba5cf3dSGeorge Wilson 183eba5cf3dSGeorge Wilson rc = ibmvtpm_crq_send_init(ibmvtpm); 184eba5cf3dSGeorge Wilson if (rc) 185eba5cf3dSGeorge Wilson dev_err(dev, "Error send_init rc=%d\n", rc); 186eba5cf3dSGeorge Wilson 187eba5cf3dSGeorge Wilson return rc; 188eba5cf3dSGeorge Wilson } 189eba5cf3dSGeorge Wilson 190eba5cf3dSGeorge Wilson /** 191f5595f5bSJarkko Sakkinen * tpm_ibmvtpm_send() - Send a TPM command 192132f7629SAshley Lai * @chip: tpm chip struct 193132f7629SAshley Lai * @buf: buffer contains data to send 19493c12f29SWinkler, Tomas * @count: size of buffer 195132f7629SAshley Lai * 19693c12f29SWinkler, Tomas * Return: 197f5595f5bSJarkko Sakkinen * 0 on success, 198f5595f5bSJarkko Sakkinen * -errno on error 199132f7629SAshley Lai */ 200132f7629SAshley Lai static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count) 201132f7629SAshley Lai { 2029e0d39d8SChristophe Ricard struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev); 203eba5cf3dSGeorge Wilson bool retry = true; 2046674ff14SStefan Berger int rc, sig; 205132f7629SAshley Lai 206132f7629SAshley Lai if (!ibmvtpm->rtce_buf) { 207132f7629SAshley Lai dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n"); 208132f7629SAshley Lai return 0; 209132f7629SAshley Lai } 210132f7629SAshley Lai 211132f7629SAshley Lai if (count > ibmvtpm->rtce_size) { 212132f7629SAshley Lai dev_err(ibmvtpm->dev, 21337ab0341SJason Gunthorpe "Invalid size in send: count=%zd, rtce_size=%d\n", 214132f7629SAshley Lai count, ibmvtpm->rtce_size); 215132f7629SAshley Lai return -EIO; 216132f7629SAshley Lai } 217132f7629SAshley Lai 2186674ff14SStefan Berger if (ibmvtpm->tpm_processing_cmd) { 2196674ff14SStefan Berger dev_info(ibmvtpm->dev, 2206674ff14SStefan Berger "Need to wait for TPM to finish\n"); 2216674ff14SStefan Berger /* wait for previous command to finish */ 2226674ff14SStefan Berger sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd); 2236674ff14SStefan Berger if (sig) 2246674ff14SStefan Berger return -EINTR; 2256674ff14SStefan Berger } 2266674ff14SStefan Berger 227132f7629SAshley Lai spin_lock(&ibmvtpm->rtce_lock); 2286674ff14SStefan Berger ibmvtpm->res_len = 0; 229132f7629SAshley Lai memcpy((void *)ibmvtpm->rtce_buf, (void *)buf, count); 230132f7629SAshley Lai 2316674ff14SStefan Berger /* 2326674ff14SStefan Berger * set the processing flag before the Hcall, since we may get the 2336674ff14SStefan Berger * result (interrupt) before even being able to check rc. 2346674ff14SStefan Berger */ 235*047d4226SStefan Berger ibmvtpm->tpm_processing_cmd = 1; 2366674ff14SStefan Berger 237eba5cf3dSGeorge Wilson again: 238fb154e0eSMichal Suchanek rc = ibmvtpm_send_crq(ibmvtpm->vdev, 239fb154e0eSMichal Suchanek IBMVTPM_VALID_CMD, VTPM_TPM_COMMAND, 240fb154e0eSMichal Suchanek count, ibmvtpm->rtce_dma_handle); 241132f7629SAshley Lai if (rc != H_SUCCESS) { 242eba5cf3dSGeorge Wilson /* 243eba5cf3dSGeorge Wilson * H_CLOSED can be returned after LPM resume. Call 244eba5cf3dSGeorge Wilson * tpm_ibmvtpm_resume() to re-enable the CRQ then retry 245eba5cf3dSGeorge Wilson * ibmvtpm_send_crq() once before failing. 246eba5cf3dSGeorge Wilson */ 247eba5cf3dSGeorge Wilson if (rc == H_CLOSED && retry) { 248eba5cf3dSGeorge Wilson tpm_ibmvtpm_resume(ibmvtpm->dev); 249eba5cf3dSGeorge Wilson retry = false; 250eba5cf3dSGeorge Wilson goto again; 251eba5cf3dSGeorge Wilson } 252132f7629SAshley Lai dev_err(ibmvtpm->dev, "tpm_ibmvtpm_send failed rc=%d\n", rc); 253*047d4226SStefan Berger ibmvtpm->tpm_processing_cmd = 0; 254eba5cf3dSGeorge Wilson } 255132f7629SAshley Lai 256132f7629SAshley Lai spin_unlock(&ibmvtpm->rtce_lock); 257eba5cf3dSGeorge Wilson return 0; 258132f7629SAshley Lai } 259132f7629SAshley Lai 260132f7629SAshley Lai static void tpm_ibmvtpm_cancel(struct tpm_chip *chip) 261132f7629SAshley Lai { 262132f7629SAshley Lai return; 263132f7629SAshley Lai } 264132f7629SAshley Lai 265132f7629SAshley Lai static u8 tpm_ibmvtpm_status(struct tpm_chip *chip) 266132f7629SAshley Lai { 267*047d4226SStefan Berger struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev); 268*047d4226SStefan Berger 269*047d4226SStefan Berger return ibmvtpm->tpm_processing_cmd; 270132f7629SAshley Lai } 271132f7629SAshley Lai 272132f7629SAshley Lai /** 273132f7629SAshley Lai * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size 27493c12f29SWinkler, Tomas * 275132f7629SAshley Lai * @ibmvtpm: vtpm device struct 276132f7629SAshley Lai * 27793c12f29SWinkler, Tomas * Return: 27893c12f29SWinkler, Tomas * 0 on success. 27993c12f29SWinkler, Tomas * Non-zero on failure. 280132f7629SAshley Lai */ 281132f7629SAshley Lai static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm) 282132f7629SAshley Lai { 283132f7629SAshley Lai int rc; 284132f7629SAshley Lai 285fb154e0eSMichal Suchanek rc = ibmvtpm_send_crq(ibmvtpm->vdev, 286fb154e0eSMichal Suchanek IBMVTPM_VALID_CMD, VTPM_GET_RTCE_BUFFER_SIZE, 0, 0); 287132f7629SAshley Lai if (rc != H_SUCCESS) 288132f7629SAshley Lai dev_err(ibmvtpm->dev, 289132f7629SAshley Lai "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc); 290132f7629SAshley Lai 291132f7629SAshley Lai return rc; 292132f7629SAshley Lai } 293132f7629SAshley Lai 294132f7629SAshley Lai /** 295132f7629SAshley Lai * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version 296132f7629SAshley Lai * - Note that this is vtpm version and not tpm version 29793c12f29SWinkler, Tomas * 298132f7629SAshley Lai * @ibmvtpm: vtpm device struct 299132f7629SAshley Lai * 30093c12f29SWinkler, Tomas * Return: 30193c12f29SWinkler, Tomas * 0 on success. 30293c12f29SWinkler, Tomas * Non-zero on failure. 303132f7629SAshley Lai */ 304132f7629SAshley Lai static int ibmvtpm_crq_get_version(struct ibmvtpm_dev *ibmvtpm) 305132f7629SAshley Lai { 306132f7629SAshley Lai int rc; 307132f7629SAshley Lai 308fb154e0eSMichal Suchanek rc = ibmvtpm_send_crq(ibmvtpm->vdev, 309fb154e0eSMichal Suchanek IBMVTPM_VALID_CMD, VTPM_GET_VERSION, 0, 0); 310132f7629SAshley Lai if (rc != H_SUCCESS) 311132f7629SAshley Lai dev_err(ibmvtpm->dev, 312132f7629SAshley Lai "ibmvtpm_crq_get_version failed rc=%d\n", rc); 313132f7629SAshley Lai 314132f7629SAshley Lai return rc; 315132f7629SAshley Lai } 316132f7629SAshley Lai 317132f7629SAshley Lai /** 318132f7629SAshley Lai * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message 319132f7629SAshley Lai * @ibmvtpm: vtpm device struct 320132f7629SAshley Lai * 32193c12f29SWinkler, Tomas * Return: 32293c12f29SWinkler, Tomas * 0 on success. 32393c12f29SWinkler, Tomas * Non-zero on failure. 324132f7629SAshley Lai */ 325132f7629SAshley Lai static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev *ibmvtpm) 326132f7629SAshley Lai { 327132f7629SAshley Lai int rc; 328132f7629SAshley Lai 329fb154e0eSMichal Suchanek rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_COMP_CMD); 330132f7629SAshley Lai if (rc != H_SUCCESS) 331132f7629SAshley Lai dev_err(ibmvtpm->dev, 332132f7629SAshley Lai "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc); 333132f7629SAshley Lai 334132f7629SAshley Lai return rc; 335132f7629SAshley Lai } 336132f7629SAshley Lai 337132f7629SAshley Lai /** 338132f7629SAshley Lai * tpm_ibmvtpm_remove - ibm vtpm remove entry point 339132f7629SAshley Lai * @vdev: vio device struct 340132f7629SAshley Lai * 34193c12f29SWinkler, Tomas * Return: Always 0. 342132f7629SAshley Lai */ 343386a966fSUwe Kleine-König static void tpm_ibmvtpm_remove(struct vio_dev *vdev) 344132f7629SAshley Lai { 3459e0d39d8SChristophe Ricard struct tpm_chip *chip = dev_get_drvdata(&vdev->dev); 3469e0d39d8SChristophe Ricard struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev); 347132f7629SAshley Lai int rc = 0; 348132f7629SAshley Lai 349afb5abc2SJarkko Sakkinen tpm_chip_unregister(chip); 350afb5abc2SJarkko Sakkinen 351132f7629SAshley Lai free_irq(vdev->irq, ibmvtpm); 352132f7629SAshley Lai 353132f7629SAshley Lai do { 354132f7629SAshley Lai if (rc) 355132f7629SAshley Lai msleep(100); 356132f7629SAshley Lai rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); 357132f7629SAshley Lai } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); 358132f7629SAshley Lai 359132f7629SAshley Lai dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle, 360132f7629SAshley Lai CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL); 361132f7629SAshley Lai free_page((unsigned long)ibmvtpm->crq_queue.crq_addr); 362132f7629SAshley Lai 363132f7629SAshley Lai if (ibmvtpm->rtce_buf) { 364132f7629SAshley Lai dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle, 365132f7629SAshley Lai ibmvtpm->rtce_size, DMA_BIDIRECTIONAL); 366132f7629SAshley Lai kfree(ibmvtpm->rtce_buf); 367132f7629SAshley Lai } 368132f7629SAshley Lai 369132f7629SAshley Lai kfree(ibmvtpm); 37031574d32SHon Ching \(Vicky\) Lo /* For tpm_ibmvtpm_get_desired_dma */ 37131574d32SHon Ching \(Vicky\) Lo dev_set_drvdata(&vdev->dev, NULL); 372132f7629SAshley Lai } 373132f7629SAshley Lai 374132f7629SAshley Lai /** 375132f7629SAshley Lai * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver 376132f7629SAshley Lai * @vdev: vio device struct 377132f7629SAshley Lai * 37893c12f29SWinkler, Tomas * Return: 37993c12f29SWinkler, Tomas * Number of bytes the driver needs to DMA map. 380132f7629SAshley Lai */ 381132f7629SAshley Lai static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev *vdev) 382132f7629SAshley Lai { 3839e0d39d8SChristophe Ricard struct tpm_chip *chip = dev_get_drvdata(&vdev->dev); 38431574d32SHon Ching \(Vicky\) Lo struct ibmvtpm_dev *ibmvtpm; 38584eb186bSHon Ching (Vicky) Lo 38693c12f29SWinkler, Tomas /* 38793c12f29SWinkler, Tomas * ibmvtpm initializes at probe time, so the data we are 38884eb186bSHon Ching (Vicky) Lo * asking for may not be set yet. Estimate that 4K required 38984eb186bSHon Ching (Vicky) Lo * for TCE-mapped buffer in addition to CRQ. 39084eb186bSHon Ching (Vicky) Lo */ 39131574d32SHon Ching \(Vicky\) Lo if (chip) 39231574d32SHon Ching \(Vicky\) Lo ibmvtpm = dev_get_drvdata(&chip->dev); 39331574d32SHon Ching \(Vicky\) Lo else 39484eb186bSHon Ching (Vicky) Lo return CRQ_RES_BUF_SIZE + PAGE_SIZE; 39584eb186bSHon Ching (Vicky) Lo 396132f7629SAshley Lai return CRQ_RES_BUF_SIZE + ibmvtpm->rtce_size; 397132f7629SAshley Lai } 398132f7629SAshley Lai 399132f7629SAshley Lai /** 400132f7629SAshley Lai * tpm_ibmvtpm_suspend - Suspend 401132f7629SAshley Lai * @dev: device struct 402132f7629SAshley Lai * 40393c12f29SWinkler, Tomas * Return: Always 0. 404132f7629SAshley Lai */ 405132f7629SAshley Lai static int tpm_ibmvtpm_suspend(struct device *dev) 406132f7629SAshley Lai { 4079e0d39d8SChristophe Ricard struct tpm_chip *chip = dev_get_drvdata(dev); 4089e0d39d8SChristophe Ricard struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev); 409132f7629SAshley Lai int rc = 0; 410132f7629SAshley Lai 411fb154e0eSMichal Suchanek rc = ibmvtpm_send_crq(ibmvtpm->vdev, 412fb154e0eSMichal Suchanek IBMVTPM_VALID_CMD, VTPM_PREPARE_TO_SUSPEND, 0, 0); 413132f7629SAshley Lai if (rc != H_SUCCESS) 414132f7629SAshley Lai dev_err(ibmvtpm->dev, 415132f7629SAshley Lai "tpm_ibmvtpm_suspend failed rc=%d\n", rc); 416132f7629SAshley Lai 417132f7629SAshley Lai return rc; 418132f7629SAshley Lai } 419132f7629SAshley Lai 420132f7629SAshley Lai /** 421132f7629SAshley Lai * ibmvtpm_reset_crq - Reset CRQ 42293c12f29SWinkler, Tomas * 423132f7629SAshley Lai * @ibmvtpm: ibm vtpm struct 424132f7629SAshley Lai * 42593c12f29SWinkler, Tomas * Return: 42693c12f29SWinkler, Tomas * 0 on success. 42793c12f29SWinkler, Tomas * Non-zero on failure. 428132f7629SAshley Lai */ 429132f7629SAshley Lai static int ibmvtpm_reset_crq(struct ibmvtpm_dev *ibmvtpm) 430132f7629SAshley Lai { 431132f7629SAshley Lai int rc = 0; 432132f7629SAshley Lai 433132f7629SAshley Lai do { 434132f7629SAshley Lai if (rc) 435132f7629SAshley Lai msleep(100); 436132f7629SAshley Lai rc = plpar_hcall_norets(H_FREE_CRQ, 437132f7629SAshley Lai ibmvtpm->vdev->unit_address); 438132f7629SAshley Lai } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); 439132f7629SAshley Lai 440132f7629SAshley Lai memset(ibmvtpm->crq_queue.crq_addr, 0, CRQ_RES_BUF_SIZE); 441132f7629SAshley Lai ibmvtpm->crq_queue.index = 0; 442132f7629SAshley Lai 443132f7629SAshley Lai return plpar_hcall_norets(H_REG_CRQ, ibmvtpm->vdev->unit_address, 444132f7629SAshley Lai ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE); 445132f7629SAshley Lai } 446132f7629SAshley Lai 4471f866057SStefan Berger static bool tpm_ibmvtpm_req_canceled(struct tpm_chip *chip, u8 status) 4481f866057SStefan Berger { 4491f866057SStefan Berger return (status == 0); 4501f866057SStefan Berger } 4511f866057SStefan Berger 45201ad1fa7SJason Gunthorpe static const struct tpm_class_ops tpm_ibmvtpm = { 453132f7629SAshley Lai .recv = tpm_ibmvtpm_recv, 454132f7629SAshley Lai .send = tpm_ibmvtpm_send, 455132f7629SAshley Lai .cancel = tpm_ibmvtpm_cancel, 456132f7629SAshley Lai .status = tpm_ibmvtpm_status, 457*047d4226SStefan Berger .req_complete_mask = 1, 458132f7629SAshley Lai .req_complete_val = 0, 4591f866057SStefan Berger .req_canceled = tpm_ibmvtpm_req_canceled, 460132f7629SAshley Lai }; 461132f7629SAshley Lai 462132f7629SAshley Lai static const struct dev_pm_ops tpm_ibmvtpm_pm_ops = { 463132f7629SAshley Lai .suspend = tpm_ibmvtpm_suspend, 464132f7629SAshley Lai .resume = tpm_ibmvtpm_resume, 465132f7629SAshley Lai }; 466132f7629SAshley Lai 467132f7629SAshley Lai /** 468132f7629SAshley Lai * ibmvtpm_crq_get_next - Get next responded crq 469132f7629SAshley Lai * 47093c12f29SWinkler, Tomas * @ibmvtpm: vtpm device struct 47193c12f29SWinkler, Tomas * 47293c12f29SWinkler, Tomas * Return: vtpm crq pointer or NULL. 473132f7629SAshley Lai */ 474132f7629SAshley Lai static struct ibmvtpm_crq *ibmvtpm_crq_get_next(struct ibmvtpm_dev *ibmvtpm) 475132f7629SAshley Lai { 476132f7629SAshley Lai struct ibmvtpm_crq_queue *crq_q = &ibmvtpm->crq_queue; 477132f7629SAshley Lai struct ibmvtpm_crq *crq = &crq_q->crq_addr[crq_q->index]; 478132f7629SAshley Lai 479132f7629SAshley Lai if (crq->valid & VTPM_MSG_RES) { 480132f7629SAshley Lai if (++crq_q->index == crq_q->num_entry) 481132f7629SAshley Lai crq_q->index = 0; 482b5666502SAshley Lai smp_rmb(); 483132f7629SAshley Lai } else 484132f7629SAshley Lai crq = NULL; 485132f7629SAshley Lai return crq; 486132f7629SAshley Lai } 487132f7629SAshley Lai 488132f7629SAshley Lai /** 489132f7629SAshley Lai * ibmvtpm_crq_process - Process responded crq 490132f7629SAshley Lai * 49193c12f29SWinkler, Tomas * @crq: crq to be processed 49293c12f29SWinkler, Tomas * @ibmvtpm: vtpm device struct 49393c12f29SWinkler, Tomas * 494132f7629SAshley Lai */ 495132f7629SAshley Lai static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq, 496132f7629SAshley Lai struct ibmvtpm_dev *ibmvtpm) 497132f7629SAshley Lai { 498132f7629SAshley Lai int rc = 0; 499132f7629SAshley Lai 500132f7629SAshley Lai switch (crq->valid) { 501132f7629SAshley Lai case VALID_INIT_CRQ: 502132f7629SAshley Lai switch (crq->msg) { 503132f7629SAshley Lai case INIT_CRQ_RES: 504132f7629SAshley Lai dev_info(ibmvtpm->dev, "CRQ initialized\n"); 505132f7629SAshley Lai rc = ibmvtpm_crq_send_init_complete(ibmvtpm); 506132f7629SAshley Lai if (rc) 507132f7629SAshley Lai dev_err(ibmvtpm->dev, "Unable to send CRQ init complete rc=%d\n", rc); 508132f7629SAshley Lai return; 509132f7629SAshley Lai case INIT_CRQ_COMP_RES: 510132f7629SAshley Lai dev_info(ibmvtpm->dev, 511132f7629SAshley Lai "CRQ initialization completed\n"); 512132f7629SAshley Lai return; 513132f7629SAshley Lai default: 514132f7629SAshley Lai dev_err(ibmvtpm->dev, "Unknown crq message type: %d\n", crq->msg); 515132f7629SAshley Lai return; 516132f7629SAshley Lai } 517132f7629SAshley Lai case IBMVTPM_VALID_CMD: 518132f7629SAshley Lai switch (crq->msg) { 519132f7629SAshley Lai case VTPM_GET_RTCE_BUFFER_SIZE_RES: 520eb71f8a5Shonclo if (be16_to_cpu(crq->len) <= 0) { 521132f7629SAshley Lai dev_err(ibmvtpm->dev, "Invalid rtce size\n"); 522132f7629SAshley Lai return; 523132f7629SAshley Lai } 524eb71f8a5Shonclo ibmvtpm->rtce_size = be16_to_cpu(crq->len); 525132f7629SAshley Lai ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size, 52660ecd86cSHon Ching \(Vicky\) Lo GFP_ATOMIC); 527132f7629SAshley Lai if (!ibmvtpm->rtce_buf) { 528132f7629SAshley Lai dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n"); 529132f7629SAshley Lai return; 530132f7629SAshley Lai } 531132f7629SAshley Lai 532132f7629SAshley Lai ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev, 533132f7629SAshley Lai ibmvtpm->rtce_buf, ibmvtpm->rtce_size, 534132f7629SAshley Lai DMA_BIDIRECTIONAL); 535132f7629SAshley Lai 536132f7629SAshley Lai if (dma_mapping_error(ibmvtpm->dev, 537132f7629SAshley Lai ibmvtpm->rtce_dma_handle)) { 538132f7629SAshley Lai kfree(ibmvtpm->rtce_buf); 539132f7629SAshley Lai ibmvtpm->rtce_buf = NULL; 540132f7629SAshley Lai dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n"); 541132f7629SAshley Lai } 542132f7629SAshley Lai 543132f7629SAshley Lai return; 544132f7629SAshley Lai case VTPM_GET_VERSION_RES: 545eb71f8a5Shonclo ibmvtpm->vtpm_version = be32_to_cpu(crq->data); 546132f7629SAshley Lai return; 547132f7629SAshley Lai case VTPM_TPM_COMMAND_RES: 548b5666502SAshley Lai /* len of the data in rtce buffer */ 549eb71f8a5Shonclo ibmvtpm->res_len = be16_to_cpu(crq->len); 550*047d4226SStefan Berger ibmvtpm->tpm_processing_cmd = 0; 551b5666502SAshley Lai wake_up_interruptible(&ibmvtpm->wq); 552132f7629SAshley Lai return; 553132f7629SAshley Lai default: 554132f7629SAshley Lai return; 555132f7629SAshley Lai } 556132f7629SAshley Lai } 557132f7629SAshley Lai return; 558132f7629SAshley Lai } 559132f7629SAshley Lai 560132f7629SAshley Lai /** 561132f7629SAshley Lai * ibmvtpm_interrupt - Interrupt handler 56293c12f29SWinkler, Tomas * 563132f7629SAshley Lai * @irq: irq number to handle 564132f7629SAshley Lai * @vtpm_instance: vtpm that received interrupt 565132f7629SAshley Lai * 566132f7629SAshley Lai * Returns: 567132f7629SAshley Lai * IRQ_HANDLED 568132f7629SAshley Lai **/ 569132f7629SAshley Lai static irqreturn_t ibmvtpm_interrupt(int irq, void *vtpm_instance) 570132f7629SAshley Lai { 571132f7629SAshley Lai struct ibmvtpm_dev *ibmvtpm = (struct ibmvtpm_dev *) vtpm_instance; 572132f7629SAshley Lai struct ibmvtpm_crq *crq; 573132f7629SAshley Lai 574b5666502SAshley Lai /* while loop is needed for initial setup (get version and 575b5666502SAshley Lai * get rtce_size). There should be only one tpm request at any 576b5666502SAshley Lai * given time. 577b5666502SAshley Lai */ 578132f7629SAshley Lai while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) { 579132f7629SAshley Lai ibmvtpm_crq_process(crq, ibmvtpm); 580d8d74ea3SStefan Berger wake_up_interruptible(&ibmvtpm->crq_queue.wq); 581132f7629SAshley Lai crq->valid = 0; 582b5666502SAshley Lai smp_wmb(); 583132f7629SAshley Lai } 584132f7629SAshley Lai 585b5666502SAshley Lai return IRQ_HANDLED; 586132f7629SAshley Lai } 587132f7629SAshley Lai 588132f7629SAshley Lai /** 589132f7629SAshley Lai * tpm_ibmvtpm_probe - ibm vtpm initialize entry point 59093c12f29SWinkler, Tomas * 591132f7629SAshley Lai * @vio_dev: vio device struct 592132f7629SAshley Lai * @id: vio device id struct 593132f7629SAshley Lai * 59493c12f29SWinkler, Tomas * Return: 59593c12f29SWinkler, Tomas * 0 on success. 59693c12f29SWinkler, Tomas * Non-zero on failure. 597132f7629SAshley Lai */ 598afc6d369SBill Pemberton static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev, 599132f7629SAshley Lai const struct vio_device_id *id) 600132f7629SAshley Lai { 601132f7629SAshley Lai struct ibmvtpm_dev *ibmvtpm; 602132f7629SAshley Lai struct device *dev = &vio_dev->dev; 603132f7629SAshley Lai struct ibmvtpm_crq_queue *crq_q; 604132f7629SAshley Lai struct tpm_chip *chip; 605132f7629SAshley Lai int rc = -ENOMEM, rc1; 606132f7629SAshley Lai 607afb5abc2SJarkko Sakkinen chip = tpmm_chip_alloc(dev, &tpm_ibmvtpm); 608afb5abc2SJarkko Sakkinen if (IS_ERR(chip)) 609afb5abc2SJarkko Sakkinen return PTR_ERR(chip); 610132f7629SAshley Lai 611132f7629SAshley Lai ibmvtpm = kzalloc(sizeof(struct ibmvtpm_dev), GFP_KERNEL); 612132f7629SAshley Lai if (!ibmvtpm) { 613132f7629SAshley Lai dev_err(dev, "kzalloc for ibmvtpm failed\n"); 614132f7629SAshley Lai goto cleanup; 615132f7629SAshley Lai } 616132f7629SAshley Lai 6179d75f089SHon Ching \(Vicky\) Lo ibmvtpm->dev = dev; 6189d75f089SHon Ching \(Vicky\) Lo ibmvtpm->vdev = vio_dev; 6199d75f089SHon Ching \(Vicky\) Lo 620132f7629SAshley Lai crq_q = &ibmvtpm->crq_queue; 621132f7629SAshley Lai crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL); 622132f7629SAshley Lai if (!crq_q->crq_addr) { 623132f7629SAshley Lai dev_err(dev, "Unable to allocate memory for crq_addr\n"); 624132f7629SAshley Lai goto cleanup; 625132f7629SAshley Lai } 626132f7629SAshley Lai 627132f7629SAshley Lai crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr); 628d8d74ea3SStefan Berger init_waitqueue_head(&crq_q->wq); 629132f7629SAshley Lai ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr, 630132f7629SAshley Lai CRQ_RES_BUF_SIZE, 631132f7629SAshley Lai DMA_BIDIRECTIONAL); 632132f7629SAshley Lai 633132f7629SAshley Lai if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) { 634132f7629SAshley Lai dev_err(dev, "dma mapping failed\n"); 635132f7629SAshley Lai goto cleanup; 636132f7629SAshley Lai } 637132f7629SAshley Lai 638132f7629SAshley Lai rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address, 639132f7629SAshley Lai ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE); 640132f7629SAshley Lai if (rc == H_RESOURCE) 641132f7629SAshley Lai rc = ibmvtpm_reset_crq(ibmvtpm); 642132f7629SAshley Lai 643132f7629SAshley Lai if (rc) { 644132f7629SAshley Lai dev_err(dev, "Unable to register CRQ rc=%d\n", rc); 645132f7629SAshley Lai goto reg_crq_cleanup; 646132f7629SAshley Lai } 647132f7629SAshley Lai 648132f7629SAshley Lai rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0, 649132f7629SAshley Lai tpm_ibmvtpm_driver_name, ibmvtpm); 650132f7629SAshley Lai if (rc) { 651132f7629SAshley Lai dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq); 652132f7629SAshley Lai goto init_irq_cleanup; 653132f7629SAshley Lai } 654132f7629SAshley Lai 655132f7629SAshley Lai rc = vio_enable_interrupts(vio_dev); 656132f7629SAshley Lai if (rc) { 657132f7629SAshley Lai dev_err(dev, "Error %d enabling interrupts\n", rc); 658132f7629SAshley Lai goto init_irq_cleanup; 659132f7629SAshley Lai } 660132f7629SAshley Lai 661b5666502SAshley Lai init_waitqueue_head(&ibmvtpm->wq); 662b5666502SAshley Lai 663132f7629SAshley Lai crq_q->index = 0; 664132f7629SAshley Lai 66575254557SStephen Rothwell dev_set_drvdata(&chip->dev, ibmvtpm); 666132f7629SAshley Lai 667132f7629SAshley Lai spin_lock_init(&ibmvtpm->rtce_lock); 668132f7629SAshley Lai 669132f7629SAshley Lai rc = ibmvtpm_crq_send_init(ibmvtpm); 670132f7629SAshley Lai if (rc) 671132f7629SAshley Lai goto init_irq_cleanup; 672132f7629SAshley Lai 673132f7629SAshley Lai rc = ibmvtpm_crq_get_version(ibmvtpm); 674132f7629SAshley Lai if (rc) 675132f7629SAshley Lai goto init_irq_cleanup; 676132f7629SAshley Lai 677132f7629SAshley Lai rc = ibmvtpm_crq_get_rtce_size(ibmvtpm); 678132f7629SAshley Lai if (rc) 679132f7629SAshley Lai goto init_irq_cleanup; 680132f7629SAshley Lai 681d8d74ea3SStefan Berger if (!wait_event_timeout(ibmvtpm->crq_queue.wq, 682d8d74ea3SStefan Berger ibmvtpm->rtce_buf != NULL, 683d8d74ea3SStefan Berger HZ)) { 684d8d74ea3SStefan Berger dev_err(dev, "CRQ response timed out\n"); 685d8d74ea3SStefan Berger goto init_irq_cleanup; 686d8d74ea3SStefan Berger } 687d8d74ea3SStefan Berger 688*047d4226SStefan Berger 689*047d4226SStefan Berger if (!strcmp(id->compat, "IBM,vtpm20")) 69072d0556dSDavid Gibson chip->flags |= TPM_CHIP_FLAG_TPM2; 691*047d4226SStefan Berger 692*047d4226SStefan Berger rc = tpm_get_timeouts(chip); 693*047d4226SStefan Berger if (rc) 694*047d4226SStefan Berger goto init_irq_cleanup; 695*047d4226SStefan Berger 696*047d4226SStefan Berger if (chip->flags & TPM_CHIP_FLAG_TPM2) { 69772d0556dSDavid Gibson rc = tpm2_get_cc_attrs_tbl(chip); 69872d0556dSDavid Gibson if (rc) 69972d0556dSDavid Gibson goto init_irq_cleanup; 70072d0556dSDavid Gibson } 70172d0556dSDavid Gibson 702afb5abc2SJarkko Sakkinen return tpm_chip_register(chip); 703132f7629SAshley Lai init_irq_cleanup: 704132f7629SAshley Lai do { 705132f7629SAshley Lai rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address); 706132f7629SAshley Lai } while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1)); 707132f7629SAshley Lai reg_crq_cleanup: 708132f7629SAshley Lai dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE, 709132f7629SAshley Lai DMA_BIDIRECTIONAL); 710132f7629SAshley Lai cleanup: 711132f7629SAshley Lai if (ibmvtpm) { 712132f7629SAshley Lai if (crq_q->crq_addr) 713132f7629SAshley Lai free_page((unsigned long)crq_q->crq_addr); 714132f7629SAshley Lai kfree(ibmvtpm); 715132f7629SAshley Lai } 716132f7629SAshley Lai 717132f7629SAshley Lai return rc; 718132f7629SAshley Lai } 719132f7629SAshley Lai 720132f7629SAshley Lai static struct vio_driver ibmvtpm_driver = { 721132f7629SAshley Lai .id_table = tpm_ibmvtpm_device_table, 722132f7629SAshley Lai .probe = tpm_ibmvtpm_probe, 723132f7629SAshley Lai .remove = tpm_ibmvtpm_remove, 724132f7629SAshley Lai .get_desired_dma = tpm_ibmvtpm_get_desired_dma, 725132f7629SAshley Lai .name = tpm_ibmvtpm_driver_name, 726132f7629SAshley Lai .pm = &tpm_ibmvtpm_pm_ops, 727132f7629SAshley Lai }; 728132f7629SAshley Lai 729132f7629SAshley Lai /** 73093c12f29SWinkler, Tomas * ibmvtpm_module_init - Initialize ibm vtpm module. 731132f7629SAshley Lai * 73293c12f29SWinkler, Tomas * 73393c12f29SWinkler, Tomas * Return: 73493c12f29SWinkler, Tomas * 0 on success. 73593c12f29SWinkler, Tomas * Non-zero on failure. 736132f7629SAshley Lai */ 737132f7629SAshley Lai static int __init ibmvtpm_module_init(void) 738132f7629SAshley Lai { 739132f7629SAshley Lai return vio_register_driver(&ibmvtpm_driver); 740132f7629SAshley Lai } 741132f7629SAshley Lai 742132f7629SAshley Lai /** 74393c12f29SWinkler, Tomas * ibmvtpm_module_exit - Tear down ibm vtpm module. 744132f7629SAshley Lai */ 745132f7629SAshley Lai static void __exit ibmvtpm_module_exit(void) 746132f7629SAshley Lai { 747132f7629SAshley Lai vio_unregister_driver(&ibmvtpm_driver); 748132f7629SAshley Lai } 749132f7629SAshley Lai 750132f7629SAshley Lai module_init(ibmvtpm_module_init); 751132f7629SAshley Lai module_exit(ibmvtpm_module_exit); 752132f7629SAshley Lai 753132f7629SAshley Lai MODULE_AUTHOR("adlai@us.ibm.com"); 754132f7629SAshley Lai MODULE_DESCRIPTION("IBM vTPM Driver"); 755132f7629SAshley Lai MODULE_VERSION("1.0"); 756132f7629SAshley Lai MODULE_LICENSE("GPL"); 757