ztex.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * ztex.c - cgminer worker for Ztex 1.15x fpga board
  3. *
  4. * Copyright (c) 2012 nelisky.btc@gmail.com
  5. *
  6. * This work is based upon the Java SDK provided by ztex which is
  7. * Copyright (C) 2009-2011 ZTEX GmbH.
  8. * http://www.ztex.de
  9. *
  10. * This work is based upon the icarus.c worker which is
  11. * Copyright 2012 Luke Dashjr
  12. * Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, see http://www.gnu.org/licenses/.
  25. **/
  26. #include <unistd.h>
  27. #include <sha2.h>
  28. #include "miner.h"
  29. #include "libztex.h"
  30. #define GOLDEN_BACKLOG 5
  31. struct device_api ztex_api, ztex_hotplug_api;
  32. // Forward declarations
  33. static void ztex_disable (struct thr_info* thr);
  34. static bool ztex_prepare(struct thr_info *thr);
  35. static void ztex_detect()
  36. {
  37. int cnt;
  38. int i;
  39. struct libztex_dev_list **ztex_devices;
  40. cnt = libztex_scanDevices(&ztex_devices);
  41. applog(LOG_WARNING, "Found %d ztex board(s)", cnt);
  42. for (i=0; i<cnt; i++) {
  43. if (total_devices == MAX_DEVICES)
  44. break;
  45. struct cgpu_info *ztex;
  46. ztex = calloc(1, sizeof(struct cgpu_info));
  47. ztex->api = &ztex_api;
  48. ztex->device_id = total_devices;
  49. ztex->device = ztex_devices[i]->dev;
  50. ztex->threads = 1;
  51. devices[total_devices++] = ztex;
  52. applog(LOG_WARNING,"%s: Found Ztex, mark as %d", ztex->device->repr, ztex->device_id);
  53. }
  54. if (cnt > 0)
  55. libztex_freeDevList(ztex_devices);
  56. }
  57. static bool ztex_updateFreq (struct libztex_device* ztex)
  58. {
  59. int i, maxM, bestM;
  60. double bestR, r;
  61. for (i=0; i<ztex->freqMaxM; i++)
  62. if (ztex->maxErrorRate[i+1]*i < ztex->maxErrorRate[i]*(i+20))
  63. ztex->maxErrorRate[i+1] = ztex->maxErrorRate[i]*(1.0+20.0/i);
  64. maxM = 0;
  65. while (maxM<ztex->freqMDefault && ztex->maxErrorRate[maxM+1]<LIBZTEX_MAXMAXERRORRATE)
  66. maxM++;
  67. while (maxM<ztex->freqMaxM && ztex->errorWeight[maxM]>150 && ztex->maxErrorRate[maxM+1]<LIBZTEX_MAXMAXERRORRATE)
  68. maxM++;
  69. bestM = 0;
  70. bestR = 0;
  71. for (i=0; i<=maxM; i++) {
  72. r = (i + 1 + ( i == ztex->freqM ? LIBZTEX_ERRORHYSTERESIS : 0))*(1-ztex->maxErrorRate[i]);
  73. if (r > bestR) {
  74. bestM = i;
  75. bestR = r;
  76. }
  77. }
  78. if (bestM != ztex->freqM)
  79. libztex_setFreq(ztex, bestM);
  80. maxM = ztex->freqMDefault;
  81. while (maxM<ztex->freqMaxM && ztex->errorWeight[maxM+1] > 100)
  82. maxM++;
  83. if ((bestM < (1.0-LIBZTEX_OVERHEATTHRESHOLD) * maxM) && bestM < maxM - 1) {
  84. libztex_resetFpga(ztex);
  85. applog(LOG_ERR, "%s: frequency drop of %.1f%% detect. This may be caused by overheating. FPGA is shut down to prevent damage.", ztex->repr, (1.0-1.0*bestM/maxM)*100);
  86. return false;
  87. }
  88. return true;
  89. }
  90. static bool ztex_checkNonce (struct libztex_device *ztex,
  91. struct work *work,
  92. struct libztex_hash_data *hdata)
  93. {
  94. uint32_t *data32 = (uint32_t *)(work->data);
  95. unsigned char swap[128];
  96. uint32_t *swap32 = (uint32_t *)swap;
  97. unsigned char hash1[32];
  98. unsigned char hash2[32];
  99. uint32_t *hash2_32 = (uint32_t *)hash2;
  100. int i;
  101. #if defined(__BIGENDIAN__) || defined(MIPSEB)
  102. hdata->nonce = swab32(hdata->nonce);
  103. hdata->hash7 = swab32(hdata->hash7);
  104. #endif
  105. work->data[64 + 12 + 0] = (hdata->nonce >> 0) & 0xff;
  106. work->data[64 + 12 + 1] = (hdata->nonce >> 8) & 0xff;
  107. work->data[64 + 12 + 2] = (hdata->nonce >> 16) & 0xff;
  108. work->data[64 + 12 + 3] = (hdata->nonce >> 24) & 0xff;
  109. for (i = 0; i < 80 / 4; i++)
  110. swap32[i] = swab32(data32[i]);
  111. sha2(swap, 80, hash1, false);
  112. sha2(hash1, 32, hash2, false);
  113. #if defined(__BIGENDIAN__) || defined(MIPSEB)
  114. if (hash2_32[7] != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
  115. #else
  116. if (swab32(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
  117. #endif
  118. ztex->errorCount[ztex->freqM] += 1.0/ztex->numNonces;
  119. applog(LOG_DEBUG, "%s: checkNonce failed for %0.8X", ztex->repr, hdata->nonce);
  120. return false;
  121. }
  122. return true;
  123. }
  124. static uint64_t ztex_scanhash(struct thr_info *thr, struct work *work,
  125. __maybe_unused uint64_t max_nonce)
  126. {
  127. struct libztex_device *ztex;
  128. unsigned char sendbuf[44];
  129. int i, j;
  130. uint32_t backlog[GOLDEN_BACKLOG];
  131. int backlog_p = 0;
  132. uint32_t lastnonce[GOLDEN_BACKLOG], nonce, noncecnt = 0;
  133. bool overflow, found, rv;
  134. struct libztex_hash_data hdata[GOLDEN_BACKLOG];
  135. ztex = thr->cgpu->device;
  136. memcpy(sendbuf, work->data + 64, 12);
  137. memcpy(sendbuf+12, work->midstate, 32);
  138. memset(backlog, 0, sizeof(backlog));
  139. i = libztex_sendHashData(ztex, sendbuf);
  140. if (i < 0) {
  141. // Something wrong happened in send
  142. applog(LOG_ERR, "%s: Failed to send hash data with err %d, retrying", ztex->repr, i);
  143. usleep(500000);
  144. i = libztex_sendHashData(ztex, sendbuf);
  145. if (i < 0) {
  146. // And there's nothing we can do about it
  147. ztex_disable(thr);
  148. applog(LOG_ERR, "%s: Failed to send hash data with err %d, giving up", ztex->repr, i);
  149. return 0;
  150. }
  151. }
  152. applog(LOG_DEBUG, "sent hashdata");
  153. for (i=0; i<ztex->numNonces; i++)
  154. lastnonce[i] = 0;
  155. overflow = false;
  156. while (!(overflow || work_restart[thr->id].restart)) {
  157. usleep(250000);
  158. if (work_restart[thr->id].restart) {
  159. applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
  160. break;
  161. }
  162. i = libztex_readHashData(ztex, &hdata[0]);
  163. if (i < 0) {
  164. // Something wrong happened in read
  165. applog(LOG_ERR, "%s: Failed to read hash data with err %d, retrying", ztex->repr, i);
  166. usleep(500000);
  167. i = libztex_readHashData(ztex, &hdata[0]);
  168. if (i < 0) {
  169. // And there's nothing we can do about it
  170. ztex_disable(thr);
  171. applog(LOG_ERR, "%s: Failed to read hash data with err %d, giving up", ztex->repr, i);
  172. return 0;
  173. }
  174. }
  175. if (work_restart[thr->id].restart) {
  176. applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
  177. break;
  178. }
  179. ztex->errorCount[ztex->freqM] *= 0.995;
  180. ztex->errorWeight[ztex->freqM] = ztex->errorWeight[ztex->freqM] * 0.995 + 1.0;
  181. for (i=0; i<ztex->numNonces; i++) {
  182. nonce = swab32(hdata[i].nonce);
  183. if (nonce > noncecnt)
  184. noncecnt = nonce;
  185. if ((nonce >> 4) < (lastnonce[i] >> 4)) {
  186. overflow = true;
  187. } else {
  188. lastnonce[i] = nonce;
  189. }
  190. if (!ztex_checkNonce(ztex, work, &hdata[i])) {
  191. thr->cgpu->hw_errors++;
  192. continue;
  193. }
  194. nonce = hdata[i].goldenNonce;
  195. if (nonce > 0) {
  196. found = false;
  197. for (j=0; j<GOLDEN_BACKLOG; j++) {
  198. if (backlog[j] == nonce) {
  199. found = true;
  200. break;
  201. }
  202. }
  203. if (!found) {
  204. applog(LOG_DEBUG, "%s: Share found", ztex->repr);
  205. backlog[backlog_p++] = nonce;
  206. if (backlog_p >= GOLDEN_BACKLOG)
  207. backlog_p = 0;
  208. #if defined(__BIGENDIAN__) || defined(MIPSEB)
  209. nonce = swab32(nonce);
  210. #endif
  211. work->blk.nonce = 0xffffffff;
  212. rv = submit_nonce(thr, work, nonce);
  213. applog(LOG_DEBUG, "%s: submitted %0.8x %d", ztex->repr, nonce, rv);
  214. }
  215. }
  216. }
  217. }
  218. ztex->errorRate[ztex->freqM] = ztex->errorCount[ztex->freqM] / ztex->errorWeight[ztex->freqM] * (ztex->errorWeight[ztex->freqM]<100 ? ztex->errorWeight[ztex->freqM]*0.01 : 1.0);
  219. if (ztex->errorRate[ztex->freqM] > ztex->maxErrorRate[ztex->freqM])
  220. ztex->maxErrorRate[ztex->freqM] = ztex->errorRate[ztex->freqM];
  221. if (!ztex_updateFreq(ztex))
  222. // Something really serious happened, so mark this thread as dead!
  223. return 0;
  224. applog(LOG_DEBUG, "%s: exit %1.8X", ztex->repr, noncecnt);
  225. work->blk.nonce = 0xffffffff;
  226. return noncecnt > 0 ? noncecnt : 1;
  227. }
  228. static void ztex_statline_before(char *buf, struct cgpu_info *cgpu)
  229. {
  230. if (cgpu->deven == DEV_ENABLED) {
  231. tailsprintf(buf, "%s | ", cgpu->device->snString);
  232. tailsprintf(buf, "%0.2fMhz | ", cgpu->device->freqM1 * (cgpu->device->freqM + 1));
  233. }
  234. }
  235. static bool ztex_prepare(struct thr_info *thr)
  236. {
  237. struct timeval now;
  238. struct cgpu_info *ztex = thr->cgpu;
  239. gettimeofday(&now, NULL);
  240. get_datestamp(ztex->init, &now);
  241. if (libztex_configureFpga(ztex->device) != 0)
  242. return false;
  243. ztex->device->freqM = -1;
  244. ztex_updateFreq(ztex->device);
  245. applog(LOG_DEBUG, "%s: prepare", ztex->device->repr);
  246. return true;
  247. }
  248. static void ztex_shutdown(struct thr_info *thr)
  249. {
  250. if (thr->cgpu->device != NULL) {
  251. applog(LOG_DEBUG, "%s: shutdown", thr->cgpu->device->repr);
  252. libztex_destroy_device(thr->cgpu->device);
  253. thr->cgpu->device = NULL;
  254. }
  255. }
  256. static void ztex_disable (struct thr_info *thr)
  257. {
  258. applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device->repr);
  259. devices[thr->cgpu->device_id]->deven = DEV_DISABLED;
  260. ztex_shutdown(thr);
  261. }
  262. struct device_api ztex_api = {
  263. .name = "ZTX",
  264. .api_detect = ztex_detect,
  265. .get_statline_before = ztex_statline_before,
  266. .thread_prepare = ztex_prepare,
  267. .scanhash = ztex_scanhash,
  268. .thread_shutdown = ztex_shutdown,
  269. };