driver-bitfury.c 25 KB

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