driver-bitfury.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * device-bitfury.c - device functions for Bitfury chip/board library
  3. *
  4. * Copyright (c) 2013 bitfury
  5. * Copyright (c) 2013 legkodymov
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. */
  26. #include "miner.h"
  27. #include <unistd.h>
  28. #include <sha2.h>
  29. #include "libbitfury.h"
  30. #include "util.h"
  31. #define GOLDEN_BACKLOG 5
  32. struct device_drv bitfury_drv;
  33. // Forward declarations
  34. static void bitfury_disable(struct thr_info* thr);
  35. static bool bitfury_prepare(struct thr_info *thr);
  36. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now);
  37. double shares_to_ghashes(int shares, int seconds);
  38. static void bitfury_detect(void)
  39. {
  40. int chip_n;
  41. int i;
  42. struct cgpu_info *bitfury_info;
  43. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  44. bitfury_info->drv = &bitfury_drv;
  45. bitfury_info->threads = 1;
  46. applog(LOG_INFO, "INFO: bitfury_detect");
  47. chip_n = libbitfury_detectChips(bitfury_info->devices);
  48. if (!chip_n) {
  49. applog(LOG_WARNING, "No Bitfury chips detected!");
  50. return;
  51. } else {
  52. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  53. }
  54. bitfury_info->chip_n = chip_n;
  55. add_cgpu(bitfury_info);
  56. }
  57. static uint32_t bitfury_checkNonce(struct work *work, uint32_t nonce)
  58. {
  59. applog(LOG_INFO, "INFO: bitfury_checkNonce");
  60. }
  61. static int64_t bitfury_scanHash(struct thr_info *thr)
  62. {
  63. static struct bitfury_device *devices; // TODO Move somewhere to appropriate place
  64. int chip_n;
  65. int chip;
  66. uint64_t hashes = 0;
  67. struct timeval now;
  68. unsigned char line[2048];
  69. int short_stat = 10;
  70. static time_t short_out_t;
  71. int long_stat = 1800;
  72. static time_t long_out_t;
  73. int long_long_stat = 60 * 30;
  74. static time_t long_long_out_t;
  75. static first = 0; //TODO Move to detect()
  76. int i;
  77. devices = thr->cgpu->devices;
  78. chip_n = thr->cgpu->chip_n;
  79. if (!first) {
  80. for (i = 0; i < chip_n; i++) {
  81. devices[i].osc6_bits = 54;
  82. }
  83. for (i = 0; i < chip_n; i++) {
  84. send_reinit(devices[i].slot, devices[i].fasync, devices[i].osc6_bits);
  85. }
  86. }
  87. first = 1;
  88. for (chip = 0; chip < chip_n; chip++) {
  89. devices[chip].job_switched = 0;
  90. if(!devices[chip].work) {
  91. devices[chip].work = get_queued(thr->cgpu);
  92. if (devices[chip].work == NULL) {
  93. return 0;
  94. }
  95. work_to_payload(&(devices[chip].payload), devices[chip].work);
  96. }
  97. }
  98. libbitfury_sendHashData(devices, chip_n);
  99. nmsleep(5);
  100. cgtime(&now);
  101. chip = 0;
  102. for (;chip < chip_n; chip++) {
  103. if (devices[chip].job_switched) {
  104. int i,j;
  105. int *res = devices[chip].results;
  106. struct work *work = devices[chip].work;
  107. struct work *owork = devices[chip].owork;
  108. struct work *o2work = devices[chip].o2work;
  109. i = devices[chip].results_n;
  110. for (j = i - 1; j >= 0; j--) {
  111. if (owork) {
  112. submit_nonce(thr, owork, bswap_32(res[j]));
  113. devices[chip].stat_ts[devices[chip].stat_counter++] =
  114. now.tv_sec;
  115. if (devices[chip].stat_counter == BITFURY_STAT_N) {
  116. devices[chip].stat_counter = 0;
  117. }
  118. }
  119. if (o2work) {
  120. // TEST
  121. //submit_nonce(thr, owork, bswap_32(res[j]));
  122. }
  123. }
  124. devices[chip].results_n = 0;
  125. devices[chip].job_switched = 0;
  126. if (devices[chip].old_nonce && o2work) {
  127. submit_nonce(thr, o2work, bswap_32(devices[chip].old_nonce));
  128. i++;
  129. }
  130. if (devices[chip].future_nonce) {
  131. submit_nonce(thr, work, bswap_32(devices[chip].future_nonce));
  132. i++;
  133. }
  134. if (o2work)
  135. work_completed(thr->cgpu, o2work);
  136. devices[chip].o2work = devices[chip].owork;
  137. devices[chip].owork = devices[chip].work;
  138. devices[chip].work = NULL;
  139. hashes += 0xffffffffull * i;
  140. }
  141. }
  142. if (now.tv_sec - short_out_t > short_stat) {
  143. int shares_first = 0, shares_last = 0, shares_total = 0;
  144. char stat_lines[32][256] = {0};
  145. int len, k;
  146. double gh[32][8] = {0};
  147. double ghsum = 0, gh1h = 0, gh2h = 0;
  148. unsigned strange_counter = 0;
  149. for (chip = 0; chip < chip_n; chip++) {
  150. int shares_found = calc_stat(devices[chip].stat_ts, short_stat, now);
  151. double ghash;
  152. len = strlen(stat_lines[devices[chip].slot]);
  153. ghash = shares_to_ghashes(shares_found, short_stat);
  154. gh[devices[chip].slot][chip & 0x07] = ghash;
  155. snprintf(stat_lines[devices[chip].slot] + len, 256 - len, "%.1f-%3.0f ", ghash, devices[chip].mhz);
  156. if(short_out_t && ghash < 0.5) {
  157. applog(LOG_WARNING, "Chip_id %d FREQ CHANGE\n", chip);
  158. send_freq(devices[chip].slot, devices[chip].fasync, devices[chip].osc6_bits - 1);
  159. nmsleep(1);
  160. send_freq(devices[chip].slot, devices[chip].fasync, devices[chip].osc6_bits);
  161. }
  162. shares_total += shares_found;
  163. shares_first += chip < 4 ? shares_found : 0;
  164. shares_last += chip > 3 ? shares_found : 0;
  165. strange_counter += devices[chip].strange_counter;
  166. devices[chip].strange_counter = 0;
  167. }
  168. sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
  169. applog(LOG_WARNING, line);
  170. sprintf(line, "stranges: %u", strange_counter);
  171. applog(LOG_WARNING, line);
  172. for(i = 0; i < 32; i++)
  173. if(strlen(stat_lines[i])) {
  174. len = strlen(stat_lines[i]);
  175. ghsum = 0;
  176. gh1h = 0;
  177. gh2h = 0;
  178. for(k = 0; k < 4; k++) {
  179. gh1h += gh[i][k];
  180. gh2h += gh[i][k+4];
  181. ghsum += gh[i][k] + gh[i][k+4];
  182. }
  183. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  184. applog(LOG_WARNING, stat_lines[i]);
  185. }
  186. short_out_t = now.tv_sec;
  187. }
  188. if (now.tv_sec - long_out_t > long_stat) {
  189. int shares_first = 0, shares_last = 0, shares_total = 0;
  190. char stat_lines[32][256] = {0};
  191. int len, k;
  192. double gh[32][8] = {0};
  193. double ghsum = 0, gh1h = 0, gh2h = 0;
  194. for (chip = 0; chip < chip_n; chip++) {
  195. int shares_found = calc_stat(devices[chip].stat_ts, long_stat, now);
  196. double ghash;
  197. len = strlen(stat_lines[devices[chip].slot]);
  198. ghash = shares_to_ghashes(shares_found, long_stat);
  199. gh[devices[chip].slot][chip & 0x07] = ghash;
  200. snprintf(stat_lines[devices[chip].slot] + len, 256 - len, "%.1f-%3.0f ", ghash, devices[chip].mhz);
  201. shares_total += shares_found;
  202. shares_first += chip < 4 ? shares_found : 0;
  203. shares_last += chip > 3 ? shares_found : 0;
  204. }
  205. sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
  206. applog(LOG_WARNING, line);
  207. for(i = 0; i < 32; i++)
  208. if(strlen(stat_lines[i])) {
  209. len = strlen(stat_lines[i]);
  210. ghsum = 0;
  211. gh1h = 0;
  212. gh2h = 0;
  213. for(k = 0; k < 4; k++) {
  214. gh1h += gh[i][k];
  215. gh2h += gh[i][k+4];
  216. ghsum += gh[i][k] + gh[i][k+4];
  217. }
  218. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  219. applog(LOG_WARNING, stat_lines[i]);
  220. }
  221. long_out_t = now.tv_sec;
  222. }
  223. return hashes;
  224. }
  225. double shares_to_ghashes(int shares, int seconds) {
  226. return (double)shares / (double)seconds * 4.84387; //orig: 4.77628
  227. }
  228. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
  229. int j;
  230. int shares_found = 0;
  231. for(j = 0; j < BITFURY_STAT_N; j++) {
  232. if (now.tv_sec - stat_ts[j] < stat) {
  233. shares_found++;
  234. }
  235. }
  236. return shares_found;
  237. }
  238. static void bitfury_statline_before(char *buf, struct cgpu_info *cgpu)
  239. {
  240. applog(LOG_INFO, "INFO bitfury_statline_before");
  241. }
  242. static bool bitfury_prepare(struct thr_info *thr)
  243. {
  244. struct timeval now;
  245. struct cgpu_info *cgpu = thr->cgpu;
  246. cgtime(&now);
  247. get_datestamp(cgpu->init, &now);
  248. applog(LOG_INFO, "INFO bitfury_prepare");
  249. return true;
  250. }
  251. static void bitfury_shutdown(struct thr_info *thr)
  252. {
  253. int chip_n;
  254. int i;
  255. chip_n = thr->cgpu->chip_n;
  256. applog(LOG_INFO, "INFO bitfury_shutdown");
  257. libbitfury_shutdownChips(thr->cgpu->devices, chip_n);
  258. }
  259. static void bitfury_disable(struct thr_info *thr)
  260. {
  261. applog(LOG_INFO, "INFO bitfury_disable");
  262. }
  263. struct device_drv bitfury_drv = {
  264. .drv_id = DRIVER_BITFURY,
  265. .dname = "bitfury",
  266. .name = "BITFURY",
  267. .drv_detect = bitfury_detect,
  268. .get_statline_before = bitfury_statline_before,
  269. .thread_prepare = bitfury_prepare,
  270. .scanwork = bitfury_scanHash,
  271. .thread_shutdown = bitfury_shutdown,
  272. .hash_work = hash_queued_work,
  273. };