Skip to content

Commit 932e3a5

Browse files
dabros-janjarkkojs
authored andcommitted
char: tpm: cr50: Use generic request/relinquish locality ops
Instead of using static functions tpm_cr50_request_locality and tpm_cr50_release_locality register callbacks from tpm class chip->ops created for this purpose. Signed-off-by: Jan Dabros <[email protected]> Signed-off-by: Grzegorz Bernacki <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]>
1 parent 02e9bda commit 932e3a5

File tree

1 file changed

+54
-40
lines changed

1 file changed

+54
-40
lines changed

drivers/char/tpm/tpm_tis_i2c_cr50.c

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include <linux/acpi.h>
20+
#include <linux/bug.h>
2021
#include <linux/completion.h>
2122
#include <linux/i2c.h>
2223
#include <linux/interrupt.h>
@@ -35,6 +36,7 @@
3536
#define TPM_CR50_I2C_MAX_RETRIES 3 /* Max retries due to I2C errors */
3637
#define TPM_CR50_I2C_RETRY_DELAY_LO 55 /* Min usecs between retries on I2C */
3738
#define TPM_CR50_I2C_RETRY_DELAY_HI 65 /* Max usecs between retries on I2C */
39+
#define TPM_CR50_I2C_DEFAULT_LOC 0
3840

3941
#define TPM_I2C_ACCESS(l) (0x0000 | ((l) << 4))
4042
#define TPM_I2C_STS(l) (0x0001 | ((l) << 4))
@@ -285,74 +287,84 @@ static int tpm_cr50_i2c_write(struct tpm_chip *chip, u8 addr, u8 *buffer,
285287
}
286288

287289
/**
288-
* tpm_cr50_check_locality() - Verify TPM locality 0 is active.
290+
* tpm_cr50_check_locality() - Verify if required TPM locality is active.
289291
* @chip: A TPM chip.
292+
* @loc: Locality to be verified
290293
*
291294
* Return:
292-
* - 0: Success.
295+
* - loc: Success.
293296
* - -errno: A POSIX error code.
294297
*/
295-
static int tpm_cr50_check_locality(struct tpm_chip *chip)
298+
static int tpm_cr50_check_locality(struct tpm_chip *chip, int loc)
296299
{
297300
u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
298301
u8 buf;
299302
int rc;
300303

301-
rc = tpm_cr50_i2c_read(chip, TPM_I2C_ACCESS(0), &buf, sizeof(buf));
304+
rc = tpm_cr50_i2c_read(chip, TPM_I2C_ACCESS(loc), &buf, sizeof(buf));
302305
if (rc < 0)
303306
return rc;
304307

305308
if ((buf & mask) == mask)
306-
return 0;
309+
return loc;
307310

308311
return -EIO;
309312
}
310313

