driver-avalon.c 29 KB

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