Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev Provide a better abstraction for the TPM interface. ...



details:   https://anonhg.NetBSD.org/src/rev/dacbdac5b132
branches:  trunk
changeset: 455153:dacbdac5b132
user:      maxv <maxv%NetBSD.org@localhost>
date:      Wed Oct 09 14:03:57 2019 +0000

description:
Provide a better abstraction for the TPM interface. Report it in the ioctl.

diffstat:

 sys/dev/acpi/tpm_acpi.c |   25 ++----
 sys/dev/ic/tpm.c        |  158 +++++++++++++++++++++++++----------------------
 sys/dev/ic/tpmvar.h     |   36 ++++++----
 sys/dev/isa/tpm_isa.c   |   25 +++----
 4 files changed, 125 insertions(+), 119 deletions(-)

diffs (truncated from 559 to 300 lines):

diff -r 3dc420692f2d -r dacbdac5b132 sys/dev/acpi/tpm_acpi.c
--- a/sys/dev/acpi/tpm_acpi.c   Wed Oct 09 13:42:37 2019 +0000
+++ b/sys/dev/acpi/tpm_acpi.c   Wed Oct 09 14:03:57 2019 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: tpm_acpi.c,v 1.10 2019/10/09 07:30:58 maxv Exp $ */
+/* $NetBSD: tpm_acpi.c,v 1.11 2019/10/09 14:03:57 maxv Exp $ */
 
 /*
  * Copyright (c) 2012, 2019 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tpm_acpi.c,v 1.10 2019/10/09 07:30:58 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tpm_acpi.c,v 1.11 2019/10/09 14:03:57 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -118,33 +118,26 @@
                    (uint64_t)mem->ar_length, TPM_SPACE_SIZE);
                goto out;
        }
+       base = mem->ar_base;
+       size = mem->ar_length;
 
        sc->sc_dev = self;
        sc->sc_ver = TPM_2_0;
        mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
        sc->sc_busy = false;
-       sc->sc_init = tpm_tis12_init;
-       sc->sc_start = tpm_tis12_start;
-       sc->sc_read = tpm_tis12_read;
-       sc->sc_write = tpm_tis12_write;
-       sc->sc_end = tpm_tis12_end;
+       sc->sc_intf = &tpm_intf_tis12;
        sc->sc_bt = aa->aa_memt;
-
-       base = mem->ar_base;
-       size = mem->ar_length;
-
        if (bus_space_map(sc->sc_bt, base, size, 0, &sc->sc_bh)) {
                aprint_error_dev(sc->sc_dev, "cannot map registers\n");
                goto out;
        }
 
-       if (!tpm_tis12_probe(sc->sc_bt, sc->sc_bh)) {
-               aprint_error_dev(sc->sc_dev, "TIS1.2 probe failed\n");
+       if ((rv = (*sc->sc_intf->probe)(sc->sc_bt, sc->sc_bh)) != 0) {
+               aprint_error_dev(sc->sc_dev, "probe failed, rv=%d\n", rv);
                goto out1;
        }
-
-       if ((*sc->sc_init)(sc) != 0) {
-               aprint_error_dev(sc->sc_dev, "cannot init device %d\n", rv);
+       if ((rv = (*sc->sc_intf->init)(sc)) != 0) {
+               aprint_error_dev(sc->sc_dev, "cannot init device, rv=%d\n", rv);
                goto out1;
        }
 
diff -r 3dc420692f2d -r dacbdac5b132 sys/dev/ic/tpm.c
--- a/sys/dev/ic/tpm.c  Wed Oct 09 13:42:37 2019 +0000
+++ b/sys/dev/ic/tpm.c  Wed Oct 09 14:03:57 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tpm.c,v 1.15 2019/10/09 07:30:58 maxv Exp $    */
+/*     $NetBSD: tpm.c,v 1.16 2019/10/09 14:03:58 maxv Exp $    */
 
 /*
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -48,7 +48,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tpm.c,v 1.15 2019/10/09 07:30:58 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tpm.c,v 1.16 2019/10/09 14:03:58 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -93,42 +93,6 @@
 }
 
 static int
-tpm_request_locality(struct tpm_softc *sc, int l)
-{
-       uint32_t r;
-       int to, rv;
-
-       if (l != 0)
-               return EINVAL;
-
-       if ((bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
-           (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) ==
-           (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY))
-               return 0;
-
-       bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
-           TPM_ACCESS_REQUEST_USE);
-
-       to = tpm_tmotohz(TPM_ACCESS_TMO);
-
-       while ((r = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
-           (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
-           (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && to--) {
-               rv = tsleep(sc->sc_init, PCATCH, "tpm_locality", 1);
-               if (rv && rv != EWOULDBLOCK) {
-                       return rv;
-               }
-       }
-
-       if ((r & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
-           (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
-               return EBUSY;
-       }
-
-       return 0;
-}
-
-static int
 tpm_getburst(struct tpm_softc *sc)
 {
        int burst, to, rv;
@@ -174,9 +138,9 @@
        };
        struct tpm_header response;
 
-       if ((*sc->sc_write)(sc, &command, sizeof(command)) != 0)
+       if ((*sc->sc_intf->write)(sc, &command, sizeof(command)) != 0)
                return false;
-       if ((*sc->sc_read)(sc, &response, sizeof(response), NULL, 0) != 0)
+       if ((*sc->sc_intf->read)(sc, &response, sizeof(response), NULL, 0) != 0)
                return false;
        if (TPM_BE32(response.code) != 0)
                return false;
@@ -195,9 +159,9 @@
        };
        struct tpm_header response;
 
-       if ((*sc->sc_write)(sc, &command, sizeof(command)) != 0)
+       if ((*sc->sc_intf->write)(sc, &command, sizeof(command)) != 0)
                return false;
-       if ((*sc->sc_read)(sc, &response, sizeof(response), NULL, 0) != 0)
+       if ((*sc->sc_intf->read)(sc, &response, sizeof(response), NULL, 0) != 0)
                return false;
        if (TPM_BE32(response.code) != 0)
                return false;
@@ -286,10 +250,46 @@
 /* -------------------------------------------------------------------------- */
 
 /*
- * TPM using TIS 1.2 interface.
+ * TPM using the TIS 1.2 interface.
  */
 
