driver-avalon.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. static int option_offset = -1;
  40. struct device_drv avalon_drv;
  41. static int avalon_init_task(struct avalon_task *at,
  42. uint8_t reset, uint8_t ff, uint8_t fan,
  43. uint8_t timeout, uint8_t asic_num,
  44. uint8_t miner_num, uint8_t nonce_elf,
  45. uint8_t gate_miner, int frequency)
  46. {
  47. uint8_t *buf;
  48. static bool first = true;
  49. if (unlikely(!at))
  50. return -1;
  51. if (unlikely(timeout <= 0 || asic_num <= 0 || miner_num <= 0))
  52. return -1;
  53. memset(at, 0, sizeof(struct avalon_task));
  54. if (unlikely(reset)) {
  55. at->reset = 1;
  56. at->fan_eft = 1;
  57. at->timer_eft = 1;
  58. first = true;
  59. }
  60. at->flush_fifo = (ff ? 1 : 0);
  61. at->fan_eft = (fan ? 1 : 0);
  62. if (unlikely(first && !at->reset)) {
  63. at->fan_eft = 1;
  64. at->timer_eft = 1;
  65. first = false;
  66. }
  67. at->fan_pwm_data = (fan ? fan : AVALON_DEFAULT_FAN_MAX_PWM);
  68. at->timeout_data = timeout;
  69. at->asic_num = asic_num;
  70. at->miner_num = miner_num;
  71. at->nonce_elf = nonce_elf;
  72. at->gate_miner_elf = 1;
  73. at->asic_pll = 1;
  74. if (unlikely(gate_miner)) {
  75. at-> gate_miner = 1;
  76. at->asic_pll = 0;
  77. }
  78. buf = (uint8_t *)at;
  79. buf[5] = 0x00;
  80. buf[8] = 0x74;
  81. buf[9] = 0x01;
  82. buf[10] = 0x00;
  83. buf[11] = 0x00;
  84. if (frequency == 256) {
  85. buf[6] = 0x03;
  86. buf[7] = 0x08;
  87. } else if (frequency == 270) {
  88. buf[6] = 0x73;
  89. buf[7] = 0x08;
  90. } else if (frequency == 282) {
  91. buf[6] = 0xd3;
  92. buf[7] = 0x08;
  93. } else if (frequency == 300) {
  94. buf[6] = 0x63;
  95. buf[7] = 0x09;
  96. }
  97. return 0;
  98. }
  99. static inline void avalon_create_task(struct avalon_task *at,
  100. struct work *work)
  101. {
  102. memcpy(at->midstate, work->midstate, 32);
  103. memcpy(at->data, work->data + 64, 12);
  104. }
  105. static int avalon_write(struct cgpu_info *avalon, char *buf, ssize_t len, int ep)
  106. {
  107. int err, amount;
  108. err = usb_write(avalon, buf, len, &amount, ep);
  109. applog(LOG_DEBUG, "%s%i: usb_write got err %d", avalon->drv->name,
  110. avalon->device_id, err);
  111. if (unlikely(err != 0)) {
  112. applog(LOG_WARNING, "usb_write error on avalon_write");
  113. return AVA_SEND_ERROR;
  114. }
  115. if (amount != len) {
  116. applog(LOG_WARNING, "usb_write length mismatch on avalon_write");
  117. return AVA_SEND_ERROR;
  118. }
  119. return AVA_SEND_OK;
  120. }
  121. static int avalon_send_task(const struct avalon_task *at, struct cgpu_info *avalon)
  122. {
  123. struct timespec p;
  124. uint8_t buf[AVALON_WRITE_SIZE + 4 * AVALON_DEFAULT_ASIC_NUM];
  125. size_t nr_len;
  126. struct avalon_info *info;
  127. uint64_t delay = 32000000; /* Default 32ms for B19200 */
  128. uint32_t nonce_range;
  129. int ret, i, ep = C_AVALON_TASK;
  130. if (at->nonce_elf)
  131. nr_len = AVALON_WRITE_SIZE + 4 * at->asic_num;
  132. else
  133. nr_len = AVALON_WRITE_SIZE;
  134. memcpy(buf, at, AVALON_WRITE_SIZE);
  135. if (at->nonce_elf) {
  136. nonce_range = (uint32_t)0xffffffff / at->asic_num;
  137. for (i = 0; i < at->asic_num; i++) {
  138. buf[AVALON_WRITE_SIZE + (i * 4) + 3] =
  139. (i * nonce_range & 0xff000000) >> 24;
  140. buf[AVALON_WRITE_SIZE + (i * 4) + 2] =
  141. (i * nonce_range & 0x00ff0000) >> 16;
  142. buf[AVALON_WRITE_SIZE + (i * 4) + 1] =
  143. (i * nonce_range & 0x0000ff00) >> 8;
  144. buf[AVALON_WRITE_SIZE + (i * 4) + 0] =
  145. (i * nonce_range & 0x000000ff) >> 0;
  146. }
  147. }
  148. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  149. uint8_t tt = 0;
  150. tt = (buf[0] & 0x0f) << 4;
  151. tt |= ((buf[0] & 0x10) ? (1 << 3) : 0);
  152. tt |= ((buf[0] & 0x20) ? (1 << 2) : 0);
  153. tt |= ((buf[0] & 0x40) ? (1 << 1) : 0);
  154. tt |= ((buf[0] & 0x80) ? (1 << 0) : 0);
  155. buf[0] = tt;
  156. tt = (buf[4] & 0x0f) << 4;
  157. tt |= ((buf[4] & 0x10) ? (1 << 3) : 0);
  158. tt |= ((buf[4] & 0x20) ? (1 << 2) : 0);
  159. tt |= ((buf[4] & 0x40) ? (1 << 1) : 0);
  160. tt |= ((buf[4] & 0x80) ? (1 << 0) : 0);
  161. buf[4] = tt;
  162. #endif
  163. if (likely(avalon)) {
  164. info = avalon->device_data;
  165. delay = nr_len * 10 * 1000000000ULL;
  166. delay = delay / info->baud;
  167. }
  168. if (at->reset) {
  169. ep = C_AVALON_RESET;
  170. nr_len = 1;
  171. }
  172. if (opt_debug) {
  173. applog(LOG_DEBUG, "Avalon: Sent(%u):", (unsigned int)nr_len);
  174. hexdump(buf, nr_len);
  175. }
  176. ret = avalon_write(avalon, (char *)buf, nr_len, ep);
  177. p.tv_sec = 0;
  178. p.tv_nsec = (long)delay + 4000000;
  179. nanosleep(&p, NULL);
  180. applog(LOG_DEBUG, "Avalon: Sent: Buffer delay: %ld", p.tv_nsec);
  181. return ret;
  182. }
  183. static bool avalon_decode_nonce(struct thr_info *thr, struct cgpu_info *avalon,
  184. struct avalon_info *info, struct avalon_result *ar,
  185. struct work *work)
  186. {
  187. uint32_t nonce;
  188. info = avalon->device_data;
  189. info->matching_work[work->subid]++;
  190. nonce = htole32(ar->nonce);
  191. applog(LOG_DEBUG, "Avalon: nonce = %0x08x", nonce);
  192. return submit_nonce(thr, work, nonce);
  193. }
  194. /* Wait until the ftdi chip returns a CTS saying we can send more data. The
  195. * status is updated every 40ms. */
  196. static void wait_avalon_ready(struct cgpu_info *avalon)
  197. {
  198. while (avalon_buffer_full(avalon)) {
  199. nmsleep(40);
  200. }
  201. }
  202. static int avalon_read(struct cgpu_info *avalon, unsigned char *buf,
  203. size_t bufsize, int timeout, int ep)
  204. {
  205. size_t total = 0, readsize = bufsize + 2;
  206. char readbuf[AVALON_READBUF_SIZE];
  207. int err, amount, ofs = 2, cp;
  208. err = usb_read_once_timeout(avalon, readbuf, readsize, &amount, timeout, ep);
  209. applog(LOG_DEBUG, "%s%i: Get avalon read got err %d",
  210. avalon->drv->name, avalon->device_id, err);
  211. /* The first 2 of every 64 bytes are status on FTDIRL */
  212. while (amount > 2) {
  213. cp = amount - 2;
  214. if (cp > 62)
  215. cp = 62;
  216. memcpy(&buf[total], &readbuf[ofs], cp);
  217. total += cp;
  218. amount -= cp + 2;
  219. ofs += 64;
  220. }
  221. return total;
  222. }
  223. static int avalon_reset(struct cgpu_info *avalon, bool initial)
  224. {
  225. struct avalon_result ar;
  226. int ret, i, spare;
  227. struct avalon_task at;
  228. uint8_t *buf, *tmp;
  229. struct timespec p;
  230. /* Send reset, then check for result */
  231. avalon_init_task(&at, 1, 0,
  232. AVALON_DEFAULT_FAN_MAX_PWM,
  233. AVALON_DEFAULT_TIMEOUT,
  234. AVALON_DEFAULT_ASIC_NUM,
  235. AVALON_DEFAULT_MINER_NUM,
  236. 0, 0,
  237. AVALON_DEFAULT_FREQUENCY);
  238. wait_avalon_ready(avalon);
  239. ret = avalon_send_task(&at, avalon);
  240. if (unlikely(ret == AVA_SEND_ERROR))
  241. return -1;
  242. if (!initial) {
  243. applog(LOG_ERR, "AVA%d reset sequence sent", avalon->device_id);
  244. return 0;
  245. }
  246. ret = avalon_read(avalon, (unsigned char *)&ar, AVALON_READ_SIZE,
  247. AVALON_RESET_TIMEOUT, C_GET_AVALON_RESET);
  248. /* What do these sleeps do?? */
  249. p.tv_sec = 0;
  250. p.tv_nsec = AVALON_RESET_PITCH;
  251. nanosleep(&p, NULL);
  252. /* Look for the first occurrence of 0xAA, the reset response should be:
  253. * AA 55 AA 55 00 00 00 00 00 00 */
  254. spare = ret - 10;
  255. buf = tmp = (uint8_t *)&ar;
  256. if (opt_debug) {
  257. applog(LOG_DEBUG, "AVA%d reset: get:", avalon->device_id);
  258. hexdump(tmp, AVALON_READ_SIZE);
  259. }
  260. for (i = 0; i <= spare; i++) {
  261. buf = &tmp[i];
  262. if (buf[0] == 0xAA)
  263. break;
  264. }
  265. i = 0;
  266. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  267. buf[2] == 0xAA && buf[3] == 0x55) {
  268. for (i = 4; i < 11; i++)
  269. if (buf[i] != 0)
  270. break;
  271. }
  272. if (i != 11) {
  273. applog(LOG_ERR, "AVA%d: Reset failed! not an Avalon?"
  274. " (%d: %02x %02x %02x %02x)", avalon->device_id,
  275. i, buf[0], buf[1], buf[2], buf[3]);
  276. /* FIXME: return 1; */
  277. } else
  278. applog(LOG_WARNING, "AVA%d: Reset succeeded",
  279. avalon->device_id);
  280. return 0;
  281. }
  282. static bool get_options(int this_option_offset, int *baud, int *miner_count,
  283. int *asic_count, int *timeout, int *frequency)
  284. {
  285. char err_buf[BUFSIZ+1];
  286. char buf[BUFSIZ+1];
  287. char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
  288. size_t max;
  289. int i, tmp;
  290. if (opt_avalon_options == NULL)
  291. buf[0] = '\0';
  292. else {
  293. ptr = opt_avalon_options;
  294. for (i = 0; i < this_option_offset; i++) {
  295. comma = strchr(ptr, ',');
  296. if (comma == NULL)
  297. break;
  298. ptr = comma + 1;
  299. }
  300. comma = strchr(ptr, ',');
  301. if (comma == NULL)
  302. max = strlen(ptr);
  303. else
  304. max = comma - ptr;
  305. if (max > BUFSIZ)
  306. max = BUFSIZ;
  307. strncpy(buf, ptr, max);
  308. buf[max] = '\0';
  309. }
  310. if (!(*buf))
  311. return false;
  312. colon = strchr(buf, ':');
  313. if (colon)
  314. *(colon++) = '\0';
  315. tmp = atoi(buf);
  316. switch (tmp) {
  317. case 115200:
  318. *baud = 115200;
  319. break;
  320. case 57600:
  321. *baud = 57600;
  322. break;
  323. case 38400:
  324. *baud = 38400;
  325. break;
  326. case 19200:
  327. *baud = 19200;
  328. break;
  329. default:
  330. sprintf(err_buf,
  331. "Invalid avalon-options for baud (%s) "
  332. "must be 115200, 57600, 38400 or 19200", buf);
  333. quit(1, err_buf);
  334. }
  335. if (colon && *colon) {
  336. colon2 = strchr(colon, ':');
  337. if (colon2)
  338. *(colon2++) = '\0';
  339. if (*colon) {
  340. tmp = atoi(colon);
  341. if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
  342. *miner_count = tmp;
  343. } else {
  344. sprintf(err_buf,
  345. "Invalid avalon-options for "
  346. "miner_count (%s) must be 1 ~ %d",
  347. colon, AVALON_DEFAULT_MINER_NUM);
  348. quit(1, err_buf);
  349. }
  350. }
  351. if (colon2 && *colon2) {
  352. colon3 = strchr(colon2, ':');
  353. if (colon3)
  354. *(colon3++) = '\0';
  355. tmp = atoi(colon2);
  356. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  357. *asic_count = tmp;
  358. else {
  359. sprintf(err_buf,
  360. "Invalid avalon-options for "
  361. "asic_count (%s) must be 1 ~ %d",
  362. colon2, AVALON_DEFAULT_ASIC_NUM);
  363. quit(1, err_buf);
  364. }
  365. if (colon3 && *colon3) {
  366. colon4 = strchr(colon3, ':');
  367. if (colon4)
  368. *(colon4++) = '\0';
  369. tmp = atoi(colon3);
  370. if (tmp > 0 && tmp <= 0xff)
  371. *timeout = tmp;
  372. else {
  373. sprintf(err_buf,
  374. "Invalid avalon-options for "
  375. "timeout (%s) must be 1 ~ %d",
  376. colon3, 0xff);
  377. quit(1, err_buf);
  378. }
  379. if (colon4 && *colon4) {
  380. tmp = atoi(colon4);
  381. switch (tmp) {
  382. case 256:
  383. case 270:
  384. case 282:
  385. case 300:
  386. *frequency = tmp;
  387. break;
  388. default:
  389. sprintf(err_buf,
  390. "Invalid avalon-options for "
  391. "frequency must be 256/270/282/300");
  392. quit(1, err_buf);
  393. }
  394. }
  395. }
  396. }
  397. }
  398. return true;
  399. }
  400. static void avalon_idle(struct cgpu_info *avalon, struct avalon_info *info)
  401. {
  402. int i;
  403. info->idle = true;
  404. wait_avalon_ready(avalon);
  405. /* Send idle to all miners */
  406. for (i = 0; i < info->miner_count; i++) {
  407. struct avalon_task at;
  408. if (unlikely(avalon_buffer_full(avalon)))
  409. break;
  410. avalon_init_task(&at, 0, 0, info->fan_pwm, info->timeout,
  411. info->asic_count, info->miner_count, 1, 1,
  412. info->frequency);
  413. avalon_send_task(&at, avalon);
  414. }
  415. applog(LOG_WARNING, "AVA%i: Idling %d miners", avalon->device_id, i);
  416. wait_avalon_ready(avalon);
  417. }
  418. static void avalon_initialise(struct cgpu_info *avalon)
  419. {
  420. int err, interface;
  421. if (avalon->usbinfo.nodev)
  422. return;
  423. interface = avalon->usbdev->found->interface;
  424. // Reset
  425. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  426. FTDI_VALUE_RESET, interface, C_RESET);
  427. applog(LOG_DEBUG, "%s%i: reset got err %d",
  428. avalon->drv->name, avalon->device_id, err);
  429. if (avalon->usbinfo.nodev)
  430. return;
  431. // Set latency
  432. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY,
  433. AVALON_READ_TIMEOUT, interface, C_LATENCY);
  434. applog(LOG_DEBUG, "%s%i: latency got err %d",
  435. avalon->drv->name, avalon->device_id, err);
  436. if (avalon->usbinfo.nodev)
  437. return;
  438. // Set data
  439. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  440. FTDI_VALUE_DATA_AVA, interface, C_SETDATA);
  441. applog(LOG_DEBUG, "%s%i: data got err %d",
  442. avalon->drv->name, avalon->device_id, err);
  443. if (avalon->usbinfo.nodev)
  444. return;
  445. // Set the baud
  446. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_AVA,
  447. (FTDI_INDEX_BAUD_AVA & 0xff00) | interface,
  448. C_SETBAUD);
  449. applog(LOG_DEBUG, "%s%i: setbaud got err %d",
  450. avalon->drv->name, avalon->device_id, err);
  451. if (avalon->usbinfo.nodev)
  452. return;
  453. // Set Modem Control
  454. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  455. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  456. applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
  457. avalon->drv->name, avalon->device_id, err);
  458. if (avalon->usbinfo.nodev)
  459. return;
  460. // Set Flow Control
  461. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  462. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  463. applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
  464. avalon->drv->name, avalon->device_id, err);
  465. if (avalon->usbinfo.nodev)
  466. return;
  467. /* Avalon repeats the following */
  468. // Set Modem Control
  469. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  470. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  471. applog(LOG_DEBUG, "%s%i: setmodemctrl 2 got err %d",
  472. avalon->drv->name, avalon->device_id, err);
  473. if (avalon->usbinfo.nodev)
  474. return;
  475. // Set Flow Control
  476. err = usb_transfer(avalon, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  477. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  478. applog(LOG_DEBUG, "%s%i: setflowctrl 2 got err %d",
  479. avalon->drv->name, avalon->device_id, err);
  480. }
  481. static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found)
  482. {
  483. int baud, miner_count, asic_count, timeout, frequency = 0;
  484. int this_option_offset = ++option_offset;
  485. struct avalon_info *info;
  486. struct cgpu_info *avalon;
  487. char devpath[20];
  488. bool configured;
  489. int ret;
  490. avalon = calloc(1, sizeof(struct cgpu_info));
  491. if (unlikely(!avalon))
  492. quit(1, "Failed to calloc avalon in avalon_detect_one");;
  493. avalon->drv = &avalon_drv;
  494. avalon->threads = AVALON_MINER_THREADS;
  495. configured = get_options(this_option_offset, &baud, &miner_count,
  496. &asic_count, &timeout, &frequency);
  497. if (!usb_init(avalon, dev, found))
  498. return false;
  499. /* Even though this is an FTDI type chip, we want to do the parsing
  500. * all ourselves so set it to std usb type */
  501. avalon->usbdev->usb_type = USB_TYPE_STD;
  502. /* We have a real Avalon! */
  503. sprintf(devpath, "%d:%d",
  504. (int)(avalon->usbinfo.bus_number),
  505. (int)(avalon->usbinfo.device_address));
  506. avalon_initialise(avalon);
  507. avalon->device_path = strdup(devpath);
  508. add_cgpu(avalon);
  509. avalon->device_data = calloc(sizeof(struct avalon_info), 1);
  510. if (unlikely(!(avalon->device_data)))
  511. quit(1, "Failed to malloc avalon_info data");
  512. info = avalon->device_data;
  513. if (configured) {
  514. info->baud = baud;
  515. info->miner_count = miner_count;
  516. info->asic_count = asic_count;
  517. info->timeout = timeout;
  518. info->frequency = frequency;
  519. } else {
  520. info->baud = AVALON_IO_SPEED;
  521. info->miner_count = AVALON_DEFAULT_MINER_NUM;
  522. info->asic_count = AVALON_DEFAULT_ASIC_NUM;
  523. info->timeout = AVALON_DEFAULT_TIMEOUT;
  524. info->frequency = AVALON_DEFAULT_FREQUENCY;
  525. }
  526. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  527. info->temp_max = 0;
  528. /* This is for check the temp/fan every 3~4s */
  529. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  530. if (info->temp_history_count <= 0)
  531. info->temp_history_count = 1;
  532. info->temp_history_index = 0;
  533. info->temp_sum = 0;
  534. info->temp_old = 0;
  535. ret = avalon_reset(avalon, true);
  536. if (ret && !configured) {
  537. usb_uninit(avalon);
  538. return false;
  539. }
  540. avalon_idle(avalon, info);
  541. applog(LOG_DEBUG, "Avalon Detected: %s "
  542. "(miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  543. devpath, info->miner_count, info->asic_count, info->timeout,
  544. info->frequency);
  545. return true;
  546. }
  547. static void avalon_detect(void)
  548. {
  549. usb_detect(&avalon_drv, avalon_detect_one);
  550. }
  551. static void avalon_init(struct cgpu_info *avalon)
  552. {
  553. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  554. }
  555. static struct work *avalon_valid_result(struct cgpu_info *avalon, struct avalon_result *ar)
  556. {
  557. return find_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
  558. (char *)ar->data, 64, 12);
  559. }
  560. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  561. struct avalon_result *ar);
  562. static void avalon_inc_nvw(struct avalon_info *info, struct thr_info *thr)
  563. {
  564. if (unlikely(info->idle))
  565. return;
  566. applog(LOG_WARNING, "%s%d: No valid work - HW error",
  567. thr->cgpu->drv->name, thr->cgpu->device_id);
  568. inc_hw_errors(thr);
  569. info->no_matching_work++;
  570. }
  571. static void avalon_parse_results(struct cgpu_info *avalon, struct avalon_info *info,
  572. struct thr_info *thr, char *buf, int *offset)
  573. {
  574. int i, spare = *offset - AVALON_READ_SIZE;
  575. bool found = false;
  576. for (i = 0; i <= spare; i++) {
  577. struct avalon_result *ar;
  578. struct work *work;
  579. ar = (struct avalon_result *)&buf[i];
  580. work = avalon_valid_result(avalon, ar);
  581. if (work) {
  582. bool gettemp = false;
  583. found = true;
  584. if (avalon_decode_nonce(thr, avalon, info, ar, work)) {
  585. mutex_lock(&info->lock);
  586. if (!info->nonces++)
  587. gettemp = true;
  588. mutex_unlock(&info->lock);
  589. }
  590. if (gettemp)
  591. avalon_update_temps(avalon, info, ar);
  592. break;
  593. }
  594. }
  595. if (!found) {
  596. spare = *offset - AVALON_READ_SIZE;
  597. /* We are buffering and haven't accumulated one more corrupt
  598. * work result. */
  599. if (spare < (int)AVALON_READ_SIZE)
  600. return;
  601. avalon_inc_nvw(info, thr);
  602. } else {
  603. spare = AVALON_READ_SIZE + i;
  604. if (i) {
  605. if (i >= (int)AVALON_READ_SIZE)
  606. avalon_inc_nvw(info, thr);
  607. else
  608. applog(LOG_WARNING, "Avalon: Discarding %d bytes from buffer", i);
  609. }
  610. }
  611. *offset -= spare;
  612. memmove(buf, buf + spare, *offset);
  613. }
  614. static void avalon_running_reset(struct cgpu_info *avalon,
  615. struct avalon_info *info)
  616. {
  617. avalon_reset(avalon, false);
  618. avalon_idle(avalon, info);
  619. avalon->results = 0;
  620. info->reset = false;
  621. }
  622. static void *avalon_get_results(void *userdata)
  623. {
  624. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  625. struct avalon_info *info = avalon->device_data;
  626. const int rsize = AVALON_FTDI_READSIZE;
  627. char readbuf[AVALON_READBUF_SIZE];
  628. struct thr_info *thr = info->thr;
  629. char threadname[24];
  630. int offset = 0;
  631. snprintf(threadname, 24, "ava_recv/%d", avalon->device_id);
  632. RenameThread(threadname);
  633. while (likely(!avalon->shutdown)) {
  634. unsigned char buf[rsize];
  635. int ret;
  636. if (offset >= (int)AVALON_READ_SIZE)
  637. avalon_parse_results(avalon, info, thr, readbuf, &offset);
  638. if (unlikely(offset + rsize >= AVALON_READBUF_SIZE)) {
  639. /* This should never happen */
  640. applog(LOG_ERR, "Avalon readbuf overflow, resetting buffer");
  641. offset = 0;
  642. }
  643. if (unlikely(info->reset)) {
  644. /* Tell the write thread it can start the reset */
  645. cgsem_post(&info->write_sem);
  646. cgsem_wait(&info->read_sem);
  647. /* Discard anything in the buffer */
  648. offset = 0;
  649. }
  650. ret = avalon_read(avalon, buf, rsize, AVALON_READ_TIMEOUT,
  651. C_AVALON_READ);
  652. if (ret < 1)
  653. continue;
  654. if (opt_debug) {
  655. applog(LOG_DEBUG, "Avalon: get:");
  656. hexdump((uint8_t *)buf, ret);
  657. }
  658. memcpy(&readbuf[offset], &buf, ret);
  659. offset += ret;
  660. }
  661. return NULL;
  662. }
  663. static void avalon_rotate_array(struct cgpu_info *avalon)
  664. {
  665. avalon->queued = 0;
  666. if (++avalon->work_array >= AVALON_ARRAY_SIZE)
  667. avalon->work_array = 0;
  668. }
  669. static void *avalon_send_tasks(void *userdata)
  670. {
  671. struct cgpu_info *avalon = (struct cgpu_info *)userdata;
  672. struct avalon_info *info = avalon->device_data;
  673. const int avalon_get_work_count = info->miner_count;
  674. char threadname[24];
  675. snprintf(threadname, 24, "ava_send/%d", avalon->device_id);
  676. RenameThread(threadname);
  677. while (likely(!avalon->shutdown)) {
  678. int start_count, end_count, i, j, ret;
  679. struct avalon_task at;
  680. int idled = 0;
  681. wait_avalon_ready(avalon);
  682. if (unlikely(info->reset)) {
  683. /* Wait till read thread tells us it's received the
  684. * reset message */
  685. cgsem_wait(&info->write_sem);
  686. avalon_running_reset(avalon, info);
  687. cgsem_post(&info->read_sem);
  688. }
  689. mutex_lock(&info->qlock);
  690. start_count = avalon->work_array * avalon_get_work_count;
  691. end_count = start_count + avalon_get_work_count;
  692. for (i = start_count, j = 0; i < end_count; i++, j++) {
  693. if (avalon_buffer_full(avalon)) {
  694. applog(LOG_INFO,
  695. "AVA%i: Buffer full after only %d of %d work queued",
  696. avalon->device_id, j, avalon_get_work_count);
  697. break;
  698. }
  699. if (likely(j < avalon->queued)) {
  700. info->idle = false;
  701. avalon_init_task(&at, 0, 0, info->fan_pwm,
  702. info->timeout, info->asic_count,
  703. info->miner_count, 1, 0, info->frequency);
  704. avalon_create_task(&at, avalon->works[i]);
  705. } else {
  706. idled++;
  707. avalon_init_task(&at, 0, 0, info->fan_pwm,
  708. info->timeout, info->asic_count,
  709. info->miner_count, 1, 1, info->frequency);
  710. }
  711. ret = avalon_send_task(&at, avalon);
  712. if (unlikely(ret == AVA_SEND_ERROR)) {
  713. applog(LOG_ERR, "AVA%i: Comms error(buffer)",
  714. avalon->device_id);
  715. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  716. info->reset = true;
  717. break;
  718. }
  719. }
  720. avalon_rotate_array(avalon);
  721. pthread_cond_signal(&info->qcond);
  722. mutex_unlock(&info->qlock);
  723. if (unlikely(idled && !info->idle)) {
  724. info->idle = true;
  725. applog(LOG_WARNING, "AVA%i: Idled %d miners",
  726. avalon->device_id, idled);
  727. }
  728. }
  729. return NULL;
  730. }
  731. static bool avalon_prepare(struct thr_info *thr)
  732. {
  733. struct cgpu_info *avalon = thr->cgpu;
  734. struct avalon_info *info = avalon->device_data;
  735. struct timeval now;
  736. free(avalon->works);
  737. avalon->works = calloc(info->miner_count * sizeof(struct work *),
  738. AVALON_ARRAY_SIZE);
  739. if (!avalon->works)
  740. quit(1, "Failed to calloc avalon works in avalon_prepare");
  741. info->thr = thr;
  742. mutex_init(&info->lock);
  743. mutex_init(&info->qlock);
  744. if (unlikely(pthread_cond_init(&info->qcond, NULL)))
  745. quit(1, "Failed to pthread_cond_init avalon qcond");
  746. cgsem_init(&info->read_sem);
  747. cgsem_init(&info->write_sem);
  748. if (pthread_create(&info->read_thr, NULL, avalon_get_results, (void *)avalon))
  749. quit(1, "Failed to create avalon read_thr");
  750. if (pthread_create(&info->write_thr, NULL, avalon_send_tasks, (void *)avalon))
  751. quit(1, "Failed to create avalon write_thr");
  752. avalon_init(avalon);
  753. cgtime(&now);
  754. get_datestamp(avalon->init, &now);
  755. return true;
  756. }
  757. static void do_avalon_close(struct thr_info *thr)
  758. {
  759. struct cgpu_info *avalon = thr->cgpu;
  760. struct avalon_info *info = avalon->device_data;
  761. pthread_join(info->read_thr, NULL);
  762. pthread_join(info->write_thr, NULL);
  763. avalon_running_reset(avalon, info);
  764. info->no_matching_work = 0;
  765. cgsem_destroy(&info->read_sem);
  766. cgsem_destroy(&info->write_sem);
  767. }
  768. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  769. {
  770. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  771. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  772. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  773. info->temp0 = ar->temp0;
  774. info->temp1 = ar->temp1;
  775. info->temp2 = ar->temp2;
  776. if (ar->temp0 & 0x80) {
  777. ar->temp0 &= 0x7f;
  778. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  779. }
  780. if (ar->temp1 & 0x80) {
  781. ar->temp1 &= 0x7f;
  782. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  783. }
  784. if (ar->temp2 & 0x80) {
  785. ar->temp2 &= 0x7f;
  786. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  787. }
  788. *temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
  789. if (info->temp0 > info->temp_max)
  790. info->temp_max = info->temp0;
  791. if (info->temp1 > info->temp_max)
  792. info->temp_max = info->temp1;
  793. if (info->temp2 > info->temp_max)
  794. info->temp_max = info->temp2;
  795. }
  796. static inline void adjust_fan(struct avalon_info *info)
  797. {
  798. int temp_new;
  799. temp_new = info->temp_sum / info->temp_history_count;
  800. if (temp_new < 35) {
  801. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  802. info->temp_old = temp_new;
  803. } else if (temp_new > 55) {
  804. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  805. info->temp_old = temp_new;
  806. } else if (abs(temp_new - info->temp_old) >= 2) {
  807. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM + (temp_new - 35) * 6.4;
  808. info->temp_old = temp_new;
  809. }
  810. }
  811. static void avalon_update_temps(struct cgpu_info *avalon, struct avalon_info *info,
  812. struct avalon_result *ar)
  813. {
  814. record_temp_fan(info, ar, &(avalon->temp));
  815. applog(LOG_INFO,
  816. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  817. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  818. info->fan0, info->fan1, info->fan2,
  819. info->temp0, info->temp1, info->temp2, info->temp_max);
  820. info->temp_history_index++;
  821. info->temp_sum += avalon->temp;
  822. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  823. info->temp_history_index, info->temp_history_count, info->temp_old);
  824. if (info->temp_history_index == info->temp_history_count) {
  825. adjust_fan(info);
  826. info->temp_history_index = 0;
  827. info->temp_sum = 0;
  828. }
  829. }
  830. static void get_avalon_statline_before(char *buf, struct cgpu_info *avalon)
  831. {
  832. struct avalon_info *info = avalon->device_data;
  833. int lowfan = 10000;
  834. /* Find the lowest fan speed of the ASIC cooling fans. */
  835. if (info->fan1 >= 0 && info->fan1 < lowfan)
  836. lowfan = info->fan1;
  837. if (info->fan2 >= 0 && info->fan2 < lowfan)
  838. lowfan = info->fan2;
  839. tailsprintf(buf, "%2d/%3dC %04dR | ", info->temp0, info->temp2, lowfan);
  840. }
  841. /* We use a replacement algorithm to only remove references to work done from
  842. * the buffer when we need the extra space for new work. */
  843. static bool avalon_fill(struct cgpu_info *avalon)
  844. {
  845. struct avalon_info *info = avalon->device_data;
  846. int subid, slot, mc;
  847. struct work *work;
  848. bool ret = true;
  849. mc = info->miner_count;
  850. mutex_lock(&info->qlock);
  851. if (avalon->queued >= mc)
  852. goto out_unlock;
  853. work = get_queued(avalon);
  854. if (unlikely(!work)) {
  855. ret = false;
  856. goto out_unlock;
  857. }
  858. subid = avalon->queued++;
  859. work->subid = subid;
  860. slot = avalon->work_array * mc + subid;
  861. if (likely(avalon->works[slot]))
  862. work_completed(avalon, avalon->works[slot]);
  863. avalon->works[slot] = work;
  864. if (avalon->queued < mc)
  865. ret = false;
  866. out_unlock:
  867. mutex_unlock(&info->qlock);
  868. return ret;
  869. }
  870. static int64_t avalon_scanhash(struct thr_info *thr)
  871. {
  872. struct cgpu_info *avalon = thr->cgpu;
  873. struct avalon_info *info = avalon->device_data;
  874. const int miner_count = info->miner_count;
  875. struct timeval now, then, tdiff;
  876. int64_t hash_count, us_timeout;
  877. struct timespec abstime;
  878. /* Full nonce range */
  879. us_timeout = 0x100000000ll / info->asic_count / info->frequency;
  880. tdiff.tv_sec = us_timeout / 1000000;
  881. tdiff.tv_usec = us_timeout - (tdiff.tv_sec * 1000000);
  882. cgtime(&now);
  883. timeradd(&now, &tdiff, &then);
  884. abstime.tv_sec = then.tv_sec;
  885. abstime.tv_nsec = then.tv_usec * 1000;
  886. /* Wait until avalon_send_tasks signals us that it has completed
  887. * sending its work or a full nonce range timeout has occurred */
  888. mutex_lock(&info->qlock);
  889. pthread_cond_timedwait(&info->qcond, &info->qlock, &abstime);
  890. mutex_unlock(&info->qlock);
  891. mutex_lock(&info->lock);
  892. hash_count = 0xffffffffull * (uint64_t)info->nonces;
  893. avalon->results += info->nonces;
  894. if (avalon->results > miner_count)
  895. avalon->results = miner_count;
  896. if (!info->idle && !info->reset)
  897. avalon->results -= miner_count / 3;
  898. else
  899. avalon->results = miner_count;
  900. info->nonces = 0;
  901. mutex_unlock(&info->lock);
  902. /* Check for nothing but consecutive bad results or consistently less
  903. * results than we should be getting and reset the FPGA if necessary */
  904. if (avalon->results < -miner_count && !info->reset) {
  905. applog(LOG_ERR, "AVA%d: Result return rate low, resetting!",
  906. avalon->device_id);
  907. info->reset = true;
  908. }
  909. if (unlikely(avalon->usbinfo.nodev)) {
  910. applog(LOG_ERR, "AVA%d: Device disappeared, shutting down thread",
  911. avalon->device_id);
  912. avalon->shutdown = true;
  913. }
  914. /* This hashmeter is just a utility counter based on returned shares */
  915. return hash_count;
  916. }
  917. static void avalon_flush_work(struct cgpu_info *avalon)
  918. {
  919. struct avalon_info *info = avalon->device_data;
  920. mutex_lock(&info->qlock);
  921. /* Will overwrite any work queued */
  922. avalon->queued = 0;
  923. pthread_cond_signal(&info->qcond);
  924. mutex_unlock(&info->qlock);
  925. }
  926. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  927. {
  928. struct api_data *root = NULL;
  929. struct avalon_info *info = cgpu->device_data;
  930. int i;
  931. root = api_add_int(root, "baud", &(info->baud), false);
  932. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  933. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  934. root = api_add_int(root, "timeout", &(info->timeout), false);
  935. root = api_add_int(root, "frequency", &(info->frequency), false);
  936. root = api_add_int(root, "fan1", &(info->fan0), false);
  937. root = api_add_int(root, "fan2", &(info->fan1), false);
  938. root = api_add_int(root, "fan3", &(info->fan2), false);
  939. root = api_add_int(root, "temp1", &(info->temp0), false);
  940. root = api_add_int(root, "temp2", &(info->temp1), false);
  941. root = api_add_int(root, "temp3", &(info->temp2), false);
  942. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  943. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  944. for (i = 0; i < info->miner_count; i++) {
  945. char mcw[24];
  946. sprintf(mcw, "match_work_count%d", i + 1);
  947. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  948. }
  949. return root;
  950. }
  951. static void avalon_shutdown(struct thr_info *thr)
  952. {
  953. do_avalon_close(thr);
  954. }
  955. struct device_drv avalon_drv = {
  956. .drv_id = DRIVER_AVALON,
  957. .dname = "avalon",
  958. .name = "AVA",
  959. .drv_detect = avalon_detect,
  960. .thread_prepare = avalon_prepare,
  961. .hash_work = hash_queued_work,
  962. .queue_full = avalon_fill,
  963. .scanwork = avalon_scanhash,
  964. .flush_work = avalon_flush_work,
  965. .get_api_stats = avalon_api_stats,
  966. .get_statline_before = get_avalon_statline_before,
  967. .reinit_device = avalon_init,
  968. .thread_shutdown = avalon_shutdown,
  969. };