driver-ztex.c 11 KB

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