driver-modminer.c 30 KB

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