driver-bitfury.c 23 KB

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