driver-avalon.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. /*
  2. * Copyright 2013 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2012-2013 Xiangfu <xiangfu@openmobilefree.com>
  4. * Copyright 2012 Luke Dashjr
  5. * Copyright 2012 Andrew Smith
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #include "config.h"
  13. #include <limits.h>
  14. #include <pthread.h>
  15. #include <stdio.h>
  16. #include <sys/time.h>
  17. #include <sys/types.h>
  18. #include <ctype.h>
  19. #include <dirent.h>
  20. #include <unistd.h>
  21. #include <time.h>
  22. #ifndef WIN32
  23. #include <sys/select.h>
  24. #include <termios.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #ifndef O_CLOEXEC
  28. #define O_CLOEXEC 0
  29. #endif
  30. #else
  31. #include "compat.h"
  32. #include <windows.h>
  33. #include <io.h>
  34. #endif
  35. #include "elist.h"
  36. #include "miner.h"
  37. #include "usbutils.h"
  38. #include "driver-avalon.h"
  39. #include "hexdump.c"
  40. #include "util.h"
  41. int opt_avalon_temp = AVALON_TEMP_TARGET;
  42. int opt_avalon_overheat = AVALON_TEMP_OVERHEAT;
  43. int opt_avalon_fan_min = AVALON_DEFAULT_FAN_MIN_PWM;
  44. int opt_avalon_fan_max = AVALON_DEFAULT_FAN_MAX_PWM;
  45. int opt_avalon_freq_min = AVALON_MIN_FREQUENCY;
  46. int opt_avalon_freq_max = AVALON_MAX_FREQUENCY;
  47. int opt_bitburner_core_voltage = BITBURNER_DEFAULT_CORE_VOLTAGE;
  48. int opt_bitburner_fury_core_voltage = BITBURNER_FURY_DEFAULT_CORE_VOLTAGE;
  49. bool opt_avalon_auto;
  50. static int option_offset = -1;
  51. static int bbf_option_offset = -1;
  52. static int avalon_init_task(struct avalon_task *at,
  53. uint8_t reset, uint8_t ff, uint8_t fan,
  54. uint8_t timeout, uint8_t asic_num,
  55. uint8_t miner_num, uint8_t nonce_elf,
  56. uint8_t gate_miner, int frequency)
  57. {
  58. uint16_t *lefreq16;
  59. uint8_t *buf;
  60. static bool first = true;
  61. if (unlikely(!at))
  62. return -1;
  63. if (unlikely(timeout <= 0 || asic_num <= 0 || miner_num <= 0))
  64. return -1;
  65. memset(at, 0, sizeof(struct avalon_task));
  66. if (unlikely(reset)) {
  67. at->reset = 1;
  68. at->fan_eft = 1;
  69. at->timer_eft = 1;
  70. first = true;
  71. }
  72. at->flush_fifo = (ff ? 1 : 0);
  73. at->fan_eft = (fan ? 1 : 0);
  74. if (unlikely(first && !at->reset)) {
  75. at->fan_eft = 1;
  76. at->timer_eft = 1;
  77. first = false;
  78. }
  79. at->fan_pwm_data = (fan ? fan : AVALON_DEFAULT_FAN_MAX_PWM);
  80. at->timeout_data = timeout;
  81. at->asic_num = asic_num;
  82. at->miner_num = miner_num;
  83. at->nonce_elf = nonce_elf;
  84. at->gate_miner_elf = 1;
  85. at->asic_pll = 1;
  86. if (unlikely(gate_miner)) {
  87. at-> gate_miner = 1;
  88. at->asic_pll = 0;
  89. }
  90. buf = (uint8_t *)at;
  91. buf[5] = 0x00;
  92. buf[8] = 0x74;
  93. buf[9] = 0x01;
  94. buf[10] = 0x00;
  95. buf[11] = 0x00;
  96. lefreq16 = (uint16_t *)&buf[6];
  97. *lefreq16 = htole16(frequency * 8);
  98. return 0;
  99. }
  100. static inline void avalon_create_task(struct avalon_task *at,
  101. struct work *work)
  102. {
  103. memcpy(at->midstate, work->midstate, 32);
  104. memcpy(at->data, work->data + 64, 12);
  105. }
  106. static int avalon_write(struct cgpu_info *avalon, char *buf, ssize_t len, int ep)
  107. {
  108. int err, amount;
  109. err = usb_write(avalon, buf, len, &amount, ep);
  110. applog(LOG_DEBUG, "%s%i: usb_write got err %d", avalon->drv->name,
  111. avalon->device_id, err);
  112. if (unlikely(err != 0)) {
  113. applog(LOG_WARNING, "usb_write error on avalon_write");
  114. return AVA_SEND_ERROR;
  115. }
  116. if (amount != len) {
  117. applog(LOG_WARNING, "usb_write length mismatch on avalon_write");
  118. return AVA_SEND_ERROR;
  119. }
  120. return AVA_SEND_OK;
  121. }
  122. static int avalon_send_task(const struct avalon_task *at, struct cgpu_info *avalon)
  123. {
  124. uint8_t buf[AVALON_WRITE_SIZE + 4 * AVALON_DEFAULT_ASIC_NUM];
  125. int ret, i, ep = C_AVALON_TASK;
  126. uint32_t nonce_range;
  127. size_t nr_len;
  128. if (at->nonce_elf)
  129. nr_len = AVALON_WRITE_SIZE + 4 * at->asic_num;
  130. else
  131. nr_len = AVALON_WRITE_SIZE;
  132. memcpy(buf, at, AVALON_WRITE_SIZE);
  133. if (at->nonce_elf) {
  134. nonce_range = (uint32_t)0xffffffff / at->asic_num;
  135. for (i = 0; i < at->asic_num; i++) {
  136. buf[AVALON_WRITE_SIZE + (i * 4) + 3] =
  137. (i * nonce_range & 0xff000000) >> 24;
  138. buf[AVALON_WRITE_SIZE + (i * 4) + 2] =
  139. (i * nonce_range & 0x00ff0000) >> 16;
  140. buf[AVALON_WRITE_SIZE + (i * 4) + 1] =
  141. (i * nonce_range & 0x0000ff00) >> 8;
  142. buf[AVALON_WRITE_SIZE + (i * 4) + 0] =
  143. (i * nonce_range & 0x000000ff) >> 0;
  144. }
  145. }
  146. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  147. uint8_t tt = 0;
  148. tt = (buf[0] & 0x0f) << 4;
  149. tt |= ((buf[0] & 0x10) ? (1 << 3) : 0);
  150. tt |= ((buf[0] & 0x20) ? (1 << 2) : 0);
  151. tt |= ((buf[0] & 0x40) ? (1 << 1) : 0);
  152. tt |= ((buf[0] & 0x80) ? (1 << 0) : 0);
  153. buf[0] = tt;
  154. tt = (buf[4] & 0x0f) << 4;
  155. tt |= ((buf[4] & 0x10) ? (1 << 3) : 0);
  156. tt |= ((buf[4] & 0x20) ? (1 << 2) : 0);
  157. tt |= ((buf[4] & 0x40) ? (1 << 1) : 0);
  158. tt |= ((buf[4] & 0x80) ? (1 << 0) : 0);
  159. buf[4] = tt;
  160. #endif
  161. if (at->reset) {
  162. ep = C_AVALON_RESET;
  163. nr_len = 1;
  164. }
  165. if (opt_debug) {
  166. applog(LOG_DEBUG, "Avalon: Sent(%u):", (unsigned int)nr_len);
  167. hexdump(buf, nr_len);
  168. }
  169. ret = avalon_write(avalon, (char *)buf, nr_len, ep);
  170. /* Avalon needs a rest between submissions :P */
  171. cgsleep_ms(4);
  172. return ret;
  173. }
  174. static int bitburner_send_task(const struct avalon_task *at, struct cgpu_info *avalon)
  175. {
  176. uint8_t buf[AVALON_WRITE_SIZE + 4 * AVALON_DEFAULT_ASIC_NUM];
  177. int ret, ep = C_AVALON_TASK;
  178. size_t nr_len;
  179. if (at->nonce_elf)
  180. nr_len = AVALON_WRITE_SIZE + 4 * at->asic_num;
  181. else
  182. nr_len = AVALON_WRITE_SIZE;
  183. memset(buf, 0, nr_len);
  184. memcpy(buf, at, AVALON_WRITE_SIZE);
  185. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  186. uint8_t tt = 0;
  187. tt = (buf[0] & 0x0f) << 4;
  188. tt |= ((buf[0] & 0x10) ? (1 << 3) : 0);
  189. tt |= ((buf[0] & 0x20) ? (1 << 2) : 0);
  190. tt |= ((buf[0] & 0x40) ? (1 << 1) : 0);
  191. tt |= ((buf[0] & 0x80) ? (1 << 0) : 0);
  192. buf[0] = tt;
  193. tt = (buf[4] & 0x0f) << 4;
  194. tt |= ((buf[4] & 0x10) ? (1 << 3) : 0);
  195. tt |= ((buf[4] & 0x20) ? (1 << 2) : 0);
  196. tt |= ((buf[4] & 0x40) ? (1 << 1) : 0);
  197. tt |= ((buf[4] & 0x80) ? (1 << 0) : 0);
  198. buf[4] = tt;
  199. #endif
  200. if (at->reset) {
  201. ep = C_AVALON_RESET;
  202. nr_len = 1;
  203. }
  204. if (opt_debug) {
  205. applog(LOG_DEBUG, "Avalon: Sent(%u):", (unsigned int)nr_len);
  206. hexdump(buf, nr_len);
  207. }
  208. ret = avalon_write(avalon, (char *)buf, nr_len, ep);
  209. return ret;
  210. }
  211. static bool avalon_decode_nonce(struct thr_info *thr, struct cgpu_info *avalon,
  212. struct avalon_info *info, struct avalon_result *ar,
  213. struct work *work)
  214. {
  215. uint32_t nonce;
  216. info = avalon->device_data;
  217. info->matching_work[work->subid]++;
  218. nonce = htole32(ar->nonce);
  219. applog(LOG_DEBUG, "Avalon: nonce = %0x08x", nonce);
  220. return submit_nonce(thr, work, nonce);
  221. }
  222. /* Wait until the ftdi chip returns a CTS saying we can send more data. */
  223. static void wait_avalon_ready(struct cgpu_info *avalon)
  224. {
  225. while (avalon_buffer_full(avalon)) {
  226. cgsleep_ms(40);
  227. }
  228. }
  229. #define AVALON_CTS (1 << 4)
  230. static inline bool avalon_cts(char c)
  231. {
  232. return (c & AVALON_CTS);
  233. }
  234. static int avalon_read(struct cgpu_info *avalon, unsigned char *buf,
  235. size_t bufsize, int timeout, int ep)
  236. {
  237. size_t total = 0, readsize = bufsize + 2;
  238. char readbuf[AVALON_READBUF_SIZE];
  239. int err, amount, ofs = 2, cp;
  240. err = usb_read_once_timeout(avalon, readbuf, readsize, &amount, timeout, ep);
  241. applog(LOG_DEBUG, "%s%i: Get avalon read got err %d",
  242. avalon->drv->name, avalon->device_id, err);
  243. if (amount < 2)
  244. goto out;
  245. /* The first 2 of every 64 bytes are status on FTDIRL */
  246. while (amount > 2) {
  247. cp = amount - 2;
  248. if (cp > 62)
  249. cp = 62;
  250. memcpy(&buf[total], &readbuf[ofs], cp);
  251. total += cp;
  252. amount -= cp + 2;
  253. ofs += 64;
  254. }
  255. out:
  256. return total;
  257. }
  258. static int avalon_reset(struct cgpu_info *avalon, bool initial)
  259. {
  260. struct avalon_result ar;
  261. int ret, i, spare;
  262. struct avalon_task at;
  263. uint8_t *buf, *tmp;
  264. struct timespec p;
  265. struct avalon_info *info = avalon->device_data;
  266. /* Send reset, then check for result */
  267. avalon_init_task(&at, 1, 0,
  268. AVALON_DEFAULT_FAN_MAX_PWM,
  269. AVALON_DEFAULT_TIMEOUT,
  270. AVALON_DEFAULT_ASIC_NUM,
  271. AVALON_DEFAULT_MINER_NUM,
  272. 0, 0,
  273. AVALON_DEFAULT_FREQUENCY);
  274. wait_avalon_ready(avalon);
  275. ret = avalon_send_task(&at, avalon);
  276. if (unlikely(ret == AVA_SEND_ERROR))
  277. return -1;
  278. if (!initial) {
  279. applog(LOG_ERR, "%s%d reset sequence sent", avalon->drv->name, avalon->device_id);
  280. return 0;
  281. }
  282. ret = avalon_read(avalon, (unsigned char *)&ar, AVALON_READ_SIZE,
  283. AVALON_RESET_TIMEOUT, C_GET_AVALON_RESET);
  284. /* What do these sleeps do?? */
  285. p.tv_sec = 0;
  286. p.tv_nsec = AVALON_RESET_PITCH;
  287. nanosleep(&p, NULL);
  288. /* Look for the first occurrence of 0xAA, the reset response should be:
  289. * AA 55 AA 55 00 00 00 00 00 00 */
  290. spare = ret - 10;
  291. buf = tmp = (uint8_t *)&ar;
  292. if (opt_debug) {
  293. applog(LOG_DEBUG, "%s%d reset: get:", avalon->drv->name, avalon->device_id);
  294. hexdump(tmp, AVALON_READ_SIZE);
  295. }
  296. for (i = 0; i <= spare; i++) {
  297. buf = &tmp[i];
  298. if (buf[0] == 0xAA)
  299. break;
  300. }
  301. i = 0;
  302. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  303. buf[2] == 0xAA && buf[3] == 0x55) {
  304. for (i = 4; i < 11; i++)
  305. if (buf[i] != 0)
  306. break;
  307. }
  308. if (i != 11) {
  309. applog(LOG_ERR, "%s%d: Reset failed! not an Avalon?"
  310. " (%d: %02x %02x %02x %02x)", avalon->drv->name, avalon->device_id,
  311. i, buf[0], buf[1], buf[2], buf[3]);
  312. /* FIXME: return 1; */
  313. } else {
  314. /* buf[44]: minor
  315. * buf[45]: day
  316. * buf[46]: year,month, d6: 201306
  317. */
  318. info->ctlr_ver = ((buf[46] >> 4) + 2000) * 1000000 +
  319. (buf[46] & 0x0f) * 10000 +
  320. buf[45] * 100 + buf[44];
  321. applog(LOG_WARNING, "%s%d: Reset succeeded (Controller version: %d)",
  322. avalon->drv->name, avalon->device_id, info->ctlr_ver);
  323. }
  324. return 0;
  325. }
  326. static int avalon_calc_timeout(int frequency)
  327. {
  328. return AVALON_TIMEOUT_FACTOR / frequency;
  329. }
  330. static bool get_options(int this_option_offset, int *baud, int *miner_count,
  331. int *asic_count, int *timeout, int *frequency, char *options)
  332. {
  333. char buf[BUFSIZ+1];
  334. char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
  335. bool timeout_default;
  336. size_t max;
  337. int i, tmp;
  338. if (options == NULL)
  339. buf[0] = '\0';
  340. else {
  341. ptr = options;
  342. for (i = 0; i < this_option_offset; i++) {
  343. comma = strchr(ptr, ',');
  344. if (comma == NULL)
  345. break;
  346. ptr = comma + 1;
  347. }
  348. comma = strchr(ptr, ',');
  349. if (comma == NULL)
  350. max = strlen(ptr);
  351. else
  352. max = comma - ptr;
  353. if (max > BUFSIZ)
  354. max = BUFSIZ;
  355. strncpy(buf, ptr, max);
  356. buf[max] = '\0';
  357. }
  358. if (!(*buf))
  359. return false;
  360. colon = strchr(buf, ':');
  361. if (colon)
  362. *(colon++) = '\0';
  363. tmp = atoi(buf);
  364. switch (tmp) {
  365. case 115200:
  366. *baud = 115200;
  367. break;
  368. case 57600:
  369. *baud = 57600;
  370. break;
  371. case 38400:
  372. *baud = 38400;
  373. break;
  374. case 19200:
  375. *baud = 19200;
  376. break;
  377. default:
  378. quit(1, "Invalid avalon-options for baud (%s) "
  379. "must be 115200, 57600, 38400 or 19200", buf);
  380. }
  381. if (colon && *colon) {
  382. colon2 = strchr(colon, ':');
  383. if (colon2)
  384. *(colon2++) = '\0';
  385. if (*colon) {
  386. tmp = atoi(colon);
  387. if (tmp > 0 && tmp <= AVALON_MAX_MINER_NUM) {
  388. *miner_count = tmp;
  389. } else {
  390. quit(1, "Invalid avalon-options for "
  391. "miner_count (%s) must be 1 ~ %d",
  392. colon, AVALON_MAX_MINER_NUM);
  393. }
  394. }
  395. if (colon2 && *colon2) {
  396. colon3 = strchr(colon2, ':');
  397. if (colon3)
  398. *(colon3++) = '\0';
  399. tmp = atoi(colon2);
  400. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  401. *asic_count = tmp;
  402. else {
  403. quit(1, "Invalid avalon-options for "
  404. "asic_count (%s) must be 1 ~ %d",
  405. colon2, AVALON_DEFAULT_ASIC_NUM);
  406. }
  407. timeout_default = false;
  408. if (colon3 && *colon3) {
  409. colon4 = strchr(colon3, ':');
  410. if (colon4)
  411. *(colon4++) = '\0';
  412. if (tolower(*colon3) == 'd')
  413. timeout_default = true;
  414. else {
  415. tmp = atoi(colon3);
  416. if (tmp > 0 && tmp <= 0xff)
  417. *timeout = tmp;
  418. else {
  419. quit(1, "Invalid avalon-options for "
  420. "timeout (%s) must be 1 ~ %d",
  421. colon3, 0xff);
  422. }
  423. }
  424. if (colon4 && *colon4) {
  425. tmp = atoi(colon4);
  426. if (tmp < AVALON_MIN_FREQUENCY || tmp > AVALON_MAX_FREQUENCY) {
  427. quit(1, "Invalid avalon-options for frequency, must be %d <= frequency <= %d",
  428. AVALON_MIN_FREQUENCY, AVALON_MAX_FREQUENCY);
  429. }
  430. *frequency = tmp;
  431. if (timeout_default)
  432. *timeout = avalon_calc_timeout(*frequency);
  433. }
  434. }
  435. }
  436. }
  437. return true;
  438. }
  439. char *set_avalon_fan(char *arg)
  440. {
  441. int val1, val2, ret;
  442. ret = sscanf(arg, "%d-%d", &val1, &val2);
  443. if (ret < 1)
  444. return "No values passed to avalon-fan";
  445. if (ret == 1)
  446. val2 = val1;
  447. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  448. return "Invalid value passed to avalon-fan";
  449. opt_avalon_fan_min = val1 * AVALON_PWM_MAX / 100;
  450. opt_avalon_fan_max = val2 * AVALON_PWM_MAX / 100;
  451. return NULL;
  452. }
  453. char *set_avalon_freq(char *arg)
  454. {
  455. int val1, val2, ret;
  456. ret = sscanf(arg, "%d-%d", &val1, &val2);
  457. if (ret < 1)
  458. return "No values passed to avalon-freq";
  459. if (ret == 1)
  460. val2 = val1;
  461. if (val1 < AVALON_MIN_FREQUENCY || val1 > AVALON_MAX_FREQUENCY ||
  462. val2 < AVALON_MIN_FREQUENCY || val2 > AVALON_MAX_FREQUENCY ||
  463. val2 < val1)
  464. return "Invalid value passed to avalon-freq";
  465. opt_avalon_freq_min = val1;
  466. opt_avalon_freq_max = val2;
  467. return NULL;
  468. }
  469. static void avalon_idle(struct cgpu_info *avalon, struct avalon_info *info)
  470. {
  471. int i;
  472. wait_avalon_ready(avalon);
  473. /* Send idle to all miners */
  474. for (i = 0; i < info->miner_count; i++) {
  475. struct avalon_task at;
  476. if (unlikely(avalon_buffer_full(avalon)))
  477. break;
  478. info->idle++;
  479. avalon_init_task(&at, 0, 0, info->fan_pwm, info->timeout,
  480. info->asic_count, info->miner_count, 1, 1,
  481. info->frequency);
  482. avalon_send_task(&at, avalon);
  483. }
  484. applog(LOG_WARNING, "%s%i: Idling %d miners", avalon->drv->name, avalon->device_id, i);
  485. wait_avalon_ready(avalon);
  486. }
  487. static void avalon_initialise(struct cgpu_info *avalon)
  488. {
  489. int err, interface;
  490. if (avalon->usbinfo.nodev)
  491. return;
  492. interface = usb_interface(avalon);
  493. // Reset
  494. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  495. FTDI_VALUE_RESET, interface, C_RESET);
  496. applog(LOG_DEBUG, "%s%i: reset got err %d",
  497. avalon->drv->name, avalon->device_id, err);
  498. if (avalon->usbinfo.nodev)
  499. return;
  500. // Set latency
  501. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY,
  502. AVALON_LATENCY, interface, C_LATENCY);
  503. applog(LOG_DEBUG, "%s%i: latency got err %d",
  504. avalon->drv->name, avalon->device_id, err);
  505. if (avalon->usbinfo.nodev)
  506. return;
  507. // Set data
  508. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  509. FTDI_VALUE_DATA_AVA, interface, C_SETDATA);
  510. applog(LOG_DEBUG, "%s%i: data got err %d",
  511. avalon->drv->name, avalon->device_id, err);
  512. if (avalon->usbinfo.nodev)
  513. return;
  514. // Set the baud
  515. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_AVA,
  516. (FTDI_INDEX_BAUD_AVA & 0xff00) | interface,
  517. C_SETBAUD);
  518. applog(LOG_DEBUG, "%s%i: setbaud got err %d",
  519. avalon->drv->name, avalon->device_id, err);
  520. if (avalon->usbinfo.nodev)
  521. return;
  522. // Set Modem Control
  523. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  524. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  525. applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
  526. avalon->drv->name, avalon->device_id, err);
  527. if (avalon->usbinfo.nodev)
  528. return;
  529. // Set Flow Control
  530. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  531. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  532. applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
  533. avalon->drv->name, avalon->device_id, err);
  534. if (avalon->usbinfo.nodev)
  535. return;
  536. /* Avalon repeats the following */
  537. // Set Modem Control
  538. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  539. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  540. applog(LOG_DEBUG, "%s%i: setmodemctrl 2 got err %d",
  541. avalon->drv->name, avalon->device_id, err);
  542. if (avalon->usbinfo.nodev)
  543. return;
  544. // Set Flow Control
  545. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  546. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  547. applog(LOG_DEBUG, "%s%i: setflowctrl 2 got err %d",
  548. avalon->drv->name, avalon->device_id, err);
  549. }
  550. static bool is_bitburner(struct cgpu_info *avalon)
  551. {
  552. enum sub_ident ident;
  553. ident = usb_ident(avalon);
  554. return ident == IDENT_BTB || ident == IDENT_BBF;
  555. }
  556. static bool bitburner_set_core_voltage(struct cgpu_info *avalon, int core_voltage)
  557. {
  558. uint8_t buf[2];
  559. int err;
  560. if (is_bitburner(avalon)) {
  561. buf[0] = (uint8_t)core_voltage;
  562. buf[1] = (uint8_t)(core_voltage >> 8);
  563. err = usb_transfer_data(avalon, FTDI_TYPE_OUT, BITBURNER_REQUEST,
  564. BITBURNER_VALUE, BITBURNER_INDEX_SET_VOLTAGE,
  565. (uint32_t *)buf, sizeof(buf), C_BB_SET_VOLTAGE);
  566. if (unlikely(err < 0)) {
  567. applog(LOG_ERR, "%s%i: SetCoreVoltage failed: err = %d",
  568. avalon->drv->name, avalon->device_id, err);
  569. return false;
  570. } else {
  571. applog(LOG_WARNING, "%s%i: Core voltage set to %d millivolts",
  572. avalon->drv->name, avalon->device_id,
  573. core_voltage);
  574. }
  575. return true;
  576. }
  577. return false;
  578. }
  579. static int bitburner_get_core_voltage(struct cgpu_info *avalon)
  580. {
  581. uint8_t buf[2];
  582. int err;
  583. int amount;
  584. if (is_bitburner(avalon)) {
  585. err = usb_transfer_read(avalon, FTDI_TYPE_IN, BITBURNER_REQUEST,
  586. BITBURNER_VALUE, BITBURNER_INDEX_GET_VOLTAGE,
  587. (char *)buf, sizeof(buf), &amount,
  588. C_BB_GET_VOLTAGE);
  589. if (unlikely(err != 0 || amount != 2)) {
  590. applog(LOG_ERR, "%s%i: GetCoreVoltage failed: err = %d, amount = %d",
  591. avalon->drv->name, avalon->device_id, err, amount);
  592. return 0;
  593. } else {
  594. return (int)(buf[0] + ((unsigned int)buf[1] << 8));
  595. }
  596. } else {
  597. return 0;
  598. }
  599. }
  600. static void bitburner_get_version(struct cgpu_info *avalon)
  601. {
  602. struct avalon_info *info = avalon->device_data;
  603. uint8_t buf[3];
  604. int err;
  605. int amount;
  606. err = usb_transfer_read(avalon, FTDI_TYPE_IN, BITBURNER_REQUEST,
  607. BITBURNER_VALUE, BITBURNER_INDEX_GET_VERSION,
  608. (char *)buf, sizeof(buf), &amount,
  609. C_GETVERSION);
  610. if (unlikely(err != 0 || amount != sizeof(buf))) {
  611. applog(LOG_DEBUG, "%s%i: GetVersion failed: err=%d, amt=%d assuming %d.%d.%d",
  612. avalon->drv->name, avalon->device_id, err, amount,
  613. BITBURNER_VERSION1, BITBURNER_VERSION2, BITBURNER_VERSION3);
  614. info->version1 = BITBURNER_VERSION1;
  615. info->version2 = BITBURNER_VERSION2;
  616. info->version3 = BITBURNER_VERSION3;
  617. } else {
  618. info->version1 = buf[0];
  619. info->version2 = buf[1];
  620. info->version3 = buf[2];
  621. }
  622. }
  623. static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found)
  624. {
  625. int baud, miner_count, asic_count, timeout, frequency;
  626. int this_option_offset;
  627. struct avalon_info *info;
  628. struct cgpu_info *avalon;
  629. bool configured;
  630. int ret;
  631. avalon = usb_alloc_cgpu(&avalon_drv, AVALON_MINER_THREADS);
  632. baud = AVALON_IO_SPEED;
  633. miner_count = AVALON_DEFAULT_MINER_NUM;
  634. asic_count = AVALON_DEFAULT_ASIC_NUM;
  635. timeout = AVALON_DEFAULT_TIMEOUT;
  636. frequency = AVALON_DEFAULT_FREQUENCY;
  637. if (!usb_init(avalon, dev, found))
  638. goto shin;
  639. this_option_offset = usb_ident(avalon) == IDENT_BBF ? ++bbf_option_offset : ++option_offset;
  640. configured = get_options(this_option_offset, &baud, &miner_count,
  641. &asic_count, &timeout, &frequency,
  642. (usb_ident(avalon) == IDENT_BBF && opt_bitburner_fury_options != NULL) ? opt_bitburner_fury_options : opt_avalon_options);
  643. /* Even though this is an FTDI type chip, we want to do the parsing
  644. * all ourselves so set it to std usb type */
  645. avalon->usbdev->usb_type = USB_TYPE_STD;
  646. /* We have a real Avalon! */
  647. avalon_initialise(avalon);
  648. avalon->device_data = calloc(sizeof(struct avalon_info), 1);
  649. if (unlikely(!(avalon->device_data)))
  650. quit(1, "Failed to calloc avalon_info data");
  651. info = avalon->device_data;
  652. if (configured) {
  653. info->baud = baud;
  654. info->miner_count = miner_count;
  655. info->asic_count = asic_count;
  656. info->timeout = timeout;
  657. info->frequency = frequency;
  658. } else {
  659. info->baud = AVALON_IO_SPEED;
  660. info->asic_count = AVALON_DEFAULT_ASIC_NUM;
  661. switch (usb_ident(avalon)) {
  662. case IDENT_BBF:
  663. info->miner_count = BITBURNER_FURY_DEFAULT_MINER_NUM;
  664. info->timeout = BITBURNER_FURY_DEFAULT_TIMEOUT;
  665. info->frequency = BITBURNER_FURY_DEFAULT_FREQUENCY;
  666. break;
  667. default:
  668. info->miner_count = AVALON_DEFAULT_MINER_NUM;
  669. info->timeout = AVALON_DEFAULT_TIMEOUT;
  670. info->frequency = AVALON_DEFAULT_FREQUENCY;
  671. }
  672. }
  673. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  674. info->temp_max = 0;
  675. /* This is for check the temp/fan every 3~4s */
  676. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  677. if (info->temp_history_count <= 0)
  678. info->temp_history_count = 1;
  679. info->temp_history_index = 0;
  680. info->temp_sum = 0;
  681. info->temp_old = 0;
  682. if (!add_cgpu(avalon))
  683. goto unshin;
  684. usb_set_cps(avalon, info->baud / 10);
  685. usb_enable_cps(avalon);
  686. ret = avalon_reset(avalon, true);
  687. if (ret && !configured)
  688. goto unshin;
  689. update_usb_stats(avalon);
  690. avalon_idle(avalon, info);
  691. applog(LOG_DEBUG, "Avalon Detected: %s "
  692. "(miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  693. avalon->device_path, info->miner_count, info->asic_count, info->timeout,
  694. info->frequency);
  695. if (usb_ident(avalon) == IDENT_BTB) {
  696. if (opt_bitburner_core_voltage < BITBURNER_MIN_COREMV ||
  697. opt_bitburner_core_voltage > BITBURNER_MAX_COREMV) {
  698. quit(1, "Invalid bitburner-voltage %d must be %dmv - %dmv",
  699. opt_bitburner_core_voltage,
  700. BITBURNER_MIN_COREMV,
  701. BITBURNER_MAX_COREMV);
  702. } else
  703. bitburner_set_core_voltage(avalon, opt_bitburner_core_voltage);
  704. } else if (usb_ident(avalon) == IDENT_BBF) {
  705. if (opt_bitburner_fury_core_voltage < BITBURNER_FURY_MIN_COREMV ||
  706. opt_bitburner_fury_core_voltage > BITBURNER_FURY_MAX_COREMV) {
  707. quit(1, "Invalid bitburner-fury-voltage %d must be %dmv - %dmv",
  708. opt_bitburner_fury_core_voltage,
  709. BITBURNER_FURY_MIN_COREMV,
  710. BITBURNER_FURY_MAX_COREMV);
  711. } else
  712. bitburner_set_core_voltage(avalon, opt_bitburner_fury_core_voltage);
  713. }
  714. if (is_bitburner(avalon)) {
  715. bitburner_get_version(avalon);
  716. }
  717. return true;
  718. unshin:
  719. usb_uninit(avalon);
  720. shin:
  721. free(avalon->device_data);
  722. avalon->device_data = NULL;
  723. avalon = usb_free_cgpu(avalon);
  724. return false;
  725. }
  726. static void avalon_detect(bool __maybe_unused hotplug)
  727. {
  728. usb_detect(&avalon_drv, avalon_detect_one);
  729. }
  730. static void avalon_init(struct cgpu_info *avalon)
  731. {
  732. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  733. }
  734. static struct work *avalon_valid_result(struct cgpu_info *avalon, struct avalon_result *ar)
  735. {
  736. return clone_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
  737. (char *)ar->data, 64, 12);
  738. }
  739. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  740. struct avalon_result *ar);
  741. static void avalon_inc_nvw(struct avalon_info *info, struct thr_info *thr)
  742. {
  743. applog(LOG_INFO, "%s%d: No matching work - HW error",
  744. thr->cgpu->drv->name, thr->cgpu->device_id);
  745. inc_hw_errors(thr);
  746. info->no_matching_work++;
  747. }
  748. static void avalon_parse_results(struct cgpu_info *avalon, struct avalon_info *info,
  749. struct thr_info *thr, char *buf, int *offset)
  750. {
  751. int i, spare = *offset - AVALON_READ_SIZE;
  752. bool found = false;
  753. for (i = 0; i <= spare; i++) {
  754. struct avalon_result *ar;
  755. struct work *work;
  756. ar = (struct avalon_result *)&buf[i];
  757. work = avalon_valid_result(avalon, ar);
  758. if (work) {
  759. bool gettemp = false;
  760. found = true;
  761. if (avalon_decode_nonce(thr, avalon, info, ar, work)) {
  762. mutex_lock(&info->lock);
  763. if (!info->nonces++)
  764. gettemp = true;
  765. info->auto_nonces++;
  766. mutex_unlock(&info->lock);
  767. } else if (opt_avalon_auto) {
  768. mutex_lock(&info->lock);
  769. info->auto_hw++;
  770. mutex_unlock(&info->lock);
  771. }
  772. free_work(work);
  773. if (gettemp)
  774. avalon_update_temps(avalon, info, ar);
  775. break;
  776. }
  777. }
  778. if (!found) {
  779. spare = *offset - AVALON_READ_SIZE;
  780. /* We are buffering and haven't accumulated one more corrupt
  781. * work result. */
  782. if (spare < (int)AVALON_READ_SIZE)
  783. return;
  784. avalon_inc_nvw(info, thr);
  785. } else {
  786. spare = AVALON_READ_SIZE + i;
  787. if (i) {
  788. if (i >= (int)AVALON_READ_SIZE)
  789. avalon_inc_nvw(info, thr);
  790. else
  791. applog(LOG_WARNING, "Avalon: Discarding %d bytes from buffer", i);
  792. }
  793. }
  794. *offset -= spare;
  795. memmove(buf, buf + spare, *offset);
  796. }
  797. static void avalon_running_reset(struct cgpu_info *avalon,
  798. struct avalon_info *info)
  799. {
  800. avalon_reset(avalon, false);
  801. avalon_idle(avalon, info);
  802. avalon->results = 0;
  803. info->reset = false;
  804. }
  805. static void *avalon_get_results(void *userdata)
  806. {
  807. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  808. struct avalon_info *info = avalon->device_data;
  809. const int rsize = AVALON_FTDI_READSIZE;
  810. char readbuf[AVALON_READBUF_SIZE];
  811. struct thr_info *thr = info->thr;
  812. cgtimer_t ts_start;
  813. int offset = 0, ret = 0;
  814. char threadname[24];
  815. snprintf(threadname, 24, "ava_recv/%d", avalon->device_id);
  816. RenameThread(threadname);
  817. cgsleep_prepare_r(&ts_start);
  818. while (likely(!avalon->shutdown)) {
  819. unsigned char buf[rsize];
  820. if (offset >= (int)AVALON_READ_SIZE)
  821. avalon_parse_results(avalon, info, thr, readbuf, &offset);
  822. if (unlikely(offset + rsize >= AVALON_READBUF_SIZE)) {
  823. /* This should never happen */
  824. applog(LOG_ERR, "Avalon readbuf overflow, resetting buffer");
  825. offset = 0;
  826. }
  827. if (unlikely(info->reset)) {
  828. avalon_running_reset(avalon, info);
  829. /* Discard anything in the buffer */
  830. offset = 0;
  831. }
  832. /* As the usb read returns after just 1ms, sleep long enough
  833. * to leave the interface idle for writes to occur, but do not
  834. * sleep if we have been receiving data, and we do not yet have
  835. * a full result as more may be coming. */
  836. if (ret < 1 || offset == 0)
  837. cgsleep_ms_r(&ts_start, AVALON_READ_TIMEOUT);
  838. cgsleep_prepare_r(&ts_start);
  839. ret = avalon_read(avalon, buf, rsize, AVALON_READ_TIMEOUT,
  840. C_AVALON_READ);
  841. if (ret < 1)
  842. continue;
  843. if (opt_debug) {
  844. applog(LOG_DEBUG, "Avalon: get:");
  845. hexdump((uint8_t *)buf, ret);
  846. }
  847. memcpy(&readbuf[offset], &buf, ret);
  848. offset += ret;
  849. }
  850. return NULL;
  851. }
  852. static void avalon_rotate_array(struct cgpu_info *avalon)
  853. {
  854. avalon->queued = 0;
  855. if (++avalon->work_array >= AVALON_ARRAY_SIZE)
  856. avalon->work_array = 0;
  857. }
  858. static void bitburner_rotate_array(struct cgpu_info *avalon)
  859. {
  860. avalon->queued = 0;
  861. if (++avalon->work_array >= BITBURNER_ARRAY_SIZE)
  862. avalon->work_array = 0;
  863. }
  864. static void avalon_set_timeout(struct avalon_info *info)
  865. {
  866. info->timeout = avalon_calc_timeout(info->frequency);
  867. }
  868. static void avalon_set_freq(struct cgpu_info *avalon, int frequency)
  869. {
  870. struct avalon_info *info = avalon->device_data;
  871. info->frequency = frequency;
  872. if (info->frequency > opt_avalon_freq_max)
  873. info->frequency = opt_avalon_freq_max;
  874. if (info->frequency < opt_avalon_freq_min)
  875. info->frequency = opt_avalon_freq_min;
  876. avalon_set_timeout(info);
  877. applog(LOG_WARNING, "%s%i: Set frequency to %d, timeout %d",
  878. avalon->drv->name, avalon->device_id,
  879. info->frequency, info->timeout);
  880. }
  881. static void avalon_inc_freq(struct avalon_info *info)
  882. {
  883. info->frequency += 2;
  884. if (info->frequency > opt_avalon_freq_max)
  885. info->frequency = opt_avalon_freq_max;
  886. avalon_set_timeout(info);
  887. applog(LOG_NOTICE, "Avalon increasing frequency to %d, timeout %d",
  888. info->frequency, info->timeout);
  889. }
  890. static void avalon_dec_freq(struct avalon_info *info)
  891. {
  892. info->frequency -= 1;
  893. if (info->frequency < opt_avalon_freq_min)
  894. info->frequency = opt_avalon_freq_min;
  895. avalon_set_timeout(info);
  896. applog(LOG_NOTICE, "Avalon decreasing frequency to %d, timeout %d",
  897. info->frequency, info->timeout);
  898. }
  899. static void avalon_reset_auto(struct avalon_info *info)
  900. {
  901. info->auto_queued =
  902. info->auto_nonces =
  903. info->auto_hw = 0;
  904. }
  905. static void avalon_adjust_freq(struct avalon_info *info, struct cgpu_info *avalon)
  906. {
  907. if (opt_avalon_auto && info->auto_queued >= AVALON_AUTO_CYCLE) {
  908. mutex_lock(&info->lock);
  909. if (!info->optimal) {
  910. if (info->fan_pwm >= opt_avalon_fan_max) {
  911. applog(LOG_WARNING,
  912. "%s%i: Above optimal temperature, throttling",
  913. avalon->drv->name, avalon->device_id);
  914. avalon_dec_freq(info);
  915. }
  916. } else if (info->auto_nonces >= (AVALON_AUTO_CYCLE * 19 / 20) &&
  917. info->auto_nonces <= (AVALON_AUTO_CYCLE * 21 / 20)) {
  918. int total = info->auto_nonces + info->auto_hw;
  919. /* Try to keep hw errors < 2% */
  920. if (info->auto_hw * 100 < total)
  921. avalon_inc_freq(info);
  922. else if (info->auto_hw * 66 > total)
  923. avalon_dec_freq(info);
  924. }
  925. avalon_reset_auto(info);
  926. mutex_unlock(&info->lock);
  927. }
  928. }
  929. static void *avalon_send_tasks(void *userdata)
  930. {
  931. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  932. struct avalon_info *info = avalon->device_data;
  933. const int avalon_get_work_count = info->miner_count;
  934. char threadname[24];
  935. snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
  936. RenameThread(threadname);
  937. while (likely(!avalon->shutdown)) {
  938. int start_count, end_count, i, j, ret;
  939. cgtimer_t ts_start;
  940. struct avalon_task at;
  941. bool idled = false;
  942. int64_t us_timeout;
  943. while (avalon_buffer_full(avalon))
  944. cgsleep_ms(40);
  945. avalon_adjust_freq(info, avalon);
  946. /* A full nonce range */
  947. us_timeout = 0x100000000ll / info->asic_count / info->frequency;
  948. cgsleep_prepare_r(&ts_start);
  949. mutex_lock(&info->qlock);
  950. start_count = avalon->work_array * avalon_get_work_count;
  951. end_count = start_count + avalon_get_work_count;
  952. for (i = start_count, j = 0; i < end_count; i++, j++) {
  953. if (avalon_buffer_full(avalon)) {
  954. applog(LOG_INFO,
  955. "%s%i: Buffer full after only %d of %d work queued",
  956. avalon->drv->name, avalon->device_id, j, avalon_get_work_count);
  957. break;
  958. }
  959. if (likely(j < avalon->queued && !info->overheat && avalon->works[i])) {
  960. avalon_init_task(&at, 0, 0, info->fan_pwm,
  961. info->timeout, info->asic_count,
  962. info->miner_count, 1, 0, info->frequency);
  963. avalon_create_task(&at, avalon->works[i]);
  964. info->auto_queued++;
  965. } else {
  966. int idle_freq = info->frequency;
  967. if (!info->idle++)
  968. idled = true;
  969. if (unlikely(info->overheat && opt_avalon_auto))
  970. idle_freq = AVALON_MIN_FREQUENCY;
  971. avalon_init_task(&at, 0, 0, info->fan_pwm,
  972. info->timeout, info->asic_count,
  973. info->miner_count, 1, 1, idle_freq);
  974. /* Reset the auto_queued count if we end up
  975. * idling any miners. */
  976. avalon_reset_auto(info);
  977. }
  978. ret = avalon_send_task(&at, avalon);
  979. if (unlikely(ret == AVA_SEND_ERROR)) {
  980. applog(LOG_ERR, "%s%i: Comms error(buffer)",
  981. avalon->drv->name, avalon->device_id);
  982. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  983. info->reset = true;
  984. break;
  985. }
  986. }
  987. avalon_rotate_array(avalon);
  988. mutex_unlock(&info->qlock);
  989. cgsem_post(&info->qsem);
  990. if (unlikely(idled)) {
  991. applog(LOG_WARNING, "%s%i: Idled %d miners",
  992. avalon->drv->name, avalon->device_id, idled);
  993. }
  994. /* Sleep how long it would take to complete a full nonce range
  995. * at the current frequency using the clock_nanosleep function
  996. * timed from before we started loading new work so it will
  997. * fall short of the full duration. */
  998. cgsleep_us_r(&ts_start, us_timeout);
  999. }
  1000. return NULL;
  1001. }
  1002. static void *bitburner_send_tasks(void *userdata)
  1003. {
  1004. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  1005. struct avalon_info *info = avalon->device_data;
  1006. const int avalon_get_work_count = info->miner_count;
  1007. char threadname[24];
  1008. snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
  1009. RenameThread(threadname);
  1010. while (likely(!avalon->shutdown)) {
  1011. int start_count, end_count, i, j, ret;
  1012. struct avalon_task at;
  1013. bool idled = false;
  1014. while (avalon_buffer_full(avalon))
  1015. cgsleep_ms(40);
  1016. avalon_adjust_freq(info, avalon);
  1017. /* Give other threads a chance to acquire qlock. */
  1018. i = 0;
  1019. do {
  1020. cgsleep_ms(40);
  1021. } while (!avalon->shutdown && i++ < 15
  1022. && avalon->queued < avalon_get_work_count);
  1023. mutex_lock(&info->qlock);
  1024. start_count = avalon->work_array * avalon_get_work_count;
  1025. end_count = start_count + avalon_get_work_count;
  1026. for (i = start_count, j = 0; i < end_count; i++, j++) {
  1027. while (avalon_buffer_full(avalon))
  1028. cgsleep_ms(40);
  1029. if (likely(j < avalon->queued && !info->overheat && avalon->works[i])) {
  1030. avalon_init_task(&at, 0, 0, info->fan_pwm,
  1031. info->timeout, info->asic_count,
  1032. info->miner_count, 1, 0, info->frequency);
  1033. avalon_create_task(&at, avalon->works[i]);
  1034. info->auto_queued++;
  1035. } else {
  1036. int idle_freq = info->frequency;
  1037. if (!info->idle++)
  1038. idled = true;
  1039. if (unlikely(info->overheat && opt_avalon_auto))
  1040. idle_freq = AVALON_MIN_FREQUENCY;
  1041. avalon_init_task(&at, 0, 0, info->fan_pwm,
  1042. info->timeout, info->asic_count,
  1043. info->miner_count, 1, 1, idle_freq);
  1044. /* Reset the auto_queued count if we end up
  1045. * idling any miners. */
  1046. avalon_reset_auto(info);
  1047. }
  1048. ret = bitburner_send_task(&at, avalon);
  1049. if (unlikely(ret == AVA_SEND_ERROR)) {
  1050. applog(LOG_ERR, "%s%i: Comms error(buffer)",
  1051. avalon->drv->name, avalon->device_id);
  1052. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  1053. info->reset = true;
  1054. break;
  1055. }
  1056. }
  1057. bitburner_rotate_array(avalon);
  1058. mutex_unlock(&info->qlock);
  1059. cgsem_post(&info->qsem);
  1060. if (unlikely(idled)) {
  1061. applog(LOG_WARNING, "%s%i: Idled %d miners",
  1062. avalon->drv->name, avalon->device_id, idled);
  1063. }
  1064. }
  1065. return NULL;
  1066. }
  1067. static bool avalon_prepare(struct thr_info *thr)
  1068. {
  1069. struct cgpu_info *avalon = thr->cgpu;
  1070. struct avalon_info *info = avalon->device_data;
  1071. int array_size = AVALON_ARRAY_SIZE;
  1072. void *(*write_thread_fn)(void *) = avalon_send_tasks;
  1073. if (is_bitburner(avalon)) {
  1074. array_size = BITBURNER_ARRAY_SIZE;
  1075. write_thread_fn = bitburner_send_tasks;
  1076. }
  1077. free(avalon->works);
  1078. avalon->works = calloc(info->miner_count * sizeof(struct work *),
  1079. array_size);
  1080. if (!avalon->works)
  1081. quit(1, "Failed to calloc avalon works in avalon_prepare");
  1082. info->thr = thr;
  1083. mutex_init(&info->lock);
  1084. mutex_init(&info->qlock);
  1085. cgsem_init(&info->qsem);
  1086. if (pthread_create(&info->read_thr, NULL, avalon_get_results, (void *)avalon))
  1087. quit(1, "Failed to create avalon read_thr");
  1088. if (pthread_create(&info->write_thr, NULL, write_thread_fn, (void *)avalon))
  1089. quit(1, "Failed to create avalon write_thr");
  1090. avalon_init(avalon);
  1091. return true;
  1092. }
  1093. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  1094. {
  1095. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  1096. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  1097. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  1098. info->temp0 = ar->temp0;
  1099. info->temp1 = ar->temp1;
  1100. info->temp2 = ar->temp2;
  1101. if (ar->temp0 & 0x80) {
  1102. ar->temp0 &= 0x7f;
  1103. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  1104. }
  1105. if (ar->temp1 & 0x80) {
  1106. ar->temp1 &= 0x7f;
  1107. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  1108. }
  1109. if (ar->temp2 & 0x80) {
  1110. ar->temp2 &= 0x7f;
  1111. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  1112. }
  1113. *temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
  1114. if (info->temp0 > info->temp_max)
  1115. info->temp_max = info->temp0;
  1116. if (info->temp1 > info->temp_max)
  1117. info->temp_max = info->temp1;
  1118. if (info->temp2 > info->temp_max)
  1119. info->temp_max = info->temp2;
  1120. }
  1121. static void temp_rise(struct avalon_info *info, int temp)
  1122. {
  1123. if (temp >= opt_avalon_temp + AVALON_TEMP_HYSTERESIS * 3) {
  1124. info->fan_pwm = AVALON_PWM_MAX;
  1125. return;
  1126. }
  1127. if (temp >= opt_avalon_temp + AVALON_TEMP_HYSTERESIS * 2)
  1128. info->fan_pwm += 10;
  1129. else if (temp > opt_avalon_temp)
  1130. info->fan_pwm += 5;
  1131. else if (temp >= opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  1132. info->fan_pwm += 1;
  1133. else
  1134. return;
  1135. if (info->fan_pwm > opt_avalon_fan_max)
  1136. info->fan_pwm = opt_avalon_fan_max;
  1137. }
  1138. static void temp_drop(struct avalon_info *info, int temp)
  1139. {
  1140. if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS * 3) {
  1141. info->fan_pwm = opt_avalon_fan_min;
  1142. return;
  1143. }
  1144. if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS * 2)
  1145. info->fan_pwm -= 10;
  1146. else if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  1147. info->fan_pwm -= 5;
  1148. else if (temp < opt_avalon_temp)
  1149. info->fan_pwm -= 1;
  1150. if (info->fan_pwm < opt_avalon_fan_min)
  1151. info->fan_pwm = opt_avalon_fan_min;
  1152. }
  1153. static inline void adjust_fan(struct avalon_info *info)
  1154. {
  1155. int temp_new;
  1156. temp_new = info->temp_sum / info->temp_history_count;
  1157. if (temp_new > info->temp_old)
  1158. temp_rise(info, temp_new);
  1159. else if (temp_new < info->temp_old)
  1160. temp_drop(info, temp_new);
  1161. else {
  1162. /* temp_new == info->temp_old */
  1163. if (temp_new > opt_avalon_temp)
  1164. temp_rise(info, temp_new);
  1165. else if (temp_new < opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  1166. temp_drop(info, temp_new);
  1167. }
  1168. info->temp_old = temp_new;
  1169. if (info->temp_old <= opt_avalon_temp)
  1170. info->optimal = true;
  1171. else
  1172. info->optimal = false;
  1173. }
  1174. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  1175. struct avalon_result *ar)
  1176. {
  1177. record_temp_fan(info, ar, &(avalon->temp));
  1178. applog(LOG_INFO,
  1179. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  1180. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  1181. info->fan0, info->fan1, info->fan2,
  1182. info->temp0, info->temp1, info->temp2, info->temp_max);
  1183. info->temp_history_index++;
  1184. info->temp_sum += avalon->temp;
  1185. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  1186. info->temp_history_index, info->temp_history_count, info->temp_old);
  1187. if (is_bitburner(avalon)) {
  1188. info->core_voltage = bitburner_get_core_voltage(avalon);
  1189. }
  1190. if (info->temp_history_index == info->temp_history_count) {
  1191. adjust_fan(info);
  1192. info->temp_history_index = 0;
  1193. info->temp_sum = 0;
  1194. }
  1195. if (unlikely(info->temp_old >= opt_avalon_overheat)) {
  1196. applog(LOG_WARNING, "%s%d overheat! Idling", avalon->drv->name, avalon->device_id);
  1197. info->overheat = true;
  1198. } else if (info->overheat && info->temp_old <= opt_avalon_temp) {
  1199. applog(LOG_WARNING, "%s%d cooled, restarting", avalon->drv->name, avalon->device_id);
  1200. info->overheat = false;
  1201. }
  1202. }
  1203. static void get_avalon_statline_before(char *buf, size_t bufsiz, struct cgpu_info *avalon)
  1204. {
  1205. struct avalon_info *info = avalon->device_data;
  1206. int lowfan = 10000;
  1207. if (is_bitburner(avalon)) {
  1208. int temp = info->temp0;
  1209. if (info->temp2 > temp)
  1210. temp = info->temp2;
  1211. if (temp > 99)
  1212. temp = 99;
  1213. if (temp < 0)
  1214. temp = 0;
  1215. tailsprintf(buf, bufsiz, "%2dC %3d %4dmV | ", temp, info->frequency, info->core_voltage);
  1216. } else {
  1217. /* Find the lowest fan speed of the ASIC cooling fans. */
  1218. if (info->fan1 >= 0 && info->fan1 < lowfan)
  1219. lowfan = info->fan1;
  1220. if (info->fan2 >= 0 && info->fan2 < lowfan)
  1221. lowfan = info->fan2;
  1222. tailsprintf(buf, bufsiz, "%2dC/%3dC %04dR | ", info->temp0, info->temp2, lowfan);
  1223. }
  1224. }
  1225. /* We use a replacement algorithm to only remove references to work done from
  1226. * the buffer when we need the extra space for new work. */
  1227. static bool avalon_fill(struct cgpu_info *avalon)
  1228. {
  1229. struct avalon_info *info = avalon->device_data;
  1230. int subid, slot, mc;
  1231. struct work *work;
  1232. bool ret = true;
  1233. mc = info->miner_count;
  1234. mutex_lock(&info->qlock);
  1235. if (avalon->queued >= mc)
  1236. goto out_unlock;
  1237. work = get_queued(avalon);
  1238. if (unlikely(!work)) {
  1239. ret = false;
  1240. goto out_unlock;
  1241. }
  1242. subid = avalon->queued++;
  1243. work->subid = subid;
  1244. slot = avalon->work_array * mc + subid;
  1245. if (likely(avalon->works[slot]))
  1246. work_completed(avalon, avalon->works[slot]);
  1247. avalon->works[slot] = work;
  1248. if (avalon->queued < mc)
  1249. ret = false;
  1250. out_unlock:
  1251. mutex_unlock(&info->qlock);
  1252. return ret;
  1253. }
  1254. static int64_t avalon_scanhash(struct thr_info *thr)
  1255. {
  1256. struct cgpu_info *avalon = thr->cgpu;
  1257. struct avalon_info *info = avalon->device_data;
  1258. const int miner_count = info->miner_count;
  1259. int64_t hash_count, ms_timeout;
  1260. /* Half nonce range */
  1261. ms_timeout = 0x80000000ll / info->asic_count / info->frequency / 1000;
  1262. /* Wait until avalon_send_tasks signals us that it has completed
  1263. * sending its work or a full nonce range timeout has occurred. We use
  1264. * cgsems to never miss a wakeup. */
  1265. cgsem_mswait(&info->qsem, ms_timeout);
  1266. mutex_lock(&info->lock);
  1267. hash_count = 0xffffffffull * (uint64_t)info->nonces;
  1268. avalon->results += info->nonces + info->idle;
  1269. if (avalon->results > miner_count)
  1270. avalon->results = miner_count;
  1271. if (!info->reset)
  1272. avalon->results--;
  1273. info->nonces = info->idle = 0;
  1274. mutex_unlock(&info->lock);
  1275. /* Check for nothing but consecutive bad results or consistently less
  1276. * results than we should be getting and reset the FPGA if necessary */
  1277. if (!is_bitburner(avalon)) {
  1278. if (avalon->results < -miner_count && !info->reset) {
  1279. applog(LOG_ERR, "%s%d: Result return rate low, resetting!",
  1280. avalon->drv->name, avalon->device_id);
  1281. info->reset = true;
  1282. }
  1283. }
  1284. if (unlikely(avalon->usbinfo.nodev)) {
  1285. applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread",
  1286. avalon->drv->name, avalon->device_id);
  1287. avalon->shutdown = true;
  1288. }
  1289. /* This hashmeter is just a utility counter based on returned shares */
  1290. return hash_count;
  1291. }
  1292. static void avalon_flush_work(struct cgpu_info *avalon)
  1293. {
  1294. struct avalon_info *info = avalon->device_data;
  1295. mutex_lock(&info->qlock);
  1296. /* Will overwrite any work queued */
  1297. avalon->queued = 0;
  1298. mutex_unlock(&info->qlock);
  1299. /* Signal main loop we need more work */
  1300. cgsem_post(&info->qsem);
  1301. }
  1302. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  1303. {
  1304. struct api_data *root = NULL;
  1305. struct avalon_info *info = cgpu->device_data;
  1306. char buf[64];
  1307. int i;
  1308. double hwp = (cgpu->hw_errors + cgpu->diff1) ?
  1309. (double)(cgpu->hw_errors) / (double)(cgpu->hw_errors + cgpu->diff1) : 0;
  1310. root = api_add_int(root, "baud", &(info->baud), false);
  1311. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  1312. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  1313. root = api_add_int(root, "timeout", &(info->timeout), false);
  1314. root = api_add_int(root, "frequency", &(info->frequency), false);
  1315. root = api_add_int(root, "fan1", &(info->fan0), false);
  1316. root = api_add_int(root, "fan2", &(info->fan1), false);
  1317. root = api_add_int(root, "fan3", &(info->fan2), false);
  1318. root = api_add_int(root, "temp1", &(info->temp0), false);
  1319. root = api_add_int(root, "temp2", &(info->temp1), false);
  1320. root = api_add_int(root, "temp3", &(info->temp2), false);
  1321. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  1322. root = api_add_percent(root, "Device Hardware%", &hwp, true);
  1323. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  1324. for (i = 0; i < info->miner_count; i++) {
  1325. char mcw[24];
  1326. sprintf(mcw, "match_work_count%d", i + 1);
  1327. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  1328. }
  1329. if (is_bitburner(cgpu)) {
  1330. root = api_add_int(root, "core_voltage", &(info->core_voltage), false);
  1331. snprintf(buf, sizeof(buf), "%"PRIu8".%"PRIu8".%"PRIu8,
  1332. info->version1, info->version2, info->version3);
  1333. root = api_add_string(root, "version", buf, true);
  1334. }
  1335. root = api_add_uint32(root, "Controller Version", &(info->ctlr_ver), false);
  1336. return root;
  1337. }
  1338. static void avalon_shutdown(struct thr_info *thr)
  1339. {
  1340. struct cgpu_info *avalon = thr->cgpu;
  1341. struct avalon_info *info = avalon->device_data;
  1342. pthread_join(info->read_thr, NULL);
  1343. pthread_join(info->write_thr, NULL);
  1344. avalon_running_reset(avalon, info);
  1345. cgsem_destroy(&info->qsem);
  1346. mutex_destroy(&info->qlock);
  1347. mutex_destroy(&info->lock);
  1348. free(avalon->works);
  1349. avalon->works = NULL;
  1350. }
  1351. static char *avalon_set_device(struct cgpu_info *avalon, char *option, char *setting, char *replybuf)
  1352. {
  1353. int val;
  1354. if (strcasecmp(option, "help") == 0) {
  1355. sprintf(replybuf, "freq: range %d-%d millivolts: range %d-%d",
  1356. AVALON_MIN_FREQUENCY, AVALON_MAX_FREQUENCY,
  1357. BITBURNER_MIN_COREMV, BITBURNER_MAX_COREMV);
  1358. return replybuf;
  1359. }
  1360. if (strcasecmp(option, "millivolts") == 0 || strcasecmp(option, "mv") == 0) {
  1361. if (!is_bitburner(avalon)) {
  1362. sprintf(replybuf, "%s cannot set millivolts", avalon->drv->name);
  1363. return replybuf;
  1364. }
  1365. if (!setting || !*setting) {
  1366. sprintf(replybuf, "missing millivolts setting");
  1367. return replybuf;
  1368. }
  1369. val = atoi(setting);
  1370. if (val < BITBURNER_MIN_COREMV || val > BITBURNER_MAX_COREMV) {
  1371. sprintf(replybuf, "invalid millivolts: '%s' valid range %d-%d",
  1372. setting, BITBURNER_MIN_COREMV, BITBURNER_MAX_COREMV);
  1373. return replybuf;
  1374. }
  1375. if (bitburner_set_core_voltage(avalon, val))
  1376. return NULL;
  1377. else {
  1378. sprintf(replybuf, "Set millivolts failed");
  1379. return replybuf;
  1380. }
  1381. }
  1382. if (strcasecmp(option, "freq") == 0) {
  1383. if (!setting || !*setting) {
  1384. sprintf(replybuf, "missing freq setting");
  1385. return replybuf;
  1386. }
  1387. val = atoi(setting);
  1388. if (val < AVALON_MIN_FREQUENCY || val > AVALON_MAX_FREQUENCY) {
  1389. sprintf(replybuf, "invalid freq: '%s' valid range %d-%d",
  1390. setting, AVALON_MIN_FREQUENCY, AVALON_MAX_FREQUENCY);
  1391. return replybuf;
  1392. }
  1393. avalon_set_freq(avalon, val);
  1394. return NULL;
  1395. }
  1396. sprintf(replybuf, "Unknown option: %s", option);
  1397. return replybuf;
  1398. }
  1399. struct device_drv avalon_drv = {
  1400. .drv_id = DRIVER_avalon,
  1401. .dname = "avalon",
  1402. .name = "AVA",
  1403. .drv_detect = avalon_detect,
  1404. .thread_prepare = avalon_prepare,
  1405. .hash_work = hash_queued_work,
  1406. .queue_full = avalon_fill,
  1407. .scanwork = avalon_scanhash,
  1408. .flush_work = avalon_flush_work,
  1409. .get_api_stats = avalon_api_stats,
  1410. .get_statline_before = get_avalon_statline_before,
  1411. .set_device = avalon_set_device,
  1412. .reinit_device = avalon_init,
  1413. .thread_shutdown = avalon_shutdown,
  1414. };