diff mbox series

[22/42] tpm-backend: store TPMIf interface, improve backend_init()

Message ID 20171009225623.29232-23-marcandre.lureau@redhat.com
State New
Headers show
Series TPM: code cleanup & CRB device | expand

Commit Message

Marc-André Lureau Oct. 9, 2017, 10:56 p.m. UTC
Store the TPM interface, the actual object may be different from
TPMState. Keep a reference on the interface, and check the backend
wasn't already initialized.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 include/sysemu/tpm_backend.h |  7 ++++---
 backends/tpm.c               | 12 +++++++++---
 hw/tpm/tpm_emulator.c        |  4 ++--
 hw/tpm/tpm_passthrough.c     |  4 ++--
 hw/tpm/tpm_tis.c             |  2 +-
 5 files changed, 18 insertions(+), 11 deletions(-)

Comments

Valluri, Amarnath Oct. 10, 2017, 8:18 a.m. UTC | #1
On Tue, 2017-10-10 at 00:56 +0200, Marc-André Lureau wrote:
> Store the TPM interface, the actual object may be different from

> TPMState. Keep a reference on the interface, and check the backend

> wasn't already initialized.

> 

> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>

> ---

>  include/sysemu/tpm_backend.h |  7 ++++---

>  backends/tpm.c               | 12 +++++++++---

>  hw/tpm/tpm_emulator.c        |  4 ++--

>  hw/tpm/tpm_passthrough.c     |  4 ++--

>  hw/tpm/tpm_tis.c             |  2 +-

>  5 files changed, 18 insertions(+), 11 deletions(-)

> 

> diff --git a/include/sysemu/tpm_backend.h

> b/include/sysemu/tpm_backend.h

> index 03ea5a3400..8d08765b3c 100644

> --- a/include/sysemu/tpm_backend.h

> +++ b/include/sysemu/tpm_backend.h

> @@ -18,6 +18,7 @@

>  #include "qapi-types.h"

>  #include "qemu/option.h"

>  #include "sysemu/tpm.h"

> +#include "hw/tpm/tpm_int.h"

This is not supposed to be internal header?

- Amarnath
Marc-André Lureau Oct. 10, 2017, 11:02 a.m. UTC | #2
Hi

----- Original Message -----
> On Tue, 2017-10-10 at 00:56 +0200, Marc-André Lureau wrote:
> > Store the TPM interface, the actual object may be different from
> > TPMState. Keep a reference on the interface, and check the backend
> > wasn't already initialized.
> > 
> > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > ---
> >  include/sysemu/tpm_backend.h |  7 ++++---
> >  backends/tpm.c               | 12 +++++++++---
> >  hw/tpm/tpm_emulator.c        |  4 ++--
> >  hw/tpm/tpm_passthrough.c     |  4 ++--
> >  hw/tpm/tpm_tis.c             |  2 +-
> >  5 files changed, 18 insertions(+), 11 deletions(-)
> > 
> > diff --git a/include/sysemu/tpm_backend.h
> > b/include/sysemu/tpm_backend.h
> > index 03ea5a3400..8d08765b3c 100644
> > --- a/include/sysemu/tpm_backend.h
> > +++ b/include/sysemu/tpm_backend.h
> > @@ -18,6 +18,7 @@
> >  #include "qapi-types.h"
> >  #include "qemu/option.h"
> >  #include "sysemu/tpm.h"
> > +#include "hw/tpm/tpm_int.h"
> This is not supposed to be internal header?

You are right, moving the interface to include/sysemu/tpm.h directly, to avoid that header inclusion and later code churn in patch "tpm: lookup the the TPM interface"

thanks
diff mbox series

Patch

diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h
index 03ea5a3400..8d08765b3c 100644
--- a/include/sysemu/tpm_backend.h
+++ b/include/sysemu/tpm_backend.h
@@ -18,6 +18,7 @@ 
 #include "qapi-types.h"
 #include "qemu/option.h"
 #include "sysemu/tpm.h"
+#include "hw/tpm/tpm_int.h"
 
 #define TYPE_TPM_BACKEND "tpm-backend"
 #define TPM_BACKEND(obj) \
