driver-bitfury.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 <limits.h>
  25. #include "miner.h"
  26. #include <unistd.h>
  27. #include <sha2.h>
  28. #include "deviceapi.h"
  29. #include "libbitfury.h"
  30. #include "util.h"
  31. #include "spidevc.h"
  32. #define GOLDEN_BACKLOG 5
  33. #define LINE_LEN 2048
  34. struct device_drv bitfury_drv;
  35. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now);
  36. double shares_to_ghashes(int shares, int seconds);
  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. }
  127. }
  128. if (-1 == differ)
  129. {
  130. applog(LOG_DEBUG, "%"PRIpreprv": %s: No differ found; trying again",
  131. proc->proc_repr, __func__);
  132. goto tryagain;
  133. }
  134. bitfury->active = differ;
  135. memcpy(&oldbuf[0], &inp[bitfury->active], 4 * (0x10 - bitfury->active));
  136. memcpy(&oldbuf[0x10 - bitfury->active], &inp[0], 4 * bitfury->active);
  137. bitfury->oldjob = inp[0x10];
  138. bitfury->desync_counter = 0;
  139. if (opt_debug)
  140. bitfury_debug_nonce_array(proc, "Init", inp);
  141. return true;
  142. }
  143. bool bitfury_init_chip(struct cgpu_info * const proc)
  144. {
  145. struct bitfury_device * const bitfury = proc->device_data;
  146. struct bitfury_payload payload = {
  147. .midstate = "\xf9\x9a\xf0\xd5\x72\x34\x41\xdc\x9e\x10\xd1\x1f\xeb\xcd\xe3\xf5"
  148. "\x52\xf1\x14\x63\x06\x14\xd1\x12\x15\x25\x39\xd1\x7d\x77\x5a\xfd",
  149. .m7 = 0xafbd0b42,
  150. .ntime = 0xb6c24563,
  151. .nbits = 0x6dfa4352,
  152. };
  153. payload_to_atrvec(bitfury->atrvec, &payload);
  154. return bitfury_init_oldbuf(proc, NULL);
  155. }
  156. static
  157. bool bitfury_init(struct thr_info *thr)
  158. {
  159. struct cgpu_info *proc;
  160. struct bitfury_device *bitfury;
  161. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  162. {
  163. bitfury = proc->device_data = malloc(sizeof(struct bitfury_device));
  164. *bitfury = (struct bitfury_device){
  165. .spi = sys_spi,
  166. .fasync = proc->proc_id,
  167. };
  168. bitfury_init_chip(proc);
  169. }
  170. return true;
  171. }
  172. static
  173. bool bitfury_queue_full(struct cgpu_info *cgpu)
  174. {
  175. struct cgpu_info *proc;
  176. struct bitfury_device *bitfury;
  177. for (proc = cgpu; proc; proc = proc->next_proc)
  178. {
  179. bitfury = proc->device_data;
  180. if (bitfury->work)
  181. continue;
  182. bitfury->work = get_queued(cgpu);
  183. if (!bitfury->work)
  184. return false;
  185. work_to_payload(&bitfury->payload, bitfury->work);
  186. }
  187. return true;
  188. }
  189. int64_t bitfury_scanHash(struct thr_info *thr)
  190. {
  191. struct cgpu_info * const cgpu = thr->cgpu;
  192. struct bitfury_device * const sds = cgpu->device_data;
  193. struct cgpu_info *proc;
  194. struct thr_info *pthr;
  195. struct bitfury_device *bitfury;
  196. struct timeval now;
  197. char line[LINE_LEN];
  198. int short_stat = 10;
  199. int long_stat = 1800;
  200. int i;
  201. if (!bitfury_queue_full(cgpu))
  202. return 0;
  203. for (proc = cgpu; proc; proc = proc->next_proc)
  204. {
  205. const int chip = proc->proc_id;
  206. pthr = proc->thr[0];
  207. bitfury = proc->device_data;
  208. bitfury->job_switched = 0;
  209. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  210. libbitfury_sendHashData1(chip, bitfury, pthr);
  211. }
  212. cgsleep_ms(5);
  213. cgtime(&now);
  214. for (proc = cgpu; proc; proc = proc->next_proc)
  215. {
  216. pthr = proc->thr[0];
  217. bitfury = proc->device_data;
  218. if (bitfury->job_switched) {
  219. int i,j;
  220. unsigned int * const res = bitfury->results;
  221. struct work * const work = bitfury->work;
  222. struct work * const owork = bitfury->owork;
  223. struct work * const o2work = bitfury->o2work;
  224. i = bitfury->results_n;
  225. for (j = i - 1; j >= 0; j--) {
  226. if (owork) {
  227. submit_nonce(pthr, owork, bswap_32(res[j]));
  228. bitfury->stat_ts[bitfury->stat_counter++] =
  229. now.tv_sec;
  230. if (bitfury->stat_counter == BITFURY_STAT_N) {
  231. bitfury->stat_counter = 0;
  232. }
  233. }
  234. if (o2work) {
  235. // TEST
  236. //submit_nonce(pthr, owork, bswap_32(res[j]));
  237. }
  238. }
  239. bitfury->results_n = 0;
  240. bitfury->job_switched = 0;
  241. if (bitfury->old_nonce && o2work) {
  242. submit_nonce(pthr, o2work, bswap_32(bitfury->old_nonce));
  243. i++;
  244. }
  245. if (bitfury->future_nonce) {
  246. submit_nonce(pthr, work, bswap_32(bitfury->future_nonce));
  247. i++;
  248. }
  249. if (o2work)
  250. work_completed(cgpu, o2work);
  251. bitfury->o2work = bitfury->owork;
  252. bitfury->owork = bitfury->work;
  253. bitfury->work = NULL;
  254. hashes_done2(pthr, 0xbd000000, NULL);
  255. }
  256. }
  257. if (now.tv_sec - sds->short_out_t > short_stat) {
  258. int shares_first = 0, shares_last = 0, shares_total = 0;
  259. char stat_lines[32][LINE_LEN] = {{0}};
  260. int len, k;
  261. double gh[32][8] = {{0}};
  262. double ghsum = 0, gh1h = 0, gh2h = 0;
  263. unsigned strange_counter = 0;
  264. for (proc = cgpu; proc; proc = proc->next_proc)
  265. {
  266. const int chip = proc->proc_id;
  267. bitfury = proc->device_data;
  268. int shares_found = calc_stat(bitfury->stat_ts, short_stat, now);
  269. double ghash;
  270. len = strlen(stat_lines[bitfury->slot]);
  271. ghash = shares_to_ghashes(shares_found, short_stat);
  272. gh[bitfury->slot][chip & 0x07] = ghash;
  273. snprintf(stat_lines[bitfury->slot] + len, LINE_LEN - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  274. if(sds->short_out_t && ghash < 0.5) {
  275. applog(LOG_WARNING, "Chip_id %d FREQ CHANGE", chip);
  276. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits - 1);
  277. cgsleep_ms(1);
  278. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  279. }
  280. shares_total += shares_found;
  281. shares_first += chip < 4 ? shares_found : 0;
  282. shares_last += chip > 3 ? shares_found : 0;
  283. strange_counter += bitfury->strange_counter;
  284. bitfury->strange_counter = 0;
  285. }
  286. sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
  287. applog(LOG_WARNING, "%s", line);
  288. sprintf(line, "stranges: %u", strange_counter);
  289. applog(LOG_WARNING, "%s", line);
  290. for(i = 0; i < 32; i++)
  291. if(strlen(stat_lines[i])) {
  292. len = strlen(stat_lines[i]);
  293. ghsum = 0;
  294. gh1h = 0;
  295. gh2h = 0;
  296. for(k = 0; k < 4; k++) {
  297. gh1h += gh[i][k];
  298. gh2h += gh[i][k+4];
  299. ghsum += gh[i][k] + gh[i][k+4];
  300. }
  301. snprintf(stat_lines[i] + len, LINE_LEN - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  302. applog(LOG_WARNING, "%s", stat_lines[i]);
  303. }
  304. sds->short_out_t = now.tv_sec;
  305. }
  306. if (now.tv_sec - sds->long_out_t > long_stat) {
  307. int shares_first = 0, shares_last = 0, shares_total = 0;
  308. char stat_lines[32][LINE_LEN] = {{0}};
  309. int len, k;
  310. double gh[32][8] = {{0}};
  311. double ghsum = 0, gh1h = 0, gh2h = 0;
  312. for (proc = cgpu; proc; proc = proc->next_proc)
  313. {
  314. const int chip = proc->proc_id;
  315. bitfury = proc->device_data;
  316. int shares_found = calc_stat(bitfury->stat_ts, long_stat, now);
  317. double ghash;
  318. len = strlen(stat_lines[bitfury->slot]);
  319. ghash = shares_to_ghashes(shares_found, long_stat);
  320. gh[bitfury->slot][chip & 0x07] = ghash;
  321. snprintf(stat_lines[bitfury->slot] + len, LINE_LEN - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  322. shares_total += shares_found;
  323. shares_first += chip < 4 ? shares_found : 0;
  324. shares_last += chip > 3 ? shares_found : 0;
  325. }
  326. sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
  327. applog(LOG_WARNING, "%s", line);
  328. for(i = 0; i < 32; i++)
  329. if(strlen(stat_lines[i])) {
  330. len = strlen(stat_lines[i]);
  331. ghsum = 0;
  332. gh1h = 0;
  333. gh2h = 0;
  334. for(k = 0; k < 4; k++) {
  335. gh1h += gh[i][k];
  336. gh2h += gh[i][k+4];
  337. ghsum += gh[i][k] + gh[i][k+4];
  338. }
  339. snprintf(stat_lines[i] + len, LINE_LEN - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  340. applog(LOG_WARNING, "%s", stat_lines[i]);
  341. }
  342. sds->long_out_t = now.tv_sec;
  343. }
  344. return 0;
  345. }
  346. double shares_to_ghashes(int shares, int seconds) {
  347. return (double)shares / (double)seconds * 4.84387; //orig: 4.77628
  348. }
  349. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
  350. int j;
  351. int shares_found = 0;
  352. for(j = 0; j < BITFURY_STAT_N; j++) {
  353. if (now.tv_sec - stat_ts[j] < stat) {
  354. shares_found++;
  355. }
  356. }
  357. return shares_found;
  358. }
  359. bool bitfury_prepare(struct thr_info *thr)
  360. {
  361. struct cgpu_info *cgpu = thr->cgpu;
  362. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  363. applog(LOG_INFO, "INFO bitfury_prepare");
  364. return true;
  365. }
  366. void bitfury_shutdown(struct thr_info *thr) {
  367. struct cgpu_info *cgpu = thr->cgpu, *proc;
  368. struct bitfury_device *bitfury;
  369. applog(LOG_INFO, "INFO bitfury_shutdown");
  370. for (proc = cgpu; proc; proc = proc->next_proc)
  371. {
  372. bitfury = proc->device_data;
  373. send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  374. }
  375. }
  376. bool bitfury_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  377. {
  378. struct cgpu_info * const proc = thr->cgpu;
  379. struct bitfury_device * const bitfury = proc->device_data;
  380. if (opt_debug)
  381. {
  382. char hex[153];
  383. bin2hex(hex, &work->data[0], 76);
  384. applog(LOG_DEBUG, "%"PRIpreprv": Preparing work %s",
  385. proc->proc_repr, hex);
  386. }
  387. work_to_payload(&bitfury->payload, work);
  388. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  389. work->blk.nonce = 0xffffffff;
  390. return true;
  391. }
  392. static
  393. bool fudge_nonce(struct work * const work, uint32_t *nonce_p) {
  394. static const uint32_t offsets[] = {0, 0xffc00000, 0xff800000, 0x02800000, 0x02C00000, 0x00400000};
  395. uint32_t nonce;
  396. int i;
  397. if (unlikely(!work))
  398. return false;
  399. for (i = 0; i < 6; ++i)
  400. {
  401. nonce = *nonce_p + offsets[i];
  402. if (test_nonce(work, nonce, false))
  403. {
  404. *nonce_p = nonce;
  405. return true;
  406. }
  407. }
  408. return false;
  409. }
  410. void bitfury_noop_job_start(struct thr_info __maybe_unused * const thr)
  411. {
  412. }
  413. void bitfury_do_io(struct thr_info * const master_thr)
  414. {
  415. struct cgpu_info *proc;
  416. struct thr_info *thr;
  417. struct bitfury_device *bitfury;
  418. const uint32_t *inp;
  419. int n, i, j;
  420. bool newjob;
  421. uint32_t nonce;
  422. int n_chips = 0, lastchip;
  423. struct spi_port *spi = NULL;
  424. bool should_be_running;
  425. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  426. ++n_chips;
  427. struct cgpu_info *procs[n_chips];
  428. void *rxbuf[n_chips];
  429. // NOTE: This code assumes:
  430. // 1) that chips on the same SPI bus are grouped together
  431. // 2) that chips are in sequential fasync order
  432. n_chips = 0;
  433. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  434. {
  435. thr = proc->thr[0];
  436. bitfury = proc->device_data;
  437. should_be_running = (proc->deven == DEV_ENABLED && !thr->pause);
  438. if (should_be_running)
  439. {
  440. if (spi != bitfury->spi)
  441. {
  442. if (spi)
  443. spi_txrx(spi);
  444. spi = bitfury->spi;
  445. spi_clear_buf(spi);
  446. spi_emit_break(spi);
  447. lastchip = 0;
  448. }
  449. procs[n_chips] = proc;
  450. spi_emit_fasync(spi, bitfury->fasync - lastchip);
  451. lastchip = bitfury->fasync;
  452. rxbuf[n_chips] = spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  453. ++n_chips;
  454. }
  455. else
  456. if (thr->work /* is currently running */ && thr->busy_state != TBS_STARTING_JOB)
  457. ;//FIXME: shutdown chip
  458. }
  459. spi_txrx(spi);
  460. for (j = 0; j < n_chips; ++j)
  461. {
  462. proc = procs[j];
  463. thr = proc->thr[0];
  464. bitfury = proc->device_data;
  465. uint32_t * const newbuf = &bitfury->newbuf[0];
  466. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  467. inp = rxbuf[j];
  468. if (unlikely(bitfury->desync_counter == 99))
  469. {
  470. bitfury_init_oldbuf(proc, inp);
  471. goto out;
  472. }
  473. if (opt_debug)
  474. bitfury_debug_nonce_array(proc, "Read", inp);
  475. // To avoid dealing with wrap-around entirely, we rotate array so previous active uint32_t is at index 0
  476. memcpy(&newbuf[0], &inp[bitfury->active], 4 * (0x10 - bitfury->active));
  477. memcpy(&newbuf[0x10 - bitfury->active], &inp[0], 4 * bitfury->active);
  478. newjob = inp[0x10];
  479. if (newbuf[0xf] != oldbuf[0xf])
  480. {
  481. inc_hw_errors2(thr, NULL, NULL);
  482. if (unlikely(++bitfury->desync_counter >= 4))
  483. {
  484. applog(LOG_WARNING, "%"PRIpreprv": Previous nonce mismatch (4th try), recalibrating",
  485. proc->proc_repr);
  486. bitfury_init_oldbuf(proc, inp);
  487. continue;
  488. }
  489. applog(LOG_DEBUG, "%"PRIpreprv": Previous nonce mismatch, ignoring response",
  490. proc->proc_repr);
  491. goto out;
  492. }
  493. else
  494. bitfury->desync_counter = 0;
  495. if (bitfury->oldjob != newjob && thr->next_work)
  496. {
  497. mt_job_transition(thr);
  498. // TODO: Delay morework until right before it's needed
  499. timer_set_now(&thr->tv_morework);
  500. job_start_complete(thr);
  501. }
  502. for (n = 0; newbuf[n] == oldbuf[n]; ++n)
  503. {
  504. if (unlikely(n >= 0xf))
  505. {
  506. inc_hw_errors2(thr, NULL, NULL);
  507. applog(LOG_DEBUG, "%"PRIpreprv": Full result match, reinitialising",
  508. proc->proc_repr);
  509. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  510. bitfury->desync_counter = 99;
  511. goto out;
  512. }
  513. }
  514. if (n)
  515. {
  516. for (i = 0; i < n; ++i)
  517. {
  518. nonce = bitfury_decnonce(newbuf[i]);
  519. if (fudge_nonce(thr->work, &nonce))
  520. {
  521. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (work=%p)",
  522. proc->proc_repr, i, (unsigned long)nonce, thr->work);
  523. submit_nonce(thr, thr->work, nonce);
  524. }
  525. else
  526. if (fudge_nonce(thr->prev_work, &nonce))
  527. {
  528. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (prev work=%p)",
  529. proc->proc_repr, i, (unsigned long)nonce, thr->prev_work);
  530. submit_nonce(thr, thr->prev_work, nonce);
  531. }
  532. else
  533. {
  534. inc_hw_errors(thr, thr->work, nonce);
  535. ++bitfury->sample_hwe;
  536. }
  537. if (++bitfury->sample_tot >= 0x40 || bitfury->sample_hwe >= 8)
  538. {
  539. if (bitfury->sample_hwe >= 8)
  540. {
  541. applog(LOG_WARNING, "%"PRIpreprv": %d of the last %d results were bad, reinitialising",
  542. proc->proc_repr, bitfury->sample_hwe, bitfury->sample_tot);
  543. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  544. bitfury->desync_counter = 99;
  545. }
  546. bitfury->sample_tot = bitfury->sample_hwe = 0;
  547. }
  548. }
  549. bitfury->active = (bitfury->active + n) % 0x10;
  550. }
  551. memcpy(&oldbuf[0], &newbuf[n], 4 * (0x10 - n));
  552. memcpy(&oldbuf[0x10 - n], &newbuf[0], 4 * n);
  553. bitfury->oldjob = newjob;
  554. out:
  555. if (unlikely(bitfury->force_reinit))
  556. {
  557. applog(LOG_DEBUG, "%"PRIpreprv": Forcing reinitialisation",
  558. proc->proc_repr);
  559. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  560. bitfury->desync_counter = 99;
  561. }
  562. }
  563. timer_set_delay_from_now(&master_thr->tv_poll, 10000);
  564. }
  565. int64_t bitfury_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  566. {
  567. // Bitfury chips process only 768/1024 of the nonce range
  568. return 0xbd000000;
  569. }
  570. struct api_data *bitfury_api_device_status(const struct cgpu_info * const cgpu)
  571. {
  572. const struct bitfury_device * const bitfury = cgpu->device_data;
  573. struct api_data *root = NULL;
  574. int clock_bits = bitfury->osc6_bits;
  575. root = api_add_int(root, "Clock Bits", &clock_bits, true);
  576. return root;
  577. }
  578. static
  579. bool _bitfury_set_device_parse_setting(int * const rv, char * const setting, char * const replybuf, const int maxval)
  580. {
  581. char *p;
  582. long int nv;
  583. if (!setting || !*setting)
  584. {
  585. sprintf(replybuf, "missing setting");
  586. return false;
  587. }
  588. nv = strtol(setting, &p, 0);
  589. if ((p && p[0]) || nv > maxval || nv < 1)
  590. {
  591. sprintf(replybuf, "invalid setting");
  592. return false;
  593. }
  594. *rv = nv;
  595. return true;
  596. }
  597. char *bitfury_set_device(struct cgpu_info * const proc, char * const option, char * const setting, char * const replybuf)
  598. {
  599. struct bitfury_device * const bitfury = proc->device_data;
  600. int newval;
  601. if (!strcasecmp(option, "help"))
  602. {
  603. sprintf(replybuf, "baud: SPI baud rate");
  604. sprintf(replybuf, "osc6_bits: range 1-55 (slow to fast)");
  605. return replybuf;
  606. }
  607. if (!strcasecmp(option, "baud"))
  608. {
  609. if (!_bitfury_set_device_parse_setting(&bitfury->spi->speed, setting, replybuf, INT_MAX))
  610. return replybuf;
  611. sprintf(replybuf, "baud changed");
  612. return replybuf;
  613. }
  614. if (!strcasecmp(option, "osc6_bits"))
  615. {
  616. newval = bitfury->osc6_bits;
  617. if (!_bitfury_set_device_parse_setting(&newval, setting, replybuf, 55))
  618. return replybuf;
  619. bitfury->osc6_bits = newval;
  620. bitfury->force_reinit = true;
  621. return replybuf;
  622. }
  623. sprintf(replybuf, "Unknown option: %s", option);
  624. return replybuf;
  625. }
  626. struct device_drv bitfury_drv = {
  627. .dname = "bitfury_gpio",
  628. .name = "BFY",
  629. .drv_detect = bitfury_detect,
  630. .thread_prepare = bitfury_prepare,
  631. .thread_init = bitfury_init,
  632. .queue_full = bitfury_queue_full,
  633. .scanwork = bitfury_scanHash,
  634. .thread_shutdown = bitfury_shutdown,
  635. .minerloop = hash_queued_work,
  636. };