driver-modminer.c 30 KB

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