driver-ztex.c 11 KB

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