driver-bitfury.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright 2013 bitfury
  3. * Copyright 2013 legkodymov
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "config.h"
  24. #include "miner.h"
  25. #include <unistd.h>
  26. #include <sha2.h>
  27. #include "fpgautils.h"
  28. #include "libbitfury.h"
  29. #include "util.h"
  30. #include "spidevc.h"
  31. #define GOLDEN_BACKLOG 5
  32. struct device_drv bitfury_drv;
  33. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now);
  34. double shares_to_ghashes(int shares, int seconds);
  35. static
  36. int bitfury_autodetect()
  37. {
  38. RUNONCE(0);
  39. int chip_n;
  40. struct cgpu_info *bitfury_info;
  41. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  42. bitfury_info->drv = &bitfury_drv;
  43. bitfury_info->threads = 1;
  44. applog(LOG_INFO, "INFO: bitfury_detect");
  45. spi_init();
  46. if (!sys_spi)
  47. return 0;
  48. chip_n = libbitfury_detectChips1(sys_spi);
  49. if (!chip_n) {
  50. applog(LOG_WARNING, "No Bitfury chips detected!");
  51. return 0;
  52. } else {
  53. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  54. }
  55. bitfury_info->procs = chip_n;
  56. add_cgpu(bitfury_info);
  57. return 1;
  58. }
  59. static void bitfury_detect(void)
  60. {
  61. noserial_detect_manual(&bitfury_drv, bitfury_autodetect);
  62. }
  63. static
  64. bool bitfury_init(struct thr_info *thr)
  65. {
  66. struct cgpu_info *proc;
  67. struct bitfury_device *bitfury;
  68. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  69. {
  70. bitfury = proc->device_data = malloc(sizeof(struct bitfury_device));
  71. *bitfury = (struct bitfury_device){
  72. .spi = sys_spi,
  73. .fasync = proc->proc_id,
  74. };
  75. }
  76. return true;
  77. }
  78. int64_t bitfury_scanHash(struct thr_info *thr)
  79. {
  80. struct cgpu_info * const cgpu = thr->cgpu;
  81. struct bitfury_device * const sds = cgpu->device_data;
  82. struct cgpu_info *proc;
  83. struct thr_info *pthr;
  84. struct bitfury_device *bitfury;
  85. struct timeval now;
  86. char line[2048];
  87. int short_stat = 10;
  88. int long_stat = 1800;
  89. int i;
  90. if (!sds->first)
  91. {
  92. // TODO: Move to init
  93. for (proc = cgpu; proc; proc = proc->next_proc)
  94. {
  95. bitfury = proc->device_data;
  96. bitfury->osc6_bits = 54;
  97. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  98. }
  99. }
  100. sds->first = 1;
  101. for (proc = cgpu; proc; proc = proc->next_proc)
  102. {
  103. const int chip = proc->proc_id;
  104. pthr = proc->thr[0];
  105. bitfury = proc->device_data;
  106. bitfury->job_switched = 0;
  107. if(!bitfury->work) {
  108. bitfury->work = get_queued(thr->cgpu);
  109. if (bitfury->work == NULL)
  110. return 0;
  111. work_to_payload(&bitfury->payload, bitfury->work);
  112. }
  113. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  114. libbitfury_sendHashData1(chip, bitfury, pthr);
  115. }
  116. cgsleep_ms(5);
  117. cgtime(&now);
  118. for (proc = cgpu; proc; proc = proc->next_proc)
  119. {
  120. pthr = proc->thr[0];
  121. bitfury = proc->device_data;
  122. if (bitfury->job_switched) {
  123. int i,j;
  124. unsigned int * const res = bitfury->results;
  125. struct work * const work = bitfury->work;
  126. struct work * const owork = bitfury->owork;
  127. struct work * const o2work = bitfury->o2work;
  128. i = bitfury->results_n;
  129. for (j = i - 1; j >= 0; j--) {
  130. if (owork) {
  131. submit_nonce(pthr, owork, bswap_32(res[j]));
  132. bitfury->stat_ts[bitfury->stat_counter++] =
  133. now.tv_sec;
  134. if (bitfury->stat_counter == BITFURY_STAT_N) {
  135. bitfury->stat_counter = 0;
  136. }
  137. }
  138. if (o2work) {
  139. // TEST
  140. //submit_nonce(pthr, owork, bswap_32(res[j]));
  141. }
  142. }
  143. bitfury->results_n = 0;
  144. bitfury->job_switched = 0;
  145. if (bitfury->old_nonce && o2work) {
  146. submit_nonce(pthr, o2work, bswap_32(bitfury->old_nonce));
  147. i++;
  148. }
  149. if (bitfury->future_nonce) {
  150. submit_nonce(pthr, work, bswap_32(bitfury->future_nonce));
  151. i++;
  152. }
  153. if (o2work)
  154. work_completed(cgpu, o2work);
  155. bitfury->o2work = bitfury->owork;
  156. bitfury->owork = bitfury->work;
  157. bitfury->work = NULL;
  158. hashes_done2(pthr, 0xbd000000, NULL);
  159. }
  160. }
  161. if (now.tv_sec - sds->short_out_t > short_stat) {
  162. int shares_first = 0, shares_last = 0, shares_total = 0;
  163. char stat_lines[32][256] = {{0}};
  164. int len, k;
  165. double gh[32][8] = {{0}};
  166. double ghsum = 0, gh1h = 0, gh2h = 0;
  167. unsigned strange_counter = 0;
  168. for (proc = cgpu; proc; proc = proc->next_proc)
  169. {
  170. const int chip = proc->proc_id;
  171. bitfury = proc->device_data;
  172. int shares_found = calc_stat(bitfury->stat_ts, short_stat, now);
  173. double ghash;
  174. len = strlen(stat_lines[bitfury->slot]);
  175. ghash = shares_to_ghashes(shares_found, short_stat);
  176. gh[bitfury->slot][chip & 0x07] = ghash;
  177. snprintf(stat_lines[bitfury->slot] + len, 256 - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  178. if(sds->short_out_t && ghash < 0.5) {
  179. applog(LOG_WARNING, "Chip_id %d FREQ CHANGE", chip);
  180. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits - 1);
  181. cgsleep_ms(1);
  182. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  183. }
  184. shares_total += shares_found;
  185. shares_first += chip < 4 ? shares_found : 0;
  186. shares_last += chip > 3 ? shares_found : 0;
  187. strange_counter += bitfury->strange_counter;
  188. bitfury->strange_counter = 0;
  189. }
  190. sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
  191. applog(LOG_WARNING, "%s", line);
  192. sprintf(line, "stranges: %u", strange_counter);
  193. applog(LOG_WARNING, "%s", line);
  194. for(i = 0; i < 32; i++)
  195. if(strlen(stat_lines[i])) {
  196. len = strlen(stat_lines[i]);
  197. ghsum = 0;
  198. gh1h = 0;
  199. gh2h = 0;
  200. for(k = 0; k < 4; k++) {
  201. gh1h += gh[i][k];
  202. gh2h += gh[i][k+4];
  203. ghsum += gh[i][k] + gh[i][k+4];
  204. }
  205. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  206. applog(LOG_WARNING, "%s", stat_lines[i]);
  207. }
  208. sds->short_out_t = now.tv_sec;
  209. }
  210. if (now.tv_sec - sds->long_out_t > long_stat) {
  211. int shares_first = 0, shares_last = 0, shares_total = 0;
  212. char stat_lines[32][256] = {{0}};
  213. int len, k;
  214. double gh[32][8] = {{0}};
  215. double ghsum = 0, gh1h = 0, gh2h = 0;
  216. for (proc = cgpu; proc; proc = proc->next_proc)
  217. {
  218. const int chip = proc->proc_id;
  219. bitfury = proc->device_data;
  220. int shares_found = calc_stat(bitfury->stat_ts, long_stat, now);
  221. double ghash;
  222. len = strlen(stat_lines[bitfury->slot]);
  223. ghash = shares_to_ghashes(shares_found, long_stat);
  224. gh[bitfury->slot][chip & 0x07] = ghash;
  225. snprintf(stat_lines[bitfury->slot] + len, 256 - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  226. shares_total += shares_found;
  227. shares_first += chip < 4 ? shares_found : 0;
  228. shares_last += chip > 3 ? shares_found : 0;
  229. }
  230. sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
  231. applog(LOG_WARNING, "%s", line);
  232. for(i = 0; i < 32; i++)
  233. if(strlen(stat_lines[i])) {
  234. len = strlen(stat_lines[i]);
  235. ghsum = 0;
  236. gh1h = 0;
  237. gh2h = 0;
  238. for(k = 0; k < 4; k++) {
  239. gh1h += gh[i][k];
  240. gh2h += gh[i][k+4];
  241. ghsum += gh[i][k] + gh[i][k+4];
  242. }
  243. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  244. applog(LOG_WARNING, "%s", stat_lines[i]);
  245. }
  246. sds->long_out_t = now.tv_sec;
  247. }
  248. return 0;
  249. }
  250. double shares_to_ghashes(int shares, int seconds) {
  251. return (double)shares / (double)seconds * 4.84387; //orig: 4.77628
  252. }
  253. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
  254. int j;
  255. int shares_found = 0;
  256. for(j = 0; j < BITFURY_STAT_N; j++) {
  257. if (now.tv_sec - stat_ts[j] < stat) {
  258. shares_found++;
  259. }
  260. }
  261. return shares_found;
  262. }
  263. bool bitfury_prepare(struct thr_info *thr)
  264. {
  265. struct cgpu_info *cgpu = thr->cgpu;
  266. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  267. applog(LOG_INFO, "INFO bitfury_prepare");
  268. return true;
  269. }
  270. void bitfury_shutdown(struct thr_info *thr) {
  271. struct cgpu_info *cgpu = thr->cgpu, *proc;
  272. struct bitfury_device *bitfury;
  273. applog(LOG_INFO, "INFO bitfury_shutdown");
  274. for (proc = cgpu; proc; proc = proc->next_proc)
  275. {
  276. bitfury = proc->device_data;
  277. send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  278. }
  279. }
  280. struct device_drv bitfury_drv = {
  281. .dname = "bitfury_gpio",
  282. .name = "BFY",
  283. .drv_detect = bitfury_detect,
  284. .thread_prepare = bitfury_prepare,
  285. .thread_init = bitfury_init,
  286. .scanwork = bitfury_scanHash,
  287. .thread_shutdown = bitfury_shutdown,
  288. .minerloop = hash_queued_work,
  289. };