driver-avalon.c 25 KB

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