driver-bitfury.c 14 KB

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