driver-modminer.c 30 KB

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