driver-bitfury.c 9.3 KB

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