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. static
  65. void *bitfury_just_io(struct bitfury_device * const bitfury)
  66. {
  67. struct spi_port * const spi = bitfury->spi;
  68. const int chip = bitfury->fasync;
  69. void *rv;
  70. spi_clear_buf(spi);
  71. spi_emit_break(spi);
  72. spi_emit_fasync(spi, chip);
  73. rv = spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  74. spi_txrx(spi);
  75. return rv;
  76. }
  77. static
  78. void bitfury_debug_nonce_array(const struct cgpu_info * const proc, const char *msg, const uint32_t * const inp)
  79. {
  80. const struct bitfury_device * const bitfury = proc->device_data;
  81. const int active = bitfury->active;
  82. char s[((1 + 8) * 0x10) + 1];
  83. char *sp = s;
  84. for (int i = 0; i < 0x10; ++i)
  85. sp += sprintf(sp, "%c%08lx",
  86. (active == i) ? '>' : ' ',
  87. (unsigned long)bitfury_decnonce(inp[i]));
  88. applog(LOG_DEBUG, "%"PRIpreprv": %s%s (job=%08lx)",
  89. proc->proc_repr, msg, s, (unsigned long)inp[0x10]);
  90. }
  91. static
  92. bool bitfury_init_oldbuf(struct cgpu_info * const proc, const uint32_t *inp)
  93. {
  94. struct bitfury_device * const bitfury = proc->device_data;
  95. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  96. uint32_t * const buf = &bitfury->newbuf[0];
  97. int i, differ, tried = 0;
  98. if (!inp)
  99. inp = bitfury_just_io(bitfury);
  100. tryagain:
  101. if (tried > 3)
  102. {
  103. applog(LOG_ERR, "%"PRIpreprv": %s: Giving up after %d tries",
  104. proc->proc_repr, __func__, tried);
  105. bitfury->desync_counter = 99;
  106. return false;
  107. }
  108. ++tried;
  109. memcpy(buf, inp, 0x10 * 4);
  110. inp = bitfury_just_io(bitfury);
  111. differ = -1;
  112. for (i = 0; i < 0x10; ++i)
  113. {
  114. if (inp[i] != buf[i])
  115. {
  116. if (differ != -1)
  117. {
  118. applog(LOG_DEBUG, "%"PRIpreprv": %s: Second differ at %d; trying again",
  119. proc->proc_repr, __func__, i);
  120. goto tryagain;
  121. }
  122. differ = i;
  123. applog(LOG_DEBUG, "%"PRIpreprv": %s: Differ at %d",
  124. proc->proc_repr, __func__, i);
  125. }
  126. }
  127. if (-1 == differ)
  128. {
  129. applog(LOG_DEBUG, "%"PRIpreprv": %s: No differ found; trying again",
  130. proc->proc_repr, __func__);
  131. goto tryagain;
  132. }
  133. bitfury->active = differ;
  134. memcpy(&oldbuf[0], &inp[bitfury->active], 4 * (0x10 - bitfury->active));
  135. memcpy(&oldbuf[0x10 - bitfury->active], &inp[0], 4 * bitfury->active);
  136. bitfury->oldjob = inp[0x10];
  137. bitfury->desync_counter = 0;
  138. if (opt_debug)
  139. bitfury_debug_nonce_array(proc, "Init", inp);
  140. return true;
  141. }
  142. bool bitfury_init_chip(struct cgpu_info * const proc)
  143. {
  144. struct bitfury_device * const bitfury = proc->device_data;
  145. struct bitfury_payload payload = {
  146. .midstate = "\xf9\x9a\xf0\xd5\x72\x34\x41\xdc\x9e\x10\xd1\x1f\xeb\xcd\xe3\xf5"
  147. "\x52\xf1\x14\x63\x06\x14\xd1\x12\x15\x25\x39\xd1\x7d\x77\x5a\xfd",
  148. .m7 = 0xafbd0b42,
  149. .ntime = 0xb6c24563,
  150. .nbits = 0x6dfa4352,
  151. };
  152. payload_to_atrvec(bitfury->atrvec, &payload);
  153. return bitfury_init_oldbuf(proc, NULL);
  154. }
  155. static
  156. bool bitfury_init(struct thr_info *thr)
  157. {
  158. struct cgpu_info *proc;
  159. struct bitfury_device *bitfury;
  160. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  161. {
  162. bitfury = proc->device_data = malloc(sizeof(struct bitfury_device));
  163. *bitfury = (struct bitfury_device){
  164. .spi = sys_spi,
  165. .fasync = proc->proc_id,
  166. };
  167. bitfury_init_chip(proc);
  168. }
  169. return true;
  170. }
  171. static
  172. bool bitfury_queue_full(struct cgpu_info *cgpu)
  173. {
  174. struct cgpu_info *proc;
  175. struct bitfury_device *bitfury;
  176. for (proc = cgpu; proc; proc = proc->next_proc)
  177. {
  178. bitfury = proc->device_data;
  179. if (bitfury->work)
  180. continue;
  181. bitfury->work = get_queued(cgpu);
  182. if (!bitfury->work)
  183. return false;
  184. work_to_payload(&bitfury->payload, bitfury->work);
  185. }
  186. return true;
  187. }
  188. int64_t bitfury_scanHash(struct thr_info *thr)
  189. {
  190. struct cgpu_info * const cgpu = thr->cgpu;
  191. struct bitfury_device * const sds = cgpu->device_data;
  192. struct cgpu_info *proc;
  193. struct thr_info *pthr;
  194. struct bitfury_device *bitfury;
  195. struct timeval now;
  196. char line[LINE_LEN];
  197. int short_stat = 10;
  198. int long_stat = 1800;
  199. int i;
  200. if (!bitfury_queue_full(cgpu))
  201. return 0;
  202. for (proc = cgpu; proc; proc = proc->next_proc)
  203. {
  204. const int chip = proc->proc_id;
  205. pthr = proc->thr[0];
  206. bitfury = proc->device_data;
  207. bitfury->job_switched = 0;
  208. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  209. libbitfury_sendHashData1(chip, bitfury, pthr);
  210. }
  211. cgsleep_ms(5);
  212. cgtime(&now);
  213. for (proc = cgpu; proc; proc = proc->next_proc)
  214. {
  215. pthr = proc->thr[0];
  216. bitfury = proc->device_data;
  217. if (bitfury->job_switched) {
  218. int i,j;
  219. unsigned int * const res = bitfury->results;
  220. struct work * const work = bitfury->work;
  221. struct work * const owork = bitfury->owork;
  222. struct work * const o2work = bitfury->o2work;
  223. i = bitfury->results_n;
  224. for (j = i - 1; j >= 0; j--) {
  225. if (owork) {
  226. submit_nonce(pthr, owork, bswap_32(res[j]));
  227. bitfury->stat_ts[bitfury->stat_counter++] =
  228. now.tv_sec;
  229. if (bitfury->stat_counter == BITFURY_STAT_N) {
  230. bitfury->stat_counter = 0;
  231. }
  232. }
  233. if (o2work) {
  234. // TEST
  235. //submit_nonce(pthr, owork, bswap_32(res[j]));
  236. }
  237. }
  238. bitfury->results_n = 0;
  239. bitfury->job_switched = 0;
  240. if (bitfury->old_nonce && o2work) {
  241. submit_nonce(pthr, o2work, bswap_32(bitfury->old_nonce));
  242. i++;
  243. }
  244. if (bitfury->future_nonce) {
  245. submit_nonce(pthr, work, bswap_32(bitfury->future_nonce));
  246. i++;
  247. }
  248. if (o2work)
  249. work_completed(cgpu, o2work);
  250. bitfury->o2work = bitfury->owork;
  251. bitfury->owork = bitfury->work;
  252. bitfury->work = NULL;
  253. hashes_done2(pthr, 0xbd000000, NULL);
  254. }
  255. }
  256. if (now.tv_sec - sds->short_out_t > short_stat) {
  257. int shares_first = 0, shares_last = 0, shares_total = 0;
  258. char stat_lines[32][LINE_LEN] = {{0}};
  259. int len, k;
  260. double gh[32][8] = {{0}};
  261. double ghsum = 0, gh1h = 0, gh2h = 0;
  262. unsigned strange_counter = 0;
  263. for (proc = cgpu; proc; proc = proc->next_proc)
  264. {
  265. const int chip = proc->proc_id;
  266. bitfury = proc->device_data;
  267. int shares_found = calc_stat(bitfury->stat_ts, short_stat, now);
  268. double ghash;
  269. len = strlen(stat_lines[bitfury->slot]);
  270. ghash = shares_to_ghashes(shares_found, short_stat);
  271. gh[bitfury->slot][chip & 0x07] = ghash;
  272. snprintf(stat_lines[bitfury->slot] + len, LINE_LEN - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  273. if(sds->short_out_t && ghash < 0.5) {
  274. applog(LOG_WARNING, "Chip_id %d FREQ CHANGE", chip);
  275. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits - 1);
  276. cgsleep_ms(1);
  277. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  278. }
  279. shares_total += shares_found;
  280. shares_first += chip < 4 ? shares_found : 0;
  281. shares_last += chip > 3 ? shares_found : 0;
  282. strange_counter += bitfury->strange_counter;
  283. bitfury->strange_counter = 0;
  284. }
  285. sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
  286. applog(LOG_WARNING, "%s", line);
  287. sprintf(line, "stranges: %u", strange_counter);
  288. applog(LOG_WARNING, "%s", line);
  289. for(i = 0; i < 32; i++)
  290. if(strlen(stat_lines[i])) {
  291. len = strlen(stat_lines[i]);
  292. ghsum = 0;
  293. gh1h = 0;
  294. gh2h = 0;
  295. for(k = 0; k < 4; k++) {
  296. gh1h += gh[i][k];
  297. gh2h += gh[i][k+4];
  298. ghsum += gh[i][k] + gh[i][k+4];
  299. }
  300. snprintf(stat_lines[i] + len, LINE_LEN - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  301. applog(LOG_WARNING, "%s", stat_lines[i]);
  302. }
  303. sds->short_out_t = now.tv_sec;
  304. }
  305. if (now.tv_sec - sds->long_out_t > long_stat) {
  306. int shares_first = 0, shares_last = 0, shares_total = 0;
  307. char stat_lines[32][LINE_LEN] = {{0}};
  308. int len, k;
  309. double gh[32][8] = {{0}};
  310. double ghsum = 0, gh1h = 0, gh2h = 0;
  311. for (proc = cgpu; proc; proc = proc->next_proc)
  312. {
  313. const int chip = proc->proc_id;
  314. bitfury = proc->device_data;
  315. int shares_found = calc_stat(bitfury->stat_ts, long_stat, now);
  316. double ghash;
  317. len = strlen(stat_lines[bitfury->slot]);
  318. ghash = shares_to_ghashes(shares_found, long_stat);
  319. gh[bitfury->slot][chip & 0x07] = ghash;
  320. snprintf(stat_lines[bitfury->slot] + len, LINE_LEN - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  321. shares_total += shares_found;
  322. shares_first += chip < 4 ? shares_found : 0;
  323. shares_last += chip > 3 ? shares_found : 0;
  324. }
  325. sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
  326. applog(LOG_WARNING, "%s", line);
  327. for(i = 0; i < 32; i++)
  328. if(strlen(stat_lines[i])) {
  329. len = strlen(stat_lines[i]);
  330. ghsum = 0;
  331. gh1h = 0;
  332. gh2h = 0;
  333. for(k = 0; k < 4; k++) {
  334. gh1h += gh[i][k];
  335. gh2h += gh[i][k+4];
  336. ghsum += gh[i][k] + gh[i][k+4];
  337. }
  338. snprintf(stat_lines[i] + len, LINE_LEN - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  339. applog(LOG_WARNING, "%s", stat_lines[i]);
  340. }
  341. sds->long_out_t = now.tv_sec;
  342. }
  343. return 0;
  344. }
  345. double shares_to_ghashes(int shares, int seconds) {
  346. return (double)shares / (double)seconds * 4.84387; //orig: 4.77628
  347. }
  348. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
  349. int j;
  350. int shares_found = 0;
  351. for(j = 0; j < BITFURY_STAT_N; j++) {
  352. if (now.tv_sec - stat_ts[j] < stat) {
  353. shares_found++;
  354. }
  355. }
  356. return shares_found;
  357. }
  358. bool bitfury_prepare(struct thr_info *thr)
  359. {
  360. struct cgpu_info *cgpu = thr->cgpu;
  361. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  362. applog(LOG_INFO, "INFO bitfury_prepare");
  363. return true;
  364. }
  365. void bitfury_shutdown(struct thr_info *thr) {
  366. struct cgpu_info *cgpu = thr->cgpu, *proc;
  367. struct bitfury_device *bitfury;
  368. applog(LOG_INFO, "INFO bitfury_shutdown");
  369. for (proc = cgpu; proc; proc = proc->next_proc)
  370. {
  371. bitfury = proc->device_data;
  372. send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  373. }
  374. }
  375. bool bitfury_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  376. {
  377. struct cgpu_info * const proc = thr->cgpu;
  378. struct bitfury_device * const bitfury = proc->device_data;
  379. if (opt_debug)
  380. {
  381. char hex[153];
  382. bin2hex(hex, &work->data[0], 76);
  383. applog(LOG_DEBUG, "%"PRIpreprv": Preparing work %s",
  384. proc->proc_repr, hex);
  385. }
  386. work_to_payload(&bitfury->payload, work);
  387. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  388. work->blk.nonce = 0xffffffff;
  389. return true;
  390. }
  391. static
  392. bool fudge_nonce(struct work * const work, uint32_t *nonce_p) {
  393. static const uint32_t offsets[] = {0, 0xffc00000, 0xff800000, 0x02800000, 0x02C00000, 0x00400000};
  394. uint32_t nonce;
  395. int i;
  396. if (unlikely(!work))
  397. return false;
  398. for (i = 0; i < 6; ++i)
  399. {
  400. nonce = *nonce_p + offsets[i];
  401. if (test_nonce(work, nonce, false))
  402. {
  403. *nonce_p = nonce;
  404. return true;
  405. }
  406. }
  407. return false;
  408. }
  409. void bitfury_noop_job_start(struct thr_info __maybe_unused * const thr)
  410. {
  411. }
  412. void bitfury_do_io(struct thr_info * const master_thr)
  413. {
  414. struct cgpu_info *proc;
  415. struct thr_info *thr;
  416. struct bitfury_device *bitfury;
  417. const uint32_t *inp;
  418. int n, i, j;
  419. bool newjob;
  420. uint32_t nonce;
  421. int n_chips = 0, lastchip;
  422. struct spi_port *spi = NULL;
  423. bool should_be_running;
  424. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  425. ++n_chips;
  426. struct cgpu_info *procs[n_chips];
  427. void *rxbuf[n_chips];
  428. // NOTE: This code assumes:
  429. // 1) that chips on the same SPI bus are grouped together
  430. // 2) that chips are in sequential fasync order
  431. n_chips = 0;
  432. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  433. {
  434. thr = proc->thr[0];
  435. bitfury = proc->device_data;
  436. should_be_running = (proc->deven == DEV_ENABLED && !thr->pause);
  437. if (should_be_running)
  438. {
  439. if (spi != bitfury->spi)
  440. {
  441. if (spi)
  442. spi_txrx(spi);
  443. spi = bitfury->spi;
  444. spi_clear_buf(spi);
  445. spi_emit_break(spi);
  446. lastchip = 0;
  447. }
  448. procs[n_chips] = proc;
  449. spi_emit_fasync(spi, bitfury->fasync - lastchip);
  450. lastchip = bitfury->fasync;
  451. rxbuf[n_chips] = spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  452. ++n_chips;
  453. }
  454. else
  455. if (thr->work /* is currently running */ && thr->busy_state != TBS_STARTING_JOB)
  456. ;//FIXME: shutdown chip
  457. }
  458. spi_txrx(spi);
  459. for (j = 0; j < n_chips; ++j)
  460. {
  461. proc = procs[j];
  462. thr = proc->thr[0];
  463. bitfury = proc->device_data;
  464. uint32_t * const newbuf = &bitfury->newbuf[0];
  465. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  466. inp = rxbuf[j];
  467. if (unlikely(bitfury->desync_counter == 99))
  468. {
  469. bitfury_init_oldbuf(proc, inp);
  470. goto out;
  471. }
  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, inp);
  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. };