driver-avalon.c 29 KB

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