driver-avalon.c 29 KB

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