driver-modminer.c 15 KB

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