driver-ztex.c 11 KB

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