driver-ztex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Copyright 2012 nelisky
  3. * Copyright 2012-2014 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 <stdbool.h>
  18. #include <stdint.h>
  19. #include "miner.h"
  20. #include <unistd.h>
  21. #include <sha2.h>
  22. #include "deviceapi.h"
  23. #include "dynclock.h"
  24. #include "libztex.h"
  25. #include "lowlevel.h"
  26. #include "lowl-usb.h"
  27. #include "util.h"
  28. #define GOLDEN_BACKLOG 5
  29. BFG_REGISTER_DRIVER(ztex_drv)
  30. // Forward declarations
  31. static void ztex_disable(struct thr_info* thr);
  32. static bool ztex_prepare(struct thr_info *thr);
  33. static void ztex_selectFpga(struct libztex_device* ztex, int16_t fpgaNum)
  34. {
  35. if (ztex->root->numberOfFpgas > 1) {
  36. if (ztex->root->selectedFpga != fpgaNum)
  37. mutex_lock(&ztex->root->mutex);
  38. libztex_selectFpga(ztex, fpgaNum);
  39. }
  40. }
  41. static void ztex_releaseFpga(struct libztex_device* ztex)
  42. {
  43. if (ztex->root->numberOfFpgas > 1) {
  44. ztex->root->selectedFpga = -1;
  45. mutex_unlock(&ztex->root->mutex);
  46. }
  47. }
  48. static struct cgpu_info *ztex_setup(struct libztex_device *dev, int fpgacount)
  49. {
  50. struct cgpu_info *ztex;
  51. char *fpganame = (char*)dev->snString;
  52. ztex = calloc(1, sizeof(struct cgpu_info));
  53. ztex->drv = &ztex_drv;
  54. ztex->device_ztex = dev;
  55. ztex->procs = fpgacount;
  56. ztex->threads = fpgacount;
  57. ztex->dev_manufacturer = dev->dev_manufacturer;
  58. ztex->dev_product = dev->dev_product;
  59. ztex->dev_serial = (char*)&dev->snString[0];
  60. ztex->name = fpganame;
  61. add_cgpu(ztex);
  62. strcpy(ztex->device_ztex->repr, ztex->dev_repr);
  63. applog(LOG_INFO, "%"PRIpreprv": Found Ztex (ZTEX %s)", ztex->dev_repr, fpganame);
  64. return ztex;
  65. }
  66. static
  67. bool ztex_lowl_match(const struct lowlevel_device_info * const info)
  68. {
  69. return lowlevel_match_lowlproduct(info, &lowl_usb, "btcminer for ZTEX");
  70. }
  71. static
  72. bool ztex_lowl_probe(const struct lowlevel_device_info * const info)
  73. {
  74. const char * const product = info->product;
  75. const char * const serial = info->serial;
  76. if (info->lowl != &lowl_usb)
  77. {
  78. bfg_probe_result_flags = BPR_WRONG_DEVTYPE;
  79. applog(LOG_DEBUG, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not usb!",
  80. __func__, product, serial);
  81. return false;
  82. }
  83. libusb_device * const usbdev = info->lowl_data;
  84. const enum ztex_check_result err = libztex_checkDevice(usbdev);
  85. switch (err)
  86. {
  87. case CHECK_ERROR:
  88. applogr(false, LOG_ERR, "%s: Can not check device %s", ztex_drv.dname, info->devid);
  89. case CHECK_IS_NOT_ZTEX:
  90. return false;
  91. case CHECK_OK:
  92. break;
  93. case CHECK_RESCAN:
  94. bfg_need_detect_rescan = true;
  95. return false;
  96. }
  97. int fpgacount;
  98. struct libztex_device *ztex_master;
  99. struct cgpu_info *ztex;
  100. ztex_master = libztex_prepare_device2(usbdev);
  101. if (!ztex_master)
  102. applogr(false, LOG_ERR, "%s: libztex_prepare_device2 failed on %s", ztex_drv.dname, info->devid);
  103. if (bfg_claim_usb(&ztex_drv, true, ztex_master->usbbus, ztex_master->usbaddress))
  104. return false;
  105. ztex_master->root = ztex_master;
  106. fpgacount = libztex_numberOfFpgas(ztex_master);
  107. ztex_master->handles = fpgacount;
  108. ztex = ztex_setup(ztex_master, fpgacount);
  109. if (fpgacount > 1)
  110. pthread_mutex_init(&ztex->device_ztex->mutex, NULL);
  111. return true;
  112. }
  113. static bool ztex_change_clock_func(struct thr_info *thr, int bestM)
  114. {
  115. struct cgpu_info *cgpu = thr->cgpu;
  116. struct libztex_device *ztex = thr->cgpu->device_ztex;
  117. ztex_selectFpga(ztex, cgpu->proc_id);
  118. libztex_setFreq(ztex, bestM, cgpu->proc_repr);
  119. ztex_releaseFpga(ztex);
  120. return true;
  121. }
  122. static bool ztex_updateFreq(struct thr_info *thr)
  123. {
  124. struct cgpu_info *cgpu = thr->cgpu;
  125. struct libztex_device *ztex = thr->cgpu->device_ztex;
  126. bool rv = dclk_updateFreq(&ztex->dclk, ztex_change_clock_func, thr);
  127. if (unlikely(!rv)) {
  128. ztex_selectFpga(ztex, cgpu->proc_id);
  129. libztex_resetFpga(ztex);
  130. ztex_releaseFpga(ztex);
  131. }
  132. return rv;
  133. }
  134. static bool ztex_checkNonce(struct cgpu_info *cgpu,
  135. struct work *work,
  136. struct libztex_hash_data *hdata)
  137. {
  138. uint32_t *data32 = (uint32_t *)(work->data);
  139. unsigned char swap[80];
  140. uint32_t *swap32 = (uint32_t *)swap;
  141. unsigned char hash1[32];
  142. unsigned char hash2[32];
  143. uint32_t *hash2_32 = (uint32_t *)hash2;
  144. swap32[76/4] = htobe32(hdata->nonce);
  145. swap32yes(swap32, data32, 76 / 4);
  146. sha256(swap, 80, hash1);
  147. sha256(hash1, 32, hash2);
  148. if (be32toh(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
  149. applog(LOG_DEBUG, "%"PRIpreprv": checkNonce failed for %08x", cgpu->proc_repr, hdata->nonce);
  150. return false;
  151. }
  152. return true;
  153. }
  154. static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
  155. __maybe_unused int64_t max_nonce)
  156. {
  157. struct cgpu_info *cgpu = thr->cgpu;
  158. struct libztex_device *ztex;
  159. unsigned char sendbuf[44];
  160. int i, j, k;
  161. uint32_t *backlog;
  162. int backlog_p = 0, backlog_max;
  163. uint32_t *lastnonce;
  164. uint32_t nonce, noncecnt = 0;
  165. bool overflow, found;
  166. struct libztex_hash_data hdata[GOLDEN_BACKLOG];
  167. if (thr->cgpu->deven == DEV_DISABLED)
  168. return -1;
  169. ztex = thr->cgpu->device_ztex;
  170. memcpy(sendbuf, work->data + 64, 12);
  171. memcpy(sendbuf + 12, work->midstate, 32);
  172. ztex_selectFpga(ztex, cgpu->proc_id);
  173. i = libztex_sendHashData(ztex, sendbuf);
  174. if (i < 0) {
  175. // Something wrong happened in send
  176. applog(LOG_ERR, "%"PRIpreprv": Failed to send hash data with err %d, retrying", cgpu->proc_repr, i);
  177. cgsleep_ms(500);
  178. i = libztex_sendHashData(ztex, sendbuf);
  179. if (i < 0) {
  180. // And there's nothing we can do about it
  181. ztex_disable(thr);
  182. applog(LOG_ERR, "%"PRIpreprv": Failed to send hash data with err %d, giving up", cgpu->proc_repr, i);
  183. ztex_releaseFpga(ztex);
  184. return -1;
  185. }
  186. }
  187. ztex_releaseFpga(ztex);
  188. applog(LOG_DEBUG, "%"PRIpreprv": sent hashdata", cgpu->proc_repr);
  189. lastnonce = calloc(1, sizeof(uint32_t)*ztex->numNonces);
  190. if (lastnonce == NULL) {
  191. applog(LOG_ERR, "%"PRIpreprv": failed to allocate lastnonce[%d]", cgpu->proc_repr, ztex->numNonces);
  192. return -1;
  193. }
  194. /* Add an extra slot for detecting dupes that lie around */
  195. backlog_max = ztex->numNonces * (2 + ztex->extraSolutions);
  196. backlog = calloc(1, sizeof(uint32_t) * backlog_max);
  197. if (backlog == NULL) {
  198. applog(LOG_ERR, "%"PRIpreprv": failed to allocate backlog[%d]", cgpu->proc_repr, backlog_max);
  199. free(lastnonce);
  200. return -1;
  201. }
  202. overflow = false;
  203. int count = 0;
  204. applog(LOG_DEBUG, "%"PRIpreprv": entering poll loop", cgpu->proc_repr);
  205. while (!(overflow || thr->work_restart)) {
  206. count++;
  207. if (!restart_wait(thr, 250))
  208. {
  209. applog(LOG_DEBUG, "%"PRIpreprv": New work detected", cgpu->proc_repr);
  210. break;
  211. }
  212. ztex_selectFpga(ztex, cgpu->proc_id);
  213. i = libztex_readHashData(ztex, &hdata[0]);
  214. if (i < 0) {
  215. // Something wrong happened in read
  216. applog(LOG_ERR, "%"PRIpreprv": Failed to read hash data with err %d, retrying", cgpu->proc_repr, i);
  217. cgsleep_ms(500);
  218. i = libztex_readHashData(ztex, &hdata[0]);
  219. if (i < 0) {
  220. // And there's nothing we can do about it
  221. ztex_disable(thr);
  222. applog(LOG_ERR, "%"PRIpreprv": Failed to read hash data with err %d, giving up", cgpu->proc_repr, i);
  223. free(lastnonce);
  224. free(backlog);
  225. ztex_releaseFpga(ztex);
  226. return -1;
  227. }
  228. }
  229. ztex_releaseFpga(ztex);
  230. if (thr->work_restart) {
  231. applog(LOG_DEBUG, "%"PRIpreprv": New work detected", cgpu->proc_repr);
  232. break;
  233. }
  234. dclk_gotNonces(&ztex->dclk);
  235. for (i = 0; i < ztex->numNonces; i++) {
  236. nonce = hdata[i].nonce;
  237. if (nonce > noncecnt)
  238. noncecnt = nonce;
  239. if (((0xffffffff - nonce) < (nonce - lastnonce[i])) || nonce < lastnonce[i]) {
  240. applog(LOG_DEBUG, "%"PRIpreprv": overflow nonce=%08x lastnonce=%08x", cgpu->proc_repr, nonce, lastnonce[i]);
  241. overflow = true;
  242. } else
  243. lastnonce[i] = nonce;
  244. if (!ztex_checkNonce(cgpu, work, &hdata[i])) {
  245. // do not count errors in the first 500ms after sendHashData (2x250 wait time)
  246. if (count > 2)
  247. dclk_errorCount(&ztex->dclk, 1.0 / ztex->numNonces);
  248. inc_hw_errors_only(thr);
  249. }
  250. for (j=0; j<=ztex->extraSolutions; j++) {
  251. nonce = hdata[i].goldenNonce[j];
  252. if (nonce == ztex->offsNonces) {
  253. continue;
  254. }
  255. found = false;
  256. for (k = 0; k < backlog_max; k++) {
  257. if (backlog[k] == nonce) {
  258. found = true;
  259. break;
  260. }
  261. }
  262. if (!found) {
  263. backlog[backlog_p++] = nonce;
  264. if (backlog_p >= backlog_max)
  265. backlog_p = 0;
  266. work->blk.nonce = 0xffffffff;
  267. if (!j || test_nonce(work, nonce, false))
  268. submit_nonce(thr, work, nonce);
  269. applog(LOG_DEBUG, "%"PRIpreprv": submitted %08x (from N%dE%d)", cgpu->proc_repr, nonce, i, j);
  270. }
  271. }
  272. }
  273. }
  274. dclk_preUpdate(&ztex->dclk);
  275. if (!ztex_updateFreq(thr)) {
  276. // Something really serious happened, so mark this thread as dead!
  277. free(lastnonce);
  278. free(backlog);
  279. return -1;
  280. }
  281. applog(LOG_DEBUG, "%"PRIpreprv": exit %1.8X", cgpu->proc_repr, noncecnt);
  282. work->blk.nonce = 0xffffffff;
  283. free(lastnonce);
  284. free(backlog);
  285. return noncecnt;
  286. }
  287. static struct api_data*
  288. get_ztex_drv_extra_device_status(struct cgpu_info *ztex)
  289. {
  290. struct api_data*root = NULL;
  291. struct libztex_device *ztexr = ztex->device_ztex;
  292. if (ztexr) {
  293. double frequency = ztexr->freqM1 * (ztexr->dclk.freqM + 1);
  294. root = api_add_freq(root, "Frequency", &frequency, true);
  295. }
  296. return root;
  297. }
  298. static bool ztex_prepare(struct thr_info *thr)
  299. {
  300. struct cgpu_info *cgpu = thr->cgpu;
  301. struct libztex_device *ztex = cgpu->device_ztex;
  302. {
  303. char *fpganame = malloc(LIBZTEX_SNSTRING_LEN+3+1);
  304. sprintf(fpganame, "%s-%u", ztex->snString, cgpu->proc_id+1);
  305. cgpu->name = fpganame;
  306. }
  307. ztex_selectFpga(ztex, cgpu->proc_id);
  308. if (libztex_configureFpga(ztex, cgpu->proc_repr) != 0) {
  309. libztex_resetFpga(ztex);
  310. ztex_releaseFpga(ztex);
  311. applog(LOG_ERR, "%"PRIpreprv": Disabling!", cgpu->proc_repr);
  312. thr->cgpu->deven = DEV_DISABLED;
  313. return true;
  314. }
  315. ztex->dclk.freqM = ztex->dclk.freqMaxM+1;
  316. //ztex_updateFreq(thr);
  317. libztex_setFreq(ztex, ztex->dclk.freqMDefault, cgpu->proc_repr);
  318. ztex_releaseFpga(ztex);
  319. notifier_init(thr->work_restart_notifier);
  320. applog(LOG_DEBUG, "%"PRIpreprv": prepare", cgpu->proc_repr);
  321. cgpu->status = LIFE_INIT2;
  322. return true;
  323. }
  324. static void ztex_shutdown(struct thr_info *thr)
  325. {
  326. struct cgpu_info *cgpu = thr->cgpu;
  327. struct libztex_device *ztex = cgpu->device_ztex;
  328. if (!ztex)
  329. return;
  330. cgpu->device_ztex = NULL;
  331. applog(LOG_DEBUG, "%"PRIpreprv": shutdown", cgpu->proc_repr);
  332. if (--ztex->handles)
  333. return;
  334. applog(LOG_DEBUG, "%s: No handles remaining, destroying libztex device", cgpu->dev_repr);
  335. if (ztex->root->numberOfFpgas > 1)
  336. pthread_mutex_destroy(&ztex->mutex);
  337. libztex_destroy_device(ztex);
  338. }
  339. static void ztex_disable(struct thr_info *thr)
  340. {
  341. applog(LOG_ERR, "%"PRIpreprv": Disabling!", thr->cgpu->proc_repr);
  342. thr->cgpu->deven = DEV_DISABLED;
  343. ztex_shutdown(thr);
  344. }
  345. struct device_drv ztex_drv = {
  346. .dname = "ztex",
  347. .name = "ZTX",
  348. .lowl_match = ztex_lowl_match,
  349. .lowl_probe = ztex_lowl_probe,
  350. .get_api_extra_device_status = get_ztex_drv_extra_device_status,
  351. .thread_init = ztex_prepare,
  352. .scanhash = ztex_scanhash,
  353. .thread_shutdown = ztex_shutdown,
  354. };