ia64/linux-2.6.18-xen.hg

view drivers/char/tpm/tpm_xen.c @ 470:5baef0e18e36

xenbus: prevent warnings on unhandled enumeration values

XenbusStateReconfiguring/XenbusStateReconfigured were introduced by
c/s 437, but aren't handled in many switch statements. This c/s also
introduced a possibly un-referenced label, which also gets eliminated
here.

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Mar 05 17:28:41 2008 +0000 (2008-03-05)
parents fedc10fba9f1
children
line source
1 /*
2 * Copyright (c) 2005, IBM Corporation
3 *
4 * Author: Stefan Berger, stefanb@us.ibm.com
5 * Grant table support: Mahadevan Gomathisankaran
6 *
7 * This code has been derived from drivers/xen/netfront/netfront.c
8 *
9 * Copyright (c) 2002-2004, K A Fraser
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 version 2
13 * as published by the Free Software Foundation; or, when distributed
14 * separately from the Linux kernel or incorporated into other
15 * software packages, subject to the following license:
16 *
17 * Permission is hereby granted, free of charge, to any person obtaining a copy
18 * of this source file (the "Software"), to deal in the Software without
19 * restriction, including without limitation the rights to use, copy, modify,
20 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
21 * and to permit persons to whom the Software is furnished to do so, subject to
22 * the following conditions:
23 *
24 * The above copyright notice and this permission notice shall be included in
25 * all copies or substantial portions of the Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 * IN THE SOFTWARE.
34 */
36 #include <linux/errno.h>
37 #include <linux/err.h>
38 #include <linux/interrupt.h>
39 #include <linux/mutex.h>
40 #include <asm/uaccess.h>
41 #include <xen/evtchn.h>
42 #include <xen/interface/grant_table.h>
43 #include <xen/interface/io/tpmif.h>
44 #include <xen/gnttab.h>
45 #include <xen/xenbus.h>
46 #include "tpm.h"
47 #include "tpm_vtpm.h"
49 #undef DEBUG
51 /* local structures */
52 struct tpm_private {
53 struct tpm_chip *chip;
55 tpmif_tx_interface_t *tx;
56 atomic_t refcnt;
57 unsigned int irq;
58 u8 is_connected;
59 u8 is_suspended;
61 spinlock_t tx_lock;
63 struct tx_buffer *tx_buffers[TPMIF_TX_RING_SIZE];
65 atomic_t tx_busy;
66 void *tx_remember;
68 domid_t backend_id;
69 wait_queue_head_t wait_q;
71 struct xenbus_device *dev;
72 int ring_ref;
73 };
75 struct tx_buffer {
76 unsigned int size; // available space in data
77 unsigned int len; // used space in data
78 unsigned char *data; // pointer to a page
79 };
82 /* locally visible variables */
83 static grant_ref_t gref_head;
84 static struct tpm_private *my_priv;
86 /* local function prototypes */
87 static irqreturn_t tpmif_int(int irq,
88 void *tpm_priv,
89 struct pt_regs *ptregs);
90 static void tpmif_rx_action(unsigned long unused);
91 static int tpmif_connect(struct xenbus_device *dev,
92 struct tpm_private *tp,
93 domid_t domid);
94 static DECLARE_TASKLET(tpmif_rx_tasklet, tpmif_rx_action, 0);
95 static int tpmif_allocate_tx_buffers(struct tpm_private *tp);
96 static void tpmif_free_tx_buffers(struct tpm_private *tp);
97 static void tpmif_set_connected_state(struct tpm_private *tp,
98 u8 newstate);
99 static int tpm_xmit(struct tpm_private *tp,
100 const u8 * buf, size_t count, int userbuffer,
101 void *remember);
102 static void destroy_tpmring(struct tpm_private *tp);
103 void __exit tpmif_exit(void);
105 #define DPRINTK(fmt, args...) \
106 pr_debug("xen_tpm_fr (%s:%d) " fmt, __FUNCTION__, __LINE__, ##args)
107 #define IPRINTK(fmt, args...) \
108 printk(KERN_INFO "xen_tpm_fr: " fmt, ##args)
109 #define WPRINTK(fmt, args...) \
110 printk(KERN_WARNING "xen_tpm_fr: " fmt, ##args)
112 #define GRANT_INVALID_REF 0
115 static inline int
116 tx_buffer_copy(struct tx_buffer *txb, const u8 *src, int len,
117 int isuserbuffer)
118 {
119 int copied = len;
121 if (len > txb->size)
122 copied = txb->size;
123 if (isuserbuffer) {
124 if (copy_from_user(txb->data, src, copied))
125 return -EFAULT;
126 } else {
127 memcpy(txb->data, src, copied);
128 }
129 txb->len = len;
130 return copied;
131 }
133 static inline struct tx_buffer *tx_buffer_alloc(void)
134 {
135 struct tx_buffer *txb;
137 txb = kzalloc(sizeof(struct tx_buffer), GFP_KERNEL);
138 if (!txb)
139 return NULL;
141 txb->len = 0;
142 txb->size = PAGE_SIZE;
143 txb->data = (unsigned char *)__get_free_page(GFP_KERNEL);
144 if (txb->data == NULL) {
145 kfree(txb);
146 txb = NULL;
147 }
149 return txb;
150 }
153 static inline void tx_buffer_free(struct tx_buffer *txb)
154 {
155 if (txb) {
156 free_page((long)txb->data);
157 kfree(txb);
158 }
159 }
161 /**************************************************************
162 Utility function for the tpm_private structure
163 **************************************************************/
164 static void tpm_private_init(struct tpm_private *tp)
165 {
166 spin_lock_init(&tp->tx_lock);
167 init_waitqueue_head(&tp->wait_q);
168 atomic_set(&tp->refcnt, 1);
169 }
171 static void tpm_private_put(void)
172 {
173 if (!atomic_dec_and_test(&my_priv->refcnt))
174 return;
176 tpmif_free_tx_buffers(my_priv);
177 kfree(my_priv);
178 my_priv = NULL;
179 }
181 static struct tpm_private *tpm_private_get(void)
182 {
183 int err;
185 if (my_priv) {
186 atomic_inc(&my_priv->refcnt);
187 return my_priv;
188 }
190 my_priv = kzalloc(sizeof(struct tpm_private), GFP_KERNEL);
191 if (!my_priv)
192 return NULL;
194 tpm_private_init(my_priv);
195 err = tpmif_allocate_tx_buffers(my_priv);
196 if (err < 0)
197 tpm_private_put();
199 return my_priv;
200 }
202 /**************************************************************
204 The interface to let the tpm plugin register its callback
205 function and send data to another partition using this module
207 **************************************************************/
209 static DEFINE_MUTEX(suspend_lock);
210 /*
211 * Send data via this module by calling this function
212 */
213 int vtpm_vd_send(struct tpm_private *tp,
214 const u8 * buf, size_t count, void *ptr)
215 {
216 int sent;
218 mutex_lock(&suspend_lock);
219 sent = tpm_xmit(tp, buf, count, 0, ptr);
220 mutex_unlock(&suspend_lock);
222 return sent;
223 }
225 /**************************************************************
226 XENBUS support code
227 **************************************************************/
229 static int setup_tpmring(struct xenbus_device *dev,
230 struct tpm_private *tp)
231 {
232 tpmif_tx_interface_t *sring;
233 int err;
235 tp->ring_ref = GRANT_INVALID_REF;
237 sring = (void *)__get_free_page(GFP_KERNEL);
238 if (!sring) {
239 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
240 return -ENOMEM;
241 }
242 tp->tx = sring;
244 err = xenbus_grant_ring(dev, virt_to_mfn(tp->tx));
245 if (err < 0) {
246 free_page((unsigned long)sring);
247 tp->tx = NULL;
248 xenbus_dev_fatal(dev, err, "allocating grant reference");
249 goto fail;
250 }
251 tp->ring_ref = err;
253 err = tpmif_connect(dev, tp, dev->otherend_id);
254 if (err)
255 goto fail;
257 return 0;
258 fail:
259 destroy_tpmring(tp);
260 return err;
261 }
264 static void destroy_tpmring(struct tpm_private *tp)
265 {
266 tpmif_set_connected_state(tp, 0);
268 if (tp->ring_ref != GRANT_INVALID_REF) {
269 gnttab_end_foreign_access(tp->ring_ref, (unsigned long)tp->tx);
270 tp->ring_ref = GRANT_INVALID_REF;
271 tp->tx = NULL;
272 }
274 if (tp->irq)
275 unbind_from_irqhandler(tp->irq, tp);
277 tp->irq = 0;
278 }
281 static int talk_to_backend(struct xenbus_device *dev,
282 struct tpm_private *tp)
283 {
284 const char *message = NULL;
285 int err;
286 struct xenbus_transaction xbt;
288 err = setup_tpmring(dev, tp);
289 if (err) {
290 xenbus_dev_fatal(dev, err, "setting up ring");
291 goto out;
292 }
294 again:
295 err = xenbus_transaction_start(&xbt);
296 if (err) {
297 xenbus_dev_fatal(dev, err, "starting transaction");
298 goto destroy_tpmring;
299 }
301 err = xenbus_printf(xbt, dev->nodename,
302 "ring-ref","%u", tp->ring_ref);
303 if (err) {
304 message = "writing ring-ref";
305 goto abort_transaction;
306 }
308 err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
309 irq_to_evtchn_port(tp->irq));
310 if (err) {
311 message = "writing event-channel";
312 goto abort_transaction;
313 }
315 err = xenbus_transaction_end(xbt, 0);
316 if (err == -EAGAIN)
317 goto again;
318 if (err) {
319 xenbus_dev_fatal(dev, err, "completing transaction");
320 goto destroy_tpmring;
321 }
323 xenbus_switch_state(dev, XenbusStateConnected);
325 return 0;
327 abort_transaction:
328 xenbus_transaction_end(xbt, 1);
329 if (message)
330 xenbus_dev_error(dev, err, "%s", message);
331 destroy_tpmring:
332 destroy_tpmring(tp);
333 out:
334 return err;
335 }
337 /**
338 * Callback received when the backend's state changes.
339 */
340 static void backend_changed(struct xenbus_device *dev,
341 enum xenbus_state backend_state)
342 {
343 struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
344 DPRINTK("\n");
346 switch (backend_state) {
347 case XenbusStateInitialising:
348 case XenbusStateInitWait:
349 case XenbusStateInitialised:
350 case XenbusStateReconfiguring:
351 case XenbusStateReconfigured:
352 case XenbusStateUnknown:
353 break;
355 case XenbusStateConnected:
356 tpmif_set_connected_state(tp, 1);
357 break;
359 case XenbusStateClosing:
360 tpmif_set_connected_state(tp, 0);
361 xenbus_frontend_closed(dev);
362 break;
364 case XenbusStateClosed:
365 tpmif_set_connected_state(tp, 0);
366 if (tp->is_suspended == 0)
367 device_unregister(&dev->dev);
368 xenbus_frontend_closed(dev);
369 break;
370 }
371 }
373 static int tpmfront_probe(struct xenbus_device *dev,
374 const struct xenbus_device_id *id)
375 {
376 int err;
377 int handle;
378 struct tpm_private *tp = tpm_private_get();
380 if (!tp)
381 return -ENOMEM;
383 tp->chip = init_vtpm(&dev->dev, tp);
384 if (IS_ERR(tp->chip))
385 return PTR_ERR(tp->chip);
387 err = xenbus_scanf(XBT_NIL, dev->nodename,
388 "handle", "%i", &handle);
389 if (XENBUS_EXIST_ERR(err))
390 return err;
392 if (err < 0) {
393 xenbus_dev_fatal(dev,err,"reading virtual-device");
394 return err;
395 }
397 tp->dev = dev;
399 err = talk_to_backend(dev, tp);
400 if (err) {
401 tpm_private_put();
402 return err;
403 }
405 return 0;
406 }
409 static int tpmfront_remove(struct xenbus_device *dev)
410 {
411 struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
412 destroy_tpmring(tp);
413 cleanup_vtpm(&dev->dev);
414 return 0;
415 }
417 static int tpmfront_suspend(struct xenbus_device *dev)
418 {
419 struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
420 u32 ctr;
422 /* Take the lock, preventing any application from sending. */
423 mutex_lock(&suspend_lock);
424 tp->is_suspended = 1;
426 for (ctr = 0; atomic_read(&tp->tx_busy); ctr++) {
427 if ((ctr % 10) == 0)
428 printk("TPM-FE [INFO]: Waiting for outstanding "
429 "request.\n");
430 /* Wait for a request to be responded to. */
431 interruptible_sleep_on_timeout(&tp->wait_q, 100);
432 }
434 return 0;
435 }
437 static int tpmfront_suspend_finish(struct tpm_private *tp)
438 {
439 tp->is_suspended = 0;
440 /* Allow applications to send again. */
441 mutex_unlock(&suspend_lock);
442 return 0;
443 }
445 static int tpmfront_suspend_cancel(struct xenbus_device *dev)
446 {
447 struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
448 return tpmfront_suspend_finish(tp);
449 }
451 static int tpmfront_resume(struct xenbus_device *dev)
452 {
453 struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
454 destroy_tpmring(tp);
455 return talk_to_backend(dev, tp);
456 }
458 static int tpmif_connect(struct xenbus_device *dev,
459 struct tpm_private *tp,
460 domid_t domid)
461 {
462 int err;
464 tp->backend_id = domid;
466 err = bind_listening_port_to_irqhandler(
467 domid, tpmif_int, SA_SAMPLE_RANDOM, "tpmif", tp);
468 if (err <= 0) {
469 WPRINTK("bind_listening_port_to_irqhandler failed "
470 "(err=%d)\n", err);
471 return err;
472 }
473 tp->irq = err;
475 return 0;
476 }
478 static struct xenbus_device_id tpmfront_ids[] = {
479 { "vtpm" },
480 { "" }
481 };
483 static struct xenbus_driver tpmfront = {
484 .name = "vtpm",
485 .owner = THIS_MODULE,
486 .ids = tpmfront_ids,
487 .probe = tpmfront_probe,
488 .remove = tpmfront_remove,
489 .resume = tpmfront_resume,
490 .otherend_changed = backend_changed,
491 .suspend = tpmfront_suspend,
492 .suspend_cancel = tpmfront_suspend_cancel,
493 };
495 static void __init init_tpm_xenbus(void)
496 {
497 xenbus_register_frontend(&tpmfront);
498 }
500 static int tpmif_allocate_tx_buffers(struct tpm_private *tp)
501 {
502 unsigned int i;
504 for (i = 0; i < TPMIF_TX_RING_SIZE; i++) {
505 tp->tx_buffers[i] = tx_buffer_alloc();
506 if (!tp->tx_buffers[i]) {
507 tpmif_free_tx_buffers(tp);
508 return -ENOMEM;
509 }
510 }
511 return 0;
512 }
514 static void tpmif_free_tx_buffers(struct tpm_private *tp)
515 {
516 unsigned int i;
518 for (i = 0; i < TPMIF_TX_RING_SIZE; i++)
519 tx_buffer_free(tp->tx_buffers[i]);
520 }
522 static void tpmif_rx_action(unsigned long priv)
523 {
524 struct tpm_private *tp = (struct tpm_private *)priv;
525 int i = 0;
526 unsigned int received;
527 unsigned int offset = 0;
528 u8 *buffer;
529 tpmif_tx_request_t *tx = &tp->tx->ring[i].req;
531 atomic_set(&tp->tx_busy, 0);
532 wake_up_interruptible(&tp->wait_q);
534 received = tx->size;
536 buffer = kmalloc(received, GFP_ATOMIC);
537 if (!buffer)
538 return;
540 for (i = 0; i < TPMIF_TX_RING_SIZE && offset < received; i++) {
541 struct tx_buffer *txb = tp->tx_buffers[i];
542 tpmif_tx_request_t *tx;
543 unsigned int tocopy;
545 tx = &tp->tx->ring[i].req;
546 tocopy = tx->size;
547 if (tocopy > PAGE_SIZE)
548 tocopy = PAGE_SIZE;
550 memcpy(&buffer[offset], txb->data, tocopy);
552 gnttab_release_grant_reference(&gref_head, tx->ref);
554 offset += tocopy;
555 }
557 vtpm_vd_recv(tp->chip, buffer, received, tp->tx_remember);
558 kfree(buffer);
559 }
562 static irqreturn_t tpmif_int(int irq, void *tpm_priv, struct pt_regs *ptregs)
563 {
564 struct tpm_private *tp = tpm_priv;
565 unsigned long flags;
567 spin_lock_irqsave(&tp->tx_lock, flags);
568 tpmif_rx_tasklet.data = (unsigned long)tp;
569 tasklet_schedule(&tpmif_rx_tasklet);
570 spin_unlock_irqrestore(&tp->tx_lock, flags);
572 return IRQ_HANDLED;
573 }
576 static int tpm_xmit(struct tpm_private *tp,
577 const u8 * buf, size_t count, int isuserbuffer,
578 void *remember)
579 {
580 tpmif_tx_request_t *tx;
581 TPMIF_RING_IDX i;
582 unsigned int offset = 0;
584 spin_lock_irq(&tp->tx_lock);
586 if (unlikely(atomic_read(&tp->tx_busy))) {
587 printk("tpm_xmit: There's an outstanding request/response "
588 "on the way!\n");
589 spin_unlock_irq(&tp->tx_lock);
590 return -EBUSY;
591 }
593 if (tp->is_connected != 1) {
594 spin_unlock_irq(&tp->tx_lock);
595 return -EIO;
596 }
598 for (i = 0; count > 0 && i < TPMIF_TX_RING_SIZE; i++) {
599 struct tx_buffer *txb = tp->tx_buffers[i];
600 int copied;
602 if (!txb) {
603 DPRINTK("txb (i=%d) is NULL. buffers initilized?\n"
604 "Not transmitting anything!\n", i);
605 spin_unlock_irq(&tp->tx_lock);
606 return -EFAULT;
607 }
609 copied = tx_buffer_copy(txb, &buf[offset], count,
610 isuserbuffer);
611 if (copied < 0) {
612 /* An error occurred */
613 spin_unlock_irq(&tp->tx_lock);
614 return copied;
615 }
616 count -= copied;
617 offset += copied;
619 tx = &tp->tx->ring[i].req;
620 tx->addr = virt_to_machine(txb->data);
621 tx->size = txb->len;
622 tx->unused = 0;
624 DPRINTK("First 4 characters sent by TPM-FE are "
625 "0x%02x 0x%02x 0x%02x 0x%02x\n",
626 txb->data[0],txb->data[1],txb->data[2],txb->data[3]);
628 /* Get the granttable reference for this page. */
629 tx->ref = gnttab_claim_grant_reference(&gref_head);
630 if (tx->ref == -ENOSPC) {
631 spin_unlock_irq(&tp->tx_lock);
632 DPRINTK("Grant table claim reference failed in "
633 "func:%s line:%d file:%s\n",
634 __FUNCTION__, __LINE__, __FILE__);
635 return -ENOSPC;
636 }
637 gnttab_grant_foreign_access_ref(tx->ref,
638 tp->backend_id,
639 virt_to_mfn(txb->data),
640 0 /*RW*/);
641 wmb();
642 }
644 atomic_set(&tp->tx_busy, 1);
645 tp->tx_remember = remember;
647 mb();
649 notify_remote_via_irq(tp->irq);
651 spin_unlock_irq(&tp->tx_lock);
652 return offset;
653 }
656 static void tpmif_notify_upperlayer(struct tpm_private *tp)
657 {
658 /* Notify upper layer about the state of the connection to the BE. */
659 vtpm_vd_status(tp->chip, (tp->is_connected
660 ? TPM_VD_STATUS_CONNECTED
661 : TPM_VD_STATUS_DISCONNECTED));
662 }
665 static void tpmif_set_connected_state(struct tpm_private *tp, u8 is_connected)
666 {
667 /*
668 * Don't notify upper layer if we are in suspend mode and
669 * should disconnect - assumption is that we will resume
670 * The mutex keeps apps from sending.
671 */
672 if (is_connected == 0 && tp->is_suspended == 1)
673 return;
675 /*
676 * Unlock the mutex if we are connected again
677 * after being suspended - now resuming.
678 * This also removes the suspend state.
679 */
680 if (is_connected == 1 && tp->is_suspended == 1)
681 tpmfront_suspend_finish(tp);
683 if (is_connected != tp->is_connected) {
684 tp->is_connected = is_connected;
685 tpmif_notify_upperlayer(tp);
686 }
687 }
691 /* =================================================================
692 * Initialization function.
693 * =================================================================
694 */
697 static int __init tpmif_init(void)
698 {
699 struct tpm_private *tp;
701 if (is_initial_xendomain())
702 return -EPERM;
704 tp = tpm_private_get();
705 if (!tp)
706 return -ENOMEM;
708 IPRINTK("Initialising the vTPM driver.\n");
709 if (gnttab_alloc_grant_references(TPMIF_TX_RING_SIZE,
710 &gref_head) < 0) {
711 tpm_private_put();
712 return -EFAULT;
713 }
715 init_tpm_xenbus();
716 return 0;
717 }
720 module_init(tpmif_init);
722 MODULE_LICENSE("Dual BSD/GPL");