driver-ztex.c 12 KB

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