blob: cd1fbdf73bee395f0501b37e649ebb242438824a [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>
Jeffy Chen19e3d332017-03-03 18:24:02 +080024#include <tpm.h>
Vadim Bendeburye31d2432016-04-09 18:33:49 -070025
26#include "tpm.h"
27
Vadim Bendebury05155c02016-06-23 12:03:18 -070028#define TPM_LOCALITY_0_SPI_BASE 0x00d40000
29
Vadim Bendeburye31d2432016-04-09 18:33:49 -070030/* Assorted TPM2 registers for interface type FIFO. */
Vadim Bendebury05155c02016-06-23 12:03:18 -070031#define TPM_ACCESS_REG (TPM_LOCALITY_0_SPI_BASE + 0)
32#define TPM_STS_REG (TPM_LOCALITY_0_SPI_BASE + 0x18)
33#define TPM_DATA_FIFO_REG (TPM_LOCALITY_0_SPI_BASE + 0x24)
34#define TPM_DID_VID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf00)
35#define TPM_RID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf04)
Vadim Bendebury58826fc2016-06-23 18:17:33 -070036#define TPM_FW_VER (TPM_LOCALITY_0_SPI_BASE + 0xf90)
Vadim Bendeburye31d2432016-04-09 18:33:49 -070037
38/* SPI Interface descriptor used by the driver. */
39struct tpm_spi_if {
Furquan Shaikh36b81af2016-12-01 01:02:44 -080040 struct spi_slave slave;
Furquan Shaikh0dba0252016-11-30 04:34:22 -080041 int (*cs_assert)(const struct spi_slave *slave);
42 void (*cs_deassert)(const struct spi_slave *slave);
43 int (*xfer)(const struct spi_slave *slave, const void *dout,
44 size_t bytesout, void *din,
45 size_t bytesin);
Vadim Bendeburye31d2432016-04-09 18:33:49 -070046};
47
48/* Use the common SPI driver wrapper as the interface callbacks. */
49static struct tpm_spi_if tpm_if = {
50 .cs_assert = spi_claim_bus,
51 .cs_deassert = spi_release_bus,
52 .xfer = spi_xfer
53};
54
55/* Cached TPM device identification. */
Aaron Durbin445c13f2017-03-27 17:19:18 -050056static struct tpm2_info tpm_info;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070057
58/*
59 * TODO(vbendeb): make CONFIG_DEBUG_TPM an int to allow different level of
60 * debug traces. Right now it is either 0 or 1.
61 */
62static const int debug_level_ = CONFIG_DEBUG_TPM;
63
64/* Locality management bits (in TPM_ACCESS_REG) */
65enum tpm_access_bits {
66 tpm_reg_valid_sts = (1 << 7),
67 active_locality = (1 << 5),
68 request_use = (1 << 1),
69 tpm_establishment = (1 << 0),
70};
71
72/*
73 * Variuous fields of the TPM status register, arguably the most important
74 * register when interfacing to a TPM.
75 */
76enum tpm_sts_bits {
77 tpm_family_shift = 26,
78 tpm_family_mask = ((1 << 2) - 1), /* 2 bits wide. */
79 tpm_family_tpm2 = 1,
80 reset_establishment_bit = (1 << 25),
81 command_cancel = (1 << 24),
82 burst_count_shift = 8,
83 burst_count_mask = ((1 << 16) - 1), /* 16 bits wide. */
84 sts_valid = (1 << 7),
85 command_ready = (1 << 6),
86 tpm_go = (1 << 5),
87 data_avail = (1 << 4),
88 expect = (1 << 3),
89 self_test_done = (1 << 2),
90 response_retry = (1 << 1),
91};
92
93/*
94 * SPI frame header for TPM transactions is 4 bytes in size, it is described
95 * in section "6.4.6 Spi Bit Protocol".
96 */
97typedef struct {
98 unsigned char body[4];
99} spi_frame_header;
100
101void tpm2_get_info(struct tpm2_info *info)
102{
103 *info = tpm_info;
104}
105
Jeffy Chen19e3d332017-03-03 18:24:02 +0800106__attribute__((weak)) int tis_plat_irq_status(void)
107{
108 static int warning_displayed;
109
110 if (!warning_displayed) {
111 printk(BIOS_WARNING, "WARNING: tis_plat_irq_status() not implemented, wasting 10ms to wait on Cr50!\n");
112 warning_displayed = 1;
113 }
114 mdelay(10);
115
116 return 1;
117}
118
119/*
120 * TPM may trigger a irq after finish processing previous transfer.
121 * Waiting for this irq to sync tpm status.
122 *
123 * Returns 1 on success, 0 on failure (timeout).
124 */
125static int tpm_sync(void)
126{
127 struct stopwatch sw;
128
129 stopwatch_init_usecs_expire(&sw, 10 * 1000);
130 while (!tis_plat_irq_status()) {
131 if (stopwatch_expired(&sw)) {
132 printk(BIOS_ERR, "Timeout wait for tpm irq!\n");
133 return 0;
134 }
135 }
136 return 1;
137}
138
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700139/*
140 * Each TPM2 SPI transaction starts the same: CS is asserted, the 4 byte
141 * header is sent to the TPM, the master waits til TPM is ready to continue.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800142 *
143 * Returns 1 on success, 0 on failure (TPM SPI flow control timeout.)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700144 */
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800145static int start_transaction(int read_write, size_t bytes, unsigned addr)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700146{
147 spi_frame_header header;
148 uint8_t byte;
149 int i;
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800150 struct stopwatch sw;
Jeffy Chen19e3d332017-03-03 18:24:02 +0800151 static int tpm_sync_needed;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700152
Jeffy Chen19e3d332017-03-03 18:24:02 +0800153 /* Wait for tpm to finish previous transaction if needed */
154 if (tpm_sync_needed)
155 tpm_sync();
156 else
157 tpm_sync_needed = 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700158
Jeffy Chenf9a40ea2017-03-03 18:24:02 +0800159 /* Try to wake cr50 if it is asleep. */
160 tpm_if.cs_assert(&tpm_if.slave);
161 udelay(1);
162 tpm_if.cs_deassert(&tpm_if.slave);
163 udelay(100);
164
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700165 /*
166 * The first byte of the frame header encodes the transaction type
167 * (read or write) and transfer size (set to lentgh - 1), limited to
168 * 64 bytes.
169 */
170 header.body[0] = (read_write ? 0x80 : 0) | 0x40 | (bytes - 1);
171
172 /* The rest of the frame header is the TPM register address. */
173 for (i = 0; i < 3; i++)
174 header.body[i + 1] = (addr >> (8 * (2 - i))) & 0xff;
175
176 /* CS assert wakes up the slave. */
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800177 tpm_if.cs_assert(&tpm_if.slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700178
179 /*
180 * The TCG TPM over SPI specification introduces the notion of SPI
181 * flow control (Section "6.4.5 Flow Control").
182 *
183 * Again, the slave (TPM device) expects each transaction to start
184 * with a 4 byte header trasmitted by master. The header indicates if
185 * the master needs to read or write a register, and the register
186 * address.
187 *
188 * If the slave needs to stall the transaction (for instance it is not
189 * ready to send the register value to the master), it sets the MOSI
190 * line to 0 during the last clock of the 4 byte header. In this case
191 * the master is supposed to start polling the SPI bus, one byte at
192 * time, until the last bit in the received byte (transferred during
193 * the last clock of the byte) is set to 1.
194 *
195 * Due to some SPI controllers' shortcomings (Rockchip comes to
196 * mind...) we trasmit the 4 byte header without checking the byte
197 * transmitted by the TPM during the transaction's last byte.
198 *
199 * We know that cr50 is guaranteed to set the flow control bit to 0
200 * during the header transfer, but real TPM2 might be fast enough not
201 * to require to stall the master, this would present an issue.
202 * crosbug.com/p/52132 has been opened to track this.
203 */
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800204 tpm_if.xfer(&tpm_if.slave, header.body, sizeof(header.body), NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700205
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800206 /*
207 * Now poll the bus until TPM removes the stall bit. Give it up to 100
208 * ms to sort it out - it could be saving stuff in nvram at some
209 * point.
210 */
211 stopwatch_init_msecs_expire(&sw, 100);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700212 do {
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800213 if (stopwatch_expired(&sw)) {
214 printk(BIOS_ERR, "TPM flow control failure\n");
Aaron Durbin5cf1fad2017-03-27 17:25:06 -0500215 tpm_if.cs_deassert(&tpm_if.slave);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800216 return 0;
217 }
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800218 tpm_if.xfer(&tpm_if.slave, NULL, 0, &byte, 1);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700219 } while (!(byte & 1));
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800220 return 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700221}
222
223/*
224 * Print out the contents of a buffer, if debug is enabled. Skip registers
225 * other than FIFO, unless debug_level_ is 2.
226 */
227static void trace_dump(const char *prefix, uint32_t reg,
228 size_t bytes, const uint8_t *buffer,
229 int force)
230{
231 static char prev_prefix;
232 static unsigned prev_reg;
233 static int current_char;
234 const int BYTES_PER_LINE = 32;
235
236 if (!force) {
237 if (!debug_level_)
238 return;
239
240 if ((debug_level_ < 2) && (reg != TPM_DATA_FIFO_REG))
241 return;
242 }
243
244 /*
245 * Do not print register address again if the last dump print was for
246 * that register.
247 */
248 if ((prev_prefix != *prefix) || (prev_reg != reg)) {
249 prev_prefix = *prefix;
250 prev_reg = reg;
251 printk(BIOS_DEBUG, "\n%s %2.2x:", prefix, reg);
252 current_char = 0;
253 }
254
255 if ((reg != TPM_DATA_FIFO_REG) && (bytes == 4)) {
256 /*
257 * This must be a regular register address, print the 32 bit
258 * value.
259 */
260 printk(BIOS_DEBUG, " %8.8x", *(const uint32_t *)buffer);
261 } else {
262 int i;
263
264 /*
265 * Data read from or written to FIFO or not in 4 byte
266 * quantiites is printed byte at a time.
267 */
268 for (i = 0; i < bytes; i++) {
269 if (current_char && !(current_char % BYTES_PER_LINE)) {
270 printk(BIOS_DEBUG, "\n ");
271 current_char = 0;
272 }
273 current_char++;
274 printk(BIOS_DEBUG, " %2.2x", buffer[i]);
275 }
276 }
277}
278
279/*
280 * Once transaction is initiated and the TPM indicated that it is ready to go,
281 * write the actual bytes to the register.
282 */
283static void write_bytes(const void *buffer, size_t bytes)
284{
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800285 tpm_if.xfer(&tpm_if.slave, buffer, bytes, NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700286}
287
288/*
289 * Once transaction is initiated and the TPM indicated that it is ready to go,
290 * read the actual bytes from the register.
291 */
292static void read_bytes(void *buffer, size_t bytes)
293{
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800294 tpm_if.xfer(&tpm_if.slave, NULL, 0, buffer, bytes);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700295}
296
297/*
298 * To write a register, start transaction, transfer data to the TPM, deassert
299 * CS when done.
300 *
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800301 * Returns one to indicate success, zero to indicate failure.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700302 */
303static int tpm2_write_reg(unsigned reg_number, const void *buffer, size_t bytes)
304{
305 trace_dump("W", reg_number, bytes, buffer, 0);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800306 if (!start_transaction(false, bytes, reg_number))
307 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700308 write_bytes(buffer, bytes);
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800309 tpm_if.cs_deassert(&tpm_if.slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700310 return 1;
311}
312
313/*
314 * To read a register, start transaction, transfer data from the TPM, deassert
315 * CS when done.
316 *
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800317 * Returns one to indicate success, zero to indicate failure.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700318 */
319static int tpm2_read_reg(unsigned reg_number, void *buffer, size_t bytes)
320{
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800321 if (!start_transaction(true, bytes, reg_number)) {
322 memset(buffer, 0, bytes);
323 return 0;
324 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700325 read_bytes(buffer, bytes);
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800326 tpm_if.cs_deassert(&tpm_if.slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700327 trace_dump("R", reg_number, bytes, buffer, 0);
328 return 1;
329}
330
331/*
332 * Status register is accessed often, wrap reading and writing it into
333 * dedicated functions.
334 */
335static int read_tpm_sts(uint32_t *status)
336{
337 return tpm2_read_reg(TPM_STS_REG, status, sizeof(*status));
338}
339
340static int write_tpm_sts(uint32_t status)
341{
342 return tpm2_write_reg(TPM_STS_REG, &status, sizeof(status));
343}
344
345/*
346 * The TPM may limit the transaction bytes count (burst count) below the 64
347 * bytes max. The current value is available as a field of the status
348 * register.
349 */
350static uint32_t get_burst_count(void)
351{
352 uint32_t status;
353
354 read_tpm_sts(&status);
355 return (status >> burst_count_shift) & burst_count_mask;
356}
357
358int tpm2_init(struct spi_slave *spi_if)
359{
360 uint32_t did_vid, status;
361 uint8_t cmd;
362
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800363 memcpy(&tpm_if.slave, spi_if, sizeof(*spi_if));
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700364
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800365 /*
366 * It is enough to check the first register read error status to bail
367 * out in case of malfunctioning TPM.
368 */
369 if (!tpm2_read_reg(TPM_DID_VID_REG, &did_vid, sizeof(did_vid)))
370 return -1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700371
372 /* Try claiming locality zero. */
373 tpm2_read_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
374 if ((cmd & (active_locality & tpm_reg_valid_sts)) ==
375 (active_locality & tpm_reg_valid_sts)) {
376 /*
377 * Locality active - maybe reset line is not connected?
378 * Release the locality and try again
379 */
380 cmd = active_locality;
381 tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
382 tpm2_read_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
383 }
384
385 /* The tpm_establishment bit can be either set or not, ignore it. */
386 if ((cmd & ~tpm_establishment) != tpm_reg_valid_sts) {
387 printk(BIOS_ERR, "invalid reset status: %#x\n", cmd);
388 return -1;
389 }
390
391 cmd = request_use;
392 tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
393 tpm2_read_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
394 if ((cmd & ~tpm_establishment) !=
395 (tpm_reg_valid_sts | active_locality)) {
396 printk(BIOS_ERR, "failed to claim locality 0, status: %#x\n",
397 cmd);
398 return -1;
399 }
400
401 read_tpm_sts(&status);
402 if (((status >> tpm_family_shift) & tpm_family_mask) !=
403 tpm_family_tpm2) {
404 printk(BIOS_ERR, "unexpected TPM family value, status: %#x\n",
405 status);
406 return -1;
407 }
408
409 /*
410 * Locality claimed, read the revision value and set up the tpm_info
411 * structure.
412 */
413 tpm2_read_reg(TPM_RID_REG, &cmd, sizeof(cmd));
414 tpm_info.vendor_id = did_vid & 0xffff;
415 tpm_info.device_id = did_vid >> 16;
416 tpm_info.revision = cmd;
417
418 printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n",
419 tpm_info.vendor_id, tpm_info.device_id, tpm_info.revision);
420
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700421 /* Let's report device FW version if available. */
422 if (tpm_info.vendor_id == 0x1ae0) {
423 int chunk_count = 0;
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700424 size_t chunk_size;
425 /*
426 * let's read 50 bytes at a time; leave room for the trailing
427 * zero.
428 */
429 char vstr[51];
430
431 chunk_size = sizeof(vstr) - 1;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700432
433 printk(BIOS_INFO, "Firmware version: ");
434
435 /*
436 * Does not really matter what's written, this just makes sure
437 * the version is reported from the beginning.
438 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700439 tpm2_write_reg(TPM_FW_VER, &chunk_size, 1);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700440
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700441 /* Print it out in sizeof(vstr) - 1 byte chunks. */
442 vstr[chunk_size] = 0;
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700443 do {
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700444 tpm2_read_reg(TPM_FW_VER, vstr, chunk_size);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700445 printk(BIOS_INFO, "%s", vstr);
446
447 /*
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700448 * While string is not over, and is no longer than 300
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700449 * characters.
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700450 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700451 } while (vstr[chunk_size - 1] &&
452 (chunk_count++ < (300 / chunk_size)));
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700453
454 printk(BIOS_INFO, "\n");
455 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700456 return 0;
457}
458
459/*
460 * This is in seconds, certain TPM commands, like key generation, can take
461 * long time to complete.
462 *
463 * Returns one to indicate success, zero (not yet implemented) to indicate
464 * failure.
465 */
466#define MAX_STATUS_TIMEOUT 120
467static int wait_for_status(uint32_t status_mask, uint32_t status_expected)
468{
469 uint32_t status;
470 struct stopwatch sw;
471
472 stopwatch_init_usecs_expire(&sw, MAX_STATUS_TIMEOUT * 1000 * 1000);
473 do {
474 udelay(1000);
475 if (stopwatch_expired(&sw)) {
476 printk(BIOS_ERR, "failed to get expected status %x\n",
477 status_expected);
478 return false;
479 }
480 read_tpm_sts(&status);
481 } while ((status & status_mask) != status_expected);
482
483 return 1;
484}
485
486enum fifo_transfer_direction {
487 fifo_transmit = 0,
488 fifo_receive = 1
489};
490
491/* Union allows to avoid casting away 'const' on transmit buffers. */
492union fifo_transfer_buffer {
493 uint8_t *rx_buffer;
494 const uint8_t *tx_buffer;
495};
496
497/*
498 * Transfer requested number of bytes to or from TPM FIFO, accounting for the
499 * current burst count value.
500 */
501static void fifo_transfer(size_t transfer_size,
502 union fifo_transfer_buffer buffer,
503 enum fifo_transfer_direction direction)
504{
505 size_t transaction_size;
506 size_t burst_count;
507 size_t handled_so_far = 0;
508
509 do {
510 do {
511 /* Could be zero when TPM is busy. */
512 burst_count = get_burst_count();
513 } while (!burst_count);
514
515 transaction_size = transfer_size - handled_so_far;
516 transaction_size = MIN(transaction_size, burst_count);
517
518 /*
519 * The SPI frame header does not allow to pass more than 64
520 * bytes.
521 */
522 transaction_size = MIN(transaction_size, 64);
523
524 if (direction == fifo_receive)
525 tpm2_read_reg(TPM_DATA_FIFO_REG,
526 buffer.rx_buffer + handled_so_far,
527 transaction_size);
528 else
529 tpm2_write_reg(TPM_DATA_FIFO_REG,
530 buffer.tx_buffer + handled_so_far,
531 transaction_size);
532
533 handled_so_far += transaction_size;
534
535 } while (handled_so_far != transfer_size);
536}
537
538size_t tpm2_process_command(const void *tpm2_command, size_t command_size,
539 void *tpm2_response, size_t max_response)
540{
541 uint32_t status;
542 uint32_t expected_status_bits;
543 size_t payload_size;
544 size_t bytes_to_go;
545 const uint8_t *cmd_body = tpm2_command;
546 uint8_t *rsp_body = tpm2_response;
547 union fifo_transfer_buffer fifo_buffer;
548 const int HEADER_SIZE = 6;
549
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800550 /* Do not try using an uninitialized TPM. */
551 if (!tpm_info.vendor_id)
552 return 0;
553
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700554 /* Skip the two byte tag, read the size field. */
555 payload_size = read_be32(cmd_body + 2);
556
557 /* Sanity check. */
558 if (payload_size != command_size) {
559 printk(BIOS_ERR,
560 "Command size mismatch: encoded %zd != requested %zd\n",
561 payload_size, command_size);
562 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
563 printk(BIOS_DEBUG, "\n");
564 return 0;
565 }
566
567 /* Let the TPM know that the command is coming. */
568 write_tpm_sts(command_ready);
569
570 /*
571 * Tpm commands and responses written to and read from the FIFO
572 * register (0x24) are datagrams of variable size, prepended by a 6
573 * byte header.
574 *
575 * The specification description of the state machine is a bit vague,
576 * but from experience it looks like there is no need to wait for the
577 * sts.expect bit to be set, at least with the 9670 and cr50 devices.
578 * Just write the command into FIFO, making sure not to exceed the
579 * burst count or the maximum PDU size, whatever is smaller.
580 */
581 fifo_buffer.tx_buffer = cmd_body;
582 fifo_transfer(command_size, fifo_buffer, fifo_transmit);
583
584 /* Now tell the TPM it can start processing the command. */
585 write_tpm_sts(tpm_go);
586
587 /* Now wait for it to report that the response is ready. */
588 expected_status_bits = sts_valid | data_avail;
589 if (!wait_for_status(expected_status_bits, expected_status_bits)) {
590 /*
591 * If timed out, which should never happen, let's at least
592 * print out the offending command.
593 */
594 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
595 printk(BIOS_DEBUG, "\n");
596 return 0;
597 }
598
599 /*
600 * The response is ready, let's read it. First we read the FIFO
601 * payload header, to see how much data to expect. The response header
602 * size is fixed to six bytes, the total payload size is stored in
603 * network order in the last four bytes.
604 */
605 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE);
606
607 /* Find out the total payload size, skipping the two byte tag. */
608 payload_size = read_be32(rsp_body + 2);
609
610 if (payload_size > max_response) {
611 /*
612 * TODO(vbendeb): at least drain the FIFO here or somehow let
613 * the TPM know that the response can be dropped.
614 */
615 printk(BIOS_ERR, " tpm response too long (%zd bytes)",
616 payload_size);
617 return 0;
618 }
619
620 /*
621 * Now let's read all but the last byte in the FIFO to make sure the
622 * status register is showing correct flow control bits: 'more data'
623 * until the last byte and then 'no more data' once the last byte is
624 * read.
625 */
626 bytes_to_go = payload_size - 1 - HEADER_SIZE;
627 fifo_buffer.rx_buffer = rsp_body + HEADER_SIZE;
628 fifo_transfer(bytes_to_go, fifo_buffer, fifo_receive);
629
630 /* Verify that there is still data to read. */
631 read_tpm_sts(&status);
632 if ((status & expected_status_bits) != expected_status_bits) {
633 printk(BIOS_ERR, "unexpected intermediate status %#x\n",
634 status);
635 return 0;
636 }
637
638 /* Read the last byte of the PDU. */
639 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1);
640
641 /* Terminate the dump, if enabled. */
642 if (debug_level_)
643 printk(BIOS_DEBUG, "\n");
644
645 /* Verify that 'data available' is not asseretd any more. */
646 read_tpm_sts(&status);
647 if ((status & expected_status_bits) != sts_valid) {
648 printk(BIOS_ERR, "unexpected final status %#x\n", status);
649 return 0;
650 }
651
652 /* Move the TPM back to idle state. */
653 write_tpm_sts(command_ready);
654
655 return payload_size;
656}