tpm_tis.c

来自「linux 内核源代码」· C语言 代码 · 共 711 行 · 第 1/2 页

C
711
字号
/* * Copyright (C) 2005, 2006 IBM Corporation * * Authors: * Leendert van Doorn <leendert@watson.ibm.com> * Kylene Hall <kjhall@us.ibm.com> * * Maintained by: <tpmdd-devel@lists.sourceforge.net> * * Device driver for TCG/TCPA TPM (trusted platform module). * Specifications at www.trustedcomputinggroup.org * * This device driver implements the TPM interface as defined in * the TCG TPM Interface Spec version 1.2, revision 1.0. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation, version 2 of the * License. */#include <linux/init.h>#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/pnp.h>#include <linux/interrupt.h>#include <linux/wait.h>#include "tpm.h"#define TPM_HEADER_SIZE 10enum tis_access {	TPM_ACCESS_VALID = 0x80,	TPM_ACCESS_ACTIVE_LOCALITY = 0x20,	TPM_ACCESS_REQUEST_PENDING = 0x04,	TPM_ACCESS_REQUEST_USE = 0x02,};enum tis_status {	TPM_STS_VALID = 0x80,	TPM_STS_COMMAND_READY = 0x40,	TPM_STS_GO = 0x20,	TPM_STS_DATA_AVAIL = 0x10,	TPM_STS_DATA_EXPECT = 0x08,};enum tis_int_flags {	TPM_GLOBAL_INT_ENABLE = 0x80000000,	TPM_INTF_BURST_COUNT_STATIC = 0x100,	TPM_INTF_CMD_READY_INT = 0x080,	TPM_INTF_INT_EDGE_FALLING = 0x040,	TPM_INTF_INT_EDGE_RISING = 0x020,	TPM_INTF_INT_LEVEL_LOW = 0x010,	TPM_INTF_INT_LEVEL_HIGH = 0x008,	TPM_INTF_LOCALITY_CHANGE_INT = 0x004,	TPM_INTF_STS_VALID_INT = 0x002,	TPM_INTF_DATA_AVAIL_INT = 0x001,};enum tis_defaults {	TIS_MEM_BASE = 0xFED40000,	TIS_MEM_LEN = 0x5000,	TIS_SHORT_TIMEOUT = 750,	/* ms */	TIS_LONG_TIMEOUT = 2000,	/* 2 sec */};#define	TPM_ACCESS(l)			(0x0000 | ((l) << 12))#define	TPM_INT_ENABLE(l)		(0x0008 | ((l) << 12))#define	TPM_INT_VECTOR(l)		(0x000C | ((l) << 12))#define	TPM_INT_STATUS(l)		(0x0010 | ((l) << 12))#define	TPM_INTF_CAPS(l)		(0x0014 | ((l) << 12))#define	TPM_STS(l)			(0x0018 | ((l) << 12))#define	TPM_DATA_FIFO(l)		(0x0024 | ((l) << 12))#define	TPM_DID_VID(l)			(0x0F00 | ((l) << 12))#define	TPM_RID(l)			(0x0F04 | ((l) << 12))static LIST_HEAD(tis_chips);static DEFINE_SPINLOCK(tis_lock);static int check_locality(struct tpm_chip *chip, int l){	if ((ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &	     (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==	    (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))		return chip->vendor.locality = l;	return -1;}static void release_locality(struct tpm_chip *chip, int l, int force){	if (force || (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &		      (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==	    (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID))		iowrite8(TPM_ACCESS_ACTIVE_LOCALITY,			 chip->vendor.iobase + TPM_ACCESS(l));}static int request_locality(struct tpm_chip *chip, int l){	unsigned long stop;	long rc;	if (check_locality(chip, l) >= 0)		return l;	iowrite8(TPM_ACCESS_REQUEST_USE,		 chip->vendor.iobase + TPM_ACCESS(l));	if (chip->vendor.irq) {		rc = wait_event_interruptible_timeout(chip->vendor.int_queue,						      (check_locality						       (chip, l) >= 0),						      chip->vendor.timeout_a);		if (rc > 0)			return l;	} else {		/* wait for burstcount */		stop = jiffies + chip->vendor.timeout_a;		do {			if (check_locality(chip, l) >= 0)				return l;			msleep(TPM_TIMEOUT);		}		while (time_before(jiffies, stop));	}	return -1;}static u8 tpm_tis_status(struct tpm_chip *chip){	return ioread8(chip->vendor.iobase +		       TPM_STS(chip->vendor.locality));}static void tpm_tis_ready(struct tpm_chip *chip){	/* this causes the current command to be aborted */	iowrite8(TPM_STS_COMMAND_READY,		 chip->vendor.iobase + TPM_STS(chip->vendor.locality));}static int get_burstcount(struct tpm_chip *chip){	unsigned long stop;	int burstcnt;	/* wait for burstcount */	/* which timeout value, spec has 2 answers (c & d) */	stop = jiffies + chip->vendor.timeout_d;	do {		burstcnt = ioread8(chip->vendor.iobase +				   TPM_STS(chip->vendor.locality) + 1);		burstcnt += ioread8(chip->vendor.iobase +				    TPM_STS(chip->vendor.locality) +				    2) << 8;		if (burstcnt)			return burstcnt;		msleep(TPM_TIMEOUT);	} while (time_before(jiffies, stop));	return -EBUSY;}static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,			 wait_queue_head_t *queue){	unsigned long stop;	long rc;	u8 status;	/* check current status */	status = tpm_tis_status(chip);	if ((status & mask) == mask)		return 0;	if (chip->vendor.irq) {		rc = wait_event_interruptible_timeout(*queue,						      ((tpm_tis_status							(chip) & mask) ==						       mask), timeout);		if (rc > 0)			return 0;	} else {		stop = jiffies + timeout;		do {			msleep(TPM_TIMEOUT);			status = tpm_tis_status(chip);			if ((status & mask) == mask)				return 0;		} while (time_before(jiffies, stop));	}	return -ETIME;}static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count){	int size = 0, burstcnt;	while (size < count &&	       wait_for_stat(chip,			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,			     chip->vendor.timeout_c,			     &chip->vendor.read_queue)	       == 0) {		burstcnt = get_burstcount(chip);		for (; burstcnt > 0 && size < count; burstcnt--)			buf[size++] = ioread8(chip->vendor.iobase +					      TPM_DATA_FIFO(chip->vendor.							    locality));	}	return size;}static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count){	int size = 0;	int expected, status;	if (count < TPM_HEADER_SIZE) {		size = -EIO;		goto out;	}	/* read first 10 bytes, including tag, paramsize, and result */	if ((size =	     recv_data(chip, buf, TPM_HEADER_SIZE)) < TPM_HEADER_SIZE) {		dev_err(chip->dev, "Unable to read header\n");		goto out;	}	expected = be32_to_cpu(*(__be32 *) (buf + 2));	if (expected > count) {		size = -EIO;		goto out;	}	if ((size +=	     recv_data(chip, &buf[TPM_HEADER_SIZE],		       expected - TPM_HEADER_SIZE)) < expected) {		dev_err(chip->dev, "Unable to read remainder of result\n");		size = -ETIME;		goto out;	}	wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,		      &chip->vendor.int_queue);	status = tpm_tis_status(chip);	if (status & TPM_STS_DATA_AVAIL) {	/* retry? */		dev_err(chip->dev, "Error left over data\n");		size = -EIO;		goto out;	}out:	tpm_tis_ready(chip);	release_locality(chip, chip->vendor.locality, 0);	return size;}/* * If interrupts are used (signaled by an irq set in the vendor structure) * tpm.c can skip polling for the data to be available as the interrupt is * waited for here */static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len){	int rc, status, burstcnt;	size_t count = 0;	u32 ordinal;	if (request_locality(chip, 0) < 0)		return -EBUSY;	status = tpm_tis_status(chip);	if ((status & TPM_STS_COMMAND_READY) == 0) {		tpm_tis_ready(chip);		if (wait_for_stat		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,		     &chip->vendor.int_queue) < 0) {			rc = -ETIME;			goto out_err;		}	}	while (count < len - 1) {		burstcnt = get_burstcount(chip);		for (; burstcnt > 0 && count < len - 1; burstcnt--) {			iowrite8(buf[count], chip->vendor.iobase +				 TPM_DATA_FIFO(chip->vendor.locality));			count++;		}		wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,			      &chip->vendor.int_queue);		status = tpm_tis_status(chip);		if ((status & TPM_STS_DATA_EXPECT) == 0) {			rc = -EIO;			goto out_err;		}	}	/* write last byte */	iowrite8(buf[count],		 chip->vendor.iobase +		 TPM_DATA_FIFO(chip->vendor.locality));	wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,		      &chip->vendor.int_queue);	status = tpm_tis_status(chip);	if ((status & TPM_STS_DATA_EXPECT) != 0) {		rc = -EIO;		goto out_err;	}	/* go and do it */	iowrite8(TPM_STS_GO,		 chip->vendor.iobase + TPM_STS(chip->vendor.locality));	if (chip->vendor.irq) {		ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));		if (wait_for_stat		    (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,		     tpm_calc_ordinal_duration(chip, ordinal),		     &chip->vendor.read_queue) < 0) {			rc = -ETIME;			goto out_err;		}	}	return len;out_err:	tpm_tis_ready(chip);	release_locality(chip, chip->vendor.locality, 0);	return rc;}static const struct file_operations tis_ops = {	.owner = THIS_MODULE,	.llseek = no_llseek,	.open = tpm_open,	.read = tpm_read,	.write = tpm_write,	.release = tpm_release,};static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,		   NULL);static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);static struct attribute *tis_attrs[] = {	&dev_attr_pubek.attr,	&dev_attr_pcrs.attr,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?