driver-ztex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 <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[128];
  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. hdata->nonce = le32toh(hdata->nonce);
  135. hdata->hash7 = le32toh(hdata->hash7);
  136. work->data[64 + 12 + 0] = (hdata->nonce >> 0) & 0xff;
  137. work->data[64 + 12 + 1] = (hdata->nonce >> 8) & 0xff;
  138. work->data[64 + 12 + 2] = (hdata->nonce >> 16) & 0xff;
  139. work->data[64 + 12 + 3] = (hdata->nonce >> 24) & 0xff;
  140. swap32yes(swap32, data32, 80 / 4);
  141. sha2(swap, 80, hash1, false);
  142. sha2(hash1, 32, hash2, false);
  143. if (htobe32(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
  144. dclk_errorCount(&ztex->dclk, 1.0 / ztex->numNonces);
  145. applog(LOG_DEBUG, "%s: checkNonce failed for %0.8X", ztex->repr, hdata->nonce);
  146. return false;
  147. }
  148. return true;
  149. }
  150. static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
  151. __maybe_unused int64_t max_nonce)
  152. {
  153. struct libztex_device *ztex;
  154. unsigned char sendbuf[44];
  155. int i, j, k;
  156. uint32_t *backlog;
  157. int backlog_p = 0, backlog_max;
  158. uint32_t *lastnonce;
  159. uint32_t nonce, noncecnt = 0;
  160. bool overflow, found, rv;
  161. struct libztex_hash_data hdata[GOLDEN_BACKLOG];
  162. ztex = thr->cgpu->device_ztex;
  163. memcpy(sendbuf, work->data + 64, 12);
  164. memcpy(sendbuf + 12, work->midstate, 32);
  165. ztex_selectFpga(ztex);
  166. i = libztex_sendHashData(ztex, sendbuf);
  167. if (i < 0) {
  168. // Something wrong happened in send
  169. applog(LOG_ERR, "%s: Failed to send hash data with err %d, retrying", ztex->repr, i);
  170. usleep(500000);
  171. i = libztex_sendHashData(ztex, sendbuf);
  172. if (i < 0) {
  173. // And there's nothing we can do about it
  174. ztex_disable(thr);
  175. applog(LOG_ERR, "%s: Failed to send hash data with err %d, giving up", ztex->repr, i);
  176. ztex_releaseFpga(ztex);
  177. return -1;
  178. }
  179. }
  180. ztex_releaseFpga(ztex);
  181. applog(LOG_DEBUG, "%s: sent hashdata", ztex->repr);
  182. lastnonce = malloc(sizeof(uint32_t)*ztex->numNonces);
  183. if (lastnonce == NULL) {
  184. applog(LOG_ERR, "%s: failed to allocate lastnonce[%d]", ztex->repr, ztex->numNonces);
  185. return -1;
  186. }
  187. memset(lastnonce, 0, sizeof(uint32_t)*ztex->numNonces);
  188. backlog_max = ztex->numNonces * (1 + ztex->extraSolutions);
  189. backlog_max *= 2;
  190. backlog = malloc(sizeof(uint32_t) * backlog_max);
  191. if (backlog == NULL) {
  192. applog(LOG_ERR, "%s: failed to allocate backlog[%d]", ztex->repr, backlog_max);
  193. free(lastnonce);
  194. return -1;
  195. }
  196. memset(backlog, 0, sizeof(uint32_t) * backlog_max);
  197. overflow = false;
  198. applog(LOG_DEBUG, "%s: entering poll loop", ztex->repr);
  199. while (!(overflow || thr->work_restart)) {
  200. usleep(250000);
  201. if (thr->work_restart) {
  202. applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
  203. break;
  204. }
  205. ztex_selectFpga(ztex);
  206. i = libztex_readHashData(ztex, &hdata[0]);
  207. if (i < 0) {
  208. // Something wrong happened in read
  209. applog(LOG_ERR, "%s: Failed to read hash data with err %d, retrying", ztex->repr, i);
  210. usleep(500000);
  211. i = libztex_readHashData(ztex, &hdata[0]);
  212. if (i < 0) {
  213. // And there's nothing we can do about it
  214. ztex_disable(thr);
  215. applog(LOG_ERR, "%s: Failed to read hash data with err %d, giving up", ztex->repr, i);
  216. free(lastnonce);
  217. free(backlog);
  218. ztex_releaseFpga(ztex);
  219. return -1;
  220. }
  221. }
  222. ztex_releaseFpga(ztex);
  223. if (thr->work_restart) {
  224. applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
  225. break;
  226. }
  227. dclk_gotNonces(&ztex->dclk);
  228. for (i = 0; i < ztex->numNonces; i++) {
  229. nonce = hdata[i].nonce;
  230. nonce = le32toh(nonce);
  231. if (nonce > noncecnt)
  232. noncecnt = nonce;
  233. if (((nonce & 0x7fffffff) >> 4) < ((lastnonce[i] & 0x7fffffff) >> 4)) {
  234. applog(LOG_DEBUG, "%s: overflow nonce=%0.8x lastnonce=%0.8x", ztex->repr, nonce, lastnonce[i]);
  235. overflow = true;
  236. } else
  237. lastnonce[i] = nonce;
  238. nonce = htole32(nonce);
  239. if (!ztex_checkNonce(ztex, work, &hdata[i])) {
  240. thr->cgpu->hw_errors++;
  241. ++hw_errors;
  242. continue;
  243. }
  244. for (j=0; j<=ztex->extraSolutions; j++) {
  245. nonce = hdata[i].goldenNonce[j];
  246. if (nonce > 0) {
  247. found = false;
  248. for (k = 0; k < backlog_max; k++) {
  249. if (backlog[k] == nonce) {
  250. found = true;
  251. break;
  252. }
  253. }
  254. if (!found) {
  255. backlog[backlog_p++] = nonce;
  256. if (backlog_p >= backlog_max)
  257. backlog_p = 0;
  258. nonce = le32toh(nonce);
  259. work->blk.nonce = 0xffffffff;
  260. if ( (rv = test_nonce(work, nonce, false)) )
  261. rv = submit_nonce(thr, work, nonce);
  262. applog(LOG_DEBUG, "%s: submitted %0.8x (from N%dE%d) %d", ztex->repr, nonce, i, j, rv);
  263. }
  264. }
  265. }
  266. }
  267. }
  268. dclk_preUpdate(&ztex->dclk);
  269. if (!ztex_updateFreq(thr)) {
  270. // Something really serious happened, so mark this thread as dead!
  271. free(lastnonce);
  272. free(backlog);
  273. return -1;
  274. }
  275. applog(LOG_DEBUG, "%s: exit %1.8X", ztex->repr, noncecnt);
  276. work->blk.nonce = 0xffffffff;
  277. free(lastnonce);
  278. free(backlog);
  279. return noncecnt;
  280. }
  281. static void ztex_statline_before(char *buf, struct cgpu_info *cgpu)
  282. {
  283. char before[] = " ";
  284. if (cgpu->device_ztex) {
  285. const char *snString = (char*)cgpu->device_ztex->snString;
  286. size_t snStringLen = strlen(snString);
  287. if (snStringLen > 14)
  288. snStringLen = 14;
  289. memcpy(before, snString, snStringLen);
  290. }
  291. tailsprintf(buf, "%s| ", &before[0]);
  292. }
  293. static struct api_data*
  294. get_ztex_api_extra_device_status(struct cgpu_info *ztex)
  295. {
  296. struct api_data*root = NULL;
  297. struct libztex_device *ztexr = ztex->device_ztex;
  298. if (ztexr) {
  299. double frequency = ztexr->freqM1 * (ztexr->dclk.freqM + 1);
  300. root = api_add_freq(root, "Frequency", &frequency, true);
  301. }
  302. return root;
  303. }
  304. static bool ztex_prepare(struct thr_info *thr)
  305. {
  306. struct timeval now;
  307. struct cgpu_info *cgpu = thr->cgpu;
  308. struct libztex_device *ztex = cgpu->device_ztex;
  309. gettimeofday(&now, NULL);
  310. get_datestamp(cgpu->init, &now);
  311. ztex_selectFpga(ztex);
  312. if (libztex_configureFpga(ztex) != 0)
  313. return false;
  314. ztex_releaseFpga(ztex);
  315. ztex->dclk.freqM = ztex->dclk.freqMaxM+1;;
  316. //ztex_updateFreq(thr);
  317. libztex_setFreq(ztex, ztex->dclk.freqMDefault);
  318. applog(LOG_DEBUG, "%s: prepare", ztex->repr);
  319. return true;
  320. }
  321. static void ztex_shutdown(struct thr_info *thr)
  322. {
  323. struct cgpu_info *cgpu = thr->cgpu;
  324. struct libztex_device *ztex = cgpu->device_ztex;
  325. if (!ztex)
  326. return;
  327. cgpu->device_ztex = NULL;
  328. if (ztex->root->numberOfFpgas > 1 && ztex->fpgaNum == 0)
  329. pthread_mutex_destroy(&ztex->mutex);
  330. applog(LOG_DEBUG, "%s: shutdown", ztex->repr);
  331. libztex_destroy_device(ztex);
  332. }
  333. static void ztex_disable(struct thr_info *thr)
  334. {
  335. applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
  336. devices[thr->cgpu->device_id]->deven = DEV_DISABLED;
  337. ztex_shutdown(thr);
  338. }
  339. struct device_api ztex_api = {
  340. .dname = "ztex",
  341. .name = "ZTX",
  342. .api_detect = ztex_detect,
  343. .get_statline_before = ztex_statline_before,
  344. .get_api_extra_device_status = get_ztex_api_extra_device_status,
  345. .thread_init = ztex_prepare,
  346. .scanhash = ztex_scanhash,
  347. .thread_shutdown = ztex_shutdown,
  348. };