driver-avalon.c 45 KB

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