driver-avalon.c 44 KB

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