driver-avalon.c 26 KB

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