driver-avalon.c 25 KB

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