driver-bitfury.c 8.8 KB

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