driver-avalon.c 26 KB

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