driver-modminer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. #include <unistd.h>
  13. #include "fpgautils.h"
  14. #include "logging.h"
  15. #include "miner.h"
  16. #define BITSTREAM_FILENAME "fpgaminer_top_fixed7_197MHz.ncd"
  17. #define BISTREAM_USER_ID "\2\4$B"
  18. struct device_api modminer_api;
  19. static inline bool
  20. _bailout(int fd, struct cgpu_info*modminer, int prio, const char *fmt, ...)
  21. {
  22. if (fd != -1)
  23. serial_close(fd);
  24. if (modminer) {
  25. modminer->device_fd = -1;
  26. mutex_unlock(&modminer->device_mutex);
  27. }
  28. va_list ap;
  29. va_start(ap, fmt);
  30. vapplog(prio, fmt, ap);
  31. va_end(ap);
  32. return false;
  33. }
  34. #define bailout(...) return _bailout(fd, NULL, __VA_ARGS__);
  35. static bool
  36. modminer_detect_one(const char *devpath)
  37. {
  38. int fd = serial_open(devpath, 0, 10, true);
  39. if (unlikely(fd == -1))
  40. bailout(LOG_DEBUG, "ModMiner detect: failed to open %s", devpath);
  41. char buf[0x100];
  42. size_t len;
  43. // Sending a "ping" first, to workaround bug in new firmware betas (see issue #62)
  44. // Sending 45 noops, just in case the device was left in "start job" reading
  45. (void)(write(fd, "\0\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", 46) ?:0);
  46. while (serial_read(fd, buf, sizeof(buf)) > 0)
  47. ;
  48. if (1 != write(fd, "\x01", 1)) // Get version
  49. bailout(LOG_DEBUG, "ModMiner detect: write failed on %s (get version)", devpath);
  50. len = serial_read(fd, buf, sizeof(buf)-1);
  51. if (len < 1)
  52. bailout(LOG_DEBUG, "ModMiner detect: no response to version request from %s", devpath);
  53. buf[len] = '\0';
  54. char*devname = strdup(buf);
  55. applog(LOG_DEBUG, "ModMiner identified as: %s", devname);
  56. if (1 != write(fd, "\x02", 1)) // Get FPGA count
  57. bailout(LOG_DEBUG, "ModMiner detect: write failed on %s (get FPGA count)", devpath);
  58. len = read(fd, buf, 1);
  59. if (len < 1)
  60. bailout(LOG_ERR, "ModMiner detect: timeout waiting for FPGA count from %s", devpath);
  61. if (!buf[0])
  62. bailout(LOG_ERR, "ModMiner detect: zero FPGAs reported on %s", devpath);
  63. applog(LOG_DEBUG, "ModMiner %s has %u FPGAs", devname, buf[0]);
  64. serial_close(fd);
  65. struct cgpu_info *modminer;
  66. modminer = calloc(1, sizeof(*modminer));
  67. modminer->api = &modminer_api;
  68. mutex_init(&modminer->device_mutex);
  69. modminer->device_path = strdup(devpath);
  70. modminer->device_fd = -1;
  71. modminer->deven = DEV_ENABLED;
  72. modminer->threads = buf[0];
  73. modminer->name = devname;
  74. modminer->cutofftemp = 85;
  75. return add_cgpu(modminer);
  76. }
  77. #undef bailout
  78. static char
  79. modminer_detect_auto()
  80. {
  81. return
  82. serial_autodetect_udev (modminer_detect_one, "BTCFPGA*ModMiner") ?:
  83. serial_autodetect_devserial(modminer_detect_one, "BTCFPGA_ModMiner") ?:
  84. 0;
  85. }
  86. static void
  87. modminer_detect()
  88. {
  89. serial_detect_auto("modminer", modminer_detect_one, modminer_detect_auto);
  90. }
  91. static void
  92. get_modminer_statline_before(char *buf, struct cgpu_info *modminer)
  93. {
  94. float gt = modminer->temp;
  95. if (gt > 0)
  96. tailsprintf(buf, "%5.1fC ", gt);
  97. else
  98. tailsprintf(buf, " ", gt);
  99. tailsprintf(buf, " | ");
  100. }
  101. #define bailout(...) return _bailout(-1, modminer, __VA_ARGS__);
  102. #define bailout2(...) return _bailout(fd, modminer, __VA_ARGS__);
  103. #define check_magic(L) do { \
  104. if (1 != fread(buf, 1, 1, f)) \
  105. bailout(LOG_ERR, "Error reading ModMiner firmware ('%c')", L); \
  106. if (buf[0] != L) \
  107. bailout(LOG_ERR, "ModMiner firmware has wrong magic ('%c')", L); \
  108. } while(0)
  109. #define read_str(eng) do { \
  110. if (1 != fread(buf, 2, 1, f)) \
  111. bailout(LOG_ERR, "Error reading ModMiner firmware (" eng " len)"); \
  112. len = (ubuf[0] << 8) | ubuf[1]; \
  113. if (len >= sizeof(buf)) \
  114. bailout(LOG_ERR, "ModMiner firmware " eng " too long"); \
  115. if (1 != fread(buf, len, 1, f)) \
  116. bailout(LOG_ERR, "Error reading ModMiner firmware (" eng ")"); \
  117. buf[len] = '\0'; \
  118. } while(0)
  119. #define status_read(eng) do { \
  120. FD_SET(fd, &fds); \
  121. select(fd+1, &fds, NULL, NULL, NULL); \
  122. if (1 != read(fd, buf, 1)) \
  123. bailout2(LOG_ERR, "%s %u: Error programming %s (" eng ")", modminer->api->name, modminer->device_id, modminer->device_path); \
  124. if (buf[0] != 1) \
  125. bailout2(LOG_ERR, "%s %u: Wrong " eng " programming %s", modminer->api->name, modminer->device_id, modminer->device_path); \
  126. } while(0)
  127. static bool
  128. modminer_fpga_upload_bitstream(struct cgpu_info*modminer)
  129. {
  130. fd_set fds;
  131. char buf[0x100];
  132. unsigned char *ubuf = (unsigned char*)buf;
  133. unsigned long len;
  134. char *p;
  135. const char *fwfile = BITSTREAM_FILENAME;
  136. char fpgaid = 4; // "all FPGAs"
  137. FILE *f = open_bitstream("modminer", fwfile);
  138. if (!f)
  139. bailout(LOG_ERR, "Error opening ModMiner firmware file %s", fwfile);
  140. if (1 != fread(buf, 2, 1, f))
  141. bailout(LOG_ERR, "Error reading ModMiner firmware (magic)");
  142. if (buf[0] || buf[1] != 9)
  143. bailout(LOG_ERR, "ModMiner firmware has wrong magic (9)");
  144. if (-1 == fseek(f, 11, SEEK_CUR))
  145. bailout(LOG_ERR, "ModMiner firmware seek failed");
  146. check_magic('a');
  147. read_str("design name");
  148. applog(LOG_DEBUG, "ModMiner firmware file %s info:", fwfile);
  149. applog(LOG_DEBUG, " Design name: %s", buf);
  150. p = strrchr(buf, ';') ?: buf;
  151. p = strrchr(buf, '=') ?: p;
  152. if (p[0] == '=')
  153. ++p;
  154. long fwusercode = strtol(p, &p, 16);
  155. if (p[0] != '\0')
  156. bailout(LOG_ERR, "Bad usercode in ModMiner firmware file");
  157. if (fwusercode == 0xffffffff)
  158. bailout(LOG_ERR, "ModMiner firmware doesn't support user code");
  159. applog(LOG_DEBUG, " Version: %u, build %u", (fwusercode >> 8) & 0xff, fwusercode & 0xff);
  160. check_magic('b');
  161. read_str("part number");
  162. applog(LOG_DEBUG, " Part number: %s", buf);
  163. check_magic('c');
  164. read_str("build date");
  165. applog(LOG_DEBUG, " Build date: %s", buf);
  166. check_magic('d');
  167. read_str("build time");
  168. applog(LOG_DEBUG, " Build time: %s", buf);
  169. check_magic('e');
  170. if (1 != fread(buf, 4, 1, f))
  171. bailout(LOG_ERR, "Error reading ModMiner firmware (data len)");
  172. len = ((unsigned long)ubuf[0] << 24) | ((unsigned long)ubuf[1] << 16) | (ubuf[2] << 8) | ubuf[3];
  173. applog(LOG_DEBUG, " Bitstream size: %lu", len);
  174. int fd = modminer->device_fd;
  175. applog(LOG_WARNING, "%s %u: Programming %s... DO NOT EXIT CGMINER UNTIL COMPLETE", modminer->api->name, modminer->device_id, modminer->device_path);
  176. buf[0] = '\x05'; // Program Bitstream
  177. buf[1] = fpgaid;
  178. buf[2] = (len >> 0) & 0xff;
  179. buf[3] = (len >> 8) & 0xff;
  180. buf[4] = (len >> 16) & 0xff;
  181. buf[5] = (len >> 24) & 0xff;
  182. if (6 != write(fd, buf, 6))
  183. bailout2(LOG_ERR, "%s %u: Error programming %s (cmd)", modminer->api->name, modminer->device_id, modminer->device_path);
  184. status_read("cmd reply");
  185. size_t buflen;
  186. while (len) {
  187. buflen = len < 32 ? len : 32;
  188. if (fread(buf, buflen, 1, f) != 1)
  189. bailout2(LOG_ERR, "%s %u: File underrun programming %s (%d bytes left)", modminer->api->name, modminer->device_id, modminer->device_path, len);
  190. if (write(fd, buf, buflen) != buflen)
  191. bailout2(LOG_ERR, "%s %u: Error programming %s (data)", modminer->api->name, modminer->device_id, modminer->device_path);
  192. status_read("status");
  193. len -= buflen;
  194. }
  195. status_read("final status");
  196. applog(LOG_WARNING, "%s %u: Done programming %s", modminer->api->name, modminer->device_id, modminer->device_path);
  197. return true;
  198. }
  199. static bool
  200. modminer_device_prepare(struct cgpu_info *modminer)
  201. {
  202. int fd = serial_open(modminer->device_path, 0, /*FIXME=-1*/3000, true);
  203. if (unlikely(-1 == fd))
  204. bailout(LOG_ERR, "%s %u: Failed to open %s", modminer->api->name, modminer->device_id, modminer->device_path);
  205. modminer->device_fd = fd;
  206. applog(LOG_INFO, "%s %u: Opened %s", modminer->api->name, modminer->device_id, modminer->device_path);
  207. struct timeval now;
  208. gettimeofday(&now, NULL);
  209. get_datestamp(modminer->init, &now);
  210. return true;
  211. }
  212. #undef bailout
  213. struct modminer_fpga_state {
  214. bool work_running;
  215. struct work running_work;
  216. struct timeval tv_workstart;
  217. uint32_t hashes;
  218. char next_work_cmd[46];
  219. unsigned char clock;
  220. int no_nonce_counter;
  221. int good_share_counter;
  222. time_t last_cutoff_reduced;
  223. };
  224. static bool
  225. modminer_fpga_prepare(struct thr_info *thr)
  226. {
  227. struct cgpu_info *modminer = thr->cgpu;
  228. // Don't need to lock the mutex here, since prepare runs from the main thread before the miner threads start
  229. if (modminer->device_fd == -1 && !modminer_device_prepare(modminer))
  230. return false;
  231. struct modminer_fpga_state *state;
  232. state = thr->cgpu_data = calloc(1, sizeof(struct modminer_fpga_state));
  233. state->next_work_cmd[0] = '\x08'; // Send Job
  234. state->next_work_cmd[1] = thr->device_thread; // FPGA id
  235. return true;
  236. }
  237. static bool
  238. modminer_reduce_clock(struct thr_info*thr, bool needlock)
  239. {
  240. struct cgpu_info*modminer = thr->cgpu;
  241. struct modminer_fpga_state *state = thr->cgpu_data;
  242. char fpgaid = thr->device_thread;
  243. int fd = modminer->device_fd;
  244. unsigned char cmd[6], buf[1];
  245. if (state->clock <= 100)
  246. return false;
  247. cmd[0] = '\x06'; // set clock speed
  248. cmd[1] = fpgaid;
  249. cmd[2] = state->clock -= 2;
  250. cmd[3] = cmd[4] = cmd[5] = '\0';
  251. if (needlock)
  252. mutex_lock(&modminer->device_mutex);
  253. if (6 != write(fd, cmd, 6))
  254. bailout2(LOG_ERR, "%s %u.%u: Error writing (set clock speed)", modminer->api->name, modminer->device_id, fpgaid);
  255. if (serial_read(fd, &buf, 1) != 1)
  256. bailout2(LOG_ERR, "%s %u.%u: Error reading (set clock speed)", modminer->api->name, modminer->device_id, fpgaid);
  257. if (needlock)
  258. mutex_unlock(&modminer->device_mutex);
  259. applog(LOG_WARNING, "%s %u.%u: Setting clock speed to %u", modminer->api->name, modminer->device_id, fpgaid, state->clock);
  260. return true;
  261. }
  262. static bool
  263. modminer_fpga_init(struct thr_info *thr)
  264. {
  265. struct cgpu_info *modminer = thr->cgpu;
  266. struct modminer_fpga_state *state = thr->cgpu_data;
  267. int fd;
  268. char fpgaid = thr->device_thread;
  269. unsigned char cmd[2], buf[4];
  270. mutex_lock(&modminer->device_mutex);
  271. fd = modminer->device_fd;
  272. if (fd == -1) {
  273. // Died in another thread...
  274. mutex_unlock(&modminer->device_mutex);
  275. return false;
  276. }
  277. cmd[0] = '\x04'; // Read USER code (bitstream id)
  278. cmd[1] = fpgaid;
  279. if (write(fd, cmd, 2) != 2)
  280. bailout2(LOG_ERR, "%s %u.%u: Error writing (read USER code)", modminer->api->name, modminer->device_id, fpgaid);
  281. if (serial_read(fd, buf, 4) != 4)
  282. bailout2(LOG_ERR, "%s %u.%u: Error reading (read USER code)", modminer->api->name, modminer->device_id, fpgaid);
  283. if (memcmp(buf, BISTREAM_USER_ID, 4)) {
  284. applog(LOG_ERR, "%s %u.%u: FPGA not programmed", modminer->api->name, modminer->device_id, fpgaid);
  285. if (!modminer_fpga_upload_bitstream(modminer))
  286. return false;
  287. }
  288. else
  289. applog(LOG_DEBUG, "%s %u.%u: FPGA is already programmed :)", modminer->api->name, modminer->device_id, fpgaid);
  290. state->clock = 212; // Will be reduced to 210 by modminer_reduce_clock
  291. modminer_reduce_clock(thr, false);
  292. mutex_unlock(&modminer->device_mutex);
  293. return true;
  294. }
  295. static bool
  296. modminer_prepare_next_work(struct modminer_fpga_state*state, struct work*work)
  297. {
  298. char *midstate = state->next_work_cmd + 2;
  299. char *taildata = midstate + 32;
  300. if (!(memcmp(midstate, work->midstate, 32) || memcmp(taildata, work->data + 64, 12)))
  301. return false;
  302. memcpy(midstate, work->midstate, 32);
  303. memcpy(taildata, work->data + 64, 12);
  304. return true;
  305. }
  306. static bool
  307. modminer_start_work(struct thr_info*thr)
  308. {
  309. fd_set fds;
  310. struct cgpu_info*modminer = thr->cgpu;
  311. struct modminer_fpga_state *state = thr->cgpu_data;
  312. char fpgaid = thr->device_thread;
  313. int fd = modminer->device_fd;
  314. char buf[1];
  315. mutex_lock(&modminer->device_mutex);
  316. if (46 != write(fd, state->next_work_cmd, 46))
  317. bailout2(LOG_ERR, "%s %u.%u: Error writing (start work)", modminer->api->name, modminer->device_id, fpgaid);
  318. gettimeofday(&state->tv_workstart, NULL);
  319. state->hashes = 0;
  320. status_read("start work");
  321. mutex_unlock(&modminer->device_mutex);
  322. return true;
  323. }
  324. #define work_restart(thr) work_restart[thr->id].restart
  325. static uint64_t
  326. modminer_process_results(struct thr_info*thr)
  327. {
  328. struct cgpu_info*modminer = thr->cgpu;
  329. struct modminer_fpga_state *state = thr->cgpu_data;
  330. char fpgaid = thr->device_thread;
  331. int fd = modminer->device_fd;
  332. struct work *work = &state->running_work;
  333. char cmd[2], temperature;
  334. uint32_t nonce;
  335. long iter;
  336. bool bad;
  337. cmd[0] = '\x0a';
  338. cmd[1] = fpgaid;
  339. mutex_lock(&modminer->device_mutex);
  340. if (2 == write(fd, cmd, 2) && read(fd, &temperature, 1) == 1)
  341. {
  342. modminer->temp = (float)temperature;
  343. if (temperature > modminer->cutofftemp - 2) {
  344. if (temperature > modminer->cutofftemp) {
  345. applog(LOG_WARNING, "%s %u.%u: Hit thermal cutoff limit, disabling device!", modminer->api->name, modminer->device_id, fpgaid);
  346. modminer->deven = DEV_RECOVER;
  347. modminer->device_last_not_well = time(NULL);
  348. modminer->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
  349. ++modminer->dev_thermal_cutoff_count;
  350. } else {
  351. time_t now = time(NULL);
  352. if (state->last_cutoff_reduced != now) {
  353. state->last_cutoff_reduced = now;
  354. modminer_reduce_clock(thr, false);
  355. }
  356. }
  357. }
  358. }
  359. cmd[0] = '\x09';
  360. iter = 200;
  361. while (1) {
  362. if (write(fd, cmd, 2) != 2)
  363. bailout2(LOG_ERR, "%s %u.%u: Error reading (get nonce)", modminer->api->name, modminer->device_id, fpgaid);
  364. serial_read(fd, &nonce, 4);
  365. mutex_unlock(&modminer->device_mutex);
  366. if (memcmp(&nonce, "\xff\xff\xff\xff", 4)) {
  367. state->no_nonce_counter = 0;
  368. bad = !test_nonce(work, nonce);
  369. if (!bad)
  370. submit_nonce(thr, work, nonce);
  371. else {
  372. ++hw_errors;
  373. if (++modminer->hw_errors * 100 > 1000 + state->good_share_counter)
  374. // Only reduce clocks if hardware errors are more than ~1% of results
  375. modminer_reduce_clock(thr, true);
  376. }
  377. }
  378. else
  379. if (++state->no_nonce_counter > 18000) {
  380. state->no_nonce_counter = 0;
  381. modminer_reduce_clock(thr, true);
  382. }
  383. if (work_restart(thr))
  384. break;
  385. usleep(10000);
  386. if (work_restart(thr) || !--iter)
  387. break;
  388. mutex_lock(&modminer->device_mutex);
  389. }
  390. struct timeval tv_workend, elapsed;
  391. gettimeofday(&tv_workend, NULL);
  392. timeval_subtract(&elapsed, &tv_workend, &state->tv_workstart);
  393. uint64_t hashes = (uint64_t)state->clock * (((uint64_t)elapsed.tv_sec * 1000000) + elapsed.tv_usec);
  394. if (hashes > 0xffffffff)
  395. hashes = 0xffffffff;
  396. else
  397. if (hashes <= state->hashes)
  398. hashes = 1;
  399. else
  400. hashes -= state->hashes;
  401. state->hashes += hashes;
  402. return hashes;
  403. }
  404. static uint64_t
  405. modminer_scanhash(struct thr_info*thr, struct work*work, uint64_t __maybe_unused max_nonce)
  406. {
  407. struct modminer_fpga_state *state = thr->cgpu_data;
  408. uint64_t hashes = 1;
  409. bool startwork;
  410. startwork = modminer_prepare_next_work(state, work);
  411. if (state->work_running) {
  412. hashes = modminer_process_results(thr);
  413. if (work_restart(thr)) {
  414. state->work_running = false;
  415. return 1;
  416. }
  417. }
  418. else
  419. state->work_running = true;
  420. if (startwork) {
  421. if (!modminer_start_work(thr))
  422. return 0;
  423. memcpy(&state->running_work, work, sizeof(state->running_work));
  424. }
  425. // This is intentionally early
  426. work->blk.nonce += hashes;
  427. return hashes;
  428. }
  429. static void
  430. modminer_fpga_shutdown(struct thr_info *thr)
  431. {
  432. free(thr->cgpu_data);
  433. }
  434. struct device_api modminer_api = {
  435. .dname = "modminer",
  436. .name = "PGA",
  437. .api_detect = modminer_detect,
  438. .get_statline_before = get_modminer_statline_before,
  439. .thread_prepare = modminer_fpga_prepare,
  440. .thread_init = modminer_fpga_init,
  441. .scanhash = modminer_scanhash,
  442. .thread_shutdown = modminer_fpga_shutdown,
  443. };