driver-bitfury.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. #include "tm_i2c.h"
  30. #define GOLDEN_BACKLOG 5
  31. struct device_drv bitfury_drv;
  32. static
  33. bool metabank_spi_txrx(struct spi_port *port)
  34. {
  35. struct cgpu_info * const proc = port->cgpu;
  36. struct bitfury_device * const bitfury = proc->device_data;
  37. tm_i2c_set_oe(bitfury->slot);
  38. const bool rv = sys_spi_txrx(port);
  39. tm_i2c_clear_oe(bitfury->slot);
  40. return rv;
  41. }
  42. static
  43. int libbitfury_detectChips(struct bitfury_device *devices) {
  44. struct spi_port *port;
  45. int n = 0;
  46. int i, j;
  47. static bool slot_on[32];
  48. struct timespec t1, t2;
  49. struct bitfury_device dummy_bitfury;
  50. struct cgpu_info dummy_cgpu;
  51. if (tm_i2c_init() < 0) {
  52. printf("I2C init error\n");
  53. return(1);
  54. }
  55. dummy_cgpu.device_data = &dummy_bitfury;
  56. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
  57. for (i = 0; i < 32; i++) {
  58. int slot_detected = tm_i2c_detect(i) != -1;
  59. slot_on[i] = slot_detected;
  60. tm_i2c_clear_oe(i);
  61. cgsleep_ms(1);
  62. }
  63. for (i = 0; i < 32; i++) {
  64. if (slot_on[i]) {
  65. int chip_n;
  66. port = malloc(sizeof(*port));
  67. *port = *sys_spi;
  68. port->cgpu = &dummy_cgpu;
  69. port->txrx = metabank_spi_txrx;
  70. dummy_bitfury.slot = i;
  71. chip_n = libbitfury_detectChips1(port);
  72. if (chip_n)
  73. {
  74. applog(LOG_WARNING, "BITFURY slot %d: %d chips detected", i, chip_n);
  75. for (j = 0; j < chip_n; ++j)
  76. {
  77. devices[n].spi = port;
  78. devices[n].slot = i;
  79. devices[n].fasync = j;
  80. n++;
  81. }
  82. }
  83. else
  84. free(port);
  85. }
  86. }
  87. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);
  88. return n; //!!!
  89. //return 1;
  90. }
  91. void libbitfury_shutdownChips(struct bitfury_device *devices, int chip_n) {
  92. int i;
  93. for (i = 0; i < chip_n; i++) {
  94. send_shutdown(devices[i].spi, devices[i].slot, devices[i].fasync);
  95. }
  96. tm_i2c_close();
  97. }
  98. // Forward declarations
  99. static bool bitfury_prepare(struct thr_info *thr);
  100. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now);
  101. double shares_to_ghashes(int shares, int seconds);
  102. static void bitfury_detect(void)
  103. {
  104. int chip_n;
  105. struct cgpu_info *bitfury_info;
  106. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  107. bitfury_info->drv = &bitfury_drv;
  108. bitfury_info->threads = 1;
  109. applog(LOG_INFO, "INFO: bitfury_detect");
  110. spi_init();
  111. if (!sys_spi)
  112. return;
  113. chip_n = libbitfury_detectChips(bitfury_info->devices);
  114. if (!chip_n) {
  115. applog(LOG_WARNING, "No Bitfury chips detected!");
  116. return;
  117. } else {
  118. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  119. }
  120. bitfury_info->chip_n = chip_n;
  121. add_cgpu(bitfury_info);
  122. }
  123. static int64_t bitfury_scanHash(struct thr_info *thr)
  124. {
  125. static struct bitfury_device *devices; // TODO Move somewhere to appropriate place
  126. int chip_n;
  127. int chip;
  128. uint64_t hashes = 0;
  129. struct timeval now;
  130. char line[2048];
  131. int short_stat = 10;
  132. static time_t short_out_t;
  133. int long_stat = 1800;
  134. static time_t long_out_t;
  135. static int first = 0; //TODO Move to detect()
  136. int i;
  137. devices = thr->cgpu->devices;
  138. chip_n = thr->cgpu->chip_n;
  139. if (!first) {
  140. for (i = 0; i < chip_n; i++) {
  141. devices[i].osc6_bits = 54;
  142. }
  143. for (i = 0; i < chip_n; i++) {
  144. send_reinit(devices[i].spi, devices[i].slot, devices[i].fasync, devices[i].osc6_bits);
  145. }
  146. }
  147. first = 1;
  148. for (chip = 0; chip < chip_n; chip++) {
  149. devices[chip].spi = sys_spi;
  150. devices[chip].job_switched = 0;
  151. if(!devices[chip].work) {
  152. devices[chip].work = get_queued(thr->cgpu);
  153. if (devices[chip].work == NULL) {
  154. return 0;
  155. }
  156. work_to_payload(&(devices[chip].payload), devices[chip].work);
  157. }
  158. }
  159. libbitfury_sendHashData(devices, chip_n);
  160. cgsleep_ms(5);
  161. cgtime(&now);
  162. chip = 0;
  163. for (;chip < chip_n; chip++) {
  164. if (devices[chip].job_switched) {
  165. int i,j;
  166. unsigned int *res = devices[chip].results;
  167. struct work *work = devices[chip].work;
  168. struct work *owork = devices[chip].owork;
  169. struct work *o2work = devices[chip].o2work;
  170. i = devices[chip].results_n;
  171. for (j = i - 1; j >= 0; j--) {
  172. if (owork) {
  173. submit_nonce(thr, owork, bswap_32(res[j]));
  174. devices[chip].stat_ts[devices[chip].stat_counter++] =
  175. now.tv_sec;
  176. if (devices[chip].stat_counter == BITFURY_STAT_N) {
  177. devices[chip].stat_counter = 0;
  178. }
  179. }
  180. if (o2work) {
  181. // TEST
  182. //submit_nonce(thr, owork, bswap_32(res[j]));
  183. }
  184. }
  185. devices[chip].results_n = 0;
  186. devices[chip].job_switched = 0;
  187. if (devices[chip].old_nonce && o2work) {
  188. submit_nonce(thr, o2work, bswap_32(devices[chip].old_nonce));
  189. i++;
  190. }
  191. if (devices[chip].future_nonce) {
  192. submit_nonce(thr, work, bswap_32(devices[chip].future_nonce));
  193. i++;
  194. }
  195. if (o2work)
  196. work_completed(thr->cgpu, o2work);
  197. devices[chip].o2work = devices[chip].owork;
  198. devices[chip].owork = devices[chip].work;
  199. devices[chip].work = NULL;
  200. hashes += 0xffffffffull * i;
  201. }
  202. }
  203. if (now.tv_sec - short_out_t > short_stat) {
  204. int shares_first = 0, shares_last = 0, shares_total = 0;
  205. char stat_lines[32][256] = {{0}};
  206. int len, k;
  207. double gh[32][8] = {{0}};
  208. double ghsum = 0, gh1h = 0, gh2h = 0;
  209. for (chip = 0; chip < chip_n; chip++) {
  210. int shares_found = calc_stat(devices[chip].stat_ts, short_stat, now);
  211. double ghash;
  212. len = strlen(stat_lines[devices[chip].slot]);
  213. ghash = shares_to_ghashes(shares_found, short_stat);
  214. gh[devices[chip].slot][chip & 0x07] = ghash;
  215. snprintf(stat_lines[devices[chip].slot] + len, 256 - len, "%.1f-%3.0f ", ghash, devices[chip].mhz);
  216. if(short_out_t && ghash < 1.0) {
  217. applog(LOG_WARNING, "Chip_id %d FREQ CHANGE\n", chip);
  218. send_freq(devices[chip].spi, devices[chip].slot, devices[chip].fasync, devices[chip].osc6_bits - 1);
  219. cgsleep_ms(1);
  220. send_freq(devices[chip].spi, devices[chip].slot, devices[chip].fasync, devices[chip].osc6_bits);
  221. }
  222. shares_total += shares_found;
  223. shares_first += chip < 4 ? shares_found : 0;
  224. shares_last += chip > 3 ? shares_found : 0;
  225. }
  226. sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
  227. applog(LOG_WARNING, "%s", line);
  228. for(i = 0; i < 32; i++)
  229. if(strlen(stat_lines[i])) {
  230. len = strlen(stat_lines[i]);
  231. ghsum = 0;
  232. gh1h = 0;
  233. gh2h = 0;
  234. for(k = 0; k < 4; k++) {
  235. gh1h += gh[i][k];
  236. gh2h += gh[i][k+4];
  237. ghsum += gh[i][k] + gh[i][k+4];
  238. }
  239. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  240. applog(LOG_WARNING, "%s", stat_lines[i]);
  241. }
  242. short_out_t = now.tv_sec;
  243. }
  244. if (now.tv_sec - long_out_t > long_stat) {
  245. int shares_first = 0, shares_last = 0, shares_total = 0;
  246. char stat_lines[32][256] = {{0}};
  247. int len, k;
  248. double gh[32][8] = {{0}};
  249. double ghsum = 0, gh1h = 0, gh2h = 0;
  250. for (chip = 0; chip < chip_n; chip++) {
  251. int shares_found = calc_stat(devices[chip].stat_ts, long_stat, now);
  252. double ghash;
  253. len = strlen(stat_lines[devices[chip].slot]);
  254. ghash = shares_to_ghashes(shares_found, long_stat);
  255. gh[devices[chip].slot][chip & 0x07] = ghash;
  256. snprintf(stat_lines[devices[chip].slot] + len, 256 - len, "%.1f-%3.0f ", ghash, devices[chip].mhz);
  257. shares_total += shares_found;
  258. shares_first += chip < 4 ? shares_found : 0;
  259. shares_last += chip > 3 ? shares_found : 0;
  260. }
  261. sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
  262. applog(LOG_WARNING, "%s", line);
  263. for(i = 0; i < 32; i++)
  264. if(strlen(stat_lines[i])) {
  265. len = strlen(stat_lines[i]);
  266. ghsum = 0;
  267. gh1h = 0;
  268. gh2h = 0;
  269. for(k = 0; k < 4; k++) {
  270. gh1h += gh[i][k];
  271. gh2h += gh[i][k+4];
  272. ghsum += gh[i][k] + gh[i][k+4];
  273. }
  274. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  275. applog(LOG_WARNING, "%s", stat_lines[i]);
  276. }
  277. long_out_t = now.tv_sec;
  278. }
  279. return hashes;
  280. }
  281. double shares_to_ghashes(int shares, int seconds) {
  282. return (double)shares / (double)seconds * 4.84387; //orig: 4.77628
  283. }
  284. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
  285. int j;
  286. int shares_found = 0;
  287. for(j = 0; j < BITFURY_STAT_N; j++) {
  288. if (now.tv_sec - stat_ts[j] < stat) {
  289. shares_found++;
  290. }
  291. }
  292. return shares_found;
  293. }
  294. static bool bitfury_prepare(struct thr_info *thr)
  295. {
  296. struct cgpu_info *cgpu = thr->cgpu;
  297. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  298. applog(LOG_INFO, "INFO bitfury_prepare");
  299. return true;
  300. }
  301. static void bitfury_shutdown(struct thr_info *thr)
  302. {
  303. int chip_n;
  304. chip_n = thr->cgpu->chip_n;
  305. applog(LOG_INFO, "INFO bitfury_shutdown");
  306. libbitfury_shutdownChips(thr->cgpu->devices, chip_n);
  307. }
  308. struct device_drv bitfury_drv = {
  309. .dname = "bitfury",
  310. .name = "BFY",
  311. .drv_detect = bitfury_detect,
  312. .thread_prepare = bitfury_prepare,
  313. .scanwork = bitfury_scanHash,
  314. .thread_shutdown = bitfury_shutdown,
  315. .minerloop = hash_queued_work,
  316. };