driver-modminer.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*
  2. * Copyright 2012 Andrew Smith
  3. * Copyright 2012 Luke Dashjr
  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 <stdarg.h>
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <math.h>
  15. #include "logging.h"
  16. #include "miner.h"
  17. #include "usbutils.h"
  18. #include "fpgautils.h"
  19. #include "util.h"
  20. #define BITSTREAM_FILENAME "fpgaminer_top_fixed7_197MHz.ncd"
  21. #define BISTREAM_USER_ID "\2\4$B"
  22. #define BITSTREAM_MAGIC_0 0
  23. #define BITSTREAM_MAGIC_1 9
  24. #define MODMINER_CUTOFF_TEMP 60.0
  25. #define MODMINER_OVERHEAT_TEMP 50.0
  26. #define MODMINER_TEMP_UP_LIMIT 48.0
  27. #define MODMINER_OVERHEAT_CLOCK -10
  28. #define MODMINER_HW_ERROR_PERCENT 0.75
  29. // N.B. in the latest firmware the limit is 250
  30. // however the voltage/temperature risks preclude that
  31. #define MODMINER_MAX_CLOCK 230
  32. #define MODMINER_DEF_CLOCK 200
  33. #define MODMINER_MIN_CLOCK 160
  34. #define MODMINER_CLOCK_DOWN -2
  35. #define MODMINER_CLOCK_SET 0
  36. #define MODMINER_CLOCK_UP 2
  37. // Commands
  38. #define MODMINER_PING "\x00"
  39. #define MODMINER_GET_VERSION "\x01"
  40. #define MODMINER_FPGA_COUNT "\x02"
  41. // Commands + require FPGAid
  42. #define MODMINER_GET_IDCODE '\x03'
  43. #define MODMINER_GET_USERCODE '\x04'
  44. #define MODMINER_PROGRAM '\x05'
  45. #define MODMINER_SET_CLOCK '\x06'
  46. #define MODMINER_READ_CLOCK '\x07'
  47. #define MODMINER_SEND_WORK '\x08'
  48. #define MODMINER_CHECK_WORK '\x09'
  49. // One byte temperature reply
  50. #define MODMINER_TEMP1 '\x0a'
  51. // Two byte temperature reply
  52. #define MODMINER_TEMP2 '\x0d'
  53. // +6 bytes
  54. #define MODMINER_SET_REG '\x0b'
  55. // +2 bytes
  56. #define MODMINER_GET_REG '\x0c'
  57. #define FPGAID_ALL 4
  58. // Maximum how many good shares in a row means clock up
  59. // 96 is ~34m22s at 200MH/s
  60. #define MODMINER_TRY_UP 96
  61. // Initially how many good shares in a row means clock up
  62. // This is doubled each down clock until it reaches MODMINER_TRY_UP
  63. // 6 is ~2m9s at 200MH/s
  64. #define MODMINER_EARLY_UP 6
  65. // Limit when reducing shares_to_good
  66. #define MODMINER_MIN_BACK 12
  67. struct device_api modminer_api;
  68. // 45 noops sent when detecting, in case the device was left in "start job" reading
  69. 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";
  70. static void do_ping(struct cgpu_info *modminer)
  71. {
  72. char buf[0x100+1];
  73. int err, amount;
  74. // Don't care if it fails
  75. err = usb_write(modminer, (char *)NOOP, sizeof(NOOP)-1, &amount, C_PING);
  76. applog(LOG_DEBUG, "%s%u: flush noop got %d err %d",
  77. modminer->api->name, modminer->fpgaid, amount, err);
  78. // Clear any outstanding data
  79. while ((err = usb_read(modminer, buf, sizeof(buf)-1, &amount, C_CLEAR)) == 0 && amount > 0)
  80. applog(LOG_DEBUG, "%s%u: clear got %d",
  81. modminer->api->name, modminer->fpgaid, amount);
  82. applog(LOG_DEBUG, "%s%u: final clear got %d err %d",
  83. modminer->api->name, modminer->fpgaid, amount, err);
  84. }
  85. static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  86. {
  87. char buf[0x100+1];
  88. char *devname = NULL;
  89. char devpath[20];
  90. int err, i, amount;
  91. bool added = false;
  92. struct cgpu_info *modminer = NULL;
  93. modminer = calloc(1, sizeof(*modminer));
  94. modminer->api = &modminer_api;
  95. modminer->modminer_mutex = calloc(1, sizeof(*(modminer->modminer_mutex)));
  96. mutex_init(modminer->modminer_mutex);
  97. modminer->fpgaid = (char)0;
  98. if (!usb_init(modminer, dev, found))
  99. goto shin;
  100. do_ping(modminer);
  101. if ((err = usb_write(modminer, MODMINER_GET_VERSION, 1, &amount, C_REQUESTVERSION)) < 0 || amount != 1) {
  102. applog(LOG_ERR, "ModMiner detect: send version request failed (%d:%d)", amount, err);
  103. goto unshin;
  104. }
  105. if ((err = usb_read(modminer, buf, sizeof(buf)-1, &amount, C_GETVERSION)) < 0 || amount < 1) {
  106. if (err < 0)
  107. applog(LOG_ERR, "ModMiner detect: no version reply (%d)", err);
  108. else
  109. applog(LOG_ERR, "ModMiner detect: empty version reply (%d)", amount);
  110. applog(LOG_DEBUG, "ModMiner detect: check the firmware");
  111. goto unshin;
  112. }
  113. buf[amount] = '\0';
  114. devname = strdup(buf);
  115. applog(LOG_DEBUG, "ModMiner identified as: %s", devname);
  116. if ((err = usb_write(modminer, MODMINER_FPGA_COUNT, 1, &amount, C_REQUESTFPGACOUNT) < 0 || amount != 1)) {
  117. applog(LOG_ERR, "ModMiner detect: FPGA count request failed (%d:%d)", amount, err);
  118. goto unshin;
  119. }
  120. if ((err = usb_read(modminer, buf, 1, &amount, C_GETFPGACOUNT)) < 0 || amount != 1) {
  121. applog(LOG_ERR, "ModMiner detect: no FPGA count reply (%d:%d)", amount, err);
  122. goto unshin;
  123. }
  124. // TODO: flag it use 1 byte temp if it is an old firmware
  125. // can detect with modminer->cgusb->serial ?
  126. if (buf[0] == 0) {
  127. applog(LOG_ERR, "ModMiner detect: zero FPGA count from %s", devname);
  128. goto unshin;
  129. }
  130. if (buf[0] < 1 || buf[0] > 4) {
  131. applog(LOG_ERR, "ModMiner detect: invalid FPGA count (%u) from %s", buf[0], devname);
  132. goto unshin;
  133. }
  134. applog(LOG_DEBUG, "ModMiner %s has %u FPGAs", devname, buf[0]);
  135. modminer->name = devname;
  136. // TODO: test with 1 board missing in the middle and each end
  137. // to see how that affects the sequence numbers
  138. for (i = 0; i < buf[0]; i++) {
  139. struct cgpu_info *tmp = calloc(1, sizeof(*tmp));
  140. tmp->api = modminer->api;
  141. tmp->name = devname;
  142. sprintf(devpath, "%d:%d:%d",
  143. (int)(modminer->usbdev->bus_number),
  144. (int)(modminer->usbdev->device_address),
  145. i);
  146. tmp->device_path = strdup(devpath);
  147. tmp->usbdev = modminer->usbdev;
  148. // Only the first copy gets the already used stats
  149. if (!added)
  150. tmp->usbstat = modminer->usbstat;
  151. tmp->fpgaid = (char)i;
  152. tmp->modminer_mutex = modminer->modminer_mutex;
  153. tmp->deven = DEV_ENABLED;
  154. tmp->threads = 1;
  155. if (!add_cgpu(tmp)) {
  156. free(tmp->device_path);
  157. free(tmp);
  158. goto unshin;
  159. }
  160. update_usb_stats(tmp);
  161. added = true;
  162. }
  163. free(modminer);
  164. return true;
  165. unshin:
  166. if (!added)
  167. usb_uninit(modminer);
  168. shin:
  169. if (!added)
  170. free(modminer->modminer_mutex);
  171. free(modminer);
  172. if (added)
  173. return true;
  174. else
  175. return false;
  176. }
  177. static void modminer_detect()
  178. {
  179. usb_detect(&modminer_api, modminer_detect_one);
  180. }
  181. static bool get_expect(struct cgpu_info *modminer, FILE *f, char c)
  182. {
  183. char buf;
  184. if (fread(&buf, 1, 1, f) != 1) {
  185. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream (%c)",
  186. modminer->api->name, modminer->device_id, errno, c);
  187. return false;
  188. }
  189. if (buf != c) {
  190. applog(LOG_ERR, "%s%u: firmware code mismatch (%c)",
  191. modminer->api->name, modminer->device_id, c);
  192. return false;
  193. }
  194. return true;
  195. }
  196. static bool get_info(struct cgpu_info *modminer, FILE *f, char *buf, int bufsiz, const char *name)
  197. {
  198. unsigned char siz[2];
  199. int len;
  200. if (fread(siz, 2, 1, f) != 1) {
  201. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream '%s' len",
  202. modminer->api->name, modminer->device_id, errno, name);
  203. return false;
  204. }
  205. len = siz[0] * 256 + siz[1];
  206. if (len >= bufsiz) {
  207. applog(LOG_ERR, "%s%u: Bitstream '%s' len too large (%d)",
  208. modminer->api->name, modminer->device_id, name, len);
  209. return false;
  210. }
  211. if (fread(buf, len, 1, f) != 1) {
  212. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream '%s'", errno,
  213. modminer->api->name, modminer->device_id, errno, name);
  214. return false;
  215. }
  216. buf[len] = '\0';
  217. return true;
  218. }
  219. #define USE_DEFAULT_TIMEOUT 0
  220. // mutex must always be locked before calling
  221. static bool get_status_timeout(struct cgpu_info *modminer, char *msg, unsigned int timeout, enum usb_cmds cmd)
  222. {
  223. int err, amount;
  224. char buf[1];
  225. if (timeout == USE_DEFAULT_TIMEOUT)
  226. err = usb_read(modminer, buf, 1, &amount, cmd);
  227. else
  228. err = usb_read_timeout(modminer, buf, 1, &amount, timeout, cmd);
  229. if (err < 0 || amount != 1) {
  230. mutex_unlock(modminer->modminer_mutex);
  231. applog(LOG_ERR, "%s%u: Error (%d:%d) getting %s reply",
  232. modminer->api->name, modminer->device_id, amount, err, msg);
  233. return false;
  234. }
  235. if (buf[0] != 1) {
  236. mutex_unlock(modminer->modminer_mutex);
  237. applog(LOG_ERR, "%s%u: Error, invalid %s reply (was %d should be 1)",
  238. modminer->api->name, modminer->device_id, msg, buf[0]);
  239. return false;
  240. }
  241. return true;
  242. }
  243. // mutex must always be locked before calling
  244. static bool get_status(struct cgpu_info *modminer, char *msg, enum usb_cmds cmd)
  245. {
  246. return get_status_timeout(modminer, msg, USE_DEFAULT_TIMEOUT, cmd);
  247. }
  248. static bool modminer_fpga_upload_bitstream(struct cgpu_info *modminer)
  249. {
  250. const char *bsfile = BITSTREAM_FILENAME;
  251. char buf[0x100], *p;
  252. char devmsg[64];
  253. unsigned char *ubuf = (unsigned char *)buf;
  254. unsigned long totlen, len;
  255. size_t buflen, remaining;
  256. float nextmsg, upto;
  257. char fpgaid = FPGAID_ALL;
  258. int err, amount, tries;
  259. char *ptr;
  260. FILE *f = open_bitstream("modminer", bsfile);
  261. if (!f) {
  262. mutex_unlock(modminer->modminer_mutex);
  263. applog(LOG_DEBUG, "%s%u: Error (%d) opening bitstream file %s",
  264. modminer->api->name, modminer->device_id, errno, bsfile);
  265. return false;
  266. }
  267. if (fread(buf, 2, 1, f) != 1) {
  268. mutex_unlock(modminer->modminer_mutex);
  269. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream magic",
  270. modminer->api->name, modminer->device_id, errno);
  271. goto dame;
  272. }
  273. if (buf[0] != BITSTREAM_MAGIC_0 || buf[1] != BITSTREAM_MAGIC_1) {
  274. mutex_unlock(modminer->modminer_mutex);
  275. applog(LOG_ERR, "%s%u: bitstream has incorrect magic (%u,%u) instead of (%u,%u)",
  276. modminer->api->name, modminer->device_id,
  277. buf[0], buf[1],
  278. BITSTREAM_MAGIC_0, BITSTREAM_MAGIC_1);
  279. goto dame;
  280. }
  281. if (fseek(f, 11L, SEEK_CUR)) {
  282. mutex_unlock(modminer->modminer_mutex);
  283. applog(LOG_ERR, "%s%u: Error (%d) bitstream seek failed",
  284. modminer->api->name, modminer->device_id, errno);
  285. goto dame;
  286. }
  287. if (!get_expect(modminer, f, 'a'))
  288. goto undame;
  289. if (!get_info(modminer, f, buf, sizeof(buf), "Design name"))
  290. goto undame;
  291. applog(LOG_DEBUG, "%s%u: bitstream file '%s' info:",
  292. modminer->api->name, modminer->device_id, bsfile);
  293. applog(LOG_DEBUG, " Design name: '%s'", buf);
  294. p = strrchr(buf, ';') ? : buf;
  295. p = strrchr(buf, '=') ? : p;
  296. if (p[0] == '=')
  297. p++;
  298. unsigned long fwusercode = (unsigned long)strtoll(p, &p, 16);
  299. if (p[0] != '\0') {
  300. mutex_unlock(modminer->modminer_mutex);
  301. applog(LOG_ERR, "%s%u: Bad usercode in bitstream file",
  302. modminer->api->name, modminer->device_id);
  303. goto dame;
  304. }
  305. if (fwusercode == 0xffffffff) {
  306. mutex_unlock(modminer->modminer_mutex);
  307. applog(LOG_ERR, "%s%u: bitstream doesn't support user code",
  308. modminer->api->name, modminer->device_id);
  309. goto dame;
  310. }
  311. applog(LOG_DEBUG, " Version: %u, build %u", (fwusercode >> 8) & 0xff, fwusercode & 0xff);
  312. if (!get_expect(modminer, f, 'b'))
  313. goto undame;
  314. if (!get_info(modminer, f, buf, sizeof(buf), "Part number"))
  315. goto undame;
  316. applog(LOG_DEBUG, " Part number: '%s'", buf);
  317. if (!get_expect(modminer, f, 'c'))
  318. goto undame;
  319. if (!get_info(modminer, f, buf, sizeof(buf), "Build date"))
  320. goto undame;
  321. applog(LOG_DEBUG, " Build date: '%s'", buf);
  322. if (!get_expect(modminer, f, 'd'))
  323. goto undame;
  324. if (!get_info(modminer, f, buf, sizeof(buf), "Build time"))
  325. goto undame;
  326. applog(LOG_DEBUG, " Build time: '%s'", buf);
  327. if (!get_expect(modminer, f, 'e'))
  328. goto undame;
  329. if (fread(buf, 4, 1, f) != 1) {
  330. mutex_unlock(modminer->modminer_mutex);
  331. applog(LOG_ERR, "%s%u: Error (%d) reading bitstream data len",
  332. modminer->api->name, modminer->device_id, errno);
  333. goto dame;
  334. }
  335. len = ((unsigned long)ubuf[0] << 24) | ((unsigned long)ubuf[1] << 16) | (ubuf[2] << 8) | ubuf[3];
  336. applog(LOG_DEBUG, " Bitstream size: %lu", len);
  337. strcpy(devmsg, modminer->device_path);
  338. ptr = strrchr(devmsg, ':');
  339. if (ptr)
  340. *ptr = '\0';
  341. applog(LOG_WARNING, "%s%u: Programming all FPGA on %s ... Mining will not start until complete",
  342. modminer->api->name, modminer->device_id, devmsg);
  343. buf[0] = MODMINER_PROGRAM;
  344. buf[1] = fpgaid;
  345. buf[2] = (len >> 0) & 0xff;
  346. buf[3] = (len >> 8) & 0xff;
  347. buf[4] = (len >> 16) & 0xff;
  348. buf[5] = (len >> 24) & 0xff;
  349. if ((err = usb_write(modminer, buf, 6, &amount, C_STARTPROGRAM)) < 0 || amount != 6) {
  350. mutex_unlock(modminer->modminer_mutex);
  351. applog(LOG_ERR, "%s%u: Program init failed (%d:%d)",
  352. modminer->api->name, modminer->device_id, amount, err);
  353. goto dame;
  354. }
  355. if (!get_status(modminer, "initialise", C_STARTPROGRAMSTATUS))
  356. goto undame;
  357. // It must be 32 bytes according to MCU legacy.c
  358. #define WRITE_SIZE 32
  359. totlen = len;
  360. nextmsg = 0.1;
  361. while (len > 0) {
  362. buflen = len < WRITE_SIZE ? len : WRITE_SIZE;
  363. if (fread(buf, buflen, 1, f) != 1) {
  364. mutex_unlock(modminer->modminer_mutex);
  365. applog(LOG_ERR, "%s%u: bitstream file read error %d (%d bytes left)",
  366. modminer->api->name, modminer->device_id, errno, len);
  367. goto dame;
  368. }
  369. tries = 0;
  370. ptr = buf;
  371. remaining = buflen;
  372. while ((err = usb_write(modminer, ptr, remaining, &amount, C_PROGRAM)) < 0 || amount != (int)remaining) {
  373. if (err == LIBUSB_ERROR_TIMEOUT && amount > 0 && ++tries < 4) {
  374. remaining -= amount;
  375. ptr += amount;
  376. if (opt_debug)
  377. applog(LOG_DEBUG, "%s%u: Program timeout (%d:%d) sent %d tries %d",
  378. modminer->api->name, modminer->device_id,
  379. amount, err, remaining, tries);
  380. if (!get_status(modminer, "write status", C_PROGRAMSTATUS2))
  381. goto dame;
  382. } else {
  383. mutex_unlock(modminer->modminer_mutex);
  384. applog(LOG_ERR, "%s%u: Program failed (%d:%d) sent %d",
  385. modminer->api->name, modminer->device_id, amount, err, remaining);
  386. goto dame;
  387. }
  388. }
  389. if (!get_status(modminer, "write status", C_PROGRAMSTATUS))
  390. goto dame;
  391. len -= buflen;
  392. upto = (float)(totlen - len) / (float)(totlen);
  393. if (upto >= nextmsg) {
  394. applog(LOG_WARNING,
  395. "%s%u: Programming %.1f%% (%d out of %d)",
  396. modminer->api->name, modminer->device_id, upto*100, (totlen - len), totlen);
  397. nextmsg += 0.1;
  398. }
  399. }
  400. if (!get_status(modminer, "final status", C_FINALPROGRAMSTATUS))
  401. goto undame;
  402. applog(LOG_WARNING, "%s%u: Programming completed for all FPGA on %s",
  403. modminer->api->name, modminer->device_id, devmsg);
  404. // Give it a 2/3s delay after programming
  405. nmsleep(666);
  406. return true;
  407. undame:
  408. ;
  409. mutex_unlock(modminer->modminer_mutex);
  410. ;
  411. dame:
  412. fclose(f);
  413. return false;
  414. }
  415. static bool modminer_fpga_prepare(struct thr_info *thr)
  416. {
  417. struct cgpu_info *modminer = thr->cgpu;
  418. struct timeval now;
  419. gettimeofday(&now, NULL);
  420. get_datestamp(modminer->init, &now);
  421. struct modminer_fpga_state *state;
  422. state = thr->cgpu_data = calloc(1, sizeof(struct modminer_fpga_state));
  423. state->next_work_cmd[0] = MODMINER_SEND_WORK;
  424. state->next_work_cmd[1] = modminer->fpgaid;
  425. state->shares_to_good = MODMINER_EARLY_UP;
  426. state->overheated = false;
  427. return true;
  428. }
  429. /*
  430. * Clocking rules:
  431. * If device exceeds cutoff temp - TODO: ?stop sending work -
  432. * and decrease the clock by MODMINER_OVERHEAT_CLOCK
  433. * for when it restarts
  434. *
  435. * When to clock down:
  436. * If device overheats
  437. * also halve shares_to_good
  438. * (so multiple temp drops can recover faster)
  439. * or
  440. * If device gets MODMINER_HW_ERROR_PERCENT errors since last clock up or down
  441. * if clock is <= default it requires 2 HW to do this test
  442. * if clock is > default it only requires 1 HW to do this test
  443. * also double shares_to_good
  444. *
  445. * When to clock up:
  446. * If device gets shares_to_good good shares in a row
  447. * and temp <= MODMINER_TEMP_UP_LIMIT
  448. *
  449. * N.B. clock must always be a multiple of 2
  450. */
  451. static bool modminer_delta_clock(struct thr_info *thr, int delta, bool temp)
  452. {
  453. struct cgpu_info *modminer = thr->cgpu;
  454. struct modminer_fpga_state *state = thr->cgpu_data;
  455. unsigned char cmd[6], buf[1];
  456. int err, amount;
  457. // Only do once if multiple shares per work or multiple reasons
  458. // Since the temperature down clock test is first in the code this is OK
  459. if (!state->new_work)
  460. return false;
  461. state->new_work = false;
  462. state->shares = 0;
  463. state->shares_last_hw = 0;
  464. state->hw_errors = 0;
  465. // If drop requested due to temperature, clock drop is always allowed
  466. if (!temp && delta < 0 && modminer->clock <= MODMINER_MIN_CLOCK)
  467. return false;
  468. if (delta > 0 && modminer->clock >= MODMINER_MAX_CLOCK)
  469. return false;
  470. if (delta < 0) {
  471. if (temp) {
  472. if (state->shares_to_good > MODMINER_MIN_BACK)
  473. state->shares_to_good /= 2;
  474. } else {
  475. if ((state->shares_to_good * 2) < MODMINER_TRY_UP)
  476. state->shares_to_good *= 2;
  477. else
  478. state->shares_to_good = MODMINER_TRY_UP;
  479. }
  480. }
  481. modminer->clock += delta;
  482. cmd[0] = MODMINER_SET_CLOCK;
  483. cmd[1] = modminer->fpgaid;
  484. cmd[2] = modminer->clock;
  485. cmd[3] = cmd[4] = cmd[5] = '\0';
  486. mutex_lock(modminer->modminer_mutex);
  487. if ((err = usb_write(modminer, (char *)cmd, 6, &amount, C_SETCLOCK)) < 0 || amount != 6) {
  488. mutex_unlock(modminer->modminer_mutex);
  489. applog(LOG_ERR, "%s%u: Error writing set clock speed (%d:%d)",
  490. modminer->api->name, modminer->device_id, amount, err);
  491. return false;
  492. }
  493. if ((err = usb_read(modminer, (char *)(&buf), 1, &amount, C_REPLYSETCLOCK)) < 0 || amount != 1) {
  494. mutex_unlock(modminer->modminer_mutex);
  495. applog(LOG_ERR, "%s%u: Error reading set clock speed (%d:%d)",
  496. modminer->api->name, modminer->device_id, amount, err);
  497. return false;
  498. }
  499. mutex_unlock(modminer->modminer_mutex);
  500. applog(LOG_WARNING, "%s%u: Set clock speed %sto %u",
  501. modminer->api->name, modminer->device_id,
  502. (delta < 0) ? "down " : (delta > 0 ? "up " : ""),
  503. modminer->clock);
  504. return true;
  505. }
  506. static bool modminer_fpga_init(struct thr_info *thr)
  507. {
  508. struct cgpu_info *modminer = thr->cgpu;
  509. unsigned char cmd[2], buf[4];
  510. int err, amount;
  511. mutex_lock(modminer->modminer_mutex);
  512. cmd[0] = MODMINER_GET_USERCODE;
  513. cmd[1] = modminer->fpgaid;
  514. if ((err = usb_write(modminer, (char *)cmd, 2, &amount, C_REQUESTUSERCODE)) < 0 || amount != 2) {
  515. mutex_unlock(modminer->modminer_mutex);
  516. applog(LOG_ERR, "%s%u: Error requesting USER code (%d:%d)",
  517. modminer->api->name, modminer->device_id, amount, err);
  518. return false;
  519. }
  520. if ((err = usb_read(modminer, (char *)buf, 4, &amount, C_GETUSERCODE)) < 0 || amount != 4) {
  521. mutex_unlock(modminer->modminer_mutex);
  522. applog(LOG_ERR, "%s%u: Error reading USER code (%d:%d)",
  523. modminer->api->name, modminer->device_id, amount, err);
  524. return false;
  525. }
  526. if (memcmp(buf, BISTREAM_USER_ID, 4)) {
  527. applog(LOG_ERR, "%s%u: FPGA not programmed",
  528. modminer->api->name, modminer->device_id);
  529. if (!modminer_fpga_upload_bitstream(modminer))
  530. return false;
  531. mutex_unlock(modminer->modminer_mutex);
  532. } else {
  533. mutex_unlock(modminer->modminer_mutex);
  534. applog(LOG_DEBUG, "%s%u: FPGA is already programmed :)",
  535. modminer->api->name, modminer->device_id);
  536. }
  537. modminer->clock = MODMINER_DEF_CLOCK;
  538. modminer_delta_clock(thr, MODMINER_CLOCK_SET, false);
  539. thr->primary_thread = true;
  540. return true;
  541. }
  542. static void get_modminer_statline_before(char *buf, struct cgpu_info *modminer)
  543. {
  544. char info[64];
  545. sprintf(info, " %s%.1fC %3uMHz | ",
  546. (modminer->temp < 10) ? " " : "",
  547. modminer->temp,
  548. (unsigned int)(modminer->clock));
  549. strcat(buf, info);
  550. }
  551. static bool modminer_prepare_next_work(struct modminer_fpga_state *state, struct work *work)
  552. {
  553. char *midstate = state->next_work_cmd + 2;
  554. char *taildata = midstate + 32;
  555. if (!(memcmp(midstate, work->midstate, 32) || memcmp(taildata, work->data + 64, 12)))
  556. return false;
  557. memcpy(midstate, work->midstate, 32);
  558. memcpy(taildata, work->data + 64, 12);
  559. return true;
  560. }
  561. static bool modminer_start_work(struct thr_info *thr)
  562. {
  563. struct cgpu_info *modminer = thr->cgpu;
  564. struct modminer_fpga_state *state = thr->cgpu_data;
  565. int err, amount;
  566. bool sta;
  567. mutex_lock(modminer->modminer_mutex);
  568. if ((err = usb_write(modminer, (char *)(state->next_work_cmd), 46, &amount, C_SENDWORK)) < 0 || amount != 46) {
  569. // TODO: err = -4 means the MMQ disappeared - need to delete it and rescan for it? (after a delay?)
  570. // but check all (4) disappeared
  571. mutex_unlock(modminer->modminer_mutex);
  572. applog(LOG_ERR, "%s%u: Start work failed (%d:%d)",
  573. modminer->api->name, modminer->device_id, amount, err);
  574. return false;
  575. }
  576. gettimeofday(&state->tv_workstart, NULL);
  577. state->hashes = 0;
  578. sta = get_status(modminer, "start work", C_SENDWORKSTATUS);
  579. if (sta) {
  580. mutex_unlock(modminer->modminer_mutex);
  581. state->new_work = true;
  582. }
  583. return sta;
  584. }
  585. static void check_temperature(struct thr_info *thr)
  586. {
  587. struct cgpu_info *modminer = thr->cgpu;
  588. struct modminer_fpga_state *state = thr->cgpu_data;
  589. char cmd[2], temperature[2];
  590. int tbytes, tamount;
  591. int amount;
  592. if (modminer->one_byte_temp) {
  593. cmd[0] = MODMINER_TEMP1;
  594. tbytes = 1;
  595. } else {
  596. cmd[0] = MODMINER_TEMP2;
  597. tbytes = 2;
  598. }
  599. cmd[1] = modminer->fpgaid;
  600. mutex_lock(modminer->modminer_mutex);
  601. if (usb_write(modminer, (char *)cmd, 2, &amount, C_REQUESTTEMPERATURE) == 0 && amount == 2
  602. && usb_read(modminer, (char *)(&temperature), tbytes, &tamount, C_GETTEMPERATURE) == 0 && tamount == tbytes)
  603. {
  604. mutex_unlock(modminer->modminer_mutex);
  605. if (modminer->one_byte_temp)
  606. modminer->temp = temperature[0];
  607. else {
  608. // Only accurate to 2 and a bit places
  609. modminer->temp = roundf((temperature[1] * 256.0 + temperature[0]) / 0.128) / 1000.0;
  610. modminer->tried_two_byte_temp = true;
  611. }
  612. if (state->overheated) {
  613. if (modminer->temp < MODMINER_OVERHEAT_TEMP) {
  614. state->overheated = false;
  615. applog(LOG_WARNING, "%s%u: Recovered, temp less than (%f) now %f",
  616. modminer->api->name, modminer->device_id,
  617. MODMINER_OVERHEAT_TEMP, modminer->temp);
  618. }
  619. }
  620. else if (modminer->temp >= MODMINER_OVERHEAT_TEMP) {
  621. if (modminer->temp >= MODMINER_CUTOFF_TEMP) {
  622. applog(LOG_WARNING, "%s%u: Hit thermal cutoff limit (%f) at %f, disabling!",
  623. modminer->api->name, modminer->device_id,
  624. MODMINER_CUTOFF_TEMP, modminer->temp);
  625. modminer_delta_clock(thr, MODMINER_OVERHEAT_CLOCK, true);
  626. state->overheated = true;
  627. dev_error(modminer, REASON_DEV_THERMAL_CUTOFF);
  628. } else {
  629. applog(LOG_WARNING, "%s%u: Overheat limit (%f) reached %f",
  630. modminer->api->name, modminer->device_id,
  631. MODMINER_OVERHEAT_TEMP, modminer->temp);
  632. modminer_delta_clock(thr, MODMINER_CLOCK_DOWN, true);
  633. dev_error(modminer, REASON_DEV_OVER_HEAT);
  634. }
  635. }
  636. } else {
  637. mutex_unlock(modminer->modminer_mutex);
  638. if (!modminer->tried_two_byte_temp) {
  639. modminer->tried_two_byte_temp = true;
  640. modminer->one_byte_temp = true;
  641. }
  642. }
  643. }
  644. #define work_restart(thr) thr->work_restart
  645. static uint64_t modminer_process_results(struct thr_info *thr)
  646. {
  647. struct cgpu_info *modminer = thr->cgpu;
  648. struct modminer_fpga_state *state = thr->cgpu_data;
  649. struct work *work = &state->running_work;
  650. char cmd[2];
  651. uint32_t nonce;
  652. long iter;
  653. uint32_t curr_hw_errors;
  654. int err, amount;
  655. int timeoutloop;
  656. check_temperature(thr);
  657. if (state->overheated == true) {
  658. if (state->work_running)
  659. state->work_running = false;
  660. // Give it 5 seconds rest and wait for the next work
  661. nmsleep(5000);
  662. return 0;
  663. }
  664. cmd[0] = MODMINER_CHECK_WORK;
  665. cmd[1] = modminer->fpgaid;
  666. iter = 200;
  667. timeoutloop = 0;
  668. while (1) {
  669. mutex_lock(modminer->modminer_mutex);
  670. if ((err = usb_write(modminer, cmd, 2, &amount, C_REQUESTWORKSTATUS)) < 0 || amount != 2) {
  671. // TODO: err = -4 means the MMQ disappeared - need to delete it and rescan for it? (after a delay?)
  672. // but check all (4) disappeared
  673. mutex_unlock(modminer->modminer_mutex);
  674. // timeoutloop never resets so the timeouts can't
  675. // accumulate much during a single item of work
  676. if (err == -7 && ++timeoutloop < 10)
  677. goto tryagain;
  678. applog(LOG_ERR, "%s%u: Error sending (get nonce) (%d:%d)",
  679. modminer->api->name, modminer->device_id, amount, err);
  680. return -1;
  681. }
  682. err = usb_read(modminer, (char *)(&nonce), 4, &amount, C_GETWORKSTATUS);
  683. mutex_unlock(modminer->modminer_mutex);
  684. if (err < 0 || amount != 4) {
  685. // timeoutloop never resets so the timeouts can't
  686. // accumulate much during a single item of work
  687. if (err == -7 && ++timeoutloop < 10)
  688. goto tryagain;
  689. applog(LOG_ERR, "%s%u: Error reading (get nonce) (%d:%d)",
  690. modminer->api->name, modminer->device_id, amount, err);
  691. }
  692. if (memcmp(&nonce, "\xff\xff\xff\xff", 4)) {
  693. state->shares++;
  694. state->no_nonce_counter = 0;
  695. curr_hw_errors = state->hw_errors;
  696. submit_nonce(thr, work, nonce);
  697. if (state->hw_errors > curr_hw_errors) {
  698. state->shares_last_hw = state->shares;
  699. if (modminer->clock > MODMINER_DEF_CLOCK || state->hw_errors > 1) {
  700. float pct = (state->hw_errors * 100.0 / (state->shares ? : 1.0));
  701. if (pct >= MODMINER_HW_ERROR_PERCENT)
  702. modminer_delta_clock(thr, MODMINER_CLOCK_DOWN, false);
  703. }
  704. } else {
  705. // If we've reached the required good shares in a row then clock up
  706. if (((state->shares - state->shares_last_hw) >= state->shares_to_good) &&
  707. modminer->temp <= MODMINER_TEMP_UP_LIMIT)
  708. modminer_delta_clock(thr, MODMINER_CLOCK_UP, false);
  709. }
  710. } else if (++state->no_nonce_counter > 18000) {
  711. // TODO: NFI what this is
  712. state->no_nonce_counter = 0;
  713. modminer_delta_clock(thr, MODMINER_CLOCK_DOWN, false);
  714. applog(LOG_ERR, "%s%u: 18000 clock down",
  715. modminer->api->name, modminer->device_id);
  716. }
  717. tryagain:
  718. if (work_restart(thr))
  719. break;
  720. nmsleep(10);
  721. if (work_restart(thr) || !--iter)
  722. break;
  723. }
  724. struct timeval tv_workend, elapsed;
  725. gettimeofday(&tv_workend, NULL);
  726. timersub(&tv_workend, &state->tv_workstart, &elapsed);
  727. uint64_t hashes = (uint64_t)modminer->clock * (((uint64_t)elapsed.tv_sec * 1000000) + elapsed.tv_usec);
  728. if (hashes > 0xffffffff)
  729. hashes = 0xffffffff;
  730. else
  731. if (hashes <= state->hashes)
  732. hashes = 1;
  733. else
  734. hashes -= state->hashes;
  735. state->hashes += hashes;
  736. return hashes;
  737. }
  738. static int64_t modminer_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  739. {
  740. struct modminer_fpga_state *state = thr->cgpu_data;
  741. int64_t hashes = 0;
  742. bool startwork;
  743. if (state->overheated == true) {
  744. if (state->work_running)
  745. state->work_running = false;
  746. check_temperature(thr);
  747. if (state->overheated == true) {
  748. // Give it 5 seconds rest and wait for the next work
  749. nmsleep(5000);
  750. return 0;
  751. }
  752. }
  753. startwork = modminer_prepare_next_work(state, work);
  754. if (state->work_running) {
  755. hashes = modminer_process_results(thr);
  756. if (hashes == -1)
  757. return hashes;
  758. if (work_restart(thr)) {
  759. state->work_running = false;
  760. return 0;
  761. }
  762. } else
  763. state->work_running = true;
  764. if (startwork) {
  765. if (!modminer_start_work(thr))
  766. return -1;
  767. __copy_work(&state->running_work, work);
  768. }
  769. // This is intentionally early
  770. work->blk.nonce += hashes;
  771. return hashes;
  772. }
  773. static void modminer_hw_error(struct thr_info *thr)
  774. {
  775. struct modminer_fpga_state *state = thr->cgpu_data;
  776. state->hw_errors++;
  777. }
  778. static void modminer_fpga_shutdown(struct thr_info *thr)
  779. {
  780. free(thr->cgpu_data);
  781. }
  782. struct device_api modminer_api = {
  783. .dname = "modminer",
  784. .name = "MMQ",
  785. .api_detect = modminer_detect,
  786. .get_statline_before = get_modminer_statline_before,
  787. .thread_prepare = modminer_fpga_prepare,
  788. .thread_init = modminer_fpga_init,
  789. .scanhash = modminer_scanhash,
  790. .hw_error = modminer_hw_error,
  791. .thread_shutdown = modminer_fpga_shutdown,
  792. };