driver-bitfury.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. for (proc = cgpu; proc; proc = proc->next_proc)
  168. {
  169. const int chip = proc->proc_id;
  170. bitfury = proc->device_data;
  171. int shares_found = calc_stat(bitfury->stat_ts, short_stat, now);
  172. double ghash;
  173. len = strlen(stat_lines[bitfury->slot]);
  174. ghash = shares_to_ghashes(shares_found, short_stat);
  175. gh[bitfury->slot][chip & 0x07] = ghash;
  176. snprintf(stat_lines[bitfury->slot] + len, 256 - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  177. if(sds->short_out_t && ghash < 1.0) {
  178. applog(LOG_WARNING, "Chip_id %d FREQ CHANGE", chip);
  179. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits - 1);
  180. cgsleep_ms(1);
  181. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  182. }
  183. shares_total += shares_found;
  184. shares_first += chip < 4 ? shares_found : 0;
  185. shares_last += chip > 3 ? shares_found : 0;
  186. }
  187. sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
  188. applog(LOG_WARNING, "%s", line);
  189. for(i = 0; i < 32; i++)
  190. if(strlen(stat_lines[i])) {
  191. len = strlen(stat_lines[i]);
  192. ghsum = 0;
  193. gh1h = 0;
  194. gh2h = 0;
  195. for(k = 0; k < 4; k++) {
  196. gh1h += gh[i][k];
  197. gh2h += gh[i][k+4];
  198. ghsum += gh[i][k] + gh[i][k+4];
  199. }
  200. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  201. applog(LOG_WARNING, "%s", stat_lines[i]);
  202. }
  203. sds->short_out_t = now.tv_sec;
  204. }
  205. if (now.tv_sec - sds->long_out_t > long_stat) {
  206. int shares_first = 0, shares_last = 0, shares_total = 0;
  207. char stat_lines[32][256] = {{0}};
  208. int len, k;
  209. double gh[32][8] = {{0}};
  210. double ghsum = 0, gh1h = 0, gh2h = 0;
  211. for (proc = cgpu; proc; proc = proc->next_proc)
  212. {
  213. const int chip = proc->proc_id;
  214. bitfury = proc->device_data;
  215. int shares_found = calc_stat(bitfury->stat_ts, long_stat, now);
  216. double ghash;
  217. len = strlen(stat_lines[bitfury->slot]);
  218. ghash = shares_to_ghashes(shares_found, long_stat);
  219. gh[bitfury->slot][chip & 0x07] = ghash;
  220. snprintf(stat_lines[bitfury->slot] + len, 256 - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  221. shares_total += shares_found;
  222. shares_first += chip < 4 ? shares_found : 0;
  223. shares_last += chip > 3 ? shares_found : 0;
  224. }
  225. sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
  226. applog(LOG_WARNING, "%s", line);
  227. for(i = 0; i < 32; i++)
  228. if(strlen(stat_lines[i])) {
  229. len = strlen(stat_lines[i]);
  230. ghsum = 0;
  231. gh1h = 0;
  232. gh2h = 0;
  233. for(k = 0; k < 4; k++) {
  234. gh1h += gh[i][k];
  235. gh2h += gh[i][k+4];
  236. ghsum += gh[i][k] + gh[i][k+4];
  237. }
  238. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  239. applog(LOG_WARNING, "%s", stat_lines[i]);
  240. }
  241. sds->long_out_t = now.tv_sec;
  242. }
  243. return 0;
  244. }
  245. double shares_to_ghashes(int shares, int seconds) {
  246. return (double)shares / (double)seconds * 4.84387; //orig: 4.77628
  247. }
  248. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
  249. int j;
  250. int shares_found = 0;
  251. for(j = 0; j < BITFURY_STAT_N; j++) {
  252. if (now.tv_sec - stat_ts[j] < stat) {
  253. shares_found++;
  254. }
  255. }
  256. return shares_found;
  257. }
  258. bool bitfury_prepare(struct thr_info *thr)
  259. {
  260. struct cgpu_info *cgpu = thr->cgpu;
  261. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  262. applog(LOG_INFO, "INFO bitfury_prepare");
  263. return true;
  264. }
  265. void bitfury_shutdown(struct thr_info *thr) {
  266. struct cgpu_info *cgpu = thr->cgpu, *proc;
  267. struct bitfury_device *bitfury;
  268. applog(LOG_INFO, "INFO bitfury_shutdown");
  269. for (proc = cgpu; proc; proc = proc->next_proc)
  270. {
  271. bitfury = proc->device_data;
  272. send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  273. }
  274. }
  275. struct device_drv bitfury_drv = {
  276. .dname = "bitfury_gpio",
  277. .name = "BFY",
  278. .drv_detect = bitfury_detect,
  279. .thread_prepare = bitfury_prepare,
  280. .thread_init = bitfury_init,
  281. .scanwork = bitfury_scanHash,
  282. .thread_shutdown = bitfury_shutdown,
  283. .minerloop = hash_queued_work,
  284. };