driver-ztex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /**
  2. * ztex.c - BFGMiner worker for Ztex 1.15x/y fpga board
  3. *
  4. * Copyright 2012 nelisky
  5. * Copyright 2012-2013 Luke Dashjr
  6. * Copyright 2012-2013 Denis Ahrens
  7. * Copyright 2012 Xiangfu
  8. *
  9. * This work is based upon the Java SDK provided by ztex which is
  10. * Copyright (C) 2009-2011 ZTEX GmbH.
  11. * http://www.ztex.de
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, see http://www.gnu.org/licenses/.
  24. **/
  25. #include "config.h"
  26. #include "miner.h"
  27. #include <unistd.h>
  28. #include <sha2.h>
  29. #include "deviceapi.h"
  30. #include "dynclock.h"
  31. #include "fpgautils.h"
  32. #include "libztex.h"
  33. #include "util.h"
  34. #define GOLDEN_BACKLOG 5
  35. struct device_drv ztex_drv;
  36. // Forward declarations
  37. static void ztex_disable(struct thr_info* thr);
  38. static bool ztex_prepare(struct thr_info *thr);
  39. static void ztex_selectFpga(struct libztex_device* ztex, int16_t fpgaNum)
  40. {
  41. if (ztex->root->numberOfFpgas > 1) {
  42. if (ztex->root->selectedFpga != fpgaNum)
  43. mutex_lock(&ztex->root->mutex);
  44. libztex_selectFpga(ztex, fpgaNum);
  45. }
  46. }
  47. static void ztex_releaseFpga(struct libztex_device* ztex)
  48. {
  49. if (ztex->root->numberOfFpgas > 1) {
  50. ztex->root->selectedFpga = -1;
  51. mutex_unlock(&ztex->root->mutex);
  52. }
  53. }
  54. static struct cgpu_info *ztex_setup(struct libztex_device *dev, int fpgacount)
  55. {
  56. struct cgpu_info *ztex;
  57. char *fpganame = (char*)dev->snString;
  58. ztex = calloc(1, sizeof(struct cgpu_info));
  59. ztex->drv = &ztex_drv;
  60. ztex->device_ztex = dev;
  61. ztex->procs = fpgacount;
  62. ztex->threads = fpgacount;
  63. add_cgpu(ztex);
  64. strcpy(ztex->device_ztex->repr, ztex->dev_repr);
  65. ztex->name = fpganame;
  66. applog(LOG_INFO, "%"PRIpreprv": Found Ztex (ZTEX %s)", ztex->dev_repr, fpganame);
  67. return ztex;
  68. }
  69. static int ztex_autodetect(void)
  70. {
  71. int cnt;
  72. int i;
  73. int fpgacount;
  74. int totaldevs = 0;
  75. struct libztex_dev_list **ztex_devices;
  76. struct libztex_device *ztex_master;
  77. struct cgpu_info *ztex;
  78. cnt = libztex_scanDevices(&ztex_devices);
  79. if (cnt > 0)
  80. applog(LOG_INFO, "Found %d ztex board%s", cnt, cnt > 1 ? "s" : "");
  81. for (i = 0; i < cnt; i++) {
  82. ztex_master = ztex_devices[i]->dev;
  83. ztex_master->root = ztex_master;
  84. fpgacount = libztex_numberOfFpgas(ztex_master);
  85. ztex_master->handles = fpgacount;
  86. ztex = ztex_setup(ztex_master, fpgacount);
  87. totaldevs += fpgacount;
  88. if (fpgacount > 1)
  89. pthread_mutex_init(&ztex->device_ztex->mutex, NULL);
  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_drv, ztex_autodetect);
  99. }
  100. static bool ztex_change_clock_func(struct thr_info *thr, int bestM)
  101. {
  102. struct cgpu_info *cgpu = thr->cgpu;
  103. struct libztex_device *ztex = thr->cgpu->device_ztex;
  104. ztex_selectFpga(ztex, cgpu->proc_id);
  105. libztex_setFreq(ztex, bestM, cgpu->proc_repr);
  106. ztex_releaseFpga(ztex);
  107. return true;
  108. }
  109. static bool ztex_updateFreq(struct thr_info *thr)
  110. {
  111. struct cgpu_info *cgpu = thr->cgpu;
  112. struct libztex_device *ztex = thr->cgpu->device_ztex;
  113. bool rv = dclk_updateFreq(&ztex->dclk, ztex_change_clock_func, thr);
  114. if (unlikely(!rv)) {
  115. ztex_selectFpga(ztex, cgpu->proc_id);
  116. libztex_resetFpga(ztex);
  117. ztex_releaseFpga(ztex);
  118. }
  119. return rv;
  120. }
  121. static bool ztex_checkNonce(struct cgpu_info *cgpu,
  122. struct work *work,
  123. struct libztex_hash_data *hdata)
  124. {
  125. uint32_t *data32 = (uint32_t *)(work->data);
  126. unsigned char swap[80];
  127. uint32_t *swap32 = (uint32_t *)swap;
  128. unsigned char hash1[32];
  129. unsigned char hash2[32];
  130. uint32_t *hash2_32 = (uint32_t *)hash2;
  131. swap32[76/4] = htobe32(hdata->nonce);
  132. swap32yes(swap32, data32, 76 / 4);
  133. sha2(swap, 80, hash1);
  134. sha2(hash1, 32, hash2);
  135. if (be32toh(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
  136. applog(LOG_DEBUG, "%"PRIpreprv": checkNonce failed for %08x", cgpu->proc_repr, hdata->nonce);
  137. return false;
  138. }
  139. return true;
  140. }
  141. static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
  142. __maybe_unused int64_t max_nonce)
  143. {
  144. struct cgpu_info *cgpu = thr->cgpu;
  145. struct libztex_device *ztex;
  146. unsigned char sendbuf[44];
  147. int i, j, k;
  148. uint32_t *backlog;
  149. int backlog_p = 0, backlog_max;
  150. uint32_t *lastnonce;
  151. uint32_t nonce, noncecnt = 0;
  152. bool overflow, found;
  153. struct libztex_hash_data hdata[GOLDEN_BACKLOG];
  154. if (thr->cgpu->deven == DEV_DISABLED)
  155. return -1;
  156. ztex = thr->cgpu->device_ztex;
  157. memcpy(sendbuf, work->data + 64, 12);
  158. memcpy(sendbuf + 12, work->midstate, 32);
  159. ztex_selectFpga(ztex, cgpu->proc_id);
  160. i = libztex_sendHashData(ztex, sendbuf);
  161. if (i < 0) {
  162. // Something wrong happened in send
  163. applog(LOG_ERR, "%"PRIpreprv": Failed to send hash data with err %d, retrying", cgpu->proc_repr, i);
  164. nmsleep(500);
  165. i = libztex_sendHashData(ztex, sendbuf);
  166. if (i < 0) {
  167. // And there's nothing we can do about it
  168. ztex_disable(thr);
  169. applog(LOG_ERR, "%"PRIpreprv": Failed to send hash data with err %d, giving up", cgpu->proc_repr, i);
  170. ztex_releaseFpga(ztex);
  171. return -1;
  172. }
  173. }
  174. ztex_releaseFpga(ztex);
  175. applog(LOG_DEBUG, "%"PRIpreprv": sent hashdata", cgpu->proc_repr);
  176. lastnonce = calloc(1, sizeof(uint32_t)*ztex->numNonces);
  177. if (lastnonce == NULL) {
  178. applog(LOG_ERR, "%"PRIpreprv": failed to allocate lastnonce[%d]", cgpu->proc_repr, ztex->numNonces);
  179. return -1;
  180. }
  181. /* Add an extra slot for detecting dupes that lie around */
  182. backlog_max = ztex->numNonces * (2 + ztex->extraSolutions);
  183. backlog = calloc(1, sizeof(uint32_t) * backlog_max);
  184. if (backlog == NULL) {
  185. applog(LOG_ERR, "%"PRIpreprv": failed to allocate backlog[%d]", cgpu->proc_repr, backlog_max);
  186. free(lastnonce);
  187. return -1;
  188. }
  189. overflow = false;
  190. int count = 0;
  191. applog(LOG_DEBUG, "%"PRIpreprv": entering poll loop", cgpu->proc_repr);
  192. while (!(overflow || thr->work_restart)) {
  193. count++;
  194. if (!restart_wait(thr, 250))
  195. {
  196. applog(LOG_DEBUG, "%"PRIpreprv": New work detected", cgpu->proc_repr);
  197. break;
  198. }
  199. ztex_selectFpga(ztex, cgpu->proc_id);
  200. i = libztex_readHashData(ztex, &hdata[0]);
  201. if (i < 0) {
  202. // Something wrong happened in read
  203. applog(LOG_ERR, "%"PRIpreprv": Failed to read hash data with err %d, retrying", cgpu->proc_repr, i);
  204. nmsleep(500);
  205. i = libztex_readHashData(ztex, &hdata[0]);
  206. if (i < 0) {
  207. // And there's nothing we can do about it
  208. ztex_disable(thr);
  209. applog(LOG_ERR, "%"PRIpreprv": Failed to read hash data with err %d, giving up", cgpu->proc_repr, i);
  210. free(lastnonce);
  211. free(backlog);
  212. ztex_releaseFpga(ztex);
  213. return -1;
  214. }
  215. }
  216. ztex_releaseFpga(ztex);
  217. if (thr->work_restart) {
  218. applog(LOG_DEBUG, "%"PRIpreprv": New work detected", cgpu->proc_repr);
  219. break;
  220. }
  221. dclk_gotNonces(&ztex->dclk);
  222. for (i = 0; i < ztex->numNonces; i++) {
  223. nonce = hdata[i].nonce;
  224. if (nonce > noncecnt)
  225. noncecnt = nonce;
  226. if (((0xffffffff - nonce) < (nonce - lastnonce[i])) || nonce < lastnonce[i]) {
  227. applog(LOG_DEBUG, "%"PRIpreprv": overflow nonce=%08x lastnonce=%08x", cgpu->proc_repr, nonce, lastnonce[i]);
  228. overflow = true;
  229. } else
  230. lastnonce[i] = nonce;
  231. if (!ztex_checkNonce(cgpu, work, &hdata[i])) {
  232. // do not count errors in the first 500ms after sendHashData (2x250 wait time)
  233. if (count > 2)
  234. dclk_errorCount(&ztex->dclk, 1.0 / ztex->numNonces);
  235. thr->cgpu->hw_errors++;
  236. ++hw_errors;
  237. }
  238. for (j=0; j<=ztex->extraSolutions; j++) {
  239. nonce = hdata[i].goldenNonce[j];
  240. if (nonce == ztex->offsNonces) {
  241. continue;
  242. }
  243. found = false;
  244. for (k = 0; k < backlog_max; k++) {
  245. if (backlog[k] == nonce) {
  246. found = true;
  247. break;
  248. }
  249. }
  250. if (!found) {
  251. backlog[backlog_p++] = nonce;
  252. if (backlog_p >= backlog_max)
  253. backlog_p = 0;
  254. work->blk.nonce = 0xffffffff;
  255. if (!j || test_nonce(work, nonce, false))
  256. submit_nonce(thr, work, nonce);
  257. applog(LOG_DEBUG, "%"PRIpreprv": submitted %08x (from N%dE%d)", cgpu->proc_repr, nonce, i, j);
  258. }
  259. }
  260. }
  261. }
  262. dclk_preUpdate(&ztex->dclk);
  263. if (!ztex_updateFreq(thr)) {
  264. // Something really serious happened, so mark this thread as dead!
  265. free(lastnonce);
  266. free(backlog);
  267. return -1;
  268. }
  269. applog(LOG_DEBUG, "%"PRIpreprv": exit %1.8X", cgpu->proc_repr, noncecnt);
  270. work->blk.nonce = 0xffffffff;
  271. free(lastnonce);
  272. free(backlog);
  273. return noncecnt;
  274. }
  275. static void ztex_statline_before(char *buf, struct cgpu_info *cgpu)
  276. {
  277. char before[] = " ";
  278. if (cgpu->device_ztex) {
  279. const char *snString = (char*)cgpu->device_ztex->snString;
  280. size_t snStringLen = strlen(snString);
  281. if (snStringLen > 14)
  282. snStringLen = 14;
  283. memcpy(before, snString, snStringLen);
  284. }
  285. tailsprintf(buf, "%s| ", &before[0]);
  286. }
  287. static struct api_data*
  288. get_ztex_drv_extra_device_status(struct cgpu_info *ztex)
  289. {
  290. struct api_data*root = NULL;
  291. struct libztex_device *ztexr = ztex->device_ztex;
  292. if (ztexr) {
  293. double frequency = ztexr->freqM1 * (ztexr->dclk.freqM + 1);
  294. root = api_add_freq(root, "Frequency", &frequency, true);
  295. }
  296. return root;
  297. }
  298. static bool ztex_prepare(struct thr_info *thr)
  299. {
  300. struct timeval now;
  301. struct cgpu_info *cgpu = thr->cgpu;
  302. struct libztex_device *ztex = cgpu->device_ztex;
  303. cgtime(&now);
  304. get_datestamp(cgpu->init, &now);
  305. {
  306. char fpganame[LIBZTEX_SNSTRING_LEN+3+1];
  307. sprintf(fpganame, "%s-%u", ztex->snString, cgpu->proc_id+1);
  308. cgpu->name = fpganame;
  309. }
  310. ztex_selectFpga(ztex, cgpu->proc_id);
  311. if (libztex_configureFpga(ztex, cgpu->proc_repr) != 0) {
  312. libztex_resetFpga(ztex);
  313. ztex_releaseFpga(ztex);
  314. applog(LOG_ERR, "%"PRIpreprv": Disabling!", cgpu->proc_repr);
  315. thr->cgpu->deven = DEV_DISABLED;
  316. return true;
  317. }
  318. ztex->dclk.freqM = ztex->dclk.freqMaxM+1;;
  319. //ztex_updateFreq(thr);
  320. libztex_setFreq(ztex, ztex->dclk.freqMDefault, cgpu->proc_repr);
  321. ztex_releaseFpga(ztex);
  322. notifier_init(thr->work_restart_notifier);
  323. applog(LOG_DEBUG, "%"PRIpreprv": prepare", cgpu->proc_repr);
  324. return true;
  325. }
  326. static void ztex_shutdown(struct thr_info *thr)
  327. {
  328. struct cgpu_info *cgpu = thr->cgpu;
  329. struct libztex_device *ztex = cgpu->device_ztex;
  330. if (!ztex)
  331. return;
  332. cgpu->device_ztex = NULL;
  333. applog(LOG_DEBUG, "%"PRIpreprv": shutdown", cgpu->proc_repr);
  334. if (--ztex->handles)
  335. return;
  336. applog(LOG_DEBUG, "%s: No handles remaining, destroying libztex device", cgpu->dev_repr);
  337. if (ztex->root->numberOfFpgas > 1)
  338. pthread_mutex_destroy(&ztex->mutex);
  339. libztex_destroy_device(ztex);
  340. }
  341. static void ztex_disable(struct thr_info *thr)
  342. {
  343. applog(LOG_ERR, "%"PRIpreprv": Disabling!", thr->cgpu->proc_repr);
  344. thr->cgpu->deven = DEV_DISABLED;
  345. ztex_shutdown(thr);
  346. }
  347. struct device_drv ztex_drv = {
  348. .dname = "ztex",
  349. .name = "ZTX",
  350. .drv_detect = ztex_detect,
  351. .get_statline_before = ztex_statline_before,
  352. .get_api_extra_device_status = get_ztex_drv_extra_device_status,
  353. .thread_init = ztex_prepare,
  354. .scanhash = ztex_scanhash,
  355. .thread_shutdown = ztex_shutdown,
  356. };