driver-bitforce.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. applog(LOG_DEBUG, "BitForce Detect: Attempting to open %s", devpath);
  45. int fdDev = BFopen(devpath);
  46. if (unlikely(fdDev == -1)) {
  47. applog(LOG_ERR, "BitForce Detect: Failed to open %s", devpath);
  48. return false;
  49. }
  50. BFwrite(fdDev, "ZGX", 3);
  51. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  52. if (unlikely(!pdevbuf[0])) {
  53. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  54. return 0;
  55. }
  56. BFclose(fdDev);
  57. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  58. applog(LOG_DEBUG, "BitForce Detect: Didn't recognise BitForce on %s", devpath);
  59. return false;
  60. }
  61. // We have a real BitForce!
  62. struct cgpu_info *bitforce;
  63. bitforce = calloc(1, sizeof(*bitforce));
  64. bitforce->api = &bitforce_api;
  65. bitforce->device_path = strdup(devpath);
  66. bitforce->deven = DEV_ENABLED;
  67. bitforce->threads = 1;
  68. if (likely((!memcmp(pdevbuf, ">>>ID: ", 7)) && (s = strstr(pdevbuf + 3, ">>>"))))
  69. {
  70. s[0] = '\0';
  71. bitforce->name = strdup(pdevbuf + 7);
  72. }
  73. return add_cgpu(bitforce);
  74. }
  75. static char bitforce_detect_auto()
  76. {
  77. return
  78. serial_autodetect_udev (bitforce_detect_one, "BitFORCE*SHA256") ?:
  79. serial_autodetect_devserial(bitforce_detect_one, "BitFORCE_SHA256") ?:
  80. 0;
  81. }
  82. static void bitforce_detect()
  83. {
  84. serial_detect_auto(bitforce_api.dname, bitforce_detect_one, bitforce_detect_auto);
  85. }
  86. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  87. {
  88. float gt = bitforce->temp;
  89. if (gt > 0)
  90. tailsprintf(buf, "%5.1fC ", gt);
  91. else
  92. tailsprintf(buf, " ", gt);
  93. tailsprintf(buf, " | ");
  94. }
  95. static bool bitforce_thread_prepare(struct thr_info *thr)
  96. {
  97. struct cgpu_info *bitforce = thr->cgpu;
  98. struct timeval now;
  99. int fdDev = BFopen(bitforce->device_path);
  100. if (unlikely(-1 == fdDev)) {
  101. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  102. return false;
  103. }
  104. bitforce->device_fd = fdDev;
  105. applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
  106. gettimeofday(&now, NULL);
  107. get_datestamp(bitforce->init, &now);
  108. return true;
  109. }
  110. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
  111. {
  112. struct cgpu_info *bitforce = thr->cgpu;
  113. int fdDev = bitforce->device_fd;
  114. char pdevbuf[0x100];
  115. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  116. int i;
  117. char *pnoncebuf;
  118. char *s;
  119. uint32_t nonce;
  120. BFwrite(fdDev, "ZDX", 3);
  121. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  122. if (unlikely(!pdevbuf[0])) {
  123. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  124. return 0;
  125. }
  126. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  127. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  128. return 0;
  129. }
  130. memcpy(ob + 8, work->midstate, 32);
  131. memcpy(ob + 8 + 32, work->data + 64, 12);
  132. BFwrite(fdDev, ob, 60);
  133. if (opt_debug) {
  134. s = bin2hex(ob + 8, 44);
  135. applog(LOG_DEBUG, "BitForce block data: %s", s);
  136. free(s);
  137. }
  138. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  139. if (unlikely(!pdevbuf[0])) {
  140. applog(LOG_ERR, "Error reading from BitForce (block data)");
  141. return 0;
  142. }
  143. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  144. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  145. return 0;
  146. }
  147. BFwrite(fdDev, "ZLX", 3);
  148. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  149. if (unlikely(!pdevbuf[0])) {
  150. applog(LOG_ERR, "Error reading from BitForce (ZKX)");
  151. return 0;
  152. }
  153. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  154. float temp = strtof(s + 1, NULL);
  155. if (temp > 0) {
  156. bitforce->temp = temp;
  157. if (temp > bitforce->cutofftemp) {
  158. applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
  159. bitforce->deven = DEV_RECOVER;
  160. bitforce->device_last_not_well = time(NULL);
  161. bitforce->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  162. bitforce->dev_thermal_cutoff_count++;
  163. }
  164. }
  165. }
  166. usleep(4500000);
  167. i = 4500;
  168. while (1) {
  169. BFwrite(fdDev, "ZFX", 3);
  170. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  171. if (unlikely(!pdevbuf[0])) {
  172. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  173. return 0;
  174. }
  175. if (pdevbuf[0] != 'B')
  176. break;
  177. usleep(10000);
  178. i += 10;
  179. }
  180. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  181. work->blk.nonce = 0xffffffff;
  182. if (pdevbuf[2] == '-')
  183. return 0xffffffff;
  184. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  185. applog(LOG_ERR, "BitForce result reports: %s", pdevbuf);
  186. return 0;
  187. }
  188. pnoncebuf = &pdevbuf[12];
  189. while (1) {
  190. hex2bin((void*)&nonce, pnoncebuf, 4);
  191. #ifndef __BIG_ENDIAN__
  192. nonce = swab32(nonce);
  193. #endif
  194. submit_nonce(thr, work, nonce);
  195. if (pnoncebuf[8] != ',')
  196. break;
  197. pnoncebuf += 9;
  198. }
  199. return 0xffffffff;
  200. }
  201. struct device_api bitforce_api = {
  202. .dname = "bitforce",
  203. .name = "BFL",
  204. .api_detect = bitforce_detect,
  205. .get_statline_before = get_bitforce_statline_before,
  206. .thread_prepare = bitforce_thread_prepare,
  207. .scanhash = bitforce_scanhash,
  208. };