driver-ztex.c 11 KB

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