driver-bitfury.c 22 KB

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