driver-bitfury.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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; // 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. devices[i].osc6_bits = 54;
  151. }
  152. for (i = 0; i < chip_n; i++) {
  153. send_reinit(devices[i].spi, devices[i].slot, devices[i].fasync, devices[i].osc6_bits);
  154. }
  155. }
  156. first = 1;
  157. for (chip = 0; chip < chip_n; chip++) {
  158. devices[chip].spi = sys_spi;
  159. devices[chip].job_switched = 0;
  160. if(!devices[chip].work) {
  161. devices[chip].work = get_queued(thr->cgpu);
  162. if (devices[chip].work == NULL) {
  163. return 0;
  164. }
  165. work_to_payload(&(devices[chip].payload), devices[chip].work);
  166. }
  167. }
  168. libbitfury_sendHashData(devices, chip_n);
  169. cgsleep_ms(5);
  170. cgtime(&now);
  171. chip = 0;
  172. for (;chip < chip_n; chip++) {
  173. if (devices[chip].job_switched) {
  174. int i,j;
  175. unsigned int *res = devices[chip].results;
  176. struct work *work = devices[chip].work;
  177. struct work *owork = devices[chip].owork;
  178. struct work *o2work = devices[chip].o2work;
  179. i = devices[chip].results_n;
  180. for (j = i - 1; j >= 0; j--) {
  181. if (owork) {
  182. submit_nonce(thr, owork, bswap_32(res[j]));
  183. devices[chip].stat_ts[devices[chip].stat_counter++] =
  184. now.tv_sec;
  185. if (devices[chip].stat_counter == BITFURY_STAT_N) {
  186. devices[chip].stat_counter = 0;
  187. }
  188. }
  189. if (o2work) {
  190. // TEST
  191. //submit_nonce(thr, owork, bswap_32(res[j]));
  192. }
  193. }
  194. devices[chip].results_n = 0;
  195. devices[chip].job_switched = 0;
  196. if (devices[chip].old_nonce && o2work) {
  197. submit_nonce(thr, o2work, bswap_32(devices[chip].old_nonce));
  198. i++;
  199. }
  200. if (devices[chip].future_nonce) {
  201. submit_nonce(thr, work, bswap_32(devices[chip].future_nonce));
  202. i++;
  203. }
  204. if (o2work)
  205. work_completed(thr->cgpu, o2work);
  206. devices[chip].o2work = devices[chip].owork;
  207. devices[chip].owork = devices[chip].work;
  208. devices[chip].work = NULL;
  209. hashes += 0xffffffffull * i;
  210. }
  211. }
  212. if (now.tv_sec - short_out_t > short_stat) {
  213. int shares_first = 0, shares_last = 0, shares_total = 0;
  214. char stat_lines[32][256] = {{0}};
  215. int len, k;
  216. double gh[32][8] = {{0}};
  217. double ghsum = 0, gh1h = 0, gh2h = 0;
  218. for (chip = 0; chip < chip_n; chip++) {
  219. int shares_found = calc_stat(devices[chip].stat_ts, short_stat, now);
  220. double ghash;
  221. len = strlen(stat_lines[devices[chip].slot]);
  222. ghash = shares_to_ghashes(shares_found, short_stat);
  223. gh[devices[chip].slot][chip & 0x07] = ghash;
  224. snprintf(stat_lines[devices[chip].slot] + len, 256 - len, "%.1f-%3.0f ", ghash, devices[chip].mhz);
  225. if(short_out_t && ghash < 1.0) {
  226. applog(LOG_WARNING, "Chip_id %d FREQ CHANGE\n", chip);
  227. send_freq(devices[chip].spi, devices[chip].slot, devices[chip].fasync, devices[chip].osc6_bits - 1);
  228. cgsleep_ms(1);
  229. send_freq(devices[chip].spi, devices[chip].slot, devices[chip].fasync, devices[chip].osc6_bits);
  230. }
  231. shares_total += shares_found;
  232. shares_first += chip < 4 ? shares_found : 0;
  233. shares_last += chip > 3 ? shares_found : 0;
  234. }
  235. sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
  236. applog(LOG_WARNING, "%s", line);
  237. for(i = 0; i < 32; i++)
  238. if(strlen(stat_lines[i])) {
  239. len = strlen(stat_lines[i]);
  240. ghsum = 0;
  241. gh1h = 0;
  242. gh2h = 0;
  243. for(k = 0; k < 4; k++) {
  244. gh1h += gh[i][k];
  245. gh2h += gh[i][k+4];
  246. ghsum += gh[i][k] + gh[i][k+4];
  247. }
  248. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  249. applog(LOG_WARNING, "%s", stat_lines[i]);
  250. }
  251. short_out_t = now.tv_sec;
  252. }
  253. if (now.tv_sec - long_out_t > long_stat) {
  254. int shares_first = 0, shares_last = 0, shares_total = 0;
  255. char stat_lines[32][256] = {{0}};
  256. int len, k;
  257. double gh[32][8] = {{0}};
  258. double ghsum = 0, gh1h = 0, gh2h = 0;
  259. for (chip = 0; chip < chip_n; chip++) {
  260. int shares_found = calc_stat(devices[chip].stat_ts, long_stat, now);
  261. double ghash;
  262. len = strlen(stat_lines[devices[chip].slot]);
  263. ghash = shares_to_ghashes(shares_found, long_stat);
  264. gh[devices[chip].slot][chip & 0x07] = ghash;
  265. snprintf(stat_lines[devices[chip].slot] + len, 256 - len, "%.1f-%3.0f ", ghash, devices[chip].mhz);
  266. shares_total += shares_found;
  267. shares_first += chip < 4 ? shares_found : 0;
  268. shares_last += chip > 3 ? shares_found : 0;
  269. }
  270. sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
  271. applog(LOG_WARNING, "%s", line);
  272. for(i = 0; i < 32; i++)
  273. if(strlen(stat_lines[i])) {
  274. len = strlen(stat_lines[i]);
  275. ghsum = 0;
  276. gh1h = 0;
  277. gh2h = 0;
  278. for(k = 0; k < 4; k++) {
  279. gh1h += gh[i][k];
  280. gh2h += gh[i][k+4];
  281. ghsum += gh[i][k] + gh[i][k+4];
  282. }
  283. snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
  284. applog(LOG_WARNING, "%s", stat_lines[i]);
  285. }
  286. long_out_t = now.tv_sec;
  287. }
  288. return hashes;
  289. }
  290. double shares_to_ghashes(int shares, int seconds) {
  291. return (double)shares / (double)seconds * 4.84387; //orig: 4.77628
  292. }
  293. int calc_stat(time_t * stat_ts, time_t stat, struct timeval now) {
  294. int j;
  295. int shares_found = 0;
  296. for(j = 0; j < BITFURY_STAT_N; j++) {
  297. if (now.tv_sec - stat_ts[j] < stat) {
  298. shares_found++;
  299. }
  300. }
  301. return shares_found;
  302. }
  303. static bool bitfury_prepare(struct thr_info *thr)
  304. {
  305. struct cgpu_info *cgpu = thr->cgpu;
  306. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  307. applog(LOG_INFO, "INFO bitfury_prepare");
  308. return true;
  309. }
  310. static void bitfury_shutdown(struct thr_info *thr)
  311. {
  312. int chip_n;
  313. chip_n = thr->cgpu->chip_n;
  314. applog(LOG_INFO, "INFO bitfury_shutdown");
  315. libbitfury_shutdownChips(thr->cgpu->devices, chip_n);
  316. }
  317. struct device_drv bitfury_drv = {
  318. .dname = "bitfury",
  319. .name = "BFY",
  320. .drv_detect = bitfury_detect,
  321. .thread_prepare = bitfury_prepare,
  322. .scanwork = bitfury_scanHash,
  323. .thread_shutdown = bitfury_shutdown,
  324. .minerloop = hash_queued_work,
  325. };