driver-bitfury.c 9.8 KB

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