-int
+static int
+tpm12_request_locality(struct tpm_softc *sc, int l)
+{
+       uint32_t r;
+       int to, rv;
+
+       if (l != 0)
+               return EINVAL;
+
+       if ((bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
+           (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) ==
+           (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY))
+               return 0;
+
+       bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
+           TPM_ACCESS_REQUEST_USE);
+
+       to = tpm_tmotohz(TPM_ACCESS_TMO);
+
+       while ((r = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
+           (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
+           (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && to--) {
+               rv = tsleep(sc->sc_intf->init, PCATCH, "tpm_locality", 1);
+               if (rv && rv != EWOULDBLOCK) {
+                       return rv;
+               }
+       }
+
+       if ((r & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
+           (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
+               return EBUSY;
+       }
+
+       return 0;
+}
+
+static int
 tpm_tis12_probe(bus_space_tag_t bt, bus_space_handle_t bh)
 {
        uint32_t cap;
@@ -298,9 +298,9 @@
 
        cap = bus_space_read_4(bt, bh, TPM_INTF_CAPABILITY);
        if (cap == 0xffffffff)
-               return 0;
+               return EINVAL;
        if ((cap & TPM_CAPS_REQUIRED) != TPM_CAPS_REQUIRED)
-               return 0;
+               return ENOTSUP;
 
        /* Request locality 0. */
        bus_space_write_1(bt, bh, TPM_ACCESS, TPM_ACCESS_REQUEST_USE);
@@ -314,18 +314,20 @@
        }
        if ((reg & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
            (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
-               return 0;
+               return ETIMEDOUT;
        }
 
        if (bus_space_read_4(bt, bh, TPM_ID) == 0xffffffff)
-               return 0;
+               return EINVAL;
 
-       return 1;
+       return 0;
 }
 
-int
+static int
 tpm_tis12_init(struct tpm_softc *sc)
 {
+       int rv;
+
        sc->sc_caps = bus_space_read_4(sc->sc_bt, sc->sc_bh,
            TPM_INTF_CAPABILITY);
        sc->sc_devid = bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_ID);
@@ -334,8 +336,8 @@
        aprint_normal_dev(sc->sc_dev, "device 0x%08x rev 0x%x\n",
            sc->sc_devid, sc->sc_rev);
 
-       if (tpm_request_locality(sc, 0))
-               return 1;
+       if ((rv = tpm12_request_locality(sc, 0)) != 0)
+               return rv;
 
        /* Abort whatever it thought it was doing. */
        bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
@@ -343,19 +345,19 @@
        return 0;
 }
 
-int
+static int
 tpm_tis12_start(struct tpm_softc *sc, int rw)
 {
        int rv;
 
        if (rw == UIO_READ) {
                rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
-                   TPM_READ_TMO, sc->sc_read);
+                   TPM_READ_TMO, sc->sc_intf->read);
                return rv;
        }
 
        /* Request the 0th locality. */
-       if ((rv = tpm_request_locality(sc, 0)) != 0)
+       if ((rv = tpm12_request_locality(sc, 0)) != 0)
                return rv;
 
        sc->sc_status = tpm_status(sc);
@@ -364,14 +366,14 @@
 
        /* Abort previous and restart. */
        bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
-       rv = tpm_waitfor(sc, TPM_STS_CMD_READY, TPM_READY_TMO, sc->sc_write);
+       rv = tpm_waitfor(sc, TPM_STS_CMD_READY, TPM_READY_TMO, sc->sc_intf->write);
        if (rv)
                return rv;
 
        return 0;
 }
 
-int
+static int
 tpm_tis12_read(struct tpm_softc *sc, void *buf, size_t len, size_t *count,
     int flags)
 {
@@ -382,7 +384,7 @@
        cnt = 0;
        while (len > 0) {
                rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
-                   TPM_READ_TMO, sc->sc_read);
+                   TPM_READ_TMO, sc->sc_intf->read);
                if (rv)
                        return rv;
 
@@ -404,7 +406,7 @@
        return 0;
 }
 
-int
+static int
 tpm_tis12_write(struct tpm_softc *sc, const void *buf, size_t len)
 {
        const uint8_t *p = buf;
@@ -413,7 +415,7 @@



Home | Main Index | Thread Index | Old Index