driver-bitfury.c 23 KB

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