driver-bitfury.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #define LINE_LEN 2048
  33. struct device_drv bitfury_drv;
  34. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now);
  35. double shares_to_ghashes(int shares, int seconds);
  36. static
  37. int bitfury_autodetect()
  38. {
  39. RUNONCE(0);
  40. int chip_n;
  41. struct cgpu_info *bitfury_info;
  42. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  43. bitfury_info->drv = &bitfury_drv;
  44. bitfury_info->threads = 1;
  45. applog(LOG_INFO, "INFO: bitfury_detect");
  46. spi_init();
  47. if (!sys_spi)
  48. return 0;
  49. chip_n = libbitfury_detectChips1(sys_spi);
  50. if (!chip_n) {
  51. applog(LOG_WARNING, "No Bitfury chips detected!");
  52. return 0;
  53. } else {
  54. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  55. }
  56. bitfury_info->procs = chip_n;
  57. add_cgpu(bitfury_info);
  58. return 1;
  59. }
  60. static void bitfury_detect(void)
  61. {
  62. noserial_detect_manual(&bitfury_drv, bitfury_autodetect);
  63. }
  64. static
  65. bool bitfury_init(struct thr_info *thr)
  66. {
  67. struct cgpu_info *proc;
  68. struct bitfury_device *bitfury;
  69. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  70. {
  71. bitfury = proc->device_data = malloc(sizeof(struct bitfury_device));
  72. *bitfury = (struct bitfury_device){
  73. .spi = sys_spi,
  74. .fasync = proc->proc_id,
  75. };
  76. }
  77. return true;
  78. }
  79. int64_t bitfury_scanHash(struct thr_info *thr)
  80. {
  81. struct cgpu_info * const cgpu = thr->cgpu;
  82. struct bitfury_device * const sds = cgpu->device_data;
  83. struct cgpu_info *proc;
  84. struct thr_info *pthr;
  85. struct bitfury_device *bitfury;
  86. struct timeval now;
  87. char line[LINE_LEN];
  88. int short_stat = 10;
  89. int long_stat = 1800;
  90. int i;
  91. if (!sds->first)
  92. {
  93. // TODO: Move to init
  94. for (proc = cgpu; proc; proc = proc->next_proc)
  95. {
  96. bitfury = proc->device_data;
  97. bitfury->osc6_bits = 54;
  98. send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  99. }
  100. }
  101. sds->first = 1;
  102. for (proc = cgpu; proc; proc = proc->next_proc)
  103. {
  104. const int chip = proc->proc_id;
  105. pthr = proc->thr[0];
  106. bitfury = proc->device_data;
  107. bitfury->job_switched = 0;
  108. if(!bitfury->work) {
  109. bitfury->work = get_queued(thr->cgpu);
  110. if (bitfury->work == NULL)
  111. return 0;
  112. work_to_payload(&bitfury->payload, bitfury->work);
  113. }
  114. payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  115. libbitfury_sendHashData1(chip, bitfury, pthr);
  116. }
  117. cgsleep_ms(5);
  118. cgtime(&now);
  119. for (proc = cgpu; proc; proc = proc->next_proc)
  120. {
  121. pthr = proc->thr[0];
  122. bitfury = proc->device_data;
  123. if (bitfury->job_switched) {
  124. int i,j;
  125. unsigned int * const res = bitfury->results;
  126. struct work * const work = bitfury->work;
  127. struct work * const owork = bitfury->owork;
  128. struct work * const o2work = bitfury->o2work;
  129. i = bitfury->results_n;
  130. for (j = i - 1; j >= 0; j--) {
  131. if (owork) {
  132. submit_nonce(pthr, owork, bswap_32(res[j]));
  133. bitfury->stat_ts[bitfury->stat_counter++] =
  134. now.tv_sec;
  135. if (bitfury->stat_counter == BITFURY_STAT_N) {
  136. bitfury->stat_counter = 0;
  137. }
  138. }
  139. if (o2work) {
  140. // TEST
  141. //submit_nonce(pthr, owork, bswap_32(res[j]));
  142. }
  143. }
  144. bitfury->results_n = 0;
  145. bitfury->job_switched = 0;
  146. if (bitfury->old_nonce && o2work) {
  147. submit_nonce(pthr, o2work, bswap_32(bitfury->old_nonce));
  148. i++;
  149. }
  150. if (bitfury->future_nonce) {
  151. submit_nonce(pthr, work, bswap_32(bitfury->future_nonce));
  152. i++;
  153. }
  154. if (o2work)
  155. work_completed(cgpu, o2work);
  156. bitfury->o2work = bitfury->owork;
  157. bitfury->owork = bitfury->work;
  158. bitfury->work = NULL;
  159. hashes_done2(pthr, 0xbd000000, NULL);
  160. }
  161. }
  162. if (now.tv_sec - sds->short_out_t > short_stat) {
  163. int shares_first = 0, shares_last = 0, shares_total = 0;
  164. char stat_lines[32][LINE_LEN] = {{0}};
  165. int len, k;
  166. double gh[32][8] = {{0}};
  167. double ghsum = 0, gh1h = 0, gh2h = 0;
  168. unsigned strange_counter = 0;
  169. for (proc = cgpu; proc; proc = proc->next_proc)
  170. {
  171. const int chip = proc->proc_id;
  172. bitfury = proc->device_data;
  173. int shares_found = calc_stat(bitfury->stat_ts, short_stat, now);
  174. double ghash;
  175. len = strlen(stat_lines[bitfury->slot]);
  176. ghash = shares_to_ghashes(shares_found, short_stat);
  177. gh[bitfury->slot][chip & 0x07] = ghash;
  178. snprintf(stat_lines[bitfury->slot] + len, LINE_LEN - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  179. if(sds->short_out_t && ghash < 0.5) {
  180. applog(LOG_WARNING, "Chip_id %d FREQ CHANGE", chip);
  181. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits - 1);
  182. cgsleep_ms(1);
  183. send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  184. }
  185. shares_total += shares_found;
  186. shares_first += chip < 4 ? shares_found : 0;
  187. shares_last += chip > 3 ? shares_found : 0;
  188. strange_counter += bitfury->strange_counter;
  189. bitfury->strange_counter = 0;
  190. }
  191. sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
  192. applog(LOG_WARNING, "%s", line);
  193. sprintf(line, "stranges: %u", strange_counter);
  194. applog(LOG_WARNING, "%s", line);
  195. for(i = 0; i < 32; i++)
  196. if(strlen(stat_lines[i])) {
  197. len = strlen(stat_lines[i]);
  198. ghsum = 0;
  199. gh1h = 0;
  200. gh2h = 0;
  201. for(k = 0; k < 4; k++) {
  202. gh1h += gh[i][k];
  203. gh2h += gh[i][k+4];
  204. ghsum += gh[i][k] + gh[i][k+4];
  205. }
  206. snprintf(stat_lines[i] + len, LINE_LEN - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  207. applog(LOG_WARNING, "%s", stat_lines[i]);
  208. }
  209. sds->short_out_t = now.tv_sec;
  210. }
  211. if (now.tv_sec - sds->long_out_t > long_stat) {
  212. int shares_first = 0, shares_last = 0, shares_total = 0;
  213. char stat_lines[32][LINE_LEN] = {{0}};
  214. int len, k;
  215. double gh[32][8] = {{0}};
  216. double ghsum = 0, gh1h = 0, gh2h = 0;
  217. for (proc = cgpu; proc; proc = proc->next_proc)
  218. {
  219. const int chip = proc->proc_id;
  220. bitfury = proc->device_data;
  221. int shares_found = calc_stat(bitfury->stat_ts, long_stat, now);
  222. double ghash;
  223. len = strlen(stat_lines[bitfury->slot]);
  224. ghash = shares_to_ghashes(shares_found, long_stat);
  225. gh[bitfury->slot][chip & 0x07] = ghash;
  226. snprintf(stat_lines[bitfury->slot] + len, LINE_LEN - len, "%.1f-%3.0f ", ghash, bitfury->mhz);
  227. shares_total += shares_found;
  228. shares_first += chip < 4 ? shares_found : 0;
  229. shares_last += chip > 3 ? shares_found : 0;
  230. }
  231. sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
  232. applog(LOG_WARNING, "%s", line);
  233. for(i = 0; i < 32; i++)
  234. if(strlen(stat_lines[i])) {
  235. len = strlen(stat_lines[i]);
  236. ghsum = 0;
  237. gh1h = 0;
  238. gh2h = 0;
  239. for(k = 0; k < 4; k++) {
  240. gh1h += gh[i][k];
  241. gh2h += gh[i][k+4];
  242. ghsum += gh[i][k] + gh[i][k+4];
  243. }
  244. snprintf(stat_lines[i] + len, LINE_LEN - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  245. applog(LOG_WARNING, "%s", stat_lines[i]);
  246. }
  247. sds->long_out_t = now.tv_sec;
  248. }
  249. return 0;
  250. }
  251. double shares_to_ghashes(int shares, int seconds) {
  252. return (double)shares / (double)seconds * 4.84387; //orig: 4.77628
  253. }
  254. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
  255. int j;
  256. int shares_found = 0;
  257. for(j = 0; j < BITFURY_STAT_N; j++) {
  258. if (now.tv_sec - stat_ts[j] < stat) {
  259. shares_found++;
  260. }
  261. }
  262. return shares_found;
  263. }
  264. bool bitfury_prepare(struct thr_info *thr)
  265. {
  266. struct cgpu_info *cgpu = thr->cgpu;
  267. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  268. applog(LOG_INFO, "INFO bitfury_prepare");
  269. return true;
  270. }
  271. void bitfury_shutdown(struct thr_info *thr) {
  272. struct cgpu_info *cgpu = thr->cgpu, *proc;
  273. struct bitfury_device *bitfury;
  274. applog(LOG_INFO, "INFO bitfury_shutdown");
  275. for (proc = cgpu; proc; proc = proc->next_proc)
  276. {
  277. bitfury = proc->device_data;
  278. send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  279. }
  280. }
  281. struct device_drv bitfury_drv = {
  282. .dname = "bitfury_gpio",
  283. .name = "BFY",
  284. .drv_detect = bitfury_detect,
  285. .thread_prepare = bitfury_prepare,
  286. .thread_init = bitfury_init,
  287. .scanwork = bitfury_scanHash,
  288. .thread_shutdown = bitfury_shutdown,
  289. .minerloop = hash_queued_work,
  290. };