Linux Audio

Check our new training course

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