driver-ztex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * ztex.c - BFGMiner 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. // 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. applog(LOG_WARNING, "Found %d ztex board(s)", cnt);
  60. for (i = 0; i < cnt; i++) {
  61. if (total_devices == MAX_DEVICES)
  62. break;
  63. ztex = calloc(1, sizeof(struct cgpu_info));
  64. ztex->api = &ztex_api;
  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->api = &ztex_api;
  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 bool ztex_checkNonce(struct libztex_device *ztex,
  130. struct work *work,
  131. struct libztex_hash_data *hdata)
  132. {
  133. uint32_t *data32 = (uint32_t *)(work->data);
  134. unsigned char swap[128];
  135. uint32_t *swap32 = (uint32_t *)swap;
  136. unsigned char hash1[32];
  137. unsigned char hash2[32];
  138. uint32_t *hash2_32 = (uint32_t *)hash2;
  139. int i;
  140. #if defined(__BIGENDIAN__) || defined(MIPSEB)
  141. hdata->nonce = swab32(hdata->nonce);
  142. hdata->hash7 = swab32(hdata->hash7);
  143. #endif
  144. work->data[64 + 12 + 0] = (hdata->nonce >> 0) & 0xff;
  145. work->data[64 + 12 + 1] = (hdata->nonce >> 8) & 0xff;
  146. work->data[64 + 12 + 2] = (hdata->nonce >> 16) & 0xff;
  147. work->data[64 + 12 + 3] = (hdata->nonce >> 24) & 0xff;
  148. for (i = 0; i < 80 / 4; i++)
  149. swap32[i] = swab32(data32[i]);
  150. sha2(swap, 80, hash1, false);
  151. sha2(hash1, 32, hash2, false);
  152. #if defined(__BIGENDIAN__) || defined(MIPSEB)
  153. if (hash2_32[7] != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
  154. #else
  155. if (swab32(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
  156. #endif
  157. ztex->errorCount[ztex->freqM] += 1.0 / ztex->numNonces;
  158. applog(LOG_DEBUG, "%s: checkNonce failed for %0.8X", ztex->repr, hdata->nonce);
  159. return false;
  160. }
  161. return true;
  162. }
  163. static uint64_t ztex_scanhash(struct thr_info *thr, struct work *work,
  164. __maybe_unused uint64_t max_nonce)
  165. {
  166. struct libztex_device *ztex;
  167. unsigned char sendbuf[44];
  168. int i, j, k;
  169. uint32_t *backlog;
  170. int backlog_p = 0, backlog_max;
  171. uint32_t *lastnonce;
  172. uint32_t nonce, noncecnt = 0;
  173. bool overflow, found, rv;
  174. struct libztex_hash_data hdata[GOLDEN_BACKLOG];
  175. ztex = thr->cgpu->device_ztex;
  176. memcpy(sendbuf, work->data + 64, 12);
  177. memcpy(sendbuf + 12, work->midstate, 32);
  178. ztex_selectFpga(ztex);
  179. i = libztex_sendHashData(ztex, sendbuf);
  180. if (i < 0) {
  181. // Something wrong happened in send
  182. applog(LOG_ERR, "%s: Failed to send hash data with err %d, retrying", ztex->repr, i);
  183. usleep(500000);
  184. i = libztex_sendHashData(ztex, sendbuf);
  185. if (i < 0) {
  186. // And there's nothing we can do about it
  187. ztex_disable(thr);
  188. applog(LOG_ERR, "%s: Failed to send hash data with err %d, giving up", ztex->repr, i);
  189. ztex_releaseFpga(ztex);
  190. return 0;
  191. }
  192. }
  193. ztex_releaseFpga(ztex);
  194. applog(LOG_DEBUG, "%s: sent hashdata", ztex->repr);
  195. lastnonce = malloc(sizeof(uint32_t)*ztex->numNonces);
  196. if (lastnonce == NULL) {
  197. applog(LOG_ERR, "%s: failed to allocate lastnonce[%d]", ztex->repr, ztex->numNonces);
  198. return 0;
  199. }
  200. memset(lastnonce, 0, sizeof(uint32_t)*ztex->numNonces);
  201. backlog_max = ztex->numNonces * (1 + ztex->extraSolutions);
  202. backlog = malloc(sizeof(uint32_t) * backlog_max);
  203. if (backlog == NULL) {
  204. applog(LOG_ERR, "%s: failed to allocate backlog[%d]", ztex->repr, backlog_max);
  205. return 0;
  206. }
  207. memset(backlog, 0, sizeof(uint32_t) * backlog_max);
  208. overflow = false;
  209. applog(LOG_DEBUG, "%s: entering poll loop", ztex->repr);
  210. while (!(overflow || thr->work_restart)) {
  211. usleep(250000);
  212. if (thr->work_restart) {
  213. applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
  214. break;
  215. }
  216. ztex_selectFpga(ztex);
  217. i = libztex_readHashData(ztex, &hdata[0]);
  218. if (i < 0) {
  219. // Something wrong happened in read
  220. applog(LOG_ERR, "%s: Failed to read hash data with err %d, retrying", ztex->repr, i);
  221. usleep(500000);
  222. i = libztex_readHashData(ztex, &hdata[0]);
  223. if (i < 0) {
  224. // And there's nothing we can do about it
  225. ztex_disable(thr);
  226. applog(LOG_ERR, "%s: Failed to read hash data with err %d, giving up", ztex->repr, i);
  227. free(lastnonce);
  228. free(backlog);
  229. ztex_releaseFpga(ztex);
  230. return 0;
  231. }
  232. }
  233. ztex_releaseFpga(ztex);
  234. if (thr->work_restart) {
  235. applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
  236. break;
  237. }
  238. ztex->errorCount[ztex->freqM] *= 0.995;
  239. ztex->errorWeight[ztex->freqM] = ztex->errorWeight[ztex->freqM] * 0.995 + 1.0;
  240. for (i = 0; i < ztex->numNonces; i++) {
  241. nonce = hdata[i].nonce;
  242. #if defined(__BIGENDIAN__) || defined(MIPSEB)
  243. nonce = swab32(nonce);
  244. #endif
  245. if (nonce > noncecnt)
  246. noncecnt = nonce;
  247. if (((nonce & 0x7fffffff) >> 4) < ((lastnonce[i] & 0x7fffffff) >> 4)) {
  248. applog(LOG_DEBUG, "%s: overflow nonce=%0.8x lastnonce=%0.8x", ztex->repr, nonce, lastnonce[i]);
  249. overflow = true;
  250. } else
  251. lastnonce[i] = nonce;
  252. #if !(defined(__BIGENDIAN__) || defined(MIPSEB))
  253. nonce = swab32(nonce);
  254. #endif
  255. if (!ztex_checkNonce(ztex, work, &hdata[i])) {
  256. thr->cgpu->hw_errors++;
  257. continue;
  258. }
  259. for (j=0; j<=ztex->extraSolutions; j++) {
  260. nonce = hdata[i].goldenNonce[j];
  261. if (nonce > 0) {
  262. found = false;
  263. for (k = 0; k < backlog_max; k++) {
  264. if (backlog[k] == nonce) {
  265. found = true;
  266. break;
  267. }
  268. }
  269. if (!found) {
  270. applog(LOG_DEBUG, "%s: Share found N%dE%d", ztex->repr, i, j);
  271. backlog[backlog_p++] = nonce;
  272. if (backlog_p >= backlog_max)
  273. backlog_p = 0;
  274. #if defined(__BIGENDIAN__) || defined(MIPSEB)
  275. nonce = swab32(nonce);
  276. #endif
  277. work->blk.nonce = 0xffffffff;
  278. rv = submit_nonce(thr, work, nonce);
  279. applog(LOG_DEBUG, "%s: submitted %0.8x %d", ztex->repr, nonce, rv);
  280. }
  281. }
  282. }
  283. }
  284. }
  285. 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);
  286. if (ztex->errorRate[ztex->freqM] > ztex->maxErrorRate[ztex->freqM])
  287. ztex->maxErrorRate[ztex->freqM] = ztex->errorRate[ztex->freqM];
  288. if (!ztex_updateFreq(ztex)) {
  289. // Something really serious happened, so mark this thread as dead!
  290. free(lastnonce);
  291. free(backlog);
  292. return 0;
  293. }
  294. applog(LOG_DEBUG, "%s: exit %1.8X", ztex->repr, noncecnt);
  295. work->blk.nonce = 0xffffffff;
  296. free(lastnonce);
  297. free(backlog);
  298. return noncecnt > 0? noncecnt: 1;
  299. }
  300. static void ztex_statline_before(char *buf, struct cgpu_info *cgpu)
  301. {
  302. if (cgpu->deven == DEV_ENABLED) {
  303. tailsprintf(buf, "%s-%d | ", cgpu->device_ztex->snString, cgpu->device_ztex->fpgaNum+1);
  304. tailsprintf(buf, "%0.2fMhz | ", cgpu->device_ztex->freqM1 * (cgpu->device_ztex->freqM + 1));
  305. }
  306. }
  307. static json_t*
  308. get_ztex_extra_device_status(struct cgpu_info *ztex)
  309. {
  310. json_t *info = json_object();
  311. struct libztex_device *ztexr = ztex->device_ztex;
  312. if (ztexr) {
  313. double frequency = ztexr->freqM1 * (ztexr->freqM + 1);
  314. json_object_set(info, "Frequency", json_real(frequency));
  315. }
  316. return info;
  317. }
  318. static bool ztex_prepare(struct thr_info *thr)
  319. {
  320. struct timeval now;
  321. struct cgpu_info *cgpu = thr->cgpu;
  322. struct libztex_device *ztex = cgpu->device_ztex;
  323. gettimeofday(&now, NULL);
  324. get_datestamp(cgpu->init, &now);
  325. ztex_selectFpga(ztex);
  326. if (libztex_configureFpga(ztex) != 0)
  327. return false;
  328. ztex_releaseFpga(ztex);
  329. ztex->freqM = ztex->freqMaxM+1;;
  330. //ztex_updateFreq(ztex);
  331. libztex_setFreq(ztex, ztex->freqMDefault);
  332. applog(LOG_DEBUG, "%s: prepare", ztex->repr);
  333. return true;
  334. }
  335. static void ztex_shutdown(struct thr_info *thr)
  336. {
  337. if (thr->cgpu->device_ztex != NULL) {
  338. if (thr->cgpu->device_ztex->fpgaNum == 0)
  339. pthread_mutex_destroy(&thr->cgpu->device_ztex->mutex);
  340. applog(LOG_DEBUG, "%s: shutdown", thr->cgpu->device_ztex->repr);
  341. libztex_destroy_device(thr->cgpu->device_ztex);
  342. thr->cgpu->device_ztex = NULL;
  343. }
  344. }
  345. static void ztex_disable(struct thr_info *thr)
  346. {
  347. applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
  348. devices[thr->cgpu->device_id]->deven = DEV_DISABLED;
  349. ztex_shutdown(thr);
  350. }
  351. struct device_api ztex_api = {
  352. .dname = "ztex",
  353. .name = "PGA",
  354. .api_detect = ztex_detect,
  355. .get_statline_before = ztex_statline_before,
  356. .get_extra_device_status = get_ztex_extra_device_status,
  357. .thread_prepare = ztex_prepare,
  358. .scanhash = ztex_scanhash,
  359. .thread_shutdown = ztex_shutdown,
  360. };