driver-avalon.c 28 KB

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