driver-ztex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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)
  40. {
  41. if (ztex->root->numberOfFpgas > 1) {
  42. if (ztex->root->selectedFpga != ztex->fpgaNum)
  43. mutex_lock(&ztex->root->mutex);
  44. libztex_selectFpga(ztex);
  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 j, 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. dev->fpgaNum = j;
  64. add_cgpu(ztex);
  65. strcpy(ztex->device_ztex->repr, ztex->proc_repr);
  66. ztex->name = fpganame;
  67. applog(LOG_INFO, "%"PRIpreprv": Found Ztex (ZTEX %s)", ztex->dev_repr, fpganame);
  68. return ztex;
  69. }
  70. static int ztex_autodetect(void)
  71. {
  72. int cnt;
  73. int i;
  74. int fpgacount;
  75. int totaldevs = 0;
  76. struct libztex_dev_list **ztex_devices;
  77. struct libztex_device *ztex_master;
  78. struct cgpu_info *ztex;
  79. cnt = libztex_scanDevices(&ztex_devices);
  80. if (cnt > 0)
  81. applog(LOG_INFO, "Found %d ztex board%s", cnt, cnt > 1 ? "s" : "");
  82. for (i = 0; i < cnt; i++) {
  83. ztex_master = ztex_devices[i]->dev;
  84. ztex_master->root = ztex_master;
  85. fpgacount = libztex_numberOfFpgas(ztex_master);
  86. ztex = ztex_setup(ztex_master, 0, 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 libztex_device *ztex = thr->cgpu->device_ztex;
  103. ztex_selectFpga(ztex);
  104. libztex_setFreq(ztex, bestM);
  105. ztex_releaseFpga(ztex);
  106. return true;
  107. }
  108. static bool ztex_updateFreq(struct thr_info *thr)
  109. {
  110. struct libztex_device *ztex = thr->cgpu->device_ztex;
  111. bool rv = dclk_updateFreq(&ztex->dclk, ztex_change_clock_func, thr);
  112. if (unlikely(!rv)) {
  113. ztex_selectFpga(ztex);
  114. libztex_resetFpga(ztex);
  115. ztex_releaseFpga(ztex);
  116. }
  117. return rv;
  118. }
  119. static bool ztex_checkNonce(struct libztex_device *ztex,
  120. struct work *work,
  121. struct libztex_hash_data *hdata)
  122. {
  123. uint32_t *data32 = (uint32_t *)(work->data);
  124. unsigned char swap[80];
  125. uint32_t *swap32 = (uint32_t *)swap;
  126. unsigned char hash1[32];
  127. unsigned char hash2[32];
  128. uint32_t *hash2_32 = (uint32_t *)hash2;
  129. swap32[76/4] = htobe32(hdata->nonce);
  130. swap32yes(swap32, data32, 76 / 4);
  131. sha2(swap, 80, hash1);
  132. sha2(hash1, 32, hash2);
  133. if (be32toh(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
  134. applog(LOG_DEBUG, "%s: checkNonce failed for %08x", ztex->repr, hdata->nonce);
  135. return false;
  136. }
  137. return true;
  138. }
  139. static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
  140. __maybe_unused int64_t max_nonce)
  141. {
  142. struct libztex_device *ztex;
  143. unsigned char sendbuf[44];
  144. int i, j, k;
  145. uint32_t *backlog;
  146. int backlog_p = 0, backlog_max;
  147. uint32_t *lastnonce;
  148. uint32_t nonce, noncecnt = 0;
  149. bool overflow, found;
  150. struct libztex_hash_data hdata[GOLDEN_BACKLOG];
  151. if (thr->cgpu->deven == DEV_DISABLED)
  152. return -1;
  153. ztex = thr->cgpu->device_ztex;
  154. memcpy(sendbuf, work->data + 64, 12);
  155. memcpy(sendbuf + 12, work->midstate, 32);
  156. ztex_selectFpga(ztex);
  157. i = libztex_sendHashData(ztex, sendbuf);
  158. if (i < 0) {
  159. // Something wrong happened in send
  160. applog(LOG_ERR, "%s: Failed to send hash data with err %d, retrying", ztex->repr, i);
  161. nmsleep(500);
  162. i = libztex_sendHashData(ztex, sendbuf);
  163. if (i < 0) {
  164. // And there's nothing we can do about it
  165. ztex_disable(thr);
  166. applog(LOG_ERR, "%s: Failed to send hash data with err %d, giving up", ztex->repr, i);
  167. ztex_releaseFpga(ztex);
  168. return -1;
  169. }
  170. }
  171. ztex_releaseFpga(ztex);
  172. applog(LOG_DEBUG, "%s: sent hashdata", ztex->repr);
  173. lastnonce = calloc(1, sizeof(uint32_t)*ztex->numNonces);
  174. if (lastnonce == NULL) {
  175. applog(LOG_ERR, "%s: failed to allocate lastnonce[%d]", ztex->repr, ztex->numNonces);
  176. return -1;
  177. }
  178. /* Add an extra slot for detecting dupes that lie around */
  179. backlog_max = ztex->numNonces * (2 + ztex->extraSolutions);
  180. backlog = calloc(1, sizeof(uint32_t) * backlog_max);
  181. if (backlog == NULL) {
  182. applog(LOG_ERR, "%s: failed to allocate backlog[%d]", ztex->repr, backlog_max);
  183. free(lastnonce);
  184. return -1;
  185. }
  186. overflow = false;
  187. int count = 0;
  188. applog(LOG_DEBUG, "%s: entering poll loop", ztex->repr);
  189. while (!(overflow || thr->work_restart)) {
  190. count++;
  191. if (!restart_wait(thr, 250))
  192. {
  193. applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
  194. break;
  195. }
  196. ztex_selectFpga(ztex);
  197. i = libztex_readHashData(ztex, &hdata[0]);
  198. if (i < 0) {
  199. // Something wrong happened in read
  200. applog(LOG_ERR, "%s: Failed to read hash data with err %d, retrying", ztex->repr, i);
  201. nmsleep(500);
  202. i = libztex_readHashData(ztex, &hdata[0]);
  203. if (i < 0) {
  204. // And there's nothing we can do about it
  205. ztex_disable(thr);
  206. applog(LOG_ERR, "%s: Failed to read hash data with err %d, giving up", ztex->repr, i);
  207. free(lastnonce);
  208. free(backlog);
  209. ztex_releaseFpga(ztex);
  210. return -1;
  211. }
  212. }
  213. ztex_releaseFpga(ztex);
  214. if (thr->work_restart) {
  215. applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
  216. break;
  217. }
  218. dclk_gotNonces(&ztex->dclk);
  219. for (i = 0; i < ztex->numNonces; i++) {
  220. nonce = hdata[i].nonce;
  221. if (nonce > noncecnt)
  222. noncecnt = nonce;
  223. if (((0xffffffff - nonce) < (nonce - lastnonce[i])) || nonce < lastnonce[i]) {
  224. applog(LOG_DEBUG, "%s: overflow nonce=%08x lastnonce=%08x", ztex->repr, nonce, lastnonce[i]);
  225. overflow = true;
  226. } else
  227. lastnonce[i] = nonce;
  228. if (!ztex_checkNonce(ztex, work, &hdata[i])) {
  229. // do not count errors in the first 500ms after sendHashData (2x250 wait time)
  230. if (count > 2)
  231. dclk_errorCount(&ztex->dclk, 1.0 / ztex->numNonces);
  232. thr->cgpu->hw_errors++;
  233. ++hw_errors;
  234. }
  235. for (j=0; j<=ztex->extraSolutions; j++) {
  236. nonce = hdata[i].goldenNonce[j];
  237. if (nonce == ztex->offsNonces) {
  238. continue;
  239. }
  240. found = false;
  241. for (k = 0; k < backlog_max; k++) {
  242. if (backlog[k] == nonce) {
  243. found = true;
  244. break;
  245. }
  246. }
  247. if (!found) {
  248. backlog[backlog_p++] = nonce;
  249. if (backlog_p >= backlog_max)
  250. backlog_p = 0;
  251. work->blk.nonce = 0xffffffff;
  252. if (!j || test_nonce(work, nonce, false))
  253. submit_nonce(thr, work, nonce);
  254. applog(LOG_DEBUG, "%s: submitted %08x (from N%dE%d)", ztex->repr, nonce, i, j);
  255. }
  256. }
  257. }
  258. }
  259. dclk_preUpdate(&ztex->dclk);
  260. if (!ztex_updateFreq(thr)) {
  261. // Something really serious happened, so mark this thread as dead!
  262. free(lastnonce);
  263. free(backlog);
  264. return -1;
  265. }
  266. applog(LOG_DEBUG, "%s: exit %1.8X", ztex->repr, noncecnt);
  267. work->blk.nonce = 0xffffffff;
  268. free(lastnonce);
  269. free(backlog);
  270. return noncecnt;
  271. }
  272. static void ztex_statline_before(char *buf, struct cgpu_info *cgpu)
  273. {
  274. char before[] = " ";
  275. if (cgpu->device_ztex) {
  276. const char *snString = (char*)cgpu->device_ztex->snString;
  277. size_t snStringLen = strlen(snString);
  278. if (snStringLen > 14)
  279. snStringLen = 14;
  280. memcpy(before, snString, snStringLen);
  281. }
  282. tailsprintf(buf, "%s| ", &before[0]);
  283. }
  284. static struct api_data*
  285. get_ztex_drv_extra_device_status(struct cgpu_info *ztex)
  286. {
  287. struct api_data*root = NULL;
  288. struct libztex_device *ztexr = ztex->device_ztex;
  289. if (ztexr) {
  290. double frequency = ztexr->freqM1 * (ztexr->dclk.freqM + 1);
  291. root = api_add_freq(root, "Frequency", &frequency, true);
  292. }
  293. return root;
  294. }
  295. static bool ztex_prepare(struct thr_info *thr)
  296. {
  297. struct timeval now;
  298. struct cgpu_info *cgpu = thr->cgpu;
  299. struct libztex_device *ztex = cgpu->device_ztex;
  300. cgtime(&now);
  301. get_datestamp(cgpu->init, &now);
  302. if (cgpu->proc_id)
  303. {
  304. struct libztex_device *ztex_master = cgpu->device->device_ztex;
  305. ztex = malloc(sizeof(struct libztex_device));
  306. memcpy(ztex, ztex_master, sizeof(*ztex));
  307. cgpu->device_ztex = ztex;
  308. ztex->root = ztex_master;
  309. ztex->fpgaNum = cgpu->proc_id;
  310. strcpy(ztex->repr, cgpu->proc_repr);
  311. }
  312. {
  313. char fpganame[LIBZTEX_SNSTRING_LEN+3+1];
  314. sprintf(fpganame, "%s-%u", ztex->snString, cgpu->proc_id+1);
  315. cgpu->name = fpganame;
  316. }
  317. ztex_selectFpga(ztex);
  318. if (libztex_configureFpga(ztex) != 0) {
  319. libztex_resetFpga(ztex);
  320. ztex_releaseFpga(ztex);
  321. applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
  322. thr->cgpu->deven = DEV_DISABLED;
  323. return true;
  324. }
  325. ztex->dclk.freqM = ztex->dclk.freqMaxM+1;;
  326. //ztex_updateFreq(thr);
  327. libztex_setFreq(ztex, ztex->dclk.freqMDefault);
  328. ztex_releaseFpga(ztex);
  329. notifier_init(thr->work_restart_notifier);
  330. applog(LOG_DEBUG, "%s: prepare", ztex->repr);
  331. return true;
  332. }
  333. static void ztex_shutdown(struct thr_info *thr)
  334. {
  335. struct cgpu_info *cgpu = thr->cgpu;
  336. struct libztex_device *ztex = cgpu->device_ztex;
  337. if (!ztex)
  338. return;
  339. cgpu->device_ztex = NULL;
  340. if (ztex->root->numberOfFpgas > 1 && ztex->fpgaNum == 0)
  341. pthread_mutex_destroy(&ztex->mutex);
  342. applog(LOG_DEBUG, "%s: shutdown", ztex->repr);
  343. libztex_destroy_device(ztex);
  344. }
  345. static void ztex_disable(struct thr_info *thr)
  346. {
  347. applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
  348. thr->cgpu->deven = DEV_DISABLED;
  349. ztex_shutdown(thr);
  350. }
  351. struct device_drv ztex_drv = {
  352. .dname = "ztex",
  353. .name = "ZTX",
  354. .drv_detect = ztex_detect,
  355. .get_statline_before = ztex_statline_before,
  356. .get_api_extra_device_status = get_ztex_drv_extra_device_status,
  357. .thread_init = ztex_prepare,
  358. .scanhash = ztex_scanhash,
  359. .thread_shutdown = ztex_shutdown,
  360. };