driver-ztex.c 11 KB

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