driver-bitfury.c 23 KB

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