driver-ztex.c 10 KB

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