driver-ztex.c 11 KB

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