blob: b2bda8c928cb447725062a2bf4cb7e0c95753244 [file] [log] [blame]
Vadim Bendeburye31d2432016-04-09 18:33:49 -07001/*
2 * Copyright 2016 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 *
6 * This is a driver for a SPI interfaced TPM2 device.
7 *
8 * It assumes that the required SPI interface has been initialized before the
9 * driver is started. A 'sruct spi_slave' pointer passed at initialization is
10 * used to direct traffic to the correct SPI interface. This dirver does not
11 * provide a way to instantiate multiple TPM devices. Also, to keep things
12 * simple, the driver unconditionally uses of TPM locality zero.
13 *
14 * References to documentation are based on the TCG issued "TPM Profile (PTP)
15 * Specification Revision 00.43".
16 */
17
18#include <commonlib/endian.h>
19#include <console/console.h>
20#include <delay.h>
21#include <endian.h>
22#include <string.h>
23#include <timer.h>
24
25#include "tpm.h"
26
Vadim Bendebury05155c02016-06-23 12:03:18 -070027#define TPM_LOCALITY_0_SPI_BASE 0x00d40000
28
Vadim Bendeburye31d2432016-04-09 18:33:49 -070029/* Assorted TPM2 registers for interface type FIFO. */
Vadim Bendebury05155c02016-06-23 12:03:18 -070030#define TPM_ACCESS_REG (TPM_LOCALITY_0_SPI_BASE + 0)
31#define TPM_STS_REG (TPM_LOCALITY_0_SPI_BASE + 0x18)
32#define TPM_DATA_FIFO_REG (TPM_LOCALITY_0_SPI_BASE + 0x24)
33#define TPM_DID_VID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf00)
34#define TPM_RID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf04)
Vadim Bendebury58826fc2016-06-23 18:17:33 -070035#define TPM_FW_VER (TPM_LOCALITY_0_SPI_BASE + 0xf90)
Vadim Bendeburye31d2432016-04-09 18:33:49 -070036
37/* SPI Interface descriptor used by the driver. */
38struct tpm_spi_if {
Furquan Shaikh36b81af2016-12-01 01:02:44 -080039 struct spi_slave slave;
Furquan Shaikh0dba0252016-11-30 04:34:22 -080040 int (*cs_assert)(const struct spi_slave *slave);
41 void (*cs_deassert)(const struct spi_slave *slave);
42 int (*xfer)(const struct spi_slave *slave, const void *dout,
43 size_t bytesout, void *din,
44 size_t bytesin);
Vadim Bendeburye31d2432016-04-09 18:33:49 -070045};
46
47/* Use the common SPI driver wrapper as the interface callbacks. */
48static struct tpm_spi_if tpm_if = {
49 .cs_assert = spi_claim_bus,
50 .cs_deassert = spi_release_bus,
51 .xfer = spi_xfer
52};
53
54/* Cached TPM device identification. */
55struct tpm2_info tpm_info;
56
57/*
58 * TODO(vbendeb): make CONFIG_DEBUG_TPM an int to allow different level of
59 * debug traces. Right now it is either 0 or 1.
60 */
61static const int debug_level_ = CONFIG_DEBUG_TPM;
62
63/* Locality management bits (in TPM_ACCESS_REG) */
64enum tpm_access_bits {
65 tpm_reg_valid_sts = (1 << 7),
66 active_locality = (1 << 5),
67 request_use = (1 << 1),
68 tpm_establishment = (1 << 0),
69};
70
71/*
72 * Variuous fields of the TPM status register, arguably the most important
73 * register when interfacing to a TPM.
74 */
75enum tpm_sts_bits {
76 tpm_family_shift = 26,
77 tpm_family_mask = ((1 << 2) - 1), /* 2 bits wide. */
78 tpm_family_tpm2 = 1,
79 reset_establishment_bit = (1 << 25),
80 command_cancel = (1 << 24),
81 burst_count_shift = 8,
82 burst_count_mask = ((1 << 16) - 1), /* 16 bits wide. */
83 sts_valid = (1 << 7),
84 command_ready = (1 << 6),
85 tpm_go = (1 << 5),
86 data_avail = (1 << 4),
87 expect = (1 << 3),
88 self_test_done = (1 << 2),
89 response_retry = (1 << 1),
90};
91
92/*
93 * SPI frame header for TPM transactions is 4 bytes in size, it is described
94 * in section "6.4.6 Spi Bit Protocol".
95 */
96typedef struct {
97 unsigned char body[4];
98} spi_frame_header;
99
100void tpm2_get_info(struct tpm2_info *info)
101{
102 *info = tpm_info;
103}
104
105/*
106 * Each TPM2 SPI transaction starts the same: CS is asserted, the 4 byte
107 * header is sent to the TPM, the master waits til TPM is ready to continue.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800108 *
109 * Returns 1 on success, 0 on failure (TPM SPI flow control timeout.)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700110 */
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800111static int start_transaction(int read_write, size_t bytes, unsigned addr)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700112{
113 spi_frame_header header;
114 uint8_t byte;
115 int i;
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800116 struct stopwatch sw;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700117
118 /*
119 * Give it 10 ms. TODO(vbendeb): remove this once cr50 SPS TPM driver
120 * performance is fixed.
121 */
122 mdelay(10);
123
124 /*
125 * The first byte of the frame header encodes the transaction type
126 * (read or write) and transfer size (set to lentgh - 1), limited to
127 * 64 bytes.
128 */
129 header.body[0] = (read_write ? 0x80 : 0) | 0x40 | (bytes - 1);
130
131 /* The rest of the frame header is the TPM register address. */
132 for (i = 0; i < 3; i++)
133 header.body[i + 1] = (addr >> (8 * (2 - i))) & 0xff;
134
135 /* CS assert wakes up the slave. */
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800136 tpm_if.cs_assert(&tpm_if.slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700137
138 /*
139 * The TCG TPM over SPI specification introduces the notion of SPI
140 * flow control (Section "6.4.5 Flow Control").
141 *
142 * Again, the slave (TPM device) expects each transaction to start
143 * with a 4 byte header trasmitted by master. The header indicates if
144 * the master needs to read or write a register, and the register
145 * address.
146 *
147 * If the slave needs to stall the transaction (for instance it is not
148 * ready to send the register value to the master), it sets the MOSI
149 * line to 0 during the last clock of the 4 byte header. In this case
150 * the master is supposed to start polling the SPI bus, one byte at
151 * time, until the last bit in the received byte (transferred during
152 * the last clock of the byte) is set to 1.
153 *
154 * Due to some SPI controllers' shortcomings (Rockchip comes to
155 * mind...) we trasmit the 4 byte header without checking the byte
156 * transmitted by the TPM during the transaction's last byte.
157 *
158 * We know that cr50 is guaranteed to set the flow control bit to 0
159 * during the header transfer, but real TPM2 might be fast enough not
160 * to require to stall the master, this would present an issue.
161 * crosbug.com/p/52132 has been opened to track this.
162 */
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800163 tpm_if.xfer(&tpm_if.slave, header.body, sizeof(header.body), NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700164
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800165 /*
166 * Now poll the bus until TPM removes the stall bit. Give it up to 100
167 * ms to sort it out - it could be saving stuff in nvram at some
168 * point.
169 */
170 stopwatch_init_msecs_expire(&sw, 100);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700171 do {
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800172 if (stopwatch_expired(&sw)) {
173 printk(BIOS_ERR, "TPM flow control failure\n");
174 return 0;
175 }
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800176 tpm_if.xfer(&tpm_if.slave, NULL, 0, &byte, 1);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700177 } while (!(byte & 1));
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800178 return 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700179}
180
181/*
182 * Print out the contents of a buffer, if debug is enabled. Skip registers
183 * other than FIFO, unless debug_level_ is 2.
184 */
185static void trace_dump(const char *prefix, uint32_t reg,
186 size_t bytes, const uint8_t *buffer,
187 int force)
188{
189 static char prev_prefix;
190 static unsigned prev_reg;
191 static int current_char;
192 const int BYTES_PER_LINE = 32;
193
194 if (!force) {
195 if (!debug_level_)
196 return;
197
198 if ((debug_level_ < 2) && (reg != TPM_DATA_FIFO_REG))
199 return;
200 }
201
202 /*
203 * Do not print register address again if the last dump print was for
204 * that register.
205 */
206 if ((prev_prefix != *prefix) || (prev_reg != reg)) {
207 prev_prefix = *prefix;
208 prev_reg = reg;
209 printk(BIOS_DEBUG, "\n%s %2.2x:", prefix, reg);
210 current_char = 0;
211 }
212
213 if ((reg != TPM_DATA_FIFO_REG) && (bytes == 4)) {
214 /*
215 * This must be a regular register address, print the 32 bit
216 * value.
217 */
218 printk(BIOS_DEBUG, " %8.8x", *(const uint32_t *)buffer);
219 } else {
220 int i;
221
222 /*
223 * Data read from or written to FIFO or not in 4 byte
224 * quantiites is printed byte at a time.
225 */
226 for (i = 0; i < bytes; i++) {
227 if (current_char && !(current_char % BYTES_PER_LINE)) {
228 printk(BIOS_DEBUG, "\n ");
229 current_char = 0;
230 }
231 current_char++;
232 printk(BIOS_DEBUG, " %2.2x", buffer[i]);
233 }
234 }
235}
236
237/*
238 * Once transaction is initiated and the TPM indicated that it is ready to go,
239 * write the actual bytes to the register.
240 */
241static void write_bytes(const void *buffer, size_t bytes)
242{
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800243 tpm_if.xfer(&tpm_if.slave, buffer, bytes, NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700244}
245
246/*
247 * Once transaction is initiated and the TPM indicated that it is ready to go,
248 * read the actual bytes from the register.
249 */
250static void read_bytes(void *buffer, size_t bytes)
251{
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800252 tpm_if.xfer(&tpm_if.slave, NULL, 0, buffer, bytes);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700253}
254
255/*
256 * To write a register, start transaction, transfer data to the TPM, deassert
257 * CS when done.
258 *
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800259 * Returns one to indicate success, zero to indicate failure.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700260 */
261static int tpm2_write_reg(unsigned reg_number, const void *buffer, size_t bytes)
262{
263 trace_dump("W", reg_number, bytes, buffer, 0);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800264 if (!start_transaction(false, bytes, reg_number))
265 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700266 write_bytes(buffer, bytes);
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800267 tpm_if.cs_deassert(&tpm_if.slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700268 return 1;
269}
270
271/*
272 * To read a register, start transaction, transfer data from the TPM, deassert
273 * CS when done.
274 *
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800275 * Returns one to indicate success, zero to indicate failure.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700276 */
277static int tpm2_read_reg(unsigned reg_number, void *buffer, size_t bytes)
278{
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800279 if (!start_transaction(true, bytes, reg_number)) {
280 memset(buffer, 0, bytes);
281 return 0;
282 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700283 read_bytes(buffer, bytes);
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800284 tpm_if.cs_deassert(&tpm_if.slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700285 trace_dump("R", reg_number, bytes, buffer, 0);
286 return 1;
287}
288
289/*
290 * Status register is accessed often, wrap reading and writing it into
291 * dedicated functions.
292 */
293static int read_tpm_sts(uint32_t *status)
294{
295 return tpm2_read_reg(TPM_STS_REG, status, sizeof(*status));
296}
297
298static int write_tpm_sts(uint32_t status)
299{
300 return tpm2_write_reg(TPM_STS_REG, &status, sizeof(status));
301}
302
303/*
304 * The TPM may limit the transaction bytes count (burst count) below the 64
305 * bytes max. The current value is available as a field of the status
306 * register.
307 */
308static uint32_t get_burst_count(void)
309{
310 uint32_t status;
311
312 read_tpm_sts(&status);
313 return (status >> burst_count_shift) & burst_count_mask;
314}
315
316int tpm2_init(struct spi_slave *spi_if)
317{
318 uint32_t did_vid, status;
319 uint8_t cmd;
320
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800321 memcpy(&tpm_if.slave, spi_if, sizeof(*spi_if));
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700322
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800323 /*
324 * It is enough to check the first register read error status to bail
325 * out in case of malfunctioning TPM.
326 */
327 if (!tpm2_read_reg(TPM_DID_VID_REG, &did_vid, sizeof(did_vid)))
328 return -1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700329
330 /* Try claiming locality zero. */
331 tpm2_read_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
332 if ((cmd & (active_locality & tpm_reg_valid_sts)) ==
333 (active_locality & tpm_reg_valid_sts)) {
334 /*
335 * Locality active - maybe reset line is not connected?
336 * Release the locality and try again
337 */
338 cmd = active_locality;
339 tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
340 tpm2_read_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
341 }
342
343 /* The tpm_establishment bit can be either set or not, ignore it. */
344 if ((cmd & ~tpm_establishment) != tpm_reg_valid_sts) {
345 printk(BIOS_ERR, "invalid reset status: %#x\n", cmd);
346 return -1;
347 }
348
349 cmd = request_use;
350 tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
351 tpm2_read_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
352 if ((cmd & ~tpm_establishment) !=
353 (tpm_reg_valid_sts | active_locality)) {
354 printk(BIOS_ERR, "failed to claim locality 0, status: %#x\n",
355 cmd);
356 return -1;
357 }
358
359 read_tpm_sts(&status);
360 if (((status >> tpm_family_shift) & tpm_family_mask) !=
361 tpm_family_tpm2) {
362 printk(BIOS_ERR, "unexpected TPM family value, status: %#x\n",
363 status);
364 return -1;
365 }
366
367 /*
368 * Locality claimed, read the revision value and set up the tpm_info
369 * structure.
370 */
371 tpm2_read_reg(TPM_RID_REG, &cmd, sizeof(cmd));
372 tpm_info.vendor_id = did_vid & 0xffff;
373 tpm_info.device_id = did_vid >> 16;
374 tpm_info.revision = cmd;
375
376 printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n",
377 tpm_info.vendor_id, tpm_info.device_id, tpm_info.revision);
378
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700379 /* Let's report device FW version if available. */
380 if (tpm_info.vendor_id == 0x1ae0) {
381 int chunk_count = 0;
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700382 size_t chunk_size;
383 /*
384 * let's read 50 bytes at a time; leave room for the trailing
385 * zero.
386 */
387 char vstr[51];
388
389 chunk_size = sizeof(vstr) - 1;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700390
391 printk(BIOS_INFO, "Firmware version: ");
392
393 /*
394 * Does not really matter what's written, this just makes sure
395 * the version is reported from the beginning.
396 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700397 tpm2_write_reg(TPM_FW_VER, &chunk_size, 1);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700398
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700399 /* Print it out in sizeof(vstr) - 1 byte chunks. */
400 vstr[chunk_size] = 0;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700401 do {
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700402 tpm2_read_reg(TPM_FW_VER, vstr, chunk_size);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700403 printk(BIOS_INFO, "%s", vstr);
404
405 /*
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700406 * While string is not over, and is no longer than 300
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700407 * characters.
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700408 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700409 } while (vstr[chunk_size - 1] &&
410 (chunk_count++ < (300 / chunk_size)));
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700411
412 printk(BIOS_INFO, "\n");
413 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700414 return 0;
415}
416
417/*
418 * This is in seconds, certain TPM commands, like key generation, can take
419 * long time to complete.
420 *
421 * Returns one to indicate success, zero (not yet implemented) to indicate
422 * failure.
423 */
424#define MAX_STATUS_TIMEOUT 120
425static int wait_for_status(uint32_t status_mask, uint32_t status_expected)
426{
427 uint32_t status;
428 struct stopwatch sw;
429
430 stopwatch_init_usecs_expire(&sw, MAX_STATUS_TIMEOUT * 1000 * 1000);
431 do {
432 udelay(1000);
433 if (stopwatch_expired(&sw)) {
434 printk(BIOS_ERR, "failed to get expected status %x\n",
435 status_expected);
436 return false;
437 }
438 read_tpm_sts(&status);
439 } while ((status & status_mask) != status_expected);
440
441 return 1;
442}
443
444enum fifo_transfer_direction {
445 fifo_transmit = 0,
446 fifo_receive = 1
447};
448
449/* Union allows to avoid casting away 'const' on transmit buffers. */
450union fifo_transfer_buffer {
451 uint8_t *rx_buffer;
452 const uint8_t *tx_buffer;
453};
454
455/*
456 * Transfer requested number of bytes to or from TPM FIFO, accounting for the
457 * current burst count value.
458 */
459static void fifo_transfer(size_t transfer_size,
460 union fifo_transfer_buffer buffer,
461 enum fifo_transfer_direction direction)
462{
463 size_t transaction_size;
464 size_t burst_count;
465 size_t handled_so_far = 0;
466
467 do {
468 do {
469 /* Could be zero when TPM is busy. */
470 burst_count = get_burst_count();
471 } while (!burst_count);
472
473 transaction_size = transfer_size - handled_so_far;
474 transaction_size = MIN(transaction_size, burst_count);
475
476 /*
477 * The SPI frame header does not allow to pass more than 64
478 * bytes.
479 */
480 transaction_size = MIN(transaction_size, 64);
481
482 if (direction == fifo_receive)
483 tpm2_read_reg(TPM_DATA_FIFO_REG,
484 buffer.rx_buffer + handled_so_far,
485 transaction_size);
486 else
487 tpm2_write_reg(TPM_DATA_FIFO_REG,
488 buffer.tx_buffer + handled_so_far,
489 transaction_size);
490
491 handled_so_far += transaction_size;
492
493 } while (handled_so_far != transfer_size);
494}
495
496size_t tpm2_process_command(const void *tpm2_command, size_t command_size,
497 void *tpm2_response, size_t max_response)
498{
499 uint32_t status;
500 uint32_t expected_status_bits;
501 size_t payload_size;
502 size_t bytes_to_go;
503 const uint8_t *cmd_body = tpm2_command;
504 uint8_t *rsp_body = tpm2_response;
505 union fifo_transfer_buffer fifo_buffer;
506 const int HEADER_SIZE = 6;
507
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800508 /* Do not try using an uninitialized TPM. */
509 if (!tpm_info.vendor_id)
510 return 0;
511
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700512 /* Skip the two byte tag, read the size field. */
513 payload_size = read_be32(cmd_body + 2);
514
515 /* Sanity check. */
516 if (payload_size != command_size) {
517 printk(BIOS_ERR,
518 "Command size mismatch: encoded %zd != requested %zd\n",
519 payload_size, command_size);
520 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
521 printk(BIOS_DEBUG, "\n");
522 return 0;
523 }
524
525 /* Let the TPM know that the command is coming. */
526 write_tpm_sts(command_ready);
527
528 /*
529 * Tpm commands and responses written to and read from the FIFO
530 * register (0x24) are datagrams of variable size, prepended by a 6
531 * byte header.
532 *
533 * The specification description of the state machine is a bit vague,
534 * but from experience it looks like there is no need to wait for the
535 * sts.expect bit to be set, at least with the 9670 and cr50 devices.
536 * Just write the command into FIFO, making sure not to exceed the
537 * burst count or the maximum PDU size, whatever is smaller.
538 */
539 fifo_buffer.tx_buffer = cmd_body;
540 fifo_transfer(command_size, fifo_buffer, fifo_transmit);
541
542 /* Now tell the TPM it can start processing the command. */
543 write_tpm_sts(tpm_go);
544
545 /* Now wait for it to report that the response is ready. */
546 expected_status_bits = sts_valid | data_avail;
547 if (!wait_for_status(expected_status_bits, expected_status_bits)) {
548 /*
549 * If timed out, which should never happen, let's at least
550 * print out the offending command.
551 */
552 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
553 printk(BIOS_DEBUG, "\n");
554 return 0;
555 }
556
557 /*
558 * The response is ready, let's read it. First we read the FIFO
559 * payload header, to see how much data to expect. The response header
560 * size is fixed to six bytes, the total payload size is stored in
561 * network order in the last four bytes.
562 */
563 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE);
564
565 /* Find out the total payload size, skipping the two byte tag. */
566 payload_size = read_be32(rsp_body + 2);
567
568 if (payload_size > max_response) {
569 /*
570 * TODO(vbendeb): at least drain the FIFO here or somehow let
571 * the TPM know that the response can be dropped.
572 */
573 printk(BIOS_ERR, " tpm response too long (%zd bytes)",
574 payload_size);
575 return 0;
576 }
577
578 /*
579 * Now let's read all but the last byte in the FIFO to make sure the
580 * status register is showing correct flow control bits: 'more data'
581 * until the last byte and then 'no more data' once the last byte is
582 * read.
583 */
584 bytes_to_go = payload_size - 1 - HEADER_SIZE;
585 fifo_buffer.rx_buffer = rsp_body + HEADER_SIZE;
586 fifo_transfer(bytes_to_go, fifo_buffer, fifo_receive);
587
588 /* Verify that there is still data to read. */
589 read_tpm_sts(&status);
590 if ((status & expected_status_bits) != expected_status_bits) {
591 printk(BIOS_ERR, "unexpected intermediate status %#x\n",
592 status);
593 return 0;
594 }
595
596 /* Read the last byte of the PDU. */
597 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1);
598
599 /* Terminate the dump, if enabled. */
600 if (debug_level_)
601 printk(BIOS_DEBUG, "\n");
602
603 /* Verify that 'data available' is not asseretd any more. */
604 read_tpm_sts(&status);
605 if ((status & expected_status_bits) != sts_valid) {
606 printk(BIOS_ERR, "unexpected final status %#x\n", status);
607 return 0;
608 }
609
610 /* Move the TPM back to idle state. */
611 write_tpm_sts(command_ready);
612
613 return payload_size;
614}