driver-ztex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 "miner.h"
  27. #include <unistd.h>
  28. #include <sha2.h>
  29. #include "libztex.h"
  30. #define GOLDEN_BACKLOG 5
  31. struct device_drv ztex_drv;
  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_selectFpga(struct libztex_device* ztex)
  36. {
  37. if (ztex->root->numberOfFpgas > 1) {
  38. if (ztex->root->selectedFpga != ztex->fpgaNum)
  39. mutex_lock(&ztex->root->mutex);
  40. libztex_selectFpga(ztex);
  41. }
  42. }
  43. static void ztex_releaseFpga(struct libztex_device* ztex)
  44. {
  45. if (ztex->root->numberOfFpgas > 1) {
  46. ztex->root->selectedFpga = -1;
  47. mutex_unlock(&ztex->root->mutex);
  48. }
  49. }
  50. static void ztex_detect(void)
  51. {
  52. int cnt;
  53. int i,j;
  54. int fpgacount;
  55. struct libztex_dev_list **ztex_devices;
  56. struct libztex_device *ztex_slave;
  57. struct cgpu_info *ztex;
  58. cnt = libztex_scanDevices(&ztex_devices);
  59. if (cnt > 0)
  60. applog(LOG_WARNING, "Found %d ztex board%s", cnt, cnt > 1 ? "s" : "");
  61. for (i = 0; i < cnt; i++) {
  62. ztex = calloc(1, sizeof(struct cgpu_info));
  63. ztex->drv = &ztex_drv;
  64. ztex->device_ztex = ztex_devices[i]->dev;
  65. ztex->threads = 1;
  66. ztex->device_ztex->fpgaNum = 0;
  67. ztex->device_ztex->root = ztex->device_ztex;
  68. add_cgpu(ztex);
  69. fpgacount = libztex_numberOfFpgas(ztex->device_ztex);
  70. if (fpgacount > 1)
  71. pthread_mutex_init(&ztex->device_ztex->mutex, NULL);
  72. for (j = 1; j < fpgacount; j++) {
  73. ztex = calloc(1, sizeof(struct cgpu_info));
  74. ztex->drv = &ztex_drv;
  75. ztex_slave = calloc(1, sizeof(struct libztex_device));
  76. memcpy(ztex_slave, ztex_devices[i]->dev, sizeof(struct libztex_device));
  77. ztex->device_ztex = ztex_slave;
  78. ztex->threads = 1;
  79. ztex_slave->fpgaNum = j;
  80. ztex_slave->root = ztex_devices[i]->dev;
  81. ztex_slave->repr[strlen(ztex_slave->repr) - 1] = ('1' + j);
  82. add_cgpu(ztex);
  83. }
  84. applog(LOG_WARNING,"%s: Found Ztex (fpga count = %d) , mark as %d", ztex->device_ztex->repr, fpgacount, ztex->device_id);
  85. }
  86. if (cnt > 0)
  87. libztex_freeDevList(ztex_devices);
  88. }
  89. static bool ztex_updateFreq(struct libztex_device* ztex)
  90. {
  91. int i, maxM, bestM;
  92. double bestR, r;
  93. for (i = 0; i < ztex->freqMaxM; i++)
  94. if (ztex->maxErrorRate[i + 1] * i < ztex->maxErrorRate[i] * (i + 20))
  95. ztex->maxErrorRate[i + 1] = ztex->maxErrorRate[i] * (1.0 + 20.0 / i);
  96. maxM = 0;
  97. while (maxM < ztex->freqMDefault && ztex->maxErrorRate[maxM + 1] < LIBZTEX_MAXMAXERRORRATE)
  98. maxM++;
  99. while (maxM < ztex->freqMaxM && ztex->errorWeight[maxM] > 150 && ztex->maxErrorRate[maxM + 1] < LIBZTEX_MAXMAXERRORRATE)
  100. maxM++;
  101. bestM = 0;
  102. bestR = 0;
  103. for (i = 0; i <= maxM; i++) {
  104. r = (i + 1 + (i == ztex->freqM? LIBZTEX_ERRORHYSTERESIS: 0)) * (1 - ztex->maxErrorRate[i]);
  105. if (r > bestR) {
  106. bestM = i;
  107. bestR = r;
  108. }
  109. }
  110. if (bestM != ztex->freqM) {
  111. ztex_selectFpga(ztex);
  112. libztex_setFreq(ztex, bestM);
  113. ztex_releaseFpga(ztex);
  114. }
  115. maxM = ztex->freqMDefault;
  116. while (maxM < ztex->freqMaxM && ztex->errorWeight[maxM + 1] > 100)
  117. maxM++;
  118. if ((bestM < (1.0 - LIBZTEX_OVERHEATTHRESHOLD) * maxM) && bestM < maxM - 1) {
  119. ztex_selectFpga(ztex);
  120. libztex_resetFpga(ztex);
  121. ztex_releaseFpga(ztex);
  122. applog(LOG_ERR, "%s: frequency drop of %.1f%% detect. This may be caused by overheating. FPGA is shut down to prevent damage.",
  123. ztex->repr, (1.0 - 1.0 * bestM / maxM) * 100);
  124. return false;
  125. }
  126. return true;
  127. }
  128. static uint32_t ztex_checkNonce(struct work *work, uint32_t nonce)
  129. {
  130. uint32_t *data32 = (uint32_t *)(work->data);
  131. unsigned char swap[80];
  132. uint32_t *swap32 = (uint32_t *)swap;
  133. unsigned char hash1[32];
  134. unsigned char hash2[32];
  135. uint32_t *hash2_32 = (uint32_t *)hash2;
  136. int i;
  137. swap32[76/4] = htonl(nonce);
  138. for (i = 0; i < 76 / 4; i++)
  139. swap32[i] = swab32(data32[i]);
  140. sha2(swap, 80, hash1);
  141. sha2(hash1, 32, hash2);
  142. return htonl(hash2_32[7]);
  143. }
  144. static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
  145. __maybe_unused int64_t max_nonce)
  146. {
  147. struct libztex_device *ztex;
  148. unsigned char sendbuf[44];
  149. int i, j, k;
  150. uint32_t *backlog;
  151. int backlog_p = 0, backlog_max;
  152. uint32_t *lastnonce;
  153. uint32_t nonce, noncecnt = 0;
  154. bool overflow, found;
  155. struct libztex_hash_data hdata[GOLDEN_BACKLOG];
  156. if (thr->cgpu->deven == DEV_DISABLED)
  157. return -1;
  158. ztex = thr->cgpu->device_ztex;
  159. memcpy(sendbuf, work->data + 64, 12);
  160. memcpy(sendbuf + 12, work->midstate, 32);
  161. ztex_selectFpga(ztex);
  162. i = libztex_sendHashData(ztex, sendbuf);
  163. if (i < 0) {
  164. // Something wrong happened in send
  165. applog(LOG_ERR, "%s: Failed to send hash data with err %d, retrying", ztex->repr, i);
  166. nmsleep(500);
  167. i = libztex_sendHashData(ztex, sendbuf);
  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 send hash data with err %d, giving up", ztex->repr, i);
  172. ztex_releaseFpga(ztex);
  173. return -1;
  174. }
  175. }
  176. ztex_releaseFpga(ztex);
  177. applog(LOG_DEBUG, "%s: sent hashdata", ztex->repr);
  178. lastnonce = calloc(1, sizeof(uint32_t)*ztex->numNonces);
  179. if (lastnonce == NULL) {
  180. applog(LOG_ERR, "%s: failed to allocate lastnonce[%d]", ztex->repr, ztex->numNonces);
  181. return -1;
  182. }
  183. /* Add an extra slot for detecting dupes that lie around */
  184. backlog_max = ztex->numNonces * (2 + ztex->extraSolutions);
  185. backlog = calloc(1, sizeof(uint32_t) * backlog_max);
  186. if (backlog == NULL) {
  187. applog(LOG_ERR, "%s: failed to allocate backlog[%d]", ztex->repr, backlog_max);
  188. return -1;
  189. }
  190. overflow = false;
  191. int count = 0;
  192. int validNonces = 0;
  193. double errorCount = 0;
  194. applog(LOG_DEBUG, "%s: entering poll loop", ztex->repr);
  195. while (!(overflow || thr->work_restart)) {
  196. count++;
  197. int sleepcount = 0;
  198. while (thr->work_restart == 0 && sleepcount < 25) {
  199. nmsleep(10);
  200. sleepcount += 1;
  201. }
  202. if (thr->work_restart) {
  203. applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
  204. break;
  205. }
  206. ztex_selectFpga(ztex);
  207. i = libztex_readHashData(ztex, &hdata[0]);
  208. if (i < 0) {
  209. // Something wrong happened in read
  210. applog(LOG_ERR, "%s: Failed to read hash data with err %d, retrying", ztex->repr, i);
  211. nmsleep(500);
  212. i = libztex_readHashData(ztex, &hdata[0]);
  213. if (i < 0) {
  214. // And there's nothing we can do about it
  215. ztex_disable(thr);
  216. applog(LOG_ERR, "%s: Failed to read hash data with err %d, giving up", ztex->repr, i);
  217. free(lastnonce);
  218. free(backlog);
  219. ztex_releaseFpga(ztex);
  220. return -1;
  221. }
  222. }
  223. ztex_releaseFpga(ztex);
  224. if (thr->work_restart) {
  225. applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
  226. break;
  227. }
  228. ztex->errorCount[ztex->freqM] *= 0.995;
  229. ztex->errorWeight[ztex->freqM] = ztex->errorWeight[ztex->freqM] * 0.995 + 1.0;
  230. for (i = 0; i < ztex->numNonces; i++) {
  231. nonce = hdata[i].nonce;
  232. if (nonce > noncecnt)
  233. noncecnt = nonce;
  234. if (((0xffffffff - nonce) < (nonce - lastnonce[i])) || nonce < lastnonce[i]) {
  235. applog(LOG_DEBUG, "%s: overflow nonce=%0.8x lastnonce=%0.8x", ztex->repr, nonce, lastnonce[i]);
  236. overflow = true;
  237. } else
  238. lastnonce[i] = nonce;
  239. if (ztex_checkNonce(work, nonce) != (hdata->hash7 + 0x5be0cd19)) {
  240. applog(LOG_DEBUG, "%s: checkNonce failed for %0.8X", ztex->repr, nonce);
  241. // do not count errors in the first 500ms after sendHashData (2x250 wait time)
  242. if (count > 2) {
  243. thr->cgpu->hw_errors++;
  244. errorCount += (1.0 / ztex->numNonces);
  245. }
  246. }
  247. else
  248. validNonces++;
  249. for (j=0; j<=ztex->extraSolutions; j++) {
  250. nonce = hdata[i].goldenNonce[j];
  251. if (nonce == ztex->offsNonces) {
  252. continue;
  253. }
  254. // precheck the extraSolutions since they often fail
  255. if (j > 0 && ztex_checkNonce(work, nonce) != 0) {
  256. continue;
  257. }
  258. found = false;
  259. for (k = 0; k < backlog_max; k++) {
  260. if (backlog[k] == nonce) {
  261. found = true;
  262. break;
  263. }
  264. }
  265. if (!found) {
  266. applog(LOG_DEBUG, "%s: Share found N%dE%d", ztex->repr, i, j);
  267. backlog[backlog_p++] = nonce;
  268. if (backlog_p >= backlog_max)
  269. backlog_p = 0;
  270. work->blk.nonce = 0xffffffff;
  271. submit_nonce(thr, work, nonce);
  272. applog(LOG_DEBUG, "%s: submitted %0.8x", ztex->repr, nonce);
  273. }
  274. }
  275. }
  276. }
  277. // only add the errorCount if we had at least some valid nonces or
  278. // had no valid nonces in the last round
  279. if (errorCount > 0.0) {
  280. if (ztex->nonceCheckValid > 0 && validNonces == 0) {
  281. applog(LOG_ERR, "%s: resetting %.1f errors", ztex->repr, errorCount);
  282. }
  283. else {
  284. ztex->errorCount[ztex->freqM] += errorCount;
  285. }
  286. }
  287. // remember the number of valid nonces for the check in the next round
  288. ztex->nonceCheckValid = validNonces;
  289. 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);
  290. if (ztex->errorRate[ztex->freqM] > ztex->maxErrorRate[ztex->freqM])
  291. ztex->maxErrorRate[ztex->freqM] = ztex->errorRate[ztex->freqM];
  292. if (!ztex_updateFreq(ztex)) {
  293. // Something really serious happened, so mark this thread as dead!
  294. free(lastnonce);
  295. free(backlog);
  296. return -1;
  297. }
  298. applog(LOG_DEBUG, "%s: exit %1.8X", ztex->repr, noncecnt);
  299. work->blk.nonce = 0xffffffff;
  300. free(lastnonce);
  301. free(backlog);
  302. return noncecnt;
  303. }
  304. static void ztex_statline_before(char *buf, struct cgpu_info *cgpu)
  305. {
  306. if (cgpu->deven == DEV_ENABLED) {
  307. tailsprintf(buf, "%s-%d | ", cgpu->device_ztex->snString, cgpu->device_ztex->fpgaNum+1);
  308. tailsprintf(buf, "%0.1fMHz | ", cgpu->device_ztex->freqM1 * (cgpu->device_ztex->freqM + 1));
  309. }
  310. }
  311. static bool ztex_prepare(struct thr_info *thr)
  312. {
  313. struct timeval now;
  314. struct cgpu_info *cgpu = thr->cgpu;
  315. struct libztex_device *ztex = cgpu->device_ztex;
  316. gettimeofday(&now, NULL);
  317. get_datestamp(cgpu->init, &now);
  318. ztex_selectFpga(ztex);
  319. if (libztex_configureFpga(ztex) != 0) {
  320. libztex_resetFpga(ztex);
  321. ztex_releaseFpga(ztex);
  322. applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
  323. thr->cgpu->deven = DEV_DISABLED;
  324. return true;
  325. }
  326. ztex->freqM = ztex->freqMaxM+1;;
  327. //ztex_updateFreq(ztex);
  328. libztex_setFreq(ztex, ztex->freqMDefault);
  329. ztex_releaseFpga(ztex);
  330. applog(LOG_DEBUG, "%s: prepare", ztex->repr);
  331. return true;
  332. }
  333. static void ztex_shutdown(struct thr_info *thr)
  334. {
  335. if (thr->cgpu->device_ztex != NULL) {
  336. if (thr->cgpu->device_ztex->fpgaNum == 0)
  337. pthread_mutex_destroy(&thr->cgpu->device_ztex->mutex);
  338. applog(LOG_DEBUG, "%s: shutdown", thr->cgpu->device_ztex->repr);
  339. libztex_destroy_device(thr->cgpu->device_ztex);
  340. thr->cgpu->device_ztex = NULL;
  341. }
  342. }
  343. static void ztex_disable(struct thr_info *thr)
  344. {
  345. struct cgpu_info *cgpu;
  346. applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
  347. cgpu = get_devices(thr->cgpu->device_id);
  348. cgpu->deven = DEV_DISABLED;
  349. ztex_shutdown(thr);
  350. }
  351. struct device_drv ztex_drv = {
  352. .drv_id = DRIVER_ZTEX,
  353. .dname = "ztex",
  354. .name = "ZTX",
  355. .drv_detect = ztex_detect,
  356. .get_statline_before = ztex_statline_before,
  357. .thread_prepare = ztex_prepare,
  358. .scanhash = ztex_scanhash,
  359. .thread_shutdown = ztex_shutdown,
  360. };