driver-avalon.c 25 KB

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