driver-bitfury.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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 <stdbool.h>
  29. #include <stdint.h>
  30. #include <sha2.h>
  31. #include "deviceapi.h"
  32. #include "driver-bitfury.h"
  33. #include "libbitfury.h"
  34. #include "util.h"
  35. #include "spidevc.h"
  36. #define GOLDEN_BACKLOG 5
  37. #define LINE_LEN 2048
  38. struct device_drv bitfury_drv;
  39. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now);
  40. double shares_to_ghashes(int shares, int seconds);
  41. static
  42. int bitfury_autodetect()
  43. {
  44. RUNONCE(0);
  45. int chip_n;
  46. struct cgpu_info *bitfury_info;
  47. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  48. bitfury_info->drv = &bitfury_drv;
  49. bitfury_info->threads = 1;
  50. applog(LOG_INFO, "INFO: bitfury_detect");
  51. spi_init();
  52. if (!sys_spi)
  53. return 0;
  54. chip_n = libbitfury_detectChips1(sys_spi);
  55. if (!chip_n) {
  56. applog(LOG_WARNING, "No Bitfury chips detected!");
  57. return 0;
  58. } else {
  59. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  60. }
  61. bitfury_info->procs = chip_n;
  62. add_cgpu(bitfury_info);
  63. return 1;
  64. }
  65. static void bitfury_detect(void)
  66. {
  67. noserial_detect_manual(&bitfury_drv, bitfury_autodetect);
  68. }
  69. static
  70. void *bitfury_just_io(struct bitfury_device * const bitfury)
  71. {
  72. struct spi_port * const spi = bitfury->spi;
  73. const int chip = bitfury->fasync;
  74. void *rv;
  75. spi_clear_buf(spi);
  76. spi_emit_break(spi);
  77. spi_emit_fasync(spi, chip);
  78. rv = spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  79. spi_txrx(spi);
  80. return rv;
  81. }
  82. static
  83. void bitfury_debug_nonce_array(const struct cgpu_info * const proc, const char *msg, const uint32_t * const inp)
  84. {
  85. const struct bitfury_device * const bitfury = proc->device_data;
  86. const int active = bitfury->active;
  87. char s[((1 + 8) * 0x10) + 1];
  88. char *sp = s;
  89. for (int i = 0; i < 0x10; ++i)
  90. sp += sprintf(sp, "%c%08lx",
  91. (active == i) ? '>' : ' ',
  92. (unsigned long)bitfury_decnonce(inp[i]));
  93. applog(LOG_DEBUG, "%"PRIpreprv": %s%s (job=%08lx)",
  94. proc->proc_repr, msg, s, (unsigned long)inp[0x10]);
  95. }
  96. static
  97. bool bitfury_init_oldbuf(struct cgpu_info * const proc, const uint32_t *inp)
  98. {
  99. struct bitfury_device * const bitfury = proc->device_data;
  100. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  101. uint32_t * const buf = &bitfury->newbuf[0];
  102. int i, differ, tried = 0;
  103. if (!inp)
  104. inp = bitfury_just_io(bitfury);
  105. tryagain:
  106. if (tried > 3)
  107. {
  108. applog(LOG_ERR, "%"PRIpreprv": %s: Giving up after %d tries",
  109. proc->proc_repr, __func__, tried);
  110. bitfury->desync_counter = 99;
  111. return false;
  112. }
  113. ++tried;
  114. memcpy(buf, inp, 0x10 * 4);
  115. inp = bitfury_just_io(bitfury);
  116. differ = -1;
  117. for (i = 0; i < 0x10; ++i)
  118. {
  119. if (inp[i] != buf[i])
  120. {
  121. if (differ != -1)
  122. {
  123. applog(LOG_DEBUG, "%"PRIpreprv": %s: Second differ at %d; trying again",
  124. proc->proc_repr, __func__, i);
  125. goto tryagain;
  126. }
  127. differ = i;
  128. applog(LOG_DEBUG, "%"PRIpreprv": %s: Differ at %d",
  129. proc->proc_repr, __func__, i);
  130. if (tried > 3)
  131. break;
  132. }
  133. }
  134. if (-1 == differ)
  135. {
  136. applog(LOG_DEBUG, "%"PRIpreprv": %s: No differ found; trying again",
  137. proc->proc_repr, __func__);
  138. goto tryagain;
  139. }
  140. bitfury->active = differ;
  141. memcpy(&oldbuf[0], &inp[bitfury->active], 4 * (0x10 - bitfury->active));
  142. memcpy(&oldbuf[0x10 - bitfury->active], &inp[0], 4 * bitfury->active);
  143. bitfury->oldjob = inp[0x10];
  144. bitfury->desync_counter = 0;
  145. if (opt_debug)
  146. bitfury_debug_nonce_array(proc, "Init", inp);
  147. return true;
  148. }
  149. bool bitfury_init_chip(struct cgpu_info * const proc)
  150. {
  151. struct bitfury_device * const bitfury = proc->device_data;
  152. struct bitfury_payload payload = {
  153. .midstate = "\xf9\x9a\xf0\xd5\x72\x34\x41\xdc\x9e\x10\xd1\x1f\xeb\xcd\xe3\xf5"
  154. "\x52\xf1\x14\x63\x06\x14\xd1\x12\x15\x25\x39\xd1\x7d\x77\x5a\xfd",
  155. .m7 = 0xafbd0b42,
  156. .ntime = 0xb6c24563,
  157. .nbits = 0x6dfa4352,
  158. };
  159. payload_to_atrvec(bitfury->atrvec, &payload);
  160. return bitfury_init_oldbuf(proc, NULL);
  161. }
  162. static
  163. bool bitfury_init(struct thr_info *thr)
  164. {
  165. struct cgpu_info *proc;
  166. struct bitfury_device *bitfury;
  167. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  168. {
  169. bitfury = proc->device_data = malloc(sizeof(struct bitfury_device));
  170. *bitfury = (struct bitfury_device){
  171. .spi = sys_spi,
  172. .fasync = proc->proc_id,
  173. };
  174. bitfury_init_chip(proc);
  175. bitfury->osc6_bits = 50;
  176. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  177. }
  178. timer_set_now(&thr->tv_poll);
  179. return true;
  180. }
  181. static
  182. bool bitfury_queue_full(struct cgpu_info *cgpu)
  183. {
  184. struct cgpu_info *proc;
  185. struct bitfury_device *bitfury;
  186. for (proc = cgpu; proc; proc = proc->next_proc)
  187. {
  188. bitfury = proc->device_data;
  189. if (bitfury->work)
  190. continue;
  191. bitfury->work = get_queued(cgpu);
  192. if (!bitfury->work)
  193. return false;
  194. work_to_payload(&bitfury->payload, bitfury->work);
  195. }
  196. return true;
  197. }
  198. int64_t bitfury_scanHash(struct thr_info *thr)
  199. {
  200. struct cgpu_info * const cgpu = thr->cgpu;
  201. struct bitfury_device * const sds = cgpu->device_data;
  202. struct cgpu_info *proc;
  203. struct thr_info *pthr;
  204. struct bitfury_device *bitfury;
  205. struct timeval now;
  206. char line[LINE_LEN];
  207. int short_stat = 10;
  208. int long_stat = 1800;
  209. int i;
  210. if (!bitfury_queue_full(cgpu))
  211. return 0;
  212. for (proc = cgpu; proc; proc = proc->next_proc)
  213. {
  214. const int chip = proc->proc_id;
  215. pthr = proc->thr[0];
  216. bitfury = proc->device_data;
  217. bitfury->job_switched = 0;
  218. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  219. libbitfury_sendHashData1(chip, bitfury, pthr);
  220. }
  221. cgsleep_ms(5);
  222. cgtime(&now);
  223. for (proc = cgpu; proc; proc = proc->next_proc)
  224. {
  225. pthr = proc->thr[0];
  226. bitfury = proc->device_data;
  227. if (bitfury->job_switched) {
  228. int i,j;
  229. unsigned int * const res = bitfury->results;
  230. struct work * const work = bitfury->work;
  231. struct work * const owork = bitfury->owork;
  232. struct work * const o2work = bitfury->o2work;
  233. i = bitfury->results_n;
  234. for (j = i - 1; j >= 0; j--) {
  235. if (owork) {
  236. submit_nonce(pthr, owork, bswap_32(res[j]));
  237. bitfury->stat_ts[bitfury->stat_counter++] =
  238. now.tv_sec;
  239. if (bitfury->stat_counter == BITFURY_STAT_N) {
  240. bitfury->stat_counter = 0;
  241. }
  242. }
  243. if (o2work) {
  244. // TEST
  245. //submit_nonce(pthr, owork, bswap_32(res[j]));
  246. }
  247. }
  248. bitfury->results_n = 0;
  249. bitfury->job_switched = 0;
  250. if (bitfury->old_nonce && o2work) {
  251. submit_nonce(pthr, o2work, bswap_32(bitfury->old_nonce));
  252. i++;
  253. }
  254. if (bitfury->future_nonce) {
  255. submit_nonce(pthr, work, bswap_32(bitfury->future_nonce));
  256. i++;
  257. }
  258. if (o2work)
  259. work_completed(cgpu, o2work);
  260. bitfury->o2work = bitfury->owork;
  261. bitfury->owork = bitfury->work;
  262. bitfury->work = NULL;
  263. hashes_done2(pthr, 0xbd000000, NULL);
  264. }
  265. }
  266. if (now.tv_sec - sds->short_out_t > short_stat) {
  267. int shares_first = 0, shares_last = 0, shares_total = 0;
  268. char stat_lines[32][LINE_LEN] = {{0}};
  269. int len, k;
  270. double gh[32][8] = {{0}};
  271. double ghsum = 0, gh1h = 0, gh2h = 0;
  272. unsigned strange_counter = 0;
  273. for (proc = cgpu; proc; proc = proc->next_proc)
  274. {
  275. const int chip = proc->proc_id;
  276. bitfury = proc->device_data;
  277. int shares_found = calc_stat(bitfury->stat_ts, short_stat, now);
  278. double ghash;
  279. len = strlen(stat_lines[bitfury->slot]);
  280. ghash = shares_to_ghashes(shares_found, short_stat);
  281. gh[bitfury->slot][chip & 0x07] = ghash;
  282. snprintf(stat_lines[bitfury->slot] + len, LINE_LEN - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  283. if(sds->short_out_t && ghash < 0.5) {
  284. applog(LOG_WARNING, "Chip_id %d FREQ CHANGE", chip);
  285. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits - 1);
  286. cgsleep_ms(1);
  287. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  288. }
  289. shares_total += shares_found;
  290. shares_first += chip < 4 ? shares_found : 0;
  291. shares_last += chip > 3 ? shares_found : 0;
  292. strange_counter += bitfury->strange_counter;
  293. bitfury->strange_counter = 0;
  294. }
  295. sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
  296. applog(LOG_WARNING, "%s", line);
  297. sprintf(line, "stranges: %u", strange_counter);
  298. applog(LOG_WARNING, "%s", line);
  299. for(i = 0; i < 32; i++)
  300. if(strlen(stat_lines[i])) {
  301. len = strlen(stat_lines[i]);
  302. ghsum = 0;
  303. gh1h = 0;
  304. gh2h = 0;
  305. for(k = 0; k < 4; k++) {
  306. gh1h += gh[i][k];
  307. gh2h += gh[i][k+4];
  308. ghsum += gh[i][k] + gh[i][k+4];
  309. }
  310. snprintf(stat_lines[i] + len, LINE_LEN - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  311. applog(LOG_WARNING, "%s", stat_lines[i]);
  312. }
  313. sds->short_out_t = now.tv_sec;
  314. }
  315. if (now.tv_sec - sds->long_out_t > long_stat) {
  316. int shares_first = 0, shares_last = 0, shares_total = 0;
  317. char stat_lines[32][LINE_LEN] = {{0}};
  318. int len, k;
  319. double gh[32][8] = {{0}};
  320. double ghsum = 0, gh1h = 0, gh2h = 0;
  321. for (proc = cgpu; proc; proc = proc->next_proc)
  322. {
  323. const int chip = proc->proc_id;
  324. bitfury = proc->device_data;
  325. int shares_found = calc_stat(bitfury->stat_ts, long_stat, now);
  326. double ghash;
  327. len = strlen(stat_lines[bitfury->slot]);
  328. ghash = shares_to_ghashes(shares_found, long_stat);
  329. gh[bitfury->slot][chip & 0x07] = ghash;
  330. snprintf(stat_lines[bitfury->slot] + len, LINE_LEN - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  331. shares_total += shares_found;
  332. shares_first += chip < 4 ? shares_found : 0;
  333. shares_last += chip > 3 ? shares_found : 0;
  334. }
  335. sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
  336. applog(LOG_WARNING, "%s", line);
  337. for(i = 0; i < 32; i++)
  338. if(strlen(stat_lines[i])) {
  339. len = strlen(stat_lines[i]);
  340. ghsum = 0;
  341. gh1h = 0;
  342. gh2h = 0;
  343. for(k = 0; k < 4; k++) {
  344. gh1h += gh[i][k];
  345. gh2h += gh[i][k+4];
  346. ghsum += gh[i][k] + gh[i][k+4];
  347. }
  348. snprintf(stat_lines[i] + len, LINE_LEN - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  349. applog(LOG_WARNING, "%s", stat_lines[i]);
  350. }
  351. sds->long_out_t = now.tv_sec;
  352. }
  353. return 0;
  354. }
  355. double shares_to_ghashes(int shares, int seconds) {
  356. return (double)shares / (double)seconds * 4.84387; //orig: 4.77628
  357. }
  358. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
  359. int j;
  360. int shares_found = 0;
  361. for(j = 0; j < BITFURY_STAT_N; j++) {
  362. if (now.tv_sec - stat_ts[j] < stat) {
  363. shares_found++;
  364. }
  365. }
  366. return shares_found;
  367. }
  368. void bitfury_shutdown(struct thr_info *thr) {
  369. struct cgpu_info *cgpu = thr->cgpu, *proc;
  370. struct bitfury_device *bitfury;
  371. applog(LOG_INFO, "INFO bitfury_shutdown");
  372. for (proc = cgpu; proc; proc = proc->next_proc)
  373. {
  374. bitfury = proc->device_data;
  375. send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  376. }
  377. }
  378. bool bitfury_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  379. {
  380. struct cgpu_info * const proc = thr->cgpu;
  381. struct bitfury_device * const bitfury = proc->device_data;
  382. if (opt_debug)
  383. {
  384. char hex[153];
  385. bin2hex(hex, &work->data[0], 76);
  386. applog(LOG_DEBUG, "%"PRIpreprv": Preparing work %s",
  387. proc->proc_repr, hex);
  388. }
  389. work_to_payload(&bitfury->payload, work);
  390. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  391. work->blk.nonce = 0xffffffff;
  392. return true;
  393. }
  394. static
  395. bool fudge_nonce(struct work * const work, uint32_t *nonce_p) {
  396. static const uint32_t offsets[] = {0, 0xffc00000, 0xff800000, 0x02800000, 0x02C00000, 0x00400000};
  397. uint32_t nonce;
  398. int i;
  399. if (unlikely(!work))
  400. return false;
  401. for (i = 0; i < 6; ++i)
  402. {
  403. nonce = *nonce_p + offsets[i];
  404. if (test_nonce(work, nonce, false))
  405. {
  406. *nonce_p = nonce;
  407. return true;
  408. }
  409. }
  410. return false;
  411. }
  412. void bitfury_noop_job_start(struct thr_info __maybe_unused * const thr)
  413. {
  414. }
  415. // freq_stat->{mh,s} are allocated such that [osc6_min] is the first valid index and [0] falls outside the allocation
  416. void bitfury_init_freq_stat(struct freq_stat * const c, const int osc6_min, const int osc6_max)
  417. {
  418. const int osc6_values = (osc6_max + 1 - osc6_min);
  419. void * const p = malloc(osc6_values * (sizeof(*c->mh) + sizeof(*c->s)));
  420. c->mh = p - (sizeof(*c->mh) * osc6_min);
  421. c->s = p + (sizeof(*c->mh) * osc6_values) - (sizeof(*c->s) * osc6_min);
  422. c->osc6_min = osc6_min;
  423. c->osc6_max = osc6_max;
  424. }
  425. void bitfury_clean_freq_stat(struct freq_stat * const c)
  426. {
  427. free(&c->mh[c->osc6_min]);
  428. }
  429. #define HOP_DONE 600
  430. typedef uint32_t bitfury_inp_t[0x11];
  431. int select_freq(struct bitfury_device *bitfury, struct cgpu_info *proc) {
  432. int freq;
  433. int random;
  434. int i;
  435. bool all_done;
  436. struct freq_stat *c;
  437. c = &bitfury->chip_stat;
  438. if (c->best_done) {
  439. freq = c->best_osc;
  440. } else {
  441. random = (int)(bitfury->mhz * 1000.0) & 1;
  442. freq = (bitfury->osc6_bits == c->osc6_max) ? c->osc6_min : bitfury->osc6_bits + random;
  443. all_done = true;
  444. for (i = c->osc6_min; i <= c->osc6_max; ++i)
  445. if (c->s[i] <= HOP_DONE)
  446. {
  447. all_done = false;
  448. break;
  449. }
  450. if (all_done)
  451. {
  452. double mh_max = 0.0;
  453. for (i = c->osc6_min; i <= c->osc6_max; ++i)
  454. {
  455. const double mh_actual = c->mh[i] / c->s[i];
  456. if (mh_max >= mh_actual)
  457. continue;
  458. mh_max = mh_actual;
  459. freq = i;
  460. }
  461. c->best_done = 1;
  462. c->best_osc = freq;
  463. applog(LOG_DEBUG, "%"PRIpreprv": best_osc = %d",
  464. proc->proc_repr, freq);
  465. }
  466. }
  467. applog(LOG_DEBUG, "%"PRIpreprv": Changing osc6_bits to %d",
  468. proc->proc_repr, freq);
  469. bitfury->osc6_bits = freq;
  470. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  471. return 0;
  472. }
  473. void bitfury_do_io(struct thr_info * const master_thr)
  474. {
  475. struct cgpu_info *proc;
  476. struct thr_info *thr;
  477. struct bitfury_device *bitfury;
  478. struct freq_stat *c;
  479. const uint32_t *inp;
  480. int n, i, j;
  481. bool newjob;
  482. uint32_t nonce;
  483. int n_chips = 0, lastchip = 0;
  484. struct spi_port *spi = NULL;
  485. bool should_be_running;
  486. struct timeval tv_now;
  487. uint32_t counter;
  488. struct timeval *tvp_stat;
  489. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  490. ++n_chips;
  491. struct cgpu_info *procs[n_chips];
  492. void *rxbuf[n_chips];
  493. bitfury_inp_t rxbuf_copy[n_chips];
  494. // NOTE: This code assumes:
  495. // 1) that chips on the same SPI bus are grouped together
  496. // 2) that chips are in sequential fasync order
  497. n_chips = 0;
  498. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  499. {
  500. thr = proc->thr[0];
  501. bitfury = proc->device_data;
  502. should_be_running = (proc->deven == DEV_ENABLED && !thr->pause);
  503. if (should_be_running)
  504. {
  505. if (spi != bitfury->spi)
  506. {
  507. if (spi)
  508. spi_txrx(spi);
  509. spi = bitfury->spi;
  510. spi_clear_buf(spi);
  511. spi_emit_break(spi);
  512. lastchip = 0;
  513. }
  514. procs[n_chips] = proc;
  515. spi_emit_fasync(spi, bitfury->fasync - lastchip);
  516. lastchip = bitfury->fasync;
  517. rxbuf[n_chips] = spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  518. ++n_chips;
  519. }
  520. else
  521. if (thr->work /* is currently running */ && thr->busy_state != TBS_STARTING_JOB)
  522. ;//FIXME: shutdown chip
  523. }
  524. timer_set_now(&tv_now);
  525. spi_txrx(spi);
  526. for (j = 0; j < n_chips; ++j)
  527. {
  528. memcpy(rxbuf_copy[j], rxbuf[j], 0x11 * 4);
  529. rxbuf[j] = rxbuf_copy[j];
  530. }
  531. for (j = 0; j < n_chips; ++j)
  532. {
  533. proc = procs[j];
  534. thr = proc->thr[0];
  535. bitfury = proc->device_data;
  536. tvp_stat = &bitfury->tv_stat;
  537. c = &bitfury->chip_stat;
  538. uint32_t * const newbuf = &bitfury->newbuf[0];
  539. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  540. inp = rxbuf[j];
  541. if (unlikely(bitfury->desync_counter == 99))
  542. {
  543. bitfury_init_oldbuf(proc, inp);
  544. goto out;
  545. }
  546. if (opt_debug)
  547. bitfury_debug_nonce_array(proc, "Read", inp);
  548. // To avoid dealing with wrap-around entirely, we rotate array so previous active uint32_t is at index 0
  549. memcpy(&newbuf[0], &inp[bitfury->active], 4 * (0x10 - bitfury->active));
  550. memcpy(&newbuf[0x10 - bitfury->active], &inp[0], 4 * bitfury->active);
  551. newjob = inp[0x10];
  552. if (newbuf[0xf] != oldbuf[0xf])
  553. {
  554. inc_hw_errors2(thr, NULL, NULL);
  555. if (unlikely(++bitfury->desync_counter >= 4))
  556. {
  557. applog(LOG_WARNING, "%"PRIpreprv": Previous nonce mismatch (4th try), recalibrating",
  558. proc->proc_repr);
  559. bitfury_init_oldbuf(proc, inp);
  560. continue;
  561. }
  562. applog(LOG_DEBUG, "%"PRIpreprv": Previous nonce mismatch, ignoring response",
  563. proc->proc_repr);
  564. goto out;
  565. }
  566. else
  567. bitfury->desync_counter = 0;
  568. if (bitfury->oldjob != newjob && thr->next_work)
  569. {
  570. mt_job_transition(thr);
  571. // TODO: Delay morework until right before it's needed
  572. timer_set_now(&thr->tv_morework);
  573. job_start_complete(thr);
  574. }
  575. for (n = 0; newbuf[n] == oldbuf[n]; ++n)
  576. {
  577. if (unlikely(n >= 0xf))
  578. {
  579. inc_hw_errors2(thr, NULL, NULL);
  580. applog(LOG_DEBUG, "%"PRIpreprv": Full result match, reinitialising",
  581. proc->proc_repr);
  582. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  583. bitfury->desync_counter = 99;
  584. goto out;
  585. }
  586. }
  587. counter = bitfury_decnonce(newbuf[n]);
  588. if ((counter & 0xFFC00000) == 0xdf800000)
  589. {
  590. counter &= 0x003fffff;
  591. int32_t cycles = counter - bitfury->counter1;
  592. if (cycles < 0)
  593. cycles += 0x00400000;
  594. if (cycles & 0x00200000)
  595. {
  596. long long unsigned int period;
  597. double ns;
  598. struct timeval d_time;
  599. timersub(&(tv_now), &(bitfury->timer1), &d_time);
  600. period = timeval_to_us(&d_time) * 1000ULL;
  601. ns = (double)period / (double)(cycles);
  602. bitfury->mhz = 1.0 / ns * 65.0 * 1000.0;
  603. bitfury->counter1 = counter;
  604. copy_time(&(bitfury->timer1), &tv_now);
  605. }
  606. }
  607. if (tvp_stat->tv_sec == 0 && tvp_stat->tv_usec == 0) {
  608. copy_time(tvp_stat, &tv_now);
  609. }
  610. if (c->osc6_max)
  611. {
  612. if (timer_elapsed(tvp_stat, &tv_now) >= 60)
  613. {
  614. double mh_diff, s_diff;
  615. const int osc = bitfury->osc6_bits;
  616. // Copy current statistics
  617. mh_diff = bitfury->counter2 - c->omh;
  618. s_diff = total_secs - c->os;
  619. applog(LOG_DEBUG, "%"PRIpreprv": %.0f completed in %f seconds",
  620. proc->proc_repr, mh_diff, s_diff);
  621. if (osc >= c->osc6_min && osc <= c->osc6_max)
  622. {
  623. c->mh[osc] += mh_diff;
  624. c->s[osc] += s_diff;
  625. }
  626. c->omh = bitfury->counter2;
  627. c->os = total_secs;
  628. if (opt_debug && !c->best_done)
  629. {
  630. char logbuf[0x100];
  631. for (i = c->osc6_min; i <= c->osc6_max; ++i)
  632. tailsprintf(logbuf, sizeof(logbuf), " %d=%.3f/%3.0fs",
  633. i, c->mh[i] / c->s[i], c->s[i]);
  634. applog(LOG_DEBUG, "%"PRIpreprv":%s",
  635. proc->proc_repr, logbuf);
  636. }
  637. // Change freq;
  638. if (!c->best_done) {
  639. select_freq(bitfury, proc);
  640. } else {
  641. applog(LOG_DEBUG, "%"PRIpreprv": Stable freq, osc6_bits: %d",
  642. proc->proc_repr, bitfury->osc6_bits);
  643. }
  644. }
  645. }
  646. if (n)
  647. {
  648. for (i = 0; i < n; ++i)
  649. {
  650. nonce = bitfury_decnonce(newbuf[i]);
  651. if (fudge_nonce(thr->work, &nonce))
  652. {
  653. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (work=%p)",
  654. proc->proc_repr, i, (unsigned long)nonce, thr->work);
  655. submit_nonce(thr, thr->work, nonce);
  656. bitfury->counter2 += 1;
  657. }
  658. else
  659. if (fudge_nonce(thr->prev_work, &nonce))
  660. {
  661. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (prev work=%p)",
  662. proc->proc_repr, i, (unsigned long)nonce, thr->prev_work);
  663. submit_nonce(thr, thr->prev_work, nonce);
  664. bitfury->counter2 += 1;
  665. }
  666. else
  667. {
  668. inc_hw_errors(thr, thr->work, nonce);
  669. ++bitfury->sample_hwe;
  670. bitfury->strange_counter += 1;
  671. }
  672. if (++bitfury->sample_tot >= 0x40 || bitfury->sample_hwe >= 8)
  673. {
  674. if (bitfury->sample_hwe >= 8)
  675. {
  676. applog(LOG_WARNING, "%"PRIpreprv": %d of the last %d results were bad, reinitialising",
  677. proc->proc_repr, bitfury->sample_hwe, bitfury->sample_tot);
  678. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  679. bitfury->desync_counter = 99;
  680. }
  681. bitfury->sample_tot = bitfury->sample_hwe = 0;
  682. }
  683. }
  684. bitfury->active = (bitfury->active + n) % 0x10;
  685. }
  686. memcpy(&oldbuf[0], &newbuf[n], 4 * (0x10 - n));
  687. memcpy(&oldbuf[0x10 - n], &newbuf[0], 4 * n);
  688. bitfury->oldjob = newjob;
  689. out:
  690. if (unlikely(bitfury->force_reinit))
  691. {
  692. applog(LOG_DEBUG, "%"PRIpreprv": Forcing reinitialisation",
  693. proc->proc_repr);
  694. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  695. bitfury->desync_counter = 99;
  696. bitfury->force_reinit = false;
  697. }
  698. if (timer_elapsed(tvp_stat, &tv_now) >= 60)
  699. copy_time(tvp_stat, &tv_now);
  700. }
  701. timer_set_delay_from_now(&master_thr->tv_poll, 10000);
  702. }
  703. int64_t bitfury_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  704. {
  705. // Bitfury chips process only 768/1024 of the nonce range
  706. return 0xbd000000;
  707. }
  708. struct api_data *bitfury_api_device_detail(struct cgpu_info * const cgpu)
  709. {
  710. struct bitfury_device * const bitfury = cgpu->device_data;
  711. struct api_data *root = NULL;
  712. root = api_add_uint(root, "fasync", &bitfury->fasync, false);
  713. return root;
  714. }
  715. struct api_data *bitfury_api_device_status(struct cgpu_info * const cgpu)
  716. {
  717. struct bitfury_device * const bitfury = cgpu->device_data;
  718. struct api_data *root = NULL;
  719. int clock_bits = bitfury->osc6_bits;
  720. root = api_add_int(root, "Clock Bits", &clock_bits, true);
  721. root = api_add_freq(root, "Frequency", &bitfury->mhz, false);
  722. return root;
  723. }
  724. static
  725. bool _bitfury_set_device_parse_setting(uint32_t * const rv, char * const setting, char * const replybuf, const int maxval)
  726. {
  727. char *p;
  728. long int nv;
  729. if (!setting || !*setting)
  730. {
  731. sprintf(replybuf, "missing setting");
  732. return false;
  733. }
  734. nv = strtol(setting, &p, 0);
  735. if (nv > maxval || nv < 1)
  736. {
  737. sprintf(replybuf, "invalid setting");
  738. return false;
  739. }
  740. *rv = nv;
  741. return true;
  742. }
  743. char *bitfury_set_device(struct cgpu_info * const proc, char * const option, char * const setting, char * const replybuf)
  744. {
  745. struct bitfury_device * const bitfury = proc->device_data;
  746. uint32_t newval;
  747. if (!strcasecmp(option, "help"))
  748. {
  749. sprintf(replybuf, "baud: SPI baud rate\nosc6_bits: range 1-%d (slow to fast)", BITFURY_MAX_OSC6_BITS);
  750. return replybuf;
  751. }
  752. if (!strcasecmp(option, "baud"))
  753. {
  754. if (!_bitfury_set_device_parse_setting(&bitfury->spi->speed, setting, replybuf, INT_MAX))
  755. return replybuf;
  756. return NULL;
  757. }
  758. if (!strcasecmp(option, "osc6_bits"))
  759. {
  760. struct freq_stat * const c = &bitfury->chip_stat;
  761. newval = bitfury->osc6_bits;
  762. if (!_bitfury_set_device_parse_setting(&newval, setting, replybuf, BITFURY_MAX_OSC6_BITS))
  763. return replybuf;
  764. bitfury->osc6_bits = newval;
  765. bitfury->force_reinit = true;
  766. c->osc6_max = 0;
  767. return NULL;
  768. }
  769. sprintf(replybuf, "Unknown option: %s", option);
  770. return replybuf;
  771. }
  772. #ifdef HAVE_CURSES
  773. void bitfury_tui_wlogprint_choices(struct cgpu_info *cgpu)
  774. {
  775. wlogprint("[O]scillator bits ");
  776. }
  777. const char *bitfury_tui_handle_choice(struct cgpu_info *cgpu, int input)
  778. {
  779. struct bitfury_device * const bitfury = cgpu->device_data;
  780. char buf[0x100];
  781. switch (input)
  782. {
  783. case 'o': case 'O':
  784. {
  785. struct freq_stat * const c = &bitfury->chip_stat;
  786. int val;
  787. char *intvar;
  788. sprintf(buf, "Set oscillator bits (range 1-%d; slow to fast)", BITFURY_MAX_OSC6_BITS);
  789. intvar = curses_input(buf);
  790. if (!intvar)
  791. return "Invalid oscillator bits\n";
  792. val = atoi(intvar);
  793. free(intvar);
  794. if (val < 1 || val > BITFURY_MAX_OSC6_BITS)
  795. return "Invalid oscillator bits\n";
  796. bitfury->osc6_bits = val;
  797. bitfury->force_reinit = true;
  798. c->osc6_max = 0;
  799. return "Oscillator bits changing\n";
  800. }
  801. }
  802. return NULL;
  803. }
  804. void bitfury_wlogprint_status(struct cgpu_info *cgpu)
  805. {
  806. struct bitfury_device * const bitfury = cgpu->device_data;
  807. wlogprint("Oscillator bits: %d\n", bitfury->osc6_bits);
  808. }
  809. #endif
  810. struct device_drv bitfury_drv = {
  811. .dname = "bitfury_gpio",
  812. .name = "BFY",
  813. .drv_detect = bitfury_detect,
  814. .thread_init = bitfury_init,
  815. .thread_shutdown = bitfury_shutdown,
  816. .minerloop = minerloop_async,
  817. .job_prepare = bitfury_job_prepare,
  818. .job_start = bitfury_noop_job_start,
  819. .poll = bitfury_do_io,
  820. .job_process_results = bitfury_job_process_results,
  821. .get_api_extra_device_detail = bitfury_api_device_detail,
  822. .get_api_extra_device_status = bitfury_api_device_status,
  823. .set_device = bitfury_set_device,
  824. #ifdef HAVE_CURSES
  825. .proc_wlogprint_status = bitfury_wlogprint_status,
  826. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  827. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  828. #endif
  829. };