driver-ztex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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(void)
  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_ztex = ztex_devices[i]->dev;
  49. ztex->threads = 1;
  50. add_cgpu(ztex);
  51. applog(LOG_WARNING,"%s: Found Ztex, mark as %d", ztex->device_ztex->repr, ztex->device_id);
  52. }
  53. if (cnt > 0)
  54. libztex_freeDevList(ztex_devices);
  55. }
  56. static bool ztex_updateFreq(struct libztex_device* ztex)
  57. {
  58. int i, maxM, bestM;
  59. double bestR, r;
  60. for (i = 0; i < ztex->freqMaxM; i++)
  61. if (ztex->maxErrorRate[i + 1] * i < ztex->maxErrorRate[i] * (i + 20))
  62. ztex->maxErrorRate[i + 1] = ztex->maxErrorRate[i] * (1.0 + 20.0 / i);
  63. maxM = 0;
  64. while (maxM < ztex->freqMDefault && ztex->maxErrorRate[maxM + 1] < LIBZTEX_MAXMAXERRORRATE)
  65. maxM++;
  66. while (maxM < ztex->freqMaxM && ztex->errorWeight[maxM] > 150 && ztex->maxErrorRate[maxM + 1] < LIBZTEX_MAXMAXERRORRATE)
  67. maxM++;
  68. bestM = 0;
  69. bestR = 0;
  70. for (i = 0; i <= maxM; i++) {
  71. r = (i + 1 + (i == ztex->freqM? LIBZTEX_ERRORHYSTERESIS: 0)) * (1 - ztex->maxErrorRate[i]);
  72. if (r > bestR) {
  73. bestM = i;
  74. bestR = r;
  75. }
  76. }
  77. if (bestM != ztex->freqM) {
  78. libztex_selectFpga(ztex, 0);
  79. libztex_setFreq(ztex, bestM);
  80. }
  81. maxM = ztex->freqMDefault;
  82. while (maxM < ztex->freqMaxM && ztex->errorWeight[maxM + 1] > 100)
  83. maxM++;
  84. if ((bestM < (1.0 - LIBZTEX_OVERHEATTHRESHOLD) * maxM) && bestM < maxM - 1) {
  85. libztex_selectFpga(ztex, 0);
  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.",
  88. ztex->repr, (1.0 - 1.0 * bestM / maxM) * 100);
  89. return false;
  90. }
  91. return true;
  92. }
  93. static bool ztex_checkNonce(struct libztex_device *ztex,
  94. struct work *work,
  95. struct libztex_hash_data *hdata)
  96. {
  97. uint32_t *data32 = (uint32_t *)(work->data);
  98. unsigned char swap[128];
  99. uint32_t *swap32 = (uint32_t *)swap;
  100. unsigned char hash1[32];
  101. unsigned char hash2[32];
  102. uint32_t *hash2_32 = (uint32_t *)hash2;
  103. int i;
  104. #if defined(__BIGENDIAN__) || defined(MIPSEB)
  105. hdata->nonce = swab32(hdata->nonce);
  106. hdata->hash7 = swab32(hdata->hash7);
  107. #endif
  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 defined(__BIGENDIAN__) || defined(MIPSEB)
  117. if (hash2_32[7] != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
  118. #else
  119. if (swab32(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
  120. #endif
  121. ztex->errorCount[ztex->freqM] += 1.0 / ztex->numNonces;
  122. applog(LOG_DEBUG, "%s: checkNonce failed for %0.8X", ztex->repr, hdata->nonce);
  123. return false;
  124. }
  125. return true;
  126. }
  127. static uint64_t ztex_scanhash(struct thr_info *thr, struct work *work,
  128. __maybe_unused uint64_t max_nonce)
  129. {
  130. struct libztex_device *ztex;
  131. unsigned char sendbuf[44];
  132. int i, j, k;
  133. uint32_t *backlog;
  134. int backlog_p = 0, backlog_max;
  135. uint32_t *lastnonce;
  136. uint32_t nonce, noncecnt = 0;
  137. bool overflow, found, rv;
  138. struct libztex_hash_data hdata[GOLDEN_BACKLOG];
  139. ztex = thr->cgpu->device_ztex;
  140. memcpy(sendbuf, work->data + 64, 12);
  141. memcpy(sendbuf + 12, work->midstate, 32);
  142. libztex_selectFpga(ztex, 0);
  143. i = libztex_sendHashData(ztex, sendbuf);
  144. if (i < 0) {
  145. // Something wrong happened in send
  146. applog(LOG_ERR, "%s: Failed to send hash data with err %d, retrying", ztex->repr, i);
  147. usleep(500000);
  148. i = libztex_sendHashData(ztex, sendbuf);
  149. if (i < 0) {
  150. // And there's nothing we can do about it
  151. ztex_disable(thr);
  152. applog(LOG_ERR, "%s: Failed to send hash data with err %d, giving up", ztex->repr, i);
  153. return 0;
  154. }
  155. }
  156. applog(LOG_DEBUG, "%s: sent hashdata", ztex->repr);
  157. lastnonce = malloc(sizeof(uint32_t)*ztex->numNonces);
  158. if (lastnonce == NULL) {
  159. applog(LOG_ERR, "%s: failed to allocate lastnonce[%d]", ztex->repr, ztex->numNonces);
  160. return 0;
  161. }
  162. memset(lastnonce, 0, sizeof(uint32_t)*ztex->numNonces);
  163. backlog_max = ztex->numNonces * (1+ztex->extraSolutions);
  164. backlog = malloc(sizeof(uint32_t)*backlog_max);
  165. if (backlog == NULL) {
  166. applog(LOG_ERR, "%s: failed to allocate backlog[%d]", ztex->repr, backlog_max);
  167. return 0;
  168. }
  169. memset(backlog, 0, sizeof(uint32_t)*backlog_max);
  170. overflow = false;
  171. // Because we may have more than one solution on 1.15y boards, lets allocate space for these
  172. for (i=0; i<GOLDEN_BACKLOG; i++)
  173. hdata[i].goldenNonce = (uint32_t*)malloc((sizeof(uint32_t))*(ztex->extraSolutions+1));
  174. if (hdata[i].goldenNonce == NULL) {
  175. applog(LOG_ERR, "%s: failed to allocate hash_data.goldenNonce[%d]", ztex->repr, ztex->extraSolutions+1);
  176. return 0;
  177. }
  178. applog(LOG_DEBUG, "%s: entering poll loop", ztex->repr);
  179. while (!(overflow || work_restart[thr->id].restart)) {
  180. usleep(250000);
  181. if (work_restart[thr->id].restart) {
  182. applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
  183. break;
  184. }
  185. libztex_selectFpga(ztex, 0);
  186. i = libztex_readHashData(ztex, &hdata[0]);
  187. if (i < 0) {
  188. // Something wrong happened in read
  189. applog(LOG_ERR, "%s: Failed to read hash data with err %d, retrying", ztex->repr, i);
  190. usleep(500000);
  191. i = libztex_readHashData(ztex, &hdata[0]);
  192. if (i < 0) {
  193. // And there's nothing we can do about it
  194. ztex_disable(thr);
  195. applog(LOG_ERR, "%s: Failed to read hash data with err %d, giving up", ztex->repr, i);
  196. for (j=0; j<GOLDEN_BACKLOG; j++)
  197. free(hdata[j].goldenNonce);
  198. free(lastnonce);
  199. free(backlog);
  200. return 0;
  201. }
  202. }
  203. if (work_restart[thr->id].restart) {
  204. applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
  205. break;
  206. }
  207. ztex->errorCount[ztex->freqM] *= 0.995;
  208. ztex->errorWeight[ztex->freqM] = ztex->errorWeight[ztex->freqM] * 0.995 + 1.0;
  209. for (i = 0; i < ztex->numNonces; i++) {
  210. nonce = hdata[i].nonce;
  211. #if defined(__BIGENDIAN__) || defined(MIPSEB)
  212. nonce = swab32(nonce);
  213. #endif
  214. if (nonce > noncecnt)
  215. noncecnt = nonce;
  216. if (((nonce & 0x7fffffff) >> 4) < ((lastnonce[i] & 0x7fffffff) >> 4)) {
  217. applog(LOG_DEBUG, "%s: overflow nonce=%0.8x lastnonce=%0.8x", ztex->repr, nonce, lastnonce[i]);
  218. overflow = true;
  219. } else
  220. lastnonce[i] = nonce;
  221. #if !(defined(__BIGENDIAN__) || defined(MIPSEB))
  222. nonce = swab32(nonce);
  223. #endif
  224. if (!ztex_checkNonce(ztex, work, &hdata[i])) {
  225. thr->cgpu->hw_errors++;
  226. continue;
  227. }
  228. for (j=0; j<=ztex->extraSolutions; j++) {
  229. nonce = hdata[i].goldenNonce[j];
  230. if (nonce > 0) {
  231. found = false;
  232. for (k = 0; k < backlog_max; k++) {
  233. if (backlog[k] == nonce) {
  234. found = true;
  235. break;
  236. }
  237. }
  238. if (!found) {
  239. applog(LOG_DEBUG, "%s: Share found N%dE%d", ztex->repr, i, j);
  240. backlog[backlog_p++] = nonce;
  241. if (backlog_p >= backlog_max)
  242. backlog_p = 0;
  243. #if defined(__BIGENDIAN__) || defined(MIPSEB)
  244. nonce = swab32(nonce);
  245. #endif
  246. work->blk.nonce = 0xffffffff;
  247. rv = submit_nonce(thr, work, nonce);
  248. applog(LOG_DEBUG, "%s: submitted %0.8x %d", ztex->repr, nonce, rv);
  249. }
  250. }
  251. }
  252. }
  253. }
  254. 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);
  255. if (ztex->errorRate[ztex->freqM] > ztex->maxErrorRate[ztex->freqM])
  256. ztex->maxErrorRate[ztex->freqM] = ztex->errorRate[ztex->freqM];
  257. if (!ztex_updateFreq(ztex)) {
  258. // Something really serious happened, so mark this thread as dead!
  259. for (i=0; i<GOLDEN_BACKLOG; i++)
  260. free(hdata[i].goldenNonce);
  261. free(lastnonce);
  262. free(backlog);
  263. return 0;
  264. }
  265. applog(LOG_DEBUG, "%s: exit %1.8X", ztex->repr, noncecnt);
  266. work->blk.nonce = 0xffffffff;
  267. for (i=0; i<GOLDEN_BACKLOG; i++)
  268. free(hdata[i].goldenNonce);
  269. free(lastnonce);
  270. free(backlog);
  271. return noncecnt > 0? noncecnt: 1;
  272. }
  273. static void ztex_statline_before(char *buf, struct cgpu_info *cgpu)
  274. {
  275. if (cgpu->deven == DEV_ENABLED) {
  276. tailsprintf(buf, "%s | ", cgpu->device_ztex->snString);
  277. tailsprintf(buf, "%0.2fMhz | ", cgpu->device_ztex->freqM1 * (cgpu->device_ztex->freqM + 1));
  278. }
  279. }
  280. static bool ztex_prepare(struct thr_info *thr)
  281. {
  282. struct timeval now;
  283. struct cgpu_info *ztex = thr->cgpu;
  284. gettimeofday(&now, NULL);
  285. get_datestamp(ztex->init, &now);
  286. if (libztex_configureFpga(ztex->device_ztex) != 0)
  287. return false;
  288. ztex->device_ztex->freqM = -1;
  289. ztex_updateFreq(ztex->device_ztex);
  290. applog(LOG_DEBUG, "%s: prepare", ztex->device_ztex->repr);
  291. return true;
  292. }
  293. static void ztex_shutdown(struct thr_info *thr)
  294. {
  295. if (thr->cgpu->device_ztex != NULL) {
  296. applog(LOG_DEBUG, "%s: shutdown", thr->cgpu->device_ztex->repr);
  297. libztex_destroy_device(thr->cgpu->device_ztex);
  298. thr->cgpu->device_ztex = NULL;
  299. }
  300. }
  301. static void ztex_disable(struct thr_info *thr)
  302. {
  303. applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
  304. devices[thr->cgpu->device_id]->deven = DEV_DISABLED;
  305. ztex_shutdown(thr);
  306. }
  307. struct device_api ztex_api = {
  308. .dname = "ztex",
  309. .name = "PGA",
  310. .api_detect = ztex_detect,
  311. .get_statline_before = ztex_statline_before,
  312. .thread_prepare = ztex_prepare,
  313. .scanhash = ztex_scanhash,
  314. .thread_shutdown = ztex_shutdown,
  315. };