driver-modminer.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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 timeval now;
  433. cgtime(&now);
  434. get_datestamp(modminer->init, &now);
  435. struct modminer_fpga_state *state;
  436. state = thr->cgpu_data = calloc(1, sizeof(struct modminer_fpga_state));
  437. state->shares_to_good = MODMINER_EARLY_UP;
  438. state->overheated = false;
  439. return true;
  440. }
  441. /*
  442. * Clocking rules:
  443. * If device exceeds cutoff or overheat temp - stop sending work until it cools
  444. * decrease the clock by MODMINER_CLOCK_CUTOFF/MODMINER_CLOCK_OVERHEAT
  445. * for when it restarts
  446. * with MODMINER_CLOCK_OVERHEAT=0 basically says that temp shouldn't
  447. * affect the clock unless we reach CUTOFF
  448. *
  449. * If device overheats
  450. * set shares_to_good back to MODMINER_MIN_BACK
  451. * to speed up clock recovery if temp drop doesnt help
  452. *
  453. * When to clock down:
  454. * If device gets MODMINER_HW_ERROR_PERCENT errors since last clock up or down
  455. * if clock is <= default it requires 2 HW to do this test
  456. * if clock is > default it only requires 1 HW to do this test
  457. * also double shares_to_good
  458. *
  459. * When to clock up:
  460. * If device gets shares_to_good good shares in a row
  461. * and temp < MODMINER_TEMP_UP_LIMIT
  462. *
  463. * N.B. clock must always be a multiple of 2
  464. */
  465. static const char *clocknodev = "clock failed - no device";
  466. static const char *clockoldwork = "clock already changed for this work";
  467. static const char *clocktoolow = "clock too low";
  468. static const char *clocktoohi = "clock too high";
  469. static const char *clocksetfail = "clock set command failed";
  470. static const char *clockreplyfail = "clock reply failed";
  471. static const char *modminer_delta_clock(struct thr_info *thr, int delta, bool temp, bool force)
  472. {
  473. struct cgpu_info *modminer = thr->cgpu;
  474. struct modminer_fpga_state *state = thr->cgpu_data;
  475. unsigned char cmd[6], buf[1];
  476. int err, amount;
  477. // Device is gone
  478. if (modminer->usbinfo.nodev)
  479. return clocknodev;
  480. // Only do once if multiple shares per work or multiple reasons
  481. if (!state->new_work && !force)
  482. return clockoldwork;
  483. state->new_work = false;
  484. state->shares = 0;
  485. state->shares_last_hw = 0;
  486. state->hw_errors = 0;
  487. // FYI clock drop has little effect on temp
  488. if (delta < 0 && (modminer->clock + delta) < MODMINER_MIN_CLOCK)
  489. return clocktoolow;
  490. if (delta > 0 && (modminer->clock + delta) > MODMINER_MAX_CLOCK)
  491. return clocktoohi;
  492. if (delta < 0) {
  493. if (temp)
  494. state->shares_to_good = MODMINER_MIN_BACK;
  495. else {
  496. if ((state->shares_to_good * 2) < MODMINER_TRY_UP)
  497. state->shares_to_good *= 2;
  498. else
  499. state->shares_to_good = MODMINER_TRY_UP;
  500. }
  501. }
  502. modminer->clock += delta;
  503. cmd[0] = MODMINER_SET_CLOCK;
  504. cmd[1] = modminer->fpgaid;
  505. cmd[2] = modminer->clock;
  506. cmd[3] = cmd[4] = cmd[5] = '\0';
  507. mutex_lock(modminer->modminer_mutex);
  508. if ((err = usb_write(modminer, (char *)cmd, 6, &amount, C_SETCLOCK)) < 0 || amount != 6) {
  509. mutex_unlock(modminer->modminer_mutex);
  510. applog(LOG_ERR, "%s%u: Error writing set clock speed (%d:%d)",
  511. modminer->drv->name, modminer->device_id, amount, err);
  512. return clocksetfail;
  513. }
  514. if ((err = usb_read(modminer, (char *)(&buf), 1, &amount, C_REPLYSETCLOCK)) < 0 || amount != 1) {
  515. mutex_unlock(modminer->modminer_mutex);
  516. applog(LOG_ERR, "%s%u: Error reading set clock speed (%d:%d)",
  517. modminer->drv->name, modminer->device_id, amount, err);
  518. return clockreplyfail;
  519. }
  520. mutex_unlock(modminer->modminer_mutex);
  521. applog(LOG_WARNING, "%s%u: Set clock speed %sto %u",
  522. modminer->drv->name, modminer->device_id,
  523. (delta < 0) ? "down " : (delta > 0 ? "up " : ""),
  524. modminer->clock);
  525. return NULL;
  526. }
  527. static bool modminer_fpga_init(struct thr_info *thr)
  528. {
  529. struct cgpu_info *modminer = thr->cgpu;
  530. unsigned char cmd[2], buf[4];
  531. int err, amount;
  532. mutex_lock(modminer->modminer_mutex);
  533. cmd[0] = MODMINER_GET_USERCODE;
  534. cmd[1] = modminer->fpgaid;
  535. if ((err = usb_write(modminer, (char *)cmd, 2, &amount, C_REQUESTUSERCODE)) < 0 || amount != 2) {
  536. mutex_unlock(modminer->modminer_mutex);
  537. applog(LOG_ERR, "%s%u: Error requesting USER code (%d:%d)",
  538. modminer->drv->name, modminer->device_id, amount, err);
  539. return false;
  540. }
  541. if ((err = usb_read(modminer, (char *)buf, 4, &amount, C_GETUSERCODE)) < 0 || amount != 4) {
  542. mutex_unlock(modminer->modminer_mutex);
  543. applog(LOG_ERR, "%s%u: Error reading USER code (%d:%d)",
  544. modminer->drv->name, modminer->device_id, amount, err);
  545. return false;
  546. }
  547. if (memcmp(buf, BISTREAM_USER_ID, 4)) {
  548. applog(LOG_ERR, "%s%u: FPGA not programmed",
  549. modminer->drv->name, modminer->device_id);
  550. if (!modminer_fpga_upload_bitstream(modminer))
  551. return false;
  552. mutex_unlock(modminer->modminer_mutex);
  553. } else {
  554. mutex_unlock(modminer->modminer_mutex);
  555. applog(LOG_DEBUG, "%s%u: FPGA is already programmed :)",
  556. modminer->drv->name, modminer->device_id);
  557. }
  558. modminer->clock = MODMINER_DEF_CLOCK;
  559. modminer_delta_clock(thr, MODMINER_CLOCK_SET, false, false);
  560. thr->primary_thread = true;
  561. return true;
  562. }
  563. static void get_modminer_statline_before(char *buf, struct cgpu_info *modminer)
  564. {
  565. char info[64];
  566. sprintf(info, " %s%.1fC %3uMHz | ",
  567. (modminer->temp < 10) ? " " : "",
  568. modminer->temp,
  569. (unsigned int)(modminer->clock));
  570. strcat(buf, info);
  571. }
  572. static bool modminer_start_work(struct thr_info *thr, struct work *work)
  573. {
  574. struct cgpu_info *modminer = thr->cgpu;
  575. struct modminer_fpga_state *state = thr->cgpu_data;
  576. int err, amount;
  577. char cmd[48];
  578. bool sta;
  579. cmd[0] = MODMINER_SEND_WORK;
  580. cmd[1] = modminer->fpgaid;
  581. memcpy(&cmd[2], work->midstate, 32);
  582. memcpy(&cmd[34], work->data + 64, 12);
  583. if (state->first_work.tv_sec == 0)
  584. cgtime(&state->first_work);
  585. if (state->last_nonce.tv_sec == 0)
  586. cgtime(&state->last_nonce);
  587. mutex_lock(modminer->modminer_mutex);
  588. if ((err = usb_write(modminer, cmd, 46, &amount, C_SENDWORK)) < 0 || amount != 46) {
  589. mutex_unlock(modminer->modminer_mutex);
  590. applog(LOG_ERR, "%s%u: Start work failed (%d:%d)",
  591. modminer->drv->name, modminer->device_id, amount, err);
  592. return false;
  593. }
  594. cgtime(&state->tv_workstart);
  595. sta = get_status(modminer, "start work", C_SENDWORKSTATUS);
  596. if (sta) {
  597. mutex_unlock(modminer->modminer_mutex);
  598. state->new_work = true;
  599. }
  600. return sta;
  601. }
  602. static void check_temperature(struct thr_info *thr)
  603. {
  604. struct cgpu_info *modminer = thr->cgpu;
  605. struct modminer_fpga_state *state = thr->cgpu_data;
  606. char cmd[2], temperature[2];
  607. int tbytes, tamount;
  608. int amount;
  609. // Device is gone
  610. if (modminer->usbinfo.nodev)
  611. return;
  612. if (state->one_byte_temp) {
  613. cmd[0] = MODMINER_TEMP1;
  614. tbytes = 1;
  615. } else {
  616. cmd[0] = MODMINER_TEMP2;
  617. tbytes = 2;
  618. }
  619. cmd[1] = modminer->fpgaid;
  620. mutex_lock(modminer->modminer_mutex);
  621. if (usb_write(modminer, (char *)cmd, 2, &amount, C_REQUESTTEMPERATURE) == 0 && amount == 2 &&
  622. usb_read(modminer, (char *)(&temperature), tbytes, &tamount, C_GETTEMPERATURE) == 0 && tamount == tbytes) {
  623. mutex_unlock(modminer->modminer_mutex);
  624. if (state->one_byte_temp)
  625. modminer->temp = temperature[0];
  626. else {
  627. // Only accurate to 2 and a bit places
  628. modminer->temp = roundf((temperature[1] * 256.0 + temperature[0]) / 0.128) / 1000.0;
  629. state->tried_two_byte_temp = true;
  630. }
  631. if (state->overheated) {
  632. // Limit recovery to lower than OVERHEAT so it doesn't just go straight over again
  633. if (modminer->temp < MODMINER_RECOVER_TEMP) {
  634. state->overheated = false;
  635. applog(LOG_WARNING, "%s%u: Recovered, temp less than (%.1f) now %.3f",
  636. modminer->drv->name, modminer->device_id,
  637. MODMINER_RECOVER_TEMP, modminer->temp);
  638. }
  639. }
  640. else if (modminer->temp >= MODMINER_OVERHEAT_TEMP) {
  641. if (modminer->temp >= MODMINER_CUTOFF_TEMP) {
  642. applog(LOG_WARNING, "%s%u: Hit thermal cutoff limit! (%.1f) at %.3f",
  643. modminer->drv->name, modminer->device_id,
  644. MODMINER_CUTOFF_TEMP, modminer->temp);
  645. modminer_delta_clock(thr, MODMINER_CLOCK_CUTOFF, true, false);
  646. state->overheated = true;
  647. dev_error(modminer, REASON_DEV_THERMAL_CUTOFF);
  648. } else {
  649. applog(LOG_WARNING, "%s%u: Overheat limit (%.1f) reached %.3f",
  650. modminer->drv->name, modminer->device_id,
  651. MODMINER_OVERHEAT_TEMP, modminer->temp);
  652. // If it's defined to be 0 then don't call modminer_delta_clock()
  653. if (MODMINER_CLOCK_OVERHEAT != 0)
  654. modminer_delta_clock(thr, MODMINER_CLOCK_OVERHEAT, true, false);
  655. state->overheated = true;
  656. dev_error(modminer, REASON_DEV_OVER_HEAT);
  657. }
  658. }
  659. } else {
  660. mutex_unlock(modminer->modminer_mutex);
  661. if (!state->tried_two_byte_temp) {
  662. state->tried_two_byte_temp = true;
  663. state->one_byte_temp = true;
  664. }
  665. }
  666. }
  667. #define work_restart(thr) thr->work_restart
  668. // 250Mhz is 17.17s - ensure we don't go idle
  669. static const double processtime = 17.0;
  670. // 160Mhz is 26.84 - when overheated ensure we don't throw away shares
  671. static const double overheattime = 26.9;
  672. static uint64_t modminer_process_results(struct thr_info *thr, struct work *work)
  673. {
  674. struct cgpu_info *modminer = thr->cgpu;
  675. struct modminer_fpga_state *state = thr->cgpu_data;
  676. struct timeval now;
  677. char cmd[2];
  678. uint32_t nonce;
  679. uint32_t curr_hw_errors;
  680. int err, amount, amount2;
  681. int timeoutloop;
  682. double timeout;
  683. int temploop;
  684. // Device is gone
  685. if (modminer->usbinfo.nodev)
  686. return -1;
  687. // If we are overheated it will just keep checking for results
  688. // since we can't stop the work
  689. // The next work will not start until the temp drops
  690. check_temperature(thr);
  691. cmd[0] = MODMINER_CHECK_WORK;
  692. cmd[1] = modminer->fpgaid;
  693. timeoutloop = 0;
  694. temploop = 0;
  695. while (0x80085) {
  696. mutex_lock(modminer->modminer_mutex);
  697. if ((err = usb_write(modminer, cmd, 2, &amount, C_REQUESTWORKSTATUS)) < 0 || amount != 2) {
  698. mutex_unlock(modminer->modminer_mutex);
  699. // timeoutloop never resets so the timeouts can't
  700. // accumulate much during a single item of work
  701. if (err == LIBUSB_ERROR_TIMEOUT && ++timeoutloop < 5) {
  702. state->timeout_fail++;
  703. goto tryagain;
  704. }
  705. applog(LOG_ERR, "%s%u: Error sending (get nonce) (%d:%d)",
  706. modminer->drv->name, modminer->device_id, amount, err);
  707. return -1;
  708. }
  709. err = usb_read(modminer, (char *)(&nonce), 4, &amount, C_GETWORKSTATUS);
  710. while (err == LIBUSB_SUCCESS && amount < 4) {
  711. size_t remain = 4 - amount;
  712. char *pos = ((char *)(&nonce)) + amount;
  713. state->success_more++;
  714. err = usb_read(modminer, pos, remain, &amount2, C_GETWORKSTATUS);
  715. amount += amount2;
  716. }
  717. mutex_unlock(modminer->modminer_mutex);
  718. if (err < 0 || amount < 4) {
  719. // timeoutloop never resets so the timeouts can't
  720. // accumulate much during a single item of work
  721. if (err == LIBUSB_ERROR_TIMEOUT && ++timeoutloop < 10) {
  722. state->timeout_fail++;
  723. goto tryagain;
  724. }
  725. applog(LOG_ERR, "%s%u: Error reading (get nonce) (%d:%d)",
  726. modminer->drv->name, modminer->device_id, amount+amount2, err);
  727. }
  728. if (memcmp(&nonce, "\xff\xff\xff\xff", 4)) {
  729. // found 'something' ...
  730. state->shares++;
  731. curr_hw_errors = state->hw_errors;
  732. submit_nonce(thr, work, nonce);
  733. if (state->hw_errors > curr_hw_errors) {
  734. cgtime(&now);
  735. // Ignore initial errors that often happen
  736. if (tdiff(&now, &state->first_work) < 2.0) {
  737. state->shares = 0;
  738. state->shares_last_hw = 0;
  739. state->hw_errors = 0;
  740. } else {
  741. state->shares_last_hw = state->shares;
  742. if (modminer->clock > MODMINER_DEF_CLOCK || state->hw_errors > 1) {
  743. float pct = (state->hw_errors * 100.0 / (state->shares ? : 1.0));
  744. if (pct >= MODMINER_HW_ERROR_PERCENT)
  745. modminer_delta_clock(thr, MODMINER_CLOCK_DOWN, false, false);
  746. }
  747. }
  748. } else {
  749. cgtime(&state->last_nonce);
  750. state->death_stage_one = false;
  751. // If we've reached the required good shares in a row then clock up
  752. if (((state->shares - state->shares_last_hw) >= state->shares_to_good) &&
  753. modminer->temp < MODMINER_TEMP_UP_LIMIT)
  754. modminer_delta_clock(thr, MODMINER_CLOCK_UP, false, false);
  755. }
  756. } else {
  757. // on rare occasions - the MMQ can just stop returning valid nonces
  758. double death = ITS_DEAD_JIM * (state->death_stage_one ? 2.0 : 1.0);
  759. cgtime(&now);
  760. if (tdiff(&now, &state->last_nonce) >= death) {
  761. if (state->death_stage_one) {
  762. modminer_delta_clock(thr, MODMINER_CLOCK_DEAD, false, true);
  763. applog(LOG_ERR, "%s%u: DEATH clock down",
  764. modminer->drv->name, modminer->device_id);
  765. // reset the death info and DISABLE it
  766. state->last_nonce.tv_sec = 0;
  767. state->last_nonce.tv_usec = 0;
  768. state->death_stage_one = false;
  769. return -1;
  770. } else {
  771. modminer_delta_clock(thr, MODMINER_CLOCK_DEAD, false, true);
  772. applog(LOG_ERR, "%s%u: death clock down",
  773. modminer->drv->name, modminer->device_id);
  774. state->death_stage_one = true;
  775. }
  776. }
  777. }
  778. tryagain:
  779. if (work_restart(thr))
  780. break;
  781. if (state->overheated == true) {
  782. // don't check every time (every ~1/2 sec)
  783. if (++temploop > 4) {
  784. check_temperature(thr);
  785. temploop = 0;
  786. }
  787. }
  788. if (state->overheated == true)
  789. timeout = overheattime;
  790. else
  791. timeout = processtime;
  792. cgtime(&now);
  793. if (tdiff(&now, &state->tv_workstart) > timeout)
  794. break;
  795. // 1/10th sec to lower CPU usage
  796. nmsleep(100);
  797. if (work_restart(thr))
  798. break;
  799. }
  800. struct timeval tv_workend, elapsed;
  801. cgtime(&tv_workend);
  802. timersub(&tv_workend, &state->tv_workstart, &elapsed);
  803. // Not exact since the clock may have changed ... but close enough I guess
  804. uint64_t hashes = (uint64_t)modminer->clock * (((uint64_t)elapsed.tv_sec * 1000000) + elapsed.tv_usec);
  805. // Overheat will complete the nonce range
  806. if (hashes > 0xffffffff)
  807. hashes = 0xffffffff;
  808. work->blk.nonce = 0xffffffff;
  809. return hashes;
  810. }
  811. static int64_t modminer_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  812. {
  813. struct modminer_fpga_state *state = thr->cgpu_data;
  814. struct timeval tv1, tv2;
  815. int64_t hashes;
  816. // Device is gone
  817. if (thr->cgpu->usbinfo.nodev)
  818. return -1;
  819. // Don't start new work if overheated
  820. if (state->overheated == true) {
  821. cgtime(&tv1);
  822. while (state->overheated == true) {
  823. check_temperature(thr);
  824. // Device is gone
  825. if (thr->cgpu->usbinfo.nodev)
  826. return -1;
  827. if (state->overheated == true) {
  828. cgtime(&tv2);
  829. // give up on this work item after 30s
  830. if (work_restart(thr) || tdiff(&tv2, &tv1) > 30)
  831. return 0;
  832. // Give it 1s rest then check again
  833. nmsleep(1000);
  834. }
  835. }
  836. }
  837. if (!modminer_start_work(thr, work))
  838. return -1;
  839. hashes = modminer_process_results(thr, work);
  840. if (hashes == -1)
  841. return hashes;
  842. return hashes;
  843. }
  844. static void modminer_hw_error(struct thr_info *thr)
  845. {
  846. struct modminer_fpga_state *state = thr->cgpu_data;
  847. state->hw_errors++;
  848. }
  849. static void modminer_fpga_shutdown(struct thr_info *thr)
  850. {
  851. free(thr->cgpu_data);
  852. thr->cgpu_data = NULL;
  853. }
  854. static char *modminer_set_device(struct cgpu_info *modminer, char *option, char *setting, char *replybuf)
  855. {
  856. const char *ret;
  857. int val;
  858. if (strcasecmp(option, "help") == 0) {
  859. sprintf(replybuf, "clock: range %d-%d and a multiple of 2",
  860. MODMINER_MIN_CLOCK, MODMINER_MAX_CLOCK);
  861. return replybuf;
  862. }
  863. if (strcasecmp(option, "clock") == 0) {
  864. if (!setting || !*setting) {
  865. sprintf(replybuf, "missing clock setting");
  866. return replybuf;
  867. }
  868. val = atoi(setting);
  869. if (val < MODMINER_MIN_CLOCK || val > MODMINER_MAX_CLOCK || (val & 1) != 0) {
  870. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d and a multiple of 2",
  871. setting, MODMINER_MIN_CLOCK, MODMINER_MAX_CLOCK);
  872. return replybuf;
  873. }
  874. val -= (int)(modminer->clock);
  875. ret = modminer_delta_clock(modminer->thr[0], val, false, true);
  876. if (ret) {
  877. sprintf(replybuf, "Set clock failed: %s", ret);
  878. return replybuf;
  879. } else
  880. return NULL;
  881. }
  882. sprintf(replybuf, "Unknown option: %s", option);
  883. return replybuf;
  884. }
  885. struct device_drv modminer_drv = {
  886. .drv_id = DRIVER_MODMINER,
  887. .dname = "ModMiner",
  888. .name = "MMQ",
  889. .drv_detect = modminer_detect,
  890. .get_statline_before = get_modminer_statline_before,
  891. .set_device = modminer_set_device,
  892. .thread_prepare = modminer_fpga_prepare,
  893. .thread_init = modminer_fpga_init,
  894. .scanhash = modminer_scanhash,
  895. .hw_error = modminer_hw_error,
  896. .thread_shutdown = modminer_fpga_shutdown,
  897. };