driver-avalon.c 28 KB

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