driver-avalon.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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 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
  359. const char *avalon_set_baud(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  360. {
  361. struct avalon_info * const info = proc->device_data;
  362. const int baud = atoi(newvalue);
  363. if (!valid_baud(baud))
  364. return "Invalid baud setting";
  365. info->baud = baud;
  366. return NULL;
  367. }
  368. static
  369. const char *avalon_set_miner_count(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  370. {
  371. struct avalon_info * const info = proc->device_data;
  372. const int miner_count = atoi(newvalue);
  373. if (miner_count <= 0 || miner_count > AVALON_DEFAULT_MINER_NUM)
  374. return "Invalid miner_count: must be 1 ~ " AVALON_DEFAULT_MINER_NUM_S;
  375. info->miner_count = miner_count;
  376. return NULL;
  377. }
  378. static
  379. const char *avalon_set_asic_count(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  380. {
  381. struct avalon_info * const info = proc->device_data;
  382. const int asic_count = atoi(newvalue);
  383. if (asic_count <= 0 || asic_count > AVALON_DEFAULT_ASIC_NUM)
  384. return "Invalid asic_count: must be 1 ~ " AVALON_DEFAULT_ASIC_NUM_S;
  385. info->asic_count = asic_count;
  386. return NULL;
  387. }
  388. static
  389. const char *avalon_set_timeout(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  390. {
  391. struct avalon_info * const info = proc->device_data;
  392. const int timeout = atoi(newvalue);
  393. if (timeout <= 0 || timeout > 0xff)
  394. return "Invalid timeout: must be 1 ~ 255";
  395. info->timeout = timeout;
  396. return NULL;
  397. }
  398. static
  399. const char *avalon_set_clock(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  400. {
  401. struct avalon_info * const info = proc->device_data;
  402. const int clock = atoi(newvalue);
  403. switch (clock) {
  404. default:
  405. return "Invalid clock: must be 256/270/282/300/325/350/375";
  406. case 256:
  407. case 270:
  408. case 282:
  409. case 300:
  410. case 325:
  411. case 350:
  412. case 375:
  413. info->frequency = clock;
  414. }
  415. return NULL;
  416. }
  417. const struct bfg_set_device_definition avalon_set_device_funcs[] = {
  418. // NOTE: Order of parameters below is important for --avalon-options
  419. {"baud" , avalon_set_baud , "serial baud rate"},
  420. {"miner_count", avalon_set_miner_count, ""},
  421. {"asic_count" , avalon_set_asic_count , ""},
  422. {"timeout" , avalon_set_timeout , "how long the device will work on a work item before accepting new work"},
  423. {"clock" , avalon_set_clock , "clock speed: 256, 270, 282, 300, 325, 350, or 375"},
  424. {NULL},
  425. };
  426. /* Non blocking clearing of anything in the buffer */
  427. static void avalon_clear_readbuf(int fd)
  428. {
  429. ssize_t ret;
  430. do {
  431. char buf[AVALON_FTDI_READSIZE];
  432. #ifndef WIN32
  433. struct timeval timeout;
  434. fd_set rd;
  435. timeout.tv_sec = timeout.tv_usec = 0;
  436. FD_ZERO(&rd);
  437. FD_SET((SOCKETTYPE)fd, &rd);
  438. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  439. if (ret > 0)
  440. #endif
  441. // Relies on serial timeout for Windows
  442. ret = read(fd, buf, AVALON_FTDI_READSIZE);
  443. } while (ret > 0);
  444. }
  445. static bool avalon_detect_one(const char *devpath)
  446. {
  447. struct avalon_info *info;
  448. struct avalon_result ar;
  449. int fd, ret;
  450. struct cgpu_info *avalon;
  451. if (serial_claim(devpath, &avalon_drv))
  452. return false;
  453. info = malloc(sizeof(*info));
  454. if (unlikely(!info))
  455. applogr(false, LOG_ERR, "Failed to malloc avalon_info data");
  456. *info = (struct avalon_info){
  457. .baud = AVALON_IO_SPEED,
  458. .miner_count = AVALON_DEFAULT_MINER_NUM - 8,
  459. .asic_count = AVALON_DEFAULT_ASIC_NUM,
  460. .timeout = AVALON_DEFAULT_TIMEOUT,
  461. .frequency = AVALON_DEFAULT_FREQUENCY,
  462. };
  463. drv_set_defaults(&avalon_drv, avalon_set_device_funcs, info, devpath, detectone_meta_info.serial, 1);
  464. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s "
  465. "(baud=%d miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  466. devpath, info->baud, info->miner_count, info->asic_count, info->timeout, info->frequency);
  467. fd = avalon_open2(devpath, info->baud, true);
  468. if (unlikely(fd == -1)) {
  469. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  470. free(info);
  471. return false;
  472. }
  473. avalon_clear_readbuf(fd);
  474. /* We have a real Avalon! */
  475. avalon = calloc(1, sizeof(struct cgpu_info));
  476. avalon->drv = &avalon_drv;
  477. avalon->device_path = strdup(devpath);
  478. avalon->device_fd = fd;
  479. avalon->threads = AVALON_MINER_THREADS;
  480. avalon->set_device_funcs = avalon_set_device_funcs;
  481. add_cgpu(avalon);
  482. ret = avalon_reset(fd, &ar);
  483. if (ret) {
  484. ; /* FIXME: I think IT IS avalon and wait on reset;
  485. * avalon_close(fd);
  486. * free(info);
  487. * return false; */
  488. }
  489. applog(LOG_INFO, "Avalon Detect: Found at %s, mark as %d",
  490. devpath, avalon->device_id);
  491. avalon->device_data = info;
  492. info->read_count = ((float)info->timeout * AVALON_HASH_TIME_FACTOR *
  493. AVALON_TIME_FACTOR) / (float)info->miner_count;
  494. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  495. info->temp_max = 0;
  496. /* This is for check the temp/fan every 3~4s */
  497. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  498. if (info->temp_history_count <= 0)
  499. info->temp_history_count = 1;
  500. info->temp_history_index = 0;
  501. info->temp_sum = 0;
  502. info->temp_old = 0;
  503. /* Set asic to idle mode after detect */
  504. avalon_idle(avalon);
  505. avalon->device_fd = -1;
  506. avalon_close(fd);
  507. return true;
  508. }
  509. static
  510. bool avalon_lowl_probe(const struct lowlevel_device_info * const info)
  511. {
  512. return vcom_lowl_probe_wrapper(info, avalon_detect_one);
  513. }
  514. static void __avalon_init(struct cgpu_info *avalon)
  515. {
  516. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  517. }
  518. static void avalon_init(struct cgpu_info *avalon)
  519. {
  520. struct avalon_info *info = avalon->device_data;
  521. struct avalon_result ar;
  522. int fd, ret;
  523. cgpu_set_defaults(avalon);
  524. avalon->set_device_funcs = NULL;
  525. avalon->device_fd = -1;
  526. fd = avalon_open(avalon->device_path, info->baud);
  527. if (unlikely(fd == -1)) {
  528. applog(LOG_ERR, "Avalon: Failed to open on %s",
  529. avalon->device_path);
  530. return;
  531. }
  532. ret = avalon_reset(fd, &ar);
  533. if (ret) {
  534. avalon_close(fd);
  535. return;
  536. }
  537. avalon->device_fd = fd;
  538. __avalon_init(avalon);
  539. }
  540. static bool avalon_prepare(struct thr_info *thr)
  541. {
  542. struct cgpu_info *avalon = thr->cgpu;
  543. struct avalon_info *info = avalon->device_data;
  544. free(avalon->works);
  545. avalon->works = calloc(info->miner_count * sizeof(struct work *),
  546. AVALON_ARRAY_SIZE);
  547. if (!avalon->works)
  548. quithere(1, "Failed to calloc avalon works");
  549. if (avalon->device_fd == -1)
  550. avalon_init(avalon);
  551. else
  552. __avalon_init(avalon);
  553. avalon->status = LIFE_INIT2;
  554. return true;
  555. }
  556. static void avalon_free_work(struct thr_info *thr)
  557. {
  558. struct cgpu_info *avalon;
  559. struct avalon_info *info;
  560. struct work **works;
  561. int i;
  562. avalon = thr->cgpu;
  563. avalon->queued = 0;
  564. if (unlikely(!avalon->works))
  565. return;
  566. works = avalon->works;
  567. info = avalon->device_data;
  568. for (i = 0; i < info->miner_count * 4; i++) {
  569. if (works[i]) {
  570. work_completed(avalon, works[i]);
  571. works[i] = NULL;
  572. }
  573. }
  574. }
  575. static void do_avalon_close(struct thr_info *thr)
  576. {
  577. struct avalon_result ar;
  578. struct cgpu_info *avalon = thr->cgpu;
  579. struct avalon_info *info = avalon->device_data;
  580. avalon_free_work(thr);
  581. cgsleep_ms(1000);
  582. avalon_reset(avalon->device_fd, &ar);
  583. avalon_idle(avalon);
  584. avalon_close(avalon->device_fd);
  585. avalon->device_fd = -1;
  586. info->no_matching_work = 0;
  587. }
  588. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  589. {
  590. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  591. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  592. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  593. info->temp0 = ar->temp0;
  594. info->temp1 = ar->temp1;
  595. info->temp2 = ar->temp2;
  596. if (ar->temp0 & 0x80) {
  597. ar->temp0 &= 0x7f;
  598. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  599. }
  600. if (ar->temp1 & 0x80) {
  601. ar->temp1 &= 0x7f;
  602. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  603. }
  604. if (ar->temp2 & 0x80) {
  605. ar->temp2 &= 0x7f;
  606. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  607. }
  608. *temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
  609. if (info->temp0 > info->temp_max)
  610. info->temp_max = info->temp0;
  611. if (info->temp1 > info->temp_max)
  612. info->temp_max = info->temp1;
  613. if (info->temp2 > info->temp_max)
  614. info->temp_max = info->temp2;
  615. }
  616. static inline void adjust_fan(struct avalon_info *info)
  617. {
  618. int temp_new;
  619. temp_new = info->temp_sum / info->temp_history_count;
  620. if (temp_new < 35) {
  621. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  622. info->temp_old = temp_new;
  623. } else if (temp_new > 55) {
  624. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  625. info->temp_old = temp_new;
  626. } else if (abs(temp_new - info->temp_old) >= 2) {
  627. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM + (temp_new - 35) * 6.4;
  628. info->temp_old = temp_new;
  629. }
  630. }
  631. /* We use a replacement algorithm to only remove references to work done from
  632. * the buffer when we need the extra space for new work. */
  633. static bool avalon_fill(struct cgpu_info *avalon)
  634. {
  635. struct avalon_info *info = avalon->device_data;
  636. int subid, slot, mc;
  637. struct work *work;
  638. mc = info->miner_count;
  639. if (avalon->queued >= mc)
  640. return true;
  641. work = get_queued(avalon);
  642. if (unlikely(!work))
  643. return false;
  644. subid = avalon->queued++;
  645. work->subid = subid;
  646. slot = avalon->work_array * mc + subid;
  647. if (likely(avalon->works[slot]))
  648. work_completed(avalon, avalon->works[slot]);
  649. avalon->works[slot] = work;
  650. if (avalon->queued >= mc)
  651. return true;
  652. return false;
  653. }
  654. static void avalon_rotate_array(struct cgpu_info *avalon)
  655. {
  656. avalon->queued = 0;
  657. if (++avalon->work_array >= AVALON_ARRAY_SIZE)
  658. avalon->work_array = 0;
  659. }
  660. static int64_t avalon_scanhash(struct thr_info *thr)
  661. {
  662. struct cgpu_info *avalon;
  663. struct work **works;
  664. int fd, ret = AVA_GETS_OK, full;
  665. struct avalon_info *info;
  666. struct avalon_task at;
  667. struct avalon_result ar;
  668. int i;
  669. int avalon_get_work_count;
  670. int start_count, end_count;
  671. struct timeval tv_start, tv_finish, elapsed;
  672. uint32_t nonce;
  673. int64_t hash_count;
  674. static int first_try = 0;
  675. int result_wrong;
  676. avalon = thr->cgpu;
  677. works = avalon->works;
  678. info = avalon->device_data;
  679. avalon_get_work_count = info->miner_count;
  680. if (unlikely(avalon->device_fd == -1)) {
  681. if (!avalon_prepare(thr)) {
  682. applog(LOG_ERR, "AVA%i: Comms error(open)",
  683. avalon->device_id);
  684. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  685. /* fail the device if the reopen attempt fails */
  686. return -1;
  687. }
  688. }
  689. fd = avalon->device_fd;
  690. #ifndef WIN32
  691. tcflush(fd, TCOFLUSH);
  692. #endif
  693. start_count = avalon->work_array * avalon_get_work_count;
  694. end_count = start_count + avalon_get_work_count;
  695. i = start_count;
  696. while (true) {
  697. avalon_init_task(&at, 0, 0, info->fan_pwm,
  698. info->timeout, info->asic_count,
  699. info->miner_count, 1, 0, info->frequency);
  700. avalon_create_task(&at, works[i]);
  701. ret = avalon_send_task(fd, &at, avalon);
  702. if (unlikely(ret == AVA_SEND_ERROR ||
  703. (ret == AVA_SEND_BUFFER_EMPTY &&
  704. (i + 1 == end_count) &&
  705. first_try))) {
  706. do_avalon_close(thr);
  707. applog(LOG_ERR, "AVA%i: Comms error(buffer)",
  708. avalon->device_id);
  709. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  710. first_try = 0;
  711. cgsleep_ms(1000);
  712. avalon_init(avalon);
  713. return 0; /* This should never happen */
  714. }
  715. if (ret == AVA_SEND_BUFFER_EMPTY && (i + 1 == end_count)) {
  716. first_try = 1;
  717. avalon_rotate_array(avalon);
  718. return 0xffffffff;
  719. }
  720. works[i]->blk.nonce = 0xffffffff;
  721. if (ret == AVA_SEND_BUFFER_FULL)
  722. break;
  723. i++;
  724. }
  725. if (unlikely(first_try))
  726. first_try = 0;
  727. elapsed.tv_sec = elapsed.tv_usec = 0;
  728. cgtime(&tv_start);
  729. result_wrong = 0;
  730. hash_count = 0;
  731. while (true) {
  732. full = avalon_buffer_full(fd);
  733. applog(LOG_DEBUG, "Avalon: Buffer full: %s",
  734. ((full == AVA_BUFFER_FULL) ? "Yes" : "No"));
  735. if (unlikely(full == AVA_BUFFER_EMPTY))
  736. break;
  737. ret = avalon_get_result(fd, &ar, thr, &tv_finish);
  738. if (unlikely(ret == AVA_GETS_ERROR)) {
  739. do_avalon_close(thr);
  740. applog(LOG_ERR,
  741. "AVA%i: Comms error(read)", avalon->device_id);
  742. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  743. return 0;
  744. }
  745. if (unlikely(ret == AVA_GETS_RESTART))
  746. break;
  747. if (unlikely(ret == AVA_GETS_TIMEOUT)) {
  748. timersub(&tv_finish, &tv_start, &elapsed);
  749. applog(LOG_DEBUG, "Avalon: no nonce in (%ld.%06lds)",
  750. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  751. continue;
  752. }
  753. if (!avalon_decode_nonce(thr, &ar, &nonce)) {
  754. info->no_matching_work++;
  755. result_wrong++;
  756. if (unlikely(result_wrong >= avalon_get_work_count))
  757. break;
  758. if (opt_debug) {
  759. timersub(&tv_finish, &tv_start, &elapsed);
  760. applog(LOG_DEBUG,"Avalon: no matching work: %d"
  761. " (%ld.%06lds)", info->no_matching_work,
  762. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  763. }
  764. continue;
  765. }
  766. hash_count += 0xffffffff;
  767. if (opt_debug) {
  768. timersub(&tv_finish, &tv_start, &elapsed);
  769. applog(LOG_DEBUG,
  770. "Avalon: nonce = 0x%08"PRIx32" = 0x%08"PRIx64" hashes "
  771. "(%ld.%06lds)", nonce, (uint64_t)hash_count,
  772. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  773. }
  774. }
  775. if (hash_count && avalon->results < AVALON_ARRAY_SIZE)
  776. avalon->results++;
  777. if (unlikely((result_wrong >= avalon_get_work_count) ||
  778. (!hash_count && ret != AVA_GETS_RESTART && --avalon->results < 0))) {
  779. /* Look for all invalid results, or consecutive failure
  780. * to generate any results suggesting the FPGA
  781. * controller has screwed up. */
  782. do_avalon_close(thr);
  783. applog(LOG_ERR,
  784. "AVA%i: FPGA controller messed up, %d wrong results",
  785. avalon->device_id, result_wrong);
  786. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  787. cgsleep_ms(1000);
  788. avalon_init(avalon);
  789. return 0;
  790. }
  791. avalon_rotate_array(avalon);
  792. if (hash_count) {
  793. record_temp_fan(info, &ar, &(avalon->temp));
  794. avalon->temp = info->temp_max;
  795. applog(LOG_INFO,
  796. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  797. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  798. info->fan0, info->fan1, info->fan2,
  799. info->temp0, info->temp1, info->temp2, info->temp_max);
  800. info->temp_history_index++;
  801. info->temp_sum += avalon->temp;
  802. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  803. info->temp_history_index, info->temp_history_count, info->temp_old);
  804. if (info->temp_history_index == info->temp_history_count) {
  805. adjust_fan(info);
  806. info->temp_history_index = 0;
  807. info->temp_sum = 0;
  808. }
  809. }
  810. /* This hashmeter is just a utility counter based on returned shares */
  811. return hash_count;
  812. }
  813. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  814. {
  815. struct api_data *root = NULL;
  816. struct avalon_info *info = cgpu->device_data;
  817. int i;
  818. root = api_add_int(root, "baud", &(info->baud), false);
  819. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  820. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  821. root = api_add_int(root, "read_count", &(info->read_count), false);
  822. root = api_add_int(root, "timeout", &(info->timeout), false);
  823. root = api_add_int(root, "frequency", &(info->frequency), false);
  824. root = api_add_int(root, "fan1", &(info->fan0), false);
  825. root = api_add_int(root, "fan2", &(info->fan1), false);
  826. root = api_add_int(root, "fan3", &(info->fan2), false);
  827. root = api_add_int(root, "temp1", &(info->temp0), false);
  828. root = api_add_int(root, "temp2", &(info->temp1), false);
  829. root = api_add_int(root, "temp3", &(info->temp2), false);
  830. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  831. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  832. for (i = 0; i < info->miner_count; i++) {
  833. char mcw[24];
  834. sprintf(mcw, "match_work_count%d", i + 1);
  835. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  836. }
  837. return root;
  838. }
  839. static void avalon_shutdown(struct thr_info *thr)
  840. {
  841. do_avalon_close(thr);
  842. }
  843. struct device_drv avalon_drv = {
  844. .dname = "avalon",
  845. .name = "AVA",
  846. .lowl_probe_by_name_only = true,
  847. .lowl_probe = avalon_lowl_probe,
  848. .thread_prepare = avalon_prepare,
  849. .minerloop = hash_queued_work,
  850. .queue_full = avalon_fill,
  851. .scanwork = avalon_scanhash,
  852. .get_api_stats = avalon_api_stats,
  853. .reinit_device = avalon_init,
  854. .thread_shutdown = avalon_shutdown,
  855. };