n2-drv.c 20 KB

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