driver-modminer.c 30 KB

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