driver-bitfury.c 7.9 KB

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