driver-bitfury.c 25 KB

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