driver-avalon.c 43 KB

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