driver-ztex.c 12 KB

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