driver-modminer.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. * Copyright 2012 Andrew Smith
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <limits.h>
  12. #include <stdarg.h>
  13. #include <stdbool.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include "compat.h"
  18. #include "dynclock.h"
  19. #include "logging.h"
  20. #include "miner.h"
  21. #include "fpgautils.h"
  22. #include "util.h"
  23. #define BITSTREAM_FILENAME "fpgaminer_x6500-overclocker-0402.bit"
  24. #define BISTREAM_USER_ID "\2\4$B"
  25. #define MODMINER_MAX_CLOCK 250
  26. #define MODMINER_DEF_CLOCK 190
  27. #define MODMINER_MIN_CLOCK 2
  28. // Commands
  29. #define MODMINER_PING "\x00"
  30. #define MODMINER_GET_VERSION "\x01"
  31. #define MODMINER_FPGA_COUNT "\x02"
  32. // Commands + require FPGAid
  33. #define MODMINER_GET_IDCODE '\x03'
  34. #define MODMINER_GET_USERCODE '\x04'
  35. #define MODMINER_PROGRAM '\x05'
  36. #define MODMINER_SET_CLOCK '\x06'
  37. #define MODMINER_READ_CLOCK '\x07'
  38. #define MODMINER_SEND_WORK '\x08'
  39. #define MODMINER_CHECK_WORK '\x09'
  40. // One byte temperature reply
  41. #define MODMINER_TEMP1 '\x0a'
  42. #define FPGAID_ALL 4
  43. struct device_drv modminer_drv;
  44. struct modminer_fpga_state {
  45. bool work_running;
  46. struct work running_work;
  47. struct work last_work;
  48. struct timeval tv_workstart;
  49. uint32_t hashes;
  50. char next_work_cmd[46];
  51. struct dclk_data dclk;
  52. uint8_t freqMaxMaxM;
  53. // Number of nonces didn't meet pdiff 1, ever
  54. int bad_share_counter;
  55. // Number of nonces did meet pdiff 1, ever
  56. int good_share_counter;
  57. // Time the clock was last reduced due to temperature
  58. struct timeval tv_last_cutoff_reduced;
  59. unsigned char temp;
  60. unsigned char pdone;
  61. };
  62. static inline bool _bailout(int fd, struct cgpu_info*modminer, int prio, const char *fmt, ...) FORMAT_SYNTAX_CHECK(printf, 4, 5);
  63. static inline bool
  64. _bailout(int fd, struct cgpu_info*modminer, int prio, const char *fmt, ...)
  65. {
  66. if (fd != -1)
  67. serial_close(fd);
  68. if (modminer) {
  69. pthread_mutex_t *mutexp = &modminer->device->device_mutex;
  70. modminer->device->device_fd = -1;
  71. mutex_unlock(mutexp);
  72. }
  73. va_list ap;
  74. char buf[LOGBUFSIZ];
  75. va_start(ap, fmt);
  76. vsnprintf(buf, sizeof(buf), fmt, ap);
  77. va_end(ap);
  78. _applog(prio, buf);
  79. return false;
  80. }
  81. #define bailout(...) return _bailout(fd, NULL, __VA_ARGS__);
  82. // 45 noops sent when detecting, in case the device was left in "start job" reading
  83. static const char NOOP[] = MODMINER_PING "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
  84. static bool
  85. modminer_detect_one(const char *devpath)
  86. {
  87. int fd = serial_open(devpath, 0, 10, true);
  88. if (unlikely(fd == -1))
  89. bailout(LOG_DEBUG, "ModMiner detect: failed to open %s", devpath);
  90. char buf[0x100];
  91. ssize_t len;
  92. // Sending a "ping" first, to workaround bug in new firmware betas (see issue #62)
  93. // Sending 45 noops, just in case the device was left in "start job" reading
  94. (void)(write(fd, NOOP, sizeof(NOOP)) ?:0);
  95. while (serial_read(fd, buf, sizeof(buf)) > 0)
  96. ;
  97. if (1 != write(fd, MODMINER_GET_VERSION, 1))
  98. bailout(LOG_DEBUG, "ModMiner detect: write failed on %s (get version)", devpath);
  99. len = serial_read(fd, buf, sizeof(buf)-1);
  100. if (len < 1)
  101. bailout(LOG_DEBUG, "ModMiner detect: no response to version request from %s", devpath);
  102. buf[len] = '\0';
  103. char*devname = strdup(buf);
  104. applog(LOG_DEBUG, "ModMiner identified as: %s", devname);
  105. if (serial_claim_v(devpath, &modminer_drv))
  106. {
  107. serial_close(fd);
  108. return false;
  109. }
  110. if (1 != write(fd, MODMINER_FPGA_COUNT, 1))
  111. bailout(LOG_DEBUG, "ModMiner detect: write failed on %s (get FPGA count)", devpath);
  112. len = read(fd, buf, 1);
  113. if (len < 1)
  114. bailout(LOG_ERR, "ModMiner detect: timeout waiting for FPGA count from %s", devpath);
  115. if (!buf[0])
  116. bailout(LOG_ERR, "ModMiner detect: zero FPGAs reported on %s", devpath);
  117. applog(LOG_DEBUG, "ModMiner %s has %u FPGAs", devname, buf[0]);
  118. serial_close(fd);
  119. struct cgpu_info *modminer;
  120. modminer = calloc(1, sizeof(*modminer));
  121. modminer->drv = &modminer_drv;
  122. mutex_init(&modminer->device_mutex);
  123. modminer->device_path = strdup(devpath);
  124. modminer->device_fd = -1;
  125. modminer->deven = DEV_ENABLED;
  126. modminer->procs = buf[0];
  127. modminer->threads = buf[0];
  128. modminer->name = devname;
  129. modminer->cutofftemp = 85;
  130. return add_cgpu(modminer);
  131. }
  132. #undef bailout
  133. static int
  134. modminer_detect_auto()
  135. {
  136. return serial_autodetect(modminer_detect_one, "ModMiner");
  137. }
  138. static void
  139. modminer_detect()
  140. {
  141. serial_detect_auto(&modminer_drv, modminer_detect_one, modminer_detect_auto);
  142. }
  143. #define bailout(...) return _bailout(-1, modminer, __VA_ARGS__);
  144. #define bailout2(...) return _bailout(fd, modminer, __VA_ARGS__);
  145. #define bailout3(...) _bailout(fd, modminer, __VA_ARGS__);
  146. static bool
  147. modminer_reopen(struct cgpu_info*modminer)
  148. {
  149. serial_close(modminer->device->device_fd);
  150. int fd = serial_open(modminer->device_path, 0, 10, true);
  151. if (unlikely(-1 == fd)) {
  152. applog(LOG_ERR, "%s: Failed to reopen %s", modminer->dev_repr, modminer->device_path);
  153. return false;
  154. }
  155. modminer->device->device_fd = fd;
  156. return true;
  157. }
  158. #define safebailout() do { \
  159. bool _safebailoutrv; \
  160. state->work_running = false; \
  161. _safebailoutrv = modminer_reopen(modminer); \
  162. mutex_unlock(mutexp); \
  163. return _safebailoutrv ? 0 : -1; \
  164. } while(0)
  165. #define check_magic(L) do { \
  166. if (1 != fread(buf, 1, 1, f)) \
  167. bailout(LOG_ERR, "Error reading ModMiner bitstream ('%c')", L); \
  168. if (buf[0] != L) \
  169. bailout(LOG_ERR, "ModMiner bitstream has wrong magic ('%c')", L); \
  170. } while(0)
  171. #define read_str(eng) do { \
  172. if (1 != fread(buf, 2, 1, f)) \
  173. bailout(LOG_ERR, "Error reading ModMiner bitstream (" eng " len)"); \
  174. len = (ubuf[0] << 8) | ubuf[1]; \
  175. if (len >= sizeof(buf)) \
  176. bailout(LOG_ERR, "ModMiner bitstream " eng " too long"); \
  177. if (1 != fread(buf, len, 1, f)) \
  178. bailout(LOG_ERR, "Error reading ModMiner bitstream (" eng ")"); \
  179. buf[len] = '\0'; \
  180. } while(0)
  181. #define status_read(eng) do { \
  182. FD_ZERO(&fds); \
  183. FD_SET(fd, &fds); \
  184. select(fd+1, &fds, NULL, NULL, NULL); \
  185. if (1 != read(fd, buf, 1)) \
  186. bailout2(LOG_ERR, "%s: Error programming %s (" eng ")", modminer->dev_repr, modminer->device_path); \
  187. if (buf[0] != 1) \
  188. bailout2(LOG_ERR, "%s: Wrong " eng " programming %s", modminer->dev_repr, modminer->device_path); \
  189. } while(0)
  190. static bool
  191. modminer_fpga_upload_bitstream(struct cgpu_info*modminer)
  192. {
  193. struct modminer_fpga_state *state = modminer->thr[0]->cgpu_data;
  194. fd_set fds;
  195. char buf[0x100];
  196. unsigned long len, flen;
  197. char fpgaid = FPGAID_ALL;
  198. FILE *f = open_xilinx_bitstream(modminer->drv->dname, modminer->dev_repr, BITSTREAM_FILENAME, &len);
  199. if (!f)
  200. return false;
  201. flen = len;
  202. int fd = modminer->device->device_fd;
  203. applog(LOG_WARNING, "%s: Programming %s... DO NOT EXIT UNTIL COMPLETE", modminer->dev_repr, modminer->device_path);
  204. buf[0] = MODMINER_PROGRAM;
  205. buf[1] = fpgaid;
  206. buf[2] = (len >> 0) & 0xff;
  207. buf[3] = (len >> 8) & 0xff;
  208. buf[4] = (len >> 16) & 0xff;
  209. buf[5] = (len >> 24) & 0xff;
  210. if (6 != write(fd, buf, 6))
  211. bailout2(LOG_ERR, "%s: Error programming %s (cmd)", modminer->dev_repr, modminer->device_path);
  212. status_read("cmd reply");
  213. ssize_t buflen;
  214. char nextstatus = 10;
  215. while (len) {
  216. buflen = len < 32 ? len : 32;
  217. if (fread(buf, buflen, 1, f) != 1)
  218. bailout2(LOG_ERR, "%s: File underrun programming %s (%lu bytes left)", modminer->dev_repr, modminer->device_path, len);
  219. if (write(fd, buf, buflen) != buflen)
  220. bailout2(LOG_ERR, "%s: Error programming %s (data)", modminer->dev_repr, modminer->device_path);
  221. state->pdone = 100 - ((len * 100) / flen);
  222. if (state->pdone >= nextstatus)
  223. {
  224. nextstatus += 10;
  225. applog(LOG_WARNING, "%s: Programming %s... %d%% complete...", modminer->dev_repr, modminer->device_path, state->pdone);
  226. }
  227. status_read("status");
  228. len -= buflen;
  229. }
  230. status_read("final status");
  231. applog(LOG_WARNING, "%s: Done programming %s", modminer->dev_repr, modminer->device_path);
  232. return true;
  233. }
  234. static bool
  235. modminer_device_prepare(struct cgpu_info *modminer)
  236. {
  237. int fd = serial_open(modminer->device_path, 0, 10, true);
  238. if (unlikely(-1 == fd))
  239. bailout(LOG_ERR, "%s: Failed to open %s", modminer->dev_repr, modminer->device_path);
  240. modminer->device->device_fd = fd;
  241. applog(LOG_INFO, "%s: Opened %s", modminer->dev_repr, modminer->device_path);
  242. return true;
  243. }
  244. #undef bailout
  245. static bool
  246. modminer_fpga_prepare(struct thr_info *thr)
  247. {
  248. struct cgpu_info *proc = thr->cgpu;
  249. struct cgpu_info *modminer = proc->device;
  250. // Don't need to lock the mutex here, since prepare runs from the main thread before the miner threads start
  251. if (modminer->device->device_fd == -1 && !modminer_device_prepare(modminer))
  252. return false;
  253. struct modminer_fpga_state *state;
  254. state = thr->cgpu_data = calloc(1, sizeof(struct modminer_fpga_state));
  255. dclk_prepare(&state->dclk);
  256. state->dclk.freqMinM = MODMINER_MIN_CLOCK / 2;
  257. state->next_work_cmd[0] = MODMINER_SEND_WORK;
  258. state->next_work_cmd[1] = proc->proc_id; // FPGA id
  259. proc->status = LIFE_INIT2;
  260. return true;
  261. }
  262. static bool
  263. modminer_change_clock(struct thr_info*thr, bool needlock, signed char delta)
  264. {
  265. struct cgpu_info*modminer = thr->cgpu;
  266. struct modminer_fpga_state *state = thr->cgpu_data;
  267. char fpgaid = modminer->proc_id;
  268. pthread_mutex_t *mutexp = &modminer->device->device_mutex;
  269. int fd;
  270. unsigned char cmd[6], buf[1];
  271. unsigned char clk;
  272. clk = (state->dclk.freqM * 2) + delta;
  273. cmd[0] = MODMINER_SET_CLOCK;
  274. cmd[1] = fpgaid;
  275. cmd[2] = clk;
  276. cmd[3] = cmd[4] = cmd[5] = '\0';
  277. if (needlock)
  278. mutex_lock(mutexp);
  279. fd = modminer->device->device_fd;
  280. if (6 != write(fd, cmd, 6))
  281. bailout2(LOG_ERR, "%s: Error writing (set frequency)", modminer->proc_repr);
  282. if (serial_read(fd, &buf, 1) != 1)
  283. bailout2(LOG_ERR, "%s: Error reading (set frequency)", modminer->proc_repr);
  284. if (needlock)
  285. mutex_unlock(mutexp);
  286. if (buf[0])
  287. state->dclk.freqM = clk / 2;
  288. else
  289. return false;
  290. return true;
  291. }
  292. static bool modminer_dclk_change_clock(struct thr_info*thr, int multiplier)
  293. {
  294. struct cgpu_info *modminer = thr->cgpu;
  295. struct modminer_fpga_state *state = thr->cgpu_data;
  296. uint8_t oldFreq = state->dclk.freqM;
  297. signed char delta = (multiplier - oldFreq) * 2;
  298. if (unlikely(!modminer_change_clock(thr, true, delta)))
  299. return false;
  300. dclk_msg_freqchange(modminer->proc_repr, oldFreq * 2, state->dclk.freqM * 2, NULL);
  301. return true;
  302. }
  303. static bool
  304. modminer_reduce_clock(struct thr_info*thr, bool needlock)
  305. {
  306. struct modminer_fpga_state *state = thr->cgpu_data;
  307. if (state->dclk.freqM <= MODMINER_MIN_CLOCK / 2)
  308. return false;
  309. return modminer_change_clock(thr, needlock, -2);
  310. }
  311. static bool _modminer_get_nonce(struct cgpu_info*modminer, char fpgaid, uint32_t*nonce)
  312. {
  313. int fd = modminer->device->device_fd;
  314. char cmd[2] = {MODMINER_CHECK_WORK, fpgaid};
  315. if (write(fd, cmd, 2) != 2) {
  316. applog(LOG_ERR, "%s: Error writing (get nonce)", modminer->proc_repr);
  317. return false;
  318. }
  319. if (4 != serial_read(fd, nonce, 4)) {
  320. applog(LOG_ERR, "%s: Short read (get nonce)", modminer->proc_repr);
  321. return false;
  322. }
  323. return true;
  324. }
  325. static bool
  326. modminer_fpga_init(struct thr_info *thr)
  327. {
  328. struct cgpu_info *modminer = thr->cgpu;
  329. struct modminer_fpga_state *state = thr->cgpu_data;
  330. int fd;
  331. char fpgaid = modminer->proc_id;
  332. pthread_mutex_t *mutexp = &modminer->device->device_mutex;
  333. uint32_t nonce;
  334. unsigned char cmd[2], buf[4];
  335. mutex_lock(mutexp);
  336. fd = modminer->device->device_fd;
  337. if (fd == -1) {
  338. // Died in another thread...
  339. mutex_unlock(mutexp);
  340. return false;
  341. }
  342. cmd[0] = MODMINER_GET_USERCODE;
  343. cmd[1] = fpgaid;
  344. if (write(fd, cmd, 2) != 2)
  345. bailout2(LOG_ERR, "%s: Error writing (read USER code)", modminer->proc_repr);
  346. if (serial_read(fd, buf, 4) != 4)
  347. bailout2(LOG_ERR, "%s: Error reading (read USER code)", modminer->proc_repr);
  348. if (memcmp(buf, BISTREAM_USER_ID, 4)) {
  349. applog(LOG_ERR, "%s: FPGA not programmed", modminer->proc_repr);
  350. if (!modminer_fpga_upload_bitstream(modminer))
  351. return false;
  352. } else if (opt_force_dev_init && !((struct modminer_fpga_state *)modminer->device->thr[0]->cgpu_data)->pdone) {
  353. applog(LOG_DEBUG, "%s: FPGA is already programmed, but --force-dev-init is set",
  354. modminer->proc_repr);
  355. if (!modminer_fpga_upload_bitstream(modminer))
  356. return false;
  357. }
  358. else
  359. applog(LOG_DEBUG, "%s: FPGA is already programmed :)", modminer->proc_repr);
  360. state->pdone = 101;
  361. state->dclk.freqM = MODMINER_MAX_CLOCK / 2 + 1; // Will be reduced immediately
  362. while (1) {
  363. if (state->dclk.freqM <= MODMINER_MIN_CLOCK / 2)
  364. bailout2(LOG_ERR, "%s: Hit minimum trying to find acceptable frequencies", modminer->proc_repr);
  365. --state->dclk.freqM;
  366. if (!modminer_change_clock(thr, false, 0))
  367. // MCU rejected assignment
  368. continue;
  369. if (!_modminer_get_nonce(modminer, fpgaid, &nonce))
  370. bailout2(LOG_ERR, "%s: Error detecting acceptable frequencies", modminer->proc_repr);
  371. if (!memcmp(&nonce, "\x00\xff\xff\xff", 4))
  372. // MCU took assignment, but disabled FPGA
  373. continue;
  374. break;
  375. }
  376. state->freqMaxMaxM =
  377. state->dclk.freqMaxM = state->dclk.freqM;
  378. if (MODMINER_DEF_CLOCK / 2 < state->dclk.freqM) {
  379. if (!modminer_change_clock(thr, false, -(state->dclk.freqM * 2 - MODMINER_DEF_CLOCK)))
  380. applog(LOG_WARNING, "%s: Failed to set desired initial frequency of %u", modminer->proc_repr, MODMINER_DEF_CLOCK);
  381. }
  382. state->dclk.freqMDefault = state->dclk.freqM;
  383. applog(LOG_WARNING, "%s: Frequency set to %u MHz (range: %u-%u)", modminer->proc_repr, state->dclk.freqM * 2, MODMINER_MIN_CLOCK, state->dclk.freqMaxM * 2);
  384. mutex_unlock(mutexp);
  385. thr->primary_thread = true;
  386. return true;
  387. }
  388. static
  389. bool get_modminer_upload_percent(char *buf, size_t bufsz, struct cgpu_info *modminer, __maybe_unused bool per_processor)
  390. {
  391. char pdone = ((struct modminer_fpga_state*)(modminer->device->thr[0]->cgpu_data))->pdone;
  392. if (pdone != 101) {
  393. tailsprintf(buf, bufsz, "%3d%% ", pdone);
  394. return true;
  395. }
  396. return false;
  397. }
  398. static void modminer_get_temperature(struct cgpu_info *modminer, struct thr_info *thr)
  399. {
  400. struct modminer_fpga_state *state = thr->cgpu_data;
  401. #ifdef WIN32
  402. /* Workaround for bug in Windows driver */
  403. if (!modminer_reopen(modminer))
  404. return;
  405. #endif
  406. int fd = modminer->device->device_fd;
  407. int fpgaid = modminer->proc_id;
  408. char cmd[2] = {MODMINER_TEMP1, fpgaid};
  409. char temperature;
  410. if (2 == write(fd, cmd, 2) && read(fd, &temperature, 1) == 1)
  411. {
  412. state->temp = temperature;
  413. if (temperature > modminer->targettemp + opt_hysteresis) {
  414. {
  415. struct timeval now;
  416. cgtime(&now);
  417. if (timer_elapsed(&state->tv_last_cutoff_reduced, &now)) {
  418. state->tv_last_cutoff_reduced = now;
  419. int oldFreq = state->dclk.freqM;
  420. if (modminer_reduce_clock(thr, false))
  421. applog(LOG_NOTICE, "%s: Frequency %s from %u to %u MHz (temp: %d)",
  422. modminer->proc_repr,
  423. (oldFreq > state->dclk.freqM ? "dropped" : "raised "),
  424. oldFreq * 2, state->dclk.freqM * 2,
  425. temperature
  426. );
  427. state->dclk.freqMaxM = state->dclk.freqM;
  428. }
  429. }
  430. }
  431. else
  432. if (state->dclk.freqMaxM < state->freqMaxMaxM && temperature < modminer->targettemp) {
  433. if (temperature < modminer->targettemp - opt_hysteresis) {
  434. state->dclk.freqMaxM = state->freqMaxMaxM;
  435. } else {
  436. ++state->dclk.freqMaxM;
  437. }
  438. }
  439. }
  440. }
  441. static bool modminer_get_stats(struct cgpu_info *modminer)
  442. {
  443. pthread_mutex_t *mutexp = &modminer->device->device_mutex;
  444. int hottest = 0;
  445. bool get_temp = (modminer->deven != DEV_ENABLED);
  446. // Getting temperature more efficiently while enabled
  447. for (int i = modminer->threads; i--; ) {
  448. struct thr_info*thr = modminer->thr[i];
  449. struct modminer_fpga_state *state = thr->cgpu_data;
  450. if (get_temp)
  451. {
  452. mutex_lock(mutexp);
  453. modminer_get_temperature(modminer, thr);
  454. mutex_unlock(mutexp);
  455. }
  456. int temp = state->temp;
  457. if (temp > hottest)
  458. hottest = temp;
  459. }
  460. modminer->temp = (float)hottest;
  461. return true;
  462. }
  463. static struct api_data*
  464. get_modminer_drv_extra_device_status(struct cgpu_info*modminer)
  465. {
  466. struct api_data*root = NULL;
  467. struct thr_info*thr = modminer->thr[0];
  468. struct modminer_fpga_state *state = thr->cgpu_data;
  469. double d;
  470. d = (double)state->dclk.freqM * 2;
  471. root = api_add_freq(root, "Frequency", &d, true);
  472. d = (double)state->dclk.freqMaxM * 2;
  473. root = api_add_freq(root, "Cool Max Frequency", &d, true);
  474. d = (double)state->freqMaxMaxM * 2;
  475. root = api_add_freq(root, "Max Frequency", &d, true);
  476. root = api_add_int(root, "Hardware Errors", &state->bad_share_counter, true);
  477. root = api_add_int(root, "Valid Nonces", &state->good_share_counter, true);
  478. return root;
  479. }
  480. static bool
  481. modminer_prepare_next_work(struct modminer_fpga_state*state, struct work*work)
  482. {
  483. char *midstate = state->next_work_cmd + 2;
  484. char *taildata = midstate + 32;
  485. if (!(memcmp(midstate, work->midstate, 32) || memcmp(taildata, work->data + 64, 12)))
  486. return false;
  487. memcpy(midstate, work->midstate, 32);
  488. memcpy(taildata, work->data + 64, 12);
  489. return true;
  490. }
  491. static bool
  492. modminer_start_work(struct thr_info*thr)
  493. {
  494. fd_set fds;
  495. struct cgpu_info*modminer = thr->cgpu;
  496. struct modminer_fpga_state *state = thr->cgpu_data;
  497. pthread_mutex_t *mutexp = &modminer->device->device_mutex;
  498. int fd;
  499. char buf[1];
  500. mutex_lock(mutexp);
  501. fd = modminer->device->device_fd;
  502. if (unlikely(fd == -1)) {
  503. if (!modminer_reopen(modminer)) {
  504. mutex_unlock(mutexp);
  505. return false;
  506. }
  507. fd = modminer->device->device_fd;
  508. }
  509. if (46 != write(fd, state->next_work_cmd, 46))
  510. bailout2(LOG_ERR, "%s: Error writing (start work)", modminer->proc_repr);
  511. timer_set_now(&state->tv_workstart);
  512. state->hashes = 0;
  513. status_read("start work");
  514. mutex_unlock(mutexp);
  515. if (opt_debug) {
  516. char xdata[161];
  517. bin2hex(xdata, state->running_work.data, 80);
  518. applog(LOG_DEBUG, "%s: Started work: %s",
  519. modminer->proc_repr, xdata);
  520. }
  521. return true;
  522. }
  523. #define work_restart(thr) thr->work_restart
  524. #define NONCE_CHARS(nonce) \
  525. (int)((unsigned char*)&nonce)[3], \
  526. (int)((unsigned char*)&nonce)[2], \
  527. (int)((unsigned char*)&nonce)[1], \
  528. (int)((unsigned char*)&nonce)[0]
  529. static int64_t
  530. modminer_process_results(struct thr_info*thr)
  531. {
  532. struct cgpu_info*modminer = thr->cgpu;
  533. struct modminer_fpga_state *state = thr->cgpu_data;
  534. char fpgaid = modminer->proc_id;
  535. pthread_mutex_t *mutexp = &modminer->device->device_mutex;
  536. struct work *work = &state->running_work;
  537. uint32_t nonce;
  538. long iter;
  539. int immediate_bad_nonces = 0, immediate_nonces = 0;
  540. bool bad;
  541. mutex_lock(mutexp);
  542. modminer_get_temperature(modminer, thr);
  543. iter = 200;
  544. while (1) {
  545. if (!_modminer_get_nonce(modminer, fpgaid, &nonce))
  546. safebailout();
  547. mutex_unlock(mutexp);
  548. if (memcmp(&nonce, "\xff\xff\xff\xff", 4)) {
  549. nonce = le32toh(nonce);
  550. bad = !test_nonce(work, nonce, false);
  551. ++immediate_nonces;
  552. if (!bad)
  553. applog(LOG_DEBUG, "%s: Nonce for current work: %02x%02x%02x%02x",
  554. modminer->proc_repr,
  555. NONCE_CHARS(nonce));
  556. else
  557. if (test_nonce(&state->last_work, nonce, false))
  558. {
  559. applog(LOG_DEBUG, "%s: Nonce for previous work: %02x%02x%02x%02x",
  560. modminer->proc_repr,
  561. NONCE_CHARS(nonce));
  562. work = &state->last_work;
  563. bad = false;
  564. }
  565. if (!bad)
  566. {
  567. ++state->good_share_counter;
  568. submit_nonce(thr, work, nonce);
  569. }
  570. else {
  571. inc_hw_errors(thr, work, nonce);
  572. ++state->bad_share_counter;
  573. ++immediate_bad_nonces;
  574. }
  575. }
  576. if (work_restart(thr) || !--iter)
  577. break;
  578. cgsleep_ms(1);
  579. if (work_restart(thr))
  580. break;
  581. mutex_lock(mutexp);
  582. }
  583. struct timeval tv_workend, elapsed;
  584. timer_set_now(&tv_workend);
  585. timersub(&tv_workend, &state->tv_workstart, &elapsed);
  586. uint64_t hashes = (uint64_t)state->dclk.freqM * 2 * (((uint64_t)elapsed.tv_sec * 1000000) + elapsed.tv_usec);
  587. if (hashes > 0xffffffff)
  588. {
  589. applog(LOG_WARNING, "%s: Finished work before new one sent", modminer->proc_repr);
  590. hashes = 0xffffffff;
  591. }
  592. if (hashes <= state->hashes)
  593. hashes = 1;
  594. else
  595. hashes -= state->hashes;
  596. state->hashes += hashes;
  597. dclk_gotNonces(&state->dclk);
  598. if (immediate_bad_nonces)
  599. dclk_errorCount(&state->dclk, ((double)immediate_bad_nonces) / (double)immediate_nonces);
  600. dclk_preUpdate(&state->dclk);
  601. if (!dclk_updateFreq(&state->dclk, modminer_dclk_change_clock, thr))
  602. return -1;
  603. return hashes;
  604. }
  605. static int64_t
  606. modminer_scanhash(struct thr_info*thr, struct work*work, int64_t __maybe_unused max_nonce)
  607. {
  608. struct modminer_fpga_state *state = thr->cgpu_data;
  609. int64_t hashes = 0;
  610. bool startwork;
  611. startwork = modminer_prepare_next_work(state, work);
  612. if (startwork) {
  613. /* HACK: For some reason, this is delayed a bit
  614. * Let last_work handle the end of the work,
  615. * and start the next one immediately
  616. */
  617. }
  618. else
  619. if (state->work_running) {
  620. hashes = modminer_process_results(thr);
  621. if (work_restart(thr)) {
  622. state->work_running = false;
  623. return hashes;
  624. }
  625. } else
  626. state->work_running = true;
  627. if (startwork) {
  628. __copy_work(&state->last_work, &state->running_work);
  629. __copy_work(&state->running_work, work);
  630. if (!modminer_start_work(thr))
  631. return -1;
  632. }
  633. // This is intentionally early
  634. work->blk.nonce += hashes;
  635. return hashes;
  636. }
  637. static void
  638. modminer_fpga_shutdown(struct thr_info *thr)
  639. {
  640. for (struct cgpu_info *proc = thr->cgpu->device; proc; proc = proc->next_proc)
  641. proc->status = LIFE_DEAD2;
  642. free(thr->cgpu_data);
  643. thr->cgpu_data = NULL;
  644. }
  645. static
  646. bool modminer_user_set_clock(struct cgpu_info *cgpu, const int val)
  647. {
  648. struct thr_info * const thr = cgpu->thr[0];
  649. struct modminer_fpga_state * const state = thr->cgpu_data;
  650. const int multiplier = val / 2;
  651. const uint8_t oldFreqM = state->dclk.freqM;
  652. const signed char delta = (multiplier - oldFreqM) * 2;
  653. state->dclk.freqMDefault = multiplier;
  654. const bool rv = modminer_change_clock(thr, true, delta);
  655. if (likely(rv))
  656. dclk_msg_freqchange(cgpu->proc_repr, oldFreqM * 2, state->dclk.freqM * 2, " on user request");
  657. return rv;
  658. }
  659. static char *modminer_set_device(struct cgpu_info *modminer, char *option, char *setting, char *replybuf)
  660. {
  661. int val;
  662. if (strcasecmp(option, "help") == 0) {
  663. sprintf(replybuf, "clock: range %d-%d and a multiple of 2",
  664. MODMINER_MIN_CLOCK, MODMINER_MAX_CLOCK);
  665. return replybuf;
  666. }
  667. if (strcasecmp(option, "clock") == 0) {
  668. if (!setting || !*setting) {
  669. sprintf(replybuf, "missing clock setting");
  670. return replybuf;
  671. }
  672. val = atoi(setting);
  673. if (val < MODMINER_MIN_CLOCK || val > MODMINER_MAX_CLOCK || (val & 1) != 0) {
  674. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d and a multiple of 2",
  675. setting, MODMINER_MIN_CLOCK, MODMINER_MAX_CLOCK);
  676. return replybuf;
  677. }
  678. if (unlikely(!modminer_user_set_clock(modminer, val)))
  679. {
  680. sprintf(replybuf, "Set clock failed: %s",
  681. modminer->proc_repr);
  682. return replybuf;
  683. }
  684. return NULL;
  685. }
  686. sprintf(replybuf, "Unknown option: %s", option);
  687. return replybuf;
  688. }
  689. #ifdef HAVE_CURSES
  690. static
  691. void modminer_tui_wlogprint_choices(struct cgpu_info *cgpu)
  692. {
  693. wlogprint("[C]lock speed ");
  694. }
  695. static
  696. const char *modminer_tui_handle_choice(struct cgpu_info *cgpu, int input)
  697. {
  698. static char buf[0x100]; // Static for replies
  699. switch (input)
  700. {
  701. case 'c': case 'C':
  702. {
  703. int val;
  704. char *intvar;
  705. sprintf(buf, "Set clock speed (range %d-%d, multiple of 2)", MODMINER_MIN_CLOCK, MODMINER_MAX_CLOCK);
  706. intvar = curses_input(buf);
  707. if (!intvar)
  708. return "Invalid clock speed\n";
  709. val = atoi(intvar);
  710. free(intvar);
  711. if (val < MODMINER_MIN_CLOCK || val > MODMINER_MAX_CLOCK || (val & 1) != 0)
  712. return "Invalid clock speed\n";
  713. if (unlikely(!modminer_user_set_clock(cgpu, val)))
  714. return "Set clock failed\n";
  715. return "Clock speed changed\n";
  716. }
  717. }
  718. return NULL;
  719. }
  720. static
  721. void modminer_wlogprint_status(struct cgpu_info *cgpu)
  722. {
  723. struct modminer_fpga_state *state = cgpu->thr[0]->cgpu_data;
  724. wlogprint("Clock speed: %d\n", (int)(state->dclk.freqM * 2));
  725. }
  726. #endif
  727. struct device_drv modminer_drv = {
  728. .dname = "modminer",
  729. .name = "MMQ",
  730. .drv_detect = modminer_detect,
  731. .override_statline_temp2 = get_modminer_upload_percent,
  732. .get_stats = modminer_get_stats,
  733. .get_api_extra_device_status = get_modminer_drv_extra_device_status,
  734. .set_device = modminer_set_device,
  735. #ifdef HAVE_CURSES
  736. .proc_wlogprint_status = modminer_wlogprint_status,
  737. .proc_tui_wlogprint_choices = modminer_tui_wlogprint_choices,
  738. .proc_tui_handle_choice = modminer_tui_handle_choice,
  739. #endif
  740. .thread_prepare = modminer_fpga_prepare,
  741. .thread_init = modminer_fpga_init,
  742. .scanhash = modminer_scanhash,
  743. .thread_shutdown = modminer_fpga_shutdown,
  744. };