driver-bitforce.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. * Copyright 2012 Con Kolivas
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include <limits.h>
  11. #include <pthread.h>
  12. #include <stdio.h>
  13. #include <strings.h>
  14. #include <sys/time.h>
  15. #include <unistd.h>
  16. #include "config.h"
  17. #include "fpgautils.h"
  18. #include "miner.h"
  19. struct device_api bitforce_api;
  20. #define BFopen(devpath) serial_open(devpath, 0, -1, true)
  21. static void BFgets(char *buf, size_t bufLen, int fd)
  22. {
  23. do
  24. --bufLen;
  25. while (likely(bufLen && read(fd, buf, 1) && (buf++)[0] != '\n'))
  26. ;
  27. buf[0] = '\0';
  28. }
  29. static ssize_t BFwrite2(int fd, const void *buf, ssize_t bufLen)
  30. {
  31. return write(fd, buf, bufLen);
  32. }
  33. #define BFwrite(fd, buf, bufLen) do { \
  34. if ((bufLen) != BFwrite2(fd, buf, bufLen)) { \
  35. applog(LOG_ERR, "Error writing to BitForce (" #buf ")"); \
  36. return 0; \
  37. } \
  38. } while(0)
  39. #define BFclose(fd) close(fd)
  40. static bool bitforce_detect_one(const char *devpath)
  41. {
  42. char *s;
  43. char pdevbuf[0x100];
  44. int fdDev = BFopen(devpath);
  45. if (unlikely(fdDev == -1)) {
  46. applog(LOG_DEBUG, "BitForce Detect: Failed to open %s", devpath);
  47. return false;
  48. }
  49. BFwrite(fdDev, "ZGX", 3);
  50. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  51. if (unlikely(!pdevbuf[0])) {
  52. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  53. return 0;
  54. }
  55. BFclose(fdDev);
  56. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  57. applog(LOG_DEBUG, "BitForce Detect: Didn't recognise BitForce on %s", devpath);
  58. return false;
  59. }
  60. // We have a real BitForce!
  61. struct cgpu_info *bitforce;
  62. bitforce = calloc(1, sizeof(*bitforce));
  63. bitforce->api = &bitforce_api;
  64. bitforce->device_path = strdup(devpath);
  65. bitforce->deven = DEV_ENABLED;
  66. bitforce->threads = 1;
  67. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>"))))
  68. {
  69. s[0] = '\0';
  70. bitforce->name = strdup(pdevbuf + 7);
  71. }
  72. return add_cgpu(bitforce);
  73. }
  74. static char bitforce_detect_auto()
  75. {
  76. return
  77. serial_autodetect_udev (bitforce_detect_one, "BitFORCE*SHA256") ?:
  78. serial_autodetect_devserial(bitforce_detect_one, "BitFORCE_SHA256") ?:
  79. 0;
  80. }
  81. static void bitforce_detect()
  82. {
  83. serial_detect_auto("bitforce", bitforce_detect_one, bitforce_detect_auto);
  84. }
  85. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  86. {
  87. float gt = bitforce->temp;
  88. if (gt > 0)
  89. tailsprintf(buf, "%5.1fC ", gt);
  90. else
  91. tailsprintf(buf, " ", gt);
  92. tailsprintf(buf, " | ");
  93. }
  94. static bool bitforce_thread_prepare(struct thr_info *thr)
  95. {
  96. struct cgpu_info *bitforce = thr->cgpu;
  97. struct timeval now;
  98. int fdDev = BFopen(bitforce->device_path);
  99. if (unlikely(-1 == fdDev)) {
  100. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  101. return false;
  102. }
  103. bitforce->device_fd = fdDev;
  104. applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
  105. gettimeofday(&now, NULL);
  106. get_datestamp(bitforce->init, &now);
  107. return true;
  108. }
  109. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
  110. {
  111. struct cgpu_info *bitforce = thr->cgpu;
  112. int fdDev = bitforce->device_fd;
  113. char pdevbuf[0x100];
  114. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  115. int i;
  116. char *pnoncebuf;
  117. char *s;
  118. uint32_t nonce;
  119. BFwrite(fdDev, "ZDX", 3);
  120. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  121. if (unlikely(!pdevbuf[0])) {
  122. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  123. return 0;
  124. }
  125. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  126. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  127. return 0;
  128. }
  129. memcpy(ob + 8, work->midstate, 32);
  130. memcpy(ob + 8 + 32, work->data + 64, 12);
  131. BFwrite(fdDev, ob, 60);
  132. if (opt_debug) {
  133. s = bin2hex(ob + 8, 44);
  134. applog(LOG_DEBUG, "BitForce block data: %s", s);
  135. free(s);
  136. }
  137. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  138. if (unlikely(!pdevbuf[0])) {
  139. applog(LOG_ERR, "Error reading from BitForce (block data)");
  140. return 0;
  141. }
  142. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  143. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  144. return 0;
  145. }
  146. BFwrite(fdDev, "ZLX", 3);
  147. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  148. if (unlikely(!pdevbuf[0])) {
  149. applog(LOG_ERR, "Error reading from BitForce (ZKX)");
  150. return 0;
  151. }
  152. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  153. float temp = strtof(s + 1, NULL);
  154. if (temp > 0) {
  155. bitforce->temp = temp;
  156. if (temp > bitforce->cutofftemp) {
  157. applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
  158. bitforce->deven = DEV_RECOVER;
  159. bitforce->device_last_not_well = time(NULL);
  160. bitforce->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  161. bitforce->dev_thermal_cutoff_count++;
  162. }
  163. }
  164. }
  165. usleep(4500000);
  166. i = 4500;
  167. while (1) {
  168. BFwrite(fdDev, "ZFX", 3);
  169. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  170. if (unlikely(!pdevbuf[0])) {
  171. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  172. return 0;
  173. }
  174. if (pdevbuf[0] != 'B')
  175. break;
  176. usleep(10000);
  177. i += 10;
  178. }
  179. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  180. work->blk.nonce = 0xffffffff;
  181. if (pdevbuf[2] == '-')
  182. return 0xffffffff;
  183. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  184. applog(LOG_ERR, "BitForce result reports: %s", pdevbuf);
  185. return 0;
  186. }
  187. pnoncebuf = &pdevbuf[12];
  188. while (1) {
  189. hex2bin((void*)&nonce, pnoncebuf, 4);
  190. #ifndef __BIG_ENDIAN__
  191. nonce = swab32(nonce);
  192. #endif
  193. submit_nonce(thr, work, nonce);
  194. if (pnoncebuf[8] != ',')
  195. break;
  196. pnoncebuf += 9;
  197. }
  198. return 0xffffffff;
  199. }
  200. struct device_api bitforce_api = {
  201. .dname = "bitforce",
  202. .name = "BFL",
  203. .api_detect = bitforce_detect,
  204. .get_statline_before = get_bitforce_statline_before,
  205. .thread_prepare = bitforce_thread_prepare,
  206. .scanhash = bitforce_scanhash,
  207. };