driver-avalon.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * Copyright 2012-2013 Xiangfu
  3. * Copyright 2013 Con Kolivas <kernel@kolivas.org>
  4. * Copyright 2012 Luke Dashjr
  5. * Copyright 2012 Andrew Smith
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #include "config.h"
  13. #include <limits.h>
  14. #include <pthread.h>
  15. #include <stdio.h>
  16. #include <sys/time.h>
  17. #include <sys/types.h>
  18. #include <dirent.h>
  19. #include <unistd.h>
  20. #ifndef WIN32
  21. #include <sys/select.h>
  22. #include <termios.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #ifndef O_CLOEXEC
  26. #define O_CLOEXEC 0
  27. #endif
  28. #else
  29. #include "compat.h"
  30. #include <windows.h>
  31. #include <io.h>
  32. #endif
  33. #include "deviceapi.h"
  34. #include "miner.h"
  35. #include "fpgautils.h"
  36. #include "driver-avalon.h"
  37. #include "logging.h"
  38. #include "util.h"
  39. static int option_offset = -1;
  40. struct device_drv avalon_drv;
  41. static int avalon_init_task(struct avalon_task *at,
  42. uint8_t reset, uint8_t ff, uint8_t fan,
  43. uint8_t timeout, uint8_t asic_num,
  44. uint8_t miner_num, uint8_t nonce_elf,
  45. uint8_t gate_miner, int frequency)
  46. {
  47. uint8_t *buf;
  48. static bool first = true;
  49. if (unlikely(!at))
  50. return -1;
  51. if (unlikely(timeout <= 0 || asic_num <= 0 || miner_num <= 0))
  52. return -1;
  53. memset(at, 0, sizeof(struct avalon_task));
  54. if (unlikely(reset)) {
  55. at->reset = 1;
  56. at->fan_eft = 1;
  57. at->timer_eft = 1;
  58. first = true;
  59. }
  60. at->flush_fifo = (ff ? 1 : 0);
  61. at->fan_eft = (fan ? 1 : 0);
  62. if (unlikely(first && !at->reset)) {
  63. at->fan_eft = 1;
  64. at->timer_eft = 1;
  65. first = false;
  66. }
  67. at->fan_pwm_data = (fan ? fan : AVALON_DEFAULT_FAN_MAX_PWM);
  68. at->timeout_data = timeout;
  69. at->asic_num = asic_num;
  70. at->miner_num = miner_num;
  71. at->nonce_elf = nonce_elf;
  72. at->gate_miner_elf = 1;
  73. at->asic_pll = 1;
  74. if (unlikely(gate_miner)) {
  75. at-> gate_miner = 1;
  76. at->asic_pll = 0;
  77. }
  78. buf = (uint8_t *)at;
  79. buf[5] = 0x00;
  80. buf[8] = 0x74;
  81. buf[9] = 0x01;
  82. buf[10] = 0x00;
  83. buf[11] = 0x00;
  84. if (frequency == 256) {
  85. buf[6] = 0x03;
  86. buf[7] = 0x08;
  87. } else if (frequency == 270) {
  88. buf[6] = 0x73;
  89. buf[7] = 0x08;
  90. } else if (frequency == 282) {
  91. buf[6] = 0xd3;
  92. buf[7] = 0x08;
  93. } else if (frequency == 300) {
  94. buf[6] = 0x63;
  95. buf[7] = 0x09;
  96. }
  97. return 0;
  98. }
  99. static inline void avalon_create_task(struct avalon_task *at,
  100. struct work *work)
  101. {
  102. memcpy(at->midstate, work->midstate, 32);
  103. memcpy(at->data, work->data + 64, 12);
  104. }
  105. static int avalon_send_task(int fd, const struct avalon_task *at,
  106. struct cgpu_info *avalon)
  107. {
  108. size_t ret;
  109. int full;
  110. struct timespec p;
  111. uint8_t buf[AVALON_WRITE_SIZE + 4 * AVALON_DEFAULT_ASIC_NUM];
  112. size_t nr_len;
  113. struct avalon_info *info;
  114. uint64_t delay = 32000000; /* Default 32ms for B19200 */
  115. uint32_t nonce_range;
  116. int i;
  117. if (at->nonce_elf)
  118. nr_len = AVALON_WRITE_SIZE + 4 * at->asic_num;
  119. else
  120. nr_len = AVALON_WRITE_SIZE;
  121. memcpy(buf, at, AVALON_WRITE_SIZE);
  122. if (at->nonce_elf) {
  123. nonce_range = (uint32_t)0xffffffff / at->asic_num;
  124. for (i = 0; i < at->asic_num; i++) {
  125. buf[AVALON_WRITE_SIZE + (i * 4) + 3] =
  126. (i * nonce_range & 0xff000000) >> 24;
  127. buf[AVALON_WRITE_SIZE + (i * 4) + 2] =
  128. (i * nonce_range & 0x00ff0000) >> 16;
  129. buf[AVALON_WRITE_SIZE + (i * 4) + 1] =
  130. (i * nonce_range & 0x0000ff00) >> 8;
  131. buf[AVALON_WRITE_SIZE + (i * 4) + 0] =
  132. (i * nonce_range & 0x000000ff) >> 0;
  133. }
  134. }
  135. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  136. uint8_t tt = 0;
  137. tt = (buf[0] & 0x0f) << 4;
  138. tt |= ((buf[0] & 0x10) ? (1 << 3) : 0);
  139. tt |= ((buf[0] & 0x20) ? (1 << 2) : 0);
  140. tt |= ((buf[0] & 0x40) ? (1 << 1) : 0);
  141. tt |= ((buf[0] & 0x80) ? (1 << 0) : 0);
  142. buf[0] = tt;
  143. tt = (buf[4] & 0x0f) << 4;
  144. tt |= ((buf[4] & 0x10) ? (1 << 3) : 0);
  145. tt |= ((buf[4] & 0x20) ? (1 << 2) : 0);
  146. tt |= ((buf[4] & 0x40) ? (1 << 1) : 0);
  147. tt |= ((buf[4] & 0x80) ? (1 << 0) : 0);
  148. buf[4] = tt;
  149. #endif
  150. if (likely(avalon)) {
  151. info = avalon->device_data;
  152. delay = nr_len * 10 * 1000000000ULL;
  153. delay = delay / info->baud;
  154. }
  155. if (at->reset)
  156. nr_len = 1;
  157. if (opt_debug) {
  158. applog(LOG_DEBUG, "Avalon: Sent(%u):", (unsigned int)nr_len);
  159. hexdump((uint8_t *)buf, nr_len);
  160. }
  161. ret = write(fd, buf, nr_len);
  162. if (unlikely(ret != nr_len))
  163. return AVA_SEND_ERROR;
  164. p.tv_sec = 0;
  165. p.tv_nsec = (long)delay + 4000000;
  166. nanosleep(&p, NULL);
  167. applog(LOG_DEBUG, "Avalon: Sent: Buffer delay: %ld", p.tv_nsec);
  168. full = avalon_buffer_full(fd);
  169. applog(LOG_DEBUG, "Avalon: Sent: Buffer full: %s",
  170. ((full == AVA_BUFFER_FULL) ? "Yes" : "No"));
  171. if (unlikely(full == AVA_BUFFER_FULL))
  172. return AVA_SEND_BUFFER_FULL;
  173. return AVA_SEND_BUFFER_EMPTY;
  174. }
  175. static inline int avalon_gets(int fd, uint8_t *buf, int read_count,
  176. struct thr_info *thr, struct timeval *tv_finish)
  177. {
  178. ssize_t ret = 0;
  179. int rc = 0;
  180. int read_amount = AVALON_READ_SIZE;
  181. bool first = true;
  182. /* Read reply 1 byte at a time to get earliest tv_finish */
  183. while (true) {
  184. ret = read(fd, buf, 1);
  185. if (ret < 0)
  186. {
  187. applog(LOG_ERR, "Avalon: Error %d on read in avalon_gets", errno);
  188. return AVA_GETS_ERROR;
  189. }
  190. if (first && likely(tv_finish))
  191. cgtime(tv_finish);
  192. if (ret >= read_amount)
  193. return AVA_GETS_OK;
  194. if (ret > 0) {
  195. buf += ret;
  196. read_amount -= ret;
  197. first = false;
  198. continue;
  199. }
  200. if (thr && thr->work_restart) {
  201. if (opt_debug) {
  202. applog(LOG_WARNING,
  203. "Avalon: Work restart at %.2f seconds",
  204. (float)(rc)/(float)AVALON_TIME_FACTOR);
  205. }
  206. return AVA_GETS_RESTART;
  207. }
  208. rc++;
  209. if (rc >= read_count) {
  210. if (opt_debug) {
  211. applog(LOG_WARNING,
  212. "Avalon: No data in %.2f seconds",
  213. (float)rc/(float)AVALON_TIME_FACTOR);
  214. }
  215. return AVA_GETS_TIMEOUT;
  216. }
  217. }
  218. }
  219. static int avalon_get_result(int fd, struct avalon_result *ar,
  220. struct thr_info *thr, struct timeval *tv_finish)
  221. {
  222. struct cgpu_info *avalon;
  223. struct avalon_info *info;
  224. uint8_t result[AVALON_READ_SIZE];
  225. int ret, read_count;
  226. avalon = thr->cgpu;
  227. info = avalon->device_data;
  228. read_count = info->read_count;
  229. memset(result, 0, AVALON_READ_SIZE);
  230. ret = avalon_gets(fd, result, read_count, thr, tv_finish);
  231. if (ret == AVA_GETS_OK) {
  232. if (opt_debug) {
  233. applog(LOG_DEBUG, "Avalon: get:");
  234. hexdump((uint8_t *)result, AVALON_READ_SIZE);
  235. }
  236. memcpy((uint8_t *)ar, result, AVALON_READ_SIZE);
  237. }
  238. return ret;
  239. }
  240. static bool avalon_decode_nonce(struct thr_info *thr, struct avalon_result *ar,
  241. uint32_t *nonce)
  242. {
  243. struct cgpu_info *avalon;
  244. struct avalon_info *info;
  245. struct work *work;
  246. avalon = thr->cgpu;
  247. if (unlikely(!avalon->works))
  248. return false;
  249. work = find_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
  250. (char *)ar->data, 64, 12);
  251. if (!work)
  252. return false;
  253. info = avalon->device_data;
  254. info->matching_work[work->subid]++;
  255. *nonce = htole32(ar->nonce);
  256. submit_nonce(thr, work, *nonce);
  257. return true;
  258. }
  259. static void avalon_get_reset(int fd, struct avalon_result *ar)
  260. {
  261. int ret;
  262. const int read_count = AVALON_RESET_FAULT_DECISECONDS * AVALON_TIME_FACTOR;
  263. memset(ar, 0, AVALON_READ_SIZE);
  264. ret = avalon_gets(fd, (uint8_t*)ar, read_count, NULL, NULL);
  265. if (ret == AVA_GETS_OK && opt_debug) {
  266. applog(LOG_DEBUG, "Avalon: get:");
  267. hexdump((uint8_t *)ar, AVALON_READ_SIZE);
  268. }
  269. }
  270. static int avalon_reset(int fd, struct avalon_result *ar)
  271. {
  272. struct avalon_task at;
  273. uint8_t *buf;
  274. int ret, i = 0;
  275. struct timespec p;
  276. avalon_init_task(&at, 1, 0,
  277. AVALON_DEFAULT_FAN_MAX_PWM,
  278. AVALON_DEFAULT_TIMEOUT,
  279. AVALON_DEFAULT_ASIC_NUM,
  280. AVALON_DEFAULT_MINER_NUM,
  281. 0, 0,
  282. AVALON_DEFAULT_FREQUENCY);
  283. ret = avalon_send_task(fd, &at, NULL);
  284. if (ret == AVA_SEND_ERROR)
  285. return 1;
  286. avalon_get_reset(fd, ar);
  287. buf = (uint8_t *)ar;
  288. /* Sometimes there is one extra 0 byte for some reason in the buffer,
  289. * so work around it. */
  290. if (buf[0] == 0)
  291. buf = (uint8_t *)(ar + 1);
  292. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  293. buf[2] == 0xAA && buf[3] == 0x55) {
  294. for (i = 4; i < 11; i++)
  295. if (buf[i] != 0)
  296. break;
  297. }
  298. p.tv_sec = 0;
  299. p.tv_nsec = AVALON_RESET_PITCH;
  300. nanosleep(&p, NULL);
  301. if (i != 11) {
  302. applog(LOG_ERR, "Avalon: Reset failed! not an Avalon?"
  303. " (%d: %02x %02x %02x %02x)",
  304. i, buf[0], buf[1], buf[2], buf[3]);
  305. /* FIXME: return 1; */
  306. } else
  307. applog(LOG_WARNING, "Avalon: Reset succeeded");
  308. return 0;
  309. }
  310. static void avalon_idle(struct cgpu_info *avalon)
  311. {
  312. int i, ret;
  313. struct avalon_task at;
  314. int fd = avalon->device_fd;
  315. struct avalon_info *info = avalon->device_data;
  316. int avalon_get_work_count = info->miner_count;
  317. i = 0;
  318. while (true) {
  319. avalon_init_task(&at, 0, 0, info->fan_pwm,
  320. info->timeout, info->asic_count,
  321. info->miner_count, 1, 1, info->frequency);
  322. ret = avalon_send_task(fd, &at, avalon);
  323. if (unlikely(ret == AVA_SEND_ERROR ||
  324. (ret == AVA_SEND_BUFFER_EMPTY &&
  325. (i + 1 == avalon_get_work_count * 2)))) {
  326. applog(LOG_ERR, "AVA%i: Comms error", avalon->device_id);
  327. return;
  328. }
  329. if (i + 1 == avalon_get_work_count * 2)
  330. break;
  331. if (ret == AVA_SEND_BUFFER_FULL)
  332. break;
  333. i++;
  334. }
  335. applog(LOG_ERR, "Avalon: Goto idle mode");
  336. }
  337. static void get_options(int this_option_offset, int *baud, int *miner_count,
  338. int *asic_count, int *timeout, int *frequency)
  339. {
  340. char buf[BUFSIZ+1];
  341. char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
  342. size_t max;
  343. int i, tmp;
  344. if (opt_avalon_options == NULL)
  345. buf[0] = '\0';
  346. else {
  347. ptr = opt_avalon_options;
  348. for (i = 0; i < this_option_offset; i++) {
  349. comma = strchr(ptr, ',');
  350. if (comma == NULL)
  351. break;
  352. ptr = comma + 1;
  353. }
  354. comma = strchr(ptr, ',');
  355. if (comma == NULL)
  356. max = strlen(ptr);
  357. else
  358. max = comma - ptr;
  359. if (max > BUFSIZ)
  360. max = BUFSIZ;
  361. strncpy(buf, ptr, max);
  362. buf[max] = '\0';
  363. }
  364. *baud = AVALON_IO_SPEED;
  365. *miner_count = AVALON_DEFAULT_MINER_NUM - 8;
  366. *asic_count = AVALON_DEFAULT_ASIC_NUM;
  367. *timeout = AVALON_DEFAULT_TIMEOUT;
  368. *frequency = AVALON_DEFAULT_FREQUENCY;
  369. if (!(*buf))
  370. return;
  371. colon = strchr(buf, ':');
  372. if (colon)
  373. *(colon++) = '\0';
  374. tmp = atoi(buf);
  375. switch (tmp) {
  376. case 115200:
  377. *baud = 115200;
  378. break;
  379. case 57600:
  380. *baud = 57600;
  381. break;
  382. case 38400:
  383. *baud = 38400;
  384. break;
  385. case 19200:
  386. *baud = 19200;
  387. break;
  388. default:
  389. quit(1, "Invalid avalon-options for baud (%s) "
  390. "must be 115200, 57600, 38400 or 19200", buf);
  391. }
  392. if (colon && *colon) {
  393. colon2 = strchr(colon, ':');
  394. if (colon2)
  395. *(colon2++) = '\0';
  396. if (*colon) {
  397. tmp = atoi(colon);
  398. if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
  399. *miner_count = tmp;
  400. } else {
  401. quit(1, "Invalid avalon-options for "
  402. "miner_count (%s) must be 1 ~ %d",
  403. colon, AVALON_DEFAULT_MINER_NUM);
  404. }
  405. }
  406. if (colon2 && *colon2) {
  407. colon3 = strchr(colon2, ':');
  408. if (colon3)
  409. *(colon3++) = '\0';
  410. tmp = atoi(colon2);
  411. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  412. *asic_count = tmp;
  413. else {
  414. quit(1, "Invalid avalon-options for "
  415. "asic_count (%s) must be 1 ~ %d",
  416. colon2, AVALON_DEFAULT_ASIC_NUM);
  417. }
  418. if (colon3 && *colon3) {
  419. colon4 = strchr(colon3, ':');
  420. if (colon4)
  421. *(colon4++) = '\0';
  422. tmp = atoi(colon3);
  423. if (tmp > 0 && tmp <= 0xff)
  424. *timeout = tmp;
  425. else {
  426. quit(1, "Invalid avalon-options for "
  427. "timeout (%s) must be 1 ~ %d",
  428. colon3, 0xff);
  429. }
  430. if (colon4 && *colon4) {
  431. tmp = atoi(colon4);
  432. switch (tmp) {
  433. case 256:
  434. case 270:
  435. case 282:
  436. case 300:
  437. *frequency = tmp;
  438. break;
  439. default:
  440. quit(1, "Invalid avalon-options for "
  441. "frequency must be 256/270/282/300");
  442. }
  443. }
  444. }
  445. }
  446. }
  447. }
  448. /* Non blocking clearing of anything in the buffer */
  449. static void avalon_clear_readbuf(int fd)
  450. {
  451. ssize_t ret;
  452. do {
  453. char buf[AVALON_FTDI_READSIZE];
  454. #ifndef WIN32
  455. struct timeval timeout;
  456. fd_set rd;
  457. timeout.tv_sec = timeout.tv_usec = 0;
  458. FD_ZERO(&rd);
  459. FD_SET((SOCKETTYPE)fd, &rd);
  460. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  461. if (ret > 0)
  462. #endif
  463. // Relies on serial timeout for Windows
  464. ret = read(fd, buf, AVALON_FTDI_READSIZE);
  465. } while (ret > 0);
  466. }
  467. static bool avalon_detect_one(const char *devpath)
  468. {
  469. struct avalon_info *info;
  470. struct avalon_result ar;
  471. int fd, ret;
  472. int baud, miner_count, asic_count, timeout, frequency = 0;
  473. struct cgpu_info *avalon;
  474. int this_option_offset = ++option_offset;
  475. get_options(this_option_offset, &baud, &miner_count, &asic_count,
  476. &timeout, &frequency);
  477. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s "
  478. "(baud=%d miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  479. devpath, baud, miner_count, asic_count, timeout, frequency);
  480. fd = avalon_open2(devpath, baud, true);
  481. if (unlikely(fd == -1)) {
  482. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  483. return false;
  484. }
  485. avalon_clear_readbuf(fd);
  486. /* We have a real Avalon! */
  487. avalon = calloc(1, sizeof(struct cgpu_info));
  488. avalon->drv = &avalon_drv;
  489. avalon->device_path = strdup(devpath);
  490. avalon->device_fd = fd;
  491. avalon->threads = AVALON_MINER_THREADS;
  492. add_cgpu(avalon);
  493. ret = avalon_reset(fd, &ar);
  494. if (ret) {
  495. ; /* FIXME: I think IT IS avalon and wait on reset;
  496. * avalon_close(fd);
  497. * return false; */
  498. }
  499. applog(LOG_INFO, "Avalon Detect: Found at %s, mark as %d",
  500. devpath, avalon->device_id);
  501. avalon->device_data = calloc(sizeof(struct avalon_info), 1);
  502. if (unlikely(!(avalon->device_data)))
  503. quit(1, "Failed to malloc avalon_info data");
  504. info = avalon->device_data;
  505. info->baud = baud;
  506. info->miner_count = miner_count;
  507. info->asic_count = asic_count;
  508. info->timeout = timeout;
  509. info->read_count = ((float)info->timeout * AVALON_HASH_TIME_FACTOR *
  510. AVALON_TIME_FACTOR) / (float)info->miner_count;
  511. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  512. info->temp_max = 0;
  513. /* This is for check the temp/fan every 3~4s */
  514. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  515. if (info->temp_history_count <= 0)
  516. info->temp_history_count = 1;
  517. info->temp_history_index = 0;
  518. info->temp_sum = 0;
  519. info->temp_old = 0;
  520. info->frequency = frequency;
  521. /* Set asic to idle mode after detect */
  522. avalon_idle(avalon);
  523. avalon->device_fd = -1;
  524. avalon_close(fd);
  525. return true;
  526. }
  527. static inline void avalon_detect()
  528. {
  529. serial_detect_byname(&avalon_drv, avalon_detect_one);
  530. }
  531. static void __avalon_init(struct cgpu_info *avalon)
  532. {
  533. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  534. }
  535. static void avalon_init(struct cgpu_info *avalon)
  536. {
  537. struct avalon_info *info = avalon->device_data;
  538. struct avalon_result ar;
  539. int fd, ret;
  540. avalon->device_fd = -1;
  541. fd = avalon_open(avalon->device_path, info->baud);
  542. if (unlikely(fd == -1)) {
  543. applog(LOG_ERR, "Avalon: Failed to open on %s",
  544. avalon->device_path);
  545. return;
  546. }
  547. ret = avalon_reset(fd, &ar);
  548. if (ret) {
  549. avalon_close(fd);
  550. return;
  551. }
  552. avalon->device_fd = fd;
  553. __avalon_init(avalon);
  554. }
  555. static bool avalon_prepare(struct thr_info *thr)
  556. {
  557. struct cgpu_info *avalon = thr->cgpu;
  558. struct avalon_info *info = avalon->device_data;
  559. struct timeval now;
  560. free(avalon->works);
  561. avalon->works = calloc(info->miner_count * sizeof(struct work *),
  562. AVALON_ARRAY_SIZE);
  563. if (!avalon->works)
  564. quit(1, "Failed to calloc avalon works in avalon_prepare");
  565. if (avalon->device_fd == -1)
  566. avalon_init(avalon);
  567. else
  568. __avalon_init(avalon);
  569. cgtime(&now);
  570. get_datestamp(avalon->init, &now);
  571. return true;
  572. }
  573. static void avalon_free_work(struct thr_info *thr)
  574. {
  575. struct cgpu_info *avalon;
  576. struct avalon_info *info;
  577. struct work **works;
  578. int i;
  579. avalon = thr->cgpu;
  580. avalon->queued = 0;
  581. if (unlikely(!avalon->works))
  582. return;
  583. works = avalon->works;
  584. info = avalon->device_data;
  585. for (i = 0; i < info->miner_count * 4; i++) {
  586. if (works[i]) {
  587. work_completed(avalon, works[i]);
  588. works[i] = NULL;
  589. }
  590. }
  591. }
  592. static void do_avalon_close(struct thr_info *thr)
  593. {
  594. struct avalon_result ar;
  595. struct cgpu_info *avalon = thr->cgpu;
  596. struct avalon_info *info = avalon->device_data;
  597. avalon_free_work(thr);
  598. nmsleep(1000);
  599. avalon_reset(avalon->device_fd, &ar);
  600. avalon_idle(avalon);
  601. avalon_close(avalon->device_fd);
  602. avalon->device_fd = -1;
  603. info->no_matching_work = 0;
  604. }
  605. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  606. {
  607. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  608. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  609. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  610. info->temp0 = ar->temp0;
  611. info->temp1 = ar->temp1;
  612. info->temp2 = ar->temp2;
  613. if (ar->temp0 & 0x80) {
  614. ar->temp0 &= 0x7f;
  615. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  616. }
  617. if (ar->temp1 & 0x80) {
  618. ar->temp1 &= 0x7f;
  619. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  620. }
  621. if (ar->temp2 & 0x80) {
  622. ar->temp2 &= 0x7f;
  623. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  624. }
  625. *temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
  626. if (info->temp0 > info->temp_max)
  627. info->temp_max = info->temp0;
  628. if (info->temp1 > info->temp_max)
  629. info->temp_max = info->temp1;
  630. if (info->temp2 > info->temp_max)
  631. info->temp_max = info->temp2;
  632. }
  633. static inline void adjust_fan(struct avalon_info *info)
  634. {
  635. int temp_new;
  636. temp_new = info->temp_sum / info->temp_history_count;
  637. if (temp_new < 35) {
  638. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  639. info->temp_old = temp_new;
  640. } else if (temp_new > 55) {
  641. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  642. info->temp_old = temp_new;
  643. } else if (abs(temp_new - info->temp_old) >= 2) {
  644. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM + (temp_new - 35) * 6.4;
  645. info->temp_old = temp_new;
  646. }
  647. }
  648. static void get_avalon_statline_before(char *buf, struct cgpu_info *avalon)
  649. {
  650. struct avalon_info *info = avalon->device_data;
  651. int lowfan = 10000;
  652. /* Find the lowest fan speed of the ASIC cooling fans. */
  653. if (info->fan1 >= 0 && info->fan1 < lowfan)
  654. lowfan = info->fan1;
  655. if (info->fan2 >= 0 && info->fan2 < lowfan)
  656. lowfan = info->fan2;
  657. tailsprintf(buf, "%2d/%3dC %04dR | ", info->temp0, info->temp2, lowfan);
  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. nmsleep(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%08x = 0x%08"PRIx64" hashes "
  799. "(%ld.%06lds)", nonce, (uint64_t)hash_count,
  800. elapsed.tv_sec, 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. nmsleep(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. applog(LOG_INFO,
  823. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  824. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  825. info->fan0, info->fan1, info->fan2,
  826. info->temp0, info->temp1, info->temp2, info->temp_max);
  827. info->temp_history_index++;
  828. info->temp_sum += avalon->temp;
  829. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  830. info->temp_history_index, info->temp_history_count, info->temp_old);
  831. if (info->temp_history_index == info->temp_history_count) {
  832. adjust_fan(info);
  833. info->temp_history_index = 0;
  834. info->temp_sum = 0;
  835. }
  836. }
  837. /* This hashmeter is just a utility counter based on returned shares */
  838. return hash_count;
  839. }
  840. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  841. {
  842. struct api_data *root = NULL;
  843. struct avalon_info *info = cgpu->device_data;
  844. int i;
  845. root = api_add_int(root, "baud", &(info->baud), false);
  846. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  847. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  848. root = api_add_int(root, "read_count", &(info->read_count), false);
  849. root = api_add_int(root, "timeout", &(info->timeout), false);
  850. root = api_add_int(root, "frequency", &(info->frequency), false);
  851. root = api_add_int(root, "fan1", &(info->fan0), false);
  852. root = api_add_int(root, "fan2", &(info->fan1), false);
  853. root = api_add_int(root, "fan3", &(info->fan2), false);
  854. root = api_add_int(root, "temp1", &(info->temp0), false);
  855. root = api_add_int(root, "temp2", &(info->temp1), false);
  856. root = api_add_int(root, "temp3", &(info->temp2), false);
  857. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  858. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  859. for (i = 0; i < info->miner_count; i++) {
  860. char mcw[24];
  861. sprintf(mcw, "match_work_count%d", i + 1);
  862. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  863. }
  864. return root;
  865. }
  866. static void avalon_shutdown(struct thr_info *thr)
  867. {
  868. do_avalon_close(thr);
  869. }
  870. struct device_drv avalon_drv = {
  871. .dname = "avalon",
  872. .name = "AVA",
  873. .drv_detect = avalon_detect,
  874. .thread_prepare = avalon_prepare,
  875. .minerloop = hash_queued_work,
  876. .queue_full = avalon_fill,
  877. .scanwork = avalon_scanhash,
  878. .get_api_stats = avalon_api_stats,
  879. .get_statline_before = get_avalon_statline_before,
  880. .reinit_device = avalon_init,
  881. .thread_shutdown = avalon_shutdown,
  882. };