n2-drv.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* n2-drv.c: Niagara-2 RNG driver.
  3. *
  4. * Copyright (C) 2008, 2011 David S. Miller <davem@davemloft.net>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/types.h>
  9. #include <linux/delay.h>
  10. #include <linux/slab.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/preempt.h>
  13. #include <linux/hw_random.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/property.h>
  17. #include <asm/hypervisor.h>
  18. #include "n2rng.h"
  19. #define DRV_MODULE_NAME "n2rng"
  20. #define PFX DRV_MODULE_NAME ": "
  21. #define DRV_MODULE_VERSION "0.3"
  22. #define DRV_MODULE_RELDATE "Jan 7, 2017"
  23. static char version[] =
  24. DRV_MODULE_NAME " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  25. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  26. MODULE_DESCRIPTION("Niagara2 RNG driver");
  27. MODULE_LICENSE("GPL");
  28. MODULE_VERSION(DRV_MODULE_VERSION);
  29. /* The Niagara2 RNG provides a 64-bit read-only random number
  30. * register, plus a control register. Access to the RNG is
  31. * virtualized through the hypervisor so that both guests and control
  32. * nodes can access the device.
  33. *
  34. * The entropy source consists of raw entropy sources, each
  35. * constructed from a voltage controlled oscillator whose phase is
  36. * jittered by thermal noise sources.
  37. *
  38. * The oscillator in each of the three raw entropy sources run at
  39. * different frequencies. Normally, all three generator outputs are
  40. * gathered, xored together, and fed into a CRC circuit, the output of
  41. * which is the 64-bit read-only register.
  42. *
  43. * Some time is necessary for all the necessary entropy to build up
  44. * such that a full 64-bits of entropy are available in the register.
  45. * In normal operating mode (RNG_CTL_LFSR is set), the chip implements
  46. * an interlock which blocks register reads until sufficient entropy
  47. * is available.
  48. *
  49. * A control register is provided for adjusting various aspects of RNG
  50. * operation, and to enable diagnostic modes. Each of the three raw
  51. * entropy sources has an enable bit (RNG_CTL_ES{1,2,3}). Also
  52. * provided are fields for controlling the minimum time in cycles
  53. * between read accesses to the register (RNG_CTL_WAIT, this controls
  54. * the interlock described in the previous paragraph).
  55. *
  56. * The standard setting is to have the mode bit (RNG_CTL_LFSR) set,
  57. * all three entropy sources enabled, and the interlock time set
  58. * appropriately.
  59. *
  60. * The CRC polynomial used by the chip is:
  61. *
  62. * P(X) = x64 + x61 + x57 + x56 + x52 + x51 + x50 + x48 + x47 + x46 +
  63. * x43 + x42 + x41 + x39 + x38 + x37 + x35 + x32 + x28 + x25 +
  64. * x22 + x21 + x17 + x15 + x13 + x12 + x11 + x7 + x5 + x + 1
  65. *
  66. * The RNG_CTL_VCO value of each noise cell must be programmed
  67. * separately. This is why 4 control register values must be provided
  68. * to the hypervisor. During a write, the hypervisor writes them all,
  69. * one at a time, to the actual RNG_CTL register. The first three
  70. * values are used to setup the desired RNG_CTL_VCO for each entropy
  71. * source, for example:
  72. *
  73. * control 0: (1 << RNG_CTL_VCO_SHIFT) | RNG_CTL_ES1
  74. * control 1: (2 << RNG_CTL_VCO_SHIFT) | RNG_CTL_ES2
  75. * control 2: (3 << RNG_CTL_VCO_SHIFT) | RNG_CTL_ES3
  76. *
  77. * And then the fourth value sets the final chip state and enables
  78. * desired.
  79. */
  80. static int n2rng_hv_err_trans(unsigned long hv_err)
  81. {
  82. switch (hv_err) {
  83. case HV_EOK:
  84. return 0;
  85. case HV_EWOULDBLOCK:
  86. return -EAGAIN;
  87. case HV_ENOACCESS:
  88. return -EPERM;
  89. case HV_EIO:
  90. return -EIO;
  91. case HV_EBUSY:
  92. return -EBUSY;
  93. case HV_EBADALIGN:
  94. case HV_ENORADDR:
  95. return -EFAULT;
  96. default:
  97. return -EINVAL;
  98. }
  99. }
  100. static unsigned long n2rng_generic_read_control_v2(unsigned long ra,
  101. unsigned long unit)
  102. {
  103. unsigned long hv_err, state, ticks, watchdog_delta, watchdog_status;
  104. int block = 0, busy = 0;
  105. while (1) {
  106. hv_err = sun4v_rng_ctl_read_v2(ra, unit, &state,
  107. &ticks,
  108. &watchdog_delta,
  109. &watchdog_status);
  110. if (hv_err == HV_EOK)
  111. break;
  112. if (hv_err == HV_EBUSY) {
  113. if (++busy >= N2RNG_BUSY_LIMIT)
  114. break;
  115. udelay(1);
  116. } else if (hv_err == HV_EWOULDBLOCK) {
  117. if (++block >= N2RNG_BLOCK_LIMIT)
  118. break;
  119. __delay(ticks);
  120. } else
  121. break;
  122. }
  123. return hv_err;
  124. }
  125. /* In multi-socket situations, the hypervisor might need to
  126. * queue up the RNG control register write if it's for a unit
  127. * that is on a cpu socket other than the one we are executing on.
  128. *
  129. * We poll here waiting for a successful read of that control
  130. * register to make sure the write has been actually performed.
  131. */
  132. static unsigned long n2rng_control_settle_v2(struct n2rng *np, int unit)
  133. {
  134. unsigned long ra = __pa(&np->scratch_control[0]);
  135. return n2rng_generic_read_control_v2(ra, unit);
  136. }
  137. static unsigned long n2rng_write_ctl_one(struct n2rng *np, int unit,
  138. unsigned long state,
  139. unsigned long control_ra,
  140. unsigned long watchdog_timeout,
  141. unsigned long *ticks)
  142. {
  143. unsigned long hv_err;
  144. if (np->hvapi_major == 1) {
  145. hv_err = sun4v_rng_ctl_write_v1(control_ra, state,
  146. watchdog_timeout, ticks);
  147. } else {
  148. hv_err = sun4v_rng_ctl_write_v2(control_ra, state,
  149. watchdog_timeout, unit);
  150. if (hv_err == HV_EOK)
  151. hv_err = n2rng_control_settle_v2(np, unit);
  152. *ticks = N2RNG_ACCUM_CYCLES_DEFAULT;
  153. }
  154. return hv_err;
  155. }
  156. static int n2rng_generic_read_data(unsigned long data_ra)
  157. {
  158. unsigned long ticks, hv_err;
  159. int block = 0, hcheck = 0;
  160. while (1) {
  161. hv_err = sun4v_rng_data_read(data_ra, &ticks);
  162. if (hv_err == HV_EOK)
  163. return 0;
  164. if (hv_err == HV_EWOULDBLOCK) {
  165. if (++block >= N2RNG_BLOCK_LIMIT)
  166. return -EWOULDBLOCK;
  167. __delay(ticks);
  168. } else if (hv_err == HV_ENOACCESS) {
  169. return -EPERM;
  170. } else if (hv_err == HV_EIO) {
  171. if (++hcheck >= N2RNG_HCHECK_LIMIT)
  172. return -EIO;
  173. udelay(10000);
  174. } else
  175. return -ENODEV;
  176. }
  177. }
  178. static unsigned long n2rng_read_diag_data_one(struct n2rng *np,
  179. unsigned long unit,
  180. unsigned long data_ra,
  181. unsigned long data_len,
  182. unsigned long *ticks)
  183. {
  184. unsigned long hv_err;
  185. if (np->hvapi_major == 1) {
  186. hv_err = sun4v_rng_data_read_diag_v1(data_ra, data_len, ticks);
  187. } else {
  188. hv_err = sun4v_rng_data_read_diag_v2(data_ra, data_len,
  189. unit, ticks);
  190. if (!*ticks)
  191. *ticks = N2RNG_ACCUM_CYCLES_DEFAULT;
  192. }
  193. return hv_err;
  194. }
  195. static int n2rng_generic_read_diag_data(struct n2rng *np,
  196. unsigned long unit,
  197. unsigned long data_ra,
  198. unsigned long data_len)
  199. {
  200. unsigned long ticks, hv_err;
  201. int block = 0;
  202. while (1) {
  203. hv_err = n2rng_read_diag_data_one(np, unit,
  204. data_ra, data_len,
  205. &ticks);
  206. if (hv_err == HV_EOK)
  207. return 0;
  208. if (hv_err == HV_EWOULDBLOCK) {
  209. if (++block >= N2RNG_BLOCK_LIMIT)
  210. return -EWOULDBLOCK;
  211. __delay(ticks);
  212. } else if (hv_err == HV_ENOACCESS) {
  213. return -EPERM;
  214. } else if (hv_err == HV_EIO) {
  215. return -EIO;
  216. } else
  217. return -ENODEV;
  218. }
  219. }
  220. static int n2rng_generic_write_control(struct n2rng *np,
  221. unsigned long control_ra,
  222. unsigned long unit,
  223. unsigned long state)
  224. {
  225. unsigned long hv_err, ticks;
  226. int block = 0, busy = 0;
  227. while (1) {
  228. hv_err = n2rng_write_ctl_one(np, unit, state, control_ra,
  229. np->wd_timeo, &ticks);
  230. if (hv_err == HV_EOK)
  231. return 0;
  232. if (hv_err == HV_EWOULDBLOCK) {
  233. if (++block >= N2RNG_BLOCK_LIMIT)
  234. return -EWOULDBLOCK;
  235. __delay(ticks);
  236. } else if (hv_err == HV_EBUSY) {
  237. if (++busy >= N2RNG_BUSY_LIMIT)
  238. return -EBUSY;
  239. udelay(1);
  240. } else
  241. return -ENODEV;
  242. }
  243. }
  244. /* Just try to see if we can successfully access the control register
  245. * of the RNG on the domain on which we are currently executing.
  246. */
  247. static int n2rng_try_read_ctl(struct n2rng *np)
  248. {
  249. unsigned long hv_err;
  250. unsigned long x;
  251. if (np->hvapi_major == 1) {
  252. hv_err = sun4v_rng_get_diag_ctl();
  253. } else {
  254. /* We purposefully give invalid arguments, HV_NOACCESS
  255. * is higher priority than the errors we'd get from
  256. * these other cases, and that's the error we are
  257. * truly interested in.
  258. */
  259. hv_err = sun4v_rng_ctl_read_v2(0UL, ~0UL, &x, &x, &x, &x);
  260. switch (hv_err) {
  261. case HV_EWOULDBLOCK:
  262. case HV_ENOACCESS:
  263. break;
  264. default:
  265. hv_err = HV_EOK;
  266. break;
  267. }
  268. }
  269. return n2rng_hv_err_trans(hv_err);
  270. }
  271. static u64 n2rng_control_default(struct n2rng *np, int ctl)
  272. {
  273. u64 val = 0;
  274. if (np->data->chip_version == 1) {
  275. val = ((2 << RNG_v1_CTL_ASEL_SHIFT) |
  276. (N2RNG_ACCUM_CYCLES_DEFAULT << RNG_v1_CTL_WAIT_SHIFT) |
  277. RNG_CTL_LFSR);
  278. switch (ctl) {
  279. case 0:
  280. val |= (1 << RNG_v1_CTL_VCO_SHIFT) | RNG_CTL_ES1;
  281. break;
  282. case 1:
  283. val |= (2 << RNG_v1_CTL_VCO_SHIFT) | RNG_CTL_ES2;
  284. break;
  285. case 2:
  286. val |= (3 << RNG_v1_CTL_VCO_SHIFT) | RNG_CTL_ES3;
  287. break;
  288. case 3:
  289. val |= RNG_CTL_ES1 | RNG_CTL_ES2 | RNG_CTL_ES3;
  290. break;
  291. default:
  292. break;
  293. }
  294. } else {
  295. val = ((2 << RNG_v2_CTL_ASEL_SHIFT) |
  296. (N2RNG_ACCUM_CYCLES_DEFAULT << RNG_v2_CTL_WAIT_SHIFT) |
  297. RNG_CTL_LFSR);
  298. switch (ctl) {
  299. case 0:
  300. val |= (1 << RNG_v2_CTL_VCO_SHIFT) | RNG_CTL_ES1;
  301. break;
  302. case 1:
  303. val |= (2 << RNG_v2_CTL_VCO_SHIFT) | RNG_CTL_ES2;
  304. break;
  305. case 2:
  306. val |= (3 << RNG_v2_CTL_VCO_SHIFT) | RNG_CTL_ES3;
  307. break;
  308. case 3:
  309. val |= RNG_CTL_ES1 | RNG_CTL_ES2 | RNG_CTL_ES3;
  310. break;
  311. default:
  312. break;
  313. }
  314. }
  315. return val;
  316. }
  317. static void n2rng_control_swstate_init(struct n2rng *np)
  318. {
  319. int i;
  320. np->flags |= N2RNG_FLAG_CONTROL;
  321. np->health_check_sec = N2RNG_HEALTH_CHECK_SEC_DEFAULT;
  322. np->accum_cycles = N2RNG_ACCUM_CYCLES_DEFAULT;
  323. np->wd_timeo = N2RNG_WD_TIMEO_DEFAULT;
  324. for (i = 0; i < np->num_units; i++) {
  325. struct n2rng_unit *up = &np->units[i];
  326. up->control[0] = n2rng_control_default(np, 0);
  327. up->control[1] = n2rng_control_default(np, 1);
  328. up->control[2] = n2rng_control_default(np, 2);
  329. up->control[3] = n2rng_control_default(np, 3);
  330. }
  331. np->hv_state = HV_RNG_STATE_UNCONFIGURED;
  332. }
  333. static int n2rng_grab_diag_control(struct n2rng *np)
  334. {
  335. int i, busy_count, err = -ENODEV;
  336. busy_count = 0;
  337. for (i = 0; i < 100; i++) {
  338. err = n2rng_try_read_ctl(np);
  339. if (err != -EAGAIN)
  340. break;
  341. if (++busy_count > 100) {
  342. dev_err(&np->op->dev,
  343. "Grab diag control timeout.\n");
  344. return -ENODEV;
  345. }
  346. udelay(1);
  347. }
  348. return err;
  349. }
  350. static int n2rng_init_control(struct n2rng *np)
  351. {
  352. int err = n2rng_grab_diag_control(np);
  353. /* Not in the control domain, that's OK we are only a consumer
  354. * of the RNG data, we don't setup and program it.
  355. */
  356. if (err == -EPERM)
  357. return 0;
  358. if (err)
  359. return err;
  360. n2rng_control_swstate_init(np);
  361. return 0;
  362. }
  363. static int n2rng_data_read(struct hwrng *rng, u32 *data)
  364. {
  365. struct n2rng *np = (struct n2rng *) rng->priv;
  366. unsigned long ra = __pa(&np->test_data);
  367. int len;
  368. if (!(np->flags & N2RNG_FLAG_READY)) {
  369. len = 0;
  370. } else if (np->flags & N2RNG_FLAG_BUFFER_VALID) {
  371. np->flags &= ~N2RNG_FLAG_BUFFER_VALID;
  372. *data = np->buffer;
  373. len = 4;
  374. } else {
  375. int err = n2rng_generic_read_data(ra);
  376. if (!err) {
  377. np->flags |= N2RNG_FLAG_BUFFER_VALID;
  378. np->buffer = np->test_data >> 32;
  379. *data = np->test_data & 0xffffffff;
  380. len = 4;
  381. } else {
  382. dev_err(&np->op->dev, "RNG error, retesting\n");
  383. np->flags &= ~N2RNG_FLAG_READY;
  384. if (!(np->flags & N2RNG_FLAG_SHUTDOWN))
  385. schedule_delayed_work(&np->work, 0);
  386. len = 0;
  387. }
  388. }
  389. return len;
  390. }
  391. /* On a guest node, just make sure we can read random data properly.
  392. * If a control node reboots or reloads it's n2rng driver, this won't
  393. * work during that time. So we have to keep probing until the device
  394. * becomes usable.
  395. */
  396. static int n2rng_guest_check(struct n2rng *np)
  397. {
  398. unsigned long ra = __pa(&np->test_data);
  399. return n2rng_generic_read_data(ra);
  400. }
  401. static int n2rng_entropy_diag_read(struct n2rng *np, unsigned long unit,
  402. u64 *pre_control, u64 pre_state,
  403. u64 *buffer, unsigned long buf_len,
  404. u64 *post_control, u64 post_state)
  405. {
  406. unsigned long post_ctl_ra = __pa(post_control);
  407. unsigned long pre_ctl_ra = __pa(pre_control);
  408. unsigned long buffer_ra = __pa(buffer);
  409. int err;
  410. err = n2rng_generic_write_control(np, pre_ctl_ra, unit, pre_state);
  411. if (err)
  412. return err;
  413. err = n2rng_generic_read_diag_data(np, unit,
  414. buffer_ra, buf_len);
  415. (void) n2rng_generic_write_control(np, post_ctl_ra, unit,
  416. post_state);
  417. return err;
  418. }
  419. static u64 advance_polynomial(u64 poly, u64 val, int count)
  420. {
  421. int i;
  422. for (i = 0; i < count; i++) {
  423. int highbit_set = ((s64)val < 0);
  424. val <<= 1;
  425. if (highbit_set)
  426. val ^= poly;
  427. }
  428. return val;
  429. }
  430. static int n2rng_test_buffer_find(struct n2rng *np, u64 val)
  431. {
  432. int i, count = 0;
  433. /* Purposefully skip over the first word. */
  434. for (i = 1; i < SELFTEST_BUFFER_WORDS; i++) {
  435. if (np->test_buffer[i] == val)
  436. count++;
  437. }
  438. return count;
  439. }
  440. static void n2rng_dump_test_buffer(struct n2rng *np)
  441. {
  442. int i;
  443. for (i = 0; i < SELFTEST_BUFFER_WORDS; i++)
  444. dev_err(&np->op->dev, "Test buffer slot %d [0x%016llx]\n",
  445. i, np->test_buffer[i]);
  446. }
  447. static int n2rng_check_selftest_buffer(struct n2rng *np, unsigned long unit)
  448. {
  449. u64 val;
  450. int err, matches, limit;
  451. switch (np->data->id) {
  452. case N2_n2_rng:
  453. case N2_vf_rng:
  454. case N2_kt_rng:
  455. case N2_m4_rng: /* yes, m4 uses the old value */
  456. val = RNG_v1_SELFTEST_VAL;
  457. break;
  458. default:
  459. val = RNG_v2_SELFTEST_VAL;
  460. break;
  461. }
  462. matches = 0;
  463. for (limit = 0; limit < SELFTEST_LOOPS_MAX; limit++) {
  464. matches += n2rng_test_buffer_find(np, val);
  465. if (matches >= SELFTEST_MATCH_GOAL)
  466. break;
  467. val = advance_polynomial(SELFTEST_POLY, val, 1);
  468. }
  469. err = 0;
  470. if (limit >= SELFTEST_LOOPS_MAX) {
  471. err = -ENODEV;
  472. dev_err(&np->op->dev, "Selftest failed on unit %lu\n", unit);
  473. n2rng_dump_test_buffer(np);
  474. } else
  475. dev_info(&np->op->dev, "Selftest passed on unit %lu\n", unit);
  476. return err;
  477. }
  478. static int n2rng_control_selftest(struct n2rng *np, unsigned long unit)
  479. {
  480. int err;
  481. u64 base, base3;
  482. switch (np->data->id) {
  483. case N2_n2_rng:
  484. case N2_vf_rng:
  485. case N2_kt_rng:
  486. base = RNG_v1_CTL_ASEL_NOOUT << RNG_v1_CTL_ASEL_SHIFT;
  487. base3 = base | RNG_CTL_LFSR |
  488. ((RNG_v1_SELFTEST_TICKS - 2) << RNG_v1_CTL_WAIT_SHIFT);
  489. break;
  490. case N2_m4_rng:
  491. base = RNG_v2_CTL_ASEL_NOOUT << RNG_v2_CTL_ASEL_SHIFT;
  492. base3 = base | RNG_CTL_LFSR |
  493. ((RNG_v1_SELFTEST_TICKS - 2) << RNG_v2_CTL_WAIT_SHIFT);
  494. break;
  495. default:
  496. base = RNG_v2_CTL_ASEL_NOOUT << RNG_v2_CTL_ASEL_SHIFT;
  497. base3 = base | RNG_CTL_LFSR |
  498. (RNG_v2_SELFTEST_TICKS << RNG_v2_CTL_WAIT_SHIFT);
  499. break;
  500. }
  501. np->test_control[0] = base;
  502. np->test_control[1] = base;
  503. np->test_control[2] = base;
  504. np->test_control[3] = base3;
  505. err = n2rng_entropy_diag_read(np, unit, np->test_control,
  506. HV_RNG_STATE_HEALTHCHECK,
  507. np->test_buffer,
  508. sizeof(np->test_buffer),
  509. &np->units[unit].control[0],
  510. np->hv_state);
  511. if (err)
  512. return err;
  513. return n2rng_check_selftest_buffer(np, unit);
  514. }
  515. static int n2rng_control_check(struct n2rng *np)
  516. {
  517. int i;
  518. for (i = 0; i < np->num_units; i++) {
  519. int err = n2rng_control_selftest(np, i);
  520. if (err)
  521. return err;
  522. }
  523. return 0;
  524. }
  525. /* The sanity checks passed, install the final configuration into the
  526. * chip, it's ready to use.
  527. */
  528. static int n2rng_control_configure_units(struct n2rng *np)
  529. {
  530. int unit, err;
  531. err = 0;
  532. for (unit = 0; unit < np->num_units; unit++) {
  533. struct n2rng_unit *up = &np->units[unit];
  534. unsigned long ctl_ra = __pa(&up->control[0]);
  535. int esrc;
  536. u64 base, shift;
  537. if (np->data->chip_version == 1) {
  538. base = ((np->accum_cycles << RNG_v1_CTL_WAIT_SHIFT) |
  539. (RNG_v1_CTL_ASEL_NOOUT << RNG_v1_CTL_ASEL_SHIFT) |
  540. RNG_CTL_LFSR);
  541. shift = RNG_v1_CTL_VCO_SHIFT;
  542. } else {
  543. base = ((np->accum_cycles << RNG_v2_CTL_WAIT_SHIFT) |
  544. (RNG_v2_CTL_ASEL_NOOUT << RNG_v2_CTL_ASEL_SHIFT) |
  545. RNG_CTL_LFSR);
  546. shift = RNG_v2_CTL_VCO_SHIFT;
  547. }
  548. /* XXX This isn't the best. We should fetch a bunch
  549. * XXX of words using each entropy source combined XXX
  550. * with each VCO setting, and see which combinations
  551. * XXX give the best random data.
  552. */
  553. for (esrc = 0; esrc < 3; esrc++)
  554. up->control[esrc] = base |
  555. (esrc << shift) |
  556. (RNG_CTL_ES1 << esrc);
  557. up->control[3] = base |
  558. (RNG_CTL_ES1 | RNG_CTL_ES2 | RNG_CTL_ES3);
  559. err = n2rng_generic_write_control(np, ctl_ra, unit,
  560. HV_RNG_STATE_CONFIGURED);
  561. if (err)
  562. break;
  563. }
  564. return err;
  565. }
  566. static void n2rng_work(struct work_struct *work)
  567. {
  568. struct n2rng *np = container_of(work, struct n2rng, work.work);
  569. int err = 0;
  570. static int retries = 4;
  571. if (!(np->flags & N2RNG_FLAG_CONTROL)) {
  572. err = n2rng_guest_check(np);
  573. } else {
  574. preempt_disable();
  575. err = n2rng_control_check(np);
  576. preempt_enable();
  577. if (!err)
  578. err = n2rng_control_configure_units(np);
  579. }
  580. if (!err) {
  581. np->flags |= N2RNG_FLAG_READY;
  582. dev_info(&np->op->dev, "RNG ready\n");
  583. }
  584. if (--retries == 0)
  585. dev_err(&np->op->dev, "Self-test retries failed, RNG not ready\n");
  586. else if (err && !(np->flags & N2RNG_FLAG_SHUTDOWN))
  587. schedule_delayed_work(&np->work, HZ * 2);
  588. }
  589. static void n2rng_driver_version(void)
  590. {
  591. static int n2rng_version_printed;
  592. if (n2rng_version_printed++ == 0)
  593. pr_info("%s", version);
  594. }
  595. static const struct of_device_id n2rng_match[];
  596. static int n2rng_probe(struct platform_device *op)
  597. {
  598. int err = -ENOMEM;
  599. struct n2rng *np;
  600. n2rng_driver_version();
  601. np = devm_kzalloc(&op->dev, sizeof(*np), GFP_KERNEL);
  602. if (!np)
  603. goto out;
  604. np->op = op;
  605. np->data = (struct n2rng_template *)device_get_match_data(&op->dev);
  606. INIT_DELAYED_WORK(&np->work, n2rng_work);
  607. if (np->data->multi_capable)
  608. np->flags |= N2RNG_FLAG_MULTI;
  609. err = -ENODEV;
  610. np->hvapi_major = 2;
  611. if (sun4v_hvapi_register(HV_GRP_RNG,
  612. np->hvapi_major,
  613. &np->hvapi_minor)) {
  614. np->hvapi_major = 1;
  615. if (sun4v_hvapi_register(HV_GRP_RNG,
  616. np->hvapi_major,
  617. &np->hvapi_minor)) {
  618. dev_err(&op->dev, "Cannot register suitable "
  619. "HVAPI version.\n");
  620. goto out;
  621. }
  622. }
  623. if (np->flags & N2RNG_FLAG_MULTI) {
  624. if (np->hvapi_major < 2) {
  625. dev_err(&op->dev, "multi-unit-capable RNG requires "
  626. "HVAPI major version 2 or later, got %lu\n",
  627. np->hvapi_major);
  628. goto out_hvapi_unregister;
  629. }
  630. np->num_units = of_getintprop_default(op->dev.of_node,
  631. "rng-#units", 0);
  632. if (!np->num_units) {
  633. dev_err(&op->dev, "VF RNG lacks rng-#units property\n");
  634. goto out_hvapi_unregister;
  635. }
  636. } else {
  637. np->num_units = 1;
  638. }
  639. dev_info(&op->dev, "Registered RNG HVAPI major %lu minor %lu\n",
  640. np->hvapi_major, np->hvapi_minor);
  641. np->units = devm_kcalloc(&op->dev, np->num_units, sizeof(*np->units),
  642. GFP_KERNEL);
  643. err = -ENOMEM;
  644. if (!np->units)
  645. goto out_hvapi_unregister;
  646. err = n2rng_init_control(np);
  647. if (err)
  648. goto out_hvapi_unregister;
  649. dev_info(&op->dev, "Found %s RNG, units: %d\n",
  650. ((np->flags & N2RNG_FLAG_MULTI) ?
  651. "multi-unit-capable" : "single-unit"),
  652. np->num_units);
  653. np->hwrng.name = DRV_MODULE_NAME;
  654. np->hwrng.data_read = n2rng_data_read;
  655. np->hwrng.priv = (unsigned long) np;
  656. err = devm_hwrng_register(&op->dev, &np->hwrng);
  657. if (err)
  658. goto out_hvapi_unregister;
  659. platform_set_drvdata(op, np);
  660. schedule_delayed_work(&np->work, 0);
  661. return 0;
  662. out_hvapi_unregister:
  663. sun4v_hvapi_unregister(HV_GRP_RNG);
  664. out:
  665. return err;
  666. }
  667. static void n2rng_remove(struct platform_device *op)
  668. {
  669. struct n2rng *np = platform_get_drvdata(op);
  670. np->flags |= N2RNG_FLAG_SHUTDOWN;
  671. cancel_delayed_work_sync(&np->work);
  672. sun4v_hvapi_unregister(HV_GRP_RNG);
  673. }
  674. static struct n2rng_template n2_template = {
  675. .id = N2_n2_rng,
  676. .multi_capable = 0,
  677. .chip_version = 1,
  678. };
  679. static struct n2rng_template vf_template = {
  680. .id = N2_vf_rng,
  681. .multi_capable = 1,
  682. .chip_version = 1,
  683. };
  684. static struct n2rng_template kt_template = {
  685. .id = N2_kt_rng,
  686. .multi_capable = 1,
  687. .chip_version = 1,
  688. };
  689. static struct n2rng_template m4_template = {
  690. .id = N2_m4_rng,
  691. .multi_capable = 1,
  692. .chip_version = 2,
  693. };
  694. static struct n2rng_template m7_template = {
  695. .id = N2_m7_rng,
  696. .multi_capable = 1,
  697. .chip_version = 2,
  698. };
  699. static const struct of_device_id n2rng_match[] = {
  700. {
  701. .name = "random-number-generator",
  702. .compatible = "SUNW,n2-rng",
  703. .data = &n2_template,
  704. },
  705. {
  706. .name = "random-number-generator",
  707. .compatible = "SUNW,vf-rng",
  708. .data = &vf_template,
  709. },
  710. {
  711. .name = "random-number-generator",
  712. .compatible = "SUNW,kt-rng",
  713. .data = &kt_template,
  714. },
  715. {
  716. .name = "random-number-generator",
  717. .compatible = "ORCL,m4-rng",
  718. .data = &m4_template,
  719. },
  720. {
  721. .name = "random-number-generator",
  722. .compatible = "ORCL,m7-rng",
  723. .data = &m7_template,
  724. },
  725. {},
  726. };
  727. MODULE_DEVICE_TABLE(of, n2rng_match);
  728. static struct platform_driver n2rng_driver = {
  729. .driver = {
  730. .name = "n2rng",
  731. .of_match_table = n2rng_match,
  732. },
  733. .probe = n2rng_probe,
  734. .remove = n2rng_remove,
  735. };
  736. module_platform_driver(n2rng_driver);