n2-drv.c 20 KB

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