driver-ztex.c 11 KB

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