driver-avalon.c 24 KB

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