driver-bitfury.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "miner.h"
  24. #include <unistd.h>
  25. #include <sha2.h>
  26. #include "libbitfury.h"
  27. #include "util.h"
  28. #include "spidevc.h"
  29. #define GOLDEN_BACKLOG 5
  30. struct device_drv bitfury_drv;
  31. // Forward declarations
  32. static bool bitfury_prepare(struct thr_info *thr);
  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 void bitfury_detect(void)
  36. {
  37. int chip_n;
  38. struct cgpu_info *bitfury_info;
  39. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  40. bitfury_info->drv = &bitfury_drv;
  41. bitfury_info->threads = 1;
  42. applog(LOG_INFO, "INFO: bitfury_detect");
  43. spi_init();
  44. if (!sys_spi)
  45. return;
  46. chip_n = libbitfury_detectChips(sys_spi, bitfury_info->devices);
  47. if (!chip_n) {
  48. applog(LOG_WARNING, "No Bitfury chips detected!");
  49. return;
  50. } else {
  51. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  52. }
  53. bitfury_info->chip_n = chip_n;
  54. add_cgpu(bitfury_info);
  55. }
  56. static int64_t bitfury_scanHash(struct thr_info *thr)
  57. {
  58. static struct bitfury_device *devices; // TODO Move somewhere to appropriate place
  59. int chip_n;
  60. int chip;
  61. uint64_t hashes = 0;
  62. struct timeval now;
  63. char line[2048];
  64. int short_stat = 10;
  65. static time_t short_out_t;
  66. int long_stat = 1800;
  67. static time_t long_out_t;
  68. static int first = 0; //TODO Move to detect()
  69. int i;
  70. devices = thr->cgpu->devices;
  71. chip_n = thr->cgpu->chip_n;
  72. if (!first) {
  73. for (i = 0; i < chip_n; i++) {
  74. devices[i].osc6_bits = 54;
  75. }
  76. for (i = 0; i < chip_n; i++) {
  77. send_reinit(devices[i].spi, devices[i].slot, devices[i].fasync, devices[i].osc6_bits);
  78. }
  79. }
  80. first = 1;
  81. for (chip = 0; chip < chip_n; chip++) {
  82. devices[chip].spi = sys_spi;
  83. devices[chip].job_switched = 0;
  84. if(!devices[chip].work) {
  85. devices[chip].work = get_queued(thr->cgpu);
  86. if (devices[chip].work == NULL) {
  87. return 0;
  88. }
  89. work_to_payload(&(devices[chip].payload), devices[chip].work);
  90. }
  91. }
  92. libbitfury_sendHashData(devices, chip_n);
  93. cgsleep_ms(5);
  94. cgtime(&now);
  95. chip = 0;
  96. for (;chip < chip_n; chip++) {
  97. if (devices[chip].job_switched) {
  98. int i,j;
  99. unsigned int *res = devices[chip].results;
  100. struct work *work = devices[chip].work;
  101. struct work *owork = devices[chip].owork;
  102. struct work *o2work = devices[chip].o2work;
  103. i = devices[chip].results_n;
  104. for (j = i - 1; j >= 0; j--) {
  105. if (owork) {
  106. submit_nonce(thr, owork, bswap_32(res[j]));
  107. devices[chip].stat_ts[devices[chip].stat_counter++] =
  108. now.tv_sec;
  109. if (devices[chip].stat_counter == BITFURY_STAT_N) {
  110. devices[chip].stat_counter = 0;
  111. }
  112. }
  113. if (o2work) {
  114. // TEST
  115. //submit_nonce(thr, owork, bswap_32(res[j]));
  116. }
  117. }
  118. devices[chip].results_n = 0;
  119. devices[chip].job_switched = 0;
  120. if (devices[chip].old_nonce && o2work) {
  121. submit_nonce(thr, o2work, bswap_32(devices[chip].old_nonce));
  122. i++;
  123. }
  124. if (devices[chip].future_nonce) {
  125. submit_nonce(thr, work, bswap_32(devices[chip].future_nonce));
  126. i++;
  127. }
  128. if (o2work)
  129. work_completed(thr->cgpu, o2work);
  130. devices[chip].o2work = devices[chip].owork;
  131. devices[chip].owork = devices[chip].work;
  132. devices[chip].work = NULL;
  133. hashes += 0xffffffffull * i;
  134. }
  135. }
  136. if (now.tv_sec - short_out_t > short_stat) {
  137. int shares_first = 0, shares_last = 0, shares_total = 0;
  138. char stat_lines[32][256] = {{0}};
  139. int len, k;
  140. double gh[32][8] = {{0}};
  141. double ghsum = 0, gh1h = 0, gh2h = 0;
  142. for (chip = 0; chip < chip_n; chip++) {
  143. int shares_found = calc_stat(devices[chip].stat_ts, short_stat, now);
  144. double ghash;
  145. len = strlen(stat_lines[devices[chip].slot]);
  146. ghash = shares_to_ghashes(shares_found, short_stat);
  147. gh[devices[chip].slot][chip & 0x07] = ghash;
  148. snprintf(stat_lines[devices[chip].slot] + len, 256 - len, "%.1f-%3.0f ", ghash, devices[chip].mhz);
  149. if(short_out_t && ghash < 1.0) {
  150. applog(LOG_WARNING, "Chip_id %d FREQ CHANGE\n", chip);
  151. send_freq(devices[chip].spi, devices[chip].slot, devices[chip].fasync, devices[chip].osc6_bits - 1);
  152. cgsleep_ms(1);
  153. send_freq(devices[chip].spi, devices[chip].slot, devices[chip].fasync, devices[chip].osc6_bits);
  154. }
  155. shares_total += shares_found;
  156. shares_first += chip < 4 ? shares_found : 0;
  157. shares_last += chip > 3 ? shares_found : 0;
  158. }
  159. sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
  160. applog(LOG_WARNING, "%s", line);
  161. for(i = 0; i < 32; i++)
  162. if(strlen(stat_lines[i])) {
  163. len = strlen(stat_lines[i]);
  164. ghsum = 0;
  165. gh1h = 0;
  166. gh2h = 0;
  167. for(k = 0; k < 4; k++) {
  168. gh1h += gh[i][k];
  169. gh2h += gh[i][k+4];
  170. ghsum += gh[i][k] + gh[i][k+4];
  171. }
  172. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  173. applog(LOG_WARNING, "%s", stat_lines[i]);
  174. }
  175. short_out_t = now.tv_sec;
  176. }
  177. if (now.tv_sec - long_out_t > long_stat) {
  178. int shares_first = 0, shares_last = 0, shares_total = 0;
  179. char stat_lines[32][256] = {{0}};
  180. int len, k;
  181. double gh[32][8] = {{0}};
  182. double ghsum = 0, gh1h = 0, gh2h = 0;
  183. for (chip = 0; chip < chip_n; chip++) {
  184. int shares_found = calc_stat(devices[chip].stat_ts, long_stat, now);
  185. double ghash;
  186. len = strlen(stat_lines[devices[chip].slot]);
  187. ghash = shares_to_ghashes(shares_found, long_stat);
  188. gh[devices[chip].slot][chip & 0x07] = ghash;
  189. snprintf(stat_lines[devices[chip].slot] + len, 256 - len, "%.1f-%3.0f ", ghash, devices[chip].mhz);
  190. shares_total += shares_found;
  191. shares_first += chip < 4 ? shares_found : 0;
  192. shares_last += chip > 3 ? shares_found : 0;
  193. }
  194. sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
  195. applog(LOG_WARNING, "%s", line);
  196. for(i = 0; i < 32; i++)
  197. if(strlen(stat_lines[i])) {
  198. len = strlen(stat_lines[i]);
  199. ghsum = 0;
  200. gh1h = 0;
  201. gh2h = 0;
  202. for(k = 0; k < 4; k++) {
  203. gh1h += gh[i][k];
  204. gh2h += gh[i][k+4];
  205. ghsum += gh[i][k] + gh[i][k+4];
  206. }
  207. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  208. applog(LOG_WARNING, "%s", stat_lines[i]);
  209. }
  210. long_out_t = now.tv_sec;
  211. }
  212. return hashes;
  213. }
  214. double shares_to_ghashes(int shares, int seconds) {
  215. return (double)shares / (double)seconds * 4.84387; //orig: 4.77628
  216. }
  217. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
  218. int j;
  219. int shares_found = 0;
  220. for(j = 0; j < BITFURY_STAT_N; j++) {
  221. if (now.tv_sec - stat_ts[j] < stat) {
  222. shares_found++;
  223. }
  224. }
  225. return shares_found;
  226. }
  227. static bool bitfury_prepare(struct thr_info *thr)
  228. {
  229. struct cgpu_info *cgpu = thr->cgpu;
  230. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  231. applog(LOG_INFO, "INFO bitfury_prepare");
  232. return true;
  233. }
  234. static void bitfury_shutdown(struct thr_info *thr)
  235. {
  236. int chip_n;
  237. chip_n = thr->cgpu->chip_n;
  238. applog(LOG_INFO, "INFO bitfury_shutdown");
  239. libbitfury_shutdownChips(thr->cgpu->devices, chip_n);
  240. }
  241. struct device_drv bitfury_drv = {
  242. .dname = "bitfury",
  243. .name = "BITFURY",
  244. .drv_detect = bitfury_detect,
  245. .thread_prepare = bitfury_prepare,
  246. .scanwork = bitfury_scanHash,
  247. .thread_shutdown = bitfury_shutdown,
  248. .minerloop = hash_queued_work,
  249. };