driver-ztex.c 12 KB

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