driver-avalon.c 25 KB

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