ztex.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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;
  32. static void ztex_detect()
  33. {
  34. int cnt;
  35. int i;
  36. struct libztex_dev_list **ztex_devices;
  37. cnt = libztex_scanDevices(&ztex_devices);
  38. applog(LOG_WARNING, "Found %d ztex board(s)", cnt);
  39. for (i=0; i<cnt; i++) {
  40. if (total_devices == MAX_DEVICES)
  41. break;
  42. struct cgpu_info *ztex;
  43. ztex = calloc(1, sizeof(struct cgpu_info));
  44. ztex->api = &ztex_api;
  45. ztex->device_id = total_devices;
  46. ztex->device = ztex_devices[i]->dev;
  47. ztex->threads = 1;
  48. devices[total_devices++] = ztex;
  49. applog(LOG_WARNING,"%s: Found Ztex, mark as %d", ztex->device->repr, ztex->device_id);
  50. }
  51. if (cnt > 0) {
  52. libztex_freeDevList(ztex_devices);
  53. }
  54. }
  55. static bool ztex_updateFreq (struct libztex_device* ztex)
  56. {
  57. int i, maxM, bestM;
  58. double bestR, r;
  59. for (i=0; i<ztex->freqMaxM; i++) {
  60. if (ztex->maxErrorRate[i+1]*i < ztex->maxErrorRate[i]*(i+20))
  61. ztex->maxErrorRate[i+1] = ztex->maxErrorRate[i]*(1.0+20.0/i);
  62. }
  63. maxM = 0;
  64. while (maxM<ztex->freqMDefault && ztex->maxErrorRate[maxM+1]<LIBZTEX_MAXMAXERRORRATE)
  65. maxM++;
  66. //applog(LOG_WARNING, "maxM:%d freqMaxM:%d errorWeight:%f maxErrorRate:%f maxMax:%f", maxM, ztex->freqMaxM, ztex->errorWeight[maxM], ztex->maxErrorRate[maxM+1], LIBZTEX_MAXMAXERRORRATE);
  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. //applog(LOG_WARNING, "maxM:%d bestM:%d bestR:%f freqM:%d", maxM, bestM, bestR, ztex->freqM);
  79. if (bestM != ztex->freqM) {
  80. libztex_setFreq(ztex, bestM);
  81. }
  82. maxM = ztex->freqMDefault;
  83. while (maxM<ztex->freqMaxM && ztex->errorWeight[maxM+1] > 100)
  84. maxM++;
  85. if ((bestM < (1.0-LIBZTEX_OVERHEATTHRESHOLD) * maxM) && bestM < maxM - 1) {
  86. libztex_resetFpga(ztex);
  87. 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);
  88. return false;
  89. }
  90. return true;
  91. }
  92. static bool ztex_checkNonce (struct libztex_device *ztex,
  93. struct work *work,
  94. struct libztex_hash_data *hdata)
  95. {
  96. uint32_t *data32 = (uint32_t *)(work->data);
  97. unsigned char swap[128];
  98. uint32_t *swap32 = (uint32_t *)swap;
  99. unsigned char hash1[32];
  100. unsigned char hash2[32];
  101. uint32_t *hash2_32 = (uint32_t *)hash2;
  102. int i;
  103. work->data[64 + 12 + 0] = (hdata->nonce >> 0) & 0xff;
  104. work->data[64 + 12 + 1] = (hdata->nonce >> 8) & 0xff;
  105. work->data[64 + 12 + 2] = (hdata->nonce >> 16) & 0xff;
  106. work->data[64 + 12 + 3] = (hdata->nonce >> 24) & 0xff;
  107. for (i = 0; i < 80 / 4; i++)
  108. swap32[i] = swab32(data32[i]);
  109. sha2(swap, 80, hash1, false);
  110. sha2(hash1, 32, hash2, false);
  111. if (swab32(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
  112. ztex->errorCount[ztex->freqM] += 1.0/ztex->numNonces;
  113. applog(LOG_ERR, "%s: checkNonce failed for %0.8X", ztex->repr, hdata->nonce);
  114. return false;
  115. }
  116. return true;
  117. }
  118. static uint64_t ztex_scanhash(struct thr_info *thr, struct work *work,
  119. __maybe_unused uint64_t max_nonce)
  120. {
  121. struct libztex_device *ztex;
  122. unsigned char sendbuf[44];
  123. int i, j;
  124. uint32_t backlog[GOLDEN_BACKLOG];
  125. int backlog_p = 0;
  126. uint32_t lastnonce[GOLDEN_BACKLOG], nonce, noncecnt = 0;
  127. bool overflow, found, rv;
  128. struct libztex_hash_data hdata[GOLDEN_BACKLOG];
  129. ztex = thr->cgpu->device;
  130. memcpy(sendbuf, work->data + 64, 12);
  131. memcpy(sendbuf+12, work->midstate, 32);
  132. memset(backlog, 0, sizeof(backlog));
  133. libztex_sendHashData(ztex, sendbuf);
  134. applog(LOG_DEBUG, "sent hashdata");
  135. for (i=0; i<ztex->numNonces; i++) {
  136. lastnonce[i] = 0;
  137. }
  138. overflow = false;
  139. while (!(overflow || work_restart[thr->id].restart)) {
  140. usleep(250000);
  141. libztex_readHashData(ztex, &hdata[0]);
  142. ztex->errorCount[ztex->freqM] *= 0.995;
  143. ztex->errorWeight[ztex->freqM] = ztex->errorWeight[ztex->freqM] * 0.995 + 1.0;
  144. for (i=0; i<ztex->numNonces; i++) {
  145. nonce = hdata[i].nonce;
  146. if (nonce > noncecnt)
  147. noncecnt = nonce;
  148. if ((nonce >> 4) < (lastnonce[i] >> 4)) {
  149. overflow = true;
  150. } else {
  151. lastnonce[i] = nonce;
  152. }
  153. if (!ztex_checkNonce(ztex, work, &hdata[i])) {
  154. continue;
  155. }
  156. nonce = hdata[i].goldenNonce;
  157. if (nonce > 0) {
  158. found = false;
  159. for (j=0; j<GOLDEN_BACKLOG; j++) {
  160. if (backlog[j] == nonce) {
  161. found = true;
  162. break;
  163. }
  164. }
  165. if (!found) {
  166. // new nonce found!
  167. backlog[backlog_p++] = nonce;
  168. if (backlog_p >= GOLDEN_BACKLOG) {
  169. backlog_p = 0;
  170. }
  171. #ifdef __BIG_ENDIAN__
  172. nonce = swab32(nonce);
  173. #endif
  174. work->blk.nonce = 0xffffffff;
  175. rv = submit_nonce(thr, work, nonce);
  176. applog(LOG_DEBUG, "submitted %0.8X %d", nonce, rv);
  177. }
  178. }
  179. }
  180. }
  181. 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);
  182. if (ztex->errorRate[ztex->freqM] > ztex->maxErrorRate[ztex->freqM]) {
  183. ztex->maxErrorRate[ztex->freqM] = ztex->errorRate[ztex->freqM];
  184. }
  185. if (!ztex_updateFreq(ztex)) {
  186. // Something really serious happened, so mark this thread as dead!
  187. thr->cgpu->status = LIFE_DEAD;
  188. }
  189. applog(LOG_WARNING, "freqM:%d errorRate:%0.3f errorCount:%d", ztex->freqM, ztex->errorRate[ztex->freqM], ztex->errorCount[ztex->freqM]);
  190. applog(LOG_DEBUG, "exit %0.8X", noncecnt);
  191. return noncecnt;
  192. }
  193. static bool ztex_prepare(struct thr_info *thr)
  194. {
  195. struct timeval now;
  196. struct cgpu_info *ztex = thr->cgpu;
  197. gettimeofday(&now, NULL);
  198. get_datestamp(ztex->init, &now);
  199. if (libztex_configureFpga(ztex->device) != 0) {
  200. return false;
  201. }
  202. ztex->device->freqM = -1;
  203. ztex_updateFreq(ztex->device);
  204. return true;
  205. }
  206. static void ztex_shutdown(struct thr_info *thr)
  207. {
  208. if (thr->cgpu) {
  209. libztex_destroy_device(thr->cgpu->device);
  210. thr->cgpu = NULL;
  211. }
  212. }
  213. struct device_api ztex_api = {
  214. .name = "ZTX",
  215. .api_detect = ztex_detect,
  216. .thread_prepare = ztex_prepare,
  217. .scanhash = ztex_scanhash,
  218. .thread_shutdown = ztex_shutdown,
  219. };