driver-ztex.c 12 KB

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