driver-avalon.c 27 KB

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