driver-ztex.c 11 KB

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