ia64/xen-unstable

changeset 6989:7a45b8ccef01

Some cleanup in tpm-related files.

Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Sep 21 10:13:11 2005 +0000 (2005-09-21)
parents b8537442f3d6
children 55fc0ecc19c3
files linux-2.6-xen-sparse/arch/xen/Kconfig linux-2.6-xen-sparse/drivers/xen/tpmback/interface.c linux-2.6-xen-sparse/drivers/xen/tpmfront/tpmfront.c linux-2.6-xen-sparse/drivers/xen/tpmfront/tpmfront.h tools/python/xen/xend/server/tpmif.py xen/include/public/io/tpmif.h
line diff
     1.1 --- a/linux-2.6-xen-sparse/arch/xen/Kconfig	Wed Sep 21 10:11:02 2005 +0000
     1.2 +++ b/linux-2.6-xen-sparse/arch/xen/Kconfig	Wed Sep 21 10:13:11 2005 +0000
     1.3 @@ -73,6 +73,8 @@ config XEN_NETDEV_BACKEND
     1.4  config XEN_TPMDEV_FRONTEND
     1.5          bool "TPM-device frontend driver"
     1.6          default n
     1.7 +	select TCG_TPM
     1.8 +	select TCG_XEN
     1.9          help
    1.10            The TPM-device frontend driver.
    1.11  
     2.1 --- a/linux-2.6-xen-sparse/drivers/xen/tpmback/interface.c	Wed Sep 21 10:11:02 2005 +0000
     2.2 +++ b/linux-2.6-xen-sparse/drivers/xen/tpmback/interface.c	Wed Sep 21 10:13:11 2005 +0000
     2.3 @@ -1,4 +1,4 @@
     2.4 -/******************************************************************************
     2.5 + /******************************************************************************
     2.6   * drivers/xen/tpmback/interface.c
     2.7   *
     2.8   * Vritual TPM interface management.
     2.9 @@ -21,180 +21,175 @@
    2.10  
    2.11  static kmem_cache_t *tpmif_cachep;
    2.12  int num_frontends = 0;
    2.13 +
    2.14  LIST_HEAD(tpmif_list);
    2.15  
    2.16 -
    2.17 -tpmif_t *alloc_tpmif(domid_t domid, long int instance)
    2.18 -{
    2.19 -    struct page *page;
    2.20 -    tpmif_t *tpmif;
    2.21 -
    2.22 -    tpmif = kmem_cache_alloc(tpmif_cachep, GFP_KERNEL);
    2.23 -    if (!tpmif)
    2.24 -        return ERR_PTR(-ENOMEM);
    2.25 -
    2.26 -    memset(tpmif, 0, sizeof(*tpmif));
    2.27 -    tpmif->domid        = domid;
    2.28 -    tpmif->status       = DISCONNECTED;
    2.29 -    tpmif->tpm_instance = instance;
    2.30 -    atomic_set(&tpmif->refcnt, 1);
    2.31 -
    2.32 -    page = balloon_alloc_empty_page_range(TPMIF_TX_RING_SIZE);
    2.33 -    BUG_ON(page == NULL);
    2.34 -    tpmif->mmap_vstart = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
    2.35 -
    2.36 -    list_add(&tpmif->tpmif_list, &tpmif_list);
    2.37 -    num_frontends++;
    2.38 -
    2.39 -    return tpmif;
    2.40 -}
    2.41 -
    2.42 -
    2.43 -void free_tpmif(tpmif_t *tpmif)
    2.44 -{
    2.45 -    num_frontends--;
    2.46 -    list_del(&tpmif->tpmif_list);
    2.47 -    kmem_cache_free(tpmif_cachep, tpmif);
    2.48 -}
    2.49 -
    2.50 -
    2.51 -tpmif_t *tpmif_find(domid_t domid, long int instance)
    2.52 +tpmif_t *
    2.53 +alloc_tpmif(domid_t domid, long int instance)
    2.54  {
    2.55 -    tpmif_t *tpmif;
    2.56 -
    2.57 -    list_for_each_entry(tpmif, &tpmif_list, tpmif_list) {
    2.58 -        if (tpmif->tpm_instance == instance) {
    2.59 -            if (tpmif->domid == domid) {
    2.60 -                tpmif_get(tpmif);
    2.61 -                return tpmif;
    2.62 -	    } else {
    2.63 -	        return NULL;
    2.64 -	    }
    2.65 -        }
    2.66 -    }
    2.67 -
    2.68 -    return alloc_tpmif(domid, instance);
    2.69 -}
    2.70 -
    2.71 +	struct page *page;
    2.72 +	tpmif_t *tpmif;
    2.73  
    2.74 -static int map_frontend_page(tpmif_t *tpmif, unsigned long localaddr,
    2.75 -			     unsigned long shared_page)
    2.76 -{
    2.77 -    struct gnttab_map_grant_ref op = {
    2.78 -        .host_addr = localaddr,
    2.79 -        .flags     = GNTMAP_host_map,
    2.80 -        .ref       = shared_page,
    2.81 -        .dom       = tpmif->domid,
    2.82 -    };
    2.83 +	tpmif = kmem_cache_alloc(tpmif_cachep, GFP_KERNEL);
    2.84 +	if (!tpmif)
    2.85 +		return ERR_PTR(-ENOMEM);
    2.86  
    2.87 -    BUG_ON( HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1) );
    2.88 +	memset(tpmif, 0, sizeof (*tpmif));
    2.89 +	tpmif->domid = domid;
    2.90 +	tpmif->status = DISCONNECTED;
    2.91 +	tpmif->tpm_instance = instance;
    2.92 +	atomic_set(&tpmif->refcnt, 1);
    2.93  
    2.94 -    if (op.handle < 0) {
    2.95 -	DPRINTK(" Grant table operation failure !\n");
    2.96 -	return op.handle;
    2.97 -    }
    2.98 +	page = balloon_alloc_empty_page_range(TPMIF_TX_RING_SIZE);
    2.99 +	BUG_ON(page == NULL);
   2.100 +	tpmif->mmap_vstart = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
   2.101  
   2.102 -    tpmif->shmem_ref    = shared_page;
   2.103 -    tpmif->shmem_handle = op.handle;
   2.104 -    tpmif->shmem_vaddr  = localaddr;
   2.105 -    return 0;
   2.106 +	list_add(&tpmif->tpmif_list, &tpmif_list);
   2.107 +	num_frontends++;
   2.108 +
   2.109 +	return tpmif;
   2.110  }
   2.111  
   2.112 -
   2.113 -static void unmap_frontend_page(tpmif_t *tpmif)
   2.114 +void
   2.115 +free_tpmif(tpmif_t * tpmif)
   2.116  {
   2.117 -    struct gnttab_unmap_grant_ref op;
   2.118 -
   2.119 -    op.host_addr = tpmif->shmem_vaddr;
   2.120 -    op.handle = tpmif->shmem_handle;
   2.121 -    op.dev_bus_addr = 0;
   2.122 -
   2.123 -    BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1));
   2.124 +	num_frontends--;
   2.125 +	list_del(&tpmif->tpmif_list);
   2.126 +	kmem_cache_free(tpmif_cachep, tpmif);
   2.127  }
   2.128  
   2.129 -
   2.130 -int tpmif_map(tpmif_t *tpmif,
   2.131 -              unsigned long shared_page, unsigned int evtchn)
   2.132 +tpmif_t *
   2.133 +tpmif_find(domid_t domid, long int instance)
   2.134  {
   2.135 -    struct vm_struct *vma;
   2.136 -    evtchn_op_t op = { .cmd = EVTCHNOP_bind_interdomain };
   2.137 -    int err;
   2.138 -
   2.139 -    BUG_ON(tpmif->remote_evtchn);
   2.140 -
   2.141 -    if ( (vma = get_vm_area(PAGE_SIZE, VM_IOREMAP)) == NULL )
   2.142 -	return -ENOMEM;
   2.143 -
   2.144 -    err = map_frontend_page(tpmif,
   2.145 -                            VMALLOC_VMADDR(vma->addr),
   2.146 -                            shared_page);
   2.147 -    if (err) {
   2.148 -        vfree(vma->addr);
   2.149 -	return err;
   2.150 -    }
   2.151 +	tpmif_t *tpmif;
   2.152  
   2.153 -    op.u.bind_interdomain.dom1 = DOMID_SELF;
   2.154 -    op.u.bind_interdomain.dom2 = tpmif->domid;
   2.155 -    op.u.bind_interdomain.port1 = 0;
   2.156 -    op.u.bind_interdomain.port2 = evtchn;
   2.157 -    err = HYPERVISOR_event_channel_op(&op);
   2.158 -    if (err) {
   2.159 -	unmap_frontend_page(tpmif);
   2.160 -	vfree(vma->addr);
   2.161 -	return err;
   2.162 -    }
   2.163 +	list_for_each_entry(tpmif, &tpmif_list, tpmif_list) {
   2.164 +		if (tpmif->tpm_instance == instance) {
   2.165 +			if (tpmif->domid == domid) {
   2.166 +				tpmif_get(tpmif);
   2.167 +				return tpmif;
   2.168 +			} else {
   2.169 +				return NULL;
   2.170 +			}
   2.171 +		}
   2.172 +	}
   2.173  
   2.174 -    tpmif->evtchn = op.u.bind_interdomain.port1;
   2.175 -    tpmif->remote_evtchn = evtchn;
   2.176 -
   2.177 -    tpmif->tx = (tpmif_tx_interface_t *) vma->addr;
   2.178 -
   2.179 -    bind_evtchn_to_irqhandler(tpmif->evtchn,
   2.180 -                              tpmif_be_int,
   2.181 -                              0,
   2.182 -                              "tpmif-backend",
   2.183 -			      tpmif);
   2.184 -    tpmif->status        = CONNECTED;
   2.185 -    tpmif->shmem_ref     = shared_page;
   2.186 -    tpmif->active        = 1;
   2.187 -
   2.188 -    return 0;
   2.189 +	return alloc_tpmif(domid, instance);
   2.190  }
   2.191  
   2.192 -
   2.193 -static void __tpmif_disconnect_complete(void *arg)
   2.194 +static int
   2.195 +map_frontend_page(tpmif_t * tpmif, unsigned long localaddr,
   2.196 +		  unsigned long shared_page)
   2.197  {
   2.198 -    evtchn_op_t op = { .cmd = EVTCHNOP_close };
   2.199 -    tpmif_t *tpmif = (tpmif_t *) arg;
   2.200 +	struct gnttab_map_grant_ref op = {
   2.201 +		.host_addr = localaddr,
   2.202 +		.flags = GNTMAP_host_map,
   2.203 +		.ref = shared_page,
   2.204 +		.dom = tpmif->domid,
   2.205 +	};
   2.206  
   2.207 -    op.u.close.port = tpmif->evtchn;
   2.208 -    op.u.close.dom  = DOMID_SELF;
   2.209 -    HYPERVISOR_event_channel_op(&op);
   2.210 -    op.u.close.port = tpmif->remote_evtchn;
   2.211 -    op.u.close.dom  = tpmif->domid;
   2.212 -    HYPERVISOR_event_channel_op(&op);
   2.213 +	BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1));
   2.214  
   2.215 -    if (tpmif->evtchn)
   2.216 -         unbind_evtchn_from_irqhandler(tpmif->evtchn, tpmif);
   2.217 +	if (op.handle < 0) {
   2.218 +		DPRINTK(" Grant table operation failure !\n");
   2.219 +		return op.handle;
   2.220 +	}
   2.221  
   2.222 -    if (tpmif->tx) {
   2.223 -        unmap_frontend_page(tpmif);
   2.224 -        vfree(tpmif->tx);
   2.225 -    }
   2.226 -
   2.227 -    free_tpmif(tpmif);
   2.228 +	tpmif->shmem_ref = shared_page;
   2.229 +	tpmif->shmem_handle = op.handle;
   2.230 +	tpmif->shmem_vaddr = localaddr;
   2.231 +	return 0;
   2.232  }
   2.233  
   2.234 +static void
   2.235 +unmap_frontend_page(tpmif_t * tpmif)
   2.236 +{
   2.237 +	struct gnttab_unmap_grant_ref op;
   2.238  
   2.239 -void tpmif_disconnect_complete(tpmif_t * tpmif)
   2.240 -{
   2.241 -    INIT_WORK(&tpmif->work, __tpmif_disconnect_complete, (void *)tpmif);
   2.242 -    schedule_work(&tpmif->work);
   2.243 +	op.host_addr = tpmif->shmem_vaddr;
   2.244 +	op.handle = tpmif->shmem_handle;
   2.245 +	op.dev_bus_addr = 0;
   2.246 +
   2.247 +	BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1));
   2.248  }
   2.249  
   2.250 -
   2.251 -void __init tpmif_interface_init(void)
   2.252 +int
   2.253 +tpmif_map(tpmif_t * tpmif, unsigned long shared_page, unsigned int evtchn)
   2.254  {
   2.255 -    tpmif_cachep = kmem_cache_create("tpmif_cache", sizeof(tpmif_t),
   2.256 -                                     0, 0, NULL, NULL);
   2.257 +	struct vm_struct *vma;
   2.258 +	evtchn_op_t op = {.cmd = EVTCHNOP_bind_interdomain };
   2.259 +	int err;
   2.260 +
   2.261 +	BUG_ON(tpmif->remote_evtchn);
   2.262 +
   2.263 +	if ((vma = get_vm_area(PAGE_SIZE, VM_IOREMAP)) == NULL)
   2.264 +		return -ENOMEM;
   2.265 +
   2.266 +	err = map_frontend_page(tpmif, VMALLOC_VMADDR(vma->addr), shared_page);
   2.267 +	if (err) {
   2.268 +		vfree(vma->addr);
   2.269 +		return err;
   2.270 +	}
   2.271 +
   2.272 +	op.u.bind_interdomain.dom1 = DOMID_SELF;
   2.273 +	op.u.bind_interdomain.dom2 = tpmif->domid;
   2.274 +	op.u.bind_interdomain.port1 = 0;
   2.275 +	op.u.bind_interdomain.port2 = evtchn;
   2.276 +	err = HYPERVISOR_event_channel_op(&op);
   2.277 +	if (err) {
   2.278 +		unmap_frontend_page(tpmif);
   2.279 +		vfree(vma->addr);
   2.280 +		return err;
   2.281 +	}
   2.282 +
   2.283 +	tpmif->evtchn = op.u.bind_interdomain.port1;
   2.284 +	tpmif->remote_evtchn = evtchn;
   2.285 +
   2.286 +	tpmif->tx = (tpmif_tx_interface_t *) vma->addr;
   2.287 +
   2.288 +	bind_evtchn_to_irqhandler(tpmif->evtchn,
   2.289 +				  tpmif_be_int, 0, "tpmif-backend", tpmif);
   2.290 +	tpmif->status = CONNECTED;
   2.291 +	tpmif->shmem_ref = shared_page;
   2.292 +	tpmif->active = 1;
   2.293 +
   2.294 +	return 0;
   2.295  }
   2.296 +
   2.297 +static void
   2.298 +__tpmif_disconnect_complete(void *arg)
   2.299 +{
   2.300 +	evtchn_op_t op = {.cmd = EVTCHNOP_close };
   2.301 +	tpmif_t *tpmif = (tpmif_t *) arg;
   2.302 +
   2.303 +	op.u.close.port = tpmif->evtchn;
   2.304 +	op.u.close.dom = DOMID_SELF;
   2.305 +	HYPERVISOR_event_channel_op(&op);
   2.306 +	op.u.close.port = tpmif->remote_evtchn;
   2.307 +	op.u.close.dom = tpmif->domid;
   2.308 +	HYPERVISOR_event_channel_op(&op);
   2.309 +
   2.310 +	if (tpmif->evtchn)
   2.311 +		unbind_evtchn_from_irqhandler(tpmif->evtchn, tpmif);
   2.312 +
   2.313 +	if (tpmif->tx) {
   2.314 +		unmap_frontend_page(tpmif);
   2.315 +		vfree(tpmif->tx);
   2.316 +	}
   2.317 +
   2.318 +	free_tpmif(tpmif);
   2.319 +}
   2.320 +
   2.321 +void
   2.322 +tpmif_disconnect_complete(tpmif_t * tpmif)
   2.323 +{
   2.324 +	INIT_WORK(&tpmif->work, __tpmif_disconnect_complete, (void *)tpmif);
   2.325 +	schedule_work(&tpmif->work);
   2.326 +}
   2.327 +
   2.328 +void __init
   2.329 +tpmif_interface_init(void)
   2.330 +{
   2.331 +	tpmif_cachep = kmem_cache_create("tpmif_cache", sizeof (tpmif_t),
   2.332 +					 0, 0, NULL, NULL);
   2.333 +}
     3.1 --- a/linux-2.6-xen-sparse/drivers/xen/tpmfront/tpmfront.c	Wed Sep 21 10:11:02 2005 +0000
     3.2 +++ b/linux-2.6-xen-sparse/drivers/xen/tpmfront/tpmfront.c	Wed Sep 21 10:13:11 2005 +0000
     3.3 @@ -257,18 +257,24 @@ static int setup_tpmring(struct xenbus_d
     3.4  
     3.5  	tpm_allocate_buffers(tp);
     3.6  
     3.7 -	info->ring_ref = gnttab_claim_grant_reference(&gref_head);
     3.8 -	ASSERT(info->ring_ref != -ENOSPC);
     3.9 -	gnttab_grant_foreign_access_ref(info->ring_ref,
    3.10 -					backend_id,
    3.11 -					(virt_to_machine(tp->tx) >> PAGE_SHIFT),
    3.12 -					0);
    3.13 +	err = gnttab_grant_foreign_access(backend_id,
    3.14 +					  (virt_to_machine(tp->tx) >> PAGE_SHIFT),
    3.15 +					  0);
    3.16 +
    3.17 +	if (err == -ENOSPC) {
    3.18 +		free_page((unsigned long)sring);
    3.19 +		tp->tx = NULL;
    3.20 +		xenbus_dev_error(dev, err, "allocating grant reference");
    3.21 +		return err;
    3.22 +	}
    3.23 +	info->ring_ref = err;
    3.24  
    3.25  	op.u.alloc_unbound.dom = backend_id;
    3.26  	err = HYPERVISOR_event_channel_op(&op);
    3.27  	if (err) {
    3.28 +		gnttab_end_foreign_access(info->ring_ref, 0);
    3.29  		free_page((unsigned long)sring);
    3.30 -		tp->tx = 0;
    3.31 +		tp->tx = NULL;
    3.32  		xenbus_dev_error(dev, err, "allocating event channel");
    3.33  		return err;
    3.34  	}
    3.35 @@ -282,6 +288,7 @@ static void destroy_tpmring(struct tpmfr
    3.36  	tpmif_set_connected_state(tp,0);
    3.37  
    3.38  	if ( tp->tx != NULL ) {
    3.39 +		gnttab_end_foreign_access(info->ring_ref, 0);
    3.40  		free_page((unsigned long)tp->tx);
    3.41  		tp->tx = NULL;
    3.42  	}
     4.1 --- a/linux-2.6-xen-sparse/drivers/xen/tpmfront/tpmfront.h	Wed Sep 21 10:11:02 2005 +0000
     4.2 +++ b/linux-2.6-xen-sparse/drivers/xen/tpmfront/tpmfront.h	Wed Sep 21 10:13:11 2005 +0000
     4.3 @@ -2,7 +2,8 @@
     4.4  #define TPM_FRONT_H
     4.5  
     4.6  
     4.7 -struct tpm_private {
     4.8 +struct tpm_private
     4.9 +{
    4.10  	tpmif_tx_interface_t *tx;
    4.11  	unsigned int evtchn;
    4.12  	int connected;
    4.13 @@ -29,7 +30,8 @@ struct tpmfront_info
    4.14  };
    4.15  
    4.16  
    4.17 -struct tx_buffer {
    4.18 +struct tx_buffer
    4.19 +{
    4.20  	unsigned int size;	// available space in data
    4.21  	unsigned int len;	// used space in data
    4.22  	unsigned char *data;    // pointer to a page
     5.1 --- a/tools/python/xen/xend/server/tpmif.py	Wed Sep 21 10:11:02 2005 +0000
     5.2 +++ b/tools/python/xen/xend/server/tpmif.py	Wed Sep 21 10:13:11 2005 +0000
     5.3 @@ -39,7 +39,7 @@ class TPMifController(DevController):
     5.4          """@see DevController.getDeviceDetails"""
     5.5          
     5.6          devid = int(sxp.child_value(config, 'instance', '0'))
     5.7 -        log.error("The domain has a TPM with instance %d." % devid)
     5.8 +        log.debug("The domain has a TPM with instance %d." % devid)
     5.9  
    5.10          back  = { 'instance' : "%i" % devid }
    5.11          front = { 'handle' : "%i" % devid }
     6.1 --- a/xen/include/public/io/tpmif.h	Wed Sep 21 10:11:02 2005 +0000
     6.2 +++ b/xen/include/public/io/tpmif.h	Wed Sep 21 10:13:11 2005 +0000
     6.3 @@ -20,8 +20,7 @@ typedef struct {
     6.4      unsigned long addr;   /* Machine address of packet.   */
     6.5      int      ref;         /* grant table access reference */
     6.6      u16      id;          /* Echoed in response message.  */
     6.7 -    u16      size:15;     /* Packet size in bytes.        */
     6.8 -    u16      mapped:1;
     6.9 +    u16      size;        /* Packet size in bytes.        */
    6.10  } tpmif_tx_request_t;
    6.11  
    6.12  /*
    6.13 @@ -30,13 +29,16 @@ typedef struct {
    6.14   */
    6.15  typedef u32 TPMIF_RING_IDX;
    6.16  
    6.17 -#define TPMIF_TX_RING_SIZE 16
    6.18 +#define TPMIF_TX_RING_SIZE 10
    6.19  
    6.20  /* This structure must fit in a memory page. */
    6.21 +
    6.22  typedef struct {
    6.23 -    union {
    6.24 -        tpmif_tx_request_t  req;
    6.25 -    } ring[TPMIF_TX_RING_SIZE];
    6.26 +    tpmif_tx_request_t req;
    6.27 +} tpmif_ring_t;
    6.28 +
    6.29 +typedef struct {
    6.30 +    tpmif_ring_t ring[TPMIF_TX_RING_SIZE];
    6.31  } tpmif_tx_interface_t;
    6.32  
    6.33  #endif