driver-bitfury.c 21 KB

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