driver-avalon.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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 inline int avalon_gets(int fd, uint8_t *buf, struct thr_info *thr,
  177. struct timeval *tv_finish)
  178. {
  179. int read_amount = AVALON_READ_SIZE;
  180. bool first = true;
  181. ssize_t ret = 0;
  182. while (true) {
  183. struct timeval timeout;
  184. fd_set rd;
  185. if (unlikely(thr->work_restart)) {
  186. applog(LOG_DEBUG, "Avalon: Work restart");
  187. return AVA_GETS_RESTART;
  188. }
  189. timeout.tv_sec = 0;
  190. timeout.tv_usec = 100000;
  191. FD_ZERO(&rd);
  192. FD_SET((SOCKETTYPE)fd, &rd);
  193. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  194. if (unlikely(ret < 0)) {
  195. applog(LOG_ERR, "Avalon: Error %d on select in avalon_gets", errno);
  196. return AVA_GETS_ERROR;
  197. }
  198. if (ret) {
  199. ret = read(fd, buf, read_amount);
  200. if (unlikely(ret < 0)) {
  201. applog(LOG_ERR, "Avalon: Error %d on read in avalon_gets", errno);
  202. return AVA_GETS_ERROR;
  203. }
  204. if (likely(first)) {
  205. cgtime(tv_finish);
  206. first = false;
  207. }
  208. if (likely(ret >= read_amount))
  209. return AVA_GETS_OK;
  210. buf += ret;
  211. read_amount -= ret;
  212. continue;
  213. }
  214. if (unlikely(thr->work_restart)) {
  215. applog(LOG_DEBUG, "Avalon: Work restart");
  216. return AVA_GETS_RESTART;
  217. }
  218. return AVA_GETS_TIMEOUT;
  219. }
  220. }
  221. static int avalon_get_result(int fd, struct avalon_result *ar,
  222. struct thr_info *thr, struct timeval *tv_finish)
  223. {
  224. uint8_t result[AVALON_READ_SIZE];
  225. int ret;
  226. memset(result, 0, AVALON_READ_SIZE);
  227. ret = avalon_gets(fd, result, thr, tv_finish);
  228. if (ret == AVA_GETS_OK) {
  229. if (opt_debug) {
  230. applog(LOG_DEBUG, "Avalon: get:");
  231. hexdump((uint8_t *)result, AVALON_READ_SIZE);
  232. }
  233. memcpy((uint8_t *)ar, result, AVALON_READ_SIZE);
  234. }
  235. return ret;
  236. }
  237. static bool avalon_decode_nonce(struct thr_info *thr, struct avalon_result *ar,
  238. uint32_t *nonce)
  239. {
  240. struct cgpu_info *avalon;
  241. struct avalon_info *info;
  242. struct work *work;
  243. avalon = thr->cgpu;
  244. if (unlikely(!avalon->works))
  245. return false;
  246. work = find_queued_work_bymidstate(avalon, (char *)ar->midstate, 32,
  247. (char *)ar->data, 64, 12);
  248. if (!work)
  249. return false;
  250. info = avalon->device_data;
  251. info->matching_work[work->subid]++;
  252. *nonce = htole32(ar->nonce);
  253. submit_nonce(thr, work, *nonce);
  254. return true;
  255. }
  256. static int avalon_write(int fd, char *buf, ssize_t len)
  257. {
  258. ssize_t wrote = 0;
  259. while (len > 0) {
  260. struct timeval timeout;
  261. ssize_t ret;
  262. fd_set wd;
  263. timeout.tv_sec = 0;
  264. timeout.tv_usec = 1000;
  265. FD_ZERO(&wd);
  266. FD_SET((SOCKETTYPE)fd, &wd);
  267. ret = select(fd + 1, NULL, &wd, NULL, &timeout);
  268. if (unlikely(ret < 1)) {
  269. applog(LOG_WARNING, "Select error on avalon_write");
  270. return AVA_SEND_ERROR;
  271. }
  272. ret = write(fd, buf + wrote, len);
  273. if (unlikely(ret < 1)) {
  274. applog(LOG_WARNING, "Write error on avalon_write");
  275. return AVA_SEND_ERROR;
  276. }
  277. wrote += ret;
  278. len -= ret;
  279. }
  280. return 0;
  281. }
  282. static int avalon_read(int fd, char *buf, ssize_t len)
  283. {
  284. ssize_t aread = 0;
  285. while (len > 0) {
  286. struct timeval timeout;
  287. ssize_t ret;
  288. fd_set rd;
  289. timeout.tv_sec = 0;
  290. timeout.tv_usec = 1000;
  291. FD_ZERO(&rd);
  292. FD_SET((SOCKETTYPE)fd, &rd);
  293. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  294. if (unlikely(ret < 1)) {
  295. applog(LOG_WARNING, "Select error on avalon_read");
  296. return AVA_GETS_ERROR;
  297. }
  298. ret = read(fd, buf + aread, len);
  299. if (unlikely(ret < 1)) {
  300. applog(LOG_WARNING, "Read error on avalon_read");
  301. return AVA_GETS_ERROR;
  302. }
  303. aread += ret;
  304. len -= ret;
  305. }
  306. return 0;
  307. }
  308. /* Non blocking clearing of anything in the buffer */
  309. static void avalon_clear_readbuf(int fd)
  310. {
  311. ssize_t ret;
  312. do {
  313. struct timeval timeout;
  314. char buf[AVALON_FTDI_READSIZE];
  315. fd_set rd;
  316. timeout.tv_sec = timeout.tv_usec = 0;
  317. FD_ZERO(&rd);
  318. FD_SET((SOCKETTYPE)fd, &rd);
  319. ret = select(fd + 1, &rd, NULL, NULL, &timeout);
  320. if (ret > 0)
  321. ret = read(fd, buf, AVALON_FTDI_READSIZE);
  322. } while (ret > 0);
  323. }
  324. static void avalon_idle(struct cgpu_info *avalon, int fd)
  325. {
  326. struct avalon_info *info = avalon->device_data;
  327. int i;
  328. for (i = 0; i < info->miner_count; i++) {
  329. struct avalon_task at;
  330. int ret;
  331. if (unlikely(avalon_buffer_full(fd))) {
  332. applog(LOG_WARNING, "Avalon buffer full in avalon_idle");
  333. break;
  334. }
  335. avalon_init_task(&at, 0, 0, info->fan_pwm,
  336. info->timeout, info->asic_count,
  337. info->miner_count, 1, 1, info->frequency);
  338. ret = avalon_write(fd, (char *)&at, AVALON_WRITE_SIZE);
  339. if (unlikely(ret == AVA_SEND_ERROR))
  340. break;
  341. }
  342. applog(LOG_ERR, "Avalon: Going to idle mode");
  343. sleep(2);
  344. avalon_clear_readbuf(fd);
  345. applog(LOG_ERR, "Avalon: Idle");
  346. }
  347. static int avalon_reset(struct cgpu_info *avalon, int fd)
  348. {
  349. struct avalon_result ar;
  350. uint8_t *buf;
  351. int ret, i = 0;
  352. struct timespec p;
  353. /* Reset once, then send command to go idle */
  354. ret = avalon_write(fd, "ad", 2);
  355. if (unlikely(ret == AVA_SEND_ERROR))
  356. return -1;
  357. p.tv_sec = 0;
  358. p.tv_nsec = AVALON_RESET_PITCH;
  359. nanosleep(&p, NULL);
  360. avalon_clear_readbuf(fd);
  361. avalon_idle(avalon, fd);
  362. /* Reset again, then check result */
  363. ret = avalon_write(fd, "ad", 2);
  364. if (unlikely(ret == AVA_SEND_ERROR))
  365. return -1;
  366. ret = avalon_read(fd, (char *)&ar, AVALON_READ_SIZE);
  367. if (unlikely(ret == AVA_GETS_ERROR))
  368. return -1;
  369. nanosleep(&p, NULL);
  370. buf = (uint8_t *)&ar;
  371. if (buf[0] == 0xAA && buf[1] == 0x55 &&
  372. buf[2] == 0xAA && buf[3] == 0x55) {
  373. for (i = 4; i < 11; i++)
  374. if (buf[i] != 0)
  375. break;
  376. }
  377. if (i != 11) {
  378. applog(LOG_ERR, "Avalon: Reset failed! not an Avalon?"
  379. " (%d: %02x %02x %02x %02x)",
  380. i, buf[0], buf[1], buf[2], buf[3]);
  381. /* FIXME: return 1; */
  382. } else
  383. applog(LOG_WARNING, "Avalon: Reset succeeded");
  384. return 0;
  385. }
  386. static void get_options(int this_option_offset, int *baud, int *miner_count,
  387. int *asic_count, int *timeout, int *frequency)
  388. {
  389. char err_buf[BUFSIZ+1];
  390. char buf[BUFSIZ+1];
  391. char *ptr, *comma, *colon, *colon2, *colon3, *colon4;
  392. size_t max;
  393. int i, tmp;
  394. if (opt_avalon_options == NULL)
  395. buf[0] = '\0';
  396. else {
  397. ptr = opt_avalon_options;
  398. for (i = 0; i < this_option_offset; i++) {
  399. comma = strchr(ptr, ',');
  400. if (comma == NULL)
  401. break;
  402. ptr = comma + 1;
  403. }
  404. comma = strchr(ptr, ',');
  405. if (comma == NULL)
  406. max = strlen(ptr);
  407. else
  408. max = comma - ptr;
  409. if (max > BUFSIZ)
  410. max = BUFSIZ;
  411. strncpy(buf, ptr, max);
  412. buf[max] = '\0';
  413. }
  414. *baud = AVALON_IO_SPEED;
  415. *miner_count = AVALON_DEFAULT_MINER_NUM - 8;
  416. *asic_count = AVALON_DEFAULT_ASIC_NUM;
  417. *timeout = AVALON_DEFAULT_TIMEOUT;
  418. *frequency = AVALON_DEFAULT_FREQUENCY;
  419. if (!(*buf))
  420. return;
  421. colon = strchr(buf, ':');
  422. if (colon)
  423. *(colon++) = '\0';
  424. tmp = atoi(buf);
  425. switch (tmp) {
  426. case 115200:
  427. *baud = 115200;
  428. break;
  429. case 57600:
  430. *baud = 57600;
  431. break;
  432. case 38400:
  433. *baud = 38400;
  434. break;
  435. case 19200:
  436. *baud = 19200;
  437. break;
  438. default:
  439. sprintf(err_buf,
  440. "Invalid avalon-options for baud (%s) "
  441. "must be 115200, 57600, 38400 or 19200", buf);
  442. quit(1, err_buf);
  443. }
  444. if (colon && *colon) {
  445. colon2 = strchr(colon, ':');
  446. if (colon2)
  447. *(colon2++) = '\0';
  448. if (*colon) {
  449. tmp = atoi(colon);
  450. if (tmp > 0 && tmp <= AVALON_DEFAULT_MINER_NUM) {
  451. *miner_count = tmp;
  452. } else {
  453. sprintf(err_buf,
  454. "Invalid avalon-options for "
  455. "miner_count (%s) must be 1 ~ %d",
  456. colon, AVALON_DEFAULT_MINER_NUM);
  457. quit(1, err_buf);
  458. }
  459. }
  460. if (colon2 && *colon2) {
  461. colon3 = strchr(colon2, ':');
  462. if (colon3)
  463. *(colon3++) = '\0';
  464. tmp = atoi(colon2);
  465. if (tmp > 0 && tmp <= AVALON_DEFAULT_ASIC_NUM)
  466. *asic_count = tmp;
  467. else {
  468. sprintf(err_buf,
  469. "Invalid avalon-options for "
  470. "asic_count (%s) must be 1 ~ %d",
  471. colon2, AVALON_DEFAULT_ASIC_NUM);
  472. quit(1, err_buf);
  473. }
  474. if (colon3 && *colon3) {
  475. colon4 = strchr(colon3, ':');
  476. if (colon4)
  477. *(colon4++) = '\0';
  478. tmp = atoi(colon3);
  479. if (tmp > 0 && tmp <= 0xff)
  480. *timeout = tmp;
  481. else {
  482. sprintf(err_buf,
  483. "Invalid avalon-options for "
  484. "timeout (%s) must be 1 ~ %d",
  485. colon3, 0xff);
  486. quit(1, err_buf);
  487. }
  488. if (colon4 && *colon4) {
  489. tmp = atoi(colon4);
  490. switch (tmp) {
  491. case 256:
  492. case 270:
  493. case 282:
  494. case 300:
  495. *frequency = tmp;
  496. break;
  497. default:
  498. sprintf(err_buf,
  499. "Invalid avalon-options for "
  500. "frequency must be 256/270/282/300");
  501. quit(1, err_buf);
  502. }
  503. }
  504. }
  505. }
  506. }
  507. }
  508. static bool avalon_detect_one(const char *devpath)
  509. {
  510. struct avalon_info *info;
  511. int fd, ret;
  512. int baud, miner_count, asic_count, timeout, frequency = 0;
  513. struct cgpu_info *avalon;
  514. int this_option_offset = ++option_offset;
  515. get_options(this_option_offset, &baud, &miner_count, &asic_count,
  516. &timeout, &frequency);
  517. applog(LOG_DEBUG, "Avalon Detect: Attempting to open %s "
  518. "(baud=%d miner_count=%d asic_count=%d timeout=%d frequency=%d)",
  519. devpath, baud, miner_count, asic_count, timeout, frequency);
  520. fd = avalon_open2(devpath, baud, true);
  521. if (unlikely(fd == -1)) {
  522. applog(LOG_ERR, "Avalon Detect: Failed to open %s", devpath);
  523. return false;
  524. }
  525. avalon_clear_readbuf(fd);
  526. /* We have a real Avalon! */
  527. avalon = calloc(1, sizeof(struct cgpu_info));
  528. avalon->drv = &avalon_drv;
  529. avalon->device_path = strdup(devpath);
  530. avalon->device_fd = fd;
  531. avalon->threads = AVALON_MINER_THREADS;
  532. add_cgpu(avalon);
  533. avalon_infos = realloc(avalon_infos,
  534. sizeof(struct avalon_info *) *
  535. (total_devices + 1));
  536. applog(LOG_INFO, "Avalon Detect: Found at %s, mark as %d",
  537. devpath, avalon->device_id);
  538. avalon_infos[avalon->device_id] = calloc(sizeof(struct avalon_info), 1);
  539. if (unlikely(!(avalon_infos[avalon->device_id])))
  540. quit(1, "Failed to calloc avalon_infos");
  541. avalon->device_data = avalon_infos[avalon->device_id];
  542. info = avalon->device_data;
  543. info->baud = baud;
  544. info->miner_count = miner_count;
  545. info->asic_count = asic_count;
  546. info->timeout = timeout;
  547. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  548. info->temp_max = 0;
  549. /* This is for check the temp/fan every 3~4s */
  550. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  551. if (info->temp_history_count <= 0)
  552. info->temp_history_count = 1;
  553. info->temp_history_index = 0;
  554. info->temp_sum = 0;
  555. info->temp_old = 0;
  556. info->frequency = frequency;
  557. avalon->device_fd = -1;
  558. ret = avalon_reset(avalon, fd);
  559. if (ret) {
  560. ; /* FIXME: I think IT IS avalon and wait on reset;
  561. * avalon_close(fd);
  562. * return false; */
  563. }
  564. avalon_close(fd);
  565. return true;
  566. }
  567. static inline void avalon_detect()
  568. {
  569. serial_detect(&avalon_drv, avalon_detect_one);
  570. }
  571. static void __avalon_init(struct cgpu_info *avalon)
  572. {
  573. applog(LOG_INFO, "Avalon: Opened on %s", avalon->device_path);
  574. }
  575. static void avalon_init(struct cgpu_info *avalon)
  576. {
  577. struct avalon_info *info = avalon->device_data;
  578. int fd, ret;
  579. avalon->device_fd = -1;
  580. fd = avalon_open(avalon->device_path, info->baud);
  581. if (unlikely(fd == -1)) {
  582. applog(LOG_ERR, "Avalon: Failed to open on %s",
  583. avalon->device_path);
  584. return;
  585. }
  586. ret = avalon_reset(avalon, fd);
  587. if (ret) {
  588. avalon_close(fd);
  589. return;
  590. }
  591. avalon->device_fd = fd;
  592. __avalon_init(avalon);
  593. }
  594. static bool avalon_prepare(struct thr_info *thr)
  595. {
  596. struct cgpu_info *avalon = thr->cgpu;
  597. struct avalon_info *info = avalon->device_data;
  598. struct timeval now;
  599. free(avalon->works);
  600. avalon->works = calloc(info->miner_count * sizeof(struct work *),
  601. AVALON_ARRAY_SIZE);
  602. if (!avalon->works)
  603. quit(1, "Failed to calloc avalon works in avalon_prepare");
  604. if (avalon->device_fd == -1)
  605. avalon_init(avalon);
  606. else
  607. __avalon_init(avalon);
  608. cgtime(&now);
  609. get_datestamp(avalon->init, &now);
  610. return true;
  611. }
  612. static void avalon_free_work(struct thr_info *thr)
  613. {
  614. struct cgpu_info *avalon;
  615. struct avalon_info *info;
  616. struct work **works;
  617. int i;
  618. avalon = thr->cgpu;
  619. avalon->queued = 0;
  620. if (unlikely(!avalon->works))
  621. return;
  622. works = avalon->works;
  623. info = avalon->device_data;
  624. for (i = 0; i < info->miner_count * 4; i++) {
  625. if (works[i]) {
  626. work_completed(avalon, works[i]);
  627. works[i] = NULL;
  628. }
  629. }
  630. }
  631. static void do_avalon_close(struct thr_info *thr)
  632. {
  633. struct cgpu_info *avalon = thr->cgpu;
  634. struct avalon_info *info = avalon->device_data;
  635. avalon_free_work(thr);
  636. sleep(1);
  637. avalon_reset(avalon, avalon->device_fd);
  638. avalon_close(avalon->device_fd);
  639. avalon->device_fd = -1;
  640. info->no_matching_work = 0;
  641. }
  642. static inline void record_temp_fan(struct avalon_info *info, struct avalon_result *ar, float *temp_avg)
  643. {
  644. info->fan0 = ar->fan0 * AVALON_FAN_FACTOR;
  645. info->fan1 = ar->fan1 * AVALON_FAN_FACTOR;
  646. info->fan2 = ar->fan2 * AVALON_FAN_FACTOR;
  647. info->temp0 = ar->temp0;
  648. info->temp1 = ar->temp1;
  649. info->temp2 = ar->temp2;
  650. if (ar->temp0 & 0x80) {
  651. ar->temp0 &= 0x7f;
  652. info->temp0 = 0 - ((~ar->temp0 & 0x7f) + 1);
  653. }
  654. if (ar->temp1 & 0x80) {
  655. ar->temp1 &= 0x7f;
  656. info->temp1 = 0 - ((~ar->temp1 & 0x7f) + 1);
  657. }
  658. if (ar->temp2 & 0x80) {
  659. ar->temp2 &= 0x7f;
  660. info->temp2 = 0 - ((~ar->temp2 & 0x7f) + 1);
  661. }
  662. *temp_avg = info->temp2 > info->temp1 ? info->temp2 : info->temp1;
  663. if (info->temp0 > info->temp_max)
  664. info->temp_max = info->temp0;
  665. if (info->temp1 > info->temp_max)
  666. info->temp_max = info->temp1;
  667. if (info->temp2 > info->temp_max)
  668. info->temp_max = info->temp2;
  669. }
  670. static inline void adjust_fan(struct avalon_info *info)
  671. {
  672. int temp_new;
  673. temp_new = info->temp_sum / info->temp_history_count;
  674. if (temp_new < 35) {
  675. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM;
  676. info->temp_old = temp_new;
  677. } else if (temp_new > 55) {
  678. info->fan_pwm = AVALON_DEFAULT_FAN_MAX_PWM;
  679. info->temp_old = temp_new;
  680. } else if (abs(temp_new - info->temp_old) >= 2) {
  681. info->fan_pwm = AVALON_DEFAULT_FAN_MIN_PWM + (temp_new - 35) * 6.4;
  682. info->temp_old = temp_new;
  683. }
  684. }
  685. /* We use a replacement algorithm to only remove references to work done from
  686. * the buffer when we need the extra space for new work. */
  687. static bool avalon_fill(struct cgpu_info *avalon)
  688. {
  689. int subid, slot, mc = avalon_infos[avalon->device_id]->miner_count;
  690. struct work *work;
  691. if (avalon->queued >= mc)
  692. return true;
  693. work = get_queued(avalon);
  694. if (unlikely(!work))
  695. return false;
  696. subid = avalon->queued++;
  697. work->subid = subid;
  698. slot = avalon->work_array * mc + subid;
  699. if (likely(avalon->works[slot]))
  700. work_completed(avalon, avalon->works[slot]);
  701. avalon->works[slot] = work;
  702. if (avalon->queued >= mc)
  703. return true;
  704. return false;
  705. }
  706. static void avalon_rotate_array(struct cgpu_info *avalon)
  707. {
  708. avalon->queued = 0;
  709. if (++avalon->work_array >= AVALON_ARRAY_SIZE)
  710. avalon->work_array = 0;
  711. }
  712. static int64_t avalon_scanhash(struct thr_info *thr)
  713. {
  714. struct cgpu_info *avalon;
  715. struct work **works;
  716. int fd, ret = AVA_GETS_OK, full;
  717. struct avalon_info *info;
  718. struct avalon_task at;
  719. struct avalon_result ar;
  720. int i;
  721. int avalon_get_work_count;
  722. int start_count, end_count;
  723. struct timeval tv_start, tv_finish, elapsed;
  724. uint32_t nonce;
  725. int64_t hash_count;
  726. static int first_try = 0;
  727. int result_wrong;
  728. avalon = thr->cgpu;
  729. works = avalon->works;
  730. info = avalon->device_data;
  731. avalon_get_work_count = info->miner_count;
  732. if (unlikely(avalon->device_fd == -1)) {
  733. if (!avalon_prepare(thr)) {
  734. applog(LOG_ERR, "AVA%i: Comms error(open)",
  735. avalon->device_id);
  736. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  737. /* fail the device if the reopen attempt fails */
  738. return -1;
  739. }
  740. }
  741. fd = avalon->device_fd;
  742. #ifndef WIN32
  743. tcflush(fd, TCOFLUSH);
  744. #endif
  745. start_count = avalon->work_array * avalon_get_work_count;
  746. end_count = start_count + avalon_get_work_count;
  747. i = start_count;
  748. while (true) {
  749. avalon_init_task(&at, 0, 0, info->fan_pwm,
  750. info->timeout, info->asic_count,
  751. info->miner_count, 1, 0, info->frequency);
  752. avalon_create_task(&at, works[i]);
  753. ret = avalon_send_task(fd, &at, avalon);
  754. if (unlikely(ret == AVA_SEND_ERROR ||
  755. (ret == AVA_SEND_BUFFER_EMPTY &&
  756. (i + 1 == end_count) &&
  757. first_try))) {
  758. do_avalon_close(thr);
  759. applog(LOG_ERR, "AVA%i: Comms error(buffer)",
  760. avalon->device_id);
  761. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  762. first_try = 0;
  763. sleep(1);
  764. avalon_init(avalon);
  765. return 0; /* This should never happen */
  766. }
  767. if (ret == AVA_SEND_BUFFER_EMPTY && (i + 1 == end_count)) {
  768. first_try = 1;
  769. avalon_rotate_array(avalon);
  770. return 0xffffffff;
  771. }
  772. works[i]->blk.nonce = 0xffffffff;
  773. if (ret == AVA_SEND_BUFFER_FULL)
  774. break;
  775. i++;
  776. }
  777. if (unlikely(first_try))
  778. first_try = 0;
  779. elapsed.tv_sec = elapsed.tv_usec = 0;
  780. cgtime(&tv_start);
  781. result_wrong = 0;
  782. hash_count = 0;
  783. while (true) {
  784. full = avalon_buffer_full(fd);
  785. applog(LOG_DEBUG, "Avalon: Buffer full: %s",
  786. ((full == AVA_BUFFER_FULL) ? "Yes" : "No"));
  787. if (unlikely(full == AVA_BUFFER_EMPTY))
  788. break;
  789. ret = avalon_get_result(fd, &ar, thr, &tv_finish);
  790. if (unlikely(ret == AVA_GETS_ERROR)) {
  791. do_avalon_close(thr);
  792. applog(LOG_ERR,
  793. "AVA%i: Comms error(read)", avalon->device_id);
  794. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  795. return 0;
  796. }
  797. if (unlikely(ret == AVA_GETS_RESTART))
  798. break;
  799. if (unlikely(ret == AVA_GETS_TIMEOUT)) {
  800. timersub(&tv_finish, &tv_start, &elapsed);
  801. applog(LOG_DEBUG, "Avalon: no nonce in (%ld.%06lds)",
  802. elapsed.tv_sec, elapsed.tv_usec);
  803. continue;
  804. }
  805. if (!avalon_decode_nonce(thr, &ar, &nonce)) {
  806. info->no_matching_work++;
  807. result_wrong++;
  808. if (unlikely(result_wrong >= avalon_get_work_count))
  809. break;
  810. if (opt_debug) {
  811. timersub(&tv_finish, &tv_start, &elapsed);
  812. applog(LOG_DEBUG,"Avalon: no matching work: %d"
  813. " (%ld.%06lds)", info->no_matching_work,
  814. elapsed.tv_sec, elapsed.tv_usec);
  815. }
  816. continue;
  817. }
  818. hash_count += 0xffffffff;
  819. if (opt_debug) {
  820. timersub(&tv_finish, &tv_start, &elapsed);
  821. applog(LOG_DEBUG,
  822. "Avalon: nonce = 0x%08x = 0x%08llx hashes "
  823. "(%ld.%06lds)", nonce, (unsigned long long)hash_count,
  824. elapsed.tv_sec, elapsed.tv_usec);
  825. }
  826. }
  827. if (hash_count && avalon->results < AVALON_ARRAY_SIZE)
  828. avalon->results++;
  829. if (unlikely((result_wrong >= avalon_get_work_count) ||
  830. (!hash_count && ret != AVA_GETS_RESTART && --avalon->results < 0))) {
  831. /* Look for all invalid results, or consecutive failure
  832. * to generate any results suggesting the FPGA
  833. * controller has screwed up. */
  834. do_avalon_close(thr);
  835. applog(LOG_ERR,
  836. "AVA%i: FPGA controller messed up, %d wrong results",
  837. avalon->device_id, result_wrong);
  838. dev_error(avalon, REASON_DEV_COMMS_ERROR);
  839. sleep(1);
  840. avalon_init(avalon);
  841. return 0;
  842. }
  843. avalon_rotate_array(avalon);
  844. if (hash_count) {
  845. record_temp_fan(info, &ar, &(avalon->temp));
  846. applog(LOG_INFO,
  847. "Avalon: Fan1: %d/m, Fan2: %d/m, Fan3: %d/m\t"
  848. "Temp1: %dC, Temp2: %dC, Temp3: %dC, TempMAX: %dC",
  849. info->fan0, info->fan1, info->fan2,
  850. info->temp0, info->temp1, info->temp2, info->temp_max);
  851. info->temp_history_index++;
  852. info->temp_sum += avalon->temp;
  853. applog(LOG_DEBUG, "Avalon: temp_index: %d, temp_count: %d, temp_old: %d",
  854. info->temp_history_index, info->temp_history_count, info->temp_old);
  855. if (info->temp_history_index == info->temp_history_count) {
  856. adjust_fan(info);
  857. info->temp_history_index = 0;
  858. info->temp_sum = 0;
  859. }
  860. }
  861. /* This hashmeter is just a utility counter based on returned shares */
  862. return hash_count;
  863. }
  864. static struct api_data *avalon_api_stats(struct cgpu_info *cgpu)
  865. {
  866. struct api_data *root = NULL;
  867. struct avalon_info *info = cgpu->device_data;
  868. int i;
  869. root = api_add_int(root, "baud", &(info->baud), false);
  870. root = api_add_int(root, "miner_count", &(info->miner_count),false);
  871. root = api_add_int(root, "asic_count", &(info->asic_count), false);
  872. root = api_add_int(root, "timeout", &(info->timeout), false);
  873. root = api_add_int(root, "frequency", &(info->frequency), false);
  874. root = api_add_int(root, "fan1", &(info->fan0), false);
  875. root = api_add_int(root, "fan2", &(info->fan1), false);
  876. root = api_add_int(root, "fan3", &(info->fan2), false);
  877. root = api_add_int(root, "temp1", &(info->temp0), false);
  878. root = api_add_int(root, "temp2", &(info->temp1), false);
  879. root = api_add_int(root, "temp3", &(info->temp2), false);
  880. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  881. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  882. for (i = 0; i < info->miner_count; i++) {
  883. char mcw[24];
  884. sprintf(mcw, "match_work_count%d", i + 1);
  885. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  886. }
  887. return root;
  888. }
  889. static void avalon_shutdown(struct thr_info *thr)
  890. {
  891. do_avalon_close(thr);
  892. }
  893. struct device_drv avalon_drv = {
  894. .drv_id = DRIVER_AVALON,
  895. .dname = "avalon",
  896. .name = "AVA",
  897. .drv_detect = avalon_detect,
  898. .thread_prepare = avalon_prepare,
  899. .hash_work = hash_queued_work,
  900. .queue_full = avalon_fill,
  901. .scanwork = avalon_scanhash,
  902. .get_api_stats = avalon_api_stats,
  903. .reinit_device = avalon_init,
  904. .thread_shutdown = avalon_shutdown,
  905. };