driver-bitfury.c 8.2 KB

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