driver-bitfury.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 "fpgautils.h"
  28. #include "libbitfury.h"
  29. #include "util.h"
  30. #include "spidevc.h"
  31. #define GOLDEN_BACKLOG 5
  32. struct device_drv bitfury_drv;
  33. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now);
  34. double shares_to_ghashes(int shares, int seconds);
  35. static
  36. int bitfury_autodetect()
  37. {
  38. RUNONCE(0);
  39. int chip_n;
  40. struct cgpu_info *bitfury_info;
  41. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  42. bitfury_info->drv = &bitfury_drv;
  43. bitfury_info->threads = 1;
  44. applog(LOG_INFO, "INFO: bitfury_detect");
  45. spi_init();
  46. if (!sys_spi)
  47. return 0;
  48. chip_n = libbitfury_detectChips1(sys_spi);
  49. if (!chip_n) {
  50. applog(LOG_WARNING, "No Bitfury chips detected!");
  51. return 0;
  52. } else {
  53. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  54. }
  55. bitfury_info->procs = chip_n;
  56. add_cgpu(bitfury_info);
  57. return 1;
  58. }
  59. static void bitfury_detect(void)
  60. {
  61. noserial_detect_manual(&bitfury_drv, bitfury_autodetect);
  62. }
  63. void *bitfury_just_io(struct bitfury_device * const bitfury)
  64. {
  65. struct spi_port * const spi = bitfury->spi;
  66. const int chip = bitfury->fasync;
  67. spi_clear_buf(spi);
  68. spi_emit_break(spi);
  69. spi_emit_fasync(spi, chip);
  70. spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  71. spi_txrx(spi);
  72. return spi_getrxbuf(spi) + 4 + chip;
  73. }
  74. extern unsigned decnonce(unsigned);
  75. static
  76. void bitfury_debug_nonce_array(char *sp, const struct bitfury_device * const bitfury, const uint32_t * const newbuf, const int active)
  77. {
  78. const int divide = 0x10 - active;
  79. for (int i = 0; i < 0x10; ++i)
  80. sp += sprintf(sp, "%c%08lx",
  81. (divide == i) ? ';' : ' ',
  82. (unsigned long)newbuf[i]);
  83. }
  84. bool bitfury_init_oldbuf(struct cgpu_info * const proc)
  85. {
  86. struct bitfury_device * const bitfury = proc->device_data;
  87. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  88. uint32_t * const buf = &bitfury->newbuf[0];
  89. const uint32_t *inp;
  90. int i, differ, tried = 0;
  91. inp = bitfury_just_io(bitfury);
  92. tryagain:
  93. if (tried > 3)
  94. {
  95. applog(LOG_ERR, "%"PRIpreprv": %s: Giving up after %d tries",
  96. proc->proc_repr, __func__, tried);
  97. return false;
  98. }
  99. ++tried;
  100. memcpy(buf, inp, 0x10 * 4);
  101. inp = bitfury_just_io(bitfury);
  102. differ = -1;
  103. for (i = 0; i < 0x10; ++i)
  104. {
  105. if (inp[i] != buf[i])
  106. {
  107. if (differ != -1)
  108. {
  109. applog(LOG_DEBUG, "%"PRIpreprv": %s: Second differ at %d; trying again",
  110. proc->proc_repr, __func__, i);
  111. goto tryagain;
  112. }
  113. differ = i;
  114. applog(LOG_DEBUG, "%"PRIpreprv": %s: Differ at %d",
  115. proc->proc_repr, __func__, i);
  116. }
  117. }
  118. if (-1 == differ)
  119. {
  120. applog(LOG_DEBUG, "%"PRIpreprv": %s: No differ found; trying again",
  121. proc->proc_repr, __func__);
  122. goto tryagain;
  123. }
  124. bitfury->active = differ;
  125. for (i = 0; i < 0x10; ++i)
  126. oldbuf[i] = decnonce(inp[(bitfury->active + i) % 0x10]);
  127. bitfury->oldjob = inp[0x10];
  128. if (opt_debug)
  129. {
  130. char s[((1 + 8) * 0x10) + 1];
  131. bitfury_debug_nonce_array(s, bitfury, oldbuf, bitfury->active);
  132. applog(LOG_DEBUG, "%"PRIpreprv": Init%s (job=%08lx)",
  133. proc->proc_repr, s, (unsigned long)inp[0x10]);
  134. }
  135. return true;
  136. }
  137. static
  138. bool bitfury_init(struct thr_info *thr)
  139. {
  140. struct cgpu_info *proc;
  141. struct bitfury_device *bitfury;
  142. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  143. {
  144. bitfury = proc->device_data = malloc(sizeof(struct bitfury_device));
  145. *bitfury = (struct bitfury_device){
  146. .spi = sys_spi,
  147. .fasync = proc->proc_id,
  148. };
  149. bitfury_init_oldbuf(proc);
  150. }
  151. return true;
  152. }
  153. int64_t bitfury_scanHash(struct thr_info *thr)
  154. {
  155. struct cgpu_info * const cgpu = thr->cgpu;
  156. struct bitfury_device * const sds = cgpu->device_data;
  157. struct cgpu_info *proc;
  158. struct thr_info *pthr;
  159. struct bitfury_device *bitfury;
  160. struct timeval now;
  161. char line[2048];
  162. int short_stat = 10;
  163. int long_stat = 1800;
  164. int i;
  165. if (!sds->first)
  166. {
  167. // TODO: Move to init
  168. for (proc = cgpu; proc; proc = proc->next_proc)
  169. {
  170. bitfury = proc->device_data;
  171. bitfury->osc6_bits = 54;
  172. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  173. }
  174. }
  175. sds->first = 1;
  176. for (proc = cgpu; proc; proc = proc->next_proc)
  177. {
  178. const int chip = proc->proc_id;
  179. pthr = proc->thr[0];
  180. bitfury = proc->device_data;
  181. bitfury->job_switched = 0;
  182. if(!bitfury->work) {
  183. bitfury->work = get_queued(thr->cgpu);
  184. if (bitfury->work == NULL)
  185. return 0;
  186. work_to_payload(&bitfury->payload, bitfury->work);
  187. }
  188. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  189. libbitfury_sendHashData1(chip, bitfury, pthr);
  190. }
  191. cgsleep_ms(5);
  192. cgtime(&now);
  193. for (proc = cgpu; proc; proc = proc->next_proc)
  194. {
  195. pthr = proc->thr[0];
  196. bitfury = proc->device_data;
  197. if (bitfury->job_switched) {
  198. int i,j;
  199. unsigned int * const res = bitfury->results;
  200. struct work * const work = bitfury->work;
  201. struct work * const owork = bitfury->owork;
  202. struct work * const o2work = bitfury->o2work;
  203. i = bitfury->results_n;
  204. for (j = i - 1; j >= 0; j--) {
  205. if (owork) {
  206. submit_nonce(pthr, owork, bswap_32(res[j]));
  207. bitfury->stat_ts[bitfury->stat_counter++] =
  208. now.tv_sec;
  209. if (bitfury->stat_counter == BITFURY_STAT_N) {
  210. bitfury->stat_counter = 0;
  211. }
  212. }
  213. if (o2work) {
  214. // TEST
  215. //submit_nonce(pthr, owork, bswap_32(res[j]));
  216. }
  217. }
  218. bitfury->results_n = 0;
  219. bitfury->job_switched = 0;
  220. if (bitfury->old_nonce && o2work) {
  221. submit_nonce(pthr, o2work, bswap_32(bitfury->old_nonce));
  222. i++;
  223. }
  224. if (bitfury->future_nonce) {
  225. submit_nonce(pthr, work, bswap_32(bitfury->future_nonce));
  226. i++;
  227. }
  228. if (o2work)
  229. work_completed(cgpu, o2work);
  230. bitfury->o2work = bitfury->owork;
  231. bitfury->owork = bitfury->work;
  232. bitfury->work = NULL;
  233. hashes_done2(pthr, 0xbd000000, NULL);
  234. }
  235. }
  236. if (now.tv_sec - sds->short_out_t > short_stat) {
  237. int shares_first = 0, shares_last = 0, shares_total = 0;
  238. char stat_lines[32][256] = {{0}};
  239. int len, k;
  240. double gh[32][8] = {{0}};
  241. double ghsum = 0, gh1h = 0, gh2h = 0;
  242. for (proc = cgpu; proc; proc = proc->next_proc)
  243. {
  244. const int chip = proc->proc_id;
  245. bitfury = proc->device_data;
  246. int shares_found = calc_stat(bitfury->stat_ts, short_stat, now);
  247. double ghash;
  248. len = strlen(stat_lines[bitfury->slot]);
  249. ghash = shares_to_ghashes(shares_found, short_stat);
  250. gh[bitfury->slot][chip & 0x07] = ghash;
  251. snprintf(stat_lines[bitfury->slot] + len, 256 - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  252. if(sds->short_out_t && ghash < 1.0) {
  253. applog(LOG_WARNING, "Chip_id %d FREQ CHANGE", chip);
  254. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits - 1);
  255. cgsleep_ms(1);
  256. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  257. }
  258. shares_total += shares_found;
  259. shares_first += chip < 4 ? shares_found : 0;
  260. shares_last += chip > 3 ? shares_found : 0;
  261. }
  262. sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
  263. applog(LOG_WARNING, "%s", line);
  264. for(i = 0; i < 32; i++)
  265. if(strlen(stat_lines[i])) {
  266. len = strlen(stat_lines[i]);
  267. ghsum = 0;
  268. gh1h = 0;
  269. gh2h = 0;
  270. for(k = 0; k < 4; k++) {
  271. gh1h += gh[i][k];
  272. gh2h += gh[i][k+4];
  273. ghsum += gh[i][k] + gh[i][k+4];
  274. }
  275. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  276. applog(LOG_WARNING, "%s", stat_lines[i]);
  277. }
  278. sds->short_out_t = now.tv_sec;
  279. }
  280. if (now.tv_sec - sds->long_out_t > long_stat) {
  281. int shares_first = 0, shares_last = 0, shares_total = 0;
  282. char stat_lines[32][256] = {{0}};
  283. int len, k;
  284. double gh[32][8] = {{0}};
  285. double ghsum = 0, gh1h = 0, gh2h = 0;
  286. for (proc = cgpu; proc; proc = proc->next_proc)
  287. {
  288. const int chip = proc->proc_id;
  289. bitfury = proc->device_data;
  290. int shares_found = calc_stat(bitfury->stat_ts, long_stat, now);
  291. double ghash;
  292. len = strlen(stat_lines[bitfury->slot]);
  293. ghash = shares_to_ghashes(shares_found, long_stat);
  294. gh[bitfury->slot][chip & 0x07] = ghash;
  295. snprintf(stat_lines[bitfury->slot] + len, 256 - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  296. shares_total += shares_found;
  297. shares_first += chip < 4 ? shares_found : 0;
  298. shares_last += chip > 3 ? shares_found : 0;
  299. }
  300. sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
  301. applog(LOG_WARNING, "%s", line);
  302. for(i = 0; i < 32; i++)
  303. if(strlen(stat_lines[i])) {
  304. len = strlen(stat_lines[i]);
  305. ghsum = 0;
  306. gh1h = 0;
  307. gh2h = 0;
  308. for(k = 0; k < 4; k++) {
  309. gh1h += gh[i][k];
  310. gh2h += gh[i][k+4];
  311. ghsum += gh[i][k] + gh[i][k+4];
  312. }
  313. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  314. applog(LOG_WARNING, "%s", stat_lines[i]);
  315. }
  316. sds->long_out_t = now.tv_sec;
  317. }
  318. return 0;
  319. }
  320. double shares_to_ghashes(int shares, int seconds) {
  321. return (double)shares / (double)seconds * 4.84387; //orig: 4.77628
  322. }
  323. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
  324. int j;
  325. int shares_found = 0;
  326. for(j = 0; j < BITFURY_STAT_N; j++) {
  327. if (now.tv_sec - stat_ts[j] < stat) {
  328. shares_found++;
  329. }
  330. }
  331. return shares_found;
  332. }
  333. bool bitfury_prepare(struct thr_info *thr)
  334. {
  335. struct cgpu_info *cgpu = thr->cgpu;
  336. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  337. applog(LOG_INFO, "INFO bitfury_prepare");
  338. return true;
  339. }
  340. void bitfury_shutdown(struct thr_info *thr) {
  341. struct cgpu_info *cgpu = thr->cgpu, *proc;
  342. struct bitfury_device *bitfury;
  343. applog(LOG_INFO, "INFO bitfury_shutdown");
  344. for (proc = cgpu; proc; proc = proc->next_proc)
  345. {
  346. bitfury = proc->device_data;
  347. send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  348. }
  349. }
  350. bool bitfury_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  351. {
  352. struct cgpu_info * const proc = thr->cgpu;
  353. struct bitfury_device * const bitfury = proc->device_data;
  354. if (opt_debug)
  355. {
  356. char hex[153];
  357. bin2hex(hex, &work->data[0], 76);
  358. applog(LOG_DEBUG, "%"PRIpreprv": Preparing work %s",
  359. proc->proc_repr, hex);
  360. }
  361. work_to_payload(&bitfury->payload, work);
  362. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  363. work->blk.nonce = 0xffffffff;
  364. return true;
  365. }
  366. static
  367. bool fudge_nonce(struct work * const work, uint32_t *nonce_p) {
  368. static const uint32_t offsets[] = {0, 0xffc00000, 0xff800000, 0x02800000, 0x02C00000, 0x00400000};
  369. uint32_t nonce;
  370. int i;
  371. if (unlikely(!work))
  372. return false;
  373. for (i = 0; i < 6; ++i)
  374. {
  375. nonce = *nonce_p + offsets[i];
  376. if (test_nonce(work, nonce, false))
  377. {
  378. *nonce_p = nonce;
  379. return true;
  380. }
  381. }
  382. return false;
  383. }
  384. void bitfury_do_io(struct thr_info *thr)
  385. {
  386. struct cgpu_info * const proc = thr->cgpu;
  387. struct bitfury_device * const bitfury = proc->device_data;
  388. const uint32_t *inp;
  389. uint32_t * const newbuf = &bitfury->newbuf[0];
  390. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  391. int n, i;
  392. bool newjob;
  393. uint32_t nonce;
  394. inp = bitfury_just_io(bitfury);
  395. // To avoid dealing with wrap-around entirely, we rotate array so previous active uint32_t is at index 0
  396. for (i = 0; i < 0x10; ++i)
  397. newbuf[i] = decnonce(inp[(bitfury->active + i) % 0x10]);
  398. newjob = inp[0x10];
  399. if (opt_debug)
  400. {
  401. char s[((1 + 8) * 0x10) + 1];
  402. bitfury_debug_nonce_array(s, bitfury, newbuf, bitfury->active);
  403. applog(LOG_DEBUG, "%"PRIpreprv": Read%s (job=%08lx)",
  404. proc->proc_repr, s, (unsigned long)inp[0x10]);
  405. }
  406. if (newbuf[0xf] != oldbuf[0xf])
  407. {
  408. inc_hw_errors2(thr, NULL, NULL);
  409. applog(LOG_DEBUG, "%"PRIpreprv": Previous nonce mismatch, ignoring response",
  410. proc->proc_repr);
  411. goto out;
  412. }
  413. if (bitfury->oldjob != newjob && thr->next_work)
  414. {
  415. mt_job_transition(thr);
  416. // TODO: Delay morework until right before it's needed
  417. timer_set_now(&thr->tv_morework);
  418. job_start_complete(thr);
  419. }
  420. for (n = 0; newbuf[n] == oldbuf[n]; ++n)
  421. {
  422. if (unlikely(n >= 0xf))
  423. {
  424. inc_hw_errors2(thr, NULL, NULL);
  425. applog(LOG_DEBUG, "%"PRIpreprv": Full result match, ignoring response",
  426. proc->proc_repr);
  427. goto out;
  428. }
  429. }
  430. if (n)
  431. {
  432. for (i = 0; i < n; ++i)
  433. {
  434. nonce = newbuf[i];
  435. if (fudge_nonce(thr->work, &nonce))
  436. {
  437. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (work=%p)",
  438. proc->proc_repr, i, (unsigned long)nonce, thr->work);
  439. submit_nonce(thr, thr->work, nonce);
  440. }
  441. else
  442. if (fudge_nonce(thr->prev_work, &nonce))
  443. {
  444. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (prev work=%p)",
  445. proc->proc_repr, i, (unsigned long)nonce, thr->prev_work);
  446. submit_nonce(thr, thr->prev_work, nonce);
  447. }
  448. else
  449. inc_hw_errors(thr, thr->work, nonce);
  450. }
  451. bitfury->active = (bitfury->active + n) % 0x10;
  452. }
  453. memcpy(&oldbuf[0], &newbuf[n], 4 * (0x10 - n));
  454. memcpy(&oldbuf[0x10 - n], &newbuf[0], 4 * n);
  455. bitfury->oldjob = newjob;
  456. out:
  457. timer_set_delay_from_now(&thr->tv_poll, 10000);
  458. }
  459. int64_t bitfury_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  460. {
  461. if (unlikely(stopping))
  462. timer_unset(&thr->tv_poll);
  463. // Bitfury chips process only 768/1024 of the nonce range
  464. return 0xbd000000;
  465. }
  466. struct device_drv bitfury_drv = {
  467. .dname = "bitfury_gpio",
  468. .name = "BFY",
  469. .drv_detect = bitfury_detect,
  470. .thread_prepare = bitfury_prepare,
  471. .thread_init = bitfury_init,
  472. .scanwork = bitfury_scanHash,
  473. .thread_shutdown = bitfury_shutdown,
  474. .minerloop = hash_queued_work,
  475. };