Commit 062807f2 authored by Jason Gunthorpe's avatar Jason Gunthorpe Committed by Jarkko Sakkinen

tpm: Remove all uses of drvdata from the TPM Core

The final thing preventing this was the way the sysfs files were
attached to the pdev. Follow the approach developed for ppi and move
the sysfs files to the chip->dev with symlinks from the pdev
for compatibility. Everything in the core now sanely uses container_of
to get the chip.
Signed-off-by: default avatarJason Gunthorpe <jgunthorpe@obsidianresearch.com>
Signed-off-by: default avatarStefan Berger <stefanb@linux.vnet.ibm.com>
Tested-by: default avatarJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Tested-by: default avatarStefan Berger <stefanb@linux.vnet.ibm.com>
Signed-off-by: default avatarJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
parent 9e0d39d8
...@@ -170,9 +170,7 @@ struct tpm_chip *tpm_chip_alloc(struct device *dev, ...@@ -170,9 +170,7 @@ struct tpm_chip *tpm_chip_alloc(struct device *dev,
chip->dev.class = tpm_class; chip->dev.class = tpm_class;
chip->dev.release = tpm_dev_release; chip->dev.release = tpm_dev_release;
chip->dev.parent = dev; chip->dev.parent = dev;
#ifdef CONFIG_ACPI
chip->dev.groups = chip->groups; chip->dev.groups = chip->groups;
#endif
if (chip->dev_num == 0) if (chip->dev_num == 0)
chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR); chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
...@@ -277,14 +275,10 @@ static void tpm_del_char_device(struct tpm_chip *chip) ...@@ -277,14 +275,10 @@ static void tpm_del_char_device(struct tpm_chip *chip)
static int tpm1_chip_register(struct tpm_chip *chip) static int tpm1_chip_register(struct tpm_chip *chip)
{ {
int rc;
if (chip->flags & TPM_CHIP_FLAG_TPM2) if (chip->flags & TPM_CHIP_FLAG_TPM2)
return 0; return 0;
rc = tpm_sysfs_add_device(chip); tpm_sysfs_add_device(chip);
if (rc)
return rc;
chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev)); chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
...@@ -298,10 +292,50 @@ static void tpm1_chip_unregister(struct tpm_chip *chip) ...@@ -298,10 +292,50 @@ static void tpm1_chip_unregister(struct tpm_chip *chip)
if (chip->bios_dir) if (chip->bios_dir)
tpm_bios_log_teardown(chip->bios_dir); tpm_bios_log_teardown(chip->bios_dir);
}
static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
{
struct attribute **i;
if (chip->flags & TPM_CHIP_FLAG_TPM2)
return;
sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
tpm_sysfs_del_device(chip); for (i = chip->groups[0]->attrs; *i != NULL; ++i)
sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
} }
/* For compatibility with legacy sysfs paths we provide symlinks from the
* parent dev directory to selected names within the tpm chip directory. Old
* kernel versions created these files directly under the parent.
*/
static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
{
struct attribute **i;
int rc;
if (chip->flags & TPM_CHIP_FLAG_TPM2)
return 0;
rc = __compat_only_sysfs_link_entry_to_kobj(
&chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
if (rc && rc != -ENOENT)
return rc;
/* All the names from tpm-sysfs */
for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
rc = __compat_only_sysfs_link_entry_to_kobj(
&chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
if (rc) {
tpm_del_legacy_sysfs(chip);
return rc;
}
}
return 0;
}
/* /*
* tpm_chip_register() - create a character device for the TPM chip * tpm_chip_register() - create a character device for the TPM chip
* @chip: TPM chip to use. * @chip: TPM chip to use.
...@@ -324,24 +358,20 @@ int tpm_chip_register(struct tpm_chip *chip) ...@@ -324,24 +358,20 @@ int tpm_chip_register(struct tpm_chip *chip)
tpm_add_ppi(chip); tpm_add_ppi(chip);
rc = tpm_add_char_device(chip); rc = tpm_add_char_device(chip);
if (rc) if (rc) {
goto out_err; tpm1_chip_unregister(chip);
return rc;
}
chip->flags |= TPM_CHIP_FLAG_REGISTERED; chip->flags |= TPM_CHIP_FLAG_REGISTERED;
if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) { rc = tpm_add_legacy_sysfs(chip);
rc = __compat_only_sysfs_link_entry_to_kobj( if (rc) {
&chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
if (rc && rc != -ENOENT) {
tpm_chip_unregister(chip); tpm_chip_unregister(chip);
return rc; return rc;
} }
}
return 0; return 0;
out_err:
tpm1_chip_unregister(chip);
return rc;
} }
EXPORT_SYMBOL_GPL(tpm_chip_register); EXPORT_SYMBOL_GPL(tpm_chip_register);
...@@ -363,8 +393,7 @@ void tpm_chip_unregister(struct tpm_chip *chip) ...@@ -363,8 +393,7 @@ void tpm_chip_unregister(struct tpm_chip *chip)
if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED)) if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
return; return;
if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) tpm_del_legacy_sysfs(chip);
sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
tpm1_chip_unregister(chip); tpm1_chip_unregister(chip);
tpm_del_char_device(chip); tpm_del_char_device(chip);
......
...@@ -432,12 +432,11 @@ static const struct tpm_input_header tpm_getcap_header = { ...@@ -432,12 +432,11 @@ static const struct tpm_input_header tpm_getcap_header = {
.ordinal = TPM_ORD_GET_CAP .ordinal = TPM_ORD_GET_CAP
}; };
ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap, ssize_t tpm_getcap(struct tpm_chip *chip, __be32 subcap_id, cap_t *cap,
const char *desc) const char *desc)
{ {
struct tpm_cmd_t tpm_cmd; struct tpm_cmd_t tpm_cmd;
int rc; int rc;
struct tpm_chip *chip = dev_get_drvdata(dev);
tpm_cmd.header.in = tpm_getcap_header; tpm_cmd.header.in = tpm_getcap_header;
if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) { if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
...@@ -935,7 +934,7 @@ static struct tpm_input_header savestate_header = { ...@@ -935,7 +934,7 @@ static struct tpm_input_header savestate_header = {
*/ */
int tpm_pm_suspend(struct device *dev) int tpm_pm_suspend(struct device *dev)
{ {
struct tpm_chip *chip = dev_get_drvdata(dev); struct tpm_chip *chip = to_tpm_chip(dev);
struct tpm_cmd_t cmd; struct tpm_cmd_t cmd;
int rc, try; int rc, try;
...@@ -996,7 +995,7 @@ EXPORT_SYMBOL_GPL(tpm_pm_suspend); ...@@ -996,7 +995,7 @@ EXPORT_SYMBOL_GPL(tpm_pm_suspend);
*/ */
int tpm_pm_resume(struct device *dev) int tpm_pm_resume(struct device *dev)
{ {
struct tpm_chip *chip = dev_get_drvdata(dev); struct tpm_chip *chip = to_tpm_chip(dev);
if (chip == NULL) if (chip == NULL)
return -ENODEV; return -ENODEV;
......
...@@ -36,7 +36,7 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr, ...@@ -36,7 +36,7 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
int i, rc; int i, rc;
char *str = buf; char *str = buf;
struct tpm_chip *chip = dev_get_drvdata(dev); struct tpm_chip *chip = to_tpm_chip(dev);
tpm_cmd.header.in = tpm_readpubek_header; tpm_cmd.header.in = tpm_readpubek_header;
err = tpm_transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE, err = tpm_transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
...@@ -92,9 +92,9 @@ static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr, ...@@ -92,9 +92,9 @@ static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
ssize_t rc; ssize_t rc;
int i, j, num_pcrs; int i, j, num_pcrs;
char *str = buf; char *str = buf;
struct tpm_chip *chip = dev_get_drvdata(dev); struct tpm_chip *chip = to_tpm_chip(dev);
rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap, rc = tpm_getcap(chip, TPM_CAP_PROP_PCR, &cap,
"attempting to determine the number of PCRS"); "attempting to determine the number of PCRS");
if (rc) if (rc)
return 0; return 0;
...@@ -119,7 +119,7 @@ static ssize_t enabled_show(struct device *dev, struct device_attribute *attr, ...@@ -119,7 +119,7 @@ static ssize_t enabled_show(struct device *dev, struct device_attribute *attr,
cap_t cap; cap_t cap;
ssize_t rc; ssize_t rc;
rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap, rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
"attempting to determine the permanent enabled state"); "attempting to determine the permanent enabled state");
if (rc) if (rc)
return 0; return 0;
...@@ -135,7 +135,7 @@ static ssize_t active_show(struct device *dev, struct device_attribute *attr, ...@@ -135,7 +135,7 @@ static ssize_t active_show(struct device *dev, struct device_attribute *attr,
cap_t cap; cap_t cap;
ssize_t rc; ssize_t rc;
rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap, rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
"attempting to determine the permanent active state"); "attempting to determine the permanent active state");
if (rc) if (rc)
return 0; return 0;
...@@ -151,7 +151,7 @@ static ssize_t owned_show(struct device *dev, struct device_attribute *attr, ...@@ -151,7 +151,7 @@ static ssize_t owned_show(struct device *dev, struct device_attribute *attr,
cap_t cap; cap_t cap;
ssize_t rc; ssize_t rc;
rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap, rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
"attempting to determine the owner state"); "attempting to determine the owner state");
if (rc) if (rc)
return 0; return 0;
...@@ -167,7 +167,7 @@ static ssize_t temp_deactivated_show(struct device *dev, ...@@ -167,7 +167,7 @@ static ssize_t temp_deactivated_show(struct device *dev,
cap_t cap; cap_t cap;
ssize_t rc; ssize_t rc;
rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap, rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
"attempting to determine the temporary state"); "attempting to determine the temporary state");
if (rc) if (rc)
return 0; return 0;
...@@ -180,11 +180,12 @@ static DEVICE_ATTR_RO(temp_deactivated); ...@@ -180,11 +180,12 @@ static DEVICE_ATTR_RO(temp_deactivated);
static ssize_t caps_show(struct device *dev, struct device_attribute *attr, static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
char *buf) char *buf)
{ {
struct tpm_chip *chip = to_tpm_chip(dev);
cap_t cap; cap_t cap;
ssize_t rc; ssize_t rc;
char *str = buf; char *str = buf;
rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap, rc = tpm_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
"attempting to determine the manufacturer"); "attempting to determine the manufacturer");
if (rc) if (rc)
return 0; return 0;
...@@ -192,7 +193,7 @@ static ssize_t caps_show(struct device *dev, struct device_attribute *attr, ...@@ -192,7 +193,7 @@ static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
be32_to_cpu(cap.manufacturer_id)); be32_to_cpu(cap.manufacturer_id));
/* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */ /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap, rc = tpm_getcap(chip, CAP_VERSION_1_2, &cap,
"attempting to determine the 1.2 version"); "attempting to determine the 1.2 version");
if (!rc) { if (!rc) {
str += sprintf(str, str += sprintf(str,
...@@ -203,7 +204,7 @@ static ssize_t caps_show(struct device *dev, struct device_attribute *attr, ...@@ -203,7 +204,7 @@ static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
cap.tpm_version_1_2.revMinor); cap.tpm_version_1_2.revMinor);
} else { } else {
/* Otherwise just use TPM_STRUCT_VER */ /* Otherwise just use TPM_STRUCT_VER */
rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap, rc = tpm_getcap(chip, CAP_VERSION_1_1, &cap,
"attempting to determine the 1.1 version"); "attempting to determine the 1.1 version");
if (rc) if (rc)
return 0; return 0;
...@@ -222,7 +223,7 @@ static DEVICE_ATTR_RO(caps); ...@@ -222,7 +223,7 @@ static DEVICE_ATTR_RO(caps);
static ssize_t cancel_store(struct device *dev, struct device_attribute *attr, static ssize_t cancel_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count) const char *buf, size_t count)
{ {
struct tpm_chip *chip = dev_get_drvdata(dev); struct tpm_chip *chip = to_tpm_chip(dev);
if (chip == NULL) if (chip == NULL)
return 0; return 0;
...@@ -234,7 +235,7 @@ static DEVICE_ATTR_WO(cancel); ...@@ -234,7 +235,7 @@ static DEVICE_ATTR_WO(cancel);
static ssize_t durations_show(struct device *dev, struct device_attribute *attr, static ssize_t durations_show(struct device *dev, struct device_attribute *attr,
char *buf) char *buf)
{ {
struct tpm_chip *chip = dev_get_drvdata(dev); struct tpm_chip *chip = to_tpm_chip(dev);
if (chip->duration[TPM_LONG] == 0) if (chip->duration[TPM_LONG] == 0)
return 0; return 0;
...@@ -251,7 +252,7 @@ static DEVICE_ATTR_RO(durations); ...@@ -251,7 +252,7 @@ static DEVICE_ATTR_RO(durations);
static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr, static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr,
char *buf) char *buf)
{ {
struct tpm_chip *chip = dev_get_drvdata(dev); struct tpm_chip *chip = to_tpm_chip(dev);
return sprintf(buf, "%d %d %d %d [%s]\n", return sprintf(buf, "%d %d %d %d [%s]\n",
jiffies_to_usecs(chip->timeout_a), jiffies_to_usecs(chip->timeout_a),
...@@ -281,24 +282,12 @@ static const struct attribute_group tpm_dev_group = { ...@@ -281,24 +282,12 @@ static const struct attribute_group tpm_dev_group = {
.attrs = tpm_dev_attrs, .attrs = tpm_dev_attrs,
}; };
int tpm_sysfs_add_device(struct tpm_chip *chip) void tpm_sysfs_add_device(struct tpm_chip *chip)
{ {
int err; /* The sysfs routines rely on an implicit tpm_try_get_ops, device_del
err = sysfs_create_group(&chip->dev.parent->kobj, * is called before ops is null'd and the sysfs core synchronizes this
&tpm_dev_group); * removal so that no callbacks are running or can run again
if (err)
dev_err(&chip->dev,
"failed to create sysfs attributes, %d\n", err);
return err;
}
void tpm_sysfs_del_device(struct tpm_chip *chip)
{
/* The sysfs routines rely on an implicit tpm_try_get_ops, this
* function is called before ops is null'd and the sysfs core
* synchronizes this removal so that no callbacks are running or can
* run again
*/ */
sysfs_remove_group(&chip->dev.parent->kobj, &tpm_dev_group); WARN_ON(chip->groups_cnt != 0);
chip->groups[chip->groups_cnt++] = &tpm_dev_group;
} }
...@@ -168,9 +168,9 @@ struct tpm_chip { ...@@ -168,9 +168,9 @@ struct tpm_chip {
struct dentry **bios_dir; struct dentry **bios_dir;
#ifdef CONFIG_ACPI const struct attribute_group *groups[3];
const struct attribute_group *groups[2];
unsigned int groups_cnt; unsigned int groups_cnt;
#ifdef CONFIG_ACPI
acpi_handle acpi_dev_handle; acpi_handle acpi_dev_handle;
char ppi_version[TPM_PPI_VERSION_LEN + 1]; char ppi_version[TPM_PPI_VERSION_LEN + 1];
#endif /* CONFIG_ACPI */ #endif /* CONFIG_ACPI */
...@@ -471,7 +471,8 @@ extern dev_t tpm_devt; ...@@ -471,7 +471,8 @@ extern dev_t tpm_devt;
extern const struct file_operations tpm_fops; extern const struct file_operations tpm_fops;
extern struct idr dev_nums_idr; extern struct idr dev_nums_idr;
ssize_t tpm_getcap(struct device *, __be32, cap_t *, const char *); ssize_t tpm_getcap(struct tpm_chip *chip, __be32 subcap_id, cap_t *cap,
const char *desc);
ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf, ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
size_t bufsiz); size_t bufsiz);
ssize_t tpm_transmit_cmd(struct tpm_chip *chip, void *cmd, int len, ssize_t tpm_transmit_cmd(struct tpm_chip *chip, void *cmd, int len,
...@@ -496,8 +497,7 @@ extern struct tpm_chip *tpmm_chip_alloc(struct device *pdev, ...@@ -496,8 +497,7 @@ extern struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
extern int tpm_chip_register(struct tpm_chip *chip); extern int tpm_chip_register(struct tpm_chip *chip);
extern void tpm_chip_unregister(struct tpm_chip *chip); extern void tpm_chip_unregister(struct tpm_chip *chip);
int tpm_sysfs_add_device(struct tpm_chip *chip); void tpm_sysfs_add_device(struct tpm_chip *chip);
void tpm_sysfs_del_device(struct tpm_chip *chip);
int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf); int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment