driver-bitfury.c 14 KB

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