driver-avalon.c 25 KB

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