@@ -43,8 +44,8 @@  struct TPMBackend {
     Object parent;
 
     /*< protected >*/
+    TPMIf *tpmif;
     bool opened;
-    TPMState *tpm_state;
     GThreadPool *thread_pool;
     bool had_startup_error;
 
@@ -96,14 +97,14 @@  enum TpmType tpm_backend_get_type(TPMBackend *s);
 /**
  * tpm_backend_init:
  * @s: the backend to initialized
- * @state: TPMState
+ * @tpmif: TPM interface
  * @datacb: callback for sending data to frontend
  *
  * Initialize the backend with the given variables.
  *
  * Returns 0 on success.
  */
-int tpm_backend_init(TPMBackend *s, TPMState *state);
+int tpm_backend_init(TPMBackend *s, TPMIf *tpmif);
 
 /**
  * tpm_backend_startup_tpm:
diff --git a/backends/tpm.c b/backends/tpm.c
index 5763f6f369..86f0e7e915 100644
--- a/backends/tpm.c
+++ b/backends/tpm.c
@@ -17,7 +17,6 @@ 
 #include "qapi/error.h"
 #include "qapi/qmp/qerror.h"
 #include "sysemu/tpm.h"
-#include "hw/tpm/tpm_int.h"
 #include "qemu/thread.h"
 
 static void tpm_backend_worker_thread(gpointer data, gpointer user_data)
@@ -44,9 +43,15 @@  enum TpmType tpm_backend_get_type(TPMBackend *s)
     return k->type;
 }
 
-int tpm_backend_init(TPMBackend *s, TPMState *state)
+int tpm_backend_init(TPMBackend *s, TPMIf *tpmif)
 {
-    s->tpm_state = state;
+    if (s->tpmif) {
+        return -1;
+    }
+
+    s->tpmif = tpmif;
+    object_ref(OBJECT(tpmif));
+
     s->had_startup_error = false;
 
     return 0;
@@ -194,6 +199,7 @@  static void tpm_backend_instance_finalize(Object *obj)
 {
     TPMBackend *s = TPM_BACKEND(obj);
 
+    object_unref(OBJECT(s->tpmif));
     g_free(s->id);
     tpm_backend_thread_end(s);
 }
diff --git a/hw/tpm/tpm_emulator.c b/hw/tpm/tpm_emulator.c
index f04f4e0830..07e7aa4abc 100644
--- a/hw/tpm/tpm_emulator.c
+++ b/hw/tpm/tpm_emulator.c
@@ -176,7 +176,7 @@  static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number,
 static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd)
 {
     TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
-    TPMIfClass *tic = TPM_IF_GET_CLASS(tb->tpm_state);
+    TPMIfClass *tic = TPM_IF_GET_CLASS(tb->tpmif);
     Error *err = NULL;
 
     DPRINTF("processing TPM command");
@@ -191,7 +191,7 @@  static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd)
         goto error;
     }
 
-    tic->request_completed(TPM_IF(tb->tpm_state));
+    tic->request_completed(tb->tpmif);
     return;
 
 error:
diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c
index c440aff4b2..2ad74badca 100644
--- a/hw/tpm/tpm_passthrough.c
+++ b/hw/tpm/tpm_passthrough.c
@@ -139,14 +139,14 @@  err_exit:
 static void tpm_passthrough_handle_request(TPMBackend *tb, TPMBackendCmd *cmd)
 {
     TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
-    TPMIfClass *tic = TPM_IF_GET_CLASS(tb->tpm_state);
+    TPMIfClass *tic = TPM_IF_GET_CLASS(tb->tpmif);
 
     DPRINTF("tpm_passthrough: processing command %p\n", cmd);
 
     tpm_passthrough_unix_tx_bufs(tpm_pt, cmd->in, cmd->in_len,
                                  cmd->out, cmd->out_len, &cmd->selftest_done);
 
-    tic->request_completed(TPM_IF(tb->tpm_state));
+    tic->request_completed(tb->tpmif);
 }
 
 static void tpm_passthrough_reset(TPMBackend *tb)
diff --git a/hw/tpm/tpm_tis.c b/hw/tpm/tpm_tis.c
index 8c5cac5fa5..c24be57136 100644
--- a/hw/tpm/tpm_tis.c
+++ b/hw/tpm/tpm_tis.c
@@ -1077,7 +1077,7 @@  static void tpm_tis_realizefn(DeviceState *dev, Error **errp)
 
     s->be_driver->fe_model = TPM_MODEL_TPM_TIS;
 
-    if (tpm_backend_init(s->be_driver, s)) {
+    if (tpm_backend_init(s->be_driver, TPM_IF(s))) {
         error_setg(errp, "tpm_tis: backend driver with id %s could not be "
                    "initialized", s->backend);
         return;