driver-avalon.c 44 KB

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