driver-ztex.c 11 KB

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