driver-avalon.c 24 KB

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