driver-avalon.c 44 KB

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