driver-ztex.c 10 KB

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