311314
/**
312315
* tpm_cr50_release_locality() - Release TPM locality.
313316
* @chip: A TPM chip.
314-
* @force: Flag to force release if set.
317+
* @loc: Locality to be released
318+
*
319+
* Return:
320+
* - 0: Success.
321+
* - -errno: A POSIX error code.
315322
*/
316-
static void tpm_cr50_release_locality(struct tpm_chip *chip, bool force)
323+
static int tpm_cr50_release_locality(struct tpm_chip *chip, int loc)
317324
{
318325
u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
319-
u8 addr = TPM_I2C_ACCESS(0);
326+
u8 addr = TPM_I2C_ACCESS(loc);
320327
u8 buf;
328+
int rc;
321329

322-
if (tpm_cr50_i2c_read(chip, addr, &buf, sizeof(buf)) < 0)
323-
return;
330+
rc = tpm_cr50_i2c_read(chip, addr, &buf, sizeof(buf));
331+
if (rc < 0)
332+
return rc;
324333

325-
if (force || (buf & mask) == mask) {
334+
if ((buf & mask) == mask) {
326335
buf = TPM_ACCESS_ACTIVE_LOCALITY;
327-
tpm_cr50_i2c_write(chip, addr, &buf, sizeof(buf));
336+
rc = tpm_cr50_i2c_write(chip, addr, &buf, sizeof(buf));
328337
}
338+
339+
return rc;
329340
}
330341

331342
/**
332-
* tpm_cr50_request_locality() - Request TPM locality 0.
343+
* tpm_cr50_request_locality() - Request TPM locality.
333344
* @chip: A TPM chip.
345+
* @loc: Locality to be requested.
334346
*
335347
* Return:
336-
* - 0: Success.
348+
* - loc: Success.
337349
* - -errno: A POSIX error code.
338350
*/
339-
static int tpm_cr50_request_locality(struct tpm_chip *chip)
351+
static int tpm_cr50_request_locality(struct tpm_chip *chip, int loc)
340352
{
341353
u8 buf = TPM_ACCESS_REQUEST_USE;
342354
unsigned long stop;
343355
int rc;
344356

345-
if (!tpm_cr50_check_locality(chip))
346-
return 0;
357+
if (tpm_cr50_check_locality(chip, loc) == loc)
358+
return loc;
347359

348-
rc = tpm_cr50_i2c_write(chip, TPM_I2C_ACCESS(0), &buf, sizeof(buf));
360+
rc = tpm_cr50_i2c_write(chip, TPM_I2C_ACCESS(loc), &buf, sizeof(buf));
349361
if (rc < 0)
350362
return rc;
351363

352364
stop = jiffies + chip->timeout_a;
353365
do {
354-
if (!tpm_cr50_check_locality(chip))
355-
return 0;
366+
if (tpm_cr50_check_locality(chip, loc) == loc)
367+
return loc;
356368

357369
msleep(TPM_CR50_TIMEOUT_SHORT_MS);
358370
} while (time_before(jiffies, stop));
@@ -373,7 +385,7 @@ static u8 tpm_cr50_i2c_tis_status(struct tpm_chip *chip)
373385
{
374386
u8 buf[4];
375387

376-
if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(0), buf, sizeof(buf)) < 0)
388+
if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(chip->locality), buf, sizeof(buf)) < 0)
377389
return 0;
378390

379391
return buf[0];
@@ -389,7 +401,7 @@ static void tpm_cr50_i2c_tis_set_ready(struct tpm_chip *chip)
389401
{
390402
u8 buf[4] = { TPM_STS_COMMAND_READY };
391403

392-
tpm_cr50_i2c_write(chip, TPM_I2C_STS(0), buf, sizeof(buf));
404+
tpm_cr50_i2c_write(chip, TPM_I2C_STS(chip->locality), buf, sizeof(buf));
393405
msleep(TPM_CR50_TIMEOUT_SHORT_MS);
394406
}
395407

@@ -419,7 +431,7 @@ static int tpm_cr50_i2c_get_burst_and_status(struct tpm_chip *chip, u8 mask,
419431
stop = jiffies + chip->timeout_b;
420432

421433
do {
422-
if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(0), buf, sizeof(buf)) < 0) {
434+
if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(chip->locality), buf, sizeof(buf)) < 0) {
423435
msleep(TPM_CR50_TIMEOUT_SHORT_MS);
424436
continue;
425437
}
@@ -453,7 +465,7 @@ static int tpm_cr50_i2c_tis_recv(struct tpm_chip *chip, u8 *buf, size_t buf_len)
453465

454466
u8 mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
455467
size_t burstcnt, cur, len, expected;
456-
u8 addr = TPM_I2C_DATA_FIFO(0);
468+
u8 addr = TPM_I2C_DATA_FIFO(chip->locality);
457469
u32 status;
458470
int rc;
459471

@@ -515,15 +527,13 @@ static int tpm_cr50_i2c_tis_recv(struct tpm_chip *chip, u8 *buf, size_t buf_len)
515527
goto out_err;
516528
}
517529

518-
tpm_cr50_release_locality(chip, false);
519530
return cur;
520531

521532
out_err:
522533
/* Abort current transaction if still pending */
523534
if (tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
524535
tpm_cr50_i2c_tis_set_ready(chip);
525536

526-
tpm_cr50_release_locality(chip, false);
527537
return rc;
528538
}
529539

@@ -545,10 +555,6 @@ static int tpm_cr50_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
545555
u32 status;
546556
int rc;
547557

