driver-ztex.c 11 KB

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