driver-avalon.c 25 KB

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