driver-bitfury.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * Copyright 2013 bitfury
  3. * Copyright 2013 legkodymov
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "config.h"
  24. #include "miner.h"
  25. #include <unistd.h>
  26. #include <sha2.h>
  27. #include "deviceapi.h"
  28. #include "libbitfury.h"
  29. #include "util.h"
  30. #include "spidevc.h"
  31. #define GOLDEN_BACKLOG 5
  32. #define LINE_LEN 2048
  33. struct device_drv bitfury_drv;
  34. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now);
  35. double shares_to_ghashes(int shares, int seconds);
  36. static
  37. int bitfury_autodetect()
  38. {
  39. RUNONCE(0);
  40. int chip_n;
  41. struct cgpu_info *bitfury_info;
  42. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  43. bitfury_info->drv = &bitfury_drv;
  44. bitfury_info->threads = 1;
  45. applog(LOG_INFO, "INFO: bitfury_detect");
  46. spi_init();
  47. if (!sys_spi)
  48. return 0;
  49. chip_n = libbitfury_detectChips1(sys_spi);
  50. if (!chip_n) {
  51. applog(LOG_WARNING, "No Bitfury chips detected!");
  52. return 0;
  53. } else {
  54. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  55. }
  56. bitfury_info->procs = chip_n;
  57. add_cgpu(bitfury_info);
  58. return 1;
  59. }
  60. static void bitfury_detect(void)
  61. {
  62. noserial_detect_manual(&bitfury_drv, bitfury_autodetect);
  63. }
  64. void *bitfury_just_io(struct bitfury_device * const bitfury)
  65. {
  66. struct spi_port * const spi = bitfury->spi;
  67. const int chip = bitfury->fasync;
  68. void *rv;
  69. spi_clear_buf(spi);
  70. spi_emit_break(spi);
  71. spi_emit_fasync(spi, chip);
  72. rv = spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  73. spi_txrx(spi);
  74. return rv;
  75. }
  76. static
  77. void bitfury_debug_nonce_array(const struct cgpu_info * const proc, const char *msg, const uint32_t * const inp)
  78. {
  79. const struct bitfury_device * const bitfury = proc->device_data;
  80. const int active = bitfury->active;
  81. char s[((1 + 8) * 0x10) + 1];
  82. char *sp = s;
  83. for (int i = 0; i < 0x10; ++i)
  84. sp += sprintf(sp, "%c%08lx",
  85. (active == i) ? '>' : ' ',
  86. (unsigned long)bitfury_decnonce(inp[i]));
  87. applog(LOG_DEBUG, "%"PRIpreprv": %s%s (job=%08lx)",
  88. proc->proc_repr, msg, s, (unsigned long)inp[0x10]);
  89. }
  90. static
  91. bool bitfury_init_oldbuf(struct cgpu_info * const proc)
  92. {
  93. struct bitfury_device * const bitfury = proc->device_data;
  94. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  95. uint32_t * const buf = &bitfury->newbuf[0];
  96. const uint32_t *inp;
  97. int i, differ, tried = 0;
  98. inp = bitfury_just_io(bitfury);
  99. tryagain:
  100. if (tried > 3)
  101. {
  102. applog(LOG_ERR, "%"PRIpreprv": %s: Giving up after %d tries",
  103. proc->proc_repr, __func__, tried);
  104. bitfury->desync_counter = 99;
  105. return false;
  106. }
  107. ++tried;
  108. memcpy(buf, inp, 0x10 * 4);
  109. inp = bitfury_just_io(bitfury);
  110. differ = -1;
  111. for (i = 0; i < 0x10; ++i)
  112. {
  113. if (inp[i] != buf[i])
  114. {
  115. if (differ != -1)
  116. {
  117. applog(LOG_DEBUG, "%"PRIpreprv": %s: Second differ at %d; trying again",
  118. proc->proc_repr, __func__, i);
  119. goto tryagain;
  120. }
  121. differ = i;
  122. applog(LOG_DEBUG, "%"PRIpreprv": %s: Differ at %d",
  123. proc->proc_repr, __func__, i);
  124. }
  125. }
  126. if (-1 == differ)
  127. {
  128. applog(LOG_DEBUG, "%"PRIpreprv": %s: No differ found; trying again",
  129. proc->proc_repr, __func__);
  130. goto tryagain;
  131. }
  132. bitfury->active = differ;
  133. memcpy(&oldbuf[0], &inp[bitfury->active], 4 * (0x10 - bitfury->active));
  134. memcpy(&oldbuf[0x10 - bitfury->active], &inp[0], 4 * bitfury->active);
  135. bitfury->oldjob = inp[0x10];
  136. bitfury->desync_counter = 0;
  137. if (opt_debug)
  138. bitfury_debug_nonce_array(proc, "Init", inp);
  139. return true;
  140. }
  141. bool bitfury_init_chip(struct cgpu_info * const proc)
  142. {
  143. struct bitfury_device * const bitfury = proc->device_data;
  144. struct bitfury_payload payload = {
  145. .midstate = "\xf9\x9a\xf0\xd5\x72\x34\x41\xdc\x9e\x10\xd1\x1f\xeb\xcd\xe3\xf5"
  146. "\x52\xf1\x14\x63\x06\x14\xd1\x12\x15\x25\x39\xd1\x7d\x77\x5a\xfd",
  147. .m7 = 0xafbd0b42,
  148. .ntime = 0xb6c24563,
  149. .nbits = 0x6dfa4352,
  150. };
  151. payload_to_atrvec(bitfury->atrvec, &payload);
  152. return bitfury_init_oldbuf(proc);
  153. }
  154. static
  155. bool bitfury_init(struct thr_info *thr)
  156. {
  157. struct cgpu_info *proc;
  158. struct bitfury_device *bitfury;
  159. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  160. {
  161. bitfury = proc->device_data = malloc(sizeof(struct bitfury_device));
  162. *bitfury = (struct bitfury_device){
  163. .spi = sys_spi,
  164. .fasync = proc->proc_id,
  165. };
  166. bitfury_init_chip(proc);
  167. }
  168. return true;
  169. }
  170. static
  171. bool bitfury_queue_full(struct cgpu_info *cgpu)
  172. {
  173. struct cgpu_info *proc;
  174. struct bitfury_device *bitfury;
  175. for (proc = cgpu; proc; proc = proc->next_proc)
  176. {
  177. bitfury = proc->device_data;
  178. if (bitfury->work)
  179. continue;
  180. bitfury->work = get_queued(cgpu);
  181. if (!bitfury->work)
  182. return false;
  183. work_to_payload(&bitfury->payload, bitfury->work);
  184. }
  185. return true;
  186. }
  187. int64_t bitfury_scanHash(struct thr_info *thr)
  188. {
  189. struct cgpu_info * const cgpu = thr->cgpu;
  190. struct bitfury_device * const sds = cgpu->device_data;
  191. struct cgpu_info *proc;
  192. struct thr_info *pthr;
  193. struct bitfury_device *bitfury;
  194. struct timeval now;
  195. char line[LINE_LEN];
  196. int short_stat = 10;
  197. int long_stat = 1800;
  198. int i;
  199. if (!bitfury_queue_full(cgpu))
  200. return 0;
  201. for (proc = cgpu; proc; proc = proc->next_proc)
  202. {
  203. const int chip = proc->proc_id;
  204. pthr = proc->thr[0];
  205. bitfury = proc->device_data;
  206. bitfury->job_switched = 0;
  207. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  208. libbitfury_sendHashData1(chip, bitfury, pthr);
  209. }
  210. cgsleep_ms(5);
  211. cgtime(&now);
  212. for (proc = cgpu; proc; proc = proc->next_proc)
  213. {
  214. pthr = proc->thr[0];
  215. bitfury = proc->device_data;
  216. if (bitfury->job_switched) {
  217. int i,j;
  218. unsigned int * const res = bitfury->results;
  219. struct work * const work = bitfury->work;
  220. struct work * const owork = bitfury->owork;
  221. struct work * const o2work = bitfury->o2work;
  222. i = bitfury->results_n;
  223. for (j = i - 1; j >= 0; j--) {
  224. if (owork) {
  225. submit_nonce(pthr, owork, bswap_32(res[j]));
  226. bitfury->stat_ts[bitfury->stat_counter++] =
  227. now.tv_sec;
  228. if (bitfury->stat_counter == BITFURY_STAT_N) {
  229. bitfury->stat_counter = 0;
  230. }
  231. }
  232. if (o2work) {
  233. // TEST
  234. //submit_nonce(pthr, owork, bswap_32(res[j]));
  235. }
  236. }
  237. bitfury->results_n = 0;
  238. bitfury->job_switched = 0;
  239. if (bitfury->old_nonce && o2work) {
  240. submit_nonce(pthr, o2work, bswap_32(bitfury->old_nonce));
  241. i++;
  242. }
  243. if (bitfury->future_nonce) {
  244. submit_nonce(pthr, work, bswap_32(bitfury->future_nonce));
  245. i++;
  246. }
  247. if (o2work)
  248. work_completed(cgpu, o2work);
  249. bitfury->o2work = bitfury->owork;
  250. bitfury->owork = bitfury->work;
  251. bitfury->work = NULL;
  252. hashes_done2(pthr, 0xbd000000, NULL);
  253. }
  254. }
  255. if (now.tv_sec - sds->short_out_t > short_stat) {
  256. int shares_first = 0, shares_last = 0, shares_total = 0;
  257. char stat_lines[32][LINE_LEN] = {{0}};
  258. int len, k;
  259. double gh[32][8] = {{0}};
  260. double ghsum = 0, gh1h = 0, gh2h = 0;
  261. unsigned strange_counter = 0;
  262. for (proc = cgpu; proc; proc = proc->next_proc)
  263. {
  264. const int chip = proc->proc_id;
  265. bitfury = proc->device_data;
  266. int shares_found = calc_stat(bitfury->stat_ts, short_stat, now);
  267. double ghash;
  268. len = strlen(stat_lines[bitfury->slot]);
  269. ghash = shares_to_ghashes(shares_found, short_stat);
  270. gh[bitfury->slot][chip & 0x07] = ghash;
  271. snprintf(stat_lines[bitfury->slot] + len, LINE_LEN - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  272. if(sds->short_out_t && ghash < 0.5) {
  273. applog(LOG_WARNING, "Chip_id %d FREQ CHANGE", chip);
  274. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits - 1);
  275. cgsleep_ms(1);
  276. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  277. }
  278. shares_total += shares_found;
  279. shares_first += chip < 4 ? shares_found : 0;
  280. shares_last += chip > 3 ? shares_found : 0;
  281. strange_counter += bitfury->strange_counter;
  282. bitfury->strange_counter = 0;
  283. }
  284. sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
  285. applog(LOG_WARNING, "%s", line);
  286. sprintf(line, "stranges: %u", strange_counter);
  287. applog(LOG_WARNING, "%s", line);
  288. for(i = 0; i < 32; i++)
  289. if(strlen(stat_lines[i])) {
  290. len = strlen(stat_lines[i]);
  291. ghsum = 0;
  292. gh1h = 0;
  293. gh2h = 0;
  294. for(k = 0; k < 4; k++) {
  295. gh1h += gh[i][k];
  296. gh2h += gh[i][k+4];
  297. ghsum += gh[i][k] + gh[i][k+4];
  298. }
  299. snprintf(stat_lines[i] + len, LINE_LEN - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  300. applog(LOG_WARNING, "%s", stat_lines[i]);
  301. }
  302. sds->short_out_t = now.tv_sec;
  303. }
  304. if (now.tv_sec - sds->long_out_t > long_stat) {
  305. int shares_first = 0, shares_last = 0, shares_total = 0;
  306. char stat_lines[32][LINE_LEN] = {{0}};
  307. int len, k;
  308. double gh[32][8] = {{0}};
  309. double ghsum = 0, gh1h = 0, gh2h = 0;
  310. for (proc = cgpu; proc; proc = proc->next_proc)
  311. {
  312. const int chip = proc->proc_id;
  313. bitfury = proc->device_data;
  314. int shares_found = calc_stat(bitfury->stat_ts, long_stat, now);
  315. double ghash;
  316. len = strlen(stat_lines[bitfury->slot]);
  317. ghash = shares_to_ghashes(shares_found, long_stat);
  318. gh[bitfury->slot][chip & 0x07] = ghash;
  319. snprintf(stat_lines[bitfury->slot] + len, LINE_LEN - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  320. shares_total += shares_found;
  321. shares_first += chip < 4 ? shares_found : 0;
  322. shares_last += chip > 3 ? shares_found : 0;
  323. }
  324. sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
  325. applog(LOG_WARNING, "%s", line);
  326. for(i = 0; i < 32; i++)
  327. if(strlen(stat_lines[i])) {
  328. len = strlen(stat_lines[i]);
  329. ghsum = 0;
  330. gh1h = 0;
  331. gh2h = 0;
  332. for(k = 0; k < 4; k++) {
  333. gh1h += gh[i][k];
  334. gh2h += gh[i][k+4];
  335. ghsum += gh[i][k] + gh[i][k+4];
  336. }
  337. snprintf(stat_lines[i] + len, LINE_LEN - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  338. applog(LOG_WARNING, "%s", stat_lines[i]);
  339. }
  340. sds->long_out_t = now.tv_sec;
  341. }
  342. return 0;
  343. }
  344. double shares_to_ghashes(int shares, int seconds) {
  345. return (double)shares / (double)seconds * 4.84387; //orig: 4.77628
  346. }
  347. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
  348. int j;
  349. int shares_found = 0;
  350. for(j = 0; j < BITFURY_STAT_N; j++) {
  351. if (now.tv_sec - stat_ts[j] < stat) {
  352. shares_found++;
  353. }
  354. }
  355. return shares_found;
  356. }
  357. bool bitfury_prepare(struct thr_info *thr)
  358. {
  359. struct cgpu_info *cgpu = thr->cgpu;
  360. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  361. applog(LOG_INFO, "INFO bitfury_prepare");
  362. return true;
  363. }
  364. void bitfury_shutdown(struct thr_info *thr) {
  365. struct cgpu_info *cgpu = thr->cgpu, *proc;
  366. struct bitfury_device *bitfury;
  367. applog(LOG_INFO, "INFO bitfury_shutdown");
  368. for (proc = cgpu; proc; proc = proc->next_proc)
  369. {
  370. bitfury = proc->device_data;
  371. send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  372. }
  373. }
  374. bool bitfury_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  375. {
  376. struct cgpu_info * const proc = thr->cgpu;
  377. struct bitfury_device * const bitfury = proc->device_data;
  378. if (opt_debug)
  379. {
  380. char hex[153];
  381. bin2hex(hex, &work->data[0], 76);
  382. applog(LOG_DEBUG, "%"PRIpreprv": Preparing work %s",
  383. proc->proc_repr, hex);
  384. }
  385. work_to_payload(&bitfury->payload, work);
  386. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  387. work->blk.nonce = 0xffffffff;
  388. return true;
  389. }
  390. static
  391. bool fudge_nonce(struct work * const work, uint32_t *nonce_p) {
  392. static const uint32_t offsets[] = {0, 0xffc00000, 0xff800000, 0x02800000, 0x02C00000, 0x00400000};
  393. uint32_t nonce;
  394. int i;
  395. if (unlikely(!work))
  396. return false;
  397. for (i = 0; i < 6; ++i)
  398. {
  399. nonce = *nonce_p + offsets[i];
  400. if (test_nonce(work, nonce, false))
  401. {
  402. *nonce_p = nonce;
  403. return true;
  404. }
  405. }
  406. return false;
  407. }
  408. void bitfury_noop_job_start(struct thr_info __maybe_unused * const thr)
  409. {
  410. }
  411. void bitfury_do_io(struct thr_info * const master_thr)
  412. {
  413. struct cgpu_info *proc;
  414. struct thr_info *thr;
  415. struct bitfury_device *bitfury;
  416. const uint32_t *inp;
  417. int n, i, j;
  418. bool newjob;
  419. uint32_t nonce;
  420. int n_chips = 0, lastchip;
  421. struct spi_port *spi = NULL;
  422. bool should_be_running;
  423. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  424. ++n_chips;
  425. struct cgpu_info *procs[n_chips];
  426. void *rxbuf[n_chips];
  427. // NOTE: This code assumes:
  428. // 1) that chips on the same SPI bus are grouped together
  429. // 2) that chips are in sequential fasync order
  430. n_chips = 0;
  431. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  432. {
  433. thr = proc->thr[0];
  434. bitfury = proc->device_data;
  435. should_be_running = (proc->deven == DEV_ENABLED && !thr->pause);
  436. if (should_be_running)
  437. {
  438. if (spi != bitfury->spi)
  439. {
  440. if (spi)
  441. spi_txrx(spi);
  442. spi = bitfury->spi;
  443. spi_clear_buf(spi);
  444. spi_emit_break(spi);
  445. lastchip = 0;
  446. }
  447. procs[n_chips] = proc;
  448. spi_emit_fasync(spi, bitfury->fasync - lastchip);
  449. lastchip = bitfury->fasync;
  450. rxbuf[n_chips] = spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  451. ++n_chips;
  452. }
  453. else
  454. if (thr->work /* is currently running */ && thr->busy_state != TBS_STARTING_JOB)
  455. ;//FIXME: shutdown chip
  456. }
  457. spi_txrx(spi);
  458. for (j = 0; j < n_chips; ++j)
  459. {
  460. proc = procs[j];
  461. thr = proc->thr[0];
  462. bitfury = proc->device_data;
  463. uint32_t * const newbuf = &bitfury->newbuf[0];
  464. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  465. if (unlikely(bitfury->desync_counter == 99))
  466. {
  467. // TODO: use current rxbuf
  468. bitfury_init_oldbuf(proc);
  469. goto out;
  470. }
  471. inp = rxbuf[j];
  472. if (opt_debug)
  473. bitfury_debug_nonce_array(proc, "Read", inp);
  474. // To avoid dealing with wrap-around entirely, we rotate array so previous active uint32_t is at index 0
  475. memcpy(&newbuf[0], &inp[bitfury->active], 4 * (0x10 - bitfury->active));
  476. memcpy(&newbuf[0x10 - bitfury->active], &inp[0], 4 * bitfury->active);
  477. newjob = inp[0x10];
  478. if (newbuf[0xf] != oldbuf[0xf])
  479. {
  480. inc_hw_errors2(thr, NULL, NULL);
  481. if (unlikely(++bitfury->desync_counter >= 4))
  482. {
  483. applog(LOG_WARNING, "%"PRIpreprv": Previous nonce mismatch (4th try), recalibrating",
  484. proc->proc_repr);
  485. bitfury_init_oldbuf(proc);
  486. continue;
  487. }
  488. applog(LOG_DEBUG, "%"PRIpreprv": Previous nonce mismatch, ignoring response",
  489. proc->proc_repr);
  490. goto out;
  491. }
  492. else
  493. bitfury->desync_counter = 0;
  494. if (bitfury->oldjob != newjob && thr->next_work)
  495. {
  496. mt_job_transition(thr);
  497. // TODO: Delay morework until right before it's needed
  498. timer_set_now(&thr->tv_morework);
  499. job_start_complete(thr);
  500. }
  501. for (n = 0; newbuf[n] == oldbuf[n]; ++n)
  502. {
  503. if (unlikely(n >= 0xf))
  504. {
  505. inc_hw_errors2(thr, NULL, NULL);
  506. applog(LOG_DEBUG, "%"PRIpreprv": Full result match, reinitialising",
  507. proc->proc_repr);
  508. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  509. bitfury->desync_counter = 99;
  510. goto out;
  511. }
  512. }
  513. if (n)
  514. {
  515. for (i = 0; i < n; ++i)
  516. {
  517. nonce = bitfury_decnonce(newbuf[i]);
  518. if (fudge_nonce(thr->work, &nonce))
  519. {
  520. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (work=%p)",
  521. proc->proc_repr, i, (unsigned long)nonce, thr->work);
  522. submit_nonce(thr, thr->work, nonce);
  523. }
  524. else
  525. if (fudge_nonce(thr->prev_work, &nonce))
  526. {
  527. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (prev work=%p)",
  528. proc->proc_repr, i, (unsigned long)nonce, thr->prev_work);
  529. submit_nonce(thr, thr->prev_work, nonce);
  530. }
  531. else
  532. {
  533. inc_hw_errors(thr, thr->work, nonce);
  534. ++bitfury->sample_hwe;
  535. }
  536. if (++bitfury->sample_tot >= 0x40 || bitfury->sample_hwe >= 8)
  537. {
  538. if (bitfury->sample_hwe >= 8)
  539. {
  540. applog(LOG_WARNING, "%"PRIpreprv": %d of the last %d results were bad, reinitialising",
  541. proc->proc_repr, bitfury->sample_hwe, bitfury->sample_tot);
  542. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  543. bitfury->desync_counter = 99;
  544. }
  545. bitfury->sample_tot = bitfury->sample_hwe = 0;
  546. }
  547. }
  548. bitfury->active = (bitfury->active + n) % 0x10;
  549. }
  550. memcpy(&oldbuf[0], &newbuf[n], 4 * (0x10 - n));
  551. memcpy(&oldbuf[0x10 - n], &newbuf[0], 4 * n);
  552. bitfury->oldjob = newjob;
  553. out:
  554. continue;
  555. }
  556. timer_set_delay_from_now(&master_thr->tv_poll, 10000);
  557. }
  558. int64_t bitfury_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  559. {
  560. // Bitfury chips process only 768/1024 of the nonce range
  561. return 0xbd000000;
  562. }
  563. struct api_data *bitfury_api_device_status(const struct cgpu_info * const cgpu)
  564. {
  565. const struct bitfury_device * const bitfury = cgpu->device_data;
  566. struct api_data *root = NULL;
  567. int clock_bits = bitfury->osc6_bits;
  568. root = api_add_int(root, "Clock Bits", &clock_bits, true);
  569. return root;
  570. }
  571. struct device_drv bitfury_drv = {
  572. .dname = "bitfury_gpio",
  573. .name = "BFY",
  574. .drv_detect = bitfury_detect,
  575. .thread_prepare = bitfury_prepare,
  576. .thread_init = bitfury_init,
  577. .queue_full = bitfury_queue_full,
  578. .scanwork = bitfury_scanHash,
  579. .thread_shutdown = bitfury_shutdown,
  580. .minerloop = hash_queued_work,
  581. };