n2-drv.c 18 KB

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