driver-ztex.c 11 KB

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