driver-bitfury.c 19 KB

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