ztex.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. struct cgpu_info *ztex;
  41. ztex = calloc(1, sizeof(struct cgpu_info));
  42. ztex->api = &ztex_hotplug_api;
  43. ztex->threads = 1;
  44. devices[total_devices++] = ztex;
  45. cnt = libztex_scanDevices(&ztex_devices);
  46. applog(LOG_WARNING, "Found %d ztex board(s)", cnt);
  47. for (i=0; i<cnt; i++) {
  48. if (total_devices == MAX_DEVICES)
  49. break;
  50. ztex = calloc(1, sizeof(struct cgpu_info));
  51. ztex->api = &ztex_api;
  52. ztex->device_id = total_devices;
  53. ztex->device = ztex_devices[i]->dev;
  54. ztex->threads = 1;
  55. devices[total_devices++] = ztex;
  56. applog(LOG_WARNING,"%s: Found Ztex, mark as %d", ztex->device->repr, ztex->device_id);
  57. }
  58. if (cnt > 0) {
  59. libztex_freeDevList(ztex_devices);
  60. }
  61. }
  62. static bool ztex_updateFreq (struct libztex_device* ztex)
  63. {
  64. int i, maxM, bestM;
  65. double bestR, r;
  66. for (i=0; i<ztex->freqMaxM; i++) {
  67. if (ztex->maxErrorRate[i+1]*i < ztex->maxErrorRate[i]*(i+20))
  68. ztex->maxErrorRate[i+1] = ztex->maxErrorRate[i]*(1.0+20.0/i);
  69. }
  70. maxM = 0;
  71. while (maxM<ztex->freqMDefault && ztex->maxErrorRate[maxM+1]<LIBZTEX_MAXMAXERRORRATE)
  72. maxM++;
  73. while (maxM<ztex->freqMaxM && ztex->errorWeight[maxM]>150 && ztex->maxErrorRate[maxM+1]<LIBZTEX_MAXMAXERRORRATE)
  74. maxM++;
  75. bestM = 0;
  76. bestR = 0;
  77. for (i=0; i<=maxM; i++) {
  78. r = (i + 1 + ( i == ztex->freqM ? LIBZTEX_ERRORHYSTERESIS : 0))*(1-ztex->maxErrorRate[i]);
  79. if (r > bestR) {
  80. bestM = i;
  81. bestR = r;
  82. }
  83. }
  84. if (bestM != ztex->freqM) {
  85. libztex_setFreq(ztex, bestM);
  86. }
  87. maxM = ztex->freqMDefault;
  88. while (maxM<ztex->freqMaxM && ztex->errorWeight[maxM+1] > 100)
  89. maxM++;
  90. if ((bestM < (1.0-LIBZTEX_OVERHEATTHRESHOLD) * maxM) && bestM < maxM - 1) {
  91. libztex_resetFpga(ztex);
  92. 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);
  93. return false;
  94. }
  95. return true;
  96. }
  97. static bool ztex_checkNonce (struct libztex_device *ztex,
  98. struct work *work,
  99. struct libztex_hash_data *hdata)
  100. {
  101. uint32_t *data32 = (uint32_t *)(work->data);
  102. unsigned char swap[128];
  103. uint32_t *swap32 = (uint32_t *)swap;
  104. unsigned char hash1[32];
  105. unsigned char hash2[32];
  106. uint32_t *hash2_32 = (uint32_t *)hash2;
  107. int i;
  108. work->data[64 + 12 + 0] = (hdata->nonce >> 0) & 0xff;
  109. work->data[64 + 12 + 1] = (hdata->nonce >> 8) & 0xff;
  110. work->data[64 + 12 + 2] = (hdata->nonce >> 16) & 0xff;
  111. work->data[64 + 12 + 3] = (hdata->nonce >> 24) & 0xff;
  112. for (i = 0; i < 80 / 4; i++)
  113. swap32[i] = swab32(data32[i]);
  114. sha2(swap, 80, hash1, false);
  115. sha2(hash1, 32, hash2, false);
  116. if (swab32(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
  117. ztex->errorCount[ztex->freqM] += 1.0/ztex->numNonces;
  118. applog(LOG_DEBUG, "%s: checkNonce failed for %0.8X", ztex->repr, hdata->nonce);
  119. return false;
  120. }
  121. return true;
  122. }
  123. static uint64_t ztex_scanhash(struct thr_info *thr, struct work *work,
  124. __maybe_unused uint64_t max_nonce)
  125. {
  126. struct libztex_device *ztex;
  127. unsigned char sendbuf[44];
  128. int i, j;
  129. uint32_t backlog[GOLDEN_BACKLOG];
  130. int backlog_p = 0;
  131. uint32_t lastnonce[GOLDEN_BACKLOG], nonce, noncecnt = 0;
  132. bool overflow, found, rv;
  133. struct libztex_hash_data hdata[GOLDEN_BACKLOG];
  134. ztex = thr->cgpu->device;
  135. memcpy(sendbuf, work->data + 64, 12);
  136. memcpy(sendbuf+12, work->midstate, 32);
  137. memset(backlog, 0, sizeof(backlog));
  138. i = libztex_sendHashData(ztex, sendbuf);
  139. if (i < 0) {
  140. // Something wrong happened in send
  141. applog(LOG_ERR, "%s: Failed to send hash data with err %d", ztex->repr, i);
  142. usleep(500000);
  143. i = libztex_sendHashData(ztex, sendbuf);
  144. if (i < 0) {
  145. // And there's nothing we can do about it
  146. ztex_disable(thr);
  147. return 0;
  148. }
  149. }
  150. applog(LOG_DEBUG, "sent hashdata");
  151. for (i=0; i<ztex->numNonces; i++) {
  152. lastnonce[i] = 0;
  153. }
  154. overflow = false;
  155. while (!(overflow || work_restart[thr->id].restart)) {
  156. usleep(250000);
  157. if (work_restart[thr->id].restart) {
  158. break;
  159. }
  160. i = libztex_readHashData(ztex, &hdata[0]);
  161. if (i < 0) {
  162. // Something wrong happened in read
  163. applog(LOG_ERR, "%s: Failed to read hash data with err %d", ztex->repr, i);
  164. usleep(500000);
  165. i = libztex_readHashData(ztex, &hdata[0]);
  166. if (i < 0) {
  167. // And there's nothing we can do about it
  168. ztex_disable(thr);
  169. return 0;
  170. }
  171. }
  172. if (work_restart[thr->id].restart) {
  173. break;
  174. }
  175. ztex->errorCount[ztex->freqM] *= 0.995;
  176. ztex->errorWeight[ztex->freqM] = ztex->errorWeight[ztex->freqM] * 0.995 + 1.0;
  177. for (i=0; i<ztex->numNonces; i++) {
  178. nonce = hdata[i].nonce;
  179. if (nonce > noncecnt)
  180. noncecnt = nonce;
  181. if ((nonce >> 4) < (lastnonce[i] >> 4)) {
  182. overflow = true;
  183. } else {
  184. lastnonce[i] = nonce;
  185. }
  186. if (!ztex_checkNonce(ztex, work, &hdata[i])) {
  187. thr->cgpu->hw_errors++;
  188. continue;
  189. }
  190. nonce = hdata[i].goldenNonce;
  191. if (nonce > 0) {
  192. found = false;
  193. for (j=0; j<GOLDEN_BACKLOG; j++) {
  194. if (backlog[j] == nonce) {
  195. found = true;
  196. break;
  197. }
  198. }
  199. if (!found) {
  200. // new nonce found!
  201. backlog[backlog_p++] = nonce;
  202. if (backlog_p >= GOLDEN_BACKLOG) {
  203. backlog_p = 0;
  204. }
  205. #ifdef __BIG_ENDIAN__
  206. nonce = swab32(nonce);
  207. #endif
  208. work->blk.nonce = 0xffffffff;
  209. rv = submit_nonce(thr, work, nonce);
  210. applog(LOG_DEBUG, "submitted %0.8X %d", nonce, rv);
  211. }
  212. }
  213. }
  214. }
  215. //if (thr->id == 1 && !work_restart[thr->id]) {
  216. // ztex_disable(thr);
  217. // return 0;
  218. // }
  219. 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);
  220. if (ztex->errorRate[ztex->freqM] > ztex->maxErrorRate[ztex->freqM]) {
  221. ztex->maxErrorRate[ztex->freqM] = ztex->errorRate[ztex->freqM];
  222. }
  223. if (!ztex_updateFreq(ztex)) {
  224. // Something really serious happened, so mark this thread as dead!
  225. return 0;
  226. }
  227. applog(LOG_DEBUG, "exit %1.8X", noncecnt);
  228. work->blk.nonce = 0xffffffff;
  229. return noncecnt > 0 ? noncecnt : 1;
  230. }
  231. static bool ztex_prepare(struct thr_info *thr)
  232. {
  233. struct timeval now;
  234. struct cgpu_info *ztex = thr->cgpu;
  235. gettimeofday(&now, NULL);
  236. get_datestamp(ztex->init, &now);
  237. if (libztex_configureFpga(ztex->device) != 0) {
  238. return false;
  239. }
  240. ztex->device->freqM = -1;
  241. ztex_updateFreq(ztex->device);
  242. return true;
  243. }
  244. static void ztex_shutdown(struct thr_info *thr)
  245. {
  246. if (thr->cgpu->device != NULL) {
  247. libztex_destroy_device(thr->cgpu->device);
  248. thr->cgpu->device = NULL;
  249. }
  250. }
  251. static void ztex_disable (struct thr_info *thr)
  252. {
  253. applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device->repr);
  254. devices[thr->cgpu->device_id]->deven = DEV_DISABLED;
  255. ztex_shutdown(thr);
  256. }
  257. struct device_api ztex_api = {
  258. .name = "ZTX",
  259. .api_detect = ztex_detect,
  260. .thread_prepare = ztex_prepare,
  261. .scanhash = ztex_scanhash,
  262. .thread_shutdown = ztex_shutdown,
  263. };