tpm-interface.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. *
  4. * Authors:
  5. * Leendert van Doorn <leendert@watson.ibm.com>
  6. * Dave Safford <safford@watson.ibm.com>
  7. * Reiner Sailer <sailer@watson.ibm.com>
  8. * Kylene Hall <kjhall@us.ibm.com>
  9. *
  10. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  11. *
  12. * Device driver for TCG/TCPA TPM (trusted platform module).
  13. * Specifications at www.trustedcomputinggroup.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation, version 2 of the
  18. * License.
  19. *
  20. * Note, the TPM chip is not interrupt driven (only polling)
  21. * and can have very long timeouts (minutes!). Hence the unusual
  22. * calls to msleep.
  23. *
  24. */
  25. #include <linux/poll.h>
  26. #include <linux/slab.h>
  27. #include <linux/mutex.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/freezer.h>
  30. #include "tpm.h"
  31. #include "tpm_eventlog.h"
  32. #define TPM_MAX_ORDINAL 243
  33. #define TSC_MAX_ORDINAL 12
  34. #define TPM_PROTECTED_COMMAND 0x00
  35. #define TPM_CONNECTION_COMMAND 0x40
  36. /*
  37. * Bug workaround - some TPM's don't flush the most
  38. * recently changed pcr on suspend, so force the flush
  39. * with an extend to the selected _unused_ non-volatile pcr.
  40. */
  41. static int tpm_suspend_pcr;
  42. module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
  43. MODULE_PARM_DESC(suspend_pcr,
  44. "PCR to use for dummy writes to faciltate flush on suspend.");
  45. static LIST_HEAD(tpm_chip_list);
  46. static DEFINE_SPINLOCK(driver_lock);
  47. static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
  48. /*
  49. * Array with one entry per ordinal defining the maximum amount
  50. * of time the chip could take to return the result. The ordinal
  51. * designation of short, medium or long is defined in a table in
  52. * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
  53. * values of the SHORT, MEDIUM, and LONG durations are retrieved
  54. * from the chip during initialization with a call to tpm_get_timeouts.
  55. */
  56. static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
  57. TPM_UNDEFINED, /* 0 */
  58. TPM_UNDEFINED,
  59. TPM_UNDEFINED,
  60. TPM_UNDEFINED,
  61. TPM_UNDEFINED,
  62. TPM_UNDEFINED, /* 5 */
  63. TPM_UNDEFINED,
  64. TPM_UNDEFINED,
  65. TPM_UNDEFINED,
  66. TPM_UNDEFINED,
  67. TPM_SHORT, /* 10 */
  68. TPM_SHORT,
  69. TPM_MEDIUM,
  70. TPM_LONG,
  71. TPM_LONG,
  72. TPM_MEDIUM, /* 15 */
  73. TPM_SHORT,
  74. TPM_SHORT,
  75. TPM_MEDIUM,
  76. TPM_LONG,
  77. TPM_SHORT, /* 20 */
  78. TPM_SHORT,
  79. TPM_MEDIUM,
  80. TPM_MEDIUM,
  81. TPM_MEDIUM,
  82. TPM_SHORT, /* 25 */
  83. TPM_SHORT,
  84. TPM_MEDIUM,
  85. TPM_SHORT,
  86. TPM_SHORT,
  87. TPM_MEDIUM, /* 30 */
  88. TPM_LONG,
  89. TPM_MEDIUM,
  90. TPM_SHORT,
  91. TPM_SHORT,
  92. TPM_SHORT, /* 35 */
  93. TPM_MEDIUM,
  94. TPM_MEDIUM,
  95. TPM_UNDEFINED,
  96. TPM_UNDEFINED,
  97. TPM_MEDIUM, /* 40 */
  98. TPM_LONG,
  99. TPM_MEDIUM,
  100. TPM_SHORT,
  101. TPM_SHORT,
  102. TPM_SHORT, /* 45 */
  103. TPM_SHORT,
  104. TPM_SHORT,
  105. TPM_SHORT,
  106. TPM_LONG,
  107. TPM_MEDIUM, /* 50 */
  108. TPM_MEDIUM,
  109. TPM_UNDEFINED,
  110. TPM_UNDEFINED,
  111. TPM_UNDEFINED,
  112. TPM_UNDEFINED, /* 55 */
  113. TPM_UNDEFINED,
  114. TPM_UNDEFINED,
  115. TPM_UNDEFINED,
  116. TPM_UNDEFINED,
  117. TPM_MEDIUM, /* 60 */
  118. TPM_MEDIUM,
  119. TPM_MEDIUM,
  120. TPM_SHORT,
  121. TPM_SHORT,
  122. TPM_MEDIUM, /* 65 */
  123. TPM_UNDEFINED,
  124. TPM_UNDEFINED,
  125. TPM_UNDEFINED,
  126. TPM_UNDEFINED,
  127. TPM_SHORT, /* 70 */
  128. TPM_SHORT,
  129. TPM_UNDEFINED,
  130. TPM_UNDEFINED,
  131. TPM_UNDEFINED,
  132. TPM_UNDEFINED, /* 75 */
  133. TPM_UNDEFINED,
  134. TPM_UNDEFINED,
  135. TPM_UNDEFINED,
  136. TPM_UNDEFINED,
  137. TPM_LONG, /* 80 */
  138. TPM_UNDEFINED,
  139. TPM_MEDIUM,
  140. TPM_LONG,
  141. TPM_SHORT,
  142. TPM_UNDEFINED, /* 85 */
  143. TPM_UNDEFINED,
  144. TPM_UNDEFINED,
  145. TPM_UNDEFINED,
  146. TPM_UNDEFINED,
  147. TPM_SHORT, /* 90 */
  148. TPM_SHORT,
  149. TPM_SHORT,
  150. TPM_SHORT,
  151. TPM_SHORT,
  152. TPM_UNDEFINED, /* 95 */
  153. TPM_UNDEFINED,
  154. TPM_UNDEFINED,
  155. TPM_UNDEFINED,
  156. TPM_UNDEFINED,
  157. TPM_MEDIUM, /* 100 */
  158. TPM_SHORT,
  159. TPM_SHORT,
  160. TPM_UNDEFINED,
  161. TPM_UNDEFINED,
  162. TPM_UNDEFINED, /* 105 */
  163. TPM_UNDEFINED,
  164. TPM_UNDEFINED,
  165. TPM_UNDEFINED,
  166. TPM_UNDEFINED,
  167. TPM_SHORT, /* 110 */
  168. TPM_SHORT,
  169. TPM_SHORT,
  170. TPM_SHORT,
  171. TPM_SHORT,
  172. TPM_SHORT, /* 115 */
  173. TPM_SHORT,
  174. TPM_SHORT,
  175. TPM_UNDEFINED,
  176. TPM_UNDEFINED,
  177. TPM_LONG, /* 120 */
  178. TPM_LONG,
  179. TPM_MEDIUM,
  180. TPM_UNDEFINED,
  181. TPM_SHORT,
  182. TPM_SHORT, /* 125 */
  183. TPM_SHORT,
  184. TPM_LONG,
  185. TPM_SHORT,
  186. TPM_SHORT,
  187. TPM_SHORT, /* 130 */
  188. TPM_MEDIUM,
  189. TPM_UNDEFINED,
  190. TPM_SHORT,
  191. TPM_MEDIUM,
  192. TPM_UNDEFINED, /* 135 */
  193. TPM_UNDEFINED,
  194. TPM_UNDEFINED,
  195. TPM_UNDEFINED,
  196. TPM_UNDEFINED,
  197. TPM_SHORT, /* 140 */
  198. TPM_SHORT,
  199. TPM_UNDEFINED,
  200. TPM_UNDEFINED,
  201. TPM_UNDEFINED,
  202. TPM_UNDEFINED, /* 145 */
  203. TPM_UNDEFINED,
  204. TPM_UNDEFINED,
  205. TPM_UNDEFINED,
  206. TPM_UNDEFINED,
  207. TPM_SHORT, /* 150 */
  208. TPM_MEDIUM,
  209. TPM_MEDIUM,
  210. TPM_SHORT,
  211. TPM_SHORT,
  212. TPM_UNDEFINED, /* 155 */
  213. TPM_UNDEFINED,
  214. TPM_UNDEFINED,
  215. TPM_UNDEFINED,
  216. TPM_UNDEFINED,
  217. TPM_SHORT, /* 160 */
  218. TPM_SHORT,
  219. TPM_SHORT,
  220. TPM_SHORT,
  221. TPM_UNDEFINED,
  222. TPM_UNDEFINED, /* 165 */
  223. TPM_UNDEFINED,
  224. TPM_UNDEFINED,
  225. TPM_UNDEFINED,
  226. TPM_UNDEFINED,
  227. TPM_LONG, /* 170 */
  228. TPM_UNDEFINED,
  229. TPM_UNDEFINED,
  230. TPM_UNDEFINED,
  231. TPM_UNDEFINED,
  232. TPM_UNDEFINED, /* 175 */
  233. TPM_UNDEFINED,
  234. TPM_UNDEFINED,
  235. TPM_UNDEFINED,
  236. TPM_UNDEFINED,
  237. TPM_MEDIUM, /* 180 */
  238. TPM_SHORT,
  239. TPM_MEDIUM,
  240. TPM_MEDIUM,
  241. TPM_MEDIUM,
  242. TPM_MEDIUM, /* 185 */
  243. TPM_SHORT,
  244. TPM_UNDEFINED,
  245. TPM_UNDEFINED,
  246. TPM_UNDEFINED,
  247. TPM_UNDEFINED, /* 190 */
  248. TPM_UNDEFINED,
  249. TPM_UNDEFINED,
  250. TPM_UNDEFINED,
  251. TPM_UNDEFINED,
  252. TPM_UNDEFINED, /* 195 */
  253. TPM_UNDEFINED,
  254. TPM_UNDEFINED,
  255. TPM_UNDEFINED,
  256. TPM_UNDEFINED,
  257. TPM_SHORT, /* 200 */
  258. TPM_UNDEFINED,
  259. TPM_UNDEFINED,
  260. TPM_UNDEFINED,
  261. TPM_SHORT,
  262. TPM_SHORT, /* 205 */
  263. TPM_SHORT,
  264. TPM_SHORT,
  265. TPM_SHORT,
  266. TPM_SHORT,
  267. TPM_MEDIUM, /* 210 */
  268. TPM_UNDEFINED,
  269. TPM_MEDIUM,
  270. TPM_MEDIUM,
  271. TPM_MEDIUM,
  272. TPM_UNDEFINED, /* 215 */
  273. TPM_MEDIUM,
  274. TPM_UNDEFINED,
  275. TPM_UNDEFINED,
  276. TPM_SHORT,
  277. TPM_SHORT, /* 220 */
  278. TPM_SHORT,
  279. TPM_SHORT,
  280. TPM_SHORT,
  281. TPM_SHORT,
  282. TPM_UNDEFINED, /* 225 */
  283. TPM_UNDEFINED,
  284. TPM_UNDEFINED,
  285. TPM_UNDEFINED,
  286. TPM_UNDEFINED,
  287. TPM_SHORT, /* 230 */
  288. TPM_LONG,
  289. TPM_MEDIUM,
  290. TPM_UNDEFINED,
  291. TPM_UNDEFINED,
  292. TPM_UNDEFINED, /* 235 */
  293. TPM_UNDEFINED,
  294. TPM_UNDEFINED,
  295. TPM_UNDEFINED,
  296. TPM_UNDEFINED,
  297. TPM_SHORT, /* 240 */
  298. TPM_UNDEFINED,
  299. TPM_MEDIUM,
  300. };
  301. /*
  302. * Returns max number of jiffies to wait
  303. */
  304. unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
  305. u32 ordinal)
  306. {
  307. int duration_idx = TPM_UNDEFINED;
  308. int duration = 0;
  309. u8 category = (ordinal >> 24) & 0xFF;
  310. if ((category == TPM_PROTECTED_COMMAND && ordinal < TPM_MAX_ORDINAL) ||
  311. (category == TPM_CONNECTION_COMMAND && ordinal < TSC_MAX_ORDINAL))
  312. duration_idx = tpm_ordinal_duration[ordinal];
  313. if (duration_idx != TPM_UNDEFINED)
  314. duration = chip->vendor.duration[duration_idx];
  315. if (duration <= 0)
  316. return 2 * 60 * HZ;
  317. else
  318. return duration;
  319. }
  320. EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
  321. /*
  322. * Internal kernel interface to transmit TPM commands
  323. */
  324. ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
  325. size_t bufsiz)
  326. {
  327. ssize_t rc;
  328. u32 count, ordinal;
  329. unsigned long stop;
  330. if (bufsiz > TPM_BUFSIZE)
  331. bufsiz = TPM_BUFSIZE;
  332. count = be32_to_cpu(*((__be32 *) (buf + 2)));
  333. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  334. if (count == 0)
  335. return -ENODATA;
  336. if (count > bufsiz) {
  337. dev_err(chip->dev,
  338. "invalid count value %x %zx\n", count, bufsiz);
  339. return -E2BIG;
  340. }
  341. mutex_lock(&chip->tpm_mutex);
  342. rc = chip->ops->send(chip, (u8 *) buf, count);
  343. if (rc < 0) {
  344. dev_err(chip->dev,
  345. "tpm_transmit: tpm_send: error %zd\n", rc);
  346. goto out;
  347. }
  348. if (chip->vendor.irq)
  349. goto out_recv;
  350. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  351. do {
  352. u8 status = chip->ops->status(chip);
  353. if ((status & chip->ops->req_complete_mask) ==
  354. chip->ops->req_complete_val)
  355. goto out_recv;
  356. if (chip->ops->req_canceled(chip, status)) {
  357. dev_err(chip->dev, "Operation Canceled\n");
  358. rc = -ECANCELED;
  359. goto out;
  360. }
  361. msleep(TPM_TIMEOUT); /* CHECK */
  362. rmb();
  363. } while (time_before(jiffies, stop));
  364. chip->ops->cancel(chip);
  365. dev_err(chip->dev, "Operation Timed out\n");
  366. rc = -ETIME;
  367. goto out;
  368. out_recv:
  369. rc = chip->ops->recv(chip, (u8 *) buf, bufsiz);
  370. if (rc < 0)
  371. dev_err(chip->dev,
  372. "tpm_transmit: tpm_recv: error %zd\n", rc);
  373. out:
  374. mutex_unlock(&chip->tpm_mutex);
  375. return rc;
  376. }
  377. #define TPM_DIGEST_SIZE 20
  378. #define TPM_RET_CODE_IDX 6
  379. static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
  380. int len, const char *desc)
  381. {
  382. int err;
  383. len = tpm_transmit(chip, (u8 *) cmd, len);
  384. if (len < 0)
  385. return len;
  386. else if (len < TPM_HEADER_SIZE)
  387. return -EFAULT;
  388. err = be32_to_cpu(cmd->header.out.return_code);
  389. if (err != 0 && desc)
  390. dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
  391. return err;
  392. }
  393. #define TPM_INTERNAL_RESULT_SIZE 200
  394. #define TPM_ORD_GET_CAP cpu_to_be32(101)
  395. #define TPM_ORD_GET_RANDOM cpu_to_be32(70)
  396. static const struct tpm_input_header tpm_getcap_header = {
  397. .tag = TPM_TAG_RQU_COMMAND,
  398. .length = cpu_to_be32(22),
  399. .ordinal = TPM_ORD_GET_CAP
  400. };
  401. ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
  402. const char *desc)
  403. {
  404. struct tpm_cmd_t tpm_cmd;
  405. int rc;
  406. struct tpm_chip *chip = dev_get_drvdata(dev);
  407. tpm_cmd.header.in = tpm_getcap_header;
  408. if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
  409. tpm_cmd.params.getcap_in.cap = subcap_id;
  410. /*subcap field not necessary */
  411. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
  412. tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
  413. } else {
  414. if (subcap_id == TPM_CAP_FLAG_PERM ||
  415. subcap_id == TPM_CAP_FLAG_VOL)
  416. tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
  417. else
  418. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  419. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  420. tpm_cmd.params.getcap_in.subcap = subcap_id;
  421. }
  422. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
  423. if (!rc)
  424. *cap = tpm_cmd.params.getcap_out.cap;
  425. return rc;
  426. }
  427. void tpm_gen_interrupt(struct tpm_chip *chip)
  428. {
  429. struct tpm_cmd_t tpm_cmd;
  430. ssize_t rc;
  431. tpm_cmd.header.in = tpm_getcap_header;
  432. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  433. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  434. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  435. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  436. "attempting to determine the timeouts");
  437. }
  438. EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
  439. #define TPM_ORD_STARTUP cpu_to_be32(153)
  440. #define TPM_ST_CLEAR cpu_to_be16(1)
  441. #define TPM_ST_STATE cpu_to_be16(2)
  442. #define TPM_ST_DEACTIVATED cpu_to_be16(3)
  443. static const struct tpm_input_header tpm_startup_header = {
  444. .tag = TPM_TAG_RQU_COMMAND,
  445. .length = cpu_to_be32(12),
  446. .ordinal = TPM_ORD_STARTUP
  447. };
  448. static int tpm_startup(struct tpm_chip *chip, __be16 startup_type)
  449. {
  450. struct tpm_cmd_t start_cmd;
  451. start_cmd.header.in = tpm_startup_header;
  452. start_cmd.params.startup_in.startup_type = startup_type;
  453. return transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE,
  454. "attempting to start the TPM");
  455. }
  456. int tpm_get_timeouts(struct tpm_chip *chip)
  457. {
  458. struct tpm_cmd_t tpm_cmd;
  459. struct timeout_t *timeout_cap;
  460. struct duration_t *duration_cap;
  461. ssize_t rc;
  462. u32 timeout;
  463. unsigned int scale = 1;
  464. tpm_cmd.header.in = tpm_getcap_header;
  465. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  466. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  467. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  468. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, NULL);
  469. if (rc == TPM_ERR_INVALID_POSTINIT) {
  470. /* The TPM is not started, we are the first to talk to it.
  471. Execute a startup command. */
  472. dev_info(chip->dev, "Issuing TPM_STARTUP");
  473. if (tpm_startup(chip, TPM_ST_CLEAR))
  474. return rc;
  475. tpm_cmd.header.in = tpm_getcap_header;
  476. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  477. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  478. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  479. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  480. NULL);
  481. }
  482. if (rc) {
  483. dev_err(chip->dev,
  484. "A TPM error (%zd) occurred attempting to determine the timeouts\n",
  485. rc);
  486. goto duration;
  487. }
  488. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  489. be32_to_cpu(tpm_cmd.header.out.length)
  490. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
  491. return -EINVAL;
  492. timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
  493. /* Don't overwrite default if value is 0 */
  494. timeout = be32_to_cpu(timeout_cap->a);
  495. if (timeout && timeout < 1000) {
  496. /* timeouts in msec rather usec */
  497. scale = 1000;
  498. chip->vendor.timeout_adjusted = true;
  499. }
  500. if (timeout)
  501. chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
  502. timeout = be32_to_cpu(timeout_cap->b);
  503. if (timeout)
  504. chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
  505. timeout = be32_to_cpu(timeout_cap->c);
  506. if (timeout)
  507. chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
  508. timeout = be32_to_cpu(timeout_cap->d);
  509. if (timeout)
  510. chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
  511. duration:
  512. tpm_cmd.header.in = tpm_getcap_header;
  513. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  514. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  515. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
  516. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  517. "attempting to determine the durations");
  518. if (rc)
  519. return rc;
  520. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  521. be32_to_cpu(tpm_cmd.header.out.length)
  522. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
  523. return -EINVAL;
  524. duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
  525. chip->vendor.duration[TPM_SHORT] =
  526. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
  527. chip->vendor.duration[TPM_MEDIUM] =
  528. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
  529. chip->vendor.duration[TPM_LONG] =
  530. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
  531. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  532. * value wrong and apparently reports msecs rather than usecs. So we
  533. * fix up the resulting too-small TPM_SHORT value to make things work.
  534. * We also scale the TPM_MEDIUM and -_LONG values by 1000.
  535. */
  536. if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
  537. chip->vendor.duration[TPM_SHORT] = HZ;
  538. chip->vendor.duration[TPM_MEDIUM] *= 1000;
  539. chip->vendor.duration[TPM_LONG] *= 1000;
  540. chip->vendor.duration_adjusted = true;
  541. dev_info(chip->dev, "Adjusting TPM timeout parameters.");
  542. }
  543. return 0;
  544. }
  545. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  546. #define TPM_ORD_CONTINUE_SELFTEST 83
  547. #define CONTINUE_SELFTEST_RESULT_SIZE 10
  548. static struct tpm_input_header continue_selftest_header = {
  549. .tag = TPM_TAG_RQU_COMMAND,
  550. .length = cpu_to_be32(10),
  551. .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
  552. };
  553. /**
  554. * tpm_continue_selftest -- run TPM's selftest
  555. * @chip: TPM chip to use
  556. *
  557. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  558. * a TPM error code.
  559. */
  560. static int tpm_continue_selftest(struct tpm_chip *chip)
  561. {
  562. int rc;
  563. struct tpm_cmd_t cmd;
  564. cmd.header.in = continue_selftest_header;
  565. rc = transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
  566. "continue selftest");
  567. return rc;
  568. }
  569. /*
  570. * tpm_chip_find_get - return tpm_chip for given chip number
  571. */
  572. static struct tpm_chip *tpm_chip_find_get(int chip_num)
  573. {
  574. struct tpm_chip *pos, *chip = NULL;
  575. rcu_read_lock();
  576. list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
  577. if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
  578. continue;
  579. if (try_module_get(pos->dev->driver->owner)) {
  580. chip = pos;
  581. break;
  582. }
  583. }
  584. rcu_read_unlock();
  585. return chip;
  586. }
  587. #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
  588. #define READ_PCR_RESULT_SIZE 30
  589. static struct tpm_input_header pcrread_header = {
  590. .tag = TPM_TAG_RQU_COMMAND,
  591. .length = cpu_to_be32(14),
  592. .ordinal = TPM_ORDINAL_PCRREAD
  593. };
  594. int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  595. {
  596. int rc;
  597. struct tpm_cmd_t cmd;
  598. cmd.header.in = pcrread_header;
  599. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
  600. rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
  601. "attempting to read a pcr value");
  602. if (rc == 0)
  603. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  604. TPM_DIGEST_SIZE);
  605. return rc;
  606. }
  607. /**
  608. * tpm_pcr_read - read a pcr value
  609. * @chip_num: tpm idx # or ANY
  610. * @pcr_idx: pcr idx to retrieve
  611. * @res_buf: TPM_PCR value
  612. * size of res_buf is 20 bytes (or NULL if you don't care)
  613. *
  614. * The TPM driver should be built-in, but for whatever reason it
  615. * isn't, protect against the chip disappearing, by incrementing
  616. * the module usage count.
  617. */
  618. int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
  619. {
  620. struct tpm_chip *chip;
  621. int rc;
  622. chip = tpm_chip_find_get(chip_num);
  623. if (chip == NULL)
  624. return -ENODEV;
  625. rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
  626. tpm_chip_put(chip);
  627. return rc;
  628. }
  629. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  630. /**
  631. * tpm_pcr_extend - extend pcr value with hash
  632. * @chip_num: tpm idx # or AN&
  633. * @pcr_idx: pcr idx to extend
  634. * @hash: hash value used to extend pcr value
  635. *
  636. * The TPM driver should be built-in, but for whatever reason it
  637. * isn't, protect against the chip disappearing, by incrementing
  638. * the module usage count.
  639. */
  640. #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
  641. #define EXTEND_PCR_RESULT_SIZE 34
  642. static struct tpm_input_header pcrextend_header = {
  643. .tag = TPM_TAG_RQU_COMMAND,
  644. .length = cpu_to_be32(34),
  645. .ordinal = TPM_ORD_PCR_EXTEND
  646. };
  647. int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
  648. {
  649. struct tpm_cmd_t cmd;
  650. int rc;
  651. struct tpm_chip *chip;
  652. chip = tpm_chip_find_get(chip_num);
  653. if (chip == NULL)
  654. return -ENODEV;
  655. cmd.header.in = pcrextend_header;
  656. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  657. memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
  658. rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  659. "attempting extend a PCR value");
  660. tpm_chip_put(chip);
  661. return rc;
  662. }
  663. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  664. /**
  665. * tpm_do_selftest - have the TPM continue its selftest and wait until it
  666. * can receive further commands
  667. * @chip: TPM chip to use
  668. *
  669. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  670. * a TPM error code.
  671. */
  672. int tpm_do_selftest(struct tpm_chip *chip)
  673. {
  674. int rc;
  675. unsigned int loops;
  676. unsigned int delay_msec = 100;
  677. unsigned long duration;
  678. struct tpm_cmd_t cmd;
  679. duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
  680. loops = jiffies_to_msecs(duration) / delay_msec;
  681. rc = tpm_continue_selftest(chip);
  682. /* This may fail if there was no TPM driver during a suspend/resume
  683. * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
  684. */
  685. if (rc)
  686. return rc;
  687. do {
  688. /* Attempt to read a PCR value */
  689. cmd.header.in = pcrread_header;
  690. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0);
  691. rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE);
  692. /* Some buggy TPMs will not respond to tpm_tis_ready() for
  693. * around 300ms while the self test is ongoing, keep trying
  694. * until the self test duration expires. */
  695. if (rc == -ETIME) {
  696. dev_info(chip->dev, HW_ERR "TPM command timed out during continue self test");
  697. msleep(delay_msec);
  698. continue;
  699. }
  700. if (rc < TPM_HEADER_SIZE)
  701. return -EFAULT;
  702. rc = be32_to_cpu(cmd.header.out.return_code);
  703. if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
  704. dev_info(chip->dev,
  705. "TPM is disabled/deactivated (0x%X)\n", rc);
  706. /* TPM is disabled and/or deactivated; driver can
  707. * proceed and TPM does handle commands for
  708. * suspend/resume correctly
  709. */
  710. return 0;
  711. }
  712. if (rc != TPM_WARN_DOING_SELFTEST)
  713. return rc;
  714. msleep(delay_msec);
  715. } while (--loops > 0);
  716. return rc;
  717. }
  718. EXPORT_SYMBOL_GPL(tpm_do_selftest);
  719. int tpm_send(u32 chip_num, void *cmd, size_t buflen)
  720. {
  721. struct tpm_chip *chip;
  722. int rc;
  723. chip = tpm_chip_find_get(chip_num);
  724. if (chip == NULL)
  725. return -ENODEV;
  726. rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
  727. tpm_chip_put(chip);
  728. return rc;
  729. }
  730. EXPORT_SYMBOL_GPL(tpm_send);
  731. static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
  732. bool check_cancel, bool *canceled)
  733. {
  734. u8 status = chip->ops->status(chip);
  735. *canceled = false;
  736. if ((status & mask) == mask)
  737. return true;
  738. if (check_cancel && chip->ops->req_canceled(chip, status)) {
  739. *canceled = true;
  740. return true;
  741. }
  742. return false;
  743. }
  744. int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  745. wait_queue_head_t *queue, bool check_cancel)
  746. {
  747. unsigned long stop;
  748. long rc;
  749. u8 status;
  750. bool canceled = false;
  751. /* check current status */
  752. status = chip->ops->status(chip);
  753. if ((status & mask) == mask)
  754. return 0;
  755. stop = jiffies + timeout;
  756. if (chip->vendor.irq) {
  757. again:
  758. timeout = stop - jiffies;
  759. if ((long)timeout <= 0)
  760. return -ETIME;
  761. rc = wait_event_interruptible_timeout(*queue,
  762. wait_for_tpm_stat_cond(chip, mask, check_cancel,
  763. &canceled),
  764. timeout);
  765. if (rc > 0) {
  766. if (canceled)
  767. return -ECANCELED;
  768. return 0;
  769. }
  770. if (rc == -ERESTARTSYS && freezing(current)) {
  771. clear_thread_flag(TIF_SIGPENDING);
  772. goto again;
  773. }
  774. } else {
  775. do {
  776. msleep(TPM_TIMEOUT);
  777. status = chip->ops->status(chip);
  778. if ((status & mask) == mask)
  779. return 0;
  780. } while (time_before(jiffies, stop));
  781. }
  782. return -ETIME;
  783. }
  784. EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
  785. void tpm_remove_hardware(struct device *dev)
  786. {
  787. struct tpm_chip *chip = dev_get_drvdata(dev);
  788. if (chip == NULL) {
  789. dev_err(dev, "No device data found\n");
  790. return;
  791. }
  792. spin_lock(&driver_lock);
  793. list_del_rcu(&chip->list);
  794. spin_unlock(&driver_lock);
  795. synchronize_rcu();
  796. tpm_dev_del_device(chip);
  797. tpm_sysfs_del_device(chip);
  798. tpm_remove_ppi(&dev->kobj);
  799. tpm_bios_log_teardown(chip->bios_dir);
  800. /* write it this way to be explicit (chip->dev == dev) */
  801. put_device(chip->dev);
  802. }
  803. EXPORT_SYMBOL_GPL(tpm_remove_hardware);
  804. #define TPM_ORD_SAVESTATE cpu_to_be32(152)
  805. #define SAVESTATE_RESULT_SIZE 10
  806. static struct tpm_input_header savestate_header = {
  807. .tag = TPM_TAG_RQU_COMMAND,
  808. .length = cpu_to_be32(10),
  809. .ordinal = TPM_ORD_SAVESTATE
  810. };
  811. /*
  812. * We are about to suspend. Save the TPM state
  813. * so that it can be restored.
  814. */
  815. int tpm_pm_suspend(struct device *dev)
  816. {
  817. struct tpm_chip *chip = dev_get_drvdata(dev);
  818. struct tpm_cmd_t cmd;
  819. int rc, try;
  820. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  821. if (chip == NULL)
  822. return -ENODEV;
  823. /* for buggy tpm, flush pcrs with extend to selected dummy */
  824. if (tpm_suspend_pcr) {
  825. cmd.header.in = pcrextend_header;
  826. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
  827. memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
  828. TPM_DIGEST_SIZE);
  829. rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  830. "extending dummy pcr before suspend");
  831. }
  832. /* now do the actual savestate */
  833. for (try = 0; try < TPM_RETRY; try++) {
  834. cmd.header.in = savestate_header;
  835. rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, NULL);
  836. /*
  837. * If the TPM indicates that it is too busy to respond to
  838. * this command then retry before giving up. It can take
  839. * several seconds for this TPM to be ready.
  840. *
  841. * This can happen if the TPM has already been sent the
  842. * SaveState command before the driver has loaded. TCG 1.2
  843. * specification states that any communication after SaveState
  844. * may cause the TPM to invalidate previously saved state.
  845. */
  846. if (rc != TPM_WARN_RETRY)
  847. break;
  848. msleep(TPM_TIMEOUT_RETRY);
  849. }
  850. if (rc)
  851. dev_err(chip->dev,
  852. "Error (%d) sending savestate before suspend\n", rc);
  853. else if (try > 0)
  854. dev_warn(chip->dev, "TPM savestate took %dms\n",
  855. try * TPM_TIMEOUT_RETRY);
  856. return rc;
  857. }
  858. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  859. /*
  860. * Resume from a power safe. The BIOS already restored
  861. * the TPM state.
  862. */
  863. int tpm_pm_resume(struct device *dev)
  864. {
  865. struct tpm_chip *chip = dev_get_drvdata(dev);
  866. if (chip == NULL)
  867. return -ENODEV;
  868. return 0;
  869. }
  870. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  871. #define TPM_GETRANDOM_RESULT_SIZE 18
  872. static struct tpm_input_header tpm_getrandom_header = {
  873. .tag = TPM_TAG_RQU_COMMAND,
  874. .length = cpu_to_be32(14),
  875. .ordinal = TPM_ORD_GET_RANDOM
  876. };
  877. /**
  878. * tpm_get_random() - Get random bytes from the tpm's RNG
  879. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  880. * @out: destination buffer for the random bytes
  881. * @max: the max number of bytes to write to @out
  882. *
  883. * Returns < 0 on error and the number of bytes read on success
  884. */
  885. int tpm_get_random(u32 chip_num, u8 *out, size_t max)
  886. {
  887. struct tpm_chip *chip;
  888. struct tpm_cmd_t tpm_cmd;
  889. u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
  890. int err, total = 0, retries = 5;
  891. u8 *dest = out;
  892. chip = tpm_chip_find_get(chip_num);
  893. if (chip == NULL)
  894. return -ENODEV;
  895. if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
  896. return -EINVAL;
  897. do {
  898. tpm_cmd.header.in = tpm_getrandom_header;
  899. tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
  900. err = transmit_cmd(chip, &tpm_cmd,
  901. TPM_GETRANDOM_RESULT_SIZE + num_bytes,
  902. "attempting get random");
  903. if (err)
  904. break;
  905. recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
  906. memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
  907. dest += recd;
  908. total += recd;
  909. num_bytes -= recd;
  910. } while (retries-- && total < max);
  911. return total ? total : -EIO;
  912. }
  913. EXPORT_SYMBOL_GPL(tpm_get_random);
  914. /* In case vendor provided release function, call it too.*/
  915. void tpm_dev_vendor_release(struct tpm_chip *chip)
  916. {
  917. if (!chip)
  918. return;
  919. clear_bit(chip->dev_num, dev_mask);
  920. }
  921. EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
  922. /*
  923. * Once all references to platform device are down to 0,
  924. * release all allocated structures.
  925. */
  926. static void tpm_dev_release(struct device *dev)
  927. {
  928. struct tpm_chip *chip = dev_get_drvdata(dev);
  929. if (!chip)
  930. return;
  931. tpm_dev_vendor_release(chip);
  932. chip->release(dev);
  933. kfree(chip);
  934. }
  935. /*
  936. * Called from tpm_<specific>.c probe function only for devices
  937. * the driver has determined it should claim. Prior to calling
  938. * this function the specific probe function has called pci_enable_device
  939. * upon errant exit from this function specific probe function should call
  940. * pci_disable_device
  941. */
  942. struct tpm_chip *tpm_register_hardware(struct device *dev,
  943. const struct tpm_class_ops *ops)
  944. {
  945. struct tpm_chip *chip;
  946. /* Driver specific per-device data */
  947. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  948. if (chip == NULL)
  949. return NULL;
  950. mutex_init(&chip->tpm_mutex);
  951. INIT_LIST_HEAD(&chip->list);
  952. chip->ops = ops;
  953. chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
  954. if (chip->dev_num >= TPM_NUM_DEVICES) {
  955. dev_err(dev, "No available tpm device numbers\n");
  956. goto out_free;
  957. }
  958. set_bit(chip->dev_num, dev_mask);
  959. scnprintf(chip->devname, sizeof(chip->devname), "%s%d", "tpm",
  960. chip->dev_num);
  961. chip->dev = get_device(dev);
  962. chip->release = dev->release;
  963. dev->release = tpm_dev_release;
  964. dev_set_drvdata(dev, chip);
  965. if (tpm_dev_add_device(chip))
  966. goto put_device;
  967. if (tpm_sysfs_add_device(chip))
  968. goto del_misc;
  969. if (tpm_add_ppi(&dev->kobj))
  970. goto del_misc;
  971. chip->bios_dir = tpm_bios_log_setup(chip->devname);
  972. /* Make chip available */
  973. spin_lock(&driver_lock);
  974. list_add_rcu(&chip->list, &tpm_chip_list);
  975. spin_unlock(&driver_lock);
  976. return chip;
  977. del_misc:
  978. tpm_dev_del_device(chip);
  979. put_device:
  980. put_device(chip->dev);
  981. out_free:
  982. kfree(chip);
  983. return NULL;
  984. }
  985. EXPORT_SYMBOL_GPL(tpm_register_hardware);
  986. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  987. MODULE_DESCRIPTION("TPM Driver");
  988. MODULE_VERSION("2.0");
  989. MODULE_LICENSE("GPL");