driver-ztex.c 11 KB

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