driver-bitfury.c 14 KB

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