driver-bitfury.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * Copyright 2013 bitfury
  3. * Copyright 2013 Anatoly Legkodymov
  4. * Copyright 2013 Luke Dashjr
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "config.h"
  25. #include <limits.h>
  26. #include "miner.h"
  27. #include <unistd.h>
  28. #include <stdbool.h>
  29. #include <stdint.h>
  30. #include <sha2.h>
  31. #include "deviceapi.h"
  32. #include "driver-bitfury.h"
  33. #include "libbitfury.h"
  34. #include "lowl-spi.h"
  35. #include "util.h"
  36. BFG_REGISTER_DRIVER(bitfury_drv)
  37. const struct bfg_set_device_definition bitfury_set_device_funcs[];
  38. static
  39. int bitfury_autodetect()
  40. {
  41. RUNONCE(0);
  42. int chip_n;
  43. struct cgpu_info *bitfury_info;
  44. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  45. bitfury_info->drv = &bitfury_drv;
  46. bitfury_info->threads = 1;
  47. applog(LOG_INFO, "INFO: bitfury_detect");
  48. spi_init();
  49. if (!sys_spi)
  50. return 0;
  51. {
  52. struct bitfury_device dummy_bitfury = {
  53. .spi = sys_spi,
  54. };
  55. drv_set_defaults(&bitfury_drv, bitfury_set_device_funcs_probe, &dummy_bitfury, NULL, NULL, 1);
  56. }
  57. chip_n = libbitfury_detectChips1(sys_spi);
  58. if (!chip_n) {
  59. applog(LOG_WARNING, "No Bitfury chips detected!");
  60. return 0;
  61. } else {
  62. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  63. }
  64. bitfury_info->procs = chip_n;
  65. bitfury_info->set_device_funcs = bitfury_set_device_funcs;
  66. add_cgpu(bitfury_info);
  67. return 1;
  68. }
  69. static void bitfury_detect(void)
  70. {
  71. noserial_detect_manual(&bitfury_drv, bitfury_autodetect);
  72. }
  73. static
  74. void *bitfury_just_io(struct bitfury_device * const bitfury)
  75. {
  76. struct spi_port * const spi = bitfury->spi;
  77. const int chip = bitfury->fasync;
  78. void *rv;
  79. spi_clear_buf(spi);
  80. spi_emit_break(spi);
  81. spi_emit_fasync(spi, chip);
  82. rv = spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  83. spi_txrx(spi);
  84. return rv;
  85. }
  86. static
  87. void bitfury_debug_nonce_array(const struct cgpu_info * const proc, const char *msg, const uint32_t * const inp)
  88. {
  89. const struct bitfury_device * const bitfury = proc->device_data;
  90. const int active = bitfury->active;
  91. char s[((1 + 8) * 0x10) + 1];
  92. char *sp = s;
  93. for (int i = 0; i < 0x10; ++i)
  94. sp += sprintf(sp, "%c%08lx",
  95. (active == i) ? '>' : ' ',
  96. (unsigned long)bitfury_decnonce(inp[i]));
  97. applog(LOG_DEBUG, "%"PRIpreprv": %s%s (job=%08lx)",
  98. proc->proc_repr, msg, s, (unsigned long)inp[0x10]);
  99. }
  100. static
  101. bool bitfury_init_oldbuf(struct cgpu_info * const proc, const uint32_t *inp)
  102. {
  103. struct bitfury_device * const bitfury = proc->device_data;
  104. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  105. uint32_t * const buf = &bitfury->newbuf[0];
  106. int i, differ, tried = 0;
  107. if (!inp)
  108. inp = bitfury_just_io(bitfury);
  109. tryagain:
  110. if (tried > 3)
  111. {
  112. applog(LOG_ERR, "%"PRIpreprv": %s: Giving up after %d tries",
  113. proc->proc_repr, __func__, tried);
  114. bitfury->desync_counter = 99;
  115. return false;
  116. }
  117. ++tried;
  118. memcpy(buf, inp, 0x10 * 4);
  119. inp = bitfury_just_io(bitfury);
  120. differ = -1;
  121. for (i = 0; i < 0x10; ++i)
  122. {
  123. if (inp[i] != buf[i])
  124. {
  125. if (differ != -1)
  126. {
  127. applog(LOG_DEBUG, "%"PRIpreprv": %s: Second differ at %d; trying again",
  128. proc->proc_repr, __func__, i);
  129. goto tryagain;
  130. }
  131. differ = i;
  132. applog(LOG_DEBUG, "%"PRIpreprv": %s: Differ at %d",
  133. proc->proc_repr, __func__, i);
  134. if (tried > 3)
  135. break;
  136. }
  137. }
  138. if (-1 == differ)
  139. {
  140. applog(LOG_DEBUG, "%"PRIpreprv": %s: No differ found; trying again",
  141. proc->proc_repr, __func__);
  142. goto tryagain;
  143. }
  144. bitfury->active = differ;
  145. memcpy(&oldbuf[0], &inp[bitfury->active], 4 * (0x10 - bitfury->active));
  146. memcpy(&oldbuf[0x10 - bitfury->active], &inp[0], 4 * bitfury->active);
  147. bitfury->oldjob = inp[0x10];
  148. bitfury->desync_counter = 0;
  149. if (opt_debug)
  150. bitfury_debug_nonce_array(proc, "Init", inp);
  151. return true;
  152. }
  153. bool bitfury_init_chip(struct cgpu_info * const proc)
  154. {
  155. struct bitfury_device * const bitfury = proc->device_data;
  156. struct bitfury_payload payload = {
  157. .midstate = "\xf9\x9a\xf0\xd5\x72\x34\x41\xdc\x9e\x10\xd1\x1f\xeb\xcd\xe3\xf5"
  158. "\x52\xf1\x14\x63\x06\x14\xd1\x12\x15\x25\x39\xd1\x7d\x77\x5a\xfd",
  159. .m7 = 0xafbd0b42,
  160. .ntime = 0xb6c24563,
  161. .nbits = 0x6dfa4352,
  162. };
  163. bitfury_payload_to_atrvec(bitfury->atrvec, &payload);
  164. return bitfury_init_oldbuf(proc, NULL);
  165. }
  166. static
  167. bool bitfury_init(struct thr_info *thr)
  168. {
  169. struct cgpu_info *proc;
  170. struct bitfury_device *bitfury;
  171. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  172. {
  173. bitfury = proc->device_data = malloc(sizeof(struct bitfury_device));
  174. *bitfury = (struct bitfury_device){
  175. .spi = sys_spi,
  176. .fasync = proc->proc_id,
  177. };
  178. bitfury_init_chip(proc);
  179. bitfury->osc6_bits = 50;
  180. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  181. }
  182. timer_set_now(&thr->tv_poll);
  183. return true;
  184. }
  185. void bitfury_disable(struct thr_info * const thr)
  186. {
  187. struct cgpu_info * const proc = thr->cgpu;
  188. struct bitfury_device * const bitfury = proc->device_data;
  189. applog(LOG_DEBUG, "%"PRIpreprv": Shutting down chip (disable)", proc->proc_repr);
  190. bitfury_send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  191. }
  192. void bitfury_enable(struct thr_info * const thr)
  193. {
  194. struct cgpu_info * const proc = thr->cgpu;
  195. struct bitfury_device * const bitfury = proc->device_data;
  196. struct cgpu_info * const dev = proc->device;
  197. struct thr_info * const master_thr = dev->thr[0];
  198. applog(LOG_DEBUG, "%"PRIpreprv": Reinitialising chip (enable)", proc->proc_repr);
  199. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  200. bitfury_init_chip(proc);
  201. if (!timer_isset(&master_thr->tv_poll))
  202. timer_set_now(&master_thr->tv_poll);
  203. }
  204. void bitfury_shutdown(struct thr_info *thr) {
  205. struct cgpu_info *cgpu = thr->cgpu, *proc;
  206. struct bitfury_device *bitfury;
  207. applog(LOG_INFO, "INFO bitfury_shutdown");
  208. for (proc = cgpu; proc; proc = proc->next_proc)
  209. {
  210. bitfury = proc->device_data;
  211. bitfury_send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  212. }
  213. }
  214. bool bitfury_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  215. {
  216. struct cgpu_info * const proc = thr->cgpu;
  217. struct bitfury_device * const bitfury = proc->device_data;
  218. if (opt_debug)
  219. {
  220. char hex[153];
  221. bin2hex(hex, &work->data[0], 76);
  222. applog(LOG_DEBUG, "%"PRIpreprv": Preparing work %s",
  223. proc->proc_repr, hex);
  224. }
  225. work_to_bitfury_payload(&bitfury->payload, work);
  226. bitfury_payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  227. work->blk.nonce = 0xffffffff;
  228. return true;
  229. }
  230. static
  231. bool fudge_nonce(struct work * const work, uint32_t *nonce_p) {
  232. static const uint32_t offsets[] = {0, 0xffc00000, 0xff800000, 0x02800000, 0x02C00000, 0x00400000};
  233. uint32_t nonce;
  234. int i;
  235. if (unlikely(!work))
  236. return false;
  237. for (i = 0; i < 6; ++i)
  238. {
  239. nonce = *nonce_p + offsets[i];
  240. if (test_nonce(work, nonce, false))
  241. {
  242. *nonce_p = nonce;
  243. return true;
  244. }
  245. }
  246. return false;
  247. }
  248. void bitfury_noop_job_start(struct thr_info __maybe_unused * const thr)
  249. {
  250. }
  251. // freq_stat->{mh,s} are allocated such that [osc6_min] is the first valid index and [0] falls outside the allocation
  252. void bitfury_init_freq_stat(struct freq_stat * const c, const int osc6_min, const int osc6_max)
  253. {
  254. const int osc6_values = (osc6_max + 1 - osc6_min);
  255. void * const p = calloc(osc6_values, (sizeof(*c->mh) + sizeof(*c->s)));
  256. c->mh = p - (sizeof(*c->mh) * osc6_min);
  257. c->s = p + (sizeof(*c->mh) * osc6_values) - (sizeof(*c->s) * osc6_min);
  258. c->osc6_min = osc6_min;
  259. c->osc6_max = osc6_max;
  260. }
  261. void bitfury_clean_freq_stat(struct freq_stat * const c)
  262. {
  263. free(&c->mh[c->osc6_min]);
  264. }
  265. #define HOP_DONE 600
  266. typedef uint32_t bitfury_inp_t[0x11];
  267. static
  268. int bitfury_select_freq(struct bitfury_device *bitfury, struct cgpu_info *proc) {
  269. int freq;
  270. int random;
  271. int i;
  272. bool all_done;
  273. struct freq_stat *c;
  274. c = &bitfury->chip_stat;
  275. if (c->best_done) {
  276. freq = c->best_osc;
  277. } else {
  278. random = (int)(bitfury->mhz * 1000.0) & 1;
  279. freq = (bitfury->osc6_bits == c->osc6_max) ? c->osc6_min : bitfury->osc6_bits + random;
  280. all_done = true;
  281. for (i = c->osc6_min; i <= c->osc6_max; ++i)
  282. if (c->s[i] <= HOP_DONE)
  283. {
  284. all_done = false;
  285. break;
  286. }
  287. if (all_done)
  288. {
  289. double mh_max = 0.0;
  290. for (i = c->osc6_min; i <= c->osc6_max; ++i)
  291. {
  292. const double mh_actual = c->mh[i] / c->s[i];
  293. if (mh_max >= mh_actual)
  294. continue;
  295. mh_max = mh_actual;
  296. freq = i;
  297. }
  298. c->best_done = 1;
  299. c->best_osc = freq;
  300. applog(LOG_DEBUG, "%"PRIpreprv": best_osc = %d",
  301. proc->proc_repr, freq);
  302. }
  303. }
  304. applog(LOG_DEBUG, "%"PRIpreprv": Changing osc6_bits to %d",
  305. proc->proc_repr, freq);
  306. bitfury->osc6_bits = freq;
  307. bitfury_send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  308. return 0;
  309. }
  310. void bitfury_do_io(struct thr_info * const master_thr)
  311. {
  312. struct cgpu_info *proc;
  313. struct thr_info *thr;
  314. struct bitfury_device *bitfury;
  315. struct freq_stat *c;
  316. const uint32_t *inp;
  317. int n, i, j;
  318. bool newjob;
  319. uint32_t nonce;
  320. int n_chips = 0, lastchip = 0;
  321. struct spi_port *spi = NULL;
  322. bool should_be_running;
  323. struct timeval tv_now;
  324. uint32_t counter;
  325. struct timeval *tvp_stat;
  326. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  327. ++n_chips;
  328. struct cgpu_info *procs[n_chips];
  329. void *rxbuf[n_chips];
  330. bitfury_inp_t rxbuf_copy[n_chips];
  331. // NOTE: This code assumes:
  332. // 1) that chips on the same SPI bus are grouped together
  333. // 2) that chips are in sequential fasync order
  334. n_chips = 0;
  335. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  336. {
  337. thr = proc->thr[0];
  338. bitfury = proc->device_data;
  339. should_be_running = (proc->deven == DEV_ENABLED && !thr->pause);
  340. if (should_be_running || thr->_job_transition_in_progress)
  341. {
  342. if (spi != bitfury->spi)
  343. {
  344. if (spi)
  345. spi_txrx(spi);
  346. spi = bitfury->spi;
  347. spi_clear_buf(spi);
  348. spi_emit_break(spi);
  349. lastchip = 0;
  350. }
  351. procs[n_chips] = proc;
  352. spi_emit_fasync(spi, bitfury->fasync - lastchip);
  353. lastchip = bitfury->fasync;
  354. rxbuf[n_chips] = spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  355. ++n_chips;
  356. }
  357. else
  358. if (thr->work /* is currently running */ && thr->busy_state != TBS_STARTING_JOB)
  359. ;//FIXME: shutdown chip
  360. }
  361. if (!spi)
  362. {
  363. timer_unset(&master_thr->tv_poll);
  364. return;
  365. }
  366. timer_set_now(&tv_now);
  367. spi_txrx(spi);
  368. for (j = 0; j < n_chips; ++j)
  369. {
  370. memcpy(rxbuf_copy[j], rxbuf[j], 0x11 * 4);
  371. rxbuf[j] = rxbuf_copy[j];
  372. }
  373. for (j = 0; j < n_chips; ++j)
  374. {
  375. proc = procs[j];
  376. thr = proc->thr[0];
  377. bitfury = proc->device_data;
  378. tvp_stat = &bitfury->tv_stat;
  379. c = &bitfury->chip_stat;
  380. uint32_t * const newbuf = &bitfury->newbuf[0];
  381. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  382. inp = rxbuf[j];
  383. if (unlikely(bitfury->desync_counter == 99))
  384. {
  385. bitfury_init_oldbuf(proc, inp);
  386. goto out;
  387. }
  388. if (opt_debug)
  389. bitfury_debug_nonce_array(proc, "Read", inp);
  390. // To avoid dealing with wrap-around entirely, we rotate array so previous active uint32_t is at index 0
  391. memcpy(&newbuf[0], &inp[bitfury->active], 4 * (0x10 - bitfury->active));
  392. memcpy(&newbuf[0x10 - bitfury->active], &inp[0], 4 * bitfury->active);
  393. newjob = inp[0x10];
  394. if (newbuf[0xf] != oldbuf[0xf])
  395. {
  396. inc_hw_errors2(thr, NULL, NULL);
  397. if (unlikely(++bitfury->desync_counter >= 4))
  398. {
  399. applog(LOG_WARNING, "%"PRIpreprv": Previous nonce mismatch (4th try), recalibrating",
  400. proc->proc_repr);
  401. bitfury_init_oldbuf(proc, inp);
  402. continue;
  403. }
  404. applog(LOG_DEBUG, "%"PRIpreprv": Previous nonce mismatch, ignoring response",
  405. proc->proc_repr);
  406. goto out;
  407. }
  408. else
  409. bitfury->desync_counter = 0;
  410. if (bitfury->oldjob != newjob && thr->next_work)
  411. {
  412. mt_job_transition(thr);
  413. // TODO: Delay morework until right before it's needed
  414. timer_set_now(&thr->tv_morework);
  415. job_start_complete(thr);
  416. }
  417. for (n = 0; newbuf[n] == oldbuf[n]; ++n)
  418. {
  419. if (unlikely(n >= 0xf))
  420. {
  421. inc_hw_errors2(thr, NULL, NULL);
  422. applog(LOG_DEBUG, "%"PRIpreprv": Full result match, reinitialising",
  423. proc->proc_repr);
  424. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  425. bitfury->desync_counter = 99;
  426. goto out;
  427. }
  428. }
  429. counter = bitfury_decnonce(newbuf[n]);
  430. if ((counter & 0xFFC00000) == 0xdf800000)
  431. {
  432. counter &= 0x003fffff;
  433. int32_t cycles = counter - bitfury->counter1;
  434. if (cycles < 0)
  435. cycles += 0x00400000;
  436. if (cycles & 0x00200000)
  437. {
  438. long long unsigned int period;
  439. double ns;
  440. struct timeval d_time;
  441. timersub(&(tv_now), &(bitfury->timer1), &d_time);
  442. period = timeval_to_us(&d_time) * 1000ULL;
  443. ns = (double)period / (double)(cycles);
  444. bitfury->mhz = 1.0 / ns * 65.0 * 1000.0;
  445. if (bitfury->mhz_best)
  446. {
  447. if (bitfury->mhz < bitfury->mhz_best / 2)
  448. {
  449. applog(LOG_WARNING, "%"PRIpreprv": Frequency drop over 50%% detected, reinitialising",
  450. proc->proc_repr);
  451. bitfury->force_reinit = true;
  452. }
  453. }
  454. if ((int)bitfury->mhz > bitfury->mhz_best && bitfury->mhz_last > bitfury->mhz_best)
  455. {
  456. // mhz_best is the lowest of two sequential readings over the previous best
  457. if ((int)bitfury->mhz > bitfury->mhz_last)
  458. bitfury->mhz_best = bitfury->mhz_last;
  459. else
  460. bitfury->mhz_best = bitfury->mhz;
  461. }
  462. bitfury->mhz_last = bitfury->mhz;
  463. bitfury->counter1 = counter;
  464. copy_time(&(bitfury->timer1), &tv_now);
  465. }
  466. }
  467. if (tvp_stat->tv_sec == 0 && tvp_stat->tv_usec == 0) {
  468. copy_time(tvp_stat, &tv_now);
  469. }
  470. if (c->osc6_max)
  471. {
  472. if (timer_elapsed(tvp_stat, &tv_now) >= 60)
  473. {
  474. double mh_diff, s_diff;
  475. const int osc = bitfury->osc6_bits;
  476. // Copy current statistics
  477. mh_diff = bitfury->counter2 - c->omh;
  478. s_diff = total_secs - c->os;
  479. applog(LOG_DEBUG, "%"PRIpreprv": %.0f completed in %f seconds",
  480. proc->proc_repr, mh_diff, s_diff);
  481. if (osc >= c->osc6_min && osc <= c->osc6_max)
  482. {
  483. c->mh[osc] += mh_diff;
  484. c->s[osc] += s_diff;
  485. }
  486. c->omh = bitfury->counter2;
  487. c->os = total_secs;
  488. if (opt_debug && !c->best_done)
  489. {
  490. char logbuf[0x100];
  491. logbuf[0] = '\0';
  492. for (i = c->osc6_min; i <= c->osc6_max; ++i)
  493. tailsprintf(logbuf, sizeof(logbuf), " %d=%.3f/%3.0fs",
  494. i, c->mh[i] / c->s[i], c->s[i]);
  495. applog(LOG_DEBUG, "%"PRIpreprv":%s",
  496. proc->proc_repr, logbuf);
  497. }
  498. // Change freq;
  499. if (!c->best_done) {
  500. bitfury_select_freq(bitfury, proc);
  501. } else {
  502. applog(LOG_DEBUG, "%"PRIpreprv": Stable freq, osc6_bits: %d",
  503. proc->proc_repr, bitfury->osc6_bits);
  504. }
  505. }
  506. }
  507. if (n)
  508. {
  509. for (i = 0; i < n; ++i)
  510. {
  511. nonce = bitfury_decnonce(newbuf[i]);
  512. if (fudge_nonce(thr->work, &nonce))
  513. {
  514. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (work=%p)",
  515. proc->proc_repr, i, (unsigned long)nonce, thr->work);
  516. submit_nonce(thr, thr->work, nonce);
  517. bitfury->counter2 += 1;
  518. }
  519. else
  520. if (fudge_nonce(thr->prev_work, &nonce))
  521. {
  522. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (prev work=%p)",
  523. proc->proc_repr, i, (unsigned long)nonce, thr->prev_work);
  524. submit_nonce(thr, thr->prev_work, nonce);
  525. bitfury->counter2 += 1;
  526. }
  527. else
  528. {
  529. inc_hw_errors(thr, thr->work, nonce);
  530. ++bitfury->sample_hwe;
  531. bitfury->strange_counter += 1;
  532. }
  533. if (++bitfury->sample_tot >= 0x40 || bitfury->sample_hwe >= 8)
  534. {
  535. if (bitfury->sample_hwe >= 8)
  536. {
  537. applog(LOG_WARNING, "%"PRIpreprv": %d of the last %d results were bad, reinitialising",
  538. proc->proc_repr, bitfury->sample_hwe, bitfury->sample_tot);
  539. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  540. bitfury->desync_counter = 99;
  541. }
  542. bitfury->sample_tot = bitfury->sample_hwe = 0;
  543. }
  544. }
  545. bitfury->active = (bitfury->active + n) % 0x10;
  546. }
  547. memcpy(&oldbuf[0], &newbuf[n], 4 * (0x10 - n));
  548. memcpy(&oldbuf[0x10 - n], &newbuf[0], 4 * n);
  549. bitfury->oldjob = newjob;
  550. out:
  551. if (unlikely(bitfury->force_reinit))
  552. {
  553. applog(LOG_DEBUG, "%"PRIpreprv": Forcing reinitialisation",
  554. proc->proc_repr);
  555. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  556. bitfury->desync_counter = 99;
  557. bitfury->mhz_last = 0;
  558. bitfury->mhz_best = 0;
  559. bitfury->force_reinit = false;
  560. }
  561. if (timer_elapsed(tvp_stat, &tv_now) >= 60)
  562. copy_time(tvp_stat, &tv_now);
  563. }
  564. timer_set_delay(&master_thr->tv_poll, &tv_now, 10000);
  565. }
  566. int64_t bitfury_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  567. {
  568. // Bitfury chips process only 768/1024 of the nonce range
  569. return 0xbd000000;
  570. }
  571. struct api_data *bitfury_api_device_detail(struct cgpu_info * const cgpu)
  572. {
  573. struct bitfury_device * const bitfury = cgpu->device_data;
  574. struct api_data *root = NULL;
  575. root = api_add_uint(root, "fasync", &bitfury->fasync, false);
  576. return root;
  577. }
  578. struct api_data *bitfury_api_device_status(struct cgpu_info * const cgpu)
  579. {
  580. struct bitfury_device * const bitfury = cgpu->device_data;
  581. struct api_data *root = NULL;
  582. int clock_bits = bitfury->osc6_bits;
  583. root = api_add_int(root, "Clock Bits", &clock_bits, true);
  584. root = api_add_freq(root, "Frequency", &bitfury->mhz, false);
  585. return root;
  586. }
  587. static
  588. bool _bitfury_set_device_parse_setting(uint32_t * const rv, const char * const setting, char * const replybuf, const int maxval)
  589. {
  590. char *p;
  591. long int nv;
  592. if (!setting || !*setting)
  593. {
  594. sprintf(replybuf, "missing setting");
  595. return false;
  596. }
  597. nv = strtol(setting, &p, 0);
  598. if (nv > maxval || nv < 1)
  599. {
  600. sprintf(replybuf, "invalid setting");
  601. return false;
  602. }
  603. *rv = nv;
  604. return true;
  605. }
  606. const char *bitfury_set_baud(struct cgpu_info * const proc, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  607. {
  608. struct bitfury_device * const bitfury = proc->device_data;
  609. if (!_bitfury_set_device_parse_setting(&bitfury->spi->speed, setting, replybuf, INT_MAX))
  610. return replybuf;
  611. return NULL;
  612. }
  613. const char *bitfury_set_osc6_bits(struct cgpu_info * const proc, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  614. {
  615. struct bitfury_device * const bitfury = proc->device_data;
  616. uint32_t newval;
  617. struct freq_stat * const c = &bitfury->chip_stat;
  618. newval = bitfury->osc6_bits;
  619. if (!_bitfury_set_device_parse_setting(&newval, setting, replybuf, BITFURY_MAX_OSC6_BITS))
  620. return replybuf;
  621. bitfury->osc6_bits = newval;
  622. bitfury->force_reinit = true;
  623. c->osc6_max = 0;
  624. return NULL;
  625. }
  626. const struct bfg_set_device_definition bitfury_set_device_funcs[] = {
  627. {"osc6_bits", bitfury_set_osc6_bits, "range 1-"BITFURY_MAX_OSC6_BITS_S" (slow to fast)"},
  628. // NOTE: bitfury_set_device_funcs_probe should begin here:
  629. {"baud", bitfury_set_baud, "SPI baud rate"},
  630. {NULL},
  631. };
  632. const struct bfg_set_device_definition *bitfury_set_device_funcs_probe = &bitfury_set_device_funcs[1];
  633. #ifdef HAVE_CURSES
  634. void bitfury_tui_wlogprint_choices(struct cgpu_info *cgpu)
  635. {
  636. wlogprint("[O]scillator bits ");
  637. }
  638. const char *bitfury_tui_handle_choice(struct cgpu_info *cgpu, int input)
  639. {
  640. struct bitfury_device * const bitfury = cgpu->device_data;
  641. char buf[0x100];
  642. switch (input)
  643. {
  644. case 'o': case 'O':
  645. {
  646. struct freq_stat * const c = &bitfury->chip_stat;
  647. int val;
  648. char *intvar;
  649. sprintf(buf, "Set oscillator bits (range 1-%d; slow to fast)", BITFURY_MAX_OSC6_BITS);
  650. intvar = curses_input(buf);
  651. if (!intvar)
  652. return "Invalid oscillator bits\n";
  653. val = atoi(intvar);
  654. free(intvar);
  655. if (val < 1 || val > BITFURY_MAX_OSC6_BITS)
  656. return "Invalid oscillator bits\n";
  657. bitfury->osc6_bits = val;
  658. bitfury->force_reinit = true;
  659. c->osc6_max = 0;
  660. return "Oscillator bits changing\n";
  661. }
  662. }
  663. return NULL;
  664. }
  665. void bitfury_wlogprint_status(struct cgpu_info *cgpu)
  666. {
  667. struct bitfury_device * const bitfury = cgpu->device_data;
  668. wlogprint("Oscillator bits: %d\n", bitfury->osc6_bits);
  669. }
  670. #endif
  671. struct device_drv bitfury_drv = {
  672. .dname = "bitfury_gpio",
  673. .name = "BFY",
  674. .drv_detect = bitfury_detect,
  675. .thread_init = bitfury_init,
  676. .thread_disable = bitfury_disable,
  677. .thread_enable = bitfury_enable,
  678. .thread_shutdown = bitfury_shutdown,
  679. .minerloop = minerloop_async,
  680. .job_prepare = bitfury_job_prepare,
  681. .job_start = bitfury_noop_job_start,
  682. .poll = bitfury_do_io,
  683. .job_process_results = bitfury_job_process_results,
  684. .get_api_extra_device_detail = bitfury_api_device_detail,
  685. .get_api_extra_device_status = bitfury_api_device_status,
  686. #ifdef HAVE_CURSES
  687. .proc_wlogprint_status = bitfury_wlogprint_status,
  688. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  689. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  690. #endif
  691. };