ia64/xen-unstable

changeset 8786:e9d78d8bd568

Clean up the TPM stack a bit.

Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
author cl349@firebug.cl.cam.ac.uk
date Tue Feb 07 20:38:44 2006 +0000 (2006-02-07)
parents aefd8b8c6b1f
children 859c8d66b203
files linux-2.6-xen-sparse/drivers/char/tpm/tpm.c linux-2.6-xen-sparse/drivers/char/tpm/tpm_xen.c linux-2.6-xen-sparse/drivers/xen/tpmfront/tpmfront.c linux-2.6-xen-sparse/drivers/xen/tpmfront/tpmfront.h linux-2.6-xen-sparse/include/xen/tpmfe.h
line diff
     1.1 --- a/linux-2.6-xen-sparse/drivers/char/tpm/tpm.c	Tue Feb 07 19:16:11 2006 +0100
     1.2 +++ b/linux-2.6-xen-sparse/drivers/char/tpm/tpm.c	Tue Feb 07 20:38:44 2006 +0000
     1.3 @@ -67,9 +67,6 @@ static ssize_t tpm_transmit(struct tpm_c
     1.4  	u32 count;
     1.5  	unsigned long stop;
     1.6  
     1.7 -	if (!chip)
     1.8 -		return -ENODEV;
     1.9 -
    1.10  	count = be32_to_cpu(*((__be32 *) (buf + 2)));
    1.11  
    1.12  	if (count == 0)
     2.1 --- a/linux-2.6-xen-sparse/drivers/char/tpm/tpm_xen.c	Tue Feb 07 19:16:11 2006 +0100
     2.2 +++ b/linux-2.6-xen-sparse/drivers/char/tpm/tpm_xen.c	Tue Feb 07 20:38:44 2006 +0000
     2.3 @@ -42,7 +42,6 @@ struct transmission {
     2.4  	unsigned int request_len;
     2.5  	unsigned char *rcv_buffer;
     2.6  	unsigned int  buffersize;
     2.7 -	struct tpm_chip     *chip;
     2.8  	unsigned int flags;
     2.9  };
    2.10  
    2.11 @@ -75,6 +74,8 @@ static struct data_exchange dataex;
    2.12  
    2.13  static unsigned long disconnect_time;
    2.14  
    2.15 +static struct tpmfe_device tpmfe;
    2.16 +
    2.17  /* local function prototypes */
    2.18  static void __exit cleanup_xen(void);
    2.19  
    2.20 @@ -119,7 +120,7 @@ transmission_free(struct transmission *t
    2.21  static int tpm_recv(const u8 *buffer, size_t count, const void *ptr)
    2.22  {
    2.23  	int ret_size = 0;
    2.24 -	struct transmission *t, *temp;
    2.25 +	struct transmission *t;
    2.26  
    2.27  	/*
    2.28  	 * The list with requests must contain one request
    2.29 @@ -127,8 +128,9 @@ static int tpm_recv(const u8 *buffer, si
    2.30  	 * was passed to me from the front-end.
    2.31  	 */
    2.32  	if (dataex.current_request != ptr) {
    2.33 -		printk("WARNING: The request pointer is different than the pointer "
    2.34 -		       "the shared memory driver returned to me. %p != %p\n",
    2.35 +		printk("WARNING: The request pointer is different than the "
    2.36 +		       "pointer the shared memory driver returned to me. "
    2.37 +		       "%p != %p\n",
    2.38  		       dataex.current_request, ptr);
    2.39  	}
    2.40  
    2.41 @@ -144,16 +146,16 @@ static int tpm_recv(const u8 *buffer, si
    2.42  		return 0;
    2.43  	}
    2.44  
    2.45 -	if (NULL != (temp = dataex.current_request)) {
    2.46 -		transmission_free(temp);
    2.47 +	if (NULL != (t = dataex.current_request)) {
    2.48 +		transmission_free(t);
    2.49  		dataex.current_request = NULL;
    2.50  	}
    2.51  
    2.52  	t = transmission_alloc();
    2.53 -	if (NULL != t) {
    2.54 +	if (t) {
    2.55  		unsigned long flags;
    2.56  		t->rcv_buffer = kmalloc(count, GFP_KERNEL);
    2.57 -		if (NULL == t->rcv_buffer) {
    2.58 +		if (! t->rcv_buffer) {
    2.59  			transmission_free(t);
    2.60  			return -ENOMEM;
    2.61  		}
    2.62 @@ -258,10 +260,6 @@ static int tpm_xen_send(struct tpm_chip 
    2.63  
    2.64  	if (t != NULL) {
    2.65  		unsigned int error = 0;
    2.66 -		t->rcv_buffer = NULL;
    2.67 -		t->buffersize = 0;
    2.68 -		t->chip = chip;
    2.69 -
    2.70  		/*
    2.71  		 * Queue the packet if the driver below is not
    2.72  		 * ready, yet, or there is any packet already
    2.73 @@ -304,9 +302,11 @@ static int tpm_xen_send(struct tpm_chip 
    2.74  				struct transmission *qt = (struct transmission *) dataex.queued_requests.next;
    2.75  				list_del(&qt->next);
    2.76  				dataex.current_request = qt;
    2.77 -				spin_unlock_irqrestore(&dataex.req_list_lock, flags);
    2.78 +				spin_unlock_irqrestore(&dataex.req_list_lock,
    2.79 +				                       flags);
    2.80  
    2.81 -				rc = tpm_fe_send(qt->request,
    2.82 +				rc = tpm_fe_send(tpmfe.tpm_private,
    2.83 +				                 qt->request,
    2.84  				                 qt->request_len,
    2.85  				                 qt);
    2.86  
    2.87 @@ -351,7 +351,8 @@ static int tpm_xen_send(struct tpm_chip 
    2.88  				 * amount of bytes in the request and
    2.89  				 * a void * pointer (here: transmission structure)
    2.90  				 */
    2.91 -				rc = tpm_fe_send(buf, count, t);
    2.92 +				rc = tpm_fe_send(tpmfe.tpm_private,
    2.93 +				                 buf, count, t);
    2.94  				/*
    2.95  				 * The generic TPM driver will call
    2.96  				 * the function to receive the response.
    2.97 @@ -373,7 +374,8 @@ queue_it:
    2.98  				 * queue it.
    2.99  				 */
   2.100  				dataex.flags |= DATAEX_FLAG_QUEUED_ONLY;
   2.101 -				list_add_tail(&t->next, &dataex.queued_requests);
   2.102 +				list_add_tail(&t->next,
   2.103 +				              &dataex.queued_requests);
   2.104  				rc = 0;
   2.105  			}
   2.106  		}
   2.107 @@ -512,7 +514,7 @@ static void __exit cleanup_xen(void)
   2.108  	tpm_fe_unregister_receiver();
   2.109  }
   2.110  
   2.111 -fs_initcall(init_xen);
   2.112 +module_init(init_xen);
   2.113  module_exit(cleanup_xen);
   2.114  
   2.115  MODULE_AUTHOR("Stefan Berger (stefanb@us.ibm.com)");
     3.1 --- a/linux-2.6-xen-sparse/drivers/xen/tpmfront/tpmfront.c	Tue Feb 07 19:16:11 2006 +0100
     3.2 +++ b/linux-2.6-xen-sparse/drivers/xen/tpmfront/tpmfront.c	Tue Feb 07 20:38:44 2006 +0000
     3.3 @@ -56,14 +56,14 @@
     3.4  
     3.5  /* locally visible variables */
     3.6  static grant_ref_t gref_head;
     3.7 -static struct tpm_private my_private;
     3.8 +static struct tpm_private *my_priv;
     3.9  
    3.10  /* local function prototypes */
    3.11  static irqreturn_t tpmif_int(int irq,
    3.12                               void *tpm_priv,
    3.13                               struct pt_regs *ptregs);
    3.14  static void tpmif_rx_action(unsigned long unused);
    3.15 -static void tpmif_connect(u16 evtchn, domid_t domid);
    3.16 +static void tpmif_connect(struct tpm_private *tp, domid_t domid);
    3.17  static DECLARE_TASKLET(tpmif_rx_tasklet, tpmif_rx_action, 0);
    3.18  static int tpm_allocate_buffers(struct tpm_private *tp);
    3.19  static void tpmif_set_connected_state(struct tpm_private *tp,
    3.20 @@ -101,7 +101,7 @@ tx_buffer_copy(struct tx_buffer *txb, co
    3.21  
    3.22  static inline struct tx_buffer *tx_buffer_alloc(void)
    3.23  {
    3.24 -	struct tx_buffer *txb = kmalloc(sizeof (struct tx_buffer),
    3.25 +	struct tx_buffer *txb = kzalloc(sizeof (struct tx_buffer),
    3.26  					GFP_KERNEL);
    3.27  
    3.28  	if (txb) {
    3.29 @@ -118,6 +118,31 @@ static inline struct tx_buffer *tx_buffe
    3.30  
    3.31  
    3.32  /**************************************************************
    3.33 + Utility function for the tpm_private structure
    3.34 +**************************************************************/
    3.35 +static inline void tpm_private_init(struct tpm_private *tp)
    3.36 +{
    3.37 +	spin_lock_init(&tp->tx_lock);
    3.38 +	init_waitqueue_head(&tp->wait_q);
    3.39 +}
    3.40 +
    3.41 +static struct tpm_private *tpm_private_get(void)
    3.42 +{
    3.43 +	if (!my_priv) {
    3.44 +	        my_priv = kzalloc(sizeof(struct tpm_private), GFP_KERNEL);
    3.45 +	        if (my_priv) {
    3.46 +                	tpm_private_init(my_priv);
    3.47 +	        }
    3.48 +        }
    3.49 +        return my_priv;
    3.50 +}
    3.51 +
    3.52 +static inline void tpm_private_free(struct tpm_private *tp)
    3.53 +{
    3.54 +	kfree(tp);
    3.55 +}
    3.56 +
    3.57 +/**************************************************************
    3.58  
    3.59   The interface to let the tpm plugin register its callback
    3.60   function and send data to another partition using this module
    3.61 @@ -131,10 +156,9 @@ static struct tpmfe_device *upperlayer_t
    3.62  /*
    3.63   * Send data via this module by calling this function
    3.64   */
    3.65 -int tpm_fe_send(const u8 * buf, size_t count, void *ptr)
    3.66 +int tpm_fe_send(struct tpm_private *tp, const u8 * buf, size_t count, void *ptr)
    3.67  {
    3.68  	int sent = 0;
    3.69 -	struct tpm_private *tp = &my_private;
    3.70  
    3.71  	down(&suspend_lock);
    3.72  	sent = tpm_xmit(tp, buf, count, 0, ptr);
    3.73 @@ -155,6 +179,7 @@ int tpm_fe_register_receiver(struct tpmf
    3.74  	if (NULL == upperlayer_tpmfe) {
    3.75  		upperlayer_tpmfe = tpmfe_dev;
    3.76  		tpmfe_dev->max_tx_size = TPMIF_TX_RING_SIZE * PAGE_SIZE;
    3.77 +		tpmfe_dev->tpm_private = tpm_private_get();
    3.78  	} else {
    3.79  		rc = -EBUSY;
    3.80  	}
    3.81 @@ -197,10 +222,9 @@ static int tpm_fe_send_upperlayer(const 
    3.82  **************************************************************/
    3.83  
    3.84  static int setup_tpmring(struct xenbus_device *dev,
    3.85 -                         struct tpmfront_info * info)
    3.86 +                         struct tpm_private *tp)
    3.87  {
    3.88  	tpmif_tx_interface_t *sring;
    3.89 -	struct tpm_private *tp = &my_private;
    3.90  	int err;
    3.91  
    3.92  	sring = (void *)__get_free_page(GFP_KERNEL);
    3.93 @@ -219,13 +243,13 @@ static int setup_tpmring(struct xenbus_d
    3.94  		xenbus_dev_fatal(dev, err, "allocating grant reference");
    3.95  		goto fail;
    3.96  	}
    3.97 -	info->ring_ref = err;
    3.98 +	tp->ring_ref = err;
    3.99  
   3.100  	err = xenbus_alloc_evtchn(dev, &tp->evtchn);
   3.101  	if (err)
   3.102  		goto fail;
   3.103  
   3.104 -	tpmif_connect(tp->evtchn, dev->otherend_id);
   3.105 +	tpmif_connect(tp, dev->otherend_id);
   3.106  
   3.107  	return 0;
   3.108  fail:
   3.109 @@ -233,11 +257,11 @@ fail:
   3.110  }
   3.111  
   3.112  
   3.113 -static void destroy_tpmring(struct tpmfront_info *info, struct tpm_private *tp)
   3.114 +static void destroy_tpmring(struct tpm_private *tp)
   3.115  {
   3.116  	tpmif_set_connected_state(tp, 0);
   3.117  	if (tp->tx != NULL) {
   3.118 -		gnttab_end_foreign_access(info->ring_ref, 0,
   3.119 +		gnttab_end_foreign_access(tp->ring_ref, 0,
   3.120  					  (unsigned long)tp->tx);
   3.121  		tp->tx = NULL;
   3.122  	}
   3.123 @@ -249,13 +273,13 @@ static void destroy_tpmring(struct tpmfr
   3.124  
   3.125  
   3.126  static int talk_to_backend(struct xenbus_device *dev,
   3.127 -                           struct tpmfront_info *info)
   3.128 +                           struct tpm_private *tp)
   3.129  {
   3.130  	const char *message = NULL;
   3.131  	int err;
   3.132  	xenbus_transaction_t xbt;
   3.133  
   3.134 -	err = setup_tpmring(dev, info);
   3.135 +	err = setup_tpmring(dev, tp);
   3.136  	if (err) {
   3.137  		xenbus_dev_fatal(dev, err, "setting up ring");
   3.138  		goto out;
   3.139 @@ -269,14 +293,14 @@ again:
   3.140  	}
   3.141  
   3.142  	err = xenbus_printf(xbt, dev->nodename,
   3.143 -	                    "ring-ref","%u", info->ring_ref);
   3.144 +	                    "ring-ref","%u", tp->ring_ref);
   3.145  	if (err) {
   3.146  		message = "writing ring-ref";
   3.147  		goto abort_transaction;
   3.148  	}
   3.149  
   3.150  	err = xenbus_printf(xbt, dev->nodename,
   3.151 -			    "event-channel", "%u", my_private.evtchn);
   3.152 +			    "event-channel", "%u", tp->evtchn);
   3.153  	if (err) {
   3.154  		message = "writing event-channel";
   3.155  		goto abort_transaction;
   3.156 @@ -301,7 +325,7 @@ abort_transaction:
   3.157  	if (message)
   3.158  		xenbus_dev_error(dev, err, "%s", message);
   3.159  destroy_tpmring:
   3.160 -	destroy_tpmring(info, &my_private);
   3.161 +	destroy_tpmring(tp);
   3.162  out:
   3.163  	return err;
   3.164  }
   3.165 @@ -312,7 +336,7 @@ out:
   3.166  static void backend_changed(struct xenbus_device *dev,
   3.167  			    XenbusState backend_state)
   3.168  {
   3.169 -	struct tpm_private *tp = &my_private;
   3.170 +	struct tpm_private *tp = dev->data;
   3.171  	DPRINTK("\n");
   3.172  
   3.173  	switch (backend_state) {
   3.174 @@ -343,8 +367,8 @@ static int tpmfront_probe(struct xenbus_
   3.175                            const struct xenbus_device_id *id)
   3.176  {
   3.177  	int err;
   3.178 -	struct tpmfront_info *info;
   3.179  	int handle;
   3.180 +	struct tpm_private *tp = tpm_private_get();
   3.181  
   3.182  	err = xenbus_scanf(XBT_NULL, dev->nodename,
   3.183  	                   "handle", "%i", &handle);
   3.184 @@ -356,20 +380,12 @@ static int tpmfront_probe(struct xenbus_
   3.185  		return err;
   3.186  	}
   3.187  
   3.188 -	info = kmalloc(sizeof(*info), GFP_KERNEL);
   3.189 -	if (!info) {
   3.190 -		err = -ENOMEM;
   3.191 -		xenbus_dev_fatal(dev,err,"allocating info structure");
   3.192 -		return err;
   3.193 -	}
   3.194 -	memset(info, 0x0, sizeof(*info));
   3.195 +        tp->dev = dev;
   3.196 +        dev->data = tp;
   3.197  
   3.198 -	info->dev = dev;
   3.199 -	dev->data = info;
   3.200 -
   3.201 -	err = talk_to_backend(dev, info);
   3.202 +	err = talk_to_backend(dev, tp);
   3.203  	if (err) {
   3.204 -		kfree(info);
   3.205 +                tpm_private_free(tp);
   3.206  		dev->data = NULL;
   3.207  		return err;
   3.208  	}
   3.209 @@ -379,18 +395,15 @@ static int tpmfront_probe(struct xenbus_
   3.210  
   3.211  static int tpmfront_remove(struct xenbus_device *dev)
   3.212  {
   3.213 -	struct tpmfront_info *info = dev->data;
   3.214 -
   3.215 -	destroy_tpmring(info, &my_private);
   3.216 -
   3.217 -	kfree(info);
   3.218 +        struct tpm_private *tp = dev->data;
   3.219 +	destroy_tpmring(tp);
   3.220  	return 0;
   3.221  }
   3.222  
   3.223  static int
   3.224  tpmfront_suspend(struct xenbus_device *dev)
   3.225  {
   3.226 -	struct tpm_private *tp = &my_private;
   3.227 +	struct tpm_private *tp = dev->data;
   3.228  	u32 ctr;
   3.229  
   3.230  	/* lock, so no app can send */
   3.231 @@ -420,17 +433,15 @@ tpmfront_suspend(struct xenbus_device *d
   3.232  static int
   3.233  tpmfront_resume(struct xenbus_device *dev)
   3.234  {
   3.235 -	struct tpmfront_info *info = dev->data;
   3.236 -	return talk_to_backend(dev, info);
   3.237 +        struct tpm_private *tp = dev->data;
   3.238 +	return talk_to_backend(dev, tp);
   3.239  }
   3.240  
   3.241  static void
   3.242 -tpmif_connect(u16 evtchn, domid_t domid)
   3.243 +tpmif_connect(struct tpm_private *tp, domid_t domid)
   3.244  {
   3.245  	int err;
   3.246 -	struct tpm_private *tp = &my_private;
   3.247  
   3.248 -	tp->evtchn = evtchn;
   3.249  	tp->backend_id = domid;
   3.250  
   3.251  	err = bind_evtchn_to_irqhandler(tp->evtchn,
   3.252 @@ -477,9 +488,9 @@ tpm_allocate_buffers(struct tpm_private 
   3.253  }
   3.254  
   3.255  static void
   3.256 -tpmif_rx_action(unsigned long unused)
   3.257 +tpmif_rx_action(unsigned long priv)
   3.258  {
   3.259 -	struct tpm_private *tp = &my_private;
   3.260 +	struct tpm_private *tp = (struct tpm_private *)priv;
   3.261  
   3.262  	int i = 0;
   3.263  	unsigned int received;
   3.264 @@ -529,6 +540,7 @@ tpmif_int(int irq, void *tpm_priv, struc
   3.265  	unsigned long flags;
   3.266  
   3.267  	spin_lock_irqsave(&tp->tx_lock, flags);
   3.268 +	tpmif_rx_tasklet.data = (unsigned long)tp;
   3.269  	tasklet_schedule(&tpmif_rx_tasklet);
   3.270  	spin_unlock_irqrestore(&tp->tx_lock, flags);
   3.271  
   3.272 @@ -678,12 +690,6 @@ tpmif_init(void)
   3.273  	                                     &gref_head ) < 0) {
   3.274  		return -EFAULT;
   3.275  	}
   3.276 -	/*
   3.277 -	 * Only don't send the driver status when we are in the
   3.278 -	 * INIT domain.
   3.279 -	 */
   3.280 -	spin_lock_init(&my_private.tx_lock);
   3.281 -	init_waitqueue_head(&my_private.wait_q);
   3.282  
   3.283  	init_tpm_xenbus();
   3.284  
     4.1 --- a/linux-2.6-xen-sparse/drivers/xen/tpmfront/tpmfront.h	Tue Feb 07 19:16:11 2006 +0100
     4.2 +++ b/linux-2.6-xen-sparse/drivers/xen/tpmfront/tpmfront.h	Tue Feb 07 20:38:44 2006 +0000
     4.3 @@ -17,9 +17,6 @@ struct tpm_private {
     4.4  	domid_t backend_id;
     4.5  	wait_queue_head_t wait_q;
     4.6  
     4.7 -};
     4.8 -
     4.9 -struct tpmfront_info {
    4.10  	struct xenbus_device *dev;
    4.11  	int ring_ref;
    4.12  };
     5.1 --- a/linux-2.6-xen-sparse/include/xen/tpmfe.h	Tue Feb 07 19:16:11 2006 +0100
     5.2 +++ b/linux-2.6-xen-sparse/include/xen/tpmfe.h	Tue Feb 07 20:38:44 2006 +0000
     5.3 @@ -1,6 +1,8 @@
     5.4  #ifndef TPM_FE_H
     5.5  #define TPM_FE_H
     5.6  
     5.7 +struct tpm_private;
     5.8 +
     5.9  struct tpmfe_device {
    5.10  	/*
    5.11  	 * Let upper layer receive data from front-end
    5.12 @@ -19,6 +21,11 @@ struct tpmfe_device {
    5.13  	 * for allocation of buffers.
    5.14  	 */
    5.15  	unsigned int max_tx_size;
    5.16 +	/*
    5.17 +	 * The following is a private structure of the underlying
    5.18 +	 * driver. It's expected as first parameter in the send function.
    5.19 +	 */
    5.20 +	struct tpm_private *tpm_private;
    5.21  };
    5.22  
    5.23  enum {
    5.24 @@ -26,7 +33,7 @@ enum {
    5.25  	TPMFE_STATUS_CONNECTED = 0x1
    5.26  };
    5.27  
    5.28 -int tpm_fe_send(const u8 * buf, size_t count, void *ptr);
    5.29 +int tpm_fe_send(struct tpm_private * tp, const u8 * buf, size_t count, void *ptr);
    5.30  int tpm_fe_register_receiver(struct tpmfe_device *);
    5.31  void tpm_fe_unregister_receiver(void);
    5.32