driver-bitfury.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. bitfury = proc->device_data;
  105. bitfury->job_switched = 0;
  106. if(!bitfury->work) {
  107. bitfury->work = get_queued(thr->cgpu);
  108. if (bitfury->work == NULL)
  109. return 0;
  110. work_to_payload(&bitfury->payload, bitfury->work);
  111. }
  112. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  113. libbitfury_sendHashData1(chip, bitfury);
  114. }
  115. cgsleep_ms(5);
  116. cgtime(&now);
  117. for (proc = cgpu; proc; proc = proc->next_proc)
  118. {
  119. pthr = proc->thr[0];
  120. bitfury = proc->device_data;
  121. if (bitfury->job_switched) {
  122. int i,j;
  123. unsigned int * const res = bitfury->results;
  124. struct work * const work = bitfury->work;
  125. struct work * const owork = bitfury->owork;
  126. struct work * const o2work = bitfury->o2work;
  127. i = bitfury->results_n;
  128. for (j = i - 1; j >= 0; j--) {
  129. if (owork) {
  130. submit_nonce(pthr, owork, bswap_32(res[j]));
  131. bitfury->stat_ts[bitfury->stat_counter++] =
  132. now.tv_sec;
  133. if (bitfury->stat_counter == BITFURY_STAT_N) {
  134. bitfury->stat_counter = 0;
  135. }
  136. }
  137. if (o2work) {
  138. // TEST
  139. //submit_nonce(pthr, owork, bswap_32(res[j]));
  140. }
  141. }
  142. bitfury->results_n = 0;
  143. bitfury->job_switched = 0;
  144. if (bitfury->old_nonce && o2work) {
  145. submit_nonce(pthr, o2work, bswap_32(bitfury->old_nonce));
  146. i++;
  147. }
  148. if (bitfury->future_nonce) {
  149. submit_nonce(pthr, work, bswap_32(bitfury->future_nonce));
  150. i++;
  151. }
  152. if (o2work)
  153. work_completed(cgpu, o2work);
  154. bitfury->o2work = bitfury->owork;
  155. bitfury->owork = bitfury->work;
  156. bitfury->work = NULL;
  157. hashes_done2(pthr, 0x100000000, NULL);
  158. }
  159. }
  160. if (now.tv_sec - sds->short_out_t > short_stat) {
  161. int shares_first = 0, shares_last = 0, shares_total = 0;
  162. char stat_lines[32][256] = {{0}};
  163. int len, k;
  164. double gh[32][8] = {{0}};
  165. double ghsum = 0, gh1h = 0, gh2h = 0;
  166. for (proc = cgpu; proc; proc = proc->next_proc)
  167. {
  168. const int chip = proc->proc_id;
  169. bitfury = proc->device_data;
  170. int shares_found = calc_stat(bitfury->stat_ts, short_stat, now);
  171. double ghash;
  172. len = strlen(stat_lines[bitfury->slot]);
  173. ghash = shares_to_ghashes(shares_found, short_stat);
  174. gh[bitfury->slot][chip & 0x07] = ghash;
  175. snprintf(stat_lines[bitfury->slot] + len, 256 - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  176. if(sds->short_out_t && ghash < 1.0) {
  177. applog(LOG_WARNING, "Chip_id %d FREQ CHANGE\n", chip);
  178. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits - 1);
  179. cgsleep_ms(1);
  180. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  181. }
  182. shares_total += shares_found;
  183. shares_first += chip < 4 ? shares_found : 0;
  184. shares_last += chip > 3 ? shares_found : 0;
  185. }
  186. sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
  187. applog(LOG_WARNING, "%s", line);
  188. for(i = 0; i < 32; i++)
  189. if(strlen(stat_lines[i])) {
  190. len = strlen(stat_lines[i]);
  191. ghsum = 0;
  192. gh1h = 0;
  193. gh2h = 0;
  194. for(k = 0; k < 4; k++) {
  195. gh1h += gh[i][k];
  196. gh2h += gh[i][k+4];
  197. ghsum += gh[i][k] + gh[i][k+4];
  198. }
  199. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  200. applog(LOG_WARNING, "%s", stat_lines[i]);
  201. }
  202. sds->short_out_t = now.tv_sec;
  203. }
  204. if (now.tv_sec - sds->long_out_t > long_stat) {
  205. int shares_first = 0, shares_last = 0, shares_total = 0;
  206. char stat_lines[32][256] = {{0}};
  207. int len, k;
  208. double gh[32][8] = {{0}};
  209. double ghsum = 0, gh1h = 0, gh2h = 0;
  210. for (proc = cgpu; proc; proc = proc->next_proc)
  211. {
  212. const int chip = proc->proc_id;
  213. bitfury = proc->device_data;
  214. int shares_found = calc_stat(bitfury->stat_ts, long_stat, now);
  215. double ghash;
  216. len = strlen(stat_lines[bitfury->slot]);
  217. ghash = shares_to_ghashes(shares_found, long_stat);
  218. gh[bitfury->slot][chip & 0x07] = ghash;
  219. snprintf(stat_lines[bitfury->slot] + len, 256 - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  220. shares_total += shares_found;
  221. shares_first += chip < 4 ? shares_found : 0;
  222. shares_last += chip > 3 ? shares_found : 0;
  223. }
  224. sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
  225. applog(LOG_WARNING, "%s", line);
  226. for(i = 0; i < 32; i++)
  227. if(strlen(stat_lines[i])) {
  228. len = strlen(stat_lines[i]);
  229. ghsum = 0;
  230. gh1h = 0;
  231. gh2h = 0;
  232. for(k = 0; k < 4; k++) {
  233. gh1h += gh[i][k];
  234. gh2h += gh[i][k+4];
  235. ghsum += gh[i][k] + gh[i][k+4];
  236. }
  237. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  238. applog(LOG_WARNING, "%s", stat_lines[i]);
  239. }
  240. sds->long_out_t = now.tv_sec;
  241. }
  242. return 0;
  243. }
  244. double shares_to_ghashes(int shares, int seconds) {
  245. return (double)shares / (double)seconds * 4.84387; //orig: 4.77628
  246. }
  247. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
  248. int j;
  249. int shares_found = 0;
  250. for(j = 0; j < BITFURY_STAT_N; j++) {
  251. if (now.tv_sec - stat_ts[j] < stat) {
  252. shares_found++;
  253. }
  254. }
  255. return shares_found;
  256. }
  257. bool bitfury_prepare(struct thr_info *thr)
  258. {
  259. struct cgpu_info *cgpu = thr->cgpu;
  260. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  261. applog(LOG_INFO, "INFO bitfury_prepare");
  262. return true;
  263. }
  264. void bitfury_shutdown(struct thr_info *thr) {
  265. struct cgpu_info *cgpu = thr->cgpu, *proc;
  266. struct bitfury_device *bitfury;
  267. applog(LOG_INFO, "INFO bitfury_shutdown");
  268. for (proc = cgpu; proc; proc = proc->next_proc)
  269. {
  270. bitfury = proc->device_data;
  271. send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  272. }
  273. }
  274. struct device_drv bitfury_drv = {
  275. .dname = "bitfury_gpio",
  276. .name = "BFY",
  277. .drv_detect = bitfury_detect,
  278. .thread_prepare = bitfury_prepare,
  279. .thread_init = bitfury_init,
  280. .scanwork = bitfury_scanHash,
  281. .thread_shutdown = bitfury_shutdown,
  282. .minerloop = hash_queued_work,
  283. };