548-
rc = tpm_cr50_request_locality(chip);
549-
if (rc < 0)
550-
return rc;
551-
552558
/* Wait until TPM is ready for a command */
553559
stop = jiffies + chip->timeout_b;
554560
while (!(tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
@@ -577,7 +583,8 @@ static int tpm_cr50_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
577583
* that is inserted by tpm_cr50_i2c_write()
578584
*/
579585
limit = min_t(size_t, burstcnt - 1, len);
580-
rc = tpm_cr50_i2c_write(chip, TPM_I2C_DATA_FIFO(0), &buf[sent], limit);
586+
rc = tpm_cr50_i2c_write(chip, TPM_I2C_DATA_FIFO(chip->locality),
587+
&buf[sent], limit);
581588
if (rc < 0) {
582589
dev_err(&chip->dev, "Write failed\n");
583590
goto out_err;
@@ -598,7 +605,7 @@ static int tpm_cr50_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
598605
}
599606

600607
/* Start the TPM command */
601-
rc = tpm_cr50_i2c_write(chip, TPM_I2C_STS(0), tpm_go,
608+
rc = tpm_cr50_i2c_write(chip, TPM_I2C_STS(chip->locality), tpm_go,
602609
sizeof(tpm_go));
603610
if (rc < 0) {
604611
dev_err(&chip->dev, "Start command failed\n");
@@ -611,7 +618,6 @@ static int tpm_cr50_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
611618
if (tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
612619
tpm_cr50_i2c_tis_set_ready(chip);
613620

614-
tpm_cr50_release_locality(chip, false);
615621
return rc;
616622
}
617623

@@ -650,6 +656,8 @@ static const struct tpm_class_ops cr50_i2c = {
650656
.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
651657
.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
652658
.req_canceled = &tpm_cr50_i2c_req_canceled,
659+
.request_locality = &tpm_cr50_request_locality,
660+
.relinquish_locality = &tpm_cr50_release_locality,
653661
};
654662

655663
#ifdef CONFIG_ACPI
@@ -684,6 +692,7 @@ static int tpm_cr50_i2c_probe(struct i2c_client *client)
684692
u32 vendor;
685693
u8 buf[4];
686694
int rc;
695+
int loc;
687696

688697
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
689698
return -ENODEV;
@@ -726,24 +735,30 @@ static int tpm_cr50_i2c_probe(struct i2c_client *client)
726735
TPM_CR50_TIMEOUT_NOIRQ_MS);
727736
}
728737

729-
rc = tpm_cr50_request_locality(chip);
730-
if (rc < 0) {
738+
loc = tpm_cr50_request_locality(chip, TPM_CR50_I2C_DEFAULT_LOC);
739+
if (loc < 0) {
731740
dev_err(dev, "Could not request locality\n");
732-
return rc;
741+
return loc;
733742
}
734743

735744
/* Read four bytes from DID_VID register */
736-
rc = tpm_cr50_i2c_read(chip, TPM_I2C_DID_VID(0), buf, sizeof(buf));
745+
rc = tpm_cr50_i2c_read(chip, TPM_I2C_DID_VID(loc), buf, sizeof(buf));
737746
if (rc < 0) {
738747
dev_err(dev, "Could not read vendor id\n");
739-
tpm_cr50_release_locality(chip, true);
748+
if (tpm_cr50_release_locality(chip, loc))
749+
dev_err(dev, "Could not release locality\n");
750+
return rc;
751+
}
752+
753+
rc = tpm_cr50_release_locality(chip, loc);
754+
if (rc) {
755+
dev_err(dev, "Could not release locality\n");
740756
return rc;
741757
}
742758

743759
vendor = le32_to_cpup((__le32 *)buf);
744760
if (vendor != TPM_CR50_I2C_DID_VID && vendor != TPM_TI50_I2C_DID_VID) {
745761
dev_err(dev, "Vendor ID did not match! ID was %08x\n", vendor);
746-
tpm_cr50_release_locality(chip, true);
747762
return -ENODEV;
748763
}
749764

@@ -772,7 +787,6 @@ static void tpm_cr50_i2c_remove(struct i2c_client *client)
772787
}
773788

774789
tpm_chip_unregister(chip);
775-
tpm_cr50_release_locality(chip, true);
776790
}
777791

778792
static SIMPLE_DEV_PM_OPS(cr50_i2c_pm, tpm_pm_suspend, tpm_pm_resume);

0 commit comments

Comments
 (0)