driver-bitfury.c 23 KB

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