driver-ztex.c 10 KB

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