driver-ztex.c 12 KB

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