driver-avalon.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  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 <dirent.h>
  19. #include <unistd.h>
  20. #ifndef WIN32
  21. #include <sys/select.h>
  22. #include <termios.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #ifndef O_CLOEXEC
  26. #define O_CLOEXEC 0
  27. #endif
  28. #else
  29. #include "compat.h"
  30. #include <windows.h>
  31. #include <io.h>
  32. #endif
  33. #include "elist.h"
  34. #include "miner.h"
  35. #include "usbutils.h"
  36. #include "driver-avalon.h"
  37. #include "hexdump.c"
  38. #include "util.h"
  39. int opt_avalon_temp = AVALON_TEMP_TARGET;
  40. int opt_avalon_overheat = AVALON_TEMP_OVERHEAT;
  41. int opt_avalon_fan_min = AVALON_DEFAULT_FAN_MIN_PWM;
  42. int opt_avalon_fan_max = AVALON_DEFAULT_FAN_MAX_PWM;
  43. int opt_avalon_freq_min = AVALON_MIN_FREQUENCY;
  44. int opt_avalon_freq_max = AVALON_MAX_FREQUENCY;
  45. int opt_bitburner_core_voltage = BITBURNER_DEFAULT_CORE_VOLTAGE;
  46. bool opt_avalon_auto;
  47. static int option_offset = -1;
  48. struct device_drv avalon_drv;
  49. static int avalon_init_task(struct avalon_task *at,
  50. uint8_t reset, uint8_t ff, uint8_t fan,
  51. uint8_t timeout, uint8_t asic_num,
  52. uint8_t miner_num, uint8_t nonce_elf,
  53. uint8_t gate_miner, int frequency)
  54. {
  55. uint16_t *lefreq16;
  56. uint8_t *buf;
  57. static bool first = true;
  58. if (unlikely(!at))
  59. return -1;
  60. if (unlikely(timeout <= 0 || asic_num <= 0 || miner_num <= 0))
  61. return -1;
  62. memset(at, 0, sizeof(struct avalon_task));
  63. if (unlikely(reset)) {
  64. at->reset = 1;
  65. at->fan_eft = 1;
  66. at->timer_eft = 1;
  67. first = true;
  68. }
  69. at->flush_fifo = (ff ? 1 : 0);
  70. at->fan_eft = (fan ? 1 : 0);
  71. if (unlikely(first && !at->reset)) {
  72. at->fan_eft = 1;
  73. at->timer_eft = 1;
  74. first = false;
  75. }
  76. at->fan_pwm_data = (fan ? fan : AVALON_DEFAULT_FAN_MAX_PWM);
  77. at->timeout_data = timeout;
  78. at->asic_num = asic_num;
  79. at->miner_num = miner_num;
  80. at->nonce_elf = nonce_elf;
  81. at->gate_miner_elf = 1;
  82. at->asic_pll = 1;
  83. if (unlikely(gate_miner)) {
  84. at-> gate_miner = 1;
  85. at->asic_pll = 0;
  86. }
  87. buf = (uint8_t *)at;
  88. buf[5] = 0x00;
  89. buf[8] = 0x74;
  90. buf[9] = 0x01;
  91. buf[10] = 0x00;
  92. buf[11] = 0x00;
  93. lefreq16 = (uint16_t *)&buf[6];
  94. *lefreq16 = htole16(frequency * 8);
  95. return 0;
  96. }
  97. static inline void avalon_create_task(struct avalon_task *at,
  98. struct work *work)
  99. {
  100. memcpy(at->midstate, work->midstate, 32);
  101. memcpy(at->data, work->data + 64, 12);
  102. }
  103. static int avalon_write(struct cgpu_info *avalon, char *buf, ssize_t len, int ep)
  104. {
  105. int err, amount;
  106. err = usb_write(avalon, buf, len, &amount, ep);
  107. applog(LOG_DEBUG, "%s%i: usb_write got err %d", avalon->drv->name,
  108. avalon->device_id, err);
  109. if (unlikely(err != 0)) {
  110. applog(LOG_WARNING, "usb_write error on avalon_write");
  111. return AVA_SEND_ERROR;
  112. }
  113. if (amount != len) {
  114. applog(LOG_WARNING, "usb_write length mismatch on avalon_write");
  115. return AVA_SEND_ERROR;
  116. }
  117. return AVA_SEND_OK;
  118. }
  119. static int avalon_send_task(const struct avalon_task *at, struct cgpu_info *avalon)
  120. {
  121. uint8_t buf[AVALON_WRITE_SIZE + 4 * AVALON_DEFAULT_ASIC_NUM];
  122. int delay, ret, i, ep = C_AVALON_TASK;
  123. struct avalon_info *info;
  124. uint32_t nonce_range;
  125. size_t nr_len;
  126. if (at->nonce_elf)
  127. nr_len = AVALON_WRITE_SIZE + 4 * at->asic_num;
  128. else
  129. nr_len = AVALON_WRITE_SIZE;
  130. memcpy(buf, at, AVALON_WRITE_SIZE);
  131. if (at->nonce_elf) {
  132. nonce_range = (uint32_t)0xffffffff / at->asic_num;
  133. for (i = 0; i < at->asic_num; i++) {
  134. buf[AVALON_WRITE_SIZE + (i * 4) + 3] =
  135. (i * nonce_range & 0xff000000) >> 24;
  136. buf[AVALON_WRITE_SIZE + (i * 4) + 2] =
  137. (i * nonce_range & 0x00ff0000) >> 16;
  138. buf[AVALON_WRITE_SIZE + (i * 4) + 1] =
  139. (i * nonce_range & 0x0000ff00) >> 8;
  140. buf[AVALON_WRITE_SIZE + (i * 4) + 0] =
  141. (i * nonce_range & 0x000000ff) >> 0;
  142. }
  143. }
  144. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  145. uint8_t tt = 0;
  146. tt = (buf[0] & 0x0f) << 4;
  147. tt |= ((buf[0] & 0x10) ? (1 << 3) : 0);
  148. tt |= ((buf[0] & 0x20) ? (1 << 2) : 0);
  149. tt |= ((buf[0] & 0x40) ? (1 << 1) : 0);
  150. tt |= ((buf[0] & 0x80) ? (1 << 0) : 0);
  151. buf[0] = tt;
  152. tt = (buf[4] & 0x0f) << 4;
  153. tt |= ((buf[4] & 0x10) ? (1 << 3) : 0);
  154. tt |= ((buf[4] & 0x20) ? (1 << 2) : 0);
  155. tt |= ((buf[4] & 0x40) ? (1 << 1) : 0);
  156. tt |= ((buf[4] & 0x80) ? (1 << 0) : 0);
  157. buf[4] = tt;
  158. #endif
  159. info = avalon->device_data;
  160. delay = nr_len * 10 * 1000000;
  161. delay = delay / info->baud;
  162. if (at->reset) {
  163. ep = C_AVALON_RESET;
  164. nr_len = 1;
  165. }
  166. if (opt_debug) {
  167. applog(LOG_DEBUG, "Avalon: Sent(%u):", (unsigned int)nr_len);
  168. hexdump(buf, nr_len);
  169. }
  170. ret = avalon_write(avalon, (char *)buf, nr_len, ep);
  171. delay += 4000;
  172. nusleep(delay);
  173. applog(LOG_DEBUG, "Avalon: Sent: Buffer delay: %dus", delay);
  174. return ret;
  175. }
  176. static bool avalon_decode_nonce(struct thr_info *thr, struct cgpu_info *avalon,
  177. struct avalon_info *info, struct avalon_result *ar,
  178. struct work *work)
  179. {
  180. uint32_t nonce;
  181. info = avalon->device_data;
  182. info->matching_work[work->subid]++;
  183. nonce = htole32(ar->nonce);
  184. applog(LOG_DEBUG, "Avalon: nonce = %0x08x", nonce);
  185. return submit_nonce(thr, work, nonce);
  186. }
  187. /* Wait until the ftdi chip returns a CTS saying we can send more data. */
  188. static void wait_avalon_ready(struct cgpu_info *avalon)
  189. {
  190. while (avalon_buffer_full(avalon)) {
  191. nmsleep(40);
  192. }
  193. }
  194. #define AVALON_CTS (1 << 4)
  195. static inline bool avalon_cts(char c)
  196. {
  197. return (c & AVALON_CTS);
  198. }
  199. static int avalon_read(struct cgpu_info *avalon, unsigned char *buf,
  200. size_t bufsize, int timeout, int ep)
  201. {
  202. struct avalon_info *info = avalon->device_data;
  203. size_t total = 0, readsize = bufsize + 2;
  204. char readbuf[AVALON_READBUF_SIZE];
  205. int err, amount, ofs = 2, cp;
  206. err = usb_read_once_timeout(avalon, readbuf, readsize, &amount, timeout, ep);
  207. applog(LOG_DEBUG, "%s%i: Get avalon read got err %d",
  208. avalon->drv->name, avalon->device_id, err);
  209. if (amount < 2)
  210. goto out;
  211. /* Use the fact that we're reading the status with the buffer to tell
  212. * the write thread it should send more work without needing to call
  213. * avalon_buffer_full directly. */
  214. if (avalon_cts(buf[0]))
  215. cgsem_post(&info->write_sem);
  216. /* The first 2 of every 64 bytes are status on FTDIRL */
  217. while (amount > 2) {
  218. cp = amount - 2;
  219. if (cp > 62)
  220. cp = 62;
  221. memcpy(&buf[total], &readbuf[ofs], cp);
  222. total += cp;
  223. amount -= cp + 2;
  224. ofs += 64;
  225. }
  226. out:
  227. return total;
  228. }
  229. static int avalon_reset(struct cgpu_info *avalon, bool initial)
  230. {
  231. struct avalon_result ar;
  232. int ret, i, spare;
  233. struct avalon_task at;
  234. uint8_t *buf, *tmp;
  235. struct timespec p;
  236. /* Send reset, then check for result */
  237. avalon_init_task(&at, 1, 0,
  238. AVALON_DEFAULT_FAN_MAX_PWM,
  239. AVALON_DEFAULT_TIMEOUT,
  240. AVALON_DEFAULT_ASIC_NUM,
  241. AVALON_DEFAULT_MINER_NUM,
  242. 0, 0,
  243. AVALON_DEFAULT_FREQUENCY);
  244. wait_avalon_ready(avalon);
  245. ret = avalon_send_task(&at, avalon);
  246. if (unlikely(ret == AVA_SEND_ERROR))
  247. return -1;
  248. if (!initial) {
  249. applog(LOG_ERR, "%s%d reset sequence sent", avalon->drv->name, avalon->device_id);
  250. return 0;
  251. }
  252. ret = avalon_read(avalon, (unsigned char *)&ar, AVALON_READ_SIZE,
  253. AVALON_RESET_TIMEOUT, C_GET_AVALON_RESET);
  254. /* What do these sleeps do?? */
  255. p.tv_sec = 0;
  256. p.tv_nsec = AVALON_RESET_PITCH;
  257. nanosleep(&p, NULL);
  258. /* Look for the first occurrence of 0xAA, the reset response should be:
  259. * AA 55 AA 55 00 00 00 00 00 00 */
  260. spare = ret - 10;
  261. buf = tmp = (uint8_t *)&ar;
  262. if (opt_debug) {
  263. applog(LOG_DEBUG, "%s%d reset: get:", avalon->drv->name, avalon->device_id);
  264. hexdump(tmp, AVALON_READ_SIZE);
  265. }
  266. for (i = 0; i <= spare; i++) {
  267. buf = &tmp[i];
  268. if (buf[0] == 0xAA)
  269. break;
  270. }
  271. i = 0;
  272. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  273. buf[2] == 0xAA && buf[3] == 0x55) {
  274. for (i = 4; i < 11; i++)
  275. if (buf[i] != 0)
  276. break;
  277. }
  278. if (i != 11) {
  279. applog(LOG_ERR, "%s%d: Reset failed! not an Avalon?"
  280. " (%d: %02x %02x %02x %02x)", avalon->drv->name, avalon->device_id,
  281. i, buf[0], buf[1], buf[2], buf[3]);
  282. /* FIXME: return 1; */
  283. } else
  284. applog(LOG_WARNING, "%s%d: Reset succeeded",
  285. avalon->drv->name, avalon->device_id);
  286. return 0;
  287. }
  288. static bool get_options(int this_option_offset, int *baud, int *miner_count,
  289. int *asic_count, int *timeout, int *frequency)
  290. {
  291. char buf[BUFSIZ+1];
  292. char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
  293. size_t max;
  294. int i, tmp;
  295. if (opt_avalon_options == NULL)
  296. buf[0] = '\0';
  297. else {
  298. ptr = opt_avalon_options;
  299. for (i = 0; i < this_option_offset; i++) {
  300. comma = strchr(ptr, ',');
  301. if (comma == NULL)
  302. break;
  303. ptr = comma + 1;
  304. }
  305. comma = strchr(ptr, ',');
  306. if (comma == NULL)
  307. max = strlen(ptr);
  308. else
  309. max = comma - ptr;
  310. if (max > BUFSIZ)
  311. max = BUFSIZ;
  312. strncpy(buf, ptr, max);
  313. buf[max] = '\0';
  314. }
  315. if (!(*buf))
  316. return false;
  317. colon = strchr(buf, ':');
  318. if (colon)
  319. *(colon++) = '\0';
  320. tmp = atoi(buf);
  321. switch (tmp) {
  322. case 115200:
  323. *baud = 115200;
  324. break;
  325. case 57600:
  326. *baud = 57600;
  327. break;
  328. case 38400:
  329. *baud = 38400;
  330. break;
  331. case 19200:
  332. *baud = 19200;
  333. break;
  334. default:
  335. quit(1, "Invalid avalon-options for baud (%s) "
  336. "must be 115200, 57600, 38400 or 19200", buf);
  337. }
  338. if (colon && *colon) {
  339. colon2 = strchr(colon, ':');
  340. if (colon2)
  341. *(colon2++) = '\0';
  342. if (*colon) {
  343. tmp = atoi(colon);
  344. if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
  345. *miner_count = tmp;
  346. } else {
  347. quit(1, "Invalid avalon-options for "
  348. "miner_count (%s) must be 1 ~ %d",
  349. colon, AVALON_DEFAULT_MINER_NUM);
  350. }
  351. }
  352. if (colon2 && *colon2) {
  353. colon3 = strchr(colon2, ':');
  354. if (colon3)
  355. *(colon3++) = '\0';
  356. tmp = atoi(colon2);
  357. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  358. *asic_count = tmp;
  359. else {
  360. quit(1, "Invalid avalon-options for "
  361. "asic_count (%s) must be 1 ~ %d",
  362. colon2, AVALON_DEFAULT_ASIC_NUM);
  363. }
  364. if (colon3 && *colon3) {
  365. colon4 = strchr(colon3, ':');
  366. if (colon4)
  367. *(colon4++) = '\0';
  368. tmp = atoi(colon3);
  369. if (tmp > 0 && tmp <= 0xff)
  370. *timeout = tmp;
  371. else {
  372. quit(1, "Invalid avalon-options for "
  373. "timeout (%s) must be 1 ~ %d",
  374. colon3, 0xff);
  375. }
  376. if (colon4 && *colon4) {
  377. tmp = atoi(colon4);
  378. if (tmp < AVALON_MIN_FREQUENCY || tmp > AVALON_MAX_FREQUENCY) {
  379. quit(1, "Invalid avalon-options for frequency, must be %d <= frequency <= %d",
  380. AVALON_MIN_FREQUENCY, AVALON_MAX_FREQUENCY);
  381. }
  382. *frequency = tmp;
  383. }
  384. }
  385. }
  386. }
  387. return true;
  388. }
  389. char *set_avalon_fan(char *arg)
  390. {
  391. int val1, val2, ret;
  392. ret = sscanf(arg, "%d-%d", &val1, &val2);
  393. if (ret < 1)
  394. return "No values passed to avalon-fan";
  395. if (ret == 1)
  396. val2 = val1;
  397. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  398. return "Invalid value passed to avalon-fan";
  399. opt_avalon_fan_min = val1 * AVALON_PWM_MAX / 100;
  400. opt_avalon_fan_max = val2 * AVALON_PWM_MAX / 100;
  401. return NULL;
  402. }
  403. char *set_avalon_freq(char *arg)
  404. {
  405. int val1, val2, ret;
  406. ret = sscanf(arg, "%d-%d", &val1, &val2);
  407. if (ret < 1)
  408. return "No values passed to avalon-freq";
  409. if (ret == 1)
  410. val2 = val1;
  411. if (val1 < AVALON_MIN_FREQUENCY || val1 > AVALON_MAX_FREQUENCY ||
  412. val2 < AVALON_MIN_FREQUENCY || val2 > AVALON_MAX_FREQUENCY ||
  413. val2 < val1)
  414. return "Invalid value passed to avalon-freq";
  415. opt_avalon_freq_min = val1;
  416. opt_avalon_freq_max = val2;
  417. return NULL;
  418. }
  419. static void avalon_idle(struct cgpu_info *avalon, struct avalon_info *info)
  420. {
  421. int i;
  422. wait_avalon_ready(avalon);
  423. /* Send idle to all miners */
  424. for (i = 0; i < info->miner_count; i++) {
  425. struct avalon_task at;
  426. if (unlikely(avalon_buffer_full(avalon)))
  427. break;
  428. info->idle++;
  429. avalon_init_task(&at, 0, 0, info->fan_pwm, info->timeout,
  430. info->asic_count, info->miner_count, 1, 1,
  431. info->frequency);
  432. avalon_send_task(&at, avalon);
  433. }
  434. applog(LOG_WARNING, "%s%i: Idling %d miners", avalon->drv->name, avalon->device_id, i);
  435. wait_avalon_ready(avalon);
  436. }
  437. static void avalon_initialise(struct cgpu_info *avalon)
  438. {
  439. int err, interface;
  440. if (avalon->usbinfo.nodev)
  441. return;
  442. interface = avalon->usbdev->found->interface;
  443. // Reset
  444. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  445. FTDI_VALUE_RESET, interface, C_RESET);
  446. applog(LOG_DEBUG, "%s%i: reset got err %d",
  447. avalon->drv->name, avalon->device_id, err);
  448. if (avalon->usbinfo.nodev)
  449. return;
  450. // Set latency
  451. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY,
  452. AVALON_LATENCY, interface, C_LATENCY);
  453. applog(LOG_DEBUG, "%s%i: latency got err %d",
  454. avalon->drv->name, avalon->device_id, err);
  455. if (avalon->usbinfo.nodev)
  456. return;
  457. // Set data
  458. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  459. FTDI_VALUE_DATA_AVA, interface, C_SETDATA);
  460. applog(LOG_DEBUG, "%s%i: data got err %d",
  461. avalon->drv->name, avalon->device_id, err);
  462. if (avalon->usbinfo.nodev)
  463. return;
  464. // Set the baud
  465. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_AVA,
  466. (FTDI_INDEX_BAUD_AVA & 0xff00) | interface,
  467. C_SETBAUD);
  468. applog(LOG_DEBUG, "%s%i: setbaud got err %d",
  469. avalon->drv->name, avalon->device_id, err);
  470. if (avalon->usbinfo.nodev)
  471. return;
  472. // Set Modem Control
  473. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  474. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  475. applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
  476. avalon->drv->name, avalon->device_id, err);
  477. if (avalon->usbinfo.nodev)
  478. return;
  479. // Set Flow Control
  480. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  481. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  482. applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
  483. avalon->drv->name, avalon->device_id, err);
  484. if (avalon->usbinfo.nodev)
  485. return;
  486. /* Avalon repeats the following */
  487. // Set Modem Control
  488. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  489. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  490. applog(LOG_DEBUG, "%s%i: setmodemctrl 2 got err %d",
  491. avalon->drv->name, avalon->device_id, err);
  492. if (avalon->usbinfo.nodev)
  493. return;
  494. // Set Flow Control
  495. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  496. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  497. applog(LOG_DEBUG, "%s%i: setflowctrl 2 got err %d",
  498. avalon->drv->name, avalon->device_id, err);
  499. }
  500. static void bitburner_set_core_voltage(struct cgpu_info *avalon, int core_voltage)
  501. {
  502. uint8_t buf[2];
  503. int err;
  504. if (usb_ident(avalon) == IDENT_BTB) {
  505. buf[0] = (uint8_t)core_voltage;
  506. buf[1] = (uint8_t)(core_voltage >> 8);
  507. err = usb_transfer_data(avalon, FTDI_TYPE_OUT, BITBURNER_REQUEST,
  508. BITBURNER_VALUE, BITBURNER_INDEX_SET_VOLTAGE,
  509. (uint32_t *)buf, sizeof(buf), C_BB_SET_VOLTAGE);
  510. if (unlikely(err < 0)) {
  511. applog(LOG_ERR, "%s%i: SetCoreVoltage failed: err = %d",
  512. avalon->drv->name, avalon->device_id, err);
  513. } else {
  514. applog(LOG_WARNING, "%s%i: Core voltage set to %d millivolts",
  515. avalon->drv->name, avalon->device_id,
  516. core_voltage);
  517. }
  518. }
  519. }
  520. static int bitburner_get_core_voltage(struct cgpu_info *avalon)
  521. {
  522. uint8_t buf[2];
  523. int err;
  524. int amount;
  525. if (usb_ident(avalon) == IDENT_BTB) {
  526. err = usb_transfer_read(avalon, FTDI_TYPE_IN, BITBURNER_REQUEST,
  527. BITBURNER_VALUE, BITBURNER_INDEX_GET_VOLTAGE,
  528. (char *)buf, sizeof(buf), &amount,
  529. C_BB_GET_VOLTAGE);
  530. if (unlikely(err != 0 || amount != 2)) {
  531. applog(LOG_ERR, "%s%i: GetCoreVoltage failed: err = %d, amount = %d",
  532. avalon->drv->name, avalon->device_id, err, amount);
  533. return 0;
  534. } else {
  535. return (int)(buf[0] + ((unsigned int)buf[1] << 8));
  536. }
  537. } else {
  538. return 0;
  539. }
  540. }
  541. static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found)
  542. {
  543. int baud, miner_count, asic_count, timeout, frequency = 0;
  544. int this_option_offset = ++option_offset;
  545. struct avalon_info *info;
  546. struct cgpu_info *avalon;
  547. bool configured;
  548. int ret;
  549. avalon = usb_alloc_cgpu(&avalon_drv, AVALON_MINER_THREADS);
  550. configured = get_options(this_option_offset, &baud, &miner_count,
  551. &asic_count, &timeout, &frequency);
  552. if (!usb_init(avalon, dev, found))
  553. goto shin;
  554. /* Even though this is an FTDI type chip, we want to do the parsing
  555. * all ourselves so set it to std usb type */
  556. avalon->usbdev->usb_type = USB_TYPE_STD;
  557. avalon->usbdev->PrefPacketSize = AVALON_USB_PACKETSIZE;
  558. /* We have a real Avalon! */
  559. avalon_initialise(avalon);
  560. avalon->device_data = calloc(sizeof(struct avalon_info), 1);
  561. if (unlikely(!(avalon->device_data)))
  562. quit(1, "Failed to calloc avalon_info data");
  563. info = avalon->device_data;
  564. if (configured) {
  565. info->baud = baud;
  566. info->miner_count = miner_count;
  567. info->asic_count = asic_count;
  568. info->timeout = timeout;
  569. info->frequency = frequency;
  570. } else {
  571. info->baud = AVALON_IO_SPEED;
  572. info->miner_count = AVALON_DEFAULT_MINER_NUM;
  573. info->asic_count = AVALON_DEFAULT_ASIC_NUM;
  574. info->timeout = AVALON_DEFAULT_TIMEOUT;
  575. info->frequency = AVALON_DEFAULT_FREQUENCY;
  576. }
  577. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  578. info->temp_max = 0;
  579. /* This is for check the temp/fan every 3~4s */
  580. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  581. if (info->temp_history_count <= 0)
  582. info->temp_history_count = 1;
  583. info->temp_history_index = 0;
  584. info->temp_sum = 0;
  585. info->temp_old = 0;
  586. if (!add_cgpu(avalon))
  587. goto unshin;
  588. ret = avalon_reset(avalon, true);
  589. if (ret && !configured)
  590. goto unshin;
  591. update_usb_stats(avalon);
  592. avalon_idle(avalon, info);
  593. applog(LOG_DEBUG, "Avalon Detected: %s "
  594. "(miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  595. avalon->device_path, info->miner_count, info->asic_count, info->timeout,
  596. info->frequency);
  597. if (usb_ident(avalon) == IDENT_BTB &&
  598. opt_bitburner_core_voltage != BITBURNER_DEFAULT_CORE_VOLTAGE)
  599. bitburner_set_core_voltage(avalon, opt_bitburner_core_voltage);
  600. return true;
  601. unshin:
  602. usb_uninit(avalon);
  603. shin:
  604. free(avalon->device_data);
  605. avalon->device_data = NULL;
  606. avalon = usb_free_cgpu(avalon);
  607. return false;
  608. }
  609. static void avalon_detect(void)
  610. {
  611. usb_detect(&avalon_drv, avalon_detect_one);
  612. }
  613. static void avalon_init(struct cgpu_info *avalon)
  614. {
  615. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  616. }
  617. static struct work *avalon_valid_result(struct cgpu_info *avalon, struct avalon_result *ar)
  618. {
  619. return find_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
  620. (char *)ar->data, 64, 12);
  621. }
  622. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  623. struct avalon_result *ar);
  624. static void avalon_inc_nvw(struct avalon_info *info, struct thr_info *thr)
  625. {
  626. applog(LOG_INFO, "%s%d: No matching work - HW error",
  627. thr->cgpu->drv->name, thr->cgpu->device_id);
  628. inc_hw_errors(thr);
  629. info->no_matching_work++;
  630. }
  631. static void avalon_parse_results(struct cgpu_info *avalon, struct avalon_info *info,
  632. struct thr_info *thr, char *buf, int *offset)
  633. {
  634. int i, spare = *offset - AVALON_READ_SIZE;
  635. bool found = false;
  636. for (i = 0; i <= spare; i++) {
  637. struct avalon_result *ar;
  638. struct work *work;
  639. ar = (struct avalon_result *)&buf[i];
  640. work = avalon_valid_result(avalon, ar);
  641. if (work) {
  642. bool gettemp = false;
  643. found = true;
  644. if (avalon_decode_nonce(thr, avalon, info, ar, work)) {
  645. mutex_lock(&info->lock);
  646. if (!info->nonces++)
  647. gettemp = true;
  648. info->auto_nonces++;
  649. mutex_unlock(&info->lock);
  650. } else if (opt_avalon_auto) {
  651. mutex_lock(&info->lock);
  652. info->auto_hw++;
  653. mutex_unlock(&info->lock);
  654. }
  655. if (gettemp)
  656. avalon_update_temps(avalon, info, ar);
  657. break;
  658. }
  659. }
  660. if (!found) {
  661. spare = *offset - AVALON_READ_SIZE;
  662. /* We are buffering and haven't accumulated one more corrupt
  663. * work result. */
  664. if (spare < (int)AVALON_READ_SIZE)
  665. return;
  666. avalon_inc_nvw(info, thr);
  667. } else {
  668. spare = AVALON_READ_SIZE + i;
  669. if (i) {
  670. if (i >= (int)AVALON_READ_SIZE)
  671. avalon_inc_nvw(info, thr);
  672. else
  673. applog(LOG_WARNING, "Avalon: Discarding %d bytes from buffer", i);
  674. }
  675. }
  676. *offset -= spare;
  677. memmove(buf, buf + spare, *offset);
  678. }
  679. static void avalon_running_reset(struct cgpu_info *avalon,
  680. struct avalon_info *info)
  681. {
  682. avalon_reset(avalon, false);
  683. avalon_idle(avalon, info);
  684. avalon->results = 0;
  685. info->reset = false;
  686. }
  687. static void *avalon_get_results(void *userdata)
  688. {
  689. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  690. struct avalon_info *info = avalon->device_data;
  691. int offset = 0, read_delay = 0, ret = 0;
  692. const int rsize = AVALON_FTDI_READSIZE;
  693. char readbuf[AVALON_READBUF_SIZE];
  694. struct thr_info *thr = info->thr;
  695. struct timeval tv_start, tv_end;
  696. char threadname[24];
  697. snprintf(threadname, 24, "ava_recv/%d", avalon->device_id);
  698. RenameThread(threadname);
  699. while (likely(!avalon->shutdown)) {
  700. unsigned char buf[rsize];
  701. struct timeval tv_diff;
  702. int us_diff;
  703. if (offset >= (int)AVALON_READ_SIZE)
  704. avalon_parse_results(avalon, info, thr, readbuf, &offset);
  705. if (unlikely(offset + rsize >= AVALON_READBUF_SIZE)) {
  706. /* This should never happen */
  707. applog(LOG_ERR, "Avalon readbuf overflow, resetting buffer");
  708. offset = 0;
  709. }
  710. if (unlikely(info->reset)) {
  711. avalon_running_reset(avalon, info);
  712. /* Discard anything in the buffer */
  713. offset = 0;
  714. }
  715. /* As the usb read returns after just 1ms, sleep long enough
  716. * to leave the interface idle for writes to occur, but do not
  717. * sleep if we have been receiving data as more may be coming. */
  718. if (ret < 1) {
  719. cgtime(&tv_end);
  720. timersub(&tv_end, &tv_start, &tv_diff);
  721. /* Assume it has not been > 1 second so ignore tv_sec */
  722. us_diff = tv_diff.tv_usec;
  723. read_delay = AVALON_READ_TIMEOUT * 1000 - us_diff;
  724. if (likely(read_delay >= 1000))
  725. nusleep(read_delay);
  726. }
  727. cgtime(&tv_start);
  728. ret = avalon_read(avalon, buf, rsize, AVALON_READ_TIMEOUT,
  729. C_AVALON_READ);
  730. if (ret < 1)
  731. continue;
  732. if (opt_debug) {
  733. applog(LOG_DEBUG, "Avalon: get:");
  734. hexdump((uint8_t *)buf, ret);
  735. }
  736. memcpy(&readbuf[offset], &buf, ret);
  737. offset += ret;
  738. }
  739. return NULL;
  740. }
  741. static void avalon_rotate_array(struct cgpu_info *avalon)
  742. {
  743. avalon->queued = 0;
  744. if (++avalon->work_array >= AVALON_ARRAY_SIZE)
  745. avalon->work_array = 0;
  746. }
  747. static void avalon_set_timeout(struct avalon_info *info)
  748. {
  749. info->timeout = AVALON_TIMEOUT_FACTOR / info->frequency;
  750. }
  751. static void avalon_inc_freq(struct avalon_info *info)
  752. {
  753. info->frequency += 2;
  754. if (info->frequency > opt_avalon_freq_max)
  755. info->frequency = opt_avalon_freq_max;
  756. avalon_set_timeout(info);
  757. applog(LOG_NOTICE, "Avalon increasing frequency to %d, timeout %d",
  758. info->frequency, info->timeout);
  759. }
  760. static void avalon_dec_freq(struct avalon_info *info)
  761. {
  762. info->frequency -= 1;
  763. if (info->frequency < opt_avalon_freq_min)
  764. info->frequency = opt_avalon_freq_min;
  765. avalon_set_timeout(info);
  766. applog(LOG_NOTICE, "Avalon decreasing frequency to %d, timeout %d",
  767. info->frequency, info->timeout);
  768. }
  769. static void avalon_reset_auto(struct avalon_info *info)
  770. {
  771. info->auto_queued =
  772. info->auto_nonces =
  773. info->auto_hw = 0;
  774. }
  775. static void *avalon_send_tasks(void *userdata)
  776. {
  777. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  778. struct avalon_info *info = avalon->device_data;
  779. const int avalon_get_work_count = info->miner_count;
  780. char threadname[24];
  781. snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
  782. RenameThread(threadname);
  783. while (likely(!avalon->shutdown)) {
  784. int start_count, end_count, i, j, ret;
  785. struct avalon_task at;
  786. bool idled = false;
  787. while (avalon_buffer_full(avalon))
  788. cgsem_wait(&info->write_sem);
  789. if (opt_avalon_auto && info->auto_queued >= AVALON_AUTO_CYCLE) {
  790. mutex_lock(&info->lock);
  791. if (!info->optimal) {
  792. if (info->fan_pwm >= opt_avalon_fan_max) {
  793. applog(LOG_WARNING,
  794. "%s%i: Above optimal temperature, throttling",
  795. avalon->drv->name, avalon->device_id);
  796. avalon_dec_freq(info);
  797. }
  798. } else if (info->auto_nonces >= (AVALON_AUTO_CYCLE * 19 / 20) &&
  799. info->auto_nonces <= (AVALON_AUTO_CYCLE * 21 / 20)) {
  800. int total = info->auto_nonces + info->auto_hw;
  801. /* Try to keep hw errors < 2% */
  802. if (info->auto_hw * 100 < total)
  803. avalon_inc_freq(info);
  804. else if (info->auto_hw * 66 > total)
  805. avalon_dec_freq(info);
  806. }
  807. avalon_reset_auto(info);
  808. mutex_unlock(&info->lock);
  809. }
  810. mutex_lock(&info->qlock);
  811. start_count = avalon->work_array * avalon_get_work_count;
  812. end_count = start_count + avalon_get_work_count;
  813. for (i = start_count, j = 0; i < end_count; i++, j++) {
  814. if (avalon_buffer_full(avalon)) {
  815. applog(LOG_INFO,
  816. "%s%i: Buffer full after only %d of %d work queued",
  817. avalon->drv->name, avalon->device_id, j, avalon_get_work_count);
  818. break;
  819. }
  820. if (likely(j < avalon->queued && !info->overheat && avalon->works[i])) {
  821. avalon_init_task(&at, 0, 0, info->fan_pwm,
  822. info->timeout, info->asic_count,
  823. info->miner_count, 1, 0, info->frequency);
  824. avalon_create_task(&at, avalon->works[i]);
  825. info->auto_queued++;
  826. } else {
  827. int idle_freq = info->frequency;
  828. if (!info->idle++)
  829. idled = true;
  830. if (unlikely(info->overheat && opt_avalon_auto))
  831. idle_freq = AVALON_MIN_FREQUENCY;
  832. avalon_init_task(&at, 0, 0, info->fan_pwm,
  833. info->timeout, info->asic_count,
  834. info->miner_count, 1, 1, idle_freq);
  835. /* Reset the auto_queued count if we end up
  836. * idling any miners. */
  837. avalon_reset_auto(info);
  838. }
  839. ret = avalon_send_task(&at, avalon);
  840. if (unlikely(ret == AVA_SEND_ERROR)) {
  841. applog(LOG_ERR, "%s%i: Comms error(buffer)",
  842. avalon->drv->name, avalon->device_id);
  843. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  844. info->reset = true;
  845. break;
  846. }
  847. }
  848. avalon_rotate_array(avalon);
  849. pthread_cond_signal(&info->qcond);
  850. mutex_unlock(&info->qlock);
  851. if (unlikely(idled)) {
  852. applog(LOG_WARNING, "%s%i: Idled %d miners",
  853. avalon->drv->name, avalon->device_id, idled);
  854. }
  855. }
  856. return NULL;
  857. }
  858. static bool avalon_prepare(struct thr_info *thr)
  859. {
  860. struct cgpu_info *avalon = thr->cgpu;
  861. struct avalon_info *info = avalon->device_data;
  862. free(avalon->works);
  863. avalon->works = calloc(info->miner_count * sizeof(struct work *),
  864. AVALON_ARRAY_SIZE);
  865. if (!avalon->works)
  866. quit(1, "Failed to calloc avalon works in avalon_prepare");
  867. info->thr = thr;
  868. mutex_init(&info->lock);
  869. mutex_init(&info->qlock);
  870. if (unlikely(pthread_cond_init(&info->qcond, NULL)))
  871. quit(1, "Failed to pthread_cond_init avalon qcond");
  872. cgsem_init(&info->write_sem);
  873. if (pthread_create(&info->read_thr, NULL, avalon_get_results, (void *)avalon))
  874. quit(1, "Failed to create avalon read_thr");
  875. if (pthread_create(&info->write_thr, NULL, avalon_send_tasks, (void *)avalon))
  876. quit(1, "Failed to create avalon write_thr");
  877. avalon_init(avalon);
  878. return true;
  879. }
  880. static void do_avalon_close(struct thr_info *thr)
  881. {
  882. struct cgpu_info *avalon = thr->cgpu;
  883. struct avalon_info *info = avalon->device_data;
  884. pthread_join(info->read_thr, NULL);
  885. pthread_join(info->write_thr, NULL);
  886. avalon_running_reset(avalon, info);
  887. info->no_matching_work = 0;
  888. cgsem_destroy(&info->write_sem);
  889. }
  890. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  891. {
  892. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  893. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  894. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  895. info->temp0 = ar->temp0;
  896. info->temp1 = ar->temp1;
  897. info->temp2 = ar->temp2;
  898. if (ar->temp0 & 0x80) {
  899. ar->temp0 &= 0x7f;
  900. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  901. }
  902. if (ar->temp1 & 0x80) {
  903. ar->temp1 &= 0x7f;
  904. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  905. }
  906. if (ar->temp2 & 0x80) {
  907. ar->temp2 &= 0x7f;
  908. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  909. }
  910. *temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
  911. if (info->temp0 > info->temp_max)
  912. info->temp_max = info->temp0;
  913. if (info->temp1 > info->temp_max)
  914. info->temp_max = info->temp1;
  915. if (info->temp2 > info->temp_max)
  916. info->temp_max = info->temp2;
  917. }
  918. static void temp_rise(struct avalon_info *info, int temp)
  919. {
  920. if (temp >= opt_avalon_temp + AVALON_TEMP_HYSTERESIS * 3) {
  921. info->fan_pwm = AVALON_PWM_MAX;
  922. return;
  923. }
  924. if (temp >= opt_avalon_temp + AVALON_TEMP_HYSTERESIS * 2)
  925. info->fan_pwm += 10;
  926. else if (temp > opt_avalon_temp)
  927. info->fan_pwm += 5;
  928. else if (temp >= opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  929. info->fan_pwm += 1;
  930. else
  931. return;
  932. if (info->fan_pwm > opt_avalon_fan_max)
  933. info->fan_pwm = opt_avalon_fan_max;
  934. }
  935. static void temp_drop(struct avalon_info *info, int temp)
  936. {
  937. if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS * 3) {
  938. info->fan_pwm = opt_avalon_fan_min;
  939. return;
  940. }
  941. if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS * 2)
  942. info->fan_pwm -= 10;
  943. else if (temp <= opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  944. info->fan_pwm -= 5;
  945. else if (temp < opt_avalon_temp)
  946. info->fan_pwm -= 1;
  947. if (info->fan_pwm < opt_avalon_fan_min)
  948. info->fan_pwm = opt_avalon_fan_min;
  949. }
  950. static inline void adjust_fan(struct avalon_info *info)
  951. {
  952. int temp_new;
  953. temp_new = info->temp_sum / info->temp_history_count;
  954. if (temp_new > info->temp_old)
  955. temp_rise(info, temp_new);
  956. else if (temp_new < info->temp_old)
  957. temp_drop(info, temp_new);
  958. else {
  959. /* temp_new == info->temp_old */
  960. if (temp_new > opt_avalon_temp)
  961. temp_rise(info, temp_new);
  962. else if (temp_new < opt_avalon_temp - AVALON_TEMP_HYSTERESIS)
  963. temp_drop(info, temp_new);
  964. }
  965. info->temp_old = temp_new;
  966. if (info->temp_old <= opt_avalon_temp)
  967. info->optimal = true;
  968. else
  969. info->optimal = false;
  970. }
  971. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  972. struct avalon_result *ar)
  973. {
  974. record_temp_fan(info, ar, &(avalon->temp));
  975. applog(LOG_INFO,
  976. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  977. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  978. info->fan0, info->fan1, info->fan2,
  979. info->temp0, info->temp1, info->temp2, info->temp_max);
  980. info->temp_history_index++;
  981. info->temp_sum += avalon->temp;
  982. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  983. info->temp_history_index, info->temp_history_count, info->temp_old);
  984. if (usb_ident(avalon) == IDENT_BTB) {
  985. info->core_voltage = bitburner_get_core_voltage(avalon);
  986. }
  987. if (info->temp_history_index == info->temp_history_count) {
  988. adjust_fan(info);
  989. info->temp_history_index = 0;
  990. info->temp_sum = 0;
  991. }
  992. if (unlikely(info->temp_old >= opt_avalon_overheat)) {
  993. applog(LOG_WARNING, "%s%d overheat! Idling", avalon->drv->name, avalon->device_id);
  994. info->overheat = true;
  995. } else if (info->overheat && info->temp_old <= opt_avalon_temp) {
  996. applog(LOG_WARNING, "%s%d cooled, restarting", avalon->drv->name, avalon->device_id);
  997. info->overheat = false;
  998. }
  999. }
  1000. static void get_avalon_statline_before(char *buf, size_t bufsiz, struct cgpu_info *avalon)
  1001. {
  1002. struct avalon_info *info = avalon->device_data;
  1003. int lowfan = 10000;
  1004. if (usb_ident(avalon) == IDENT_BTB) {
  1005. tailsprintf(buf, bufsiz, "%2d/%3dC %4dmV | ", info->temp0, info->temp2, info->core_voltage);
  1006. } else {
  1007. /* Find the lowest fan speed of the ASIC cooling fans. */
  1008. if (info->fan1 >= 0 && info->fan1 < lowfan)
  1009. lowfan = info->fan1;
  1010. if (info->fan2 >= 0 && info->fan2 < lowfan)
  1011. lowfan = info->fan2;
  1012. tailsprintf(buf, bufsiz, "%2d/%3dC %04dR | ", info->temp0, info->temp2, lowfan);
  1013. }
  1014. }
  1015. /* We use a replacement algorithm to only remove references to work done from
  1016. * the buffer when we need the extra space for new work. */
  1017. static bool avalon_fill(struct cgpu_info *avalon)
  1018. {
  1019. struct avalon_info *info = avalon->device_data;
  1020. int subid, slot, mc;
  1021. struct work *work;
  1022. bool ret = true;
  1023. mc = info->miner_count;
  1024. mutex_lock(&info->qlock);
  1025. if (avalon->queued >= mc)
  1026. goto out_unlock;
  1027. work = get_queued(avalon);
  1028. if (unlikely(!work)) {
  1029. ret = false;
  1030. goto out_unlock;
  1031. }
  1032. subid = avalon->queued++;
  1033. work->subid = subid;
  1034. slot = avalon->work_array * mc + subid;
  1035. if (likely(avalon->works[slot]))
  1036. work_completed(avalon, avalon->works[slot]);
  1037. avalon->works[slot] = work;
  1038. if (avalon->queued < mc)
  1039. ret = false;
  1040. out_unlock:
  1041. mutex_unlock(&info->qlock);
  1042. return ret;
  1043. }
  1044. static int64_t avalon_scanhash(struct thr_info *thr)
  1045. {
  1046. struct cgpu_info *avalon = thr->cgpu;
  1047. struct avalon_info *info = avalon->device_data;
  1048. const int miner_count = info->miner_count;
  1049. struct timeval now, then, tdiff;
  1050. int64_t hash_count, us_timeout;
  1051. struct timespec abstime;
  1052. int ret;
  1053. /* Half nonce range */
  1054. us_timeout = 0x80000000ll / info->asic_count / info->frequency;
  1055. tdiff.tv_sec = us_timeout / 1000000;
  1056. tdiff.tv_usec = us_timeout - (tdiff.tv_sec * 1000000);
  1057. cgtime(&now);
  1058. timeradd(&now, &tdiff, &then);
  1059. abstime.tv_sec = then.tv_sec;
  1060. abstime.tv_nsec = then.tv_usec * 1000;
  1061. /* Wait until avalon_send_tasks signals us that it has completed
  1062. * sending its work or a full nonce range timeout has occurred */
  1063. mutex_lock(&info->qlock);
  1064. ret = pthread_cond_timedwait(&info->qcond, &info->qlock, &abstime);
  1065. mutex_unlock(&info->qlock);
  1066. /* If we timed out, avalon_send_tasks may be stuck waiting on the
  1067. * write_sem, so force it to check for avalon_buffer_full itself. */
  1068. if (ret)
  1069. cgsem_post(&info->write_sem);
  1070. mutex_lock(&info->lock);
  1071. hash_count = 0xffffffffull * (uint64_t)info->nonces;
  1072. avalon->results += info->nonces + info->idle;
  1073. if (avalon->results > miner_count)
  1074. avalon->results = miner_count;
  1075. if (!info->reset)
  1076. avalon->results--;
  1077. info->nonces = info->idle = 0;
  1078. mutex_unlock(&info->lock);
  1079. /* Check for nothing but consecutive bad results or consistently less
  1080. * results than we should be getting and reset the FPGA if necessary */
  1081. if (usb_ident(avalon) != IDENT_BTB) {
  1082. if (avalon->results < -miner_count && !info->reset) {
  1083. applog(LOG_ERR, "%s%d: Result return rate low, resetting!",
  1084. avalon->drv->name, avalon->device_id);
  1085. info->reset = true;
  1086. }
  1087. }
  1088. if (unlikely(avalon->usbinfo.nodev)) {
  1089. applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread",
  1090. avalon->drv->name, avalon->device_id);
  1091. avalon->shutdown = true;
  1092. }
  1093. /* This hashmeter is just a utility counter based on returned shares */
  1094. return hash_count;
  1095. }
  1096. static void avalon_flush_work(struct cgpu_info *avalon)
  1097. {
  1098. struct avalon_info *info = avalon->device_data;
  1099. mutex_lock(&info->qlock);
  1100. /* Will overwrite any work queued */
  1101. avalon->queued = 0;
  1102. pthread_cond_signal(&info->qcond);
  1103. mutex_unlock(&info->qlock);
  1104. }
  1105. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  1106. {
  1107. struct api_data *root = NULL;
  1108. struct avalon_info *info = cgpu->device_data;
  1109. int i;
  1110. root = api_add_int(root, "baud", &(info->baud), false);
  1111. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  1112. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  1113. root = api_add_int(root, "timeout", &(info->timeout), false);
  1114. root = api_add_int(root, "frequency", &(info->frequency), false);
  1115. root = api_add_int(root, "fan1", &(info->fan0), false);
  1116. root = api_add_int(root, "fan2", &(info->fan1), false);
  1117. root = api_add_int(root, "fan3", &(info->fan2), false);
  1118. root = api_add_int(root, "temp1", &(info->temp0), false);
  1119. root = api_add_int(root, "temp2", &(info->temp1), false);
  1120. root = api_add_int(root, "temp3", &(info->temp2), false);
  1121. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  1122. root = api_add_int(root, "core_voltage", &(info->core_voltage), false);
  1123. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  1124. for (i = 0; i < info->miner_count; i++) {
  1125. char mcw[24];
  1126. sprintf(mcw, "match_work_count%d", i + 1);
  1127. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  1128. }
  1129. return root;
  1130. }
  1131. static void avalon_shutdown(struct thr_info *thr)
  1132. {
  1133. do_avalon_close(thr);
  1134. }
  1135. struct device_drv avalon_drv = {
  1136. .drv_id = DRIVER_AVALON,
  1137. .dname = "avalon",
  1138. .name = "AVA",
  1139. .drv_detect = avalon_detect,
  1140. .thread_prepare = avalon_prepare,
  1141. .hash_work = hash_queued_work,
  1142. .queue_full = avalon_fill,
  1143. .scanwork = avalon_scanhash,
  1144. .flush_work = avalon_flush_work,
  1145. .get_api_stats = avalon_api_stats,
  1146. .get_statline_before = get_avalon_statline_before,
  1147. .reinit_device = avalon_init,
  1148. .thread_shutdown = avalon_shutdown,
  1149. };