n2-drv.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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.2"
  20. #define DRV_MODULE_RELDATE "July 27, 2011"
  21. static char version[] =
  22. DRV_MODULE_NAME ".c: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. #define CONTROL_DEFAULT_BASE \
  270. ((2 << RNG_CTL_ASEL_SHIFT) | \
  271. (N2RNG_ACCUM_CYCLES_DEFAULT << RNG_CTL_WAIT_SHIFT) | \
  272. RNG_CTL_LFSR)
  273. #define CONTROL_DEFAULT_0 \
  274. (CONTROL_DEFAULT_BASE | \
  275. (1 << RNG_CTL_VCO_SHIFT) | \
  276. RNG_CTL_ES1)
  277. #define CONTROL_DEFAULT_1 \
  278. (CONTROL_DEFAULT_BASE | \
  279. (2 << RNG_CTL_VCO_SHIFT) | \
  280. RNG_CTL_ES2)
  281. #define CONTROL_DEFAULT_2 \
  282. (CONTROL_DEFAULT_BASE | \
  283. (3 << RNG_CTL_VCO_SHIFT) | \
  284. RNG_CTL_ES3)
  285. #define CONTROL_DEFAULT_3 \
  286. (CONTROL_DEFAULT_BASE | \
  287. RNG_CTL_ES1 | RNG_CTL_ES2 | RNG_CTL_ES3)
  288. static void n2rng_control_swstate_init(struct n2rng *np)
  289. {
  290. int i;
  291. np->flags |= N2RNG_FLAG_CONTROL;
  292. np->health_check_sec = N2RNG_HEALTH_CHECK_SEC_DEFAULT;
  293. np->accum_cycles = N2RNG_ACCUM_CYCLES_DEFAULT;
  294. np->wd_timeo = N2RNG_WD_TIMEO_DEFAULT;
  295. for (i = 0; i < np->num_units; i++) {
  296. struct n2rng_unit *up = &np->units[i];
  297. up->control[0] = CONTROL_DEFAULT_0;
  298. up->control[1] = CONTROL_DEFAULT_1;
  299. up->control[2] = CONTROL_DEFAULT_2;
  300. up->control[3] = CONTROL_DEFAULT_3;
  301. }
  302. np->hv_state = HV_RNG_STATE_UNCONFIGURED;
  303. }
  304. static int n2rng_grab_diag_control(struct n2rng *np)
  305. {
  306. int i, busy_count, err = -ENODEV;
  307. busy_count = 0;
  308. for (i = 0; i < 100; i++) {
  309. err = n2rng_try_read_ctl(np);
  310. if (err != -EAGAIN)
  311. break;
  312. if (++busy_count > 100) {
  313. dev_err(&np->op->dev,
  314. "Grab diag control timeout.\n");
  315. return -ENODEV;
  316. }
  317. udelay(1);
  318. }
  319. return err;
  320. }
  321. static int n2rng_init_control(struct n2rng *np)
  322. {
  323. int err = n2rng_grab_diag_control(np);
  324. /* Not in the control domain, that's OK we are only a consumer
  325. * of the RNG data, we don't setup and program it.
  326. */
  327. if (err == -EPERM)
  328. return 0;
  329. if (err)
  330. return err;
  331. n2rng_control_swstate_init(np);
  332. return 0;
  333. }
  334. static int n2rng_data_read(struct hwrng *rng, u32 *data)
  335. {
  336. struct n2rng *np = (struct n2rng *) rng->priv;
  337. unsigned long ra = __pa(&np->test_data);
  338. int len;
  339. if (!(np->flags & N2RNG_FLAG_READY)) {
  340. len = 0;
  341. } else if (np->flags & N2RNG_FLAG_BUFFER_VALID) {
  342. np->flags &= ~N2RNG_FLAG_BUFFER_VALID;
  343. *data = np->buffer;
  344. len = 4;
  345. } else {
  346. int err = n2rng_generic_read_data(ra);
  347. if (!err) {
  348. np->buffer = np->test_data >> 32;
  349. *data = np->test_data & 0xffffffff;
  350. len = 4;
  351. } else {
  352. dev_err(&np->op->dev, "RNG error, restesting\n");
  353. np->flags &= ~N2RNG_FLAG_READY;
  354. if (!(np->flags & N2RNG_FLAG_SHUTDOWN))
  355. schedule_delayed_work(&np->work, 0);
  356. len = 0;
  357. }
  358. }
  359. return len;
  360. }
  361. /* On a guest node, just make sure we can read random data properly.
  362. * If a control node reboots or reloads it's n2rng driver, this won't
  363. * work during that time. So we have to keep probing until the device
  364. * becomes usable.
  365. */
  366. static int n2rng_guest_check(struct n2rng *np)
  367. {
  368. unsigned long ra = __pa(&np->test_data);
  369. return n2rng_generic_read_data(ra);
  370. }
  371. static int n2rng_entropy_diag_read(struct n2rng *np, unsigned long unit,
  372. u64 *pre_control, u64 pre_state,
  373. u64 *buffer, unsigned long buf_len,
  374. u64 *post_control, u64 post_state)
  375. {
  376. unsigned long post_ctl_ra = __pa(post_control);
  377. unsigned long pre_ctl_ra = __pa(pre_control);
  378. unsigned long buffer_ra = __pa(buffer);
  379. int err;
  380. err = n2rng_generic_write_control(np, pre_ctl_ra, unit, pre_state);
  381. if (err)
  382. return err;
  383. err = n2rng_generic_read_diag_data(np, unit,
  384. buffer_ra, buf_len);
  385. (void) n2rng_generic_write_control(np, post_ctl_ra, unit,
  386. post_state);
  387. return err;
  388. }
  389. static u64 advance_polynomial(u64 poly, u64 val, int count)
  390. {
  391. int i;
  392. for (i = 0; i < count; i++) {
  393. int highbit_set = ((s64)val < 0);
  394. val <<= 1;
  395. if (highbit_set)
  396. val ^= poly;
  397. }
  398. return val;
  399. }
  400. static int n2rng_test_buffer_find(struct n2rng *np, u64 val)
  401. {
  402. int i, count = 0;
  403. /* Purposefully skip over the first word. */
  404. for (i = 1; i < SELFTEST_BUFFER_WORDS; i++) {
  405. if (np->test_buffer[i] == val)
  406. count++;
  407. }
  408. return count;
  409. }
  410. static void n2rng_dump_test_buffer(struct n2rng *np)
  411. {
  412. int i;
  413. for (i = 0; i < SELFTEST_BUFFER_WORDS; i++)
  414. dev_err(&np->op->dev, "Test buffer slot %d [0x%016llx]\n",
  415. i, np->test_buffer[i]);
  416. }
  417. static int n2rng_check_selftest_buffer(struct n2rng *np, unsigned long unit)
  418. {
  419. u64 val = SELFTEST_VAL;
  420. int err, matches, limit;
  421. matches = 0;
  422. for (limit = 0; limit < SELFTEST_LOOPS_MAX; limit++) {
  423. matches += n2rng_test_buffer_find(np, val);
  424. if (matches >= SELFTEST_MATCH_GOAL)
  425. break;
  426. val = advance_polynomial(SELFTEST_POLY, val, 1);
  427. }
  428. err = 0;
  429. if (limit >= SELFTEST_LOOPS_MAX) {
  430. err = -ENODEV;
  431. dev_err(&np->op->dev, "Selftest failed on unit %lu\n", unit);
  432. n2rng_dump_test_buffer(np);
  433. } else
  434. dev_info(&np->op->dev, "Selftest passed on unit %lu\n", unit);
  435. return err;
  436. }
  437. static int n2rng_control_selftest(struct n2rng *np, unsigned long unit)
  438. {
  439. int err;
  440. np->test_control[0] = (0x2 << RNG_CTL_ASEL_SHIFT);
  441. np->test_control[1] = (0x2 << RNG_CTL_ASEL_SHIFT);
  442. np->test_control[2] = (0x2 << RNG_CTL_ASEL_SHIFT);
  443. np->test_control[3] = ((0x2 << RNG_CTL_ASEL_SHIFT) |
  444. RNG_CTL_LFSR |
  445. ((SELFTEST_TICKS - 2) << RNG_CTL_WAIT_SHIFT));
  446. err = n2rng_entropy_diag_read(np, unit, np->test_control,
  447. HV_RNG_STATE_HEALTHCHECK,
  448. np->test_buffer,
  449. sizeof(np->test_buffer),
  450. &np->units[unit].control[0],
  451. np->hv_state);
  452. if (err)
  453. return err;
  454. return n2rng_check_selftest_buffer(np, unit);
  455. }
  456. static int n2rng_control_check(struct n2rng *np)
  457. {
  458. int i;
  459. for (i = 0; i < np->num_units; i++) {
  460. int err = n2rng_control_selftest(np, i);
  461. if (err)
  462. return err;
  463. }
  464. return 0;
  465. }
  466. /* The sanity checks passed, install the final configuration into the
  467. * chip, it's ready to use.
  468. */
  469. static int n2rng_control_configure_units(struct n2rng *np)
  470. {
  471. int unit, err;
  472. err = 0;
  473. for (unit = 0; unit < np->num_units; unit++) {
  474. struct n2rng_unit *up = &np->units[unit];
  475. unsigned long ctl_ra = __pa(&up->control[0]);
  476. int esrc;
  477. u64 base;
  478. base = ((np->accum_cycles << RNG_CTL_WAIT_SHIFT) |
  479. (2 << RNG_CTL_ASEL_SHIFT) |
  480. RNG_CTL_LFSR);
  481. /* XXX This isn't the best. We should fetch a bunch
  482. * XXX of words using each entropy source combined XXX
  483. * with each VCO setting, and see which combinations
  484. * XXX give the best random data.
  485. */
  486. for (esrc = 0; esrc < 3; esrc++)
  487. up->control[esrc] = base |
  488. (esrc << RNG_CTL_VCO_SHIFT) |
  489. (RNG_CTL_ES1 << esrc);
  490. up->control[3] = base |
  491. (RNG_CTL_ES1 | RNG_CTL_ES2 | RNG_CTL_ES3);
  492. err = n2rng_generic_write_control(np, ctl_ra, unit,
  493. HV_RNG_STATE_CONFIGURED);
  494. if (err)
  495. break;
  496. }
  497. return err;
  498. }
  499. static void n2rng_work(struct work_struct *work)
  500. {
  501. struct n2rng *np = container_of(work, struct n2rng, work.work);
  502. int err = 0;
  503. if (!(np->flags & N2RNG_FLAG_CONTROL)) {
  504. err = n2rng_guest_check(np);
  505. } else {
  506. preempt_disable();
  507. err = n2rng_control_check(np);
  508. preempt_enable();
  509. if (!err)
  510. err = n2rng_control_configure_units(np);
  511. }
  512. if (!err) {
  513. np->flags |= N2RNG_FLAG_READY;
  514. dev_info(&np->op->dev, "RNG ready\n");
  515. }
  516. if (err && !(np->flags & N2RNG_FLAG_SHUTDOWN))
  517. schedule_delayed_work(&np->work, HZ * 2);
  518. }
  519. static void n2rng_driver_version(void)
  520. {
  521. static int n2rng_version_printed;
  522. if (n2rng_version_printed++ == 0)
  523. pr_info("%s", version);
  524. }
  525. static const struct of_device_id n2rng_match[];
  526. static int n2rng_probe(struct platform_device *op)
  527. {
  528. const struct of_device_id *match;
  529. int multi_capable;
  530. int err = -ENOMEM;
  531. struct n2rng *np;
  532. match = of_match_device(n2rng_match, &op->dev);
  533. if (!match)
  534. return -EINVAL;
  535. multi_capable = (match->data != NULL);
  536. n2rng_driver_version();
  537. np = devm_kzalloc(&op->dev, sizeof(*np), GFP_KERNEL);
  538. if (!np)
  539. goto out;
  540. np->op = op;
  541. INIT_DELAYED_WORK(&np->work, n2rng_work);
  542. if (multi_capable)
  543. np->flags |= N2RNG_FLAG_MULTI;
  544. err = -ENODEV;
  545. np->hvapi_major = 2;
  546. if (sun4v_hvapi_register(HV_GRP_RNG,
  547. np->hvapi_major,
  548. &np->hvapi_minor)) {
  549. np->hvapi_major = 1;
  550. if (sun4v_hvapi_register(HV_GRP_RNG,
  551. np->hvapi_major,
  552. &np->hvapi_minor)) {
  553. dev_err(&op->dev, "Cannot register suitable "
  554. "HVAPI version.\n");
  555. goto out;
  556. }
  557. }
  558. if (np->flags & N2RNG_FLAG_MULTI) {
  559. if (np->hvapi_major < 2) {
  560. dev_err(&op->dev, "multi-unit-capable RNG requires "
  561. "HVAPI major version 2 or later, got %lu\n",
  562. np->hvapi_major);
  563. goto out_hvapi_unregister;
  564. }
  565. np->num_units = of_getintprop_default(op->dev.of_node,
  566. "rng-#units", 0);
  567. if (!np->num_units) {
  568. dev_err(&op->dev, "VF RNG lacks rng-#units property\n");
  569. goto out_hvapi_unregister;
  570. }
  571. } else
  572. np->num_units = 1;
  573. dev_info(&op->dev, "Registered RNG HVAPI major %lu minor %lu\n",
  574. np->hvapi_major, np->hvapi_minor);
  575. np->units = devm_kzalloc(&op->dev,
  576. sizeof(struct n2rng_unit) * np->num_units,
  577. GFP_KERNEL);
  578. err = -ENOMEM;
  579. if (!np->units)
  580. goto out_hvapi_unregister;
  581. err = n2rng_init_control(np);
  582. if (err)
  583. goto out_hvapi_unregister;
  584. dev_info(&op->dev, "Found %s RNG, units: %d\n",
  585. ((np->flags & N2RNG_FLAG_MULTI) ?
  586. "multi-unit-capable" : "single-unit"),
  587. np->num_units);
  588. np->hwrng.name = "n2rng";
  589. np->hwrng.data_read = n2rng_data_read;
  590. np->hwrng.priv = (unsigned long) np;
  591. err = hwrng_register(&np->hwrng);
  592. if (err)
  593. goto out_hvapi_unregister;
  594. platform_set_drvdata(op, np);
  595. schedule_delayed_work(&np->work, 0);
  596. return 0;
  597. out_hvapi_unregister:
  598. sun4v_hvapi_unregister(HV_GRP_RNG);
  599. out:
  600. return err;
  601. }
  602. static int n2rng_remove(struct platform_device *op)
  603. {
  604. struct n2rng *np = platform_get_drvdata(op);
  605. np->flags |= N2RNG_FLAG_SHUTDOWN;
  606. cancel_delayed_work_sync(&np->work);
  607. hwrng_unregister(&np->hwrng);
  608. sun4v_hvapi_unregister(HV_GRP_RNG);
  609. return 0;
  610. }
  611. static const struct of_device_id n2rng_match[] = {
  612. {
  613. .name = "random-number-generator",
  614. .compatible = "SUNW,n2-rng",
  615. },
  616. {
  617. .name = "random-number-generator",
  618. .compatible = "SUNW,vf-rng",
  619. .data = (void *) 1,
  620. },
  621. {
  622. .name = "random-number-generator",
  623. .compatible = "SUNW,kt-rng",
  624. .data = (void *) 1,
  625. },
  626. {},
  627. };
  628. MODULE_DEVICE_TABLE(of, n2rng_match);
  629. static struct platform_driver n2rng_driver = {
  630. .driver = {
  631. .name = "n2rng",
  632. .of_match_table = n2rng_match,
  633. },
  634. .probe = n2rng_probe,
  635. .remove = n2rng_remove,
  636. };
  637. module_platform_driver(n2rng_driver);